]> git.proxmox.com Git - mirror_ovs.git/blame - lib/nx-match.c
test-classifier: Test CLASSIFIER_FOR_EACH_EXACT_RULE.
[mirror_ovs.git] / lib / nx-match.c
CommitLineData
09246b99
BP
1/*
2 * Copyright (c) 2010 Nicira Networks.
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 "nx-match.h"
20
21#include "classifier.h"
22#include "dynamic-string.h"
23#include "ofp-util.h"
24#include "ofpbuf.h"
25#include "openflow/nicira-ext.h"
26#include "packets.h"
27#include "unaligned.h"
28#include "vlog.h"
29
30VLOG_DEFINE_THIS_MODULE(nx_match);
31
32/* Rate limit for nx_match parse errors. These always indicate a bug in the
33 * peer and so there's not much point in showing a lot of them. */
34static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
35
36enum {
37 NXM_INVALID = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_INVALID),
38 NXM_BAD_TYPE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_TYPE),
39 NXM_BAD_VALUE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_VALUE),
40 NXM_BAD_MASK = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_MASK),
41 NXM_BAD_PREREQ = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_PREREQ),
42 NXM_DUP_TYPE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_DUP_TYPE),
43 BAD_ARGUMENT = OFP_MKERR(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT)
44};
45
46/* For each NXM_* field, define NFI_NXM_* as consecutive integers starting from
47 * zero. */
48enum nxm_field_index {
49#define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) NFI_NXM_##HEADER,
50#include "nx-match.def"
51 N_NXM_FIELDS
52};
53
54struct nxm_field {
55 struct hmap_node hmap_node;
56 enum nxm_field_index index; /* NFI_* value. */
57 uint32_t header; /* NXM_* value. */
58 uint32_t wildcard; /* Wildcard bit, if exactly one. */
59 ovs_be16 dl_type; /* dl_type prerequisite, if nonzero. */
60 uint8_t nw_proto; /* nw_proto prerequisite, if nonzero. */
61 const char *name; /* "NXM_*" string. */
62};
63
64/* All the known fields. */
65static struct nxm_field nxm_fields[N_NXM_FIELDS] = {
66#define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) \
67 { HMAP_NODE_NULL_INITIALIZER, NFI_NXM_##HEADER, NXM_##HEADER, WILDCARD, \
68 CONSTANT_HTONS(DL_TYPE), NW_PROTO, "NXM_" #HEADER },
69#include "nx-match.def"
70};
71
72/* Hash table of 'nxm_fields'. */
73static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
74
75static void
76nxm_init(void)
77{
78 if (hmap_is_empty(&all_nxm_fields)) {
79 int i;
80
81 for (i = 0; i < N_NXM_FIELDS; i++) {
82 struct nxm_field *f = &nxm_fields[i];
83 hmap_insert(&all_nxm_fields, &f->hmap_node,
84 hash_int(f->header, 0));
85 }
86
87 /* Verify that the header values are unique (duplicate "case" values
88 * cause a compile error). */
89 switch (0) {
90#define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) \
91 case NXM_##HEADER: break;
92#include "nx-match.def"
93 }
94 }
95}
96
97static const struct nxm_field *
98nxm_field_lookup(uint32_t header)
99{
100 struct nxm_field *f;
101
102 nxm_init();
103
104 HMAP_FOR_EACH_WITH_HASH (f, hmap_node, hash_int(header, 0),
105 &all_nxm_fields) {
106 if (f->header == header) {
107 return f;
108 }
109 }
110
111 return NULL;
112}
113
114/* Returns the width of the data for a field with the given 'header', in
115 * bytes. */
116static int
117nxm_field_bytes(uint32_t header)
118{
119 unsigned int length = NXM_LENGTH(header);
120 return NXM_HASMASK(header) ? length / 2 : length;
121}
122\f
123/* nx_pull_match() and helpers. */
124
125static int
126parse_tci(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask)
127{
128 enum { OFPFW_DL_TCI = OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP };
129 if ((rule->wc.wildcards & OFPFW_DL_TCI) != OFPFW_DL_TCI) {
130 return NXM_DUP_TYPE;
131 } else {
132 return cls_rule_set_dl_tci_masked(rule, tci, mask) ? 0 : NXM_INVALID;
133 }
134}
135
136static int
137parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
138 const void *value, const void *mask)
139{
140 struct flow_wildcards *wc = &rule->wc;
141 struct flow *flow = &rule->flow;
142
143 switch (f->index) {
144 /* Metadata. */
145 case NFI_NXM_OF_IN_PORT:
146 flow->in_port = ntohs(get_unaligned_u16(value));
147 if (flow->in_port == OFPP_LOCAL) {
148 flow->in_port = ODPP_LOCAL;
149 }
150 return 0;
151
152 /* Ethernet header. */
153 case NFI_NXM_OF_ETH_DST:
154 memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
155 return 0;
156 case NFI_NXM_OF_ETH_SRC:
157 memcpy(flow->dl_src, value, ETH_ADDR_LEN);
158 return 0;
159 case NFI_NXM_OF_ETH_TYPE:
160 flow->dl_type = get_unaligned_u16(value);
161 return 0;
162
163 /* 802.1Q header. */
164 case NFI_NXM_OF_VLAN_TCI:
165 return parse_tci(rule, get_unaligned_u16(value), htons(UINT16_MAX));
166
167 case NFI_NXM_OF_VLAN_TCI_W:
168 return parse_tci(rule, get_unaligned_u16(value),
169 get_unaligned_u16(mask));
170
171 /* IP header. */
172 case NFI_NXM_OF_IP_TOS:
173 if (*(uint8_t *) value & 0x03) {
174 return NXM_BAD_VALUE;
175 } else {
176 flow->nw_tos = *(uint8_t *) value;
177 return 0;
178 }
179 case NFI_NXM_OF_IP_PROTO:
180 flow->nw_proto = *(uint8_t *) value;
181 return 0;
182
183 /* IP addresses in IP and ARP headers. */
184 case NFI_NXM_OF_IP_SRC:
185 case NFI_NXM_OF_ARP_SPA:
186 if (wc->nw_src_mask) {
187 return NXM_DUP_TYPE;
188 } else {
189 cls_rule_set_nw_src(rule, get_unaligned_u32(value));
190 return 0;
191 }
192 case NFI_NXM_OF_IP_SRC_W:
193 case NFI_NXM_OF_ARP_SPA_W:
194 if (wc->nw_src_mask) {
195 return NXM_DUP_TYPE;
196 } else {
197 ovs_be32 ip = get_unaligned_u32(value);
198 ovs_be32 netmask = get_unaligned_u32(mask);
199 if (!cls_rule_set_nw_src_masked(rule, ip, netmask)) {
200 return NXM_BAD_MASK;
201 }
202 return 0;
203 }
204 case NFI_NXM_OF_IP_DST:
205 case NFI_NXM_OF_ARP_TPA:
206 if (wc->nw_dst_mask) {
207 return NXM_DUP_TYPE;
208 } else {
209 cls_rule_set_nw_dst(rule, get_unaligned_u32(value));
210 return 0;
211 }
212 case NFI_NXM_OF_IP_DST_W:
213 case NFI_NXM_OF_ARP_TPA_W:
214 if (wc->nw_dst_mask) {
215 return NXM_DUP_TYPE;
216 } else {
217 ovs_be32 ip = get_unaligned_u32(value);
218 ovs_be32 netmask = get_unaligned_u32(mask);
219 if (!cls_rule_set_nw_dst_masked(rule, ip, netmask)) {
220 return NXM_BAD_MASK;
221 }
222 return 0;
223 }
224
225 /* TCP header. */
226 case NFI_NXM_OF_TCP_SRC:
227 flow->tp_src = get_unaligned_u16(value);
228 return 0;
229 case NFI_NXM_OF_TCP_DST:
230 flow->tp_dst = get_unaligned_u16(value);
231 return 0;
232
233 /* UDP header. */
234 case NFI_NXM_OF_UDP_SRC:
235 flow->tp_src = get_unaligned_u16(value);
236 return 0;
237 case NFI_NXM_OF_UDP_DST:
238 flow->tp_dst = get_unaligned_u16(value);
239 return 0;
240
241 /* ICMP header. */
242 case NFI_NXM_OF_ICMP_TYPE:
243 flow->tp_src = htons(*(uint8_t *) value);
244 return 0;
245 case NFI_NXM_OF_ICMP_CODE:
246 flow->tp_dst = htons(*(uint8_t *) value);
247 return 0;
248
249 /* ARP header. */
250 case NFI_NXM_OF_ARP_OP:
251 if (ntohs(get_unaligned_u16(value)) > 255) {
252 return NXM_BAD_VALUE;
253 } else {
254 flow->nw_proto = ntohs(get_unaligned_u16(value));
255 return 0;
256 }
257
258 /* Tunnel ID. */
259 case NFI_NXM_NX_TUN_ID:
260 flow->tun_id = htonl(ntohll(get_unaligned_u64(value)));
261 return 0;
262
263 case N_NXM_FIELDS:
264 NOT_REACHED();
265 }
266 NOT_REACHED();
267}
268
269static bool
270nxm_prereqs_ok(const struct nxm_field *field, const struct flow *flow)
271{
272 return (!field->dl_type
273 || (field->dl_type == flow->dl_type
274 && (!field->nw_proto || field->nw_proto == flow->nw_proto)));
275}
276
277static uint32_t
278nx_entry_ok(const void *p, unsigned int match_len)
279{
280 unsigned int payload_len;
281 ovs_be32 header_be;
282 uint32_t header;
283
284 if (match_len < 4) {
285 if (match_len) {
286 VLOG_DBG_RL(&rl, "nx_match ends with partial nxm_header");
287 }
288 return 0;
289 }
290 memcpy(&header_be, p, 4);
291 header = ntohl(header_be);
292
293 payload_len = NXM_LENGTH(header);
294 if (!payload_len) {
295 VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
296 "length 0", header);
297 return 0;
298 }
299 if (match_len < payload_len + 4) {
300 VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
301 "%u bytes left in nx_match", payload_len + 4, match_len);
302 return 0;
303 }
304
305 return header;
306}
307
308int
309nx_pull_match(struct ofpbuf *b, unsigned int match_len, uint16_t priority,
310 struct cls_rule *rule)
311{
312 uint32_t header;
313 uint8_t *p;
314
315 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
316 if (!p) {
317 VLOG_DBG_RL(&rl, "nx_match length %zu, rounded up to a "
318 "multiple of 8, is longer than space in message (max "
319 "length %zu)", match_len, b->size);
320 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
321 }
322
323 cls_rule_init_catchall(rule, priority);
324 while ((header = nx_entry_ok(p, match_len)) != 0) {
325 unsigned length = NXM_LENGTH(header);
326 const struct nxm_field *f;
327 int error;
328
329 f = nxm_field_lookup(header);
330 if (!f) {
331 error = NXM_BAD_TYPE;
332 } else if (!nxm_prereqs_ok(f, &rule->flow)) {
333 error = NXM_BAD_PREREQ;
334 } else if (f->wildcard && !(rule->wc.wildcards & f->wildcard)) {
335 error = NXM_DUP_TYPE;
336 } else {
337 /* 'hasmask' and 'length' are known to be correct at this point
338 * because they are included in 'header' and nxm_field_lookup()
339 * checked them already. */
340 rule->wc.wildcards &= ~f->wildcard;
341 error = parse_nxm_entry(rule, f, p + 4, p + 4 + length / 2);
342 }
343 if (error) {
344 VLOG_DBG_RL(&rl, "bad nxm_entry with vendor=%"PRIu32", "
345 "field=%"PRIu32", hasmask=%"PRIu32", type=%"PRIu32" "
346 "(error %x)",
347 NXM_VENDOR(header), NXM_FIELD(header),
348 NXM_HASMASK(header), NXM_TYPE(header),
349 error);
350 return error;
351 }
352
353
354 p += 4 + length;
355 match_len -= 4 + length;
356 }
357
358 return match_len ? NXM_INVALID : 0;
359}
360\f
361/* nx_put_match() and helpers.
362 *
363 * 'put' functions whose names end in 'w' add a wildcarded field.
364 * 'put' functions whose names end in 'm' add a field that might be wildcarded.
365 * Other 'put' functions add exact-match fields.
366 */
367
368static void
369nxm_put_header(struct ofpbuf *b, uint32_t header)
370{
371 ovs_be32 n_header = htonl(header);
372 ofpbuf_put(b, &n_header, sizeof n_header);
373}
374
375static void
376nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
377{
378 nxm_put_header(b, header);
379 ofpbuf_put(b, &value, sizeof value);
380}
381
382static void
383nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
384{
385 nxm_put_header(b, header);
386 ofpbuf_put(b, &value, sizeof value);
387}
388
389static void
390nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
391{
392 nxm_put_header(b, header);
393 ofpbuf_put(b, &value, sizeof value);
394 ofpbuf_put(b, &mask, sizeof mask);
395}
396
397static void
398nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
399{
400 nxm_put_header(b, header);
401 ofpbuf_put(b, &value, sizeof value);
402}
403
404static void
405nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
406{
407 nxm_put_header(b, header);
408 ofpbuf_put(b, &value, sizeof value);
409 ofpbuf_put(b, &mask, sizeof mask);
410}
411
412static void
413nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
414{
415 switch (mask) {
416 case 0:
417 break;
418
419 case UINT32_MAX:
420 nxm_put_32(b, header, value);
421 break;
422
423 default:
424 nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
425 break;
426 }
427}
428
429static void
430nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
431{
432 nxm_put_header(b, header);
433 ofpbuf_put(b, &value, sizeof value);
434}
435
436
437static void
438nxm_put_eth(struct ofpbuf *b, uint32_t header,
439 const uint8_t value[ETH_ADDR_LEN])
440{
441 nxm_put_header(b, header);
442 ofpbuf_put(b, value, ETH_ADDR_LEN);
443}
444
445int
446nx_put_match(struct ofpbuf *b, const struct cls_rule *cr)
447{
448 const uint32_t wc = cr->wc.wildcards;
449 const struct flow *flow = &cr->flow;
450 const size_t start_len = b->size;
451 ovs_be16 vid, pcp;
452 int match_len;
453
454 /* Metadata. */
455 if (!(wc & OFPFW_IN_PORT)) {
456 uint16_t in_port = flow->in_port;
457 if (in_port == ODPP_LOCAL) {
458 in_port = OFPP_LOCAL;
459 }
460 nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
461 }
462
463 /* Ethernet. */
464 if (!(wc & OFPFW_DL_DST)) {
465 nxm_put_eth(b, NXM_OF_ETH_DST, flow->dl_dst);
466 }
467 if (!(wc & OFPFW_DL_SRC)) {
468 nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
469 }
470 if (!(wc & OFPFW_DL_TYPE)) {
471 nxm_put_16(b, NXM_OF_ETH_TYPE, flow->dl_type);
472 }
473
474 /* 802.1Q. */
475 vid = flow->dl_vlan & htons(VLAN_VID_MASK);
476 pcp = htons((flow->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
477 switch (wc & (OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP)) {
478 case OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP:
479 break;
480 case OFPFW_DL_VLAN:
481 nxm_put_16w(b, NXM_OF_VLAN_TCI_W, pcp | htons(VLAN_CFI),
482 htons(VLAN_PCP_MASK | VLAN_CFI));
483 break;
484 case OFPFW_DL_VLAN_PCP:
485 if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
486 nxm_put_16(b, NXM_OF_VLAN_TCI, 0);
487 } else {
488 nxm_put_16w(b, NXM_OF_VLAN_TCI_W, vid | htons(VLAN_CFI),
489 htons(VLAN_VID_MASK | VLAN_CFI));
490 }
491 break;
492 case 0:
493 if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
494 nxm_put_16(b, NXM_OF_VLAN_TCI, 0);
495 } else {
496 nxm_put_16(b, NXM_OF_VLAN_TCI, vid | pcp | htons(VLAN_CFI));
497 }
498 break;
499 }
500
501 if (!(wc & OFPFW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
502 /* IP. */
503 if (!(wc & OFPFW_NW_TOS)) {
504 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
505 }
506 nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
507 nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
508
509 if (!(wc & OFPFW_NW_PROTO)) {
510 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
511 switch (flow->nw_proto) {
512 /* TCP. */
513 case IP_TYPE_TCP:
514 if (!(wc & OFPFW_TP_SRC)) {
515 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
516 }
517 if (!(wc & OFPFW_TP_DST)) {
518 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
519 }
520 break;
521
522 /* UDP. */
523 case IP_TYPE_UDP:
524 if (!(wc & OFPFW_TP_SRC)) {
525 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
526 }
527 if (!(wc & OFPFW_TP_DST)) {
528 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
529 }
530 break;
531
532 /* ICMP. */
533 case IP_TYPE_ICMP:
534 if (!(wc & OFPFW_TP_SRC)) {
535 nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
536 }
537 if (!(wc & OFPFW_TP_DST)) {
538 nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
539 }
540 break;
541 }
542 }
543 } else if (!(wc & OFPFW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
544 /* ARP. */
545 if (!(wc & OFPFW_NW_PROTO)) {
546 nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
547 }
548 nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
549 nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
550 }
551
552 /* Tunnel ID. */
553 if (!(wc & NXFW_TUN_ID)) {
554 nxm_put_64(b, NXM_NX_TUN_ID, htonll(ntohl(flow->tun_id)));
555 }
556
557 match_len = b->size - start_len;
558 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
559 return match_len;
560}
561\f
562/* nx_match_to_string() and helpers. */
563
564char *
565nx_match_to_string(const uint8_t *p, unsigned int match_len)
566{
567 uint32_t header;
568 struct ds s;
569
570 if (!match_len) {
571 return xstrdup("<any>");
572 }
573
574 ds_init(&s);
575 while ((header = nx_entry_ok(p, match_len)) != 0) {
576 unsigned int length = NXM_LENGTH(header);
577 unsigned int value_len = nxm_field_bytes(header);
578 const uint8_t *value = p + 4;
579 const uint8_t *mask = value + value_len;
580 const struct nxm_field *f;
581 unsigned int i;
582
583 if (s.length) {
584 ds_put_cstr(&s, ", ");
585 }
586
587 f = nxm_field_lookup(header);
588 if (f) {
589 ds_put_cstr(&s, f->name);
590 } else {
591 ds_put_format(&s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
592 }
593
594 ds_put_char(&s, '(');
595
596 for (i = 0; i < value_len; i++) {
597 ds_put_format(&s, "%02x", value[i]);
598 }
599 if (NXM_HASMASK(header)) {
600 ds_put_char(&s, '/');
601 for (i = 0; i < value_len; i++) {
602 ds_put_format(&s, "%02x", mask[i]);
603 }
604 }
605 ds_put_char(&s, ')');
606
607 p += 4 + length;
608 match_len -= 4 + length;
609 }
610
611 if (match_len) {
612 if (s.length) {
613 ds_put_cstr(&s, ", ");
614 }
615
616 ds_put_format(&s, "<%u invalid bytes>", match_len);
617 }
618
619 return ds_steal_cstr(&s);
620}
621
622static const struct nxm_field *
623lookup_nxm_field(const char *name, int name_len)
624{
625 const struct nxm_field *f;
626
627 for (f = nxm_fields; f < &nxm_fields[ARRAY_SIZE(nxm_fields)]; f++) {
628 if (!strncmp(f->name, name, name_len) && f->name[name_len] == '\0') {
629 return f;
630 }
631 }
632
633 return NULL;
634}
635
636static const char *
637parse_hex_bytes(struct ofpbuf *b, const char *s, unsigned int n)
638{
639 while (n--) {
640 int low, high;
641 uint8_t byte;
642
643 s += strspn(s, " ");
644 low = hexit_value(*s);
645 high = low < 0 ? low : hexit_value(s[1]);
646 if (low < 0 || high < 0) {
647 ovs_fatal(0, "%.2s: hex digits expected", s);
648 }
649
650 byte = 16 * low + high;
651 ofpbuf_put(b, &byte, 1);
652 s += 2;
653 }
654 return s;
655}
656\f
657/* nx_match_from_string(). */
658
659int
660nx_match_from_string(const char *s, struct ofpbuf *b)
661{
662 const char *full_s = s;
663 const size_t start_len = b->size;
664 int match_len;
665
666 if (!strcmp(s, "<any>")) {
667 /* Ensure that 'b->data' isn't actually null. */
668 ofpbuf_prealloc_tailroom(b, 1);
669 return 0;
670 }
671
672 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
673 const struct nxm_field *f;
674 int name_len;
675
676 name_len = strcspn(s, "(");
677 if (s[name_len] != '(') {
678 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
679 }
680
681 f = lookup_nxm_field(s, name_len);
682 if (!f) {
683 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
684 }
685
686 s += name_len + 1;
687
688 nxm_put_header(b, f->header);
689 s = parse_hex_bytes(b, s, nxm_field_bytes(f->header));
690 if (NXM_HASMASK(f->header)) {
691 s += strspn(s, " ");
692 if (*s != '/') {
693 ovs_fatal(0, "%s: missing / in masked field %s",
694 full_s, f->name);
695 }
696 s = parse_hex_bytes(b, s + 1, nxm_field_bytes(f->header));
697 }
698
699 s += strspn(s, " ");
700 if (*s != ')') {
701 ovs_fatal(0, "%s: missing ) following field %s", full_s, f->name);
702 }
703 s++;
704 }
705
706 match_len = b->size - start_len;
707 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
708 return match_len;
709}