blob: 093c191d4fc2571e0a34079d0f0d99cdb50b51de [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Raju Subramaniancaf2ee12012-05-03 18:55:23 -07002 * Copyright (c) 2007-2011 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>
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>
43#include <net/ipv6.h>
44#include <net/ndisc.h>
45
46static struct kmem_cache *flow_cache;
47
48static int check_header(struct sk_buff *skb, int len)
49{
50 if (unlikely(skb->len < len))
51 return -EINVAL;
52 if (unlikely(!pskb_may_pull(skb, len)))
53 return -ENOMEM;
54 return 0;
55}
56
57static bool arphdr_ok(struct sk_buff *skb)
58{
59 return pskb_may_pull(skb, skb_network_offset(skb) +
60 sizeof(struct arp_eth_header));
61}
62
63static int check_iphdr(struct sk_buff *skb)
64{
65 unsigned int nh_ofs = skb_network_offset(skb);
66 unsigned int ip_len;
67 int err;
68
69 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
70 if (unlikely(err))
71 return err;
72
73 ip_len = ip_hdrlen(skb);
74 if (unlikely(ip_len < sizeof(struct iphdr) ||
75 skb->len < nh_ofs + ip_len))
76 return -EINVAL;
77
78 skb_set_transport_header(skb, nh_ofs + ip_len);
79 return 0;
80}
81
82static bool tcphdr_ok(struct sk_buff *skb)
83{
84 int th_ofs = skb_transport_offset(skb);
85 int tcp_len;
86
87 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
88 return false;
89
90 tcp_len = tcp_hdrlen(skb);
91 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
92 skb->len < th_ofs + tcp_len))
93 return false;
94
95 return true;
96}
97
98static bool udphdr_ok(struct sk_buff *skb)
99{
100 return pskb_may_pull(skb, skb_transport_offset(skb) +
101 sizeof(struct udphdr));
102}
103
104static bool icmphdr_ok(struct sk_buff *skb)
105{
106 return pskb_may_pull(skb, skb_transport_offset(skb) +
107 sizeof(struct icmphdr));
108}
109
110u64 ovs_flow_used_time(unsigned long flow_jiffies)
111{
112 struct timespec cur_ts;
113 u64 cur_ms, idle_ms;
114
115 ktime_get_ts(&cur_ts);
116 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
117 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
118 cur_ts.tv_nsec / NSEC_PER_MSEC;
119
120 return cur_ms - idle_ms;
121}
122
123#define SW_FLOW_KEY_OFFSET(field) \
124 (offsetof(struct sw_flow_key, field) + \
125 FIELD_SIZEOF(struct sw_flow_key, field))
126
127static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key,
128 int *key_lenp)
129{
130 unsigned int nh_ofs = skb_network_offset(skb);
131 unsigned int nh_len;
132 int payload_ofs;
133 struct ipv6hdr *nh;
134 uint8_t nexthdr;
135 __be16 frag_off;
136 int err;
137
138 *key_lenp = SW_FLOW_KEY_OFFSET(ipv6.label);
139
140 err = check_header(skb, nh_ofs + sizeof(*nh));
141 if (unlikely(err))
142 return err;
143
144 nh = ipv6_hdr(skb);
145 nexthdr = nh->nexthdr;
146 payload_ofs = (u8 *)(nh + 1) - skb->data;
147
148 key->ip.proto = NEXTHDR_NONE;
149 key->ip.tos = ipv6_get_dsfield(nh);
150 key->ip.ttl = nh->hop_limit;
151 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
152 key->ipv6.addr.src = nh->saddr;
153 key->ipv6.addr.dst = nh->daddr;
154
155 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
156 if (unlikely(payload_ofs < 0))
157 return -EINVAL;
158
159 if (frag_off) {
160 if (frag_off & htons(~0x7))
161 key->ip.frag = OVS_FRAG_TYPE_LATER;
162 else
163 key->ip.frag = OVS_FRAG_TYPE_FIRST;
164 }
165
166 nh_len = payload_ofs - nh_ofs;
167 skb_set_transport_header(skb, nh_ofs + nh_len);
168 key->ip.proto = nexthdr;
169 return nh_len;
170}
171
172static bool icmp6hdr_ok(struct sk_buff *skb)
173{
174 return pskb_may_pull(skb, skb_transport_offset(skb) +
175 sizeof(struct icmp6hdr));
176}
177
178#define TCP_FLAGS_OFFSET 13
179#define TCP_FLAG_MASK 0x3f
180
181void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
182{
183 u8 tcp_flags = 0;
184
Jesse Grossc55177e2012-04-02 15:13:36 -0700185 if ((flow->key.eth.type == htons(ETH_P_IP) ||
186 flow->key.eth.type == htons(ETH_P_IPV6)) &&
Jesse Grossbf32fec2012-04-02 14:26:27 -0700187 flow->key.ip.proto == IPPROTO_TCP &&
188 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
Jesse Grossccb13522011-10-25 19:26:31 -0700189 u8 *tcp = (u8 *)tcp_hdr(skb);
190 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
191 }
192
193 spin_lock(&flow->lock);
194 flow->used = jiffies;
195 flow->packet_count++;
196 flow->byte_count += skb->len;
197 flow->tcp_flags |= tcp_flags;
198 spin_unlock(&flow->lock);
199}
200
201struct sw_flow_actions *ovs_flow_actions_alloc(const struct nlattr *actions)
202{
203 int actions_len = nla_len(actions);
204 struct sw_flow_actions *sfa;
205
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700206 if (actions_len > MAX_ACTIONS_BUFSIZE)
Jesse Grossccb13522011-10-25 19:26:31 -0700207 return ERR_PTR(-EINVAL);
208
209 sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL);
210 if (!sfa)
211 return ERR_PTR(-ENOMEM);
212
213 sfa->actions_len = actions_len;
Thomas Graf32686a92013-03-29 14:46:48 +0100214 nla_memcpy(sfa->actions, actions, actions_len);
Jesse Grossccb13522011-10-25 19:26:31 -0700215 return sfa;
216}
217
218struct sw_flow *ovs_flow_alloc(void)
219{
220 struct sw_flow *flow;
221
222 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
223 if (!flow)
224 return ERR_PTR(-ENOMEM);
225
226 spin_lock_init(&flow->lock);
227 flow->sf_acts = NULL;
228
229 return flow;
230}
231
232static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
233{
234 hash = jhash_1word(hash, table->hash_seed);
235 return flex_array_get(table->buckets,
236 (hash & (table->n_buckets - 1)));
237}
238
239static struct flex_array *alloc_buckets(unsigned int n_buckets)
240{
241 struct flex_array *buckets;
242 int i, err;
243
244 buckets = flex_array_alloc(sizeof(struct hlist_head *),
245 n_buckets, GFP_KERNEL);
246 if (!buckets)
247 return NULL;
248
249 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
250 if (err) {
251 flex_array_free(buckets);
252 return NULL;
253 }
254
255 for (i = 0; i < n_buckets; i++)
256 INIT_HLIST_HEAD((struct hlist_head *)
257 flex_array_get(buckets, i));
258
259 return buckets;
260}
261
262static void free_buckets(struct flex_array *buckets)
263{
264 flex_array_free(buckets);
265}
266
267struct flow_table *ovs_flow_tbl_alloc(int new_size)
268{
269 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
270
271 if (!table)
272 return NULL;
273
274 table->buckets = alloc_buckets(new_size);
275
276 if (!table->buckets) {
277 kfree(table);
278 return NULL;
279 }
280 table->n_buckets = new_size;
281 table->count = 0;
282 table->node_ver = 0;
283 table->keep_flows = false;
284 get_random_bytes(&table->hash_seed, sizeof(u32));
285
286 return table;
287}
288
289void ovs_flow_tbl_destroy(struct flow_table *table)
290{
291 int i;
292
293 if (!table)
294 return;
295
296 if (table->keep_flows)
297 goto skip_flows;
298
299 for (i = 0; i < table->n_buckets; i++) {
300 struct sw_flow *flow;
301 struct hlist_head *head = flex_array_get(table->buckets, i);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800302 struct hlist_node *n;
Jesse Grossccb13522011-10-25 19:26:31 -0700303 int ver = table->node_ver;
304
Sasha Levinb67bfe02013-02-27 17:06:00 -0800305 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
Jesse Grossccb13522011-10-25 19:26:31 -0700306 hlist_del_rcu(&flow->hash_node[ver]);
307 ovs_flow_free(flow);
308 }
309 }
310
311skip_flows:
312 free_buckets(table->buckets);
313 kfree(table);
314}
315
316static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
317{
318 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
319
320 ovs_flow_tbl_destroy(table);
321}
322
323void ovs_flow_tbl_deferred_destroy(struct flow_table *table)
324{
325 if (!table)
326 return;
327
328 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
329}
330
331struct sw_flow *ovs_flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last)
332{
333 struct sw_flow *flow;
334 struct hlist_head *head;
Jesse Grossccb13522011-10-25 19:26:31 -0700335 int ver;
336 int i;
337
338 ver = table->node_ver;
339 while (*bucket < table->n_buckets) {
340 i = 0;
341 head = flex_array_get(table->buckets, *bucket);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800342 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
Jesse Grossccb13522011-10-25 19:26:31 -0700343 if (i < *last) {
344 i++;
345 continue;
346 }
347 *last = i + 1;
348 return flow;
349 }
350 (*bucket)++;
351 *last = 0;
352 }
353
354 return NULL;
355}
356
357static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
358{
359 int old_ver;
360 int i;
361
362 old_ver = old->node_ver;
363 new->node_ver = !old_ver;
364
365 /* Insert in new table. */
366 for (i = 0; i < old->n_buckets; i++) {
367 struct sw_flow *flow;
368 struct hlist_head *head;
Jesse Grossccb13522011-10-25 19:26:31 -0700369
370 head = flex_array_get(old->buckets, i);
371
Sasha Levinb67bfe02013-02-27 17:06:00 -0800372 hlist_for_each_entry(flow, head, hash_node[old_ver])
Jesse Grossccb13522011-10-25 19:26:31 -0700373 ovs_flow_tbl_insert(new, flow);
374 }
375 old->keep_flows = true;
376}
377
378static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
379{
380 struct flow_table *new_table;
381
382 new_table = ovs_flow_tbl_alloc(n_buckets);
383 if (!new_table)
384 return ERR_PTR(-ENOMEM);
385
386 flow_table_copy_flows(table, new_table);
387
388 return new_table;
389}
390
391struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
392{
393 return __flow_tbl_rehash(table, table->n_buckets);
394}
395
396struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
397{
398 return __flow_tbl_rehash(table, table->n_buckets * 2);
399}
400
401void ovs_flow_free(struct sw_flow *flow)
402{
403 if (unlikely(!flow))
404 return;
405
406 kfree((struct sf_flow_acts __force *)flow->sf_acts);
407 kmem_cache_free(flow_cache, flow);
408}
409
410/* RCU callback used by ovs_flow_deferred_free. */
411static void rcu_free_flow_callback(struct rcu_head *rcu)
412{
413 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
414
415 ovs_flow_free(flow);
416}
417
418/* Schedules 'flow' to be freed after the next RCU grace period.
419 * The caller must hold rcu_read_lock for this to be sensible. */
420void ovs_flow_deferred_free(struct sw_flow *flow)
421{
422 call_rcu(&flow->rcu, rcu_free_flow_callback);
423}
424
Jesse Grossccb13522011-10-25 19:26:31 -0700425/* Schedules 'sf_acts' to be freed after the next RCU grace period.
426 * The caller must hold rcu_read_lock for this to be sensible. */
427void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
428{
Wei Yongjun80f0fd82012-08-26 18:20:45 +0000429 kfree_rcu(sf_acts, rcu);
Jesse Grossccb13522011-10-25 19:26:31 -0700430}
431
432static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
433{
434 struct qtag_prefix {
435 __be16 eth_type; /* ETH_P_8021Q */
436 __be16 tci;
437 };
438 struct qtag_prefix *qp;
439
440 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
441 return 0;
442
443 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
444 sizeof(__be16))))
445 return -ENOMEM;
446
447 qp = (struct qtag_prefix *) skb->data;
448 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
449 __skb_pull(skb, sizeof(struct qtag_prefix));
450
451 return 0;
452}
453
454static __be16 parse_ethertype(struct sk_buff *skb)
455{
456 struct llc_snap_hdr {
457 u8 dsap; /* Always 0xAA */
458 u8 ssap; /* Always 0xAA */
459 u8 ctrl;
460 u8 oui[3];
461 __be16 ethertype;
462 };
463 struct llc_snap_hdr *llc;
464 __be16 proto;
465
466 proto = *(__be16 *) skb->data;
467 __skb_pull(skb, sizeof(__be16));
468
Simon Hormane5c5d222013-03-28 13:38:25 +0900469 if (ntohs(proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700470 return proto;
471
472 if (skb->len < sizeof(struct llc_snap_hdr))
473 return htons(ETH_P_802_2);
474
475 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
476 return htons(0);
477
478 llc = (struct llc_snap_hdr *) skb->data;
479 if (llc->dsap != LLC_SAP_SNAP ||
480 llc->ssap != LLC_SAP_SNAP ||
481 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
482 return htons(ETH_P_802_2);
483
484 __skb_pull(skb, sizeof(struct llc_snap_hdr));
Rich Lane17b682a2013-02-19 11:10:30 -0800485
Simon Hormane5c5d222013-03-28 13:38:25 +0900486 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
Rich Lane17b682a2013-02-19 11:10:30 -0800487 return llc->ethertype;
488
489 return htons(ETH_P_802_2);
Jesse Grossccb13522011-10-25 19:26:31 -0700490}
491
492static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
493 int *key_lenp, int nh_len)
494{
495 struct icmp6hdr *icmp = icmp6_hdr(skb);
496 int error = 0;
497 int key_len;
498
499 /* The ICMPv6 type and code fields use the 16-bit transport port
500 * fields, so we need to store them in 16-bit network byte order.
501 */
502 key->ipv6.tp.src = htons(icmp->icmp6_type);
503 key->ipv6.tp.dst = htons(icmp->icmp6_code);
504 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
505
506 if (icmp->icmp6_code == 0 &&
507 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
508 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
509 int icmp_len = skb->len - skb_transport_offset(skb);
510 struct nd_msg *nd;
511 int offset;
512
513 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
514
515 /* In order to process neighbor discovery options, we need the
516 * entire packet.
517 */
518 if (unlikely(icmp_len < sizeof(*nd)))
519 goto out;
520 if (unlikely(skb_linearize(skb))) {
521 error = -ENOMEM;
522 goto out;
523 }
524
525 nd = (struct nd_msg *)skb_transport_header(skb);
526 key->ipv6.nd.target = nd->target;
527 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
528
529 icmp_len -= sizeof(*nd);
530 offset = 0;
531 while (icmp_len >= 8) {
532 struct nd_opt_hdr *nd_opt =
533 (struct nd_opt_hdr *)(nd->opt + offset);
534 int opt_len = nd_opt->nd_opt_len * 8;
535
536 if (unlikely(!opt_len || opt_len > icmp_len))
537 goto invalid;
538
539 /* Store the link layer address if the appropriate
540 * option is provided. It is considered an error if
541 * the same link layer option is specified twice.
542 */
543 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
544 && opt_len == 8) {
545 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
546 goto invalid;
547 memcpy(key->ipv6.nd.sll,
548 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
549 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
550 && opt_len == 8) {
551 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
552 goto invalid;
553 memcpy(key->ipv6.nd.tll,
554 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
555 }
556
557 icmp_len -= opt_len;
558 offset += opt_len;
559 }
560 }
561
562 goto out;
563
564invalid:
565 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
566 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
567 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
568
569out:
570 *key_lenp = key_len;
571 return error;
572}
573
574/**
575 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
576 * @skb: sk_buff that contains the frame, with skb->data pointing to the
577 * Ethernet header
578 * @in_port: port number on which @skb was received.
579 * @key: output flow key
580 * @key_lenp: length of output flow key
581 *
582 * The caller must ensure that skb->len >= ETH_HLEN.
583 *
584 * Returns 0 if successful, otherwise a negative errno value.
585 *
586 * Initializes @skb header pointers as follows:
587 *
588 * - skb->mac_header: the Ethernet header.
589 *
590 * - skb->network_header: just past the Ethernet header, or just past the
591 * VLAN header, to the first byte of the Ethernet payload.
592 *
Lorand Jakab34d94f22013-06-03 10:01:14 -0700593 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
Jesse Grossccb13522011-10-25 19:26:31 -0700594 * on output, then just past the IP header, if one is present and
595 * of a correct length, otherwise the same as skb->network_header.
Lorand Jakab34d94f22013-06-03 10:01:14 -0700596 * For other key->eth.type values it is left untouched.
Jesse Grossccb13522011-10-25 19:26:31 -0700597 */
598int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
599 int *key_lenp)
600{
601 int error = 0;
602 int key_len = SW_FLOW_KEY_OFFSET(eth);
603 struct ethhdr *eth;
604
605 memset(key, 0, sizeof(*key));
606
607 key->phy.priority = skb->priority;
608 key->phy.in_port = in_port;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800609 key->phy.skb_mark = skb->mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700610
611 skb_reset_mac_header(skb);
612
613 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
614 * header in the linear data area.
615 */
616 eth = eth_hdr(skb);
617 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
618 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
619
620 __skb_pull(skb, 2 * ETH_ALEN);
Pravin B Shelarb34df5e2013-06-13 11:11:44 -0700621 /* We are going to push all headers that we pull, so no need to
622 * update skb->csum here.
623 */
Jesse Grossccb13522011-10-25 19:26:31 -0700624
625 if (vlan_tx_tag_present(skb))
626 key->eth.tci = htons(skb->vlan_tci);
627 else if (eth->h_proto == htons(ETH_P_8021Q))
628 if (unlikely(parse_vlan(skb, key)))
629 return -ENOMEM;
630
631 key->eth.type = parse_ethertype(skb);
632 if (unlikely(key->eth.type == htons(0)))
633 return -ENOMEM;
634
635 skb_reset_network_header(skb);
636 __skb_push(skb, skb->data - skb_mac_header(skb));
637
638 /* Network layer. */
639 if (key->eth.type == htons(ETH_P_IP)) {
640 struct iphdr *nh;
641 __be16 offset;
642
643 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
644
645 error = check_iphdr(skb);
646 if (unlikely(error)) {
647 if (error == -EINVAL) {
648 skb->transport_header = skb->network_header;
649 error = 0;
650 }
651 goto out;
652 }
653
654 nh = ip_hdr(skb);
655 key->ipv4.addr.src = nh->saddr;
656 key->ipv4.addr.dst = nh->daddr;
657
658 key->ip.proto = nh->protocol;
659 key->ip.tos = nh->tos;
660 key->ip.ttl = nh->ttl;
661
662 offset = nh->frag_off & htons(IP_OFFSET);
663 if (offset) {
664 key->ip.frag = OVS_FRAG_TYPE_LATER;
665 goto out;
666 }
667 if (nh->frag_off & htons(IP_MF) ||
668 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
669 key->ip.frag = OVS_FRAG_TYPE_FIRST;
670
671 /* Transport layer. */
672 if (key->ip.proto == IPPROTO_TCP) {
673 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
674 if (tcphdr_ok(skb)) {
675 struct tcphdr *tcp = tcp_hdr(skb);
676 key->ipv4.tp.src = tcp->source;
677 key->ipv4.tp.dst = tcp->dest;
678 }
679 } else if (key->ip.proto == IPPROTO_UDP) {
680 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
681 if (udphdr_ok(skb)) {
682 struct udphdr *udp = udp_hdr(skb);
683 key->ipv4.tp.src = udp->source;
684 key->ipv4.tp.dst = udp->dest;
685 }
686 } else if (key->ip.proto == IPPROTO_ICMP) {
687 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
688 if (icmphdr_ok(skb)) {
689 struct icmphdr *icmp = icmp_hdr(skb);
690 /* The ICMP type and code fields use the 16-bit
691 * transport port fields, so we need to store
692 * them in 16-bit network byte order. */
693 key->ipv4.tp.src = htons(icmp->type);
694 key->ipv4.tp.dst = htons(icmp->code);
695 }
696 }
697
Mehak Mahajanc0618532012-11-02 14:14:31 -0700698 } else if ((key->eth.type == htons(ETH_P_ARP) ||
699 key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
Jesse Grossccb13522011-10-25 19:26:31 -0700700 struct arp_eth_header *arp;
701
702 arp = (struct arp_eth_header *)skb_network_header(skb);
703
704 if (arp->ar_hrd == htons(ARPHRD_ETHER)
705 && arp->ar_pro == htons(ETH_P_IP)
706 && arp->ar_hln == ETH_ALEN
707 && arp->ar_pln == 4) {
708
709 /* We only match on the lower 8 bits of the opcode. */
710 if (ntohs(arp->ar_op) <= 0xff)
711 key->ip.proto = ntohs(arp->ar_op);
Mehak Mahajand04d3822012-10-30 15:50:28 -0700712 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
713 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
714 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
715 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
716 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
Jesse Grossccb13522011-10-25 19:26:31 -0700717 }
718 } else if (key->eth.type == htons(ETH_P_IPV6)) {
719 int nh_len; /* IPv6 Header + Extensions */
720
721 nh_len = parse_ipv6hdr(skb, key, &key_len);
722 if (unlikely(nh_len < 0)) {
723 if (nh_len == -EINVAL)
724 skb->transport_header = skb->network_header;
725 else
726 error = nh_len;
727 goto out;
728 }
729
730 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
731 goto out;
732 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
733 key->ip.frag = OVS_FRAG_TYPE_FIRST;
734
735 /* Transport layer. */
736 if (key->ip.proto == NEXTHDR_TCP) {
737 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
738 if (tcphdr_ok(skb)) {
739 struct tcphdr *tcp = tcp_hdr(skb);
740 key->ipv6.tp.src = tcp->source;
741 key->ipv6.tp.dst = tcp->dest;
742 }
743 } else if (key->ip.proto == NEXTHDR_UDP) {
744 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
745 if (udphdr_ok(skb)) {
746 struct udphdr *udp = udp_hdr(skb);
747 key->ipv6.tp.src = udp->source;
748 key->ipv6.tp.dst = udp->dest;
749 }
750 } else if (key->ip.proto == NEXTHDR_ICMP) {
751 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
752 if (icmp6hdr_ok(skb)) {
753 error = parse_icmpv6(skb, key, &key_len, nh_len);
754 if (error < 0)
755 goto out;
756 }
757 }
758 }
759
760out:
761 *key_lenp = key_len;
762 return error;
763}
764
765u32 ovs_flow_hash(const struct sw_flow_key *key, int key_len)
766{
767 return jhash2((u32 *)key, DIV_ROUND_UP(key_len, sizeof(u32)), 0);
768}
769
770struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table,
771 struct sw_flow_key *key, int key_len)
772{
773 struct sw_flow *flow;
Jesse Grossccb13522011-10-25 19:26:31 -0700774 struct hlist_head *head;
775 u32 hash;
776
777 hash = ovs_flow_hash(key, key_len);
778
779 head = find_bucket(table, hash);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800780 hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
Jesse Grossccb13522011-10-25 19:26:31 -0700781
782 if (flow->hash == hash &&
783 !memcmp(&flow->key, key, key_len)) {
784 return flow;
785 }
786 }
787 return NULL;
788}
789
790void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
791{
792 struct hlist_head *head;
793
794 head = find_bucket(table, flow->hash);
795 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
796 table->count++;
797}
798
799void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
800{
Hong Zhiguod3e11012013-03-27 20:41:17 +0800801 BUG_ON(table->count == 0);
Jesse Grossccb13522011-10-25 19:26:31 -0700802 hlist_del_rcu(&flow->hash_node[table->node_ver]);
803 table->count--;
Jesse Grossccb13522011-10-25 19:26:31 -0700804}
805
806/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
807const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
808 [OVS_KEY_ATTR_ENCAP] = -1,
809 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
810 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800811 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
Jesse Grossccb13522011-10-25 19:26:31 -0700812 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
813 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
814 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
815 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
816 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
817 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
818 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
819 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
820 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
821 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
822 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
823};
824
825static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
826 const struct nlattr *a[], u32 *attrs)
827{
828 const struct ovs_key_icmp *icmp_key;
829 const struct ovs_key_tcp *tcp_key;
830 const struct ovs_key_udp *udp_key;
831
832 switch (swkey->ip.proto) {
833 case IPPROTO_TCP:
834 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
835 return -EINVAL;
836 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
837
838 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
839 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
840 swkey->ipv4.tp.src = tcp_key->tcp_src;
841 swkey->ipv4.tp.dst = tcp_key->tcp_dst;
842 break;
843
844 case IPPROTO_UDP:
845 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
846 return -EINVAL;
847 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
848
849 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
850 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
851 swkey->ipv4.tp.src = udp_key->udp_src;
852 swkey->ipv4.tp.dst = udp_key->udp_dst;
853 break;
854
855 case IPPROTO_ICMP:
856 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
857 return -EINVAL;
858 *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
859
860 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
861 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
862 swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
863 swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
864 break;
865 }
866
867 return 0;
868}
869
870static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
871 const struct nlattr *a[], u32 *attrs)
872{
873 const struct ovs_key_icmpv6 *icmpv6_key;
874 const struct ovs_key_tcp *tcp_key;
875 const struct ovs_key_udp *udp_key;
876
877 switch (swkey->ip.proto) {
878 case IPPROTO_TCP:
879 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
880 return -EINVAL;
881 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
882
883 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
884 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
885 swkey->ipv6.tp.src = tcp_key->tcp_src;
886 swkey->ipv6.tp.dst = tcp_key->tcp_dst;
887 break;
888
889 case IPPROTO_UDP:
890 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
891 return -EINVAL;
892 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
893
894 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
895 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
896 swkey->ipv6.tp.src = udp_key->udp_src;
897 swkey->ipv6.tp.dst = udp_key->udp_dst;
898 break;
899
900 case IPPROTO_ICMPV6:
901 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
902 return -EINVAL;
903 *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
904
905 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
906 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
907 swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
908 swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
909
910 if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
911 swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
912 const struct ovs_key_nd *nd_key;
913
914 if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
915 return -EINVAL;
916 *attrs &= ~(1 << OVS_KEY_ATTR_ND);
917
918 *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
919 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
920 memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
921 sizeof(swkey->ipv6.nd.target));
922 memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
923 memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
924 }
925 break;
926 }
927
928 return 0;
929}
930
931static int parse_flow_nlattrs(const struct nlattr *attr,
932 const struct nlattr *a[], u32 *attrsp)
933{
934 const struct nlattr *nla;
935 u32 attrs;
936 int rem;
937
938 attrs = 0;
939 nla_for_each_nested(nla, attr, rem) {
940 u16 type = nla_type(nla);
941 int expected_len;
942
943 if (type > OVS_KEY_ATTR_MAX || attrs & (1 << type))
944 return -EINVAL;
945
946 expected_len = ovs_key_lens[type];
947 if (nla_len(nla) != expected_len && expected_len != -1)
948 return -EINVAL;
949
950 attrs |= 1 << type;
951 a[type] = nla;
952 }
953 if (rem)
954 return -EINVAL;
955
956 *attrsp = attrs;
957 return 0;
958}
959
960/**
961 * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
962 * @swkey: receives the extracted flow key.
963 * @key_lenp: number of bytes used in @swkey.
964 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
965 * sequence.
966 */
967int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
968 const struct nlattr *attr)
969{
970 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
971 const struct ovs_key_ethernet *eth_key;
972 int key_len;
973 u32 attrs;
974 int err;
975
976 memset(swkey, 0, sizeof(struct sw_flow_key));
977 key_len = SW_FLOW_KEY_OFFSET(eth);
978
979 err = parse_flow_nlattrs(attr, a, &attrs);
980 if (err)
981 return err;
982
983 /* Metadata attributes. */
984 if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
985 swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
986 attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
987 }
988 if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
989 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
990 if (in_port >= DP_MAX_PORTS)
991 return -EINVAL;
992 swkey->phy.in_port = in_port;
993 attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
994 } else {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700995 swkey->phy.in_port = DP_MAX_PORTS;
Jesse Grossccb13522011-10-25 19:26:31 -0700996 }
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800997 if (attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
998 swkey->phy.skb_mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
999 attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1000 }
Jesse Grossccb13522011-10-25 19:26:31 -07001001
1002 /* Data attributes. */
1003 if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
1004 return -EINVAL;
1005 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1006
1007 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1008 memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
1009 memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
1010
1011 if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
1012 nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
1013 const struct nlattr *encap;
1014 __be16 tci;
1015
1016 if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
1017 (1 << OVS_KEY_ATTR_ETHERTYPE) |
1018 (1 << OVS_KEY_ATTR_ENCAP)))
1019 return -EINVAL;
1020
1021 encap = a[OVS_KEY_ATTR_ENCAP];
1022 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1023 if (tci & htons(VLAN_TAG_PRESENT)) {
1024 swkey->eth.tci = tci;
1025
1026 err = parse_flow_nlattrs(encap, a, &attrs);
1027 if (err)
1028 return err;
1029 } else if (!tci) {
1030 /* Corner case for truncated 802.1Q header. */
1031 if (nla_len(encap))
1032 return -EINVAL;
1033
1034 swkey->eth.type = htons(ETH_P_8021Q);
1035 *key_lenp = key_len;
1036 return 0;
1037 } else {
1038 return -EINVAL;
1039 }
1040 }
1041
1042 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1043 swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
Simon Hormane5c5d222013-03-28 13:38:25 +09001044 if (ntohs(swkey->eth.type) < ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -07001045 return -EINVAL;
1046 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1047 } else {
1048 swkey->eth.type = htons(ETH_P_802_2);
1049 }
1050
1051 if (swkey->eth.type == htons(ETH_P_IP)) {
1052 const struct ovs_key_ipv4 *ipv4_key;
1053
1054 if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
1055 return -EINVAL;
1056 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1057
1058 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
1059 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1060 if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
1061 return -EINVAL;
1062 swkey->ip.proto = ipv4_key->ipv4_proto;
1063 swkey->ip.tos = ipv4_key->ipv4_tos;
1064 swkey->ip.ttl = ipv4_key->ipv4_ttl;
1065 swkey->ip.frag = ipv4_key->ipv4_frag;
1066 swkey->ipv4.addr.src = ipv4_key->ipv4_src;
1067 swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
1068
1069 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1070 err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1071 if (err)
1072 return err;
1073 }
1074 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1075 const struct ovs_key_ipv6 *ipv6_key;
1076
1077 if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
1078 return -EINVAL;
1079 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1080
1081 key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
1082 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1083 if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
1084 return -EINVAL;
1085 swkey->ipv6.label = ipv6_key->ipv6_label;
1086 swkey->ip.proto = ipv6_key->ipv6_proto;
1087 swkey->ip.tos = ipv6_key->ipv6_tclass;
1088 swkey->ip.ttl = ipv6_key->ipv6_hlimit;
1089 swkey->ip.frag = ipv6_key->ipv6_frag;
1090 memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
1091 sizeof(swkey->ipv6.addr.src));
1092 memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
1093 sizeof(swkey->ipv6.addr.dst));
1094
1095 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1096 err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1097 if (err)
1098 return err;
1099 }
Mehak Mahajanc0618532012-11-02 14:14:31 -07001100 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1101 swkey->eth.type == htons(ETH_P_RARP)) {
Jesse Grossccb13522011-10-25 19:26:31 -07001102 const struct ovs_key_arp *arp_key;
1103
1104 if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
1105 return -EINVAL;
1106 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1107
1108 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
1109 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1110 swkey->ipv4.addr.src = arp_key->arp_sip;
1111 swkey->ipv4.addr.dst = arp_key->arp_tip;
1112 if (arp_key->arp_op & htons(0xff00))
1113 return -EINVAL;
1114 swkey->ip.proto = ntohs(arp_key->arp_op);
1115 memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
1116 memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
1117 }
1118
1119 if (attrs)
1120 return -EINVAL;
1121 *key_lenp = key_len;
1122
1123 return 0;
1124}
1125
1126/**
1127 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001128 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
1129 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
Jesse Grossccb13522011-10-25 19:26:31 -07001130 * sequence.
1131 *
1132 * This parses a series of Netlink attributes that form a flow key, which must
1133 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1134 * get the metadata, that is, the parts of the flow key that cannot be
1135 * extracted from the packet itself.
1136 */
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001137int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
1138 const struct nlattr *attr)
Jesse Grossccb13522011-10-25 19:26:31 -07001139{
1140 const struct nlattr *nla;
1141 int rem;
1142
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001143 flow->key.phy.in_port = DP_MAX_PORTS;
1144 flow->key.phy.priority = 0;
1145 flow->key.phy.skb_mark = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001146
1147 nla_for_each_nested(nla, attr, rem) {
1148 int type = nla_type(nla);
1149
1150 if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
1151 if (nla_len(nla) != ovs_key_lens[type])
1152 return -EINVAL;
1153
1154 switch (type) {
1155 case OVS_KEY_ATTR_PRIORITY:
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001156 flow->key.phy.priority = nla_get_u32(nla);
Jesse Grossccb13522011-10-25 19:26:31 -07001157 break;
1158
1159 case OVS_KEY_ATTR_IN_PORT:
1160 if (nla_get_u32(nla) >= DP_MAX_PORTS)
1161 return -EINVAL;
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001162 flow->key.phy.in_port = nla_get_u32(nla);
Jesse Grossccb13522011-10-25 19:26:31 -07001163 break;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -08001164
1165 case OVS_KEY_ATTR_SKB_MARK:
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001166 flow->key.phy.skb_mark = nla_get_u32(nla);
Ansis Atteka39c7caeb2012-11-26 11:24:11 -08001167 break;
Jesse Grossccb13522011-10-25 19:26:31 -07001168 }
1169 }
1170 }
1171 if (rem)
1172 return -EINVAL;
1173 return 0;
1174}
1175
1176int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
1177{
1178 struct ovs_key_ethernet *eth_key;
1179 struct nlattr *nla, *encap;
1180
David S. Miller028d6a62012-03-29 23:20:48 -04001181 if (swkey->phy.priority &&
1182 nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority))
1183 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001184
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001185 if (swkey->phy.in_port != DP_MAX_PORTS &&
David S. Miller028d6a62012-03-29 23:20:48 -04001186 nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port))
1187 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001188
Ansis Atteka39c7caeb2012-11-26 11:24:11 -08001189 if (swkey->phy.skb_mark &&
1190 nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, swkey->phy.skb_mark))
1191 goto nla_put_failure;
1192
Jesse Grossccb13522011-10-25 19:26:31 -07001193 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1194 if (!nla)
1195 goto nla_put_failure;
1196 eth_key = nla_data(nla);
1197 memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
1198 memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
1199
1200 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
David S. Miller028d6a62012-03-29 23:20:48 -04001201 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q)) ||
1202 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci))
1203 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001204 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1205 if (!swkey->eth.tci)
1206 goto unencap;
1207 } else {
1208 encap = NULL;
1209 }
1210
1211 if (swkey->eth.type == htons(ETH_P_802_2))
1212 goto unencap;
1213
David S. Miller028d6a62012-03-29 23:20:48 -04001214 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type))
1215 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001216
1217 if (swkey->eth.type == htons(ETH_P_IP)) {
1218 struct ovs_key_ipv4 *ipv4_key;
1219
1220 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1221 if (!nla)
1222 goto nla_put_failure;
1223 ipv4_key = nla_data(nla);
1224 ipv4_key->ipv4_src = swkey->ipv4.addr.src;
1225 ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
1226 ipv4_key->ipv4_proto = swkey->ip.proto;
1227 ipv4_key->ipv4_tos = swkey->ip.tos;
1228 ipv4_key->ipv4_ttl = swkey->ip.ttl;
1229 ipv4_key->ipv4_frag = swkey->ip.frag;
1230 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1231 struct ovs_key_ipv6 *ipv6_key;
1232
1233 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1234 if (!nla)
1235 goto nla_put_failure;
1236 ipv6_key = nla_data(nla);
1237 memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
1238 sizeof(ipv6_key->ipv6_src));
1239 memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
1240 sizeof(ipv6_key->ipv6_dst));
1241 ipv6_key->ipv6_label = swkey->ipv6.label;
1242 ipv6_key->ipv6_proto = swkey->ip.proto;
1243 ipv6_key->ipv6_tclass = swkey->ip.tos;
1244 ipv6_key->ipv6_hlimit = swkey->ip.ttl;
1245 ipv6_key->ipv6_frag = swkey->ip.frag;
Mehak Mahajanc0618532012-11-02 14:14:31 -07001246 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1247 swkey->eth.type == htons(ETH_P_RARP)) {
Jesse Grossccb13522011-10-25 19:26:31 -07001248 struct ovs_key_arp *arp_key;
1249
1250 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1251 if (!nla)
1252 goto nla_put_failure;
1253 arp_key = nla_data(nla);
1254 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1255 arp_key->arp_sip = swkey->ipv4.addr.src;
1256 arp_key->arp_tip = swkey->ipv4.addr.dst;
1257 arp_key->arp_op = htons(swkey->ip.proto);
1258 memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
1259 memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
1260 }
1261
1262 if ((swkey->eth.type == htons(ETH_P_IP) ||
1263 swkey->eth.type == htons(ETH_P_IPV6)) &&
1264 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1265
1266 if (swkey->ip.proto == IPPROTO_TCP) {
1267 struct ovs_key_tcp *tcp_key;
1268
1269 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1270 if (!nla)
1271 goto nla_put_failure;
1272 tcp_key = nla_data(nla);
1273 if (swkey->eth.type == htons(ETH_P_IP)) {
1274 tcp_key->tcp_src = swkey->ipv4.tp.src;
1275 tcp_key->tcp_dst = swkey->ipv4.tp.dst;
1276 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1277 tcp_key->tcp_src = swkey->ipv6.tp.src;
1278 tcp_key->tcp_dst = swkey->ipv6.tp.dst;
1279 }
1280 } else if (swkey->ip.proto == IPPROTO_UDP) {
1281 struct ovs_key_udp *udp_key;
1282
1283 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1284 if (!nla)
1285 goto nla_put_failure;
1286 udp_key = nla_data(nla);
1287 if (swkey->eth.type == htons(ETH_P_IP)) {
1288 udp_key->udp_src = swkey->ipv4.tp.src;
1289 udp_key->udp_dst = swkey->ipv4.tp.dst;
1290 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1291 udp_key->udp_src = swkey->ipv6.tp.src;
1292 udp_key->udp_dst = swkey->ipv6.tp.dst;
1293 }
1294 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1295 swkey->ip.proto == IPPROTO_ICMP) {
1296 struct ovs_key_icmp *icmp_key;
1297
1298 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1299 if (!nla)
1300 goto nla_put_failure;
1301 icmp_key = nla_data(nla);
1302 icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
1303 icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
1304 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1305 swkey->ip.proto == IPPROTO_ICMPV6) {
1306 struct ovs_key_icmpv6 *icmpv6_key;
1307
1308 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1309 sizeof(*icmpv6_key));
1310 if (!nla)
1311 goto nla_put_failure;
1312 icmpv6_key = nla_data(nla);
1313 icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
1314 icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
1315
1316 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1317 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1318 struct ovs_key_nd *nd_key;
1319
1320 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1321 if (!nla)
1322 goto nla_put_failure;
1323 nd_key = nla_data(nla);
1324 memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
1325 sizeof(nd_key->nd_target));
1326 memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
1327 memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
1328 }
1329 }
1330 }
1331
1332unencap:
1333 if (encap)
1334 nla_nest_end(skb, encap);
1335
1336 return 0;
1337
1338nla_put_failure:
1339 return -EMSGSIZE;
1340}
1341
1342/* Initializes the flow module.
1343 * Returns zero if successful or a negative error code. */
1344int ovs_flow_init(void)
1345{
1346 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1347 0, NULL);
1348 if (flow_cache == NULL)
1349 return -ENOMEM;
1350
1351 return 0;
1352}
1353
1354/* Uninitializes the flow module. */
1355void ovs_flow_exit(void)
1356{
1357 kmem_cache_destroy(flow_cache);
1358}