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