2 * Copyright (c) 2007-2013 Nicira, Inc.
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 void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
51 struct sw_flow_key_range *range, u8 val);
53 static void update_range__(struct sw_flow_match *match,
54 size_t offset, size_t size, bool is_mask)
56 struct sw_flow_key_range *range = NULL;
57 size_t start = offset;
58 size_t end = offset + size;
61 range = &match->range;
63 range = &match->mask->range;
68 if (range->start == range->end) {
74 if (range->start > start)
81 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
83 update_range__(match, offsetof(struct sw_flow_key, field), \
84 sizeof((match)->key->field), is_mask); \
85 if (is_mask && match->mask != NULL) { \
86 (match)->mask->key.field = value; \
88 (match)->key->field = value; \
92 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
94 update_range__(match, offsetof(struct sw_flow_key, field), \
96 if (is_mask && match->mask != NULL) { \
97 memcpy(&(match)->mask->key.field, value_p, len); \
99 memcpy(&(match)->key->field, value_p, len); \
103 void ovs_match_init(struct sw_flow_match *match,
104 struct sw_flow_key *key,
105 struct sw_flow_mask *mask)
107 memset(match, 0, sizeof(*match));
111 memset(key, 0, sizeof(*key));
114 memset(&mask->key, 0, sizeof(mask->key));
115 mask->range.start = mask->range.end = 0;
119 static bool ovs_match_validate(const struct sw_flow_match *match,
120 u64 key_attrs, u64 mask_attrs)
122 u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
123 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
125 /* The following mask attributes allowed only if they
126 * pass the validation tests. */
127 mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
128 | (1ULL << OVS_KEY_ATTR_IPV6)
129 | (1ULL << OVS_KEY_ATTR_TCP)
130 | (1ULL << OVS_KEY_ATTR_UDP)
131 | (1ULL << OVS_KEY_ATTR_ICMP)
132 | (1ULL << OVS_KEY_ATTR_ICMPV6)
133 | (1ULL << OVS_KEY_ATTR_ARP)
134 | (1ULL << OVS_KEY_ATTR_ND));
136 if (match->key->phy.in_port == DP_MAX_PORTS &&
137 match->mask && (match->mask->key.phy.in_port == 0xffff))
138 mask_allowed |= (1ULL << OVS_KEY_ATTR_IN_PORT);
140 if (match->key->eth.type == htons(ETH_P_802_2) &&
141 match->mask && (match->mask->key.eth.type == htons(0xffff)))
142 mask_allowed |= (1ULL << OVS_KEY_ATTR_ETHERTYPE);
144 /* Check key attributes. */
145 if (match->key->eth.type == htons(ETH_P_ARP)
146 || match->key->eth.type == htons(ETH_P_RARP)) {
147 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
148 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
149 mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
152 if (match->key->eth.type == htons(ETH_P_IP)) {
153 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
154 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
155 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
157 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
158 if (match->key->ip.proto == IPPROTO_UDP) {
159 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
160 if (match->mask && (match->mask->key.ip.proto == 0xff))
161 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
164 if (match->key->ip.proto == IPPROTO_TCP) {
165 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
166 if (match->mask && (match->mask->key.ip.proto == 0xff))
167 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
170 if (match->key->ip.proto == IPPROTO_ICMP) {
171 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
172 if (match->mask && (match->mask->key.ip.proto == 0xff))
173 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
178 if (match->key->eth.type == htons(ETH_P_IPV6)) {
179 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
180 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
181 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
183 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
184 if (match->key->ip.proto == IPPROTO_UDP) {
185 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
186 if (match->mask && (match->mask->key.ip.proto == 0xff))
187 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
190 if (match->key->ip.proto == IPPROTO_TCP) {
191 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
192 if (match->mask && (match->mask->key.ip.proto == 0xff))
193 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
196 if (match->key->ip.proto == IPPROTO_ICMPV6) {
197 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
198 if (match->mask && (match->mask->key.ip.proto == 0xff))
199 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
201 if (match->key->ipv6.tp.src ==
202 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
203 match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
204 key_expected |= 1ULL << OVS_KEY_ATTR_ND;
205 if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
206 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
212 if ((key_attrs & key_expected) != key_expected) {
213 /* Key attributes check failed. */
214 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
215 key_attrs, key_expected);
219 if ((mask_attrs & mask_allowed) != mask_attrs) {
220 /* Mask attributes check failed. */
221 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
222 mask_attrs, mask_allowed);
229 static int check_header(struct sk_buff *skb, int len)
231 if (unlikely(skb->len < len))
233 if (unlikely(!pskb_may_pull(skb, len)))
238 static bool arphdr_ok(struct sk_buff *skb)
240 return pskb_may_pull(skb, skb_network_offset(skb) +
241 sizeof(struct arp_eth_header));
244 static int check_iphdr(struct sk_buff *skb)
246 unsigned int nh_ofs = skb_network_offset(skb);
250 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
254 ip_len = ip_hdrlen(skb);
255 if (unlikely(ip_len < sizeof(struct iphdr) ||
256 skb->len < nh_ofs + ip_len))
259 skb_set_transport_header(skb, nh_ofs + ip_len);
263 static bool tcphdr_ok(struct sk_buff *skb)
265 int th_ofs = skb_transport_offset(skb);
268 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
271 tcp_len = tcp_hdrlen(skb);
272 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
273 skb->len < th_ofs + tcp_len))
279 static bool udphdr_ok(struct sk_buff *skb)
281 return pskb_may_pull(skb, skb_transport_offset(skb) +
282 sizeof(struct udphdr));
285 static bool icmphdr_ok(struct sk_buff *skb)
287 return pskb_may_pull(skb, skb_transport_offset(skb) +
288 sizeof(struct icmphdr));
291 u64 ovs_flow_used_time(unsigned long flow_jiffies)
293 struct timespec cur_ts;
296 ktime_get_ts(&cur_ts);
297 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
298 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
299 cur_ts.tv_nsec / NSEC_PER_MSEC;
301 return cur_ms - idle_ms;
304 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
306 unsigned int nh_ofs = skb_network_offset(skb);
314 err = check_header(skb, nh_ofs + sizeof(*nh));
319 nexthdr = nh->nexthdr;
320 payload_ofs = (u8 *)(nh + 1) - skb->data;
322 key->ip.proto = NEXTHDR_NONE;
323 key->ip.tos = ipv6_get_dsfield(nh);
324 key->ip.ttl = nh->hop_limit;
325 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
326 key->ipv6.addr.src = nh->saddr;
327 key->ipv6.addr.dst = nh->daddr;
329 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
330 if (unlikely(payload_ofs < 0))
334 if (frag_off & htons(~0x7))
335 key->ip.frag = OVS_FRAG_TYPE_LATER;
337 key->ip.frag = OVS_FRAG_TYPE_FIRST;
340 nh_len = payload_ofs - nh_ofs;
341 skb_set_transport_header(skb, nh_ofs + nh_len);
342 key->ip.proto = nexthdr;
346 static bool icmp6hdr_ok(struct sk_buff *skb)
348 return pskb_may_pull(skb, skb_transport_offset(skb) +
349 sizeof(struct icmp6hdr));
352 static void flow_key_mask(struct sw_flow_key *dst,
353 const struct sw_flow_key *src,
354 const struct sw_flow_mask *mask)
356 u8 *m = (u8 *)&mask->key + mask->range.start;
357 u8 *s = (u8 *)src + mask->range.start;
358 u8 *d = (u8 *)dst + mask->range.start;
361 memset(dst, 0, sizeof(*dst));
362 for (i = 0; i < ovs_sw_flow_mask_size_roundup(mask); i++) {
368 #define TCP_FLAGS_OFFSET 13
369 #define TCP_FLAG_MASK 0x3f
371 void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
375 if ((flow->key.eth.type == htons(ETH_P_IP) ||
376 flow->key.eth.type == htons(ETH_P_IPV6)) &&
377 flow->key.ip.proto == IPPROTO_TCP &&
378 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
379 u8 *tcp = (u8 *)tcp_hdr(skb);
380 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
383 spin_lock(&flow->lock);
384 flow->used = jiffies;
385 flow->packet_count++;
386 flow->byte_count += skb->len;
387 flow->tcp_flags |= tcp_flags;
388 spin_unlock(&flow->lock);
391 struct sw_flow_actions *ovs_flow_actions_alloc(int size)
393 struct sw_flow_actions *sfa;
395 if (size > MAX_ACTIONS_BUFSIZE)
396 return ERR_PTR(-EINVAL);
398 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
400 return ERR_PTR(-ENOMEM);
402 sfa->actions_len = 0;
406 struct sw_flow *ovs_flow_alloc(void)
408 struct sw_flow *flow;
410 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
412 return ERR_PTR(-ENOMEM);
414 spin_lock_init(&flow->lock);
415 flow->sf_acts = NULL;
421 static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
423 hash = jhash_1word(hash, table->hash_seed);
424 return flex_array_get(table->buckets,
425 (hash & (table->n_buckets - 1)));
428 static struct flex_array *alloc_buckets(unsigned int n_buckets)
430 struct flex_array *buckets;
433 buckets = flex_array_alloc(sizeof(struct hlist_head *),
434 n_buckets, GFP_KERNEL);
438 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
440 flex_array_free(buckets);
444 for (i = 0; i < n_buckets; i++)
445 INIT_HLIST_HEAD((struct hlist_head *)
446 flex_array_get(buckets, i));
451 static void free_buckets(struct flex_array *buckets)
453 flex_array_free(buckets);
456 static struct flow_table *__flow_tbl_alloc(int new_size)
458 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
463 table->buckets = alloc_buckets(new_size);
465 if (!table->buckets) {
469 table->n_buckets = new_size;
472 table->keep_flows = false;
473 get_random_bytes(&table->hash_seed, sizeof(u32));
474 table->mask_list = NULL;
479 static void __flow_tbl_destroy(struct flow_table *table)
483 if (table->keep_flows)
486 for (i = 0; i < table->n_buckets; i++) {
487 struct sw_flow *flow;
488 struct hlist_head *head = flex_array_get(table->buckets, i);
489 struct hlist_node *n;
490 int ver = table->node_ver;
492 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
493 hlist_del_rcu(&flow->hash_node[ver]);
494 ovs_flow_free(flow, false);
498 BUG_ON(!list_empty(table->mask_list));
499 kfree(table->mask_list);
502 free_buckets(table->buckets);
506 struct flow_table *ovs_flow_tbl_alloc(int new_size)
508 struct flow_table *table = __flow_tbl_alloc(new_size);
513 table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
514 if (!table->mask_list) {
515 table->keep_flows = true;
516 __flow_tbl_destroy(table);
519 INIT_LIST_HEAD(table->mask_list);
524 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
526 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
528 __flow_tbl_destroy(table);
531 void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
537 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
539 __flow_tbl_destroy(table);
542 struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *last)
544 struct sw_flow *flow;
545 struct hlist_head *head;
549 ver = table->node_ver;
550 while (*bucket < table->n_buckets) {
552 head = flex_array_get(table->buckets, *bucket);
553 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
568 static void __tbl_insert(struct flow_table *table, struct sw_flow *flow)
570 struct hlist_head *head;
572 head = find_bucket(table, flow->hash);
573 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
578 static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
583 old_ver = old->node_ver;
584 new->node_ver = !old_ver;
586 /* Insert in new table. */
587 for (i = 0; i < old->n_buckets; i++) {
588 struct sw_flow *flow;
589 struct hlist_head *head;
591 head = flex_array_get(old->buckets, i);
593 hlist_for_each_entry(flow, head, hash_node[old_ver])
594 __tbl_insert(new, flow);
597 new->mask_list = old->mask_list;
598 old->keep_flows = true;
601 static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
603 struct flow_table *new_table;
605 new_table = __flow_tbl_alloc(n_buckets);
607 return ERR_PTR(-ENOMEM);
609 flow_table_copy_flows(table, new_table);
614 struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
616 return __flow_tbl_rehash(table, table->n_buckets);
619 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
621 return __flow_tbl_rehash(table, table->n_buckets * 2);
624 static void __flow_free(struct sw_flow *flow)
626 kfree((struct sf_flow_acts __force *)flow->sf_acts);
627 kmem_cache_free(flow_cache, flow);
630 static void rcu_free_flow_callback(struct rcu_head *rcu)
632 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
637 void ovs_flow_free(struct sw_flow *flow, bool deferred)
642 ovs_sw_flow_mask_del_ref((struct sw_flow_mask __force *)flow->mask,
646 call_rcu(&flow->rcu, rcu_free_flow_callback);
651 /* RCU callback used by ovs_flow_deferred_free_acts. */
652 static void rcu_free_acts_callback(struct rcu_head *rcu)
654 struct sw_flow_actions *sf_acts = container_of(rcu,
655 struct sw_flow_actions, rcu);
659 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
660 * The caller must hold rcu_read_lock for this to be sensible. */
661 void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
663 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
666 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
669 __be16 eth_type; /* ETH_P_8021Q */
672 struct qtag_prefix *qp;
674 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
677 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
681 qp = (struct qtag_prefix *) skb->data;
682 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
683 __skb_pull(skb, sizeof(struct qtag_prefix));
688 static __be16 parse_ethertype(struct sk_buff *skb)
690 struct llc_snap_hdr {
691 u8 dsap; /* Always 0xAA */
692 u8 ssap; /* Always 0xAA */
697 struct llc_snap_hdr *llc;
700 proto = *(__be16 *) skb->data;
701 __skb_pull(skb, sizeof(__be16));
703 if (ntohs(proto) >= ETH_P_802_3_MIN)
706 if (skb->len < sizeof(struct llc_snap_hdr))
707 return htons(ETH_P_802_2);
709 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
712 llc = (struct llc_snap_hdr *) skb->data;
713 if (llc->dsap != LLC_SAP_SNAP ||
714 llc->ssap != LLC_SAP_SNAP ||
715 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
716 return htons(ETH_P_802_2);
718 __skb_pull(skb, sizeof(struct llc_snap_hdr));
720 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
721 return llc->ethertype;
723 return htons(ETH_P_802_2);
726 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
729 struct icmp6hdr *icmp = icmp6_hdr(skb);
731 /* The ICMPv6 type and code fields use the 16-bit transport port
732 * fields, so we need to store them in 16-bit network byte order.
734 key->ipv6.tp.src = htons(icmp->icmp6_type);
735 key->ipv6.tp.dst = htons(icmp->icmp6_code);
737 if (icmp->icmp6_code == 0 &&
738 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
739 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
740 int icmp_len = skb->len - skb_transport_offset(skb);
744 /* In order to process neighbor discovery options, we need the
747 if (unlikely(icmp_len < sizeof(*nd)))
750 if (unlikely(skb_linearize(skb)))
753 nd = (struct nd_msg *)skb_transport_header(skb);
754 key->ipv6.nd.target = nd->target;
756 icmp_len -= sizeof(*nd);
758 while (icmp_len >= 8) {
759 struct nd_opt_hdr *nd_opt =
760 (struct nd_opt_hdr *)(nd->opt + offset);
761 int opt_len = nd_opt->nd_opt_len * 8;
763 if (unlikely(!opt_len || opt_len > icmp_len))
766 /* Store the link layer address if the appropriate
767 * option is provided. It is considered an error if
768 * the same link layer option is specified twice.
770 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
772 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
774 memcpy(key->ipv6.nd.sll,
775 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
776 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
778 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
780 memcpy(key->ipv6.nd.tll,
781 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
792 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
793 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
794 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
800 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
801 * @skb: sk_buff that contains the frame, with skb->data pointing to the
803 * @in_port: port number on which @skb was received.
804 * @key: output flow key
805 * @key_lenp: length of output flow key
807 * The caller must ensure that skb->len >= ETH_HLEN.
809 * Returns 0 if successful, otherwise a negative errno value.
811 * Initializes @skb header pointers as follows:
813 * - skb->mac_header: the Ethernet header.
815 * - skb->network_header: just past the Ethernet header, or just past the
816 * VLAN header, to the first byte of the Ethernet payload.
818 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
819 * on output, then just past the IP header, if one is present and
820 * of a correct length, otherwise the same as skb->network_header.
821 * For other key->eth.type values it is left untouched.
823 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
828 memset(key, 0, sizeof(*key));
830 key->phy.priority = skb->priority;
831 if (OVS_CB(skb)->tun_key)
832 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
833 key->phy.in_port = in_port;
834 key->phy.skb_mark = skb_get_mark(skb);
836 skb_reset_mac_header(skb);
838 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
839 * header in the linear data area.
842 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
843 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
845 __skb_pull(skb, 2 * ETH_ALEN);
846 /* We are going to push all headers that we pull, so no need to
847 * update skb->csum here. */
849 if (vlan_tx_tag_present(skb))
850 key->eth.tci = htons(vlan_get_tci(skb));
851 else if (eth->h_proto == htons(ETH_P_8021Q))
852 if (unlikely(parse_vlan(skb, key)))
855 key->eth.type = parse_ethertype(skb);
856 if (unlikely(key->eth.type == htons(0)))
859 skb_reset_network_header(skb);
860 __skb_push(skb, skb->data - skb_mac_header(skb));
863 if (key->eth.type == htons(ETH_P_IP)) {
867 error = check_iphdr(skb);
868 if (unlikely(error)) {
869 if (error == -EINVAL) {
870 skb->transport_header = skb->network_header;
877 key->ipv4.addr.src = nh->saddr;
878 key->ipv4.addr.dst = nh->daddr;
880 key->ip.proto = nh->protocol;
881 key->ip.tos = nh->tos;
882 key->ip.ttl = nh->ttl;
884 offset = nh->frag_off & htons(IP_OFFSET);
886 key->ip.frag = OVS_FRAG_TYPE_LATER;
889 if (nh->frag_off & htons(IP_MF) ||
890 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
891 key->ip.frag = OVS_FRAG_TYPE_FIRST;
893 /* Transport layer. */
894 if (key->ip.proto == IPPROTO_TCP) {
895 if (tcphdr_ok(skb)) {
896 struct tcphdr *tcp = tcp_hdr(skb);
897 key->ipv4.tp.src = tcp->source;
898 key->ipv4.tp.dst = tcp->dest;
900 } else if (key->ip.proto == IPPROTO_UDP) {
901 if (udphdr_ok(skb)) {
902 struct udphdr *udp = udp_hdr(skb);
903 key->ipv4.tp.src = udp->source;
904 key->ipv4.tp.dst = udp->dest;
906 } else if (key->ip.proto == IPPROTO_ICMP) {
907 if (icmphdr_ok(skb)) {
908 struct icmphdr *icmp = icmp_hdr(skb);
909 /* The ICMP type and code fields use the 16-bit
910 * transport port fields, so we need to store
911 * them in 16-bit network byte order. */
912 key->ipv4.tp.src = htons(icmp->type);
913 key->ipv4.tp.dst = htons(icmp->code);
917 } else if ((key->eth.type == htons(ETH_P_ARP) ||
918 key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
919 struct arp_eth_header *arp;
921 arp = (struct arp_eth_header *)skb_network_header(skb);
923 if (arp->ar_hrd == htons(ARPHRD_ETHER)
924 && arp->ar_pro == htons(ETH_P_IP)
925 && arp->ar_hln == ETH_ALEN
926 && arp->ar_pln == 4) {
928 /* We only match on the lower 8 bits of the opcode. */
929 if (ntohs(arp->ar_op) <= 0xff)
930 key->ip.proto = ntohs(arp->ar_op);
931 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
932 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
933 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
934 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
936 } else if (key->eth.type == htons(ETH_P_IPV6)) {
937 int nh_len; /* IPv6 Header + Extensions */
939 nh_len = parse_ipv6hdr(skb, key);
940 if (unlikely(nh_len < 0)) {
941 if (nh_len == -EINVAL) {
942 skb->transport_header = skb->network_header;
950 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
952 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
953 key->ip.frag = OVS_FRAG_TYPE_FIRST;
955 /* Transport layer. */
956 if (key->ip.proto == NEXTHDR_TCP) {
957 if (tcphdr_ok(skb)) {
958 struct tcphdr *tcp = tcp_hdr(skb);
959 key->ipv6.tp.src = tcp->source;
960 key->ipv6.tp.dst = tcp->dest;
962 } else if (key->ip.proto == NEXTHDR_UDP) {
963 if (udphdr_ok(skb)) {
964 struct udphdr *udp = udp_hdr(skb);
965 key->ipv6.tp.src = udp->source;
966 key->ipv6.tp.dst = udp->dest;
968 } else if (key->ip.proto == NEXTHDR_ICMP) {
969 if (icmp6hdr_ok(skb)) {
970 error = parse_icmpv6(skb, key, nh_len);
980 static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start, int key_len)
982 return jhash2((u32 *)((u8 *)key + key_start),
983 DIV_ROUND_UP(key_len - key_start, sizeof(u32)), 0);
986 static int flow_key_start(const struct sw_flow_key *key)
988 if (key->tun_key.ipv4_dst)
991 return offsetof(struct sw_flow_key, phy);
994 static bool __cmp_key(const struct sw_flow_key *key1,
995 const struct sw_flow_key *key2, int key_start, int key_len)
997 return !memcmp((u8 *)key1 + key_start,
998 (u8 *)key2 + key_start, (key_len - key_start));
1001 static bool __flow_cmp_key(const struct sw_flow *flow,
1002 const struct sw_flow_key *key, int key_start, int key_len)
1004 return __cmp_key(&flow->key, key, key_start, key_len);
1007 static bool __flow_cmp_unmasked_key(const struct sw_flow *flow,
1008 const struct sw_flow_key *key, int key_start, int key_len)
1010 return __cmp_key(&flow->unmasked_key, key, key_start, key_len);
1013 bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
1014 const struct sw_flow_key *key, int key_len)
1017 key_start = flow_key_start(key);
1019 return __flow_cmp_unmasked_key(flow, key, key_start, key_len);
1023 struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
1024 struct sw_flow_match *match)
1026 struct sw_flow_key *unmasked = match->key;
1027 int key_len = match->range.end;
1028 struct sw_flow *flow;
1030 flow = ovs_flow_lookup(table, unmasked);
1031 if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_len)))
1037 static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
1038 const struct sw_flow_key *flow_key,
1039 struct sw_flow_mask *mask)
1041 struct sw_flow *flow;
1042 struct hlist_head *head;
1043 int key_start = mask->range.start;
1044 int key_len = mask->range.end;
1046 struct sw_flow_key masked_key;
1048 flow_key_mask(&masked_key, flow_key, mask);
1049 hash = ovs_flow_hash(&masked_key, key_start, key_len);
1050 head = find_bucket(table, hash);
1051 hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
1052 if (__flow_cmp_key(flow, &masked_key, key_start, key_len))
1058 struct sw_flow *ovs_flow_lookup(struct flow_table *tbl,
1059 const struct sw_flow_key *key)
1061 struct sw_flow *flow = NULL;
1062 struct sw_flow_mask *mask;
1064 list_for_each_entry_rcu(mask, tbl->mask_list, list) {
1065 flow = ovs_masked_flow_lookup(tbl, key, mask);
1066 if (flow) /* Found */
1074 void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow,
1075 const struct sw_flow_key *key, int key_len)
1077 flow->unmasked_key = *key;
1078 flow_key_mask(&flow->key, &flow->unmasked_key, ovsl_dereference(flow->mask));
1079 flow->hash = ovs_flow_hash(&flow->key,
1080 ovsl_dereference(flow->mask)->range.start,
1081 ovsl_dereference(flow->mask)->range.end);
1082 __tbl_insert(table, flow);
1085 void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow)
1087 BUG_ON(table->count == 0);
1088 hlist_del_rcu(&flow->hash_node[table->node_ver]);
1092 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
1093 const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
1094 [OVS_KEY_ATTR_ENCAP] = -1,
1095 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
1096 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
1097 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
1098 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
1099 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
1100 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
1101 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
1102 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
1103 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
1104 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
1105 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
1106 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
1107 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
1108 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
1109 [OVS_KEY_ATTR_TUNNEL] = -1,
1112 static bool is_all_zero(const u8 *fp, size_t size)
1119 for (i = 0; i < size; i++)
1126 static int __parse_flow_nlattrs(const struct nlattr *attr,
1127 const struct nlattr *a[],
1128 u64 *attrsp, bool nz)
1130 const struct nlattr *nla;
1135 nla_for_each_nested(nla, attr, rem) {
1136 u16 type = nla_type(nla);
1139 if (type > OVS_KEY_ATTR_MAX) {
1140 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
1141 type, OVS_KEY_ATTR_MAX);
1144 if (attrs & (1ULL << type)) {
1145 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
1149 expected_len = ovs_key_lens[type];
1150 if (nla_len(nla) != expected_len && expected_len != -1) {
1151 OVS_NLERR("Key attribute has unexpected length (type=%d"
1152 ", length=%d, expected=%d).\n", type,
1153 nla_len(nla), expected_len);
1157 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
1158 attrs |= 1ULL << type;
1163 OVS_NLERR("Message has %d unknown bytes.\n", rem);
1171 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
1172 const struct nlattr *a[], u64 *attrsp)
1174 return __parse_flow_nlattrs(attr, a, attrsp, true);
1177 static int parse_flow_nlattrs(const struct nlattr *attr,
1178 const struct nlattr *a[], u64 *attrsp)
1180 return __parse_flow_nlattrs(attr, a, attrsp, false);
1183 int ipv4_tun_from_nlattr(const struct nlattr *attr,
1184 struct sw_flow_match *match, bool is_mask)
1189 __be16 tun_flags = 0;
1191 nla_for_each_nested(a, attr, rem) {
1192 int type = nla_type(a);
1193 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1194 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
1195 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
1196 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
1197 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
1198 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
1199 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
1200 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
1203 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
1204 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
1205 type, OVS_TUNNEL_KEY_ATTR_MAX);
1209 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
1210 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
1211 " legnth (type=%d, length=%d, expected=%d).\n",
1212 type, nla_len(a), ovs_tunnel_key_lens[type]);
1217 case OVS_TUNNEL_KEY_ATTR_ID:
1218 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
1219 nla_get_be64(a), is_mask);
1220 tun_flags |= TUNNEL_KEY;
1222 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1223 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
1224 nla_get_be32(a), is_mask);
1226 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1227 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
1228 nla_get_be32(a), is_mask);
1230 case OVS_TUNNEL_KEY_ATTR_TOS:
1231 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
1232 nla_get_u8(a), is_mask);
1234 case OVS_TUNNEL_KEY_ATTR_TTL:
1235 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
1236 nla_get_u8(a), is_mask);
1239 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1240 tun_flags |= TUNNEL_DONT_FRAGMENT;
1242 case OVS_TUNNEL_KEY_ATTR_CSUM:
1243 tun_flags |= TUNNEL_CSUM;
1250 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
1253 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
1257 if (!match->key->tun_key.ipv4_dst) {
1258 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
1263 OVS_NLERR("IPv4 tunnel TTL is zero.\n");
1270 int ipv4_tun_to_nlattr(struct sk_buff *skb,
1271 const struct ovs_key_ipv4_tunnel *tun_key,
1272 const struct ovs_key_ipv4_tunnel *output)
1276 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
1280 if (output->tun_flags & TUNNEL_KEY &&
1281 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
1283 if (output->ipv4_src &&
1284 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
1286 if (output->ipv4_dst &&
1287 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
1289 if (output->ipv4_tos &&
1290 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
1292 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
1294 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
1295 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
1297 if ((output->tun_flags & TUNNEL_CSUM) &&
1298 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
1301 nla_nest_end(skb, nla);
1306 static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
1307 const struct nlattr **a, bool is_mask)
1309 if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
1310 SW_FLOW_KEY_PUT(match, phy.priority,
1311 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1312 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
1315 if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
1316 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1318 if (!is_mask && in_port >= DP_MAX_PORTS)
1320 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1321 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
1322 } else if (!is_mask) {
1323 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1326 if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
1327 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1328 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
1329 if (!is_mask && mark != 0) {
1330 OVS_NLERR("skb->mark must be zero on this kernel (mark=%d).\n", mark);
1334 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1335 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
1337 if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
1338 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1341 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
1346 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
1347 const struct nlattr **a, bool is_mask)
1350 u64 orig_attrs = attrs;
1352 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
1356 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
1357 const struct ovs_key_ethernet *eth_key;
1359 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1360 SW_FLOW_KEY_MEMCPY(match, eth.src,
1361 eth_key->eth_src, ETH_ALEN, is_mask);
1362 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1363 eth_key->eth_dst, ETH_ALEN, is_mask);
1364 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
1367 if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
1370 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1372 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1373 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
1377 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
1378 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
1381 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
1384 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1385 if (!is_mask && ntohs(eth_type) < ETH_P_802_3_MIN) {
1386 OVS_NLERR("EtherType is less than mimimum (type=%x, min=%x).\n",
1387 ntohs(eth_type), ETH_P_802_3_MIN);
1391 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1392 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1393 } else if (!is_mask) {
1394 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1397 if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1398 const struct ovs_key_ipv4 *ipv4_key;
1400 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1401 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
1402 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
1403 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
1406 SW_FLOW_KEY_PUT(match, ip.proto,
1407 ipv4_key->ipv4_proto, is_mask);
1408 SW_FLOW_KEY_PUT(match, ip.tos,
1409 ipv4_key->ipv4_tos, is_mask);
1410 SW_FLOW_KEY_PUT(match, ip.ttl,
1411 ipv4_key->ipv4_ttl, is_mask);
1412 SW_FLOW_KEY_PUT(match, ip.frag,
1413 ipv4_key->ipv4_frag, is_mask);
1414 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1415 ipv4_key->ipv4_src, is_mask);
1416 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1417 ipv4_key->ipv4_dst, is_mask);
1418 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
1421 if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
1422 const struct ovs_key_ipv6 *ipv6_key;
1424 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1425 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
1426 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
1427 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
1430 SW_FLOW_KEY_PUT(match, ipv6.label,
1431 ipv6_key->ipv6_label, is_mask);
1432 SW_FLOW_KEY_PUT(match, ip.proto,
1433 ipv6_key->ipv6_proto, is_mask);
1434 SW_FLOW_KEY_PUT(match, ip.tos,
1435 ipv6_key->ipv6_tclass, is_mask);
1436 SW_FLOW_KEY_PUT(match, ip.ttl,
1437 ipv6_key->ipv6_hlimit, is_mask);
1438 SW_FLOW_KEY_PUT(match, ip.frag,
1439 ipv6_key->ipv6_frag, is_mask);
1440 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1442 sizeof(match->key->ipv6.addr.src),
1444 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1446 sizeof(match->key->ipv6.addr.dst),
1449 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
1452 if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
1453 const struct ovs_key_arp *arp_key;
1455 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1456 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
1457 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
1462 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1463 arp_key->arp_sip, is_mask);
1464 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1465 arp_key->arp_tip, is_mask);
1466 SW_FLOW_KEY_PUT(match, ip.proto,
1467 ntohs(arp_key->arp_op), is_mask);
1468 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1469 arp_key->arp_sha, ETH_ALEN, is_mask);
1470 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1471 arp_key->arp_tha, ETH_ALEN, is_mask);
1473 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
1476 if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
1477 const struct ovs_key_tcp *tcp_key;
1479 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1480 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1481 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1482 tcp_key->tcp_src, is_mask);
1483 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1484 tcp_key->tcp_dst, is_mask);
1486 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1487 tcp_key->tcp_src, is_mask);
1488 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1489 tcp_key->tcp_dst, is_mask);
1491 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
1494 if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
1495 const struct ovs_key_udp *udp_key;
1497 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1498 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1499 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1500 udp_key->udp_src, is_mask);
1501 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1502 udp_key->udp_dst, is_mask);
1504 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1505 udp_key->udp_src, is_mask);
1506 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1507 udp_key->udp_dst, is_mask);
1509 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
1512 if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
1513 const struct ovs_key_icmp *icmp_key;
1515 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1516 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1517 htons(icmp_key->icmp_type), is_mask);
1518 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1519 htons(icmp_key->icmp_code), is_mask);
1520 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
1523 if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
1524 const struct ovs_key_icmpv6 *icmpv6_key;
1526 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1527 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1528 htons(icmpv6_key->icmpv6_type), is_mask);
1529 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1530 htons(icmpv6_key->icmpv6_code), is_mask);
1531 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
1534 if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
1535 const struct ovs_key_nd *nd_key;
1537 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1538 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1540 sizeof(match->key->ipv6.nd.target),
1542 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1543 nd_key->nd_sll, ETH_ALEN, is_mask);
1544 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1545 nd_key->nd_tll, ETH_ALEN, is_mask);
1546 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
1556 * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and
1557 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1558 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1559 * does not include any don't care bit.
1560 * @match: receives the extracted flow match information.
1561 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1562 * sequence. The fields should of the packet that triggered the creation
1564 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1565 * attribute specifies the mask field of the wildcarded flow.
1567 int ovs_match_from_nlattrs(struct sw_flow_match *match,
1568 const struct nlattr *key,
1569 const struct nlattr *mask)
1571 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1572 const struct nlattr *encap;
1575 bool encap_valid = false;
1578 err = parse_flow_nlattrs(key, a, &key_attrs);
1582 if (key_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
1583 encap = a[OVS_KEY_ATTR_ENCAP];
1584 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1585 if (nla_len(encap)) {
1586 __be16 eth_type = 0; /* ETH_P_8021Q */
1588 if (a[OVS_KEY_ATTR_ETHERTYPE])
1589 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1591 if ((eth_type == htons(ETH_P_8021Q)) && (a[OVS_KEY_ATTR_VLAN])) {
1593 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1594 err = parse_flow_nlattrs(encap, a, &key_attrs);
1596 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
1605 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
1610 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
1614 if ((mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) && encap_valid) {
1615 __be16 eth_type = 0;
1617 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1618 if (a[OVS_KEY_ATTR_ETHERTYPE])
1619 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1620 if (eth_type == htons(0xffff)) {
1621 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1622 encap = a[OVS_KEY_ATTR_ENCAP];
1623 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
1625 OVS_NLERR("VLAN frames must have an exact match"
1626 " on the TPID (mask=%x).\n",
1635 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
1639 /* Populate exact match flow's key mask. */
1641 ovs_sw_flow_mask_set(match->mask, &match->range, 0xff);
1644 if (!ovs_match_validate(match, key_attrs, mask_attrs))
1651 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1652 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
1653 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1656 * This parses a series of Netlink attributes that form a flow key, which must
1657 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1658 * get the metadata, that is, the parts of the flow key that cannot be
1659 * extracted from the packet itself.
1662 int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
1663 const struct nlattr *attr)
1665 struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
1666 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1669 struct sw_flow_match match;
1671 flow->key.phy.in_port = DP_MAX_PORTS;
1672 flow->key.phy.priority = 0;
1673 flow->key.phy.skb_mark = 0;
1674 memset(tun_key, 0, sizeof(flow->key.tun_key));
1676 err = parse_flow_nlattrs(attr, a, &attrs);
1680 memset(&match, 0, sizeof(match));
1681 match.key = &flow->key;
1683 err = metadata_from_nlattrs(&match, &attrs, a, false);
1690 int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey,
1691 const struct sw_flow_key *output, struct sk_buff *skb)
1693 struct ovs_key_ethernet *eth_key;
1694 struct nlattr *nla, *encap;
1696 if (output->phy.priority &&
1697 nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1698 goto nla_put_failure;
1700 if (swkey->tun_key.ipv4_dst &&
1701 ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
1702 goto nla_put_failure;
1704 if (swkey->phy.in_port == DP_MAX_PORTS) {
1705 if ((swkey != output) && (output->phy.in_port == 0xffff))
1706 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1707 goto nla_put_failure;
1710 upper_u16 = (swkey == output) ? 0 : 0xffff;
1712 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1713 (upper_u16 << 16) | output->phy.in_port))
1714 goto nla_put_failure;
1717 if (output->phy.skb_mark &&
1718 nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1719 goto nla_put_failure;
1721 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1723 goto nla_put_failure;
1725 eth_key = nla_data(nla);
1726 memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
1727 memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
1729 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1731 eth_type = (swkey == output) ? htons(ETH_P_8021Q) : htons(0xffff) ;
1732 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1733 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1734 goto nla_put_failure;
1735 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1736 if (!swkey->eth.tci)
1741 if (swkey->eth.type == htons(ETH_P_802_2)) {
1743 * Ethertype 802.2 is represented in the netlink with omitted
1744 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1745 * 0xffff in the mask attribute. Ethertype can also
1748 if (swkey != output && output->eth.type)
1749 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1751 goto nla_put_failure;
1755 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1756 goto nla_put_failure;
1758 if (swkey->eth.type == htons(ETH_P_IP)) {
1759 struct ovs_key_ipv4 *ipv4_key;
1761 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1763 goto nla_put_failure;
1764 ipv4_key = nla_data(nla);
1765 ipv4_key->ipv4_src = output->ipv4.addr.src;
1766 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1767 ipv4_key->ipv4_proto = output->ip.proto;
1768 ipv4_key->ipv4_tos = output->ip.tos;
1769 ipv4_key->ipv4_ttl = output->ip.ttl;
1770 ipv4_key->ipv4_frag = output->ip.frag;
1771 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1772 struct ovs_key_ipv6 *ipv6_key;
1774 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1776 goto nla_put_failure;
1777 ipv6_key = nla_data(nla);
1778 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1779 sizeof(ipv6_key->ipv6_src));
1780 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1781 sizeof(ipv6_key->ipv6_dst));
1782 ipv6_key->ipv6_label = output->ipv6.label;
1783 ipv6_key->ipv6_proto = output->ip.proto;
1784 ipv6_key->ipv6_tclass = output->ip.tos;
1785 ipv6_key->ipv6_hlimit = output->ip.ttl;
1786 ipv6_key->ipv6_frag = output->ip.frag;
1787 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1788 swkey->eth.type == htons(ETH_P_RARP)) {
1789 struct ovs_key_arp *arp_key;
1791 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1793 goto nla_put_failure;
1794 arp_key = nla_data(nla);
1795 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1796 arp_key->arp_sip = output->ipv4.addr.src;
1797 arp_key->arp_tip = output->ipv4.addr.dst;
1798 arp_key->arp_op = htons(output->ip.proto);
1799 memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN);
1800 memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN);
1803 if ((swkey->eth.type == htons(ETH_P_IP) ||
1804 swkey->eth.type == htons(ETH_P_IPV6)) &&
1805 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1807 if (swkey->ip.proto == IPPROTO_TCP) {
1808 struct ovs_key_tcp *tcp_key;
1810 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1812 goto nla_put_failure;
1813 tcp_key = nla_data(nla);
1814 if (swkey->eth.type == htons(ETH_P_IP)) {
1815 tcp_key->tcp_src = output->ipv4.tp.src;
1816 tcp_key->tcp_dst = output->ipv4.tp.dst;
1817 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1818 tcp_key->tcp_src = output->ipv6.tp.src;
1819 tcp_key->tcp_dst = output->ipv6.tp.dst;
1821 } else if (swkey->ip.proto == IPPROTO_UDP) {
1822 struct ovs_key_udp *udp_key;
1824 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1826 goto nla_put_failure;
1827 udp_key = nla_data(nla);
1828 if (swkey->eth.type == htons(ETH_P_IP)) {
1829 udp_key->udp_src = output->ipv4.tp.src;
1830 udp_key->udp_dst = output->ipv4.tp.dst;
1831 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1832 udp_key->udp_src = output->ipv6.tp.src;
1833 udp_key->udp_dst = output->ipv6.tp.dst;
1835 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1836 swkey->ip.proto == IPPROTO_ICMP) {
1837 struct ovs_key_icmp *icmp_key;
1839 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1841 goto nla_put_failure;
1842 icmp_key = nla_data(nla);
1843 icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
1844 icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
1845 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1846 swkey->ip.proto == IPPROTO_ICMPV6) {
1847 struct ovs_key_icmpv6 *icmpv6_key;
1849 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1850 sizeof(*icmpv6_key));
1852 goto nla_put_failure;
1853 icmpv6_key = nla_data(nla);
1854 icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
1855 icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
1857 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1858 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1859 struct ovs_key_nd *nd_key;
1861 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1863 goto nla_put_failure;
1864 nd_key = nla_data(nla);
1865 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1866 sizeof(nd_key->nd_target));
1867 memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN);
1868 memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN);
1875 nla_nest_end(skb, encap);
1883 /* Initializes the flow module.
1884 * Returns zero if successful or a negative error code. */
1885 int ovs_flow_init(void)
1887 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1889 if (flow_cache == NULL)
1895 /* Uninitializes the flow module. */
1896 void ovs_flow_exit(void)
1898 kmem_cache_destroy(flow_cache);
1901 struct sw_flow_mask *ovs_sw_flow_mask_alloc(void)
1903 struct sw_flow_mask *mask;
1905 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
1907 mask->ref_count = 0;
1912 void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask)
1917 static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu)
1919 struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu);
1924 void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
1929 BUG_ON(!mask->ref_count);
1932 if (!mask->ref_count) {
1933 list_del_rcu(&mask->list);
1935 call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb);
1941 static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a,
1942 const struct sw_flow_mask *b)
1944 u8 *a_ = (u8 *)&a->key + a->range.start;
1945 u8 *b_ = (u8 *)&b->key + b->range.start;
1947 return (a->range.end == b->range.end)
1948 && (a->range.start == b->range.start)
1949 && (memcmp(a_, b_, ovs_sw_flow_mask_actual_size(a)) == 0);
1952 struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
1953 const struct sw_flow_mask *mask)
1955 struct list_head *ml;
1957 list_for_each(ml, tbl->mask_list) {
1958 struct sw_flow_mask *m;
1959 m = container_of(ml, struct sw_flow_mask, list);
1960 if (ovs_sw_flow_mask_equal(mask, m))
1968 * add a new mask into the mask list.
1969 * The caller needs to make sure that 'mask' is not the same
1970 * as any masks that are already on the list.
1972 void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask)
1974 list_add_rcu(&mask->list, tbl->mask_list);
1978 * Set 'range' fields in the mask to the value of 'val'.
1980 static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
1981 struct sw_flow_key_range *range, u8 val)
1983 u8 *m = (u8 *)&mask->key + range->start;
1985 mask->range = *range;
1986 memset(m, val, ovs_sw_flow_mask_size_roundup(mask));