]> git.proxmox.com Git - ovs.git/blame - lib/meta-flow.c
OF 1.1 pop vlan compatibility.
[ovs.git] / lib / meta-flow.c
CommitLineData
6a885fd0 1/*
cb22974d 2 * Copyright (c) 2011, 2012, 2013 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"
816fd533 28#include "ofp-errors.h"
6a885fd0 29#include "ofp-util.h"
4663f9e0 30#include "ovs-thread.h"
6a885fd0
BP
31#include "packets.h"
32#include "random.h"
33#include "shash.h"
34#include "socket-util.h"
35#include "unaligned.h"
816fd533
BP
36#include "vlog.h"
37
38VLOG_DEFINE_THIS_MODULE(meta_flow);
6a885fd0
BP
39
40#define MF_FIELD_SIZES(MEMBER) \
41 sizeof ((union mf_value *)0)->MEMBER, \
42 8 * sizeof ((union mf_value *)0)->MEMBER
43
44static const struct mf_field mf_fields[MFF_N_IDS] = {
45 /* ## -------- ## */
46 /* ## metadata ## */
47 /* ## -------- ## */
48
49 {
50 MFF_TUN_ID, "tun_id", NULL,
51 MF_FIELD_SIZES(be64),
0bdc4bec 52 MFM_FULLY,
6a885fd0
BP
53 MFS_HEXADECIMAL,
54 MFP_NONE,
28da1f8f
BP
55 true,
56 NXM_NX_TUN_ID, "NXM_NX_TUN_ID",
2f2eb46c 57 OXM_OF_TUNNEL_ID, "OXM_OF_TUNNEL_ID",
db0b6c29
JR
58 OFPUTIL_P_NXM_OXM_ANY,
59 OFPUTIL_P_NXM_OXM_ANY,
4fe3445a
PS
60 }, {
61 MFF_TUN_SRC, "tun_src", NULL,
62 MF_FIELD_SIZES(be32),
0ad90c84 63 MFM_FULLY,
4fe3445a
PS
64 MFS_IPV4,
65 MFP_NONE,
0ad90c84
JR
66 true,
67 NXM_NX_TUN_IPV4_SRC, "NXM_NX_TUN_IPV4_SRC",
68 NXM_NX_TUN_IPV4_SRC, "NXM_NX_TUN_IPV4_SRC",
db0b6c29
JR
69 OFPUTIL_P_NXM_OXM_ANY,
70 OFPUTIL_P_NXM_OXM_ANY,
4fe3445a
PS
71 }, {
72 MFF_TUN_DST, "tun_dst", NULL,
73 MF_FIELD_SIZES(be32),
0ad90c84 74 MFM_FULLY,
4fe3445a
PS
75 MFS_IPV4,
76 MFP_NONE,
0ad90c84
JR
77 true,
78 NXM_NX_TUN_IPV4_DST, "NXM_NX_TUN_IPV4_DST",
79 NXM_NX_TUN_IPV4_DST, "NXM_NX_TUN_IPV4_DST",
db0b6c29
JR
80 OFPUTIL_P_NXM_OXM_ANY,
81 OFPUTIL_P_NXM_OXM_ANY,
4fe3445a
PS
82 }, {
83 MFF_TUN_FLAGS, "tun_flags", NULL,
84 MF_FIELD_SIZES(be16),
85 MFM_NONE,
86 MFS_TNL_FLAGS,
87 MFP_NONE,
88 false,
89 0, NULL,
90 0, NULL,
db0b6c29
JR
91 OFPUTIL_P_NONE,
92 OFPUTIL_P_NONE,
4fe3445a 93 }, {
216a32dd 94 MFF_TUN_TTL, "tun_ttl", NULL,
4fe3445a
PS
95 MF_FIELD_SIZES(u8),
96 MFM_NONE,
97 MFS_DECIMAL,
98 MFP_NONE,
99 false,
100 0, NULL,
101 0, NULL,
db0b6c29
JR
102 OFPUTIL_P_NONE,
103 OFPUTIL_P_NONE,
4fe3445a 104 }, {
216a32dd 105 MFF_TUN_TOS, "tun_tos", NULL,
4fe3445a
PS
106 MF_FIELD_SIZES(u8),
107 MFM_NONE,
108 MFS_DECIMAL,
109 MFP_NONE,
110 false,
111 0, NULL,
112 0, NULL,
db0b6c29
JR
113 OFPUTIL_P_NONE,
114 OFPUTIL_P_NONE,
969fc56c
JS
115 }, {
116 MFF_METADATA, "metadata", NULL,
117 MF_FIELD_SIZES(be64),
0bdc4bec 118 MFM_FULLY,
969fc56c
JS
119 MFS_HEXADECIMAL,
120 MFP_NONE,
121 true,
122 OXM_OF_METADATA, "OXM_OF_METADATA",
123 OXM_OF_METADATA, "OXM_OF_METADATA",
db0b6c29
JR
124 OFPUTIL_P_NXM_OF11_UP,
125 OFPUTIL_P_NXM_OF11_UP,
6a885fd0
BP
126 }, {
127 MFF_IN_PORT, "in_port", NULL,
128 MF_FIELD_SIZES(be16),
0bdc4bec 129 MFM_NONE,
6a885fd0
BP
130 MFS_OFP_PORT,
131 MFP_NONE,
557323cd 132 true,
28da1f8f 133 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
72333065 134 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
db0b6c29
JR
135 OFPUTIL_P_ANY, /* OF11+ via mapping to 32 bits. */
136 OFPUTIL_P_NONE,
72333065
JR
137 }, {
138 MFF_IN_PORT_OXM, "in_port_oxm", NULL,
139 MF_FIELD_SIZES(be32),
140 MFM_NONE,
141 MFS_OFP_PORT_OXM,
142 MFP_NONE,
143 true,
144 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
36f3999b 145 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
db0b6c29
JR
146 OFPUTIL_P_OF11_UP,
147 OFPUTIL_P_NONE,
1b567fb9
AA
148 }, {
149 MFF_SKB_PRIORITY, "skb_priority", NULL,
150 MF_FIELD_SIZES(be32),
151 MFM_NONE,
152 MFS_HEXADECIMAL,
153 MFP_NONE,
154 false,
155 0, NULL,
156 0, NULL,
db0b6c29
JR
157 OFPUTIL_P_NONE,
158 OFPUTIL_P_NONE,
1b567fb9 159 }, {
1362e248 160 MFF_PKT_MARK, "pkt_mark", NULL,
1b567fb9 161 MF_FIELD_SIZES(be32),
ac923e91 162 MFM_FULLY,
1b567fb9
AA
163 MFS_HEXADECIMAL,
164 MFP_NONE,
ac923e91
JG
165 true,
166 NXM_NX_PKT_MARK, "NXM_NX_PKT_MARK",
167 NXM_NX_PKT_MARK, "NXM_NX_PKT_MARK",
db0b6c29
JR
168 OFPUTIL_P_NXM_OXM_ANY,
169 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0
BP
170 },
171
172#define REGISTER(IDX) \
173 { \
174 MFF_REG##IDX, "reg" #IDX, NULL, \
175 MF_FIELD_SIZES(be32), \
0bdc4bec 176 MFM_FULLY, \
6a885fd0
BP
177 MFS_HEXADECIMAL, \
178 MFP_NONE, \
28da1f8f 179 true, \
b5e5143b
BP
180 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
181 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
db0b6c29
JR
182 OFPUTIL_P_NXM_OXM_ANY, \
183 OFPUTIL_P_NXM_OXM_ANY, \
6a885fd0
BP
184 }
185#if FLOW_N_REGS > 0
186 REGISTER(0),
187#endif
188#if FLOW_N_REGS > 1
189 REGISTER(1),
190#endif
191#if FLOW_N_REGS > 2
192 REGISTER(2),
193#endif
194#if FLOW_N_REGS > 3
195 REGISTER(3),
196#endif
197#if FLOW_N_REGS > 4
d2c0fed9
JP
198 REGISTER(4),
199#endif
200#if FLOW_N_REGS > 5
e9358af6
EJ
201 REGISTER(5),
202#endif
203#if FLOW_N_REGS > 6
204 REGISTER(6),
205#endif
206#if FLOW_N_REGS > 7
207 REGISTER(7),
208#endif
209#if FLOW_N_REGS > 8
6a885fd0
BP
210#error
211#endif
212
213 /* ## -- ## */
214 /* ## L2 ## */
215 /* ## -- ## */
216
217 {
218 MFF_ETH_SRC, "eth_src", "dl_src",
219 MF_FIELD_SIZES(mac),
0bdc4bec 220 MFM_FULLY,
6a885fd0
BP
221 MFS_ETHERNET,
222 MFP_NONE,
28da1f8f
BP
223 true,
224 NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
36f3999b 225 OXM_OF_ETH_SRC, "OXM_OF_ETH_SRC",
db0b6c29
JR
226 OFPUTIL_P_ANY,
227 OFPUTIL_P_NXM_OF11_UP, /* Bitwise masking only with NXM and OF11+! */
6a885fd0
BP
228 }, {
229 MFF_ETH_DST, "eth_dst", "dl_dst",
230 MF_FIELD_SIZES(mac),
0bdc4bec 231 MFM_FULLY,
6a885fd0
BP
232 MFS_ETHERNET,
233 MFP_NONE,
28da1f8f
BP
234 true,
235 NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
36f3999b 236 OXM_OF_ETH_DST, "OXM_OF_ETH_DST",
db0b6c29
JR
237 OFPUTIL_P_ANY,
238 OFPUTIL_P_NXM_OF11_UP, /* Bitwise masking only with NXM and OF11+! */
6a885fd0
BP
239 }, {
240 MFF_ETH_TYPE, "eth_type", "dl_type",
241 MF_FIELD_SIZES(be16),
0bdc4bec 242 MFM_NONE,
6a885fd0
BP
243 MFS_HEXADECIMAL,
244 MFP_NONE,
28da1f8f
BP
245 false,
246 NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
36f3999b 247 OXM_OF_ETH_TYPE, "OXM_OF_ETH_TYPE",
db0b6c29
JR
248 OFPUTIL_P_ANY,
249 OFPUTIL_P_NONE,
6a885fd0
BP
250 },
251
252 {
253 MFF_VLAN_TCI, "vlan_tci", NULL,
254 MF_FIELD_SIZES(be16),
0bdc4bec 255 MFM_FULLY,
6a885fd0
BP
256 MFS_HEXADECIMAL,
257 MFP_NONE,
28da1f8f
BP
258 true,
259 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
b5e5143b 260 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
db0b6c29
JR
261 OFPUTIL_P_ANY,
262 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0 263 }, {
441c57a9 264 MFF_DL_VLAN, "dl_vlan", NULL,
6a885fd0 265 sizeof(ovs_be16), 12,
0bdc4bec 266 MFM_NONE,
6a885fd0
BP
267 MFS_DECIMAL,
268 MFP_NONE,
28da1f8f 269 true,
cc34bc8c
BP
270 0, NULL,
271 0, NULL,
db0b6c29
JR
272 OFPUTIL_P_ANY,
273 OFPUTIL_P_NXM_OXM_ANY,
cc34bc8c
BP
274 }, {
275 MFF_VLAN_VID, "vlan_vid", NULL,
276 sizeof(ovs_be16), 12,
0bdc4bec 277 MFM_FULLY,
cc34bc8c
BP
278 MFS_DECIMAL,
279 MFP_NONE,
280 true,
b5e5143b 281 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
36f3999b 282 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
db0b6c29
JR
283 OFPUTIL_P_ANY,
284 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0 285 }, {
441c57a9 286 MFF_DL_VLAN_PCP, "dl_vlan_pcp", NULL,
6a885fd0 287 1, 3,
0bdc4bec 288 MFM_NONE,
6a885fd0
BP
289 MFS_DECIMAL,
290 MFP_NONE,
28da1f8f 291 true,
cc34bc8c
BP
292 0, NULL,
293 0, NULL,
db0b6c29
JR
294 OFPUTIL_P_ANY, /* Will be mapped to NXM and OXM. */
295 OFPUTIL_P_NONE,
cc34bc8c
BP
296 }, {
297 MFF_VLAN_PCP, "vlan_pcp", NULL,
298 1, 3,
0bdc4bec 299 MFM_NONE,
cc34bc8c 300 MFS_DECIMAL,
8069b0da 301 MFP_VLAN_VID,
cc34bc8c 302 true,
b5e5143b 303 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
36f3999b 304 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
db0b6c29
JR
305 OFPUTIL_P_ANY, /* Will be mapped to OF10 and NXM. */
306 OFPUTIL_P_NONE,
6a885fd0
BP
307 },
308
b02475c5
SH
309 /* ## ---- ## */
310 /* ## L2.5 ## */
311 /* ## ---- ## */
312 {
313 MFF_MPLS_LABEL, "mpls_label", NULL,
314 4, 20,
315 MFM_NONE,
316 MFS_DECIMAL,
317 MFP_MPLS,
318 true,
319 OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL",
320 OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL",
db0b6c29
JR
321 OFPUTIL_P_NXM_OF11_UP,
322 OFPUTIL_P_NONE,
b02475c5
SH
323 }, {
324 MFF_MPLS_TC, "mpls_tc", NULL,
325 1, 3,
326 MFM_NONE,
327 MFS_DECIMAL,
328 MFP_MPLS,
329 true,
330 OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC",
331 OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC",
db0b6c29
JR
332 OFPUTIL_P_NXM_OF11_UP,
333 OFPUTIL_P_NONE,
b02475c5
SH
334 }, {
335 MFF_MPLS_BOS, "mpls_bos", NULL,
336 1, 1,
337 MFM_NONE,
338 MFS_DECIMAL,
339 MFP_MPLS,
340 false,
341 OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS",
342 OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS",
db0b6c29
JR
343 OFPUTIL_P_NXM_OXM_ANY,
344 OFPUTIL_P_NONE,
b02475c5
SH
345 },
346
6a885fd0
BP
347 /* ## -- ## */
348 /* ## L3 ## */
349 /* ## -- ## */
350
351 {
352 MFF_IPV4_SRC, "ip_src", "nw_src",
353 MF_FIELD_SIZES(be32),
0bdc4bec 354 MFM_FULLY,
6a885fd0
BP
355 MFS_IPV4,
356 MFP_IPV4,
28da1f8f
BP
357 true,
358 NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
36f3999b 359 OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC",
db0b6c29
JR
360 OFPUTIL_P_ANY,
361 OFPUTIL_P_NXM_OF11_UP,
6a885fd0
BP
362 }, {
363 MFF_IPV4_DST, "ip_dst", "nw_dst",
364 MF_FIELD_SIZES(be32),
0bdc4bec 365 MFM_FULLY,
6a885fd0
BP
366 MFS_IPV4,
367 MFP_IPV4,
28da1f8f
BP
368 true,
369 NXM_OF_IP_DST, "NXM_OF_IP_DST",
36f3999b 370 OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST",
db0b6c29
JR
371 OFPUTIL_P_ANY,
372 OFPUTIL_P_NXM_OF11_UP,
6a885fd0
BP
373 },
374
375 {
376 MFF_IPV6_SRC, "ipv6_src", NULL,
377 MF_FIELD_SIZES(ipv6),
0bdc4bec 378 MFM_FULLY,
6a885fd0
BP
379 MFS_IPV6,
380 MFP_IPV6,
28da1f8f
BP
381 true,
382 NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
36f3999b 383 OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC",
db0b6c29
JR
384 OFPUTIL_P_NXM_OXM_ANY,
385 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0
BP
386 }, {
387 MFF_IPV6_DST, "ipv6_dst", NULL,
388 MF_FIELD_SIZES(ipv6),
0bdc4bec 389 MFM_FULLY,
6a885fd0
BP
390 MFS_IPV6,
391 MFP_IPV6,
28da1f8f
BP
392 true,
393 NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
36f3999b 394 OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST",
db0b6c29
JR
395 OFPUTIL_P_NXM_OXM_ANY,
396 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0 397 },
fa8223b7
JP
398 {
399 MFF_IPV6_LABEL, "ipv6_label", NULL,
400 4, 20,
0bdc4bec 401 MFM_FULLY,
fa8223b7
JP
402 MFS_HEXADECIMAL,
403 MFP_IPV6,
28da1f8f
BP
404 false,
405 NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
36f3999b 406 OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL",
db0b6c29
JR
407 OFPUTIL_P_NXM_OXM_ANY,
408 OFPUTIL_P_NXM_OXM_ANY,
fa8223b7 409 },
6a885fd0
BP
410
411 {
412 MFF_IP_PROTO, "nw_proto", NULL,
413 MF_FIELD_SIZES(u8),
0bdc4bec 414 MFM_NONE,
6a885fd0
BP
415 MFS_DECIMAL,
416 MFP_IP_ANY,
28da1f8f
BP
417 false,
418 NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
36f3999b 419 OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO",
db0b6c29
JR
420 OFPUTIL_P_ANY,
421 OFPUTIL_P_NONE,
6a885fd0 422 }, {
530180fd 423 MFF_IP_DSCP, "nw_tos", NULL,
6a885fd0 424 MF_FIELD_SIZES(u8),
0bdc4bec 425 MFM_NONE,
6a885fd0
BP
426 MFS_DECIMAL,
427 MFP_IP_ANY,
28da1f8f 428 true,
36f3999b 429 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
1638b906 430 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
db0b6c29
JR
431 OFPUTIL_P_ANY, /* Will be shifted for OXM. */
432 OFPUTIL_P_NONE,
1638b906
BP
433 }, {
434 MFF_IP_DSCP_SHIFTED, "nw_tos_shifted", NULL,
435 MF_FIELD_SIZES(u8),
436 MFM_NONE,
437 MFS_DECIMAL,
438 MFP_IP_ANY,
439 true,
440 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
36f3999b 441 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
db0b6c29
JR
442 OFPUTIL_P_ANY, /* Will be shifted for non-OXM. */
443 OFPUTIL_P_NONE,
530180fd
JP
444 }, {
445 MFF_IP_ECN, "nw_ecn", NULL,
446 1, 2,
0bdc4bec 447 MFM_NONE,
530180fd
JP
448 MFS_DECIMAL,
449 MFP_IP_ANY,
28da1f8f
BP
450 true,
451 NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
36f3999b 452 OXM_OF_IP_ECN, "OXM_OF_IP_ECN",
db0b6c29
JR
453 OFPUTIL_P_NXM_OXM_ANY,
454 OFPUTIL_P_NONE,
a61680c6
JP
455 }, {
456 MFF_IP_TTL, "nw_ttl", NULL,
457 MF_FIELD_SIZES(u8),
0bdc4bec 458 MFM_NONE,
a61680c6
JP
459 MFS_DECIMAL,
460 MFP_IP_ANY,
28da1f8f 461 true,
36f3999b 462 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
b5e5143b 463 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
db0b6c29
JR
464 OFPUTIL_P_NXM_OXM_ANY,
465 OFPUTIL_P_NONE,
7257b535
BP
466 }, {
467 MFF_IP_FRAG, "ip_frag", NULL,
468 1, 2,
0bdc4bec 469 MFM_FULLY,
7257b535
BP
470 MFS_FRAG,
471 MFP_IP_ANY,
28da1f8f 472 false,
36f3999b 473 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
b5e5143b 474 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
db0b6c29
JR
475 OFPUTIL_P_NXM_OXM_ANY,
476 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0
BP
477 },
478
479 {
480 MFF_ARP_OP, "arp_op", NULL,
481 MF_FIELD_SIZES(be16),
0bdc4bec 482 MFM_NONE,
6a885fd0
BP
483 MFS_DECIMAL,
484 MFP_ARP,
f6c8a6b1 485 true,
28da1f8f 486 NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
36f3999b 487 OXM_OF_ARP_OP, "OXM_OF_ARP_OP",
db0b6c29
JR
488 OFPUTIL_P_ANY,
489 OFPUTIL_P_NONE,
6a885fd0
BP
490 }, {
491 MFF_ARP_SPA, "arp_spa", NULL,
492 MF_FIELD_SIZES(be32),
0bdc4bec 493 MFM_FULLY,
6a885fd0
BP
494 MFS_IPV4,
495 MFP_ARP,
f6c8a6b1 496 true,
28da1f8f 497 NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
36f3999b 498 OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA",
db0b6c29
JR
499 OFPUTIL_P_ANY,
500 OFPUTIL_P_NXM_OF11_UP,
6a885fd0
BP
501 }, {
502 MFF_ARP_TPA, "arp_tpa", NULL,
503 MF_FIELD_SIZES(be32),
0bdc4bec 504 MFM_FULLY,
6a885fd0
BP
505 MFS_IPV4,
506 MFP_ARP,
f6c8a6b1 507 true,
28da1f8f 508 NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
36f3999b 509 OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA",
db0b6c29
JR
510 OFPUTIL_P_ANY,
511 OFPUTIL_P_NXM_OF11_UP,
6a885fd0
BP
512 }, {
513 MFF_ARP_SHA, "arp_sha", NULL,
514 MF_FIELD_SIZES(mac),
0bdc4bec 515 MFM_FULLY,
6a885fd0
BP
516 MFS_ETHERNET,
517 MFP_ARP,
f6c8a6b1 518 true,
28da1f8f 519 NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
36f3999b 520 OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA",
db0b6c29
JR
521 OFPUTIL_P_NXM_OXM_ANY,
522 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0
BP
523 }, {
524 MFF_ARP_THA, "arp_tha", NULL,
525 MF_FIELD_SIZES(mac),
0bdc4bec 526 MFM_FULLY,
6a885fd0
BP
527 MFS_ETHERNET,
528 MFP_ARP,
f6c8a6b1 529 true,
28da1f8f 530 NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
36f3999b 531 OXM_OF_ARP_THA, "OXM_OF_ARP_THA",
db0b6c29
JR
532 OFPUTIL_P_NXM_OXM_ANY,
533 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0
BP
534 },
535
536 /* ## -- ## */
537 /* ## L4 ## */
538 /* ## -- ## */
539
540 {
541 MFF_TCP_SRC, "tcp_src", "tp_src",
542 MF_FIELD_SIZES(be16),
0bdc4bec 543 MFM_FULLY,
6a885fd0
BP
544 MFS_DECIMAL,
545 MFP_TCP,
28da1f8f
BP
546 true,
547 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
36f3999b 548 OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC",
db0b6c29
JR
549 OFPUTIL_P_ANY,
550 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0
BP
551 }, {
552 MFF_TCP_DST, "tcp_dst", "tp_dst",
553 MF_FIELD_SIZES(be16),
0bdc4bec 554 MFM_FULLY,
6a885fd0
BP
555 MFS_DECIMAL,
556 MFP_TCP,
28da1f8f
BP
557 true,
558 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
36f3999b 559 OXM_OF_TCP_DST, "OXM_OF_TCP_DST",
db0b6c29
JR
560 OFPUTIL_P_ANY,
561 OFPUTIL_P_NXM_OXM_ANY,
dc235f7f
JR
562 }, {
563 MFF_TCP_FLAGS, "tcp_flags", NULL,
564 2, 12,
565 MFM_FULLY,
566 MFS_HEXADECIMAL,
567 MFP_TCP,
568 false,
569 NXM_NX_TCP_FLAGS, "NXM_NX_TCP_FLAGS",
570 NXM_NX_TCP_FLAGS, "NXM_NX_TCP_FLAGS",
571 OFPUTIL_P_NXM_OXM_ANY,
572 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0
BP
573 },
574
575 {
576 MFF_UDP_SRC, "udp_src", NULL,
577 MF_FIELD_SIZES(be16),
0bdc4bec 578 MFM_FULLY,
6a885fd0
BP
579 MFS_DECIMAL,
580 MFP_UDP,
28da1f8f
BP
581 true,
582 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
36f3999b 583 OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
db0b6c29
JR
584 OFPUTIL_P_ANY,
585 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0
BP
586 }, {
587 MFF_UDP_DST, "udp_dst", NULL,
588 MF_FIELD_SIZES(be16),
0bdc4bec 589 MFM_FULLY,
6a885fd0
BP
590 MFS_DECIMAL,
591 MFP_UDP,
28da1f8f
BP
592 true,
593 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
36f3999b 594 OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
db0b6c29
JR
595 OFPUTIL_P_ANY,
596 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0
BP
597 },
598
0d56eaf2
JS
599 {
600 MFF_SCTP_SRC, "sctp_src", NULL,
601 MF_FIELD_SIZES(be16),
602 MFM_FULLY,
603 MFS_DECIMAL,
604 MFP_SCTP,
605 true,
606 OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC",
607 OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC",
db0b6c29
JR
608 OFPUTIL_P_NXM_OF11_UP,
609 OFPUTIL_P_NXM_OXM_ANY,
0d56eaf2
JS
610 }, {
611 MFF_SCTP_DST, "sctp_dst", NULL,
612 MF_FIELD_SIZES(be16),
613 MFM_FULLY,
614 MFS_DECIMAL,
615 MFP_SCTP,
616 true,
617 OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST",
618 OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST",
db0b6c29
JR
619 OFPUTIL_P_NXM_OF11_UP,
620 OFPUTIL_P_NXM_OXM_ANY,
0d56eaf2
JS
621 },
622
6a885fd0 623 {
268a95e0 624 MFF_ICMPV4_TYPE, "icmp_type", NULL,
6a885fd0 625 MF_FIELD_SIZES(u8),
0bdc4bec 626 MFM_NONE,
6a885fd0 627 MFS_DECIMAL,
268a95e0 628 MFP_ICMPV4,
28da1f8f
BP
629 false,
630 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
36f3999b 631 OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
db0b6c29
JR
632 OFPUTIL_P_ANY,
633 OFPUTIL_P_NONE,
6a885fd0 634 }, {
268a95e0 635 MFF_ICMPV4_CODE, "icmp_code", NULL,
6a885fd0 636 MF_FIELD_SIZES(u8),
0bdc4bec 637 MFM_NONE,
6a885fd0 638 MFS_DECIMAL,
268a95e0 639 MFP_ICMPV4,
28da1f8f
BP
640 false,
641 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
36f3999b 642 OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
db0b6c29
JR
643 OFPUTIL_P_ANY,
644 OFPUTIL_P_NONE,
28da1f8f
BP
645 },
646
647 {
268a95e0
BP
648 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
649 MF_FIELD_SIZES(u8),
0bdc4bec 650 MFM_NONE,
268a95e0
BP
651 MFS_DECIMAL,
652 MFP_ICMPV6,
28da1f8f
BP
653 false,
654 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
36f3999b 655 OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
db0b6c29
JR
656 OFPUTIL_P_NXM_OXM_ANY,
657 OFPUTIL_P_NONE,
268a95e0
BP
658 }, {
659 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
660 MF_FIELD_SIZES(u8),
0bdc4bec 661 MFM_NONE,
268a95e0
BP
662 MFS_DECIMAL,
663 MFP_ICMPV6,
28da1f8f
BP
664 false,
665 NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
36f3999b 666 OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
db0b6c29
JR
667 OFPUTIL_P_NXM_OXM_ANY,
668 OFPUTIL_P_NONE,
6a885fd0
BP
669 },
670
671 /* ## ---- ## */
672 /* ## L"5" ## */
673 /* ## ---- ## */
674
675 {
676 MFF_ND_TARGET, "nd_target", NULL,
677 MF_FIELD_SIZES(ipv6),
0bdc4bec 678 MFM_FULLY,
6a885fd0
BP
679 MFS_IPV6,
680 MFP_ND,
28da1f8f
BP
681 false,
682 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
36f3999b 683 OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
db0b6c29
JR
684 OFPUTIL_P_NXM_OXM_ANY,
685 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0
BP
686 }, {
687 MFF_ND_SLL, "nd_sll", NULL,
688 MF_FIELD_SIZES(mac),
0bdc4bec 689 MFM_FULLY,
6a885fd0
BP
690 MFS_ETHERNET,
691 MFP_ND_SOLICIT,
28da1f8f
BP
692 false,
693 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
36f3999b 694 OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
db0b6c29
JR
695 OFPUTIL_P_NXM_OXM_ANY,
696 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0
BP
697 }, {
698 MFF_ND_TLL, "nd_tll", NULL,
699 MF_FIELD_SIZES(mac),
0bdc4bec 700 MFM_FULLY,
6a885fd0
BP
701 MFS_ETHERNET,
702 MFP_ND_ADVERT,
28da1f8f
BP
703 false,
704 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
36f3999b 705 OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
db0b6c29
JR
706 OFPUTIL_P_NXM_OXM_ANY,
707 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0
BP
708 }
709};
710
b5e5143b 711/* Maps an NXM or OXM header value to an mf_field. */
28da1f8f 712struct nxm_field {
b5e5143b
BP
713 struct hmap_node hmap_node; /* In 'all_fields' hmap. */
714 uint32_t header; /* NXM or OXM header value. */
28da1f8f
BP
715 const struct mf_field *mf;
716};
717
b5e5143b 718/* Contains 'struct nxm_field's. */
4663f9e0
BP
719static struct hmap all_fields;
720
721/* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
722static struct shash mf_by_name;
28da1f8f 723
816fd533
BP
724/* Rate limit for parse errors. These always indicate a bug in an OpenFlow
725 * controller and so there's not much point in showing a lot of them. */
726static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
727
b5e5143b 728const struct mf_field *mf_from_nxm_header__(uint32_t header);
4663f9e0 729static void nxm_init(void);
b5e5143b 730
6a885fd0
BP
731/* Returns the field with the given 'id'. */
732const struct mf_field *
733mf_from_id(enum mf_field_id id)
734{
cb22974d 735 ovs_assert((unsigned int) id < MFF_N_IDS);
6a885fd0
BP
736 return &mf_fields[id];
737}
738
739/* Returns the field with the given 'name', or a null pointer if no field has
740 * that name. */
741const struct mf_field *
742mf_from_name(const char *name)
743{
4663f9e0 744 nxm_init();
6a885fd0
BP
745 return shash_find_data(&mf_by_name, name);
746}
747
28da1f8f 748static void
b5e5143b 749add_nxm_field(uint32_t header, const struct mf_field *mf)
28da1f8f
BP
750{
751 struct nxm_field *f;
752
753 f = xmalloc(sizeof *f);
b5e5143b
BP
754 hmap_insert(&all_fields, &f->hmap_node, hash_int(header, 0));
755 f->header = header;
28da1f8f
BP
756 f->mf = mf;
757}
758
b5ae8913
SH
759static void
760nxm_init_add_field(const struct mf_field *mf, uint32_t header)
761{
b5e5143b 762 if (header) {
cb22974d 763 ovs_assert(!mf_from_nxm_header__(header));
b5e5143b
BP
764 add_nxm_field(header, mf);
765 if (mf->maskable != MFM_NONE) {
766 add_nxm_field(NXM_MAKE_WILD_HEADER(header), mf);
767 }
b5ae8913 768 }
b5ae8913 769}
b5ae8913 770
28da1f8f 771static void
4663f9e0 772nxm_do_init(void)
28da1f8f 773{
216a32dd 774 int i;
28da1f8f 775
4663f9e0
BP
776 hmap_init(&all_fields);
777 shash_init(&mf_by_name);
216a32dd
JR
778 for (i = 0; i < MFF_N_IDS; i++) {
779 const struct mf_field *mf = &mf_fields[i];
780
781 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
782
b5ae8913 783 nxm_init_add_field(mf, mf->nxm_header);
b5e5143b
BP
784 if (mf->oxm_header != mf->nxm_header) {
785 nxm_init_add_field(mf, mf->oxm_header);
786 }
4663f9e0
BP
787
788 shash_add_once(&mf_by_name, mf->name, mf);
789 if (mf->extra_name) {
790 shash_add_once(&mf_by_name, mf->extra_name, mf);
791 }
28da1f8f 792 }
28da1f8f
BP
793}
794
4663f9e0
BP
795static void
796nxm_init(void)
797{
798 static pthread_once_t once = PTHREAD_ONCE_INIT;
799 pthread_once(&once, nxm_do_init);
800}
801
28da1f8f
BP
802const struct mf_field *
803mf_from_nxm_header(uint32_t header)
804{
4663f9e0 805 nxm_init();
b5e5143b
BP
806 return mf_from_nxm_header__(header);
807}
28da1f8f 808
b5e5143b
BP
809const struct mf_field *
810mf_from_nxm_header__(uint32_t header)
811{
812 const struct nxm_field *f;
813
814 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0), &all_fields) {
815 if (f->header == header) {
28da1f8f
BP
816 return f->mf;
817 }
818 }
819
820 return NULL;
821}
822
6a885fd0
BP
823/* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
824 * specifies at least one bit in the field.
825 *
826 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
827 * meets 'mf''s prerequisites. */
828bool
829mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
830{
831 switch (mf->id) {
4fe3445a 832 case MFF_TUN_SRC:
0ad90c84 833 return !wc->masks.tunnel.ip_src;
4fe3445a 834 case MFF_TUN_DST:
0ad90c84
JR
835 return !wc->masks.tunnel.ip_dst;
836 case MFF_TUN_ID:
4fe3445a
PS
837 case MFF_TUN_TOS:
838 case MFF_TUN_TTL:
839 case MFF_TUN_FLAGS:
296e07ac 840 return !wc->masks.tunnel.tun_id;
969fc56c 841 case MFF_METADATA:
26720e24 842 return !wc->masks.metadata;
0bdc4bec 843 case MFF_IN_PORT:
72333065 844 case MFF_IN_PORT_OXM:
4e022ec0 845 return !wc->masks.in_port.ofp_port;
1b567fb9
AA
846 case MFF_SKB_PRIORITY:
847 return !wc->masks.skb_priority;
1362e248
JG
848 case MFF_PKT_MARK:
849 return !wc->masks.pkt_mark;
0d7e2fe4 850 CASE_MFF_REGS:
26720e24 851 return !wc->masks.regs[mf->id - MFF_REG0];
6a885fd0 852
73c0ce34 853 case MFF_ETH_SRC:
26720e24 854 return eth_addr_is_zero(wc->masks.dl_src);
6a885fd0 855 case MFF_ETH_DST:
26720e24 856 return eth_addr_is_zero(wc->masks.dl_dst);
e2170cff 857 case MFF_ETH_TYPE:
26720e24 858 return !wc->masks.dl_type;
6a885fd0 859
e878338b
SH
860 case MFF_ARP_SHA:
861 case MFF_ND_SLL:
26720e24 862 return eth_addr_is_zero(wc->masks.arp_sha);
e878338b
SH
863
864 case MFF_ARP_THA:
865 case MFF_ND_TLL:
26720e24 866 return eth_addr_is_zero(wc->masks.arp_tha);
e878338b 867
6a885fd0 868 case MFF_VLAN_TCI:
26720e24 869 return !wc->masks.vlan_tci;
441c57a9 870 case MFF_DL_VLAN:
26720e24 871 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
cc34bc8c 872 case MFF_VLAN_VID:
26720e24 873 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
441c57a9 874 case MFF_DL_VLAN_PCP:
cc34bc8c 875 case MFF_VLAN_PCP:
26720e24 876 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
6a885fd0 877
b02475c5
SH
878 case MFF_MPLS_LABEL:
879 return !(wc->masks.mpls_lse & htonl(MPLS_LABEL_MASK));
880 case MFF_MPLS_TC:
881 return !(wc->masks.mpls_lse & htonl(MPLS_TC_MASK));
882 case MFF_MPLS_BOS:
883 return !(wc->masks.mpls_lse & htonl(MPLS_BOS_MASK));
884
6a885fd0 885 case MFF_IPV4_SRC:
26720e24 886 return !wc->masks.nw_src;
6a885fd0 887 case MFF_IPV4_DST:
26720e24 888 return !wc->masks.nw_dst;
6a885fd0
BP
889
890 case MFF_IPV6_SRC:
26720e24 891 return ipv6_mask_is_any(&wc->masks.ipv6_src);
6a885fd0 892 case MFF_IPV6_DST:
26720e24 893 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
6a885fd0 894
32455024 895 case MFF_IPV6_LABEL:
26720e24 896 return !wc->masks.ipv6_label;
32455024 897
851d3105 898 case MFF_IP_PROTO:
26720e24 899 return !wc->masks.nw_proto;
5d9499c4 900 case MFF_IP_DSCP:
1638b906 901 case MFF_IP_DSCP_SHIFTED:
26720e24 902 return !(wc->masks.nw_tos & IP_DSCP_MASK);
5d9499c4 903 case MFF_IP_ECN:
26720e24 904 return !(wc->masks.nw_tos & IP_ECN_MASK);
3840c406 905 case MFF_IP_TTL:
26720e24 906 return !wc->masks.nw_ttl;
5d9499c4 907
47284b1f 908 case MFF_ND_TARGET:
26720e24 909 return ipv6_mask_is_any(&wc->masks.nd_target);
47284b1f 910
7257b535 911 case MFF_IP_FRAG:
26720e24 912 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
7257b535 913
851d3105 914 case MFF_ARP_OP:
26720e24 915 return !wc->masks.nw_proto;
6a885fd0 916 case MFF_ARP_SPA:
26720e24 917 return !wc->masks.nw_src;
6a885fd0 918 case MFF_ARP_TPA:
26720e24 919 return !wc->masks.nw_dst;
6a885fd0 920
73f33563
BP
921 case MFF_TCP_SRC:
922 case MFF_UDP_SRC:
0d56eaf2 923 case MFF_SCTP_SRC:
73f33563
BP
924 case MFF_ICMPV4_TYPE:
925 case MFF_ICMPV6_TYPE:
26720e24 926 return !wc->masks.tp_src;
73f33563
BP
927 case MFF_TCP_DST:
928 case MFF_UDP_DST:
0d56eaf2 929 case MFF_SCTP_DST:
73f33563
BP
930 case MFF_ICMPV4_CODE:
931 case MFF_ICMPV6_CODE:
26720e24 932 return !wc->masks.tp_dst;
dc235f7f
JR
933 case MFF_TCP_FLAGS:
934 return !wc->masks.tcp_flags;
73f33563 935
6a885fd0
BP
936 case MFF_N_IDS:
937 default:
938 NOT_REACHED();
939 }
940}
941
942/* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
943 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
944 * purposes, or to 0 if it is wildcarded.
945 *
946 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
947 * meets 'mf''s prerequisites. */
948void
949mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
950 union mf_value *mask)
951{
fb15feb0 952 mf_get_value(mf, &wc->masks, mask);
6a885fd0
BP
953}
954
955/* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
956 * if the mask is valid, false otherwise. */
957bool
958mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
959{
960 switch (mf->maskable) {
961 case MFM_NONE:
962 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
963 is_all_ones((const uint8_t *) mask, mf->n_bytes));
964
965 case MFM_FULLY:
966 return true;
6a885fd0
BP
967 }
968
969 NOT_REACHED();
970}
971
6a885fd0
BP
972static bool
973is_icmpv4(const struct flow *flow)
974{
975 return (flow->dl_type == htons(ETH_TYPE_IP)
976 && flow->nw_proto == IPPROTO_ICMP);
977}
978
979static bool
980is_icmpv6(const struct flow *flow)
981{
982 return (flow->dl_type == htons(ETH_TYPE_IPV6)
983 && flow->nw_proto == IPPROTO_ICMPV6);
984}
985
986/* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
987bool
988mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
989{
990 switch (mf->prereqs) {
991 case MFP_NONE:
992 return true;
993
994 case MFP_ARP:
8087f5ff
MM
995 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
996 flow->dl_type == htons(ETH_TYPE_RARP));
6a885fd0
BP
997 case MFP_IPV4:
998 return flow->dl_type == htons(ETH_TYPE_IP);
999 case MFP_IPV6:
1000 return flow->dl_type == htons(ETH_TYPE_IPV6);
8069b0da 1001 case MFP_VLAN_VID:
5921e99a 1002 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
b02475c5
SH
1003 case MFP_MPLS:
1004 return eth_type_mpls(flow->dl_type);
6a885fd0
BP
1005 case MFP_IP_ANY:
1006 return is_ip_any(flow);
1007
1008 case MFP_TCP:
1009 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
1010 case MFP_UDP:
1011 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
0d56eaf2
JS
1012 case MFP_SCTP:
1013 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP;
268a95e0
BP
1014 case MFP_ICMPV4:
1015 return is_icmpv4(flow);
6a885fd0
BP
1016 case MFP_ICMPV6:
1017 return is_icmpv6(flow);
6a885fd0
BP
1018
1019 case MFP_ND:
1020 return (is_icmpv6(flow)
3ee8a9f0
BP
1021 && flow->tp_dst == htons(0)
1022 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1023 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
6a885fd0
BP
1024 case MFP_ND_SOLICIT:
1025 return (is_icmpv6(flow)
3ee8a9f0
BP
1026 && flow->tp_dst == htons(0)
1027 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
6a885fd0
BP
1028 case MFP_ND_ADVERT:
1029 return (is_icmpv6(flow)
3ee8a9f0
BP
1030 && flow->tp_dst == htons(0)
1031 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
6a885fd0
BP
1032 }
1033
1034 NOT_REACHED();
1035}
1036
b283836c
JR
1037/* Set field and it's prerequisities in the mask.
1038 * This is only ever called for writeable 'mf's, but we do not make the
1039 * distinction here. */
1040void
1041mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow *mask)
1042{
1043 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
1044
1045 mf_set_flow_value(mf, &exact_match_mask, mask);
1046
1047 switch (mf->prereqs) {
1048 case MFP_ND:
1049 case MFP_ND_SOLICIT:
1050 case MFP_ND_ADVERT:
1051 mask->tp_src = OVS_BE16_MAX;
1052 mask->tp_dst = OVS_BE16_MAX;
1053 /* Fall through. */
1054 case MFP_TCP:
1055 case MFP_UDP:
1056 case MFP_SCTP:
1057 case MFP_ICMPV4:
1058 case MFP_ICMPV6:
1059 mask->nw_proto = 0xff;
1060 /* Fall through. */
1061 case MFP_ARP:
1062 case MFP_IPV4:
1063 case MFP_IPV6:
1064 case MFP_MPLS:
1065 case MFP_IP_ANY:
1066 mask->dl_type = OVS_BE16_MAX;
1067 break;
1068 case MFP_VLAN_VID:
1069 mask->vlan_tci |= htons(VLAN_CFI);
1070 break;
1071 case MFP_NONE:
1072 break;
1073 }
1074}
1075
1076
6a885fd0
BP
1077/* Returns true if 'value' may be a valid value *as part of a masked match*,
1078 * false otherwise.
1079 *
1080 * A value is not rejected just because it is not valid for the field in
1081 * question, but only if it doesn't make sense to test the bits in question at
1082 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
1083 * without the VLAN_CFI bit being set, but we can't reject those values because
1084 * it is still legitimate to test just for those bits (see the documentation
1085 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
530180fd 1086 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
6a885fd0
BP
1087bool
1088mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
1089{
1090 switch (mf->id) {
1091 case MFF_TUN_ID:
4fe3445a
PS
1092 case MFF_TUN_SRC:
1093 case MFF_TUN_DST:
1094 case MFF_TUN_TOS:
1095 case MFF_TUN_TTL:
1096 case MFF_TUN_FLAGS:
969fc56c 1097 case MFF_METADATA:
6a885fd0 1098 case MFF_IN_PORT:
1b567fb9 1099 case MFF_SKB_PRIORITY:
1362e248 1100 case MFF_PKT_MARK:
0d7e2fe4 1101 CASE_MFF_REGS:
6a885fd0
BP
1102 case MFF_ETH_SRC:
1103 case MFF_ETH_DST:
1104 case MFF_ETH_TYPE:
1105 case MFF_VLAN_TCI:
1106 case MFF_IPV4_SRC:
1107 case MFF_IPV4_DST:
1108 case MFF_IPV6_SRC:
1109 case MFF_IPV6_DST:
1110 case MFF_IP_PROTO:
a61680c6 1111 case MFF_IP_TTL:
6a885fd0
BP
1112 case MFF_ARP_SPA:
1113 case MFF_ARP_TPA:
1114 case MFF_ARP_SHA:
1115 case MFF_ARP_THA:
1116 case MFF_TCP_SRC:
1117 case MFF_TCP_DST:
1118 case MFF_UDP_SRC:
1119 case MFF_UDP_DST:
0d56eaf2
JS
1120 case MFF_SCTP_SRC:
1121 case MFF_SCTP_DST:
268a95e0
BP
1122 case MFF_ICMPV4_TYPE:
1123 case MFF_ICMPV4_CODE:
1124 case MFF_ICMPV6_TYPE:
1125 case MFF_ICMPV6_CODE:
6a885fd0
BP
1126 case MFF_ND_TARGET:
1127 case MFF_ND_SLL:
1128 case MFF_ND_TLL:
1129 return true;
1130
72333065 1131 case MFF_IN_PORT_OXM: {
4e022ec0 1132 ofp_port_t port;
72333065
JR
1133 return !ofputil_port_from_ofp11(value->be32, &port);
1134 }
1135
530180fd 1136 case MFF_IP_DSCP:
7257b535 1137 return !(value->u8 & ~IP_DSCP_MASK);
1638b906
BP
1138 case MFF_IP_DSCP_SHIFTED:
1139 return !(value->u8 & (~IP_DSCP_MASK >> 2));
530180fd
JP
1140 case MFF_IP_ECN:
1141 return !(value->u8 & ~IP_ECN_MASK);
7257b535 1142 case MFF_IP_FRAG:
eadef313 1143 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
dc235f7f
JR
1144 case MFF_TCP_FLAGS:
1145 return !(value->be16 & ~htons(0x0fff));
6a885fd0
BP
1146
1147 case MFF_ARP_OP:
1148 return !(value->be16 & htons(0xff00));
1149
441c57a9 1150 case MFF_DL_VLAN:
6a885fd0 1151 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
cc34bc8c
BP
1152 case MFF_VLAN_VID:
1153 return !(value->be16 & htons(VLAN_PCP_MASK));
6a885fd0 1154
441c57a9 1155 case MFF_DL_VLAN_PCP:
cc34bc8c 1156 case MFF_VLAN_PCP:
24016f5a 1157 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
6a885fd0 1158
fa8223b7
JP
1159 case MFF_IPV6_LABEL:
1160 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
1161
b02475c5
SH
1162 case MFF_MPLS_LABEL:
1163 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
1164
1165 case MFF_MPLS_TC:
1166 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
1167
1168 case MFF_MPLS_BOS:
1169 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
1170
6a885fd0
BP
1171 case MFF_N_IDS:
1172 default:
1173 NOT_REACHED();
1174 }
1175}
1176
1177/* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
1178 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
1179void
1180mf_get_value(const struct mf_field *mf, const struct flow *flow,
1181 union mf_value *value)
1182{
1183 switch (mf->id) {
1184 case MFF_TUN_ID:
296e07ac 1185 value->be64 = flow->tunnel.tun_id;
6a885fd0 1186 break;
4fe3445a
PS
1187 case MFF_TUN_SRC:
1188 value->be32 = flow->tunnel.ip_src;
1189 break;
1190 case MFF_TUN_DST:
1191 value->be32 = flow->tunnel.ip_dst;
1192 break;
1193 case MFF_TUN_FLAGS:
1194 value->be16 = htons(flow->tunnel.flags);
1195 break;
1196 case MFF_TUN_TTL:
1197 value->u8 = flow->tunnel.ip_ttl;
1198 break;
1199 case MFF_TUN_TOS:
1200 value->u8 = flow->tunnel.ip_tos;
1201 break;
1202
969fc56c
JS
1203 case MFF_METADATA:
1204 value->be64 = flow->metadata;
1205 break;
6a885fd0
BP
1206
1207 case MFF_IN_PORT:
4e022ec0 1208 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
6a885fd0 1209 break;
72333065 1210 case MFF_IN_PORT_OXM:
4e022ec0 1211 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
72333065
JR
1212 break;
1213
1b567fb9 1214 case MFF_SKB_PRIORITY:
a81f0a44 1215 value->be32 = htonl(flow->skb_priority);
1b567fb9
AA
1216 break;
1217
1362e248
JG
1218 case MFF_PKT_MARK:
1219 value->be32 = htonl(flow->pkt_mark);
1b567fb9
AA
1220 break;
1221
0d7e2fe4 1222 CASE_MFF_REGS:
2f98b0b7 1223 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
6a885fd0
BP
1224 break;
1225
1226 case MFF_ETH_SRC:
1227 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1228 break;
1229
1230 case MFF_ETH_DST:
1231 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1232 break;
1233
1234 case MFF_ETH_TYPE:
1235 value->be16 = flow->dl_type;
1236 break;
1237
1238 case MFF_VLAN_TCI:
1239 value->be16 = flow->vlan_tci;
1240 break;
1241
441c57a9 1242 case MFF_DL_VLAN:
6a885fd0
BP
1243 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1244 break;
cc34bc8c
BP
1245 case MFF_VLAN_VID:
1246 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
1247 break;
6a885fd0 1248
441c57a9 1249 case MFF_DL_VLAN_PCP:
cc34bc8c 1250 case MFF_VLAN_PCP:
6a885fd0
BP
1251 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1252 break;
1253
b02475c5
SH
1254 case MFF_MPLS_LABEL:
1255 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse));
1256 break;
1257
1258 case MFF_MPLS_TC:
1259 value->u8 = mpls_lse_to_tc(flow->mpls_lse);
1260 break;
1261
1262 case MFF_MPLS_BOS:
1263 value->u8 = mpls_lse_to_bos(flow->mpls_lse);
1264 break;
1265
6a885fd0
BP
1266 case MFF_IPV4_SRC:
1267 value->be32 = flow->nw_src;
1268 break;
1269
1270 case MFF_IPV4_DST:
1271 value->be32 = flow->nw_dst;
1272 break;
1273
1274 case MFF_IPV6_SRC:
1275 value->ipv6 = flow->ipv6_src;
1276 break;
1277
1278 case MFF_IPV6_DST:
1279 value->ipv6 = flow->ipv6_dst;
1280 break;
1281
fa8223b7
JP
1282 case MFF_IPV6_LABEL:
1283 value->be32 = flow->ipv6_label;
1284 break;
1285
6a885fd0
BP
1286 case MFF_IP_PROTO:
1287 value->u8 = flow->nw_proto;
1288 break;
1289
530180fd 1290 case MFF_IP_DSCP:
eadef313 1291 value->u8 = flow->nw_tos & IP_DSCP_MASK;
7257b535
BP
1292 break;
1293
1638b906
BP
1294 case MFF_IP_DSCP_SHIFTED:
1295 value->u8 = flow->nw_tos >> 2;
1296 break;
1297
530180fd 1298 case MFF_IP_ECN:
eadef313 1299 value->u8 = flow->nw_tos & IP_ECN_MASK;
530180fd
JP
1300 break;
1301
a61680c6
JP
1302 case MFF_IP_TTL:
1303 value->u8 = flow->nw_ttl;
1304 break;
1305
7257b535 1306 case MFF_IP_FRAG:
eadef313 1307 value->u8 = flow->nw_frag;
6a885fd0
BP
1308 break;
1309
1310 case MFF_ARP_OP:
1311 value->be16 = htons(flow->nw_proto);
1312 break;
1313
1314 case MFF_ARP_SPA:
1315 value->be32 = flow->nw_src;
1316 break;
1317
1318 case MFF_ARP_TPA:
1319 value->be32 = flow->nw_dst;
1320 break;
1321
1322 case MFF_ARP_SHA:
1323 case MFF_ND_SLL:
1324 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1325 break;
1326
1327 case MFF_ARP_THA:
1328 case MFF_ND_TLL:
1329 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1330 break;
1331
1332 case MFF_TCP_SRC:
6a885fd0 1333 case MFF_UDP_SRC:
0d56eaf2 1334 case MFF_SCTP_SRC:
6a885fd0
BP
1335 value->be16 = flow->tp_src;
1336 break;
1337
71baec06 1338 case MFF_TCP_DST:
6a885fd0 1339 case MFF_UDP_DST:
0d56eaf2 1340 case MFF_SCTP_DST:
6a885fd0
BP
1341 value->be16 = flow->tp_dst;
1342 break;
1343
dc235f7f
JR
1344 case MFF_TCP_FLAGS:
1345 value->be16 = flow->tcp_flags;
1346 break;
1347
268a95e0
BP
1348 case MFF_ICMPV4_TYPE:
1349 case MFF_ICMPV6_TYPE:
6a885fd0
BP
1350 value->u8 = ntohs(flow->tp_src);
1351 break;
1352
268a95e0
BP
1353 case MFF_ICMPV4_CODE:
1354 case MFF_ICMPV6_CODE:
6a885fd0
BP
1355 value->u8 = ntohs(flow->tp_dst);
1356 break;
1357
1358 case MFF_ND_TARGET:
1359 value->ipv6 = flow->nd_target;
1360 break;
1361
1362 case MFF_N_IDS:
1363 default:
1364 NOT_REACHED();
1365 }
1366}
1367
81a76618
BP
1368/* Makes 'match' match field 'mf' exactly, with the value matched taken from
1369 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
6a885fd0
BP
1370 * prerequisites. */
1371void
1372mf_set_value(const struct mf_field *mf,
81a76618 1373 const union mf_value *value, struct match *match)
6a885fd0
BP
1374{
1375 switch (mf->id) {
1376 case MFF_TUN_ID:
81a76618 1377 match_set_tun_id(match, value->be64);
6a885fd0 1378 break;
4fe3445a
PS
1379 case MFF_TUN_SRC:
1380 match_set_tun_src(match, value->be32);
1381 break;
1382 case MFF_TUN_DST:
1383 match_set_tun_dst(match, value->be32);
1384 break;
1385 case MFF_TUN_FLAGS:
1386 match_set_tun_flags(match, ntohs(value->be16));
1387 break;
1388 case MFF_TUN_TOS:
1389 match_set_tun_tos(match, value->u8);
1390 break;
1391 case MFF_TUN_TTL:
1392 match_set_tun_ttl(match, value->u8);
1393 break;
1394
969fc56c 1395 case MFF_METADATA:
81a76618 1396 match_set_metadata(match, value->be64);
969fc56c 1397 break;
6a885fd0
BP
1398
1399 case MFF_IN_PORT:
4e022ec0 1400 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
6a885fd0
BP
1401 break;
1402
72333065 1403 case MFF_IN_PORT_OXM: {
4e022ec0 1404 ofp_port_t port;
bc146369 1405 ofputil_port_from_ofp11(value->be32, &port);
72333065
JR
1406 match_set_in_port(match, port);
1407 break;
1408 }
1409
1b567fb9
AA
1410 case MFF_SKB_PRIORITY:
1411 match_set_skb_priority(match, ntohl(value->be32));
1412 break;
1413
1362e248
JG
1414 case MFF_PKT_MARK:
1415 match_set_pkt_mark(match, ntohl(value->be32));
1b567fb9
AA
1416 break;
1417
0d7e2fe4 1418 CASE_MFF_REGS:
81a76618 1419 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
6a885fd0 1420 break;
6a885fd0
BP
1421
1422 case MFF_ETH_SRC:
81a76618 1423 match_set_dl_src(match, value->mac);
6a885fd0
BP
1424 break;
1425
1426 case MFF_ETH_DST:
81a76618 1427 match_set_dl_dst(match, value->mac);
6a885fd0
BP
1428 break;
1429
1430 case MFF_ETH_TYPE:
81a76618 1431 match_set_dl_type(match, value->be16);
6a885fd0
BP
1432 break;
1433
1434 case MFF_VLAN_TCI:
81a76618 1435 match_set_dl_tci(match, value->be16);
6a885fd0
BP
1436 break;
1437
441c57a9 1438 case MFF_DL_VLAN:
81a76618 1439 match_set_dl_vlan(match, value->be16);
6a885fd0 1440 break;
cc34bc8c 1441 case MFF_VLAN_VID:
81a76618 1442 match_set_vlan_vid(match, value->be16);
cc34bc8c 1443 break;
6a885fd0 1444
441c57a9 1445 case MFF_DL_VLAN_PCP:
cc34bc8c 1446 case MFF_VLAN_PCP:
81a76618 1447 match_set_dl_vlan_pcp(match, value->u8);
6a885fd0
BP
1448 break;
1449
b02475c5
SH
1450 case MFF_MPLS_LABEL:
1451 match_set_mpls_label(match, value->be32);
1452 break;
1453
1454 case MFF_MPLS_TC:
1455 match_set_mpls_tc(match, value->u8);
1456 break;
1457
1458 case MFF_MPLS_BOS:
1459 match_set_mpls_bos(match, value->u8);
1460 break;
1461
6a885fd0 1462 case MFF_IPV4_SRC:
81a76618 1463 match_set_nw_src(match, value->be32);
6a885fd0
BP
1464 break;
1465
1466 case MFF_IPV4_DST:
81a76618 1467 match_set_nw_dst(match, value->be32);
6a885fd0
BP
1468 break;
1469
1470 case MFF_IPV6_SRC:
81a76618 1471 match_set_ipv6_src(match, &value->ipv6);
6a885fd0
BP
1472 break;
1473
1474 case MFF_IPV6_DST:
81a76618 1475 match_set_ipv6_dst(match, &value->ipv6);
6a885fd0
BP
1476 break;
1477
fa8223b7 1478 case MFF_IPV6_LABEL:
81a76618 1479 match_set_ipv6_label(match, value->be32);
fa8223b7
JP
1480 break;
1481
6a885fd0 1482 case MFF_IP_PROTO:
81a76618 1483 match_set_nw_proto(match, value->u8);
6a885fd0
BP
1484 break;
1485
530180fd 1486 case MFF_IP_DSCP:
81a76618 1487 match_set_nw_dscp(match, value->u8);
530180fd
JP
1488 break;
1489
1638b906
BP
1490 case MFF_IP_DSCP_SHIFTED:
1491 match_set_nw_dscp(match, value->u8 << 2);
1492 break;
1493
530180fd 1494 case MFF_IP_ECN:
81a76618 1495 match_set_nw_ecn(match, value->u8);
6a885fd0
BP
1496 break;
1497
a61680c6 1498 case MFF_IP_TTL:
81a76618 1499 match_set_nw_ttl(match, value->u8);
a61680c6
JP
1500 break;
1501
7257b535 1502 case MFF_IP_FRAG:
81a76618 1503 match_set_nw_frag(match, value->u8);
7257b535
BP
1504 break;
1505
6a885fd0 1506 case MFF_ARP_OP:
81a76618 1507 match_set_nw_proto(match, ntohs(value->be16));
6a885fd0
BP
1508 break;
1509
1510 case MFF_ARP_SPA:
81a76618 1511 match_set_nw_src(match, value->be32);
6a885fd0
BP
1512 break;
1513
1514 case MFF_ARP_TPA:
81a76618 1515 match_set_nw_dst(match, value->be32);
6a885fd0
BP
1516 break;
1517
1518 case MFF_ARP_SHA:
1519 case MFF_ND_SLL:
81a76618 1520 match_set_arp_sha(match, value->mac);
6a885fd0
BP
1521 break;
1522
1523 case MFF_ARP_THA:
1524 case MFF_ND_TLL:
81a76618 1525 match_set_arp_tha(match, value->mac);
6a885fd0
BP
1526 break;
1527
1528 case MFF_TCP_SRC:
6a885fd0 1529 case MFF_UDP_SRC:
0d56eaf2 1530 case MFF_SCTP_SRC:
81a76618 1531 match_set_tp_src(match, value->be16);
6a885fd0
BP
1532 break;
1533
71baec06 1534 case MFF_TCP_DST:
6a885fd0 1535 case MFF_UDP_DST:
0d56eaf2 1536 case MFF_SCTP_DST:
81a76618 1537 match_set_tp_dst(match, value->be16);
6a885fd0
BP
1538 break;
1539
dc235f7f
JR
1540 case MFF_TCP_FLAGS:
1541 match_set_tcp_flags(match, value->be16);
1542 break;
1543
268a95e0
BP
1544 case MFF_ICMPV4_TYPE:
1545 case MFF_ICMPV6_TYPE:
81a76618 1546 match_set_icmp_type(match, value->u8);
6a885fd0
BP
1547 break;
1548
268a95e0
BP
1549 case MFF_ICMPV4_CODE:
1550 case MFF_ICMPV6_CODE:
81a76618 1551 match_set_icmp_code(match, value->u8);
6a885fd0
BP
1552 break;
1553
1554 case MFF_ND_TARGET:
81a76618 1555 match_set_nd_target(match, &value->ipv6);
6a885fd0
BP
1556 break;
1557
1558 case MFF_N_IDS:
1559 default:
1560 NOT_REACHED();
1561 }
1562}
1563
5a0a5702
GS
1564/* Unwildcard 'mask' member field described by 'mf'. The caller is
1565 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1566void
1567mf_mask_field(const struct mf_field *mf, struct flow *mask)
1568{
1569 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
1570
1571 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1572 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1573 * special case. For the rest, calling mf_set_flow_value() is good
1574 * enough. */
1575 if (mf->id == MFF_DL_VLAN) {
1576 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1577 } else {
1578 mf_set_flow_value(mf, &exact_match_mask, mask);
1579 }
1580}
1581
910fb1d8
JP
1582/* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1583 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
28da1f8f
BP
1584void
1585mf_set_flow_value(const struct mf_field *mf,
1586 const union mf_value *value, struct flow *flow)
1587{
1588 switch (mf->id) {
1589 case MFF_TUN_ID:
296e07ac 1590 flow->tunnel.tun_id = value->be64;
28da1f8f 1591 break;
4fe3445a
PS
1592 case MFF_TUN_SRC:
1593 flow->tunnel.ip_src = value->be32;
1594 break;
1595 case MFF_TUN_DST:
1596 flow->tunnel.ip_dst = value->be32;
1597 break;
1598 case MFF_TUN_FLAGS:
1599 flow->tunnel.flags = ntohs(value->be16);
1600 break;
1601 case MFF_TUN_TOS:
1602 flow->tunnel.ip_tos = value->u8;
1603 break;
1604 case MFF_TUN_TTL:
1605 flow->tunnel.ip_ttl = value->u8;
1606 break;
1607
969fc56c
JS
1608 case MFF_METADATA:
1609 flow->metadata = value->be64;
1610 break;
28da1f8f
BP
1611
1612 case MFF_IN_PORT:
4e022ec0 1613 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
28da1f8f
BP
1614 break;
1615
72333065 1616 case MFF_IN_PORT_OXM: {
4e022ec0 1617 ofp_port_t port;
bc146369 1618 ofputil_port_from_ofp11(value->be32, &port);
4e022ec0 1619 flow->in_port.ofp_port = port;
72333065
JR
1620 break;
1621 }
1622
1b567fb9
AA
1623 case MFF_SKB_PRIORITY:
1624 flow->skb_priority = ntohl(value->be32);
1625 break;
1626
1362e248
JG
1627 case MFF_PKT_MARK:
1628 flow->pkt_mark = ntohl(value->be32);
1b567fb9
AA
1629 break;
1630
0d7e2fe4 1631 CASE_MFF_REGS:
28da1f8f
BP
1632 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1633 break;
28da1f8f
BP
1634
1635 case MFF_ETH_SRC:
1636 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1637 break;
1638
1639 case MFF_ETH_DST:
b5a36e38 1640 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
28da1f8f
BP
1641 break;
1642
1643 case MFF_ETH_TYPE:
1644 flow->dl_type = value->be16;
1645 break;
1646
1647 case MFF_VLAN_TCI:
1648 flow->vlan_tci = value->be16;
1649 break;
1650
441c57a9 1651 case MFF_DL_VLAN:
fb0451d9 1652 flow_set_dl_vlan(flow, value->be16);
28da1f8f 1653 break;
cc34bc8c
BP
1654 case MFF_VLAN_VID:
1655 flow_set_vlan_vid(flow, value->be16);
1656 break;
28da1f8f 1657
441c57a9 1658 case MFF_DL_VLAN_PCP:
cc34bc8c 1659 case MFF_VLAN_PCP:
28da1f8f
BP
1660 flow_set_vlan_pcp(flow, value->u8);
1661 break;
1662
b02475c5
SH
1663 case MFF_MPLS_LABEL:
1664 flow_set_mpls_label(flow, value->be32);
1665 break;
1666
1667 case MFF_MPLS_TC:
1668 flow_set_mpls_tc(flow, value->u8);
1669 break;
1670
1671 case MFF_MPLS_BOS:
1672 flow_set_mpls_bos(flow, value->u8);
1673 break;
1674
28da1f8f
BP
1675 case MFF_IPV4_SRC:
1676 flow->nw_src = value->be32;
1677 break;
1678
1679 case MFF_IPV4_DST:
1680 flow->nw_dst = value->be32;
1681 break;
1682
1683 case MFF_IPV6_SRC:
1684 flow->ipv6_src = value->ipv6;
1685 break;
1686
1687 case MFF_IPV6_DST:
1688 flow->ipv6_dst = value->ipv6;
1689 break;
1690
1691 case MFF_IPV6_LABEL:
1692 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1693 break;
1694
1695 case MFF_IP_PROTO:
1696 flow->nw_proto = value->u8;
1697 break;
1698
1699 case MFF_IP_DSCP:
1700 flow->nw_tos &= ~IP_DSCP_MASK;
1701 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1702 break;
1703
1638b906
BP
1704 case MFF_IP_DSCP_SHIFTED:
1705 flow->nw_tos &= ~IP_DSCP_MASK;
1706 flow->nw_tos |= value->u8 << 2;
1707 break;
1708
28da1f8f
BP
1709 case MFF_IP_ECN:
1710 flow->nw_tos &= ~IP_ECN_MASK;
1711 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1712 break;
1713
1714 case MFF_IP_TTL:
1715 flow->nw_ttl = value->u8;
1716 break;
1717
1718 case MFF_IP_FRAG:
1719 flow->nw_frag &= value->u8;
1720 break;
1721
1722 case MFF_ARP_OP:
1723 flow->nw_proto = ntohs(value->be16);
1724 break;
1725
1726 case MFF_ARP_SPA:
1727 flow->nw_src = value->be32;
1728 break;
1729
1730 case MFF_ARP_TPA:
1731 flow->nw_dst = value->be32;
1732 break;
1733
1734 case MFF_ARP_SHA:
1735 case MFF_ND_SLL:
1736 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1737 break;
1738
1739 case MFF_ARP_THA:
1740 case MFF_ND_TLL:
1741 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1742 break;
1743
1744 case MFF_TCP_SRC:
1745 case MFF_UDP_SRC:
0d56eaf2 1746 case MFF_SCTP_SRC:
28da1f8f
BP
1747 flow->tp_src = value->be16;
1748 break;
1749
1750 case MFF_TCP_DST:
1751 case MFF_UDP_DST:
0d56eaf2 1752 case MFF_SCTP_DST:
28da1f8f
BP
1753 flow->tp_dst = value->be16;
1754 break;
1755
dc235f7f
JR
1756 case MFF_TCP_FLAGS:
1757 flow->tcp_flags = value->be16;
1758 break;
1759
28da1f8f
BP
1760 case MFF_ICMPV4_TYPE:
1761 case MFF_ICMPV6_TYPE:
1762 flow->tp_src = htons(value->u8);
1763 break;
1764
1765 case MFF_ICMPV4_CODE:
1766 case MFF_ICMPV6_CODE:
1767 flow->tp_dst = htons(value->u8);
1768 break;
1769
1770 case MFF_ND_TARGET:
1771 flow->nd_target = value->ipv6;
1772 break;
1773
1774 case MFF_N_IDS:
1775 default:
1776 NOT_REACHED();
1777 }
1778}
1779
ccbe50f8
BP
1780/* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1781 *
1782 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1783 * prerequisites. */
1784bool
1785mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1786{
1787 union mf_value value;
1788
1789 mf_get_value(mf, flow, &value);
1790 return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1791}
1792
81a76618 1793/* Makes 'match' wildcard field 'mf'.
6a885fd0 1794 *
81a76618 1795 * The caller is responsible for ensuring that 'match' meets 'mf''s
6a885fd0
BP
1796 * prerequisites. */
1797void
81a76618 1798mf_set_wild(const struct mf_field *mf, struct match *match)
6a885fd0
BP
1799{
1800 switch (mf->id) {
1801 case MFF_TUN_ID:
81a76618 1802 match_set_tun_id_masked(match, htonll(0), htonll(0));
6a885fd0 1803 break;
4fe3445a
PS
1804 case MFF_TUN_SRC:
1805 match_set_tun_src_masked(match, htonl(0), htonl(0));
1806 break;
1807 case MFF_TUN_DST:
1808 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1809 break;
1810 case MFF_TUN_FLAGS:
1811 match_set_tun_flags_masked(match, 0, 0);
1812 break;
1813 case MFF_TUN_TOS:
1814 match_set_tun_tos_masked(match, 0, 0);
1815 break;
1816 case MFF_TUN_TTL:
1817 match_set_tun_ttl_masked(match, 0, 0);
1818 break;
1819
969fc56c 1820 case MFF_METADATA:
81a76618 1821 match_set_metadata_masked(match, htonll(0), htonll(0));
801d2b2a 1822 break;
6a885fd0
BP
1823
1824 case MFF_IN_PORT:
72333065 1825 case MFF_IN_PORT_OXM:
4e022ec0
AW
1826 match->flow.in_port.ofp_port = 0;
1827 match->wc.masks.in_port.ofp_port = 0;
6a885fd0
BP
1828 break;
1829
1b567fb9
AA
1830 case MFF_SKB_PRIORITY:
1831 match->flow.skb_priority = 0;
1832 match->wc.masks.skb_priority = 0;
1833 break;
1834
1362e248
JG
1835 case MFF_PKT_MARK:
1836 match->flow.pkt_mark = 0;
1837 match->wc.masks.pkt_mark = 0;
1b567fb9
AA
1838 break;
1839
0d7e2fe4 1840 CASE_MFF_REGS:
81a76618 1841 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
e9358af6 1842 break;
6a885fd0
BP
1843
1844 case MFF_ETH_SRC:
81a76618
BP
1845 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1846 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
6a885fd0
BP
1847 break;
1848
1849 case MFF_ETH_DST:
81a76618
BP
1850 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1851 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
6a885fd0
BP
1852 break;
1853
1854 case MFF_ETH_TYPE:
81a76618
BP
1855 match->flow.dl_type = htons(0);
1856 match->wc.masks.dl_type = htons(0);
6a885fd0
BP
1857 break;
1858
1859 case MFF_VLAN_TCI:
81a76618 1860 match_set_dl_tci_masked(match, htons(0), htons(0));
6a885fd0
BP
1861 break;
1862
441c57a9 1863 case MFF_DL_VLAN:
cc34bc8c 1864 case MFF_VLAN_VID:
81a76618 1865 match_set_any_vid(match);
6a885fd0
BP
1866 break;
1867
441c57a9 1868 case MFF_DL_VLAN_PCP:
cc34bc8c 1869 case MFF_VLAN_PCP:
81a76618 1870 match_set_any_pcp(match);
6a885fd0
BP
1871 break;
1872
b02475c5
SH
1873 case MFF_MPLS_LABEL:
1874 match_set_any_mpls_label(match);
1875 break;
1876
1877 case MFF_MPLS_TC:
1878 match_set_any_mpls_tc(match);
1879 break;
1880
1881 case MFF_MPLS_BOS:
1882 match_set_any_mpls_bos(match);
1883 break;
1884
6a885fd0
BP
1885 case MFF_IPV4_SRC:
1886 case MFF_ARP_SPA:
81a76618 1887 match_set_nw_src_masked(match, htonl(0), htonl(0));
6a885fd0
BP
1888 break;
1889
1890 case MFF_IPV4_DST:
1891 case MFF_ARP_TPA:
81a76618 1892 match_set_nw_dst_masked(match, htonl(0), htonl(0));
6a885fd0
BP
1893 break;
1894
1895 case MFF_IPV6_SRC:
81a76618
BP
1896 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1897 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
6a885fd0
BP
1898 break;
1899
1900 case MFF_IPV6_DST:
81a76618
BP
1901 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1902 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
6a885fd0
BP
1903 break;
1904
fa8223b7 1905 case MFF_IPV6_LABEL:
81a76618
BP
1906 match->wc.masks.ipv6_label = htonl(0);
1907 match->flow.ipv6_label = htonl(0);
fa8223b7
JP
1908 break;
1909
6a885fd0 1910 case MFF_IP_PROTO:
81a76618
BP
1911 match->wc.masks.nw_proto = 0;
1912 match->flow.nw_proto = 0;
6a885fd0
BP
1913 break;
1914
530180fd 1915 case MFF_IP_DSCP:
1638b906 1916 case MFF_IP_DSCP_SHIFTED:
81a76618
BP
1917 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1918 match->flow.nw_tos &= ~IP_DSCP_MASK;
7257b535
BP
1919 break;
1920
530180fd 1921 case MFF_IP_ECN:
81a76618
BP
1922 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1923 match->flow.nw_tos &= ~IP_ECN_MASK;
530180fd
JP
1924 break;
1925
a61680c6 1926 case MFF_IP_TTL:
81a76618
BP
1927 match->wc.masks.nw_ttl = 0;
1928 match->flow.nw_ttl = 0;
a61680c6
JP
1929 break;
1930
7257b535 1931 case MFF_IP_FRAG:
81a76618
BP
1932 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1933 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
6a885fd0
BP
1934 break;
1935
1936 case MFF_ARP_OP:
81a76618
BP
1937 match->wc.masks.nw_proto = 0;
1938 match->flow.nw_proto = 0;
6a885fd0
BP
1939 break;
1940
1941 case MFF_ARP_SHA:
1942 case MFF_ND_SLL:
81a76618
BP
1943 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1944 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
6a885fd0
BP
1945 break;
1946
1947 case MFF_ARP_THA:
1948 case MFF_ND_TLL:
81a76618
BP
1949 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1950 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
6a885fd0
BP
1951 break;
1952
1953 case MFF_TCP_SRC:
1954 case MFF_UDP_SRC:
0d56eaf2 1955 case MFF_SCTP_SRC:
268a95e0
BP
1956 case MFF_ICMPV4_TYPE:
1957 case MFF_ICMPV6_TYPE:
81a76618
BP
1958 match->wc.masks.tp_src = htons(0);
1959 match->flow.tp_src = htons(0);
6a885fd0
BP
1960 break;
1961
1962 case MFF_TCP_DST:
1963 case MFF_UDP_DST:
0d56eaf2 1964 case MFF_SCTP_DST:
268a95e0
BP
1965 case MFF_ICMPV4_CODE:
1966 case MFF_ICMPV6_CODE:
81a76618
BP
1967 match->wc.masks.tp_dst = htons(0);
1968 match->flow.tp_dst = htons(0);
6a885fd0
BP
1969 break;
1970
dc235f7f
JR
1971 case MFF_TCP_FLAGS:
1972 match->wc.masks.tcp_flags = htons(0);
1973 match->flow.tcp_flags = htons(0);
1974 break;
1975
6a885fd0 1976 case MFF_ND_TARGET:
81a76618
BP
1977 memset(&match->wc.masks.nd_target, 0,
1978 sizeof match->wc.masks.nd_target);
1979 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
6a885fd0
BP
1980 break;
1981
1982 case MFF_N_IDS:
1983 default:
1984 NOT_REACHED();
1985 }
1986}
1987
81a76618 1988/* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
6a885fd0
BP
1989 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1990 * with a 1-bit indicating that the corresponding value bit must match and a
1991 * 0-bit indicating a don't-care.
1992 *
1993 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
81a76618
BP
1994 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1995 * call is equivalent to mf_set_wild(mf, match).
6a885fd0
BP
1996 *
1997 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
81a76618 1998 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
db0b6c29 1999enum ofputil_protocol
6a885fd0
BP
2000mf_set(const struct mf_field *mf,
2001 const union mf_value *value, const union mf_value *mask,
81a76618 2002 struct match *match)
6a885fd0
BP
2003{
2004 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
81a76618 2005 mf_set_value(mf, value, match);
db0b6c29 2006 return mf->usable_protocols;
6a885fd0 2007 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
81a76618 2008 mf_set_wild(mf, match);
db0b6c29 2009 return OFPUTIL_P_ANY;
6a885fd0
BP
2010 }
2011
2012 switch (mf->id) {
2013 case MFF_IN_PORT:
72333065 2014 case MFF_IN_PORT_OXM:
1b567fb9 2015 case MFF_SKB_PRIORITY:
6a885fd0 2016 case MFF_ETH_TYPE:
441c57a9
BP
2017 case MFF_DL_VLAN:
2018 case MFF_DL_VLAN_PCP:
cc34bc8c 2019 case MFF_VLAN_PCP:
b02475c5
SH
2020 case MFF_MPLS_LABEL:
2021 case MFF_MPLS_TC:
2022 case MFF_MPLS_BOS:
6a885fd0 2023 case MFF_IP_PROTO:
a61680c6 2024 case MFF_IP_TTL:
530180fd 2025 case MFF_IP_DSCP:
1638b906 2026 case MFF_IP_DSCP_SHIFTED:
530180fd 2027 case MFF_IP_ECN:
6a885fd0 2028 case MFF_ARP_OP:
268a95e0
BP
2029 case MFF_ICMPV4_TYPE:
2030 case MFF_ICMPV4_CODE:
2031 case MFF_ICMPV6_TYPE:
2032 case MFF_ICMPV6_CODE:
af69bf19 2033 return OFPUTIL_P_NONE;
6a885fd0
BP
2034
2035 case MFF_TUN_ID:
81a76618 2036 match_set_tun_id_masked(match, value->be64, mask->be64);
6a885fd0 2037 break;
4fe3445a
PS
2038 case MFF_TUN_SRC:
2039 match_set_tun_src_masked(match, value->be32, mask->be32);
2040 break;
2041 case MFF_TUN_DST:
2042 match_set_tun_dst_masked(match, value->be32, mask->be32);
2043 break;
2044 case MFF_TUN_FLAGS:
2045 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
2046 break;
2047 case MFF_TUN_TTL:
2048 match_set_tun_ttl_masked(match, value->u8, mask->u8);
2049 break;
2050 case MFF_TUN_TOS:
2051 match_set_tun_tos_masked(match, value->u8, mask->u8);
2052 break;
2053
969fc56c 2054 case MFF_METADATA:
81a76618 2055 match_set_metadata_masked(match, value->be64, mask->be64);
969fc56c 2056 break;
6a885fd0 2057
0d7e2fe4 2058 CASE_MFF_REGS:
81a76618
BP
2059 match_set_reg_masked(match, mf->id - MFF_REG0,
2060 ntohl(value->be32), ntohl(mask->be32));
6a885fd0 2061 break;
ac923e91
JG
2062
2063 case MFF_PKT_MARK:
2064 match_set_pkt_mark_masked(match, ntohl(value->be32),
2065 ntohl(mask->be32));
2066 break;
6a885fd0
BP
2067
2068 case MFF_ETH_DST:
81a76618 2069 match_set_dl_dst_masked(match, value->mac, mask->mac);
73c0ce34
JS
2070 break;
2071
2072 case MFF_ETH_SRC:
81a76618 2073 match_set_dl_src_masked(match, value->mac, mask->mac);
6a885fd0
BP
2074 break;
2075
e878338b
SH
2076 case MFF_ARP_SHA:
2077 case MFF_ND_SLL:
81a76618 2078 match_set_arp_sha_masked(match, value->mac, mask->mac);
e878338b
SH
2079 break;
2080
2081 case MFF_ARP_THA:
2082 case MFF_ND_TLL:
81a76618 2083 match_set_arp_tha_masked(match, value->mac, mask->mac);
e878338b
SH
2084 break;
2085
6a885fd0 2086 case MFF_VLAN_TCI:
81a76618 2087 match_set_dl_tci_masked(match, value->be16, mask->be16);
6a885fd0
BP
2088 break;
2089
cc34bc8c 2090 case MFF_VLAN_VID:
81a76618 2091 match_set_vlan_vid_masked(match, value->be16, mask->be16);
cc34bc8c
BP
2092 break;
2093
6a885fd0 2094 case MFF_IPV4_SRC:
81a76618 2095 match_set_nw_src_masked(match, value->be32, mask->be32);
db0b6c29 2096 goto cidr_check;
6a885fd0
BP
2097
2098 case MFF_IPV4_DST:
81a76618 2099 match_set_nw_dst_masked(match, value->be32, mask->be32);
db0b6c29 2100 goto cidr_check;
6a885fd0
BP
2101
2102 case MFF_IPV6_SRC:
81a76618 2103 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
6a885fd0
BP
2104 break;
2105
2106 case MFF_IPV6_DST:
81a76618 2107 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
6a885fd0
BP
2108 break;
2109
32455024
SH
2110 case MFF_IPV6_LABEL:
2111 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
81a76618 2112 mf_set_value(mf, value, match);
32455024 2113 } else {
81a76618 2114 match_set_ipv6_label_masked(match, value->be32, mask->be32);
32455024
SH
2115 }
2116 break;
2117
47284b1f 2118 case MFF_ND_TARGET:
81a76618 2119 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
47284b1f
AA
2120 break;
2121
7257b535 2122 case MFF_IP_FRAG:
81a76618 2123 match_set_nw_frag_masked(match, value->u8, mask->u8);
7257b535
BP
2124 break;
2125
6a885fd0 2126 case MFF_ARP_SPA:
81a76618 2127 match_set_nw_src_masked(match, value->be32, mask->be32);
db0b6c29 2128 goto cidr_check;
6a885fd0
BP
2129
2130 case MFF_ARP_TPA:
81a76618 2131 match_set_nw_dst_masked(match, value->be32, mask->be32);
db0b6c29 2132 goto cidr_check;
6a885fd0 2133
73f33563
BP
2134 case MFF_TCP_SRC:
2135 case MFF_UDP_SRC:
0d56eaf2 2136 case MFF_SCTP_SRC:
81a76618 2137 match_set_tp_src_masked(match, value->be16, mask->be16);
73f33563
BP
2138 break;
2139
2140 case MFF_TCP_DST:
2141 case MFF_UDP_DST:
0d56eaf2 2142 case MFF_SCTP_DST:
81a76618 2143 match_set_tp_dst_masked(match, value->be16, mask->be16);
73f33563
BP
2144 break;
2145
dc235f7f
JR
2146 case MFF_TCP_FLAGS:
2147 match_set_tcp_flags_masked(match, value->be16, mask->be16);
2148 break;
2149
6a885fd0
BP
2150 case MFF_N_IDS:
2151 default:
2152 NOT_REACHED();
2153 }
db0b6c29
JR
2154
2155 return mf->usable_protocols_bitwise;
2156
2157cidr_check:
2158 return ip_is_cidr(mask->be32) ? mf->usable_protocols :
2159 mf->usable_protocols_bitwise;
6a885fd0
BP
2160}
2161
816fd533
BP
2162static enum ofperr
2163mf_check__(const struct mf_subfield *sf, const struct flow *flow,
2164 const char *type)
6a885fd0 2165{
816fd533
BP
2166 if (!sf->field) {
2167 VLOG_WARN_RL(&rl, "unknown %s field", type);
9e404d1e 2168 return OFPERR_OFPBAC_BAD_SET_TYPE;
816fd533
BP
2169 } else if (!sf->n_bits) {
2170 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
9e404d1e 2171 return OFPERR_OFPBAC_BAD_SET_LEN;
816fd533
BP
2172 } else if (sf->ofs >= sf->field->n_bits) {
2173 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
2174 sf->ofs, sf->field->n_bits, type, sf->field->name);
9e404d1e 2175 return OFPERR_OFPBAC_BAD_SET_LEN;
816fd533
BP
2176 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
2177 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
2178 "of %s field %s", sf->ofs, sf->n_bits,
2179 sf->field->n_bits, type, sf->field->name);
9e404d1e 2180 return OFPERR_OFPBAC_BAD_SET_LEN;
816fd533
BP
2181 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
2182 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
2183 type, sf->field->name);
9e404d1e 2184 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
6a885fd0 2185 } else {
816fd533
BP
2186 return 0;
2187 }
816fd533 2188}
6a885fd0 2189
816fd533
BP
2190/* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2191 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2192 * ofp_mkerr()). */
2193enum ofperr
2194mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
2195{
2196 return mf_check__(sf, flow, "source");
2197}
6a885fd0 2198
816fd533
BP
2199/* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2200 * if so, otherwise an OpenFlow error code (e.g. as returned by
2201 * ofp_mkerr()). */
2202enum ofperr
2203mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
2204{
2205 int error = mf_check__(sf, flow, "destination");
2206 if (!error && !sf->field->writable) {
2207 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2208 sf->field->name);
9e404d1e 2209 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6a885fd0 2210 }
816fd533 2211 return error;
6a885fd0
BP
2212}
2213
81a76618 2214/* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
6a885fd0
BP
2215 * 'value' and 'mask', respectively. */
2216void
81a76618 2217mf_get(const struct mf_field *mf, const struct match *match,
6a885fd0
BP
2218 union mf_value *value, union mf_value *mask)
2219{
81a76618
BP
2220 mf_get_value(mf, &match->flow, value);
2221 mf_get_mask(mf, &match->wc, mask);
6a885fd0
BP
2222}
2223
2224/* Assigns a random value for field 'mf' to 'value'. */
2225void
2226mf_random_value(const struct mf_field *mf, union mf_value *value)
2227{
2228 random_bytes(value, mf->n_bytes);
2229
2230 switch (mf->id) {
2231 case MFF_TUN_ID:
4fe3445a
PS
2232 case MFF_TUN_SRC:
2233 case MFF_TUN_DST:
2234 case MFF_TUN_TOS:
2235 case MFF_TUN_TTL:
2236 case MFF_TUN_FLAGS:
969fc56c 2237 case MFF_METADATA:
6a885fd0 2238 case MFF_IN_PORT:
1362e248 2239 case MFF_PKT_MARK:
1b567fb9 2240 case MFF_SKB_PRIORITY:
0d7e2fe4 2241 CASE_MFF_REGS:
6a885fd0
BP
2242 case MFF_ETH_SRC:
2243 case MFF_ETH_DST:
2244 case MFF_ETH_TYPE:
2245 case MFF_VLAN_TCI:
2246 case MFF_IPV4_SRC:
2247 case MFF_IPV4_DST:
2248 case MFF_IPV6_SRC:
2249 case MFF_IPV6_DST:
2250 case MFF_IP_PROTO:
a61680c6 2251 case MFF_IP_TTL:
6a885fd0
BP
2252 case MFF_ARP_SPA:
2253 case MFF_ARP_TPA:
2254 case MFF_ARP_SHA:
2255 case MFF_ARP_THA:
2256 case MFF_TCP_SRC:
2257 case MFF_TCP_DST:
2258 case MFF_UDP_SRC:
2259 case MFF_UDP_DST:
0d56eaf2
JS
2260 case MFF_SCTP_SRC:
2261 case MFF_SCTP_DST:
268a95e0
BP
2262 case MFF_ICMPV4_TYPE:
2263 case MFF_ICMPV4_CODE:
2264 case MFF_ICMPV6_TYPE:
2265 case MFF_ICMPV6_CODE:
6a885fd0
BP
2266 case MFF_ND_TARGET:
2267 case MFF_ND_SLL:
2268 case MFF_ND_TLL:
2269 break;
2270
dc235f7f
JR
2271 case MFF_TCP_FLAGS:
2272 value->be16 &= htons(0x0fff);
2273 break;
2274
72333065 2275 case MFF_IN_PORT_OXM:
4e022ec0 2276 value->be32 = ofputil_port_to_ofp11(u16_to_ofp(ntohs(value->be16)));
72333065
JR
2277 break;
2278
fa8223b7 2279 case MFF_IPV6_LABEL:
ec45f343 2280 value->be32 &= htonl(IPV6_LABEL_MASK);
fa8223b7
JP
2281 break;
2282
530180fd
JP
2283 case MFF_IP_DSCP:
2284 value->u8 &= IP_DSCP_MASK;
2285 break;
2286
1638b906
BP
2287 case MFF_IP_DSCP_SHIFTED:
2288 value->u8 &= IP_DSCP_MASK >> 2;
2289 break;
2290
530180fd
JP
2291 case MFF_IP_ECN:
2292 value->u8 &= IP_ECN_MASK;
6a885fd0
BP
2293 break;
2294
7257b535 2295 case MFF_IP_FRAG:
eadef313 2296 value->u8 &= FLOW_NW_FRAG_MASK;
7257b535
BP
2297 break;
2298
6a885fd0
BP
2299 case MFF_ARP_OP:
2300 value->be16 &= htons(0xff);
2301 break;
2302
441c57a9 2303 case MFF_DL_VLAN:
6a885fd0
BP
2304 value->be16 &= htons(VLAN_VID_MASK);
2305 break;
cc34bc8c
BP
2306 case MFF_VLAN_VID:
2307 value->be16 &= htons(VLAN_VID_MASK | VLAN_CFI);
2308 break;
6a885fd0 2309
441c57a9 2310 case MFF_DL_VLAN_PCP:
cc34bc8c 2311 case MFF_VLAN_PCP:
6a885fd0
BP
2312 value->u8 &= 0x07;
2313 break;
2314
b02475c5
SH
2315 case MFF_MPLS_LABEL:
2316 value->be32 &= htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT);
2317 break;
2318
2319 case MFF_MPLS_TC:
2320 value->u8 &= MPLS_TC_MASK >> MPLS_TC_SHIFT;
2321 break;
2322
2323 case MFF_MPLS_BOS:
2324 value->u8 &= MPLS_BOS_MASK >> MPLS_BOS_SHIFT;
2325 break;
2326
6a885fd0
BP
2327 case MFF_N_IDS:
2328 default:
2329 NOT_REACHED();
2330 }
2331}
2332
2333static char *
2334mf_from_integer_string(const struct mf_field *mf, const char *s,
2335 uint8_t *valuep, uint8_t *maskp)
2336{
2337 unsigned long long int integer, mask;
2338 char *tail;
2339 int i;
2340
2341 errno = 0;
2342 integer = strtoull(s, &tail, 0);
2343 if (errno || (*tail != '\0' && *tail != '/')) {
2344 goto syntax_error;
2345 }
2346
2347 if (*tail == '/') {
2348 mask = strtoull(tail + 1, &tail, 0);
2349 if (errno || *tail != '\0') {
2350 goto syntax_error;
2351 }
2352 } else {
2353 mask = ULLONG_MAX;
2354 }
2355
2356 for (i = mf->n_bytes - 1; i >= 0; i--) {
2357 valuep[i] = integer;
2358 maskp[i] = mask;
2359 integer >>= 8;
2360 mask >>= 8;
2361 }
2362 if (integer) {
2363 return xasprintf("%s: value too large for %u-byte field %s",
2364 s, mf->n_bytes, mf->name);
2365 }
2366 return NULL;
2367
2368syntax_error:
2369 return xasprintf("%s: bad syntax for %s", s, mf->name);
2370}
2371
2372static char *
2373mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2374 uint8_t mac[ETH_ADDR_LEN],
2375 uint8_t mask[ETH_ADDR_LEN])
2376{
279fb135 2377 int n;
6a885fd0 2378
279fb135 2379 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
6a885fd0 2380
279fb135
BP
2381 n = -1;
2382 if (sscanf(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n) > 0
2383 && n == strlen(s)) {
6a885fd0
BP
2384 memset(mask, 0xff, ETH_ADDR_LEN);
2385 return NULL;
279fb135 2386 }
6a885fd0 2387
279fb135
BP
2388 n = -1;
2389 if (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
2390 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n) > 0
2391 && n == strlen(s)) {
2392 return NULL;
6a885fd0 2393 }
279fb135
BP
2394
2395 return xasprintf("%s: invalid Ethernet address", s);
6a885fd0
BP
2396}
2397
2398static char *
2399mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2400 ovs_be32 *ip, ovs_be32 *mask)
2401{
2402 int prefix;
2403
cb22974d 2404 ovs_assert(mf->n_bytes == sizeof *ip);
6a885fd0
BP
2405
2406 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2407 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
2408 /* OK. */
2409 } else if (sscanf(s, IP_SCAN_FMT"/%d",
2410 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
2411 if (prefix <= 0 || prefix > 32) {
2412 return xasprintf("%s: network prefix bits not between 1 and "
2413 "32", s);
2414 } else if (prefix == 32) {
b8266395 2415 *mask = OVS_BE32_MAX;
6a885fd0
BP
2416 } else {
2417 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2418 }
2419 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
b8266395 2420 *mask = OVS_BE32_MAX;
6a885fd0
BP
2421 } else {
2422 return xasprintf("%s: invalid IP address", s);
2423 }
2424 return NULL;
2425}
2426
2427static char *
2428mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2429 struct in6_addr *value, struct in6_addr *mask)
2430{
2431 char *str = xstrdup(s);
2432 char *save_ptr = NULL;
2433 const char *name, *netmask;
2434 int retval;
2435
cb22974d 2436 ovs_assert(mf->n_bytes == sizeof *value);
6a885fd0
BP
2437
2438 name = strtok_r(str, "/", &save_ptr);
2439 retval = name ? lookup_ipv6(name, value) : EINVAL;
2440 if (retval) {
2441 char *err;
2442
2443 err = xasprintf("%s: could not convert to IPv6 address", str);
2444 free(str);
2445
2446 return err;
2447 }
2448
2449 netmask = strtok_r(NULL, "/", &save_ptr);
2450 if (netmask) {
ff0b06ee
BP
2451 if (inet_pton(AF_INET6, netmask, mask) != 1) {
2452 int prefix = atoi(netmask);
2453 if (prefix <= 0 || prefix > 128) {
2454 free(str);
2455 return xasprintf("%s: prefix bits not between 1 and 128", s);
2456 } else {
2457 *mask = ipv6_create_mask(prefix);
2458 }
6a885fd0
BP
2459 }
2460 } else {
2461 *mask = in6addr_exact;
2462 }
2463 free(str);
2464
2465 return NULL;
2466}
2467
2468static char *
2469mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2470 ovs_be16 *valuep, ovs_be16 *maskp)
2471{
4e022ec0 2472 ofp_port_t port;
6a885fd0 2473
cb22974d 2474 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
33ab38d9
JR
2475
2476 if (ofputil_port_from_string(s, &port)) {
4e022ec0 2477 *valuep = htons(ofp_to_u16(port));
b8266395 2478 *maskp = OVS_BE16_MAX;
6a885fd0 2479 return NULL;
6a885fd0 2480 }
33ab38d9 2481 return xasprintf("%s: port value out of range for %s", s, mf->name);
6a885fd0
BP
2482}
2483
72333065
JR
2484static char *
2485mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2486 ovs_be32 *valuep, ovs_be32 *maskp)
2487{
4e022ec0 2488 ofp_port_t port;
72333065
JR
2489
2490 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2491 if (ofputil_port_from_string(s, &port)) {
2492 *valuep = ofputil_port_to_ofp11(port);
b8266395 2493 *maskp = OVS_BE32_MAX;
72333065
JR
2494 return NULL;
2495 }
2496 return xasprintf("%s: port value out of range for %s", s, mf->name);
2497}
2498
7257b535
BP
2499struct frag_handling {
2500 const char *name;
2501 uint8_t mask;
2502 uint8_t value;
2503};
2504
2505static const struct frag_handling all_frags[] = {
eadef313
JP
2506#define A FLOW_NW_FRAG_ANY
2507#define L FLOW_NW_FRAG_LATER
7257b535
BP
2508 /* name mask value */
2509
2510 { "no", A|L, 0 },
2511 { "first", A|L, A },
2512 { "later", A|L, A|L },
2513
2514 { "no", A, 0 },
2515 { "yes", A, A },
2516
2517 { "not_later", L, 0 },
2518 { "later", L, L },
2519#undef A
2520#undef L
2521};
2522
2523static char *
2524mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2525{
2526 const struct frag_handling *h;
2527
2528 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2529 if (!strcasecmp(s, h->name)) {
2530 /* We force the upper bits of the mask on to make mf_parse_value()
2531 * happy (otherwise it will never think it's an exact match.) */
eadef313 2532 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
7257b535
BP
2533 *valuep = h->value;
2534 return NULL;
2535 }
2536 }
2537
2538 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2539 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2540}
2541
4fe3445a
PS
2542static int
2543parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
2544 ovs_be16 *res)
2545{
2546 uint32_t result = 0;
2547 char *save_ptr = NULL;
2548 char *name;
2549 int rc = 0;
2550 char *s = xstrdup(s_);
2551
2552 for (name = strtok_r((char *)s, " |", &save_ptr); name;
2553 name = strtok_r(NULL, " |", &save_ptr)) {
2554 int name_len;
2555 unsigned long long int flags;
2556 uint32_t bit;
2557 int n0;
2558
2559 if (sscanf(name, "%lli%n", &flags, &n0) > 0 && n0 > 0) {
2560 result |= flags;
2561 continue;
2562 }
2563 name_len = strlen(name);
2564 for (bit = 1; bit; bit <<= 1) {
2565 const char *fname = bit_to_string(bit);
2566 size_t len;
2567
2568 if (!fname) {
2569 continue;
2570 }
2571
2572 len = strlen(fname);
2573 if (len != name_len) {
2574 continue;
2575 }
2576 if (!strncmp(name, fname, len)) {
2577 result |= bit;
2578 break;
2579 }
2580 }
2581
2582 if (!bit) {
2583 rc = -ENOENT;
2584 goto out;
2585 }
2586 }
2587
2588 *res = htons(result);
2589out:
2590 free(s);
2591 return rc;
2592}
2593
2594static char *
b0aa8146 2595mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
4fe3445a
PS
2596{
2597 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
b8266395 2598 *maskp = OVS_BE16_MAX;
4fe3445a
PS
2599 return NULL;
2600 }
2601
2602 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
2603 "\"csum\", \"key\"", s);
2604}
2605
6a885fd0
BP
2606/* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2607 * NULL if successful, otherwise a malloc()'d string describing the error. */
2608char *
2609mf_parse(const struct mf_field *mf, const char *s,
2610 union mf_value *value, union mf_value *mask)
2611{
3634dff8
BP
2612 char *error;
2613
bedde04c 2614 if (!strcmp(s, "*")) {
6a885fd0
BP
2615 memset(value, 0, mf->n_bytes);
2616 memset(mask, 0, mf->n_bytes);
2617 return NULL;
2618 }
2619
2620 switch (mf->string) {
2621 case MFS_DECIMAL:
2622 case MFS_HEXADECIMAL:
3634dff8
BP
2623 error = mf_from_integer_string(mf, s,
2624 (uint8_t *) value, (uint8_t *) mask);
2625 break;
6a885fd0
BP
2626
2627 case MFS_ETHERNET:
3634dff8
BP
2628 error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2629 break;
6a885fd0
BP
2630
2631 case MFS_IPV4:
3634dff8
BP
2632 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2633 break;
6a885fd0
BP
2634
2635 case MFS_IPV6:
3634dff8
BP
2636 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2637 break;
6a885fd0
BP
2638
2639 case MFS_OFP_PORT:
3634dff8
BP
2640 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2641 break;
7257b535 2642
72333065 2643 case MFS_OFP_PORT_OXM:
3634dff8
BP
2644 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2645 break;
72333065 2646
7257b535 2647 case MFS_FRAG:
3634dff8
BP
2648 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2649 break;
4fe3445a
PS
2650
2651 case MFS_TNL_FLAGS:
cb22974d 2652 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
3634dff8
BP
2653 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2654 break;
2655
2656 default:
2657 NOT_REACHED();
6a885fd0 2658 }
3634dff8
BP
2659
2660 if (!error && !mf_is_mask_valid(mf, mask)) {
2661 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2662 }
2663 return error;
6a885fd0
BP
2664}
2665
2666/* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2667 * successful, otherwise a malloc()'d string describing the error. */
2668char *
2669mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2670{
2671 union mf_value mask;
2672 char *error;
2673
2674 error = mf_parse(mf, s, value, &mask);
2675 if (error) {
2676 return error;
2677 }
2678
2679 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2680 return xasprintf("%s: wildcards not allowed here", s);
2681 }
2682 return NULL;
2683}
2684
2685static void
2686mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2687 const uint8_t *maskp, struct ds *s)
2688{
2689 unsigned long long int integer;
2690 int i;
2691
cb22974d 2692 ovs_assert(mf->n_bytes <= 8);
6a885fd0
BP
2693
2694 integer = 0;
2695 for (i = 0; i < mf->n_bytes; i++) {
2696 integer = (integer << 8) | valuep[i];
2697 }
2698 if (mf->string == MFS_HEXADECIMAL) {
2699 ds_put_format(s, "%#llx", integer);
2700 } else {
2701 ds_put_format(s, "%lld", integer);
2702 }
2703
2704 if (maskp) {
2705 unsigned long long int mask;
2706
2707 mask = 0;
2708 for (i = 0; i < mf->n_bytes; i++) {
2709 mask = (mask << 8) | maskp[i];
2710 }
2711
2712 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2713 * not sure that that a bit-mask written in decimal is ever easier to
2714 * understand than the same bit-mask written in hexadecimal. */
2715 ds_put_format(s, "/%#llx", mask);
2716 }
2717}
2718
7257b535 2719static void
8f75dea3 2720mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
7257b535
BP
2721{
2722 const struct frag_handling *h;
7257b535 2723
eadef313 2724 mask &= FLOW_NW_FRAG_MASK;
8f75dea3 2725 value &= mask;
7257b535
BP
2726
2727 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2728 if (value == h->value && mask == h->mask) {
2729 ds_put_cstr(s, h->name);
2730 return;
2731 }
2732 }
2733 ds_put_cstr(s, "<error>");
2734}
2735
4fe3445a
PS
2736static void
2737mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2738{
2739 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2740}
2741
6a885fd0
BP
2742/* Appends to 's' a string representation of field 'mf' whose value is in
2743 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2744void
2745mf_format(const struct mf_field *mf,
2746 const union mf_value *value, const union mf_value *mask,
2747 struct ds *s)
2748{
2749 if (mask) {
2750 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2751 ds_put_cstr(s, "ANY");
2752 return;
2753 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2754 mask = NULL;
2755 }
2756 }
2757
2758 switch (mf->string) {
72333065
JR
2759 case MFS_OFP_PORT_OXM:
2760 if (!mask) {
4e022ec0 2761 ofp_port_t port;
bc146369 2762 ofputil_port_from_ofp11(value->be32, &port);
72333065
JR
2763 ofputil_format_port(port, s);
2764 break;
2765 }
2766 /* fall through */
6a885fd0
BP
2767 case MFS_OFP_PORT:
2768 if (!mask) {
4e022ec0 2769 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
6a885fd0
BP
2770 break;
2771 }
2772 /* fall through */
2773 case MFS_DECIMAL:
2774 case MFS_HEXADECIMAL:
2775 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2776 break;
2777
2778 case MFS_ETHERNET:
3b4d8ad3 2779 eth_format_masked(value->mac, mask->mac, s);
6a885fd0
BP
2780 break;
2781
2782 case MFS_IPV4:
b8266395 2783 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
6a885fd0
BP
2784 break;
2785
2786 case MFS_IPV6:
2787 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2788 break;
2789
7257b535 2790 case MFS_FRAG:
8f75dea3 2791 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
7257b535
BP
2792 break;
2793
4fe3445a
PS
2794 case MFS_TNL_FLAGS:
2795 mf_format_tnl_flags_string(&value->be16, s);
2796 break;
2797
6a885fd0
BP
2798 default:
2799 NOT_REACHED();
2800 }
2801}
816fd533 2802\f
9bab681f
IY
2803/* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2804 * least-significant bits in 'x'.
2805 */
2806void
2807mf_write_subfield_flow(const struct mf_subfield *sf,
2808 const union mf_subvalue *x, struct flow *flow)
2809{
2810 const struct mf_field *field = sf->field;
2811 union mf_value value;
2812
2813 mf_get_value(field, flow, &value);
158edc8d 2814 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
9bab681f
IY
2815 sf->ofs, sf->n_bits);
2816 mf_set_flow_value(field, &value, flow);
2817}
2818
81a76618 2819/* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
1b35df45 2820 * least-significant bits in 'x'.
615660a9 2821 */
1b35df45
BP
2822void
2823mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
81a76618 2824 struct match *match)
1b35df45
BP
2825{
2826 const struct mf_field *field = sf->field;
2827 union mf_value value, mask;
2828
81a76618 2829 mf_get(field, match, &value, &mask);
1b35df45
BP
2830 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2831 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
81a76618 2832 mf_set(field, &value, &mask, match);
1b35df45
BP
2833}
2834
1b35df45
BP
2835/* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2836 * reading 'flow', e.g. as checked by mf_check_src(). */
2837void
2838mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2839 union mf_subvalue *x)
2840{
2841 union mf_value value;
2842
2843 mf_get_value(sf->field, flow, &value);
2844
2845 memset(x, 0, sizeof *x);
2846 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2847 x, sizeof *x, 0,
2848 sf->n_bits);
2849}
2850
816fd533
BP
2851/* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2852 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2853 * less. */
2854uint64_t
2855mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2856{
2857 union mf_value value;
2858
2859 mf_get_value(sf->field, flow, &value);
2860 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2861}
2862
2863/* Formats 'sf' into 's' in a format normally acceptable to
2864 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2865 * sf->field has no NXM name.) */
2866void
2867mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2868{
2869 if (!sf->field) {
2870 ds_put_cstr(s, "<unknown>");
2871 } else if (sf->field->nxm_name) {
2872 ds_put_cstr(s, sf->field->nxm_name);
2873 } else if (sf->field->nxm_header) {
2874 uint32_t header = sf->field->nxm_header;
2875 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2876 } else {
2877 ds_put_cstr(s, sf->field->name);
2878 }
2879
33500edd 2880 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
816fd533
BP
2881 ds_put_cstr(s, "[]");
2882 } else if (sf->n_bits == 1) {
2883 ds_put_format(s, "[%d]", sf->ofs);
2884 } else {
2885 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2886 }
2887}
2888
2889static const struct mf_field *
2890mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2891{
2892 int i;
2893
2894 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2895 if (*wild) {
2896 name_len -= 2;
2897 }
2898
2899 for (i = 0; i < MFF_N_IDS; i++) {
2900 const struct mf_field *mf = mf_from_id(i);
2901
2902 if (mf->nxm_name
2903 && !strncmp(mf->nxm_name, name, name_len)
2904 && mf->nxm_name[name_len] == '\0') {
2905 return mf;
2906 }
b5e5143b
BP
2907 if (mf->oxm_name
2908 && !strncmp(mf->oxm_name, name, name_len)
2909 && mf->oxm_name[name_len] == '\0') {
2910 return mf;
2911 }
816fd533
BP
2912 }
2913
2914 return NULL;
2915}
2916
2917/* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2918 * returns NULL and advances '*sp' to the first byte following the parsed
2919 * string. On failure, returns a malloc()'d error message, does not modify
2920 * '*sp', and does not properly initialize 'sf'.
2921 *
2922 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2923 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2924 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2925 * may both be omitted (the [] are still required) to indicate an entire
2926 * field. */
bdda5aca 2927char * WARN_UNUSED_RESULT
816fd533
BP
2928mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2929{
2930 const struct mf_field *field;
2931 const char *name;
2932 int start, end;
2933 const char *s;
2934 int name_len;
2935 bool wild;
2936
2937 s = *sp;
2938 name = s;
2939 name_len = strcspn(s, "[");
2940 if (s[name_len] != '[') {
2941 return xasprintf("%s: missing [ looking for field name", *sp);
2942 }
2943
2944 field = mf_parse_subfield_name(name, name_len, &wild);
2945 if (!field) {
2946 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2947 }
2948
2949 s += name_len;
2950 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2951 /* Nothing to do. */
2952 } else if (sscanf(s, "[%d]", &start) == 1) {
2953 end = start;
2954 } else if (!strncmp(s, "[]", 2)) {
2955 start = 0;
2956 end = field->n_bits - 1;
2957 } else {
2958 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2959 "[<start>..<end>]", *sp);
2960 }
2961 s = strchr(s, ']') + 1;
2962
2963 if (start > end) {
2964 return xasprintf("%s: starting bit %d is after ending bit %d",
2965 *sp, start, end);
2966 } else if (start >= field->n_bits) {
2967 return xasprintf("%s: starting bit %d is not valid because field is "
2968 "only %d bits wide", *sp, start, field->n_bits);
2969 } else if (end >= field->n_bits){
2970 return xasprintf("%s: ending bit %d is not valid because field is "
2971 "only %d bits wide", *sp, end, field->n_bits);
2972 }
2973
2974 sf->field = field;
2975 sf->ofs = start;
2976 sf->n_bits = end - start + 1;
2977
2978 *sp = s;
2979 return NULL;
2980}
2981
bdda5aca
BP
2982/* Parses a subfield from the entirety of 's' into 'sf'. Returns NULL if
2983 * successful, otherwise a malloc()'d string describing the error. The caller
2984 * is responsible for freeing the returned string.
816fd533
BP
2985 *
2986 * The syntax parsed from 's' takes the form "header[start..end]" where
2987 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2988 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2989 * may both be omitted (the [] are still required) to indicate an entire
2990 * field. */
bdda5aca 2991char * WARN_UNUSED_RESULT
816fd533
BP
2992mf_parse_subfield(struct mf_subfield *sf, const char *s)
2993{
bdda5aca
BP
2994 char *error = mf_parse_subfield__(sf, &s);
2995 if (!error && s[0]) {
2996 error = xstrdup("unexpected input following field syntax");
816fd533 2997 }
bdda5aca 2998 return error;
816fd533 2999}
9bab681f
IY
3000
3001void
3002mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
3003{
3004 int i;
3005
3006 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
3007 if (subvalue->u8[i]) {
3008 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
3009 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
3010 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
3011 }
3012 return;
3013 }
3014 }
3015 ds_put_char(s, '0');
3016}