]> git.proxmox.com Git - mirror_ovs.git/blob - lib/meta-flow.c
64a8cf12047ae8e38df25e574e8587558e4a3377
[mirror_ovs.git] / lib / meta-flow.c
1 /*
2 * Copyright (c) 2011-2017 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18
19 #include "openvswitch/meta-flow.h"
20
21 #include <errno.h>
22 #include <limits.h>
23 #include <netinet/icmp6.h>
24 #include <netinet/ip6.h>
25
26 #include "classifier.h"
27 #include "openvswitch/dynamic-string.h"
28 #include "nx-match.h"
29 #include "openvswitch/ofp-util.h"
30 #include "ovs-atomic.h"
31 #include "ovs-rcu.h"
32 #include "ovs-thread.h"
33 #include "packets.h"
34 #include "random.h"
35 #include "openvswitch/shash.h"
36 #include "socket-util.h"
37 #include "tun-metadata.h"
38 #include "unaligned.h"
39 #include "util.h"
40 #include "openvswitch/ofp-errors.h"
41 #include "openvswitch/vlog.h"
42 #include "vl-mff-map.h"
43
44 VLOG_DEFINE_THIS_MODULE(meta_flow);
45
46 #define FLOW_U32OFS(FIELD) \
47 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
48
49 #define MF_FIELD_SIZES(MEMBER) \
50 sizeof ((union mf_value *)0)->MEMBER, \
51 8 * sizeof ((union mf_value *)0)->MEMBER
52
53 extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */
54
55 const struct mf_field mf_fields[MFF_N_IDS] = {
56 #include "meta-flow.inc"
57 };
58
59 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
60 static struct shash mf_by_name;
61
62 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
63 * controller and so there's not much point in showing a lot of them. */
64 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
65
66 #define MF_VALUE_EXACT_8 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
67 #define MF_VALUE_EXACT_16 MF_VALUE_EXACT_8, MF_VALUE_EXACT_8
68 #define MF_VALUE_EXACT_32 MF_VALUE_EXACT_16, MF_VALUE_EXACT_16
69 #define MF_VALUE_EXACT_64 MF_VALUE_EXACT_32, MF_VALUE_EXACT_32
70 #define MF_VALUE_EXACT_128 MF_VALUE_EXACT_64, MF_VALUE_EXACT_64
71 #define MF_VALUE_EXACT_INITIALIZER { .tun_metadata = { MF_VALUE_EXACT_128 } }
72
73 const union mf_value exact_match_mask = MF_VALUE_EXACT_INITIALIZER;
74
75 static void nxm_init(void);
76
77 /* Returns the field with the given 'name', or a null pointer if no field has
78 * that name. */
79 const struct mf_field *
80 mf_from_name(const char *name)
81 {
82 nxm_init();
83 return shash_find_data(&mf_by_name, name);
84 }
85
86 /* Returns the field with the given 'name' (which is 'len' bytes long), or a
87 * null pointer if no field has that name. */
88 const struct mf_field *
89 mf_from_name_len(const char *name, size_t len)
90 {
91 nxm_init();
92
93 struct shash_node *node = shash_find_len(&mf_by_name, name, len);
94 return node ? node->data : NULL;
95 }
96
97 static void
98 nxm_do_init(void)
99 {
100 int i;
101
102 shash_init(&mf_by_name);
103 for (i = 0; i < MFF_N_IDS; i++) {
104 const struct mf_field *mf = &mf_fields[i];
105
106 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
107
108 shash_add_once(&mf_by_name, mf->name, mf);
109 if (mf->extra_name) {
110 shash_add_once(&mf_by_name, mf->extra_name, mf);
111 }
112 }
113 }
114
115 static void
116 nxm_init(void)
117 {
118 static pthread_once_t once = PTHREAD_ONCE_INIT;
119 pthread_once(&once, nxm_do_init);
120 }
121
122 /* Consider the two value/mask pairs 'a_value/a_mask' and 'b_value/b_mask' as
123 * restrictions on a field's value. Then, this function initializes
124 * 'dst_value/dst_mask' such that it combines the restrictions of both pairs.
125 * This is not always possible, i.e. if one pair insists on a value of 0 in
126 * some bit and the other pair insists on a value of 1 in that bit. This
127 * function returns false in a case where the combined restriction is
128 * impossible (in which case 'dst_value/dst_mask' is not fully initialized),
129 * true otherwise.
130 *
131 * (As usually true for value/mask pairs in OVS, any 1-bit in a value must have
132 * a corresponding 1-bit in its mask.) */
133 bool
134 mf_subvalue_intersect(const union mf_subvalue *a_value,
135 const union mf_subvalue *a_mask,
136 const union mf_subvalue *b_value,
137 const union mf_subvalue *b_mask,
138 union mf_subvalue *dst_value,
139 union mf_subvalue *dst_mask)
140 {
141 for (int i = 0; i < ARRAY_SIZE(a_value->be64); i++) {
142 ovs_be64 av = a_value->be64[i];
143 ovs_be64 am = a_mask->be64[i];
144 ovs_be64 bv = b_value->be64[i];
145 ovs_be64 bm = b_mask->be64[i];
146 ovs_be64 *dv = &dst_value->be64[i];
147 ovs_be64 *dm = &dst_mask->be64[i];
148
149 if ((av ^ bv) & (am & bm)) {
150 return false;
151 }
152 *dv = av | bv;
153 *dm = am | bm;
154 }
155 return true;
156 }
157
158 /* Returns the "number of bits" in 'v', e.g. 1 if only the lowest-order bit is
159 * set, 2 if the second-lowest-order bit is set, and so on. */
160 int
161 mf_subvalue_width(const union mf_subvalue *v)
162 {
163 return 1 + bitwise_rscan(v, sizeof *v, true, sizeof *v * 8 - 1, -1);
164 }
165
166 /* For positive 'n', shifts the bits in 'value' 'n' bits to the left, and for
167 * negative 'n', shifts the bits '-n' bits to the right. */
168 void
169 mf_subvalue_shift(union mf_subvalue *value, int n)
170 {
171 if (n) {
172 union mf_subvalue tmp;
173 memset(&tmp, 0, sizeof tmp);
174
175 if (n > 0 && n < 8 * sizeof tmp) {
176 bitwise_copy(value, sizeof *value, 0,
177 &tmp, sizeof tmp, n,
178 8 * sizeof tmp - n);
179 } else if (n < 0 && n > -8 * sizeof tmp) {
180 bitwise_copy(value, sizeof *value, -n,
181 &tmp, sizeof tmp, 0,
182 8 * sizeof tmp + n);
183 }
184 *value = tmp;
185 }
186 }
187
188 /* Appends a formatted representation of 'sv' to 's'. */
189 void
190 mf_subvalue_format(const union mf_subvalue *sv, struct ds *s)
191 {
192 ds_put_hex(s, sv, sizeof *sv);
193 }
194
195 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
196 * specifies at least one bit in the field.
197 *
198 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
199 * meets 'mf''s prerequisites. */
200 bool
201 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
202 {
203 switch (mf->id) {
204 case MFF_DP_HASH:
205 return !wc->masks.dp_hash;
206 case MFF_RECIRC_ID:
207 return !wc->masks.recirc_id;
208 case MFF_PACKET_TYPE:
209 return !wc->masks.packet_type;
210 case MFF_CONJ_ID:
211 return !wc->masks.conj_id;
212 case MFF_TUN_SRC:
213 return !wc->masks.tunnel.ip_src;
214 case MFF_TUN_DST:
215 return !wc->masks.tunnel.ip_dst;
216 case MFF_TUN_IPV6_SRC:
217 return ipv6_mask_is_any(&wc->masks.tunnel.ipv6_src);
218 case MFF_TUN_IPV6_DST:
219 return ipv6_mask_is_any(&wc->masks.tunnel.ipv6_dst);
220 case MFF_TUN_ID:
221 return !wc->masks.tunnel.tun_id;
222 case MFF_TUN_TOS:
223 return !wc->masks.tunnel.ip_tos;
224 case MFF_TUN_TTL:
225 return !wc->masks.tunnel.ip_ttl;
226 case MFF_TUN_FLAGS:
227 return !(wc->masks.tunnel.flags & FLOW_TNL_PUB_F_MASK);
228 case MFF_TUN_GBP_ID:
229 return !wc->masks.tunnel.gbp_id;
230 case MFF_TUN_GBP_FLAGS:
231 return !wc->masks.tunnel.gbp_flags;
232 CASE_MFF_TUN_METADATA:
233 return !ULLONG_GET(wc->masks.tunnel.metadata.present.map,
234 mf->id - MFF_TUN_METADATA0);
235 case MFF_METADATA:
236 return !wc->masks.metadata;
237 case MFF_IN_PORT:
238 case MFF_IN_PORT_OXM:
239 return !wc->masks.in_port.ofp_port;
240 case MFF_SKB_PRIORITY:
241 return !wc->masks.skb_priority;
242 case MFF_PKT_MARK:
243 return !wc->masks.pkt_mark;
244 case MFF_CT_STATE:
245 return !wc->masks.ct_state;
246 case MFF_CT_ZONE:
247 return !wc->masks.ct_zone;
248 case MFF_CT_MARK:
249 return !wc->masks.ct_mark;
250 case MFF_CT_LABEL:
251 return ovs_u128_is_zero(wc->masks.ct_label);
252 case MFF_CT_NW_PROTO:
253 return !wc->masks.ct_nw_proto;
254 case MFF_CT_NW_SRC:
255 return !wc->masks.ct_nw_src;
256 case MFF_CT_NW_DST:
257 return !wc->masks.ct_nw_dst;
258 case MFF_CT_TP_SRC:
259 return !wc->masks.ct_tp_src;
260 case MFF_CT_TP_DST:
261 return !wc->masks.ct_tp_dst;
262 case MFF_CT_IPV6_SRC:
263 return ipv6_mask_is_any(&wc->masks.ct_ipv6_src);
264 case MFF_CT_IPV6_DST:
265 return ipv6_mask_is_any(&wc->masks.ct_ipv6_dst);
266 CASE_MFF_REGS:
267 return !wc->masks.regs[mf->id - MFF_REG0];
268 CASE_MFF_XREGS:
269 return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
270 CASE_MFF_XXREGS: {
271 ovs_u128 value = flow_get_xxreg(&wc->masks, mf->id - MFF_XXREG0);
272 return ovs_u128_is_zero(value);
273 }
274 case MFF_ACTSET_OUTPUT:
275 return !wc->masks.actset_output;
276
277 case MFF_ETH_SRC:
278 return eth_addr_is_zero(wc->masks.dl_src);
279 case MFF_ETH_DST:
280 return eth_addr_is_zero(wc->masks.dl_dst);
281 case MFF_ETH_TYPE:
282 return !wc->masks.dl_type;
283
284 case MFF_ARP_SHA:
285 case MFF_ND_SLL:
286 return eth_addr_is_zero(wc->masks.arp_sha);
287
288 case MFF_ARP_THA:
289 case MFF_ND_TLL:
290 return eth_addr_is_zero(wc->masks.arp_tha);
291
292 case MFF_VLAN_TCI:
293 return !wc->masks.vlans[0].tci;
294 case MFF_DL_VLAN:
295 return !(wc->masks.vlans[0].tci & htons(VLAN_VID_MASK));
296 case MFF_VLAN_VID:
297 return !(wc->masks.vlans[0].tci & htons(VLAN_VID_MASK | VLAN_CFI));
298 case MFF_DL_VLAN_PCP:
299 case MFF_VLAN_PCP:
300 return !(wc->masks.vlans[0].tci & htons(VLAN_PCP_MASK));
301
302 case MFF_MPLS_LABEL:
303 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
304 case MFF_MPLS_TC:
305 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
306 case MFF_MPLS_BOS:
307 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
308 case MFF_MPLS_TTL:
309 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK));
310
311 case MFF_IPV4_SRC:
312 return !wc->masks.nw_src;
313 case MFF_IPV4_DST:
314 return !wc->masks.nw_dst;
315
316 case MFF_IPV6_SRC:
317 return ipv6_mask_is_any(&wc->masks.ipv6_src);
318 case MFF_IPV6_DST:
319 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
320
321 case MFF_IPV6_LABEL:
322 return !wc->masks.ipv6_label;
323
324 case MFF_IP_PROTO:
325 return !wc->masks.nw_proto;
326 case MFF_IP_DSCP:
327 case MFF_IP_DSCP_SHIFTED:
328 return !(wc->masks.nw_tos & IP_DSCP_MASK);
329 case MFF_IP_ECN:
330 return !(wc->masks.nw_tos & IP_ECN_MASK);
331 case MFF_IP_TTL:
332 return !wc->masks.nw_ttl;
333
334 case MFF_ND_TARGET:
335 return ipv6_mask_is_any(&wc->masks.nd_target);
336
337 case MFF_IP_FRAG:
338 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
339
340 case MFF_ARP_OP:
341 return !wc->masks.nw_proto;
342 case MFF_ARP_SPA:
343 return !wc->masks.nw_src;
344 case MFF_ARP_TPA:
345 return !wc->masks.nw_dst;
346
347 case MFF_TCP_SRC:
348 case MFF_UDP_SRC:
349 case MFF_SCTP_SRC:
350 case MFF_ICMPV4_TYPE:
351 case MFF_ICMPV6_TYPE:
352 return !wc->masks.tp_src;
353 case MFF_TCP_DST:
354 case MFF_UDP_DST:
355 case MFF_SCTP_DST:
356 case MFF_ICMPV4_CODE:
357 case MFF_ICMPV6_CODE:
358 return !wc->masks.tp_dst;
359 case MFF_TCP_FLAGS:
360 return !wc->masks.tcp_flags;
361
362 case MFF_NSH_FLAGS:
363 return !wc->masks.nsh.flags;
364 case MFF_NSH_MDTYPE:
365 return !wc->masks.nsh.mdtype;
366 case MFF_NSH_NP:
367 return !wc->masks.nsh.np;
368 case MFF_NSH_SPI:
369 return !wc->masks.nsh.spi;
370 case MFF_NSH_SI:
371 return !wc->masks.nsh.si;
372 case MFF_NSH_C1:
373 case MFF_NSH_C2:
374 case MFF_NSH_C3:
375 case MFF_NSH_C4:
376 return !wc->masks.nsh.c[mf->id - MFF_NSH_C1];
377
378 case MFF_N_IDS:
379 default:
380 OVS_NOT_REACHED();
381 }
382 }
383
384 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
385 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
386 * purposes, or to 0 if it is wildcarded.
387 *
388 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
389 * meets 'mf''s prerequisites. */
390 void
391 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
392 union mf_value *mask)
393 {
394 mf_get_value(mf, &wc->masks, mask);
395 }
396
397 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
398 * if the mask is valid, false otherwise. */
399 bool
400 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
401 {
402 switch (mf->maskable) {
403 case MFM_NONE:
404 return (is_all_zeros(mask, mf->n_bytes) ||
405 is_all_ones(mask, mf->n_bytes));
406
407 case MFM_FULLY:
408 return true;
409 }
410
411 OVS_NOT_REACHED();
412 }
413
414 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
415 * If a non-NULL 'mask' is passed, zero-valued matches can also be verified.
416 * Sets inspected bits in 'wc', if non-NULL. */
417 static bool
418 mf_are_prereqs_ok__(const struct mf_field *mf, const struct flow *flow,
419 const struct flow_wildcards *mask,
420 struct flow_wildcards *wc)
421 {
422 ovs_be16 dl_type = get_dl_type(flow);
423
424 switch (mf->prereqs) {
425 case MFP_NONE:
426 return true;
427 case MFP_ETHERNET:
428 return is_ethernet(flow, wc);
429 case MFP_ARP:
430 return (dl_type == htons(ETH_TYPE_ARP) ||
431 dl_type == htons(ETH_TYPE_RARP));
432 case MFP_IPV4:
433 return dl_type == htons(ETH_TYPE_IP);
434 case MFP_IPV6:
435 return dl_type == htons(ETH_TYPE_IPV6);
436 case MFP_VLAN_VID:
437 return is_vlan(flow, wc);
438 case MFP_MPLS:
439 return eth_type_mpls(dl_type);
440 case MFP_IP_ANY:
441 return is_ip_any(flow);
442 case MFP_NSH:
443 return dl_type == htons(ETH_TYPE_NSH);
444 case MFP_CT_VALID:
445 return is_ct_valid(flow, mask, wc);
446 case MFP_TCP:
447 /* Matching !FRAG_LATER is not enforced (mask is not checked). */
448 return is_tcp(flow, wc) && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
449 case MFP_UDP:
450 return is_udp(flow, wc) && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
451 case MFP_SCTP:
452 return is_sctp(flow, wc) && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
453 case MFP_ICMPV4:
454 return is_icmpv4(flow, wc);
455 case MFP_ICMPV6:
456 return is_icmpv6(flow, wc);
457 case MFP_ND:
458 return is_nd(flow, wc);
459 case MFP_ND_SOLICIT:
460 return is_nd(flow, wc) && flow->tp_src == htons(ND_NEIGHBOR_SOLICIT);
461 case MFP_ND_ADVERT:
462 return is_nd(flow, wc) && flow->tp_src == htons(ND_NEIGHBOR_ADVERT);
463 }
464
465 OVS_NOT_REACHED();
466 }
467
468 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
469 * Sets inspected bits in 'wc', if non-NULL. */
470 bool
471 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow,
472 struct flow_wildcards *wc)
473 {
474 return mf_are_prereqs_ok__(mf, flow, NULL, wc);
475 }
476
477 /* Returns true if 'match' meets the prerequisites for 'mf', false otherwise.
478 */
479 bool
480 mf_are_match_prereqs_ok(const struct mf_field *mf, const struct match *match)
481 {
482 return mf_are_prereqs_ok__(mf, &match->flow, &match->wc, NULL);
483 }
484
485 /* Returns true if 'value' may be a valid value *as part of a masked match*,
486 * false otherwise.
487 *
488 * A value is not rejected just because it is not valid for the field in
489 * question, but only if it doesn't make sense to test the bits in question at
490 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
491 * without the VLAN_CFI bit being set, but we can't reject those values because
492 * it is still legitimate to test just for those bits (see the documentation
493 * for NXM_OF_VLAN_TCI in meta-flow.h). On the other hand, there is never a
494 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
495 bool
496 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
497 {
498 switch (mf->id) {
499 case MFF_DP_HASH:
500 case MFF_RECIRC_ID:
501 case MFF_PACKET_TYPE:
502 case MFF_CONJ_ID:
503 case MFF_TUN_ID:
504 case MFF_TUN_SRC:
505 case MFF_TUN_DST:
506 case MFF_TUN_IPV6_SRC:
507 case MFF_TUN_IPV6_DST:
508 case MFF_TUN_TOS:
509 case MFF_TUN_TTL:
510 case MFF_TUN_GBP_ID:
511 case MFF_TUN_GBP_FLAGS:
512 CASE_MFF_TUN_METADATA:
513 case MFF_METADATA:
514 case MFF_IN_PORT:
515 case MFF_SKB_PRIORITY:
516 case MFF_PKT_MARK:
517 case MFF_CT_ZONE:
518 case MFF_CT_MARK:
519 case MFF_CT_LABEL:
520 case MFF_CT_NW_PROTO:
521 case MFF_CT_NW_SRC:
522 case MFF_CT_NW_DST:
523 case MFF_CT_IPV6_SRC:
524 case MFF_CT_IPV6_DST:
525 case MFF_CT_TP_SRC:
526 case MFF_CT_TP_DST:
527 CASE_MFF_REGS:
528 CASE_MFF_XREGS:
529 CASE_MFF_XXREGS:
530 case MFF_ETH_SRC:
531 case MFF_ETH_DST:
532 case MFF_ETH_TYPE:
533 case MFF_VLAN_TCI:
534 case MFF_MPLS_TTL:
535 case MFF_IPV4_SRC:
536 case MFF_IPV4_DST:
537 case MFF_IPV6_SRC:
538 case MFF_IPV6_DST:
539 case MFF_IP_PROTO:
540 case MFF_IP_TTL:
541 case MFF_ARP_SPA:
542 case MFF_ARP_TPA:
543 case MFF_ARP_SHA:
544 case MFF_ARP_THA:
545 case MFF_TCP_SRC:
546 case MFF_TCP_DST:
547 case MFF_UDP_SRC:
548 case MFF_UDP_DST:
549 case MFF_SCTP_SRC:
550 case MFF_SCTP_DST:
551 case MFF_ICMPV4_TYPE:
552 case MFF_ICMPV4_CODE:
553 case MFF_ICMPV6_TYPE:
554 case MFF_ICMPV6_CODE:
555 case MFF_ND_TARGET:
556 case MFF_ND_SLL:
557 case MFF_ND_TLL:
558 return true;
559
560 case MFF_IN_PORT_OXM:
561 case MFF_ACTSET_OUTPUT: {
562 ofp_port_t port;
563 return !ofputil_port_from_ofp11(value->be32, &port);
564 }
565
566 case MFF_IP_DSCP:
567 return !(value->u8 & ~IP_DSCP_MASK);
568 case MFF_IP_DSCP_SHIFTED:
569 return !(value->u8 & (~IP_DSCP_MASK >> 2));
570 case MFF_IP_ECN:
571 return !(value->u8 & ~IP_ECN_MASK);
572 case MFF_IP_FRAG:
573 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
574 case MFF_TCP_FLAGS:
575 return !(value->be16 & ~htons(0x0fff));
576
577 case MFF_ARP_OP:
578 return !(value->be16 & htons(0xff00));
579
580 case MFF_DL_VLAN:
581 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
582 case MFF_VLAN_VID:
583 return !(value->be16 & htons(VLAN_PCP_MASK));
584
585 case MFF_DL_VLAN_PCP:
586 case MFF_VLAN_PCP:
587 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
588
589 case MFF_IPV6_LABEL:
590 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
591
592 case MFF_MPLS_LABEL:
593 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
594
595 case MFF_MPLS_TC:
596 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
597
598 case MFF_MPLS_BOS:
599 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
600
601 case MFF_TUN_FLAGS:
602 return !(value->be16 & ~htons(FLOW_TNL_PUB_F_MASK));
603
604 case MFF_CT_STATE:
605 return !(value->be32 & ~htonl(CS_SUPPORTED_MASK));
606
607 case MFF_NSH_FLAGS:
608 return true;
609 case MFF_NSH_MDTYPE:
610 return (value->u8 == 1 || value->u8 == 2);
611 case MFF_NSH_NP:
612 return true;
613 case MFF_NSH_SPI:
614 return !(value->be32 & htonl(0xFF000000));
615 case MFF_NSH_SI:
616 case MFF_NSH_C1:
617 case MFF_NSH_C2:
618 case MFF_NSH_C3:
619 case MFF_NSH_C4:
620 return true;
621
622 case MFF_N_IDS:
623 default:
624 OVS_NOT_REACHED();
625 }
626 }
627
628 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
629 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
630 void
631 mf_get_value(const struct mf_field *mf, const struct flow *flow,
632 union mf_value *value)
633 {
634 switch (mf->id) {
635 case MFF_DP_HASH:
636 value->be32 = htonl(flow->dp_hash);
637 break;
638 case MFF_RECIRC_ID:
639 value->be32 = htonl(flow->recirc_id);
640 break;
641 case MFF_PACKET_TYPE:
642 value->be32 = flow->packet_type;
643 break;
644 case MFF_CONJ_ID:
645 value->be32 = htonl(flow->conj_id);
646 break;
647 case MFF_TUN_ID:
648 value->be64 = flow->tunnel.tun_id;
649 break;
650 case MFF_TUN_SRC:
651 value->be32 = flow->tunnel.ip_src;
652 break;
653 case MFF_TUN_DST:
654 value->be32 = flow->tunnel.ip_dst;
655 break;
656 case MFF_TUN_IPV6_SRC:
657 value->ipv6 = flow->tunnel.ipv6_src;
658 break;
659 case MFF_TUN_IPV6_DST:
660 value->ipv6 = flow->tunnel.ipv6_dst;
661 break;
662 case MFF_TUN_FLAGS:
663 value->be16 = htons(flow->tunnel.flags & FLOW_TNL_PUB_F_MASK);
664 break;
665 case MFF_TUN_GBP_ID:
666 value->be16 = flow->tunnel.gbp_id;
667 break;
668 case MFF_TUN_GBP_FLAGS:
669 value->u8 = flow->tunnel.gbp_flags;
670 break;
671 case MFF_TUN_TTL:
672 value->u8 = flow->tunnel.ip_ttl;
673 break;
674 case MFF_TUN_TOS:
675 value->u8 = flow->tunnel.ip_tos;
676 break;
677 CASE_MFF_TUN_METADATA:
678 tun_metadata_read(&flow->tunnel, mf, value);
679 break;
680
681 case MFF_METADATA:
682 value->be64 = flow->metadata;
683 break;
684
685 case MFF_IN_PORT:
686 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
687 break;
688 case MFF_IN_PORT_OXM:
689 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
690 break;
691 case MFF_ACTSET_OUTPUT:
692 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
693 break;
694
695 case MFF_SKB_PRIORITY:
696 value->be32 = htonl(flow->skb_priority);
697 break;
698
699 case MFF_PKT_MARK:
700 value->be32 = htonl(flow->pkt_mark);
701 break;
702
703 case MFF_CT_STATE:
704 value->be32 = htonl(flow->ct_state);
705 break;
706
707 case MFF_CT_ZONE:
708 value->be16 = htons(flow->ct_zone);
709 break;
710
711 case MFF_CT_MARK:
712 value->be32 = htonl(flow->ct_mark);
713 break;
714
715 case MFF_CT_LABEL:
716 value->be128 = hton128(flow->ct_label);
717 break;
718
719 case MFF_CT_NW_PROTO:
720 value->u8 = flow->ct_nw_proto;
721 break;
722
723 case MFF_CT_NW_SRC:
724 value->be32 = flow->ct_nw_src;
725 break;
726
727 case MFF_CT_NW_DST:
728 value->be32 = flow->ct_nw_dst;
729 break;
730
731 case MFF_CT_IPV6_SRC:
732 value->ipv6 = flow->ct_ipv6_src;
733 break;
734
735 case MFF_CT_IPV6_DST:
736 value->ipv6 = flow->ct_ipv6_dst;
737 break;
738
739 case MFF_CT_TP_SRC:
740 value->be16 = flow->ct_tp_src;
741 break;
742
743 case MFF_CT_TP_DST:
744 value->be16 = flow->ct_tp_dst;
745 break;
746
747 CASE_MFF_REGS:
748 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
749 break;
750
751 CASE_MFF_XREGS:
752 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
753 break;
754
755 CASE_MFF_XXREGS:
756 value->be128 = hton128(flow_get_xxreg(flow, mf->id - MFF_XXREG0));
757 break;
758
759 case MFF_ETH_SRC:
760 value->mac = flow->dl_src;
761 break;
762
763 case MFF_ETH_DST:
764 value->mac = flow->dl_dst;
765 break;
766
767 case MFF_ETH_TYPE:
768 value->be16 = flow->dl_type;
769 break;
770
771 case MFF_VLAN_TCI:
772 value->be16 = flow->vlans[0].tci;
773 break;
774
775 case MFF_DL_VLAN:
776 value->be16 = flow->vlans[0].tci & htons(VLAN_VID_MASK);
777 break;
778 case MFF_VLAN_VID:
779 value->be16 = flow->vlans[0].tci & htons(VLAN_VID_MASK | VLAN_CFI);
780 break;
781
782 case MFF_DL_VLAN_PCP:
783 case MFF_VLAN_PCP:
784 value->u8 = vlan_tci_to_pcp(flow->vlans[0].tci);
785 break;
786
787 case MFF_MPLS_LABEL:
788 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
789 break;
790
791 case MFF_MPLS_TC:
792 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
793 break;
794
795 case MFF_MPLS_BOS:
796 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
797 break;
798
799 case MFF_MPLS_TTL:
800 value->u8 = mpls_lse_to_ttl(flow->mpls_lse[0]);
801 break;
802
803 case MFF_IPV4_SRC:
804 value->be32 = flow->nw_src;
805 break;
806
807 case MFF_IPV4_DST:
808 value->be32 = flow->nw_dst;
809 break;
810
811 case MFF_IPV6_SRC:
812 value->ipv6 = flow->ipv6_src;
813 break;
814
815 case MFF_IPV6_DST:
816 value->ipv6 = flow->ipv6_dst;
817 break;
818
819 case MFF_IPV6_LABEL:
820 value->be32 = flow->ipv6_label;
821 break;
822
823 case MFF_IP_PROTO:
824 value->u8 = flow->nw_proto;
825 break;
826
827 case MFF_IP_DSCP:
828 value->u8 = flow->nw_tos & IP_DSCP_MASK;
829 break;
830
831 case MFF_IP_DSCP_SHIFTED:
832 value->u8 = flow->nw_tos >> 2;
833 break;
834
835 case MFF_IP_ECN:
836 value->u8 = flow->nw_tos & IP_ECN_MASK;
837 break;
838
839 case MFF_IP_TTL:
840 value->u8 = flow->nw_ttl;
841 break;
842
843 case MFF_IP_FRAG:
844 value->u8 = flow->nw_frag;
845 break;
846
847 case MFF_ARP_OP:
848 value->be16 = htons(flow->nw_proto);
849 break;
850
851 case MFF_ARP_SPA:
852 value->be32 = flow->nw_src;
853 break;
854
855 case MFF_ARP_TPA:
856 value->be32 = flow->nw_dst;
857 break;
858
859 case MFF_ARP_SHA:
860 case MFF_ND_SLL:
861 value->mac = flow->arp_sha;
862 break;
863
864 case MFF_ARP_THA:
865 case MFF_ND_TLL:
866 value->mac = flow->arp_tha;
867 break;
868
869 case MFF_TCP_SRC:
870 case MFF_UDP_SRC:
871 case MFF_SCTP_SRC:
872 value->be16 = flow->tp_src;
873 break;
874
875 case MFF_TCP_DST:
876 case MFF_UDP_DST:
877 case MFF_SCTP_DST:
878 value->be16 = flow->tp_dst;
879 break;
880
881 case MFF_TCP_FLAGS:
882 value->be16 = flow->tcp_flags;
883 break;
884
885 case MFF_ICMPV4_TYPE:
886 case MFF_ICMPV6_TYPE:
887 value->u8 = ntohs(flow->tp_src);
888 break;
889
890 case MFF_ICMPV4_CODE:
891 case MFF_ICMPV6_CODE:
892 value->u8 = ntohs(flow->tp_dst);
893 break;
894
895 case MFF_ND_TARGET:
896 value->ipv6 = flow->nd_target;
897 break;
898
899 case MFF_NSH_FLAGS:
900 value->u8 = flow->nsh.flags;
901 break;
902 case MFF_NSH_MDTYPE:
903 value->u8 = flow->nsh.mdtype;
904 break;
905 case MFF_NSH_NP:
906 value->u8 = flow->nsh.np;
907 break;
908 case MFF_NSH_SPI:
909 value->be32 = flow->nsh.spi;
910 break;
911 case MFF_NSH_SI:
912 value->u8 = flow->nsh.si;
913 break;
914 case MFF_NSH_C1:
915 case MFF_NSH_C2:
916 case MFF_NSH_C3:
917 case MFF_NSH_C4:
918 value->be32 = flow->nsh.c[mf->id - MFF_NSH_C1];
919 break;
920
921 case MFF_N_IDS:
922 default:
923 OVS_NOT_REACHED();
924 }
925 }
926
927 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
928 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
929 * prerequisites.
930 *
931 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
932 * with the request or NULL if there is no error. The caller is reponsible
933 * for freeing the string. */
934 void
935 mf_set_value(const struct mf_field *mf,
936 const union mf_value *value, struct match *match, char **err_str)
937 {
938 if (err_str) {
939 *err_str = NULL;
940 }
941
942 switch (mf->id) {
943 case MFF_DP_HASH:
944 match_set_dp_hash(match, ntohl(value->be32));
945 break;
946 case MFF_RECIRC_ID:
947 match_set_recirc_id(match, ntohl(value->be32));
948 break;
949 case MFF_PACKET_TYPE:
950 match_set_packet_type(match, value->be32);
951 break;
952 case MFF_CONJ_ID:
953 match_set_conj_id(match, ntohl(value->be32));
954 break;
955 case MFF_TUN_ID:
956 match_set_tun_id(match, value->be64);
957 break;
958 case MFF_TUN_SRC:
959 match_set_tun_src(match, value->be32);
960 break;
961 case MFF_TUN_DST:
962 match_set_tun_dst(match, value->be32);
963 break;
964 case MFF_TUN_IPV6_SRC:
965 match_set_tun_ipv6_src(match, &value->ipv6);
966 break;
967 case MFF_TUN_IPV6_DST:
968 match_set_tun_ipv6_dst(match, &value->ipv6);
969 break;
970 case MFF_TUN_FLAGS:
971 match_set_tun_flags(match, ntohs(value->be16));
972 break;
973 case MFF_TUN_GBP_ID:
974 match_set_tun_gbp_id(match, value->be16);
975 break;
976 case MFF_TUN_GBP_FLAGS:
977 match_set_tun_gbp_flags(match, value->u8);
978 break;
979 case MFF_TUN_TOS:
980 match_set_tun_tos(match, value->u8);
981 break;
982 case MFF_TUN_TTL:
983 match_set_tun_ttl(match, value->u8);
984 break;
985 CASE_MFF_TUN_METADATA:
986 tun_metadata_set_match(mf, value, NULL, match, err_str);
987 break;
988
989 case MFF_METADATA:
990 match_set_metadata(match, value->be64);
991 break;
992
993 case MFF_IN_PORT:
994 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
995 break;
996
997 case MFF_IN_PORT_OXM: {
998 ofp_port_t port;
999 ofputil_port_from_ofp11(value->be32, &port);
1000 match_set_in_port(match, port);
1001 break;
1002 }
1003 case MFF_ACTSET_OUTPUT: {
1004 ofp_port_t port;
1005 ofputil_port_from_ofp11(value->be32, &port);
1006 match_set_actset_output(match, port);
1007 break;
1008 }
1009
1010 case MFF_SKB_PRIORITY:
1011 match_set_skb_priority(match, ntohl(value->be32));
1012 break;
1013
1014 case MFF_PKT_MARK:
1015 match_set_pkt_mark(match, ntohl(value->be32));
1016 break;
1017
1018 case MFF_CT_STATE:
1019 match_set_ct_state(match, ntohl(value->be32));
1020 break;
1021
1022 case MFF_CT_ZONE:
1023 match_set_ct_zone(match, ntohs(value->be16));
1024 break;
1025
1026 case MFF_CT_MARK:
1027 match_set_ct_mark(match, ntohl(value->be32));
1028 break;
1029
1030 case MFF_CT_LABEL:
1031 match_set_ct_label(match, ntoh128(value->be128));
1032 break;
1033
1034 case MFF_CT_NW_PROTO:
1035 match_set_ct_nw_proto(match, value->u8);
1036 break;
1037
1038 case MFF_CT_NW_SRC:
1039 match_set_ct_nw_src(match, value->be32);
1040 break;
1041
1042 case MFF_CT_NW_DST:
1043 match_set_ct_nw_dst(match, value->be32);
1044 break;
1045
1046 case MFF_CT_IPV6_SRC:
1047 match_set_ct_ipv6_src(match, &value->ipv6);
1048 break;
1049
1050 case MFF_CT_IPV6_DST:
1051 match_set_ct_ipv6_dst(match, &value->ipv6);
1052 break;
1053
1054 case MFF_CT_TP_SRC:
1055 match_set_ct_tp_src(match, value->be16);
1056 break;
1057
1058 case MFF_CT_TP_DST:
1059 match_set_ct_tp_dst(match, value->be16);
1060 break;
1061
1062 CASE_MFF_REGS:
1063 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
1064 break;
1065
1066 CASE_MFF_XREGS:
1067 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
1068 break;
1069
1070 CASE_MFF_XXREGS:
1071 match_set_xxreg(match, mf->id - MFF_XXREG0, ntoh128(value->be128));
1072 break;
1073
1074 case MFF_ETH_SRC:
1075 match_set_dl_src(match, value->mac);
1076 break;
1077
1078 case MFF_ETH_DST:
1079 match_set_dl_dst(match, value->mac);
1080 break;
1081
1082 case MFF_ETH_TYPE:
1083 match_set_dl_type(match, value->be16);
1084 break;
1085
1086 case MFF_VLAN_TCI:
1087 match_set_dl_tci(match, value->be16);
1088 break;
1089
1090 case MFF_DL_VLAN:
1091 match_set_dl_vlan(match, value->be16);
1092 break;
1093 case MFF_VLAN_VID:
1094 match_set_vlan_vid(match, value->be16);
1095 break;
1096
1097 case MFF_DL_VLAN_PCP:
1098 case MFF_VLAN_PCP:
1099 match_set_dl_vlan_pcp(match, value->u8);
1100 break;
1101
1102 case MFF_MPLS_LABEL:
1103 match_set_mpls_label(match, 0, value->be32);
1104 break;
1105
1106 case MFF_MPLS_TC:
1107 match_set_mpls_tc(match, 0, value->u8);
1108 break;
1109
1110 case MFF_MPLS_BOS:
1111 match_set_mpls_bos(match, 0, value->u8);
1112 break;
1113
1114 case MFF_MPLS_TTL:
1115 match_set_mpls_ttl(match, 0, value->u8);
1116 break;
1117
1118 case MFF_IPV4_SRC:
1119 match_set_nw_src(match, value->be32);
1120 break;
1121
1122 case MFF_IPV4_DST:
1123 match_set_nw_dst(match, value->be32);
1124 break;
1125
1126 case MFF_IPV6_SRC:
1127 match_set_ipv6_src(match, &value->ipv6);
1128 break;
1129
1130 case MFF_IPV6_DST:
1131 match_set_ipv6_dst(match, &value->ipv6);
1132 break;
1133
1134 case MFF_IPV6_LABEL:
1135 match_set_ipv6_label(match, value->be32);
1136 break;
1137
1138 case MFF_IP_PROTO:
1139 match_set_nw_proto(match, value->u8);
1140 break;
1141
1142 case MFF_IP_DSCP:
1143 match_set_nw_dscp(match, value->u8);
1144 break;
1145
1146 case MFF_IP_DSCP_SHIFTED:
1147 match_set_nw_dscp(match, value->u8 << 2);
1148 break;
1149
1150 case MFF_IP_ECN:
1151 match_set_nw_ecn(match, value->u8);
1152 break;
1153
1154 case MFF_IP_TTL:
1155 match_set_nw_ttl(match, value->u8);
1156 break;
1157
1158 case MFF_IP_FRAG:
1159 match_set_nw_frag(match, value->u8);
1160 break;
1161
1162 case MFF_ARP_OP:
1163 match_set_nw_proto(match, ntohs(value->be16));
1164 break;
1165
1166 case MFF_ARP_SPA:
1167 match_set_nw_src(match, value->be32);
1168 break;
1169
1170 case MFF_ARP_TPA:
1171 match_set_nw_dst(match, value->be32);
1172 break;
1173
1174 case MFF_ARP_SHA:
1175 case MFF_ND_SLL:
1176 match_set_arp_sha(match, value->mac);
1177 break;
1178
1179 case MFF_ARP_THA:
1180 case MFF_ND_TLL:
1181 match_set_arp_tha(match, value->mac);
1182 break;
1183
1184 case MFF_TCP_SRC:
1185 case MFF_UDP_SRC:
1186 case MFF_SCTP_SRC:
1187 match_set_tp_src(match, value->be16);
1188 break;
1189
1190 case MFF_TCP_DST:
1191 case MFF_UDP_DST:
1192 case MFF_SCTP_DST:
1193 match_set_tp_dst(match, value->be16);
1194 break;
1195
1196 case MFF_TCP_FLAGS:
1197 match_set_tcp_flags(match, value->be16);
1198 break;
1199
1200 case MFF_ICMPV4_TYPE:
1201 case MFF_ICMPV6_TYPE:
1202 match_set_icmp_type(match, value->u8);
1203 break;
1204
1205 case MFF_ICMPV4_CODE:
1206 case MFF_ICMPV6_CODE:
1207 match_set_icmp_code(match, value->u8);
1208 break;
1209
1210 case MFF_ND_TARGET:
1211 match_set_nd_target(match, &value->ipv6);
1212 break;
1213
1214 case MFF_NSH_FLAGS:
1215 MATCH_SET_FIELD_UINT8(match, nsh.flags, value->u8);
1216 break;
1217 case MFF_NSH_MDTYPE:
1218 MATCH_SET_FIELD_UINT8(match, nsh.mdtype, value->u8);
1219 break;
1220 case MFF_NSH_NP:
1221 MATCH_SET_FIELD_UINT8(match, nsh.np, value->u8);
1222 break;
1223 case MFF_NSH_SPI:
1224 MATCH_SET_FIELD_BE32(match, nsh.spi, value->be32);
1225 break;
1226 case MFF_NSH_SI:
1227 MATCH_SET_FIELD_UINT8(match, nsh.si, value->u8);
1228 break;
1229 case MFF_NSH_C1:
1230 case MFF_NSH_C2:
1231 case MFF_NSH_C3:
1232 case MFF_NSH_C4:
1233 MATCH_SET_FIELD_BE32(match, nsh.c[mf->id - MFF_NSH_C1], value->be32);
1234 break;
1235
1236 case MFF_N_IDS:
1237 default:
1238 OVS_NOT_REACHED();
1239 }
1240 }
1241
1242 /* Unwildcard the bits in 'mask' of the 'wc' member field described by 'mf'.
1243 * The caller is responsible for ensuring that 'wc' meets 'mf''s
1244 * prerequisites. */
1245 void
1246 mf_mask_field_masked(const struct mf_field *mf, const union mf_value *mask,
1247 struct flow_wildcards *wc)
1248 {
1249 union mf_value temp_mask;
1250 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan() as that
1251 * will be considered as OFP10_VLAN_NONE. So make sure the mask only has
1252 * valid bits in this case. */
1253 if (mf->id == MFF_DL_VLAN) {
1254 temp_mask.be16 = htons(VLAN_VID_MASK) & mask->be16;
1255 mask = &temp_mask;
1256 }
1257
1258 union mf_value mask_value;
1259
1260 mf_get_value(mf, &wc->masks, &mask_value);
1261 for (size_t i = 0; i < mf->n_bytes; i++) {
1262 mask_value.b[i] |= mask->b[i];
1263 }
1264 mf_set_flow_value(mf, &mask_value, &wc->masks);
1265 }
1266
1267 /* Unwildcard 'wc' member field described by 'mf'. The caller is
1268 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1269 void
1270 mf_mask_field(const struct mf_field *mf, struct flow_wildcards *wc)
1271 {
1272 mf_mask_field_masked(mf, &exact_match_mask, wc);
1273 }
1274
1275 static int
1276 field_len(const struct mf_field *mf, const union mf_value *value_)
1277 {
1278 const uint8_t *value = &value_->u8;
1279 int i;
1280
1281 if (!mf->variable_len) {
1282 return mf->n_bytes;
1283 }
1284
1285 if (!value) {
1286 return 0;
1287 }
1288
1289 for (i = 0; i < mf->n_bytes; i++) {
1290 if (value[i] != 0) {
1291 break;
1292 }
1293 }
1294
1295 return mf->n_bytes - i;
1296 }
1297
1298 /* Returns the effective length of the field. For fixed length fields,
1299 * this is just the defined length. For variable length fields, it is
1300 * the minimum size encoding that retains the same meaning (i.e.
1301 * discarding leading zeros).
1302 *
1303 * 'is_masked' returns (if non-NULL) whether the original contained
1304 * a mask. Otherwise, a mask that is the same length as the value
1305 * might be misinterpreted as an exact match. */
1306 int
1307 mf_field_len(const struct mf_field *mf, const union mf_value *value,
1308 const union mf_value *mask, bool *is_masked_)
1309 {
1310 int len, mask_len;
1311 bool is_masked = mask && !is_all_ones(mask, mf->n_bytes);
1312
1313 len = field_len(mf, value);
1314 if (is_masked) {
1315 mask_len = field_len(mf, mask);
1316 len = MAX(len, mask_len);
1317 }
1318
1319 if (is_masked_) {
1320 *is_masked_ = is_masked;
1321 }
1322
1323 return len;
1324 }
1325
1326 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1327 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1328 void
1329 mf_set_flow_value(const struct mf_field *mf,
1330 const union mf_value *value, struct flow *flow)
1331 {
1332 switch (mf->id) {
1333 case MFF_DP_HASH:
1334 flow->dp_hash = ntohl(value->be32);
1335 break;
1336 case MFF_RECIRC_ID:
1337 flow->recirc_id = ntohl(value->be32);
1338 break;
1339 case MFF_PACKET_TYPE:
1340 flow->packet_type = value->be32;
1341 break;
1342 case MFF_CONJ_ID:
1343 flow->conj_id = ntohl(value->be32);
1344 break;
1345 case MFF_TUN_ID:
1346 flow->tunnel.tun_id = value->be64;
1347 break;
1348 case MFF_TUN_SRC:
1349 flow->tunnel.ip_src = value->be32;
1350 break;
1351 case MFF_TUN_DST:
1352 flow->tunnel.ip_dst = value->be32;
1353 break;
1354 case MFF_TUN_IPV6_SRC:
1355 flow->tunnel.ipv6_src = value->ipv6;
1356 break;
1357 case MFF_TUN_IPV6_DST:
1358 flow->tunnel.ipv6_dst = value->ipv6;
1359 break;
1360 case MFF_TUN_FLAGS:
1361 flow->tunnel.flags = (flow->tunnel.flags & ~FLOW_TNL_PUB_F_MASK) |
1362 ntohs(value->be16);
1363 break;
1364 case MFF_TUN_GBP_ID:
1365 flow->tunnel.gbp_id = value->be16;
1366 break;
1367 case MFF_TUN_GBP_FLAGS:
1368 flow->tunnel.gbp_flags = value->u8;
1369 break;
1370 case MFF_TUN_TOS:
1371 flow->tunnel.ip_tos = value->u8;
1372 break;
1373 case MFF_TUN_TTL:
1374 flow->tunnel.ip_ttl = value->u8;
1375 break;
1376 CASE_MFF_TUN_METADATA:
1377 tun_metadata_write(&flow->tunnel, mf, value);
1378 break;
1379 case MFF_METADATA:
1380 flow->metadata = value->be64;
1381 break;
1382
1383 case MFF_IN_PORT:
1384 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1385 break;
1386 case MFF_IN_PORT_OXM:
1387 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
1388 break;
1389 case MFF_ACTSET_OUTPUT:
1390 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
1391 break;
1392
1393 case MFF_SKB_PRIORITY:
1394 flow->skb_priority = ntohl(value->be32);
1395 break;
1396
1397 case MFF_PKT_MARK:
1398 flow->pkt_mark = ntohl(value->be32);
1399 break;
1400
1401 case MFF_CT_STATE:
1402 flow->ct_state = ntohl(value->be32);
1403 break;
1404
1405 case MFF_CT_ZONE:
1406 flow->ct_zone = ntohs(value->be16);
1407 break;
1408
1409 case MFF_CT_MARK:
1410 flow->ct_mark = ntohl(value->be32);
1411 break;
1412
1413 case MFF_CT_LABEL:
1414 flow->ct_label = ntoh128(value->be128);
1415 break;
1416
1417 case MFF_CT_NW_PROTO:
1418 flow->ct_nw_proto = value->u8;
1419 break;
1420
1421 case MFF_CT_NW_SRC:
1422 flow->ct_nw_src = value->be32;
1423 break;
1424
1425 case MFF_CT_NW_DST:
1426 flow->ct_nw_dst = value->be32;
1427 break;
1428
1429 case MFF_CT_IPV6_SRC:
1430 flow->ct_ipv6_src = value->ipv6;
1431 break;
1432
1433 case MFF_CT_IPV6_DST:
1434 flow->ct_ipv6_dst = value->ipv6;
1435 break;
1436
1437 case MFF_CT_TP_SRC:
1438 flow->ct_tp_src = value->be16;
1439 break;
1440
1441 case MFF_CT_TP_DST:
1442 flow->ct_tp_dst = value->be16;
1443 break;
1444
1445 CASE_MFF_REGS:
1446 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1447 break;
1448
1449 CASE_MFF_XREGS:
1450 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1451 break;
1452
1453 CASE_MFF_XXREGS:
1454 flow_set_xxreg(flow, mf->id - MFF_XXREG0, ntoh128(value->be128));
1455 break;
1456
1457 case MFF_ETH_SRC:
1458 flow->dl_src = value->mac;
1459 break;
1460
1461 case MFF_ETH_DST:
1462 flow->dl_dst = value->mac;
1463 break;
1464
1465 case MFF_ETH_TYPE:
1466 flow->dl_type = value->be16;
1467 break;
1468
1469 case MFF_VLAN_TCI:
1470 flow->vlans[0].tci = value->be16;
1471 flow_fix_vlan_tpid(flow);
1472 break;
1473
1474 case MFF_DL_VLAN:
1475 flow_set_dl_vlan(flow, value->be16);
1476 flow_fix_vlan_tpid(flow);
1477 break;
1478
1479 case MFF_VLAN_VID:
1480 flow_set_vlan_vid(flow, value->be16);
1481 flow_fix_vlan_tpid(flow);
1482 break;
1483
1484 case MFF_DL_VLAN_PCP:
1485 case MFF_VLAN_PCP:
1486 flow_set_vlan_pcp(flow, value->u8);
1487 flow_fix_vlan_tpid(flow);
1488 break;
1489
1490 case MFF_MPLS_LABEL:
1491 flow_set_mpls_label(flow, 0, value->be32);
1492 break;
1493
1494 case MFF_MPLS_TC:
1495 flow_set_mpls_tc(flow, 0, value->u8);
1496 break;
1497
1498 case MFF_MPLS_BOS:
1499 flow_set_mpls_bos(flow, 0, value->u8);
1500 break;
1501
1502 case MFF_MPLS_TTL:
1503 flow_set_mpls_ttl(flow, 0, value->u8);
1504 break;
1505
1506 case MFF_IPV4_SRC:
1507 flow->nw_src = value->be32;
1508 break;
1509
1510 case MFF_IPV4_DST:
1511 flow->nw_dst = value->be32;
1512 break;
1513
1514 case MFF_IPV6_SRC:
1515 flow->ipv6_src = value->ipv6;
1516 break;
1517
1518 case MFF_IPV6_DST:
1519 flow->ipv6_dst = value->ipv6;
1520 break;
1521
1522 case MFF_IPV6_LABEL:
1523 flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
1524 break;
1525
1526 case MFF_IP_PROTO:
1527 flow->nw_proto = value->u8;
1528 break;
1529
1530 case MFF_IP_DSCP:
1531 flow->nw_tos &= ~IP_DSCP_MASK;
1532 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1533 break;
1534
1535 case MFF_IP_DSCP_SHIFTED:
1536 flow->nw_tos &= ~IP_DSCP_MASK;
1537 flow->nw_tos |= value->u8 << 2;
1538 break;
1539
1540 case MFF_IP_ECN:
1541 flow->nw_tos &= ~IP_ECN_MASK;
1542 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1543 break;
1544
1545 case MFF_IP_TTL:
1546 flow->nw_ttl = value->u8;
1547 break;
1548
1549 case MFF_IP_FRAG:
1550 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1551 break;
1552
1553 case MFF_ARP_OP:
1554 flow->nw_proto = ntohs(value->be16);
1555 break;
1556
1557 case MFF_ARP_SPA:
1558 flow->nw_src = value->be32;
1559 break;
1560
1561 case MFF_ARP_TPA:
1562 flow->nw_dst = value->be32;
1563 break;
1564
1565 case MFF_ARP_SHA:
1566 case MFF_ND_SLL:
1567 flow->arp_sha = value->mac;
1568 break;
1569
1570 case MFF_ARP_THA:
1571 case MFF_ND_TLL:
1572 flow->arp_tha = value->mac;
1573 break;
1574
1575 case MFF_TCP_SRC:
1576 case MFF_UDP_SRC:
1577 case MFF_SCTP_SRC:
1578 flow->tp_src = value->be16;
1579 break;
1580
1581 case MFF_TCP_DST:
1582 case MFF_UDP_DST:
1583 case MFF_SCTP_DST:
1584 flow->tp_dst = value->be16;
1585 break;
1586
1587 case MFF_TCP_FLAGS:
1588 flow->tcp_flags = value->be16;
1589 break;
1590
1591 case MFF_ICMPV4_TYPE:
1592 case MFF_ICMPV6_TYPE:
1593 flow->tp_src = htons(value->u8);
1594 break;
1595
1596 case MFF_ICMPV4_CODE:
1597 case MFF_ICMPV6_CODE:
1598 flow->tp_dst = htons(value->u8);
1599 break;
1600
1601 case MFF_ND_TARGET:
1602 flow->nd_target = value->ipv6;
1603 break;
1604
1605 case MFF_NSH_FLAGS:
1606 flow->nsh.flags = value->u8;
1607 break;
1608 case MFF_NSH_MDTYPE:
1609 flow->nsh.mdtype = value->u8;
1610 break;
1611 case MFF_NSH_NP:
1612 flow->nsh.np = value->u8;
1613 break;
1614 case MFF_NSH_SPI:
1615 flow->nsh.spi = value->be32;
1616 break;
1617 case MFF_NSH_SI:
1618 flow->nsh.si = value->u8;
1619 break;
1620 case MFF_NSH_C1:
1621 case MFF_NSH_C2:
1622 case MFF_NSH_C3:
1623 case MFF_NSH_C4:
1624 flow->nsh.c[mf->id - MFF_NSH_C1] = value->be32;
1625 break;
1626
1627 case MFF_N_IDS:
1628 default:
1629 OVS_NOT_REACHED();
1630 }
1631 }
1632
1633 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1634 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1635 * src[i]. */
1636 static void
1637 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1638 {
1639 size_t i;
1640
1641 for (i = 0; i < n; i++) {
1642 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1643 }
1644 }
1645
1646 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1647 * for which 'mask' has a 0-bit keep their existing values. The caller is
1648 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1649 void
1650 mf_set_flow_value_masked(const struct mf_field *field,
1651 const union mf_value *value,
1652 const union mf_value *mask,
1653 struct flow *flow)
1654 {
1655 union mf_value tmp;
1656
1657 mf_get_value(field, flow, &tmp);
1658 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1659 (uint8_t *) &tmp, field->n_bytes);
1660 mf_set_flow_value(field, &tmp, flow);
1661 }
1662
1663 bool
1664 mf_is_tun_metadata(const struct mf_field *mf)
1665 {
1666 return mf->id >= MFF_TUN_METADATA0 &&
1667 mf->id < MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS;
1668 }
1669
1670 bool
1671 mf_is_pipeline_field(const struct mf_field *mf)
1672 {
1673 switch (mf->id) {
1674 case MFF_TUN_ID:
1675 case MFF_TUN_SRC:
1676 case MFF_TUN_DST:
1677 case MFF_TUN_IPV6_SRC:
1678 case MFF_TUN_IPV6_DST:
1679 case MFF_TUN_FLAGS:
1680 case MFF_TUN_GBP_ID:
1681 case MFF_TUN_GBP_FLAGS:
1682 CASE_MFF_TUN_METADATA:
1683 case MFF_METADATA:
1684 case MFF_IN_PORT:
1685 case MFF_IN_PORT_OXM:
1686 CASE_MFF_REGS:
1687 CASE_MFF_XREGS:
1688 CASE_MFF_XXREGS:
1689 case MFF_PACKET_TYPE:
1690 return true;
1691
1692 case MFF_DP_HASH:
1693 case MFF_RECIRC_ID:
1694 case MFF_CONJ_ID:
1695 case MFF_TUN_TTL:
1696 case MFF_TUN_TOS:
1697 case MFF_ACTSET_OUTPUT:
1698 case MFF_SKB_PRIORITY:
1699 case MFF_PKT_MARK:
1700 case MFF_CT_STATE:
1701 case MFF_CT_ZONE:
1702 case MFF_CT_MARK:
1703 case MFF_CT_LABEL:
1704 case MFF_CT_NW_PROTO:
1705 case MFF_CT_NW_SRC:
1706 case MFF_CT_NW_DST:
1707 case MFF_CT_IPV6_SRC:
1708 case MFF_CT_IPV6_DST:
1709 case MFF_CT_TP_SRC:
1710 case MFF_CT_TP_DST:
1711 case MFF_ETH_SRC:
1712 case MFF_ETH_DST:
1713 case MFF_ETH_TYPE:
1714 case MFF_VLAN_TCI:
1715 case MFF_DL_VLAN:
1716 case MFF_VLAN_VID:
1717 case MFF_DL_VLAN_PCP:
1718 case MFF_VLAN_PCP:
1719 case MFF_MPLS_LABEL:
1720 case MFF_MPLS_TC:
1721 case MFF_MPLS_BOS:
1722 case MFF_MPLS_TTL:
1723 case MFF_IPV4_SRC:
1724 case MFF_IPV4_DST:
1725 case MFF_IPV6_SRC:
1726 case MFF_IPV6_DST:
1727 case MFF_IPV6_LABEL:
1728 case MFF_IP_PROTO:
1729 case MFF_IP_DSCP:
1730 case MFF_IP_DSCP_SHIFTED:
1731 case MFF_IP_ECN:
1732 case MFF_IP_TTL:
1733 case MFF_IP_FRAG:
1734 case MFF_ARP_OP:
1735 case MFF_ARP_SPA:
1736 case MFF_ARP_TPA:
1737 case MFF_ARP_SHA:
1738 case MFF_ARP_THA:
1739 case MFF_TCP_SRC:
1740 case MFF_TCP_DST:
1741 case MFF_TCP_FLAGS:
1742 case MFF_UDP_SRC:
1743 case MFF_UDP_DST:
1744 case MFF_SCTP_SRC:
1745 case MFF_SCTP_DST:
1746 case MFF_ICMPV4_TYPE:
1747 case MFF_ICMPV4_CODE:
1748 case MFF_ICMPV6_TYPE:
1749 case MFF_ICMPV6_CODE:
1750 case MFF_ND_TARGET:
1751 case MFF_ND_SLL:
1752 case MFF_ND_TLL:
1753 case MFF_NSH_FLAGS:
1754 case MFF_NSH_MDTYPE:
1755 case MFF_NSH_NP:
1756 case MFF_NSH_SPI:
1757 case MFF_NSH_SI:
1758 case MFF_NSH_C1:
1759 case MFF_NSH_C2:
1760 case MFF_NSH_C3:
1761 case MFF_NSH_C4:
1762 return false;
1763
1764 case MFF_N_IDS:
1765 default:
1766 OVS_NOT_REACHED();
1767 }
1768 }
1769
1770 /* Returns true if 'mf' has previously been set in 'flow', false if
1771 * it contains a non-default value.
1772 *
1773 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1774 * prerequisites. */
1775 bool
1776 mf_is_set(const struct mf_field *mf, const struct flow *flow)
1777 {
1778 if (!mf_is_tun_metadata(mf)) {
1779 union mf_value value;
1780
1781 mf_get_value(mf, flow, &value);
1782 return !is_all_zeros(&value, mf->n_bytes);
1783 } else {
1784 return ULLONG_GET(flow->tunnel.metadata.present.map,
1785 mf->id - MFF_TUN_METADATA0);
1786 }
1787 }
1788
1789 /* Makes 'match' wildcard field 'mf'.
1790 *
1791 * The caller is responsible for ensuring that 'match' meets 'mf''s
1792 * prerequisites.
1793 *
1794 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1795 * with the request or NULL if there is no error. The caller is reponsible
1796 * for freeing the string. */
1797 void
1798 mf_set_wild(const struct mf_field *mf, struct match *match, char **err_str)
1799 {
1800 if (err_str) {
1801 *err_str = NULL;
1802 }
1803
1804 switch (mf->id) {
1805 case MFF_DP_HASH:
1806 match->flow.dp_hash = 0;
1807 match->wc.masks.dp_hash = 0;
1808 break;
1809 case MFF_RECIRC_ID:
1810 match->flow.recirc_id = 0;
1811 match->wc.masks.recirc_id = 0;
1812 break;
1813 case MFF_PACKET_TYPE:
1814 match->flow.packet_type = 0;
1815 match->wc.masks.packet_type = 0;
1816 break;
1817 case MFF_CONJ_ID:
1818 match->flow.conj_id = 0;
1819 match->wc.masks.conj_id = 0;
1820 break;
1821 case MFF_TUN_ID:
1822 match_set_tun_id_masked(match, htonll(0), htonll(0));
1823 break;
1824 case MFF_TUN_SRC:
1825 match_set_tun_src_masked(match, htonl(0), htonl(0));
1826 break;
1827 case MFF_TUN_DST:
1828 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1829 break;
1830 case MFF_TUN_IPV6_SRC:
1831 memset(&match->wc.masks.tunnel.ipv6_src, 0,
1832 sizeof match->wc.masks.tunnel.ipv6_src);
1833 memset(&match->flow.tunnel.ipv6_src, 0,
1834 sizeof match->flow.tunnel.ipv6_src);
1835 break;
1836 case MFF_TUN_IPV6_DST:
1837 memset(&match->wc.masks.tunnel.ipv6_dst, 0,
1838 sizeof match->wc.masks.tunnel.ipv6_dst);
1839 memset(&match->flow.tunnel.ipv6_dst, 0,
1840 sizeof match->flow.tunnel.ipv6_dst);
1841 break;
1842 case MFF_TUN_FLAGS:
1843 match_set_tun_flags_masked(match, 0, 0);
1844 break;
1845 case MFF_TUN_GBP_ID:
1846 match_set_tun_gbp_id_masked(match, 0, 0);
1847 break;
1848 case MFF_TUN_GBP_FLAGS:
1849 match_set_tun_gbp_flags_masked(match, 0, 0);
1850 break;
1851 case MFF_TUN_TOS:
1852 match_set_tun_tos_masked(match, 0, 0);
1853 break;
1854 case MFF_TUN_TTL:
1855 match_set_tun_ttl_masked(match, 0, 0);
1856 break;
1857 CASE_MFF_TUN_METADATA:
1858 tun_metadata_set_match(mf, NULL, NULL, match, err_str);
1859 break;
1860
1861 case MFF_METADATA:
1862 match_set_metadata_masked(match, htonll(0), htonll(0));
1863 break;
1864
1865 case MFF_IN_PORT:
1866 case MFF_IN_PORT_OXM:
1867 match->flow.in_port.ofp_port = 0;
1868 match->wc.masks.in_port.ofp_port = 0;
1869 break;
1870 case MFF_ACTSET_OUTPUT:
1871 match->flow.actset_output = 0;
1872 match->wc.masks.actset_output = 0;
1873 break;
1874
1875 case MFF_SKB_PRIORITY:
1876 match->flow.skb_priority = 0;
1877 match->wc.masks.skb_priority = 0;
1878 break;
1879
1880 case MFF_PKT_MARK:
1881 match->flow.pkt_mark = 0;
1882 match->wc.masks.pkt_mark = 0;
1883 break;
1884
1885 case MFF_CT_STATE:
1886 match->flow.ct_state = 0;
1887 match->wc.masks.ct_state = 0;
1888 break;
1889
1890 case MFF_CT_ZONE:
1891 match->flow.ct_zone = 0;
1892 match->wc.masks.ct_zone = 0;
1893 break;
1894
1895 case MFF_CT_MARK:
1896 match->flow.ct_mark = 0;
1897 match->wc.masks.ct_mark = 0;
1898 break;
1899
1900 case MFF_CT_LABEL:
1901 memset(&match->flow.ct_label, 0, sizeof(match->flow.ct_label));
1902 memset(&match->wc.masks.ct_label, 0, sizeof(match->wc.masks.ct_label));
1903 break;
1904
1905 case MFF_CT_NW_PROTO:
1906 match->flow.ct_nw_proto = 0;
1907 match->wc.masks.ct_nw_proto = 0;
1908 break;
1909
1910 case MFF_CT_NW_SRC:
1911 match->flow.ct_nw_src = 0;
1912 match->wc.masks.ct_nw_src = 0;
1913 break;
1914
1915 case MFF_CT_NW_DST:
1916 match->flow.ct_nw_dst = 0;
1917 match->wc.masks.ct_nw_dst = 0;
1918 break;
1919
1920 case MFF_CT_IPV6_SRC:
1921 memset(&match->flow.ct_ipv6_src, 0, sizeof(match->flow.ct_ipv6_src));
1922 WC_UNMASK_FIELD(&match->wc, ct_ipv6_src);
1923 break;
1924
1925 case MFF_CT_IPV6_DST:
1926 memset(&match->flow.ct_ipv6_dst, 0, sizeof(match->flow.ct_ipv6_dst));
1927 WC_UNMASK_FIELD(&match->wc, ct_ipv6_dst);
1928 break;
1929
1930 case MFF_CT_TP_SRC:
1931 match->flow.ct_tp_src = 0;
1932 match->wc.masks.ct_tp_src = 0;
1933 break;
1934
1935 case MFF_CT_TP_DST:
1936 match->flow.ct_tp_dst = 0;
1937 match->wc.masks.ct_tp_dst = 0;
1938 break;
1939
1940 CASE_MFF_REGS:
1941 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1942 break;
1943
1944 CASE_MFF_XREGS:
1945 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1946 break;
1947
1948 CASE_MFF_XXREGS: {
1949 match_set_xxreg_masked(match, mf->id - MFF_XXREG0, OVS_U128_ZERO,
1950 OVS_U128_ZERO);
1951 break;
1952 }
1953
1954 case MFF_ETH_SRC:
1955 match->flow.dl_src = eth_addr_zero;
1956 match->wc.masks.dl_src = eth_addr_zero;
1957 break;
1958
1959 case MFF_ETH_DST:
1960 match->flow.dl_dst = eth_addr_zero;
1961 match->wc.masks.dl_dst = eth_addr_zero;
1962 break;
1963
1964 case MFF_ETH_TYPE:
1965 match->flow.dl_type = htons(0);
1966 match->wc.masks.dl_type = htons(0);
1967 break;
1968
1969 case MFF_VLAN_TCI:
1970 match_set_dl_tci_masked(match, htons(0), htons(0));
1971 break;
1972
1973 case MFF_DL_VLAN:
1974 case MFF_VLAN_VID:
1975 match_set_any_vid(match);
1976 break;
1977
1978 case MFF_DL_VLAN_PCP:
1979 case MFF_VLAN_PCP:
1980 match_set_any_pcp(match);
1981 break;
1982
1983 case MFF_MPLS_LABEL:
1984 match_set_any_mpls_label(match, 0);
1985 break;
1986
1987 case MFF_MPLS_TC:
1988 match_set_any_mpls_tc(match, 0);
1989 break;
1990
1991 case MFF_MPLS_BOS:
1992 match_set_any_mpls_bos(match, 0);
1993 break;
1994
1995 case MFF_MPLS_TTL:
1996 match_set_any_mpls_ttl(match, 0);
1997 break;
1998
1999 case MFF_IPV4_SRC:
2000 case MFF_ARP_SPA:
2001 match_set_nw_src_masked(match, htonl(0), htonl(0));
2002 break;
2003
2004 case MFF_IPV4_DST:
2005 case MFF_ARP_TPA:
2006 match_set_nw_dst_masked(match, htonl(0), htonl(0));
2007 break;
2008
2009 case MFF_IPV6_SRC:
2010 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
2011 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
2012 break;
2013
2014 case MFF_IPV6_DST:
2015 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
2016 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
2017 break;
2018
2019 case MFF_IPV6_LABEL:
2020 match->wc.masks.ipv6_label = htonl(0);
2021 match->flow.ipv6_label = htonl(0);
2022 break;
2023
2024 case MFF_IP_PROTO:
2025 match->wc.masks.nw_proto = 0;
2026 match->flow.nw_proto = 0;
2027 break;
2028
2029 case MFF_IP_DSCP:
2030 case MFF_IP_DSCP_SHIFTED:
2031 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
2032 match->flow.nw_tos &= ~IP_DSCP_MASK;
2033 break;
2034
2035 case MFF_IP_ECN:
2036 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
2037 match->flow.nw_tos &= ~IP_ECN_MASK;
2038 break;
2039
2040 case MFF_IP_TTL:
2041 match->wc.masks.nw_ttl = 0;
2042 match->flow.nw_ttl = 0;
2043 break;
2044
2045 case MFF_IP_FRAG:
2046 match->wc.masks.nw_frag &= ~FLOW_NW_FRAG_MASK;
2047 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
2048 break;
2049
2050 case MFF_ARP_OP:
2051 match->wc.masks.nw_proto = 0;
2052 match->flow.nw_proto = 0;
2053 break;
2054
2055 case MFF_ARP_SHA:
2056 case MFF_ND_SLL:
2057 match->flow.arp_sha = eth_addr_zero;
2058 match->wc.masks.arp_sha = eth_addr_zero;
2059 break;
2060
2061 case MFF_ARP_THA:
2062 case MFF_ND_TLL:
2063 match->flow.arp_tha = eth_addr_zero;
2064 match->wc.masks.arp_tha = eth_addr_zero;
2065 break;
2066
2067 case MFF_TCP_SRC:
2068 case MFF_UDP_SRC:
2069 case MFF_SCTP_SRC:
2070 case MFF_ICMPV4_TYPE:
2071 case MFF_ICMPV6_TYPE:
2072 match->wc.masks.tp_src = htons(0);
2073 match->flow.tp_src = htons(0);
2074 break;
2075
2076 case MFF_TCP_DST:
2077 case MFF_UDP_DST:
2078 case MFF_SCTP_DST:
2079 case MFF_ICMPV4_CODE:
2080 case MFF_ICMPV6_CODE:
2081 match->wc.masks.tp_dst = htons(0);
2082 match->flow.tp_dst = htons(0);
2083 break;
2084
2085 case MFF_TCP_FLAGS:
2086 match->wc.masks.tcp_flags = htons(0);
2087 match->flow.tcp_flags = htons(0);
2088 break;
2089
2090 case MFF_ND_TARGET:
2091 memset(&match->wc.masks.nd_target, 0,
2092 sizeof match->wc.masks.nd_target);
2093 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
2094 break;
2095
2096 case MFF_NSH_FLAGS:
2097 MATCH_SET_FIELD_MASKED(match, nsh.flags, 0, 0);
2098 break;
2099 case MFF_NSH_MDTYPE:
2100 MATCH_SET_FIELD_MASKED(match, nsh.mdtype, 0, 0);
2101 break;
2102 case MFF_NSH_NP:
2103 MATCH_SET_FIELD_MASKED(match, nsh.np, 0, 0);
2104 break;
2105 case MFF_NSH_SPI:
2106 MATCH_SET_FIELD_MASKED(match, nsh.spi, htonl(0), htonl(0));
2107 break;
2108 case MFF_NSH_SI:
2109 MATCH_SET_FIELD_MASKED(match, nsh.si, 0, 0);
2110 break;
2111 case MFF_NSH_C1:
2112 case MFF_NSH_C2:
2113 case MFF_NSH_C3:
2114 case MFF_NSH_C4:
2115 MATCH_SET_FIELD_MASKED(match, nsh.c[mf->id - MFF_NSH_C1],
2116 htonl(0), htonl(0));
2117 break;
2118
2119 case MFF_N_IDS:
2120 default:
2121 OVS_NOT_REACHED();
2122 }
2123 }
2124
2125 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
2126 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
2127 * with a 1-bit indicating that the corresponding value bit must match and a
2128 * 0-bit indicating a don't-care.
2129 *
2130 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
2131 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
2132 * call is equivalent to mf_set_wild(mf, match).
2133 *
2134 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
2135 * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
2136 *
2137 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
2138 * with the request or NULL if there is no error. The caller is reponsible
2139 * for freeing the string.
2140 *
2141 * Return a set of enum ofputil_protocol bits (as an uint32_t to avoid circular
2142 * dependency on enum ofputil_protocol definition) indicating which OpenFlow
2143 * protocol versions can support this functionality. */
2144 uint32_t
2145 mf_set(const struct mf_field *mf,
2146 const union mf_value *value, const union mf_value *mask,
2147 struct match *match, char **err_str)
2148 {
2149 if (!mask || is_all_ones(mask, mf->n_bytes)) {
2150 mf_set_value(mf, value, match, err_str);
2151 return mf->usable_protocols_exact;
2152 } else if (is_all_zeros(mask, mf->n_bytes) && !mf_is_tun_metadata(mf)) {
2153 /* Tunnel metadata matches on the existence of the field itself, so
2154 * it still needs to be encoded even if the value is wildcarded. */
2155 mf_set_wild(mf, match, err_str);
2156 return OFPUTIL_P_ANY;
2157 }
2158
2159 if (err_str) {
2160 *err_str = NULL;
2161 }
2162
2163 switch (mf->id) {
2164 case MFF_CT_ZONE:
2165 case MFF_CT_NW_PROTO:
2166 case MFF_CT_NW_SRC:
2167 case MFF_CT_NW_DST:
2168 case MFF_CT_IPV6_SRC:
2169 case MFF_CT_IPV6_DST:
2170 case MFF_CT_TP_SRC:
2171 case MFF_CT_TP_DST:
2172 case MFF_RECIRC_ID:
2173 case MFF_PACKET_TYPE:
2174 case MFF_CONJ_ID:
2175 case MFF_IN_PORT:
2176 case MFF_IN_PORT_OXM:
2177 case MFF_ACTSET_OUTPUT:
2178 case MFF_SKB_PRIORITY:
2179 case MFF_ETH_TYPE:
2180 case MFF_DL_VLAN:
2181 case MFF_DL_VLAN_PCP:
2182 case MFF_VLAN_PCP:
2183 case MFF_MPLS_LABEL:
2184 case MFF_MPLS_TC:
2185 case MFF_MPLS_BOS:
2186 case MFF_MPLS_TTL:
2187 case MFF_IP_PROTO:
2188 case MFF_IP_TTL:
2189 case MFF_IP_DSCP:
2190 case MFF_IP_DSCP_SHIFTED:
2191 case MFF_IP_ECN:
2192 case MFF_ARP_OP:
2193 case MFF_ICMPV4_TYPE:
2194 case MFF_ICMPV4_CODE:
2195 case MFF_ICMPV6_TYPE:
2196 case MFF_ICMPV6_CODE:
2197 return OFPUTIL_P_NONE;
2198
2199 case MFF_DP_HASH:
2200 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
2201 break;
2202 case MFF_TUN_ID:
2203 match_set_tun_id_masked(match, value->be64, mask->be64);
2204 break;
2205 case MFF_TUN_SRC:
2206 match_set_tun_src_masked(match, value->be32, mask->be32);
2207 break;
2208 case MFF_TUN_DST:
2209 match_set_tun_dst_masked(match, value->be32, mask->be32);
2210 break;
2211 case MFF_TUN_IPV6_SRC:
2212 match_set_tun_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
2213 break;
2214 case MFF_TUN_IPV6_DST:
2215 match_set_tun_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
2216 break;
2217 case MFF_TUN_FLAGS:
2218 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
2219 break;
2220 case MFF_TUN_GBP_ID:
2221 match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
2222 break;
2223 case MFF_TUN_GBP_FLAGS:
2224 match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
2225 break;
2226 case MFF_TUN_TTL:
2227 match_set_tun_ttl_masked(match, value->u8, mask->u8);
2228 break;
2229 case MFF_TUN_TOS:
2230 match_set_tun_tos_masked(match, value->u8, mask->u8);
2231 break;
2232 CASE_MFF_TUN_METADATA:
2233 tun_metadata_set_match(mf, value, mask, match, err_str);
2234 break;
2235
2236 case MFF_METADATA:
2237 match_set_metadata_masked(match, value->be64, mask->be64);
2238 break;
2239
2240 CASE_MFF_REGS:
2241 match_set_reg_masked(match, mf->id - MFF_REG0,
2242 ntohl(value->be32), ntohl(mask->be32));
2243 break;
2244
2245 CASE_MFF_XREGS:
2246 match_set_xreg_masked(match, mf->id - MFF_XREG0,
2247 ntohll(value->be64), ntohll(mask->be64));
2248 break;
2249
2250 CASE_MFF_XXREGS: {
2251 match_set_xxreg_masked(match, mf->id - MFF_XXREG0,
2252 ntoh128(value->be128), ntoh128(mask->be128));
2253 break;
2254 }
2255
2256 case MFF_PKT_MARK:
2257 match_set_pkt_mark_masked(match, ntohl(value->be32),
2258 ntohl(mask->be32));
2259 break;
2260
2261 case MFF_CT_STATE:
2262 match_set_ct_state_masked(match, ntohl(value->be32), ntohl(mask->be32));
2263 break;
2264
2265 case MFF_CT_MARK:
2266 match_set_ct_mark_masked(match, ntohl(value->be32), ntohl(mask->be32));
2267 break;
2268
2269 case MFF_CT_LABEL:
2270 match_set_ct_label_masked(match, ntoh128(value->be128),
2271 ntoh128(mask->be128));
2272 break;
2273
2274 case MFF_ETH_DST:
2275 match_set_dl_dst_masked(match, value->mac, mask->mac);
2276 break;
2277
2278 case MFF_ETH_SRC:
2279 match_set_dl_src_masked(match, value->mac, mask->mac);
2280 break;
2281
2282 case MFF_ARP_SHA:
2283 case MFF_ND_SLL:
2284 match_set_arp_sha_masked(match, value->mac, mask->mac);
2285 break;
2286
2287 case MFF_ARP_THA:
2288 case MFF_ND_TLL:
2289 match_set_arp_tha_masked(match, value->mac, mask->mac);
2290 break;
2291
2292 case MFF_VLAN_TCI:
2293 match_set_dl_tci_masked(match, value->be16, mask->be16);
2294 break;
2295
2296 case MFF_VLAN_VID:
2297 match_set_vlan_vid_masked(match, value->be16, mask->be16);
2298 break;
2299
2300 case MFF_IPV4_SRC:
2301 match_set_nw_src_masked(match, value->be32, mask->be32);
2302 break;
2303
2304 case MFF_IPV4_DST:
2305 match_set_nw_dst_masked(match, value->be32, mask->be32);
2306 break;
2307
2308 case MFF_IPV6_SRC:
2309 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
2310 break;
2311
2312 case MFF_IPV6_DST:
2313 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
2314 break;
2315
2316 case MFF_IPV6_LABEL:
2317 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
2318 mf_set_value(mf, value, match, err_str);
2319 } else {
2320 match_set_ipv6_label_masked(match, value->be32, mask->be32);
2321 }
2322 break;
2323
2324 case MFF_ND_TARGET:
2325 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
2326 break;
2327
2328 case MFF_IP_FRAG:
2329 match_set_nw_frag_masked(match, value->u8, mask->u8);
2330 break;
2331
2332 case MFF_ARP_SPA:
2333 match_set_nw_src_masked(match, value->be32, mask->be32);
2334 break;
2335
2336 case MFF_ARP_TPA:
2337 match_set_nw_dst_masked(match, value->be32, mask->be32);
2338 break;
2339
2340 case MFF_TCP_SRC:
2341 case MFF_UDP_SRC:
2342 case MFF_SCTP_SRC:
2343 match_set_tp_src_masked(match, value->be16, mask->be16);
2344 break;
2345
2346 case MFF_TCP_DST:
2347 case MFF_UDP_DST:
2348 case MFF_SCTP_DST:
2349 match_set_tp_dst_masked(match, value->be16, mask->be16);
2350 break;
2351
2352 case MFF_TCP_FLAGS:
2353 match_set_tcp_flags_masked(match, value->be16, mask->be16);
2354 break;
2355
2356 case MFF_NSH_FLAGS:
2357 MATCH_SET_FIELD_MASKED(match, nsh.flags, value->u8, mask->u8);
2358 break;
2359 case MFF_NSH_MDTYPE:
2360 MATCH_SET_FIELD_MASKED(match, nsh.mdtype, value->u8, mask->u8);
2361 break;
2362 case MFF_NSH_NP:
2363 MATCH_SET_FIELD_MASKED(match, nsh.np, value->u8, mask->u8);
2364 break;
2365 case MFF_NSH_SPI:
2366 MATCH_SET_FIELD_MASKED(match, nsh.spi, value->be32, mask->be32);
2367 break;
2368 case MFF_NSH_SI:
2369 MATCH_SET_FIELD_MASKED(match, nsh.si, value->u8, mask->u8);
2370 break;
2371 case MFF_NSH_C1:
2372 case MFF_NSH_C2:
2373 case MFF_NSH_C3:
2374 case MFF_NSH_C4:
2375 MATCH_SET_FIELD_MASKED(match, nsh.c[mf->id - MFF_NSH_C1],
2376 value->be32, mask->be32);
2377 break;
2378
2379 case MFF_N_IDS:
2380 default:
2381 OVS_NOT_REACHED();
2382 }
2383
2384 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
2385 || ip_is_cidr(mask->be32))
2386 ? mf->usable_protocols_cidr
2387 : mf->usable_protocols_bitwise);
2388 }
2389
2390 static enum ofperr
2391 mf_check__(const struct mf_subfield *sf, const struct match *match,
2392 const char *type)
2393 {
2394 if (!sf->field) {
2395 VLOG_WARN_RL(&rl, "unknown %s field", type);
2396 return OFPERR_OFPBAC_BAD_SET_TYPE;
2397 } else if (!sf->n_bits) {
2398 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
2399 return OFPERR_OFPBAC_BAD_SET_LEN;
2400 } else if (sf->ofs >= sf->field->n_bits) {
2401 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
2402 sf->ofs, sf->field->n_bits, type, sf->field->name);
2403 return OFPERR_OFPBAC_BAD_SET_LEN;
2404 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
2405 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
2406 "of %s field %s", sf->ofs, sf->n_bits,
2407 sf->field->n_bits, type, sf->field->name);
2408 return OFPERR_OFPBAC_BAD_SET_LEN;
2409 } else if (match && !mf_are_match_prereqs_ok(sf->field, match)) {
2410 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
2411 type, sf->field->name);
2412 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2413 } else {
2414 return 0;
2415 }
2416 }
2417
2418 /* Sets all the bits in 'sf' to 1 within 'wc', if 'wc' is nonnull. */
2419 static void
2420 unwildcard_subfield(const struct mf_subfield *sf, struct flow_wildcards *wc)
2421 {
2422 if (wc) {
2423 union mf_value mask;
2424
2425 memset(&mask, 0, sizeof mask);
2426 bitwise_one(&mask, sf->field->n_bytes, sf->ofs, sf->n_bits);
2427 mf_mask_field_masked(sf->field, &mask, wc);
2428 }
2429 }
2430
2431 /* Copies 'src' into 'dst' within 'flow', and sets all the bits in 'src' and
2432 * 'dst' to 1s in 'wc', if 'wc' is nonnull.
2433 *
2434 * 'src' and 'dst' may overlap. */
2435 void
2436 mf_subfield_copy(const struct mf_subfield *src,
2437 const struct mf_subfield *dst,
2438 struct flow *flow, struct flow_wildcards *wc)
2439 {
2440 ovs_assert(src->n_bits == dst->n_bits);
2441 if (mf_are_prereqs_ok(dst->field, flow, wc)
2442 && mf_are_prereqs_ok(src->field, flow, wc)) {
2443 unwildcard_subfield(src, wc);
2444 unwildcard_subfield(dst, wc);
2445
2446 union mf_value src_value;
2447 union mf_value dst_value;
2448 mf_get_value(dst->field, flow, &dst_value);
2449 mf_get_value(src->field, flow, &src_value);
2450 bitwise_copy(&src_value, src->field->n_bytes, src->ofs,
2451 &dst_value, dst->field->n_bytes, dst->ofs,
2452 src->n_bits);
2453 mf_set_flow_value(dst->field, &dst_value, flow);
2454 }
2455 }
2456
2457 /* Swaps the bits in 'src' and 'dst' within 'flow', and sets all the bits in
2458 * 'src' and 'dst' to 1s in 'wc', if 'wc' is nonnull.
2459 *
2460 * 'src' and 'dst' may overlap. */
2461 void
2462 mf_subfield_swap(const struct mf_subfield *a,
2463 const struct mf_subfield *b,
2464 struct flow *flow, struct flow_wildcards *wc)
2465 {
2466 ovs_assert(a->n_bits == b->n_bits);
2467 if (mf_are_prereqs_ok(a->field, flow, wc)
2468 && mf_are_prereqs_ok(b->field, flow, wc)) {
2469 unwildcard_subfield(a, wc);
2470 unwildcard_subfield(b, wc);
2471
2472 union mf_value a_value;
2473 union mf_value b_value;
2474 mf_get_value(a->field, flow, &a_value);
2475 mf_get_value(b->field, flow, &b_value);
2476 union mf_value b2_value = b_value;
2477
2478 /* Copy 'a' into 'b'. */
2479 bitwise_copy(&a_value, a->field->n_bytes, a->ofs,
2480 &b_value, b->field->n_bytes, b->ofs,
2481 a->n_bits);
2482 mf_set_flow_value(b->field, &b_value, flow);
2483
2484 /* Copy original 'b' into 'a'. */
2485 bitwise_copy(&b2_value, b->field->n_bytes, b->ofs,
2486 &a_value, a->field->n_bytes, a->ofs,
2487 b->n_bits);
2488 mf_set_flow_value(a->field, &a_value, flow);
2489 }
2490 }
2491
2492 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2493 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2494 * ofp_mkerr()). */
2495 enum ofperr
2496 mf_check_src(const struct mf_subfield *sf, const struct match *match)
2497 {
2498 return mf_check__(sf, match, "source");
2499 }
2500
2501 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2502 * if so, otherwise an OpenFlow error code (e.g. as returned by
2503 * ofp_mkerr()). */
2504 enum ofperr
2505 mf_check_dst(const struct mf_subfield *sf, const struct match *match)
2506 {
2507 int error = mf_check__(sf, match, "destination");
2508 if (!error && !sf->field->writable) {
2509 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2510 sf->field->name);
2511 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2512 }
2513 return error;
2514 }
2515
2516 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2517 * 'value' and 'mask', respectively. */
2518 void
2519 mf_get(const struct mf_field *mf, const struct match *match,
2520 union mf_value *value, union mf_value *mask)
2521 {
2522 mf_get_value(mf, &match->flow, value);
2523 mf_get_mask(mf, &match->wc, mask);
2524 }
2525
2526 static char *
2527 mf_from_integer_string(const struct mf_field *mf, const char *s,
2528 uint8_t *valuep, uint8_t *maskp)
2529 {
2530 char *tail;
2531 const char *err_str = "";
2532 int err;
2533
2534 err = parse_int_string(s, valuep, mf->n_bytes, &tail);
2535 if (err || (*tail != '\0' && *tail != '/')) {
2536 err_str = "value";
2537 goto syntax_error;
2538 }
2539
2540 if (*tail == '/') {
2541 err = parse_int_string(tail + 1, maskp, mf->n_bytes, &tail);
2542 if (err || *tail != '\0') {
2543 err_str = "mask";
2544 goto syntax_error;
2545 }
2546 } else {
2547 memset(maskp, 0xff, mf->n_bytes);
2548 }
2549
2550 return NULL;
2551
2552 syntax_error:
2553 if (err == ERANGE) {
2554 return xasprintf("%s: %s too large for %u-byte field %s",
2555 s, err_str, mf->n_bytes, mf->name);
2556 } else {
2557 return xasprintf("%s: bad syntax for %s %s", s, mf->name, err_str);
2558 }
2559 }
2560
2561 static char *
2562 mf_from_packet_type_string(const char *s, ovs_be32 *packet_type)
2563 {
2564 char *tail;
2565 const char *err_str = "";
2566 int err;
2567
2568 if (*s != '(') {
2569 err_str = "missing '('";
2570 goto syntax_error;
2571 }
2572 s++;
2573 err = parse_int_string(s, (uint8_t *)packet_type, 2, &tail);
2574 if (err) {
2575 err_str = "ns";
2576 goto syntax_error;
2577 }
2578 if (*tail != ',') {
2579 err_str = "missing ','";
2580 goto syntax_error;
2581 }
2582 s = tail + 1;
2583 err = parse_int_string(s, ((uint8_t *)packet_type) + 2, 2, &tail);
2584 if (err) {
2585 err_str = "ns_type";
2586 goto syntax_error;
2587 }
2588 if (*tail != ')') {
2589 err_str = "missing ')'";
2590 goto syntax_error;
2591 }
2592
2593 return NULL;
2594
2595 syntax_error:
2596 return xasprintf("%s: bad syntax for packet type %s", s, err_str);
2597 }
2598
2599 static char *
2600 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2601 struct eth_addr *mac, struct eth_addr *mask)
2602 {
2603 int n;
2604
2605 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
2606
2607 n = -1;
2608 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*mac), &n)
2609 && n == strlen(s)) {
2610 *mask = eth_addr_exact;
2611 return NULL;
2612 }
2613
2614 n = -1;
2615 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
2616 ETH_ADDR_SCAN_ARGS(*mac), ETH_ADDR_SCAN_ARGS(*mask), &n)
2617 && n == strlen(s)) {
2618 return NULL;
2619 }
2620
2621 return xasprintf("%s: invalid Ethernet address", s);
2622 }
2623
2624 static char *
2625 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2626 ovs_be32 *ip, ovs_be32 *mask)
2627 {
2628 ovs_assert(mf->n_bytes == sizeof *ip);
2629 return ip_parse_masked(s, ip, mask);
2630 }
2631
2632 static char *
2633 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2634 struct in6_addr *ipv6, struct in6_addr *mask)
2635 {
2636 ovs_assert(mf->n_bytes == sizeof *ipv6);
2637 return ipv6_parse_masked(s, ipv6, mask);
2638 }
2639
2640 static char *
2641 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2642 const struct ofputil_port_map *port_map,
2643 ovs_be16 *valuep, ovs_be16 *maskp)
2644 {
2645 ofp_port_t port;
2646
2647 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2648
2649 if (ofputil_port_from_string(s, port_map, &port)) {
2650 *valuep = htons(ofp_to_u16(port));
2651 *maskp = OVS_BE16_MAX;
2652 return NULL;
2653 }
2654 return xasprintf("%s: invalid or unknown port for %s", s, mf->name);
2655 }
2656
2657 static char *
2658 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2659 const struct ofputil_port_map *port_map,
2660 ovs_be32 *valuep, ovs_be32 *maskp)
2661 {
2662 ofp_port_t port;
2663
2664 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2665 if (ofputil_port_from_string(s, port_map, &port)) {
2666 *valuep = ofputil_port_to_ofp11(port);
2667 *maskp = OVS_BE32_MAX;
2668 return NULL;
2669 }
2670 return xasprintf("%s: port value out of range for %s", s, mf->name);
2671 }
2672
2673 struct frag_handling {
2674 const char *name;
2675 uint8_t mask;
2676 uint8_t value;
2677 };
2678
2679 static const struct frag_handling all_frags[] = {
2680 #define A FLOW_NW_FRAG_ANY
2681 #define L FLOW_NW_FRAG_LATER
2682 /* name mask value */
2683
2684 { "no", A|L, 0 },
2685 { "first", A|L, A },
2686 { "later", A|L, A|L },
2687
2688 { "no", A, 0 },
2689 { "yes", A, A },
2690
2691 { "not_later", L, 0 },
2692 { "later", L, L },
2693 #undef A
2694 #undef L
2695 };
2696
2697 static char *
2698 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2699 {
2700 const struct frag_handling *h;
2701
2702 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2703 if (!strcasecmp(s, h->name)) {
2704 /* We force the upper bits of the mask on to make mf_parse_value()
2705 * happy (otherwise it will never think it's an exact match.) */
2706 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2707 *valuep = h->value;
2708 return NULL;
2709 }
2710 }
2711
2712 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2713 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2714 }
2715
2716 static char *
2717 parse_mf_flags(const char *s, const char *(*bit_to_string)(uint32_t),
2718 const char *field_name, ovs_be16 *flagsp, ovs_be16 allowed,
2719 ovs_be16 *maskp)
2720 {
2721 int err;
2722 char *err_str;
2723 uint32_t flags, mask;
2724
2725 err = parse_flags(s, bit_to_string, '\0', field_name, &err_str,
2726 &flags, ntohs(allowed), maskp ? &mask : NULL);
2727 if (err < 0) {
2728 return err_str;
2729 }
2730
2731 *flagsp = htons(flags);
2732 if (maskp) {
2733 *maskp = htons(mask);
2734 }
2735
2736 return NULL;
2737 }
2738
2739 static char *
2740 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2741 {
2742 return parse_mf_flags(s, packet_tcp_flag_to_string, "TCP", flagsp,
2743 TCP_FLAGS_BE16(OVS_BE16_MAX), maskp);
2744 }
2745
2746 static char *
2747 mf_from_tun_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2748 {
2749 return parse_mf_flags(s, flow_tun_flag_to_string, "tunnel", flagsp,
2750 htons(FLOW_TNL_PUB_F_MASK), maskp);
2751 }
2752
2753 static char *
2754 mf_from_ct_state_string(const char *s, ovs_be32 *flagsp, ovs_be32 *maskp)
2755 {
2756 int err;
2757 char *err_str;
2758 uint32_t flags, mask;
2759
2760 err = parse_flags(s, ct_state_to_string, '\0', "ct_state", &err_str,
2761 &flags, CS_SUPPORTED_MASK, maskp ? &mask : NULL);
2762 if (err < 0) {
2763 return err_str;
2764 }
2765
2766 *flagsp = htonl(flags);
2767 if (maskp) {
2768 *maskp = htonl(mask);
2769 }
2770
2771 return NULL;
2772 }
2773
2774 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2775 * NULL if successful, otherwise a malloc()'d string describing the error. */
2776 char *
2777 mf_parse(const struct mf_field *mf, const char *s,
2778 const struct ofputil_port_map *port_map,
2779 union mf_value *value, union mf_value *mask)
2780 {
2781 char *error;
2782
2783 if (!strcmp(s, "*")) {
2784 memset(value, 0, mf->n_bytes);
2785 memset(mask, 0, mf->n_bytes);
2786 return NULL;
2787 }
2788
2789 switch (mf->string) {
2790 case MFS_DECIMAL:
2791 case MFS_HEXADECIMAL:
2792 error = mf_from_integer_string(mf, s,
2793 (uint8_t *) value, (uint8_t *) mask);
2794 break;
2795
2796 case MFS_CT_STATE:
2797 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2798 error = mf_from_ct_state_string(s, &value->be32, &mask->be32);
2799 break;
2800
2801 case MFS_ETHERNET:
2802 error = mf_from_ethernet_string(mf, s, &value->mac, &mask->mac);
2803 break;
2804
2805 case MFS_IPV4:
2806 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2807 break;
2808
2809 case MFS_IPV6:
2810 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2811 break;
2812
2813 case MFS_OFP_PORT:
2814 error = mf_from_ofp_port_string(mf, s, port_map,
2815 &value->be16, &mask->be16);
2816 break;
2817
2818 case MFS_OFP_PORT_OXM:
2819 error = mf_from_ofp_port_string32(mf, s, port_map,
2820 &value->be32, &mask->be32);
2821 break;
2822
2823 case MFS_FRAG:
2824 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2825 break;
2826
2827 case MFS_TNL_FLAGS:
2828 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2829 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2830 break;
2831
2832 case MFS_TCP_FLAGS:
2833 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2834 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2835 break;
2836
2837 case MFS_PACKET_TYPE:
2838 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2839 error = mf_from_packet_type_string(s, &value->be32);
2840 mask->be32 = OVS_BE32_MAX;
2841 break;
2842
2843 default:
2844 OVS_NOT_REACHED();
2845 }
2846
2847 if (!error && !mf_is_mask_valid(mf, mask)) {
2848 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2849 }
2850 return error;
2851 }
2852
2853 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2854 * successful, otherwise a malloc()'d string describing the error. */
2855 char *
2856 mf_parse_value(const struct mf_field *mf, const char *s,
2857 const struct ofputil_port_map *port_map, union mf_value *value)
2858 {
2859 union mf_value mask;
2860 char *error;
2861
2862 error = mf_parse(mf, s, port_map, value, &mask);
2863 if (error) {
2864 return error;
2865 }
2866
2867 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2868 return xasprintf("%s: wildcards not allowed here", s);
2869 }
2870 return NULL;
2871 }
2872
2873 static void
2874 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2875 const uint8_t *maskp, struct ds *s)
2876 {
2877 if (mf->string == MFS_HEXADECIMAL) {
2878 ds_put_hex(s, valuep, mf->n_bytes);
2879 } else {
2880 unsigned long long int integer = 0;
2881 int i;
2882
2883 ovs_assert(mf->n_bytes <= 8);
2884 for (i = 0; i < mf->n_bytes; i++) {
2885 integer = (integer << 8) | valuep[i];
2886 }
2887 ds_put_format(s, "%lld", integer);
2888 }
2889
2890 if (maskp) {
2891 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2892 * not sure that that a bit-mask written in decimal is ever easier to
2893 * understand than the same bit-mask written in hexadecimal. */
2894 ds_put_char(s, '/');
2895 ds_put_hex(s, maskp, mf->n_bytes);
2896 }
2897 }
2898
2899 static void
2900 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2901 {
2902 const struct frag_handling *h;
2903
2904 mask &= FLOW_NW_FRAG_MASK;
2905 value &= mask;
2906
2907 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2908 if (value == h->value && mask == h->mask) {
2909 ds_put_cstr(s, h->name);
2910 return;
2911 }
2912 }
2913 ds_put_cstr(s, "<error>");
2914 }
2915
2916 static void
2917 mf_format_tnl_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2918 {
2919 format_flags_masked(s, NULL, flow_tun_flag_to_string, ntohs(value),
2920 ntohs(mask) & FLOW_TNL_PUB_F_MASK, FLOW_TNL_PUB_F_MASK);
2921 }
2922
2923 static void
2924 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2925 {
2926 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2927 TCP_FLAGS(mask), TCP_FLAGS(OVS_BE16_MAX));
2928 }
2929
2930 static void
2931 mf_format_ct_state_string(ovs_be32 value, ovs_be32 mask, struct ds *s)
2932 {
2933 format_flags_masked(s, NULL, ct_state_to_string, ntohl(value),
2934 ntohl(mask), UINT16_MAX);
2935 }
2936
2937 static void
2938 mf_format_packet_type_string(ovs_be32 value, ovs_be32 mask, struct ds *s)
2939 {
2940 format_packet_type_masked(s, value, mask);
2941 }
2942
2943 /* Appends to 's' a string representation of field 'mf' whose value is in
2944 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2945 void
2946 mf_format(const struct mf_field *mf,
2947 const union mf_value *value, const union mf_value *mask,
2948 const struct ofputil_port_map *port_map,
2949 struct ds *s)
2950 {
2951 if (mask) {
2952 if (is_all_zeros(mask, mf->n_bytes)) {
2953 ds_put_cstr(s, "ANY");
2954 return;
2955 } else if (is_all_ones(mask, mf->n_bytes)) {
2956 mask = NULL;
2957 }
2958 }
2959
2960 switch (mf->string) {
2961 case MFS_OFP_PORT_OXM:
2962 if (!mask) {
2963 ofp_port_t port;
2964 ofputil_port_from_ofp11(value->be32, &port);
2965 ofputil_format_port(port, port_map, s);
2966 break;
2967 }
2968 /* fall through */
2969 case MFS_OFP_PORT:
2970 if (!mask) {
2971 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), port_map, s);
2972 break;
2973 }
2974 /* fall through */
2975 case MFS_DECIMAL:
2976 case MFS_HEXADECIMAL:
2977 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2978 break;
2979
2980 case MFS_CT_STATE:
2981 mf_format_ct_state_string(value->be32,
2982 mask ? mask->be32 : OVS_BE32_MAX, s);
2983 break;
2984
2985 case MFS_ETHERNET:
2986 eth_format_masked(value->mac, mask ? &mask->mac : NULL, s);
2987 break;
2988
2989 case MFS_IPV4:
2990 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2991 break;
2992
2993 case MFS_IPV6:
2994 ipv6_format_masked(&value->ipv6, mask ? &mask->ipv6 : NULL, s);
2995 break;
2996
2997 case MFS_FRAG:
2998 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2999 break;
3000
3001 case MFS_TNL_FLAGS:
3002 mf_format_tnl_flags_string(value->be16,
3003 mask ? mask->be16 : OVS_BE16_MAX, s);
3004 break;
3005
3006 case MFS_TCP_FLAGS:
3007 mf_format_tcp_flags_string(value->be16,
3008 mask ? mask->be16 : OVS_BE16_MAX, s);
3009 break;
3010
3011 case MFS_PACKET_TYPE:
3012 mf_format_packet_type_string(value->be32,
3013 mask ? mask->be32 : OVS_BE32_MAX, s);
3014 break;
3015
3016 default:
3017 OVS_NOT_REACHED();
3018 }
3019 }
3020 \f
3021 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
3022 * least-significant bits in 'x'.
3023 */
3024 void
3025 mf_write_subfield_flow(const struct mf_subfield *sf,
3026 const union mf_subvalue *x, struct flow *flow)
3027 {
3028 const struct mf_field *field = sf->field;
3029 union mf_value value;
3030
3031 mf_get_value(field, flow, &value);
3032 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
3033 sf->ofs, sf->n_bits);
3034 mf_set_flow_value(field, &value, flow);
3035 }
3036
3037 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
3038 * least-significant bits in 'x'.
3039 */
3040 void
3041 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
3042 struct match *match)
3043 {
3044 const struct mf_field *field = sf->field;
3045 union mf_value value, mask;
3046
3047 mf_get(field, match, &value, &mask);
3048 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
3049 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
3050 mf_set(field, &value, &mask, match, NULL);
3051 }
3052
3053 void
3054 mf_write_subfield_value(const struct mf_subfield *sf, const void *src,
3055 struct match *match)
3056 {
3057 const struct mf_field *field = sf->field;
3058 union mf_value value, mask;
3059 unsigned int size = DIV_ROUND_UP(sf->n_bits, 8);
3060
3061 mf_get(field, match, &value, &mask);
3062 bitwise_copy(src, size, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
3063 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
3064 mf_set(field, &value, &mask, match, NULL);
3065 }
3066
3067 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
3068 * 'match' in the correspond positions. */
3069 void
3070 mf_mask_subfield(const struct mf_field *field,
3071 const union mf_subvalue *v,
3072 const union mf_subvalue *m,
3073 struct match *match)
3074 {
3075 union mf_value value, mask;
3076
3077 mf_get(field, match, &value, &mask);
3078 bitwise_copy(v, sizeof *v, 0, &value, field->n_bytes, 0, field->n_bits);
3079 bitwise_copy(m, sizeof *m, 0, &mask, field->n_bytes, 0, field->n_bits);
3080 mf_set(field, &value, &mask, match, NULL);
3081 }
3082
3083 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
3084 * reading 'flow', e.g. as checked by mf_check_src(). */
3085 void
3086 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
3087 union mf_subvalue *x)
3088 {
3089 union mf_value value;
3090
3091 mf_get_value(sf->field, flow, &value);
3092
3093 memset(x, 0, sizeof *x);
3094 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
3095 x, sizeof *x, 0,
3096 sf->n_bits);
3097 }
3098
3099 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
3100 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
3101 * less. */
3102 uint64_t
3103 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
3104 {
3105 union mf_value value;
3106
3107 mf_get_value(sf->field, flow, &value);
3108 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
3109 }
3110
3111 void
3112 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
3113 {
3114 ds_put_hex(s, subvalue->u8, sizeof subvalue->u8);
3115 }
3116
3117 void
3118 field_array_set(enum mf_field_id id, const union mf_value *value,
3119 struct field_array *fa)
3120 {
3121 size_t i, offset = 0;
3122
3123 ovs_assert(id < MFF_N_IDS);
3124
3125 /* Find the spot for 'id'. */
3126 BITMAP_FOR_EACH_1 (i, id, fa->used.bm) {
3127 offset += mf_from_id(i)->n_bytes;
3128 }
3129
3130 size_t value_size = mf_from_id(id)->n_bytes;
3131
3132 /* make room if necessary. */
3133 if (!bitmap_is_set(fa->used.bm, id)) {
3134 fa->values = xrealloc(fa->values, fa->values_size + value_size);
3135 /* Move remainder forward, if any. */
3136 if (offset < fa->values_size) {
3137 memmove(fa->values + offset + value_size, fa->values + offset,
3138 fa->values_size - offset);
3139 }
3140 fa->values_size += value_size;
3141 }
3142 bitmap_set1(fa->used.bm, id);
3143
3144 memcpy(fa->values + offset, value, value_size);
3145 }
3146
3147 /* A wrapper for variable length mf_fields that is maintained by
3148 * struct vl_mff_map.*/
3149 struct vl_mf_field {
3150 struct mf_field mf;
3151 struct ovs_refcount ref_cnt;
3152 struct cmap_node cmap_node; /* In ofproto->vl_mff_map->cmap. */
3153 };
3154
3155 static inline uint32_t
3156 mf_field_hash(uint32_t key)
3157 {
3158 return hash_int(key, 0);
3159 }
3160
3161 static void
3162 vmf_delete(struct vl_mf_field *vmf)
3163 {
3164 if (ovs_refcount_unref(&vmf->ref_cnt) == 1) {
3165 /* Postpone as this function is typically called immediately
3166 * after removing from cmap. */
3167 ovsrcu_postpone(free, vmf);
3168 } else {
3169 VLOG_WARN_RL(&rl,
3170 "Attempted to delete VMF %s but refcount is nonzero!",
3171 vmf->mf.name);
3172 }
3173 }
3174
3175 enum ofperr
3176 mf_vl_mff_map_clear(struct vl_mff_map *vl_mff_map, bool force)
3177 OVS_REQUIRES(vl_mff_map->mutex)
3178 {
3179 struct vl_mf_field *vmf;
3180
3181 if (!force) {
3182 CMAP_FOR_EACH (vmf, cmap_node, &vl_mff_map->cmap) {
3183 if (ovs_refcount_read(&vmf->ref_cnt) != 1) {
3184 return OFPERR_NXTTMFC_INVALID_TLV_DEL;
3185 }
3186 }
3187 }
3188
3189 CMAP_FOR_EACH (vmf, cmap_node, &vl_mff_map->cmap) {
3190 cmap_remove(&vl_mff_map->cmap, &vmf->cmap_node,
3191 mf_field_hash(vmf->mf.id));
3192 vmf_delete(vmf);
3193 }
3194
3195 return 0;
3196 }
3197
3198 static struct vl_mf_field *
3199 mf_get_vl_mff__(uint32_t id, const struct vl_mff_map *vl_mff_map)
3200 {
3201 struct vl_mf_field *vmf;
3202
3203 CMAP_FOR_EACH_WITH_HASH (vmf, cmap_node, mf_field_hash(id),
3204 &vl_mff_map->cmap) {
3205 if (vmf->mf.id == id) {
3206 return vmf;
3207 }
3208 }
3209
3210 return NULL;
3211 }
3212
3213 /* If 'mff' is a variable length field, looks up 'vl_mff_map', returns a
3214 * pointer to the variable length meta-flow field corresponding to 'mff'.
3215 * Returns NULL if no mapping is existed for 'mff'. */
3216 const struct mf_field *
3217 mf_get_vl_mff(const struct mf_field *mff,
3218 const struct vl_mff_map *vl_mff_map)
3219 {
3220 if (mff && mff->variable_len && vl_mff_map) {
3221 return &mf_get_vl_mff__(mff->id, vl_mff_map)->mf;
3222 }
3223
3224 return NULL;
3225 }
3226
3227 static enum ofperr
3228 mf_vl_mff_map_del(struct vl_mff_map *vl_mff_map,
3229 const struct ofputil_tlv_table_mod *ttm, bool force)
3230 OVS_REQUIRES(vl_mff_map->mutex)
3231 {
3232 struct ofputil_tlv_map *tlv_map;
3233 struct vl_mf_field *vmf;
3234 unsigned int idx;
3235
3236 if (!force) {
3237 LIST_FOR_EACH (tlv_map, list_node, &ttm->mappings) {
3238 idx = MFF_TUN_METADATA0 + tlv_map->index;
3239 if (idx >= MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS) {
3240 return OFPERR_NXTTMFC_BAD_FIELD_IDX;
3241 }
3242
3243 vmf = mf_get_vl_mff__(idx, vl_mff_map);
3244 if (vmf && ovs_refcount_read(&vmf->ref_cnt) != 1) {
3245 return OFPERR_NXTTMFC_INVALID_TLV_DEL;
3246 }
3247 }
3248 }
3249
3250 LIST_FOR_EACH (tlv_map, list_node, &ttm->mappings) {
3251 idx = MFF_TUN_METADATA0 + tlv_map->index;
3252 if (idx >= MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS) {
3253 return OFPERR_NXTTMFC_BAD_FIELD_IDX;
3254 }
3255
3256 vmf = mf_get_vl_mff__(idx, vl_mff_map);
3257 if (vmf) {
3258 cmap_remove(&vl_mff_map->cmap, &vmf->cmap_node,
3259 mf_field_hash(idx));
3260 vmf_delete(vmf);
3261 }
3262 }
3263
3264 return 0;
3265 }
3266
3267 static enum ofperr
3268 mf_vl_mff_map_add(struct vl_mff_map *vl_mff_map,
3269 const struct ofputil_tlv_table_mod *ttm)
3270 OVS_REQUIRES(vl_mff_map->mutex)
3271 {
3272 struct ofputil_tlv_map *tlv_map;
3273 struct vl_mf_field *vmf;
3274 unsigned int idx;
3275
3276 LIST_FOR_EACH (tlv_map, list_node, &ttm->mappings) {
3277 idx = MFF_TUN_METADATA0 + tlv_map->index;
3278 if (idx >= MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS) {
3279 return OFPERR_NXTTMFC_BAD_FIELD_IDX;
3280 }
3281
3282 vmf = xmalloc(sizeof *vmf);
3283 vmf->mf = mf_fields[idx];
3284 vmf->mf.n_bytes = tlv_map->option_len;
3285 vmf->mf.n_bits = tlv_map->option_len * 8;
3286 vmf->mf.mapped = true;
3287 ovs_refcount_init(&vmf->ref_cnt);
3288
3289 cmap_insert(&vl_mff_map->cmap, &vmf->cmap_node,
3290 mf_field_hash(idx));
3291 }
3292
3293 return 0;
3294 }
3295
3296 /* Updates the tun_metadata mf_field in 'vl_mff_map' according to 'ttm'.
3297 * This function must be invoked after tun_metadata_table_mod().
3298 * Returns OFPERR_NXTTMFC_BAD_FIELD_IDX, if the index for the vl_mf_field is
3299 * invalid.
3300 * Returns OFPERR_NXTTMFC_INVALID_TLV_DEL, if 'ttm' tries to delete an
3301 * vl_mf_field that is still used by any active flow.*/
3302 enum ofperr
3303 mf_vl_mff_map_mod_from_tun_metadata(struct vl_mff_map *vl_mff_map,
3304 const struct ofputil_tlv_table_mod *ttm)
3305 OVS_REQUIRES(vl_mff_map->mutex)
3306 {
3307 switch (ttm->command) {
3308 case NXTTMC_ADD:
3309 return mf_vl_mff_map_add(vl_mff_map, ttm);
3310
3311 case NXTTMC_DELETE:
3312 return mf_vl_mff_map_del(vl_mff_map, ttm, false);
3313
3314 case NXTTMC_CLEAR:
3315 return mf_vl_mff_map_clear(vl_mff_map, false);
3316
3317 default:
3318 OVS_NOT_REACHED();
3319 }
3320
3321 return 0;
3322 }
3323
3324 /* Returns true if a variable length meta-flow field 'mff' is not mapped in
3325 * the 'vl_mff_map'. */
3326 bool
3327 mf_vl_mff_invalid(const struct mf_field *mff, const struct vl_mff_map *map)
3328 {
3329 return map && mff && mff->variable_len && !mff->mapped;
3330 }
3331
3332 void
3333 mf_vl_mff_set_tlv_bitmap(const struct mf_field *mff, uint64_t *tlv_bitmap)
3334 {
3335 if (mff && mff->mapped) {
3336 ovs_assert(mf_is_tun_metadata(mff));
3337 ULLONG_SET1(*tlv_bitmap, mff->id - MFF_TUN_METADATA0);
3338 }
3339 }
3340
3341 static void
3342 mf_vl_mff_ref_cnt_mod(const struct vl_mff_map *map, uint64_t tlv_bitmap,
3343 bool ref)
3344 {
3345 struct vl_mf_field *vmf;
3346 int i;
3347
3348 if (map) {
3349 ULLONG_FOR_EACH_1 (i, tlv_bitmap) {
3350 vmf = mf_get_vl_mff__(i + MFF_TUN_METADATA0, map);
3351 if (vmf) {
3352 if (ref) {
3353 ovs_refcount_ref(&vmf->ref_cnt);
3354 } else {
3355 ovs_refcount_unref(&vmf->ref_cnt);
3356 }
3357 } else {
3358 VLOG_WARN("Invalid TLV index %d.", i);
3359 }
3360 }
3361 }
3362 }
3363
3364 void
3365 mf_vl_mff_ref(const struct vl_mff_map *map, uint64_t tlv_bitmap)
3366 {
3367 mf_vl_mff_ref_cnt_mod(map, tlv_bitmap, true);
3368 }
3369
3370 void
3371 mf_vl_mff_unref(const struct vl_mff_map *map, uint64_t tlv_bitmap)
3372 {
3373 mf_vl_mff_ref_cnt_mod(map, tlv_bitmap, false);
3374 }
3375
3376 enum ofperr
3377 mf_vl_mff_nx_pull_header(struct ofpbuf *b, const struct vl_mff_map *vl_mff_map,
3378 const struct mf_field **field, bool *masked,
3379 uint64_t *tlv_bitmap)
3380 {
3381 enum ofperr error = nx_pull_header(b, vl_mff_map, field, masked);
3382 if (error) {
3383 return error;
3384 }
3385
3386 mf_vl_mff_set_tlv_bitmap(*field, tlv_bitmap);
3387 return 0;
3388 }
3389
3390 enum ofperr
3391 mf_vl_mff_nx_pull_entry(struct ofpbuf *b, const struct vl_mff_map *vl_mff_map,
3392 const struct mf_field **field, union mf_value *value,
3393 union mf_value *mask, uint64_t *tlv_bitmap)
3394 {
3395 enum ofperr error = nx_pull_entry(b, vl_mff_map, field, value, mask);
3396 if (error) {
3397 return error;
3398 }
3399
3400 mf_vl_mff_set_tlv_bitmap(*field, tlv_bitmap);
3401 return 0;
3402 }
3403
3404 enum ofperr
3405 mf_vl_mff_mf_from_nxm_header(uint32_t header,
3406 const struct vl_mff_map *vl_mff_map,
3407 const struct mf_field **field,
3408 uint64_t *tlv_bitmap)
3409 {
3410 *field = mf_from_nxm_header(header, vl_mff_map);
3411 if (mf_vl_mff_invalid(*field, vl_mff_map)) {
3412 return OFPERR_NXFMFC_INVALID_TLV_FIELD;
3413 }
3414
3415 mf_vl_mff_set_tlv_bitmap(*field, tlv_bitmap);
3416 return 0;
3417 }