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