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/sctp.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 void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
52 struct sw_flow_key_range *range, u8 val);
54 static void update_range__(struct sw_flow_match *match,
55 size_t offset, size_t size, bool is_mask)
57 struct sw_flow_key_range *range = NULL;
58 size_t start = rounddown(offset, sizeof(long));
59 size_t end = roundup(offset + size, sizeof(long));
62 range = &match->range;
64 range = &match->mask->range;
69 if (range->start == range->end) {
75 if (range->start > start)
82 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
84 update_range__(match, offsetof(struct sw_flow_key, field), \
85 sizeof((match)->key->field), is_mask); \
88 (match)->mask->key.field = value; \
90 (match)->key->field = value; \
94 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
96 update_range__(match, offsetof(struct sw_flow_key, field), \
100 memcpy(&(match)->mask->key.field, value_p, len);\
102 memcpy(&(match)->key->field, value_p, len); \
106 static u16 range_n_bytes(const struct sw_flow_key_range *range)
108 return range->end - range->start;
111 void ovs_match_init(struct sw_flow_match *match,
112 struct sw_flow_key *key,
113 struct sw_flow_mask *mask)
115 memset(match, 0, sizeof(*match));
119 memset(key, 0, sizeof(*key));
122 memset(&mask->key, 0, sizeof(mask->key));
123 mask->range.start = mask->range.end = 0;
127 static bool ovs_match_validate(const struct sw_flow_match *match,
128 u64 key_attrs, u64 mask_attrs)
130 u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
131 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
133 /* The following mask attributes allowed only if they
134 * pass the validation tests. */
135 mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
136 | (1ULL << OVS_KEY_ATTR_IPV6)
137 | (1ULL << OVS_KEY_ATTR_TCP)
138 | (1ULL << OVS_KEY_ATTR_UDP)
139 | (1ULL << OVS_KEY_ATTR_SCTP)
140 | (1ULL << OVS_KEY_ATTR_ICMP)
141 | (1ULL << OVS_KEY_ATTR_ICMPV6)
142 | (1ULL << OVS_KEY_ATTR_ARP)
143 | (1ULL << OVS_KEY_ATTR_ND));
145 /* Always allowed mask fields. */
146 mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL)
147 | (1ULL << OVS_KEY_ATTR_IN_PORT)
148 | (1ULL << OVS_KEY_ATTR_ETHERTYPE));
150 /* Check key attributes. */
151 if (match->key->eth.type == htons(ETH_P_ARP)
152 || match->key->eth.type == htons(ETH_P_RARP)) {
153 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
154 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
155 mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
158 if (match->key->eth.type == htons(ETH_P_IP)) {
159 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
160 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
161 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
163 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
164 if (match->key->ip.proto == IPPROTO_UDP) {
165 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
166 if (match->mask && (match->mask->key.ip.proto == 0xff))
167 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
170 if (match->key->ip.proto == IPPROTO_SCTP) {
171 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
172 if (match->mask && (match->mask->key.ip.proto == 0xff))
173 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
176 if (match->key->ip.proto == IPPROTO_TCP) {
177 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
178 if (match->mask && (match->mask->key.ip.proto == 0xff))
179 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
182 if (match->key->ip.proto == IPPROTO_ICMP) {
183 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
184 if (match->mask && (match->mask->key.ip.proto == 0xff))
185 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
190 if (match->key->eth.type == htons(ETH_P_IPV6)) {
191 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
192 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
193 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
195 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
196 if (match->key->ip.proto == IPPROTO_UDP) {
197 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
198 if (match->mask && (match->mask->key.ip.proto == 0xff))
199 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
202 if (match->key->ip.proto == IPPROTO_SCTP) {
203 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
204 if (match->mask && (match->mask->key.ip.proto == 0xff))
205 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
208 if (match->key->ip.proto == IPPROTO_TCP) {
209 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
210 if (match->mask && (match->mask->key.ip.proto == 0xff))
211 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
214 if (match->key->ip.proto == IPPROTO_ICMPV6) {
215 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
216 if (match->mask && (match->mask->key.ip.proto == 0xff))
217 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
219 if (match->key->ipv6.tp.src ==
220 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
221 match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
222 key_expected |= 1ULL << OVS_KEY_ATTR_ND;
223 if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
224 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
230 if ((key_attrs & key_expected) != key_expected) {
231 /* Key attributes check failed. */
232 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
233 key_attrs, key_expected);
237 if ((mask_attrs & mask_allowed) != mask_attrs) {
238 /* Mask attributes check failed. */
239 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
240 mask_attrs, mask_allowed);
247 static int check_header(struct sk_buff *skb, int len)
249 if (unlikely(skb->len < len))
251 if (unlikely(!pskb_may_pull(skb, len)))
256 static bool arphdr_ok(struct sk_buff *skb)
258 return pskb_may_pull(skb, skb_network_offset(skb) +
259 sizeof(struct arp_eth_header));
262 static int check_iphdr(struct sk_buff *skb)
264 unsigned int nh_ofs = skb_network_offset(skb);
268 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
272 ip_len = ip_hdrlen(skb);
273 if (unlikely(ip_len < sizeof(struct iphdr) ||
274 skb->len < nh_ofs + ip_len))
277 skb_set_transport_header(skb, nh_ofs + ip_len);
281 static bool tcphdr_ok(struct sk_buff *skb)
283 int th_ofs = skb_transport_offset(skb);
286 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
289 tcp_len = tcp_hdrlen(skb);
290 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
291 skb->len < th_ofs + tcp_len))
297 static bool udphdr_ok(struct sk_buff *skb)
299 return pskb_may_pull(skb, skb_transport_offset(skb) +
300 sizeof(struct udphdr));
303 static bool sctphdr_ok(struct sk_buff *skb)
305 return pskb_may_pull(skb, skb_transport_offset(skb) +
306 sizeof(struct sctphdr));
309 static bool icmphdr_ok(struct sk_buff *skb)
311 return pskb_may_pull(skb, skb_transport_offset(skb) +
312 sizeof(struct icmphdr));
315 u64 ovs_flow_used_time(unsigned long flow_jiffies)
317 struct timespec cur_ts;
320 ktime_get_ts(&cur_ts);
321 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
322 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
323 cur_ts.tv_nsec / NSEC_PER_MSEC;
325 return cur_ms - idle_ms;
328 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
330 unsigned int nh_ofs = skb_network_offset(skb);
338 err = check_header(skb, nh_ofs + sizeof(*nh));
343 nexthdr = nh->nexthdr;
344 payload_ofs = (u8 *)(nh + 1) - skb->data;
346 key->ip.proto = NEXTHDR_NONE;
347 key->ip.tos = ipv6_get_dsfield(nh);
348 key->ip.ttl = nh->hop_limit;
349 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
350 key->ipv6.addr.src = nh->saddr;
351 key->ipv6.addr.dst = nh->daddr;
353 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
354 if (unlikely(payload_ofs < 0))
358 if (frag_off & htons(~0x7))
359 key->ip.frag = OVS_FRAG_TYPE_LATER;
361 key->ip.frag = OVS_FRAG_TYPE_FIRST;
364 nh_len = payload_ofs - nh_ofs;
365 skb_set_transport_header(skb, nh_ofs + nh_len);
366 key->ip.proto = nexthdr;
370 static bool icmp6hdr_ok(struct sk_buff *skb)
372 return pskb_may_pull(skb, skb_transport_offset(skb) +
373 sizeof(struct icmp6hdr));
376 void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src,
377 const struct sw_flow_mask *mask)
379 const long *m = (long *)((u8 *)&mask->key + mask->range.start);
380 const long *s = (long *)((u8 *)src + mask->range.start);
381 long *d = (long *)((u8 *)dst + mask->range.start);
384 /* The memory outside of the 'mask->range' are not set since
385 * further operations on 'dst' only uses contents within
388 for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long))
392 #define TCP_FLAGS_OFFSET 13
393 #define TCP_FLAG_MASK 0x3f
395 void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
399 if ((flow->key.eth.type == htons(ETH_P_IP) ||
400 flow->key.eth.type == htons(ETH_P_IPV6)) &&
401 flow->key.ip.proto == IPPROTO_TCP &&
402 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
403 u8 *tcp = (u8 *)tcp_hdr(skb);
404 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
407 spin_lock(&flow->lock);
408 flow->used = jiffies;
409 flow->packet_count++;
410 flow->byte_count += skb->len;
411 flow->tcp_flags |= tcp_flags;
412 spin_unlock(&flow->lock);
415 struct sw_flow_actions *ovs_flow_actions_alloc(int size)
417 struct sw_flow_actions *sfa;
419 if (size > MAX_ACTIONS_BUFSIZE)
420 return ERR_PTR(-EINVAL);
422 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
424 return ERR_PTR(-ENOMEM);
426 sfa->actions_len = 0;
430 struct sw_flow *ovs_flow_alloc(void)
432 struct sw_flow *flow;
434 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
436 return ERR_PTR(-ENOMEM);
438 spin_lock_init(&flow->lock);
439 flow->sf_acts = NULL;
445 static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
447 hash = jhash_1word(hash, table->hash_seed);
448 return flex_array_get(table->buckets,
449 (hash & (table->n_buckets - 1)));
452 static struct flex_array *alloc_buckets(unsigned int n_buckets)
454 struct flex_array *buckets;
457 buckets = flex_array_alloc(sizeof(struct hlist_head),
458 n_buckets, GFP_KERNEL);
462 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
464 flex_array_free(buckets);
468 for (i = 0; i < n_buckets; i++)
469 INIT_HLIST_HEAD((struct hlist_head *)
470 flex_array_get(buckets, i));
475 static void free_buckets(struct flex_array *buckets)
477 flex_array_free(buckets);
480 static struct flow_table *__flow_tbl_alloc(int new_size)
482 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
487 table->buckets = alloc_buckets(new_size);
489 if (!table->buckets) {
493 table->n_buckets = new_size;
496 table->keep_flows = false;
497 get_random_bytes(&table->hash_seed, sizeof(u32));
498 table->mask_list = NULL;
503 static void __flow_tbl_destroy(struct flow_table *table)
505 if (!table->keep_flows) {
506 BUG_ON(!list_empty(table->mask_list));
507 kfree(table->mask_list);
510 free_buckets(table->buckets);
514 struct flow_table *ovs_flow_tbl_alloc(int new_size)
516 struct flow_table *table = __flow_tbl_alloc(new_size);
521 table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
522 if (!table->mask_list) {
523 table->keep_flows = true;
524 __flow_tbl_destroy(table);
527 INIT_LIST_HEAD(table->mask_list);
532 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
534 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
536 __flow_tbl_destroy(table);
539 void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
546 if (table->keep_flows)
549 for (i = 0; i < table->n_buckets; i++) {
550 struct sw_flow *flow;
551 struct hlist_head *head = flex_array_get(table->buckets, i);
552 struct hlist_node *n;
553 int ver = table->node_ver;
555 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
556 hlist_del_rcu(&flow->hash_node[ver]);
557 ovs_flow_free(flow, deferred);
563 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
565 __flow_tbl_destroy(table);
568 struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *last)
570 struct sw_flow *flow;
571 struct hlist_head *head;
575 ver = table->node_ver;
576 while (*bucket < table->n_buckets) {
578 head = flex_array_get(table->buckets, *bucket);
579 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
594 static void __tbl_insert(struct flow_table *table, struct sw_flow *flow)
596 struct hlist_head *head;
598 head = find_bucket(table, flow->hash);
599 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
604 static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
609 old_ver = old->node_ver;
610 new->node_ver = !old_ver;
612 /* Insert in new table. */
613 for (i = 0; i < old->n_buckets; i++) {
614 struct sw_flow *flow;
615 struct hlist_head *head;
617 head = flex_array_get(old->buckets, i);
619 hlist_for_each_entry(flow, head, hash_node[old_ver])
620 __tbl_insert(new, flow);
623 new->mask_list = old->mask_list;
624 old->keep_flows = true;
627 static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
629 struct flow_table *new_table;
631 new_table = __flow_tbl_alloc(n_buckets);
633 return ERR_PTR(-ENOMEM);
635 flow_table_copy_flows(table, new_table);
640 struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
642 return __flow_tbl_rehash(table, table->n_buckets);
645 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
647 return __flow_tbl_rehash(table, table->n_buckets * 2);
650 static void __flow_free(struct sw_flow *flow)
652 kfree((struct sf_flow_acts __force *)flow->sf_acts);
653 kmem_cache_free(flow_cache, flow);
656 static void rcu_free_flow_callback(struct rcu_head *rcu)
658 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
663 void ovs_flow_free(struct sw_flow *flow, bool deferred)
668 ovs_sw_flow_mask_del_ref(flow->mask, deferred);
671 call_rcu(&flow->rcu, rcu_free_flow_callback);
676 /* RCU callback used by ovs_flow_deferred_free_acts. */
677 static void rcu_free_acts_callback(struct rcu_head *rcu)
679 struct sw_flow_actions *sf_acts = container_of(rcu,
680 struct sw_flow_actions, rcu);
684 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
685 * The caller must hold rcu_read_lock for this to be sensible. */
686 void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
688 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
691 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
694 __be16 eth_type; /* ETH_P_8021Q */
697 struct qtag_prefix *qp;
699 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
702 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
706 qp = (struct qtag_prefix *) skb->data;
707 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
708 __skb_pull(skb, sizeof(struct qtag_prefix));
713 static __be16 parse_ethertype(struct sk_buff *skb)
715 struct llc_snap_hdr {
716 u8 dsap; /* Always 0xAA */
717 u8 ssap; /* Always 0xAA */
722 struct llc_snap_hdr *llc;
725 proto = *(__be16 *) skb->data;
726 __skb_pull(skb, sizeof(__be16));
728 if (ntohs(proto) >= ETH_P_802_3_MIN)
731 if (skb->len < sizeof(struct llc_snap_hdr))
732 return htons(ETH_P_802_2);
734 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
737 llc = (struct llc_snap_hdr *) skb->data;
738 if (llc->dsap != LLC_SAP_SNAP ||
739 llc->ssap != LLC_SAP_SNAP ||
740 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
741 return htons(ETH_P_802_2);
743 __skb_pull(skb, sizeof(struct llc_snap_hdr));
745 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
746 return llc->ethertype;
748 return htons(ETH_P_802_2);
751 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
754 struct icmp6hdr *icmp = icmp6_hdr(skb);
756 /* The ICMPv6 type and code fields use the 16-bit transport port
757 * fields, so we need to store them in 16-bit network byte order.
759 key->ipv6.tp.src = htons(icmp->icmp6_type);
760 key->ipv6.tp.dst = htons(icmp->icmp6_code);
762 if (icmp->icmp6_code == 0 &&
763 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
764 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
765 int icmp_len = skb->len - skb_transport_offset(skb);
769 /* In order to process neighbor discovery options, we need the
772 if (unlikely(icmp_len < sizeof(*nd)))
775 if (unlikely(skb_linearize(skb)))
778 nd = (struct nd_msg *)skb_transport_header(skb);
779 key->ipv6.nd.target = nd->target;
781 icmp_len -= sizeof(*nd);
783 while (icmp_len >= 8) {
784 struct nd_opt_hdr *nd_opt =
785 (struct nd_opt_hdr *)(nd->opt + offset);
786 int opt_len = nd_opt->nd_opt_len * 8;
788 if (unlikely(!opt_len || opt_len > icmp_len))
791 /* Store the link layer address if the appropriate
792 * option is provided. It is considered an error if
793 * the same link layer option is specified twice.
795 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
797 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
799 memcpy(key->ipv6.nd.sll,
800 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
801 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
803 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
805 memcpy(key->ipv6.nd.tll,
806 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
817 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
818 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
819 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
825 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
826 * @skb: sk_buff that contains the frame, with skb->data pointing to the
828 * @in_port: port number on which @skb was received.
829 * @key: output flow key
831 * The caller must ensure that skb->len >= ETH_HLEN.
833 * Returns 0 if successful, otherwise a negative errno value.
835 * Initializes @skb header pointers as follows:
837 * - skb->mac_header: the Ethernet header.
839 * - skb->network_header: just past the Ethernet header, or just past the
840 * VLAN header, to the first byte of the Ethernet payload.
842 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
843 * on output, then just past the IP header, if one is present and
844 * of a correct length, otherwise the same as skb->network_header.
845 * For other key->eth.type values it is left untouched.
847 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
852 memset(key, 0, sizeof(*key));
854 key->phy.priority = skb->priority;
855 if (OVS_CB(skb)->tun_key)
856 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
857 key->phy.in_port = in_port;
858 key->phy.skb_mark = skb->mark;
860 skb_reset_mac_header(skb);
862 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
863 * header in the linear data area.
866 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
867 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
869 __skb_pull(skb, 2 * ETH_ALEN);
870 /* We are going to push all headers that we pull, so no need to
871 * update skb->csum here. */
873 if (vlan_tx_tag_present(skb))
874 key->eth.tci = htons(vlan_get_tci(skb));
875 else if (eth->h_proto == htons(ETH_P_8021Q))
876 if (unlikely(parse_vlan(skb, key)))
879 key->eth.type = parse_ethertype(skb);
880 if (unlikely(key->eth.type == htons(0)))
883 skb_reset_network_header(skb);
884 __skb_push(skb, skb->data - skb_mac_header(skb));
887 if (key->eth.type == htons(ETH_P_IP)) {
891 error = check_iphdr(skb);
892 if (unlikely(error)) {
893 if (error == -EINVAL) {
894 skb->transport_header = skb->network_header;
901 key->ipv4.addr.src = nh->saddr;
902 key->ipv4.addr.dst = nh->daddr;
904 key->ip.proto = nh->protocol;
905 key->ip.tos = nh->tos;
906 key->ip.ttl = nh->ttl;
908 offset = nh->frag_off & htons(IP_OFFSET);
910 key->ip.frag = OVS_FRAG_TYPE_LATER;
913 if (nh->frag_off & htons(IP_MF) ||
914 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
915 key->ip.frag = OVS_FRAG_TYPE_FIRST;
917 /* Transport layer. */
918 if (key->ip.proto == IPPROTO_TCP) {
919 if (tcphdr_ok(skb)) {
920 struct tcphdr *tcp = tcp_hdr(skb);
921 key->ipv4.tp.src = tcp->source;
922 key->ipv4.tp.dst = tcp->dest;
924 } else if (key->ip.proto == IPPROTO_UDP) {
925 if (udphdr_ok(skb)) {
926 struct udphdr *udp = udp_hdr(skb);
927 key->ipv4.tp.src = udp->source;
928 key->ipv4.tp.dst = udp->dest;
930 } else if (key->ip.proto == IPPROTO_SCTP) {
931 if (sctphdr_ok(skb)) {
932 struct sctphdr *sctp = sctp_hdr(skb);
933 key->ipv4.tp.src = sctp->source;
934 key->ipv4.tp.dst = sctp->dest;
936 } else if (key->ip.proto == IPPROTO_ICMP) {
937 if (icmphdr_ok(skb)) {
938 struct icmphdr *icmp = icmp_hdr(skb);
939 /* The ICMP type and code fields use the 16-bit
940 * transport port fields, so we need to store
941 * them in 16-bit network byte order. */
942 key->ipv4.tp.src = htons(icmp->type);
943 key->ipv4.tp.dst = htons(icmp->code);
947 } else if ((key->eth.type == htons(ETH_P_ARP) ||
948 key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
949 struct arp_eth_header *arp;
951 arp = (struct arp_eth_header *)skb_network_header(skb);
953 if (arp->ar_hrd == htons(ARPHRD_ETHER)
954 && arp->ar_pro == htons(ETH_P_IP)
955 && arp->ar_hln == ETH_ALEN
956 && arp->ar_pln == 4) {
958 /* We only match on the lower 8 bits of the opcode. */
959 if (ntohs(arp->ar_op) <= 0xff)
960 key->ip.proto = ntohs(arp->ar_op);
961 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
962 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
963 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
964 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
966 } else if (key->eth.type == htons(ETH_P_IPV6)) {
967 int nh_len; /* IPv6 Header + Extensions */
969 nh_len = parse_ipv6hdr(skb, key);
970 if (unlikely(nh_len < 0)) {
971 if (nh_len == -EINVAL) {
972 skb->transport_header = skb->network_header;
980 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
982 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
983 key->ip.frag = OVS_FRAG_TYPE_FIRST;
985 /* Transport layer. */
986 if (key->ip.proto == NEXTHDR_TCP) {
987 if (tcphdr_ok(skb)) {
988 struct tcphdr *tcp = tcp_hdr(skb);
989 key->ipv6.tp.src = tcp->source;
990 key->ipv6.tp.dst = tcp->dest;
992 } else if (key->ip.proto == NEXTHDR_UDP) {
993 if (udphdr_ok(skb)) {
994 struct udphdr *udp = udp_hdr(skb);
995 key->ipv6.tp.src = udp->source;
996 key->ipv6.tp.dst = udp->dest;
998 } else if (key->ip.proto == NEXTHDR_SCTP) {
999 if (sctphdr_ok(skb)) {
1000 struct sctphdr *sctp = sctp_hdr(skb);
1001 key->ipv6.tp.src = sctp->source;
1002 key->ipv6.tp.dst = sctp->dest;
1004 } else if (key->ip.proto == NEXTHDR_ICMP) {
1005 if (icmp6hdr_ok(skb)) {
1006 error = parse_icmpv6(skb, key, nh_len);
1016 static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start,
1019 u32 *hash_key = (u32 *)((u8 *)key + key_start);
1020 int hash_u32s = (key_end - key_start) >> 2;
1022 /* Make sure number of hash bytes are multiple of u32. */
1023 BUILD_BUG_ON(sizeof(long) % sizeof(u32));
1025 return jhash2(hash_key, hash_u32s, 0);
1028 static int flow_key_start(const struct sw_flow_key *key)
1030 if (key->tun_key.ipv4_dst)
1033 return rounddown(offsetof(struct sw_flow_key, phy),
1037 static bool __cmp_key(const struct sw_flow_key *key1,
1038 const struct sw_flow_key *key2, int key_start, int key_end)
1040 const long *cp1 = (long *)((u8 *)key1 + key_start);
1041 const long *cp2 = (long *)((u8 *)key2 + key_start);
1045 for (i = key_start; i < key_end; i += sizeof(long))
1046 diffs |= *cp1++ ^ *cp2++;
1051 static bool __flow_cmp_masked_key(const struct sw_flow *flow,
1052 const struct sw_flow_key *key, int key_start, int key_end)
1054 return __cmp_key(&flow->key, key, key_start, key_end);
1057 static bool __flow_cmp_unmasked_key(const struct sw_flow *flow,
1058 const struct sw_flow_key *key, int key_start, int key_end)
1060 return __cmp_key(&flow->unmasked_key, key, key_start, key_end);
1063 bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
1064 const struct sw_flow_key *key, int key_end)
1067 key_start = flow_key_start(key);
1069 return __flow_cmp_unmasked_key(flow, key, key_start, key_end);
1073 struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
1074 struct sw_flow_match *match)
1076 struct sw_flow_key *unmasked = match->key;
1077 int key_end = match->range.end;
1078 struct sw_flow *flow;
1080 flow = ovs_flow_lookup(table, unmasked);
1081 if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_end)))
1087 static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
1088 const struct sw_flow_key *unmasked,
1089 struct sw_flow_mask *mask)
1091 struct sw_flow *flow;
1092 struct hlist_head *head;
1093 int key_start = mask->range.start;
1094 int key_end = mask->range.end;
1096 struct sw_flow_key masked_key;
1098 ovs_flow_key_mask(&masked_key, unmasked, mask);
1099 hash = ovs_flow_hash(&masked_key, key_start, key_end);
1100 head = find_bucket(table, hash);
1101 hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
1102 if (flow->mask == mask &&
1103 __flow_cmp_masked_key(flow, &masked_key,
1104 key_start, key_end))
1110 struct sw_flow *ovs_flow_lookup(struct flow_table *tbl,
1111 const struct sw_flow_key *key)
1113 struct sw_flow *flow = NULL;
1114 struct sw_flow_mask *mask;
1116 list_for_each_entry_rcu(mask, tbl->mask_list, list) {
1117 flow = ovs_masked_flow_lookup(tbl, key, mask);
1118 if (flow) /* Found */
1126 void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow)
1128 flow->hash = ovs_flow_hash(&flow->key, flow->mask->range.start,
1129 flow->mask->range.end);
1130 __tbl_insert(table, flow);
1133 void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow)
1135 BUG_ON(table->count == 0);
1136 hlist_del_rcu(&flow->hash_node[table->node_ver]);
1140 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
1141 const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
1142 [OVS_KEY_ATTR_ENCAP] = -1,
1143 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
1144 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
1145 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
1146 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
1147 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
1148 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
1149 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
1150 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
1151 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
1152 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
1153 [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
1154 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
1155 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
1156 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
1157 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
1158 [OVS_KEY_ATTR_TUNNEL] = -1,
1161 static bool is_all_zero(const u8 *fp, size_t size)
1168 for (i = 0; i < size; i++)
1175 static int __parse_flow_nlattrs(const struct nlattr *attr,
1176 const struct nlattr *a[],
1177 u64 *attrsp, bool nz)
1179 const struct nlattr *nla;
1184 nla_for_each_nested(nla, attr, rem) {
1185 u16 type = nla_type(nla);
1188 if (type > OVS_KEY_ATTR_MAX) {
1189 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
1190 type, OVS_KEY_ATTR_MAX);
1194 if (attrs & (1ULL << type)) {
1195 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
1199 expected_len = ovs_key_lens[type];
1200 if (nla_len(nla) != expected_len && expected_len != -1) {
1201 OVS_NLERR("Key attribute has unexpected length (type=%d"
1202 ", length=%d, expected=%d).\n", type,
1203 nla_len(nla), expected_len);
1207 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
1208 attrs |= 1ULL << type;
1213 OVS_NLERR("Message has %d unknown bytes.\n", rem);
1221 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
1222 const struct nlattr *a[], u64 *attrsp)
1224 return __parse_flow_nlattrs(attr, a, attrsp, true);
1227 static int parse_flow_nlattrs(const struct nlattr *attr,
1228 const struct nlattr *a[], u64 *attrsp)
1230 return __parse_flow_nlattrs(attr, a, attrsp, false);
1233 int ovs_ipv4_tun_from_nlattr(const struct nlattr *attr,
1234 struct sw_flow_match *match, bool is_mask)
1239 __be16 tun_flags = 0;
1241 nla_for_each_nested(a, attr, rem) {
1242 int type = nla_type(a);
1243 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1244 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
1245 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
1246 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
1247 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
1248 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
1249 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
1250 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
1253 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
1254 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
1255 type, OVS_TUNNEL_KEY_ATTR_MAX);
1259 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
1260 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
1261 " length (type=%d, length=%d, expected=%d).\n",
1262 type, nla_len(a), ovs_tunnel_key_lens[type]);
1267 case OVS_TUNNEL_KEY_ATTR_ID:
1268 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
1269 nla_get_be64(a), is_mask);
1270 tun_flags |= TUNNEL_KEY;
1272 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1273 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
1274 nla_get_be32(a), is_mask);
1276 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1277 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
1278 nla_get_be32(a), is_mask);
1280 case OVS_TUNNEL_KEY_ATTR_TOS:
1281 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
1282 nla_get_u8(a), is_mask);
1284 case OVS_TUNNEL_KEY_ATTR_TTL:
1285 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
1286 nla_get_u8(a), is_mask);
1289 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1290 tun_flags |= TUNNEL_DONT_FRAGMENT;
1292 case OVS_TUNNEL_KEY_ATTR_CSUM:
1293 tun_flags |= TUNNEL_CSUM;
1300 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
1303 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
1308 if (!match->key->tun_key.ipv4_dst) {
1309 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
1314 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
1322 int ovs_ipv4_tun_to_nlattr(struct sk_buff *skb,
1323 const struct ovs_key_ipv4_tunnel *tun_key,
1324 const struct ovs_key_ipv4_tunnel *output)
1328 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
1332 if (output->tun_flags & TUNNEL_KEY &&
1333 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
1335 if (output->ipv4_src &&
1336 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
1338 if (output->ipv4_dst &&
1339 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
1341 if (output->ipv4_tos &&
1342 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
1344 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
1346 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
1347 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
1349 if ((output->tun_flags & TUNNEL_CSUM) &&
1350 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
1353 nla_nest_end(skb, nla);
1358 static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
1359 const struct nlattr **a, bool is_mask)
1361 if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
1362 SW_FLOW_KEY_PUT(match, phy.priority,
1363 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1364 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
1367 if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
1368 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1371 in_port = 0xffffffff; /* Always exact match in_port. */
1372 else if (in_port >= DP_MAX_PORTS)
1375 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1376 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
1377 } else if (!is_mask) {
1378 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1381 if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
1382 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1384 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1385 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
1387 if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
1388 if (ovs_ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1391 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
1396 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
1397 const struct nlattr **a, bool is_mask)
1400 u64 orig_attrs = attrs;
1402 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
1406 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
1407 const struct ovs_key_ethernet *eth_key;
1409 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1410 SW_FLOW_KEY_MEMCPY(match, eth.src,
1411 eth_key->eth_src, ETH_ALEN, is_mask);
1412 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1413 eth_key->eth_dst, ETH_ALEN, is_mask);
1414 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
1417 if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
1420 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1421 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1423 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
1425 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
1430 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
1431 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
1432 } else if (!is_mask)
1433 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1435 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
1438 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1440 /* Always exact match EtherType. */
1441 eth_type = htons(0xffff);
1442 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
1443 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
1444 ntohs(eth_type), ETH_P_802_3_MIN);
1448 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1449 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1450 } else if (!is_mask) {
1451 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1454 if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1455 const struct ovs_key_ipv4 *ipv4_key;
1457 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1458 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
1459 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
1460 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
1463 SW_FLOW_KEY_PUT(match, ip.proto,
1464 ipv4_key->ipv4_proto, is_mask);
1465 SW_FLOW_KEY_PUT(match, ip.tos,
1466 ipv4_key->ipv4_tos, is_mask);
1467 SW_FLOW_KEY_PUT(match, ip.ttl,
1468 ipv4_key->ipv4_ttl, is_mask);
1469 SW_FLOW_KEY_PUT(match, ip.frag,
1470 ipv4_key->ipv4_frag, is_mask);
1471 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1472 ipv4_key->ipv4_src, is_mask);
1473 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1474 ipv4_key->ipv4_dst, is_mask);
1475 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
1478 if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
1479 const struct ovs_key_ipv6 *ipv6_key;
1481 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1482 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
1483 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
1484 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
1487 SW_FLOW_KEY_PUT(match, ipv6.label,
1488 ipv6_key->ipv6_label, is_mask);
1489 SW_FLOW_KEY_PUT(match, ip.proto,
1490 ipv6_key->ipv6_proto, is_mask);
1491 SW_FLOW_KEY_PUT(match, ip.tos,
1492 ipv6_key->ipv6_tclass, is_mask);
1493 SW_FLOW_KEY_PUT(match, ip.ttl,
1494 ipv6_key->ipv6_hlimit, is_mask);
1495 SW_FLOW_KEY_PUT(match, ip.frag,
1496 ipv6_key->ipv6_frag, is_mask);
1497 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1499 sizeof(match->key->ipv6.addr.src),
1501 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1503 sizeof(match->key->ipv6.addr.dst),
1506 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
1509 if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
1510 const struct ovs_key_arp *arp_key;
1512 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1513 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
1514 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
1519 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1520 arp_key->arp_sip, is_mask);
1521 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1522 arp_key->arp_tip, is_mask);
1523 SW_FLOW_KEY_PUT(match, ip.proto,
1524 ntohs(arp_key->arp_op), is_mask);
1525 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1526 arp_key->arp_sha, ETH_ALEN, is_mask);
1527 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1528 arp_key->arp_tha, ETH_ALEN, is_mask);
1530 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
1533 if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
1534 const struct ovs_key_tcp *tcp_key;
1536 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1537 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1538 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1539 tcp_key->tcp_src, is_mask);
1540 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1541 tcp_key->tcp_dst, is_mask);
1543 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1544 tcp_key->tcp_src, is_mask);
1545 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1546 tcp_key->tcp_dst, is_mask);
1548 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
1551 if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
1552 const struct ovs_key_udp *udp_key;
1554 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1555 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1556 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1557 udp_key->udp_src, is_mask);
1558 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1559 udp_key->udp_dst, is_mask);
1561 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1562 udp_key->udp_src, is_mask);
1563 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1564 udp_key->udp_dst, is_mask);
1566 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
1569 if (attrs & (1ULL << OVS_KEY_ATTR_SCTP)) {
1570 const struct ovs_key_sctp *sctp_key;
1572 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
1573 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1574 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1575 sctp_key->sctp_src, is_mask);
1576 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1577 sctp_key->sctp_dst, is_mask);
1579 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1580 sctp_key->sctp_src, is_mask);
1581 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1582 sctp_key->sctp_dst, is_mask);
1584 attrs &= ~(1ULL << OVS_KEY_ATTR_SCTP);
1587 if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
1588 const struct ovs_key_icmp *icmp_key;
1590 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1591 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1592 htons(icmp_key->icmp_type), is_mask);
1593 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1594 htons(icmp_key->icmp_code), is_mask);
1595 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
1598 if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
1599 const struct ovs_key_icmpv6 *icmpv6_key;
1601 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1602 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1603 htons(icmpv6_key->icmpv6_type), is_mask);
1604 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1605 htons(icmpv6_key->icmpv6_code), is_mask);
1606 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
1609 if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
1610 const struct ovs_key_nd *nd_key;
1612 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1613 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1615 sizeof(match->key->ipv6.nd.target),
1617 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1618 nd_key->nd_sll, ETH_ALEN, is_mask);
1619 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1620 nd_key->nd_tll, ETH_ALEN, is_mask);
1621 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
1631 * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and
1632 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1633 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1634 * does not include any don't care bit.
1635 * @match: receives the extracted flow match information.
1636 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1637 * sequence. The fields should of the packet that triggered the creation
1639 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1640 * attribute specifies the mask field of the wildcarded flow.
1642 int ovs_match_from_nlattrs(struct sw_flow_match *match,
1643 const struct nlattr *key,
1644 const struct nlattr *mask)
1646 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1647 const struct nlattr *encap;
1650 bool encap_valid = false;
1653 err = parse_flow_nlattrs(key, a, &key_attrs);
1657 if ((key_attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1658 (key_attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) &&
1659 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
1662 if (!((key_attrs & (1ULL << OVS_KEY_ATTR_VLAN)) &&
1663 (key_attrs & (1ULL << OVS_KEY_ATTR_ENCAP)))) {
1664 OVS_NLERR("Invalid Vlan frame.\n");
1668 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1669 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1670 encap = a[OVS_KEY_ATTR_ENCAP];
1671 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1674 if (tci & htons(VLAN_TAG_PRESENT)) {
1675 err = parse_flow_nlattrs(encap, a, &key_attrs);
1679 /* Corner case for truncated 802.1Q header. */
1680 if (nla_len(encap)) {
1681 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
1685 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
1690 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
1695 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
1699 if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
1700 __be16 eth_type = 0;
1704 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
1708 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1709 if (a[OVS_KEY_ATTR_ETHERTYPE])
1710 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1712 if (eth_type == htons(0xffff)) {
1713 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1714 encap = a[OVS_KEY_ATTR_ENCAP];
1715 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
1717 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
1722 if (a[OVS_KEY_ATTR_VLAN])
1723 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1725 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1726 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
1731 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
1735 /* Populate exact match flow's key mask. */
1737 ovs_sw_flow_mask_set(match->mask, &match->range, 0xff);
1740 if (!ovs_match_validate(match, key_attrs, mask_attrs))
1747 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1748 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
1749 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1752 * This parses a series of Netlink attributes that form a flow key, which must
1753 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1754 * get the metadata, that is, the parts of the flow key that cannot be
1755 * extracted from the packet itself.
1758 int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
1759 const struct nlattr *attr)
1761 struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
1762 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1765 struct sw_flow_match match;
1767 flow->key.phy.in_port = DP_MAX_PORTS;
1768 flow->key.phy.priority = 0;
1769 flow->key.phy.skb_mark = 0;
1770 memset(tun_key, 0, sizeof(flow->key.tun_key));
1772 err = parse_flow_nlattrs(attr, a, &attrs);
1776 memset(&match, 0, sizeof(match));
1777 match.key = &flow->key;
1779 err = metadata_from_nlattrs(&match, &attrs, a, false);
1786 int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey,
1787 const struct sw_flow_key *output, struct sk_buff *skb)
1789 struct ovs_key_ethernet *eth_key;
1790 struct nlattr *nla, *encap;
1791 bool is_mask = (swkey != output);
1793 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1794 goto nla_put_failure;
1796 if ((swkey->tun_key.ipv4_dst || is_mask) &&
1797 ovs_ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
1798 goto nla_put_failure;
1800 if (swkey->phy.in_port == DP_MAX_PORTS) {
1801 if (is_mask && (output->phy.in_port == 0xffff))
1802 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1803 goto nla_put_failure;
1806 upper_u16 = !is_mask ? 0 : 0xffff;
1808 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1809 (upper_u16 << 16) | output->phy.in_port))
1810 goto nla_put_failure;
1813 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1814 goto nla_put_failure;
1816 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1818 goto nla_put_failure;
1820 eth_key = nla_data(nla);
1821 memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
1822 memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
1824 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1826 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1827 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1828 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1829 goto nla_put_failure;
1830 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1831 if (!swkey->eth.tci)
1836 if (swkey->eth.type == htons(ETH_P_802_2)) {
1838 * Ethertype 802.2 is represented in the netlink with omitted
1839 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1840 * 0xffff in the mask attribute. Ethertype can also
1843 if (is_mask && output->eth.type)
1844 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1846 goto nla_put_failure;
1850 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1851 goto nla_put_failure;
1853 if (swkey->eth.type == htons(ETH_P_IP)) {
1854 struct ovs_key_ipv4 *ipv4_key;
1856 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1858 goto nla_put_failure;
1859 ipv4_key = nla_data(nla);
1860 ipv4_key->ipv4_src = output->ipv4.addr.src;
1861 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1862 ipv4_key->ipv4_proto = output->ip.proto;
1863 ipv4_key->ipv4_tos = output->ip.tos;
1864 ipv4_key->ipv4_ttl = output->ip.ttl;
1865 ipv4_key->ipv4_frag = output->ip.frag;
1866 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1867 struct ovs_key_ipv6 *ipv6_key;
1869 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1871 goto nla_put_failure;
1872 ipv6_key = nla_data(nla);
1873 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1874 sizeof(ipv6_key->ipv6_src));
1875 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1876 sizeof(ipv6_key->ipv6_dst));
1877 ipv6_key->ipv6_label = output->ipv6.label;
1878 ipv6_key->ipv6_proto = output->ip.proto;
1879 ipv6_key->ipv6_tclass = output->ip.tos;
1880 ipv6_key->ipv6_hlimit = output->ip.ttl;
1881 ipv6_key->ipv6_frag = output->ip.frag;
1882 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1883 swkey->eth.type == htons(ETH_P_RARP)) {
1884 struct ovs_key_arp *arp_key;
1886 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1888 goto nla_put_failure;
1889 arp_key = nla_data(nla);
1890 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1891 arp_key->arp_sip = output->ipv4.addr.src;
1892 arp_key->arp_tip = output->ipv4.addr.dst;
1893 arp_key->arp_op = htons(output->ip.proto);
1894 memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN);
1895 memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN);
1898 if ((swkey->eth.type == htons(ETH_P_IP) ||
1899 swkey->eth.type == htons(ETH_P_IPV6)) &&
1900 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1902 if (swkey->ip.proto == IPPROTO_TCP) {
1903 struct ovs_key_tcp *tcp_key;
1905 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1907 goto nla_put_failure;
1908 tcp_key = nla_data(nla);
1909 if (swkey->eth.type == htons(ETH_P_IP)) {
1910 tcp_key->tcp_src = output->ipv4.tp.src;
1911 tcp_key->tcp_dst = output->ipv4.tp.dst;
1912 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1913 tcp_key->tcp_src = output->ipv6.tp.src;
1914 tcp_key->tcp_dst = output->ipv6.tp.dst;
1916 } else if (swkey->ip.proto == IPPROTO_UDP) {
1917 struct ovs_key_udp *udp_key;
1919 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1921 goto nla_put_failure;
1922 udp_key = nla_data(nla);
1923 if (swkey->eth.type == htons(ETH_P_IP)) {
1924 udp_key->udp_src = output->ipv4.tp.src;
1925 udp_key->udp_dst = output->ipv4.tp.dst;
1926 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1927 udp_key->udp_src = output->ipv6.tp.src;
1928 udp_key->udp_dst = output->ipv6.tp.dst;
1930 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1931 struct ovs_key_sctp *sctp_key;
1933 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1935 goto nla_put_failure;
1936 sctp_key = nla_data(nla);
1937 if (swkey->eth.type == htons(ETH_P_IP)) {
1938 sctp_key->sctp_src = swkey->ipv4.tp.src;
1939 sctp_key->sctp_dst = swkey->ipv4.tp.dst;
1940 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1941 sctp_key->sctp_src = swkey->ipv6.tp.src;
1942 sctp_key->sctp_dst = swkey->ipv6.tp.dst;
1944 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1945 swkey->ip.proto == IPPROTO_ICMP) {
1946 struct ovs_key_icmp *icmp_key;
1948 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1950 goto nla_put_failure;
1951 icmp_key = nla_data(nla);
1952 icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
1953 icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
1954 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1955 swkey->ip.proto == IPPROTO_ICMPV6) {
1956 struct ovs_key_icmpv6 *icmpv6_key;
1958 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1959 sizeof(*icmpv6_key));
1961 goto nla_put_failure;
1962 icmpv6_key = nla_data(nla);
1963 icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
1964 icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
1966 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1967 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1968 struct ovs_key_nd *nd_key;
1970 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1972 goto nla_put_failure;
1973 nd_key = nla_data(nla);
1974 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1975 sizeof(nd_key->nd_target));
1976 memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN);
1977 memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN);
1984 nla_nest_end(skb, encap);
1992 /* Initializes the flow module.
1993 * Returns zero if successful or a negative error code. */
1994 int ovs_flow_init(void)
1996 BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
1997 BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
1999 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
2001 if (flow_cache == NULL)
2007 /* Uninitializes the flow module. */
2008 void ovs_flow_exit(void)
2010 kmem_cache_destroy(flow_cache);
2013 struct sw_flow_mask *ovs_sw_flow_mask_alloc(void)
2015 struct sw_flow_mask *mask;
2017 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
2019 mask->ref_count = 0;
2024 void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask)
2029 static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu)
2031 struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu);
2036 void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
2041 BUG_ON(!mask->ref_count);
2044 if (!mask->ref_count) {
2045 list_del_rcu(&mask->list);
2047 call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb);
2053 static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a,
2054 const struct sw_flow_mask *b)
2056 u8 *a_ = (u8 *)&a->key + a->range.start;
2057 u8 *b_ = (u8 *)&b->key + b->range.start;
2059 return (a->range.end == b->range.end)
2060 && (a->range.start == b->range.start)
2061 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
2064 struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
2065 const struct sw_flow_mask *mask)
2067 struct list_head *ml;
2069 list_for_each(ml, tbl->mask_list) {
2070 struct sw_flow_mask *m;
2071 m = container_of(ml, struct sw_flow_mask, list);
2072 if (ovs_sw_flow_mask_equal(mask, m))
2080 * add a new mask into the mask list.
2081 * The caller needs to make sure that 'mask' is not the same
2082 * as any masks that are already on the list.
2084 void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask)
2086 list_add_rcu(&mask->list, tbl->mask_list);
2090 * Set 'range' fields in the mask to the value of 'val'.
2092 static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
2093 struct sw_flow_key_range *range, u8 val)
2095 u8 *m = (u8 *)&mask->key + range->start;
2097 mask->range = *range;
2098 memset(m, val, range_n_bytes(range));