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