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