ofp-actions: Centralize all OpenFlow action code for maintainability.
[cascardo/ovs.git] / lib / nx-match.c
1 /*
2  * Copyright (c) 2010, 2011, 2012, 2013, 2014 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 {
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         }
174
175         /* Check if the match is for a cookie rather than a classifier rule. */
176         if ((header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W) && cookie) {
177             if (*cookie_mask) {
178                 error = OFPERR_OFPBMC_DUP_FIELD;
179             } else {
180                 unsigned int width = sizeof *cookie;
181
182                 memcpy(cookie, p + 4, width);
183                 if (NXM_HASMASK(header)) {
184                     memcpy(cookie_mask, p + 4 + width, width);
185                 } else {
186                     *cookie_mask = OVS_BE64_MAX;
187                 }
188                 error = 0;
189             }
190         }
191
192         if (error) {
193             VLOG_DBG_RL(&rl, "bad nxm_entry %#08"PRIx32" (vendor=%"PRIu32", "
194                         "field=%"PRIu32", hasmask=%"PRIu32", len=%"PRIu32"), "
195                         "(%s)", header,
196                         NXM_VENDOR(header), NXM_FIELD(header),
197                         NXM_HASMASK(header), NXM_LENGTH(header),
198                         ofperr_to_string(error));
199             return error;
200         }
201     }
202
203     return match_len ? OFPERR_OFPBMC_BAD_LEN : 0;
204 }
205
206 static enum ofperr
207 nx_pull_match__(struct ofpbuf *b, unsigned int match_len, bool strict,
208                 struct match *match,
209                 ovs_be64 *cookie, ovs_be64 *cookie_mask)
210 {
211     uint8_t *p = NULL;
212
213     if (match_len) {
214         p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
215         if (!p) {
216             VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
217                         "multiple of 8, is longer than space in message (max "
218                         "length %"PRIu32")", match_len, ofpbuf_size(b));
219             return OFPERR_OFPBMC_BAD_LEN;
220         }
221     }
222
223     return nx_pull_raw(p, match_len, strict, match, cookie, cookie_mask);
224 }
225
226 /* Parses the nx_match formatted match description in 'b' with length
227  * 'match_len'.  Stores the results in 'match'.  If 'cookie' and 'cookie_mask'
228  * are valid pointers, then stores the cookie and mask in them if 'b' contains
229  * a "NXM_NX_COOKIE*" match.  Otherwise, stores 0 in both.
230  *
231  * Fails with an error upon encountering an unknown NXM header.
232  *
233  * Returns 0 if successful, otherwise an OpenFlow error code. */
234 enum ofperr
235 nx_pull_match(struct ofpbuf *b, unsigned int match_len, struct match *match,
236               ovs_be64 *cookie, ovs_be64 *cookie_mask)
237 {
238     return nx_pull_match__(b, match_len, true, match, cookie, cookie_mask);
239 }
240
241 /* Behaves the same as nx_pull_match(), but skips over unknown NXM headers,
242  * instead of failing with an error. */
243 enum ofperr
244 nx_pull_match_loose(struct ofpbuf *b, unsigned int match_len,
245                     struct match *match,
246                     ovs_be64 *cookie, ovs_be64 *cookie_mask)
247 {
248     return nx_pull_match__(b, match_len, false, match, cookie, cookie_mask);
249 }
250
251 static enum ofperr
252 oxm_pull_match__(struct ofpbuf *b, bool strict, struct match *match)
253 {
254     struct ofp11_match_header *omh = ofpbuf_data(b);
255     uint8_t *p;
256     uint16_t match_len;
257
258     if (ofpbuf_size(b) < sizeof *omh) {
259         return OFPERR_OFPBMC_BAD_LEN;
260     }
261
262     match_len = ntohs(omh->length);
263     if (match_len < sizeof *omh) {
264         return OFPERR_OFPBMC_BAD_LEN;
265     }
266
267     if (omh->type != htons(OFPMT_OXM)) {
268         return OFPERR_OFPBMC_BAD_TYPE;
269     }
270
271     p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
272     if (!p) {
273         VLOG_DBG_RL(&rl, "oxm length %u, rounded up to a "
274                     "multiple of 8, is longer than space in message (max "
275                     "length %"PRIu32")", match_len, ofpbuf_size(b));
276         return OFPERR_OFPBMC_BAD_LEN;
277     }
278
279     return nx_pull_raw(p + sizeof *omh, match_len - sizeof *omh,
280                        strict, match, NULL, NULL);
281 }
282
283 /* Parses the oxm formatted match description preceded by a struct
284  * ofp11_match_header in 'b'.  Stores the result in 'match'.
285  *
286  * Fails with an error when encountering unknown OXM headers.
287  *
288  * Returns 0 if successful, otherwise an OpenFlow error code. */
289 enum ofperr
290 oxm_pull_match(struct ofpbuf *b, struct match *match)
291 {
292     return oxm_pull_match__(b, true, match);
293 }
294
295 /* Behaves the same as oxm_pull_match() with one exception.  Skips over unknown
296  * OXM headers instead of failing with an error when they are encountered. */
297 enum ofperr
298 oxm_pull_match_loose(struct ofpbuf *b, struct match *match)
299 {
300     return oxm_pull_match__(b, false, match);
301 }
302 \f
303 /* nx_put_match() and helpers.
304  *
305  * 'put' functions whose names end in 'w' add a wildcarded field.
306  * 'put' functions whose names end in 'm' add a field that might be wildcarded.
307  * Other 'put' functions add exact-match fields.
308  */
309
310 static void
311 nxm_put_header(struct ofpbuf *b, uint32_t header)
312 {
313     ovs_be32 n_header = htonl(header);
314     ofpbuf_put(b, &n_header, sizeof n_header);
315 }
316
317 static void
318 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
319 {
320     nxm_put_header(b, header);
321     ofpbuf_put(b, &value, sizeof value);
322 }
323
324 static void
325 nxm_put_8m(struct ofpbuf *b, uint32_t header, uint8_t value, uint8_t mask)
326 {
327     switch (mask) {
328     case 0:
329         break;
330
331     case UINT8_MAX:
332         nxm_put_8(b, header, value);
333         break;
334
335     default:
336         nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
337         ofpbuf_put(b, &value, sizeof value);
338         ofpbuf_put(b, &mask, sizeof mask);
339     }
340 }
341
342 static void
343 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
344 {
345     nxm_put_header(b, header);
346     ofpbuf_put(b, &value, sizeof value);
347 }
348
349 static void
350 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
351 {
352     nxm_put_header(b, header);
353     ofpbuf_put(b, &value, sizeof value);
354     ofpbuf_put(b, &mask, sizeof mask);
355 }
356
357 static void
358 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
359 {
360     switch (mask) {
361     case 0:
362         break;
363
364     case OVS_BE16_MAX:
365         nxm_put_16(b, header, value);
366         break;
367
368     default:
369         nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
370         break;
371     }
372 }
373
374 static void
375 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
376 {
377     nxm_put_header(b, header);
378     ofpbuf_put(b, &value, sizeof value);
379 }
380
381 static void
382 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
383 {
384     nxm_put_header(b, header);
385     ofpbuf_put(b, &value, sizeof value);
386     ofpbuf_put(b, &mask, sizeof mask);
387 }
388
389 static void
390 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
391 {
392     switch (mask) {
393     case 0:
394         break;
395
396     case OVS_BE32_MAX:
397         nxm_put_32(b, header, value);
398         break;
399
400     default:
401         nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
402         break;
403     }
404 }
405
406 static void
407 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
408 {
409     nxm_put_header(b, header);
410     ofpbuf_put(b, &value, sizeof value);
411 }
412
413 static void
414 nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
415 {
416     nxm_put_header(b, header);
417     ofpbuf_put(b, &value, sizeof value);
418     ofpbuf_put(b, &mask, sizeof mask);
419 }
420
421 static void
422 nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
423 {
424     switch (mask) {
425     case 0:
426         break;
427
428     case OVS_BE64_MAX:
429         nxm_put_64(b, header, value);
430         break;
431
432     default:
433         nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
434         break;
435     }
436 }
437
438 static void
439 nxm_put_eth(struct ofpbuf *b, uint32_t header,
440             const uint8_t value[ETH_ADDR_LEN])
441 {
442     nxm_put_header(b, header);
443     ofpbuf_put(b, value, ETH_ADDR_LEN);
444 }
445
446 static void
447 nxm_put_eth_masked(struct ofpbuf *b, uint32_t header,
448                    const uint8_t value[ETH_ADDR_LEN],
449                    const uint8_t mask[ETH_ADDR_LEN])
450 {
451     if (!eth_addr_is_zero(mask)) {
452         if (eth_mask_is_exact(mask)) {
453             nxm_put_eth(b, header, value);
454         } else {
455             nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
456             ofpbuf_put(b, value, ETH_ADDR_LEN);
457             ofpbuf_put(b, mask, ETH_ADDR_LEN);
458         }
459     }
460 }
461
462 static void
463 nxm_put_ipv6(struct ofpbuf *b, uint32_t header,
464              const struct in6_addr *value, const struct in6_addr *mask)
465 {
466     if (ipv6_mask_is_any(mask)) {
467         return;
468     } else if (ipv6_mask_is_exact(mask)) {
469         nxm_put_header(b, header);
470         ofpbuf_put(b, value, sizeof *value);
471     } else {
472         nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
473         ofpbuf_put(b, value, sizeof *value);
474         ofpbuf_put(b, mask, sizeof *mask);
475     }
476 }
477
478 static void
479 nxm_put_frag(struct ofpbuf *b, const struct match *match)
480 {
481     uint8_t nw_frag = match->flow.nw_frag;
482     uint8_t nw_frag_mask = match->wc.masks.nw_frag;
483
484     switch (nw_frag_mask) {
485     case 0:
486         break;
487
488     case FLOW_NW_FRAG_MASK:
489         nxm_put_8(b, NXM_NX_IP_FRAG, nw_frag);
490         break;
491
492     default:
493         nxm_put_8m(b, NXM_NX_IP_FRAG, nw_frag,
494                    nw_frag_mask & FLOW_NW_FRAG_MASK);
495         break;
496     }
497 }
498
499 /* Appends to 'b' a set of OXM or NXM matches for the IPv4 or IPv6 fields in
500  * 'match'.  */
501 static void
502 nxm_put_ip(struct ofpbuf *b, const struct match *match, enum ofp_version oxm)
503 {
504     const struct flow *flow = &match->flow;
505
506     if (flow->dl_type == htons(ETH_TYPE_IP)) {
507         nxm_put_32m(b, mf_oxm_header(MFF_IPV4_SRC, oxm),
508                     flow->nw_src, match->wc.masks.nw_src);
509         nxm_put_32m(b, mf_oxm_header(MFF_IPV4_DST, oxm),
510                     flow->nw_dst, match->wc.masks.nw_dst);
511     } else {
512         nxm_put_ipv6(b, mf_oxm_header(MFF_IPV6_SRC, oxm),
513                      &flow->ipv6_src, &match->wc.masks.ipv6_src);
514         nxm_put_ipv6(b, mf_oxm_header(MFF_IPV6_DST, oxm),
515                      &flow->ipv6_dst, &match->wc.masks.ipv6_dst);
516     }
517
518     nxm_put_frag(b, match);
519
520     if (match->wc.masks.nw_tos & IP_DSCP_MASK) {
521         if (oxm) {
522             nxm_put_8(b, mf_oxm_header(MFF_IP_DSCP_SHIFTED, oxm),
523                       flow->nw_tos >> 2);
524         } else {
525             nxm_put_8(b, mf_oxm_header(MFF_IP_DSCP, oxm),
526                       flow->nw_tos & IP_DSCP_MASK);
527         }
528     }
529
530     if (match->wc.masks.nw_tos & IP_ECN_MASK) {
531         nxm_put_8(b, mf_oxm_header(MFF_IP_ECN, oxm),
532                   flow->nw_tos & IP_ECN_MASK);
533     }
534
535     if (!oxm && match->wc.masks.nw_ttl) {
536         nxm_put_8(b, mf_oxm_header(MFF_IP_TTL, oxm), flow->nw_ttl);
537     }
538
539     nxm_put_32m(b, mf_oxm_header(MFF_IPV6_LABEL, oxm),
540                 flow->ipv6_label, match->wc.masks.ipv6_label);
541
542     if (match->wc.masks.nw_proto) {
543         nxm_put_8(b, mf_oxm_header(MFF_IP_PROTO, oxm), flow->nw_proto);
544
545         if (flow->nw_proto == IPPROTO_TCP) {
546             nxm_put_16m(b, mf_oxm_header(MFF_TCP_SRC, oxm),
547                         flow->tp_src, match->wc.masks.tp_src);
548             nxm_put_16m(b, mf_oxm_header(MFF_TCP_DST, oxm),
549                         flow->tp_dst, match->wc.masks.tp_dst);
550             nxm_put_16m(b, mf_oxm_header(MFF_TCP_FLAGS, oxm),
551                         flow->tcp_flags, match->wc.masks.tcp_flags);
552         } else if (flow->nw_proto == IPPROTO_UDP) {
553             nxm_put_16m(b, mf_oxm_header(MFF_UDP_SRC, oxm),
554                         flow->tp_src, match->wc.masks.tp_src);
555             nxm_put_16m(b, mf_oxm_header(MFF_UDP_DST, oxm),
556                         flow->tp_dst, match->wc.masks.tp_dst);
557         } else if (flow->nw_proto == IPPROTO_SCTP) {
558             nxm_put_16m(b, mf_oxm_header(MFF_SCTP_SRC, oxm), flow->tp_src,
559                         match->wc.masks.tp_src);
560             nxm_put_16m(b, mf_oxm_header(MFF_SCTP_DST, oxm), flow->tp_dst,
561                         match->wc.masks.tp_dst);
562         } else if (is_icmpv4(flow)) {
563             if (match->wc.masks.tp_src) {
564                 nxm_put_8(b, mf_oxm_header(MFF_ICMPV4_TYPE, oxm),
565                           ntohs(flow->tp_src));
566             }
567             if (match->wc.masks.tp_dst) {
568                 nxm_put_8(b, mf_oxm_header(MFF_ICMPV4_CODE, oxm),
569                           ntohs(flow->tp_dst));
570             }
571         } else if (is_icmpv6(flow)) {
572             if (match->wc.masks.tp_src) {
573                 nxm_put_8(b, mf_oxm_header(MFF_ICMPV6_TYPE, oxm),
574                           ntohs(flow->tp_src));
575             }
576             if (match->wc.masks.tp_dst) {
577                 nxm_put_8(b, mf_oxm_header(MFF_ICMPV6_CODE, oxm),
578                           ntohs(flow->tp_dst));
579             }
580             if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
581                 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
582                 nxm_put_ipv6(b, mf_oxm_header(MFF_ND_TARGET, oxm),
583                              &flow->nd_target, &match->wc.masks.nd_target);
584                 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
585                     nxm_put_eth_masked(b, mf_oxm_header(MFF_ND_SLL, oxm),
586                                        flow->arp_sha, match->wc.masks.arp_sha);
587                 }
588                 if (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
589                     nxm_put_eth_masked(b, mf_oxm_header(MFF_ND_TLL, oxm),
590                                        flow->arp_tha, match->wc.masks.arp_tha);
591                 }
592             }
593         }
594     }
595 }
596
597 /* Appends to 'b' the nx_match format that expresses 'match'.  For Flow Mod and
598  * Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
599  * Otherwise, 'cookie_mask' should be zero.
600  *
601  * Specify 'oxm' as 0 to express the match in NXM format; otherwise, specify
602  * 'oxm' as the OpenFlow version number for the OXM format to use.
603  *
604  * This function can cause 'b''s data to be reallocated.
605  *
606  * Returns the number of bytes appended to 'b', excluding padding.
607  *
608  * If 'match' is a catch-all rule that matches every packet, then this function
609  * appends nothing to 'b' and returns 0. */
610 static int
611 nx_put_raw(struct ofpbuf *b, enum ofp_version oxm, const struct match *match,
612            ovs_be64 cookie, ovs_be64 cookie_mask)
613 {
614     const struct flow *flow = &match->flow;
615     const size_t start_len = ofpbuf_size(b);
616     int match_len;
617     int i;
618
619     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 27);
620
621     /* Metadata. */
622     if (match->wc.masks.dp_hash) {
623         if (!oxm) {
624             nxm_put_32m(b, NXM_NX_DP_HASH, htonl(flow->dp_hash),
625                         htonl(match->wc.masks.dp_hash));
626         }
627     }
628
629     if (match->wc.masks.recirc_id) {
630         if (!oxm) {
631             nxm_put_32(b, NXM_NX_RECIRC_ID, htonl(flow->recirc_id));
632         }
633     }
634
635     if (match->wc.masks.in_port.ofp_port) {
636         ofp_port_t in_port = flow->in_port.ofp_port;
637         if (oxm) {
638             nxm_put_32(b, mf_oxm_header(MFF_IN_PORT_OXM, oxm),
639                        ofputil_port_to_ofp11(in_port));
640         } else {
641             nxm_put_16(b, mf_oxm_header(MFF_IN_PORT, oxm),
642                        htons(ofp_to_u16(in_port)));
643         }
644     }
645
646     /* Ethernet. */
647     nxm_put_eth_masked(b, mf_oxm_header(MFF_ETH_SRC, oxm),
648                        flow->dl_src, match->wc.masks.dl_src);
649     nxm_put_eth_masked(b, mf_oxm_header(MFF_ETH_DST, oxm),
650                        flow->dl_dst, match->wc.masks.dl_dst);
651     nxm_put_16m(b, mf_oxm_header(MFF_ETH_TYPE, oxm),
652                 ofputil_dl_type_to_openflow(flow->dl_type),
653                 match->wc.masks.dl_type);
654
655     /* 802.1Q. */
656     if (oxm) {
657         ovs_be16 VID_CFI_MASK = htons(VLAN_VID_MASK | VLAN_CFI);
658         ovs_be16 vid = flow->vlan_tci & VID_CFI_MASK;
659         ovs_be16 mask = match->wc.masks.vlan_tci & VID_CFI_MASK;
660
661         if (mask == htons(VLAN_VID_MASK | VLAN_CFI)) {
662             nxm_put_16(b, mf_oxm_header(MFF_VLAN_VID, oxm), vid);
663         } else if (mask) {
664             nxm_put_16m(b, mf_oxm_header(MFF_VLAN_VID, oxm), vid, mask);
665         }
666
667         if (vid && vlan_tci_to_pcp(match->wc.masks.vlan_tci)) {
668             nxm_put_8(b, mf_oxm_header(MFF_VLAN_PCP, oxm),
669                       vlan_tci_to_pcp(flow->vlan_tci));
670         }
671
672     } else {
673         nxm_put_16m(b, mf_oxm_header(MFF_VLAN_TCI, oxm), flow->vlan_tci,
674                     match->wc.masks.vlan_tci);
675     }
676
677     /* MPLS. */
678     if (eth_type_mpls(flow->dl_type)) {
679         if (match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
680             nxm_put_8(b, mf_oxm_header(MFF_MPLS_TC, oxm),
681                       mpls_lse_to_tc(flow->mpls_lse[0]));
682         }
683
684         if (match->wc.masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
685             nxm_put_8(b, mf_oxm_header(MFF_MPLS_BOS, oxm),
686                       mpls_lse_to_bos(flow->mpls_lse[0]));
687         }
688
689         if (match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
690             nxm_put_32(b, mf_oxm_header(MFF_MPLS_LABEL, oxm),
691                        htonl(mpls_lse_to_label(flow->mpls_lse[0])));
692         }
693     }
694
695     /* L3. */
696     if (is_ip_any(flow)) {
697         nxm_put_ip(b, match, oxm);
698     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
699                flow->dl_type == htons(ETH_TYPE_RARP)) {
700         /* ARP. */
701         if (match->wc.masks.nw_proto) {
702             nxm_put_16(b, mf_oxm_header(MFF_ARP_OP, oxm),
703                        htons(flow->nw_proto));
704         }
705         nxm_put_32m(b, mf_oxm_header(MFF_ARP_SPA, oxm),
706                     flow->nw_src, match->wc.masks.nw_src);
707         nxm_put_32m(b, mf_oxm_header(MFF_ARP_TPA, oxm),
708                     flow->nw_dst, match->wc.masks.nw_dst);
709         nxm_put_eth_masked(b, mf_oxm_header(MFF_ARP_SHA, oxm),
710                            flow->arp_sha, match->wc.masks.arp_sha);
711         nxm_put_eth_masked(b, mf_oxm_header(MFF_ARP_THA, oxm),
712                            flow->arp_tha, match->wc.masks.arp_tha);
713     }
714
715     /* Tunnel ID. */
716     nxm_put_64m(b, mf_oxm_header(MFF_TUN_ID, oxm),
717                 flow->tunnel.tun_id, match->wc.masks.tunnel.tun_id);
718
719     /* Other tunnel metadata. */
720     nxm_put_32m(b, mf_oxm_header(MFF_TUN_SRC, oxm),
721                 flow->tunnel.ip_src, match->wc.masks.tunnel.ip_src);
722     nxm_put_32m(b, mf_oxm_header(MFF_TUN_DST, oxm),
723                 flow->tunnel.ip_dst, match->wc.masks.tunnel.ip_dst);
724
725     /* Registers. */
726     for (i = 0; i < FLOW_N_REGS; i++) {
727         nxm_put_32m(b, mf_oxm_header(MFF_REG0 + i, oxm),
728                     htonl(flow->regs[i]), htonl(match->wc.masks.regs[i]));
729     }
730
731     /* Mark. */
732     nxm_put_32m(b, mf_oxm_header(MFF_PKT_MARK, oxm), htonl(flow->pkt_mark),
733                 htonl(match->wc.masks.pkt_mark));
734
735     /* OpenFlow 1.1+ Metadata. */
736     nxm_put_64m(b, mf_oxm_header(MFF_METADATA, oxm),
737                 flow->metadata, match->wc.masks.metadata);
738
739     /* Cookie. */
740     nxm_put_64m(b, NXM_NX_COOKIE, cookie, cookie_mask);
741
742     match_len = ofpbuf_size(b) - start_len;
743     return match_len;
744 }
745
746 /* Appends to 'b' the nx_match format that expresses 'match', plus enough zero
747  * bytes to pad the nx_match out to a multiple of 8.  For Flow Mod and Flow
748  * Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
749  * Otherwise, 'cookie_mask' should be zero.
750  *
751  * This function can cause 'b''s data to be reallocated.
752  *
753  * Returns the number of bytes appended to 'b', excluding padding.  The return
754  * value can be zero if it appended nothing at all to 'b' (which happens if
755  * 'cr' is a catch-all rule that matches every packet). */
756 int
757 nx_put_match(struct ofpbuf *b, const struct match *match,
758              ovs_be64 cookie, ovs_be64 cookie_mask)
759 {
760     int match_len = nx_put_raw(b, 0, match, cookie, cookie_mask);
761
762     ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
763     return match_len;
764 }
765
766 /* Appends to 'b' an struct ofp11_match_header followed by the OXM format that
767  * expresses 'cr', plus enough zero bytes to pad the data appended out to a
768  * multiple of 8.
769  *
770  * OXM differs slightly among versions of OpenFlow.  Specify the OpenFlow
771  * version in use as 'version'.
772  *
773  * This function can cause 'b''s data to be reallocated.
774  *
775  * Returns the number of bytes appended to 'b', excluding the padding.  Never
776  * returns zero. */
777 int
778 oxm_put_match(struct ofpbuf *b, const struct match *match,
779               enum ofp_version version)
780 {
781     int match_len;
782     struct ofp11_match_header *omh;
783     size_t start_len = ofpbuf_size(b);
784     ovs_be64 cookie = htonll(0), cookie_mask = htonll(0);
785
786     ofpbuf_put_uninit(b, sizeof *omh);
787     match_len = (nx_put_raw(b, version, match, cookie, cookie_mask)
788                  + sizeof *omh);
789     ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
790
791     omh = ofpbuf_at(b, start_len, sizeof *omh);
792     omh->type = htons(OFPMT_OXM);
793     omh->length = htons(match_len);
794
795     return match_len;
796 }
797 \f
798 /* nx_match_to_string() and helpers. */
799
800 static void format_nxm_field_name(struct ds *, uint32_t header);
801
802 char *
803 nx_match_to_string(const uint8_t *p, unsigned int match_len)
804 {
805     uint32_t header;
806     struct ds s;
807
808     if (!match_len) {
809         return xstrdup("<any>");
810     }
811
812     ds_init(&s);
813     while ((header = nx_entry_ok(p, match_len)) != 0) {
814         unsigned int length = NXM_LENGTH(header);
815         unsigned int value_len = nxm_field_bytes(header);
816         const uint8_t *value = p + 4;
817         const uint8_t *mask = value + value_len;
818         unsigned int i;
819
820         if (s.length) {
821             ds_put_cstr(&s, ", ");
822         }
823
824         format_nxm_field_name(&s, header);
825         ds_put_char(&s, '(');
826
827         for (i = 0; i < value_len; i++) {
828             ds_put_format(&s, "%02x", value[i]);
829         }
830         if (NXM_HASMASK(header)) {
831             ds_put_char(&s, '/');
832             for (i = 0; i < value_len; i++) {
833                 ds_put_format(&s, "%02x", mask[i]);
834             }
835         }
836         ds_put_char(&s, ')');
837
838         p += 4 + length;
839         match_len -= 4 + length;
840     }
841
842     if (match_len) {
843         if (s.length) {
844             ds_put_cstr(&s, ", ");
845         }
846
847         ds_put_format(&s, "<%u invalid bytes>", match_len);
848     }
849
850     return ds_steal_cstr(&s);
851 }
852
853 char *
854 oxm_match_to_string(const struct ofpbuf *p, unsigned int match_len)
855 {
856     const struct ofp11_match_header *omh = ofpbuf_data(p);
857     uint16_t match_len_;
858     struct ds s;
859
860     ds_init(&s);
861
862     if (match_len < sizeof *omh) {
863         ds_put_format(&s, "<match too short: %u>", match_len);
864         goto err;
865     }
866
867     if (omh->type != htons(OFPMT_OXM)) {
868         ds_put_format(&s, "<bad match type field: %u>", ntohs(omh->type));
869         goto err;
870     }
871
872     match_len_ = ntohs(omh->length);
873     if (match_len_ < sizeof *omh) {
874         ds_put_format(&s, "<match length field too short: %u>", match_len_);
875         goto err;
876     }
877
878     if (match_len_ != match_len) {
879         ds_put_format(&s, "<match length field incorrect: %u != %u>",
880                       match_len_, match_len);
881         goto err;
882     }
883
884     return nx_match_to_string(ofpbuf_at(p, sizeof *omh, 0),
885                               match_len - sizeof *omh);
886
887 err:
888     return ds_steal_cstr(&s);
889 }
890
891 static void
892 format_nxm_field_name(struct ds *s, uint32_t header)
893 {
894     const struct mf_field *mf = mf_from_nxm_header(header);
895     if (mf) {
896         ds_put_cstr(s, IS_OXM_HEADER(header) ? mf->oxm_name : mf->nxm_name);
897         if (NXM_HASMASK(header)) {
898             ds_put_cstr(s, "_W");
899         }
900     } else if (header == NXM_NX_COOKIE) {
901         ds_put_cstr(s, "NXM_NX_COOKIE");
902     } else if (header == NXM_NX_COOKIE_W) {
903         ds_put_cstr(s, "NXM_NX_COOKIE_W");
904     } else {
905         ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
906     }
907 }
908
909 static uint32_t
910 parse_nxm_field_name(const char *name, int name_len)
911 {
912     bool wild;
913     int i;
914
915     /* Check whether it's a field name. */
916     wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
917     if (wild) {
918         name_len -= 2;
919     }
920
921     for (i = 0; i < MFF_N_IDS; i++) {
922         const struct mf_field *mf = mf_from_id(i);
923         uint32_t header;
924
925         if (mf->nxm_name &&
926             !strncmp(mf->nxm_name, name, name_len) &&
927             mf->nxm_name[name_len] == '\0') {
928             header = mf->nxm_header;
929         } else if (mf->oxm_name &&
930                    !strncmp(mf->oxm_name, name, name_len) &&
931                    mf->oxm_name[name_len] == '\0') {
932             header = mf->oxm_header;
933         } else {
934             continue;
935         }
936
937         if (!wild) {
938             return header;
939         } else if (mf->maskable != MFM_NONE) {
940             return NXM_MAKE_WILD_HEADER(header);
941         }
942     }
943
944     if (!strncmp("NXM_NX_COOKIE", name, name_len) &&
945         (name_len == strlen("NXM_NX_COOKIE"))) {
946         if (!wild) {
947             return NXM_NX_COOKIE;
948         } else {
949             return NXM_NX_COOKIE_W;
950         }
951     }
952
953     /* Check whether it's a 32-bit field header value as hex.
954      * (This isn't ordinarily useful except for testing error behavior.) */
955     if (name_len == 8) {
956         uint32_t header = hexits_value(name, name_len, NULL);
957         if (header != UINT_MAX) {
958             return header;
959         }
960     }
961
962     return 0;
963 }
964 \f
965 /* nx_match_from_string(). */
966
967 static int
968 nx_match_from_string_raw(const char *s, struct ofpbuf *b)
969 {
970     const char *full_s = s;
971     const size_t start_len = ofpbuf_size(b);
972
973     if (!strcmp(s, "<any>")) {
974         /* Ensure that 'ofpbuf_data(b)' isn't actually null. */
975         ofpbuf_prealloc_tailroom(b, 1);
976         return 0;
977     }
978
979     for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
980         const char *name;
981         uint32_t header;
982         int name_len;
983         size_t n;
984
985         name = s;
986         name_len = strcspn(s, "(");
987         if (s[name_len] != '(') {
988             ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
989         }
990
991         header = parse_nxm_field_name(name, name_len);
992         if (!header) {
993             ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
994         }
995
996         s += name_len + 1;
997
998         nxm_put_header(b, header);
999         s = ofpbuf_put_hex(b, s, &n);
1000         if (n != nxm_field_bytes(header)) {
1001             ovs_fatal(0, "%.2s: hex digits expected", s);
1002         }
1003         if (NXM_HASMASK(header)) {
1004             s += strspn(s, " ");
1005             if (*s != '/') {
1006                 ovs_fatal(0, "%s: missing / in masked field %.*s",
1007                           full_s, name_len, name);
1008             }
1009             s = ofpbuf_put_hex(b, s + 1, &n);
1010             if (n != nxm_field_bytes(header)) {
1011                 ovs_fatal(0, "%.2s: hex digits expected", s);
1012             }
1013         }
1014
1015         s += strspn(s, " ");
1016         if (*s != ')') {
1017             ovs_fatal(0, "%s: missing ) following field %.*s",
1018                       full_s, name_len, name);
1019         }
1020         s++;
1021     }
1022
1023     return ofpbuf_size(b) - start_len;
1024 }
1025
1026 int
1027 nx_match_from_string(const char *s, struct ofpbuf *b)
1028 {
1029     int match_len = nx_match_from_string_raw(s, b);
1030     ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1031     return match_len;
1032 }
1033
1034 int
1035 oxm_match_from_string(const char *s, struct ofpbuf *b)
1036 {
1037     int match_len;
1038     struct ofp11_match_header *omh;
1039     size_t start_len = ofpbuf_size(b);
1040
1041     ofpbuf_put_uninit(b, sizeof *omh);
1042     match_len = nx_match_from_string_raw(s, b) + sizeof *omh;
1043     ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1044
1045     omh = ofpbuf_at(b, start_len, sizeof *omh);
1046     omh->type = htons(OFPMT_OXM);
1047     omh->length = htons(match_len);
1048
1049     return match_len;
1050 }
1051 \f
1052 /* Parses 's' as a "move" action, in the form described in ovs-ofctl(8), into
1053  * '*move'.
1054  *
1055  * Returns NULL if successful, otherwise a malloc()'d string describing the
1056  * error.  The caller is responsible for freeing the returned string. */
1057 char * WARN_UNUSED_RESULT
1058 nxm_parse_reg_move(struct ofpact_reg_move *move, const char *s)
1059 {
1060     const char *full_s = s;
1061     char *error;
1062
1063     error = mf_parse_subfield__(&move->src, &s);
1064     if (error) {
1065         return error;
1066     }
1067     if (strncmp(s, "->", 2)) {
1068         return xasprintf("%s: missing `->' following source", full_s);
1069     }
1070     s += 2;
1071     error = mf_parse_subfield(&move->dst, s);
1072     if (error) {
1073         return error;
1074     }
1075
1076     if (move->src.n_bits != move->dst.n_bits) {
1077         return xasprintf("%s: source field is %d bits wide but destination is "
1078                          "%d bits wide", full_s,
1079                          move->src.n_bits, move->dst.n_bits);
1080     }
1081     return NULL;
1082 }
1083
1084 /* Parses 's' as a "load" action, in the form described in ovs-ofctl(8), into
1085  * '*load'.
1086  *
1087  * Returns NULL if successful, otherwise a malloc()'d string describing the
1088  * error.  The caller is responsible for freeing the returned string. */
1089 char * WARN_UNUSED_RESULT
1090 nxm_parse_reg_load(struct ofpact_reg_load *load, const char *s)
1091 {
1092     const char *full_s = s;
1093     uint64_t value = strtoull(s, (char **) &s, 0);
1094     char *error;
1095
1096     if (strncmp(s, "->", 2)) {
1097         return xasprintf("%s: missing `->' following value", full_s);
1098     }
1099     s += 2;
1100     error = mf_parse_subfield(&load->dst, s);
1101     if (error) {
1102         return error;
1103     }
1104
1105     if (load->dst.n_bits < 64 && (value >> load->dst.n_bits) != 0) {
1106         return xasprintf("%s: value %"PRIu64" does not fit into %d bits",
1107                          full_s, value, load->dst.n_bits);
1108     }
1109
1110     load->subvalue.be64[0] = htonll(0);
1111     load->subvalue.be64[1] = htonll(value);
1112     return NULL;
1113 }
1114 \f
1115 /* nxm_format_reg_move(), nxm_format_reg_load(). */
1116
1117 void
1118 nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s)
1119 {
1120     ds_put_format(s, "move:");
1121     mf_format_subfield(&move->src, s);
1122     ds_put_cstr(s, "->");
1123     mf_format_subfield(&move->dst, s);
1124 }
1125
1126 void
1127 nxm_format_reg_load(const struct ofpact_reg_load *load, struct ds *s)
1128 {
1129     ds_put_cstr(s, "load:");
1130     mf_format_subvalue(&load->subvalue, s);
1131     ds_put_cstr(s, "->");
1132     mf_format_subfield(&load->dst, s);
1133 }
1134 \f
1135 enum ofperr
1136 nxm_reg_move_check(const struct ofpact_reg_move *move, const struct flow *flow)
1137 {
1138     enum ofperr error;
1139
1140     error = mf_check_src(&move->src, flow);
1141     if (error) {
1142         return error;
1143     }
1144
1145     return mf_check_dst(&move->dst, NULL);
1146 }
1147
1148 enum ofperr
1149 nxm_reg_load_check(const struct ofpact_reg_load *load, const struct flow *flow)
1150 {
1151     return mf_check_dst(&load->dst, flow);
1152 }
1153 \f
1154 \f
1155 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
1156
1157 void
1158 nxm_execute_reg_move(const struct ofpact_reg_move *move,
1159                      struct flow *flow, struct flow_wildcards *wc)
1160 {
1161     union mf_value src_value;
1162     union mf_value dst_value;
1163
1164     mf_mask_field_and_prereqs(move->dst.field, &wc->masks);
1165     mf_mask_field_and_prereqs(move->src.field, &wc->masks);
1166
1167     mf_get_value(move->dst.field, flow, &dst_value);
1168     mf_get_value(move->src.field, flow, &src_value);
1169     bitwise_copy(&src_value, move->src.field->n_bytes, move->src.ofs,
1170                  &dst_value, move->dst.field->n_bytes, move->dst.ofs,
1171                  move->src.n_bits);
1172     mf_set_flow_value(move->dst.field, &dst_value, flow);
1173 }
1174
1175 void
1176 nxm_execute_reg_load(const struct ofpact_reg_load *load, struct flow *flow,
1177                      struct flow_wildcards *wc)
1178 {
1179     /* Since at the datapath interface we do not have set actions for
1180      * individual fields, but larger sets of fields for a given protocol
1181      * layer, the set action will in practice only ever apply to exactly
1182      * matched flows for the given protocol layer.  For example, if the
1183      * reg_load changes the IP TTL, the corresponding datapath action will
1184      * rewrite also the IP addresses and TOS byte.  Since these other field
1185      * values may not be explicitly set, they depend on the incoming flow field
1186      * values, and are hence all of them are set in the wildcards masks, when
1187      * the action is committed to the datapath.  For the rare case, where the
1188      * reg_load action does not actually change the value, and no other flow
1189      * field values are set (or loaded), the datapath action is skipped, and
1190      * no mask bits are set.  Such a datapath flow should, however, be
1191      * dependent on the specific field value, so the corresponding wildcard
1192      * mask bits must be set, lest the datapath flow be applied to packets
1193      * containing some other value in the field and the field value remain
1194      * unchanged regardless of the incoming value.
1195      *
1196      * We set the masks here for the whole fields, and their prerequisities.
1197      * Even if only the lower byte of a TCP destination port is set,
1198      * we set the mask for the whole field, and also the ip_proto in the IP
1199      * header, so that the kernel flow would not be applied on, e.g., a UDP
1200      * packet, or any other IP protocol in addition to TCP packets.
1201      */
1202     mf_mask_field_and_prereqs(load->dst.field, &wc->masks);
1203     mf_write_subfield_flow(&load->dst, &load->subvalue, flow);
1204 }
1205
1206 void
1207 nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data,
1208              struct flow *flow, struct flow_wildcards *wc)
1209 {
1210     union mf_subvalue src_subvalue;
1211     union mf_subvalue mask_value;
1212     ovs_be64 src_data_be = htonll(src_data);
1213
1214     memset(&mask_value, 0xff, sizeof mask_value);
1215     mf_write_subfield_flow(dst, &mask_value, &wc->masks);
1216
1217     bitwise_copy(&src_data_be, sizeof src_data_be, 0,
1218                  &src_subvalue, sizeof src_subvalue, 0,
1219                  sizeof src_data_be * 8);
1220     mf_write_subfield_flow(dst, &src_subvalue, flow);
1221 }
1222 \f
1223 /* nxm_parse_stack_action, works for both push() and pop(). */
1224
1225 /* Parses 's' as a "push" or "pop" action, in the form described in
1226  * ovs-ofctl(8), into '*stack_action'.
1227  *
1228  * Returns NULL if successful, otherwise a malloc()'d string describing the
1229  * error.  The caller is responsible for freeing the returned string. */
1230 char * WARN_UNUSED_RESULT
1231 nxm_parse_stack_action(struct ofpact_stack *stack_action, const char *s)
1232 {
1233     char *error;
1234
1235     error = mf_parse_subfield__(&stack_action->subfield, &s);
1236     if (error) {
1237         return error;
1238     }
1239
1240     if (*s != '\0') {
1241         return xasprintf("%s: trailing garbage following push or pop", s);
1242     }
1243
1244     return NULL;
1245 }
1246
1247 void
1248 nxm_format_stack_push(const struct ofpact_stack *push, struct ds *s)
1249 {
1250     ds_put_cstr(s, "push:");
1251     mf_format_subfield(&push->subfield, s);
1252 }
1253
1254 void
1255 nxm_format_stack_pop(const struct ofpact_stack *pop, struct ds *s)
1256 {
1257     ds_put_cstr(s, "pop:");
1258     mf_format_subfield(&pop->subfield, s);
1259 }
1260
1261 enum ofperr
1262 nxm_stack_push_check(const struct ofpact_stack *push,
1263                      const struct flow *flow)
1264 {
1265     return mf_check_src(&push->subfield, flow);
1266 }
1267
1268 enum ofperr
1269 nxm_stack_pop_check(const struct ofpact_stack *pop,
1270                     const struct flow *flow)
1271 {
1272     return mf_check_dst(&pop->subfield, flow);
1273 }
1274
1275 /* nxm_execute_stack_push(), nxm_execute_stack_pop(). */
1276 static void
1277 nx_stack_push(struct ofpbuf *stack, union mf_subvalue *v)
1278 {
1279     ofpbuf_put(stack, v, sizeof *v);
1280 }
1281
1282 static union mf_subvalue *
1283 nx_stack_pop(struct ofpbuf *stack)
1284 {
1285     union mf_subvalue *v = NULL;
1286
1287     if (ofpbuf_size(stack)) {
1288
1289         ofpbuf_set_size(stack, ofpbuf_size(stack) - sizeof *v);
1290         v = (union mf_subvalue *) ofpbuf_tail(stack);
1291     }
1292
1293     return v;
1294 }
1295
1296 void
1297 nxm_execute_stack_push(const struct ofpact_stack *push,
1298                        const struct flow *flow, struct flow_wildcards *wc,
1299                        struct ofpbuf *stack)
1300 {
1301     union mf_subvalue mask_value;
1302     union mf_subvalue dst_value;
1303
1304     memset(&mask_value, 0xff, sizeof mask_value);
1305     mf_write_subfield_flow(&push->subfield, &mask_value, &wc->masks);
1306
1307     mf_read_subfield(&push->subfield, flow, &dst_value);
1308     nx_stack_push(stack, &dst_value);
1309 }
1310
1311 void
1312 nxm_execute_stack_pop(const struct ofpact_stack *pop,
1313                       struct flow *flow, struct flow_wildcards *wc,
1314                       struct ofpbuf *stack)
1315 {
1316     union mf_subvalue *src_value;
1317
1318     src_value = nx_stack_pop(stack);
1319
1320     /* Only pop if stack is not empty. Otherwise, give warning. */
1321     if (src_value) {
1322         union mf_subvalue mask_value;
1323
1324         memset(&mask_value, 0xff, sizeof mask_value);
1325         mf_write_subfield_flow(&pop->subfield, &mask_value, &wc->masks);
1326         mf_write_subfield_flow(&pop->subfield, src_value, flow);
1327     } else {
1328         if (!VLOG_DROP_WARN(&rl)) {
1329             char *flow_str = flow_to_string(flow);
1330             VLOG_WARN_RL(&rl, "Failed to pop from an empty stack. On flow \n"
1331                            " %s", flow_str);
1332             free(flow_str);
1333         }
1334     }
1335 }