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