2 * Copyright (c) 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
26 #include "odp-netlink.h"
28 #include "ovs-thread.h"
31 #include "tun-metadata.h"
33 struct tun_meta_entry {
34 struct hmap_node node; /* In struct tun_table's key_hmap. */
35 uint32_t key; /* (class << 16) | type. */
36 struct tun_metadata_loc loc;
37 bool valid; /* True if allocated to a class and type. */
40 /* Maps from Geneve option class+type to positions in a struct tun_metadata's
43 /* TUN_METADATA<i> is stored in element <i>. */
44 struct tun_meta_entry entries[TUN_METADATA_NUM_OPTS];
46 /* Each bit represents 4 bytes of space, 0-bits are free space. */
47 unsigned long alloc_map[BITMAP_N_LONGS(TUN_METADATA_TOT_OPT_SIZE / 4)];
49 /* The valid elements in entries[], indexed by class+type. */
52 BUILD_ASSERT_DECL(TUN_METADATA_TOT_OPT_SIZE % 4 == 0);
54 static struct ovs_mutex tab_mutex = OVS_MUTEX_INITIALIZER;
55 static OVSRCU_TYPE(struct tun_table *) metadata_tab;
57 static enum ofperr tun_metadata_add_entry(struct tun_table *map, uint8_t idx,
58 uint16_t opt_class, uint8_t type,
59 uint8_t len) OVS_REQUIRES(tab_mutex);
60 static void tun_metadata_del_entry(struct tun_table *map, uint8_t idx)
61 OVS_REQUIRES(tab_mutex);
62 static void memcpy_to_metadata(struct tun_metadata *dst, const void *src,
63 const struct tun_metadata_loc *,
65 static void memcpy_from_metadata(void *dst, const struct tun_metadata *src,
66 const struct tun_metadata_loc *);
69 tun_meta_key(ovs_be16 class, uint8_t type)
71 return (OVS_FORCE uint16_t)class << 8 | type;
75 tun_key_class(uint32_t key)
77 return (OVS_FORCE ovs_be16)(key >> 8);
81 tun_key_type(uint32_t key)
86 /* Returns a newly allocated tun_table. If 'old_map' is nonnull then the new
87 * tun_table is a deep copy of the old one. */
88 static struct tun_table *
89 table_alloc(const struct tun_table *old_map) OVS_REQUIRES(tab_mutex)
91 struct tun_table *new_map;
93 new_map = xzalloc(sizeof *new_map);
96 struct tun_meta_entry *entry;
99 hmap_init(&new_map->key_hmap);
101 HMAP_FOR_EACH (entry, node, &old_map->key_hmap) {
102 struct tun_meta_entry *new_entry;
103 struct tun_metadata_loc_chain *chain;
105 new_entry = &new_map->entries[entry - old_map->entries];
106 hmap_insert(&new_map->key_hmap, &new_entry->node, entry->node.hash);
108 chain = &new_entry->loc.c;
109 while (chain->next) {
110 chain->next = xmemdup(chain->next, sizeof *chain->next);
115 hmap_init(&new_map->key_hmap);
121 /* Frees 'map' and all the memory it owns. */
123 table_free(struct tun_table *map) OVS_REQUIRES(tab_mutex)
125 struct tun_meta_entry *entry;
131 HMAP_FOR_EACH (entry, node, &map->key_hmap) {
132 tun_metadata_del_entry(map, entry - map->entries);
138 /* Creates a global tunnel metadata mapping table, if none already exists. */
140 tun_metadata_init(void)
142 ovs_mutex_lock(&tab_mutex);
144 if (!ovsrcu_get_protected(struct tun_table *, &metadata_tab)) {
145 ovsrcu_set(&metadata_tab, table_alloc(NULL));
148 ovs_mutex_unlock(&tab_mutex);
152 tun_metadata_table_mod(struct ofputil_geneve_table_mod *gtm)
154 struct tun_table *old_map, *new_map;
155 struct ofputil_geneve_map *ofp_map;
158 ovs_mutex_lock(&tab_mutex);
160 old_map = ovsrcu_get_protected(struct tun_table *, &metadata_tab);
162 switch (gtm->command) {
164 new_map = table_alloc(old_map);
166 LIST_FOR_EACH (ofp_map, list_node, >m->mappings) {
167 err = tun_metadata_add_entry(new_map, ofp_map->index,
168 ofp_map->option_class,
169 ofp_map->option_type,
170 ofp_map->option_len);
179 new_map = table_alloc(old_map);
181 LIST_FOR_EACH (ofp_map, list_node, >m->mappings) {
182 tun_metadata_del_entry(new_map, ofp_map->index);
187 new_map = table_alloc(NULL);
194 ovsrcu_set(&metadata_tab, new_map);
195 ovsrcu_postpone(table_free, old_map);
198 ovs_mutex_unlock(&tab_mutex);
203 tun_metadata_table_request(struct ofputil_geneve_table_reply *gtr)
205 struct tun_table *map = ovsrcu_get(struct tun_table *, &metadata_tab);
208 gtr->max_option_space = TUN_METADATA_TOT_OPT_SIZE;
209 gtr->max_fields = TUN_METADATA_NUM_OPTS;
210 list_init(>r->mappings);
212 for (i = 0; i < TUN_METADATA_NUM_OPTS; i++) {
213 struct tun_meta_entry *entry = &map->entries[i];
214 struct ofputil_geneve_map *map;
220 map = xmalloc(sizeof *map);
221 map->option_class = ntohs(tun_key_class(entry->key));
222 map->option_type = tun_key_type(entry->key);
223 map->option_len = entry->loc.len;
226 list_push_back(>r->mappings, &map->list_node);
230 /* Copies the value of field 'mf' from 'tnl' (which must be in non-UDPIF format) * into 'value'.
232 * 'mf' must be an MFF_TUN_METADATA* field.
234 * This uses the global tunnel metadata mapping table created by
235 * tun_metadata_init(). If no such table has been created or if 'mf' hasn't
236 * been allocated in it yet, this just zeros 'value'. */
238 tun_metadata_read(const struct flow_tnl *tnl,
239 const struct mf_field *mf, union mf_value *value)
241 struct tun_table *map = ovsrcu_get(struct tun_table *, &metadata_tab);
242 unsigned int idx = mf->id - MFF_TUN_METADATA0;
243 struct tun_metadata_loc *loc;
246 memset(value->tun_metadata, 0, mf->n_bytes);
250 loc = &map->entries[idx].loc;
252 memset(value->tun_metadata, 0, mf->n_bytes - loc->len);
253 memcpy_from_metadata(value->tun_metadata + mf->n_bytes - loc->len,
254 &tnl->metadata, loc);
257 /* Copies 'value' into field 'mf' in 'tnl' (in non-UDPIF format).
259 * 'mf' must be an MFF_TUN_METADATA* field.
261 * This uses the global tunnel metadata mapping table created by
262 * tun_metadata_init(). If no such table has been created or if 'mf' hasn't
263 * been allocated in it yet, this function does nothing. */
265 tun_metadata_write(struct flow_tnl *tnl,
266 const struct mf_field *mf, const union mf_value *value)
268 struct tun_table *map = ovsrcu_get(struct tun_table *, &metadata_tab);
269 unsigned int idx = mf->id - MFF_TUN_METADATA0;
270 struct tun_metadata_loc *loc;
272 if (!map || !map->entries[idx].valid) {
276 loc = &map->entries[idx].loc;
277 memcpy_to_metadata(&tnl->metadata,
278 value->tun_metadata + mf->n_bytes - loc->len, loc, idx);
281 static const struct tun_metadata_loc *
282 metadata_loc_from_match(struct tun_table *map, struct match *match,
283 unsigned int idx, unsigned int field_len, bool masked)
285 ovs_assert(idx < TUN_METADATA_NUM_OPTS);
288 if (map->entries[idx].valid) {
289 return &map->entries[idx].loc;
295 if (match->tun_md.alloc_offset + field_len >= TUN_METADATA_TOT_OPT_SIZE ||
296 ULLONG_GET(match->wc.masks.tunnel.metadata.present.map, idx)) {
300 match->tun_md.entry[idx].loc.len = field_len;
301 match->tun_md.entry[idx].loc.c.offset = match->tun_md.alloc_offset;
302 match->tun_md.entry[idx].loc.c.len = field_len;
303 match->tun_md.entry[idx].loc.c.next = NULL;
304 match->tun_md.entry[idx].masked = masked;
305 match->tun_md.alloc_offset += field_len;
306 match->tun_md.valid = true;
308 return &match->tun_md.entry[idx].loc;
311 /* Makes 'match' match 'value'/'mask' on field 'mf'.
313 * 'mf' must be an MFF_TUN_METADATA* field. 'match' must be in non-UDPIF format.
315 * If there is global tunnel metadata matching table, this function is
316 * effective only if there is already a mapping for 'mf'. Otherwise, the
317 * metadata mapping table integrated into 'match' is used, adding 'mf' to its
318 * mapping table if it isn't already mapped (and if there is room). If 'mf'
319 * isn't or can't be mapped, this function returns without modifying 'match'.
321 * 'value' may be NULL; if so, then 'mf' is made to match on an all-zeros
324 * 'mask' may be NULL; if so, then 'mf' is made exact-match.
327 tun_metadata_set_match(const struct mf_field *mf, const union mf_value *value,
328 const union mf_value *mask, struct match *match)
330 struct tun_table *map = ovsrcu_get(struct tun_table *, &metadata_tab);
331 const struct tun_metadata_loc *loc;
332 unsigned int idx = mf->id - MFF_TUN_METADATA0;
333 unsigned int field_len;
335 unsigned int data_offset;
338 ovs_assert(!(match->flow.tunnel.flags & FLOW_TNL_F_UDPIF));
340 field_len = mf_field_len(mf, value, mask, &is_masked);
341 loc = metadata_loc_from_match(map, match, idx, field_len, is_masked);
346 data_offset = mf->n_bytes - loc->len;
349 memset(data.tun_metadata, 0, loc->len);
351 memcpy(data.tun_metadata, value->tun_metadata + data_offset, loc->len);
354 for (i = 0; i < loc->len; i++) {
355 data.tun_metadata[i] = value->tun_metadata[data_offset + i] &
356 mask->tun_metadata[data_offset + i];
359 memcpy_to_metadata(&match->flow.tunnel.metadata, data.tun_metadata,
363 memset(data.tun_metadata, 0, loc->len);
365 memset(data.tun_metadata, 0xff, loc->len);
367 memcpy(data.tun_metadata, mask->tun_metadata + data_offset, loc->len);
369 memcpy_to_metadata(&match->wc.masks.tunnel.metadata, data.tun_metadata,
374 udpif_to_parsed(const struct flow_tnl *flow, const struct flow_tnl *mask,
375 struct flow_tnl *flow_xlate, struct flow_tnl *mask_xlate)
377 if (flow->flags & FLOW_TNL_F_UDPIF) {
380 err = tun_metadata_from_geneve_udpif(flow, flow, flow_xlate);
386 tun_metadata_from_geneve_udpif(flow, mask, mask_xlate);
392 if (flow->metadata.present.map == 0) {
393 /* There is no tunnel metadata, don't bother copying. */
397 memcpy(flow_xlate, flow, sizeof *flow_xlate);
399 memcpy(mask_xlate, mask, sizeof *mask_xlate);
402 if (!flow_xlate->metadata.tab) {
403 flow_xlate->metadata.tab = ovsrcu_get(struct tun_table *,
411 /* Copies all MFF_TUN_METADATA* fields from 'tnl' to 'flow_metadata'. */
413 tun_metadata_get_fmd(const struct flow_tnl *tnl, struct match *flow_metadata)
415 struct flow_tnl flow;
418 if (!udpif_to_parsed(tnl, NULL, &flow, NULL)) {
422 ULLONG_FOR_EACH_1 (i, flow.metadata.present.map) {
424 const struct tun_metadata_loc *old_loc = &flow.metadata.tab->entries[i].loc;
425 const struct tun_metadata_loc *new_loc;
427 new_loc = metadata_loc_from_match(NULL, flow_metadata, i,
428 old_loc->len, false);
430 memcpy_from_metadata(opts.tun_metadata, &flow.metadata, old_loc);
431 memcpy_to_metadata(&flow_metadata->flow.tunnel.metadata,
432 opts.tun_metadata, new_loc, i);
434 memset(opts.tun_metadata, 0xff, old_loc->len);
435 memcpy_to_metadata(&flow_metadata->wc.masks.tunnel.metadata,
436 opts.tun_metadata, new_loc, i);
441 tun_meta_hash(uint32_t key)
443 return hash_int(key, 0);
446 static struct tun_meta_entry *
447 tun_meta_find_key(const struct hmap *hmap, uint32_t key)
449 struct tun_meta_entry *entry;
451 HMAP_FOR_EACH_IN_BUCKET (entry, node, tun_meta_hash(key), hmap) {
452 if (entry->key == key) {
460 memcpy_to_metadata(struct tun_metadata *dst, const void *src,
461 const struct tun_metadata_loc *loc, unsigned int idx)
463 const struct tun_metadata_loc_chain *chain = &loc->c;
467 memcpy(dst->opts.u8 + loc->c.offset + addr, (uint8_t *)src + addr,
473 ULLONG_SET1(dst->present.map, idx);
477 memcpy_from_metadata(void *dst, const struct tun_metadata *src,
478 const struct tun_metadata_loc *loc)
480 const struct tun_metadata_loc_chain *chain = &loc->c;
484 memcpy((uint8_t *)dst + addr, src->opts.u8 + loc->c.offset + addr,
492 tun_metadata_alloc_chain(struct tun_table *map, uint8_t len,
493 struct tun_metadata_loc_chain *loc)
494 OVS_REQUIRES(tab_mutex)
496 int alloc_len = len / 4;
498 int scan_end = TUN_METADATA_TOT_OPT_SIZE / 4;
499 int pos_start, pos_end, pos_len;
500 int best_start = 0, best_len = 0;
503 pos_start = bitmap_scan(map->alloc_map, 0, scan_start, scan_end);
504 if (pos_start == scan_end) {
508 pos_end = bitmap_scan(map->alloc_map, 1, pos_start,
509 MIN(pos_start + alloc_len, scan_end));
510 pos_len = pos_end - pos_start;
511 if (pos_len == alloc_len) {
515 if (pos_len > best_len) {
516 best_start = pos_start;
519 scan_start = pos_end + 1;
526 pos_start = best_start;
530 bitmap_set_multiple(map->alloc_map, pos_start, pos_len, 1);
531 loc->offset = pos_start * 4;
532 loc->len = pos_len * 4;
538 tun_metadata_add_entry(struct tun_table *map, uint8_t idx, uint16_t opt_class,
539 uint8_t type, uint8_t len) OVS_REQUIRES(tab_mutex)
541 struct tun_meta_entry *entry;
542 struct tun_metadata_loc_chain *cur_chain, *prev_chain;
544 ovs_assert(idx < TUN_METADATA_NUM_OPTS);
546 entry = &map->entries[idx];
548 return OFPERR_NXGTMFC_ALREADY_MAPPED;
551 entry->key = tun_meta_key(htons(opt_class), type);
552 if (tun_meta_find_key(&map->key_hmap, entry->key)) {
553 return OFPERR_NXGTMFC_DUP_ENTRY;
557 hmap_insert(&map->key_hmap, &entry->node,
558 tun_meta_hash(entry->key));
560 entry->loc.len = len;
561 cur_chain = &entry->loc.c;
562 memset(cur_chain, 0, sizeof *cur_chain);
569 cur_chain = xzalloc(sizeof *cur_chain);
572 err = tun_metadata_alloc_chain(map, len, cur_chain);
574 tun_metadata_del_entry(map, idx);
575 return OFPERR_NXGTMFC_TABLE_FULL;
578 len -= cur_chain->len;
581 prev_chain->next = cur_chain;
583 prev_chain = cur_chain;
591 tun_metadata_del_entry(struct tun_table *map, uint8_t idx)
592 OVS_REQUIRES(tab_mutex)
594 struct tun_meta_entry *entry;
595 struct tun_metadata_loc_chain *chain;
597 if (idx >= TUN_METADATA_NUM_OPTS) {
601 entry = &map->entries[idx];
606 chain = &entry->loc.c;
608 struct tun_metadata_loc_chain *next = chain->next;
610 bitmap_set_multiple(map->alloc_map, chain->offset / 4,
612 if (chain != &entry->loc.c) {
618 entry->valid = false;
619 hmap_remove(&map->key_hmap, &entry->node);
620 memset(&entry->loc, 0, sizeof entry->loc);
624 tun_metadata_from_geneve__(const struct tun_metadata *flow_metadata,
625 const struct geneve_opt *opt,
626 const struct geneve_opt *flow_opt, int opts_len,
627 struct tun_metadata *metadata)
629 struct tun_table *map;
630 bool is_mask = flow_opt != opt;
633 map = ovsrcu_get(struct tun_table *, &metadata_tab);
636 map = flow_metadata->tab;
643 while (opts_len > 0) {
645 struct tun_meta_entry *entry;
647 if (opts_len < sizeof(*opt)) {
651 len = sizeof(*opt) + flow_opt->length * 4;
652 if (len > opts_len) {
656 entry = tun_meta_find_key(&map->key_hmap,
657 tun_meta_key(flow_opt->opt_class,
660 if (entry->loc.len == flow_opt->length * 4) {
661 memcpy_to_metadata(metadata, opt + 1, &entry->loc,
662 entry - map->entries);
666 } else if (flow_opt->type & GENEVE_CRIT_OPT_TYPE) {
670 opt = opt + len / sizeof(*opt);
671 flow_opt = flow_opt + len / sizeof(*opt);
678 static const struct nlattr *
679 tun_metadata_find_geneve_key(const struct nlattr *key, uint32_t key_len)
681 const struct nlattr *tnl_key;
683 tnl_key = nl_attr_find__(key, key_len, OVS_KEY_ATTR_TUNNEL);
688 return nl_attr_find_nested(tnl_key, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS);
691 /* Converts from Geneve netlink attributes in 'attr' to tunnel metadata
692 * in 'tun'. The result may either in be UDPIF format or not, as determined
695 * In the event that a mask is being converted, it is also necessary to
696 * pass in flow information. This includes the full set of netlink attributes
697 * (i.e. not just the Geneve attribute) in 'flow_attrs'/'flow_attr_len' and
698 * the previously converted tunnel metadata 'flow_tun'.
700 * If a flow rather than mask is being converted, 'flow_attrs' must be NULL. */
702 tun_metadata_from_geneve_nlattr(const struct nlattr *attr,
703 const struct nlattr *flow_attrs,
704 size_t flow_attr_len,
705 const struct flow_tnl *flow_tun, bool udpif,
706 struct flow_tnl *tun)
708 bool is_mask = !!flow_attrs;
709 int attr_len = nl_attr_get_size(attr);
710 const struct nlattr *flow;
712 /* No need for real translation, just copy things over. */
714 memcpy(tun->metadata.opts.gnv, nl_attr_get(attr), attr_len);
717 tun->metadata.present.len = attr_len;
718 tun->flags |= FLOW_TNL_F_UDPIF;
720 /* We need to exact match on the length so we don't
721 * accidentally match on sets of options that are the same
722 * at the beginning but with additional options after. */
723 tun->metadata.present.len = 0xff;
730 flow = tun_metadata_find_geneve_key(flow_attrs, flow_attr_len);
732 return attr_len ? EINVAL : 0;
735 if (attr_len != nl_attr_get_size(flow)) {
742 return tun_metadata_from_geneve__(&flow_tun->metadata, nl_attr_get(attr),
743 nl_attr_get(flow), nl_attr_get_size(flow),
747 /* Converts from the flat Geneve options representation extracted directly
748 * from the tunnel header to the representation that maps options to
749 * pre-allocated locations. The original version (in UDPIF form) is passed
750 * in 'src' and the translated form in stored in 'dst'. To handle masks, the
751 * flow must also be passed in through 'flow' (in the original, raw form). */
753 tun_metadata_from_geneve_udpif(const struct flow_tnl *flow,
754 const struct flow_tnl *src,
755 struct flow_tnl *dst)
757 ovs_assert(flow->flags & FLOW_TNL_F_UDPIF);
760 dst->flags = flow->flags & ~FLOW_TNL_F_UDPIF;
762 dst->metadata.tab = NULL;
764 dst->metadata.present.map = 0;
765 return tun_metadata_from_geneve__(&flow->metadata, src->metadata.opts.gnv,
766 flow->metadata.opts.gnv,
767 flow->metadata.present.len,
772 tun_metadata_to_geneve__(const struct tun_metadata *flow, struct ofpbuf *b,
775 struct tun_table *map;
780 map = ovsrcu_get(struct tun_table *, &metadata_tab);
785 ULLONG_FOR_EACH_1 (i, flow->present.map) {
786 struct tun_meta_entry *entry = &map->entries[i];
787 struct geneve_opt *opt;
789 opt = ofpbuf_put_uninit(b, sizeof *opt + entry->loc.len);
791 opt->opt_class = tun_key_class(entry->key);
792 opt->type = tun_key_type(entry->key);
793 opt->length = entry->loc.len / 4;
798 memcpy_from_metadata(opt + 1, flow, &entry->loc);
799 *crit_opt |= !!(opt->type & GENEVE_CRIT_OPT_TYPE);
804 tun_metadata_to_geneve_nlattr_flow(const struct flow_tnl *flow,
807 size_t nlattr_offset;
810 if (!flow->metadata.present.map) {
814 /* For all intents and purposes, the Geneve options are nested
815 * attributes even if this doesn't show up directly to netlink. It's
816 * similar enough that we can use the same mechanism. */
817 nlattr_offset = nl_msg_start_nested(b, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS);
819 tun_metadata_to_geneve__(&flow->metadata, b, &crit_opt);
821 nl_msg_end_nested(b, nlattr_offset);
824 /* Converts from processed tunnel metadata information (in non-udpif
825 * format) in 'flow' to a stream of Geneve options suitable for
826 * transmission in 'opts'. Additionally returns whether there were
827 * any critical options in 'crit_opt' as well as the total length of
830 tun_metadata_to_geneve_header(const struct flow_tnl *flow,
831 struct geneve_opt *opts, bool *crit_opt)
835 ovs_assert(!(flow->flags & FLOW_TNL_F_UDPIF));
837 ofpbuf_use_stack(&b, opts, GENEVE_TOT_OPT_SIZE);
838 tun_metadata_to_geneve__(&flow->metadata, &b, crit_opt);
844 tun_metadata_to_geneve_mask__(const struct tun_metadata *flow,
845 const struct tun_metadata *mask,
846 struct geneve_opt *opt, int opts_len)
848 struct tun_table *map = flow->tab;
854 /* All of these options have already been validated, so no need
855 * for sanity checking. */
856 while (opts_len > 0) {
857 struct tun_meta_entry *entry;
858 int len = sizeof(*opt) + opt->length * 4;
860 entry = tun_meta_find_key(&map->key_hmap,
861 tun_meta_key(opt->opt_class, opt->type));
863 memcpy_from_metadata(opt + 1, mask, &entry->loc);
865 memset(opt + 1, 0, opt->length * 4);
868 opt->opt_class = htons(0xffff);
875 opt = opt + len / sizeof(*opt);
881 tun_metadata_to_geneve_nlattr_mask(const struct ofpbuf *key,
882 const struct flow_tnl *mask,
883 const struct flow_tnl *flow,
886 const struct nlattr *geneve_key;
887 struct nlattr *geneve_mask;
888 struct geneve_opt *opt;
895 geneve_key = tun_metadata_find_geneve_key(key->data, key->size);
900 geneve_mask = ofpbuf_tail(b);
901 nl_msg_put(b, geneve_key, geneve_key->nla_len);
903 opt = CONST_CAST(struct geneve_opt *, nl_attr_get(geneve_mask));
904 opts_len = nl_attr_get_size(geneve_mask);
906 tun_metadata_to_geneve_mask__(&flow->metadata, &mask->metadata,
910 /* Convert from the tunnel metadata in 'tun' to netlink attributes stored
911 * in 'b'. Either UDPIF or non-UDPIF input forms are accepted.
913 * To assist with parsing, it is necessary to also pass in the tunnel metadata
914 * from the flow in 'flow' as well in the original netlink form of the flow in
917 tun_metadata_to_geneve_nlattr(const struct flow_tnl *tun,
918 const struct flow_tnl *flow,
919 const struct ofpbuf *key,
922 bool is_mask = tun != flow;
924 if (!(flow->flags & FLOW_TNL_F_UDPIF)) {
926 tun_metadata_to_geneve_nlattr_flow(tun, b);
928 tun_metadata_to_geneve_nlattr_mask(key, tun, flow, b);
930 } else if (flow->metadata.present.len || is_mask) {
931 nl_msg_put_unspec(b, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
932 tun->metadata.opts.gnv,
933 flow->metadata.present.len);
937 /* Converts 'mask_src' (in non-UDPIF format) to a series of masked options in
938 * 'dst'. 'flow_src' (also in non-UDPIF format) and the original set of
939 * options 'flow_src_opt'/'opts_len' are needed as a guide to interpret the
942 tun_metadata_to_geneve_udpif_mask(const struct flow_tnl *flow_src,
943 const struct flow_tnl *mask_src,
944 const struct geneve_opt *flow_src_opt,
945 int opts_len, struct geneve_opt *dst)
947 ovs_assert(!(flow_src->flags & FLOW_TNL_F_UDPIF));
949 memcpy(dst, flow_src_opt, opts_len);
950 tun_metadata_to_geneve_mask__(&flow_src->metadata,
951 &mask_src->metadata, dst, opts_len);
954 static const struct tun_metadata_loc *
955 metadata_loc_from_match_read(struct tun_table *map, const struct match *match,
956 unsigned int idx, struct flow_tnl *mask,
959 union mf_value mask_opts;
961 if (match->tun_md.valid) {
962 *is_masked = match->tun_md.entry[idx].masked;
963 return &match->tun_md.entry[idx].loc;
966 memcpy_from_metadata(mask_opts.tun_metadata, &mask->metadata,
967 &map->entries[idx].loc);
969 *is_masked = map->entries[idx].loc.len == 0 ||
970 !is_all_ones(mask_opts.tun_metadata,
971 map->entries[idx].loc.len);
972 return &map->entries[idx].loc;
976 tun_metadata_to_nx_match(struct ofpbuf *b, enum ofp_version oxm,
977 const struct match *match)
979 struct flow_tnl flow, mask;
982 if (!udpif_to_parsed(&match->flow.tunnel, &match->wc.masks.tunnel,
987 ULLONG_FOR_EACH_1 (i, mask.metadata.present.map) {
988 const struct tun_metadata_loc *loc;
991 union mf_value mask_opts;
993 loc = metadata_loc_from_match_read(flow.metadata.tab, match, i,
995 memcpy_from_metadata(opts.tun_metadata, &flow.metadata, loc);
996 memcpy_from_metadata(mask_opts.tun_metadata, &mask.metadata, loc);
997 nxm_put__(b, MFF_TUN_METADATA0 + i, oxm, opts.tun_metadata,
998 is_masked ? mask_opts.tun_metadata : NULL, loc->len);
1003 tun_metadata_match_format(struct ds *s, const struct match *match)
1005 struct flow_tnl flow, mask;
1008 if (!udpif_to_parsed(&match->flow.tunnel, &match->wc.masks.tunnel,
1013 ULLONG_FOR_EACH_1 (i, mask.metadata.present.map) {
1014 const struct tun_metadata_loc *loc;
1016 union mf_value opts, mask_opts;
1018 loc = metadata_loc_from_match_read(flow.metadata.tab, match, i,
1021 ds_put_format(s, "tun_metadata%u", i);
1022 memcpy_from_metadata(mask_opts.tun_metadata, &mask.metadata, loc);
1024 if (!ULLONG_GET(flow.metadata.present.map, i)) {
1025 /* Indicate that we are matching on the field being not present. */
1026 ds_put_cstr(s, "=NP");
1027 } else if (!(is_masked &&
1028 is_all_zeros(mask_opts.tun_metadata, loc->len))) {
1029 ds_put_char(s, '=');
1031 memcpy_from_metadata(opts.tun_metadata, &flow.metadata, loc);
1032 ds_put_hex(s, opts.tun_metadata, loc->len);
1034 if (!is_all_ones(mask_opts.tun_metadata, loc->len)) {
1035 ds_put_char(s, '/');
1036 ds_put_hex(s, mask_opts.tun_metadata, loc->len);
1039 ds_put_char(s, ',');