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