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