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 u8 *tcp = (u8 *)tcp_hdr(skb);
190 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
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);
201 struct sw_flow_actions *ovs_flow_actions_alloc(const struct nlattr *actions)
203 int actions_len = nla_len(actions);
204 struct sw_flow_actions *sfa;
206 if (actions_len > MAX_ACTIONS_BUFSIZE)
207 return ERR_PTR(-EINVAL);
209 sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL);
211 return ERR_PTR(-ENOMEM);
213 sfa->actions_len = actions_len;
214 memcpy(sfa->actions, nla_data(actions), actions_len);
218 struct sw_flow *ovs_flow_alloc(void)
220 struct sw_flow *flow;
222 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
224 return ERR_PTR(-ENOMEM);
226 spin_lock_init(&flow->lock);
227 atomic_set(&flow->refcnt, 1);
228 flow->sf_acts = NULL;
234 static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
236 hash = jhash_1word(hash, table->hash_seed);
237 return flex_array_get(table->buckets,
238 (hash & (table->n_buckets - 1)));
241 static struct flex_array *alloc_buckets(unsigned int n_buckets)
243 struct flex_array *buckets;
246 buckets = flex_array_alloc(sizeof(struct hlist_head *),
247 n_buckets, GFP_KERNEL);
251 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
253 flex_array_free(buckets);
257 for (i = 0; i < n_buckets; i++)
258 INIT_HLIST_HEAD((struct hlist_head *)
259 flex_array_get(buckets, i));
264 static void free_buckets(struct flex_array *buckets)
266 flex_array_free(buckets);
269 struct flow_table *ovs_flow_tbl_alloc(int new_size)
271 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
276 table->buckets = alloc_buckets(new_size);
278 if (!table->buckets) {
282 table->n_buckets = new_size;
285 table->keep_flows = false;
286 get_random_bytes(&table->hash_seed, sizeof(u32));
291 static void flow_free(struct sw_flow *flow)
297 void ovs_flow_tbl_destroy(struct flow_table *table)
304 if (table->keep_flows)
307 for (i = 0; i < table->n_buckets; i++) {
308 struct sw_flow *flow;
309 struct hlist_head *head = flex_array_get(table->buckets, i);
310 struct hlist_node *node, *n;
311 int ver = table->node_ver;
313 hlist_for_each_entry_safe(flow, node, n, head, hash_node[ver]) {
314 hlist_del_rcu(&flow->hash_node[ver]);
320 free_buckets(table->buckets);
324 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
326 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
328 ovs_flow_tbl_destroy(table);
331 void ovs_flow_tbl_deferred_destroy(struct flow_table *table)
336 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
339 struct sw_flow *ovs_flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last)
341 struct sw_flow *flow;
342 struct hlist_head *head;
343 struct hlist_node *n;
347 ver = table->node_ver;
348 while (*bucket < table->n_buckets) {
350 head = flex_array_get(table->buckets, *bucket);
351 hlist_for_each_entry_rcu(flow, n, head, hash_node[ver]) {
366 static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
371 old_ver = old->node_ver;
372 new->node_ver = !old_ver;
374 /* Insert in new table. */
375 for (i = 0; i < old->n_buckets; i++) {
376 struct sw_flow *flow;
377 struct hlist_head *head;
378 struct hlist_node *n;
380 head = flex_array_get(old->buckets, i);
382 hlist_for_each_entry(flow, n, head, hash_node[old_ver])
383 ovs_flow_tbl_insert(new, flow);
385 old->keep_flows = true;
388 static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
390 struct flow_table *new_table;
392 new_table = ovs_flow_tbl_alloc(n_buckets);
394 return ERR_PTR(-ENOMEM);
396 flow_table_copy_flows(table, new_table);
401 struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
403 return __flow_tbl_rehash(table, table->n_buckets);
406 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
408 return __flow_tbl_rehash(table, table->n_buckets * 2);
411 /* RCU callback used by ovs_flow_deferred_free. */
412 static void rcu_free_flow_callback(struct rcu_head *rcu)
414 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
420 /* Schedules 'flow' to be freed after the next RCU grace period.
421 * The caller must hold rcu_read_lock for this to be sensible. */
422 void ovs_flow_deferred_free(struct sw_flow *flow)
424 call_rcu(&flow->rcu, rcu_free_flow_callback);
427 void ovs_flow_hold(struct sw_flow *flow)
429 atomic_inc(&flow->refcnt);
432 void ovs_flow_put(struct sw_flow *flow)
437 if (atomic_dec_and_test(&flow->refcnt)) {
438 kfree((struct sf_flow_acts __force *)flow->sf_acts);
439 kmem_cache_free(flow_cache, flow);
443 /* RCU callback used by ovs_flow_deferred_free_acts. */
444 static void rcu_free_acts_callback(struct rcu_head *rcu)
446 struct sw_flow_actions *sf_acts = container_of(rcu,
447 struct sw_flow_actions, rcu);
451 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
452 * The caller must hold rcu_read_lock for this to be sensible. */
453 void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
455 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
458 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
461 __be16 eth_type; /* ETH_P_8021Q */
464 struct qtag_prefix *qp;
466 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
469 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
473 qp = (struct qtag_prefix *) skb->data;
474 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
475 __skb_pull(skb, sizeof(struct qtag_prefix));
480 static __be16 parse_ethertype(struct sk_buff *skb)
482 struct llc_snap_hdr {
483 u8 dsap; /* Always 0xAA */
484 u8 ssap; /* Always 0xAA */
489 struct llc_snap_hdr *llc;
492 proto = *(__be16 *) skb->data;
493 __skb_pull(skb, sizeof(__be16));
495 if (ntohs(proto) >= 1536)
498 if (skb->len < sizeof(struct llc_snap_hdr))
499 return htons(ETH_P_802_2);
501 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
504 llc = (struct llc_snap_hdr *) skb->data;
505 if (llc->dsap != LLC_SAP_SNAP ||
506 llc->ssap != LLC_SAP_SNAP ||
507 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
508 return htons(ETH_P_802_2);
510 __skb_pull(skb, sizeof(struct llc_snap_hdr));
511 return llc->ethertype;
514 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
515 int *key_lenp, int nh_len)
517 struct icmp6hdr *icmp = icmp6_hdr(skb);
521 /* The ICMPv6 type and code fields use the 16-bit transport port
522 * fields, so we need to store them in 16-bit network byte order.
524 key->ipv6.tp.src = htons(icmp->icmp6_type);
525 key->ipv6.tp.dst = htons(icmp->icmp6_code);
526 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
528 if (icmp->icmp6_code == 0 &&
529 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
530 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
531 int icmp_len = skb->len - skb_transport_offset(skb);
535 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
537 /* In order to process neighbor discovery options, we need the
540 if (unlikely(icmp_len < sizeof(*nd)))
542 if (unlikely(skb_linearize(skb))) {
547 nd = (struct nd_msg *)skb_transport_header(skb);
548 key->ipv6.nd.target = nd->target;
549 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
551 icmp_len -= sizeof(*nd);
553 while (icmp_len >= 8) {
554 struct nd_opt_hdr *nd_opt =
555 (struct nd_opt_hdr *)(nd->opt + offset);
556 int opt_len = nd_opt->nd_opt_len * 8;
558 if (unlikely(!opt_len || opt_len > icmp_len))
561 /* Store the link layer address if the appropriate
562 * option is provided. It is considered an error if
563 * the same link layer option is specified twice.
565 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
567 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
569 memcpy(key->ipv6.nd.sll,
570 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
571 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
573 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
575 memcpy(key->ipv6.nd.tll,
576 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
587 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
588 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
589 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
597 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
598 * @skb: sk_buff that contains the frame, with skb->data pointing to the
600 * @in_port: port number on which @skb was received.
601 * @key: output flow key
602 * @key_lenp: length of output flow key
604 * The caller must ensure that skb->len >= ETH_HLEN.
606 * Returns 0 if successful, otherwise a negative errno value.
608 * Initializes @skb header pointers as follows:
610 * - skb->mac_header: the Ethernet header.
612 * - skb->network_header: just past the Ethernet header, or just past the
613 * VLAN header, to the first byte of the Ethernet payload.
615 * - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6
616 * on output, then just past the IP header, if one is present and
617 * of a correct length, otherwise the same as skb->network_header.
618 * For other key->dl_type values it is left untouched.
620 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
624 int key_len = SW_FLOW_KEY_OFFSET(eth);
627 memset(key, 0, sizeof(*key));
629 key->phy.priority = skb->priority;
630 key->phy.tun_id = OVS_CB(skb)->tun_id;
631 key->phy.in_port = in_port;
633 skb_reset_mac_header(skb);
635 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
636 * header in the linear data area.
639 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
640 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
642 __skb_pull(skb, 2 * ETH_ALEN);
644 if (vlan_tx_tag_present(skb))
645 key->eth.tci = htons(vlan_get_tci(skb));
646 else if (eth->h_proto == htons(ETH_P_8021Q))
647 if (unlikely(parse_vlan(skb, key)))
650 key->eth.type = parse_ethertype(skb);
651 if (unlikely(key->eth.type == htons(0)))
654 skb_reset_network_header(skb);
655 __skb_push(skb, skb->data - skb_mac_header(skb));
658 if (key->eth.type == htons(ETH_P_IP)) {
662 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
664 error = check_iphdr(skb);
665 if (unlikely(error)) {
666 if (error == -EINVAL) {
667 skb->transport_header = skb->network_header;
674 key->ipv4.addr.src = nh->saddr;
675 key->ipv4.addr.dst = nh->daddr;
677 key->ip.proto = nh->protocol;
678 key->ip.tos = nh->tos;
679 key->ip.ttl = nh->ttl;
681 offset = nh->frag_off & htons(IP_OFFSET);
683 key->ip.frag = OVS_FRAG_TYPE_LATER;
686 if (nh->frag_off & htons(IP_MF) ||
687 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
688 key->ip.frag = OVS_FRAG_TYPE_FIRST;
690 /* Transport layer. */
691 if (key->ip.proto == IPPROTO_TCP) {
692 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
693 if (tcphdr_ok(skb)) {
694 struct tcphdr *tcp = tcp_hdr(skb);
695 key->ipv4.tp.src = tcp->source;
696 key->ipv4.tp.dst = tcp->dest;
698 } else if (key->ip.proto == IPPROTO_UDP) {
699 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
700 if (udphdr_ok(skb)) {
701 struct udphdr *udp = udp_hdr(skb);
702 key->ipv4.tp.src = udp->source;
703 key->ipv4.tp.dst = udp->dest;
705 } else if (key->ip.proto == IPPROTO_ICMP) {
706 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
707 if (icmphdr_ok(skb)) {
708 struct icmphdr *icmp = icmp_hdr(skb);
709 /* The ICMP type and code fields use the 16-bit
710 * transport port fields, so we need to store
711 * them in 16-bit network byte order. */
712 key->ipv4.tp.src = htons(icmp->type);
713 key->ipv4.tp.dst = htons(icmp->code);
717 } else if (key->eth.type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
718 struct arp_eth_header *arp;
720 arp = (struct arp_eth_header *)skb_network_header(skb);
722 if (arp->ar_hrd == htons(ARPHRD_ETHER)
723 && arp->ar_pro == htons(ETH_P_IP)
724 && arp->ar_hln == ETH_ALEN
725 && arp->ar_pln == 4) {
727 /* We only match on the lower 8 bits of the opcode. */
728 if (ntohs(arp->ar_op) <= 0xff)
729 key->ip.proto = ntohs(arp->ar_op);
731 if (key->ip.proto == ARPOP_REQUEST
732 || key->ip.proto == ARPOP_REPLY) {
733 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
734 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
735 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
736 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
737 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
740 } else if (key->eth.type == htons(ETH_P_IPV6)) {
741 int nh_len; /* IPv6 Header + Extensions */
743 nh_len = parse_ipv6hdr(skb, key, &key_len);
744 if (unlikely(nh_len < 0)) {
745 if (nh_len == -EINVAL)
746 skb->transport_header = skb->network_header;
752 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
754 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
755 key->ip.frag = OVS_FRAG_TYPE_FIRST;
757 /* Transport layer. */
758 if (key->ip.proto == NEXTHDR_TCP) {
759 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
760 if (tcphdr_ok(skb)) {
761 struct tcphdr *tcp = tcp_hdr(skb);
762 key->ipv6.tp.src = tcp->source;
763 key->ipv6.tp.dst = tcp->dest;
765 } else if (key->ip.proto == NEXTHDR_UDP) {
766 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
767 if (udphdr_ok(skb)) {
768 struct udphdr *udp = udp_hdr(skb);
769 key->ipv6.tp.src = udp->source;
770 key->ipv6.tp.dst = udp->dest;
772 } else if (key->ip.proto == NEXTHDR_ICMP) {
773 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
774 if (icmp6hdr_ok(skb)) {
775 error = parse_icmpv6(skb, key, &key_len, nh_len);
787 u32 ovs_flow_hash(const struct sw_flow_key *key, int key_len)
789 return jhash2((u32 *)key, DIV_ROUND_UP(key_len, sizeof(u32)), 0);
792 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table,
793 struct sw_flow_key *key, int key_len)
795 struct sw_flow *flow;
796 struct hlist_node *n;
797 struct hlist_head *head;
800 hash = ovs_flow_hash(key, key_len);
802 head = find_bucket(table, hash);
803 hlist_for_each_entry_rcu(flow, n, head, hash_node[table->node_ver]) {
805 if (flow->hash == hash &&
806 !memcmp(&flow->key, key, key_len)) {
813 void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
815 struct hlist_head *head;
817 head = find_bucket(table, flow->hash);
818 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
822 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
824 hlist_del_rcu(&flow->hash_node[table->node_ver]);
826 BUG_ON(table->count < 0);
829 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
830 const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
831 [OVS_KEY_ATTR_ENCAP] = -1,
832 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
833 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
834 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
835 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
836 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
837 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
838 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
839 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
840 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
841 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
842 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
843 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
844 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
847 [OVS_KEY_ATTR_TUN_ID] = sizeof(__be64),
850 static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
851 const struct nlattr *a[], u64 *attrs)
853 const struct ovs_key_icmp *icmp_key;
854 const struct ovs_key_tcp *tcp_key;
855 const struct ovs_key_udp *udp_key;
857 switch (swkey->ip.proto) {
859 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
861 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
863 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
864 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
865 swkey->ipv4.tp.src = tcp_key->tcp_src;
866 swkey->ipv4.tp.dst = tcp_key->tcp_dst;
870 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
872 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
874 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
875 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
876 swkey->ipv4.tp.src = udp_key->udp_src;
877 swkey->ipv4.tp.dst = udp_key->udp_dst;
881 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
883 *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
885 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
886 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
887 swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
888 swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
895 static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
896 const struct nlattr *a[], u64 *attrs)
898 const struct ovs_key_icmpv6 *icmpv6_key;
899 const struct ovs_key_tcp *tcp_key;
900 const struct ovs_key_udp *udp_key;
902 switch (swkey->ip.proto) {
904 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
906 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
908 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
909 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
910 swkey->ipv6.tp.src = tcp_key->tcp_src;
911 swkey->ipv6.tp.dst = tcp_key->tcp_dst;
915 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
917 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
919 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
920 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
921 swkey->ipv6.tp.src = udp_key->udp_src;
922 swkey->ipv6.tp.dst = udp_key->udp_dst;
926 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
928 *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
930 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
931 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
932 swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
933 swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
935 if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
936 swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
937 const struct ovs_key_nd *nd_key;
939 if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
941 *attrs &= ~(1 << OVS_KEY_ATTR_ND);
943 *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
944 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
945 memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
946 sizeof(swkey->ipv6.nd.target));
947 memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
948 memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
956 static int parse_flow_nlattrs(const struct nlattr *attr,
957 const struct nlattr *a[], u64 *attrsp)
959 const struct nlattr *nla;
964 nla_for_each_nested(nla, attr, rem) {
965 u16 type = nla_type(nla);
968 if (type > OVS_KEY_ATTR_MAX || attrs & (1ULL << type))
971 expected_len = ovs_key_lens[type];
972 if (nla_len(nla) != expected_len && expected_len != -1)
975 attrs |= 1ULL << type;
986 * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
987 * @swkey: receives the extracted flow key.
988 * @key_lenp: number of bytes used in @swkey.
989 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
992 int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
993 const struct nlattr *attr)
995 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
996 const struct ovs_key_ethernet *eth_key;
1001 memset(swkey, 0, sizeof(struct sw_flow_key));
1002 key_len = SW_FLOW_KEY_OFFSET(eth);
1004 err = parse_flow_nlattrs(attr, a, &attrs);
1008 /* Metadata attributes. */
1009 if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1010 swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
1011 attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1013 if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1014 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1015 if (in_port >= DP_MAX_PORTS)
1017 swkey->phy.in_port = in_port;
1018 attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1020 swkey->phy.in_port = DP_MAX_PORTS;
1023 if (attrs & (1ULL << OVS_KEY_ATTR_TUN_ID)) {
1024 swkey->phy.tun_id = nla_get_be64(a[OVS_KEY_ATTR_TUN_ID]);
1025 attrs &= ~(1ULL << OVS_KEY_ATTR_TUN_ID);
1028 /* Data attributes. */
1029 if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
1031 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1033 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1034 memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
1035 memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
1037 if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
1038 nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
1039 const struct nlattr *encap;
1042 if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
1043 (1 << OVS_KEY_ATTR_ETHERTYPE) |
1044 (1 << OVS_KEY_ATTR_ENCAP)))
1047 encap = a[OVS_KEY_ATTR_ENCAP];
1048 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1049 if (tci & htons(VLAN_TAG_PRESENT)) {
1050 swkey->eth.tci = tci;
1052 err = parse_flow_nlattrs(encap, a, &attrs);
1056 /* Corner case for truncated 802.1Q header. */
1060 swkey->eth.type = htons(ETH_P_8021Q);
1061 *key_lenp = key_len;
1068 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1069 swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1070 if (ntohs(swkey->eth.type) < 1536)
1072 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1074 swkey->eth.type = htons(ETH_P_802_2);
1077 if (swkey->eth.type == htons(ETH_P_IP)) {
1078 const struct ovs_key_ipv4 *ipv4_key;
1080 if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
1082 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1084 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
1085 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1086 if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
1088 swkey->ip.proto = ipv4_key->ipv4_proto;
1089 swkey->ip.tos = ipv4_key->ipv4_tos;
1090 swkey->ip.ttl = ipv4_key->ipv4_ttl;
1091 swkey->ip.frag = ipv4_key->ipv4_frag;
1092 swkey->ipv4.addr.src = ipv4_key->ipv4_src;
1093 swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
1095 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1096 err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1100 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1101 const struct ovs_key_ipv6 *ipv6_key;
1103 if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
1105 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1107 key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
1108 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1109 if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
1111 swkey->ipv6.label = ipv6_key->ipv6_label;
1112 swkey->ip.proto = ipv6_key->ipv6_proto;
1113 swkey->ip.tos = ipv6_key->ipv6_tclass;
1114 swkey->ip.ttl = ipv6_key->ipv6_hlimit;
1115 swkey->ip.frag = ipv6_key->ipv6_frag;
1116 memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
1117 sizeof(swkey->ipv6.addr.src));
1118 memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
1119 sizeof(swkey->ipv6.addr.dst));
1121 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1122 err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1126 } else if (swkey->eth.type == htons(ETH_P_ARP)) {
1127 const struct ovs_key_arp *arp_key;
1129 if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
1131 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1133 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
1134 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1135 swkey->ipv4.addr.src = arp_key->arp_sip;
1136 swkey->ipv4.addr.dst = arp_key->arp_tip;
1137 if (arp_key->arp_op & htons(0xff00))
1139 swkey->ip.proto = ntohs(arp_key->arp_op);
1140 memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
1141 memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
1146 *key_lenp = key_len;
1152 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1153 * @in_port: receives the extracted input port.
1154 * @tun_id: receives the extracted tunnel ID.
1155 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1158 * This parses a series of Netlink attributes that form a flow key, which must
1159 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1160 * get the metadata, that is, the parts of the flow key that cannot be
1161 * extracted from the packet itself.
1163 int ovs_flow_metadata_from_nlattrs(u32 *priority, u16 *in_port, __be64 *tun_id,
1164 const struct nlattr *attr)
1166 const struct nlattr *nla;
1169 *in_port = DP_MAX_PORTS;
1173 nla_for_each_nested(nla, attr, rem) {
1174 int type = nla_type(nla);
1176 if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
1177 if (nla_len(nla) != ovs_key_lens[type])
1181 case OVS_KEY_ATTR_PRIORITY:
1182 *priority = nla_get_u32(nla);
1185 case OVS_KEY_ATTR_TUN_ID:
1186 *tun_id = nla_get_be64(nla);
1189 case OVS_KEY_ATTR_IN_PORT:
1190 if (nla_get_u32(nla) >= DP_MAX_PORTS)
1192 *in_port = nla_get_u32(nla);
1202 int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
1204 struct ovs_key_ethernet *eth_key;
1205 struct nlattr *nla, *encap;
1207 if (swkey->phy.priority)
1208 NLA_PUT_U32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority);
1210 if (swkey->phy.tun_id != cpu_to_be64(0))
1211 NLA_PUT_BE64(skb, OVS_KEY_ATTR_TUN_ID, swkey->phy.tun_id);
1213 if (swkey->phy.in_port != DP_MAX_PORTS)
1214 NLA_PUT_U32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port);
1216 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1218 goto nla_put_failure;
1219 eth_key = nla_data(nla);
1220 memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
1221 memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
1223 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1224 NLA_PUT_BE16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q));
1225 NLA_PUT_BE16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci);
1226 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1227 if (!swkey->eth.tci)
1233 if (swkey->eth.type == htons(ETH_P_802_2))
1236 NLA_PUT_BE16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type);
1238 if (swkey->eth.type == htons(ETH_P_IP)) {
1239 struct ovs_key_ipv4 *ipv4_key;
1241 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1243 goto nla_put_failure;
1244 ipv4_key = nla_data(nla);
1245 ipv4_key->ipv4_src = swkey->ipv4.addr.src;
1246 ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
1247 ipv4_key->ipv4_proto = swkey->ip.proto;
1248 ipv4_key->ipv4_tos = swkey->ip.tos;
1249 ipv4_key->ipv4_ttl = swkey->ip.ttl;
1250 ipv4_key->ipv4_frag = swkey->ip.frag;
1251 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1252 struct ovs_key_ipv6 *ipv6_key;
1254 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1256 goto nla_put_failure;
1257 ipv6_key = nla_data(nla);
1258 memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
1259 sizeof(ipv6_key->ipv6_src));
1260 memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
1261 sizeof(ipv6_key->ipv6_dst));
1262 ipv6_key->ipv6_label = swkey->ipv6.label;
1263 ipv6_key->ipv6_proto = swkey->ip.proto;
1264 ipv6_key->ipv6_tclass = swkey->ip.tos;
1265 ipv6_key->ipv6_hlimit = swkey->ip.ttl;
1266 ipv6_key->ipv6_frag = swkey->ip.frag;
1267 } else if (swkey->eth.type == htons(ETH_P_ARP)) {
1268 struct ovs_key_arp *arp_key;
1270 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1272 goto nla_put_failure;
1273 arp_key = nla_data(nla);
1274 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1275 arp_key->arp_sip = swkey->ipv4.addr.src;
1276 arp_key->arp_tip = swkey->ipv4.addr.dst;
1277 arp_key->arp_op = htons(swkey->ip.proto);
1278 memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
1279 memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
1282 if ((swkey->eth.type == htons(ETH_P_IP) ||
1283 swkey->eth.type == htons(ETH_P_IPV6)) &&
1284 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1286 if (swkey->ip.proto == IPPROTO_TCP) {
1287 struct ovs_key_tcp *tcp_key;
1289 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1291 goto nla_put_failure;
1292 tcp_key = nla_data(nla);
1293 if (swkey->eth.type == htons(ETH_P_IP)) {
1294 tcp_key->tcp_src = swkey->ipv4.tp.src;
1295 tcp_key->tcp_dst = swkey->ipv4.tp.dst;
1296 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1297 tcp_key->tcp_src = swkey->ipv6.tp.src;
1298 tcp_key->tcp_dst = swkey->ipv6.tp.dst;
1300 } else if (swkey->ip.proto == IPPROTO_UDP) {
1301 struct ovs_key_udp *udp_key;
1303 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1305 goto nla_put_failure;
1306 udp_key = nla_data(nla);
1307 if (swkey->eth.type == htons(ETH_P_IP)) {
1308 udp_key->udp_src = swkey->ipv4.tp.src;
1309 udp_key->udp_dst = swkey->ipv4.tp.dst;
1310 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1311 udp_key->udp_src = swkey->ipv6.tp.src;
1312 udp_key->udp_dst = swkey->ipv6.tp.dst;
1314 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1315 swkey->ip.proto == IPPROTO_ICMP) {
1316 struct ovs_key_icmp *icmp_key;
1318 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1320 goto nla_put_failure;
1321 icmp_key = nla_data(nla);
1322 icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
1323 icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
1324 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1325 swkey->ip.proto == IPPROTO_ICMPV6) {
1326 struct ovs_key_icmpv6 *icmpv6_key;
1328 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1329 sizeof(*icmpv6_key));
1331 goto nla_put_failure;
1332 icmpv6_key = nla_data(nla);
1333 icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
1334 icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
1336 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1337 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1338 struct ovs_key_nd *nd_key;
1340 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1342 goto nla_put_failure;
1343 nd_key = nla_data(nla);
1344 memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
1345 sizeof(nd_key->nd_target));
1346 memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
1347 memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
1354 nla_nest_end(skb, encap);
1362 /* Initializes the flow module.
1363 * Returns zero if successful or a negative error code. */
1364 int ovs_flow_init(void)
1366 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1368 if (flow_cache == NULL)
1374 /* Uninitializes the flow module. */
1375 void ovs_flow_exit(void)
1377 kmem_cache_destroy(flow_cache);