]> git.proxmox.com Git - mirror_ovs.git/blame - lib/meta-flow.c
lib: Keep track of usable protocols while parsing.
[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 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
PS
93 }, {
94 MFF_TUN_TOS, "tun_tos", NULL,
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
PS
104 }, {
105 MFF_TUN_TTL, "tun_ttl", NULL,
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,
28da1f8f
BP
485 false,
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,
28da1f8f
BP
496 false,
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,
28da1f8f
BP
507 false,
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,
28da1f8f
BP
518 false,
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,
28da1f8f
BP
529 false,
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,
6a885fd0
BP
562 },
563
564 {
565 MFF_UDP_SRC, "udp_src", NULL,
566 MF_FIELD_SIZES(be16),
0bdc4bec 567 MFM_FULLY,
6a885fd0
BP
568 MFS_DECIMAL,
569 MFP_UDP,
28da1f8f
BP
570 true,
571 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
36f3999b 572 OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
db0b6c29
JR
573 OFPUTIL_P_ANY,
574 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0
BP
575 }, {
576 MFF_UDP_DST, "udp_dst", 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_DST, "NXM_OF_UDP_DST",
36f3999b 583 OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
db0b6c29
JR
584 OFPUTIL_P_ANY,
585 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0
BP
586 },
587
0d56eaf2
JS
588 {
589 MFF_SCTP_SRC, "sctp_src", NULL,
590 MF_FIELD_SIZES(be16),
591 MFM_FULLY,
592 MFS_DECIMAL,
593 MFP_SCTP,
594 true,
595 OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC",
596 OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC",
db0b6c29
JR
597 OFPUTIL_P_NXM_OF11_UP,
598 OFPUTIL_P_NXM_OXM_ANY,
0d56eaf2
JS
599 }, {
600 MFF_SCTP_DST, "sctp_dst", NULL,
601 MF_FIELD_SIZES(be16),
602 MFM_FULLY,
603 MFS_DECIMAL,
604 MFP_SCTP,
605 true,
606 OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST",
607 OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST",
db0b6c29
JR
608 OFPUTIL_P_NXM_OF11_UP,
609 OFPUTIL_P_NXM_OXM_ANY,
0d56eaf2
JS
610 },
611
6a885fd0 612 {
268a95e0 613 MFF_ICMPV4_TYPE, "icmp_type", NULL,
6a885fd0 614 MF_FIELD_SIZES(u8),
0bdc4bec 615 MFM_NONE,
6a885fd0 616 MFS_DECIMAL,
268a95e0 617 MFP_ICMPV4,
28da1f8f
BP
618 false,
619 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
36f3999b 620 OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
db0b6c29
JR
621 OFPUTIL_P_ANY,
622 OFPUTIL_P_NONE,
6a885fd0 623 }, {
268a95e0 624 MFF_ICMPV4_CODE, "icmp_code", 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_CODE, "NXM_OF_ICMP_CODE",
36f3999b 631 OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
db0b6c29
JR
632 OFPUTIL_P_ANY,
633 OFPUTIL_P_NONE,
28da1f8f
BP
634 },
635
636 {
268a95e0
BP
637 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
638 MF_FIELD_SIZES(u8),
0bdc4bec 639 MFM_NONE,
268a95e0
BP
640 MFS_DECIMAL,
641 MFP_ICMPV6,
28da1f8f
BP
642 false,
643 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
36f3999b 644 OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
db0b6c29
JR
645 OFPUTIL_P_NXM_OXM_ANY,
646 OFPUTIL_P_NONE,
268a95e0
BP
647 }, {
648 MFF_ICMPV6_CODE, "icmpv6_code", 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_CODE, "NXM_NX_ICMPV6_CODE",
36f3999b 655 OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
db0b6c29
JR
656 OFPUTIL_P_NXM_OXM_ANY,
657 OFPUTIL_P_NONE,
6a885fd0
BP
658 },
659
660 /* ## ---- ## */
661 /* ## L"5" ## */
662 /* ## ---- ## */
663
664 {
665 MFF_ND_TARGET, "nd_target", NULL,
666 MF_FIELD_SIZES(ipv6),
0bdc4bec 667 MFM_FULLY,
6a885fd0
BP
668 MFS_IPV6,
669 MFP_ND,
28da1f8f
BP
670 false,
671 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
36f3999b 672 OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
db0b6c29
JR
673 OFPUTIL_P_NXM_OXM_ANY,
674 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0
BP
675 }, {
676 MFF_ND_SLL, "nd_sll", NULL,
677 MF_FIELD_SIZES(mac),
0bdc4bec 678 MFM_FULLY,
6a885fd0
BP
679 MFS_ETHERNET,
680 MFP_ND_SOLICIT,
28da1f8f
BP
681 false,
682 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
36f3999b 683 OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
db0b6c29
JR
684 OFPUTIL_P_NXM_OXM_ANY,
685 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0
BP
686 }, {
687 MFF_ND_TLL, "nd_tll", NULL,
688 MF_FIELD_SIZES(mac),
0bdc4bec 689 MFM_FULLY,
6a885fd0
BP
690 MFS_ETHERNET,
691 MFP_ND_ADVERT,
28da1f8f
BP
692 false,
693 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
36f3999b 694 OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
db0b6c29
JR
695 OFPUTIL_P_NXM_OXM_ANY,
696 OFPUTIL_P_NXM_OXM_ANY,
6a885fd0
BP
697 }
698};
699
b5e5143b 700/* Maps an NXM or OXM header value to an mf_field. */
28da1f8f 701struct nxm_field {
b5e5143b
BP
702 struct hmap_node hmap_node; /* In 'all_fields' hmap. */
703 uint32_t header; /* NXM or OXM header value. */
28da1f8f
BP
704 const struct mf_field *mf;
705};
706
b5e5143b 707/* Contains 'struct nxm_field's. */
4663f9e0
BP
708static struct hmap all_fields;
709
710/* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
711static struct shash mf_by_name;
28da1f8f 712
816fd533
BP
713/* Rate limit for parse errors. These always indicate a bug in an OpenFlow
714 * controller and so there's not much point in showing a lot of them. */
715static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
716
b5e5143b 717const struct mf_field *mf_from_nxm_header__(uint32_t header);
4663f9e0 718static void nxm_init(void);
b5e5143b 719
6a885fd0
BP
720/* Returns the field with the given 'id'. */
721const struct mf_field *
722mf_from_id(enum mf_field_id id)
723{
cb22974d 724 ovs_assert((unsigned int) id < MFF_N_IDS);
6a885fd0
BP
725 return &mf_fields[id];
726}
727
728/* Returns the field with the given 'name', or a null pointer if no field has
729 * that name. */
730const struct mf_field *
731mf_from_name(const char *name)
732{
4663f9e0 733 nxm_init();
6a885fd0
BP
734 return shash_find_data(&mf_by_name, name);
735}
736
28da1f8f 737static void
b5e5143b 738add_nxm_field(uint32_t header, const struct mf_field *mf)
28da1f8f
BP
739{
740 struct nxm_field *f;
741
742 f = xmalloc(sizeof *f);
b5e5143b
BP
743 hmap_insert(&all_fields, &f->hmap_node, hash_int(header, 0));
744 f->header = header;
28da1f8f
BP
745 f->mf = mf;
746}
747
b5ae8913
SH
748static void
749nxm_init_add_field(const struct mf_field *mf, uint32_t header)
750{
b5e5143b 751 if (header) {
cb22974d 752 ovs_assert(!mf_from_nxm_header__(header));
b5e5143b
BP
753 add_nxm_field(header, mf);
754 if (mf->maskable != MFM_NONE) {
755 add_nxm_field(NXM_MAKE_WILD_HEADER(header), mf);
756 }
b5ae8913 757 }
b5ae8913 758}
b5ae8913 759
28da1f8f 760static void
4663f9e0 761nxm_do_init(void)
28da1f8f
BP
762{
763 const struct mf_field *mf;
764
4663f9e0
BP
765 hmap_init(&all_fields);
766 shash_init(&mf_by_name);
28da1f8f 767 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
b5ae8913 768 nxm_init_add_field(mf, mf->nxm_header);
b5e5143b
BP
769 if (mf->oxm_header != mf->nxm_header) {
770 nxm_init_add_field(mf, mf->oxm_header);
771 }
4663f9e0
BP
772
773 shash_add_once(&mf_by_name, mf->name, mf);
774 if (mf->extra_name) {
775 shash_add_once(&mf_by_name, mf->extra_name, mf);
776 }
28da1f8f 777 }
28da1f8f
BP
778}
779
4663f9e0
BP
780static void
781nxm_init(void)
782{
783 static pthread_once_t once = PTHREAD_ONCE_INIT;
784 pthread_once(&once, nxm_do_init);
785}
786
28da1f8f
BP
787const struct mf_field *
788mf_from_nxm_header(uint32_t header)
789{
4663f9e0 790 nxm_init();
b5e5143b
BP
791 return mf_from_nxm_header__(header);
792}
28da1f8f 793
b5e5143b
BP
794const struct mf_field *
795mf_from_nxm_header__(uint32_t header)
796{
797 const struct nxm_field *f;
798
799 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0), &all_fields) {
800 if (f->header == header) {
28da1f8f
BP
801 return f->mf;
802 }
803 }
804
805 return NULL;
806}
807
6a885fd0
BP
808/* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
809 * specifies at least one bit in the field.
810 *
811 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
812 * meets 'mf''s prerequisites. */
813bool
814mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
815{
816 switch (mf->id) {
4fe3445a 817 case MFF_TUN_SRC:
0ad90c84 818 return !wc->masks.tunnel.ip_src;
4fe3445a 819 case MFF_TUN_DST:
0ad90c84
JR
820 return !wc->masks.tunnel.ip_dst;
821 case MFF_TUN_ID:
4fe3445a
PS
822 case MFF_TUN_TOS:
823 case MFF_TUN_TTL:
824 case MFF_TUN_FLAGS:
296e07ac 825 return !wc->masks.tunnel.tun_id;
969fc56c 826 case MFF_METADATA:
26720e24 827 return !wc->masks.metadata;
0bdc4bec 828 case MFF_IN_PORT:
72333065 829 case MFF_IN_PORT_OXM:
4e022ec0 830 return !wc->masks.in_port.ofp_port;
1b567fb9
AA
831 case MFF_SKB_PRIORITY:
832 return !wc->masks.skb_priority;
1362e248
JG
833 case MFF_PKT_MARK:
834 return !wc->masks.pkt_mark;
0d7e2fe4 835 CASE_MFF_REGS:
26720e24 836 return !wc->masks.regs[mf->id - MFF_REG0];
6a885fd0 837
73c0ce34 838 case MFF_ETH_SRC:
26720e24 839 return eth_addr_is_zero(wc->masks.dl_src);
6a885fd0 840 case MFF_ETH_DST:
26720e24 841 return eth_addr_is_zero(wc->masks.dl_dst);
e2170cff 842 case MFF_ETH_TYPE:
26720e24 843 return !wc->masks.dl_type;
6a885fd0 844
e878338b
SH
845 case MFF_ARP_SHA:
846 case MFF_ND_SLL:
26720e24 847 return eth_addr_is_zero(wc->masks.arp_sha);
e878338b
SH
848
849 case MFF_ARP_THA:
850 case MFF_ND_TLL:
26720e24 851 return eth_addr_is_zero(wc->masks.arp_tha);
e878338b 852
6a885fd0 853 case MFF_VLAN_TCI:
26720e24 854 return !wc->masks.vlan_tci;
441c57a9 855 case MFF_DL_VLAN:
26720e24 856 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
cc34bc8c 857 case MFF_VLAN_VID:
26720e24 858 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
441c57a9 859 case MFF_DL_VLAN_PCP:
cc34bc8c 860 case MFF_VLAN_PCP:
26720e24 861 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
6a885fd0 862
b02475c5
SH
863 case MFF_MPLS_LABEL:
864 return !(wc->masks.mpls_lse & htonl(MPLS_LABEL_MASK));
865 case MFF_MPLS_TC:
866 return !(wc->masks.mpls_lse & htonl(MPLS_TC_MASK));
867 case MFF_MPLS_BOS:
868 return !(wc->masks.mpls_lse & htonl(MPLS_BOS_MASK));
869
6a885fd0 870 case MFF_IPV4_SRC:
26720e24 871 return !wc->masks.nw_src;
6a885fd0 872 case MFF_IPV4_DST:
26720e24 873 return !wc->masks.nw_dst;
6a885fd0
BP
874
875 case MFF_IPV6_SRC:
26720e24 876 return ipv6_mask_is_any(&wc->masks.ipv6_src);
6a885fd0 877 case MFF_IPV6_DST:
26720e24 878 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
6a885fd0 879
32455024 880 case MFF_IPV6_LABEL:
26720e24 881 return !wc->masks.ipv6_label;
32455024 882
851d3105 883 case MFF_IP_PROTO:
26720e24 884 return !wc->masks.nw_proto;
5d9499c4 885 case MFF_IP_DSCP:
1638b906 886 case MFF_IP_DSCP_SHIFTED:
26720e24 887 return !(wc->masks.nw_tos & IP_DSCP_MASK);
5d9499c4 888 case MFF_IP_ECN:
26720e24 889 return !(wc->masks.nw_tos & IP_ECN_MASK);
3840c406 890 case MFF_IP_TTL:
26720e24 891 return !wc->masks.nw_ttl;
5d9499c4 892
47284b1f 893 case MFF_ND_TARGET:
26720e24 894 return ipv6_mask_is_any(&wc->masks.nd_target);
47284b1f 895
7257b535 896 case MFF_IP_FRAG:
26720e24 897 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
7257b535 898
851d3105 899 case MFF_ARP_OP:
26720e24 900 return !wc->masks.nw_proto;
6a885fd0 901 case MFF_ARP_SPA:
26720e24 902 return !wc->masks.nw_src;
6a885fd0 903 case MFF_ARP_TPA:
26720e24 904 return !wc->masks.nw_dst;
6a885fd0 905
73f33563
BP
906 case MFF_TCP_SRC:
907 case MFF_UDP_SRC:
0d56eaf2 908 case MFF_SCTP_SRC:
73f33563
BP
909 case MFF_ICMPV4_TYPE:
910 case MFF_ICMPV6_TYPE:
26720e24 911 return !wc->masks.tp_src;
73f33563
BP
912 case MFF_TCP_DST:
913 case MFF_UDP_DST:
0d56eaf2 914 case MFF_SCTP_DST:
73f33563
BP
915 case MFF_ICMPV4_CODE:
916 case MFF_ICMPV6_CODE:
26720e24 917 return !wc->masks.tp_dst;
73f33563 918
6a885fd0
BP
919 case MFF_N_IDS:
920 default:
921 NOT_REACHED();
922 }
923}
924
925/* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
926 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
927 * purposes, or to 0 if it is wildcarded.
928 *
929 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
930 * meets 'mf''s prerequisites. */
931void
932mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
933 union mf_value *mask)
934{
fb15feb0 935 mf_get_value(mf, &wc->masks, mask);
6a885fd0
BP
936}
937
938/* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
939 * if the mask is valid, false otherwise. */
940bool
941mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
942{
943 switch (mf->maskable) {
944 case MFM_NONE:
945 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
946 is_all_ones((const uint8_t *) mask, mf->n_bytes));
947
948 case MFM_FULLY:
949 return true;
6a885fd0
BP
950 }
951
952 NOT_REACHED();
953}
954
6a885fd0
BP
955static bool
956is_icmpv4(const struct flow *flow)
957{
958 return (flow->dl_type == htons(ETH_TYPE_IP)
959 && flow->nw_proto == IPPROTO_ICMP);
960}
961
962static bool
963is_icmpv6(const struct flow *flow)
964{
965 return (flow->dl_type == htons(ETH_TYPE_IPV6)
966 && flow->nw_proto == IPPROTO_ICMPV6);
967}
968
969/* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
970bool
971mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
972{
973 switch (mf->prereqs) {
974 case MFP_NONE:
975 return true;
976
977 case MFP_ARP:
8087f5ff
MM
978 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
979 flow->dl_type == htons(ETH_TYPE_RARP));
6a885fd0
BP
980 case MFP_IPV4:
981 return flow->dl_type == htons(ETH_TYPE_IP);
982 case MFP_IPV6:
983 return flow->dl_type == htons(ETH_TYPE_IPV6);
8069b0da 984 case MFP_VLAN_VID:
5921e99a 985 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
b02475c5
SH
986 case MFP_MPLS:
987 return eth_type_mpls(flow->dl_type);
6a885fd0
BP
988 case MFP_IP_ANY:
989 return is_ip_any(flow);
990
991 case MFP_TCP:
992 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
993 case MFP_UDP:
994 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
0d56eaf2
JS
995 case MFP_SCTP:
996 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP;
268a95e0
BP
997 case MFP_ICMPV4:
998 return is_icmpv4(flow);
6a885fd0
BP
999 case MFP_ICMPV6:
1000 return is_icmpv6(flow);
6a885fd0
BP
1001
1002 case MFP_ND:
1003 return (is_icmpv6(flow)
3ee8a9f0
BP
1004 && flow->tp_dst == htons(0)
1005 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1006 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
6a885fd0
BP
1007 case MFP_ND_SOLICIT:
1008 return (is_icmpv6(flow)
3ee8a9f0
BP
1009 && flow->tp_dst == htons(0)
1010 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
6a885fd0
BP
1011 case MFP_ND_ADVERT:
1012 return (is_icmpv6(flow)
3ee8a9f0
BP
1013 && flow->tp_dst == htons(0)
1014 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
6a885fd0
BP
1015 }
1016
1017 NOT_REACHED();
1018}
1019
1020/* Returns true if 'value' may be a valid value *as part of a masked match*,
1021 * false otherwise.
1022 *
1023 * A value is not rejected just because it is not valid for the field in
1024 * question, but only if it doesn't make sense to test the bits in question at
1025 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
1026 * without the VLAN_CFI bit being set, but we can't reject those values because
1027 * it is still legitimate to test just for those bits (see the documentation
1028 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
530180fd 1029 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
6a885fd0
BP
1030bool
1031mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
1032{
1033 switch (mf->id) {
1034 case MFF_TUN_ID:
4fe3445a
PS
1035 case MFF_TUN_SRC:
1036 case MFF_TUN_DST:
1037 case MFF_TUN_TOS:
1038 case MFF_TUN_TTL:
1039 case MFF_TUN_FLAGS:
969fc56c 1040 case MFF_METADATA:
6a885fd0 1041 case MFF_IN_PORT:
1b567fb9 1042 case MFF_SKB_PRIORITY:
1362e248 1043 case MFF_PKT_MARK:
0d7e2fe4 1044 CASE_MFF_REGS:
6a885fd0
BP
1045 case MFF_ETH_SRC:
1046 case MFF_ETH_DST:
1047 case MFF_ETH_TYPE:
1048 case MFF_VLAN_TCI:
1049 case MFF_IPV4_SRC:
1050 case MFF_IPV4_DST:
1051 case MFF_IPV6_SRC:
1052 case MFF_IPV6_DST:
1053 case MFF_IP_PROTO:
a61680c6 1054 case MFF_IP_TTL:
6a885fd0
BP
1055 case MFF_ARP_SPA:
1056 case MFF_ARP_TPA:
1057 case MFF_ARP_SHA:
1058 case MFF_ARP_THA:
1059 case MFF_TCP_SRC:
1060 case MFF_TCP_DST:
1061 case MFF_UDP_SRC:
1062 case MFF_UDP_DST:
0d56eaf2
JS
1063 case MFF_SCTP_SRC:
1064 case MFF_SCTP_DST:
268a95e0
BP
1065 case MFF_ICMPV4_TYPE:
1066 case MFF_ICMPV4_CODE:
1067 case MFF_ICMPV6_TYPE:
1068 case MFF_ICMPV6_CODE:
6a885fd0
BP
1069 case MFF_ND_TARGET:
1070 case MFF_ND_SLL:
1071 case MFF_ND_TLL:
1072 return true;
1073
72333065 1074 case MFF_IN_PORT_OXM: {
4e022ec0 1075 ofp_port_t port;
72333065
JR
1076 return !ofputil_port_from_ofp11(value->be32, &port);
1077 }
1078
530180fd 1079 case MFF_IP_DSCP:
7257b535 1080 return !(value->u8 & ~IP_DSCP_MASK);
1638b906
BP
1081 case MFF_IP_DSCP_SHIFTED:
1082 return !(value->u8 & (~IP_DSCP_MASK >> 2));
530180fd
JP
1083 case MFF_IP_ECN:
1084 return !(value->u8 & ~IP_ECN_MASK);
7257b535 1085 case MFF_IP_FRAG:
eadef313 1086 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
6a885fd0
BP
1087
1088 case MFF_ARP_OP:
1089 return !(value->be16 & htons(0xff00));
1090
441c57a9 1091 case MFF_DL_VLAN:
6a885fd0 1092 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
cc34bc8c
BP
1093 case MFF_VLAN_VID:
1094 return !(value->be16 & htons(VLAN_PCP_MASK));
6a885fd0 1095
441c57a9 1096 case MFF_DL_VLAN_PCP:
cc34bc8c 1097 case MFF_VLAN_PCP:
24016f5a 1098 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
6a885fd0 1099
fa8223b7
JP
1100 case MFF_IPV6_LABEL:
1101 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
1102
b02475c5
SH
1103 case MFF_MPLS_LABEL:
1104 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
1105
1106 case MFF_MPLS_TC:
1107 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
1108
1109 case MFF_MPLS_BOS:
1110 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
1111
6a885fd0
BP
1112 case MFF_N_IDS:
1113 default:
1114 NOT_REACHED();
1115 }
1116}
1117
1118/* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
1119 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
1120void
1121mf_get_value(const struct mf_field *mf, const struct flow *flow,
1122 union mf_value *value)
1123{
1124 switch (mf->id) {
1125 case MFF_TUN_ID:
296e07ac 1126 value->be64 = flow->tunnel.tun_id;
6a885fd0 1127 break;
4fe3445a
PS
1128 case MFF_TUN_SRC:
1129 value->be32 = flow->tunnel.ip_src;
1130 break;
1131 case MFF_TUN_DST:
1132 value->be32 = flow->tunnel.ip_dst;
1133 break;
1134 case MFF_TUN_FLAGS:
1135 value->be16 = htons(flow->tunnel.flags);
1136 break;
1137 case MFF_TUN_TTL:
1138 value->u8 = flow->tunnel.ip_ttl;
1139 break;
1140 case MFF_TUN_TOS:
1141 value->u8 = flow->tunnel.ip_tos;
1142 break;
1143
969fc56c
JS
1144 case MFF_METADATA:
1145 value->be64 = flow->metadata;
1146 break;
6a885fd0
BP
1147
1148 case MFF_IN_PORT:
4e022ec0 1149 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
6a885fd0 1150 break;
72333065 1151 case MFF_IN_PORT_OXM:
4e022ec0 1152 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
72333065
JR
1153 break;
1154
1b567fb9 1155 case MFF_SKB_PRIORITY:
a81f0a44 1156 value->be32 = htonl(flow->skb_priority);
1b567fb9
AA
1157 break;
1158
1362e248
JG
1159 case MFF_PKT_MARK:
1160 value->be32 = htonl(flow->pkt_mark);
1b567fb9
AA
1161 break;
1162
0d7e2fe4 1163 CASE_MFF_REGS:
2f98b0b7 1164 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
6a885fd0
BP
1165 break;
1166
1167 case MFF_ETH_SRC:
1168 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1169 break;
1170
1171 case MFF_ETH_DST:
1172 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1173 break;
1174
1175 case MFF_ETH_TYPE:
1176 value->be16 = flow->dl_type;
1177 break;
1178
1179 case MFF_VLAN_TCI:
1180 value->be16 = flow->vlan_tci;
1181 break;
1182
441c57a9 1183 case MFF_DL_VLAN:
6a885fd0
BP
1184 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1185 break;
cc34bc8c
BP
1186 case MFF_VLAN_VID:
1187 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
1188 break;
6a885fd0 1189
441c57a9 1190 case MFF_DL_VLAN_PCP:
cc34bc8c 1191 case MFF_VLAN_PCP:
6a885fd0
BP
1192 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1193 break;
1194
b02475c5
SH
1195 case MFF_MPLS_LABEL:
1196 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse));
1197 break;
1198
1199 case MFF_MPLS_TC:
1200 value->u8 = mpls_lse_to_tc(flow->mpls_lse);
1201 break;
1202
1203 case MFF_MPLS_BOS:
1204 value->u8 = mpls_lse_to_bos(flow->mpls_lse);
1205 break;
1206
6a885fd0
BP
1207 case MFF_IPV4_SRC:
1208 value->be32 = flow->nw_src;
1209 break;
1210
1211 case MFF_IPV4_DST:
1212 value->be32 = flow->nw_dst;
1213 break;
1214
1215 case MFF_IPV6_SRC:
1216 value->ipv6 = flow->ipv6_src;
1217 break;
1218
1219 case MFF_IPV6_DST:
1220 value->ipv6 = flow->ipv6_dst;
1221 break;
1222
fa8223b7
JP
1223 case MFF_IPV6_LABEL:
1224 value->be32 = flow->ipv6_label;
1225 break;
1226
6a885fd0
BP
1227 case MFF_IP_PROTO:
1228 value->u8 = flow->nw_proto;
1229 break;
1230
530180fd 1231 case MFF_IP_DSCP:
eadef313 1232 value->u8 = flow->nw_tos & IP_DSCP_MASK;
7257b535
BP
1233 break;
1234
1638b906
BP
1235 case MFF_IP_DSCP_SHIFTED:
1236 value->u8 = flow->nw_tos >> 2;
1237 break;
1238
530180fd 1239 case MFF_IP_ECN:
eadef313 1240 value->u8 = flow->nw_tos & IP_ECN_MASK;
530180fd
JP
1241 break;
1242
a61680c6
JP
1243 case MFF_IP_TTL:
1244 value->u8 = flow->nw_ttl;
1245 break;
1246
7257b535 1247 case MFF_IP_FRAG:
eadef313 1248 value->u8 = flow->nw_frag;
6a885fd0
BP
1249 break;
1250
1251 case MFF_ARP_OP:
1252 value->be16 = htons(flow->nw_proto);
1253 break;
1254
1255 case MFF_ARP_SPA:
1256 value->be32 = flow->nw_src;
1257 break;
1258
1259 case MFF_ARP_TPA:
1260 value->be32 = flow->nw_dst;
1261 break;
1262
1263 case MFF_ARP_SHA:
1264 case MFF_ND_SLL:
1265 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1266 break;
1267
1268 case MFF_ARP_THA:
1269 case MFF_ND_TLL:
1270 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1271 break;
1272
1273 case MFF_TCP_SRC:
6a885fd0 1274 case MFF_UDP_SRC:
0d56eaf2 1275 case MFF_SCTP_SRC:
6a885fd0
BP
1276 value->be16 = flow->tp_src;
1277 break;
1278
71baec06 1279 case MFF_TCP_DST:
6a885fd0 1280 case MFF_UDP_DST:
0d56eaf2 1281 case MFF_SCTP_DST:
6a885fd0
BP
1282 value->be16 = flow->tp_dst;
1283 break;
1284
268a95e0
BP
1285 case MFF_ICMPV4_TYPE:
1286 case MFF_ICMPV6_TYPE:
6a885fd0
BP
1287 value->u8 = ntohs(flow->tp_src);
1288 break;
1289
268a95e0
BP
1290 case MFF_ICMPV4_CODE:
1291 case MFF_ICMPV6_CODE:
6a885fd0
BP
1292 value->u8 = ntohs(flow->tp_dst);
1293 break;
1294
1295 case MFF_ND_TARGET:
1296 value->ipv6 = flow->nd_target;
1297 break;
1298
1299 case MFF_N_IDS:
1300 default:
1301 NOT_REACHED();
1302 }
1303}
1304
81a76618
BP
1305/* Makes 'match' match field 'mf' exactly, with the value matched taken from
1306 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
6a885fd0
BP
1307 * prerequisites. */
1308void
1309mf_set_value(const struct mf_field *mf,
81a76618 1310 const union mf_value *value, struct match *match)
6a885fd0
BP
1311{
1312 switch (mf->id) {
1313 case MFF_TUN_ID:
81a76618 1314 match_set_tun_id(match, value->be64);
6a885fd0 1315 break;
4fe3445a
PS
1316 case MFF_TUN_SRC:
1317 match_set_tun_src(match, value->be32);
1318 break;
1319 case MFF_TUN_DST:
1320 match_set_tun_dst(match, value->be32);
1321 break;
1322 case MFF_TUN_FLAGS:
1323 match_set_tun_flags(match, ntohs(value->be16));
1324 break;
1325 case MFF_TUN_TOS:
1326 match_set_tun_tos(match, value->u8);
1327 break;
1328 case MFF_TUN_TTL:
1329 match_set_tun_ttl(match, value->u8);
1330 break;
1331
969fc56c 1332 case MFF_METADATA:
81a76618 1333 match_set_metadata(match, value->be64);
969fc56c 1334 break;
6a885fd0
BP
1335
1336 case MFF_IN_PORT:
4e022ec0 1337 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
6a885fd0
BP
1338 break;
1339
72333065 1340 case MFF_IN_PORT_OXM: {
4e022ec0 1341 ofp_port_t port;
bc146369 1342 ofputil_port_from_ofp11(value->be32, &port);
72333065
JR
1343 match_set_in_port(match, port);
1344 break;
1345 }
1346
1b567fb9
AA
1347 case MFF_SKB_PRIORITY:
1348 match_set_skb_priority(match, ntohl(value->be32));
1349 break;
1350
1362e248
JG
1351 case MFF_PKT_MARK:
1352 match_set_pkt_mark(match, ntohl(value->be32));
1b567fb9
AA
1353 break;
1354
0d7e2fe4 1355 CASE_MFF_REGS:
81a76618 1356 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
6a885fd0 1357 break;
6a885fd0
BP
1358
1359 case MFF_ETH_SRC:
81a76618 1360 match_set_dl_src(match, value->mac);
6a885fd0
BP
1361 break;
1362
1363 case MFF_ETH_DST:
81a76618 1364 match_set_dl_dst(match, value->mac);
6a885fd0
BP
1365 break;
1366
1367 case MFF_ETH_TYPE:
81a76618 1368 match_set_dl_type(match, value->be16);
6a885fd0
BP
1369 break;
1370
1371 case MFF_VLAN_TCI:
81a76618 1372 match_set_dl_tci(match, value->be16);
6a885fd0
BP
1373 break;
1374
441c57a9 1375 case MFF_DL_VLAN:
81a76618 1376 match_set_dl_vlan(match, value->be16);
6a885fd0 1377 break;
cc34bc8c 1378 case MFF_VLAN_VID:
81a76618 1379 match_set_vlan_vid(match, value->be16);
cc34bc8c 1380 break;
6a885fd0 1381
441c57a9 1382 case MFF_DL_VLAN_PCP:
cc34bc8c 1383 case MFF_VLAN_PCP:
81a76618 1384 match_set_dl_vlan_pcp(match, value->u8);
6a885fd0
BP
1385 break;
1386
b02475c5
SH
1387 case MFF_MPLS_LABEL:
1388 match_set_mpls_label(match, value->be32);
1389 break;
1390
1391 case MFF_MPLS_TC:
1392 match_set_mpls_tc(match, value->u8);
1393 break;
1394
1395 case MFF_MPLS_BOS:
1396 match_set_mpls_bos(match, value->u8);
1397 break;
1398
6a885fd0 1399 case MFF_IPV4_SRC:
81a76618 1400 match_set_nw_src(match, value->be32);
6a885fd0
BP
1401 break;
1402
1403 case MFF_IPV4_DST:
81a76618 1404 match_set_nw_dst(match, value->be32);
6a885fd0
BP
1405 break;
1406
1407 case MFF_IPV6_SRC:
81a76618 1408 match_set_ipv6_src(match, &value->ipv6);
6a885fd0
BP
1409 break;
1410
1411 case MFF_IPV6_DST:
81a76618 1412 match_set_ipv6_dst(match, &value->ipv6);
6a885fd0
BP
1413 break;
1414
fa8223b7 1415 case MFF_IPV6_LABEL:
81a76618 1416 match_set_ipv6_label(match, value->be32);
fa8223b7
JP
1417 break;
1418
6a885fd0 1419 case MFF_IP_PROTO:
81a76618 1420 match_set_nw_proto(match, value->u8);
6a885fd0
BP
1421 break;
1422
530180fd 1423 case MFF_IP_DSCP:
81a76618 1424 match_set_nw_dscp(match, value->u8);
530180fd
JP
1425 break;
1426
1638b906
BP
1427 case MFF_IP_DSCP_SHIFTED:
1428 match_set_nw_dscp(match, value->u8 << 2);
1429 break;
1430
530180fd 1431 case MFF_IP_ECN:
81a76618 1432 match_set_nw_ecn(match, value->u8);
6a885fd0
BP
1433 break;
1434
a61680c6 1435 case MFF_IP_TTL:
81a76618 1436 match_set_nw_ttl(match, value->u8);
a61680c6
JP
1437 break;
1438
7257b535 1439 case MFF_IP_FRAG:
81a76618 1440 match_set_nw_frag(match, value->u8);
7257b535
BP
1441 break;
1442
6a885fd0 1443 case MFF_ARP_OP:
81a76618 1444 match_set_nw_proto(match, ntohs(value->be16));
6a885fd0
BP
1445 break;
1446
1447 case MFF_ARP_SPA:
81a76618 1448 match_set_nw_src(match, value->be32);
6a885fd0
BP
1449 break;
1450
1451 case MFF_ARP_TPA:
81a76618 1452 match_set_nw_dst(match, value->be32);
6a885fd0
BP
1453 break;
1454
1455 case MFF_ARP_SHA:
1456 case MFF_ND_SLL:
81a76618 1457 match_set_arp_sha(match, value->mac);
6a885fd0
BP
1458 break;
1459
1460 case MFF_ARP_THA:
1461 case MFF_ND_TLL:
81a76618 1462 match_set_arp_tha(match, value->mac);
6a885fd0
BP
1463 break;
1464
1465 case MFF_TCP_SRC:
6a885fd0 1466 case MFF_UDP_SRC:
0d56eaf2 1467 case MFF_SCTP_SRC:
81a76618 1468 match_set_tp_src(match, value->be16);
6a885fd0
BP
1469 break;
1470
71baec06 1471 case MFF_TCP_DST:
6a885fd0 1472 case MFF_UDP_DST:
0d56eaf2 1473 case MFF_SCTP_DST:
81a76618 1474 match_set_tp_dst(match, value->be16);
6a885fd0
BP
1475 break;
1476
268a95e0
BP
1477 case MFF_ICMPV4_TYPE:
1478 case MFF_ICMPV6_TYPE:
81a76618 1479 match_set_icmp_type(match, value->u8);
6a885fd0
BP
1480 break;
1481
268a95e0
BP
1482 case MFF_ICMPV4_CODE:
1483 case MFF_ICMPV6_CODE:
81a76618 1484 match_set_icmp_code(match, value->u8);
6a885fd0
BP
1485 break;
1486
1487 case MFF_ND_TARGET:
81a76618 1488 match_set_nd_target(match, &value->ipv6);
6a885fd0
BP
1489 break;
1490
1491 case MFF_N_IDS:
1492 default:
1493 NOT_REACHED();
1494 }
1495}
1496
910fb1d8
JP
1497/* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1498 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
28da1f8f
BP
1499void
1500mf_set_flow_value(const struct mf_field *mf,
1501 const union mf_value *value, struct flow *flow)
1502{
1503 switch (mf->id) {
1504 case MFF_TUN_ID:
296e07ac 1505 flow->tunnel.tun_id = value->be64;
28da1f8f 1506 break;
4fe3445a
PS
1507 case MFF_TUN_SRC:
1508 flow->tunnel.ip_src = value->be32;
1509 break;
1510 case MFF_TUN_DST:
1511 flow->tunnel.ip_dst = value->be32;
1512 break;
1513 case MFF_TUN_FLAGS:
1514 flow->tunnel.flags = ntohs(value->be16);
1515 break;
1516 case MFF_TUN_TOS:
1517 flow->tunnel.ip_tos = value->u8;
1518 break;
1519 case MFF_TUN_TTL:
1520 flow->tunnel.ip_ttl = value->u8;
1521 break;
1522
969fc56c
JS
1523 case MFF_METADATA:
1524 flow->metadata = value->be64;
1525 break;
28da1f8f
BP
1526
1527 case MFF_IN_PORT:
4e022ec0 1528 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
28da1f8f
BP
1529 break;
1530
72333065 1531 case MFF_IN_PORT_OXM: {
4e022ec0 1532 ofp_port_t port;
bc146369 1533 ofputil_port_from_ofp11(value->be32, &port);
4e022ec0 1534 flow->in_port.ofp_port = port;
72333065
JR
1535 break;
1536 }
1537
1b567fb9
AA
1538 case MFF_SKB_PRIORITY:
1539 flow->skb_priority = ntohl(value->be32);
1540 break;
1541
1362e248
JG
1542 case MFF_PKT_MARK:
1543 flow->pkt_mark = ntohl(value->be32);
1b567fb9
AA
1544 break;
1545
0d7e2fe4 1546 CASE_MFF_REGS:
28da1f8f
BP
1547 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1548 break;
28da1f8f
BP
1549
1550 case MFF_ETH_SRC:
1551 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1552 break;
1553
1554 case MFF_ETH_DST:
b5a36e38 1555 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
28da1f8f
BP
1556 break;
1557
1558 case MFF_ETH_TYPE:
1559 flow->dl_type = value->be16;
1560 break;
1561
1562 case MFF_VLAN_TCI:
1563 flow->vlan_tci = value->be16;
1564 break;
1565
441c57a9 1566 case MFF_DL_VLAN:
fb0451d9 1567 flow_set_dl_vlan(flow, value->be16);
28da1f8f 1568 break;
cc34bc8c
BP
1569 case MFF_VLAN_VID:
1570 flow_set_vlan_vid(flow, value->be16);
1571 break;
28da1f8f 1572
441c57a9 1573 case MFF_DL_VLAN_PCP:
cc34bc8c 1574 case MFF_VLAN_PCP:
28da1f8f
BP
1575 flow_set_vlan_pcp(flow, value->u8);
1576 break;
1577
b02475c5
SH
1578 case MFF_MPLS_LABEL:
1579 flow_set_mpls_label(flow, value->be32);
1580 break;
1581
1582 case MFF_MPLS_TC:
1583 flow_set_mpls_tc(flow, value->u8);
1584 break;
1585
1586 case MFF_MPLS_BOS:
1587 flow_set_mpls_bos(flow, value->u8);
1588 break;
1589
28da1f8f
BP
1590 case MFF_IPV4_SRC:
1591 flow->nw_src = value->be32;
1592 break;
1593
1594 case MFF_IPV4_DST:
1595 flow->nw_dst = value->be32;
1596 break;
1597
1598 case MFF_IPV6_SRC:
1599 flow->ipv6_src = value->ipv6;
1600 break;
1601
1602 case MFF_IPV6_DST:
1603 flow->ipv6_dst = value->ipv6;
1604 break;
1605
1606 case MFF_IPV6_LABEL:
1607 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1608 break;
1609
1610 case MFF_IP_PROTO:
1611 flow->nw_proto = value->u8;
1612 break;
1613
1614 case MFF_IP_DSCP:
1615 flow->nw_tos &= ~IP_DSCP_MASK;
1616 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1617 break;
1618
1638b906
BP
1619 case MFF_IP_DSCP_SHIFTED:
1620 flow->nw_tos &= ~IP_DSCP_MASK;
1621 flow->nw_tos |= value->u8 << 2;
1622 break;
1623
28da1f8f
BP
1624 case MFF_IP_ECN:
1625 flow->nw_tos &= ~IP_ECN_MASK;
1626 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1627 break;
1628
1629 case MFF_IP_TTL:
1630 flow->nw_ttl = value->u8;
1631 break;
1632
1633 case MFF_IP_FRAG:
1634 flow->nw_frag &= value->u8;
1635 break;
1636
1637 case MFF_ARP_OP:
1638 flow->nw_proto = ntohs(value->be16);
1639 break;
1640
1641 case MFF_ARP_SPA:
1642 flow->nw_src = value->be32;
1643 break;
1644
1645 case MFF_ARP_TPA:
1646 flow->nw_dst = value->be32;
1647 break;
1648
1649 case MFF_ARP_SHA:
1650 case MFF_ND_SLL:
1651 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1652 break;
1653
1654 case MFF_ARP_THA:
1655 case MFF_ND_TLL:
1656 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1657 break;
1658
1659 case MFF_TCP_SRC:
1660 case MFF_UDP_SRC:
0d56eaf2 1661 case MFF_SCTP_SRC:
28da1f8f
BP
1662 flow->tp_src = value->be16;
1663 break;
1664
1665 case MFF_TCP_DST:
1666 case MFF_UDP_DST:
0d56eaf2 1667 case MFF_SCTP_DST:
28da1f8f
BP
1668 flow->tp_dst = value->be16;
1669 break;
1670
1671 case MFF_ICMPV4_TYPE:
1672 case MFF_ICMPV6_TYPE:
1673 flow->tp_src = htons(value->u8);
1674 break;
1675
1676 case MFF_ICMPV4_CODE:
1677 case MFF_ICMPV6_CODE:
1678 flow->tp_dst = htons(value->u8);
1679 break;
1680
1681 case MFF_ND_TARGET:
1682 flow->nd_target = value->ipv6;
1683 break;
1684
1685 case MFF_N_IDS:
1686 default:
1687 NOT_REACHED();
1688 }
1689}
1690
ccbe50f8
BP
1691/* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1692 *
1693 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1694 * prerequisites. */
1695bool
1696mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1697{
1698 union mf_value value;
1699
1700 mf_get_value(mf, flow, &value);
1701 return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1702}
1703
81a76618 1704/* Makes 'match' wildcard field 'mf'.
6a885fd0 1705 *
81a76618 1706 * The caller is responsible for ensuring that 'match' meets 'mf''s
6a885fd0
BP
1707 * prerequisites. */
1708void
81a76618 1709mf_set_wild(const struct mf_field *mf, struct match *match)
6a885fd0
BP
1710{
1711 switch (mf->id) {
1712 case MFF_TUN_ID:
81a76618 1713 match_set_tun_id_masked(match, htonll(0), htonll(0));
6a885fd0 1714 break;
4fe3445a
PS
1715 case MFF_TUN_SRC:
1716 match_set_tun_src_masked(match, htonl(0), htonl(0));
1717 break;
1718 case MFF_TUN_DST:
1719 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1720 break;
1721 case MFF_TUN_FLAGS:
1722 match_set_tun_flags_masked(match, 0, 0);
1723 break;
1724 case MFF_TUN_TOS:
1725 match_set_tun_tos_masked(match, 0, 0);
1726 break;
1727 case MFF_TUN_TTL:
1728 match_set_tun_ttl_masked(match, 0, 0);
1729 break;
1730
969fc56c 1731 case MFF_METADATA:
81a76618 1732 match_set_metadata_masked(match, htonll(0), htonll(0));
801d2b2a 1733 break;
6a885fd0
BP
1734
1735 case MFF_IN_PORT:
72333065 1736 case MFF_IN_PORT_OXM:
4e022ec0
AW
1737 match->flow.in_port.ofp_port = 0;
1738 match->wc.masks.in_port.ofp_port = 0;
6a885fd0
BP
1739 break;
1740
1b567fb9
AA
1741 case MFF_SKB_PRIORITY:
1742 match->flow.skb_priority = 0;
1743 match->wc.masks.skb_priority = 0;
1744 break;
1745
1362e248
JG
1746 case MFF_PKT_MARK:
1747 match->flow.pkt_mark = 0;
1748 match->wc.masks.pkt_mark = 0;
1b567fb9
AA
1749 break;
1750
0d7e2fe4 1751 CASE_MFF_REGS:
81a76618 1752 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
e9358af6 1753 break;
6a885fd0
BP
1754
1755 case MFF_ETH_SRC:
81a76618
BP
1756 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1757 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
6a885fd0
BP
1758 break;
1759
1760 case MFF_ETH_DST:
81a76618
BP
1761 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1762 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
6a885fd0
BP
1763 break;
1764
1765 case MFF_ETH_TYPE:
81a76618
BP
1766 match->flow.dl_type = htons(0);
1767 match->wc.masks.dl_type = htons(0);
6a885fd0
BP
1768 break;
1769
1770 case MFF_VLAN_TCI:
81a76618 1771 match_set_dl_tci_masked(match, htons(0), htons(0));
6a885fd0
BP
1772 break;
1773
441c57a9 1774 case MFF_DL_VLAN:
cc34bc8c 1775 case MFF_VLAN_VID:
81a76618 1776 match_set_any_vid(match);
6a885fd0
BP
1777 break;
1778
441c57a9 1779 case MFF_DL_VLAN_PCP:
cc34bc8c 1780 case MFF_VLAN_PCP:
81a76618 1781 match_set_any_pcp(match);
6a885fd0
BP
1782 break;
1783
b02475c5
SH
1784 case MFF_MPLS_LABEL:
1785 match_set_any_mpls_label(match);
1786 break;
1787
1788 case MFF_MPLS_TC:
1789 match_set_any_mpls_tc(match);
1790 break;
1791
1792 case MFF_MPLS_BOS:
1793 match_set_any_mpls_bos(match);
1794 break;
1795
6a885fd0
BP
1796 case MFF_IPV4_SRC:
1797 case MFF_ARP_SPA:
81a76618 1798 match_set_nw_src_masked(match, htonl(0), htonl(0));
6a885fd0
BP
1799 break;
1800
1801 case MFF_IPV4_DST:
1802 case MFF_ARP_TPA:
81a76618 1803 match_set_nw_dst_masked(match, htonl(0), htonl(0));
6a885fd0
BP
1804 break;
1805
1806 case MFF_IPV6_SRC:
81a76618
BP
1807 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1808 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
6a885fd0
BP
1809 break;
1810
1811 case MFF_IPV6_DST:
81a76618
BP
1812 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1813 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
6a885fd0
BP
1814 break;
1815
fa8223b7 1816 case MFF_IPV6_LABEL:
81a76618
BP
1817 match->wc.masks.ipv6_label = htonl(0);
1818 match->flow.ipv6_label = htonl(0);
fa8223b7
JP
1819 break;
1820
6a885fd0 1821 case MFF_IP_PROTO:
81a76618
BP
1822 match->wc.masks.nw_proto = 0;
1823 match->flow.nw_proto = 0;
6a885fd0
BP
1824 break;
1825
530180fd 1826 case MFF_IP_DSCP:
1638b906 1827 case MFF_IP_DSCP_SHIFTED:
81a76618
BP
1828 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1829 match->flow.nw_tos &= ~IP_DSCP_MASK;
7257b535
BP
1830 break;
1831
530180fd 1832 case MFF_IP_ECN:
81a76618
BP
1833 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1834 match->flow.nw_tos &= ~IP_ECN_MASK;
530180fd
JP
1835 break;
1836
a61680c6 1837 case MFF_IP_TTL:
81a76618
BP
1838 match->wc.masks.nw_ttl = 0;
1839 match->flow.nw_ttl = 0;
a61680c6
JP
1840 break;
1841
7257b535 1842 case MFF_IP_FRAG:
81a76618
BP
1843 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1844 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
6a885fd0
BP
1845 break;
1846
1847 case MFF_ARP_OP:
81a76618
BP
1848 match->wc.masks.nw_proto = 0;
1849 match->flow.nw_proto = 0;
6a885fd0
BP
1850 break;
1851
1852 case MFF_ARP_SHA:
1853 case MFF_ND_SLL:
81a76618
BP
1854 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1855 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
6a885fd0
BP
1856 break;
1857
1858 case MFF_ARP_THA:
1859 case MFF_ND_TLL:
81a76618
BP
1860 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1861 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
6a885fd0
BP
1862 break;
1863
1864 case MFF_TCP_SRC:
1865 case MFF_UDP_SRC:
0d56eaf2 1866 case MFF_SCTP_SRC:
268a95e0
BP
1867 case MFF_ICMPV4_TYPE:
1868 case MFF_ICMPV6_TYPE:
81a76618
BP
1869 match->wc.masks.tp_src = htons(0);
1870 match->flow.tp_src = htons(0);
6a885fd0
BP
1871 break;
1872
1873 case MFF_TCP_DST:
1874 case MFF_UDP_DST:
0d56eaf2 1875 case MFF_SCTP_DST:
268a95e0
BP
1876 case MFF_ICMPV4_CODE:
1877 case MFF_ICMPV6_CODE:
81a76618
BP
1878 match->wc.masks.tp_dst = htons(0);
1879 match->flow.tp_dst = htons(0);
6a885fd0
BP
1880 break;
1881
1882 case MFF_ND_TARGET:
81a76618
BP
1883 memset(&match->wc.masks.nd_target, 0,
1884 sizeof match->wc.masks.nd_target);
1885 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
6a885fd0
BP
1886 break;
1887
1888 case MFF_N_IDS:
1889 default:
1890 NOT_REACHED();
1891 }
1892}
1893
81a76618 1894/* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
6a885fd0
BP
1895 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1896 * with a 1-bit indicating that the corresponding value bit must match and a
1897 * 0-bit indicating a don't-care.
1898 *
1899 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
81a76618
BP
1900 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1901 * call is equivalent to mf_set_wild(mf, match).
6a885fd0
BP
1902 *
1903 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
81a76618 1904 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
db0b6c29 1905enum ofputil_protocol
6a885fd0
BP
1906mf_set(const struct mf_field *mf,
1907 const union mf_value *value, const union mf_value *mask,
81a76618 1908 struct match *match)
6a885fd0
BP
1909{
1910 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
81a76618 1911 mf_set_value(mf, value, match);
db0b6c29 1912 return mf->usable_protocols;
6a885fd0 1913 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
81a76618 1914 mf_set_wild(mf, match);
db0b6c29 1915 return OFPUTIL_P_ANY;
6a885fd0
BP
1916 }
1917
1918 switch (mf->id) {
1919 case MFF_IN_PORT:
72333065 1920 case MFF_IN_PORT_OXM:
1b567fb9 1921 case MFF_SKB_PRIORITY:
6a885fd0 1922 case MFF_ETH_TYPE:
441c57a9
BP
1923 case MFF_DL_VLAN:
1924 case MFF_DL_VLAN_PCP:
cc34bc8c 1925 case MFF_VLAN_PCP:
b02475c5
SH
1926 case MFF_MPLS_LABEL:
1927 case MFF_MPLS_TC:
1928 case MFF_MPLS_BOS:
6a885fd0 1929 case MFF_IP_PROTO:
a61680c6 1930 case MFF_IP_TTL:
530180fd 1931 case MFF_IP_DSCP:
1638b906 1932 case MFF_IP_DSCP_SHIFTED:
530180fd 1933 case MFF_IP_ECN:
6a885fd0 1934 case MFF_ARP_OP:
268a95e0
BP
1935 case MFF_ICMPV4_TYPE:
1936 case MFF_ICMPV4_CODE:
1937 case MFF_ICMPV6_TYPE:
1938 case MFF_ICMPV6_CODE:
6a885fd0
BP
1939 NOT_REACHED();
1940
1941 case MFF_TUN_ID:
81a76618 1942 match_set_tun_id_masked(match, value->be64, mask->be64);
6a885fd0 1943 break;
4fe3445a
PS
1944 case MFF_TUN_SRC:
1945 match_set_tun_src_masked(match, value->be32, mask->be32);
1946 break;
1947 case MFF_TUN_DST:
1948 match_set_tun_dst_masked(match, value->be32, mask->be32);
1949 break;
1950 case MFF_TUN_FLAGS:
1951 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1952 break;
1953 case MFF_TUN_TTL:
1954 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1955 break;
1956 case MFF_TUN_TOS:
1957 match_set_tun_tos_masked(match, value->u8, mask->u8);
1958 break;
1959
969fc56c 1960 case MFF_METADATA:
81a76618 1961 match_set_metadata_masked(match, value->be64, mask->be64);
969fc56c 1962 break;
6a885fd0 1963
0d7e2fe4 1964 CASE_MFF_REGS:
81a76618
BP
1965 match_set_reg_masked(match, mf->id - MFF_REG0,
1966 ntohl(value->be32), ntohl(mask->be32));
6a885fd0 1967 break;
ac923e91
JG
1968
1969 case MFF_PKT_MARK:
1970 match_set_pkt_mark_masked(match, ntohl(value->be32),
1971 ntohl(mask->be32));
1972 break;
6a885fd0
BP
1973
1974 case MFF_ETH_DST:
81a76618 1975 match_set_dl_dst_masked(match, value->mac, mask->mac);
73c0ce34
JS
1976 break;
1977
1978 case MFF_ETH_SRC:
81a76618 1979 match_set_dl_src_masked(match, value->mac, mask->mac);
6a885fd0
BP
1980 break;
1981
e878338b
SH
1982 case MFF_ARP_SHA:
1983 case MFF_ND_SLL:
81a76618 1984 match_set_arp_sha_masked(match, value->mac, mask->mac);
e878338b
SH
1985 break;
1986
1987 case MFF_ARP_THA:
1988 case MFF_ND_TLL:
81a76618 1989 match_set_arp_tha_masked(match, value->mac, mask->mac);
e878338b
SH
1990 break;
1991
6a885fd0 1992 case MFF_VLAN_TCI:
81a76618 1993 match_set_dl_tci_masked(match, value->be16, mask->be16);
6a885fd0
BP
1994 break;
1995
cc34bc8c 1996 case MFF_VLAN_VID:
81a76618 1997 match_set_vlan_vid_masked(match, value->be16, mask->be16);
cc34bc8c
BP
1998 break;
1999
6a885fd0 2000 case MFF_IPV4_SRC:
81a76618 2001 match_set_nw_src_masked(match, value->be32, mask->be32);
db0b6c29 2002 goto cidr_check;
6a885fd0
BP
2003
2004 case MFF_IPV4_DST:
81a76618 2005 match_set_nw_dst_masked(match, value->be32, mask->be32);
db0b6c29 2006 goto cidr_check;
6a885fd0
BP
2007
2008 case MFF_IPV6_SRC:
81a76618 2009 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
6a885fd0
BP
2010 break;
2011
2012 case MFF_IPV6_DST:
81a76618 2013 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
6a885fd0
BP
2014 break;
2015
32455024
SH
2016 case MFF_IPV6_LABEL:
2017 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
81a76618 2018 mf_set_value(mf, value, match);
32455024 2019 } else {
81a76618 2020 match_set_ipv6_label_masked(match, value->be32, mask->be32);
32455024
SH
2021 }
2022 break;
2023
47284b1f 2024 case MFF_ND_TARGET:
81a76618 2025 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
47284b1f
AA
2026 break;
2027
7257b535 2028 case MFF_IP_FRAG:
81a76618 2029 match_set_nw_frag_masked(match, value->u8, mask->u8);
7257b535
BP
2030 break;
2031
6a885fd0 2032 case MFF_ARP_SPA:
81a76618 2033 match_set_nw_src_masked(match, value->be32, mask->be32);
db0b6c29 2034 goto cidr_check;
6a885fd0
BP
2035
2036 case MFF_ARP_TPA:
81a76618 2037 match_set_nw_dst_masked(match, value->be32, mask->be32);
db0b6c29 2038 goto cidr_check;
6a885fd0 2039
73f33563
BP
2040 case MFF_TCP_SRC:
2041 case MFF_UDP_SRC:
0d56eaf2 2042 case MFF_SCTP_SRC:
81a76618 2043 match_set_tp_src_masked(match, value->be16, mask->be16);
73f33563
BP
2044 break;
2045
2046 case MFF_TCP_DST:
2047 case MFF_UDP_DST:
0d56eaf2 2048 case MFF_SCTP_DST:
81a76618 2049 match_set_tp_dst_masked(match, value->be16, mask->be16);
73f33563
BP
2050 break;
2051
6a885fd0
BP
2052 case MFF_N_IDS:
2053 default:
2054 NOT_REACHED();
2055 }
db0b6c29
JR
2056
2057 return mf->usable_protocols_bitwise;
2058
2059cidr_check:
2060 return ip_is_cidr(mask->be32) ? mf->usable_protocols :
2061 mf->usable_protocols_bitwise;
6a885fd0
BP
2062}
2063
816fd533
BP
2064static enum ofperr
2065mf_check__(const struct mf_subfield *sf, const struct flow *flow,
2066 const char *type)
6a885fd0 2067{
816fd533
BP
2068 if (!sf->field) {
2069 VLOG_WARN_RL(&rl, "unknown %s field", type);
9e404d1e 2070 return OFPERR_OFPBAC_BAD_SET_TYPE;
816fd533
BP
2071 } else if (!sf->n_bits) {
2072 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
9e404d1e 2073 return OFPERR_OFPBAC_BAD_SET_LEN;
816fd533
BP
2074 } else if (sf->ofs >= sf->field->n_bits) {
2075 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
2076 sf->ofs, sf->field->n_bits, type, sf->field->name);
9e404d1e 2077 return OFPERR_OFPBAC_BAD_SET_LEN;
816fd533
BP
2078 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
2079 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
2080 "of %s field %s", sf->ofs, sf->n_bits,
2081 sf->field->n_bits, type, sf->field->name);
9e404d1e 2082 return OFPERR_OFPBAC_BAD_SET_LEN;
816fd533
BP
2083 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
2084 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
2085 type, sf->field->name);
9e404d1e 2086 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
6a885fd0 2087 } else {
816fd533
BP
2088 return 0;
2089 }
816fd533 2090}
6a885fd0 2091
816fd533
BP
2092/* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2093 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2094 * ofp_mkerr()). */
2095enum ofperr
2096mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
2097{
2098 return mf_check__(sf, flow, "source");
2099}
6a885fd0 2100
816fd533
BP
2101/* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2102 * if so, otherwise an OpenFlow error code (e.g. as returned by
2103 * ofp_mkerr()). */
2104enum ofperr
2105mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
2106{
2107 int error = mf_check__(sf, flow, "destination");
2108 if (!error && !sf->field->writable) {
2109 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2110 sf->field->name);
9e404d1e 2111 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6a885fd0 2112 }
816fd533 2113 return error;
6a885fd0
BP
2114}
2115
81a76618 2116/* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
6a885fd0
BP
2117 * 'value' and 'mask', respectively. */
2118void
81a76618 2119mf_get(const struct mf_field *mf, const struct match *match,
6a885fd0
BP
2120 union mf_value *value, union mf_value *mask)
2121{
81a76618
BP
2122 mf_get_value(mf, &match->flow, value);
2123 mf_get_mask(mf, &match->wc, mask);
6a885fd0
BP
2124}
2125
2126/* Assigns a random value for field 'mf' to 'value'. */
2127void
2128mf_random_value(const struct mf_field *mf, union mf_value *value)
2129{
2130 random_bytes(value, mf->n_bytes);
2131
2132 switch (mf->id) {
2133 case MFF_TUN_ID:
4fe3445a
PS
2134 case MFF_TUN_SRC:
2135 case MFF_TUN_DST:
2136 case MFF_TUN_TOS:
2137 case MFF_TUN_TTL:
2138 case MFF_TUN_FLAGS:
969fc56c 2139 case MFF_METADATA:
6a885fd0 2140 case MFF_IN_PORT:
1362e248 2141 case MFF_PKT_MARK:
1b567fb9 2142 case MFF_SKB_PRIORITY:
0d7e2fe4 2143 CASE_MFF_REGS:
6a885fd0
BP
2144 case MFF_ETH_SRC:
2145 case MFF_ETH_DST:
2146 case MFF_ETH_TYPE:
2147 case MFF_VLAN_TCI:
2148 case MFF_IPV4_SRC:
2149 case MFF_IPV4_DST:
2150 case MFF_IPV6_SRC:
2151 case MFF_IPV6_DST:
2152 case MFF_IP_PROTO:
a61680c6 2153 case MFF_IP_TTL:
6a885fd0
BP
2154 case MFF_ARP_SPA:
2155 case MFF_ARP_TPA:
2156 case MFF_ARP_SHA:
2157 case MFF_ARP_THA:
2158 case MFF_TCP_SRC:
2159 case MFF_TCP_DST:
2160 case MFF_UDP_SRC:
2161 case MFF_UDP_DST:
0d56eaf2
JS
2162 case MFF_SCTP_SRC:
2163 case MFF_SCTP_DST:
268a95e0
BP
2164 case MFF_ICMPV4_TYPE:
2165 case MFF_ICMPV4_CODE:
2166 case MFF_ICMPV6_TYPE:
2167 case MFF_ICMPV6_CODE:
6a885fd0
BP
2168 case MFF_ND_TARGET:
2169 case MFF_ND_SLL:
2170 case MFF_ND_TLL:
2171 break;
2172
72333065 2173 case MFF_IN_PORT_OXM:
4e022ec0 2174 value->be32 = ofputil_port_to_ofp11(u16_to_ofp(ntohs(value->be16)));
72333065
JR
2175 break;
2176
fa8223b7
JP
2177 case MFF_IPV6_LABEL:
2178 value->be32 &= ~htonl(IPV6_LABEL_MASK);
2179 break;
2180
530180fd
JP
2181 case MFF_IP_DSCP:
2182 value->u8 &= IP_DSCP_MASK;
2183 break;
2184
1638b906
BP
2185 case MFF_IP_DSCP_SHIFTED:
2186 value->u8 &= IP_DSCP_MASK >> 2;
2187 break;
2188
530180fd
JP
2189 case MFF_IP_ECN:
2190 value->u8 &= IP_ECN_MASK;
6a885fd0
BP
2191 break;
2192
7257b535 2193 case MFF_IP_FRAG:
eadef313 2194 value->u8 &= FLOW_NW_FRAG_MASK;
7257b535
BP
2195 break;
2196
6a885fd0
BP
2197 case MFF_ARP_OP:
2198 value->be16 &= htons(0xff);
2199 break;
2200
441c57a9 2201 case MFF_DL_VLAN:
6a885fd0
BP
2202 value->be16 &= htons(VLAN_VID_MASK);
2203 break;
cc34bc8c
BP
2204 case MFF_VLAN_VID:
2205 value->be16 &= htons(VLAN_VID_MASK | VLAN_CFI);
2206 break;
6a885fd0 2207
441c57a9 2208 case MFF_DL_VLAN_PCP:
cc34bc8c 2209 case MFF_VLAN_PCP:
6a885fd0
BP
2210 value->u8 &= 0x07;
2211 break;
2212
b02475c5
SH
2213 case MFF_MPLS_LABEL:
2214 value->be32 &= htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT);
2215 break;
2216
2217 case MFF_MPLS_TC:
2218 value->u8 &= MPLS_TC_MASK >> MPLS_TC_SHIFT;
2219 break;
2220
2221 case MFF_MPLS_BOS:
2222 value->u8 &= MPLS_BOS_MASK >> MPLS_BOS_SHIFT;
2223 break;
2224
6a885fd0
BP
2225 case MFF_N_IDS:
2226 default:
2227 NOT_REACHED();
2228 }
2229}
2230
2231static char *
2232mf_from_integer_string(const struct mf_field *mf, const char *s,
2233 uint8_t *valuep, uint8_t *maskp)
2234{
2235 unsigned long long int integer, mask;
2236 char *tail;
2237 int i;
2238
2239 errno = 0;
2240 integer = strtoull(s, &tail, 0);
2241 if (errno || (*tail != '\0' && *tail != '/')) {
2242 goto syntax_error;
2243 }
2244
2245 if (*tail == '/') {
2246 mask = strtoull(tail + 1, &tail, 0);
2247 if (errno || *tail != '\0') {
2248 goto syntax_error;
2249 }
2250 } else {
2251 mask = ULLONG_MAX;
2252 }
2253
2254 for (i = mf->n_bytes - 1; i >= 0; i--) {
2255 valuep[i] = integer;
2256 maskp[i] = mask;
2257 integer >>= 8;
2258 mask >>= 8;
2259 }
2260 if (integer) {
2261 return xasprintf("%s: value too large for %u-byte field %s",
2262 s, mf->n_bytes, mf->name);
2263 }
2264 return NULL;
2265
2266syntax_error:
2267 return xasprintf("%s: bad syntax for %s", s, mf->name);
2268}
2269
2270static char *
2271mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2272 uint8_t mac[ETH_ADDR_LEN],
2273 uint8_t mask[ETH_ADDR_LEN])
2274{
279fb135 2275 int n;
6a885fd0 2276
279fb135 2277 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
6a885fd0 2278
279fb135
BP
2279 n = -1;
2280 if (sscanf(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n) > 0
2281 && n == strlen(s)) {
6a885fd0
BP
2282 memset(mask, 0xff, ETH_ADDR_LEN);
2283 return NULL;
279fb135 2284 }
6a885fd0 2285
279fb135
BP
2286 n = -1;
2287 if (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
2288 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n) > 0
2289 && n == strlen(s)) {
2290 return NULL;
6a885fd0 2291 }
279fb135
BP
2292
2293 return xasprintf("%s: invalid Ethernet address", s);
6a885fd0
BP
2294}
2295
2296static char *
2297mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2298 ovs_be32 *ip, ovs_be32 *mask)
2299{
2300 int prefix;
2301
cb22974d 2302 ovs_assert(mf->n_bytes == sizeof *ip);
6a885fd0
BP
2303
2304 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2305 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
2306 /* OK. */
2307 } else if (sscanf(s, IP_SCAN_FMT"/%d",
2308 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
2309 if (prefix <= 0 || prefix > 32) {
2310 return xasprintf("%s: network prefix bits not between 1 and "
2311 "32", s);
2312 } else if (prefix == 32) {
2313 *mask = htonl(UINT32_MAX);
2314 } else {
2315 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2316 }
2317 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
2318 *mask = htonl(UINT32_MAX);
2319 } else {
2320 return xasprintf("%s: invalid IP address", s);
2321 }
2322 return NULL;
2323}
2324
2325static char *
2326mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2327 struct in6_addr *value, struct in6_addr *mask)
2328{
2329 char *str = xstrdup(s);
2330 char *save_ptr = NULL;
2331 const char *name, *netmask;
2332 int retval;
2333
cb22974d 2334 ovs_assert(mf->n_bytes == sizeof *value);
6a885fd0
BP
2335
2336 name = strtok_r(str, "/", &save_ptr);
2337 retval = name ? lookup_ipv6(name, value) : EINVAL;
2338 if (retval) {
2339 char *err;
2340
2341 err = xasprintf("%s: could not convert to IPv6 address", str);
2342 free(str);
2343
2344 return err;
2345 }
2346
2347 netmask = strtok_r(NULL, "/", &save_ptr);
2348 if (netmask) {
ff0b06ee
BP
2349 if (inet_pton(AF_INET6, netmask, mask) != 1) {
2350 int prefix = atoi(netmask);
2351 if (prefix <= 0 || prefix > 128) {
2352 free(str);
2353 return xasprintf("%s: prefix bits not between 1 and 128", s);
2354 } else {
2355 *mask = ipv6_create_mask(prefix);
2356 }
6a885fd0
BP
2357 }
2358 } else {
2359 *mask = in6addr_exact;
2360 }
2361 free(str);
2362
2363 return NULL;
2364}
2365
2366static char *
2367mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2368 ovs_be16 *valuep, ovs_be16 *maskp)
2369{
4e022ec0 2370 ofp_port_t port;
6a885fd0 2371
cb22974d 2372 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
33ab38d9
JR
2373
2374 if (ofputil_port_from_string(s, &port)) {
4e022ec0 2375 *valuep = htons(ofp_to_u16(port));
6a885fd0
BP
2376 *maskp = htons(UINT16_MAX);
2377 return NULL;
6a885fd0 2378 }
33ab38d9 2379 return xasprintf("%s: port value out of range for %s", s, mf->name);
6a885fd0
BP
2380}
2381
72333065
JR
2382static char *
2383mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2384 ovs_be32 *valuep, ovs_be32 *maskp)
2385{
4e022ec0 2386 ofp_port_t port;
72333065
JR
2387
2388 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2389 if (ofputil_port_from_string(s, &port)) {
2390 *valuep = ofputil_port_to_ofp11(port);
2391 *maskp = htonl(UINT32_MAX);
2392 return NULL;
2393 }
2394 return xasprintf("%s: port value out of range for %s", s, mf->name);
2395}
2396
7257b535
BP
2397struct frag_handling {
2398 const char *name;
2399 uint8_t mask;
2400 uint8_t value;
2401};
2402
2403static const struct frag_handling all_frags[] = {
eadef313
JP
2404#define A FLOW_NW_FRAG_ANY
2405#define L FLOW_NW_FRAG_LATER
7257b535
BP
2406 /* name mask value */
2407
2408 { "no", A|L, 0 },
2409 { "first", A|L, A },
2410 { "later", A|L, A|L },
2411
2412 { "no", A, 0 },
2413 { "yes", A, A },
2414
2415 { "not_later", L, 0 },
2416 { "later", L, L },
2417#undef A
2418#undef L
2419};
2420
2421static char *
2422mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2423{
2424 const struct frag_handling *h;
2425
2426 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2427 if (!strcasecmp(s, h->name)) {
2428 /* We force the upper bits of the mask on to make mf_parse_value()
2429 * happy (otherwise it will never think it's an exact match.) */
eadef313 2430 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
7257b535
BP
2431 *valuep = h->value;
2432 return NULL;
2433 }
2434 }
2435
2436 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2437 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2438}
2439
4fe3445a
PS
2440static int
2441parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
2442 ovs_be16 *res)
2443{
2444 uint32_t result = 0;
2445 char *save_ptr = NULL;
2446 char *name;
2447 int rc = 0;
2448 char *s = xstrdup(s_);
2449
2450 for (name = strtok_r((char *)s, " |", &save_ptr); name;
2451 name = strtok_r(NULL, " |", &save_ptr)) {
2452 int name_len;
2453 unsigned long long int flags;
2454 uint32_t bit;
2455 int n0;
2456
2457 if (sscanf(name, "%lli%n", &flags, &n0) > 0 && n0 > 0) {
2458 result |= flags;
2459 continue;
2460 }
2461 name_len = strlen(name);
2462 for (bit = 1; bit; bit <<= 1) {
2463 const char *fname = bit_to_string(bit);
2464 size_t len;
2465
2466 if (!fname) {
2467 continue;
2468 }
2469
2470 len = strlen(fname);
2471 if (len != name_len) {
2472 continue;
2473 }
2474 if (!strncmp(name, fname, len)) {
2475 result |= bit;
2476 break;
2477 }
2478 }
2479
2480 if (!bit) {
2481 rc = -ENOENT;
2482 goto out;
2483 }
2484 }
2485
2486 *res = htons(result);
2487out:
2488 free(s);
2489 return rc;
2490}
2491
2492static char *
b0aa8146 2493mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
4fe3445a
PS
2494{
2495 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
b0aa8146 2496 *maskp = htons(UINT16_MAX);
4fe3445a
PS
2497 return NULL;
2498 }
2499
2500 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
2501 "\"csum\", \"key\"", s);
2502}
2503
6a885fd0
BP
2504/* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2505 * NULL if successful, otherwise a malloc()'d string describing the error. */
2506char *
2507mf_parse(const struct mf_field *mf, const char *s,
2508 union mf_value *value, union mf_value *mask)
2509{
bedde04c 2510 if (!strcmp(s, "*")) {
6a885fd0
BP
2511 memset(value, 0, mf->n_bytes);
2512 memset(mask, 0, mf->n_bytes);
2513 return NULL;
2514 }
2515
2516 switch (mf->string) {
2517 case MFS_DECIMAL:
2518 case MFS_HEXADECIMAL:
2519 return mf_from_integer_string(mf, s,
2520 (uint8_t *) value, (uint8_t *) mask);
2521
2522 case MFS_ETHERNET:
2523 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2524
2525 case MFS_IPV4:
2526 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2527
2528 case MFS_IPV6:
2529 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2530
2531 case MFS_OFP_PORT:
2532 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
7257b535 2533
72333065
JR
2534 case MFS_OFP_PORT_OXM:
2535 return mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2536
7257b535
BP
2537 case MFS_FRAG:
2538 return mf_from_frag_string(s, &value->u8, &mask->u8);
4fe3445a
PS
2539
2540 case MFS_TNL_FLAGS:
cb22974d 2541 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
b0aa8146 2542 return mf_from_tun_flags_string(s, &value->be16, &mask->be16);
6a885fd0
BP
2543 }
2544 NOT_REACHED();
2545}
2546
2547/* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2548 * successful, otherwise a malloc()'d string describing the error. */
2549char *
2550mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2551{
2552 union mf_value mask;
2553 char *error;
2554
2555 error = mf_parse(mf, s, value, &mask);
2556 if (error) {
2557 return error;
2558 }
2559
2560 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2561 return xasprintf("%s: wildcards not allowed here", s);
2562 }
2563 return NULL;
2564}
2565
2566static void
2567mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2568 const uint8_t *maskp, struct ds *s)
2569{
2570 unsigned long long int integer;
2571 int i;
2572
cb22974d 2573 ovs_assert(mf->n_bytes <= 8);
6a885fd0
BP
2574
2575 integer = 0;
2576 for (i = 0; i < mf->n_bytes; i++) {
2577 integer = (integer << 8) | valuep[i];
2578 }
2579 if (mf->string == MFS_HEXADECIMAL) {
2580 ds_put_format(s, "%#llx", integer);
2581 } else {
2582 ds_put_format(s, "%lld", integer);
2583 }
2584
2585 if (maskp) {
2586 unsigned long long int mask;
2587
2588 mask = 0;
2589 for (i = 0; i < mf->n_bytes; i++) {
2590 mask = (mask << 8) | maskp[i];
2591 }
2592
2593 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2594 * not sure that that a bit-mask written in decimal is ever easier to
2595 * understand than the same bit-mask written in hexadecimal. */
2596 ds_put_format(s, "/%#llx", mask);
2597 }
2598}
2599
7257b535 2600static void
8f75dea3 2601mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
7257b535
BP
2602{
2603 const struct frag_handling *h;
7257b535 2604
eadef313 2605 mask &= FLOW_NW_FRAG_MASK;
8f75dea3 2606 value &= mask;
7257b535
BP
2607
2608 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2609 if (value == h->value && mask == h->mask) {
2610 ds_put_cstr(s, h->name);
2611 return;
2612 }
2613 }
2614 ds_put_cstr(s, "<error>");
2615}
2616
4fe3445a
PS
2617static void
2618mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2619{
2620 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2621}
2622
6a885fd0
BP
2623/* Appends to 's' a string representation of field 'mf' whose value is in
2624 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2625void
2626mf_format(const struct mf_field *mf,
2627 const union mf_value *value, const union mf_value *mask,
2628 struct ds *s)
2629{
2630 if (mask) {
2631 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2632 ds_put_cstr(s, "ANY");
2633 return;
2634 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2635 mask = NULL;
2636 }
2637 }
2638
2639 switch (mf->string) {
72333065
JR
2640 case MFS_OFP_PORT_OXM:
2641 if (!mask) {
4e022ec0 2642 ofp_port_t port;
bc146369 2643 ofputil_port_from_ofp11(value->be32, &port);
72333065
JR
2644 ofputil_format_port(port, s);
2645 break;
2646 }
2647 /* fall through */
6a885fd0
BP
2648 case MFS_OFP_PORT:
2649 if (!mask) {
4e022ec0 2650 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
6a885fd0
BP
2651 break;
2652 }
2653 /* fall through */
2654 case MFS_DECIMAL:
2655 case MFS_HEXADECIMAL:
2656 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2657 break;
2658
2659 case MFS_ETHERNET:
3b4d8ad3 2660 eth_format_masked(value->mac, mask->mac, s);
6a885fd0
BP
2661 break;
2662
2663 case MFS_IPV4:
2664 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2665 s);
2666 break;
2667
2668 case MFS_IPV6:
2669 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2670 break;
2671
7257b535 2672 case MFS_FRAG:
8f75dea3 2673 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
7257b535
BP
2674 break;
2675
4fe3445a
PS
2676 case MFS_TNL_FLAGS:
2677 mf_format_tnl_flags_string(&value->be16, s);
2678 break;
2679
6a885fd0
BP
2680 default:
2681 NOT_REACHED();
2682 }
2683}
816fd533 2684\f
9bab681f
IY
2685/* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2686 * least-significant bits in 'x'.
2687 */
2688void
2689mf_write_subfield_flow(const struct mf_subfield *sf,
2690 const union mf_subvalue *x, struct flow *flow)
2691{
2692 const struct mf_field *field = sf->field;
2693 union mf_value value;
2694
2695 mf_get_value(field, flow, &value);
158edc8d 2696 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
9bab681f
IY
2697 sf->ofs, sf->n_bits);
2698 mf_set_flow_value(field, &value, flow);
2699}
2700
81a76618 2701/* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
1b35df45 2702 * least-significant bits in 'x'.
615660a9 2703 */
1b35df45
BP
2704void
2705mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
81a76618 2706 struct match *match)
1b35df45
BP
2707{
2708 const struct mf_field *field = sf->field;
2709 union mf_value value, mask;
2710
81a76618 2711 mf_get(field, match, &value, &mask);
1b35df45
BP
2712 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2713 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
81a76618 2714 mf_set(field, &value, &mask, match);
1b35df45
BP
2715}
2716
1b35df45
BP
2717/* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2718 * reading 'flow', e.g. as checked by mf_check_src(). */
2719void
2720mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2721 union mf_subvalue *x)
2722{
2723 union mf_value value;
2724
2725 mf_get_value(sf->field, flow, &value);
2726
2727 memset(x, 0, sizeof *x);
2728 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2729 x, sizeof *x, 0,
2730 sf->n_bits);
2731}
2732
816fd533
BP
2733/* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2734 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2735 * less. */
2736uint64_t
2737mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2738{
2739 union mf_value value;
2740
2741 mf_get_value(sf->field, flow, &value);
2742 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2743}
2744
2745/* Formats 'sf' into 's' in a format normally acceptable to
2746 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2747 * sf->field has no NXM name.) */
2748void
2749mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2750{
2751 if (!sf->field) {
2752 ds_put_cstr(s, "<unknown>");
2753 } else if (sf->field->nxm_name) {
2754 ds_put_cstr(s, sf->field->nxm_name);
2755 } else if (sf->field->nxm_header) {
2756 uint32_t header = sf->field->nxm_header;
2757 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2758 } else {
2759 ds_put_cstr(s, sf->field->name);
2760 }
2761
33500edd 2762 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
816fd533
BP
2763 ds_put_cstr(s, "[]");
2764 } else if (sf->n_bits == 1) {
2765 ds_put_format(s, "[%d]", sf->ofs);
2766 } else {
2767 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2768 }
2769}
2770
2771static const struct mf_field *
2772mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2773{
2774 int i;
2775
2776 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2777 if (*wild) {
2778 name_len -= 2;
2779 }
2780
2781 for (i = 0; i < MFF_N_IDS; i++) {
2782 const struct mf_field *mf = mf_from_id(i);
2783
2784 if (mf->nxm_name
2785 && !strncmp(mf->nxm_name, name, name_len)
2786 && mf->nxm_name[name_len] == '\0') {
2787 return mf;
2788 }
b5e5143b
BP
2789 if (mf->oxm_name
2790 && !strncmp(mf->oxm_name, name, name_len)
2791 && mf->oxm_name[name_len] == '\0') {
2792 return mf;
2793 }
816fd533
BP
2794 }
2795
2796 return NULL;
2797}
2798
2799/* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2800 * returns NULL and advances '*sp' to the first byte following the parsed
2801 * string. On failure, returns a malloc()'d error message, does not modify
2802 * '*sp', and does not properly initialize 'sf'.
2803 *
2804 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2805 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2806 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2807 * may both be omitted (the [] are still required) to indicate an entire
2808 * field. */
bdda5aca 2809char * WARN_UNUSED_RESULT
816fd533
BP
2810mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2811{
2812 const struct mf_field *field;
2813 const char *name;
2814 int start, end;
2815 const char *s;
2816 int name_len;
2817 bool wild;
2818
2819 s = *sp;
2820 name = s;
2821 name_len = strcspn(s, "[");
2822 if (s[name_len] != '[') {
2823 return xasprintf("%s: missing [ looking for field name", *sp);
2824 }
2825
2826 field = mf_parse_subfield_name(name, name_len, &wild);
2827 if (!field) {
2828 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2829 }
2830
2831 s += name_len;
2832 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2833 /* Nothing to do. */
2834 } else if (sscanf(s, "[%d]", &start) == 1) {
2835 end = start;
2836 } else if (!strncmp(s, "[]", 2)) {
2837 start = 0;
2838 end = field->n_bits - 1;
2839 } else {
2840 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2841 "[<start>..<end>]", *sp);
2842 }
2843 s = strchr(s, ']') + 1;
2844
2845 if (start > end) {
2846 return xasprintf("%s: starting bit %d is after ending bit %d",
2847 *sp, start, end);
2848 } else if (start >= field->n_bits) {
2849 return xasprintf("%s: starting bit %d is not valid because field is "
2850 "only %d bits wide", *sp, start, field->n_bits);
2851 } else if (end >= field->n_bits){
2852 return xasprintf("%s: ending bit %d is not valid because field is "
2853 "only %d bits wide", *sp, end, field->n_bits);
2854 }
2855
2856 sf->field = field;
2857 sf->ofs = start;
2858 sf->n_bits = end - start + 1;
2859
2860 *sp = s;
2861 return NULL;
2862}
2863
bdda5aca
BP
2864/* Parses a subfield from the entirety of 's' into 'sf'. Returns NULL if
2865 * successful, otherwise a malloc()'d string describing the error. The caller
2866 * is responsible for freeing the returned string.
816fd533
BP
2867 *
2868 * The syntax parsed from 's' takes the form "header[start..end]" where
2869 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2870 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2871 * may both be omitted (the [] are still required) to indicate an entire
2872 * field. */
bdda5aca 2873char * WARN_UNUSED_RESULT
816fd533
BP
2874mf_parse_subfield(struct mf_subfield *sf, const char *s)
2875{
bdda5aca
BP
2876 char *error = mf_parse_subfield__(sf, &s);
2877 if (!error && s[0]) {
2878 error = xstrdup("unexpected input following field syntax");
816fd533 2879 }
bdda5aca 2880 return error;
816fd533 2881}
9bab681f
IY
2882
2883void
2884mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2885{
2886 int i;
2887
2888 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2889 if (subvalue->u8[i]) {
2890 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2891 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2892 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2893 }
2894 return;
2895 }
2896 }
2897 ds_put_char(s, '0');
2898}