ofproto-dpif: Store relevant fields for wildcarding in facet.
[cascardo/ovs.git] / lib / nx-match.c
1 /*
2  * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc.
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-actions.h"
27 #include "ofp-errors.h"
28 #include "ofp-util.h"
29 #include "ofpbuf.h"
30 #include "openflow/nicira-ext.h"
31 #include "packets.h"
32 #include "unaligned.h"
33 #include "util.h"
34 #include "vlog.h"
35
36 VLOG_DEFINE_THIS_MODULE(nx_match);
37
38 /* Rate limit for nx_match parse errors.  These always indicate a bug in the
39  * peer and so there's not much point in showing a lot of them. */
40 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
41
42 /* Returns the width of the data for a field with the given 'header', in
43  * bytes. */
44 int
45 nxm_field_bytes(uint32_t header)
46 {
47     unsigned int length = NXM_LENGTH(header);
48     return NXM_HASMASK(header) ? length / 2 : length;
49 }
50
51 /* Returns the width of the data for a field with the given 'header', in
52  * bits. */
53 int
54 nxm_field_bits(uint32_t header)
55 {
56     return nxm_field_bytes(header) * 8;
57 }
58 \f
59 /* nx_pull_match() and helpers. */
60
61 static uint32_t
62 nx_entry_ok(const void *p, unsigned int match_len)
63 {
64     unsigned int payload_len;
65     ovs_be32 header_be;
66     uint32_t header;
67
68     if (match_len < 4) {
69         if (match_len) {
70             VLOG_DBG_RL(&rl, "nx_match ends with partial (%u-byte) nxm_header",
71                         match_len);
72         }
73         return 0;
74     }
75     memcpy(&header_be, p, 4);
76     header = ntohl(header_be);
77
78     payload_len = NXM_LENGTH(header);
79     if (!payload_len) {
80         VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
81                     "length 0", header);
82         return 0;
83     }
84     if (match_len < payload_len + 4) {
85         VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
86                     "%u bytes left in nx_match", payload_len + 4, match_len);
87         return 0;
88     }
89
90     return header;
91 }
92
93 /* Given NXM/OXM value 'value' and mask 'mask', each 'width' bytes long,
94  * checks for any 1-bit in the value where there is a 0-bit in the mask.  If it
95  * finds one, logs a warning. */
96 static void
97 check_mask_consistency(const uint8_t *p, const struct mf_field *mf)
98 {
99     unsigned int width = mf->n_bytes;
100     const uint8_t *value = p + 4;
101     const uint8_t *mask = p + 4 + width;
102     unsigned int i;
103
104     for (i = 0; i < width; i++) {
105         if (value[i] & ~mask[i]) {
106             if (!VLOG_DROP_WARN(&rl)) {
107                 char *s = nx_match_to_string(p, width * 2 + 4);
108                 VLOG_WARN_RL(&rl, "NXM/OXM entry %s has 1-bits in value for "
109                              "bits wildcarded by the mask.  (Future versions "
110                              "of OVS may report this as an OpenFlow error.)",
111                              s);
112                 break;
113             }
114         }
115     }
116 }
117
118 static enum ofperr
119 nx_pull_raw(const uint8_t *p, unsigned int match_len, bool strict,
120             struct match *match, ovs_be64 *cookie, ovs_be64 *cookie_mask)
121 {
122     uint32_t header;
123
124     ovs_assert((cookie != NULL) == (cookie_mask != NULL));
125
126     match_init_catchall(match);
127     if (cookie) {
128         *cookie = *cookie_mask = htonll(0);
129     }
130     if (!match_len) {
131         return 0;
132     }
133
134     for (;
135          (header = nx_entry_ok(p, match_len)) != 0;
136          p += 4 + NXM_LENGTH(header), match_len -= 4 + NXM_LENGTH(header)) {
137         const struct mf_field *mf;
138         enum ofperr error;
139
140         mf = mf_from_nxm_header(header);
141         if (!mf) {
142             if (strict) {
143                 error = OFPERR_OFPBMC_BAD_FIELD;
144             } else {
145                 continue;
146             }
147         } else if (!mf_are_prereqs_ok(mf, &match->flow)) {
148             error = OFPERR_OFPBMC_BAD_PREREQ;
149         } else if (!mf_is_all_wild(mf, &match->wc)) {
150             error = OFPERR_OFPBMC_DUP_FIELD;
151         } else if (header != OXM_OF_IN_PORT) {
152             unsigned int width = mf->n_bytes;
153             union mf_value value;
154
155             memcpy(&value, p + 4, width);
156             if (!mf_is_value_valid(mf, &value)) {
157                 error = OFPERR_OFPBMC_BAD_VALUE;
158             } else if (!NXM_HASMASK(header)) {
159                 error = 0;
160                 mf_set_value(mf, &value, match);
161             } else {
162                 union mf_value mask;
163
164                 memcpy(&mask, p + 4 + width, width);
165                 if (!mf_is_mask_valid(mf, &mask)) {
166                     error = OFPERR_OFPBMC_BAD_MASK;
167                 } else {
168                     error = 0;
169                     check_mask_consistency(p, mf);
170                     mf_set(mf, &value, &mask, match);
171                 }
172             }
173         } else {
174             /* Special case for 32bit ports when using OXM,
175              * ports are 16 bits wide otherwise. */
176             ovs_be32 port_of11;
177             uint16_t port;
178
179             memcpy(&port_of11, p + 4, sizeof port_of11);
180             error = ofputil_port_from_ofp11(port_of11, &port);
181             if (!error) {
182                 match_set_in_port(match, port);
183             }
184         }
185
186         /* Check if the match is for a cookie rather than a classifier rule. */
187         if ((header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W) && cookie) {
188             if (*cookie_mask) {
189                 error = OFPERR_OFPBMC_DUP_FIELD;
190             } else {
191                 unsigned int width = sizeof *cookie;
192
193                 memcpy(cookie, p + 4, width);
194                 if (NXM_HASMASK(header)) {
195                     memcpy(cookie_mask, p + 4 + width, width);
196                 } else {
197                     *cookie_mask = htonll(UINT64_MAX);
198                 }
199                 error = 0;
200             }
201         }
202
203         if (error) {
204             VLOG_DBG_RL(&rl, "bad nxm_entry %#08"PRIx32" (vendor=%"PRIu32", "
205                         "field=%"PRIu32", hasmask=%"PRIu32", len=%"PRIu32"), "
206                         "(%s)", header,
207                         NXM_VENDOR(header), NXM_FIELD(header),
208                         NXM_HASMASK(header), NXM_LENGTH(header),
209                         ofperr_to_string(error));
210             return error;
211         }
212     }
213
214     return match_len ? OFPERR_OFPBMC_BAD_LEN : 0;
215 }
216
217 static enum ofperr
218 nx_pull_match__(struct ofpbuf *b, unsigned int match_len, bool strict,
219                 struct match *match,
220                 ovs_be64 *cookie, ovs_be64 *cookie_mask)
221 {
222     uint8_t *p = NULL;
223
224     if (match_len) {
225         p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
226         if (!p) {
227             VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
228                         "multiple of 8, is longer than space in message (max "
229                         "length %zu)", match_len, b->size);
230             return OFPERR_OFPBMC_BAD_LEN;
231         }
232     }
233
234     return nx_pull_raw(p, match_len, strict, match, cookie, cookie_mask);
235 }
236
237 /* Parses the nx_match formatted match description in 'b' with length
238  * 'match_len'.  Stores the results in 'match'.  If 'cookie' and 'cookie_mask'
239  * are valid pointers, then stores the cookie and mask in them if 'b' contains
240  * a "NXM_NX_COOKIE*" match.  Otherwise, stores 0 in both.
241  *
242  * Fails with an error upon encountering an unknown NXM header.
243  *
244  * Returns 0 if successful, otherwise an OpenFlow error code. */
245 enum ofperr
246 nx_pull_match(struct ofpbuf *b, unsigned int match_len, struct match *match,
247               ovs_be64 *cookie, ovs_be64 *cookie_mask)
248 {
249     return nx_pull_match__(b, match_len, true, match, cookie, cookie_mask);
250 }
251
252 /* Behaves the same as nx_pull_match(), but skips over unknown NXM headers,
253  * instead of failing with an error. */
254 enum ofperr
255 nx_pull_match_loose(struct ofpbuf *b, unsigned int match_len,
256                     struct match *match,
257                     ovs_be64 *cookie, ovs_be64 *cookie_mask)
258 {
259     return nx_pull_match__(b, match_len, false, match, cookie, cookie_mask);
260 }
261
262 static enum ofperr
263 oxm_pull_match__(struct ofpbuf *b, bool strict, struct match *match)
264 {
265     struct ofp11_match_header *omh = b->data;
266     uint8_t *p;
267     uint16_t match_len;
268
269     if (b->size < sizeof *omh) {
270         return OFPERR_OFPBMC_BAD_LEN;
271     }
272
273     match_len = ntohs(omh->length);
274     if (match_len < sizeof *omh) {
275         return OFPERR_OFPBMC_BAD_LEN;
276     }
277
278     if (omh->type != htons(OFPMT_OXM)) {
279         return OFPERR_OFPBMC_BAD_TYPE;
280     }
281
282     p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
283     if (!p) {
284         VLOG_DBG_RL(&rl, "oxm length %u, rounded up to a "
285                     "multiple of 8, is longer than space in message (max "
286                     "length %zu)", match_len, b->size);
287         return OFPERR_OFPBMC_BAD_LEN;
288     }
289
290     return nx_pull_raw(p + sizeof *omh, match_len - sizeof *omh,
291                        strict, match, NULL, NULL);
292 }
293
294 /* Parses the oxm formatted match description preceeded by a struct ofp11_match
295  * in 'b' with length 'match_len'.  Stores the result in 'match'.
296  *
297  * Fails with an error when encountering unknown OXM headers.
298  *
299  * Returns 0 if successful, otherwise an OpenFlow error code. */
300 enum ofperr
301 oxm_pull_match(struct ofpbuf *b, struct match *match)
302 {
303     return oxm_pull_match__(b, true, match);
304 }
305
306 /* Behaves the same as oxm_pull_match() with one exception.  Skips over unknown
307  * PXM headers instead of failing with an error when they are encountered. */
308 enum ofperr
309 oxm_pull_match_loose(struct ofpbuf *b, struct match *match)
310 {
311     return oxm_pull_match__(b, false, match);
312 }
313 \f
314 /* nx_put_match() and helpers.
315  *
316  * 'put' functions whose names end in 'w' add a wildcarded field.
317  * 'put' functions whose names end in 'm' add a field that might be wildcarded.
318  * Other 'put' functions add exact-match fields.
319  */
320
321 static void
322 nxm_put_header(struct ofpbuf *b, uint32_t header)
323 {
324     ovs_be32 n_header = htonl(header);
325     ofpbuf_put(b, &n_header, sizeof n_header);
326 }
327
328 static void
329 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
330 {
331     nxm_put_header(b, header);
332     ofpbuf_put(b, &value, sizeof value);
333 }
334
335 static void
336 nxm_put_8m(struct ofpbuf *b, uint32_t header, uint8_t value, uint8_t mask)
337 {
338     switch (mask) {
339     case 0:
340         break;
341
342     case UINT8_MAX:
343         nxm_put_8(b, header, value);
344         break;
345
346     default:
347         nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
348         ofpbuf_put(b, &value, sizeof value);
349         ofpbuf_put(b, &mask, sizeof mask);
350     }
351 }
352
353 static void
354 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
355 {
356     nxm_put_header(b, header);
357     ofpbuf_put(b, &value, sizeof value);
358 }
359
360 static void
361 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
362 {
363     nxm_put_header(b, header);
364     ofpbuf_put(b, &value, sizeof value);
365     ofpbuf_put(b, &mask, sizeof mask);
366 }
367
368 static void
369 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
370 {
371     switch (mask) {
372     case 0:
373         break;
374
375     case CONSTANT_HTONS(UINT16_MAX):
376         nxm_put_16(b, header, value);
377         break;
378
379     default:
380         nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
381         break;
382     }
383 }
384
385 static void
386 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
387 {
388     nxm_put_header(b, header);
389     ofpbuf_put(b, &value, sizeof value);
390 }
391
392 static void
393 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
394 {
395     nxm_put_header(b, header);
396     ofpbuf_put(b, &value, sizeof value);
397     ofpbuf_put(b, &mask, sizeof mask);
398 }
399
400 static void
401 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
402 {
403     switch (mask) {
404     case 0:
405         break;
406
407     case CONSTANT_HTONL(UINT32_MAX):
408         nxm_put_32(b, header, value);
409         break;
410
411     default:
412         nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
413         break;
414     }
415 }
416
417 static void
418 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
419 {
420     nxm_put_header(b, header);
421     ofpbuf_put(b, &value, sizeof value);
422 }
423
424 static void
425 nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
426 {
427     nxm_put_header(b, header);
428     ofpbuf_put(b, &value, sizeof value);
429     ofpbuf_put(b, &mask, sizeof mask);
430 }
431
432 static void
433 nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
434 {
435     switch (mask) {
436     case 0:
437         break;
438
439     case CONSTANT_HTONLL(UINT64_MAX):
440         nxm_put_64(b, header, value);
441         break;
442
443     default:
444         nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
445         break;
446     }
447 }
448
449 static void
450 nxm_put_eth(struct ofpbuf *b, uint32_t header,
451             const uint8_t value[ETH_ADDR_LEN])
452 {
453     nxm_put_header(b, header);
454     ofpbuf_put(b, value, ETH_ADDR_LEN);
455 }
456
457 static void
458 nxm_put_eth_masked(struct ofpbuf *b, uint32_t header,
459                    const uint8_t value[ETH_ADDR_LEN],
460                    const uint8_t mask[ETH_ADDR_LEN])
461 {
462     if (!eth_addr_is_zero(mask)) {
463         if (eth_mask_is_exact(mask)) {
464             nxm_put_eth(b, header, value);
465         } else {
466             nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
467             ofpbuf_put(b, value, ETH_ADDR_LEN);
468             ofpbuf_put(b, mask, ETH_ADDR_LEN);
469         }
470     }
471 }
472
473 static void
474 nxm_put_ipv6(struct ofpbuf *b, uint32_t header,
475              const struct in6_addr *value, const struct in6_addr *mask)
476 {
477     if (ipv6_mask_is_any(mask)) {
478         return;
479     } else if (ipv6_mask_is_exact(mask)) {
480         nxm_put_header(b, header);
481         ofpbuf_put(b, value, sizeof *value);
482     } else {
483         nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
484         ofpbuf_put(b, value, sizeof *value);
485         ofpbuf_put(b, mask, sizeof *mask);
486     }
487 }
488
489 static void
490 nxm_put_frag(struct ofpbuf *b, const struct match *match)
491 {
492     uint8_t nw_frag = match->flow.nw_frag;
493     uint8_t nw_frag_mask = match->wc.masks.nw_frag;
494
495     switch (nw_frag_mask) {
496     case 0:
497         break;
498
499     case FLOW_NW_FRAG_MASK:
500         nxm_put_8(b, NXM_NX_IP_FRAG, nw_frag);
501         break;
502
503     default:
504         nxm_put_8m(b, NXM_NX_IP_FRAG, nw_frag,
505                    nw_frag_mask & FLOW_NW_FRAG_MASK);
506         break;
507     }
508 }
509
510 static void
511 nxm_put_ip(struct ofpbuf *b, const struct match *match,
512            uint8_t icmp_proto, uint32_t icmp_type, uint32_t icmp_code,
513            bool oxm)
514 {
515     const struct flow *flow = &match->flow;
516
517     nxm_put_frag(b, match);
518
519     if (match->wc.masks.nw_tos & IP_DSCP_MASK) {
520         if (oxm) {
521             nxm_put_8(b, OXM_OF_IP_DSCP, flow->nw_tos >> 2);
522         } else {
523             nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & IP_DSCP_MASK);
524         }
525     }
526
527     if (match->wc.masks.nw_tos & IP_ECN_MASK) {
528         nxm_put_8(b, oxm ? OXM_OF_IP_ECN : NXM_NX_IP_ECN,
529                   flow->nw_tos & IP_ECN_MASK);
530     }
531
532     if (!oxm && match->wc.masks.nw_ttl) {
533         nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl);
534     }
535
536     if (match->wc.masks.nw_proto) {
537         nxm_put_8(b, oxm ? OXM_OF_IP_PROTO : NXM_OF_IP_PROTO, flow->nw_proto);
538
539         if (flow->nw_proto == IPPROTO_TCP) {
540             nxm_put_16m(b, oxm ? OXM_OF_TCP_SRC : NXM_OF_TCP_SRC,
541                         flow->tp_src, match->wc.masks.tp_src);
542             nxm_put_16m(b, oxm ? OXM_OF_TCP_DST : NXM_OF_TCP_DST,
543                         flow->tp_dst, match->wc.masks.tp_dst);
544         } else if (flow->nw_proto == IPPROTO_UDP) {
545             nxm_put_16m(b, oxm ? OXM_OF_UDP_SRC : NXM_OF_UDP_SRC,
546                         flow->tp_src, match->wc.masks.tp_src);
547             nxm_put_16m(b, oxm ? OXM_OF_UDP_DST : NXM_OF_UDP_DST,
548                         flow->tp_dst, match->wc.masks.tp_dst);
549         } else if (flow->nw_proto == icmp_proto) {
550             if (match->wc.masks.tp_src) {
551                 nxm_put_8(b, icmp_type, ntohs(flow->tp_src));
552             }
553             if (match->wc.masks.tp_dst) {
554                 nxm_put_8(b, icmp_code, ntohs(flow->tp_dst));
555             }
556         }
557     }
558 }
559
560 /* Appends to 'b' the nx_match format that expresses 'match'.  For Flow Mod and
561  * Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
562  * Otherwise, 'cookie_mask' should be zero.
563  *
564  * This function can cause 'b''s data to be reallocated.
565  *
566  * Returns the number of bytes appended to 'b', excluding padding.
567  *
568  * If 'match' is a catch-all rule that matches every packet, then this function
569  * appends nothing to 'b' and returns 0. */
570 static int
571 nx_put_raw(struct ofpbuf *b, bool oxm, const struct match *match,
572            ovs_be64 cookie, ovs_be64 cookie_mask)
573 {
574     const struct flow *flow = &match->flow;
575     const size_t start_len = b->size;
576     int match_len;
577     int i;
578
579     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 20);
580
581     /* Metadata. */
582     if (match->wc.masks.in_port) {
583         uint16_t in_port = flow->in_port;
584         if (oxm) {
585             nxm_put_32(b, OXM_OF_IN_PORT, ofputil_port_to_ofp11(in_port));
586         } else {
587             nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
588         }
589     }
590
591     /* Ethernet. */
592     nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_SRC : NXM_OF_ETH_SRC,
593                        flow->dl_src, match->wc.masks.dl_src);
594     nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_DST : NXM_OF_ETH_DST,
595                        flow->dl_dst, match->wc.masks.dl_dst);
596     nxm_put_16m(b, oxm ? OXM_OF_ETH_TYPE : NXM_OF_ETH_TYPE,
597                 ofputil_dl_type_to_openflow(flow->dl_type),
598                 match->wc.masks.dl_type);
599
600     /* 802.1Q. */
601     if (oxm) {
602         ovs_be16 VID_CFI_MASK = htons(VLAN_VID_MASK | VLAN_CFI);
603         ovs_be16 vid = flow->vlan_tci & VID_CFI_MASK;
604         ovs_be16 mask = match->wc.masks.vlan_tci & VID_CFI_MASK;
605
606         if (mask == htons(VLAN_VID_MASK | VLAN_CFI)) {
607             nxm_put_16(b, OXM_OF_VLAN_VID, vid);
608         } else if (mask) {
609             nxm_put_16m(b, OXM_OF_VLAN_VID, vid, mask);
610         }
611
612         if (vid && vlan_tci_to_pcp(match->wc.masks.vlan_tci)) {
613             nxm_put_8(b, OXM_OF_VLAN_PCP, vlan_tci_to_pcp(flow->vlan_tci));
614         }
615
616     } else {
617         nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci,
618                     match->wc.masks.vlan_tci);
619     }
620
621     /* MPLS. */
622     if (eth_type_mpls(flow->dl_type)) {
623         if (match->wc.masks.mpls_lse & htonl(MPLS_TC_MASK)) {
624             nxm_put_8(b, OXM_OF_MPLS_TC, mpls_lse_to_tc(flow->mpls_lse));
625         }
626
627         if (match->wc.masks.mpls_lse & htonl(MPLS_BOS_MASK)) {
628             nxm_put_8(b, OXM_OF_MPLS_BOS, mpls_lse_to_bos(flow->mpls_lse));
629         }
630
631         if (match->wc.masks.mpls_lse & htonl(MPLS_LABEL_MASK)) {
632             nxm_put_32(b, OXM_OF_MPLS_LABEL,
633                        htonl(mpls_lse_to_label(flow->mpls_lse)));
634         }
635     }
636
637     /* L3. */
638     if (flow->dl_type == htons(ETH_TYPE_IP)) {
639         /* IP. */
640         nxm_put_32m(b, oxm ? OXM_OF_IPV4_SRC : NXM_OF_IP_SRC,
641                     flow->nw_src, match->wc.masks.nw_src);
642         nxm_put_32m(b, oxm ? OXM_OF_IPV4_DST : NXM_OF_IP_DST,
643                     flow->nw_dst, match->wc.masks.nw_dst);
644         nxm_put_ip(b, match, IPPROTO_ICMP,
645                    oxm ? OXM_OF_ICMPV4_TYPE : NXM_OF_ICMP_TYPE,
646                    oxm ? OXM_OF_ICMPV4_CODE : NXM_OF_ICMP_CODE, oxm);
647     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
648         /* IPv6. */
649         nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_SRC : NXM_NX_IPV6_SRC,
650                      &flow->ipv6_src, &match->wc.masks.ipv6_src);
651         nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_DST : NXM_NX_IPV6_DST,
652                      &flow->ipv6_dst, &match->wc.masks.ipv6_dst);
653         nxm_put_ip(b, match, IPPROTO_ICMPV6,
654                    oxm ? OXM_OF_ICMPV6_TYPE : NXM_NX_ICMPV6_TYPE,
655                    oxm ? OXM_OF_ICMPV6_CODE : NXM_NX_ICMPV6_CODE, oxm);
656
657         nxm_put_32m(b, oxm ? OXM_OF_IPV6_FLABEL : NXM_NX_IPV6_LABEL,
658                     flow->ipv6_label, match->wc.masks.ipv6_label);
659
660         if (flow->nw_proto == IPPROTO_ICMPV6
661             && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
662                 flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
663             nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_ND_TARGET : NXM_NX_ND_TARGET,
664                          &flow->nd_target, &match->wc.masks.nd_target);
665             if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
666                 nxm_put_eth_masked(b, oxm ? OXM_OF_IPV6_ND_SLL : NXM_NX_ND_SLL,
667                                    flow->arp_sha, match->wc.masks.arp_sha);
668             }
669             if (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
670                 nxm_put_eth_masked(b, oxm ? OXM_OF_IPV6_ND_TLL : NXM_NX_ND_TLL,
671                                    flow->arp_tha, match->wc.masks.arp_tha);
672             }
673         }
674     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
675                flow->dl_type == htons(ETH_TYPE_RARP)) {
676         /* ARP. */
677         if (match->wc.masks.nw_proto) {
678             nxm_put_16(b, oxm ? OXM_OF_ARP_OP : NXM_OF_ARP_OP,
679                        htons(flow->nw_proto));
680         }
681         nxm_put_32m(b, oxm ? OXM_OF_ARP_SPA : NXM_OF_ARP_SPA,
682                     flow->nw_src, match->wc.masks.nw_src);
683         nxm_put_32m(b, oxm ? OXM_OF_ARP_TPA : NXM_OF_ARP_TPA,
684                     flow->nw_dst, match->wc.masks.nw_dst);
685         nxm_put_eth_masked(b, oxm ? OXM_OF_ARP_SHA : NXM_NX_ARP_SHA,
686                            flow->arp_sha, match->wc.masks.arp_sha);
687         nxm_put_eth_masked(b, oxm ? OXM_OF_ARP_THA : NXM_NX_ARP_THA,
688                            flow->arp_tha, match->wc.masks.arp_tha);
689     }
690
691     /* Tunnel ID. */
692     nxm_put_64m(b, oxm ? OXM_OF_TUNNEL_ID : NXM_NX_TUN_ID,
693                 flow->tunnel.tun_id, match->wc.masks.tunnel.tun_id);
694
695     /* Registers. */
696     for (i = 0; i < FLOW_N_REGS; i++) {
697         nxm_put_32m(b, NXM_NX_REG(i),
698                     htonl(flow->regs[i]), htonl(match->wc.masks.regs[i]));
699     }
700
701     /* OpenFlow 1.1+ Metadata. */
702     nxm_put_64m(b, OXM_OF_METADATA, flow->metadata, match->wc.masks.metadata);
703
704     /* Cookie. */
705     nxm_put_64m(b, NXM_NX_COOKIE, cookie, cookie_mask);
706
707     match_len = b->size - start_len;
708     return match_len;
709 }
710
711 /* Appends to 'b' the nx_match format that expresses 'match', plus enough zero
712  * bytes to pad the nx_match out to a multiple of 8.  For Flow Mod and Flow
713  * Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
714  * Otherwise, 'cookie_mask' should be zero.
715  *
716  * This function can cause 'b''s data to be reallocated.
717  *
718  * Returns the number of bytes appended to 'b', excluding padding.  The return
719  * value can be zero if it appended nothing at all to 'b' (which happens if
720  * 'cr' is a catch-all rule that matches every packet). */
721 int
722 nx_put_match(struct ofpbuf *b, const struct match *match,
723              ovs_be64 cookie, ovs_be64 cookie_mask)
724 {
725     int match_len = nx_put_raw(b, false, match, cookie, cookie_mask);
726
727     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
728     return match_len;
729 }
730
731
732 /* Appends to 'b' an struct ofp11_match_header followed by the oxm format that
733  * expresses 'cr', plus enough zero bytes to pad the data appended out to a
734  * multiple of 8.
735  *
736  * This function can cause 'b''s data to be reallocated.
737  *
738  * Returns the number of bytes appended to 'b', excluding the padding.  Never
739  * returns zero. */
740 int
741 oxm_put_match(struct ofpbuf *b, const struct match *match)
742 {
743     int match_len;
744     struct ofp11_match_header *omh;
745     size_t start_len = b->size;
746     ovs_be64 cookie = htonll(0), cookie_mask = htonll(0);
747
748     ofpbuf_put_uninit(b, sizeof *omh);
749     match_len = nx_put_raw(b, true, match, cookie, cookie_mask) + sizeof *omh;
750     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
751
752     omh = (struct ofp11_match_header *)((char *)b->data + start_len);
753     omh->type = htons(OFPMT_OXM);
754     omh->length = htons(match_len);
755
756     return match_len;
757 }
758 \f
759 /* nx_match_to_string() and helpers. */
760
761 static void format_nxm_field_name(struct ds *, uint32_t header);
762
763 char *
764 nx_match_to_string(const uint8_t *p, unsigned int match_len)
765 {
766     uint32_t header;
767     struct ds s;
768
769     if (!match_len) {
770         return xstrdup("<any>");
771     }
772
773     ds_init(&s);
774     while ((header = nx_entry_ok(p, match_len)) != 0) {
775         unsigned int length = NXM_LENGTH(header);
776         unsigned int value_len = nxm_field_bytes(header);
777         const uint8_t *value = p + 4;
778         const uint8_t *mask = value + value_len;
779         unsigned int i;
780
781         if (s.length) {
782             ds_put_cstr(&s, ", ");
783         }
784
785         format_nxm_field_name(&s, header);
786         ds_put_char(&s, '(');
787
788         for (i = 0; i < value_len; i++) {
789             ds_put_format(&s, "%02x", value[i]);
790         }
791         if (NXM_HASMASK(header)) {
792             ds_put_char(&s, '/');
793             for (i = 0; i < value_len; i++) {
794                 ds_put_format(&s, "%02x", mask[i]);
795             }
796         }
797         ds_put_char(&s, ')');
798
799         p += 4 + length;
800         match_len -= 4 + length;
801     }
802
803     if (match_len) {
804         if (s.length) {
805             ds_put_cstr(&s, ", ");
806         }
807
808         ds_put_format(&s, "<%u invalid bytes>", match_len);
809     }
810
811     return ds_steal_cstr(&s);
812 }
813
814 char *
815 oxm_match_to_string(const uint8_t *p, unsigned int match_len)
816 {
817     const struct ofp11_match_header *omh = (struct ofp11_match_header *)p;
818     uint16_t match_len_;
819     struct ds s;
820
821     ds_init(&s);
822
823     if (match_len < sizeof *omh) {
824         ds_put_format(&s, "<match too short: %u>", match_len);
825         goto err;
826     }
827
828     if (omh->type != htons(OFPMT_OXM)) {
829         ds_put_format(&s, "<bad match type field: %u>", ntohs(omh->type));
830         goto err;
831     }
832
833     match_len_ = ntohs(omh->length);
834     if (match_len_ < sizeof *omh) {
835         ds_put_format(&s, "<match length field too short: %u>", match_len_);
836         goto err;
837     }
838
839     if (match_len_ != match_len) {
840         ds_put_format(&s, "<match length field incorrect: %u != %u>",
841                       match_len_, match_len);
842         goto err;
843     }
844
845     return nx_match_to_string(p + sizeof *omh, match_len - sizeof *omh);
846
847 err:
848     return ds_steal_cstr(&s);
849 }
850
851 static void
852 format_nxm_field_name(struct ds *s, uint32_t header)
853 {
854     const struct mf_field *mf = mf_from_nxm_header(header);
855     if (mf) {
856         ds_put_cstr(s, IS_OXM_HEADER(header) ? mf->oxm_name : mf->nxm_name);
857         if (NXM_HASMASK(header)) {
858             ds_put_cstr(s, "_W");
859         }
860     } else if (header == NXM_NX_COOKIE) {
861         ds_put_cstr(s, "NXM_NX_COOKIE");
862     } else if (header == NXM_NX_COOKIE_W) {
863         ds_put_cstr(s, "NXM_NX_COOKIE_W");
864     } else {
865         ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
866     }
867 }
868
869 static uint32_t
870 parse_nxm_field_name(const char *name, int name_len)
871 {
872     bool wild;
873     int i;
874
875     /* Check whether it's a field name. */
876     wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
877     if (wild) {
878         name_len -= 2;
879     }
880
881     for (i = 0; i < MFF_N_IDS; i++) {
882         const struct mf_field *mf = mf_from_id(i);
883         uint32_t header;
884
885         if (mf->nxm_name &&
886             !strncmp(mf->nxm_name, name, name_len) &&
887             mf->nxm_name[name_len] == '\0') {
888             header = mf->nxm_header;
889         } else if (mf->oxm_name &&
890                    !strncmp(mf->oxm_name, name, name_len) &&
891                    mf->oxm_name[name_len] == '\0') {
892             header = mf->oxm_header;
893         } else {
894             continue;
895         }
896
897         if (!wild) {
898             return header;
899         } else if (mf->maskable != MFM_NONE) {
900             return NXM_MAKE_WILD_HEADER(header);
901         }
902     }
903
904     if (!strncmp("NXM_NX_COOKIE", name, name_len) &&
905         (name_len == strlen("NXM_NX_COOKIE"))) {
906         if (!wild) {
907             return NXM_NX_COOKIE;
908         } else {
909             return NXM_NX_COOKIE_W;
910         }
911     }
912
913     /* Check whether it's a 32-bit field header value as hex.
914      * (This isn't ordinarily useful except for testing error behavior.) */
915     if (name_len == 8) {
916         uint32_t header = hexits_value(name, name_len, NULL);
917         if (header != UINT_MAX) {
918             return header;
919         }
920     }
921
922     return 0;
923 }
924 \f
925 /* nx_match_from_string(). */
926
927 static int
928 nx_match_from_string_raw(const char *s, struct ofpbuf *b)
929 {
930     const char *full_s = s;
931     const size_t start_len = b->size;
932
933     if (!strcmp(s, "<any>")) {
934         /* Ensure that 'b->data' isn't actually null. */
935         ofpbuf_prealloc_tailroom(b, 1);
936         return 0;
937     }
938
939     for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
940         const char *name;
941         uint32_t header;
942         int name_len;
943         size_t n;
944
945         name = s;
946         name_len = strcspn(s, "(");
947         if (s[name_len] != '(') {
948             ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
949         }
950
951         header = parse_nxm_field_name(name, name_len);
952         if (!header) {
953             ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
954         }
955
956         s += name_len + 1;
957
958         nxm_put_header(b, header);
959         s = ofpbuf_put_hex(b, s, &n);
960         if (n != nxm_field_bytes(header)) {
961             ovs_fatal(0, "%.2s: hex digits expected", s);
962         }
963         if (NXM_HASMASK(header)) {
964             s += strspn(s, " ");
965             if (*s != '/') {
966                 ovs_fatal(0, "%s: missing / in masked field %.*s",
967                           full_s, name_len, name);
968             }
969             s = ofpbuf_put_hex(b, s + 1, &n);
970             if (n != nxm_field_bytes(header)) {
971                 ovs_fatal(0, "%.2s: hex digits expected", s);
972             }
973         }
974
975         s += strspn(s, " ");
976         if (*s != ')') {
977             ovs_fatal(0, "%s: missing ) following field %.*s",
978                       full_s, name_len, name);
979         }
980         s++;
981     }
982
983     return b->size - start_len;
984 }
985
986 int
987 nx_match_from_string(const char *s, struct ofpbuf *b)
988 {
989     int match_len = nx_match_from_string_raw(s, b);
990     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
991     return match_len;
992 }
993
994 int
995 oxm_match_from_string(const char *s, struct ofpbuf *b)
996 {
997     int match_len;
998     struct ofp11_match_header *omh;
999     size_t start_len = b->size;
1000
1001     ofpbuf_put_uninit(b, sizeof *omh);
1002     match_len = nx_match_from_string_raw(s, b) + sizeof *omh;
1003     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
1004
1005     omh = (struct ofp11_match_header *)((char *)b->data + start_len);
1006     omh->type = htons(OFPMT_OXM);
1007     omh->length = htons(match_len);
1008
1009     return match_len;
1010 }
1011 \f
1012 void
1013 nxm_parse_reg_move(struct ofpact_reg_move *move, const char *s)
1014 {
1015     const char *full_s = s;
1016
1017     s = mf_parse_subfield(&move->src, s);
1018     if (strncmp(s, "->", 2)) {
1019         ovs_fatal(0, "%s: missing `->' following source", full_s);
1020     }
1021     s += 2;
1022     s = mf_parse_subfield(&move->dst, s);
1023     if (*s != '\0') {
1024         ovs_fatal(0, "%s: trailing garbage following destination", full_s);
1025     }
1026
1027     if (move->src.n_bits != move->dst.n_bits) {
1028         ovs_fatal(0, "%s: source field is %d bits wide but destination is "
1029                   "%d bits wide", full_s,
1030                   move->src.n_bits, move->dst.n_bits);
1031     }
1032 }
1033
1034 void
1035 nxm_parse_reg_load(struct ofpact_reg_load *load, const char *s)
1036 {
1037     const char *full_s = s;
1038     uint64_t value = strtoull(s, (char **) &s, 0);
1039
1040     if (strncmp(s, "->", 2)) {
1041         ovs_fatal(0, "%s: missing `->' following value", full_s);
1042     }
1043     s += 2;
1044     s = mf_parse_subfield(&load->dst, s);
1045     if (*s != '\0') {
1046         ovs_fatal(0, "%s: trailing garbage following destination", full_s);
1047     }
1048
1049     if (load->dst.n_bits < 64 && (value >> load->dst.n_bits) != 0) {
1050         ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
1051                   full_s, value, load->dst.n_bits);
1052     }
1053
1054     load->subvalue.be64[0] = htonll(0);
1055     load->subvalue.be64[1] = htonll(value);
1056 }
1057 \f
1058 /* nxm_format_reg_move(), nxm_format_reg_load(). */
1059
1060 void
1061 nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s)
1062 {
1063     ds_put_format(s, "move:");
1064     mf_format_subfield(&move->src, s);
1065     ds_put_cstr(s, "->");
1066     mf_format_subfield(&move->dst, s);
1067 }
1068
1069 static void
1070 set_field_format(const struct ofpact_reg_load *load, struct ds *s)
1071 {
1072     const struct mf_field *mf = load->dst.field;
1073     union mf_value value;
1074
1075     ovs_assert(load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD);
1076     ds_put_format(s, "set_field:");
1077     memset(&value, 0, sizeof value);
1078     bitwise_copy(&load->subvalue, sizeof load->subvalue, 0,
1079                  &value, mf->n_bytes, 0, load->dst.n_bits);
1080     mf_format(mf, &value, NULL, s);
1081     ds_put_format(s, "->%s", mf->name);
1082 }
1083
1084 static void
1085 load_format(const struct ofpact_reg_load *load, struct ds *s)
1086 {
1087     ds_put_cstr(s, "load:");
1088     mf_format_subvalue(&load->subvalue, s);
1089     ds_put_cstr(s, "->");
1090     mf_format_subfield(&load->dst, s);
1091 }
1092
1093 void
1094 nxm_format_reg_load(const struct ofpact_reg_load *load, struct ds *s)
1095 {
1096     if (load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD) {
1097         set_field_format(load, s);
1098     } else {
1099         load_format(load, s);
1100     }
1101 }
1102 \f
1103 enum ofperr
1104 nxm_reg_move_from_openflow(const struct nx_action_reg_move *narm,
1105                            struct ofpbuf *ofpacts)
1106 {
1107     struct ofpact_reg_move *move;
1108
1109     move = ofpact_put_REG_MOVE(ofpacts);
1110     move->src.field = mf_from_nxm_header(ntohl(narm->src));
1111     move->src.ofs = ntohs(narm->src_ofs);
1112     move->src.n_bits = ntohs(narm->n_bits);
1113     move->dst.field = mf_from_nxm_header(ntohl(narm->dst));
1114     move->dst.ofs = ntohs(narm->dst_ofs);
1115     move->dst.n_bits = ntohs(narm->n_bits);
1116
1117     return nxm_reg_move_check(move, NULL);
1118 }
1119
1120 enum ofperr
1121 nxm_reg_load_from_openflow(const struct nx_action_reg_load *narl,
1122                            struct ofpbuf *ofpacts)
1123 {
1124     struct ofpact_reg_load *load;
1125
1126     load = ofpact_put_REG_LOAD(ofpacts);
1127     load->dst.field = mf_from_nxm_header(ntohl(narl->dst));
1128     load->dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
1129     load->dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
1130     load->subvalue.be64[1] = narl->value;
1131
1132     /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
1133      * narl->value. */
1134     if (load->dst.n_bits < 64 &&
1135         ntohll(narl->value) >> load->dst.n_bits) {
1136         return OFPERR_OFPBAC_BAD_ARGUMENT;
1137     }
1138
1139     return nxm_reg_load_check(load, NULL);
1140 }
1141
1142 enum ofperr
1143 nxm_reg_load_from_openflow12_set_field(
1144     const struct ofp12_action_set_field * oasf, struct ofpbuf *ofpacts)
1145 {
1146     uint16_t oasf_len = ntohs(oasf->len);
1147     uint32_t oxm_header = ntohl(oasf->dst);
1148     uint8_t oxm_length = NXM_LENGTH(oxm_header);
1149     struct ofpact_reg_load *load;
1150     const struct mf_field *mf;
1151
1152     /* ofp12_action_set_field is padded to 64 bits by zero */
1153     if (oasf_len != ROUND_UP(sizeof(*oasf) + oxm_length, 8)) {
1154         return OFPERR_OFPBAC_BAD_ARGUMENT;
1155     }
1156     if (!is_all_zeros((const uint8_t *)(oasf) + sizeof *oasf + oxm_length,
1157                       oasf_len - oxm_length - sizeof *oasf)) {
1158         return OFPERR_OFPBAC_BAD_ARGUMENT;
1159     }
1160
1161     if (NXM_HASMASK(oxm_header)) {
1162         return OFPERR_OFPBAC_BAD_ARGUMENT;
1163     }
1164     mf = mf_from_nxm_header(oxm_header);
1165     if (!mf) {
1166         return OFPERR_OFPBAC_BAD_ARGUMENT;
1167     }
1168     load = ofpact_put_REG_LOAD(ofpacts);
1169     ofpact_set_field_init(load, mf, oasf + 1);
1170
1171     return nxm_reg_load_check(load, NULL);
1172 }
1173 \f
1174 enum ofperr
1175 nxm_reg_move_check(const struct ofpact_reg_move *move, const struct flow *flow)
1176 {
1177     enum ofperr error;
1178
1179     error = mf_check_src(&move->src, flow);
1180     if (error) {
1181         return error;
1182     }
1183
1184     return mf_check_dst(&move->dst, NULL);
1185 }
1186
1187 enum ofperr
1188 nxm_reg_load_check(const struct ofpact_reg_load *load, const struct flow *flow)
1189 {
1190     return mf_check_dst(&load->dst, flow);
1191 }
1192 \f
1193 void
1194 nxm_reg_move_to_nxast(const struct ofpact_reg_move *move,
1195                       struct ofpbuf *openflow)
1196 {
1197     struct nx_action_reg_move *narm;
1198
1199     narm = ofputil_put_NXAST_REG_MOVE(openflow);
1200     narm->n_bits = htons(move->dst.n_bits);
1201     narm->src_ofs = htons(move->src.ofs);
1202     narm->dst_ofs = htons(move->dst.ofs);
1203     narm->src = htonl(move->src.field->nxm_header);
1204     narm->dst = htonl(move->dst.field->nxm_header);
1205 }
1206
1207 static void
1208 reg_load_to_nxast(const struct ofpact_reg_load *load, struct ofpbuf *openflow)
1209 {
1210     struct nx_action_reg_load *narl;
1211
1212     narl = ofputil_put_NXAST_REG_LOAD(openflow);
1213     narl->ofs_nbits = nxm_encode_ofs_nbits(load->dst.ofs, load->dst.n_bits);
1214     narl->dst = htonl(load->dst.field->nxm_header);
1215     narl->value = load->subvalue.be64[1];
1216 }
1217
1218 static void
1219 set_field_to_ofast(const struct ofpact_reg_load *load,
1220                       struct ofpbuf *openflow)
1221 {
1222     const struct mf_field *mf = load->dst.field;
1223     uint16_t padded_value_len = ROUND_UP(mf->n_bytes, 8);
1224     struct ofp12_action_set_field *oasf;
1225     char *value;
1226
1227     /* Set field is the only action of variable length (so far),
1228      * so handling the variable length portion is open-coded here */
1229     oasf = ofputil_put_OFPAT12_SET_FIELD(openflow);
1230     oasf->dst = htonl(mf->oxm_header);
1231     oasf->len = htons(ntohs(oasf->len) + padded_value_len);
1232
1233     value = ofpbuf_put_zeros(openflow, padded_value_len);
1234     bitwise_copy(&load->subvalue, sizeof load->subvalue, load->dst.ofs,
1235                  value, mf->n_bytes, load->dst.ofs, load->dst.n_bits);
1236 }
1237
1238 void
1239 nxm_reg_load_to_nxast(const struct ofpact_reg_load *load,
1240                       struct ofpbuf *openflow)
1241 {
1242
1243     if (load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD) {
1244         struct ofp_header *oh = (struct ofp_header *)openflow->l2;
1245
1246         switch(oh->version) {
1247         case OFP13_VERSION:
1248         case OFP12_VERSION:
1249             set_field_to_ofast(load, openflow);
1250             break;
1251
1252         case OFP11_VERSION:
1253         case OFP10_VERSION:
1254             if (load->dst.n_bits < 64) {
1255                 reg_load_to_nxast(load, openflow);
1256             } else {
1257                 /* Split into 64bit chunks */
1258                 int chunk, ofs;
1259                 for (ofs = 0; ofs < load->dst.n_bits; ofs += chunk) {
1260                     struct ofpact_reg_load subload = *load;
1261
1262                     chunk = MIN(load->dst.n_bits - ofs, 64);
1263
1264                     subload.dst.field =  load->dst.field;
1265                     subload.dst.ofs = load->dst.ofs + ofs;
1266                     subload.dst.n_bits = chunk;
1267                     bitwise_copy(&load->subvalue, sizeof load->subvalue, ofs,
1268                                  &subload.subvalue, sizeof subload.subvalue, 0,
1269                                  chunk);
1270                     reg_load_to_nxast(&subload, openflow);
1271                 }
1272             }
1273             break;
1274
1275         default:
1276             NOT_REACHED();
1277         }
1278     } else {
1279         reg_load_to_nxast(load, openflow);
1280     }
1281 }
1282 \f
1283 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
1284
1285 void
1286 nxm_execute_reg_move(const struct ofpact_reg_move *move,
1287                      struct flow *flow, struct flow_wildcards *wc)
1288 {
1289     union mf_subvalue mask_value;
1290     union mf_value src_value;
1291     union mf_value dst_value;
1292
1293     memset(&mask_value, 0xff, sizeof mask_value);
1294     mf_write_subfield_flow(&move->src, &mask_value, &wc->masks);
1295
1296     mf_get_value(move->dst.field, flow, &dst_value);
1297     mf_get_value(move->src.field, flow, &src_value);
1298     bitwise_copy(&src_value, move->src.field->n_bytes, move->src.ofs,
1299                  &dst_value, move->dst.field->n_bytes, move->dst.ofs,
1300                  move->src.n_bits);
1301     mf_set_flow_value(move->dst.field, &dst_value, flow);
1302 }
1303
1304 void
1305 nxm_execute_reg_load(const struct ofpact_reg_load *load, struct flow *flow)
1306 {
1307     mf_write_subfield_flow(&load->dst, &load->subvalue, flow);
1308 }
1309
1310 void
1311 nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data,
1312              struct flow *flow)
1313 {
1314     union mf_subvalue src_subvalue;
1315     ovs_be64 src_data_be = htonll(src_data);
1316
1317     bitwise_copy(&src_data_be, sizeof src_data_be, 0,
1318                  &src_subvalue, sizeof src_subvalue, 0,
1319                  sizeof src_data_be * 8);
1320     mf_write_subfield_flow(dst, &src_subvalue, flow);
1321 }
1322 \f
1323 /* nxm_parse_stack_action, works for both push() and pop(). */
1324 void
1325 nxm_parse_stack_action(struct ofpact_stack *stack_action, const char *s)
1326 {
1327     s = mf_parse_subfield(&stack_action->subfield, s);
1328     if (*s != '\0') {
1329         ovs_fatal(0, "%s: trailing garbage following push or pop", s);
1330     }
1331 }
1332
1333 void
1334 nxm_format_stack_push(const struct ofpact_stack *push, struct ds *s)
1335 {
1336     ds_put_cstr(s, "push:");
1337     mf_format_subfield(&push->subfield, s);
1338 }
1339
1340 void
1341 nxm_format_stack_pop(const struct ofpact_stack *pop, struct ds *s)
1342 {
1343     ds_put_cstr(s, "pop:");
1344     mf_format_subfield(&pop->subfield, s);
1345 }
1346
1347 /* Common set for both push and pop actions. */
1348 static void
1349 stack_action_from_openflow__(const struct nx_action_stack *nasp,
1350                                     struct ofpact_stack *stack_action)
1351 {
1352     stack_action->subfield.field = mf_from_nxm_header(ntohl(nasp->field));
1353     stack_action->subfield.ofs = ntohs(nasp->offset);
1354     stack_action->subfield.n_bits = ntohs(nasp->n_bits);
1355 }
1356
1357 static void
1358 nxm_stack_to_nxast__(const struct ofpact_stack *stack_action,
1359                             struct nx_action_stack *nasp)
1360 {
1361     nasp->offset = htons(stack_action->subfield.ofs);
1362     nasp->n_bits = htons(stack_action->subfield.n_bits);
1363     nasp->field = htonl(stack_action->subfield.field->nxm_header);
1364 }
1365
1366 enum ofperr
1367 nxm_stack_push_from_openflow(const struct nx_action_stack *nasp,
1368                              struct ofpbuf *ofpacts)
1369 {
1370     struct ofpact_stack *push;
1371
1372     push = ofpact_put_STACK_PUSH(ofpacts);
1373     stack_action_from_openflow__(nasp, push);
1374
1375     return nxm_stack_push_check(push, NULL);
1376 }
1377
1378 enum ofperr
1379 nxm_stack_pop_from_openflow(const struct nx_action_stack *nasp,
1380                              struct ofpbuf *ofpacts)
1381 {
1382     struct ofpact_stack *pop;
1383
1384     pop = ofpact_put_STACK_POP(ofpacts);
1385     stack_action_from_openflow__(nasp, pop);
1386
1387     return nxm_stack_pop_check(pop, NULL);
1388 }
1389
1390 enum ofperr
1391 nxm_stack_push_check(const struct ofpact_stack *push,
1392                      const struct flow *flow)
1393 {
1394     return mf_check_src(&push->subfield, flow);
1395 }
1396
1397 enum ofperr
1398 nxm_stack_pop_check(const struct ofpact_stack *pop,
1399                     const struct flow *flow)
1400 {
1401     return mf_check_dst(&pop->subfield, flow);
1402 }
1403
1404 void
1405 nxm_stack_push_to_nxast(const struct ofpact_stack *stack,
1406                         struct ofpbuf *openflow)
1407 {
1408     nxm_stack_to_nxast__(stack, ofputil_put_NXAST_STACK_PUSH(openflow));
1409 }
1410
1411 void
1412 nxm_stack_pop_to_nxast(const struct ofpact_stack *stack,
1413                        struct ofpbuf *openflow)
1414 {
1415     nxm_stack_to_nxast__(stack, ofputil_put_NXAST_STACK_POP(openflow));
1416 }
1417
1418 /* nxm_execute_stack_push(), nxm_execute_stack_pop(). */
1419 static void
1420 nx_stack_push(struct ofpbuf *stack, union mf_subvalue *v)
1421 {
1422     ofpbuf_put(stack, v, sizeof *v);
1423 }
1424
1425 static union mf_subvalue *
1426 nx_stack_pop(struct ofpbuf *stack)
1427 {
1428     union mf_subvalue *v = NULL;
1429
1430     if (stack->size) {
1431         stack->size -= sizeof *v;
1432         v = (union mf_subvalue *) ofpbuf_tail(stack);
1433     }
1434
1435     return v;
1436 }
1437
1438 void
1439 nxm_execute_stack_push(const struct ofpact_stack *push,
1440                        const struct flow *flow, struct flow_wildcards *wc,
1441                        struct ofpbuf *stack)
1442 {
1443     union mf_subvalue mask_value;
1444     union mf_subvalue dst_value;
1445
1446     memset(&mask_value, 0xff, sizeof mask_value);
1447     mf_write_subfield_flow(&push->subfield, &mask_value, &wc->masks);
1448
1449     mf_read_subfield(&push->subfield, flow, &dst_value);
1450     nx_stack_push(stack, &dst_value);
1451 }
1452
1453 void
1454 nxm_execute_stack_pop(const struct ofpact_stack *pop,
1455                       struct flow *flow, struct ofpbuf *stack)
1456 {
1457     union mf_subvalue *src_value;
1458
1459     src_value = nx_stack_pop(stack);
1460
1461     /* Only pop if stack is not empty. Otherwise, give warning. */
1462     if (src_value) {
1463         mf_write_subfield_flow(&pop->subfield, src_value, flow);
1464     } else {
1465         if (!VLOG_DROP_WARN(&rl)) {
1466             char *flow_str = flow_to_string(flow);
1467             VLOG_WARN_RL(&rl, "Failed to pop from an empty stack. On flow \n"
1468                            " %s", flow_str);
1469             free(flow_str);
1470         }
1471     }
1472 }