2 * Copyright (c) 2007-2011 Nicira Networks.
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.
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.
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
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>
33 #include <linux/rcupdate.h>
34 #include <linux/if_arp.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>
44 #include <net/ndisc.h>
48 static struct kmem_cache *flow_cache;
50 static int check_header(struct sk_buff *skb, int len)
52 if (unlikely(skb->len < len))
54 if (unlikely(!pskb_may_pull(skb, len)))
59 static bool arphdr_ok(struct sk_buff *skb)
61 return pskb_may_pull(skb, skb_network_offset(skb) +
62 sizeof(struct arp_eth_header));
65 static int check_iphdr(struct sk_buff *skb)
67 unsigned int nh_ofs = skb_network_offset(skb);
71 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
75 ip_len = ip_hdrlen(skb);
76 if (unlikely(ip_len < sizeof(struct iphdr) ||
77 skb->len < nh_ofs + ip_len))
80 skb_set_transport_header(skb, nh_ofs + ip_len);
84 static bool tcphdr_ok(struct sk_buff *skb)
86 int th_ofs = skb_transport_offset(skb);
89 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
92 tcp_len = tcp_hdrlen(skb);
93 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
94 skb->len < th_ofs + tcp_len))
100 static bool udphdr_ok(struct sk_buff *skb)
102 return pskb_may_pull(skb, skb_transport_offset(skb) +
103 sizeof(struct udphdr));
106 static bool icmphdr_ok(struct sk_buff *skb)
108 return pskb_may_pull(skb, skb_transport_offset(skb) +
109 sizeof(struct icmphdr));
112 u64 ovs_flow_used_time(unsigned long flow_jiffies)
114 struct timespec cur_ts;
117 ktime_get_ts(&cur_ts);
118 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
119 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
120 cur_ts.tv_nsec / NSEC_PER_MSEC;
122 return cur_ms - idle_ms;
125 #define SW_FLOW_KEY_OFFSET(field) \
126 (offsetof(struct sw_flow_key, field) + \
127 FIELD_SIZEOF(struct sw_flow_key, field))
129 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key,
132 unsigned int nh_ofs = skb_network_offset(skb);
140 *key_lenp = SW_FLOW_KEY_OFFSET(ipv6.label);
142 err = check_header(skb, nh_ofs + sizeof(*nh));
147 nexthdr = nh->nexthdr;
148 payload_ofs = (u8 *)(nh + 1) - skb->data;
150 key->ip.proto = NEXTHDR_NONE;
151 key->ip.tos = ipv6_get_dsfield(nh);
152 key->ip.ttl = nh->hop_limit;
153 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
154 key->ipv6.addr.src = nh->saddr;
155 key->ipv6.addr.dst = nh->daddr;
157 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
158 if (unlikely(payload_ofs < 0))
162 if (frag_off & htons(~0x7))
163 key->ip.frag = OVS_FRAG_TYPE_LATER;
165 key->ip.frag = OVS_FRAG_TYPE_FIRST;
168 nh_len = payload_ofs - nh_ofs;
169 skb_set_transport_header(skb, nh_ofs + nh_len);
170 key->ip.proto = nexthdr;
174 static bool icmp6hdr_ok(struct sk_buff *skb)
176 return pskb_may_pull(skb, skb_transport_offset(skb) +
177 sizeof(struct icmp6hdr));
180 #define TCP_FLAGS_OFFSET 13
181 #define TCP_FLAG_MASK 0x3f
183 void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
187 if (flow->key.eth.type == htons(ETH_P_IP) &&
188 flow->key.ip.proto == IPPROTO_TCP &&
189 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
190 u8 *tcp = (u8 *)tcp_hdr(skb);
191 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
194 spin_lock(&flow->lock);
195 flow->used = jiffies;
196 flow->packet_count++;
197 flow->byte_count += skb->len;
198 flow->tcp_flags |= tcp_flags;
199 spin_unlock(&flow->lock);
202 struct sw_flow_actions *ovs_flow_actions_alloc(const struct nlattr *actions)
204 int actions_len = nla_len(actions);
205 struct sw_flow_actions *sfa;
207 if (actions_len > MAX_ACTIONS_BUFSIZE)
208 return ERR_PTR(-EINVAL);
210 sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL);
212 return ERR_PTR(-ENOMEM);
214 sfa->actions_len = actions_len;
215 memcpy(sfa->actions, nla_data(actions), actions_len);
219 struct sw_flow *ovs_flow_alloc(void)
221 struct sw_flow *flow;
223 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
225 return ERR_PTR(-ENOMEM);
227 spin_lock_init(&flow->lock);
228 atomic_set(&flow->refcnt, 1);
229 flow->sf_acts = NULL;
235 static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
237 hash = jhash_1word(hash, table->hash_seed);
238 return flex_array_get(table->buckets,
239 (hash & (table->n_buckets - 1)));
242 static struct flex_array *alloc_buckets(unsigned int n_buckets)
244 struct flex_array *buckets;
247 buckets = flex_array_alloc(sizeof(struct hlist_head *),
248 n_buckets, GFP_KERNEL);
252 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
254 flex_array_free(buckets);
258 for (i = 0; i < n_buckets; i++)
259 INIT_HLIST_HEAD((struct hlist_head *)
260 flex_array_get(buckets, i));
265 static void free_buckets(struct flex_array *buckets)
267 flex_array_free(buckets);
270 struct flow_table *ovs_flow_tbl_alloc(int new_size)
272 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
277 table->buckets = alloc_buckets(new_size);
279 if (!table->buckets) {
283 table->n_buckets = new_size;
286 table->keep_flows = false;
287 get_random_bytes(&table->hash_seed, sizeof(u32));
292 static void flow_free(struct sw_flow *flow)
298 void ovs_flow_tbl_destroy(struct flow_table *table)
305 if (table->keep_flows)
308 for (i = 0; i < table->n_buckets; i++) {
309 struct sw_flow *flow;
310 struct hlist_head *head = flex_array_get(table->buckets, i);
311 struct hlist_node *node, *n;
312 int ver = table->node_ver;
314 hlist_for_each_entry_safe(flow, node, n, head, hash_node[ver]) {
315 hlist_del_rcu(&flow->hash_node[ver]);
321 free_buckets(table->buckets);
325 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
327 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
329 ovs_flow_tbl_destroy(table);
332 void ovs_flow_tbl_deferred_destroy(struct flow_table *table)
337 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
340 struct sw_flow *ovs_flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last)
342 struct sw_flow *flow;
343 struct hlist_head *head;
344 struct hlist_node *n;
348 ver = table->node_ver;
349 while (*bucket < table->n_buckets) {
351 head = flex_array_get(table->buckets, *bucket);
352 hlist_for_each_entry_rcu(flow, n, head, hash_node[ver]) {
367 static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
372 old_ver = old->node_ver;
373 new->node_ver = !old_ver;
375 /* Insert in new table. */
376 for (i = 0; i < old->n_buckets; i++) {
377 struct sw_flow *flow;
378 struct hlist_head *head;
379 struct hlist_node *n;
381 head = flex_array_get(old->buckets, i);
383 hlist_for_each_entry(flow, n, head, hash_node[old_ver])
384 ovs_flow_tbl_insert(new, flow);
386 old->keep_flows = true;
389 static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
391 struct flow_table *new_table;
393 new_table = ovs_flow_tbl_alloc(n_buckets);
395 return ERR_PTR(-ENOMEM);
397 flow_table_copy_flows(table, new_table);
402 struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
404 return __flow_tbl_rehash(table, table->n_buckets);
407 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
409 return __flow_tbl_rehash(table, table->n_buckets * 2);
412 /* RCU callback used by ovs_flow_deferred_free. */
413 static void rcu_free_flow_callback(struct rcu_head *rcu)
415 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
421 /* Schedules 'flow' to be freed after the next RCU grace period.
422 * The caller must hold rcu_read_lock for this to be sensible. */
423 void ovs_flow_deferred_free(struct sw_flow *flow)
425 call_rcu(&flow->rcu, rcu_free_flow_callback);
428 void ovs_flow_hold(struct sw_flow *flow)
430 atomic_inc(&flow->refcnt);
433 void ovs_flow_put(struct sw_flow *flow)
438 if (atomic_dec_and_test(&flow->refcnt)) {
439 kfree((struct sf_flow_acts __force *)flow->sf_acts);
440 kmem_cache_free(flow_cache, flow);
444 /* RCU callback used by ovs_flow_deferred_free_acts. */
445 static void rcu_free_acts_callback(struct rcu_head *rcu)
447 struct sw_flow_actions *sf_acts = container_of(rcu,
448 struct sw_flow_actions, rcu);
452 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
453 * The caller must hold rcu_read_lock for this to be sensible. */
454 void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
456 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
459 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
462 __be16 eth_type; /* ETH_P_8021Q */
465 struct qtag_prefix *qp;
467 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
470 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
474 qp = (struct qtag_prefix *) skb->data;
475 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
476 __skb_pull(skb, sizeof(struct qtag_prefix));
481 static __be16 parse_ethertype(struct sk_buff *skb)
483 struct llc_snap_hdr {
484 u8 dsap; /* Always 0xAA */
485 u8 ssap; /* Always 0xAA */
490 struct llc_snap_hdr *llc;
493 proto = *(__be16 *) skb->data;
494 __skb_pull(skb, sizeof(__be16));
496 if (ntohs(proto) >= 1536)
499 if (skb->len < sizeof(struct llc_snap_hdr))
500 return htons(ETH_P_802_2);
502 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
505 llc = (struct llc_snap_hdr *) skb->data;
506 if (llc->dsap != LLC_SAP_SNAP ||
507 llc->ssap != LLC_SAP_SNAP ||
508 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
509 return htons(ETH_P_802_2);
511 __skb_pull(skb, sizeof(struct llc_snap_hdr));
512 return llc->ethertype;
515 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
516 int *key_lenp, int nh_len)
518 struct icmp6hdr *icmp = icmp6_hdr(skb);
522 /* The ICMPv6 type and code fields use the 16-bit transport port
523 * fields, so we need to store them in 16-bit network byte order.
525 key->ipv6.tp.src = htons(icmp->icmp6_type);
526 key->ipv6.tp.dst = htons(icmp->icmp6_code);
527 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
529 if (icmp->icmp6_code == 0 &&
530 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
531 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
532 int icmp_len = skb->len - skb_transport_offset(skb);
536 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
538 /* In order to process neighbor discovery options, we need the
541 if (unlikely(icmp_len < sizeof(*nd)))
543 if (unlikely(skb_linearize(skb))) {
548 nd = (struct nd_msg *)skb_transport_header(skb);
549 key->ipv6.nd.target = nd->target;
550 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
552 icmp_len -= sizeof(*nd);
554 while (icmp_len >= 8) {
555 struct nd_opt_hdr *nd_opt =
556 (struct nd_opt_hdr *)(nd->opt + offset);
557 int opt_len = nd_opt->nd_opt_len * 8;
559 if (unlikely(!opt_len || opt_len > icmp_len))
562 /* Store the link layer address if the appropriate
563 * option is provided. It is considered an error if
564 * the same link layer option is specified twice.
566 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
568 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
570 memcpy(key->ipv6.nd.sll,
571 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
572 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
574 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
576 memcpy(key->ipv6.nd.tll,
577 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
588 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
589 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
590 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
598 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
599 * @skb: sk_buff that contains the frame, with skb->data pointing to the
601 * @in_port: port number on which @skb was received.
602 * @key: output flow key
603 * @key_lenp: length of output flow key
605 * The caller must ensure that skb->len >= ETH_HLEN.
607 * Returns 0 if successful, otherwise a negative errno value.
609 * Initializes @skb header pointers as follows:
611 * - skb->mac_header: the Ethernet header.
613 * - skb->network_header: just past the Ethernet header, or just past the
614 * VLAN header, to the first byte of the Ethernet payload.
616 * - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6
617 * on output, then just past the IP header, if one is present and
618 * of a correct length, otherwise the same as skb->network_header.
619 * For other key->dl_type values it is left untouched.
621 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
625 int key_len = SW_FLOW_KEY_OFFSET(eth);
628 memset(key, 0, sizeof(*key));
630 key->phy.priority = skb->priority;
631 key->phy.tun_id = OVS_CB(skb)->tun_id;
632 key->phy.in_port = in_port;
634 skb_reset_mac_header(skb);
636 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
637 * header in the linear data area.
640 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
641 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
643 __skb_pull(skb, 2 * ETH_ALEN);
645 if (vlan_tx_tag_present(skb))
646 key->eth.tci = htons(vlan_get_tci(skb));
647 else if (eth->h_proto == htons(ETH_P_8021Q))
648 if (unlikely(parse_vlan(skb, key)))
651 key->eth.type = parse_ethertype(skb);
652 if (unlikely(key->eth.type == htons(0)))
655 skb_reset_network_header(skb);
656 __skb_push(skb, skb->data - skb_mac_header(skb));
659 if (key->eth.type == htons(ETH_P_IP)) {
663 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
665 error = check_iphdr(skb);
666 if (unlikely(error)) {
667 if (error == -EINVAL) {
668 skb->transport_header = skb->network_header;
675 key->ipv4.addr.src = nh->saddr;
676 key->ipv4.addr.dst = nh->daddr;
678 key->ip.proto = nh->protocol;
679 key->ip.tos = nh->tos;
680 key->ip.ttl = nh->ttl;
682 offset = nh->frag_off & htons(IP_OFFSET);
684 key->ip.frag = OVS_FRAG_TYPE_LATER;
687 if (nh->frag_off & htons(IP_MF) ||
688 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
689 key->ip.frag = OVS_FRAG_TYPE_FIRST;
691 /* Transport layer. */
692 if (key->ip.proto == IPPROTO_TCP) {
693 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
694 if (tcphdr_ok(skb)) {
695 struct tcphdr *tcp = tcp_hdr(skb);
696 key->ipv4.tp.src = tcp->source;
697 key->ipv4.tp.dst = tcp->dest;
699 } else if (key->ip.proto == IPPROTO_UDP) {
700 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
701 if (udphdr_ok(skb)) {
702 struct udphdr *udp = udp_hdr(skb);
703 key->ipv4.tp.src = udp->source;
704 key->ipv4.tp.dst = udp->dest;
706 } else if (key->ip.proto == IPPROTO_ICMP) {
707 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
708 if (icmphdr_ok(skb)) {
709 struct icmphdr *icmp = icmp_hdr(skb);
710 /* The ICMP type and code fields use the 16-bit
711 * transport port fields, so we need to store
712 * them in 16-bit network byte order. */
713 key->ipv4.tp.src = htons(icmp->type);
714 key->ipv4.tp.dst = htons(icmp->code);
718 } else if (key->eth.type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
719 struct arp_eth_header *arp;
721 arp = (struct arp_eth_header *)skb_network_header(skb);
723 if (arp->ar_hrd == htons(ARPHRD_ETHER)
724 && arp->ar_pro == htons(ETH_P_IP)
725 && arp->ar_hln == ETH_ALEN
726 && arp->ar_pln == 4) {
728 /* We only match on the lower 8 bits of the opcode. */
729 if (ntohs(arp->ar_op) <= 0xff)
730 key->ip.proto = ntohs(arp->ar_op);
732 if (key->ip.proto == ARPOP_REQUEST
733 || key->ip.proto == ARPOP_REPLY) {
734 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
735 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
736 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
737 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
738 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
741 } else if (key->eth.type == htons(ETH_P_IPV6)) {
742 int nh_len; /* IPv6 Header + Extensions */
744 nh_len = parse_ipv6hdr(skb, key, &key_len);
745 if (unlikely(nh_len < 0)) {
746 if (nh_len == -EINVAL)
747 skb->transport_header = skb->network_header;
753 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
755 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
756 key->ip.frag = OVS_FRAG_TYPE_FIRST;
758 /* Transport layer. */
759 if (key->ip.proto == NEXTHDR_TCP) {
760 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
761 if (tcphdr_ok(skb)) {
762 struct tcphdr *tcp = tcp_hdr(skb);
763 key->ipv6.tp.src = tcp->source;
764 key->ipv6.tp.dst = tcp->dest;
766 } else if (key->ip.proto == NEXTHDR_UDP) {
767 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
768 if (udphdr_ok(skb)) {
769 struct udphdr *udp = udp_hdr(skb);
770 key->ipv6.tp.src = udp->source;
771 key->ipv6.tp.dst = udp->dest;
773 } else if (key->ip.proto == NEXTHDR_ICMP) {
774 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
775 if (icmp6hdr_ok(skb)) {
776 error = parse_icmpv6(skb, key, &key_len, nh_len);
788 u32 ovs_flow_hash(const struct sw_flow_key *key, int key_len)
790 return jhash2((u32 *)key, DIV_ROUND_UP(key_len, sizeof(u32)), 0);
793 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table,
794 struct sw_flow_key *key, int key_len)
796 struct sw_flow *flow;
797 struct hlist_node *n;
798 struct hlist_head *head;
801 hash = ovs_flow_hash(key, key_len);
803 head = find_bucket(table, hash);
804 hlist_for_each_entry_rcu(flow, n, head, hash_node[table->node_ver]) {
806 if (flow->hash == hash &&
807 !memcmp(&flow->key, key, key_len)) {
814 void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
816 struct hlist_head *head;
818 head = find_bucket(table, flow->hash);
819 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
823 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
825 hlist_del_rcu(&flow->hash_node[table->node_ver]);
827 BUG_ON(table->count < 0);
830 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
831 const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
832 [OVS_KEY_ATTR_ENCAP] = -1,
833 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
834 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
835 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
836 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
837 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
838 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
839 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
840 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
841 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
842 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
843 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
844 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
845 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
848 [OVS_KEY_ATTR_TUN_ID] = sizeof(__be64),
851 static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
852 const struct nlattr *a[], u64 *attrs)
854 const struct ovs_key_icmp *icmp_key;
855 const struct ovs_key_tcp *tcp_key;
856 const struct ovs_key_udp *udp_key;
858 switch (swkey->ip.proto) {
860 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
862 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
864 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
865 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
866 swkey->ipv4.tp.src = tcp_key->tcp_src;
867 swkey->ipv4.tp.dst = tcp_key->tcp_dst;
871 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
873 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
875 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
876 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
877 swkey->ipv4.tp.src = udp_key->udp_src;
878 swkey->ipv4.tp.dst = udp_key->udp_dst;
882 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
884 *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
886 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
887 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
888 swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
889 swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
896 static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
897 const struct nlattr *a[], u64 *attrs)
899 const struct ovs_key_icmpv6 *icmpv6_key;
900 const struct ovs_key_tcp *tcp_key;
901 const struct ovs_key_udp *udp_key;
903 switch (swkey->ip.proto) {
905 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
907 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
909 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
910 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
911 swkey->ipv6.tp.src = tcp_key->tcp_src;
912 swkey->ipv6.tp.dst = tcp_key->tcp_dst;
916 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
918 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
920 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
921 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
922 swkey->ipv6.tp.src = udp_key->udp_src;
923 swkey->ipv6.tp.dst = udp_key->udp_dst;
927 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
929 *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
931 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
932 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
933 swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
934 swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
936 if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
937 swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
938 const struct ovs_key_nd *nd_key;
940 if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
942 *attrs &= ~(1 << OVS_KEY_ATTR_ND);
944 *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
945 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
946 memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
947 sizeof(swkey->ipv6.nd.target));
948 memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
949 memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
957 static int parse_flow_nlattrs(const struct nlattr *attr,
958 const struct nlattr *a[], u64 *attrsp)
960 const struct nlattr *nla;
965 nla_for_each_nested(nla, attr, rem) {
966 u16 type = nla_type(nla);
969 if (type > OVS_KEY_ATTR_MAX || attrs & (1ULL << type))
972 expected_len = ovs_key_lens[type];
973 if (nla_len(nla) != expected_len && expected_len != -1)
976 attrs |= 1ULL << type;
987 * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
988 * @swkey: receives the extracted flow key.
989 * @key_lenp: number of bytes used in @swkey.
990 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
993 int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
994 const struct nlattr *attr)
996 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
997 const struct ovs_key_ethernet *eth_key;
1002 memset(swkey, 0, sizeof(struct sw_flow_key));
1003 key_len = SW_FLOW_KEY_OFFSET(eth);
1005 err = parse_flow_nlattrs(attr, a, &attrs);
1009 /* Metadata attributes. */
1010 if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1011 swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
1012 attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1014 if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1015 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1016 if (in_port >= DP_MAX_PORTS)
1018 swkey->phy.in_port = in_port;
1019 attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1021 swkey->phy.in_port = DP_MAX_PORTS;
1024 if (attrs & (1ULL << OVS_KEY_ATTR_TUN_ID)) {
1025 swkey->phy.tun_id = nla_get_be64(a[OVS_KEY_ATTR_TUN_ID]);
1026 attrs &= ~(1ULL << OVS_KEY_ATTR_TUN_ID);
1029 /* Data attributes. */
1030 if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
1032 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1034 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1035 memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
1036 memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
1038 if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
1039 nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
1040 const struct nlattr *encap;
1043 if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
1044 (1 << OVS_KEY_ATTR_ETHERTYPE) |
1045 (1 << OVS_KEY_ATTR_ENCAP)))
1048 encap = a[OVS_KEY_ATTR_ENCAP];
1049 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1050 if (tci & htons(VLAN_TAG_PRESENT)) {
1051 swkey->eth.tci = tci;
1053 err = parse_flow_nlattrs(encap, a, &attrs);
1057 /* Corner case for truncated 802.1Q header. */
1061 swkey->eth.type = htons(ETH_P_8021Q);
1062 *key_lenp = key_len;
1069 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1070 swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1071 if (ntohs(swkey->eth.type) < 1536)
1073 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1075 swkey->eth.type = htons(ETH_P_802_2);
1078 if (swkey->eth.type == htons(ETH_P_IP)) {
1079 const struct ovs_key_ipv4 *ipv4_key;
1081 if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
1083 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1085 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
1086 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1087 if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
1089 swkey->ip.proto = ipv4_key->ipv4_proto;
1090 swkey->ip.tos = ipv4_key->ipv4_tos;
1091 swkey->ip.ttl = ipv4_key->ipv4_ttl;
1092 swkey->ip.frag = ipv4_key->ipv4_frag;
1093 swkey->ipv4.addr.src = ipv4_key->ipv4_src;
1094 swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
1096 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1097 err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1101 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1102 const struct ovs_key_ipv6 *ipv6_key;
1104 if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
1106 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1108 key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
1109 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1110 if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
1112 swkey->ipv6.label = ipv6_key->ipv6_label;
1113 swkey->ip.proto = ipv6_key->ipv6_proto;
1114 swkey->ip.tos = ipv6_key->ipv6_tclass;
1115 swkey->ip.ttl = ipv6_key->ipv6_hlimit;
1116 swkey->ip.frag = ipv6_key->ipv6_frag;
1117 memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
1118 sizeof(swkey->ipv6.addr.src));
1119 memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
1120 sizeof(swkey->ipv6.addr.dst));
1122 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1123 err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1127 } else if (swkey->eth.type == htons(ETH_P_ARP)) {
1128 const struct ovs_key_arp *arp_key;
1130 if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
1132 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1134 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
1135 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1136 swkey->ipv4.addr.src = arp_key->arp_sip;
1137 swkey->ipv4.addr.dst = arp_key->arp_tip;
1138 if (arp_key->arp_op & htons(0xff00))
1140 swkey->ip.proto = ntohs(arp_key->arp_op);
1141 memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
1142 memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
1147 *key_lenp = key_len;
1153 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1154 * @in_port: receives the extracted input port.
1155 * @tun_id: receives the extracted tunnel ID.
1156 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1159 * This parses a series of Netlink attributes that form a flow key, which must
1160 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1161 * get the metadata, that is, the parts of the flow key that cannot be
1162 * extracted from the packet itself.
1164 int ovs_flow_metadata_from_nlattrs(u32 *priority, u16 *in_port, __be64 *tun_id,
1165 const struct nlattr *attr)
1167 const struct nlattr *nla;
1170 *in_port = DP_MAX_PORTS;
1174 nla_for_each_nested(nla, attr, rem) {
1175 int type = nla_type(nla);
1177 if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
1178 if (nla_len(nla) != ovs_key_lens[type])
1182 case OVS_KEY_ATTR_PRIORITY:
1183 *priority = nla_get_u32(nla);
1186 case OVS_KEY_ATTR_TUN_ID:
1187 *tun_id = nla_get_be64(nla);
1190 case OVS_KEY_ATTR_IN_PORT:
1191 if (nla_get_u32(nla) >= DP_MAX_PORTS)
1193 *in_port = nla_get_u32(nla);
1203 int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
1205 struct ovs_key_ethernet *eth_key;
1206 struct nlattr *nla, *encap;
1208 if (swkey->phy.priority)
1209 NLA_PUT_U32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority);
1211 if (swkey->phy.tun_id != cpu_to_be64(0))
1212 NLA_PUT_BE64(skb, OVS_KEY_ATTR_TUN_ID, swkey->phy.tun_id);
1214 if (swkey->phy.in_port != DP_MAX_PORTS)
1215 NLA_PUT_U32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port);
1217 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1219 goto nla_put_failure;
1220 eth_key = nla_data(nla);
1221 memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
1222 memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
1224 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1225 NLA_PUT_BE16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q));
1226 NLA_PUT_BE16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci);
1227 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1228 if (!swkey->eth.tci)
1234 if (swkey->eth.type == htons(ETH_P_802_2))
1237 NLA_PUT_BE16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type);
1239 if (swkey->eth.type == htons(ETH_P_IP)) {
1240 struct ovs_key_ipv4 *ipv4_key;
1242 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1244 goto nla_put_failure;
1245 ipv4_key = nla_data(nla);
1246 ipv4_key->ipv4_src = swkey->ipv4.addr.src;
1247 ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
1248 ipv4_key->ipv4_proto = swkey->ip.proto;
1249 ipv4_key->ipv4_tos = swkey->ip.tos;
1250 ipv4_key->ipv4_ttl = swkey->ip.ttl;
1251 ipv4_key->ipv4_frag = swkey->ip.frag;
1252 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1253 struct ovs_key_ipv6 *ipv6_key;
1255 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1257 goto nla_put_failure;
1258 ipv6_key = nla_data(nla);
1259 memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
1260 sizeof(ipv6_key->ipv6_src));
1261 memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
1262 sizeof(ipv6_key->ipv6_dst));
1263 ipv6_key->ipv6_label = swkey->ipv6.label;
1264 ipv6_key->ipv6_proto = swkey->ip.proto;
1265 ipv6_key->ipv6_tclass = swkey->ip.tos;
1266 ipv6_key->ipv6_hlimit = swkey->ip.ttl;
1267 ipv6_key->ipv6_frag = swkey->ip.frag;
1268 } else if (swkey->eth.type == htons(ETH_P_ARP)) {
1269 struct ovs_key_arp *arp_key;
1271 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1273 goto nla_put_failure;
1274 arp_key = nla_data(nla);
1275 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1276 arp_key->arp_sip = swkey->ipv4.addr.src;
1277 arp_key->arp_tip = swkey->ipv4.addr.dst;
1278 arp_key->arp_op = htons(swkey->ip.proto);
1279 memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
1280 memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
1283 if ((swkey->eth.type == htons(ETH_P_IP) ||
1284 swkey->eth.type == htons(ETH_P_IPV6)) &&
1285 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1287 if (swkey->ip.proto == IPPROTO_TCP) {
1288 struct ovs_key_tcp *tcp_key;
1290 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1292 goto nla_put_failure;
1293 tcp_key = nla_data(nla);
1294 if (swkey->eth.type == htons(ETH_P_IP)) {
1295 tcp_key->tcp_src = swkey->ipv4.tp.src;
1296 tcp_key->tcp_dst = swkey->ipv4.tp.dst;
1297 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1298 tcp_key->tcp_src = swkey->ipv6.tp.src;
1299 tcp_key->tcp_dst = swkey->ipv6.tp.dst;
1301 } else if (swkey->ip.proto == IPPROTO_UDP) {
1302 struct ovs_key_udp *udp_key;
1304 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1306 goto nla_put_failure;
1307 udp_key = nla_data(nla);
1308 if (swkey->eth.type == htons(ETH_P_IP)) {
1309 udp_key->udp_src = swkey->ipv4.tp.src;
1310 udp_key->udp_dst = swkey->ipv4.tp.dst;
1311 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1312 udp_key->udp_src = swkey->ipv6.tp.src;
1313 udp_key->udp_dst = swkey->ipv6.tp.dst;
1315 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1316 swkey->ip.proto == IPPROTO_ICMP) {
1317 struct ovs_key_icmp *icmp_key;
1319 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1321 goto nla_put_failure;
1322 icmp_key = nla_data(nla);
1323 icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
1324 icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
1325 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1326 swkey->ip.proto == IPPROTO_ICMPV6) {
1327 struct ovs_key_icmpv6 *icmpv6_key;
1329 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1330 sizeof(*icmpv6_key));
1332 goto nla_put_failure;
1333 icmpv6_key = nla_data(nla);
1334 icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
1335 icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
1337 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1338 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1339 struct ovs_key_nd *nd_key;
1341 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1343 goto nla_put_failure;
1344 nd_key = nla_data(nla);
1345 memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
1346 sizeof(nd_key->nd_target));
1347 memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
1348 memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
1355 nla_nest_end(skb, encap);
1363 /* Initializes the flow module.
1364 * Returns zero if successful or a negative error code. */
1365 int ovs_flow_init(void)
1367 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1369 if (flow_cache == NULL)
1375 /* Uninitializes the flow module. */
1376 void ovs_flow_exit(void)
1378 kmem_cache_destroy(flow_cache);