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