]> git.proxmox.com Git - mirror_ovs.git/blob - lib/nx-match.c
openvswitch/types.h: New macros OVS_BE16_MAX, OVS_BE32_MAX, OVS_BE64_MAX.
[mirror_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 {
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 %zu)", match_len, b->size);
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 = b->data;
255 uint8_t *p;
256 uint16_t match_len;
257
258 if (b->size < 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 %zu)", match_len, b->size);
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 ofp11_match
284 * in 'b' with length 'match_len'. 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 * PXM 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 static void
500 nxm_put_ip(struct ofpbuf *b, const struct match *match,
501 uint8_t icmp_proto, uint32_t icmp_type, uint32_t icmp_code,
502 bool oxm)
503 {
504 const struct flow *flow = &match->flow;
505
506 nxm_put_frag(b, match);
507
508 if (match->wc.masks.nw_tos & IP_DSCP_MASK) {
509 if (oxm) {
510 nxm_put_8(b, OXM_OF_IP_DSCP, flow->nw_tos >> 2);
511 } else {
512 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & IP_DSCP_MASK);
513 }
514 }
515
516 if (match->wc.masks.nw_tos & IP_ECN_MASK) {
517 nxm_put_8(b, oxm ? OXM_OF_IP_ECN : NXM_NX_IP_ECN,
518 flow->nw_tos & IP_ECN_MASK);
519 }
520
521 if (!oxm && match->wc.masks.nw_ttl) {
522 nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl);
523 }
524
525 if (match->wc.masks.nw_proto) {
526 nxm_put_8(b, oxm ? OXM_OF_IP_PROTO : NXM_OF_IP_PROTO, flow->nw_proto);
527
528 if (flow->nw_proto == IPPROTO_TCP) {
529 nxm_put_16m(b, oxm ? OXM_OF_TCP_SRC : NXM_OF_TCP_SRC,
530 flow->tp_src, match->wc.masks.tp_src);
531 nxm_put_16m(b, oxm ? OXM_OF_TCP_DST : NXM_OF_TCP_DST,
532 flow->tp_dst, match->wc.masks.tp_dst);
533 } else if (flow->nw_proto == IPPROTO_UDP) {
534 nxm_put_16m(b, oxm ? OXM_OF_UDP_SRC : NXM_OF_UDP_SRC,
535 flow->tp_src, match->wc.masks.tp_src);
536 nxm_put_16m(b, oxm ? OXM_OF_UDP_DST : NXM_OF_UDP_DST,
537 flow->tp_dst, match->wc.masks.tp_dst);
538 } else if (flow->nw_proto == IPPROTO_SCTP) {
539 nxm_put_16m(b, OXM_OF_SCTP_SRC, flow->tp_src,
540 match->wc.masks.tp_src);
541 nxm_put_16m(b, OXM_OF_SCTP_DST, flow->tp_dst,
542 match->wc.masks.tp_dst);
543 } else if (flow->nw_proto == icmp_proto) {
544 if (match->wc.masks.tp_src) {
545 nxm_put_8(b, icmp_type, ntohs(flow->tp_src));
546 }
547 if (match->wc.masks.tp_dst) {
548 nxm_put_8(b, icmp_code, ntohs(flow->tp_dst));
549 }
550 }
551 }
552 }
553
554 /* Appends to 'b' the nx_match format that expresses 'match'. For Flow Mod and
555 * Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
556 * Otherwise, 'cookie_mask' should be zero.
557 *
558 * This function can cause 'b''s data to be reallocated.
559 *
560 * Returns the number of bytes appended to 'b', excluding padding.
561 *
562 * If 'match' is a catch-all rule that matches every packet, then this function
563 * appends nothing to 'b' and returns 0. */
564 static int
565 nx_put_raw(struct ofpbuf *b, bool oxm, const struct match *match,
566 ovs_be64 cookie, ovs_be64 cookie_mask)
567 {
568 const struct flow *flow = &match->flow;
569 const size_t start_len = b->size;
570 int match_len;
571 int i;
572
573 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 20);
574
575 /* Metadata. */
576 if (match->wc.masks.in_port.ofp_port) {
577 ofp_port_t in_port = flow->in_port.ofp_port;
578 if (oxm) {
579 nxm_put_32(b, OXM_OF_IN_PORT, ofputil_port_to_ofp11(in_port));
580 } else {
581 nxm_put_16(b, NXM_OF_IN_PORT, htons(ofp_to_u16(in_port)));
582 }
583 }
584
585 /* Ethernet. */
586 nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_SRC : NXM_OF_ETH_SRC,
587 flow->dl_src, match->wc.masks.dl_src);
588 nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_DST : NXM_OF_ETH_DST,
589 flow->dl_dst, match->wc.masks.dl_dst);
590 nxm_put_16m(b, oxm ? OXM_OF_ETH_TYPE : NXM_OF_ETH_TYPE,
591 ofputil_dl_type_to_openflow(flow->dl_type),
592 match->wc.masks.dl_type);
593
594 /* 802.1Q. */
595 if (oxm) {
596 ovs_be16 VID_CFI_MASK = htons(VLAN_VID_MASK | VLAN_CFI);
597 ovs_be16 vid = flow->vlan_tci & VID_CFI_MASK;
598 ovs_be16 mask = match->wc.masks.vlan_tci & VID_CFI_MASK;
599
600 if (mask == htons(VLAN_VID_MASK | VLAN_CFI)) {
601 nxm_put_16(b, OXM_OF_VLAN_VID, vid);
602 } else if (mask) {
603 nxm_put_16m(b, OXM_OF_VLAN_VID, vid, mask);
604 }
605
606 if (vid && vlan_tci_to_pcp(match->wc.masks.vlan_tci)) {
607 nxm_put_8(b, OXM_OF_VLAN_PCP, vlan_tci_to_pcp(flow->vlan_tci));
608 }
609
610 } else {
611 nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci,
612 match->wc.masks.vlan_tci);
613 }
614
615 /* MPLS. */
616 if (eth_type_mpls(flow->dl_type)) {
617 if (match->wc.masks.mpls_lse & htonl(MPLS_TC_MASK)) {
618 nxm_put_8(b, OXM_OF_MPLS_TC, mpls_lse_to_tc(flow->mpls_lse));
619 }
620
621 if (match->wc.masks.mpls_lse & htonl(MPLS_BOS_MASK)) {
622 nxm_put_8(b, OXM_OF_MPLS_BOS, mpls_lse_to_bos(flow->mpls_lse));
623 }
624
625 if (match->wc.masks.mpls_lse & htonl(MPLS_LABEL_MASK)) {
626 nxm_put_32(b, OXM_OF_MPLS_LABEL,
627 htonl(mpls_lse_to_label(flow->mpls_lse)));
628 }
629 }
630
631 /* L3. */
632 if (flow->dl_type == htons(ETH_TYPE_IP)) {
633 /* IP. */
634 nxm_put_32m(b, oxm ? OXM_OF_IPV4_SRC : NXM_OF_IP_SRC,
635 flow->nw_src, match->wc.masks.nw_src);
636 nxm_put_32m(b, oxm ? OXM_OF_IPV4_DST : NXM_OF_IP_DST,
637 flow->nw_dst, match->wc.masks.nw_dst);
638 nxm_put_ip(b, match, IPPROTO_ICMP,
639 oxm ? OXM_OF_ICMPV4_TYPE : NXM_OF_ICMP_TYPE,
640 oxm ? OXM_OF_ICMPV4_CODE : NXM_OF_ICMP_CODE, oxm);
641 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
642 /* IPv6. */
643 nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_SRC : NXM_NX_IPV6_SRC,
644 &flow->ipv6_src, &match->wc.masks.ipv6_src);
645 nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_DST : NXM_NX_IPV6_DST,
646 &flow->ipv6_dst, &match->wc.masks.ipv6_dst);
647 nxm_put_ip(b, match, IPPROTO_ICMPV6,
648 oxm ? OXM_OF_ICMPV6_TYPE : NXM_NX_ICMPV6_TYPE,
649 oxm ? OXM_OF_ICMPV6_CODE : NXM_NX_ICMPV6_CODE, oxm);
650
651 nxm_put_32m(b, oxm ? OXM_OF_IPV6_FLABEL : NXM_NX_IPV6_LABEL,
652 flow->ipv6_label, match->wc.masks.ipv6_label);
653
654 if (flow->nw_proto == IPPROTO_ICMPV6
655 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
656 flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
657 nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_ND_TARGET : NXM_NX_ND_TARGET,
658 &flow->nd_target, &match->wc.masks.nd_target);
659 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
660 nxm_put_eth_masked(b, oxm ? OXM_OF_IPV6_ND_SLL : NXM_NX_ND_SLL,
661 flow->arp_sha, match->wc.masks.arp_sha);
662 }
663 if (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
664 nxm_put_eth_masked(b, oxm ? OXM_OF_IPV6_ND_TLL : NXM_NX_ND_TLL,
665 flow->arp_tha, match->wc.masks.arp_tha);
666 }
667 }
668 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
669 flow->dl_type == htons(ETH_TYPE_RARP)) {
670 /* ARP. */
671 if (match->wc.masks.nw_proto) {
672 nxm_put_16(b, oxm ? OXM_OF_ARP_OP : NXM_OF_ARP_OP,
673 htons(flow->nw_proto));
674 }
675 nxm_put_32m(b, oxm ? OXM_OF_ARP_SPA : NXM_OF_ARP_SPA,
676 flow->nw_src, match->wc.masks.nw_src);
677 nxm_put_32m(b, oxm ? OXM_OF_ARP_TPA : NXM_OF_ARP_TPA,
678 flow->nw_dst, match->wc.masks.nw_dst);
679 nxm_put_eth_masked(b, oxm ? OXM_OF_ARP_SHA : NXM_NX_ARP_SHA,
680 flow->arp_sha, match->wc.masks.arp_sha);
681 nxm_put_eth_masked(b, oxm ? OXM_OF_ARP_THA : NXM_NX_ARP_THA,
682 flow->arp_tha, match->wc.masks.arp_tha);
683 }
684
685 /* Tunnel ID. */
686 nxm_put_64m(b, oxm ? OXM_OF_TUNNEL_ID : NXM_NX_TUN_ID,
687 flow->tunnel.tun_id, match->wc.masks.tunnel.tun_id);
688
689 /* Other tunnel metadata. */
690 nxm_put_32m(b, NXM_NX_TUN_IPV4_SRC,
691 flow->tunnel.ip_src, match->wc.masks.tunnel.ip_src);
692 nxm_put_32m(b, NXM_NX_TUN_IPV4_DST,
693 flow->tunnel.ip_dst, match->wc.masks.tunnel.ip_dst);
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 /* Mark. */
702 nxm_put_32m(b, NXM_NX_PKT_MARK, htonl(flow->pkt_mark),
703 htonl(match->wc.masks.pkt_mark));
704
705 /* OpenFlow 1.1+ Metadata. */
706 nxm_put_64m(b, OXM_OF_METADATA, flow->metadata, match->wc.masks.metadata);
707
708 /* Cookie. */
709 nxm_put_64m(b, NXM_NX_COOKIE, cookie, cookie_mask);
710
711 match_len = b->size - start_len;
712 return match_len;
713 }
714
715 /* Appends to 'b' the nx_match format that expresses 'match', plus enough zero
716 * bytes to pad the nx_match out to a multiple of 8. For Flow Mod and Flow
717 * Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
718 * Otherwise, 'cookie_mask' should be zero.
719 *
720 * This function can cause 'b''s data to be reallocated.
721 *
722 * Returns the number of bytes appended to 'b', excluding padding. The return
723 * value can be zero if it appended nothing at all to 'b' (which happens if
724 * 'cr' is a catch-all rule that matches every packet). */
725 int
726 nx_put_match(struct ofpbuf *b, const struct match *match,
727 ovs_be64 cookie, ovs_be64 cookie_mask)
728 {
729 int match_len = nx_put_raw(b, false, match, cookie, cookie_mask);
730
731 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
732 return match_len;
733 }
734
735
736 /* Appends to 'b' an struct ofp11_match_header followed by the oxm format that
737 * expresses 'cr', plus enough zero bytes to pad the data appended out to a
738 * multiple of 8.
739 *
740 * This function can cause 'b''s data to be reallocated.
741 *
742 * Returns the number of bytes appended to 'b', excluding the padding. Never
743 * returns zero. */
744 int
745 oxm_put_match(struct ofpbuf *b, const struct match *match)
746 {
747 int match_len;
748 struct ofp11_match_header *omh;
749 size_t start_len = b->size;
750 ovs_be64 cookie = htonll(0), cookie_mask = htonll(0);
751
752 ofpbuf_put_uninit(b, sizeof *omh);
753 match_len = nx_put_raw(b, true, match, cookie, cookie_mask) + sizeof *omh;
754 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
755
756 omh = ofpbuf_at(b, start_len, sizeof *omh);
757 omh->type = htons(OFPMT_OXM);
758 omh->length = htons(match_len);
759
760 return match_len;
761 }
762 \f
763 /* nx_match_to_string() and helpers. */
764
765 static void format_nxm_field_name(struct ds *, uint32_t header);
766
767 char *
768 nx_match_to_string(const uint8_t *p, unsigned int match_len)
769 {
770 uint32_t header;
771 struct ds s;
772
773 if (!match_len) {
774 return xstrdup("<any>");
775 }
776
777 ds_init(&s);
778 while ((header = nx_entry_ok(p, match_len)) != 0) {
779 unsigned int length = NXM_LENGTH(header);
780 unsigned int value_len = nxm_field_bytes(header);
781 const uint8_t *value = p + 4;
782 const uint8_t *mask = value + value_len;
783 unsigned int i;
784
785 if (s.length) {
786 ds_put_cstr(&s, ", ");
787 }
788
789 format_nxm_field_name(&s, header);
790 ds_put_char(&s, '(');
791
792 for (i = 0; i < value_len; i++) {
793 ds_put_format(&s, "%02x", value[i]);
794 }
795 if (NXM_HASMASK(header)) {
796 ds_put_char(&s, '/');
797 for (i = 0; i < value_len; i++) {
798 ds_put_format(&s, "%02x", mask[i]);
799 }
800 }
801 ds_put_char(&s, ')');
802
803 p += 4 + length;
804 match_len -= 4 + length;
805 }
806
807 if (match_len) {
808 if (s.length) {
809 ds_put_cstr(&s, ", ");
810 }
811
812 ds_put_format(&s, "<%u invalid bytes>", match_len);
813 }
814
815 return ds_steal_cstr(&s);
816 }
817
818 char *
819 oxm_match_to_string(const struct ofpbuf *p, unsigned int match_len)
820 {
821 const struct ofp11_match_header *omh = p->data;
822 uint16_t match_len_;
823 struct ds s;
824
825 ds_init(&s);
826
827 if (match_len < sizeof *omh) {
828 ds_put_format(&s, "<match too short: %u>", match_len);
829 goto err;
830 }
831
832 if (omh->type != htons(OFPMT_OXM)) {
833 ds_put_format(&s, "<bad match type field: %u>", ntohs(omh->type));
834 goto err;
835 }
836
837 match_len_ = ntohs(omh->length);
838 if (match_len_ < sizeof *omh) {
839 ds_put_format(&s, "<match length field too short: %u>", match_len_);
840 goto err;
841 }
842
843 if (match_len_ != match_len) {
844 ds_put_format(&s, "<match length field incorrect: %u != %u>",
845 match_len_, match_len);
846 goto err;
847 }
848
849 return nx_match_to_string(ofpbuf_at(p, sizeof *omh, 0),
850 match_len - sizeof *omh);
851
852 err:
853 return ds_steal_cstr(&s);
854 }
855
856 static void
857 format_nxm_field_name(struct ds *s, uint32_t header)
858 {
859 const struct mf_field *mf = mf_from_nxm_header(header);
860 if (mf) {
861 ds_put_cstr(s, IS_OXM_HEADER(header) ? mf->oxm_name : mf->nxm_name);
862 if (NXM_HASMASK(header)) {
863 ds_put_cstr(s, "_W");
864 }
865 } else if (header == NXM_NX_COOKIE) {
866 ds_put_cstr(s, "NXM_NX_COOKIE");
867 } else if (header == NXM_NX_COOKIE_W) {
868 ds_put_cstr(s, "NXM_NX_COOKIE_W");
869 } else {
870 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
871 }
872 }
873
874 static uint32_t
875 parse_nxm_field_name(const char *name, int name_len)
876 {
877 bool wild;
878 int i;
879
880 /* Check whether it's a field name. */
881 wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
882 if (wild) {
883 name_len -= 2;
884 }
885
886 for (i = 0; i < MFF_N_IDS; i++) {
887 const struct mf_field *mf = mf_from_id(i);
888 uint32_t header;
889
890 if (mf->nxm_name &&
891 !strncmp(mf->nxm_name, name, name_len) &&
892 mf->nxm_name[name_len] == '\0') {
893 header = mf->nxm_header;
894 } else if (mf->oxm_name &&
895 !strncmp(mf->oxm_name, name, name_len) &&
896 mf->oxm_name[name_len] == '\0') {
897 header = mf->oxm_header;
898 } else {
899 continue;
900 }
901
902 if (!wild) {
903 return header;
904 } else if (mf->maskable != MFM_NONE) {
905 return NXM_MAKE_WILD_HEADER(header);
906 }
907 }
908
909 if (!strncmp("NXM_NX_COOKIE", name, name_len) &&
910 (name_len == strlen("NXM_NX_COOKIE"))) {
911 if (!wild) {
912 return NXM_NX_COOKIE;
913 } else {
914 return NXM_NX_COOKIE_W;
915 }
916 }
917
918 /* Check whether it's a 32-bit field header value as hex.
919 * (This isn't ordinarily useful except for testing error behavior.) */
920 if (name_len == 8) {
921 uint32_t header = hexits_value(name, name_len, NULL);
922 if (header != UINT_MAX) {
923 return header;
924 }
925 }
926
927 return 0;
928 }
929 \f
930 /* nx_match_from_string(). */
931
932 static int
933 nx_match_from_string_raw(const char *s, struct ofpbuf *b)
934 {
935 const char *full_s = s;
936 const size_t start_len = b->size;
937
938 if (!strcmp(s, "<any>")) {
939 /* Ensure that 'b->data' isn't actually null. */
940 ofpbuf_prealloc_tailroom(b, 1);
941 return 0;
942 }
943
944 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
945 const char *name;
946 uint32_t header;
947 int name_len;
948 size_t n;
949
950 name = s;
951 name_len = strcspn(s, "(");
952 if (s[name_len] != '(') {
953 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
954 }
955
956 header = parse_nxm_field_name(name, name_len);
957 if (!header) {
958 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
959 }
960
961 s += name_len + 1;
962
963 nxm_put_header(b, header);
964 s = ofpbuf_put_hex(b, s, &n);
965 if (n != nxm_field_bytes(header)) {
966 ovs_fatal(0, "%.2s: hex digits expected", s);
967 }
968 if (NXM_HASMASK(header)) {
969 s += strspn(s, " ");
970 if (*s != '/') {
971 ovs_fatal(0, "%s: missing / in masked field %.*s",
972 full_s, name_len, name);
973 }
974 s = ofpbuf_put_hex(b, s + 1, &n);
975 if (n != nxm_field_bytes(header)) {
976 ovs_fatal(0, "%.2s: hex digits expected", s);
977 }
978 }
979
980 s += strspn(s, " ");
981 if (*s != ')') {
982 ovs_fatal(0, "%s: missing ) following field %.*s",
983 full_s, name_len, name);
984 }
985 s++;
986 }
987
988 return b->size - start_len;
989 }
990
991 int
992 nx_match_from_string(const char *s, struct ofpbuf *b)
993 {
994 int match_len = nx_match_from_string_raw(s, b);
995 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
996 return match_len;
997 }
998
999 int
1000 oxm_match_from_string(const char *s, struct ofpbuf *b)
1001 {
1002 int match_len;
1003 struct ofp11_match_header *omh;
1004 size_t start_len = b->size;
1005
1006 ofpbuf_put_uninit(b, sizeof *omh);
1007 match_len = nx_match_from_string_raw(s, b) + sizeof *omh;
1008 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
1009
1010 omh = ofpbuf_at(b, start_len, sizeof *omh);
1011 omh->type = htons(OFPMT_OXM);
1012 omh->length = htons(match_len);
1013
1014 return match_len;
1015 }
1016 \f
1017 /* Parses 's' as a "move" action, in the form described in ovs-ofctl(8), into
1018 * '*move'.
1019 *
1020 * Returns NULL if successful, otherwise a malloc()'d string describing the
1021 * error. The caller is responsible for freeing the returned string. */
1022 char * WARN_UNUSED_RESULT
1023 nxm_parse_reg_move(struct ofpact_reg_move *move, const char *s)
1024 {
1025 const char *full_s = s;
1026 char *error;
1027
1028 error = mf_parse_subfield__(&move->src, &s);
1029 if (error) {
1030 return error;
1031 }
1032 if (strncmp(s, "->", 2)) {
1033 return xasprintf("%s: missing `->' following source", full_s);
1034 }
1035 s += 2;
1036 error = mf_parse_subfield(&move->dst, s);
1037 if (error) {
1038 return error;
1039 }
1040
1041 if (move->src.n_bits != move->dst.n_bits) {
1042 return xasprintf("%s: source field is %d bits wide but destination is "
1043 "%d bits wide", full_s,
1044 move->src.n_bits, move->dst.n_bits);
1045 }
1046 return NULL;
1047 }
1048
1049 /* Parses 's' as a "load" action, in the form described in ovs-ofctl(8), into
1050 * '*load'.
1051 *
1052 * Returns NULL if successful, otherwise a malloc()'d string describing the
1053 * error. The caller is responsible for freeing the returned string. */
1054 char * WARN_UNUSED_RESULT
1055 nxm_parse_reg_load(struct ofpact_reg_load *load, const char *s)
1056 {
1057 const char *full_s = s;
1058 uint64_t value = strtoull(s, (char **) &s, 0);
1059 char *error;
1060
1061 if (strncmp(s, "->", 2)) {
1062 return xasprintf("%s: missing `->' following value", full_s);
1063 }
1064 s += 2;
1065 error = mf_parse_subfield(&load->dst, s);
1066 if (error) {
1067 return error;
1068 }
1069
1070 if (load->dst.n_bits < 64 && (value >> load->dst.n_bits) != 0) {
1071 return xasprintf("%s: value %"PRIu64" does not fit into %d bits",
1072 full_s, value, load->dst.n_bits);
1073 }
1074
1075 load->subvalue.be64[0] = htonll(0);
1076 load->subvalue.be64[1] = htonll(value);
1077 return NULL;
1078 }
1079 \f
1080 /* nxm_format_reg_move(), nxm_format_reg_load(). */
1081
1082 void
1083 nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s)
1084 {
1085 ds_put_format(s, "move:");
1086 mf_format_subfield(&move->src, s);
1087 ds_put_cstr(s, "->");
1088 mf_format_subfield(&move->dst, s);
1089 }
1090
1091 static void
1092 set_field_format(const struct ofpact_reg_load *load, struct ds *s)
1093 {
1094 const struct mf_field *mf = load->dst.field;
1095 union mf_value value;
1096
1097 ovs_assert(load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD);
1098 ds_put_format(s, "set_field:");
1099 memset(&value, 0, sizeof value);
1100 bitwise_copy(&load->subvalue, sizeof load->subvalue, 0,
1101 &value, mf->n_bytes, 0, load->dst.n_bits);
1102 mf_format(mf, &value, NULL, s);
1103 ds_put_format(s, "->%s", mf->name);
1104 }
1105
1106 static void
1107 load_format(const struct ofpact_reg_load *load, struct ds *s)
1108 {
1109 ds_put_cstr(s, "load:");
1110 mf_format_subvalue(&load->subvalue, s);
1111 ds_put_cstr(s, "->");
1112 mf_format_subfield(&load->dst, s);
1113 }
1114
1115 void
1116 nxm_format_reg_load(const struct ofpact_reg_load *load, struct ds *s)
1117 {
1118 if (load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD) {
1119 set_field_format(load, s);
1120 } else {
1121 load_format(load, s);
1122 }
1123 }
1124 \f
1125 enum ofperr
1126 nxm_reg_move_from_openflow(const struct nx_action_reg_move *narm,
1127 struct ofpbuf *ofpacts)
1128 {
1129 struct ofpact_reg_move *move;
1130
1131 move = ofpact_put_REG_MOVE(ofpacts);
1132 move->src.field = mf_from_nxm_header(ntohl(narm->src));
1133 move->src.ofs = ntohs(narm->src_ofs);
1134 move->src.n_bits = ntohs(narm->n_bits);
1135 move->dst.field = mf_from_nxm_header(ntohl(narm->dst));
1136 move->dst.ofs = ntohs(narm->dst_ofs);
1137 move->dst.n_bits = ntohs(narm->n_bits);
1138
1139 return nxm_reg_move_check(move, NULL);
1140 }
1141
1142 enum ofperr
1143 nxm_reg_load_from_openflow(const struct nx_action_reg_load *narl,
1144 struct ofpbuf *ofpacts)
1145 {
1146 struct ofpact_reg_load *load;
1147
1148 load = ofpact_put_REG_LOAD(ofpacts);
1149 load->dst.field = mf_from_nxm_header(ntohl(narl->dst));
1150 load->dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
1151 load->dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
1152 load->subvalue.be64[1] = narl->value;
1153
1154 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
1155 * narl->value. */
1156 if (load->dst.n_bits < 64 &&
1157 ntohll(narl->value) >> load->dst.n_bits) {
1158 return OFPERR_OFPBAC_BAD_ARGUMENT;
1159 }
1160
1161 return nxm_reg_load_check(load, NULL);
1162 }
1163
1164 enum ofperr
1165 nxm_reg_load_from_openflow12_set_field(
1166 const struct ofp12_action_set_field * oasf, struct ofpbuf *ofpacts)
1167 {
1168 uint16_t oasf_len = ntohs(oasf->len);
1169 uint32_t oxm_header = ntohl(oasf->dst);
1170 uint8_t oxm_length = NXM_LENGTH(oxm_header);
1171 struct ofpact_reg_load *load;
1172 const struct mf_field *mf;
1173
1174 /* ofp12_action_set_field is padded to 64 bits by zero */
1175 if (oasf_len != ROUND_UP(sizeof(*oasf) + oxm_length, 8)) {
1176 return OFPERR_OFPBAC_BAD_SET_LEN;
1177 }
1178 if (!is_all_zeros((const uint8_t *)(oasf) + sizeof *oasf + oxm_length,
1179 oasf_len - oxm_length - sizeof *oasf)) {
1180 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1181 }
1182
1183 if (NXM_HASMASK(oxm_header)) {
1184 return OFPERR_OFPBAC_BAD_SET_TYPE;
1185 }
1186 mf = mf_from_nxm_header(oxm_header);
1187 if (!mf) {
1188 return OFPERR_OFPBAC_BAD_SET_TYPE;
1189 }
1190 load = ofpact_put_REG_LOAD(ofpacts);
1191 ofpact_set_field_init(load, mf, oasf + 1);
1192
1193 return nxm_reg_load_check(load, NULL);
1194 }
1195 \f
1196 enum ofperr
1197 nxm_reg_move_check(const struct ofpact_reg_move *move, const struct flow *flow)
1198 {
1199 enum ofperr error;
1200
1201 error = mf_check_src(&move->src, flow);
1202 if (error) {
1203 return error;
1204 }
1205
1206 return mf_check_dst(&move->dst, NULL);
1207 }
1208
1209 enum ofperr
1210 nxm_reg_load_check(const struct ofpact_reg_load *load, const struct flow *flow)
1211 {
1212 return mf_check_dst(&load->dst, flow);
1213 }
1214 \f
1215 void
1216 nxm_reg_move_to_nxast(const struct ofpact_reg_move *move,
1217 struct ofpbuf *openflow)
1218 {
1219 struct nx_action_reg_move *narm;
1220
1221 narm = ofputil_put_NXAST_REG_MOVE(openflow);
1222 narm->n_bits = htons(move->dst.n_bits);
1223 narm->src_ofs = htons(move->src.ofs);
1224 narm->dst_ofs = htons(move->dst.ofs);
1225 narm->src = htonl(move->src.field->nxm_header);
1226 narm->dst = htonl(move->dst.field->nxm_header);
1227 }
1228
1229 static void
1230 reg_load_to_nxast(const struct ofpact_reg_load *load, struct ofpbuf *openflow)
1231 {
1232 struct nx_action_reg_load *narl;
1233
1234 narl = ofputil_put_NXAST_REG_LOAD(openflow);
1235 narl->ofs_nbits = nxm_encode_ofs_nbits(load->dst.ofs, load->dst.n_bits);
1236 narl->dst = htonl(load->dst.field->nxm_header);
1237 narl->value = load->subvalue.be64[1];
1238 }
1239
1240 static void
1241 set_field_to_ofast(const struct ofpact_reg_load *load,
1242 struct ofpbuf *openflow)
1243 {
1244 const struct mf_field *mf = load->dst.field;
1245 uint16_t padded_value_len = ROUND_UP(mf->n_bytes, 8);
1246 struct ofp12_action_set_field *oasf;
1247 char *value;
1248
1249 /* Set field is the only action of variable length (so far),
1250 * so handling the variable length portion is open-coded here */
1251 oasf = ofputil_put_OFPAT12_SET_FIELD(openflow);
1252 oasf->dst = htonl(mf->oxm_header);
1253 oasf->len = htons(ntohs(oasf->len) + padded_value_len);
1254
1255 value = ofpbuf_put_zeros(openflow, padded_value_len);
1256 bitwise_copy(&load->subvalue, sizeof load->subvalue, load->dst.ofs,
1257 value, mf->n_bytes, load->dst.ofs, load->dst.n_bits);
1258 }
1259
1260 void
1261 nxm_reg_load_to_nxast(const struct ofpact_reg_load *load,
1262 struct ofpbuf *openflow)
1263 {
1264
1265 if (load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD) {
1266 struct ofp_header *oh = (struct ofp_header *)openflow->l2;
1267
1268 switch(oh->version) {
1269 case OFP13_VERSION:
1270 case OFP12_VERSION:
1271 set_field_to_ofast(load, openflow);
1272 break;
1273
1274 case OFP11_VERSION:
1275 case OFP10_VERSION:
1276 if (load->dst.n_bits < 64) {
1277 reg_load_to_nxast(load, openflow);
1278 } else {
1279 /* Split into 64bit chunks */
1280 int chunk, ofs;
1281 for (ofs = 0; ofs < load->dst.n_bits; ofs += chunk) {
1282 struct ofpact_reg_load subload = *load;
1283
1284 chunk = MIN(load->dst.n_bits - ofs, 64);
1285
1286 subload.dst.field = load->dst.field;
1287 subload.dst.ofs = load->dst.ofs + ofs;
1288 subload.dst.n_bits = chunk;
1289 bitwise_copy(&load->subvalue, sizeof load->subvalue, ofs,
1290 &subload.subvalue, sizeof subload.subvalue, 0,
1291 chunk);
1292 reg_load_to_nxast(&subload, openflow);
1293 }
1294 }
1295 break;
1296
1297 default:
1298 NOT_REACHED();
1299 }
1300 } else {
1301 reg_load_to_nxast(load, openflow);
1302 }
1303 }
1304 \f
1305 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
1306
1307 void
1308 nxm_execute_reg_move(const struct ofpact_reg_move *move,
1309 struct flow *flow, struct flow_wildcards *wc)
1310 {
1311 union mf_subvalue mask_value;
1312 union mf_value src_value;
1313 union mf_value dst_value;
1314
1315 memset(&mask_value, 0xff, sizeof mask_value);
1316 mf_write_subfield_flow(&move->dst, &mask_value, &wc->masks);
1317 mf_write_subfield_flow(&move->src, &mask_value, &wc->masks);
1318
1319 mf_get_value(move->dst.field, flow, &dst_value);
1320 mf_get_value(move->src.field, flow, &src_value);
1321 bitwise_copy(&src_value, move->src.field->n_bytes, move->src.ofs,
1322 &dst_value, move->dst.field->n_bytes, move->dst.ofs,
1323 move->src.n_bits);
1324 mf_set_flow_value(move->dst.field, &dst_value, flow);
1325 }
1326
1327 void
1328 nxm_execute_reg_load(const struct ofpact_reg_load *load, struct flow *flow)
1329 {
1330 mf_write_subfield_flow(&load->dst, &load->subvalue, flow);
1331 }
1332
1333 void
1334 nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data,
1335 struct flow *flow, struct flow_wildcards *wc)
1336 {
1337 union mf_subvalue src_subvalue;
1338 union mf_subvalue mask_value;
1339 ovs_be64 src_data_be = htonll(src_data);
1340
1341 memset(&mask_value, 0xff, sizeof mask_value);
1342 mf_write_subfield_flow(dst, &mask_value, &wc->masks);
1343
1344 bitwise_copy(&src_data_be, sizeof src_data_be, 0,
1345 &src_subvalue, sizeof src_subvalue, 0,
1346 sizeof src_data_be * 8);
1347 mf_write_subfield_flow(dst, &src_subvalue, flow);
1348 }
1349 \f
1350 /* nxm_parse_stack_action, works for both push() and pop(). */
1351
1352 /* Parses 's' as a "push" or "pop" action, in the form described in
1353 * ovs-ofctl(8), into '*stack_action'.
1354 *
1355 * Returns NULL if successful, otherwise a malloc()'d string describing the
1356 * error. The caller is responsible for freeing the returned string. */
1357 char * WARN_UNUSED_RESULT
1358 nxm_parse_stack_action(struct ofpact_stack *stack_action, const char *s)
1359 {
1360 char *error;
1361
1362 error = mf_parse_subfield__(&stack_action->subfield, &s);
1363 if (error) {
1364 return error;
1365 }
1366
1367 if (*s != '\0') {
1368 return xasprintf("%s: trailing garbage following push or pop", s);
1369 }
1370
1371 return NULL;
1372 }
1373
1374 void
1375 nxm_format_stack_push(const struct ofpact_stack *push, struct ds *s)
1376 {
1377 ds_put_cstr(s, "push:");
1378 mf_format_subfield(&push->subfield, s);
1379 }
1380
1381 void
1382 nxm_format_stack_pop(const struct ofpact_stack *pop, struct ds *s)
1383 {
1384 ds_put_cstr(s, "pop:");
1385 mf_format_subfield(&pop->subfield, s);
1386 }
1387
1388 /* Common set for both push and pop actions. */
1389 static void
1390 stack_action_from_openflow__(const struct nx_action_stack *nasp,
1391 struct ofpact_stack *stack_action)
1392 {
1393 stack_action->subfield.field = mf_from_nxm_header(ntohl(nasp->field));
1394 stack_action->subfield.ofs = ntohs(nasp->offset);
1395 stack_action->subfield.n_bits = ntohs(nasp->n_bits);
1396 }
1397
1398 static void
1399 nxm_stack_to_nxast__(const struct ofpact_stack *stack_action,
1400 struct nx_action_stack *nasp)
1401 {
1402 nasp->offset = htons(stack_action->subfield.ofs);
1403 nasp->n_bits = htons(stack_action->subfield.n_bits);
1404 nasp->field = htonl(stack_action->subfield.field->nxm_header);
1405 }
1406
1407 enum ofperr
1408 nxm_stack_push_from_openflow(const struct nx_action_stack *nasp,
1409 struct ofpbuf *ofpacts)
1410 {
1411 struct ofpact_stack *push;
1412
1413 push = ofpact_put_STACK_PUSH(ofpacts);
1414 stack_action_from_openflow__(nasp, push);
1415
1416 return nxm_stack_push_check(push, NULL);
1417 }
1418
1419 enum ofperr
1420 nxm_stack_pop_from_openflow(const struct nx_action_stack *nasp,
1421 struct ofpbuf *ofpacts)
1422 {
1423 struct ofpact_stack *pop;
1424
1425 pop = ofpact_put_STACK_POP(ofpacts);
1426 stack_action_from_openflow__(nasp, pop);
1427
1428 return nxm_stack_pop_check(pop, NULL);
1429 }
1430
1431 enum ofperr
1432 nxm_stack_push_check(const struct ofpact_stack *push,
1433 const struct flow *flow)
1434 {
1435 return mf_check_src(&push->subfield, flow);
1436 }
1437
1438 enum ofperr
1439 nxm_stack_pop_check(const struct ofpact_stack *pop,
1440 const struct flow *flow)
1441 {
1442 return mf_check_dst(&pop->subfield, flow);
1443 }
1444
1445 void
1446 nxm_stack_push_to_nxast(const struct ofpact_stack *stack,
1447 struct ofpbuf *openflow)
1448 {
1449 nxm_stack_to_nxast__(stack, ofputil_put_NXAST_STACK_PUSH(openflow));
1450 }
1451
1452 void
1453 nxm_stack_pop_to_nxast(const struct ofpact_stack *stack,
1454 struct ofpbuf *openflow)
1455 {
1456 nxm_stack_to_nxast__(stack, ofputil_put_NXAST_STACK_POP(openflow));
1457 }
1458
1459 /* nxm_execute_stack_push(), nxm_execute_stack_pop(). */
1460 static void
1461 nx_stack_push(struct ofpbuf *stack, union mf_subvalue *v)
1462 {
1463 ofpbuf_put(stack, v, sizeof *v);
1464 }
1465
1466 static union mf_subvalue *
1467 nx_stack_pop(struct ofpbuf *stack)
1468 {
1469 union mf_subvalue *v = NULL;
1470
1471 if (stack->size) {
1472 stack->size -= sizeof *v;
1473 v = (union mf_subvalue *) ofpbuf_tail(stack);
1474 }
1475
1476 return v;
1477 }
1478
1479 void
1480 nxm_execute_stack_push(const struct ofpact_stack *push,
1481 const struct flow *flow, struct flow_wildcards *wc,
1482 struct ofpbuf *stack)
1483 {
1484 union mf_subvalue mask_value;
1485 union mf_subvalue dst_value;
1486
1487 memset(&mask_value, 0xff, sizeof mask_value);
1488 mf_write_subfield_flow(&push->subfield, &mask_value, &wc->masks);
1489
1490 mf_read_subfield(&push->subfield, flow, &dst_value);
1491 nx_stack_push(stack, &dst_value);
1492 }
1493
1494 void
1495 nxm_execute_stack_pop(const struct ofpact_stack *pop,
1496 struct flow *flow, struct flow_wildcards *wc,
1497 struct ofpbuf *stack)
1498 {
1499 union mf_subvalue *src_value;
1500
1501 src_value = nx_stack_pop(stack);
1502
1503 /* Only pop if stack is not empty. Otherwise, give warning. */
1504 if (src_value) {
1505 union mf_subvalue mask_value;
1506
1507 memset(&mask_value, 0xff, sizeof mask_value);
1508 mf_write_subfield_flow(&pop->subfield, &mask_value, &wc->masks);
1509 mf_write_subfield_flow(&pop->subfield, src_value, flow);
1510 } else {
1511 if (!VLOG_DROP_WARN(&rl)) {
1512 char *flow_str = flow_to_string(flow);
1513 VLOG_WARN_RL(&rl, "Failed to pop from an empty stack. On flow \n"
1514 " %s", flow_str);
1515 free(flow_str);
1516 }
1517 }
1518 }