Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1 | /* |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2007-2013 Nicira, Inc. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of version 2 of the GNU General Public |
| 6 | * License as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 16 | * 02110-1301, USA |
| 17 | */ |
| 18 | |
| 19 | #include "flow.h" |
| 20 | #include "datapath.h" |
| 21 | #include <linux/uaccess.h> |
| 22 | #include <linux/netdevice.h> |
| 23 | #include <linux/etherdevice.h> |
| 24 | #include <linux/if_ether.h> |
| 25 | #include <linux/if_vlan.h> |
| 26 | #include <net/llc_pdu.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/jhash.h> |
| 29 | #include <linux/jiffies.h> |
| 30 | #include <linux/llc.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/in.h> |
| 33 | #include <linux/rcupdate.h> |
| 34 | #include <linux/if_arp.h> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 35 | #include <linux/ip.h> |
| 36 | #include <linux/ipv6.h> |
| 37 | #include <linux/tcp.h> |
| 38 | #include <linux/udp.h> |
| 39 | #include <linux/icmp.h> |
| 40 | #include <linux/icmpv6.h> |
| 41 | #include <linux/rculist.h> |
| 42 | #include <net/ip.h> |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 43 | #include <net/ip_tunnels.h> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 44 | #include <net/ipv6.h> |
| 45 | #include <net/ndisc.h> |
| 46 | |
| 47 | static struct kmem_cache *flow_cache; |
| 48 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 49 | static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask, |
| 50 | struct sw_flow_key_range *range, u8 val); |
| 51 | |
| 52 | static void update_range__(struct sw_flow_match *match, |
| 53 | size_t offset, size_t size, bool is_mask) |
| 54 | { |
| 55 | struct sw_flow_key_range *range = NULL; |
| 56 | size_t start = offset; |
| 57 | size_t end = offset + size; |
| 58 | |
| 59 | if (!is_mask) |
| 60 | range = &match->range; |
| 61 | else if (match->mask) |
| 62 | range = &match->mask->range; |
| 63 | |
| 64 | if (!range) |
| 65 | return; |
| 66 | |
| 67 | if (range->start == range->end) { |
| 68 | range->start = start; |
| 69 | range->end = end; |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | if (range->start > start) |
| 74 | range->start = start; |
| 75 | |
| 76 | if (range->end < end) |
| 77 | range->end = end; |
| 78 | } |
| 79 | |
| 80 | #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \ |
| 81 | do { \ |
| 82 | update_range__(match, offsetof(struct sw_flow_key, field), \ |
| 83 | sizeof((match)->key->field), is_mask); \ |
| 84 | if (is_mask) { \ |
| 85 | if ((match)->mask) \ |
| 86 | (match)->mask->key.field = value; \ |
| 87 | } else { \ |
| 88 | (match)->key->field = value; \ |
| 89 | } \ |
| 90 | } while (0) |
| 91 | |
| 92 | #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \ |
| 93 | do { \ |
| 94 | update_range__(match, offsetof(struct sw_flow_key, field), \ |
| 95 | len, is_mask); \ |
| 96 | if (is_mask) { \ |
| 97 | if ((match)->mask) \ |
| 98 | memcpy(&(match)->mask->key.field, value_p, len);\ |
| 99 | } else { \ |
| 100 | memcpy(&(match)->key->field, value_p, len); \ |
| 101 | } \ |
| 102 | } while (0) |
| 103 | |
| 104 | void ovs_match_init(struct sw_flow_match *match, |
| 105 | struct sw_flow_key *key, |
| 106 | struct sw_flow_mask *mask) |
| 107 | { |
| 108 | memset(match, 0, sizeof(*match)); |
| 109 | match->key = key; |
| 110 | match->mask = mask; |
| 111 | |
| 112 | memset(key, 0, sizeof(*key)); |
| 113 | |
| 114 | if (mask) { |
| 115 | memset(&mask->key, 0, sizeof(mask->key)); |
| 116 | mask->range.start = mask->range.end = 0; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | static bool ovs_match_validate(const struct sw_flow_match *match, |
| 121 | u64 key_attrs, u64 mask_attrs) |
| 122 | { |
| 123 | u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET; |
| 124 | u64 mask_allowed = key_attrs; /* At most allow all key attributes */ |
| 125 | |
| 126 | /* The following mask attributes allowed only if they |
| 127 | * pass the validation tests. */ |
| 128 | mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4) |
| 129 | | (1 << OVS_KEY_ATTR_IPV6) |
| 130 | | (1 << OVS_KEY_ATTR_TCP) |
| 131 | | (1 << OVS_KEY_ATTR_UDP) |
| 132 | | (1 << OVS_KEY_ATTR_ICMP) |
| 133 | | (1 << OVS_KEY_ATTR_ICMPV6) |
| 134 | | (1 << OVS_KEY_ATTR_ARP) |
| 135 | | (1 << OVS_KEY_ATTR_ND)); |
| 136 | |
| 137 | /* Always allowed mask fields. */ |
| 138 | mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL) |
| 139 | | (1 << OVS_KEY_ATTR_IN_PORT) |
| 140 | | (1 << OVS_KEY_ATTR_ETHERTYPE)); |
| 141 | |
| 142 | /* Check key attributes. */ |
| 143 | if (match->key->eth.type == htons(ETH_P_ARP) |
| 144 | || match->key->eth.type == htons(ETH_P_RARP)) { |
| 145 | key_expected |= 1 << OVS_KEY_ATTR_ARP; |
| 146 | if (match->mask && (match->mask->key.eth.type == htons(0xffff))) |
| 147 | mask_allowed |= 1 << OVS_KEY_ATTR_ARP; |
| 148 | } |
| 149 | |
| 150 | if (match->key->eth.type == htons(ETH_P_IP)) { |
| 151 | key_expected |= 1 << OVS_KEY_ATTR_IPV4; |
| 152 | if (match->mask && (match->mask->key.eth.type == htons(0xffff))) |
| 153 | mask_allowed |= 1 << OVS_KEY_ATTR_IPV4; |
| 154 | |
| 155 | if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) { |
| 156 | if (match->key->ip.proto == IPPROTO_UDP) { |
| 157 | key_expected |= 1 << OVS_KEY_ATTR_UDP; |
| 158 | if (match->mask && (match->mask->key.ip.proto == 0xff)) |
| 159 | mask_allowed |= 1 << OVS_KEY_ATTR_UDP; |
| 160 | } |
| 161 | |
| 162 | if (match->key->ip.proto == IPPROTO_TCP) { |
| 163 | key_expected |= 1 << OVS_KEY_ATTR_TCP; |
| 164 | if (match->mask && (match->mask->key.ip.proto == 0xff)) |
| 165 | mask_allowed |= 1 << OVS_KEY_ATTR_TCP; |
| 166 | } |
| 167 | |
| 168 | if (match->key->ip.proto == IPPROTO_ICMP) { |
| 169 | key_expected |= 1 << OVS_KEY_ATTR_ICMP; |
| 170 | if (match->mask && (match->mask->key.ip.proto == 0xff)) |
| 171 | mask_allowed |= 1 << OVS_KEY_ATTR_ICMP; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if (match->key->eth.type == htons(ETH_P_IPV6)) { |
| 177 | key_expected |= 1 << OVS_KEY_ATTR_IPV6; |
| 178 | if (match->mask && (match->mask->key.eth.type == htons(0xffff))) |
| 179 | mask_allowed |= 1 << OVS_KEY_ATTR_IPV6; |
| 180 | |
| 181 | if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) { |
| 182 | if (match->key->ip.proto == IPPROTO_UDP) { |
| 183 | key_expected |= 1 << OVS_KEY_ATTR_UDP; |
| 184 | if (match->mask && (match->mask->key.ip.proto == 0xff)) |
| 185 | mask_allowed |= 1 << OVS_KEY_ATTR_UDP; |
| 186 | } |
| 187 | |
| 188 | if (match->key->ip.proto == IPPROTO_TCP) { |
| 189 | key_expected |= 1 << OVS_KEY_ATTR_TCP; |
| 190 | if (match->mask && (match->mask->key.ip.proto == 0xff)) |
| 191 | mask_allowed |= 1 << OVS_KEY_ATTR_TCP; |
| 192 | } |
| 193 | |
| 194 | if (match->key->ip.proto == IPPROTO_ICMPV6) { |
| 195 | key_expected |= 1 << OVS_KEY_ATTR_ICMPV6; |
| 196 | if (match->mask && (match->mask->key.ip.proto == 0xff)) |
| 197 | mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6; |
| 198 | |
| 199 | if (match->key->ipv6.tp.src == |
| 200 | htons(NDISC_NEIGHBOUR_SOLICITATION) || |
| 201 | match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) { |
| 202 | key_expected |= 1 << OVS_KEY_ATTR_ND; |
| 203 | if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff))) |
| 204 | mask_allowed |= 1 << OVS_KEY_ATTR_ND; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if ((key_attrs & key_expected) != key_expected) { |
| 211 | /* Key attributes check failed. */ |
| 212 | OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n", |
| 213 | key_attrs, key_expected); |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | if ((mask_attrs & mask_allowed) != mask_attrs) { |
| 218 | /* Mask attributes check failed. */ |
| 219 | OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n", |
| 220 | mask_attrs, mask_allowed); |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 227 | static int check_header(struct sk_buff *skb, int len) |
| 228 | { |
| 229 | if (unlikely(skb->len < len)) |
| 230 | return -EINVAL; |
| 231 | if (unlikely(!pskb_may_pull(skb, len))) |
| 232 | return -ENOMEM; |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static bool arphdr_ok(struct sk_buff *skb) |
| 237 | { |
| 238 | return pskb_may_pull(skb, skb_network_offset(skb) + |
| 239 | sizeof(struct arp_eth_header)); |
| 240 | } |
| 241 | |
| 242 | static int check_iphdr(struct sk_buff *skb) |
| 243 | { |
| 244 | unsigned int nh_ofs = skb_network_offset(skb); |
| 245 | unsigned int ip_len; |
| 246 | int err; |
| 247 | |
| 248 | err = check_header(skb, nh_ofs + sizeof(struct iphdr)); |
| 249 | if (unlikely(err)) |
| 250 | return err; |
| 251 | |
| 252 | ip_len = ip_hdrlen(skb); |
| 253 | if (unlikely(ip_len < sizeof(struct iphdr) || |
| 254 | skb->len < nh_ofs + ip_len)) |
| 255 | return -EINVAL; |
| 256 | |
| 257 | skb_set_transport_header(skb, nh_ofs + ip_len); |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | static bool tcphdr_ok(struct sk_buff *skb) |
| 262 | { |
| 263 | int th_ofs = skb_transport_offset(skb); |
| 264 | int tcp_len; |
| 265 | |
| 266 | if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr)))) |
| 267 | return false; |
| 268 | |
| 269 | tcp_len = tcp_hdrlen(skb); |
| 270 | if (unlikely(tcp_len < sizeof(struct tcphdr) || |
| 271 | skb->len < th_ofs + tcp_len)) |
| 272 | return false; |
| 273 | |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | static bool udphdr_ok(struct sk_buff *skb) |
| 278 | { |
| 279 | return pskb_may_pull(skb, skb_transport_offset(skb) + |
| 280 | sizeof(struct udphdr)); |
| 281 | } |
| 282 | |
| 283 | static bool icmphdr_ok(struct sk_buff *skb) |
| 284 | { |
| 285 | return pskb_may_pull(skb, skb_transport_offset(skb) + |
| 286 | sizeof(struct icmphdr)); |
| 287 | } |
| 288 | |
| 289 | u64 ovs_flow_used_time(unsigned long flow_jiffies) |
| 290 | { |
| 291 | struct timespec cur_ts; |
| 292 | u64 cur_ms, idle_ms; |
| 293 | |
| 294 | ktime_get_ts(&cur_ts); |
| 295 | idle_ms = jiffies_to_msecs(jiffies - flow_jiffies); |
| 296 | cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC + |
| 297 | cur_ts.tv_nsec / NSEC_PER_MSEC; |
| 298 | |
| 299 | return cur_ms - idle_ms; |
| 300 | } |
| 301 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 302 | static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 303 | { |
| 304 | unsigned int nh_ofs = skb_network_offset(skb); |
| 305 | unsigned int nh_len; |
| 306 | int payload_ofs; |
| 307 | struct ipv6hdr *nh; |
| 308 | uint8_t nexthdr; |
| 309 | __be16 frag_off; |
| 310 | int err; |
| 311 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 312 | err = check_header(skb, nh_ofs + sizeof(*nh)); |
| 313 | if (unlikely(err)) |
| 314 | return err; |
| 315 | |
| 316 | nh = ipv6_hdr(skb); |
| 317 | nexthdr = nh->nexthdr; |
| 318 | payload_ofs = (u8 *)(nh + 1) - skb->data; |
| 319 | |
| 320 | key->ip.proto = NEXTHDR_NONE; |
| 321 | key->ip.tos = ipv6_get_dsfield(nh); |
| 322 | key->ip.ttl = nh->hop_limit; |
| 323 | key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL); |
| 324 | key->ipv6.addr.src = nh->saddr; |
| 325 | key->ipv6.addr.dst = nh->daddr; |
| 326 | |
| 327 | payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off); |
| 328 | if (unlikely(payload_ofs < 0)) |
| 329 | return -EINVAL; |
| 330 | |
| 331 | if (frag_off) { |
| 332 | if (frag_off & htons(~0x7)) |
| 333 | key->ip.frag = OVS_FRAG_TYPE_LATER; |
| 334 | else |
| 335 | key->ip.frag = OVS_FRAG_TYPE_FIRST; |
| 336 | } |
| 337 | |
| 338 | nh_len = payload_ofs - nh_ofs; |
| 339 | skb_set_transport_header(skb, nh_ofs + nh_len); |
| 340 | key->ip.proto = nexthdr; |
| 341 | return nh_len; |
| 342 | } |
| 343 | |
| 344 | static bool icmp6hdr_ok(struct sk_buff *skb) |
| 345 | { |
| 346 | return pskb_may_pull(skb, skb_transport_offset(skb) + |
| 347 | sizeof(struct icmp6hdr)); |
| 348 | } |
| 349 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 350 | void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src, |
| 351 | const struct sw_flow_mask *mask) |
| 352 | { |
| 353 | u8 *m = (u8 *)&mask->key + mask->range.start; |
| 354 | u8 *s = (u8 *)src + mask->range.start; |
| 355 | u8 *d = (u8 *)dst + mask->range.start; |
| 356 | int i; |
| 357 | |
| 358 | memset(dst, 0, sizeof(*dst)); |
| 359 | for (i = 0; i < ovs_sw_flow_mask_size_roundup(mask); i++) { |
| 360 | *d = *s & *m; |
| 361 | d++, s++, m++; |
| 362 | } |
| 363 | } |
| 364 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 365 | #define TCP_FLAGS_OFFSET 13 |
| 366 | #define TCP_FLAG_MASK 0x3f |
| 367 | |
| 368 | void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb) |
| 369 | { |
| 370 | u8 tcp_flags = 0; |
| 371 | |
Jesse Gross | c55177e | 2012-04-02 15:13:36 -0700 | [diff] [blame] | 372 | if ((flow->key.eth.type == htons(ETH_P_IP) || |
| 373 | flow->key.eth.type == htons(ETH_P_IPV6)) && |
Jesse Gross | bf32fec | 2012-04-02 14:26:27 -0700 | [diff] [blame] | 374 | flow->key.ip.proto == IPPROTO_TCP && |
| 375 | likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 376 | u8 *tcp = (u8 *)tcp_hdr(skb); |
| 377 | tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK; |
| 378 | } |
| 379 | |
| 380 | spin_lock(&flow->lock); |
| 381 | flow->used = jiffies; |
| 382 | flow->packet_count++; |
| 383 | flow->byte_count += skb->len; |
| 384 | flow->tcp_flags |= tcp_flags; |
| 385 | spin_unlock(&flow->lock); |
| 386 | } |
| 387 | |
Pravin B Shelar | 74f84a5 | 2013-06-17 17:50:12 -0700 | [diff] [blame] | 388 | struct sw_flow_actions *ovs_flow_actions_alloc(int size) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 389 | { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 390 | struct sw_flow_actions *sfa; |
| 391 | |
Pravin B Shelar | 74f84a5 | 2013-06-17 17:50:12 -0700 | [diff] [blame] | 392 | if (size > MAX_ACTIONS_BUFSIZE) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 393 | return ERR_PTR(-EINVAL); |
| 394 | |
Pravin B Shelar | 74f84a5 | 2013-06-17 17:50:12 -0700 | [diff] [blame] | 395 | sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 396 | if (!sfa) |
| 397 | return ERR_PTR(-ENOMEM); |
| 398 | |
Pravin B Shelar | 74f84a5 | 2013-06-17 17:50:12 -0700 | [diff] [blame] | 399 | sfa->actions_len = 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 400 | return sfa; |
| 401 | } |
| 402 | |
| 403 | struct sw_flow *ovs_flow_alloc(void) |
| 404 | { |
| 405 | struct sw_flow *flow; |
| 406 | |
| 407 | flow = kmem_cache_alloc(flow_cache, GFP_KERNEL); |
| 408 | if (!flow) |
| 409 | return ERR_PTR(-ENOMEM); |
| 410 | |
| 411 | spin_lock_init(&flow->lock); |
| 412 | flow->sf_acts = NULL; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 413 | flow->mask = NULL; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 414 | |
| 415 | return flow; |
| 416 | } |
| 417 | |
| 418 | static struct hlist_head *find_bucket(struct flow_table *table, u32 hash) |
| 419 | { |
| 420 | hash = jhash_1word(hash, table->hash_seed); |
| 421 | return flex_array_get(table->buckets, |
| 422 | (hash & (table->n_buckets - 1))); |
| 423 | } |
| 424 | |
| 425 | static struct flex_array *alloc_buckets(unsigned int n_buckets) |
| 426 | { |
| 427 | struct flex_array *buckets; |
| 428 | int i, err; |
| 429 | |
Pravin B Shelar | 42415c9 | 2013-07-30 15:44:14 -0700 | [diff] [blame] | 430 | buckets = flex_array_alloc(sizeof(struct hlist_head), |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 431 | n_buckets, GFP_KERNEL); |
| 432 | if (!buckets) |
| 433 | return NULL; |
| 434 | |
| 435 | err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL); |
| 436 | if (err) { |
| 437 | flex_array_free(buckets); |
| 438 | return NULL; |
| 439 | } |
| 440 | |
| 441 | for (i = 0; i < n_buckets; i++) |
| 442 | INIT_HLIST_HEAD((struct hlist_head *) |
| 443 | flex_array_get(buckets, i)); |
| 444 | |
| 445 | return buckets; |
| 446 | } |
| 447 | |
| 448 | static void free_buckets(struct flex_array *buckets) |
| 449 | { |
| 450 | flex_array_free(buckets); |
| 451 | } |
| 452 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 453 | static struct flow_table *__flow_tbl_alloc(int new_size) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 454 | { |
| 455 | struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL); |
| 456 | |
| 457 | if (!table) |
| 458 | return NULL; |
| 459 | |
| 460 | table->buckets = alloc_buckets(new_size); |
| 461 | |
| 462 | if (!table->buckets) { |
| 463 | kfree(table); |
| 464 | return NULL; |
| 465 | } |
| 466 | table->n_buckets = new_size; |
| 467 | table->count = 0; |
| 468 | table->node_ver = 0; |
| 469 | table->keep_flows = false; |
| 470 | get_random_bytes(&table->hash_seed, sizeof(u32)); |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 471 | table->mask_list = NULL; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 472 | |
| 473 | return table; |
| 474 | } |
| 475 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 476 | static void __flow_tbl_destroy(struct flow_table *table) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 477 | { |
| 478 | int i; |
| 479 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 480 | if (table->keep_flows) |
| 481 | goto skip_flows; |
| 482 | |
| 483 | for (i = 0; i < table->n_buckets; i++) { |
| 484 | struct sw_flow *flow; |
| 485 | struct hlist_head *head = flex_array_get(table->buckets, i); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 486 | struct hlist_node *n; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 487 | int ver = table->node_ver; |
| 488 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 489 | hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) { |
Pravin B Shelar | 76a66c7 | 2013-07-30 15:45:59 -0700 | [diff] [blame] | 490 | hlist_del(&flow->hash_node[ver]); |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 491 | ovs_flow_free(flow, false); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 495 | BUG_ON(!list_empty(table->mask_list)); |
| 496 | kfree(table->mask_list); |
| 497 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 498 | skip_flows: |
| 499 | free_buckets(table->buckets); |
| 500 | kfree(table); |
| 501 | } |
| 502 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 503 | struct flow_table *ovs_flow_tbl_alloc(int new_size) |
| 504 | { |
| 505 | struct flow_table *table = __flow_tbl_alloc(new_size); |
| 506 | |
| 507 | if (!table) |
| 508 | return NULL; |
| 509 | |
| 510 | table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL); |
| 511 | if (!table->mask_list) { |
| 512 | table->keep_flows = true; |
| 513 | __flow_tbl_destroy(table); |
| 514 | return NULL; |
| 515 | } |
| 516 | INIT_LIST_HEAD(table->mask_list); |
| 517 | |
| 518 | return table; |
| 519 | } |
| 520 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 521 | static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu) |
| 522 | { |
| 523 | struct flow_table *table = container_of(rcu, struct flow_table, rcu); |
| 524 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 525 | __flow_tbl_destroy(table); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 528 | void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 529 | { |
| 530 | if (!table) |
| 531 | return; |
| 532 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 533 | if (deferred) |
| 534 | call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb); |
| 535 | else |
| 536 | __flow_tbl_destroy(table); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 539 | struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *last) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 540 | { |
| 541 | struct sw_flow *flow; |
| 542 | struct hlist_head *head; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 543 | int ver; |
| 544 | int i; |
| 545 | |
| 546 | ver = table->node_ver; |
| 547 | while (*bucket < table->n_buckets) { |
| 548 | i = 0; |
| 549 | head = flex_array_get(table->buckets, *bucket); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 550 | hlist_for_each_entry_rcu(flow, head, hash_node[ver]) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 551 | if (i < *last) { |
| 552 | i++; |
| 553 | continue; |
| 554 | } |
| 555 | *last = i + 1; |
| 556 | return flow; |
| 557 | } |
| 558 | (*bucket)++; |
| 559 | *last = 0; |
| 560 | } |
| 561 | |
| 562 | return NULL; |
| 563 | } |
| 564 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 565 | static void __tbl_insert(struct flow_table *table, struct sw_flow *flow) |
Pravin B Shelar | a3e8299 | 2013-06-17 17:50:28 -0700 | [diff] [blame] | 566 | { |
| 567 | struct hlist_head *head; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 568 | |
Pravin B Shelar | a3e8299 | 2013-06-17 17:50:28 -0700 | [diff] [blame] | 569 | head = find_bucket(table, flow->hash); |
| 570 | hlist_add_head_rcu(&flow->hash_node[table->node_ver], head); |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 571 | |
Pravin B Shelar | a3e8299 | 2013-06-17 17:50:28 -0700 | [diff] [blame] | 572 | table->count++; |
| 573 | } |
| 574 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 575 | static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new) |
| 576 | { |
| 577 | int old_ver; |
| 578 | int i; |
| 579 | |
| 580 | old_ver = old->node_ver; |
| 581 | new->node_ver = !old_ver; |
| 582 | |
| 583 | /* Insert in new table. */ |
| 584 | for (i = 0; i < old->n_buckets; i++) { |
| 585 | struct sw_flow *flow; |
| 586 | struct hlist_head *head; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 587 | |
| 588 | head = flex_array_get(old->buckets, i); |
| 589 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 590 | hlist_for_each_entry(flow, head, hash_node[old_ver]) |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 591 | __tbl_insert(new, flow); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 592 | } |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 593 | |
| 594 | new->mask_list = old->mask_list; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 595 | old->keep_flows = true; |
| 596 | } |
| 597 | |
| 598 | static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets) |
| 599 | { |
| 600 | struct flow_table *new_table; |
| 601 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 602 | new_table = __flow_tbl_alloc(n_buckets); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 603 | if (!new_table) |
| 604 | return ERR_PTR(-ENOMEM); |
| 605 | |
| 606 | flow_table_copy_flows(table, new_table); |
| 607 | |
| 608 | return new_table; |
| 609 | } |
| 610 | |
| 611 | struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table) |
| 612 | { |
| 613 | return __flow_tbl_rehash(table, table->n_buckets); |
| 614 | } |
| 615 | |
| 616 | struct flow_table *ovs_flow_tbl_expand(struct flow_table *table) |
| 617 | { |
| 618 | return __flow_tbl_rehash(table, table->n_buckets * 2); |
| 619 | } |
| 620 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 621 | static void __flow_free(struct sw_flow *flow) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 622 | { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 623 | kfree((struct sf_flow_acts __force *)flow->sf_acts); |
| 624 | kmem_cache_free(flow_cache, flow); |
| 625 | } |
| 626 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 627 | static void rcu_free_flow_callback(struct rcu_head *rcu) |
| 628 | { |
| 629 | struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu); |
| 630 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 631 | __flow_free(flow); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 634 | void ovs_flow_free(struct sw_flow *flow, bool deferred) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 635 | { |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 636 | if (!flow) |
| 637 | return; |
| 638 | |
| 639 | ovs_sw_flow_mask_del_ref(flow->mask, deferred); |
| 640 | |
| 641 | if (deferred) |
| 642 | call_rcu(&flow->rcu, rcu_free_flow_callback); |
| 643 | else |
| 644 | __flow_free(flow); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 647 | /* Schedules 'sf_acts' to be freed after the next RCU grace period. |
| 648 | * The caller must hold rcu_read_lock for this to be sensible. */ |
| 649 | void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts) |
| 650 | { |
Wei Yongjun | 80f0fd8 | 2012-08-26 18:20:45 +0000 | [diff] [blame] | 651 | kfree_rcu(sf_acts, rcu); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key) |
| 655 | { |
| 656 | struct qtag_prefix { |
| 657 | __be16 eth_type; /* ETH_P_8021Q */ |
| 658 | __be16 tci; |
| 659 | }; |
| 660 | struct qtag_prefix *qp; |
| 661 | |
| 662 | if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16))) |
| 663 | return 0; |
| 664 | |
| 665 | if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) + |
| 666 | sizeof(__be16)))) |
| 667 | return -ENOMEM; |
| 668 | |
| 669 | qp = (struct qtag_prefix *) skb->data; |
| 670 | key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT); |
| 671 | __skb_pull(skb, sizeof(struct qtag_prefix)); |
| 672 | |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | static __be16 parse_ethertype(struct sk_buff *skb) |
| 677 | { |
| 678 | struct llc_snap_hdr { |
| 679 | u8 dsap; /* Always 0xAA */ |
| 680 | u8 ssap; /* Always 0xAA */ |
| 681 | u8 ctrl; |
| 682 | u8 oui[3]; |
| 683 | __be16 ethertype; |
| 684 | }; |
| 685 | struct llc_snap_hdr *llc; |
| 686 | __be16 proto; |
| 687 | |
| 688 | proto = *(__be16 *) skb->data; |
| 689 | __skb_pull(skb, sizeof(__be16)); |
| 690 | |
Simon Horman | e5c5d22 | 2013-03-28 13:38:25 +0900 | [diff] [blame] | 691 | if (ntohs(proto) >= ETH_P_802_3_MIN) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 692 | return proto; |
| 693 | |
| 694 | if (skb->len < sizeof(struct llc_snap_hdr)) |
| 695 | return htons(ETH_P_802_2); |
| 696 | |
| 697 | if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr)))) |
| 698 | return htons(0); |
| 699 | |
| 700 | llc = (struct llc_snap_hdr *) skb->data; |
| 701 | if (llc->dsap != LLC_SAP_SNAP || |
| 702 | llc->ssap != LLC_SAP_SNAP || |
| 703 | (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0) |
| 704 | return htons(ETH_P_802_2); |
| 705 | |
| 706 | __skb_pull(skb, sizeof(struct llc_snap_hdr)); |
Rich Lane | 17b682a | 2013-02-19 11:10:30 -0800 | [diff] [blame] | 707 | |
Simon Horman | e5c5d22 | 2013-03-28 13:38:25 +0900 | [diff] [blame] | 708 | if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN) |
Rich Lane | 17b682a | 2013-02-19 11:10:30 -0800 | [diff] [blame] | 709 | return llc->ethertype; |
| 710 | |
| 711 | return htons(ETH_P_802_2); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key, |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 715 | int nh_len) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 716 | { |
| 717 | struct icmp6hdr *icmp = icmp6_hdr(skb); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 718 | |
| 719 | /* The ICMPv6 type and code fields use the 16-bit transport port |
| 720 | * fields, so we need to store them in 16-bit network byte order. |
| 721 | */ |
| 722 | key->ipv6.tp.src = htons(icmp->icmp6_type); |
| 723 | key->ipv6.tp.dst = htons(icmp->icmp6_code); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 724 | |
| 725 | if (icmp->icmp6_code == 0 && |
| 726 | (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION || |
| 727 | icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) { |
| 728 | int icmp_len = skb->len - skb_transport_offset(skb); |
| 729 | struct nd_msg *nd; |
| 730 | int offset; |
| 731 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 732 | /* In order to process neighbor discovery options, we need the |
| 733 | * entire packet. |
| 734 | */ |
| 735 | if (unlikely(icmp_len < sizeof(*nd))) |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 736 | return 0; |
| 737 | |
| 738 | if (unlikely(skb_linearize(skb))) |
| 739 | return -ENOMEM; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 740 | |
| 741 | nd = (struct nd_msg *)skb_transport_header(skb); |
| 742 | key->ipv6.nd.target = nd->target; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 743 | |
| 744 | icmp_len -= sizeof(*nd); |
| 745 | offset = 0; |
| 746 | while (icmp_len >= 8) { |
| 747 | struct nd_opt_hdr *nd_opt = |
| 748 | (struct nd_opt_hdr *)(nd->opt + offset); |
| 749 | int opt_len = nd_opt->nd_opt_len * 8; |
| 750 | |
| 751 | if (unlikely(!opt_len || opt_len > icmp_len)) |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 752 | return 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 753 | |
| 754 | /* Store the link layer address if the appropriate |
| 755 | * option is provided. It is considered an error if |
| 756 | * the same link layer option is specified twice. |
| 757 | */ |
| 758 | if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR |
| 759 | && opt_len == 8) { |
| 760 | if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll))) |
| 761 | goto invalid; |
| 762 | memcpy(key->ipv6.nd.sll, |
| 763 | &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN); |
| 764 | } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR |
| 765 | && opt_len == 8) { |
| 766 | if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll))) |
| 767 | goto invalid; |
| 768 | memcpy(key->ipv6.nd.tll, |
| 769 | &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN); |
| 770 | } |
| 771 | |
| 772 | icmp_len -= opt_len; |
| 773 | offset += opt_len; |
| 774 | } |
| 775 | } |
| 776 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 777 | return 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 778 | |
| 779 | invalid: |
| 780 | memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target)); |
| 781 | memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll)); |
| 782 | memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll)); |
| 783 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 784 | return 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /** |
| 788 | * ovs_flow_extract - extracts a flow key from an Ethernet frame. |
| 789 | * @skb: sk_buff that contains the frame, with skb->data pointing to the |
| 790 | * Ethernet header |
| 791 | * @in_port: port number on which @skb was received. |
| 792 | * @key: output flow key |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 793 | * |
| 794 | * The caller must ensure that skb->len >= ETH_HLEN. |
| 795 | * |
| 796 | * Returns 0 if successful, otherwise a negative errno value. |
| 797 | * |
| 798 | * Initializes @skb header pointers as follows: |
| 799 | * |
| 800 | * - skb->mac_header: the Ethernet header. |
| 801 | * |
| 802 | * - skb->network_header: just past the Ethernet header, or just past the |
| 803 | * VLAN header, to the first byte of the Ethernet payload. |
| 804 | * |
Lorand Jakab | 34d94f2 | 2013-06-03 10:01:14 -0700 | [diff] [blame] | 805 | * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6 |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 806 | * on output, then just past the IP header, if one is present and |
| 807 | * of a correct length, otherwise the same as skb->network_header. |
Lorand Jakab | 34d94f2 | 2013-06-03 10:01:14 -0700 | [diff] [blame] | 808 | * For other key->eth.type values it is left untouched. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 809 | */ |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 810 | int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 811 | { |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 812 | int error; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 813 | struct ethhdr *eth; |
| 814 | |
| 815 | memset(key, 0, sizeof(*key)); |
| 816 | |
| 817 | key->phy.priority = skb->priority; |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 818 | if (OVS_CB(skb)->tun_key) |
| 819 | memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 820 | key->phy.in_port = in_port; |
Ansis Atteka | 39c7caeb | 2012-11-26 11:24:11 -0800 | [diff] [blame] | 821 | key->phy.skb_mark = skb->mark; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 822 | |
| 823 | skb_reset_mac_header(skb); |
| 824 | |
| 825 | /* Link layer. We are guaranteed to have at least the 14 byte Ethernet |
| 826 | * header in the linear data area. |
| 827 | */ |
| 828 | eth = eth_hdr(skb); |
| 829 | memcpy(key->eth.src, eth->h_source, ETH_ALEN); |
| 830 | memcpy(key->eth.dst, eth->h_dest, ETH_ALEN); |
| 831 | |
| 832 | __skb_pull(skb, 2 * ETH_ALEN); |
Pravin B Shelar | b34df5e | 2013-06-13 11:11:44 -0700 | [diff] [blame] | 833 | /* We are going to push all headers that we pull, so no need to |
| 834 | * update skb->csum here. |
| 835 | */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 836 | |
| 837 | if (vlan_tx_tag_present(skb)) |
| 838 | key->eth.tci = htons(skb->vlan_tci); |
| 839 | else if (eth->h_proto == htons(ETH_P_8021Q)) |
| 840 | if (unlikely(parse_vlan(skb, key))) |
| 841 | return -ENOMEM; |
| 842 | |
| 843 | key->eth.type = parse_ethertype(skb); |
| 844 | if (unlikely(key->eth.type == htons(0))) |
| 845 | return -ENOMEM; |
| 846 | |
| 847 | skb_reset_network_header(skb); |
| 848 | __skb_push(skb, skb->data - skb_mac_header(skb)); |
| 849 | |
| 850 | /* Network layer. */ |
| 851 | if (key->eth.type == htons(ETH_P_IP)) { |
| 852 | struct iphdr *nh; |
| 853 | __be16 offset; |
| 854 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 855 | error = check_iphdr(skb); |
| 856 | if (unlikely(error)) { |
| 857 | if (error == -EINVAL) { |
| 858 | skb->transport_header = skb->network_header; |
| 859 | error = 0; |
| 860 | } |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 861 | return error; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | nh = ip_hdr(skb); |
| 865 | key->ipv4.addr.src = nh->saddr; |
| 866 | key->ipv4.addr.dst = nh->daddr; |
| 867 | |
| 868 | key->ip.proto = nh->protocol; |
| 869 | key->ip.tos = nh->tos; |
| 870 | key->ip.ttl = nh->ttl; |
| 871 | |
| 872 | offset = nh->frag_off & htons(IP_OFFSET); |
| 873 | if (offset) { |
| 874 | key->ip.frag = OVS_FRAG_TYPE_LATER; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 875 | return 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 876 | } |
| 877 | if (nh->frag_off & htons(IP_MF) || |
| 878 | skb_shinfo(skb)->gso_type & SKB_GSO_UDP) |
| 879 | key->ip.frag = OVS_FRAG_TYPE_FIRST; |
| 880 | |
| 881 | /* Transport layer. */ |
| 882 | if (key->ip.proto == IPPROTO_TCP) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 883 | if (tcphdr_ok(skb)) { |
| 884 | struct tcphdr *tcp = tcp_hdr(skb); |
| 885 | key->ipv4.tp.src = tcp->source; |
| 886 | key->ipv4.tp.dst = tcp->dest; |
| 887 | } |
| 888 | } else if (key->ip.proto == IPPROTO_UDP) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 889 | if (udphdr_ok(skb)) { |
| 890 | struct udphdr *udp = udp_hdr(skb); |
| 891 | key->ipv4.tp.src = udp->source; |
| 892 | key->ipv4.tp.dst = udp->dest; |
| 893 | } |
| 894 | } else if (key->ip.proto == IPPROTO_ICMP) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 895 | if (icmphdr_ok(skb)) { |
| 896 | struct icmphdr *icmp = icmp_hdr(skb); |
| 897 | /* The ICMP type and code fields use the 16-bit |
| 898 | * transport port fields, so we need to store |
| 899 | * them in 16-bit network byte order. */ |
| 900 | key->ipv4.tp.src = htons(icmp->type); |
| 901 | key->ipv4.tp.dst = htons(icmp->code); |
| 902 | } |
| 903 | } |
| 904 | |
Mehak Mahajan | c061853 | 2012-11-02 14:14:31 -0700 | [diff] [blame] | 905 | } else if ((key->eth.type == htons(ETH_P_ARP) || |
| 906 | key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 907 | struct arp_eth_header *arp; |
| 908 | |
| 909 | arp = (struct arp_eth_header *)skb_network_header(skb); |
| 910 | |
| 911 | if (arp->ar_hrd == htons(ARPHRD_ETHER) |
| 912 | && arp->ar_pro == htons(ETH_P_IP) |
| 913 | && arp->ar_hln == ETH_ALEN |
| 914 | && arp->ar_pln == 4) { |
| 915 | |
| 916 | /* We only match on the lower 8 bits of the opcode. */ |
| 917 | if (ntohs(arp->ar_op) <= 0xff) |
| 918 | key->ip.proto = ntohs(arp->ar_op); |
Mehak Mahajan | d04d382 | 2012-10-30 15:50:28 -0700 | [diff] [blame] | 919 | memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src)); |
| 920 | memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst)); |
| 921 | memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN); |
| 922 | memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 923 | } |
| 924 | } else if (key->eth.type == htons(ETH_P_IPV6)) { |
| 925 | int nh_len; /* IPv6 Header + Extensions */ |
| 926 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 927 | nh_len = parse_ipv6hdr(skb, key); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 928 | if (unlikely(nh_len < 0)) { |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 929 | if (nh_len == -EINVAL) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 930 | skb->transport_header = skb->network_header; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 931 | error = 0; |
| 932 | } else { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 933 | error = nh_len; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 934 | } |
| 935 | return error; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | if (key->ip.frag == OVS_FRAG_TYPE_LATER) |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 939 | return 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 940 | if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) |
| 941 | key->ip.frag = OVS_FRAG_TYPE_FIRST; |
| 942 | |
| 943 | /* Transport layer. */ |
| 944 | if (key->ip.proto == NEXTHDR_TCP) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 945 | if (tcphdr_ok(skb)) { |
| 946 | struct tcphdr *tcp = tcp_hdr(skb); |
| 947 | key->ipv6.tp.src = tcp->source; |
| 948 | key->ipv6.tp.dst = tcp->dest; |
| 949 | } |
| 950 | } else if (key->ip.proto == NEXTHDR_UDP) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 951 | if (udphdr_ok(skb)) { |
| 952 | struct udphdr *udp = udp_hdr(skb); |
| 953 | key->ipv6.tp.src = udp->source; |
| 954 | key->ipv6.tp.dst = udp->dest; |
| 955 | } |
| 956 | } else if (key->ip.proto == NEXTHDR_ICMP) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 957 | if (icmp6hdr_ok(skb)) { |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 958 | error = parse_icmpv6(skb, key, nh_len); |
| 959 | if (error) |
| 960 | return error; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | } |
| 964 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 965 | return 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 966 | } |
| 967 | |
Pravin B Shelar | a3e8299 | 2013-06-17 17:50:28 -0700 | [diff] [blame] | 968 | static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start, int key_len) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 969 | { |
Pravin B Shelar | a3e8299 | 2013-06-17 17:50:28 -0700 | [diff] [blame] | 970 | return jhash2((u32 *)((u8 *)key + key_start), |
| 971 | DIV_ROUND_UP(key_len - key_start, sizeof(u32)), 0); |
| 972 | } |
| 973 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 974 | static int flow_key_start(const struct sw_flow_key *key) |
Pravin B Shelar | a3e8299 | 2013-06-17 17:50:28 -0700 | [diff] [blame] | 975 | { |
| 976 | if (key->tun_key.ipv4_dst) |
| 977 | return 0; |
| 978 | else |
| 979 | return offsetof(struct sw_flow_key, phy); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 982 | static bool __cmp_key(const struct sw_flow_key *key1, |
| 983 | const struct sw_flow_key *key2, int key_start, int key_len) |
| 984 | { |
| 985 | return !memcmp((u8 *)key1 + key_start, |
| 986 | (u8 *)key2 + key_start, (key_len - key_start)); |
| 987 | } |
| 988 | |
| 989 | static bool __flow_cmp_key(const struct sw_flow *flow, |
| 990 | const struct sw_flow_key *key, int key_start, int key_len) |
| 991 | { |
| 992 | return __cmp_key(&flow->key, key, key_start, key_len); |
| 993 | } |
| 994 | |
| 995 | static bool __flow_cmp_unmasked_key(const struct sw_flow *flow, |
| 996 | const struct sw_flow_key *key, int key_start, int key_len) |
| 997 | { |
| 998 | return __cmp_key(&flow->unmasked_key, key, key_start, key_len); |
| 999 | } |
| 1000 | |
| 1001 | bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow, |
| 1002 | const struct sw_flow_key *key, int key_len) |
| 1003 | { |
| 1004 | int key_start; |
| 1005 | key_start = flow_key_start(key); |
| 1006 | |
| 1007 | return __flow_cmp_unmasked_key(flow, key, key_start, key_len); |
| 1008 | |
| 1009 | } |
| 1010 | |
| 1011 | struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table, |
| 1012 | struct sw_flow_match *match) |
| 1013 | { |
| 1014 | struct sw_flow_key *unmasked = match->key; |
| 1015 | int key_len = match->range.end; |
| 1016 | struct sw_flow *flow; |
| 1017 | |
| 1018 | flow = ovs_flow_lookup(table, unmasked); |
| 1019 | if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_len))) |
| 1020 | flow = NULL; |
| 1021 | |
| 1022 | return flow; |
| 1023 | } |
| 1024 | |
| 1025 | static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table, |
| 1026 | const struct sw_flow_key *flow_key, |
| 1027 | struct sw_flow_mask *mask) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1028 | { |
| 1029 | struct sw_flow *flow; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1030 | struct hlist_head *head; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1031 | int key_start = mask->range.start; |
| 1032 | int key_len = mask->range.end; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1033 | u32 hash; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1034 | struct sw_flow_key masked_key; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1035 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1036 | ovs_flow_key_mask(&masked_key, flow_key, mask); |
| 1037 | hash = ovs_flow_hash(&masked_key, key_start, key_len); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1038 | head = find_bucket(table, hash); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1039 | hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) { |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1040 | if (flow->mask == mask && |
| 1041 | __flow_cmp_key(flow, &masked_key, key_start, key_len)) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1042 | return flow; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1043 | } |
| 1044 | return NULL; |
| 1045 | } |
| 1046 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1047 | struct sw_flow *ovs_flow_lookup(struct flow_table *tbl, |
| 1048 | const struct sw_flow_key *key) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1049 | { |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1050 | struct sw_flow *flow = NULL; |
| 1051 | struct sw_flow_mask *mask; |
| 1052 | |
| 1053 | list_for_each_entry_rcu(mask, tbl->mask_list, list) { |
| 1054 | flow = ovs_masked_flow_lookup(tbl, key, mask); |
| 1055 | if (flow) /* Found */ |
| 1056 | break; |
| 1057 | } |
| 1058 | |
| 1059 | return flow; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1062 | |
| 1063 | void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow) |
| 1064 | { |
| 1065 | flow->hash = ovs_flow_hash(&flow->key, flow->mask->range.start, |
| 1066 | flow->mask->range.end); |
| 1067 | __tbl_insert(table, flow); |
| 1068 | } |
| 1069 | |
| 1070 | void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1071 | { |
Hong Zhiguo | d3e1101 | 2013-03-27 20:41:17 +0800 | [diff] [blame] | 1072 | BUG_ON(table->count == 0); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1073 | hlist_del_rcu(&flow->hash_node[table->node_ver]); |
| 1074 | table->count--; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */ |
| 1078 | const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = { |
| 1079 | [OVS_KEY_ATTR_ENCAP] = -1, |
| 1080 | [OVS_KEY_ATTR_PRIORITY] = sizeof(u32), |
| 1081 | [OVS_KEY_ATTR_IN_PORT] = sizeof(u32), |
Ansis Atteka | 39c7caeb | 2012-11-26 11:24:11 -0800 | [diff] [blame] | 1082 | [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32), |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1083 | [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet), |
| 1084 | [OVS_KEY_ATTR_VLAN] = sizeof(__be16), |
| 1085 | [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16), |
| 1086 | [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4), |
| 1087 | [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6), |
| 1088 | [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp), |
| 1089 | [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp), |
| 1090 | [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp), |
| 1091 | [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6), |
| 1092 | [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp), |
| 1093 | [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd), |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1094 | [OVS_KEY_ATTR_TUNNEL] = -1, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1095 | }; |
| 1096 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1097 | static bool is_all_zero(const u8 *fp, size_t size) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1098 | { |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1099 | int i; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1100 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1101 | if (!fp) |
| 1102 | return false; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1103 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1104 | for (i = 0; i < size; i++) |
| 1105 | if (fp[i]) |
| 1106 | return false; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1107 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1108 | return true; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1109 | } |
| 1110 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1111 | static int __parse_flow_nlattrs(const struct nlattr *attr, |
| 1112 | const struct nlattr *a[], |
| 1113 | u64 *attrsp, bool nz) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1114 | { |
| 1115 | const struct nlattr *nla; |
| 1116 | u32 attrs; |
| 1117 | int rem; |
| 1118 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1119 | attrs = *attrsp; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1120 | nla_for_each_nested(nla, attr, rem) { |
| 1121 | u16 type = nla_type(nla); |
| 1122 | int expected_len; |
| 1123 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1124 | if (type > OVS_KEY_ATTR_MAX) { |
| 1125 | OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n", |
| 1126 | type, OVS_KEY_ATTR_MAX); |
| 1127 | } |
| 1128 | |
| 1129 | if (attrs & (1 << type)) { |
| 1130 | OVS_NLERR("Duplicate key attribute (type %d).\n", type); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1131 | return -EINVAL; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1132 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1133 | |
| 1134 | expected_len = ovs_key_lens[type]; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1135 | if (nla_len(nla) != expected_len && expected_len != -1) { |
| 1136 | OVS_NLERR("Key attribute has unexpected length (type=%d" |
| 1137 | ", length=%d, expected=%d).\n", type, |
| 1138 | nla_len(nla), expected_len); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1139 | return -EINVAL; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1140 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1141 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1142 | if (!nz || !is_all_zero(nla_data(nla), expected_len)) { |
| 1143 | attrs |= 1 << type; |
| 1144 | a[type] = nla; |
| 1145 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1146 | } |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1147 | if (rem) { |
| 1148 | OVS_NLERR("Message has %d unknown bytes.\n", rem); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1149 | return -EINVAL; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1150 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1151 | |
| 1152 | *attrsp = attrs; |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1156 | static int parse_flow_mask_nlattrs(const struct nlattr *attr, |
| 1157 | const struct nlattr *a[], u64 *attrsp) |
| 1158 | { |
| 1159 | return __parse_flow_nlattrs(attr, a, attrsp, true); |
| 1160 | } |
| 1161 | |
| 1162 | static int parse_flow_nlattrs(const struct nlattr *attr, |
| 1163 | const struct nlattr *a[], u64 *attrsp) |
| 1164 | { |
| 1165 | return __parse_flow_nlattrs(attr, a, attrsp, false); |
| 1166 | } |
| 1167 | |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1168 | int ovs_ipv4_tun_from_nlattr(const struct nlattr *attr, |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1169 | struct sw_flow_match *match, bool is_mask) |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1170 | { |
| 1171 | struct nlattr *a; |
| 1172 | int rem; |
| 1173 | bool ttl = false; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1174 | __be16 tun_flags = 0; |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1175 | |
| 1176 | nla_for_each_nested(a, attr, rem) { |
| 1177 | int type = nla_type(a); |
| 1178 | static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = { |
| 1179 | [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64), |
| 1180 | [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32), |
| 1181 | [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32), |
| 1182 | [OVS_TUNNEL_KEY_ATTR_TOS] = 1, |
| 1183 | [OVS_TUNNEL_KEY_ATTR_TTL] = 1, |
| 1184 | [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0, |
| 1185 | [OVS_TUNNEL_KEY_ATTR_CSUM] = 0, |
| 1186 | }; |
| 1187 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1188 | if (type > OVS_TUNNEL_KEY_ATTR_MAX) { |
| 1189 | OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n", |
| 1190 | type, OVS_TUNNEL_KEY_ATTR_MAX); |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1191 | return -EINVAL; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1192 | } |
| 1193 | |
| 1194 | if (ovs_tunnel_key_lens[type] != nla_len(a)) { |
| 1195 | OVS_NLERR("IPv4 tunnel attribute type has unexpected " |
| 1196 | " length (type=%d, length=%d, expected=%d).\n", |
| 1197 | type, nla_len(a), ovs_tunnel_key_lens[type]); |
| 1198 | return -EINVAL; |
| 1199 | } |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1200 | |
| 1201 | switch (type) { |
| 1202 | case OVS_TUNNEL_KEY_ATTR_ID: |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1203 | SW_FLOW_KEY_PUT(match, tun_key.tun_id, |
| 1204 | nla_get_be64(a), is_mask); |
| 1205 | tun_flags |= TUNNEL_KEY; |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1206 | break; |
| 1207 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1208 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_src, |
| 1209 | nla_get_be32(a), is_mask); |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1210 | break; |
| 1211 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1212 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst, |
| 1213 | nla_get_be32(a), is_mask); |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1214 | break; |
| 1215 | case OVS_TUNNEL_KEY_ATTR_TOS: |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1216 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos, |
| 1217 | nla_get_u8(a), is_mask); |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1218 | break; |
| 1219 | case OVS_TUNNEL_KEY_ATTR_TTL: |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1220 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl, |
| 1221 | nla_get_u8(a), is_mask); |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1222 | ttl = true; |
| 1223 | break; |
| 1224 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1225 | tun_flags |= TUNNEL_DONT_FRAGMENT; |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1226 | break; |
| 1227 | case OVS_TUNNEL_KEY_ATTR_CSUM: |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1228 | tun_flags |= TUNNEL_CSUM; |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1229 | break; |
| 1230 | default: |
| 1231 | return -EINVAL; |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1232 | } |
| 1233 | } |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1234 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1235 | SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask); |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1236 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1237 | if (rem > 0) { |
| 1238 | OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem); |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1239 | return -EINVAL; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1240 | } |
| 1241 | |
| 1242 | if (!is_mask) { |
| 1243 | if (!match->key->tun_key.ipv4_dst) { |
| 1244 | OVS_NLERR("IPv4 tunnel destination address is zero.\n"); |
| 1245 | return -EINVAL; |
| 1246 | } |
| 1247 | |
| 1248 | if (!ttl) { |
| 1249 | OVS_NLERR("IPv4 tunnel TTL not specified.\n"); |
| 1250 | return -EINVAL; |
| 1251 | } |
| 1252 | } |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1253 | |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | int ovs_ipv4_tun_to_nlattr(struct sk_buff *skb, |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1258 | const struct ovs_key_ipv4_tunnel *tun_key, |
| 1259 | const struct ovs_key_ipv4_tunnel *output) |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1260 | { |
| 1261 | struct nlattr *nla; |
| 1262 | |
| 1263 | nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL); |
| 1264 | if (!nla) |
| 1265 | return -EMSGSIZE; |
| 1266 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1267 | if (output->tun_flags & TUNNEL_KEY && |
| 1268 | nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id)) |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1269 | return -EMSGSIZE; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1270 | if (output->ipv4_src && |
| 1271 | nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src)) |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1272 | return -EMSGSIZE; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1273 | if (output->ipv4_dst && |
| 1274 | nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst)) |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1275 | return -EMSGSIZE; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1276 | if (output->ipv4_tos && |
| 1277 | nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos)) |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1278 | return -EMSGSIZE; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1279 | if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl)) |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1280 | return -EMSGSIZE; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1281 | if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) && |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1282 | nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT)) |
| 1283 | return -EMSGSIZE; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1284 | if ((output->tun_flags & TUNNEL_CSUM) && |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1285 | nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM)) |
| 1286 | return -EMSGSIZE; |
| 1287 | |
| 1288 | nla_nest_end(skb, nla); |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1292 | static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs, |
| 1293 | const struct nlattr **a, bool is_mask) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1294 | { |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1295 | if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) { |
| 1296 | SW_FLOW_KEY_PUT(match, phy.priority, |
| 1297 | nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask); |
| 1298 | *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY); |
| 1299 | } |
| 1300 | |
| 1301 | if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) { |
| 1302 | u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]); |
| 1303 | |
| 1304 | if (is_mask) |
| 1305 | in_port = 0xffffffff; /* Always exact match in_port. */ |
| 1306 | else if (in_port >= DP_MAX_PORTS) |
| 1307 | return -EINVAL; |
| 1308 | |
| 1309 | SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask); |
| 1310 | *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT); |
| 1311 | } else if (!is_mask) { |
| 1312 | SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask); |
| 1313 | } |
| 1314 | |
| 1315 | if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) { |
| 1316 | uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]); |
| 1317 | |
| 1318 | SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask); |
| 1319 | *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK); |
| 1320 | } |
| 1321 | if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) { |
| 1322 | if (ovs_ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match, |
| 1323 | is_mask)) |
| 1324 | return -EINVAL; |
| 1325 | *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL); |
| 1326 | } |
| 1327 | return 0; |
| 1328 | } |
| 1329 | |
| 1330 | static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs, |
| 1331 | const struct nlattr **a, bool is_mask) |
| 1332 | { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1333 | int err; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1334 | u64 orig_attrs = attrs; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1335 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1336 | err = metadata_from_nlattrs(match, &attrs, a, is_mask); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1337 | if (err) |
| 1338 | return err; |
| 1339 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1340 | if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) { |
| 1341 | const struct ovs_key_ethernet *eth_key; |
| 1342 | |
| 1343 | eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]); |
| 1344 | SW_FLOW_KEY_MEMCPY(match, eth.src, |
| 1345 | eth_key->eth_src, ETH_ALEN, is_mask); |
| 1346 | SW_FLOW_KEY_MEMCPY(match, eth.dst, |
| 1347 | eth_key->eth_dst, ETH_ALEN, is_mask); |
| 1348 | attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET); |
Ansis Atteka | 39c7caeb | 2012-11-26 11:24:11 -0800 | [diff] [blame] | 1349 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1350 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1351 | if (attrs & (1 << OVS_KEY_ATTR_VLAN)) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1352 | __be16 tci; |
| 1353 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1354 | tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]); |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1355 | if (!(tci & htons(VLAN_TAG_PRESENT))) { |
| 1356 | if (is_mask) |
| 1357 | OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n"); |
| 1358 | else |
| 1359 | OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n"); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1360 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1361 | return -EINVAL; |
| 1362 | } |
| 1363 | |
| 1364 | SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask); |
| 1365 | attrs &= ~(1 << OVS_KEY_ATTR_VLAN); |
| 1366 | } else if (!is_mask) |
| 1367 | SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true); |
| 1368 | |
| 1369 | if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) { |
| 1370 | __be16 eth_type; |
| 1371 | |
| 1372 | eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]); |
| 1373 | if (is_mask) { |
| 1374 | /* Always exact match EtherType. */ |
| 1375 | eth_type = htons(0xffff); |
| 1376 | } else if (ntohs(eth_type) < ETH_P_802_3_MIN) { |
| 1377 | OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n", |
| 1378 | ntohs(eth_type), ETH_P_802_3_MIN); |
| 1379 | return -EINVAL; |
| 1380 | } |
| 1381 | |
| 1382 | SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask); |
| 1383 | attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE); |
| 1384 | } else if (!is_mask) { |
| 1385 | SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask); |
| 1386 | } |
| 1387 | |
| 1388 | if (attrs & (1 << OVS_KEY_ATTR_IPV4)) { |
| 1389 | const struct ovs_key_ipv4 *ipv4_key; |
| 1390 | |
| 1391 | ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]); |
| 1392 | if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) { |
| 1393 | OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n", |
| 1394 | ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX); |
| 1395 | return -EINVAL; |
| 1396 | } |
| 1397 | SW_FLOW_KEY_PUT(match, ip.proto, |
| 1398 | ipv4_key->ipv4_proto, is_mask); |
| 1399 | SW_FLOW_KEY_PUT(match, ip.tos, |
| 1400 | ipv4_key->ipv4_tos, is_mask); |
| 1401 | SW_FLOW_KEY_PUT(match, ip.ttl, |
| 1402 | ipv4_key->ipv4_ttl, is_mask); |
| 1403 | SW_FLOW_KEY_PUT(match, ip.frag, |
| 1404 | ipv4_key->ipv4_frag, is_mask); |
| 1405 | SW_FLOW_KEY_PUT(match, ipv4.addr.src, |
| 1406 | ipv4_key->ipv4_src, is_mask); |
| 1407 | SW_FLOW_KEY_PUT(match, ipv4.addr.dst, |
| 1408 | ipv4_key->ipv4_dst, is_mask); |
| 1409 | attrs &= ~(1 << OVS_KEY_ATTR_IPV4); |
| 1410 | } |
| 1411 | |
| 1412 | if (attrs & (1 << OVS_KEY_ATTR_IPV6)) { |
| 1413 | const struct ovs_key_ipv6 *ipv6_key; |
| 1414 | |
| 1415 | ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]); |
| 1416 | if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) { |
| 1417 | OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n", |
| 1418 | ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX); |
| 1419 | return -EINVAL; |
| 1420 | } |
| 1421 | SW_FLOW_KEY_PUT(match, ipv6.label, |
| 1422 | ipv6_key->ipv6_label, is_mask); |
| 1423 | SW_FLOW_KEY_PUT(match, ip.proto, |
| 1424 | ipv6_key->ipv6_proto, is_mask); |
| 1425 | SW_FLOW_KEY_PUT(match, ip.tos, |
| 1426 | ipv6_key->ipv6_tclass, is_mask); |
| 1427 | SW_FLOW_KEY_PUT(match, ip.ttl, |
| 1428 | ipv6_key->ipv6_hlimit, is_mask); |
| 1429 | SW_FLOW_KEY_PUT(match, ip.frag, |
| 1430 | ipv6_key->ipv6_frag, is_mask); |
| 1431 | SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src, |
| 1432 | ipv6_key->ipv6_src, |
| 1433 | sizeof(match->key->ipv6.addr.src), |
| 1434 | is_mask); |
| 1435 | SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst, |
| 1436 | ipv6_key->ipv6_dst, |
| 1437 | sizeof(match->key->ipv6.addr.dst), |
| 1438 | is_mask); |
| 1439 | |
| 1440 | attrs &= ~(1 << OVS_KEY_ATTR_IPV6); |
| 1441 | } |
| 1442 | |
| 1443 | if (attrs & (1 << OVS_KEY_ATTR_ARP)) { |
| 1444 | const struct ovs_key_arp *arp_key; |
| 1445 | |
| 1446 | arp_key = nla_data(a[OVS_KEY_ATTR_ARP]); |
| 1447 | if (!is_mask && (arp_key->arp_op & htons(0xff00))) { |
| 1448 | OVS_NLERR("Unknown ARP opcode (opcode=%d).\n", |
| 1449 | arp_key->arp_op); |
| 1450 | return -EINVAL; |
| 1451 | } |
| 1452 | |
| 1453 | SW_FLOW_KEY_PUT(match, ipv4.addr.src, |
| 1454 | arp_key->arp_sip, is_mask); |
| 1455 | SW_FLOW_KEY_PUT(match, ipv4.addr.dst, |
| 1456 | arp_key->arp_tip, is_mask); |
| 1457 | SW_FLOW_KEY_PUT(match, ip.proto, |
| 1458 | ntohs(arp_key->arp_op), is_mask); |
| 1459 | SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha, |
| 1460 | arp_key->arp_sha, ETH_ALEN, is_mask); |
| 1461 | SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha, |
| 1462 | arp_key->arp_tha, ETH_ALEN, is_mask); |
| 1463 | |
| 1464 | attrs &= ~(1 << OVS_KEY_ATTR_ARP); |
| 1465 | } |
| 1466 | |
| 1467 | if (attrs & (1 << OVS_KEY_ATTR_TCP)) { |
| 1468 | const struct ovs_key_tcp *tcp_key; |
| 1469 | |
| 1470 | tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]); |
| 1471 | if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) { |
| 1472 | SW_FLOW_KEY_PUT(match, ipv4.tp.src, |
| 1473 | tcp_key->tcp_src, is_mask); |
| 1474 | SW_FLOW_KEY_PUT(match, ipv4.tp.dst, |
| 1475 | tcp_key->tcp_dst, is_mask); |
| 1476 | } else { |
| 1477 | SW_FLOW_KEY_PUT(match, ipv6.tp.src, |
| 1478 | tcp_key->tcp_src, is_mask); |
| 1479 | SW_FLOW_KEY_PUT(match, ipv6.tp.dst, |
| 1480 | tcp_key->tcp_dst, is_mask); |
| 1481 | } |
| 1482 | attrs &= ~(1 << OVS_KEY_ATTR_TCP); |
| 1483 | } |
| 1484 | |
| 1485 | if (attrs & (1 << OVS_KEY_ATTR_UDP)) { |
| 1486 | const struct ovs_key_udp *udp_key; |
| 1487 | |
| 1488 | udp_key = nla_data(a[OVS_KEY_ATTR_UDP]); |
| 1489 | if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) { |
| 1490 | SW_FLOW_KEY_PUT(match, ipv4.tp.src, |
| 1491 | udp_key->udp_src, is_mask); |
| 1492 | SW_FLOW_KEY_PUT(match, ipv4.tp.dst, |
| 1493 | udp_key->udp_dst, is_mask); |
| 1494 | } else { |
| 1495 | SW_FLOW_KEY_PUT(match, ipv6.tp.src, |
| 1496 | udp_key->udp_src, is_mask); |
| 1497 | SW_FLOW_KEY_PUT(match, ipv6.tp.dst, |
| 1498 | udp_key->udp_dst, is_mask); |
| 1499 | } |
| 1500 | attrs &= ~(1 << OVS_KEY_ATTR_UDP); |
| 1501 | } |
| 1502 | |
| 1503 | if (attrs & (1 << OVS_KEY_ATTR_ICMP)) { |
| 1504 | const struct ovs_key_icmp *icmp_key; |
| 1505 | |
| 1506 | icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]); |
| 1507 | SW_FLOW_KEY_PUT(match, ipv4.tp.src, |
| 1508 | htons(icmp_key->icmp_type), is_mask); |
| 1509 | SW_FLOW_KEY_PUT(match, ipv4.tp.dst, |
| 1510 | htons(icmp_key->icmp_code), is_mask); |
| 1511 | attrs &= ~(1 << OVS_KEY_ATTR_ICMP); |
| 1512 | } |
| 1513 | |
| 1514 | if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) { |
| 1515 | const struct ovs_key_icmpv6 *icmpv6_key; |
| 1516 | |
| 1517 | icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]); |
| 1518 | SW_FLOW_KEY_PUT(match, ipv6.tp.src, |
| 1519 | htons(icmpv6_key->icmpv6_type), is_mask); |
| 1520 | SW_FLOW_KEY_PUT(match, ipv6.tp.dst, |
| 1521 | htons(icmpv6_key->icmpv6_code), is_mask); |
| 1522 | attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6); |
| 1523 | } |
| 1524 | |
| 1525 | if (attrs & (1 << OVS_KEY_ATTR_ND)) { |
| 1526 | const struct ovs_key_nd *nd_key; |
| 1527 | |
| 1528 | nd_key = nla_data(a[OVS_KEY_ATTR_ND]); |
| 1529 | SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target, |
| 1530 | nd_key->nd_target, |
| 1531 | sizeof(match->key->ipv6.nd.target), |
| 1532 | is_mask); |
| 1533 | SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll, |
| 1534 | nd_key->nd_sll, ETH_ALEN, is_mask); |
| 1535 | SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll, |
| 1536 | nd_key->nd_tll, ETH_ALEN, is_mask); |
| 1537 | attrs &= ~(1 << OVS_KEY_ATTR_ND); |
| 1538 | } |
| 1539 | |
| 1540 | if (attrs != 0) |
| 1541 | return -EINVAL; |
| 1542 | |
| 1543 | return 0; |
| 1544 | } |
| 1545 | |
| 1546 | /** |
| 1547 | * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and |
| 1548 | * mask. In case the 'mask' is NULL, the flow is treated as exact match |
| 1549 | * flow. Otherwise, it is treated as a wildcarded flow, except the mask |
| 1550 | * does not include any don't care bit. |
| 1551 | * @match: receives the extracted flow match information. |
| 1552 | * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute |
| 1553 | * sequence. The fields should of the packet that triggered the creation |
| 1554 | * of this flow. |
| 1555 | * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink |
| 1556 | * attribute specifies the mask field of the wildcarded flow. |
| 1557 | */ |
| 1558 | int ovs_match_from_nlattrs(struct sw_flow_match *match, |
| 1559 | const struct nlattr *key, |
| 1560 | const struct nlattr *mask) |
| 1561 | { |
| 1562 | const struct nlattr *a[OVS_KEY_ATTR_MAX + 1]; |
| 1563 | const struct nlattr *encap; |
| 1564 | u64 key_attrs = 0; |
| 1565 | u64 mask_attrs = 0; |
| 1566 | bool encap_valid = false; |
| 1567 | int err; |
| 1568 | |
| 1569 | err = parse_flow_nlattrs(key, a, &key_attrs); |
| 1570 | if (err) |
| 1571 | return err; |
| 1572 | |
| 1573 | if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) && |
| 1574 | (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) && |
| 1575 | (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) { |
| 1576 | __be16 tci; |
| 1577 | |
| 1578 | if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) && |
| 1579 | (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) { |
| 1580 | OVS_NLERR("Invalid Vlan frame.\n"); |
| 1581 | return -EINVAL; |
| 1582 | } |
| 1583 | |
| 1584 | key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE); |
| 1585 | tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]); |
| 1586 | encap = a[OVS_KEY_ATTR_ENCAP]; |
| 1587 | key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP); |
| 1588 | encap_valid = true; |
| 1589 | |
| 1590 | if (tci & htons(VLAN_TAG_PRESENT)) { |
| 1591 | err = parse_flow_nlattrs(encap, a, &key_attrs); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1592 | if (err) |
| 1593 | return err; |
| 1594 | } else if (!tci) { |
| 1595 | /* Corner case for truncated 802.1Q header. */ |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1596 | if (nla_len(encap)) { |
| 1597 | OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n"); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1598 | return -EINVAL; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1599 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1600 | } else { |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1601 | OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n"); |
| 1602 | return -EINVAL; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1603 | } |
| 1604 | } |
| 1605 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1606 | err = ovs_key_from_nlattrs(match, key_attrs, a, false); |
| 1607 | if (err) |
| 1608 | return err; |
| 1609 | |
| 1610 | if (mask) { |
| 1611 | err = parse_flow_mask_nlattrs(mask, a, &mask_attrs); |
| 1612 | if (err) |
| 1613 | return err; |
| 1614 | |
| 1615 | if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) { |
| 1616 | __be16 eth_type = 0; |
| 1617 | __be16 tci = 0; |
| 1618 | |
| 1619 | if (!encap_valid) { |
| 1620 | OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n"); |
| 1621 | return -EINVAL; |
| 1622 | } |
| 1623 | |
| 1624 | mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP); |
| 1625 | if (a[OVS_KEY_ATTR_ETHERTYPE]) |
| 1626 | eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]); |
| 1627 | |
| 1628 | if (eth_type == htons(0xffff)) { |
| 1629 | mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE); |
| 1630 | encap = a[OVS_KEY_ATTR_ENCAP]; |
| 1631 | err = parse_flow_mask_nlattrs(encap, a, &mask_attrs); |
| 1632 | } else { |
| 1633 | OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n", |
| 1634 | ntohs(eth_type)); |
| 1635 | return -EINVAL; |
| 1636 | } |
| 1637 | |
| 1638 | if (a[OVS_KEY_ATTR_VLAN]) |
| 1639 | tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]); |
| 1640 | |
| 1641 | if (!(tci & htons(VLAN_TAG_PRESENT))) { |
| 1642 | OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci)); |
| 1643 | return -EINVAL; |
| 1644 | } |
| 1645 | } |
| 1646 | |
| 1647 | err = ovs_key_from_nlattrs(match, mask_attrs, a, true); |
| 1648 | if (err) |
| 1649 | return err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1650 | } else { |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1651 | /* Populate exact match flow's key mask. */ |
| 1652 | if (match->mask) |
| 1653 | ovs_sw_flow_mask_set(match->mask, &match->range, 0xff); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1654 | } |
| 1655 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1656 | if (!ovs_match_validate(match, key_attrs, mask_attrs)) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1657 | return -EINVAL; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1658 | |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
| 1662 | /** |
| 1663 | * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key. |
Pravin B Shelar | 93d8fd1 | 2013-06-13 11:11:32 -0700 | [diff] [blame] | 1664 | * @flow: Receives extracted in_port, priority, tun_key and skb_mark. |
| 1665 | * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1666 | * sequence. |
| 1667 | * |
| 1668 | * This parses a series of Netlink attributes that form a flow key, which must |
| 1669 | * take the same form accepted by flow_from_nlattrs(), but only enough of it to |
| 1670 | * get the metadata, that is, the parts of the flow key that cannot be |
| 1671 | * extracted from the packet itself. |
| 1672 | */ |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1673 | |
| 1674 | int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow, |
| 1675 | const struct nlattr *attr) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1676 | { |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1677 | struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1678 | const struct nlattr *a[OVS_KEY_ATTR_MAX + 1]; |
| 1679 | u64 attrs = 0; |
| 1680 | int err; |
| 1681 | struct sw_flow_match match; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1682 | |
Pravin B Shelar | 93d8fd1 | 2013-06-13 11:11:32 -0700 | [diff] [blame] | 1683 | flow->key.phy.in_port = DP_MAX_PORTS; |
| 1684 | flow->key.phy.priority = 0; |
| 1685 | flow->key.phy.skb_mark = 0; |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1686 | memset(tun_key, 0, sizeof(flow->key.tun_key)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1687 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1688 | err = parse_flow_nlattrs(attr, a, &attrs); |
| 1689 | if (err) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1690 | return -EINVAL; |
Pravin B Shelar | a3e8299 | 2013-06-17 17:50:28 -0700 | [diff] [blame] | 1691 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1692 | memset(&match, 0, sizeof(match)); |
| 1693 | match.key = &flow->key; |
| 1694 | |
| 1695 | err = metadata_from_nlattrs(&match, &attrs, a, false); |
| 1696 | if (err) |
| 1697 | return err; |
Pravin B Shelar | a3e8299 | 2013-06-17 17:50:28 -0700 | [diff] [blame] | 1698 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1699 | return 0; |
| 1700 | } |
| 1701 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1702 | int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, |
| 1703 | const struct sw_flow_key *output, struct sk_buff *skb) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1704 | { |
| 1705 | struct ovs_key_ethernet *eth_key; |
| 1706 | struct nlattr *nla, *encap; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1707 | bool is_mask = (swkey != output); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1708 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1709 | if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority)) |
David S. Miller | 028d6a6 | 2012-03-29 23:20:48 -0400 | [diff] [blame] | 1710 | goto nla_put_failure; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1711 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1712 | if ((swkey->tun_key.ipv4_dst || is_mask) && |
| 1713 | ovs_ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key)) |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 1714 | goto nla_put_failure; |
| 1715 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1716 | if (swkey->phy.in_port == DP_MAX_PORTS) { |
| 1717 | if (is_mask && (output->phy.in_port == 0xffff)) |
| 1718 | if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff)) |
| 1719 | goto nla_put_failure; |
| 1720 | } else { |
| 1721 | u16 upper_u16; |
| 1722 | upper_u16 = !is_mask ? 0 : 0xffff; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1723 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1724 | if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, |
| 1725 | (upper_u16 << 16) | output->phy.in_port)) |
| 1726 | goto nla_put_failure; |
| 1727 | } |
| 1728 | |
| 1729 | if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark)) |
Ansis Atteka | 39c7caeb | 2012-11-26 11:24:11 -0800 | [diff] [blame] | 1730 | goto nla_put_failure; |
| 1731 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1732 | nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key)); |
| 1733 | if (!nla) |
| 1734 | goto nla_put_failure; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1735 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1736 | eth_key = nla_data(nla); |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1737 | memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN); |
| 1738 | memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1739 | |
| 1740 | if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) { |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1741 | __be16 eth_type; |
| 1742 | eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff); |
| 1743 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) || |
| 1744 | nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci)) |
David S. Miller | 028d6a6 | 2012-03-29 23:20:48 -0400 | [diff] [blame] | 1745 | goto nla_put_failure; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1746 | encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP); |
| 1747 | if (!swkey->eth.tci) |
| 1748 | goto unencap; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1749 | } else |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1750 | encap = NULL; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1751 | |
| 1752 | if (swkey->eth.type == htons(ETH_P_802_2)) { |
| 1753 | /* |
| 1754 | * Ethertype 802.2 is represented in the netlink with omitted |
| 1755 | * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and |
| 1756 | * 0xffff in the mask attribute. Ethertype can also |
| 1757 | * be wildcarded. |
| 1758 | */ |
| 1759 | if (is_mask && output->eth.type) |
| 1760 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, |
| 1761 | output->eth.type)) |
| 1762 | goto nla_put_failure; |
| 1763 | goto unencap; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1766 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type)) |
David S. Miller | 028d6a6 | 2012-03-29 23:20:48 -0400 | [diff] [blame] | 1767 | goto nla_put_failure; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1768 | |
| 1769 | if (swkey->eth.type == htons(ETH_P_IP)) { |
| 1770 | struct ovs_key_ipv4 *ipv4_key; |
| 1771 | |
| 1772 | nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key)); |
| 1773 | if (!nla) |
| 1774 | goto nla_put_failure; |
| 1775 | ipv4_key = nla_data(nla); |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1776 | ipv4_key->ipv4_src = output->ipv4.addr.src; |
| 1777 | ipv4_key->ipv4_dst = output->ipv4.addr.dst; |
| 1778 | ipv4_key->ipv4_proto = output->ip.proto; |
| 1779 | ipv4_key->ipv4_tos = output->ip.tos; |
| 1780 | ipv4_key->ipv4_ttl = output->ip.ttl; |
| 1781 | ipv4_key->ipv4_frag = output->ip.frag; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1782 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { |
| 1783 | struct ovs_key_ipv6 *ipv6_key; |
| 1784 | |
| 1785 | nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key)); |
| 1786 | if (!nla) |
| 1787 | goto nla_put_failure; |
| 1788 | ipv6_key = nla_data(nla); |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1789 | memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1790 | sizeof(ipv6_key->ipv6_src)); |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1791 | memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1792 | sizeof(ipv6_key->ipv6_dst)); |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1793 | ipv6_key->ipv6_label = output->ipv6.label; |
| 1794 | ipv6_key->ipv6_proto = output->ip.proto; |
| 1795 | ipv6_key->ipv6_tclass = output->ip.tos; |
| 1796 | ipv6_key->ipv6_hlimit = output->ip.ttl; |
| 1797 | ipv6_key->ipv6_frag = output->ip.frag; |
Mehak Mahajan | c061853 | 2012-11-02 14:14:31 -0700 | [diff] [blame] | 1798 | } else if (swkey->eth.type == htons(ETH_P_ARP) || |
| 1799 | swkey->eth.type == htons(ETH_P_RARP)) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1800 | struct ovs_key_arp *arp_key; |
| 1801 | |
| 1802 | nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key)); |
| 1803 | if (!nla) |
| 1804 | goto nla_put_failure; |
| 1805 | arp_key = nla_data(nla); |
| 1806 | memset(arp_key, 0, sizeof(struct ovs_key_arp)); |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1807 | arp_key->arp_sip = output->ipv4.addr.src; |
| 1808 | arp_key->arp_tip = output->ipv4.addr.dst; |
| 1809 | arp_key->arp_op = htons(output->ip.proto); |
| 1810 | memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN); |
| 1811 | memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | if ((swkey->eth.type == htons(ETH_P_IP) || |
| 1815 | swkey->eth.type == htons(ETH_P_IPV6)) && |
| 1816 | swkey->ip.frag != OVS_FRAG_TYPE_LATER) { |
| 1817 | |
| 1818 | if (swkey->ip.proto == IPPROTO_TCP) { |
| 1819 | struct ovs_key_tcp *tcp_key; |
| 1820 | |
| 1821 | nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key)); |
| 1822 | if (!nla) |
| 1823 | goto nla_put_failure; |
| 1824 | tcp_key = nla_data(nla); |
| 1825 | if (swkey->eth.type == htons(ETH_P_IP)) { |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1826 | tcp_key->tcp_src = output->ipv4.tp.src; |
| 1827 | tcp_key->tcp_dst = output->ipv4.tp.dst; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1828 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1829 | tcp_key->tcp_src = output->ipv6.tp.src; |
| 1830 | tcp_key->tcp_dst = output->ipv6.tp.dst; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1831 | } |
| 1832 | } else if (swkey->ip.proto == IPPROTO_UDP) { |
| 1833 | struct ovs_key_udp *udp_key; |
| 1834 | |
| 1835 | nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key)); |
| 1836 | if (!nla) |
| 1837 | goto nla_put_failure; |
| 1838 | udp_key = nla_data(nla); |
| 1839 | if (swkey->eth.type == htons(ETH_P_IP)) { |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1840 | udp_key->udp_src = output->ipv4.tp.src; |
| 1841 | udp_key->udp_dst = output->ipv4.tp.dst; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1842 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1843 | udp_key->udp_src = output->ipv6.tp.src; |
| 1844 | udp_key->udp_dst = output->ipv6.tp.dst; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1845 | } |
| 1846 | } else if (swkey->eth.type == htons(ETH_P_IP) && |
| 1847 | swkey->ip.proto == IPPROTO_ICMP) { |
| 1848 | struct ovs_key_icmp *icmp_key; |
| 1849 | |
| 1850 | nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key)); |
| 1851 | if (!nla) |
| 1852 | goto nla_put_failure; |
| 1853 | icmp_key = nla_data(nla); |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1854 | icmp_key->icmp_type = ntohs(output->ipv4.tp.src); |
| 1855 | icmp_key->icmp_code = ntohs(output->ipv4.tp.dst); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1856 | } else if (swkey->eth.type == htons(ETH_P_IPV6) && |
| 1857 | swkey->ip.proto == IPPROTO_ICMPV6) { |
| 1858 | struct ovs_key_icmpv6 *icmpv6_key; |
| 1859 | |
| 1860 | nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6, |
| 1861 | sizeof(*icmpv6_key)); |
| 1862 | if (!nla) |
| 1863 | goto nla_put_failure; |
| 1864 | icmpv6_key = nla_data(nla); |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1865 | icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src); |
| 1866 | icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1867 | |
| 1868 | if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION || |
| 1869 | icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) { |
| 1870 | struct ovs_key_nd *nd_key; |
| 1871 | |
| 1872 | nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key)); |
| 1873 | if (!nla) |
| 1874 | goto nla_put_failure; |
| 1875 | nd_key = nla_data(nla); |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1876 | memcpy(nd_key->nd_target, &output->ipv6.nd.target, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1877 | sizeof(nd_key->nd_target)); |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1878 | memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN); |
| 1879 | memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1880 | } |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | unencap: |
| 1885 | if (encap) |
| 1886 | nla_nest_end(skb, encap); |
| 1887 | |
| 1888 | return 0; |
| 1889 | |
| 1890 | nla_put_failure: |
| 1891 | return -EMSGSIZE; |
| 1892 | } |
| 1893 | |
| 1894 | /* Initializes the flow module. |
| 1895 | * Returns zero if successful or a negative error code. */ |
| 1896 | int ovs_flow_init(void) |
| 1897 | { |
| 1898 | flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0, |
| 1899 | 0, NULL); |
| 1900 | if (flow_cache == NULL) |
| 1901 | return -ENOMEM; |
| 1902 | |
| 1903 | return 0; |
| 1904 | } |
| 1905 | |
| 1906 | /* Uninitializes the flow module. */ |
| 1907 | void ovs_flow_exit(void) |
| 1908 | { |
| 1909 | kmem_cache_destroy(flow_cache); |
| 1910 | } |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame^] | 1911 | |
| 1912 | struct sw_flow_mask *ovs_sw_flow_mask_alloc(void) |
| 1913 | { |
| 1914 | struct sw_flow_mask *mask; |
| 1915 | |
| 1916 | mask = kmalloc(sizeof(*mask), GFP_KERNEL); |
| 1917 | if (mask) |
| 1918 | mask->ref_count = 0; |
| 1919 | |
| 1920 | return mask; |
| 1921 | } |
| 1922 | |
| 1923 | void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask) |
| 1924 | { |
| 1925 | mask->ref_count++; |
| 1926 | } |
| 1927 | |
| 1928 | void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred) |
| 1929 | { |
| 1930 | if (!mask) |
| 1931 | return; |
| 1932 | |
| 1933 | BUG_ON(!mask->ref_count); |
| 1934 | mask->ref_count--; |
| 1935 | |
| 1936 | if (!mask->ref_count) { |
| 1937 | list_del_rcu(&mask->list); |
| 1938 | if (deferred) |
| 1939 | kfree_rcu(mask, rcu); |
| 1940 | else |
| 1941 | kfree(mask); |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a, |
| 1946 | const struct sw_flow_mask *b) |
| 1947 | { |
| 1948 | u8 *a_ = (u8 *)&a->key + a->range.start; |
| 1949 | u8 *b_ = (u8 *)&b->key + b->range.start; |
| 1950 | |
| 1951 | return (a->range.end == b->range.end) |
| 1952 | && (a->range.start == b->range.start) |
| 1953 | && (memcmp(a_, b_, ovs_sw_flow_mask_actual_size(a)) == 0); |
| 1954 | } |
| 1955 | |
| 1956 | struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl, |
| 1957 | const struct sw_flow_mask *mask) |
| 1958 | { |
| 1959 | struct list_head *ml; |
| 1960 | |
| 1961 | list_for_each(ml, tbl->mask_list) { |
| 1962 | struct sw_flow_mask *m; |
| 1963 | m = container_of(ml, struct sw_flow_mask, list); |
| 1964 | if (ovs_sw_flow_mask_equal(mask, m)) |
| 1965 | return m; |
| 1966 | } |
| 1967 | |
| 1968 | return NULL; |
| 1969 | } |
| 1970 | |
| 1971 | /** |
| 1972 | * add a new mask into the mask list. |
| 1973 | * The caller needs to make sure that 'mask' is not the same |
| 1974 | * as any masks that are already on the list. |
| 1975 | */ |
| 1976 | void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask) |
| 1977 | { |
| 1978 | list_add_rcu(&mask->list, tbl->mask_list); |
| 1979 | } |
| 1980 | |
| 1981 | /** |
| 1982 | * Set 'range' fields in the mask to the value of 'val'. |
| 1983 | */ |
| 1984 | static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask, |
| 1985 | struct sw_flow_key_range *range, u8 val) |
| 1986 | { |
| 1987 | u8 *m = (u8 *)&mask->key + range->start; |
| 1988 | |
| 1989 | mask->range = *range; |
| 1990 | memset(m, val, ovs_sw_flow_mask_size_roundup(mask)); |
| 1991 | } |