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