1 /* 2 * Copyright (C) 2017 Netronome Systems, Inc. 3 * 4 * This software is dual licensed under the GNU General License Version 2, 5 * June 1991 as shown in the file COPYING in the top-level directory of this 6 * source tree or the BSD 2-Clause License provided below. You have the 7 * option to license this software under the complete terms of either license. 8 * 9 * The BSD 2-Clause License: 10 * 11 * Redistribution and use in source and binary forms, with or 12 * without modification, are permitted provided that the following 13 * conditions are met: 14 * 15 * 1. Redistributions of source code must retain the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer. 18 * 19 * 2. Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials 22 * provided with the distribution. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31 * SOFTWARE. 32 */ 33 34 #include <linux/bitfield.h> 35 #include <net/pkt_cls.h> 36 37 #include "cmsg.h" 38 #include "main.h" 39 40 static void 41 nfp_flower_compile_meta_tci(struct nfp_flower_meta_tci *frame, 42 struct tc_cls_flower_offload *flow, u8 key_type, 43 bool mask_version) 44 { 45 struct fl_flow_key *target = mask_version ? flow->mask : flow->key; 46 struct flow_dissector_key_vlan *flow_vlan; 47 u16 tmp_tci; 48 49 memset(frame, 0, sizeof(struct nfp_flower_meta_tci)); 50 /* Populate the metadata frame. */ 51 frame->nfp_flow_key_layer = key_type; 52 frame->mask_id = ~0; 53 54 if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_VLAN)) { 55 flow_vlan = skb_flow_dissector_target(flow->dissector, 56 FLOW_DISSECTOR_KEY_VLAN, 57 target); 58 /* Populate the tci field. */ 59 if (flow_vlan->vlan_id) { 60 tmp_tci = FIELD_PREP(NFP_FLOWER_MASK_VLAN_PRIO, 61 flow_vlan->vlan_priority) | 62 FIELD_PREP(NFP_FLOWER_MASK_VLAN_VID, 63 flow_vlan->vlan_id) | 64 NFP_FLOWER_MASK_VLAN_CFI; 65 frame->tci = cpu_to_be16(tmp_tci); 66 } 67 } 68 } 69 70 static void 71 nfp_flower_compile_ext_meta(struct nfp_flower_ext_meta *frame, u32 key_ext) 72 { 73 frame->nfp_flow_key_layer2 = cpu_to_be32(key_ext); 74 } 75 76 static int 77 nfp_flower_compile_port(struct nfp_flower_in_port *frame, u32 cmsg_port, 78 bool mask_version, enum nfp_flower_tun_type tun_type) 79 { 80 if (mask_version) { 81 frame->in_port = cpu_to_be32(~0); 82 return 0; 83 } 84 85 if (tun_type) 86 frame->in_port = cpu_to_be32(NFP_FL_PORT_TYPE_TUN | tun_type); 87 else 88 frame->in_port = cpu_to_be32(cmsg_port); 89 90 return 0; 91 } 92 93 static void 94 nfp_flower_compile_mac(struct nfp_flower_mac_mpls *frame, 95 struct tc_cls_flower_offload *flow, 96 bool mask_version) 97 { 98 struct fl_flow_key *target = mask_version ? flow->mask : flow->key; 99 struct flow_dissector_key_eth_addrs *addr; 100 101 memset(frame, 0, sizeof(struct nfp_flower_mac_mpls)); 102 103 if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) { 104 addr = skb_flow_dissector_target(flow->dissector, 105 FLOW_DISSECTOR_KEY_ETH_ADDRS, 106 target); 107 /* Populate mac frame. */ 108 ether_addr_copy(frame->mac_dst, &addr->dst[0]); 109 ether_addr_copy(frame->mac_src, &addr->src[0]); 110 } 111 112 if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_MPLS)) { 113 struct flow_dissector_key_mpls *mpls; 114 u32 t_mpls; 115 116 mpls = skb_flow_dissector_target(flow->dissector, 117 FLOW_DISSECTOR_KEY_MPLS, 118 target); 119 120 t_mpls = FIELD_PREP(NFP_FLOWER_MASK_MPLS_LB, mpls->mpls_label) | 121 FIELD_PREP(NFP_FLOWER_MASK_MPLS_TC, mpls->mpls_tc) | 122 FIELD_PREP(NFP_FLOWER_MASK_MPLS_BOS, mpls->mpls_bos) | 123 NFP_FLOWER_MASK_MPLS_Q; 124 125 frame->mpls_lse = cpu_to_be32(t_mpls); 126 } 127 } 128 129 static void 130 nfp_flower_compile_tport(struct nfp_flower_tp_ports *frame, 131 struct tc_cls_flower_offload *flow, 132 bool mask_version) 133 { 134 struct fl_flow_key *target = mask_version ? flow->mask : flow->key; 135 struct flow_dissector_key_ports *tp; 136 137 memset(frame, 0, sizeof(struct nfp_flower_tp_ports)); 138 139 if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_PORTS)) { 140 tp = skb_flow_dissector_target(flow->dissector, 141 FLOW_DISSECTOR_KEY_PORTS, 142 target); 143 frame->port_src = tp->src; 144 frame->port_dst = tp->dst; 145 } 146 } 147 148 static void 149 nfp_flower_compile_ipv4(struct nfp_flower_ipv4 *frame, 150 struct tc_cls_flower_offload *flow, 151 bool mask_version) 152 { 153 struct fl_flow_key *target = mask_version ? flow->mask : flow->key; 154 struct flow_dissector_key_ipv4_addrs *addr; 155 struct flow_dissector_key_basic *basic; 156 157 memset(frame, 0, sizeof(struct nfp_flower_ipv4)); 158 159 if (dissector_uses_key(flow->dissector, 160 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) { 161 addr = skb_flow_dissector_target(flow->dissector, 162 FLOW_DISSECTOR_KEY_IPV4_ADDRS, 163 target); 164 frame->ipv4_src = addr->src; 165 frame->ipv4_dst = addr->dst; 166 } 167 168 if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_BASIC)) { 169 basic = skb_flow_dissector_target(flow->dissector, 170 FLOW_DISSECTOR_KEY_BASIC, 171 target); 172 frame->proto = basic->ip_proto; 173 } 174 175 if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_IP)) { 176 struct flow_dissector_key_ip *flow_ip; 177 178 flow_ip = skb_flow_dissector_target(flow->dissector, 179 FLOW_DISSECTOR_KEY_IP, 180 target); 181 frame->tos = flow_ip->tos; 182 frame->ttl = flow_ip->ttl; 183 } 184 } 185 186 static void 187 nfp_flower_compile_ipv6(struct nfp_flower_ipv6 *frame, 188 struct tc_cls_flower_offload *flow, 189 bool mask_version) 190 { 191 struct fl_flow_key *target = mask_version ? flow->mask : flow->key; 192 struct flow_dissector_key_ipv6_addrs *addr; 193 struct flow_dissector_key_basic *basic; 194 195 memset(frame, 0, sizeof(struct nfp_flower_ipv6)); 196 197 if (dissector_uses_key(flow->dissector, 198 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) { 199 addr = skb_flow_dissector_target(flow->dissector, 200 FLOW_DISSECTOR_KEY_IPV6_ADDRS, 201 target); 202 frame->ipv6_src = addr->src; 203 frame->ipv6_dst = addr->dst; 204 } 205 206 if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_BASIC)) { 207 basic = skb_flow_dissector_target(flow->dissector, 208 FLOW_DISSECTOR_KEY_BASIC, 209 target); 210 frame->proto = basic->ip_proto; 211 } 212 213 if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_IP)) { 214 struct flow_dissector_key_ip *flow_ip; 215 216 flow_ip = skb_flow_dissector_target(flow->dissector, 217 FLOW_DISSECTOR_KEY_IP, 218 target); 219 frame->tos = flow_ip->tos; 220 frame->ttl = flow_ip->ttl; 221 } 222 } 223 224 static void 225 nfp_flower_compile_ipv4_udp_tun(struct nfp_flower_ipv4_udp_tun *frame, 226 struct tc_cls_flower_offload *flow, 227 bool mask_version) 228 { 229 struct fl_flow_key *target = mask_version ? flow->mask : flow->key; 230 struct flow_dissector_key_ipv4_addrs *tun_ips; 231 struct flow_dissector_key_keyid *vni; 232 233 memset(frame, 0, sizeof(struct nfp_flower_ipv4_udp_tun)); 234 235 if (dissector_uses_key(flow->dissector, 236 FLOW_DISSECTOR_KEY_ENC_KEYID)) { 237 u32 temp_vni; 238 239 vni = skb_flow_dissector_target(flow->dissector, 240 FLOW_DISSECTOR_KEY_ENC_KEYID, 241 target); 242 temp_vni = be32_to_cpu(vni->keyid) << NFP_FL_TUN_VNI_OFFSET; 243 frame->tun_id = cpu_to_be32(temp_vni); 244 } 245 246 if (dissector_uses_key(flow->dissector, 247 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) { 248 tun_ips = 249 skb_flow_dissector_target(flow->dissector, 250 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, 251 target); 252 frame->ip_src = tun_ips->src; 253 frame->ip_dst = tun_ips->dst; 254 } 255 } 256 257 int nfp_flower_compile_flow_match(struct tc_cls_flower_offload *flow, 258 struct nfp_fl_key_ls *key_ls, 259 struct net_device *netdev, 260 struct nfp_fl_payload *nfp_flow, 261 enum nfp_flower_tun_type tun_type) 262 { 263 struct nfp_repr *netdev_repr; 264 int err; 265 u8 *ext; 266 u8 *msk; 267 268 memset(nfp_flow->unmasked_data, 0, key_ls->key_size); 269 memset(nfp_flow->mask_data, 0, key_ls->key_size); 270 271 ext = nfp_flow->unmasked_data; 272 msk = nfp_flow->mask_data; 273 274 /* Populate Exact Metadata. */ 275 nfp_flower_compile_meta_tci((struct nfp_flower_meta_tci *)ext, 276 flow, key_ls->key_layer, false); 277 /* Populate Mask Metadata. */ 278 nfp_flower_compile_meta_tci((struct nfp_flower_meta_tci *)msk, 279 flow, key_ls->key_layer, true); 280 ext += sizeof(struct nfp_flower_meta_tci); 281 msk += sizeof(struct nfp_flower_meta_tci); 282 283 /* Populate Extended Metadata if Required. */ 284 if (NFP_FLOWER_LAYER_EXT_META & key_ls->key_layer) { 285 nfp_flower_compile_ext_meta((struct nfp_flower_ext_meta *)ext, 286 key_ls->key_layer_two); 287 nfp_flower_compile_ext_meta((struct nfp_flower_ext_meta *)msk, 288 key_ls->key_layer_two); 289 ext += sizeof(struct nfp_flower_ext_meta); 290 msk += sizeof(struct nfp_flower_ext_meta); 291 } 292 293 /* Populate Exact Port data. */ 294 err = nfp_flower_compile_port((struct nfp_flower_in_port *)ext, 295 nfp_repr_get_port_id(netdev), 296 false, tun_type); 297 if (err) 298 return err; 299 300 /* Populate Mask Port Data. */ 301 err = nfp_flower_compile_port((struct nfp_flower_in_port *)msk, 302 nfp_repr_get_port_id(netdev), 303 true, tun_type); 304 if (err) 305 return err; 306 307 ext += sizeof(struct nfp_flower_in_port); 308 msk += sizeof(struct nfp_flower_in_port); 309 310 if (NFP_FLOWER_LAYER_MAC & key_ls->key_layer) { 311 /* Populate Exact MAC Data. */ 312 nfp_flower_compile_mac((struct nfp_flower_mac_mpls *)ext, 313 flow, false); 314 /* Populate Mask MAC Data. */ 315 nfp_flower_compile_mac((struct nfp_flower_mac_mpls *)msk, 316 flow, true); 317 ext += sizeof(struct nfp_flower_mac_mpls); 318 msk += sizeof(struct nfp_flower_mac_mpls); 319 } 320 321 if (NFP_FLOWER_LAYER_TP & key_ls->key_layer) { 322 /* Populate Exact TP Data. */ 323 nfp_flower_compile_tport((struct nfp_flower_tp_ports *)ext, 324 flow, false); 325 /* Populate Mask TP Data. */ 326 nfp_flower_compile_tport((struct nfp_flower_tp_ports *)msk, 327 flow, true); 328 ext += sizeof(struct nfp_flower_tp_ports); 329 msk += sizeof(struct nfp_flower_tp_ports); 330 } 331 332 if (NFP_FLOWER_LAYER_IPV4 & key_ls->key_layer) { 333 /* Populate Exact IPv4 Data. */ 334 nfp_flower_compile_ipv4((struct nfp_flower_ipv4 *)ext, 335 flow, false); 336 /* Populate Mask IPv4 Data. */ 337 nfp_flower_compile_ipv4((struct nfp_flower_ipv4 *)msk, 338 flow, true); 339 ext += sizeof(struct nfp_flower_ipv4); 340 msk += sizeof(struct nfp_flower_ipv4); 341 } 342 343 if (NFP_FLOWER_LAYER_IPV6 & key_ls->key_layer) { 344 /* Populate Exact IPv4 Data. */ 345 nfp_flower_compile_ipv6((struct nfp_flower_ipv6 *)ext, 346 flow, false); 347 /* Populate Mask IPv4 Data. */ 348 nfp_flower_compile_ipv6((struct nfp_flower_ipv6 *)msk, 349 flow, true); 350 ext += sizeof(struct nfp_flower_ipv6); 351 msk += sizeof(struct nfp_flower_ipv6); 352 } 353 354 if (key_ls->key_layer & NFP_FLOWER_LAYER_VXLAN || 355 key_ls->key_layer_two & NFP_FLOWER_LAYER2_GENEVE) { 356 __be32 tun_dst; 357 358 /* Populate Exact VXLAN Data. */ 359 nfp_flower_compile_ipv4_udp_tun((void *)ext, flow, false); 360 /* Populate Mask VXLAN Data. */ 361 nfp_flower_compile_ipv4_udp_tun((void *)msk, flow, true); 362 tun_dst = ((struct nfp_flower_ipv4_udp_tun *)ext)->ip_dst; 363 ext += sizeof(struct nfp_flower_ipv4_udp_tun); 364 msk += sizeof(struct nfp_flower_ipv4_udp_tun); 365 366 /* Configure tunnel end point MAC. */ 367 if (nfp_netdev_is_nfp_repr(netdev)) { 368 netdev_repr = netdev_priv(netdev); 369 nfp_tunnel_write_macs(netdev_repr->app); 370 371 /* Store the tunnel destination in the rule data. 372 * This must be present and be an exact match. 373 */ 374 nfp_flow->nfp_tun_ipv4_addr = tun_dst; 375 nfp_tunnel_add_ipv4_off(netdev_repr->app, tun_dst); 376 } 377 } 378 379 return 0; 380 } 381