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