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