nx-match: Factor redundant code out of nx_put_match().
[sliver-openvswitch.git] / lib / nx-match.c
1 /*
2  * Copyright (c) 2010, 2011, 2012 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 <netinet/icmp6.h>
22
23 #include "classifier.h"
24 #include "dynamic-string.h"
25 #include "meta-flow.h"
26 #include "ofp-errors.h"
27 #include "ofp-util.h"
28 #include "ofpbuf.h"
29 #include "openflow/nicira-ext.h"
30 #include "packets.h"
31 #include "unaligned.h"
32 #include "util.h"
33 #include "vlog.h"
34
35 VLOG_DEFINE_THIS_MODULE(nx_match);
36
37 /* Rate limit for nx_match parse errors.  These always indicate a bug in the
38  * peer and so there's not much point in showing a lot of them. */
39 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
40
41 /* Returns the width of the data for a field with the given 'header', in
42  * bytes. */
43 int
44 nxm_field_bytes(uint32_t header)
45 {
46     unsigned int length = NXM_LENGTH(header);
47     return NXM_HASMASK(header) ? length / 2 : length;
48 }
49
50 /* Returns the width of the data for a field with the given 'header', in
51  * bits. */
52 int
53 nxm_field_bits(uint32_t header)
54 {
55     return nxm_field_bytes(header) * 8;
56 }
57 \f
58 /* nx_pull_match() and helpers. */
59
60 static uint32_t
61 nx_entry_ok(const void *p, unsigned int match_len)
62 {
63     unsigned int payload_len;
64     ovs_be32 header_be;
65     uint32_t header;
66
67     if (match_len < 4) {
68         if (match_len) {
69             VLOG_DBG_RL(&rl, "nx_match ends with partial nxm_header");
70         }
71         return 0;
72     }
73     memcpy(&header_be, p, 4);
74     header = ntohl(header_be);
75
76     payload_len = NXM_LENGTH(header);
77     if (!payload_len) {
78         VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
79                     "length 0", header);
80         return 0;
81     }
82     if (match_len < payload_len + 4) {
83         VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
84                     "%u bytes left in nx_match", payload_len + 4, match_len);
85         return 0;
86     }
87
88     return header;
89 }
90
91 static enum ofperr
92 nx_pull_match__(struct ofpbuf *b, unsigned int match_len, bool strict,
93                 uint16_t priority, struct cls_rule *rule,
94                 ovs_be64 *cookie, ovs_be64 *cookie_mask)
95 {
96     uint32_t header;
97     uint8_t *p;
98
99     assert((cookie != NULL) == (cookie_mask != NULL));
100
101     p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
102     if (!p) {
103         VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
104                     "multiple of 8, is longer than space in message (max "
105                     "length %zu)", match_len, b->size);
106         return OFPERR_OFPBRC_BAD_LEN;
107     }
108
109     cls_rule_init_catchall(rule, priority);
110     if (cookie) {
111         *cookie = *cookie_mask = htonll(0);
112     }
113     for (;
114          (header = nx_entry_ok(p, match_len)) != 0;
115          p += 4 + NXM_LENGTH(header), match_len -= 4 + NXM_LENGTH(header)) {
116         const struct mf_field *mf;
117         enum ofperr error;
118
119         mf = mf_from_nxm_header(header);
120         if (!mf) {
121             if (strict) {
122                 error = OFPERR_NXBRC_NXM_BAD_TYPE;
123             } else {
124                 continue;
125             }
126         } else if (!mf_are_prereqs_ok(mf, &rule->flow)) {
127             error = OFPERR_NXBRC_NXM_BAD_PREREQ;
128         } else if (!mf_is_all_wild(mf, &rule->wc)) {
129             error = OFPERR_NXBRC_NXM_DUP_TYPE;
130         } else {
131             unsigned int width = mf->n_bytes;
132             union mf_value value;
133
134             memcpy(&value, p + 4, width);
135             if (!mf_is_value_valid(mf, &value)) {
136                 error = OFPERR_NXBRC_NXM_BAD_VALUE;
137             } else if (!NXM_HASMASK(header)) {
138                 error = 0;
139                 mf_set_value(mf, &value, rule);
140             } else {
141                 union mf_value mask;
142
143                 memcpy(&mask, p + 4 + width, width);
144                 if (!mf_is_mask_valid(mf, &mask)) {
145                     error = OFPERR_NXBRC_NXM_BAD_MASK;
146                 } else {
147                     error = 0;
148                     mf_set(mf, &value, &mask, rule);
149                 }
150             }
151         }
152
153         /* Check if the match is for a cookie rather than a classifier rule. */
154         if ((header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W) && cookie) {
155             if (*cookie_mask) {
156                 error = OFPERR_NXBRC_NXM_DUP_TYPE;
157             } else {
158                 unsigned int width = sizeof *cookie;
159
160                 memcpy(cookie, p + 4, width);
161                 if (NXM_HASMASK(header)) {
162                     memcpy(cookie_mask, p + 4 + width, width);
163                 } else {
164                     *cookie_mask = htonll(UINT64_MAX);
165                 }
166                 error = 0;
167             }
168         }
169
170         if (error) {
171             VLOG_DBG_RL(&rl, "bad nxm_entry %#08"PRIx32" (vendor=%"PRIu32", "
172                         "field=%"PRIu32", hasmask=%"PRIu32", len=%"PRIu32"), "
173                         "(%s)", header,
174                         NXM_VENDOR(header), NXM_FIELD(header),
175                         NXM_HASMASK(header), NXM_LENGTH(header),
176                         ofperr_to_string(error));
177             return error;
178         }
179     }
180
181     return match_len ? OFPERR_NXBRC_NXM_INVALID : 0;
182 }
183
184 /* Parses the nx_match formatted match description in 'b' with length
185  * 'match_len'.  The results are stored in 'rule', which is initialized with
186  * 'priority'.  If 'cookie' and 'cookie_mask' contain valid pointers, then the
187  * cookie and mask will be stored in them if a "NXM_NX_COOKIE*" match is
188  * defined.  Otherwise, 0 is stored in both.
189  *
190  * Fails with an error when encountering unknown NXM headers.
191  *
192  * Returns 0 if successful, otherwise an OpenFlow error code. */
193 enum ofperr
194 nx_pull_match(struct ofpbuf *b, unsigned int match_len,
195               uint16_t priority, struct cls_rule *rule,
196               ovs_be64 *cookie, ovs_be64 *cookie_mask)
197 {
198     return nx_pull_match__(b, match_len, true, priority, rule, cookie,
199                            cookie_mask);
200 }
201
202 /* Behaves the same as nx_pull_match() with one exception.  Skips over unknown
203  * NXM headers instead of failing with an error when they are encountered. */
204 enum ofperr
205 nx_pull_match_loose(struct ofpbuf *b, unsigned int match_len,
206                     uint16_t priority, struct cls_rule *rule,
207                     ovs_be64 *cookie, ovs_be64 *cookie_mask)
208 {
209     return nx_pull_match__(b, match_len, false, priority, rule, cookie,
210                            cookie_mask);
211 }
212 \f
213 /* nx_put_match() and helpers.
214  *
215  * 'put' functions whose names end in 'w' add a wildcarded field.
216  * 'put' functions whose names end in 'm' add a field that might be wildcarded.
217  * Other 'put' functions add exact-match fields.
218  */
219
220 static void
221 nxm_put_header(struct ofpbuf *b, uint32_t header)
222 {
223     ovs_be32 n_header = htonl(header);
224     ofpbuf_put(b, &n_header, sizeof n_header);
225 }
226
227 static void
228 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
229 {
230     nxm_put_header(b, header);
231     ofpbuf_put(b, &value, sizeof value);
232 }
233
234 static void
235 nxm_put_8m(struct ofpbuf *b, uint32_t header, uint8_t value, uint8_t mask)
236 {
237     switch (mask) {
238     case 0:
239         break;
240
241     case UINT8_MAX:
242         nxm_put_8(b, header, value);
243         break;
244
245     default:
246         nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
247         ofpbuf_put(b, &value, sizeof value);
248         ofpbuf_put(b, &mask, sizeof mask);
249     }
250 }
251
252 static void
253 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
254 {
255     nxm_put_header(b, header);
256     ofpbuf_put(b, &value, sizeof value);
257 }
258
259 static void
260 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
261 {
262     nxm_put_header(b, header);
263     ofpbuf_put(b, &value, sizeof value);
264     ofpbuf_put(b, &mask, sizeof mask);
265 }
266
267 static void
268 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
269 {
270     switch (mask) {
271     case 0:
272         break;
273
274     case CONSTANT_HTONS(UINT16_MAX):
275         nxm_put_16(b, header, value);
276         break;
277
278     default:
279         nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
280         break;
281     }
282 }
283
284 static void
285 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
286 {
287     nxm_put_header(b, header);
288     ofpbuf_put(b, &value, sizeof value);
289 }
290
291 static void
292 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
293 {
294     nxm_put_header(b, header);
295     ofpbuf_put(b, &value, sizeof value);
296     ofpbuf_put(b, &mask, sizeof mask);
297 }
298
299 static void
300 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
301 {
302     switch (mask) {
303     case 0:
304         break;
305
306     case CONSTANT_HTONL(UINT32_MAX):
307         nxm_put_32(b, header, value);
308         break;
309
310     default:
311         nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
312         break;
313     }
314 }
315
316 static void
317 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
318 {
319     nxm_put_header(b, header);
320     ofpbuf_put(b, &value, sizeof value);
321 }
322
323 static void
324 nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
325 {
326     nxm_put_header(b, header);
327     ofpbuf_put(b, &value, sizeof value);
328     ofpbuf_put(b, &mask, sizeof mask);
329 }
330
331 static void
332 nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
333 {
334     switch (mask) {
335     case 0:
336         break;
337
338     case CONSTANT_HTONLL(UINT64_MAX):
339         nxm_put_64(b, header, value);
340         break;
341
342     default:
343         nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
344         break;
345     }
346 }
347
348 static void
349 nxm_put_eth(struct ofpbuf *b, uint32_t header,
350             const uint8_t value[ETH_ADDR_LEN])
351 {
352     nxm_put_header(b, header);
353     ofpbuf_put(b, value, ETH_ADDR_LEN);
354 }
355
356 static void
357 nxm_put_eth_dst(struct ofpbuf *b,
358                 flow_wildcards_t wc, const uint8_t value[ETH_ADDR_LEN])
359 {
360     switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
361     case FWW_DL_DST | FWW_ETH_MCAST:
362         break;
363     default:
364         nxm_put_header(b, NXM_OF_ETH_DST_W);
365         ofpbuf_put(b, value, ETH_ADDR_LEN);
366         ofpbuf_put(b, flow_wildcards_to_dl_dst_mask(wc), ETH_ADDR_LEN);
367         break;
368     case 0:
369         nxm_put_eth(b, NXM_OF_ETH_DST, value);
370         break;
371     }
372 }
373
374 static void
375 nxm_put_ipv6(struct ofpbuf *b, uint32_t header,
376              const struct in6_addr *value, const struct in6_addr *mask)
377 {
378     if (ipv6_mask_is_any(mask)) {
379         return;
380     } else if (ipv6_mask_is_exact(mask)) {
381         nxm_put_header(b, header);
382         ofpbuf_put(b, value, sizeof *value);
383     } else {
384         nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
385         ofpbuf_put(b, value, sizeof *value);
386         ofpbuf_put(b, mask, sizeof *mask);
387     }
388 }
389
390 static void
391 nxm_put_frag(struct ofpbuf *b, const struct cls_rule *cr)
392 {
393     uint8_t nw_frag = cr->flow.nw_frag;
394     uint8_t nw_frag_mask = cr->wc.nw_frag_mask;
395
396     switch (nw_frag_mask) {
397     case 0:
398         break;
399
400     case FLOW_NW_FRAG_MASK:
401         nxm_put_8(b, NXM_NX_IP_FRAG, nw_frag);
402         break;
403
404     default:
405         nxm_put_8m(b, NXM_NX_IP_FRAG, nw_frag,
406                    nw_frag_mask & FLOW_NW_FRAG_MASK);
407         break;
408     }
409 }
410
411 static void
412 nxm_put_ip(struct ofpbuf *b, const struct cls_rule *cr,
413            uint8_t icmp_proto, uint32_t icmp_type, uint32_t icmp_code)
414 {
415     const flow_wildcards_t wc = cr->wc.wildcards;
416     const struct flow *flow = &cr->flow;
417
418     nxm_put_frag(b, cr);
419
420     if (!(wc & FWW_NW_DSCP)) {
421         nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & IP_DSCP_MASK);
422     }
423
424     if (!(wc & FWW_NW_ECN)) {
425         nxm_put_8(b, NXM_NX_IP_ECN, flow->nw_tos & IP_ECN_MASK);
426     }
427
428     if (!(wc & FWW_NW_TTL)) {
429         nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl);
430     }
431
432     if (!(wc & FWW_NW_PROTO)) {
433         nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
434
435         if (flow->nw_proto == IPPROTO_TCP) {
436             if (!(wc & FWW_TP_SRC)) {
437                 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
438             }
439             if (!(wc & FWW_TP_DST)) {
440                 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
441             }
442         } else if (flow->nw_proto == IPPROTO_UDP) {
443             if (!(wc & FWW_TP_SRC)) {
444                 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
445             }
446             if (!(wc & FWW_TP_DST)) {
447                 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
448             }
449         } else if (flow->nw_proto == icmp_proto) {
450             if (!(wc & FWW_TP_SRC)) {
451                 nxm_put_8(b, icmp_type, ntohs(flow->tp_src));
452             }
453             if (!(wc & FWW_TP_DST)) {
454                 nxm_put_8(b, icmp_code, ntohs(flow->tp_dst));
455             }
456         }
457     }
458 }
459
460 /* Appends to 'b' the nx_match format that expresses 'cr' (except for
461  * 'cr->priority', because priority is not part of nx_match), plus enough
462  * zero bytes to pad the nx_match out to a multiple of 8.  For Flow Mod
463  * and Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be
464  * supplied.  Otherwise, 'cookie_mask' should be zero.
465  *
466  * This function can cause 'b''s data to be reallocated.
467  *
468  * Returns the number of bytes appended to 'b', excluding padding.
469  *
470  * If 'cr' is a catch-all rule that matches every packet, then this function
471  * appends nothing to 'b' and returns 0. */
472 int
473 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr,
474              ovs_be64 cookie, ovs_be64 cookie_mask)
475 {
476     const flow_wildcards_t wc = cr->wc.wildcards;
477     const struct flow *flow = &cr->flow;
478     const size_t start_len = b->size;
479     int match_len;
480     int i;
481
482     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
483
484     /* Metadata. */
485     if (!(wc & FWW_IN_PORT)) {
486         uint16_t in_port = flow->in_port;
487         nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
488     }
489
490     /* Ethernet. */
491     nxm_put_eth_dst(b, wc, flow->dl_dst);
492     if (!(wc & FWW_DL_SRC)) {
493         nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
494     }
495     if (!(wc & FWW_DL_TYPE)) {
496         nxm_put_16(b, NXM_OF_ETH_TYPE,
497                    ofputil_dl_type_to_openflow(flow->dl_type));
498     }
499
500     /* 802.1Q. */
501     nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, cr->wc.vlan_tci_mask);
502
503     /* L3. */
504     if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
505         /* IP. */
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         nxm_put_ip(b, cr, IPPROTO_ICMP, NXM_OF_ICMP_TYPE, NXM_OF_ICMP_CODE);
509     } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IPV6)) {
510         /* IPv6. */
511         nxm_put_ipv6(b, NXM_NX_IPV6_SRC, &flow->ipv6_src,
512                 &cr->wc.ipv6_src_mask);
513         nxm_put_ipv6(b, NXM_NX_IPV6_DST, &flow->ipv6_dst,
514                 &cr->wc.ipv6_dst_mask);
515         nxm_put_ip(b, cr,
516                    IPPROTO_ICMPV6, NXM_NX_ICMPV6_TYPE, NXM_NX_ICMPV6_CODE);
517
518         if (!(wc & FWW_IPV6_LABEL)) {
519             nxm_put_32(b, NXM_NX_IPV6_LABEL, flow->ipv6_label);
520         }
521
522         if (flow->nw_proto == IPPROTO_ICMPV6
523             && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
524                 flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
525             if (!(wc & FWW_ND_TARGET)) {
526                 nxm_put_ipv6(b, NXM_NX_ND_TARGET, &flow->nd_target,
527                              &in6addr_exact);
528             }
529             if (!(wc & FWW_ARP_SHA)
530                 && flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
531                 nxm_put_eth(b, NXM_NX_ND_SLL, flow->arp_sha);
532             }
533             if (!(wc & FWW_ARP_THA)
534                 && flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
535                 nxm_put_eth(b, NXM_NX_ND_TLL, flow->arp_tha);
536             }
537         }
538     } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
539         /* ARP. */
540         if (!(wc & FWW_NW_PROTO)) {
541             nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
542         }
543         nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
544         nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
545         if (!(wc & FWW_ARP_SHA)) {
546             nxm_put_eth(b, NXM_NX_ARP_SHA, flow->arp_sha);
547         }
548         if (!(wc & FWW_ARP_THA)) {
549             nxm_put_eth(b, NXM_NX_ARP_THA, flow->arp_tha);
550         }
551     }
552
553     /* Tunnel ID. */
554     nxm_put_64m(b, NXM_NX_TUN_ID, flow->tun_id, cr->wc.tun_id_mask);
555
556     /* Registers. */
557     for (i = 0; i < FLOW_N_REGS; i++) {
558         nxm_put_32m(b, NXM_NX_REG(i),
559                     htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
560     }
561
562     /* Cookie. */
563     nxm_put_64m(b, NXM_NX_COOKIE, cookie, cookie_mask);
564
565     match_len = b->size - start_len;
566     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
567     return match_len;
568 }
569 \f
570 /* nx_match_to_string() and helpers. */
571
572 static void format_nxm_field_name(struct ds *, uint32_t header);
573
574 char *
575 nx_match_to_string(const uint8_t *p, unsigned int match_len)
576 {
577     uint32_t header;
578     struct ds s;
579
580     if (!match_len) {
581         return xstrdup("<any>");
582     }
583
584     ds_init(&s);
585     while ((header = nx_entry_ok(p, match_len)) != 0) {
586         unsigned int length = NXM_LENGTH(header);
587         unsigned int value_len = nxm_field_bytes(header);
588         const uint8_t *value = p + 4;
589         const uint8_t *mask = value + value_len;
590         unsigned int i;
591
592         if (s.length) {
593             ds_put_cstr(&s, ", ");
594         }
595
596         format_nxm_field_name(&s, header);
597         ds_put_char(&s, '(');
598
599         for (i = 0; i < value_len; i++) {
600             ds_put_format(&s, "%02x", value[i]);
601         }
602         if (NXM_HASMASK(header)) {
603             ds_put_char(&s, '/');
604             for (i = 0; i < value_len; i++) {
605                 ds_put_format(&s, "%02x", mask[i]);
606             }
607         }
608         ds_put_char(&s, ')');
609
610         p += 4 + length;
611         match_len -= 4 + length;
612     }
613
614     if (match_len) {
615         if (s.length) {
616             ds_put_cstr(&s, ", ");
617         }
618
619         ds_put_format(&s, "<%u invalid bytes>", match_len);
620     }
621
622     return ds_steal_cstr(&s);
623 }
624
625 static void
626 format_nxm_field_name(struct ds *s, uint32_t header)
627 {
628     const struct mf_field *mf = mf_from_nxm_header(header);
629     if (mf) {
630         ds_put_cstr(s, mf->nxm_name);
631         if (NXM_HASMASK(header)) {
632             ds_put_cstr(s, "_W");
633         }
634     } else if (header == NXM_NX_COOKIE) {
635         ds_put_cstr(s, "NXM_NX_COOKIE");
636     } else if (header == NXM_NX_COOKIE_W) {
637         ds_put_cstr(s, "NXM_NX_COOKIE_W");
638     } else {
639         ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
640     }
641 }
642
643 static uint32_t
644 parse_nxm_field_name(const char *name, int name_len)
645 {
646     bool wild;
647     int i;
648
649     /* Check whether it's a field name. */
650     wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
651     if (wild) {
652         name_len -= 2;
653     }
654
655     for (i = 0; i < MFF_N_IDS; i++) {
656         const struct mf_field *mf = mf_from_id(i);
657
658         if (mf->nxm_name
659             && !strncmp(mf->nxm_name, name, name_len)
660             && mf->nxm_name[name_len] == '\0') {
661             if (!wild) {
662                 return mf->nxm_header;
663             } else if (mf->maskable != MFM_NONE) {
664                 return NXM_MAKE_WILD_HEADER(mf->nxm_header);
665             }
666         }
667     }
668
669     if (!strncmp("NXM_NX_COOKIE", name, name_len)
670                 && (name_len == strlen("NXM_NX_COOKIE"))) {
671         if (!wild) {
672             return NXM_NX_COOKIE;
673         } else {
674             return NXM_NX_COOKIE_W;
675         }
676     }
677
678     /* Check whether it's a 32-bit field header value as hex.
679      * (This isn't ordinarily useful except for testing error behavior.) */
680     if (name_len == 8) {
681         uint32_t header = hexits_value(name, name_len, NULL);
682         if (header != UINT_MAX) {
683             return header;
684         }
685     }
686
687     return 0;
688 }
689 \f
690 /* nx_match_from_string(). */
691
692 int
693 nx_match_from_string(const char *s, struct ofpbuf *b)
694 {
695     const char *full_s = s;
696     const size_t start_len = b->size;
697     int match_len;
698
699     if (!strcmp(s, "<any>")) {
700         /* Ensure that 'b->data' isn't actually null. */
701         ofpbuf_prealloc_tailroom(b, 1);
702         return 0;
703     }
704
705     for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
706         const char *name;
707         uint32_t header;
708         int name_len;
709         size_t n;
710
711         name = s;
712         name_len = strcspn(s, "(");
713         if (s[name_len] != '(') {
714             ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
715         }
716
717         header = parse_nxm_field_name(name, name_len);
718         if (!header) {
719             ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
720         }
721
722         s += name_len + 1;
723
724         nxm_put_header(b, header);
725         s = ofpbuf_put_hex(b, s, &n);
726         if (n != nxm_field_bytes(header)) {
727             ovs_fatal(0, "%.2s: hex digits expected", s);
728         }
729         if (NXM_HASMASK(header)) {
730             s += strspn(s, " ");
731             if (*s != '/') {
732                 ovs_fatal(0, "%s: missing / in masked field %.*s",
733                           full_s, name_len, name);
734             }
735             s = ofpbuf_put_hex(b, s + 1, &n);
736             if (n != nxm_field_bytes(header)) {
737                 ovs_fatal(0, "%.2s: hex digits expected", s);
738             }
739         }
740
741         s += strspn(s, " ");
742         if (*s != ')') {
743             ovs_fatal(0, "%s: missing ) following field %.*s",
744                       full_s, name_len, name);
745         }
746         s++;
747     }
748
749     match_len = b->size - start_len;
750     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
751     return match_len;
752 }
753 \f
754 void
755 nxm_parse_reg_move(struct nx_action_reg_move *move, const char *s)
756 {
757     const char *full_s = s;
758     struct mf_subfield src, dst;
759
760     s = mf_parse_subfield(&src, s);
761     if (strncmp(s, "->", 2)) {
762         ovs_fatal(0, "%s: missing `->' following source", full_s);
763     }
764     s += 2;
765     s = mf_parse_subfield(&dst, s);
766     if (*s != '\0') {
767         ovs_fatal(0, "%s: trailing garbage following destination", full_s);
768     }
769
770     if (src.n_bits != dst.n_bits) {
771         ovs_fatal(0, "%s: source field is %d bits wide but destination is "
772                   "%d bits wide", full_s, src.n_bits, dst.n_bits);
773     }
774
775     ofputil_init_NXAST_REG_MOVE(move);
776     move->n_bits = htons(src.n_bits);
777     move->src_ofs = htons(src.ofs);
778     move->dst_ofs = htons(dst.ofs);
779     move->src = htonl(src.field->nxm_header);
780     move->dst = htonl(dst.field->nxm_header);
781 }
782
783 void
784 nxm_parse_reg_load(struct nx_action_reg_load *load, const char *s)
785 {
786     const char *full_s = s;
787     struct mf_subfield dst;
788     uint64_t value;
789
790     value = strtoull(s, (char **) &s, 0);
791     if (strncmp(s, "->", 2)) {
792         ovs_fatal(0, "%s: missing `->' following value", full_s);
793     }
794     s += 2;
795     s = mf_parse_subfield(&dst, s);
796     if (*s != '\0') {
797         ovs_fatal(0, "%s: trailing garbage following destination", full_s);
798     }
799
800     if (dst.n_bits < 64 && (value >> dst.n_bits) != 0) {
801         ovs_fatal(0, "%s: value %"PRIu64" does not fit into %u bits",
802                   full_s, value, dst.n_bits);
803     }
804
805     ofputil_init_NXAST_REG_LOAD(load);
806     load->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
807     load->dst = htonl(dst.field->nxm_header);
808     load->value = htonll(value);
809 }
810 \f
811 /* nxm_format_reg_move(), nxm_format_reg_load(). */
812
813 void
814 nxm_format_reg_move(const struct nx_action_reg_move *move, struct ds *s)
815 {
816     struct mf_subfield src, dst;
817
818     nxm_decode_discrete(&src, move->src, move->src_ofs, move->n_bits);
819     nxm_decode_discrete(&dst, move->dst, move->dst_ofs, move->n_bits);
820
821     ds_put_format(s, "move:");
822     mf_format_subfield(&src, s);
823     ds_put_cstr(s, "->");
824     mf_format_subfield(&dst, s);
825 }
826
827 void
828 nxm_format_reg_load(const struct nx_action_reg_load *load, struct ds *s)
829 {
830     struct mf_subfield dst;
831
832     ds_put_format(s, "load:%#"PRIx64"->", ntohll(load->value));
833
834     nxm_decode(&dst, load->dst, load->ofs_nbits);
835     mf_format_subfield(&dst, s);
836 }
837 \f
838 /* nxm_check_reg_move(), nxm_check_reg_load(). */
839
840 enum ofperr
841 nxm_check_reg_move(const struct nx_action_reg_move *action,
842                    const struct flow *flow)
843 {
844     struct mf_subfield src;
845     struct mf_subfield dst;
846     int error;
847
848     nxm_decode_discrete(&src, action->src, action->src_ofs, action->n_bits);
849     error = mf_check_src(&src, flow);
850     if (error) {
851         return error;
852     }
853
854     nxm_decode_discrete(&dst, action->dst, action->dst_ofs, action->n_bits);
855     return mf_check_dst(&dst, flow);
856 }
857
858 enum ofperr
859 nxm_check_reg_load(const struct nx_action_reg_load *action,
860                    const struct flow *flow)
861 {
862     struct mf_subfield dst;
863     enum ofperr error;
864
865     nxm_decode(&dst, action->dst, action->ofs_nbits);
866     error = mf_check_dst(&dst, flow);
867     if (error) {
868         return error;
869     }
870
871     /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
872      * action->value. */
873     if (dst.n_bits < 64 && ntohll(action->value) >> dst.n_bits) {
874         return OFPERR_OFPBAC_BAD_ARGUMENT;
875     }
876
877     return 0;
878 }
879 \f
880 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
881
882 void
883 nxm_execute_reg_move(const struct nx_action_reg_move *action,
884                      struct flow *flow)
885 {
886     struct mf_subfield src, dst;
887     union mf_value src_value;
888     union mf_value dst_value;
889
890     nxm_decode_discrete(&src, action->src, action->src_ofs, action->n_bits);
891     nxm_decode_discrete(&dst, action->dst, action->dst_ofs, action->n_bits);
892
893     mf_get_value(dst.field, flow, &dst_value);
894     mf_get_value(src.field, flow, &src_value);
895     bitwise_copy(&src_value, src.field->n_bytes, src.ofs,
896                  &dst_value, dst.field->n_bytes, dst.ofs,
897                  src.n_bits);
898     mf_set_flow_value(dst.field, &dst_value, flow);
899 }
900
901 void
902 nxm_execute_reg_load(const struct nx_action_reg_load *action,
903                      struct flow *flow)
904 {
905     struct mf_subfield dst;
906
907     nxm_decode(&dst, action->dst, action->ofs_nbits);
908     mf_set_subfield_value(&dst, ntohll(action->value), flow);
909 }
910
911 /* Initializes 'sf->field' with the field corresponding to the given NXM
912  * 'header' and 'sf->ofs' and 'sf->n_bits' decoded from 'ofs_nbits' with
913  * nxm_decode_ofs() and nxm_decode_n_bits(), respectively.
914  *
915  * Afterward, 'sf' might be invalid in a few different ways:
916  *
917  *   - 'sf->field' will be NULL if 'header' is unknown.
918  *
919  *   - 'sf->ofs' and 'sf->n_bits' might exceed the width of sf->field.
920  *
921  * The caller should call mf_check_src() or mf_check_dst() to check for these
922  * problems. */
923 void
924 nxm_decode(struct mf_subfield *sf, ovs_be32 header, ovs_be16 ofs_nbits)
925 {
926     sf->field = mf_from_nxm_header(ntohl(header));
927     sf->ofs = nxm_decode_ofs(ofs_nbits);
928     sf->n_bits = nxm_decode_n_bits(ofs_nbits);
929 }
930
931 /* Initializes 'sf->field' with the field corresponding to the given NXM
932  * 'header' and 'sf->ofs' and 'sf->n_bits' from 'ofs' and 'n_bits',
933  * respectively.
934  *
935  * Afterward, 'sf' might be invalid in a few different ways:
936  *
937  *   - 'sf->field' will be NULL if 'header' is unknown.
938  *
939  *   - 'sf->ofs' and 'sf->n_bits' might exceed the width of sf->field.
940  *
941  * The caller should call mf_check_src() or mf_check_dst() to check for these
942  * problems. */
943 void
944 nxm_decode_discrete(struct mf_subfield *sf, ovs_be32 header,
945                     ovs_be16 ofs, ovs_be16 n_bits)
946 {
947     sf->field = mf_from_nxm_header(ntohl(header));
948     sf->ofs = ntohs(ofs);
949     sf->n_bits = ntohs(n_bits);
950 }