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