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>
35 #include <linux/if_ether.h>
37 #include <linux/ipv6.h>
38 #include <linux/tcp.h>
39 #include <linux/udp.h>
40 #include <linux/icmp.h>
41 #include <linux/icmpv6.h>
42 #include <linux/rculist.h>
45 #include <net/ndisc.h>
49 static struct kmem_cache *flow_cache;
51 static int check_header(struct sk_buff *skb, int len)
53 if (unlikely(skb->len < len))
55 if (unlikely(!pskb_may_pull(skb, len)))
60 static bool arphdr_ok(struct sk_buff *skb)
62 return pskb_may_pull(skb, skb_network_offset(skb) +
63 sizeof(struct arp_eth_header));
66 static int check_iphdr(struct sk_buff *skb)
68 unsigned int nh_ofs = skb_network_offset(skb);
72 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
76 ip_len = ip_hdrlen(skb);
77 if (unlikely(ip_len < sizeof(struct iphdr) ||
78 skb->len < nh_ofs + ip_len))
81 skb_set_transport_header(skb, nh_ofs + ip_len);
85 static bool tcphdr_ok(struct sk_buff *skb)
87 int th_ofs = skb_transport_offset(skb);
90 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
93 tcp_len = tcp_hdrlen(skb);
94 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
95 skb->len < th_ofs + tcp_len))
101 static bool udphdr_ok(struct sk_buff *skb)
103 return pskb_may_pull(skb, skb_transport_offset(skb) +
104 sizeof(struct udphdr));
107 static bool icmphdr_ok(struct sk_buff *skb)
109 return pskb_may_pull(skb, skb_transport_offset(skb) +
110 sizeof(struct icmphdr));
113 u64 ovs_flow_used_time(unsigned long flow_jiffies)
115 struct timespec cur_ts;
118 ktime_get_ts(&cur_ts);
119 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
120 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
121 cur_ts.tv_nsec / NSEC_PER_MSEC;
123 return cur_ms - idle_ms;
126 #define SW_FLOW_KEY_OFFSET(field) \
127 (offsetof(struct sw_flow_key, field) + \
128 FIELD_SIZEOF(struct sw_flow_key, field))
130 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key,
133 unsigned int nh_ofs = skb_network_offset(skb);
141 *key_lenp = SW_FLOW_KEY_OFFSET(ipv6.label);
143 err = check_header(skb, nh_ofs + sizeof(*nh));
148 nexthdr = nh->nexthdr;
149 payload_ofs = (u8 *)(nh + 1) - skb->data;
151 key->ip.proto = NEXTHDR_NONE;
152 key->ip.tos = ipv6_get_dsfield(nh);
153 key->ip.ttl = nh->hop_limit;
154 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
155 key->ipv6.addr.src = nh->saddr;
156 key->ipv6.addr.dst = nh->daddr;
158 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
159 if (unlikely(payload_ofs < 0))
163 if (frag_off & htons(~0x7))
164 key->ip.frag = OVS_FRAG_TYPE_LATER;
166 key->ip.frag = OVS_FRAG_TYPE_FIRST;
169 nh_len = payload_ofs - nh_ofs;
170 skb_set_transport_header(skb, nh_ofs + nh_len);
171 key->ip.proto = nexthdr;
175 static bool icmp6hdr_ok(struct sk_buff *skb)
177 return pskb_may_pull(skb, skb_transport_offset(skb) +
178 sizeof(struct icmp6hdr));
181 #define TCP_FLAGS_OFFSET 13
182 #define TCP_FLAG_MASK 0x3f
184 void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
188 if (flow->key.eth.type == htons(ETH_P_IP) &&
189 flow->key.ip.proto == IPPROTO_TCP) {
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 /* At least DP_MAX_PORTS actions are required to be able to flood a
208 * packet to every port. Factor of 2 allows for setting VLAN tags,
210 if (actions_len > 2 * DP_MAX_PORTS * nla_total_size(4))
211 return ERR_PTR(-EINVAL);
213 sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL);
215 return ERR_PTR(-ENOMEM);
217 sfa->actions_len = actions_len;
218 memcpy(sfa->actions, nla_data(actions), actions_len);
222 struct sw_flow *ovs_flow_alloc(void)
224 struct sw_flow *flow;
226 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
228 return ERR_PTR(-ENOMEM);
230 spin_lock_init(&flow->lock);
231 atomic_set(&flow->refcnt, 1);
232 flow->sf_acts = NULL;
238 static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
240 hash = jhash_1word(hash, table->hash_seed);
241 return flex_array_get(table->buckets,
242 (hash & (table->n_buckets - 1)));
245 static struct flex_array *alloc_buckets(unsigned int n_buckets)
247 struct flex_array *buckets;
250 buckets = flex_array_alloc(sizeof(struct hlist_head *),
251 n_buckets, GFP_KERNEL);
255 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
257 flex_array_free(buckets);
261 for (i = 0; i < n_buckets; i++)
262 INIT_HLIST_HEAD((struct hlist_head *)
263 flex_array_get(buckets, i));
268 static void free_buckets(struct flex_array *buckets)
270 flex_array_free(buckets);
273 struct flow_table *ovs_flow_tbl_alloc(int new_size)
275 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
280 table->buckets = alloc_buckets(new_size);
282 if (!table->buckets) {
286 table->n_buckets = new_size;
289 table->keep_flows = false;
290 get_random_bytes(&table->hash_seed, sizeof(u32));
295 static void flow_free(struct sw_flow *flow)
301 void ovs_flow_tbl_destroy(struct flow_table *table)
308 if (table->keep_flows)
311 for (i = 0; i < table->n_buckets; i++) {
312 struct sw_flow *flow;
313 struct hlist_head *head = flex_array_get(table->buckets, i);
314 struct hlist_node *node, *n;
315 int ver = table->node_ver;
317 hlist_for_each_entry_safe(flow, node, n, head, hash_node[ver]) {
318 hlist_del_rcu(&flow->hash_node[ver]);
324 free_buckets(table->buckets);
328 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
330 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
332 ovs_flow_tbl_destroy(table);
335 void ovs_flow_tbl_deferred_destroy(struct flow_table *table)
340 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
343 struct sw_flow *ovs_flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last)
345 struct sw_flow *flow;
346 struct hlist_head *head;
347 struct hlist_node *n;
351 ver = table->node_ver;
352 while (*bucket < table->n_buckets) {
354 head = flex_array_get(table->buckets, *bucket);
355 hlist_for_each_entry_rcu(flow, n, head, hash_node[ver]) {
370 static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
375 old_ver = old->node_ver;
376 new->node_ver = !old_ver;
378 /* Insert in new table. */
379 for (i = 0; i < old->n_buckets; i++) {
380 struct sw_flow *flow;
381 struct hlist_head *head;
382 struct hlist_node *n;
384 head = flex_array_get(old->buckets, i);
386 hlist_for_each_entry(flow, n, head, hash_node[old_ver])
387 ovs_flow_tbl_insert(new, flow);
389 old->keep_flows = true;
392 static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
394 struct flow_table *new_table;
396 new_table = ovs_flow_tbl_alloc(n_buckets);
398 return ERR_PTR(-ENOMEM);
400 flow_table_copy_flows(table, new_table);
405 struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
407 return __flow_tbl_rehash(table, table->n_buckets);
410 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
412 return __flow_tbl_rehash(table, table->n_buckets * 2);
415 /* RCU callback used by ovs_flow_deferred_free. */
416 static void rcu_free_flow_callback(struct rcu_head *rcu)
418 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
424 /* Schedules 'flow' to be freed after the next RCU grace period.
425 * The caller must hold rcu_read_lock for this to be sensible. */
426 void ovs_flow_deferred_free(struct sw_flow *flow)
428 call_rcu(&flow->rcu, rcu_free_flow_callback);
431 void ovs_flow_hold(struct sw_flow *flow)
433 atomic_inc(&flow->refcnt);
436 void ovs_flow_put(struct sw_flow *flow)
441 if (atomic_dec_and_test(&flow->refcnt)) {
442 kfree((struct sf_flow_acts __force *)flow->sf_acts);
443 kmem_cache_free(flow_cache, flow);
447 /* RCU callback used by ovs_flow_deferred_free_acts. */
448 static void rcu_free_acts_callback(struct rcu_head *rcu)
450 struct sw_flow_actions *sf_acts = container_of(rcu,
451 struct sw_flow_actions, rcu);
455 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
456 * The caller must hold rcu_read_lock for this to be sensible. */
457 void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
459 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
462 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
465 __be16 eth_type; /* ETH_P_8021Q */
468 struct qtag_prefix *qp;
470 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
473 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
477 qp = (struct qtag_prefix *) skb->data;
478 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
479 __skb_pull(skb, sizeof(struct qtag_prefix));
484 static __be16 parse_ethertype(struct sk_buff *skb)
486 struct llc_snap_hdr {
487 u8 dsap; /* Always 0xAA */
488 u8 ssap; /* Always 0xAA */
493 struct llc_snap_hdr *llc;
496 proto = *(__be16 *) skb->data;
497 __skb_pull(skb, sizeof(__be16));
499 if (ntohs(proto) >= 1536)
502 if (skb->len < sizeof(struct llc_snap_hdr))
503 return htons(ETH_P_802_2);
505 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
508 llc = (struct llc_snap_hdr *) skb->data;
509 if (llc->dsap != LLC_SAP_SNAP ||
510 llc->ssap != LLC_SAP_SNAP ||
511 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
512 return htons(ETH_P_802_2);
514 __skb_pull(skb, sizeof(struct llc_snap_hdr));
515 return llc->ethertype;
518 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
519 int *key_lenp, int nh_len)
521 struct icmp6hdr *icmp = icmp6_hdr(skb);
525 /* The ICMPv6 type and code fields use the 16-bit transport port
526 * fields, so we need to store them in 16-bit network byte order.
528 key->ipv6.tp.src = htons(icmp->icmp6_type);
529 key->ipv6.tp.dst = htons(icmp->icmp6_code);
530 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
532 if (icmp->icmp6_code == 0 &&
533 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
534 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
535 int icmp_len = skb->len - skb_transport_offset(skb);
539 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
541 /* In order to process neighbor discovery options, we need the
544 if (unlikely(icmp_len < sizeof(*nd)))
546 if (unlikely(skb_linearize(skb))) {
551 nd = (struct nd_msg *)skb_transport_header(skb);
552 key->ipv6.nd.target = nd->target;
553 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
555 icmp_len -= sizeof(*nd);
557 while (icmp_len >= 8) {
558 struct nd_opt_hdr *nd_opt =
559 (struct nd_opt_hdr *)(nd->opt + offset);
560 int opt_len = nd_opt->nd_opt_len * 8;
562 if (unlikely(!opt_len || opt_len > icmp_len))
565 /* Store the link layer address if the appropriate
566 * option is provided. It is considered an error if
567 * the same link layer option is specified twice.
569 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
571 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
573 memcpy(key->ipv6.nd.sll,
574 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
575 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
577 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
579 memcpy(key->ipv6.nd.tll,
580 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
591 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
592 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
593 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
601 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
602 * @skb: sk_buff that contains the frame, with skb->data pointing to the
604 * @in_port: port number on which @skb was received.
605 * @key: output flow key
606 * @key_lenp: length of output flow key
608 * The caller must ensure that skb->len >= ETH_HLEN.
610 * Returns 0 if successful, otherwise a negative errno value.
612 * Initializes @skb header pointers as follows:
614 * - skb->mac_header: the Ethernet header.
616 * - skb->network_header: just past the Ethernet header, or just past the
617 * VLAN header, to the first byte of the Ethernet payload.
619 * - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6
620 * on output, then just past the IP header, if one is present and
621 * of a correct length, otherwise the same as skb->network_header.
622 * For other key->dl_type values it is left untouched.
624 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
628 int key_len = SW_FLOW_KEY_OFFSET(eth);
631 memset(key, 0, sizeof(*key));
633 key->phy.priority = skb->priority;
634 key->phy.tun_id = OVS_CB(skb)->tun_id;
635 key->phy.in_port = in_port;
637 skb_reset_mac_header(skb);
639 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
640 * header in the linear data area.
643 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
644 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
646 __skb_pull(skb, 2 * ETH_ALEN);
648 if (vlan_tx_tag_present(skb))
649 key->eth.tci = htons(vlan_get_tci(skb));
650 else if (eth->h_proto == htons(ETH_P_8021Q))
651 if (unlikely(parse_vlan(skb, key)))
654 key->eth.type = parse_ethertype(skb);
655 if (unlikely(key->eth.type == htons(0)))
658 skb_reset_network_header(skb);
659 __skb_push(skb, skb->data - skb_mac_header(skb));
662 if (key->eth.type == htons(ETH_P_IP)) {
666 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
668 error = check_iphdr(skb);
669 if (unlikely(error)) {
670 if (error == -EINVAL) {
671 skb->transport_header = skb->network_header;
678 key->ipv4.addr.src = nh->saddr;
679 key->ipv4.addr.dst = nh->daddr;
681 key->ip.proto = nh->protocol;
682 key->ip.tos = nh->tos;
683 key->ip.ttl = nh->ttl;
685 offset = nh->frag_off & htons(IP_OFFSET);
687 key->ip.frag = OVS_FRAG_TYPE_LATER;
690 if (nh->frag_off & htons(IP_MF) ||
691 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
692 key->ip.frag = OVS_FRAG_TYPE_FIRST;
694 /* Transport layer. */
695 if (key->ip.proto == IPPROTO_TCP) {
696 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
697 if (tcphdr_ok(skb)) {
698 struct tcphdr *tcp = tcp_hdr(skb);
699 key->ipv4.tp.src = tcp->source;
700 key->ipv4.tp.dst = tcp->dest;
702 } else if (key->ip.proto == IPPROTO_UDP) {
703 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
704 if (udphdr_ok(skb)) {
705 struct udphdr *udp = udp_hdr(skb);
706 key->ipv4.tp.src = udp->source;
707 key->ipv4.tp.dst = udp->dest;
709 } else if (key->ip.proto == IPPROTO_ICMP) {
710 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
711 if (icmphdr_ok(skb)) {
712 struct icmphdr *icmp = icmp_hdr(skb);
713 /* The ICMP type and code fields use the 16-bit
714 * transport port fields, so we need to store
715 * them in 16-bit network byte order. */
716 key->ipv4.tp.src = htons(icmp->type);
717 key->ipv4.tp.dst = htons(icmp->code);
721 } else if (key->eth.type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
722 struct arp_eth_header *arp;
724 arp = (struct arp_eth_header *)skb_network_header(skb);
726 if (arp->ar_hrd == htons(ARPHRD_ETHER)
727 && arp->ar_pro == htons(ETH_P_IP)
728 && arp->ar_hln == ETH_ALEN
729 && arp->ar_pln == 4) {
731 /* We only match on the lower 8 bits of the opcode. */
732 if (ntohs(arp->ar_op) <= 0xff)
733 key->ip.proto = ntohs(arp->ar_op);
735 if (key->ip.proto == ARPOP_REQUEST
736 || key->ip.proto == ARPOP_REPLY) {
737 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
738 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
739 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
740 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
741 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
744 } else if (key->eth.type == htons(ETH_P_IPV6)) {
745 int nh_len; /* IPv6 Header + Extensions */
747 nh_len = parse_ipv6hdr(skb, key, &key_len);
748 if (unlikely(nh_len < 0)) {
749 if (nh_len == -EINVAL)
750 skb->transport_header = skb->network_header;
756 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
758 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
759 key->ip.frag = OVS_FRAG_TYPE_FIRST;
761 /* Transport layer. */
762 if (key->ip.proto == NEXTHDR_TCP) {
763 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
764 if (tcphdr_ok(skb)) {
765 struct tcphdr *tcp = tcp_hdr(skb);
766 key->ipv6.tp.src = tcp->source;
767 key->ipv6.tp.dst = tcp->dest;
769 } else if (key->ip.proto == NEXTHDR_UDP) {
770 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
771 if (udphdr_ok(skb)) {
772 struct udphdr *udp = udp_hdr(skb);
773 key->ipv6.tp.src = udp->source;
774 key->ipv6.tp.dst = udp->dest;
776 } else if (key->ip.proto == NEXTHDR_ICMP) {
777 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
778 if (icmp6hdr_ok(skb)) {
779 error = parse_icmpv6(skb, key, &key_len, nh_len);
791 u32 ovs_flow_hash(const struct sw_flow_key *key, int key_len)
793 return jhash2((u32 *)key, DIV_ROUND_UP(key_len, sizeof(u32)), 0);
796 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table,
797 struct sw_flow_key *key, int key_len)
799 struct sw_flow *flow;
800 struct hlist_node *n;
801 struct hlist_head *head;
804 hash = ovs_flow_hash(key, key_len);
806 head = find_bucket(table, hash);
807 hlist_for_each_entry_rcu(flow, n, head, hash_node[table->node_ver]) {
809 if (flow->hash == hash &&
810 !memcmp(&flow->key, key, key_len)) {
817 void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
819 struct hlist_head *head;
821 head = find_bucket(table, flow->hash);
822 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
826 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
828 hlist_del_rcu(&flow->hash_node[table->node_ver]);
830 BUG_ON(table->count < 0);
833 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
834 const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
835 [OVS_KEY_ATTR_ENCAP] = -1,
836 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
837 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
838 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
839 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
840 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
841 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
842 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
843 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
844 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
845 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
846 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
847 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
848 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
851 [OVS_KEY_ATTR_TUN_ID] = sizeof(__be64),
854 static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
855 const struct nlattr *a[], u64 *attrs)
857 const struct ovs_key_icmp *icmp_key;
858 const struct ovs_key_tcp *tcp_key;
859 const struct ovs_key_udp *udp_key;
861 switch (swkey->ip.proto) {
863 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
865 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
867 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
868 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
869 swkey->ipv4.tp.src = tcp_key->tcp_src;
870 swkey->ipv4.tp.dst = tcp_key->tcp_dst;
874 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
876 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
878 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
879 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
880 swkey->ipv4.tp.src = udp_key->udp_src;
881 swkey->ipv4.tp.dst = udp_key->udp_dst;
885 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
887 *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
889 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
890 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
891 swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
892 swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
899 static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
900 const struct nlattr *a[], u64 *attrs)
902 const struct ovs_key_icmpv6 *icmpv6_key;
903 const struct ovs_key_tcp *tcp_key;
904 const struct ovs_key_udp *udp_key;
906 switch (swkey->ip.proto) {
908 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
910 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
912 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
913 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
914 swkey->ipv6.tp.src = tcp_key->tcp_src;
915 swkey->ipv6.tp.dst = tcp_key->tcp_dst;
919 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
921 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
923 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
924 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
925 swkey->ipv6.tp.src = udp_key->udp_src;
926 swkey->ipv6.tp.dst = udp_key->udp_dst;
930 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
932 *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
934 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
935 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
936 swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
937 swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
939 if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
940 swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
941 const struct ovs_key_nd *nd_key;
943 if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
945 *attrs &= ~(1 << OVS_KEY_ATTR_ND);
947 *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
948 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
949 memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
950 sizeof(swkey->ipv6.nd.target));
951 memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
952 memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
960 static int parse_flow_nlattrs(const struct nlattr *attr,
961 const struct nlattr *a[], u64 *attrsp)
963 const struct nlattr *nla;
968 nla_for_each_nested(nla, attr, rem) {
969 u16 type = nla_type(nla);
972 if (type > OVS_KEY_ATTR_MAX || attrs & (1ULL << type))
975 expected_len = ovs_key_lens[type];
976 if (nla_len(nla) != expected_len && expected_len != -1)
979 attrs |= 1ULL << type;
990 * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
991 * @swkey: receives the extracted flow key.
992 * @key_lenp: number of bytes used in @swkey.
993 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
996 int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
997 const struct nlattr *attr)
999 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1000 const struct ovs_key_ethernet *eth_key;
1005 memset(swkey, 0, sizeof(struct sw_flow_key));
1006 key_len = SW_FLOW_KEY_OFFSET(eth);
1008 err = parse_flow_nlattrs(attr, a, &attrs);
1012 /* Metadata attributes. */
1013 if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1014 swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
1015 attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1017 if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1018 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1019 if (in_port >= DP_MAX_PORTS)
1021 swkey->phy.in_port = in_port;
1022 attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1024 swkey->phy.in_port = USHRT_MAX;
1027 if (attrs & (1ULL << OVS_KEY_ATTR_TUN_ID)) {
1028 swkey->phy.tun_id = nla_get_be64(a[OVS_KEY_ATTR_TUN_ID]);
1029 attrs &= ~(1ULL << OVS_KEY_ATTR_TUN_ID);
1032 /* Data attributes. */
1033 if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
1035 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1037 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1038 memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
1039 memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
1041 if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
1042 nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
1043 const struct nlattr *encap;
1046 if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
1047 (1 << OVS_KEY_ATTR_ETHERTYPE) |
1048 (1 << OVS_KEY_ATTR_ENCAP)))
1051 encap = a[OVS_KEY_ATTR_ENCAP];
1052 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1053 if (tci & htons(VLAN_TAG_PRESENT)) {
1054 swkey->eth.tci = tci;
1056 err = parse_flow_nlattrs(encap, a, &attrs);
1060 /* Corner case for truncated 802.1Q header. */
1064 swkey->eth.type = htons(ETH_P_8021Q);
1065 *key_lenp = key_len;
1072 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1073 swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1074 if (ntohs(swkey->eth.type) < 1536)
1076 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1078 swkey->eth.type = htons(ETH_P_802_2);
1081 if (swkey->eth.type == htons(ETH_P_IP)) {
1082 const struct ovs_key_ipv4 *ipv4_key;
1084 if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
1086 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1088 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
1089 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1090 if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
1092 swkey->ip.proto = ipv4_key->ipv4_proto;
1093 swkey->ip.tos = ipv4_key->ipv4_tos;
1094 swkey->ip.ttl = ipv4_key->ipv4_ttl;
1095 swkey->ip.frag = ipv4_key->ipv4_frag;
1096 swkey->ipv4.addr.src = ipv4_key->ipv4_src;
1097 swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
1099 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1100 err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1104 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1105 const struct ovs_key_ipv6 *ipv6_key;
1107 if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
1109 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1111 key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
1112 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1113 if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
1115 swkey->ipv6.label = ipv6_key->ipv6_label;
1116 swkey->ip.proto = ipv6_key->ipv6_proto;
1117 swkey->ip.tos = ipv6_key->ipv6_tclass;
1118 swkey->ip.ttl = ipv6_key->ipv6_hlimit;
1119 swkey->ip.frag = ipv6_key->ipv6_frag;
1120 memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
1121 sizeof(swkey->ipv6.addr.src));
1122 memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
1123 sizeof(swkey->ipv6.addr.dst));
1125 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1126 err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1130 } else if (swkey->eth.type == htons(ETH_P_ARP)) {
1131 const struct ovs_key_arp *arp_key;
1133 if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
1135 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1137 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
1138 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1139 swkey->ipv4.addr.src = arp_key->arp_sip;
1140 swkey->ipv4.addr.dst = arp_key->arp_tip;
1141 if (arp_key->arp_op & htons(0xff00))
1143 swkey->ip.proto = ntohs(arp_key->arp_op);
1144 memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
1145 memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
1150 *key_lenp = key_len;
1156 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1157 * @in_port: receives the extracted input port.
1158 * @tun_id: receives the extracted tunnel ID.
1159 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1162 * This parses a series of Netlink attributes that form a flow key, which must
1163 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1164 * get the metadata, that is, the parts of the flow key that cannot be
1165 * extracted from the packet itself.
1167 int ovs_flow_metadata_from_nlattrs(u32 *priority, u16 *in_port, __be64 *tun_id,
1168 const struct nlattr *attr)
1170 const struct nlattr *nla;
1173 *in_port = USHRT_MAX;
1177 nla_for_each_nested(nla, attr, rem) {
1178 int type = nla_type(nla);
1180 if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
1181 if (nla_len(nla) != ovs_key_lens[type])
1185 case OVS_KEY_ATTR_PRIORITY:
1186 *priority = nla_get_u32(nla);
1189 case OVS_KEY_ATTR_TUN_ID:
1190 *tun_id = nla_get_be64(nla);
1193 case OVS_KEY_ATTR_IN_PORT:
1194 if (nla_get_u32(nla) >= DP_MAX_PORTS)
1196 *in_port = nla_get_u32(nla);
1206 int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
1208 struct ovs_key_ethernet *eth_key;
1209 struct nlattr *nla, *encap;
1211 if (swkey->phy.priority)
1212 NLA_PUT_U32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority);
1214 if (swkey->phy.tun_id != cpu_to_be64(0))
1215 NLA_PUT_BE64(skb, OVS_KEY_ATTR_TUN_ID, swkey->phy.tun_id);
1217 if (swkey->phy.in_port != USHRT_MAX)
1218 NLA_PUT_U32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port);
1220 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1222 goto nla_put_failure;
1223 eth_key = nla_data(nla);
1224 memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
1225 memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
1227 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1228 NLA_PUT_BE16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q));
1229 NLA_PUT_BE16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci);
1230 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1231 if (!swkey->eth.tci)
1237 if (swkey->eth.type == htons(ETH_P_802_2))
1240 NLA_PUT_BE16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type);
1242 if (swkey->eth.type == htons(ETH_P_IP)) {
1243 struct ovs_key_ipv4 *ipv4_key;
1245 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1247 goto nla_put_failure;
1248 ipv4_key = nla_data(nla);
1249 ipv4_key->ipv4_src = swkey->ipv4.addr.src;
1250 ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
1251 ipv4_key->ipv4_proto = swkey->ip.proto;
1252 ipv4_key->ipv4_tos = swkey->ip.tos;
1253 ipv4_key->ipv4_ttl = swkey->ip.ttl;
1254 ipv4_key->ipv4_frag = swkey->ip.frag;
1255 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1256 struct ovs_key_ipv6 *ipv6_key;
1258 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1260 goto nla_put_failure;
1261 ipv6_key = nla_data(nla);
1262 memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
1263 sizeof(ipv6_key->ipv6_src));
1264 memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
1265 sizeof(ipv6_key->ipv6_dst));
1266 ipv6_key->ipv6_label = swkey->ipv6.label;
1267 ipv6_key->ipv6_proto = swkey->ip.proto;
1268 ipv6_key->ipv6_tclass = swkey->ip.tos;
1269 ipv6_key->ipv6_hlimit = swkey->ip.ttl;
1270 ipv6_key->ipv6_frag = swkey->ip.frag;
1271 } else if (swkey->eth.type == htons(ETH_P_ARP)) {
1272 struct ovs_key_arp *arp_key;
1274 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1276 goto nla_put_failure;
1277 arp_key = nla_data(nla);
1278 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1279 arp_key->arp_sip = swkey->ipv4.addr.src;
1280 arp_key->arp_tip = swkey->ipv4.addr.dst;
1281 arp_key->arp_op = htons(swkey->ip.proto);
1282 memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
1283 memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
1286 if ((swkey->eth.type == htons(ETH_P_IP) ||
1287 swkey->eth.type == htons(ETH_P_IPV6)) &&
1288 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1290 if (swkey->ip.proto == IPPROTO_TCP) {
1291 struct ovs_key_tcp *tcp_key;
1293 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1295 goto nla_put_failure;
1296 tcp_key = nla_data(nla);
1297 if (swkey->eth.type == htons(ETH_P_IP)) {
1298 tcp_key->tcp_src = swkey->ipv4.tp.src;
1299 tcp_key->tcp_dst = swkey->ipv4.tp.dst;
1300 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1301 tcp_key->tcp_src = swkey->ipv6.tp.src;
1302 tcp_key->tcp_dst = swkey->ipv6.tp.dst;
1304 } else if (swkey->ip.proto == IPPROTO_UDP) {
1305 struct ovs_key_udp *udp_key;
1307 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1309 goto nla_put_failure;
1310 udp_key = nla_data(nla);
1311 if (swkey->eth.type == htons(ETH_P_IP)) {
1312 udp_key->udp_src = swkey->ipv4.tp.src;
1313 udp_key->udp_dst = swkey->ipv4.tp.dst;
1314 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1315 udp_key->udp_src = swkey->ipv6.tp.src;
1316 udp_key->udp_dst = swkey->ipv6.tp.dst;
1318 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1319 swkey->ip.proto == IPPROTO_ICMP) {
1320 struct ovs_key_icmp *icmp_key;
1322 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1324 goto nla_put_failure;
1325 icmp_key = nla_data(nla);
1326 icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
1327 icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
1328 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1329 swkey->ip.proto == IPPROTO_ICMPV6) {
1330 struct ovs_key_icmpv6 *icmpv6_key;
1332 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1333 sizeof(*icmpv6_key));
1335 goto nla_put_failure;
1336 icmpv6_key = nla_data(nla);
1337 icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
1338 icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
1340 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1341 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1342 struct ovs_key_nd *nd_key;
1344 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1346 goto nla_put_failure;
1347 nd_key = nla_data(nla);
1348 memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
1349 sizeof(nd_key->nd_target));
1350 memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
1351 memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
1358 nla_nest_end(skb, encap);
1366 /* Initializes the flow module.
1367 * Returns zero if successful or a negative error code. */
1368 int ovs_flow_init(void)
1370 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1372 if (flow_cache == NULL)
1378 /* Uninitializes the flow module. */
1379 void ovs_flow_exit(void)
1381 kmem_cache_destroy(flow_cache);