]> git.proxmox.com Git - ovs.git/blob - lib/meta-flow.c
flow: Support OF1.5+ (draft) actset_output field.
[ovs.git] / lib / meta-flow.c
1 /*
2 * Copyright (c) 2011, 2012, 2013, 2014 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 "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 "dynamic-string.h"
28 #include "nx-match.h"
29 #include "ofp-errors.h"
30 #include "ofp-util.h"
31 #include "ovs-thread.h"
32 #include "packets.h"
33 #include "random.h"
34 #include "shash.h"
35 #include "socket-util.h"
36 #include "unaligned.h"
37 #include "vlog.h"
38
39 VLOG_DEFINE_THIS_MODULE(meta_flow);
40
41 #define FLOW_U32OFS(FIELD) \
42 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
43
44 #define MF_FIELD_SIZES(MEMBER) \
45 sizeof ((union mf_value *)0)->MEMBER, \
46 8 * sizeof ((union mf_value *)0)->MEMBER
47
48 extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */
49
50 const struct mf_field mf_fields[MFF_N_IDS] = {
51 #include "meta-flow.inc"
52 };
53
54 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
55 static struct shash mf_by_name;
56
57 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
58 * controller and so there's not much point in showing a lot of them. */
59 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
60
61 static void nxm_init(void);
62
63 /* Returns the field with the given 'name', or a null pointer if no field has
64 * that name. */
65 const struct mf_field *
66 mf_from_name(const char *name)
67 {
68 nxm_init();
69 return shash_find_data(&mf_by_name, name);
70 }
71
72 static void
73 nxm_do_init(void)
74 {
75 int i;
76
77 shash_init(&mf_by_name);
78 for (i = 0; i < MFF_N_IDS; i++) {
79 const struct mf_field *mf = &mf_fields[i];
80
81 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
82
83 shash_add_once(&mf_by_name, mf->name, mf);
84 if (mf->extra_name) {
85 shash_add_once(&mf_by_name, mf->extra_name, mf);
86 }
87 }
88 }
89
90 static void
91 nxm_init(void)
92 {
93 static pthread_once_t once = PTHREAD_ONCE_INIT;
94 pthread_once(&once, nxm_do_init);
95 }
96
97 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
98 * specifies at least one bit in the field.
99 *
100 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
101 * meets 'mf''s prerequisites. */
102 bool
103 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
104 {
105 switch (mf->id) {
106 case MFF_DP_HASH:
107 return !wc->masks.dp_hash;
108 case MFF_RECIRC_ID:
109 return !wc->masks.recirc_id;
110 case MFF_TUN_SRC:
111 return !wc->masks.tunnel.ip_src;
112 case MFF_TUN_DST:
113 return !wc->masks.tunnel.ip_dst;
114 case MFF_TUN_ID:
115 case MFF_TUN_TOS:
116 case MFF_TUN_TTL:
117 case MFF_TUN_FLAGS:
118 return !wc->masks.tunnel.tun_id;
119 case MFF_METADATA:
120 return !wc->masks.metadata;
121 case MFF_IN_PORT:
122 case MFF_IN_PORT_OXM:
123 return !wc->masks.in_port.ofp_port;
124 case MFF_SKB_PRIORITY:
125 return !wc->masks.skb_priority;
126 case MFF_PKT_MARK:
127 return !wc->masks.pkt_mark;
128 CASE_MFF_REGS:
129 return !wc->masks.regs[mf->id - MFF_REG0];
130 CASE_MFF_XREGS:
131 return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
132 case MFF_ACTSET_OUTPUT:
133 return !wc->masks.actset_output;
134
135 case MFF_ETH_SRC:
136 return eth_addr_is_zero(wc->masks.dl_src);
137 case MFF_ETH_DST:
138 return eth_addr_is_zero(wc->masks.dl_dst);
139 case MFF_ETH_TYPE:
140 return !wc->masks.dl_type;
141
142 case MFF_ARP_SHA:
143 case MFF_ND_SLL:
144 return eth_addr_is_zero(wc->masks.arp_sha);
145
146 case MFF_ARP_THA:
147 case MFF_ND_TLL:
148 return eth_addr_is_zero(wc->masks.arp_tha);
149
150 case MFF_VLAN_TCI:
151 return !wc->masks.vlan_tci;
152 case MFF_DL_VLAN:
153 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
154 case MFF_VLAN_VID:
155 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
156 case MFF_DL_VLAN_PCP:
157 case MFF_VLAN_PCP:
158 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
159
160 case MFF_MPLS_LABEL:
161 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
162 case MFF_MPLS_TC:
163 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
164 case MFF_MPLS_BOS:
165 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
166
167 case MFF_IPV4_SRC:
168 return !wc->masks.nw_src;
169 case MFF_IPV4_DST:
170 return !wc->masks.nw_dst;
171
172 case MFF_IPV6_SRC:
173 return ipv6_mask_is_any(&wc->masks.ipv6_src);
174 case MFF_IPV6_DST:
175 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
176
177 case MFF_IPV6_LABEL:
178 return !wc->masks.ipv6_label;
179
180 case MFF_IP_PROTO:
181 return !wc->masks.nw_proto;
182 case MFF_IP_DSCP:
183 case MFF_IP_DSCP_SHIFTED:
184 return !(wc->masks.nw_tos & IP_DSCP_MASK);
185 case MFF_IP_ECN:
186 return !(wc->masks.nw_tos & IP_ECN_MASK);
187 case MFF_IP_TTL:
188 return !wc->masks.nw_ttl;
189
190 case MFF_ND_TARGET:
191 return ipv6_mask_is_any(&wc->masks.nd_target);
192
193 case MFF_IP_FRAG:
194 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
195
196 case MFF_ARP_OP:
197 return !wc->masks.nw_proto;
198 case MFF_ARP_SPA:
199 return !wc->masks.nw_src;
200 case MFF_ARP_TPA:
201 return !wc->masks.nw_dst;
202
203 case MFF_TCP_SRC:
204 case MFF_UDP_SRC:
205 case MFF_SCTP_SRC:
206 case MFF_ICMPV4_TYPE:
207 case MFF_ICMPV6_TYPE:
208 return !wc->masks.tp_src;
209 case MFF_TCP_DST:
210 case MFF_UDP_DST:
211 case MFF_SCTP_DST:
212 case MFF_ICMPV4_CODE:
213 case MFF_ICMPV6_CODE:
214 return !wc->masks.tp_dst;
215 case MFF_TCP_FLAGS:
216 return !wc->masks.tcp_flags;
217
218 case MFF_N_IDS:
219 default:
220 OVS_NOT_REACHED();
221 }
222 }
223
224 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
225 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
226 * purposes, or to 0 if it is wildcarded.
227 *
228 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
229 * meets 'mf''s prerequisites. */
230 void
231 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
232 union mf_value *mask)
233 {
234 mf_get_value(mf, &wc->masks, mask);
235 }
236
237 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
238 * if the mask is valid, false otherwise. */
239 bool
240 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
241 {
242 switch (mf->maskable) {
243 case MFM_NONE:
244 return (is_all_zeros(mask, mf->n_bytes) ||
245 is_all_ones(mask, mf->n_bytes));
246
247 case MFM_FULLY:
248 return true;
249 }
250
251 OVS_NOT_REACHED();
252 }
253
254 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
255 bool
256 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
257 {
258 switch (mf->prereqs) {
259 case MFP_NONE:
260 return true;
261
262 case MFP_ARP:
263 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
264 flow->dl_type == htons(ETH_TYPE_RARP));
265 case MFP_IPV4:
266 return flow->dl_type == htons(ETH_TYPE_IP);
267 case MFP_IPV6:
268 return flow->dl_type == htons(ETH_TYPE_IPV6);
269 case MFP_VLAN_VID:
270 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
271 case MFP_MPLS:
272 return eth_type_mpls(flow->dl_type);
273 case MFP_IP_ANY:
274 return is_ip_any(flow);
275
276 case MFP_TCP:
277 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
278 case MFP_UDP:
279 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
280 case MFP_SCTP:
281 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP;
282 case MFP_ICMPV4:
283 return is_icmpv4(flow);
284 case MFP_ICMPV6:
285 return is_icmpv6(flow);
286
287 case MFP_ND:
288 return (is_icmpv6(flow)
289 && flow->tp_dst == htons(0)
290 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
291 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
292 case MFP_ND_SOLICIT:
293 return (is_icmpv6(flow)
294 && flow->tp_dst == htons(0)
295 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
296 case MFP_ND_ADVERT:
297 return (is_icmpv6(flow)
298 && flow->tp_dst == htons(0)
299 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
300 }
301
302 OVS_NOT_REACHED();
303 }
304
305 /* Set field and it's prerequisities in the mask.
306 * This is only ever called for writeable 'mf's, but we do not make the
307 * distinction here. */
308 void
309 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow *mask)
310 {
311 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
312
313 mf_set_flow_value(mf, &exact_match_mask, mask);
314
315 switch (mf->prereqs) {
316 case MFP_ND:
317 case MFP_ND_SOLICIT:
318 case MFP_ND_ADVERT:
319 mask->tp_src = OVS_BE16_MAX;
320 mask->tp_dst = OVS_BE16_MAX;
321 /* Fall through. */
322 case MFP_TCP:
323 case MFP_UDP:
324 case MFP_SCTP:
325 case MFP_ICMPV4:
326 case MFP_ICMPV6:
327 mask->nw_proto = 0xff;
328 /* Fall through. */
329 case MFP_ARP:
330 case MFP_IPV4:
331 case MFP_IPV6:
332 case MFP_MPLS:
333 case MFP_IP_ANY:
334 mask->dl_type = OVS_BE16_MAX;
335 break;
336 case MFP_VLAN_VID:
337 mask->vlan_tci |= htons(VLAN_CFI);
338 break;
339 case MFP_NONE:
340 break;
341 }
342 }
343
344
345 /* Returns true if 'value' may be a valid value *as part of a masked match*,
346 * false otherwise.
347 *
348 * A value is not rejected just because it is not valid for the field in
349 * question, but only if it doesn't make sense to test the bits in question at
350 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
351 * without the VLAN_CFI bit being set, but we can't reject those values because
352 * it is still legitimate to test just for those bits (see the documentation
353 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
354 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
355 bool
356 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
357 {
358 switch (mf->id) {
359 case MFF_DP_HASH:
360 case MFF_RECIRC_ID:
361 case MFF_TUN_ID:
362 case MFF_TUN_SRC:
363 case MFF_TUN_DST:
364 case MFF_TUN_TOS:
365 case MFF_TUN_TTL:
366 case MFF_TUN_FLAGS:
367 case MFF_METADATA:
368 case MFF_IN_PORT:
369 case MFF_SKB_PRIORITY:
370 case MFF_PKT_MARK:
371 CASE_MFF_REGS:
372 CASE_MFF_XREGS:
373 case MFF_ETH_SRC:
374 case MFF_ETH_DST:
375 case MFF_ETH_TYPE:
376 case MFF_VLAN_TCI:
377 case MFF_IPV4_SRC:
378 case MFF_IPV4_DST:
379 case MFF_IPV6_SRC:
380 case MFF_IPV6_DST:
381 case MFF_IP_PROTO:
382 case MFF_IP_TTL:
383 case MFF_ARP_SPA:
384 case MFF_ARP_TPA:
385 case MFF_ARP_SHA:
386 case MFF_ARP_THA:
387 case MFF_TCP_SRC:
388 case MFF_TCP_DST:
389 case MFF_UDP_SRC:
390 case MFF_UDP_DST:
391 case MFF_SCTP_SRC:
392 case MFF_SCTP_DST:
393 case MFF_ICMPV4_TYPE:
394 case MFF_ICMPV4_CODE:
395 case MFF_ICMPV6_TYPE:
396 case MFF_ICMPV6_CODE:
397 case MFF_ND_TARGET:
398 case MFF_ND_SLL:
399 case MFF_ND_TLL:
400 return true;
401
402 case MFF_IN_PORT_OXM:
403 case MFF_ACTSET_OUTPUT: {
404 ofp_port_t port;
405 return !ofputil_port_from_ofp11(value->be32, &port);
406 }
407
408 case MFF_IP_DSCP:
409 return !(value->u8 & ~IP_DSCP_MASK);
410 case MFF_IP_DSCP_SHIFTED:
411 return !(value->u8 & (~IP_DSCP_MASK >> 2));
412 case MFF_IP_ECN:
413 return !(value->u8 & ~IP_ECN_MASK);
414 case MFF_IP_FRAG:
415 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
416 case MFF_TCP_FLAGS:
417 return !(value->be16 & ~htons(0x0fff));
418
419 case MFF_ARP_OP:
420 return !(value->be16 & htons(0xff00));
421
422 case MFF_DL_VLAN:
423 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
424 case MFF_VLAN_VID:
425 return !(value->be16 & htons(VLAN_PCP_MASK));
426
427 case MFF_DL_VLAN_PCP:
428 case MFF_VLAN_PCP:
429 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
430
431 case MFF_IPV6_LABEL:
432 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
433
434 case MFF_MPLS_LABEL:
435 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
436
437 case MFF_MPLS_TC:
438 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
439
440 case MFF_MPLS_BOS:
441 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
442
443 case MFF_N_IDS:
444 default:
445 OVS_NOT_REACHED();
446 }
447 }
448
449 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
450 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
451 void
452 mf_get_value(const struct mf_field *mf, const struct flow *flow,
453 union mf_value *value)
454 {
455 switch (mf->id) {
456 case MFF_DP_HASH:
457 value->be32 = htonl(flow->dp_hash);
458 break;
459 case MFF_RECIRC_ID:
460 value->be32 = htonl(flow->recirc_id);
461 break;
462 case MFF_TUN_ID:
463 value->be64 = flow->tunnel.tun_id;
464 break;
465 case MFF_TUN_SRC:
466 value->be32 = flow->tunnel.ip_src;
467 break;
468 case MFF_TUN_DST:
469 value->be32 = flow->tunnel.ip_dst;
470 break;
471 case MFF_TUN_FLAGS:
472 value->be16 = htons(flow->tunnel.flags);
473 break;
474 case MFF_TUN_TTL:
475 value->u8 = flow->tunnel.ip_ttl;
476 break;
477 case MFF_TUN_TOS:
478 value->u8 = flow->tunnel.ip_tos;
479 break;
480
481 case MFF_METADATA:
482 value->be64 = flow->metadata;
483 break;
484
485 case MFF_IN_PORT:
486 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
487 break;
488 case MFF_IN_PORT_OXM:
489 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
490 break;
491 case MFF_ACTSET_OUTPUT:
492 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
493 break;
494
495 case MFF_SKB_PRIORITY:
496 value->be32 = htonl(flow->skb_priority);
497 break;
498
499 case MFF_PKT_MARK:
500 value->be32 = htonl(flow->pkt_mark);
501 break;
502
503 CASE_MFF_REGS:
504 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
505 break;
506
507 CASE_MFF_XREGS:
508 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
509 break;
510
511 case MFF_ETH_SRC:
512 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
513 break;
514
515 case MFF_ETH_DST:
516 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
517 break;
518
519 case MFF_ETH_TYPE:
520 value->be16 = flow->dl_type;
521 break;
522
523 case MFF_VLAN_TCI:
524 value->be16 = flow->vlan_tci;
525 break;
526
527 case MFF_DL_VLAN:
528 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
529 break;
530 case MFF_VLAN_VID:
531 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
532 break;
533
534 case MFF_DL_VLAN_PCP:
535 case MFF_VLAN_PCP:
536 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
537 break;
538
539 case MFF_MPLS_LABEL:
540 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
541 break;
542
543 case MFF_MPLS_TC:
544 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
545 break;
546
547 case MFF_MPLS_BOS:
548 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
549 break;
550
551 case MFF_IPV4_SRC:
552 value->be32 = flow->nw_src;
553 break;
554
555 case MFF_IPV4_DST:
556 value->be32 = flow->nw_dst;
557 break;
558
559 case MFF_IPV6_SRC:
560 value->ipv6 = flow->ipv6_src;
561 break;
562
563 case MFF_IPV6_DST:
564 value->ipv6 = flow->ipv6_dst;
565 break;
566
567 case MFF_IPV6_LABEL:
568 value->be32 = flow->ipv6_label;
569 break;
570
571 case MFF_IP_PROTO:
572 value->u8 = flow->nw_proto;
573 break;
574
575 case MFF_IP_DSCP:
576 value->u8 = flow->nw_tos & IP_DSCP_MASK;
577 break;
578
579 case MFF_IP_DSCP_SHIFTED:
580 value->u8 = flow->nw_tos >> 2;
581 break;
582
583 case MFF_IP_ECN:
584 value->u8 = flow->nw_tos & IP_ECN_MASK;
585 break;
586
587 case MFF_IP_TTL:
588 value->u8 = flow->nw_ttl;
589 break;
590
591 case MFF_IP_FRAG:
592 value->u8 = flow->nw_frag;
593 break;
594
595 case MFF_ARP_OP:
596 value->be16 = htons(flow->nw_proto);
597 break;
598
599 case MFF_ARP_SPA:
600 value->be32 = flow->nw_src;
601 break;
602
603 case MFF_ARP_TPA:
604 value->be32 = flow->nw_dst;
605 break;
606
607 case MFF_ARP_SHA:
608 case MFF_ND_SLL:
609 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
610 break;
611
612 case MFF_ARP_THA:
613 case MFF_ND_TLL:
614 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
615 break;
616
617 case MFF_TCP_SRC:
618 case MFF_UDP_SRC:
619 case MFF_SCTP_SRC:
620 value->be16 = flow->tp_src;
621 break;
622
623 case MFF_TCP_DST:
624 case MFF_UDP_DST:
625 case MFF_SCTP_DST:
626 value->be16 = flow->tp_dst;
627 break;
628
629 case MFF_TCP_FLAGS:
630 value->be16 = flow->tcp_flags;
631 break;
632
633 case MFF_ICMPV4_TYPE:
634 case MFF_ICMPV6_TYPE:
635 value->u8 = ntohs(flow->tp_src);
636 break;
637
638 case MFF_ICMPV4_CODE:
639 case MFF_ICMPV6_CODE:
640 value->u8 = ntohs(flow->tp_dst);
641 break;
642
643 case MFF_ND_TARGET:
644 value->ipv6 = flow->nd_target;
645 break;
646
647 case MFF_N_IDS:
648 default:
649 OVS_NOT_REACHED();
650 }
651 }
652
653 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
654 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
655 * prerequisites. */
656 void
657 mf_set_value(const struct mf_field *mf,
658 const union mf_value *value, struct match *match)
659 {
660 switch (mf->id) {
661 case MFF_DP_HASH:
662 match_set_dp_hash(match, ntohl(value->be32));
663 break;
664 case MFF_RECIRC_ID:
665 match_set_recirc_id(match, ntohl(value->be32));
666 break;
667 case MFF_TUN_ID:
668 match_set_tun_id(match, value->be64);
669 break;
670 case MFF_TUN_SRC:
671 match_set_tun_src(match, value->be32);
672 break;
673 case MFF_TUN_DST:
674 match_set_tun_dst(match, value->be32);
675 break;
676 case MFF_TUN_FLAGS:
677 match_set_tun_flags(match, ntohs(value->be16));
678 break;
679 case MFF_TUN_TOS:
680 match_set_tun_tos(match, value->u8);
681 break;
682 case MFF_TUN_TTL:
683 match_set_tun_ttl(match, value->u8);
684 break;
685
686 case MFF_METADATA:
687 match_set_metadata(match, value->be64);
688 break;
689
690 case MFF_IN_PORT:
691 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
692 break;
693
694 case MFF_IN_PORT_OXM: {
695 ofp_port_t port;
696 ofputil_port_from_ofp11(value->be32, &port);
697 match_set_in_port(match, port);
698 break;
699 }
700 case MFF_ACTSET_OUTPUT: {
701 ofp_port_t port;
702 ofputil_port_from_ofp11(value->be32, &port);
703 match_set_actset_output(match, port);
704 break;
705 }
706
707 case MFF_SKB_PRIORITY:
708 match_set_skb_priority(match, ntohl(value->be32));
709 break;
710
711 case MFF_PKT_MARK:
712 match_set_pkt_mark(match, ntohl(value->be32));
713 break;
714
715 CASE_MFF_REGS:
716 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
717 break;
718
719 CASE_MFF_XREGS:
720 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
721 break;
722
723 case MFF_ETH_SRC:
724 match_set_dl_src(match, value->mac);
725 break;
726
727 case MFF_ETH_DST:
728 match_set_dl_dst(match, value->mac);
729 break;
730
731 case MFF_ETH_TYPE:
732 match_set_dl_type(match, value->be16);
733 break;
734
735 case MFF_VLAN_TCI:
736 match_set_dl_tci(match, value->be16);
737 break;
738
739 case MFF_DL_VLAN:
740 match_set_dl_vlan(match, value->be16);
741 break;
742 case MFF_VLAN_VID:
743 match_set_vlan_vid(match, value->be16);
744 break;
745
746 case MFF_DL_VLAN_PCP:
747 case MFF_VLAN_PCP:
748 match_set_dl_vlan_pcp(match, value->u8);
749 break;
750
751 case MFF_MPLS_LABEL:
752 match_set_mpls_label(match, 0, value->be32);
753 break;
754
755 case MFF_MPLS_TC:
756 match_set_mpls_tc(match, 0, value->u8);
757 break;
758
759 case MFF_MPLS_BOS:
760 match_set_mpls_bos(match, 0, value->u8);
761 break;
762
763 case MFF_IPV4_SRC:
764 match_set_nw_src(match, value->be32);
765 break;
766
767 case MFF_IPV4_DST:
768 match_set_nw_dst(match, value->be32);
769 break;
770
771 case MFF_IPV6_SRC:
772 match_set_ipv6_src(match, &value->ipv6);
773 break;
774
775 case MFF_IPV6_DST:
776 match_set_ipv6_dst(match, &value->ipv6);
777 break;
778
779 case MFF_IPV6_LABEL:
780 match_set_ipv6_label(match, value->be32);
781 break;
782
783 case MFF_IP_PROTO:
784 match_set_nw_proto(match, value->u8);
785 break;
786
787 case MFF_IP_DSCP:
788 match_set_nw_dscp(match, value->u8);
789 break;
790
791 case MFF_IP_DSCP_SHIFTED:
792 match_set_nw_dscp(match, value->u8 << 2);
793 break;
794
795 case MFF_IP_ECN:
796 match_set_nw_ecn(match, value->u8);
797 break;
798
799 case MFF_IP_TTL:
800 match_set_nw_ttl(match, value->u8);
801 break;
802
803 case MFF_IP_FRAG:
804 match_set_nw_frag(match, value->u8);
805 break;
806
807 case MFF_ARP_OP:
808 match_set_nw_proto(match, ntohs(value->be16));
809 break;
810
811 case MFF_ARP_SPA:
812 match_set_nw_src(match, value->be32);
813 break;
814
815 case MFF_ARP_TPA:
816 match_set_nw_dst(match, value->be32);
817 break;
818
819 case MFF_ARP_SHA:
820 case MFF_ND_SLL:
821 match_set_arp_sha(match, value->mac);
822 break;
823
824 case MFF_ARP_THA:
825 case MFF_ND_TLL:
826 match_set_arp_tha(match, value->mac);
827 break;
828
829 case MFF_TCP_SRC:
830 case MFF_UDP_SRC:
831 case MFF_SCTP_SRC:
832 match_set_tp_src(match, value->be16);
833 break;
834
835 case MFF_TCP_DST:
836 case MFF_UDP_DST:
837 case MFF_SCTP_DST:
838 match_set_tp_dst(match, value->be16);
839 break;
840
841 case MFF_TCP_FLAGS:
842 match_set_tcp_flags(match, value->be16);
843 break;
844
845 case MFF_ICMPV4_TYPE:
846 case MFF_ICMPV6_TYPE:
847 match_set_icmp_type(match, value->u8);
848 break;
849
850 case MFF_ICMPV4_CODE:
851 case MFF_ICMPV6_CODE:
852 match_set_icmp_code(match, value->u8);
853 break;
854
855 case MFF_ND_TARGET:
856 match_set_nd_target(match, &value->ipv6);
857 break;
858
859 case MFF_N_IDS:
860 default:
861 OVS_NOT_REACHED();
862 }
863 }
864
865 /* Unwildcard 'mask' member field described by 'mf'. The caller is
866 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
867 void
868 mf_mask_field(const struct mf_field *mf, struct flow *mask)
869 {
870 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
871
872 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
873 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
874 * special case. For the rest, calling mf_set_flow_value() is good
875 * enough. */
876 if (mf->id == MFF_DL_VLAN) {
877 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
878 } else {
879 mf_set_flow_value(mf, &exact_match_mask, mask);
880 }
881 }
882
883 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
884 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
885 void
886 mf_set_flow_value(const struct mf_field *mf,
887 const union mf_value *value, struct flow *flow)
888 {
889 switch (mf->id) {
890 case MFF_DP_HASH:
891 flow->dp_hash = ntohl(value->be32);
892 break;
893 case MFF_RECIRC_ID:
894 flow->recirc_id = ntohl(value->be32);
895 break;
896 case MFF_TUN_ID:
897 flow->tunnel.tun_id = value->be64;
898 break;
899 case MFF_TUN_SRC:
900 flow->tunnel.ip_src = value->be32;
901 break;
902 case MFF_TUN_DST:
903 flow->tunnel.ip_dst = value->be32;
904 break;
905 case MFF_TUN_FLAGS:
906 flow->tunnel.flags = ntohs(value->be16);
907 break;
908 case MFF_TUN_TOS:
909 flow->tunnel.ip_tos = value->u8;
910 break;
911 case MFF_TUN_TTL:
912 flow->tunnel.ip_ttl = value->u8;
913 break;
914
915 case MFF_METADATA:
916 flow->metadata = value->be64;
917 break;
918
919 case MFF_IN_PORT:
920 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
921 break;
922
923 case MFF_IN_PORT_OXM:
924 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
925 break;
926 case MFF_ACTSET_OUTPUT:
927 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
928 break;
929
930 case MFF_SKB_PRIORITY:
931 flow->skb_priority = ntohl(value->be32);
932 break;
933
934 case MFF_PKT_MARK:
935 flow->pkt_mark = ntohl(value->be32);
936 break;
937
938 CASE_MFF_REGS:
939 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
940 break;
941
942 CASE_MFF_XREGS:
943 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
944 break;
945
946 case MFF_ETH_SRC:
947 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
948 break;
949
950 case MFF_ETH_DST:
951 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
952 break;
953
954 case MFF_ETH_TYPE:
955 flow->dl_type = value->be16;
956 break;
957
958 case MFF_VLAN_TCI:
959 flow->vlan_tci = value->be16;
960 break;
961
962 case MFF_DL_VLAN:
963 flow_set_dl_vlan(flow, value->be16);
964 break;
965 case MFF_VLAN_VID:
966 flow_set_vlan_vid(flow, value->be16);
967 break;
968
969 case MFF_DL_VLAN_PCP:
970 case MFF_VLAN_PCP:
971 flow_set_vlan_pcp(flow, value->u8);
972 break;
973
974 case MFF_MPLS_LABEL:
975 flow_set_mpls_label(flow, 0, value->be32);
976 break;
977
978 case MFF_MPLS_TC:
979 flow_set_mpls_tc(flow, 0, value->u8);
980 break;
981
982 case MFF_MPLS_BOS:
983 flow_set_mpls_bos(flow, 0, value->u8);
984 break;
985
986 case MFF_IPV4_SRC:
987 flow->nw_src = value->be32;
988 break;
989
990 case MFF_IPV4_DST:
991 flow->nw_dst = value->be32;
992 break;
993
994 case MFF_IPV6_SRC:
995 flow->ipv6_src = value->ipv6;
996 break;
997
998 case MFF_IPV6_DST:
999 flow->ipv6_dst = value->ipv6;
1000 break;
1001
1002 case MFF_IPV6_LABEL:
1003 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1004 break;
1005
1006 case MFF_IP_PROTO:
1007 flow->nw_proto = value->u8;
1008 break;
1009
1010 case MFF_IP_DSCP:
1011 flow->nw_tos &= ~IP_DSCP_MASK;
1012 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1013 break;
1014
1015 case MFF_IP_DSCP_SHIFTED:
1016 flow->nw_tos &= ~IP_DSCP_MASK;
1017 flow->nw_tos |= value->u8 << 2;
1018 break;
1019
1020 case MFF_IP_ECN:
1021 flow->nw_tos &= ~IP_ECN_MASK;
1022 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1023 break;
1024
1025 case MFF_IP_TTL:
1026 flow->nw_ttl = value->u8;
1027 break;
1028
1029 case MFF_IP_FRAG:
1030 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1031 break;
1032
1033 case MFF_ARP_OP:
1034 flow->nw_proto = ntohs(value->be16);
1035 break;
1036
1037 case MFF_ARP_SPA:
1038 flow->nw_src = value->be32;
1039 break;
1040
1041 case MFF_ARP_TPA:
1042 flow->nw_dst = value->be32;
1043 break;
1044
1045 case MFF_ARP_SHA:
1046 case MFF_ND_SLL:
1047 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1048 break;
1049
1050 case MFF_ARP_THA:
1051 case MFF_ND_TLL:
1052 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1053 break;
1054
1055 case MFF_TCP_SRC:
1056 case MFF_UDP_SRC:
1057 case MFF_SCTP_SRC:
1058 flow->tp_src = value->be16;
1059 break;
1060
1061 case MFF_TCP_DST:
1062 case MFF_UDP_DST:
1063 case MFF_SCTP_DST:
1064 flow->tp_dst = value->be16;
1065 break;
1066
1067 case MFF_TCP_FLAGS:
1068 flow->tcp_flags = value->be16;
1069 break;
1070
1071 case MFF_ICMPV4_TYPE:
1072 case MFF_ICMPV6_TYPE:
1073 flow->tp_src = htons(value->u8);
1074 break;
1075
1076 case MFF_ICMPV4_CODE:
1077 case MFF_ICMPV6_CODE:
1078 flow->tp_dst = htons(value->u8);
1079 break;
1080
1081 case MFF_ND_TARGET:
1082 flow->nd_target = value->ipv6;
1083 break;
1084
1085 case MFF_N_IDS:
1086 default:
1087 OVS_NOT_REACHED();
1088 }
1089 }
1090
1091 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1092 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1093 * src[i]. */
1094 static void
1095 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1096 {
1097 size_t i;
1098
1099 for (i = 0; i < n; i++) {
1100 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1101 }
1102 }
1103
1104 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1105 * for which 'mask' has a 0-bit keep their existing values. The caller is
1106 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1107 void
1108 mf_set_flow_value_masked(const struct mf_field *field,
1109 const union mf_value *value,
1110 const union mf_value *mask,
1111 struct flow *flow)
1112 {
1113 union mf_value tmp;
1114
1115 mf_get_value(field, flow, &tmp);
1116 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1117 (uint8_t *) &tmp, field->n_bytes);
1118 mf_set_flow_value(field, &tmp, flow);
1119 }
1120
1121 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1122 *
1123 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1124 * prerequisites. */
1125 bool
1126 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1127 {
1128 union mf_value value;
1129
1130 mf_get_value(mf, flow, &value);
1131 return is_all_zeros(&value, mf->n_bytes);
1132 }
1133
1134 /* Makes 'match' wildcard field 'mf'.
1135 *
1136 * The caller is responsible for ensuring that 'match' meets 'mf''s
1137 * prerequisites. */
1138 void
1139 mf_set_wild(const struct mf_field *mf, struct match *match)
1140 {
1141 switch (mf->id) {
1142 case MFF_DP_HASH:
1143 match->flow.dp_hash = 0;
1144 match->wc.masks.dp_hash = 0;
1145 break;
1146 case MFF_RECIRC_ID:
1147 match->flow.recirc_id = 0;
1148 match->wc.masks.recirc_id = 0;
1149 break;
1150 case MFF_TUN_ID:
1151 match_set_tun_id_masked(match, htonll(0), htonll(0));
1152 break;
1153 case MFF_TUN_SRC:
1154 match_set_tun_src_masked(match, htonl(0), htonl(0));
1155 break;
1156 case MFF_TUN_DST:
1157 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1158 break;
1159 case MFF_TUN_FLAGS:
1160 match_set_tun_flags_masked(match, 0, 0);
1161 break;
1162 case MFF_TUN_TOS:
1163 match_set_tun_tos_masked(match, 0, 0);
1164 break;
1165 case MFF_TUN_TTL:
1166 match_set_tun_ttl_masked(match, 0, 0);
1167 break;
1168
1169 case MFF_METADATA:
1170 match_set_metadata_masked(match, htonll(0), htonll(0));
1171 break;
1172
1173 case MFF_IN_PORT:
1174 case MFF_IN_PORT_OXM:
1175 match->flow.in_port.ofp_port = 0;
1176 match->wc.masks.in_port.ofp_port = 0;
1177 break;
1178 case MFF_ACTSET_OUTPUT:
1179 match->flow.actset_output = 0;
1180 match->wc.masks.actset_output = 0;
1181 break;
1182
1183 case MFF_SKB_PRIORITY:
1184 match->flow.skb_priority = 0;
1185 match->wc.masks.skb_priority = 0;
1186 break;
1187
1188 case MFF_PKT_MARK:
1189 match->flow.pkt_mark = 0;
1190 match->wc.masks.pkt_mark = 0;
1191 break;
1192
1193 CASE_MFF_REGS:
1194 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1195 break;
1196
1197 CASE_MFF_XREGS:
1198 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1199 break;
1200
1201 case MFF_ETH_SRC:
1202 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1203 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1204 break;
1205
1206 case MFF_ETH_DST:
1207 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1208 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1209 break;
1210
1211 case MFF_ETH_TYPE:
1212 match->flow.dl_type = htons(0);
1213 match->wc.masks.dl_type = htons(0);
1214 break;
1215
1216 case MFF_VLAN_TCI:
1217 match_set_dl_tci_masked(match, htons(0), htons(0));
1218 break;
1219
1220 case MFF_DL_VLAN:
1221 case MFF_VLAN_VID:
1222 match_set_any_vid(match);
1223 break;
1224
1225 case MFF_DL_VLAN_PCP:
1226 case MFF_VLAN_PCP:
1227 match_set_any_pcp(match);
1228 break;
1229
1230 case MFF_MPLS_LABEL:
1231 match_set_any_mpls_label(match, 0);
1232 break;
1233
1234 case MFF_MPLS_TC:
1235 match_set_any_mpls_tc(match, 0);
1236 break;
1237
1238 case MFF_MPLS_BOS:
1239 match_set_any_mpls_bos(match, 0);
1240 break;
1241
1242 case MFF_IPV4_SRC:
1243 case MFF_ARP_SPA:
1244 match_set_nw_src_masked(match, htonl(0), htonl(0));
1245 break;
1246
1247 case MFF_IPV4_DST:
1248 case MFF_ARP_TPA:
1249 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1250 break;
1251
1252 case MFF_IPV6_SRC:
1253 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1254 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1255 break;
1256
1257 case MFF_IPV6_DST:
1258 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1259 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1260 break;
1261
1262 case MFF_IPV6_LABEL:
1263 match->wc.masks.ipv6_label = htonl(0);
1264 match->flow.ipv6_label = htonl(0);
1265 break;
1266
1267 case MFF_IP_PROTO:
1268 match->wc.masks.nw_proto = 0;
1269 match->flow.nw_proto = 0;
1270 break;
1271
1272 case MFF_IP_DSCP:
1273 case MFF_IP_DSCP_SHIFTED:
1274 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1275 match->flow.nw_tos &= ~IP_DSCP_MASK;
1276 break;
1277
1278 case MFF_IP_ECN:
1279 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1280 match->flow.nw_tos &= ~IP_ECN_MASK;
1281 break;
1282
1283 case MFF_IP_TTL:
1284 match->wc.masks.nw_ttl = 0;
1285 match->flow.nw_ttl = 0;
1286 break;
1287
1288 case MFF_IP_FRAG:
1289 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1290 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1291 break;
1292
1293 case MFF_ARP_OP:
1294 match->wc.masks.nw_proto = 0;
1295 match->flow.nw_proto = 0;
1296 break;
1297
1298 case MFF_ARP_SHA:
1299 case MFF_ND_SLL:
1300 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1301 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1302 break;
1303
1304 case MFF_ARP_THA:
1305 case MFF_ND_TLL:
1306 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1307 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1308 break;
1309
1310 case MFF_TCP_SRC:
1311 case MFF_UDP_SRC:
1312 case MFF_SCTP_SRC:
1313 case MFF_ICMPV4_TYPE:
1314 case MFF_ICMPV6_TYPE:
1315 match->wc.masks.tp_src = htons(0);
1316 match->flow.tp_src = htons(0);
1317 break;
1318
1319 case MFF_TCP_DST:
1320 case MFF_UDP_DST:
1321 case MFF_SCTP_DST:
1322 case MFF_ICMPV4_CODE:
1323 case MFF_ICMPV6_CODE:
1324 match->wc.masks.tp_dst = htons(0);
1325 match->flow.tp_dst = htons(0);
1326 break;
1327
1328 case MFF_TCP_FLAGS:
1329 match->wc.masks.tcp_flags = htons(0);
1330 match->flow.tcp_flags = htons(0);
1331 break;
1332
1333 case MFF_ND_TARGET:
1334 memset(&match->wc.masks.nd_target, 0,
1335 sizeof match->wc.masks.nd_target);
1336 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1337 break;
1338
1339 case MFF_N_IDS:
1340 default:
1341 OVS_NOT_REACHED();
1342 }
1343 }
1344
1345 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1346 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1347 * with a 1-bit indicating that the corresponding value bit must match and a
1348 * 0-bit indicating a don't-care.
1349 *
1350 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1351 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1352 * call is equivalent to mf_set_wild(mf, match).
1353 *
1354 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1355 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1356 enum ofputil_protocol
1357 mf_set(const struct mf_field *mf,
1358 const union mf_value *value, const union mf_value *mask,
1359 struct match *match)
1360 {
1361 if (!mask || is_all_ones(mask, mf->n_bytes)) {
1362 mf_set_value(mf, value, match);
1363 return mf->usable_protocols_exact;
1364 } else if (is_all_zeros(mask, mf->n_bytes)) {
1365 mf_set_wild(mf, match);
1366 return OFPUTIL_P_ANY;
1367 }
1368
1369 switch (mf->id) {
1370 case MFF_RECIRC_ID:
1371 case MFF_IN_PORT:
1372 case MFF_IN_PORT_OXM:
1373 case MFF_ACTSET_OUTPUT:
1374 case MFF_SKB_PRIORITY:
1375 case MFF_ETH_TYPE:
1376 case MFF_DL_VLAN:
1377 case MFF_DL_VLAN_PCP:
1378 case MFF_VLAN_PCP:
1379 case MFF_MPLS_LABEL:
1380 case MFF_MPLS_TC:
1381 case MFF_MPLS_BOS:
1382 case MFF_IP_PROTO:
1383 case MFF_IP_TTL:
1384 case MFF_IP_DSCP:
1385 case MFF_IP_DSCP_SHIFTED:
1386 case MFF_IP_ECN:
1387 case MFF_ARP_OP:
1388 case MFF_ICMPV4_TYPE:
1389 case MFF_ICMPV4_CODE:
1390 case MFF_ICMPV6_TYPE:
1391 case MFF_ICMPV6_CODE:
1392 return OFPUTIL_P_NONE;
1393
1394 case MFF_DP_HASH:
1395 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1396 break;
1397 case MFF_TUN_ID:
1398 match_set_tun_id_masked(match, value->be64, mask->be64);
1399 break;
1400 case MFF_TUN_SRC:
1401 match_set_tun_src_masked(match, value->be32, mask->be32);
1402 break;
1403 case MFF_TUN_DST:
1404 match_set_tun_dst_masked(match, value->be32, mask->be32);
1405 break;
1406 case MFF_TUN_FLAGS:
1407 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1408 break;
1409 case MFF_TUN_TTL:
1410 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1411 break;
1412 case MFF_TUN_TOS:
1413 match_set_tun_tos_masked(match, value->u8, mask->u8);
1414 break;
1415
1416 case MFF_METADATA:
1417 match_set_metadata_masked(match, value->be64, mask->be64);
1418 break;
1419
1420 CASE_MFF_REGS:
1421 match_set_reg_masked(match, mf->id - MFF_REG0,
1422 ntohl(value->be32), ntohl(mask->be32));
1423 break;
1424
1425 CASE_MFF_XREGS:
1426 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1427 ntohll(value->be64), ntohll(mask->be64));
1428 break;
1429
1430 case MFF_PKT_MARK:
1431 match_set_pkt_mark_masked(match, ntohl(value->be32),
1432 ntohl(mask->be32));
1433 break;
1434
1435 case MFF_ETH_DST:
1436 match_set_dl_dst_masked(match, value->mac, mask->mac);
1437 break;
1438
1439 case MFF_ETH_SRC:
1440 match_set_dl_src_masked(match, value->mac, mask->mac);
1441 break;
1442
1443 case MFF_ARP_SHA:
1444 case MFF_ND_SLL:
1445 match_set_arp_sha_masked(match, value->mac, mask->mac);
1446 break;
1447
1448 case MFF_ARP_THA:
1449 case MFF_ND_TLL:
1450 match_set_arp_tha_masked(match, value->mac, mask->mac);
1451 break;
1452
1453 case MFF_VLAN_TCI:
1454 match_set_dl_tci_masked(match, value->be16, mask->be16);
1455 break;
1456
1457 case MFF_VLAN_VID:
1458 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1459 break;
1460
1461 case MFF_IPV4_SRC:
1462 match_set_nw_src_masked(match, value->be32, mask->be32);
1463 break;
1464
1465 case MFF_IPV4_DST:
1466 match_set_nw_dst_masked(match, value->be32, mask->be32);
1467 break;
1468
1469 case MFF_IPV6_SRC:
1470 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1471 break;
1472
1473 case MFF_IPV6_DST:
1474 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1475 break;
1476
1477 case MFF_IPV6_LABEL:
1478 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1479 mf_set_value(mf, value, match);
1480 } else {
1481 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1482 }
1483 break;
1484
1485 case MFF_ND_TARGET:
1486 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1487 break;
1488
1489 case MFF_IP_FRAG:
1490 match_set_nw_frag_masked(match, value->u8, mask->u8);
1491 break;
1492
1493 case MFF_ARP_SPA:
1494 match_set_nw_src_masked(match, value->be32, mask->be32);
1495 break;
1496
1497 case MFF_ARP_TPA:
1498 match_set_nw_dst_masked(match, value->be32, mask->be32);
1499 break;
1500
1501 case MFF_TCP_SRC:
1502 case MFF_UDP_SRC:
1503 case MFF_SCTP_SRC:
1504 match_set_tp_src_masked(match, value->be16, mask->be16);
1505 break;
1506
1507 case MFF_TCP_DST:
1508 case MFF_UDP_DST:
1509 case MFF_SCTP_DST:
1510 match_set_tp_dst_masked(match, value->be16, mask->be16);
1511 break;
1512
1513 case MFF_TCP_FLAGS:
1514 match_set_tcp_flags_masked(match, value->be16, mask->be16);
1515 break;
1516
1517 case MFF_N_IDS:
1518 default:
1519 OVS_NOT_REACHED();
1520 }
1521
1522 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1523 || ip_is_cidr(mask->be32))
1524 ? mf->usable_protocols_cidr
1525 : mf->usable_protocols_bitwise);
1526 }
1527
1528 static enum ofperr
1529 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1530 const char *type)
1531 {
1532 if (!sf->field) {
1533 VLOG_WARN_RL(&rl, "unknown %s field", type);
1534 return OFPERR_OFPBAC_BAD_SET_TYPE;
1535 } else if (!sf->n_bits) {
1536 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1537 return OFPERR_OFPBAC_BAD_SET_LEN;
1538 } else if (sf->ofs >= sf->field->n_bits) {
1539 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1540 sf->ofs, sf->field->n_bits, type, sf->field->name);
1541 return OFPERR_OFPBAC_BAD_SET_LEN;
1542 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1543 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1544 "of %s field %s", sf->ofs, sf->n_bits,
1545 sf->field->n_bits, type, sf->field->name);
1546 return OFPERR_OFPBAC_BAD_SET_LEN;
1547 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1548 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1549 type, sf->field->name);
1550 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1551 } else {
1552 return 0;
1553 }
1554 }
1555
1556 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1557 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1558 * ofp_mkerr()). */
1559 enum ofperr
1560 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1561 {
1562 return mf_check__(sf, flow, "source");
1563 }
1564
1565 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1566 * if so, otherwise an OpenFlow error code (e.g. as returned by
1567 * ofp_mkerr()). */
1568 enum ofperr
1569 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1570 {
1571 int error = mf_check__(sf, flow, "destination");
1572 if (!error && !sf->field->writable) {
1573 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1574 sf->field->name);
1575 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1576 }
1577 return error;
1578 }
1579
1580 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1581 * 'value' and 'mask', respectively. */
1582 void
1583 mf_get(const struct mf_field *mf, const struct match *match,
1584 union mf_value *value, union mf_value *mask)
1585 {
1586 mf_get_value(mf, &match->flow, value);
1587 mf_get_mask(mf, &match->wc, mask);
1588 }
1589
1590 static char *
1591 mf_from_integer_string(const struct mf_field *mf, const char *s,
1592 uint8_t *valuep, uint8_t *maskp)
1593 {
1594 unsigned long long int integer, mask;
1595 char *tail;
1596 int i;
1597
1598 errno = 0;
1599 integer = strtoull(s, &tail, 0);
1600 if (errno || (*tail != '\0' && *tail != '/')) {
1601 goto syntax_error;
1602 }
1603
1604 if (*tail == '/') {
1605 mask = strtoull(tail + 1, &tail, 0);
1606 if (errno || *tail != '\0') {
1607 goto syntax_error;
1608 }
1609 } else {
1610 mask = ULLONG_MAX;
1611 }
1612
1613 for (i = mf->n_bytes - 1; i >= 0; i--) {
1614 valuep[i] = integer;
1615 maskp[i] = mask;
1616 integer >>= 8;
1617 mask >>= 8;
1618 }
1619 if (integer) {
1620 return xasprintf("%s: value too large for %u-byte field %s",
1621 s, mf->n_bytes, mf->name);
1622 }
1623 return NULL;
1624
1625 syntax_error:
1626 return xasprintf("%s: bad syntax for %s", s, mf->name);
1627 }
1628
1629 static char *
1630 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1631 uint8_t mac[ETH_ADDR_LEN],
1632 uint8_t mask[ETH_ADDR_LEN])
1633 {
1634 int n;
1635
1636 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
1637
1638 n = -1;
1639 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n)
1640 && n == strlen(s)) {
1641 memset(mask, 0xff, ETH_ADDR_LEN);
1642 return NULL;
1643 }
1644
1645 n = -1;
1646 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
1647 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n)
1648 && n == strlen(s)) {
1649 return NULL;
1650 }
1651
1652 return xasprintf("%s: invalid Ethernet address", s);
1653 }
1654
1655 static char *
1656 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1657 ovs_be32 *ip, ovs_be32 *mask)
1658 {
1659 int prefix;
1660
1661 ovs_assert(mf->n_bytes == sizeof *ip);
1662
1663 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1664 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
1665 /* OK. */
1666 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
1667 if (prefix <= 0 || prefix > 32) {
1668 return xasprintf("%s: network prefix bits not between 1 and "
1669 "32", s);
1670 } else if (prefix == 32) {
1671 *mask = OVS_BE32_MAX;
1672 } else {
1673 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1674 }
1675 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
1676 *mask = OVS_BE32_MAX;
1677 } else {
1678 return xasprintf("%s: invalid IP address", s);
1679 }
1680 return NULL;
1681 }
1682
1683 static char *
1684 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1685 struct in6_addr *value, struct in6_addr *mask)
1686 {
1687 char *str = xstrdup(s);
1688 char *save_ptr = NULL;
1689 const char *name, *netmask;
1690 int retval;
1691
1692 ovs_assert(mf->n_bytes == sizeof *value);
1693
1694 name = strtok_r(str, "/", &save_ptr);
1695 retval = name ? lookup_ipv6(name, value) : EINVAL;
1696 if (retval) {
1697 char *err;
1698
1699 err = xasprintf("%s: could not convert to IPv6 address", str);
1700 free(str);
1701
1702 return err;
1703 }
1704
1705 netmask = strtok_r(NULL, "/", &save_ptr);
1706 if (netmask) {
1707 if (inet_pton(AF_INET6, netmask, mask) != 1) {
1708 int prefix = atoi(netmask);
1709 if (prefix <= 0 || prefix > 128) {
1710 free(str);
1711 return xasprintf("%s: prefix bits not between 1 and 128", s);
1712 } else {
1713 *mask = ipv6_create_mask(prefix);
1714 }
1715 }
1716 } else {
1717 *mask = in6addr_exact;
1718 }
1719 free(str);
1720
1721 return NULL;
1722 }
1723
1724 static char *
1725 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1726 ovs_be16 *valuep, ovs_be16 *maskp)
1727 {
1728 ofp_port_t port;
1729
1730 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1731
1732 if (ofputil_port_from_string(s, &port)) {
1733 *valuep = htons(ofp_to_u16(port));
1734 *maskp = OVS_BE16_MAX;
1735 return NULL;
1736 }
1737 return xasprintf("%s: port value out of range for %s", s, mf->name);
1738 }
1739
1740 static char *
1741 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
1742 ovs_be32 *valuep, ovs_be32 *maskp)
1743 {
1744 ofp_port_t port;
1745
1746 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
1747 if (ofputil_port_from_string(s, &port)) {
1748 *valuep = ofputil_port_to_ofp11(port);
1749 *maskp = OVS_BE32_MAX;
1750 return NULL;
1751 }
1752 return xasprintf("%s: port value out of range for %s", s, mf->name);
1753 }
1754
1755 struct frag_handling {
1756 const char *name;
1757 uint8_t mask;
1758 uint8_t value;
1759 };
1760
1761 static const struct frag_handling all_frags[] = {
1762 #define A FLOW_NW_FRAG_ANY
1763 #define L FLOW_NW_FRAG_LATER
1764 /* name mask value */
1765
1766 { "no", A|L, 0 },
1767 { "first", A|L, A },
1768 { "later", A|L, A|L },
1769
1770 { "no", A, 0 },
1771 { "yes", A, A },
1772
1773 { "not_later", L, 0 },
1774 { "later", L, L },
1775 #undef A
1776 #undef L
1777 };
1778
1779 static char *
1780 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1781 {
1782 const struct frag_handling *h;
1783
1784 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1785 if (!strcasecmp(s, h->name)) {
1786 /* We force the upper bits of the mask on to make mf_parse_value()
1787 * happy (otherwise it will never think it's an exact match.) */
1788 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1789 *valuep = h->value;
1790 return NULL;
1791 }
1792 }
1793
1794 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1795 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1796 }
1797
1798 static int
1799 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
1800 ovs_be16 *res)
1801 {
1802 uint32_t result = 0;
1803 char *save_ptr = NULL;
1804 char *name;
1805 int rc = 0;
1806 char *s = xstrdup(s_);
1807
1808 for (name = strtok_r((char *)s, " |", &save_ptr); name;
1809 name = strtok_r(NULL, " |", &save_ptr)) {
1810 int name_len;
1811 unsigned long long int flags;
1812 uint32_t bit;
1813
1814 if (ovs_scan(name, "%lli", &flags)) {
1815 result |= flags;
1816 continue;
1817 }
1818 name_len = strlen(name);
1819 for (bit = 1; bit; bit <<= 1) {
1820 const char *fname = bit_to_string(bit);
1821 size_t len;
1822
1823 if (!fname) {
1824 continue;
1825 }
1826
1827 len = strlen(fname);
1828 if (len != name_len) {
1829 continue;
1830 }
1831 if (!strncmp(name, fname, len)) {
1832 result |= bit;
1833 break;
1834 }
1835 }
1836
1837 if (!bit) {
1838 rc = -ENOENT;
1839 goto out;
1840 }
1841 }
1842
1843 *res = htons(result);
1844 out:
1845 free(s);
1846 return rc;
1847 }
1848
1849 static char *
1850 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
1851 {
1852 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
1853 *maskp = OVS_BE16_MAX;
1854 return NULL;
1855 }
1856
1857 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
1858 "\"csum\", \"key\")", s);
1859 }
1860
1861 static char *
1862 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
1863 {
1864 uint16_t flags = 0;
1865 uint16_t mask = 0;
1866 uint16_t bit;
1867 int n;
1868
1869 if (ovs_scan(s, "%"SCNi16"/%"SCNi16"%n", &flags, &mask, &n) && !s[n]) {
1870 *flagsp = htons(flags);
1871 *maskp = htons(mask);
1872 return NULL;
1873 }
1874 if (ovs_scan(s, "%"SCNi16"%n", &flags, &n) && !s[n]) {
1875 *flagsp = htons(flags);
1876 *maskp = OVS_BE16_MAX;
1877 return NULL;
1878 }
1879
1880 while (*s != '\0') {
1881 bool set;
1882 int name_len;
1883
1884 switch (*s) {
1885 case '+':
1886 set = true;
1887 break;
1888 case '-':
1889 set = false;
1890 break;
1891 default:
1892 return xasprintf("%s: TCP flag must be preceded by '+' (for SET) "
1893 "or '-' (NOT SET)", s);
1894 }
1895 s++;
1896
1897 name_len = strcspn(s,"+-");
1898
1899 for (bit = 1; bit; bit <<= 1) {
1900 const char *fname = packet_tcp_flag_to_string(bit);
1901 size_t len;
1902
1903 if (!fname) {
1904 continue;
1905 }
1906
1907 len = strlen(fname);
1908 if (len != name_len) {
1909 continue;
1910 }
1911 if (!strncmp(s, fname, len)) {
1912 if (mask & bit) {
1913 return xasprintf("%s: Each TCP flag can be specified only "
1914 "once", s);
1915 }
1916 if (set) {
1917 flags |= bit;
1918 }
1919 mask |= bit;
1920 break;
1921 }
1922 }
1923
1924 if (!bit) {
1925 return xasprintf("%s: unknown TCP flag(s)", s);
1926 }
1927 s += name_len;
1928 }
1929
1930 *flagsp = htons(flags);
1931 *maskp = htons(mask);
1932 return NULL;
1933 }
1934
1935
1936 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1937 * NULL if successful, otherwise a malloc()'d string describing the error. */
1938 char *
1939 mf_parse(const struct mf_field *mf, const char *s,
1940 union mf_value *value, union mf_value *mask)
1941 {
1942 char *error;
1943
1944 if (!strcmp(s, "*")) {
1945 memset(value, 0, mf->n_bytes);
1946 memset(mask, 0, mf->n_bytes);
1947 return NULL;
1948 }
1949
1950 switch (mf->string) {
1951 case MFS_DECIMAL:
1952 case MFS_HEXADECIMAL:
1953 error = mf_from_integer_string(mf, s,
1954 (uint8_t *) value, (uint8_t *) mask);
1955 break;
1956
1957 case MFS_ETHERNET:
1958 error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1959 break;
1960
1961 case MFS_IPV4:
1962 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1963 break;
1964
1965 case MFS_IPV6:
1966 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1967 break;
1968
1969 case MFS_OFP_PORT:
1970 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1971 break;
1972
1973 case MFS_OFP_PORT_OXM:
1974 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
1975 break;
1976
1977 case MFS_FRAG:
1978 error = mf_from_frag_string(s, &value->u8, &mask->u8);
1979 break;
1980
1981 case MFS_TNL_FLAGS:
1982 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1983 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
1984 break;
1985
1986 case MFS_TCP_FLAGS:
1987 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1988 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
1989 break;
1990
1991 default:
1992 OVS_NOT_REACHED();
1993 }
1994
1995 if (!error && !mf_is_mask_valid(mf, mask)) {
1996 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
1997 }
1998 return error;
1999 }
2000
2001 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2002 * successful, otherwise a malloc()'d string describing the error. */
2003 char *
2004 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2005 {
2006 union mf_value mask;
2007 char *error;
2008
2009 error = mf_parse(mf, s, value, &mask);
2010 if (error) {
2011 return error;
2012 }
2013
2014 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2015 return xasprintf("%s: wildcards not allowed here", s);
2016 }
2017 return NULL;
2018 }
2019
2020 static void
2021 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2022 const uint8_t *maskp, struct ds *s)
2023 {
2024 unsigned long long int integer;
2025 int i;
2026
2027 ovs_assert(mf->n_bytes <= 8);
2028
2029 integer = 0;
2030 for (i = 0; i < mf->n_bytes; i++) {
2031 integer = (integer << 8) | valuep[i];
2032 }
2033 if (mf->string == MFS_HEXADECIMAL) {
2034 ds_put_format(s, "%#llx", integer);
2035 } else {
2036 ds_put_format(s, "%lld", integer);
2037 }
2038
2039 if (maskp) {
2040 unsigned long long int mask;
2041
2042 mask = 0;
2043 for (i = 0; i < mf->n_bytes; i++) {
2044 mask = (mask << 8) | maskp[i];
2045 }
2046
2047 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2048 * not sure that that a bit-mask written in decimal is ever easier to
2049 * understand than the same bit-mask written in hexadecimal. */
2050 ds_put_format(s, "/%#llx", mask);
2051 }
2052 }
2053
2054 static void
2055 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2056 {
2057 const struct frag_handling *h;
2058
2059 mask &= FLOW_NW_FRAG_MASK;
2060 value &= mask;
2061
2062 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2063 if (value == h->value && mask == h->mask) {
2064 ds_put_cstr(s, h->name);
2065 return;
2066 }
2067 }
2068 ds_put_cstr(s, "<error>");
2069 }
2070
2071 static void
2072 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2073 {
2074 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2075 }
2076
2077 static void
2078 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2079 {
2080 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2081 TCP_FLAGS(mask));
2082 }
2083
2084 /* Appends to 's' a string representation of field 'mf' whose value is in
2085 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2086 void
2087 mf_format(const struct mf_field *mf,
2088 const union mf_value *value, const union mf_value *mask,
2089 struct ds *s)
2090 {
2091 if (mask) {
2092 if (is_all_zeros(mask, mf->n_bytes)) {
2093 ds_put_cstr(s, "ANY");
2094 return;
2095 } else if (is_all_ones(mask, mf->n_bytes)) {
2096 mask = NULL;
2097 }
2098 }
2099
2100 switch (mf->string) {
2101 case MFS_OFP_PORT_OXM:
2102 if (!mask) {
2103 ofp_port_t port;
2104 ofputil_port_from_ofp11(value->be32, &port);
2105 ofputil_format_port(port, s);
2106 break;
2107 }
2108 /* fall through */
2109 case MFS_OFP_PORT:
2110 if (!mask) {
2111 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2112 break;
2113 }
2114 /* fall through */
2115 case MFS_DECIMAL:
2116 case MFS_HEXADECIMAL:
2117 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2118 break;
2119
2120 case MFS_ETHERNET:
2121 eth_format_masked(value->mac, mask->mac, s);
2122 break;
2123
2124 case MFS_IPV4:
2125 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2126 break;
2127
2128 case MFS_IPV6:
2129 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2130 break;
2131
2132 case MFS_FRAG:
2133 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2134 break;
2135
2136 case MFS_TNL_FLAGS:
2137 mf_format_tnl_flags_string(&value->be16, s);
2138 break;
2139
2140 case MFS_TCP_FLAGS:
2141 mf_format_tcp_flags_string(value->be16,
2142 mask ? mask->be16 : OVS_BE16_MAX, s);
2143 break;
2144
2145 default:
2146 OVS_NOT_REACHED();
2147 }
2148 }
2149 \f
2150 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2151 * least-significant bits in 'x'.
2152 */
2153 void
2154 mf_write_subfield_flow(const struct mf_subfield *sf,
2155 const union mf_subvalue *x, struct flow *flow)
2156 {
2157 const struct mf_field *field = sf->field;
2158 union mf_value value;
2159
2160 mf_get_value(field, flow, &value);
2161 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2162 sf->ofs, sf->n_bits);
2163 mf_set_flow_value(field, &value, flow);
2164 }
2165
2166 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2167 * least-significant bits in 'x'.
2168 */
2169 void
2170 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2171 struct match *match)
2172 {
2173 const struct mf_field *field = sf->field;
2174 union mf_value value, mask;
2175
2176 mf_get(field, match, &value, &mask);
2177 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2178 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2179 mf_set(field, &value, &mask, match);
2180 }
2181
2182 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2183 * reading 'flow', e.g. as checked by mf_check_src(). */
2184 void
2185 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2186 union mf_subvalue *x)
2187 {
2188 union mf_value value;
2189
2190 mf_get_value(sf->field, flow, &value);
2191
2192 memset(x, 0, sizeof *x);
2193 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2194 x, sizeof *x, 0,
2195 sf->n_bits);
2196 }
2197
2198 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2199 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2200 * less. */
2201 uint64_t
2202 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2203 {
2204 union mf_value value;
2205
2206 mf_get_value(sf->field, flow, &value);
2207 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2208 }
2209
2210 void
2211 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2212 {
2213 int i;
2214
2215 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2216 if (subvalue->u8[i]) {
2217 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2218 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2219 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2220 }
2221 return;
2222 }
2223 }
2224 ds_put_char(s, '0');
2225 }