blob: 80bcb96467a5802227b6f00bffc41a61eb067154 [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Andy Zhou03f0d912013-08-07 20:01:00 -07002 * Copyright (c) 2007-2013 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07003 *
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 Grossccb13522011-10-25 19:26:31 -070035#include <linux/ip.h>
36#include <linux/ipv6.h>
Joe Stringera175a722013-08-22 12:30:48 -070037#include <linux/sctp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070038#include <linux/tcp.h>
39#include <linux/udp.h>
40#include <linux/icmp.h>
41#include <linux/icmpv6.h>
42#include <linux/rculist.h>
43#include <net/ip.h>
Pravin B Shelar7d5437c2013-06-17 17:50:18 -070044#include <net/ip_tunnels.h>
Jesse Grossccb13522011-10-25 19:26:31 -070045#include <net/ipv6.h>
46#include <net/ndisc.h>
47
48static struct kmem_cache *flow_cache;
49
Andy Zhou03f0d912013-08-07 20:01:00 -070050static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
51 struct sw_flow_key_range *range, u8 val);
52
53static void update_range__(struct sw_flow_match *match,
54 size_t offset, size_t size, bool is_mask)
55{
56 struct sw_flow_key_range *range = NULL;
57 size_t start = offset;
58 size_t end = offset + size;
59
60 if (!is_mask)
61 range = &match->range;
62 else if (match->mask)
63 range = &match->mask->range;
64
65 if (!range)
66 return;
67
68 if (range->start == range->end) {
69 range->start = start;
70 range->end = end;
71 return;
72 }
73
74 if (range->start > start)
75 range->start = start;
76
77 if (range->end < end)
78 range->end = end;
79}
80
81#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
82 do { \
83 update_range__(match, offsetof(struct sw_flow_key, field), \
84 sizeof((match)->key->field), is_mask); \
85 if (is_mask) { \
86 if ((match)->mask) \
87 (match)->mask->key.field = value; \
88 } else { \
89 (match)->key->field = value; \
90 } \
91 } while (0)
92
93#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
94 do { \
95 update_range__(match, offsetof(struct sw_flow_key, field), \
96 len, is_mask); \
97 if (is_mask) { \
98 if ((match)->mask) \
99 memcpy(&(match)->mask->key.field, value_p, len);\
100 } else { \
101 memcpy(&(match)->key->field, value_p, len); \
102 } \
103 } while (0)
104
105void ovs_match_init(struct sw_flow_match *match,
106 struct sw_flow_key *key,
107 struct sw_flow_mask *mask)
108{
109 memset(match, 0, sizeof(*match));
110 match->key = key;
111 match->mask = mask;
112
113 memset(key, 0, sizeof(*key));
114
115 if (mask) {
116 memset(&mask->key, 0, sizeof(mask->key));
117 mask->range.start = mask->range.end = 0;
118 }
119}
120
121static bool ovs_match_validate(const struct sw_flow_match *match,
122 u64 key_attrs, u64 mask_attrs)
123{
124 u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET;
125 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
126
127 /* The following mask attributes allowed only if they
128 * pass the validation tests. */
129 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
130 | (1 << OVS_KEY_ATTR_IPV6)
131 | (1 << OVS_KEY_ATTR_TCP)
132 | (1 << OVS_KEY_ATTR_UDP)
Joe Stringera175a722013-08-22 12:30:48 -0700133 | (1 << OVS_KEY_ATTR_SCTP)
Andy Zhou03f0d912013-08-07 20:01:00 -0700134 | (1 << OVS_KEY_ATTR_ICMP)
135 | (1 << OVS_KEY_ATTR_ICMPV6)
136 | (1 << OVS_KEY_ATTR_ARP)
137 | (1 << OVS_KEY_ATTR_ND));
138
139 /* Always allowed mask fields. */
140 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
141 | (1 << OVS_KEY_ATTR_IN_PORT)
142 | (1 << OVS_KEY_ATTR_ETHERTYPE));
143
144 /* Check key attributes. */
145 if (match->key->eth.type == htons(ETH_P_ARP)
146 || match->key->eth.type == htons(ETH_P_RARP)) {
147 key_expected |= 1 << OVS_KEY_ATTR_ARP;
148 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
149 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
150 }
151
152 if (match->key->eth.type == htons(ETH_P_IP)) {
153 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
154 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
155 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
156
157 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
158 if (match->key->ip.proto == IPPROTO_UDP) {
159 key_expected |= 1 << OVS_KEY_ATTR_UDP;
160 if (match->mask && (match->mask->key.ip.proto == 0xff))
161 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
162 }
163
Joe Stringera175a722013-08-22 12:30:48 -0700164 if (match->key->ip.proto == IPPROTO_SCTP) {
165 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
166 if (match->mask && (match->mask->key.ip.proto == 0xff))
167 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
168 }
169
Andy Zhou03f0d912013-08-07 20:01:00 -0700170 if (match->key->ip.proto == IPPROTO_TCP) {
171 key_expected |= 1 << OVS_KEY_ATTR_TCP;
172 if (match->mask && (match->mask->key.ip.proto == 0xff))
173 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
174 }
175
176 if (match->key->ip.proto == IPPROTO_ICMP) {
177 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
178 if (match->mask && (match->mask->key.ip.proto == 0xff))
179 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
180 }
181 }
182 }
183
184 if (match->key->eth.type == htons(ETH_P_IPV6)) {
185 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
186 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
187 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
188
189 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
190 if (match->key->ip.proto == IPPROTO_UDP) {
191 key_expected |= 1 << OVS_KEY_ATTR_UDP;
192 if (match->mask && (match->mask->key.ip.proto == 0xff))
193 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
194 }
195
Joe Stringera175a722013-08-22 12:30:48 -0700196 if (match->key->ip.proto == IPPROTO_SCTP) {
197 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
198 if (match->mask && (match->mask->key.ip.proto == 0xff))
199 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
200 }
201
Andy Zhou03f0d912013-08-07 20:01:00 -0700202 if (match->key->ip.proto == IPPROTO_TCP) {
203 key_expected |= 1 << OVS_KEY_ATTR_TCP;
204 if (match->mask && (match->mask->key.ip.proto == 0xff))
205 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
206 }
207
208 if (match->key->ip.proto == IPPROTO_ICMPV6) {
209 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
210 if (match->mask && (match->mask->key.ip.proto == 0xff))
211 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
212
213 if (match->key->ipv6.tp.src ==
214 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
215 match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
216 key_expected |= 1 << OVS_KEY_ATTR_ND;
217 if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
218 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
219 }
220 }
221 }
222 }
223
224 if ((key_attrs & key_expected) != key_expected) {
225 /* Key attributes check failed. */
226 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
227 key_attrs, key_expected);
228 return false;
229 }
230
231 if ((mask_attrs & mask_allowed) != mask_attrs) {
232 /* Mask attributes check failed. */
233 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
234 mask_attrs, mask_allowed);
235 return false;
236 }
237
238 return true;
239}
240
Jesse Grossccb13522011-10-25 19:26:31 -0700241static int check_header(struct sk_buff *skb, int len)
242{
243 if (unlikely(skb->len < len))
244 return -EINVAL;
245 if (unlikely(!pskb_may_pull(skb, len)))
246 return -ENOMEM;
247 return 0;
248}
249
250static bool arphdr_ok(struct sk_buff *skb)
251{
252 return pskb_may_pull(skb, skb_network_offset(skb) +
253 sizeof(struct arp_eth_header));
254}
255
256static int check_iphdr(struct sk_buff *skb)
257{
258 unsigned int nh_ofs = skb_network_offset(skb);
259 unsigned int ip_len;
260 int err;
261
262 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
263 if (unlikely(err))
264 return err;
265
266 ip_len = ip_hdrlen(skb);
267 if (unlikely(ip_len < sizeof(struct iphdr) ||
268 skb->len < nh_ofs + ip_len))
269 return -EINVAL;
270
271 skb_set_transport_header(skb, nh_ofs + ip_len);
272 return 0;
273}
274
275static bool tcphdr_ok(struct sk_buff *skb)
276{
277 int th_ofs = skb_transport_offset(skb);
278 int tcp_len;
279
280 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
281 return false;
282
283 tcp_len = tcp_hdrlen(skb);
284 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
285 skb->len < th_ofs + tcp_len))
286 return false;
287
288 return true;
289}
290
291static bool udphdr_ok(struct sk_buff *skb)
292{
293 return pskb_may_pull(skb, skb_transport_offset(skb) +
294 sizeof(struct udphdr));
295}
296
Joe Stringera175a722013-08-22 12:30:48 -0700297static bool sctphdr_ok(struct sk_buff *skb)
298{
299 return pskb_may_pull(skb, skb_transport_offset(skb) +
300 sizeof(struct sctphdr));
301}
302
Jesse Grossccb13522011-10-25 19:26:31 -0700303static bool icmphdr_ok(struct sk_buff *skb)
304{
305 return pskb_may_pull(skb, skb_transport_offset(skb) +
306 sizeof(struct icmphdr));
307}
308
309u64 ovs_flow_used_time(unsigned long flow_jiffies)
310{
311 struct timespec cur_ts;
312 u64 cur_ms, idle_ms;
313
314 ktime_get_ts(&cur_ts);
315 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
316 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
317 cur_ts.tv_nsec / NSEC_PER_MSEC;
318
319 return cur_ms - idle_ms;
320}
321
Andy Zhou03f0d912013-08-07 20:01:00 -0700322static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700323{
324 unsigned int nh_ofs = skb_network_offset(skb);
325 unsigned int nh_len;
326 int payload_ofs;
327 struct ipv6hdr *nh;
328 uint8_t nexthdr;
329 __be16 frag_off;
330 int err;
331
Jesse Grossccb13522011-10-25 19:26:31 -0700332 err = check_header(skb, nh_ofs + sizeof(*nh));
333 if (unlikely(err))
334 return err;
335
336 nh = ipv6_hdr(skb);
337 nexthdr = nh->nexthdr;
338 payload_ofs = (u8 *)(nh + 1) - skb->data;
339
340 key->ip.proto = NEXTHDR_NONE;
341 key->ip.tos = ipv6_get_dsfield(nh);
342 key->ip.ttl = nh->hop_limit;
343 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
344 key->ipv6.addr.src = nh->saddr;
345 key->ipv6.addr.dst = nh->daddr;
346
347 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
348 if (unlikely(payload_ofs < 0))
349 return -EINVAL;
350
351 if (frag_off) {
352 if (frag_off & htons(~0x7))
353 key->ip.frag = OVS_FRAG_TYPE_LATER;
354 else
355 key->ip.frag = OVS_FRAG_TYPE_FIRST;
356 }
357
358 nh_len = payload_ofs - nh_ofs;
359 skb_set_transport_header(skb, nh_ofs + nh_len);
360 key->ip.proto = nexthdr;
361 return nh_len;
362}
363
364static bool icmp6hdr_ok(struct sk_buff *skb)
365{
366 return pskb_may_pull(skb, skb_transport_offset(skb) +
367 sizeof(struct icmp6hdr));
368}
369
Andy Zhou03f0d912013-08-07 20:01:00 -0700370void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src,
371 const struct sw_flow_mask *mask)
372{
373 u8 *m = (u8 *)&mask->key + mask->range.start;
374 u8 *s = (u8 *)src + mask->range.start;
375 u8 *d = (u8 *)dst + mask->range.start;
376 int i;
377
378 memset(dst, 0, sizeof(*dst));
379 for (i = 0; i < ovs_sw_flow_mask_size_roundup(mask); i++) {
380 *d = *s & *m;
381 d++, s++, m++;
382 }
383}
384
Jesse Grossccb13522011-10-25 19:26:31 -0700385#define TCP_FLAGS_OFFSET 13
386#define TCP_FLAG_MASK 0x3f
387
388void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
389{
390 u8 tcp_flags = 0;
391
Jesse Grossc55177e2012-04-02 15:13:36 -0700392 if ((flow->key.eth.type == htons(ETH_P_IP) ||
393 flow->key.eth.type == htons(ETH_P_IPV6)) &&
Jesse Grossbf32fec2012-04-02 14:26:27 -0700394 flow->key.ip.proto == IPPROTO_TCP &&
395 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
Jesse Grossccb13522011-10-25 19:26:31 -0700396 u8 *tcp = (u8 *)tcp_hdr(skb);
397 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
398 }
399
400 spin_lock(&flow->lock);
401 flow->used = jiffies;
402 flow->packet_count++;
403 flow->byte_count += skb->len;
404 flow->tcp_flags |= tcp_flags;
405 spin_unlock(&flow->lock);
406}
407
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700408struct sw_flow_actions *ovs_flow_actions_alloc(int size)
Jesse Grossccb13522011-10-25 19:26:31 -0700409{
Jesse Grossccb13522011-10-25 19:26:31 -0700410 struct sw_flow_actions *sfa;
411
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700412 if (size > MAX_ACTIONS_BUFSIZE)
Jesse Grossccb13522011-10-25 19:26:31 -0700413 return ERR_PTR(-EINVAL);
414
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700415 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -0700416 if (!sfa)
417 return ERR_PTR(-ENOMEM);
418
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700419 sfa->actions_len = 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700420 return sfa;
421}
422
423struct sw_flow *ovs_flow_alloc(void)
424{
425 struct sw_flow *flow;
426
427 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
428 if (!flow)
429 return ERR_PTR(-ENOMEM);
430
431 spin_lock_init(&flow->lock);
432 flow->sf_acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700433 flow->mask = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700434
435 return flow;
436}
437
438static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
439{
440 hash = jhash_1word(hash, table->hash_seed);
441 return flex_array_get(table->buckets,
442 (hash & (table->n_buckets - 1)));
443}
444
445static struct flex_array *alloc_buckets(unsigned int n_buckets)
446{
447 struct flex_array *buckets;
448 int i, err;
449
Pravin B Shelar42415c92013-07-30 15:44:14 -0700450 buckets = flex_array_alloc(sizeof(struct hlist_head),
Jesse Grossccb13522011-10-25 19:26:31 -0700451 n_buckets, GFP_KERNEL);
452 if (!buckets)
453 return NULL;
454
455 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
456 if (err) {
457 flex_array_free(buckets);
458 return NULL;
459 }
460
461 for (i = 0; i < n_buckets; i++)
462 INIT_HLIST_HEAD((struct hlist_head *)
463 flex_array_get(buckets, i));
464
465 return buckets;
466}
467
468static void free_buckets(struct flex_array *buckets)
469{
470 flex_array_free(buckets);
471}
472
Andy Zhou03f0d912013-08-07 20:01:00 -0700473static struct flow_table *__flow_tbl_alloc(int new_size)
Jesse Grossccb13522011-10-25 19:26:31 -0700474{
475 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
476
477 if (!table)
478 return NULL;
479
480 table->buckets = alloc_buckets(new_size);
481
482 if (!table->buckets) {
483 kfree(table);
484 return NULL;
485 }
486 table->n_buckets = new_size;
487 table->count = 0;
488 table->node_ver = 0;
489 table->keep_flows = false;
490 get_random_bytes(&table->hash_seed, sizeof(u32));
Andy Zhou03f0d912013-08-07 20:01:00 -0700491 table->mask_list = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700492
493 return table;
494}
495
Andy Zhou03f0d912013-08-07 20:01:00 -0700496static void __flow_tbl_destroy(struct flow_table *table)
Jesse Grossccb13522011-10-25 19:26:31 -0700497{
498 int i;
499
Jesse Grossccb13522011-10-25 19:26:31 -0700500 if (table->keep_flows)
501 goto skip_flows;
502
503 for (i = 0; i < table->n_buckets; i++) {
504 struct sw_flow *flow;
505 struct hlist_head *head = flex_array_get(table->buckets, i);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800506 struct hlist_node *n;
Jesse Grossccb13522011-10-25 19:26:31 -0700507 int ver = table->node_ver;
508
Sasha Levinb67bfe02013-02-27 17:06:00 -0800509 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
Pravin B Shelar76a66c72013-07-30 15:45:59 -0700510 hlist_del(&flow->hash_node[ver]);
Andy Zhou03f0d912013-08-07 20:01:00 -0700511 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700512 }
513 }
514
Andy Zhou03f0d912013-08-07 20:01:00 -0700515 BUG_ON(!list_empty(table->mask_list));
516 kfree(table->mask_list);
517
Jesse Grossccb13522011-10-25 19:26:31 -0700518skip_flows:
519 free_buckets(table->buckets);
520 kfree(table);
521}
522
Andy Zhou03f0d912013-08-07 20:01:00 -0700523struct flow_table *ovs_flow_tbl_alloc(int new_size)
524{
525 struct flow_table *table = __flow_tbl_alloc(new_size);
526
527 if (!table)
528 return NULL;
529
530 table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
531 if (!table->mask_list) {
532 table->keep_flows = true;
533 __flow_tbl_destroy(table);
534 return NULL;
535 }
536 INIT_LIST_HEAD(table->mask_list);
537
538 return table;
539}
540
Jesse Grossccb13522011-10-25 19:26:31 -0700541static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
542{
543 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
544
Andy Zhou03f0d912013-08-07 20:01:00 -0700545 __flow_tbl_destroy(table);
Jesse Grossccb13522011-10-25 19:26:31 -0700546}
547
Andy Zhou03f0d912013-08-07 20:01:00 -0700548void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
Jesse Grossccb13522011-10-25 19:26:31 -0700549{
550 if (!table)
551 return;
552
Andy Zhou03f0d912013-08-07 20:01:00 -0700553 if (deferred)
554 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
555 else
556 __flow_tbl_destroy(table);
Jesse Grossccb13522011-10-25 19:26:31 -0700557}
558
Andy Zhou03f0d912013-08-07 20:01:00 -0700559struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *last)
Jesse Grossccb13522011-10-25 19:26:31 -0700560{
561 struct sw_flow *flow;
562 struct hlist_head *head;
Jesse Grossccb13522011-10-25 19:26:31 -0700563 int ver;
564 int i;
565
566 ver = table->node_ver;
567 while (*bucket < table->n_buckets) {
568 i = 0;
569 head = flex_array_get(table->buckets, *bucket);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800570 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
Jesse Grossccb13522011-10-25 19:26:31 -0700571 if (i < *last) {
572 i++;
573 continue;
574 }
575 *last = i + 1;
576 return flow;
577 }
578 (*bucket)++;
579 *last = 0;
580 }
581
582 return NULL;
583}
584
Andy Zhou03f0d912013-08-07 20:01:00 -0700585static void __tbl_insert(struct flow_table *table, struct sw_flow *flow)
Pravin B Shelara3e82992013-06-17 17:50:28 -0700586{
587 struct hlist_head *head;
Andy Zhou03f0d912013-08-07 20:01:00 -0700588
Pravin B Shelara3e82992013-06-17 17:50:28 -0700589 head = find_bucket(table, flow->hash);
590 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
Andy Zhou03f0d912013-08-07 20:01:00 -0700591
Pravin B Shelara3e82992013-06-17 17:50:28 -0700592 table->count++;
593}
594
Jesse Grossccb13522011-10-25 19:26:31 -0700595static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
596{
597 int old_ver;
598 int i;
599
600 old_ver = old->node_ver;
601 new->node_ver = !old_ver;
602
603 /* Insert in new table. */
604 for (i = 0; i < old->n_buckets; i++) {
605 struct sw_flow *flow;
606 struct hlist_head *head;
Jesse Grossccb13522011-10-25 19:26:31 -0700607
608 head = flex_array_get(old->buckets, i);
609
Sasha Levinb67bfe02013-02-27 17:06:00 -0800610 hlist_for_each_entry(flow, head, hash_node[old_ver])
Andy Zhou03f0d912013-08-07 20:01:00 -0700611 __tbl_insert(new, flow);
Jesse Grossccb13522011-10-25 19:26:31 -0700612 }
Andy Zhou03f0d912013-08-07 20:01:00 -0700613
614 new->mask_list = old->mask_list;
Jesse Grossccb13522011-10-25 19:26:31 -0700615 old->keep_flows = true;
616}
617
618static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
619{
620 struct flow_table *new_table;
621
Andy Zhou03f0d912013-08-07 20:01:00 -0700622 new_table = __flow_tbl_alloc(n_buckets);
Jesse Grossccb13522011-10-25 19:26:31 -0700623 if (!new_table)
624 return ERR_PTR(-ENOMEM);
625
626 flow_table_copy_flows(table, new_table);
627
628 return new_table;
629}
630
631struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
632{
633 return __flow_tbl_rehash(table, table->n_buckets);
634}
635
636struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
637{
638 return __flow_tbl_rehash(table, table->n_buckets * 2);
639}
640
Andy Zhou03f0d912013-08-07 20:01:00 -0700641static void __flow_free(struct sw_flow *flow)
Jesse Grossccb13522011-10-25 19:26:31 -0700642{
Jesse Grossccb13522011-10-25 19:26:31 -0700643 kfree((struct sf_flow_acts __force *)flow->sf_acts);
644 kmem_cache_free(flow_cache, flow);
645}
646
Jesse Grossccb13522011-10-25 19:26:31 -0700647static void rcu_free_flow_callback(struct rcu_head *rcu)
648{
649 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
650
Andy Zhou03f0d912013-08-07 20:01:00 -0700651 __flow_free(flow);
Jesse Grossccb13522011-10-25 19:26:31 -0700652}
653
Andy Zhou03f0d912013-08-07 20:01:00 -0700654void ovs_flow_free(struct sw_flow *flow, bool deferred)
Jesse Grossccb13522011-10-25 19:26:31 -0700655{
Andy Zhou03f0d912013-08-07 20:01:00 -0700656 if (!flow)
657 return;
658
659 ovs_sw_flow_mask_del_ref(flow->mask, deferred);
660
661 if (deferred)
662 call_rcu(&flow->rcu, rcu_free_flow_callback);
663 else
664 __flow_free(flow);
Jesse Grossccb13522011-10-25 19:26:31 -0700665}
666
Jesse Grossccb13522011-10-25 19:26:31 -0700667/* Schedules 'sf_acts' to be freed after the next RCU grace period.
668 * The caller must hold rcu_read_lock for this to be sensible. */
669void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
670{
Wei Yongjun80f0fd82012-08-26 18:20:45 +0000671 kfree_rcu(sf_acts, rcu);
Jesse Grossccb13522011-10-25 19:26:31 -0700672}
673
674static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
675{
676 struct qtag_prefix {
677 __be16 eth_type; /* ETH_P_8021Q */
678 __be16 tci;
679 };
680 struct qtag_prefix *qp;
681
682 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
683 return 0;
684
685 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
686 sizeof(__be16))))
687 return -ENOMEM;
688
689 qp = (struct qtag_prefix *) skb->data;
690 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
691 __skb_pull(skb, sizeof(struct qtag_prefix));
692
693 return 0;
694}
695
696static __be16 parse_ethertype(struct sk_buff *skb)
697{
698 struct llc_snap_hdr {
699 u8 dsap; /* Always 0xAA */
700 u8 ssap; /* Always 0xAA */
701 u8 ctrl;
702 u8 oui[3];
703 __be16 ethertype;
704 };
705 struct llc_snap_hdr *llc;
706 __be16 proto;
707
708 proto = *(__be16 *) skb->data;
709 __skb_pull(skb, sizeof(__be16));
710
Simon Hormane5c5d222013-03-28 13:38:25 +0900711 if (ntohs(proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700712 return proto;
713
714 if (skb->len < sizeof(struct llc_snap_hdr))
715 return htons(ETH_P_802_2);
716
717 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
718 return htons(0);
719
720 llc = (struct llc_snap_hdr *) skb->data;
721 if (llc->dsap != LLC_SAP_SNAP ||
722 llc->ssap != LLC_SAP_SNAP ||
723 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
724 return htons(ETH_P_802_2);
725
726 __skb_pull(skb, sizeof(struct llc_snap_hdr));
Rich Lane17b682a2013-02-19 11:10:30 -0800727
Simon Hormane5c5d222013-03-28 13:38:25 +0900728 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
Rich Lane17b682a2013-02-19 11:10:30 -0800729 return llc->ethertype;
730
731 return htons(ETH_P_802_2);
Jesse Grossccb13522011-10-25 19:26:31 -0700732}
733
734static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
Andy Zhou03f0d912013-08-07 20:01:00 -0700735 int nh_len)
Jesse Grossccb13522011-10-25 19:26:31 -0700736{
737 struct icmp6hdr *icmp = icmp6_hdr(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700738
739 /* The ICMPv6 type and code fields use the 16-bit transport port
740 * fields, so we need to store them in 16-bit network byte order.
741 */
742 key->ipv6.tp.src = htons(icmp->icmp6_type);
743 key->ipv6.tp.dst = htons(icmp->icmp6_code);
Jesse Grossccb13522011-10-25 19:26:31 -0700744
745 if (icmp->icmp6_code == 0 &&
746 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
747 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
748 int icmp_len = skb->len - skb_transport_offset(skb);
749 struct nd_msg *nd;
750 int offset;
751
Jesse Grossccb13522011-10-25 19:26:31 -0700752 /* In order to process neighbor discovery options, we need the
753 * entire packet.
754 */
755 if (unlikely(icmp_len < sizeof(*nd)))
Andy Zhou03f0d912013-08-07 20:01:00 -0700756 return 0;
757
758 if (unlikely(skb_linearize(skb)))
759 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -0700760
761 nd = (struct nd_msg *)skb_transport_header(skb);
762 key->ipv6.nd.target = nd->target;
Jesse Grossccb13522011-10-25 19:26:31 -0700763
764 icmp_len -= sizeof(*nd);
765 offset = 0;
766 while (icmp_len >= 8) {
767 struct nd_opt_hdr *nd_opt =
768 (struct nd_opt_hdr *)(nd->opt + offset);
769 int opt_len = nd_opt->nd_opt_len * 8;
770
771 if (unlikely(!opt_len || opt_len > icmp_len))
Andy Zhou03f0d912013-08-07 20:01:00 -0700772 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700773
774 /* Store the link layer address if the appropriate
775 * option is provided. It is considered an error if
776 * the same link layer option is specified twice.
777 */
778 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
779 && opt_len == 8) {
780 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
781 goto invalid;
782 memcpy(key->ipv6.nd.sll,
783 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
784 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
785 && opt_len == 8) {
786 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
787 goto invalid;
788 memcpy(key->ipv6.nd.tll,
789 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
790 }
791
792 icmp_len -= opt_len;
793 offset += opt_len;
794 }
795 }
796
Andy Zhou03f0d912013-08-07 20:01:00 -0700797 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700798
799invalid:
800 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
801 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
802 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
803
Andy Zhou03f0d912013-08-07 20:01:00 -0700804 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700805}
806
807/**
808 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
809 * @skb: sk_buff that contains the frame, with skb->data pointing to the
810 * Ethernet header
811 * @in_port: port number on which @skb was received.
812 * @key: output flow key
Jesse Grossccb13522011-10-25 19:26:31 -0700813 *
814 * The caller must ensure that skb->len >= ETH_HLEN.
815 *
816 * Returns 0 if successful, otherwise a negative errno value.
817 *
818 * Initializes @skb header pointers as follows:
819 *
820 * - skb->mac_header: the Ethernet header.
821 *
822 * - skb->network_header: just past the Ethernet header, or just past the
823 * VLAN header, to the first byte of the Ethernet payload.
824 *
Lorand Jakab34d94f22013-06-03 10:01:14 -0700825 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
Jesse Grossccb13522011-10-25 19:26:31 -0700826 * on output, then just past the IP header, if one is present and
827 * of a correct length, otherwise the same as skb->network_header.
Lorand Jakab34d94f22013-06-03 10:01:14 -0700828 * For other key->eth.type values it is left untouched.
Jesse Grossccb13522011-10-25 19:26:31 -0700829 */
Andy Zhou03f0d912013-08-07 20:01:00 -0700830int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700831{
Andy Zhou03f0d912013-08-07 20:01:00 -0700832 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700833 struct ethhdr *eth;
834
835 memset(key, 0, sizeof(*key));
836
837 key->phy.priority = skb->priority;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700838 if (OVS_CB(skb)->tun_key)
839 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
Jesse Grossccb13522011-10-25 19:26:31 -0700840 key->phy.in_port = in_port;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800841 key->phy.skb_mark = skb->mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700842
843 skb_reset_mac_header(skb);
844
845 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
846 * header in the linear data area.
847 */
848 eth = eth_hdr(skb);
849 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
850 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
851
852 __skb_pull(skb, 2 * ETH_ALEN);
Pravin B Shelarb34df5e2013-06-13 11:11:44 -0700853 /* We are going to push all headers that we pull, so no need to
854 * update skb->csum here.
855 */
Jesse Grossccb13522011-10-25 19:26:31 -0700856
857 if (vlan_tx_tag_present(skb))
858 key->eth.tci = htons(skb->vlan_tci);
859 else if (eth->h_proto == htons(ETH_P_8021Q))
860 if (unlikely(parse_vlan(skb, key)))
861 return -ENOMEM;
862
863 key->eth.type = parse_ethertype(skb);
864 if (unlikely(key->eth.type == htons(0)))
865 return -ENOMEM;
866
867 skb_reset_network_header(skb);
868 __skb_push(skb, skb->data - skb_mac_header(skb));
869
870 /* Network layer. */
871 if (key->eth.type == htons(ETH_P_IP)) {
872 struct iphdr *nh;
873 __be16 offset;
874
Jesse Grossccb13522011-10-25 19:26:31 -0700875 error = check_iphdr(skb);
876 if (unlikely(error)) {
877 if (error == -EINVAL) {
878 skb->transport_header = skb->network_header;
879 error = 0;
880 }
Andy Zhou03f0d912013-08-07 20:01:00 -0700881 return error;
Jesse Grossccb13522011-10-25 19:26:31 -0700882 }
883
884 nh = ip_hdr(skb);
885 key->ipv4.addr.src = nh->saddr;
886 key->ipv4.addr.dst = nh->daddr;
887
888 key->ip.proto = nh->protocol;
889 key->ip.tos = nh->tos;
890 key->ip.ttl = nh->ttl;
891
892 offset = nh->frag_off & htons(IP_OFFSET);
893 if (offset) {
894 key->ip.frag = OVS_FRAG_TYPE_LATER;
Andy Zhou03f0d912013-08-07 20:01:00 -0700895 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700896 }
897 if (nh->frag_off & htons(IP_MF) ||
898 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
899 key->ip.frag = OVS_FRAG_TYPE_FIRST;
900
901 /* Transport layer. */
902 if (key->ip.proto == IPPROTO_TCP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700903 if (tcphdr_ok(skb)) {
904 struct tcphdr *tcp = tcp_hdr(skb);
905 key->ipv4.tp.src = tcp->source;
906 key->ipv4.tp.dst = tcp->dest;
907 }
908 } else if (key->ip.proto == IPPROTO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700909 if (udphdr_ok(skb)) {
910 struct udphdr *udp = udp_hdr(skb);
911 key->ipv4.tp.src = udp->source;
912 key->ipv4.tp.dst = udp->dest;
913 }
Joe Stringera175a722013-08-22 12:30:48 -0700914 } else if (key->ip.proto == IPPROTO_SCTP) {
915 if (sctphdr_ok(skb)) {
916 struct sctphdr *sctp = sctp_hdr(skb);
917 key->ipv4.tp.src = sctp->source;
918 key->ipv4.tp.dst = sctp->dest;
919 }
Jesse Grossccb13522011-10-25 19:26:31 -0700920 } else if (key->ip.proto == IPPROTO_ICMP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700921 if (icmphdr_ok(skb)) {
922 struct icmphdr *icmp = icmp_hdr(skb);
923 /* The ICMP type and code fields use the 16-bit
924 * transport port fields, so we need to store
925 * them in 16-bit network byte order. */
926 key->ipv4.tp.src = htons(icmp->type);
927 key->ipv4.tp.dst = htons(icmp->code);
928 }
929 }
930
Mehak Mahajanc0618532012-11-02 14:14:31 -0700931 } else if ((key->eth.type == htons(ETH_P_ARP) ||
932 key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
Jesse Grossccb13522011-10-25 19:26:31 -0700933 struct arp_eth_header *arp;
934
935 arp = (struct arp_eth_header *)skb_network_header(skb);
936
937 if (arp->ar_hrd == htons(ARPHRD_ETHER)
938 && arp->ar_pro == htons(ETH_P_IP)
939 && arp->ar_hln == ETH_ALEN
940 && arp->ar_pln == 4) {
941
942 /* We only match on the lower 8 bits of the opcode. */
943 if (ntohs(arp->ar_op) <= 0xff)
944 key->ip.proto = ntohs(arp->ar_op);
Mehak Mahajand04d3822012-10-30 15:50:28 -0700945 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
946 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
947 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
948 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
Jesse Grossccb13522011-10-25 19:26:31 -0700949 }
950 } else if (key->eth.type == htons(ETH_P_IPV6)) {
951 int nh_len; /* IPv6 Header + Extensions */
952
Andy Zhou03f0d912013-08-07 20:01:00 -0700953 nh_len = parse_ipv6hdr(skb, key);
Jesse Grossccb13522011-10-25 19:26:31 -0700954 if (unlikely(nh_len < 0)) {
Andy Zhou03f0d912013-08-07 20:01:00 -0700955 if (nh_len == -EINVAL) {
Jesse Grossccb13522011-10-25 19:26:31 -0700956 skb->transport_header = skb->network_header;
Andy Zhou03f0d912013-08-07 20:01:00 -0700957 error = 0;
958 } else {
Jesse Grossccb13522011-10-25 19:26:31 -0700959 error = nh_len;
Andy Zhou03f0d912013-08-07 20:01:00 -0700960 }
961 return error;
Jesse Grossccb13522011-10-25 19:26:31 -0700962 }
963
964 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
Andy Zhou03f0d912013-08-07 20:01:00 -0700965 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700966 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
967 key->ip.frag = OVS_FRAG_TYPE_FIRST;
968
969 /* Transport layer. */
970 if (key->ip.proto == NEXTHDR_TCP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700971 if (tcphdr_ok(skb)) {
972 struct tcphdr *tcp = tcp_hdr(skb);
973 key->ipv6.tp.src = tcp->source;
974 key->ipv6.tp.dst = tcp->dest;
975 }
976 } else if (key->ip.proto == NEXTHDR_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700977 if (udphdr_ok(skb)) {
978 struct udphdr *udp = udp_hdr(skb);
979 key->ipv6.tp.src = udp->source;
980 key->ipv6.tp.dst = udp->dest;
981 }
Joe Stringera175a722013-08-22 12:30:48 -0700982 } else if (key->ip.proto == NEXTHDR_SCTP) {
983 if (sctphdr_ok(skb)) {
984 struct sctphdr *sctp = sctp_hdr(skb);
985 key->ipv6.tp.src = sctp->source;
986 key->ipv6.tp.dst = sctp->dest;
987 }
Jesse Grossccb13522011-10-25 19:26:31 -0700988 } else if (key->ip.proto == NEXTHDR_ICMP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700989 if (icmp6hdr_ok(skb)) {
Andy Zhou03f0d912013-08-07 20:01:00 -0700990 error = parse_icmpv6(skb, key, nh_len);
991 if (error)
992 return error;
Jesse Grossccb13522011-10-25 19:26:31 -0700993 }
994 }
995 }
996
Andy Zhou03f0d912013-08-07 20:01:00 -0700997 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700998}
999
Andy Zhou02237372013-08-22 12:12:57 -07001000static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start,
1001 int key_end)
Jesse Grossccb13522011-10-25 19:26:31 -07001002{
Pravin B Shelara3e82992013-06-17 17:50:28 -07001003 return jhash2((u32 *)((u8 *)key + key_start),
Andy Zhou02237372013-08-22 12:12:57 -07001004 DIV_ROUND_UP(key_end - key_start, sizeof(u32)), 0);
Pravin B Shelara3e82992013-06-17 17:50:28 -07001005}
1006
Andy Zhou03f0d912013-08-07 20:01:00 -07001007static int flow_key_start(const struct sw_flow_key *key)
Pravin B Shelara3e82992013-06-17 17:50:28 -07001008{
1009 if (key->tun_key.ipv4_dst)
1010 return 0;
1011 else
1012 return offsetof(struct sw_flow_key, phy);
Jesse Grossccb13522011-10-25 19:26:31 -07001013}
1014
Andy Zhou03f0d912013-08-07 20:01:00 -07001015static bool __cmp_key(const struct sw_flow_key *key1,
Andy Zhou02237372013-08-22 12:12:57 -07001016 const struct sw_flow_key *key2, int key_start, int key_end)
Andy Zhou03f0d912013-08-07 20:01:00 -07001017{
1018 return !memcmp((u8 *)key1 + key_start,
Andy Zhou02237372013-08-22 12:12:57 -07001019 (u8 *)key2 + key_start, (key_end - key_start));
Andy Zhou03f0d912013-08-07 20:01:00 -07001020}
1021
1022static bool __flow_cmp_key(const struct sw_flow *flow,
Andy Zhou02237372013-08-22 12:12:57 -07001023 const struct sw_flow_key *key, int key_start, int key_end)
Andy Zhou03f0d912013-08-07 20:01:00 -07001024{
Andy Zhou02237372013-08-22 12:12:57 -07001025 return __cmp_key(&flow->key, key, key_start, key_end);
Andy Zhou03f0d912013-08-07 20:01:00 -07001026}
1027
1028static bool __flow_cmp_unmasked_key(const struct sw_flow *flow,
Andy Zhou02237372013-08-22 12:12:57 -07001029 const struct sw_flow_key *key, int key_start, int key_end)
Andy Zhou03f0d912013-08-07 20:01:00 -07001030{
Andy Zhou02237372013-08-22 12:12:57 -07001031 return __cmp_key(&flow->unmasked_key, key, key_start, key_end);
Andy Zhou03f0d912013-08-07 20:01:00 -07001032}
1033
1034bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
Andy Zhou02237372013-08-22 12:12:57 -07001035 const struct sw_flow_key *key, int key_end)
Andy Zhou03f0d912013-08-07 20:01:00 -07001036{
1037 int key_start;
1038 key_start = flow_key_start(key);
1039
Andy Zhou02237372013-08-22 12:12:57 -07001040 return __flow_cmp_unmasked_key(flow, key, key_start, key_end);
Andy Zhou03f0d912013-08-07 20:01:00 -07001041
1042}
1043
1044struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
1045 struct sw_flow_match *match)
1046{
1047 struct sw_flow_key *unmasked = match->key;
Andy Zhou02237372013-08-22 12:12:57 -07001048 int key_end = match->range.end;
Andy Zhou03f0d912013-08-07 20:01:00 -07001049 struct sw_flow *flow;
1050
1051 flow = ovs_flow_lookup(table, unmasked);
Andy Zhou02237372013-08-22 12:12:57 -07001052 if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_end)))
Andy Zhou03f0d912013-08-07 20:01:00 -07001053 flow = NULL;
1054
1055 return flow;
1056}
1057
1058static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
1059 const struct sw_flow_key *flow_key,
1060 struct sw_flow_mask *mask)
Jesse Grossccb13522011-10-25 19:26:31 -07001061{
1062 struct sw_flow *flow;
Jesse Grossccb13522011-10-25 19:26:31 -07001063 struct hlist_head *head;
Andy Zhou03f0d912013-08-07 20:01:00 -07001064 int key_start = mask->range.start;
Andy Zhou02237372013-08-22 12:12:57 -07001065 int key_end = mask->range.end;
Jesse Grossccb13522011-10-25 19:26:31 -07001066 u32 hash;
Andy Zhou03f0d912013-08-07 20:01:00 -07001067 struct sw_flow_key masked_key;
Jesse Grossccb13522011-10-25 19:26:31 -07001068
Andy Zhou03f0d912013-08-07 20:01:00 -07001069 ovs_flow_key_mask(&masked_key, flow_key, mask);
Andy Zhou02237372013-08-22 12:12:57 -07001070 hash = ovs_flow_hash(&masked_key, key_start, key_end);
Jesse Grossccb13522011-10-25 19:26:31 -07001071 head = find_bucket(table, hash);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001072 hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
Andy Zhou03f0d912013-08-07 20:01:00 -07001073 if (flow->mask == mask &&
Andy Zhou02237372013-08-22 12:12:57 -07001074 __flow_cmp_key(flow, &masked_key, key_start, key_end))
Jesse Grossccb13522011-10-25 19:26:31 -07001075 return flow;
Jesse Grossccb13522011-10-25 19:26:31 -07001076 }
1077 return NULL;
1078}
1079
Andy Zhou03f0d912013-08-07 20:01:00 -07001080struct sw_flow *ovs_flow_lookup(struct flow_table *tbl,
1081 const struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -07001082{
Andy Zhou03f0d912013-08-07 20:01:00 -07001083 struct sw_flow *flow = NULL;
1084 struct sw_flow_mask *mask;
1085
1086 list_for_each_entry_rcu(mask, tbl->mask_list, list) {
1087 flow = ovs_masked_flow_lookup(tbl, key, mask);
1088 if (flow) /* Found */
1089 break;
1090 }
1091
1092 return flow;
Jesse Grossccb13522011-10-25 19:26:31 -07001093}
1094
Andy Zhou03f0d912013-08-07 20:01:00 -07001095
1096void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow)
1097{
1098 flow->hash = ovs_flow_hash(&flow->key, flow->mask->range.start,
1099 flow->mask->range.end);
1100 __tbl_insert(table, flow);
1101}
1102
1103void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow)
Jesse Grossccb13522011-10-25 19:26:31 -07001104{
Hong Zhiguod3e11012013-03-27 20:41:17 +08001105 BUG_ON(table->count == 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001106 hlist_del_rcu(&flow->hash_node[table->node_ver]);
1107 table->count--;
Jesse Grossccb13522011-10-25 19:26:31 -07001108}
1109
1110/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
1111const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
1112 [OVS_KEY_ATTR_ENCAP] = -1,
1113 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
1114 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
Ansis Atteka39c7caeb2012-11-26 11:24:11 -08001115 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
Jesse Grossccb13522011-10-25 19:26:31 -07001116 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
1117 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
1118 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
1119 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
1120 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
1121 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
1122 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
Joe Stringera175a722013-08-22 12:30:48 -07001123 [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
Jesse Grossccb13522011-10-25 19:26:31 -07001124 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
1125 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
1126 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
1127 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001128 [OVS_KEY_ATTR_TUNNEL] = -1,
Jesse Grossccb13522011-10-25 19:26:31 -07001129};
1130
Andy Zhou03f0d912013-08-07 20:01:00 -07001131static bool is_all_zero(const u8 *fp, size_t size)
Jesse Grossccb13522011-10-25 19:26:31 -07001132{
Andy Zhou03f0d912013-08-07 20:01:00 -07001133 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001134
Andy Zhou03f0d912013-08-07 20:01:00 -07001135 if (!fp)
1136 return false;
Jesse Grossccb13522011-10-25 19:26:31 -07001137
Andy Zhou03f0d912013-08-07 20:01:00 -07001138 for (i = 0; i < size; i++)
1139 if (fp[i])
1140 return false;
Jesse Grossccb13522011-10-25 19:26:31 -07001141
Andy Zhou03f0d912013-08-07 20:01:00 -07001142 return true;
Jesse Grossccb13522011-10-25 19:26:31 -07001143}
1144
Andy Zhou03f0d912013-08-07 20:01:00 -07001145static int __parse_flow_nlattrs(const struct nlattr *attr,
1146 const struct nlattr *a[],
1147 u64 *attrsp, bool nz)
Jesse Grossccb13522011-10-25 19:26:31 -07001148{
1149 const struct nlattr *nla;
1150 u32 attrs;
1151 int rem;
1152
Andy Zhou03f0d912013-08-07 20:01:00 -07001153 attrs = *attrsp;
Jesse Grossccb13522011-10-25 19:26:31 -07001154 nla_for_each_nested(nla, attr, rem) {
1155 u16 type = nla_type(nla);
1156 int expected_len;
1157
Andy Zhou03f0d912013-08-07 20:01:00 -07001158 if (type > OVS_KEY_ATTR_MAX) {
1159 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
1160 type, OVS_KEY_ATTR_MAX);
1161 }
1162
1163 if (attrs & (1 << type)) {
1164 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
Jesse Grossccb13522011-10-25 19:26:31 -07001165 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001166 }
Jesse Grossccb13522011-10-25 19:26:31 -07001167
1168 expected_len = ovs_key_lens[type];
Andy Zhou03f0d912013-08-07 20:01:00 -07001169 if (nla_len(nla) != expected_len && expected_len != -1) {
1170 OVS_NLERR("Key attribute has unexpected length (type=%d"
1171 ", length=%d, expected=%d).\n", type,
1172 nla_len(nla), expected_len);
Jesse Grossccb13522011-10-25 19:26:31 -07001173 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001174 }
Jesse Grossccb13522011-10-25 19:26:31 -07001175
Andy Zhou03f0d912013-08-07 20:01:00 -07001176 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
1177 attrs |= 1 << type;
1178 a[type] = nla;
1179 }
Jesse Grossccb13522011-10-25 19:26:31 -07001180 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001181 if (rem) {
1182 OVS_NLERR("Message has %d unknown bytes.\n", rem);
Jesse Grossccb13522011-10-25 19:26:31 -07001183 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001184 }
Jesse Grossccb13522011-10-25 19:26:31 -07001185
1186 *attrsp = attrs;
1187 return 0;
1188}
1189
Andy Zhou03f0d912013-08-07 20:01:00 -07001190static int parse_flow_mask_nlattrs(const struct nlattr *attr,
1191 const struct nlattr *a[], u64 *attrsp)
1192{
1193 return __parse_flow_nlattrs(attr, a, attrsp, true);
1194}
1195
1196static int parse_flow_nlattrs(const struct nlattr *attr,
1197 const struct nlattr *a[], u64 *attrsp)
1198{
1199 return __parse_flow_nlattrs(attr, a, attrsp, false);
1200}
1201
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001202int ovs_ipv4_tun_from_nlattr(const struct nlattr *attr,
Andy Zhou03f0d912013-08-07 20:01:00 -07001203 struct sw_flow_match *match, bool is_mask)
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001204{
1205 struct nlattr *a;
1206 int rem;
1207 bool ttl = false;
Andy Zhou03f0d912013-08-07 20:01:00 -07001208 __be16 tun_flags = 0;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001209
1210 nla_for_each_nested(a, attr, rem) {
1211 int type = nla_type(a);
1212 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1213 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
1214 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
1215 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
1216 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
1217 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
1218 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
1219 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
1220 };
1221
Andy Zhou03f0d912013-08-07 20:01:00 -07001222 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
1223 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
1224 type, OVS_TUNNEL_KEY_ATTR_MAX);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001225 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001226 }
1227
1228 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
1229 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
1230 " length (type=%d, length=%d, expected=%d).\n",
1231 type, nla_len(a), ovs_tunnel_key_lens[type]);
1232 return -EINVAL;
1233 }
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001234
1235 switch (type) {
1236 case OVS_TUNNEL_KEY_ATTR_ID:
Andy Zhou03f0d912013-08-07 20:01:00 -07001237 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
1238 nla_get_be64(a), is_mask);
1239 tun_flags |= TUNNEL_KEY;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001240 break;
1241 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
Andy Zhou03f0d912013-08-07 20:01:00 -07001242 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
1243 nla_get_be32(a), is_mask);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001244 break;
1245 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
Andy Zhou03f0d912013-08-07 20:01:00 -07001246 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
1247 nla_get_be32(a), is_mask);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001248 break;
1249 case OVS_TUNNEL_KEY_ATTR_TOS:
Andy Zhou03f0d912013-08-07 20:01:00 -07001250 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
1251 nla_get_u8(a), is_mask);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001252 break;
1253 case OVS_TUNNEL_KEY_ATTR_TTL:
Andy Zhou03f0d912013-08-07 20:01:00 -07001254 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
1255 nla_get_u8(a), is_mask);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001256 ttl = true;
1257 break;
1258 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
Andy Zhou03f0d912013-08-07 20:01:00 -07001259 tun_flags |= TUNNEL_DONT_FRAGMENT;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001260 break;
1261 case OVS_TUNNEL_KEY_ATTR_CSUM:
Andy Zhou03f0d912013-08-07 20:01:00 -07001262 tun_flags |= TUNNEL_CSUM;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001263 break;
1264 default:
1265 return -EINVAL;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001266 }
1267 }
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001268
Andy Zhou03f0d912013-08-07 20:01:00 -07001269 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001270
Andy Zhou03f0d912013-08-07 20:01:00 -07001271 if (rem > 0) {
1272 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001273 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001274 }
1275
1276 if (!is_mask) {
1277 if (!match->key->tun_key.ipv4_dst) {
1278 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
1279 return -EINVAL;
1280 }
1281
1282 if (!ttl) {
1283 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
1284 return -EINVAL;
1285 }
1286 }
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001287
1288 return 0;
1289}
1290
1291int ovs_ipv4_tun_to_nlattr(struct sk_buff *skb,
Andy Zhou03f0d912013-08-07 20:01:00 -07001292 const struct ovs_key_ipv4_tunnel *tun_key,
1293 const struct ovs_key_ipv4_tunnel *output)
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001294{
1295 struct nlattr *nla;
1296
1297 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
1298 if (!nla)
1299 return -EMSGSIZE;
1300
Andy Zhou03f0d912013-08-07 20:01:00 -07001301 if (output->tun_flags & TUNNEL_KEY &&
1302 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001303 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -07001304 if (output->ipv4_src &&
1305 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001306 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -07001307 if (output->ipv4_dst &&
1308 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001309 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -07001310 if (output->ipv4_tos &&
1311 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001312 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -07001313 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001314 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -07001315 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001316 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
1317 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -07001318 if ((output->tun_flags & TUNNEL_CSUM) &&
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001319 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
1320 return -EMSGSIZE;
1321
1322 nla_nest_end(skb, nla);
1323 return 0;
1324}
1325
Andy Zhou03f0d912013-08-07 20:01:00 -07001326static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
1327 const struct nlattr **a, bool is_mask)
Jesse Grossccb13522011-10-25 19:26:31 -07001328{
Andy Zhou03f0d912013-08-07 20:01:00 -07001329 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1330 SW_FLOW_KEY_PUT(match, phy.priority,
1331 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1332 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1333 }
1334
1335 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1336 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1337
1338 if (is_mask)
1339 in_port = 0xffffffff; /* Always exact match in_port. */
1340 else if (in_port >= DP_MAX_PORTS)
1341 return -EINVAL;
1342
1343 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1344 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1345 } else if (!is_mask) {
1346 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1347 }
1348
1349 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1350 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1351
1352 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1353 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1354 }
1355 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
1356 if (ovs_ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1357 is_mask))
1358 return -EINVAL;
1359 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
1360 }
1361 return 0;
1362}
1363
1364static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
1365 const struct nlattr **a, bool is_mask)
1366{
Jesse Grossccb13522011-10-25 19:26:31 -07001367 int err;
Andy Zhou03f0d912013-08-07 20:01:00 -07001368 u64 orig_attrs = attrs;
Jesse Grossccb13522011-10-25 19:26:31 -07001369
Andy Zhou03f0d912013-08-07 20:01:00 -07001370 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
Jesse Grossccb13522011-10-25 19:26:31 -07001371 if (err)
1372 return err;
1373
Andy Zhou03f0d912013-08-07 20:01:00 -07001374 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
1375 const struct ovs_key_ethernet *eth_key;
1376
1377 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1378 SW_FLOW_KEY_MEMCPY(match, eth.src,
1379 eth_key->eth_src, ETH_ALEN, is_mask);
1380 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1381 eth_key->eth_dst, ETH_ALEN, is_mask);
1382 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
Ansis Atteka39c7caeb2012-11-26 11:24:11 -08001383 }
Jesse Grossccb13522011-10-25 19:26:31 -07001384
Andy Zhou03f0d912013-08-07 20:01:00 -07001385 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
Jesse Grossccb13522011-10-25 19:26:31 -07001386 __be16 tci;
1387
Jesse Grossccb13522011-10-25 19:26:31 -07001388 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
Andy Zhou03f0d912013-08-07 20:01:00 -07001389 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1390 if (is_mask)
1391 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
1392 else
1393 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001394
Andy Zhou03f0d912013-08-07 20:01:00 -07001395 return -EINVAL;
1396 }
1397
1398 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
1399 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
1400 } else if (!is_mask)
1401 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1402
1403 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1404 __be16 eth_type;
1405
1406 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1407 if (is_mask) {
1408 /* Always exact match EtherType. */
1409 eth_type = htons(0xffff);
1410 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
1411 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
1412 ntohs(eth_type), ETH_P_802_3_MIN);
1413 return -EINVAL;
1414 }
1415
1416 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1417 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1418 } else if (!is_mask) {
1419 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1420 }
1421
1422 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1423 const struct ovs_key_ipv4 *ipv4_key;
1424
1425 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1426 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
1427 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
1428 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
1429 return -EINVAL;
1430 }
1431 SW_FLOW_KEY_PUT(match, ip.proto,
1432 ipv4_key->ipv4_proto, is_mask);
1433 SW_FLOW_KEY_PUT(match, ip.tos,
1434 ipv4_key->ipv4_tos, is_mask);
1435 SW_FLOW_KEY_PUT(match, ip.ttl,
1436 ipv4_key->ipv4_ttl, is_mask);
1437 SW_FLOW_KEY_PUT(match, ip.frag,
1438 ipv4_key->ipv4_frag, is_mask);
1439 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1440 ipv4_key->ipv4_src, is_mask);
1441 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1442 ipv4_key->ipv4_dst, is_mask);
1443 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1444 }
1445
1446 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
1447 const struct ovs_key_ipv6 *ipv6_key;
1448
1449 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1450 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
1451 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
1452 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
1453 return -EINVAL;
1454 }
1455 SW_FLOW_KEY_PUT(match, ipv6.label,
1456 ipv6_key->ipv6_label, is_mask);
1457 SW_FLOW_KEY_PUT(match, ip.proto,
1458 ipv6_key->ipv6_proto, is_mask);
1459 SW_FLOW_KEY_PUT(match, ip.tos,
1460 ipv6_key->ipv6_tclass, is_mask);
1461 SW_FLOW_KEY_PUT(match, ip.ttl,
1462 ipv6_key->ipv6_hlimit, is_mask);
1463 SW_FLOW_KEY_PUT(match, ip.frag,
1464 ipv6_key->ipv6_frag, is_mask);
1465 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1466 ipv6_key->ipv6_src,
1467 sizeof(match->key->ipv6.addr.src),
1468 is_mask);
1469 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1470 ipv6_key->ipv6_dst,
1471 sizeof(match->key->ipv6.addr.dst),
1472 is_mask);
1473
1474 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1475 }
1476
1477 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
1478 const struct ovs_key_arp *arp_key;
1479
1480 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1481 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
1482 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
1483 arp_key->arp_op);
1484 return -EINVAL;
1485 }
1486
1487 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1488 arp_key->arp_sip, is_mask);
1489 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1490 arp_key->arp_tip, is_mask);
1491 SW_FLOW_KEY_PUT(match, ip.proto,
1492 ntohs(arp_key->arp_op), is_mask);
1493 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1494 arp_key->arp_sha, ETH_ALEN, is_mask);
1495 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1496 arp_key->arp_tha, ETH_ALEN, is_mask);
1497
1498 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1499 }
1500
1501 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1502 const struct ovs_key_tcp *tcp_key;
1503
1504 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1505 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1506 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1507 tcp_key->tcp_src, is_mask);
1508 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1509 tcp_key->tcp_dst, is_mask);
1510 } else {
1511 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1512 tcp_key->tcp_src, is_mask);
1513 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1514 tcp_key->tcp_dst, is_mask);
1515 }
1516 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1517 }
1518
1519 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1520 const struct ovs_key_udp *udp_key;
1521
1522 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1523 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1524 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1525 udp_key->udp_src, is_mask);
1526 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1527 udp_key->udp_dst, is_mask);
1528 } else {
1529 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1530 udp_key->udp_src, is_mask);
1531 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1532 udp_key->udp_dst, is_mask);
1533 }
1534 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1535 }
1536
Joe Stringera175a722013-08-22 12:30:48 -07001537 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1538 const struct ovs_key_sctp *sctp_key;
1539
1540 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
1541 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1542 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1543 sctp_key->sctp_src, is_mask);
1544 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1545 sctp_key->sctp_dst, is_mask);
1546 } else {
1547 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1548 sctp_key->sctp_src, is_mask);
1549 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1550 sctp_key->sctp_dst, is_mask);
1551 }
1552 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1553 }
1554
Andy Zhou03f0d912013-08-07 20:01:00 -07001555 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1556 const struct ovs_key_icmp *icmp_key;
1557
1558 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1559 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1560 htons(icmp_key->icmp_type), is_mask);
1561 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1562 htons(icmp_key->icmp_code), is_mask);
1563 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1564 }
1565
1566 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1567 const struct ovs_key_icmpv6 *icmpv6_key;
1568
1569 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1570 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1571 htons(icmpv6_key->icmpv6_type), is_mask);
1572 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1573 htons(icmpv6_key->icmpv6_code), is_mask);
1574 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1575 }
1576
1577 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1578 const struct ovs_key_nd *nd_key;
1579
1580 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1581 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1582 nd_key->nd_target,
1583 sizeof(match->key->ipv6.nd.target),
1584 is_mask);
1585 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1586 nd_key->nd_sll, ETH_ALEN, is_mask);
1587 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1588 nd_key->nd_tll, ETH_ALEN, is_mask);
1589 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1590 }
1591
1592 if (attrs != 0)
1593 return -EINVAL;
1594
1595 return 0;
1596}
1597
1598/**
1599 * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and
1600 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1601 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1602 * does not include any don't care bit.
1603 * @match: receives the extracted flow match information.
1604 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1605 * sequence. The fields should of the packet that triggered the creation
1606 * of this flow.
1607 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1608 * attribute specifies the mask field of the wildcarded flow.
1609 */
1610int ovs_match_from_nlattrs(struct sw_flow_match *match,
1611 const struct nlattr *key,
1612 const struct nlattr *mask)
1613{
1614 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1615 const struct nlattr *encap;
1616 u64 key_attrs = 0;
1617 u64 mask_attrs = 0;
1618 bool encap_valid = false;
1619 int err;
1620
1621 err = parse_flow_nlattrs(key, a, &key_attrs);
1622 if (err)
1623 return err;
1624
1625 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
1626 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
1627 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
1628 __be16 tci;
1629
1630 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
1631 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
1632 OVS_NLERR("Invalid Vlan frame.\n");
1633 return -EINVAL;
1634 }
1635
1636 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1637 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1638 encap = a[OVS_KEY_ATTR_ENCAP];
1639 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1640 encap_valid = true;
1641
1642 if (tci & htons(VLAN_TAG_PRESENT)) {
1643 err = parse_flow_nlattrs(encap, a, &key_attrs);
Jesse Grossccb13522011-10-25 19:26:31 -07001644 if (err)
1645 return err;
1646 } else if (!tci) {
1647 /* Corner case for truncated 802.1Q header. */
Andy Zhou03f0d912013-08-07 20:01:00 -07001648 if (nla_len(encap)) {
1649 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001650 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001651 }
Jesse Grossccb13522011-10-25 19:26:31 -07001652 } else {
Andy Zhou03f0d912013-08-07 20:01:00 -07001653 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
1654 return -EINVAL;
Jesse Grossccb13522011-10-25 19:26:31 -07001655 }
1656 }
1657
Andy Zhou03f0d912013-08-07 20:01:00 -07001658 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
1659 if (err)
1660 return err;
1661
1662 if (mask) {
1663 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
1664 if (err)
1665 return err;
1666
1667 if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
1668 __be16 eth_type = 0;
1669 __be16 tci = 0;
1670
1671 if (!encap_valid) {
1672 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
1673 return -EINVAL;
1674 }
1675
1676 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1677 if (a[OVS_KEY_ATTR_ETHERTYPE])
1678 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1679
1680 if (eth_type == htons(0xffff)) {
1681 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1682 encap = a[OVS_KEY_ATTR_ENCAP];
1683 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
1684 } else {
1685 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
1686 ntohs(eth_type));
1687 return -EINVAL;
1688 }
1689
1690 if (a[OVS_KEY_ATTR_VLAN])
1691 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1692
1693 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1694 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
1695 return -EINVAL;
1696 }
1697 }
1698
1699 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
1700 if (err)
1701 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001702 } else {
Andy Zhou03f0d912013-08-07 20:01:00 -07001703 /* Populate exact match flow's key mask. */
1704 if (match->mask)
1705 ovs_sw_flow_mask_set(match->mask, &match->range, 0xff);
Jesse Grossccb13522011-10-25 19:26:31 -07001706 }
1707
Andy Zhou03f0d912013-08-07 20:01:00 -07001708 if (!ovs_match_validate(match, key_attrs, mask_attrs))
Jesse Grossccb13522011-10-25 19:26:31 -07001709 return -EINVAL;
Jesse Grossccb13522011-10-25 19:26:31 -07001710
1711 return 0;
1712}
1713
1714/**
1715 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001716 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
1717 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
Jesse Grossccb13522011-10-25 19:26:31 -07001718 * sequence.
1719 *
1720 * This parses a series of Netlink attributes that form a flow key, which must
1721 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1722 * get the metadata, that is, the parts of the flow key that cannot be
1723 * extracted from the packet itself.
1724 */
Andy Zhou03f0d912013-08-07 20:01:00 -07001725
1726int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
1727 const struct nlattr *attr)
Jesse Grossccb13522011-10-25 19:26:31 -07001728{
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001729 struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
Andy Zhou03f0d912013-08-07 20:01:00 -07001730 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1731 u64 attrs = 0;
1732 int err;
1733 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001734
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001735 flow->key.phy.in_port = DP_MAX_PORTS;
1736 flow->key.phy.priority = 0;
1737 flow->key.phy.skb_mark = 0;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001738 memset(tun_key, 0, sizeof(flow->key.tun_key));
Jesse Grossccb13522011-10-25 19:26:31 -07001739
Andy Zhou03f0d912013-08-07 20:01:00 -07001740 err = parse_flow_nlattrs(attr, a, &attrs);
1741 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001742 return -EINVAL;
Pravin B Shelara3e82992013-06-17 17:50:28 -07001743
Andy Zhou03f0d912013-08-07 20:01:00 -07001744 memset(&match, 0, sizeof(match));
1745 match.key = &flow->key;
1746
1747 err = metadata_from_nlattrs(&match, &attrs, a, false);
1748 if (err)
1749 return err;
Pravin B Shelara3e82992013-06-17 17:50:28 -07001750
Jesse Grossccb13522011-10-25 19:26:31 -07001751 return 0;
1752}
1753
Andy Zhou03f0d912013-08-07 20:01:00 -07001754int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey,
1755 const struct sw_flow_key *output, struct sk_buff *skb)
Jesse Grossccb13522011-10-25 19:26:31 -07001756{
1757 struct ovs_key_ethernet *eth_key;
1758 struct nlattr *nla, *encap;
Andy Zhou03f0d912013-08-07 20:01:00 -07001759 bool is_mask = (swkey != output);
Jesse Grossccb13522011-10-25 19:26:31 -07001760
Andy Zhou03f0d912013-08-07 20:01:00 -07001761 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
David S. Miller028d6a62012-03-29 23:20:48 -04001762 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001763
Andy Zhou03f0d912013-08-07 20:01:00 -07001764 if ((swkey->tun_key.ipv4_dst || is_mask) &&
1765 ovs_ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001766 goto nla_put_failure;
1767
Andy Zhou03f0d912013-08-07 20:01:00 -07001768 if (swkey->phy.in_port == DP_MAX_PORTS) {
1769 if (is_mask && (output->phy.in_port == 0xffff))
1770 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1771 goto nla_put_failure;
1772 } else {
1773 u16 upper_u16;
1774 upper_u16 = !is_mask ? 0 : 0xffff;
Jesse Grossccb13522011-10-25 19:26:31 -07001775
Andy Zhou03f0d912013-08-07 20:01:00 -07001776 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1777 (upper_u16 << 16) | output->phy.in_port))
1778 goto nla_put_failure;
1779 }
1780
1781 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
Ansis Atteka39c7caeb2012-11-26 11:24:11 -08001782 goto nla_put_failure;
1783
Jesse Grossccb13522011-10-25 19:26:31 -07001784 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1785 if (!nla)
1786 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -07001787
Jesse Grossccb13522011-10-25 19:26:31 -07001788 eth_key = nla_data(nla);
Andy Zhou03f0d912013-08-07 20:01:00 -07001789 memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
1790 memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
Jesse Grossccb13522011-10-25 19:26:31 -07001791
1792 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
Andy Zhou03f0d912013-08-07 20:01:00 -07001793 __be16 eth_type;
1794 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1795 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1796 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
David S. Miller028d6a62012-03-29 23:20:48 -04001797 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001798 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1799 if (!swkey->eth.tci)
1800 goto unencap;
Andy Zhou03f0d912013-08-07 20:01:00 -07001801 } else
Jesse Grossccb13522011-10-25 19:26:31 -07001802 encap = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001803
1804 if (swkey->eth.type == htons(ETH_P_802_2)) {
1805 /*
1806 * Ethertype 802.2 is represented in the netlink with omitted
1807 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1808 * 0xffff in the mask attribute. Ethertype can also
1809 * be wildcarded.
1810 */
1811 if (is_mask && output->eth.type)
1812 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1813 output->eth.type))
1814 goto nla_put_failure;
1815 goto unencap;
Jesse Grossccb13522011-10-25 19:26:31 -07001816 }
1817
Andy Zhou03f0d912013-08-07 20:01:00 -07001818 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
David S. Miller028d6a62012-03-29 23:20:48 -04001819 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001820
1821 if (swkey->eth.type == htons(ETH_P_IP)) {
1822 struct ovs_key_ipv4 *ipv4_key;
1823
1824 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1825 if (!nla)
1826 goto nla_put_failure;
1827 ipv4_key = nla_data(nla);
Andy Zhou03f0d912013-08-07 20:01:00 -07001828 ipv4_key->ipv4_src = output->ipv4.addr.src;
1829 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1830 ipv4_key->ipv4_proto = output->ip.proto;
1831 ipv4_key->ipv4_tos = output->ip.tos;
1832 ipv4_key->ipv4_ttl = output->ip.ttl;
1833 ipv4_key->ipv4_frag = output->ip.frag;
Jesse Grossccb13522011-10-25 19:26:31 -07001834 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1835 struct ovs_key_ipv6 *ipv6_key;
1836
1837 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1838 if (!nla)
1839 goto nla_put_failure;
1840 ipv6_key = nla_data(nla);
Andy Zhou03f0d912013-08-07 20:01:00 -07001841 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
Jesse Grossccb13522011-10-25 19:26:31 -07001842 sizeof(ipv6_key->ipv6_src));
Andy Zhou03f0d912013-08-07 20:01:00 -07001843 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
Jesse Grossccb13522011-10-25 19:26:31 -07001844 sizeof(ipv6_key->ipv6_dst));
Andy Zhou03f0d912013-08-07 20:01:00 -07001845 ipv6_key->ipv6_label = output->ipv6.label;
1846 ipv6_key->ipv6_proto = output->ip.proto;
1847 ipv6_key->ipv6_tclass = output->ip.tos;
1848 ipv6_key->ipv6_hlimit = output->ip.ttl;
1849 ipv6_key->ipv6_frag = output->ip.frag;
Mehak Mahajanc0618532012-11-02 14:14:31 -07001850 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1851 swkey->eth.type == htons(ETH_P_RARP)) {
Jesse Grossccb13522011-10-25 19:26:31 -07001852 struct ovs_key_arp *arp_key;
1853
1854 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1855 if (!nla)
1856 goto nla_put_failure;
1857 arp_key = nla_data(nla);
1858 memset(arp_key, 0, sizeof(struct ovs_key_arp));
Andy Zhou03f0d912013-08-07 20:01:00 -07001859 arp_key->arp_sip = output->ipv4.addr.src;
1860 arp_key->arp_tip = output->ipv4.addr.dst;
1861 arp_key->arp_op = htons(output->ip.proto);
1862 memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN);
1863 memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN);
Jesse Grossccb13522011-10-25 19:26:31 -07001864 }
1865
1866 if ((swkey->eth.type == htons(ETH_P_IP) ||
1867 swkey->eth.type == htons(ETH_P_IPV6)) &&
1868 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1869
1870 if (swkey->ip.proto == IPPROTO_TCP) {
1871 struct ovs_key_tcp *tcp_key;
1872
1873 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1874 if (!nla)
1875 goto nla_put_failure;
1876 tcp_key = nla_data(nla);
1877 if (swkey->eth.type == htons(ETH_P_IP)) {
Andy Zhou03f0d912013-08-07 20:01:00 -07001878 tcp_key->tcp_src = output->ipv4.tp.src;
1879 tcp_key->tcp_dst = output->ipv4.tp.dst;
Jesse Grossccb13522011-10-25 19:26:31 -07001880 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
Andy Zhou03f0d912013-08-07 20:01:00 -07001881 tcp_key->tcp_src = output->ipv6.tp.src;
1882 tcp_key->tcp_dst = output->ipv6.tp.dst;
Jesse Grossccb13522011-10-25 19:26:31 -07001883 }
1884 } else if (swkey->ip.proto == IPPROTO_UDP) {
1885 struct ovs_key_udp *udp_key;
1886
1887 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1888 if (!nla)
1889 goto nla_put_failure;
1890 udp_key = nla_data(nla);
1891 if (swkey->eth.type == htons(ETH_P_IP)) {
Andy Zhou03f0d912013-08-07 20:01:00 -07001892 udp_key->udp_src = output->ipv4.tp.src;
1893 udp_key->udp_dst = output->ipv4.tp.dst;
Jesse Grossccb13522011-10-25 19:26:31 -07001894 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
Andy Zhou03f0d912013-08-07 20:01:00 -07001895 udp_key->udp_src = output->ipv6.tp.src;
1896 udp_key->udp_dst = output->ipv6.tp.dst;
Jesse Grossccb13522011-10-25 19:26:31 -07001897 }
Joe Stringera175a722013-08-22 12:30:48 -07001898 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1899 struct ovs_key_sctp *sctp_key;
1900
1901 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1902 if (!nla)
1903 goto nla_put_failure;
1904 sctp_key = nla_data(nla);
1905 if (swkey->eth.type == htons(ETH_P_IP)) {
1906 sctp_key->sctp_src = swkey->ipv4.tp.src;
1907 sctp_key->sctp_dst = swkey->ipv4.tp.dst;
1908 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1909 sctp_key->sctp_src = swkey->ipv6.tp.src;
1910 sctp_key->sctp_dst = swkey->ipv6.tp.dst;
1911 }
Jesse Grossccb13522011-10-25 19:26:31 -07001912 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1913 swkey->ip.proto == IPPROTO_ICMP) {
1914 struct ovs_key_icmp *icmp_key;
1915
1916 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1917 if (!nla)
1918 goto nla_put_failure;
1919 icmp_key = nla_data(nla);
Andy Zhou03f0d912013-08-07 20:01:00 -07001920 icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
1921 icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
Jesse Grossccb13522011-10-25 19:26:31 -07001922 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1923 swkey->ip.proto == IPPROTO_ICMPV6) {
1924 struct ovs_key_icmpv6 *icmpv6_key;
1925
1926 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1927 sizeof(*icmpv6_key));
1928 if (!nla)
1929 goto nla_put_failure;
1930 icmpv6_key = nla_data(nla);
Andy Zhou03f0d912013-08-07 20:01:00 -07001931 icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
1932 icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
Jesse Grossccb13522011-10-25 19:26:31 -07001933
1934 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1935 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1936 struct ovs_key_nd *nd_key;
1937
1938 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1939 if (!nla)
1940 goto nla_put_failure;
1941 nd_key = nla_data(nla);
Andy Zhou03f0d912013-08-07 20:01:00 -07001942 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
Jesse Grossccb13522011-10-25 19:26:31 -07001943 sizeof(nd_key->nd_target));
Andy Zhou03f0d912013-08-07 20:01:00 -07001944 memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN);
1945 memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN);
Jesse Grossccb13522011-10-25 19:26:31 -07001946 }
1947 }
1948 }
1949
1950unencap:
1951 if (encap)
1952 nla_nest_end(skb, encap);
1953
1954 return 0;
1955
1956nla_put_failure:
1957 return -EMSGSIZE;
1958}
1959
1960/* Initializes the flow module.
1961 * Returns zero if successful or a negative error code. */
1962int ovs_flow_init(void)
1963{
1964 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1965 0, NULL);
1966 if (flow_cache == NULL)
1967 return -ENOMEM;
1968
1969 return 0;
1970}
1971
1972/* Uninitializes the flow module. */
1973void ovs_flow_exit(void)
1974{
1975 kmem_cache_destroy(flow_cache);
1976}
Andy Zhou03f0d912013-08-07 20:01:00 -07001977
1978struct sw_flow_mask *ovs_sw_flow_mask_alloc(void)
1979{
1980 struct sw_flow_mask *mask;
1981
1982 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
1983 if (mask)
1984 mask->ref_count = 0;
1985
1986 return mask;
1987}
1988
1989void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask)
1990{
1991 mask->ref_count++;
1992}
1993
1994void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
1995{
1996 if (!mask)
1997 return;
1998
1999 BUG_ON(!mask->ref_count);
2000 mask->ref_count--;
2001
2002 if (!mask->ref_count) {
2003 list_del_rcu(&mask->list);
2004 if (deferred)
2005 kfree_rcu(mask, rcu);
2006 else
2007 kfree(mask);
2008 }
2009}
2010
2011static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a,
2012 const struct sw_flow_mask *b)
2013{
2014 u8 *a_ = (u8 *)&a->key + a->range.start;
2015 u8 *b_ = (u8 *)&b->key + b->range.start;
2016
2017 return (a->range.end == b->range.end)
2018 && (a->range.start == b->range.start)
2019 && (memcmp(a_, b_, ovs_sw_flow_mask_actual_size(a)) == 0);
2020}
2021
2022struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
2023 const struct sw_flow_mask *mask)
2024{
2025 struct list_head *ml;
2026
2027 list_for_each(ml, tbl->mask_list) {
2028 struct sw_flow_mask *m;
2029 m = container_of(ml, struct sw_flow_mask, list);
2030 if (ovs_sw_flow_mask_equal(mask, m))
2031 return m;
2032 }
2033
2034 return NULL;
2035}
2036
2037/**
2038 * add a new mask into the mask list.
2039 * The caller needs to make sure that 'mask' is not the same
2040 * as any masks that are already on the list.
2041 */
2042void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask)
2043{
2044 list_add_rcu(&mask->list, tbl->mask_list);
2045}
2046
2047/**
2048 * Set 'range' fields in the mask to the value of 'val'.
2049 */
2050static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
2051 struct sw_flow_key_range *range, u8 val)
2052{
2053 u8 *m = (u8 *)&mask->key + range->start;
2054
2055 mask->range = *range;
2056 memset(m, val, ovs_sw_flow_mask_size_roundup(mask));
2057}