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 *);
64 static void memcpy_from_metadata(void *dst, const struct tun_metadata *src,
65 const struct tun_metadata_loc *);
68 tun_meta_key(ovs_be16 class, uint8_t type)
70 return (OVS_FORCE uint16_t)class << 8 | type;
74 tun_key_class(uint32_t key)
76 return (OVS_FORCE ovs_be16)(key >> 8);
80 tun_key_type(uint32_t key)
85 /* Returns a newly allocated tun_table. If 'old_map' is nonnull then the new
86 * tun_table is a deep copy of the old one. */
87 static struct tun_table *
88 table_alloc(const struct tun_table *old_map) OVS_REQUIRES(tab_mutex)
90 struct tun_table *new_map;
92 new_map = xzalloc(sizeof *new_map);
95 struct tun_meta_entry *entry;
98 hmap_init(&new_map->key_hmap);
100 HMAP_FOR_EACH (entry, node, &old_map->key_hmap) {
101 struct tun_meta_entry *new_entry;
102 struct tun_metadata_loc_chain *chain;
104 new_entry = &new_map->entries[entry - old_map->entries];
105 hmap_insert(&new_map->key_hmap, &new_entry->node, entry->node.hash);
107 chain = &new_entry->loc.c;
108 while (chain->next) {
109 chain->next = xmemdup(chain->next, sizeof *chain->next);
114 hmap_init(&new_map->key_hmap);
120 /* Frees 'map' and all the memory it owns. */
122 table_free(struct tun_table *map) OVS_REQUIRES(tab_mutex)
124 struct tun_meta_entry *entry;
130 HMAP_FOR_EACH (entry, node, &map->key_hmap) {
131 tun_metadata_del_entry(map, entry - map->entries);
137 /* Creates a global tunnel metadata mapping table, if none already exists. */
139 tun_metadata_init(void)
141 ovs_mutex_lock(&tab_mutex);
143 if (!ovsrcu_get_protected(struct tun_table *, &metadata_tab)) {
144 ovsrcu_set(&metadata_tab, table_alloc(NULL));
147 ovs_mutex_unlock(&tab_mutex);
151 tun_metadata_table_mod(struct ofputil_geneve_table_mod *gtm)
153 struct tun_table *old_map, *new_map;
154 struct ofputil_geneve_map *ofp_map;
157 ovs_mutex_lock(&tab_mutex);
159 old_map = ovsrcu_get_protected(struct tun_table *, &metadata_tab);
161 switch (gtm->command) {
163 new_map = table_alloc(old_map);
165 LIST_FOR_EACH (ofp_map, list_node, >m->mappings) {
166 err = tun_metadata_add_entry(new_map, ofp_map->index,
167 ofp_map->option_class,
168 ofp_map->option_type,
169 ofp_map->option_len);
178 new_map = table_alloc(old_map);
180 LIST_FOR_EACH (ofp_map, list_node, >m->mappings) {
181 tun_metadata_del_entry(new_map, ofp_map->index);
186 new_map = table_alloc(NULL);
193 ovsrcu_set(&metadata_tab, new_map);
194 ovsrcu_postpone(table_free, old_map);
197 ovs_mutex_unlock(&tab_mutex);
202 tun_metadata_table_request(struct ofputil_geneve_table_reply *gtr)
204 struct tun_table *map = ovsrcu_get(struct tun_table *, &metadata_tab);
207 gtr->max_option_space = TUN_METADATA_TOT_OPT_SIZE;
208 gtr->max_fields = TUN_METADATA_NUM_OPTS;
209 list_init(>r->mappings);
211 for (i = 0; i < TUN_METADATA_NUM_OPTS; i++) {
212 struct tun_meta_entry *entry = &map->entries[i];
213 struct ofputil_geneve_map *map;
219 map = xmalloc(sizeof *map);
220 map->option_class = ntohs(tun_key_class(entry->key));
221 map->option_type = tun_key_type(entry->key);
222 map->option_len = entry->loc.len;
225 list_push_back(>r->mappings, &map->list_node);
229 /* Copies the value of field 'mf' from 'tnl' (which must be in non-UDPIF format) * into 'value'.
231 * 'mf' must be an MFF_TUN_METADATA* field.
233 * This uses the global tunnel metadata mapping table created by
234 * tun_metadata_init(). If no such table has been created or if 'mf' hasn't
235 * been allocated in it yet, this just zeros 'value'. */
237 tun_metadata_read(const struct flow_tnl *tnl,
238 const struct mf_field *mf, union mf_value *value)
240 struct tun_table *map = ovsrcu_get(struct tun_table *, &metadata_tab);
241 unsigned int idx = mf->id - MFF_TUN_METADATA0;
242 struct tun_metadata_loc *loc;
245 memset(value->tun_metadata, 0, mf->n_bytes);
249 loc = &map->entries[idx].loc;
251 memset(value->tun_metadata, 0, mf->n_bytes - loc->len);
252 memcpy_from_metadata(value->tun_metadata + mf->n_bytes - loc->len,
253 &tnl->metadata, loc);
256 /* Copies 'value' into field 'mf' in 'tnl' (in non-UDPIF format).
258 * 'mf' must be an MFF_TUN_METADATA* field.
260 * This uses the global tunnel metadata mapping table created by
261 * tun_metadata_init(). If no such table has been created or if 'mf' hasn't
262 * been allocated in it yet, this function does nothing. */
264 tun_metadata_write(struct flow_tnl *tnl,
265 const struct mf_field *mf, const union mf_value *value)
267 struct tun_table *map = ovsrcu_get(struct tun_table *, &metadata_tab);
268 unsigned int idx = mf->id - MFF_TUN_METADATA0;
269 struct tun_metadata_loc *loc;
271 if (!map || !map->entries[idx].valid) {
275 loc = &map->entries[idx].loc;
277 ULLONG_SET1(tnl->metadata.present.map, idx);
278 memcpy_to_metadata(&tnl->metadata,
279 value->tun_metadata + mf->n_bytes - loc->len, loc);
282 static const struct tun_metadata_loc *
283 metadata_loc_from_match(struct tun_table *map, struct match *match,
284 unsigned int idx, unsigned int field_len)
286 ovs_assert(idx < TUN_METADATA_NUM_OPTS);
289 if (map->entries[idx].valid) {
290 return &map->entries[idx].loc;
296 if (match->tun_md.alloc_offset + field_len >= TUN_METADATA_TOT_OPT_SIZE ||
297 match->tun_md.loc[idx].len) {
301 match->tun_md.loc[idx].len = field_len;
302 match->tun_md.loc[idx].c.offset = match->tun_md.alloc_offset;
303 match->tun_md.loc[idx].c.len = field_len;
304 match->tun_md.loc[idx].c.next = NULL;
305 match->tun_md.alloc_offset += field_len;
306 match->tun_md.valid = true;
308 return &match->tun_md.loc[idx];
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;
334 unsigned int data_offset;
337 ovs_assert(!(match->flow.tunnel.flags & FLOW_TNL_F_UDPIF));
339 field_len = mf_field_len(mf, value, mask);
340 loc = metadata_loc_from_match(map, match, idx, field_len);
345 data_offset = mf->n_bytes - loc->len;
348 memset(data.tun_metadata, 0, loc->len);
350 memcpy(data.tun_metadata, value->tun_metadata + data_offset, loc->len);
353 for (i = 0; i < loc->len; i++) {
354 data.tun_metadata[i] = value->tun_metadata[data_offset + i] &
355 mask->tun_metadata[data_offset + i];
358 ULLONG_SET1(match->flow.tunnel.metadata.present.map, idx);
359 memcpy_to_metadata(&match->flow.tunnel.metadata, data.tun_metadata, loc);
362 memset(data.tun_metadata, 0, loc->len);
364 memset(data.tun_metadata, 0xff, loc->len);
366 memcpy(data.tun_metadata, mask->tun_metadata + data_offset, loc->len);
368 ULLONG_SET1(match->wc.masks.tunnel.metadata.present.map, idx);
369 memcpy_to_metadata(&match->wc.masks.tunnel.metadata, data.tun_metadata, loc);
373 udpif_to_parsed(const struct flow_tnl *flow, const struct flow_tnl *mask,
374 struct flow_tnl *flow_xlate, struct flow_tnl *mask_xlate)
376 if (flow->flags & FLOW_TNL_F_UDPIF) {
379 err = tun_metadata_from_geneve_udpif(flow, flow, flow_xlate);
385 tun_metadata_from_geneve_udpif(flow, mask, mask_xlate);
391 if (flow->metadata.present.map == 0) {
392 /* There is no tunnel metadata, don't bother copying. */
396 memcpy(flow_xlate, flow, sizeof *flow_xlate);
398 memcpy(mask_xlate, mask, sizeof *mask_xlate);
401 if (!flow_xlate->metadata.tab) {
402 flow_xlate->metadata.tab = ovsrcu_get(struct tun_table *,
410 /* Copies all MFF_TUN_METADATA* fields from 'tnl' to 'flow_metadata'. */
412 tun_metadata_get_fmd(const struct flow_tnl *tnl, struct match *flow_metadata)
414 struct flow_tnl flow;
417 if (!udpif_to_parsed(tnl, NULL, &flow, NULL)) {
421 ULLONG_FOR_EACH_1 (i, flow.metadata.present.map) {
423 const struct tun_metadata_loc *old_loc = &flow.metadata.tab->entries[i].loc;
424 const struct tun_metadata_loc *new_loc;
426 new_loc = metadata_loc_from_match(NULL, flow_metadata, i, old_loc->len);
428 memcpy_from_metadata(opts.tun_metadata, &flow.metadata, old_loc);
429 memcpy_to_metadata(&flow_metadata->flow.tunnel.metadata,
430 opts.tun_metadata, new_loc);
432 memset(opts.tun_metadata, 0xff, old_loc->len);
433 memcpy_to_metadata(&flow_metadata->wc.masks.tunnel.metadata,
434 opts.tun_metadata, new_loc);
439 tun_meta_hash(uint32_t key)
441 return hash_int(key, 0);
444 static struct tun_meta_entry *
445 tun_meta_find_key(const struct hmap *hmap, uint32_t key)
447 struct tun_meta_entry *entry;
449 HMAP_FOR_EACH_IN_BUCKET (entry, node, tun_meta_hash(key), hmap) {
450 if (entry->key == key) {
458 memcpy_to_metadata(struct tun_metadata *dst, const void *src,
459 const struct tun_metadata_loc *loc)
461 const struct tun_metadata_loc_chain *chain = &loc->c;
465 memcpy(dst->opts.u8 + loc->c.offset + addr, (uint8_t *)src + addr,
473 memcpy_from_metadata(void *dst, const struct tun_metadata *src,
474 const struct tun_metadata_loc *loc)
476 const struct tun_metadata_loc_chain *chain = &loc->c;
480 memcpy((uint8_t *)dst + addr, src->opts.u8 + loc->c.offset + addr,
488 tun_metadata_alloc_chain(struct tun_table *map, uint8_t len,
489 struct tun_metadata_loc_chain *loc)
490 OVS_REQUIRES(tab_mutex)
492 int alloc_len = len / 4;
494 int scan_end = TUN_METADATA_TOT_OPT_SIZE / 4;
495 int pos_start, pos_end, pos_len;
496 int best_start = 0, best_len = 0;
499 pos_start = bitmap_scan(map->alloc_map, 0, scan_start, scan_end);
500 if (pos_start == scan_end) {
504 pos_end = bitmap_scan(map->alloc_map, 1, pos_start,
505 MIN(pos_start + alloc_len, scan_end));
506 pos_len = pos_end - pos_start;
507 if (pos_len == alloc_len) {
511 if (pos_len > best_len) {
512 best_start = pos_start;
515 scan_start = pos_end + 1;
522 pos_start = best_start;
526 bitmap_set_multiple(map->alloc_map, pos_start, pos_len, 1);
527 loc->offset = pos_start * 4;
528 loc->len = pos_len * 4;
534 tun_metadata_add_entry(struct tun_table *map, uint8_t idx, uint16_t opt_class,
535 uint8_t type, uint8_t len) OVS_REQUIRES(tab_mutex)
537 struct tun_meta_entry *entry;
538 struct tun_metadata_loc_chain *cur_chain, *prev_chain;
540 ovs_assert(idx < TUN_METADATA_NUM_OPTS);
542 entry = &map->entries[idx];
544 return OFPERR_NXGTMFC_ALREADY_MAPPED;
547 entry->key = tun_meta_key(htons(opt_class), type);
548 if (tun_meta_find_key(&map->key_hmap, entry->key)) {
549 return OFPERR_NXGTMFC_DUP_ENTRY;
553 hmap_insert(&map->key_hmap, &entry->node,
554 tun_meta_hash(entry->key));
556 entry->loc.len = len;
557 cur_chain = &entry->loc.c;
558 memset(cur_chain, 0, sizeof *cur_chain);
565 cur_chain = xzalloc(sizeof *cur_chain);
568 err = tun_metadata_alloc_chain(map, len, cur_chain);
570 tun_metadata_del_entry(map, idx);
571 return OFPERR_NXGTMFC_TABLE_FULL;
574 len -= cur_chain->len;
577 prev_chain->next = cur_chain;
579 prev_chain = cur_chain;
587 tun_metadata_del_entry(struct tun_table *map, uint8_t idx)
588 OVS_REQUIRES(tab_mutex)
590 struct tun_meta_entry *entry;
591 struct tun_metadata_loc_chain *chain;
593 if (idx >= TUN_METADATA_NUM_OPTS) {
597 entry = &map->entries[idx];
602 chain = &entry->loc.c;
604 struct tun_metadata_loc_chain *next = chain->next;
606 bitmap_set_multiple(map->alloc_map, chain->offset / 4,
608 if (chain != &entry->loc.c) {
614 entry->valid = false;
615 hmap_remove(&map->key_hmap, &entry->node);
616 memset(&entry->loc, 0, sizeof entry->loc);
620 tun_metadata_from_geneve__(const struct tun_metadata *flow_metadata,
621 const struct geneve_opt *opt,
622 const struct geneve_opt *flow_opt, int opts_len,
623 struct tun_metadata *metadata)
625 struct tun_table *map;
626 bool is_mask = flow_opt != opt;
629 map = ovsrcu_get(struct tun_table *, &metadata_tab);
632 map = flow_metadata->tab;
639 while (opts_len > 0) {
641 struct tun_meta_entry *entry;
643 if (opts_len < sizeof(*opt)) {
647 len = sizeof(*opt) + flow_opt->length * 4;
648 if (len > opts_len) {
652 entry = tun_meta_find_key(&map->key_hmap,
653 tun_meta_key(flow_opt->opt_class,
656 if (entry->loc.len == flow_opt->length * 4) {
657 memcpy_to_metadata(metadata, opt + 1, &entry->loc);
658 ULLONG_SET1(metadata->present.map, entry - map->entries);
662 } else if (flow_opt->type & GENEVE_CRIT_OPT_TYPE) {
666 opt = opt + len / sizeof(*opt);
667 flow_opt = flow_opt + len / sizeof(*opt);
674 static const struct nlattr *
675 tun_metadata_find_geneve_key(const struct nlattr *key, uint32_t key_len)
677 const struct nlattr *tnl_key;
679 tnl_key = nl_attr_find__(key, key_len, OVS_KEY_ATTR_TUNNEL);
684 return nl_attr_find_nested(tnl_key, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS);
687 /* Converts from Geneve netlink attributes in 'attr' to tunnel metadata
688 * in 'tun'. The result may either in be UDPIF format or not, as determined
691 * In the event that a mask is being converted, it is also necessary to
692 * pass in flow information. This includes the full set of netlink attributes
693 * (i.e. not just the Geneve attribute) in 'flow_attrs'/'flow_attr_len' and
694 * the previously converted tunnel metadata 'flow_tun'.
696 * If a flow rather than mask is being converted, 'flow_attrs' must be NULL. */
698 tun_metadata_from_geneve_nlattr(const struct nlattr *attr,
699 const struct nlattr *flow_attrs,
700 size_t flow_attr_len,
701 const struct flow_tnl *flow_tun, bool udpif,
702 struct flow_tnl *tun)
704 bool is_mask = !!flow_attrs;
705 int attr_len = nl_attr_get_size(attr);
706 const struct nlattr *flow;
708 /* No need for real translation, just copy things over. */
710 memcpy(tun->metadata.opts.gnv, nl_attr_get(attr), attr_len);
713 tun->metadata.present.len = attr_len;
714 tun->flags |= FLOW_TNL_F_UDPIF;
716 /* We need to exact match on the length so we don't
717 * accidentally match on sets of options that are the same
718 * at the beginning but with additional options after. */
719 tun->metadata.present.len = 0xff;
726 flow = tun_metadata_find_geneve_key(flow_attrs, flow_attr_len);
728 return attr_len ? EINVAL : 0;
731 if (attr_len != nl_attr_get_size(flow)) {
738 return tun_metadata_from_geneve__(&flow_tun->metadata, nl_attr_get(attr),
739 nl_attr_get(flow), nl_attr_get_size(flow),
743 /* Converts from the flat Geneve options representation extracted directly
744 * from the tunnel header to the representation that maps options to
745 * pre-allocated locations. The original version (in UDPIF form) is passed
746 * in 'src' and the translated form in stored in 'dst'. To handle masks, the
747 * flow must also be passed in through 'flow' (in the original, raw form). */
749 tun_metadata_from_geneve_udpif(const struct flow_tnl *flow,
750 const struct flow_tnl *src,
751 struct flow_tnl *dst)
753 ovs_assert(flow->flags & FLOW_TNL_F_UDPIF);
756 dst->flags = flow->flags & ~FLOW_TNL_F_UDPIF;
758 dst->metadata.tab = NULL;
760 dst->metadata.present.map = 0;
761 return tun_metadata_from_geneve__(&flow->metadata, src->metadata.opts.gnv,
762 flow->metadata.opts.gnv,
763 flow->metadata.present.len,
768 tun_metadata_to_geneve__(const struct tun_metadata *flow, struct ofpbuf *b,
771 struct tun_table *map;
776 map = ovsrcu_get(struct tun_table *, &metadata_tab);
781 ULLONG_FOR_EACH_1 (i, flow->present.map) {
782 struct tun_meta_entry *entry = &map->entries[i];
783 struct geneve_opt *opt;
785 opt = ofpbuf_put_uninit(b, sizeof *opt + entry->loc.len);
787 opt->opt_class = tun_key_class(entry->key);
788 opt->type = tun_key_type(entry->key);
789 opt->length = entry->loc.len / 4;
794 memcpy_from_metadata(opt + 1, flow, &entry->loc);
795 *crit_opt |= !!(opt->type & GENEVE_CRIT_OPT_TYPE);
800 tun_metadata_to_geneve_nlattr_flow(const struct flow_tnl *flow,
803 size_t nlattr_offset;
806 if (!flow->metadata.present.map) {
810 /* For all intents and purposes, the Geneve options are nested
811 * attributes even if this doesn't show up directly to netlink. It's
812 * similar enough that we can use the same mechanism. */
813 nlattr_offset = nl_msg_start_nested(b, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS);
815 tun_metadata_to_geneve__(&flow->metadata, b, &crit_opt);
817 nl_msg_end_nested(b, nlattr_offset);
820 /* Converts from processed tunnel metadata information (in non-udpif
821 * format) in 'flow' to a stream of Geneve options suitable for
822 * transmission in 'opts'. Additionally returns whether there were
823 * any critical options in 'crit_opt' as well as the total length of
826 tun_metadata_to_geneve_header(const struct flow_tnl *flow,
827 struct geneve_opt *opts, bool *crit_opt)
831 ovs_assert(!(flow->flags & FLOW_TNL_F_UDPIF));
833 ofpbuf_use_stack(&b, opts, GENEVE_TOT_OPT_SIZE);
834 tun_metadata_to_geneve__(&flow->metadata, &b, crit_opt);
840 tun_metadata_to_geneve_mask__(const struct tun_metadata *flow,
841 const struct tun_metadata *mask,
842 struct geneve_opt *opt, int opts_len)
844 struct tun_table *map = flow->tab;
850 /* All of these options have already been validated, so no need
851 * for sanity checking. */
852 while (opts_len > 0) {
853 struct tun_meta_entry *entry;
854 int len = sizeof(*opt) + opt->length * 4;
856 entry = tun_meta_find_key(&map->key_hmap,
857 tun_meta_key(opt->opt_class, opt->type));
859 memcpy_from_metadata(opt + 1, mask, &entry->loc);
861 memset(opt + 1, 0, opt->length * 4);
864 opt->opt_class = htons(0xffff);
871 opt = opt + len / sizeof(*opt);
877 tun_metadata_to_geneve_nlattr_mask(const struct ofpbuf *key,
878 const struct flow_tnl *mask,
879 const struct flow_tnl *flow,
882 const struct nlattr *geneve_key;
883 struct nlattr *geneve_mask;
884 struct geneve_opt *opt;
891 geneve_key = tun_metadata_find_geneve_key(key->data, key->size);
896 geneve_mask = ofpbuf_tail(b);
897 nl_msg_put(b, geneve_key, geneve_key->nla_len);
899 opt = CONST_CAST(struct geneve_opt *, nl_attr_get(geneve_mask));
900 opts_len = nl_attr_get_size(geneve_mask);
902 tun_metadata_to_geneve_mask__(&flow->metadata, &mask->metadata,
906 /* Convert from the tunnel metadata in 'tun' to netlink attributes stored
907 * in 'b'. Either UDPIF or non-UDPIF input forms are accepted.
909 * To assist with parsing, it is necessary to also pass in the tunnel metadata
910 * from the flow in 'flow' as well in the original netlink form of the flow in
913 tun_metadata_to_geneve_nlattr(const struct flow_tnl *tun,
914 const struct flow_tnl *flow,
915 const struct ofpbuf *key,
918 bool is_mask = tun != flow;
920 if (!(flow->flags & FLOW_TNL_F_UDPIF)) {
922 tun_metadata_to_geneve_nlattr_flow(tun, b);
924 tun_metadata_to_geneve_nlattr_mask(key, tun, flow, b);
926 } else if (flow->metadata.present.len || is_mask) {
927 nl_msg_put_unspec(b, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
928 tun->metadata.opts.gnv,
929 flow->metadata.present.len);
933 /* Converts 'mask_src' (in non-UDPIF format) to a series of masked options in
934 * 'dst'. 'flow_src' (also in non-UDPIF format) and the original set of
935 * options 'flow_src_opt'/'opts_len' are needed as a guide to interpret the
938 tun_metadata_to_geneve_udpif_mask(const struct flow_tnl *flow_src,
939 const struct flow_tnl *mask_src,
940 const struct geneve_opt *flow_src_opt,
941 int opts_len, struct geneve_opt *dst)
943 ovs_assert(!(flow_src->flags & FLOW_TNL_F_UDPIF));
945 memcpy(dst, flow_src_opt, opts_len);
946 tun_metadata_to_geneve_mask__(&flow_src->metadata,
947 &mask_src->metadata, dst, opts_len);
950 static const struct tun_metadata_loc *
951 metadata_loc_from_match_read(struct tun_table *map, const struct match *match,
954 if (match->tun_md.valid) {
955 return &match->tun_md.loc[idx];
958 return &map->entries[idx].loc;
962 tun_metadata_to_nx_match(struct ofpbuf *b, enum ofp_version oxm,
963 const struct match *match)
965 struct flow_tnl flow, mask;
968 if (!udpif_to_parsed(&match->flow.tunnel, &match->wc.masks.tunnel,
973 ULLONG_FOR_EACH_1 (i, mask.metadata.present.map) {
974 const struct tun_metadata_loc *loc;
976 union mf_value mask_opts;
978 loc = metadata_loc_from_match_read(flow.metadata.tab, match, i);
979 memcpy_from_metadata(opts.tun_metadata, &flow.metadata, loc);
980 memcpy_from_metadata(mask_opts.tun_metadata, &mask.metadata, loc);
981 nxm_put(b, MFF_TUN_METADATA0 + i, oxm, opts.tun_metadata,
982 mask_opts.tun_metadata, loc->len);
987 tun_metadata_match_format(struct ds *s, const struct match *match)
989 struct flow_tnl flow, mask;
992 if (!udpif_to_parsed(&match->flow.tunnel, &match->wc.masks.tunnel,
997 ULLONG_FOR_EACH_1 (i, mask.metadata.present.map) {
998 const struct tun_metadata_loc *loc;
1001 loc = metadata_loc_from_match_read(flow.metadata.tab, match, i);
1003 ds_put_format(s, "tun_metadata%u=", i);
1004 memcpy_from_metadata(opts.tun_metadata, &flow.metadata, loc);
1005 ds_put_hex(s, opts.tun_metadata, loc->len);
1007 memcpy_from_metadata(opts.tun_metadata, &mask.metadata, loc);
1008 if (!is_all_ones(opts.tun_metadata, loc->len)) {
1009 ds_put_char(s, '/');
1010 ds_put_hex(s, opts.tun_metadata, loc->len);
1012 ds_put_char(s, ',');