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