]>
Commit | Line | Data |
---|---|---|
09246b99 | 1 | /* |
1638b906 | 2 | * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc. |
09246b99 BP |
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 | ||
685a51a5 JP |
21 | #include <netinet/icmp6.h> |
22 | ||
09246b99 BP |
23 | #include "classifier.h" |
24 | #include "dynamic-string.h" | |
6a885fd0 | 25 | #include "meta-flow.h" |
f25d0cf3 | 26 | #include "ofp-actions.h" |
90bf1e07 | 27 | #include "ofp-errors.h" |
09246b99 BP |
28 | #include "ofp-util.h" |
29 | #include "ofpbuf.h" | |
30 | #include "openflow/nicira-ext.h" | |
31 | #include "packets.h" | |
32 | #include "unaligned.h" | |
ddc4f8e2 | 33 | #include "util.h" |
09246b99 BP |
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 | ||
09246b99 BP |
42 | /* Returns the width of the data for a field with the given 'header', in |
43 | * bytes. */ | |
4291acd2 | 44 | int |
09246b99 BP |
45 | nxm_field_bytes(uint32_t header) |
46 | { | |
47 | unsigned int length = NXM_LENGTH(header); | |
48 | return NXM_HASMASK(header) ? length / 2 : length; | |
49 | } | |
b6c9e612 BP |
50 | |
51 | /* Returns the width of the data for a field with the given 'header', in | |
52 | * bits. */ | |
4291acd2 | 53 | int |
b6c9e612 BP |
54 | nxm_field_bits(uint32_t header) |
55 | { | |
56 | return nxm_field_bytes(header) * 8; | |
57 | } | |
6a885fd0 BP |
58 | \f |
59 | /* nx_pull_match() and helpers. */ | |
09246b99 BP |
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) { | |
74f868c6 BP |
70 | VLOG_DBG_RL(&rl, "nx_match ends with partial (%u-byte) nxm_header", |
71 | match_len); | |
09246b99 BP |
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 | ||
e1cfc4e4 BP |
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 | ||
90bf1e07 | 118 | static enum ofperr |
7623f4dd | 119 | nx_pull_raw(const uint8_t *p, unsigned int match_len, bool strict, |
81a76618 | 120 | struct match *match, ovs_be64 *cookie, ovs_be64 *cookie_mask) |
09246b99 BP |
121 | { |
122 | uint32_t header; | |
09246b99 | 123 | |
cb22974d | 124 | ovs_assert((cookie != NULL) == (cookie_mask != NULL)); |
e729e793 | 125 | |
81a76618 | 126 | match_init_catchall(match); |
a3a0c29e BP |
127 | if (cookie) { |
128 | *cookie = *cookie_mask = htonll(0); | |
129 | } | |
130 | if (!match_len) { | |
131 | return 0; | |
132 | } | |
133 | ||
102ce766 EJ |
134 | for (; |
135 | (header = nx_entry_ok(p, match_len)) != 0; | |
136 | p += 4 + NXM_LENGTH(header), match_len -= 4 + NXM_LENGTH(header)) { | |
28da1f8f | 137 | const struct mf_field *mf; |
90bf1e07 | 138 | enum ofperr error; |
09246b99 | 139 | |
28da1f8f BP |
140 | mf = mf_from_nxm_header(header); |
141 | if (!mf) { | |
102ce766 | 142 | if (strict) { |
2e0525bc | 143 | error = OFPERR_OFPBMC_BAD_FIELD; |
102ce766 EJ |
144 | } else { |
145 | continue; | |
146 | } | |
81a76618 | 147 | } else if (!mf_are_prereqs_ok(mf, &match->flow)) { |
2e0525bc | 148 | error = OFPERR_OFPBMC_BAD_PREREQ; |
81a76618 | 149 | } else if (!mf_is_all_wild(mf, &match->wc)) { |
2e0525bc | 150 | error = OFPERR_OFPBMC_DUP_FIELD; |
72333065 | 151 | } else { |
28da1f8f | 152 | unsigned int width = mf->n_bytes; |
6a885fd0 BP |
153 | union mf_value value; |
154 | ||
155 | memcpy(&value, p + 4, width); | |
28da1f8f | 156 | if (!mf_is_value_valid(mf, &value)) { |
2e0525bc | 157 | error = OFPERR_OFPBMC_BAD_VALUE; |
6a885fd0 BP |
158 | } else if (!NXM_HASMASK(header)) { |
159 | error = 0; | |
81a76618 | 160 | mf_set_value(mf, &value, match); |
6a885fd0 BP |
161 | } else { |
162 | union mf_value mask; | |
163 | ||
164 | memcpy(&mask, p + 4 + width, width); | |
28da1f8f | 165 | if (!mf_is_mask_valid(mf, &mask)) { |
2e0525bc | 166 | error = OFPERR_OFPBMC_BAD_MASK; |
6a885fd0 BP |
167 | } else { |
168 | error = 0; | |
e1cfc4e4 | 169 | check_mask_consistency(p, mf); |
81a76618 | 170 | mf_set(mf, &value, &mask, match); |
6a885fd0 BP |
171 | } |
172 | } | |
09246b99 | 173 | } |
6a885fd0 | 174 | |
e729e793 JP |
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) { | |
2e0525bc | 178 | error = OFPERR_OFPBMC_DUP_FIELD; |
e729e793 JP |
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 = htonll(UINT64_MAX); | |
187 | } | |
188 | error = 0; | |
189 | } | |
190 | } | |
191 | ||
09246b99 | 192 | if (error) { |
28da1f8f BP |
193 | VLOG_DBG_RL(&rl, "bad nxm_entry %#08"PRIx32" (vendor=%"PRIu32", " |
194 | "field=%"PRIu32", hasmask=%"PRIu32", len=%"PRIu32"), " | |
195 | "(%s)", header, | |
09246b99 | 196 | NXM_VENDOR(header), NXM_FIELD(header), |
28da1f8f | 197 | NXM_HASMASK(header), NXM_LENGTH(header), |
90bf1e07 | 198 | ofperr_to_string(error)); |
09246b99 BP |
199 | return error; |
200 | } | |
09246b99 BP |
201 | } |
202 | ||
2e0525bc | 203 | return match_len ? OFPERR_OFPBMC_BAD_LEN : 0; |
09246b99 | 204 | } |
102ce766 | 205 | |
7623f4dd SH |
206 | static enum ofperr |
207 | nx_pull_match__(struct ofpbuf *b, unsigned int match_len, bool strict, | |
81a76618 | 208 | struct match *match, |
7623f4dd SH |
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 | ||
81a76618 | 223 | return nx_pull_raw(p, match_len, strict, match, cookie, cookie_mask); |
7623f4dd SH |
224 | } |
225 | ||
102ce766 | 226 | /* Parses the nx_match formatted match description in 'b' with length |
81a76618 BP |
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. | |
102ce766 | 230 | * |
81a76618 | 231 | * Fails with an error upon encountering an unknown NXM header. |
102ce766 EJ |
232 | * |
233 | * Returns 0 if successful, otherwise an OpenFlow error code. */ | |
90bf1e07 | 234 | enum ofperr |
81a76618 | 235 | nx_pull_match(struct ofpbuf *b, unsigned int match_len, struct match *match, |
102ce766 EJ |
236 | ovs_be64 *cookie, ovs_be64 *cookie_mask) |
237 | { | |
81a76618 | 238 | return nx_pull_match__(b, match_len, true, match, cookie, cookie_mask); |
102ce766 EJ |
239 | } |
240 | ||
81a76618 BP |
241 | /* Behaves the same as nx_pull_match(), but skips over unknown NXM headers, |
242 | * instead of failing with an error. */ | |
90bf1e07 | 243 | enum ofperr |
102ce766 | 244 | nx_pull_match_loose(struct ofpbuf *b, unsigned int match_len, |
81a76618 | 245 | struct match *match, |
102ce766 EJ |
246 | ovs_be64 *cookie, ovs_be64 *cookie_mask) |
247 | { | |
81a76618 | 248 | return nx_pull_match__(b, match_len, false, match, cookie, cookie_mask); |
102ce766 | 249 | } |
7623f4dd SH |
250 | |
251 | static enum ofperr | |
81a76618 | 252 | oxm_pull_match__(struct ofpbuf *b, bool strict, struct match *match) |
7623f4dd SH |
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, | |
81a76618 | 280 | strict, match, NULL, NULL); |
7623f4dd SH |
281 | } |
282 | ||
ec9f40dc | 283 | /* Parses the oxm formatted match description preceded by a struct ofp11_match |
81a76618 | 284 | * in 'b' with length 'match_len'. Stores the result in 'match'. |
7623f4dd SH |
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 | |
81a76618 | 290 | oxm_pull_match(struct ofpbuf *b, struct match *match) |
7623f4dd | 291 | { |
81a76618 | 292 | return oxm_pull_match__(b, true, match); |
7623f4dd SH |
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 | |
81a76618 | 298 | oxm_pull_match_loose(struct ofpbuf *b, struct match *match) |
7623f4dd | 299 | { |
81a76618 | 300 | return oxm_pull_match__(b, false, match); |
7623f4dd | 301 | } |
09246b99 BP |
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 | ||
7257b535 BP |
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 | ||
09246b99 BP |
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 | ||
66642cb4 BP |
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 CONSTANT_HTONS(UINT16_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 | ||
09246b99 BP |
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 | ||
66642cb4 | 396 | case CONSTANT_HTONL(UINT32_MAX): |
09246b99 BP |
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 | ||
8368c090 BP |
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 CONSTANT_HTONLL(UINT64_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 | ||
09246b99 BP |
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 | ||
1e37a2d7 | 446 | static void |
73c0ce34 JS |
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]) | |
1e37a2d7 | 450 | { |
73c0ce34 JS |
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 | } | |
1e37a2d7 BP |
459 | } |
460 | } | |
461 | ||
d31f1109 JP |
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 | ||
7257b535 | 478 | static void |
81a76618 | 479 | nxm_put_frag(struct ofpbuf *b, const struct match *match) |
7257b535 | 480 | { |
81a76618 BP |
481 | uint8_t nw_frag = match->flow.nw_frag; |
482 | uint8_t nw_frag_mask = match->wc.masks.nw_frag; | |
7257b535 | 483 | |
eadef313 | 484 | switch (nw_frag_mask) { |
7257b535 BP |
485 | case 0: |
486 | break; | |
487 | ||
eadef313 JP |
488 | case FLOW_NW_FRAG_MASK: |
489 | nxm_put_8(b, NXM_NX_IP_FRAG, nw_frag); | |
7257b535 BP |
490 | break; |
491 | ||
492 | default: | |
eadef313 JP |
493 | nxm_put_8m(b, NXM_NX_IP_FRAG, nw_frag, |
494 | nw_frag_mask & FLOW_NW_FRAG_MASK); | |
7257b535 BP |
495 | break; |
496 | } | |
497 | } | |
498 | ||
8e7082b0 | 499 | static void |
81a76618 | 500 | nxm_put_ip(struct ofpbuf *b, const struct match *match, |
b5ae8913 SH |
501 | uint8_t icmp_proto, uint32_t icmp_type, uint32_t icmp_code, |
502 | bool oxm) | |
8e7082b0 | 503 | { |
81a76618 | 504 | const struct flow *flow = &match->flow; |
8e7082b0 | 505 | |
81a76618 | 506 | nxm_put_frag(b, match); |
8e7082b0 | 507 | |
81a76618 | 508 | if (match->wc.masks.nw_tos & IP_DSCP_MASK) { |
1638b906 BP |
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 | } | |
8e7082b0 BP |
514 | } |
515 | ||
81a76618 | 516 | if (match->wc.masks.nw_tos & IP_ECN_MASK) { |
b5ae8913 SH |
517 | nxm_put_8(b, oxm ? OXM_OF_IP_ECN : NXM_NX_IP_ECN, |
518 | flow->nw_tos & IP_ECN_MASK); | |
8e7082b0 BP |
519 | } |
520 | ||
81a76618 | 521 | if (!oxm && match->wc.masks.nw_ttl) { |
8e7082b0 BP |
522 | nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl); |
523 | } | |
524 | ||
81a76618 | 525 | if (match->wc.masks.nw_proto) { |
b5ae8913 | 526 | nxm_put_8(b, oxm ? OXM_OF_IP_PROTO : NXM_OF_IP_PROTO, flow->nw_proto); |
8e7082b0 BP |
527 | |
528 | if (flow->nw_proto == IPPROTO_TCP) { | |
b5ae8913 | 529 | nxm_put_16m(b, oxm ? OXM_OF_TCP_SRC : NXM_OF_TCP_SRC, |
81a76618 | 530 | flow->tp_src, match->wc.masks.tp_src); |
b5ae8913 | 531 | nxm_put_16m(b, oxm ? OXM_OF_TCP_DST : NXM_OF_TCP_DST, |
81a76618 | 532 | flow->tp_dst, match->wc.masks.tp_dst); |
8e7082b0 | 533 | } else if (flow->nw_proto == IPPROTO_UDP) { |
b5ae8913 | 534 | nxm_put_16m(b, oxm ? OXM_OF_UDP_SRC : NXM_OF_UDP_SRC, |
81a76618 | 535 | flow->tp_src, match->wc.masks.tp_src); |
b5ae8913 | 536 | nxm_put_16m(b, oxm ? OXM_OF_UDP_DST : NXM_OF_UDP_DST, |
81a76618 | 537 | flow->tp_dst, match->wc.masks.tp_dst); |
8e7082b0 | 538 | } else if (flow->nw_proto == icmp_proto) { |
81a76618 | 539 | if (match->wc.masks.tp_src) { |
8e7082b0 BP |
540 | nxm_put_8(b, icmp_type, ntohs(flow->tp_src)); |
541 | } | |
81a76618 | 542 | if (match->wc.masks.tp_dst) { |
8e7082b0 BP |
543 | nxm_put_8(b, icmp_code, ntohs(flow->tp_dst)); |
544 | } | |
545 | } | |
546 | } | |
547 | } | |
548 | ||
81a76618 | 549 | /* Appends to 'b' the nx_match format that expresses 'match'. For Flow Mod and |
7623f4dd SH |
550 | * Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied. |
551 | * Otherwise, 'cookie_mask' should be zero. | |
4d0ed519 BP |
552 | * |
553 | * This function can cause 'b''s data to be reallocated. | |
554 | * | |
555 | * Returns the number of bytes appended to 'b', excluding padding. | |
556 | * | |
81a76618 | 557 | * If 'match' is a catch-all rule that matches every packet, then this function |
4d0ed519 | 558 | * appends nothing to 'b' and returns 0. */ |
7623f4dd | 559 | static int |
81a76618 | 560 | nx_put_raw(struct ofpbuf *b, bool oxm, const struct match *match, |
7623f4dd | 561 | ovs_be64 cookie, ovs_be64 cookie_mask) |
09246b99 | 562 | { |
81a76618 | 563 | const struct flow *flow = &match->flow; |
09246b99 | 564 | const size_t start_len = b->size; |
09246b99 | 565 | int match_len; |
b6c9e612 | 566 | int i; |
09246b99 | 567 | |
cff78c88 | 568 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 20); |
a877206f | 569 | |
09246b99 | 570 | /* Metadata. */ |
81a76618 | 571 | if (match->wc.masks.in_port) { |
09246b99 | 572 | uint16_t in_port = flow->in_port; |
b5ae8913 SH |
573 | if (oxm) { |
574 | nxm_put_32(b, OXM_OF_IN_PORT, ofputil_port_to_ofp11(in_port)); | |
575 | } else { | |
576 | nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port)); | |
577 | } | |
09246b99 BP |
578 | } |
579 | ||
580 | /* Ethernet. */ | |
b5ae8913 | 581 | nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_SRC : NXM_OF_ETH_SRC, |
81a76618 | 582 | flow->dl_src, match->wc.masks.dl_src); |
b5ae8913 | 583 | nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_DST : NXM_OF_ETH_DST, |
81a76618 | 584 | flow->dl_dst, match->wc.masks.dl_dst); |
e2170cff BP |
585 | nxm_put_16m(b, oxm ? OXM_OF_ETH_TYPE : NXM_OF_ETH_TYPE, |
586 | ofputil_dl_type_to_openflow(flow->dl_type), | |
81a76618 | 587 | match->wc.masks.dl_type); |
09246b99 | 588 | |
95f61ba8 SH |
589 | /* 802.1Q. */ |
590 | if (oxm) { | |
26720e24 BP |
591 | ovs_be16 VID_CFI_MASK = htons(VLAN_VID_MASK | VLAN_CFI); |
592 | ovs_be16 vid = flow->vlan_tci & VID_CFI_MASK; | |
81a76618 | 593 | ovs_be16 mask = match->wc.masks.vlan_tci & VID_CFI_MASK; |
95f61ba8 SH |
594 | |
595 | if (mask == htons(VLAN_VID_MASK | VLAN_CFI)) { | |
596 | nxm_put_16(b, OXM_OF_VLAN_VID, vid); | |
597 | } else if (mask) { | |
598 | nxm_put_16m(b, OXM_OF_VLAN_VID, vid, mask); | |
599 | } | |
600 | ||
81a76618 | 601 | if (vid && vlan_tci_to_pcp(match->wc.masks.vlan_tci)) { |
95f61ba8 SH |
602 | nxm_put_8(b, OXM_OF_VLAN_PCP, vlan_tci_to_pcp(flow->vlan_tci)); |
603 | } | |
604 | ||
605 | } else { | |
81a76618 BP |
606 | nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, |
607 | match->wc.masks.vlan_tci); | |
95f61ba8 | 608 | } |
09246b99 | 609 | |
b02475c5 SH |
610 | /* MPLS. */ |
611 | if (eth_type_mpls(flow->dl_type)) { | |
612 | if (match->wc.masks.mpls_lse & htonl(MPLS_TC_MASK)) { | |
613 | nxm_put_8(b, OXM_OF_MPLS_TC, mpls_lse_to_tc(flow->mpls_lse)); | |
614 | } | |
615 | ||
616 | if (match->wc.masks.mpls_lse & htonl(MPLS_BOS_MASK)) { | |
617 | nxm_put_8(b, OXM_OF_MPLS_BOS, mpls_lse_to_bos(flow->mpls_lse)); | |
618 | } | |
619 | ||
620 | if (match->wc.masks.mpls_lse & htonl(MPLS_LABEL_MASK)) { | |
621 | nxm_put_32(b, OXM_OF_MPLS_LABEL, | |
622 | htonl(mpls_lse_to_label(flow->mpls_lse))); | |
623 | } | |
624 | } | |
625 | ||
66642cb4 | 626 | /* L3. */ |
e2170cff | 627 | if (flow->dl_type == htons(ETH_TYPE_IP)) { |
09246b99 | 628 | /* IP. */ |
b5ae8913 | 629 | nxm_put_32m(b, oxm ? OXM_OF_IPV4_SRC : NXM_OF_IP_SRC, |
81a76618 | 630 | flow->nw_src, match->wc.masks.nw_src); |
b5ae8913 | 631 | nxm_put_32m(b, oxm ? OXM_OF_IPV4_DST : NXM_OF_IP_DST, |
81a76618 BP |
632 | flow->nw_dst, match->wc.masks.nw_dst); |
633 | nxm_put_ip(b, match, IPPROTO_ICMP, | |
b5ae8913 SH |
634 | oxm ? OXM_OF_ICMPV4_TYPE : NXM_OF_ICMP_TYPE, |
635 | oxm ? OXM_OF_ICMPV4_CODE : NXM_OF_ICMP_CODE, oxm); | |
e2170cff | 636 | } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) { |
d31f1109 | 637 | /* IPv6. */ |
b5ae8913 | 638 | nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_SRC : NXM_NX_IPV6_SRC, |
81a76618 | 639 | &flow->ipv6_src, &match->wc.masks.ipv6_src); |
b5ae8913 | 640 | nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_DST : NXM_NX_IPV6_DST, |
81a76618 BP |
641 | &flow->ipv6_dst, &match->wc.masks.ipv6_dst); |
642 | nxm_put_ip(b, match, IPPROTO_ICMPV6, | |
b5ae8913 SH |
643 | oxm ? OXM_OF_ICMPV6_TYPE : NXM_NX_ICMPV6_TYPE, |
644 | oxm ? OXM_OF_ICMPV6_CODE : NXM_NX_ICMPV6_CODE, oxm); | |
9e44d715 | 645 | |
32455024 | 646 | nxm_put_32m(b, oxm ? OXM_OF_IPV6_FLABEL : NXM_NX_IPV6_LABEL, |
81a76618 | 647 | flow->ipv6_label, match->wc.masks.ipv6_label); |
d31f1109 | 648 | |
8e7082b0 BP |
649 | if (flow->nw_proto == IPPROTO_ICMPV6 |
650 | && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) || | |
651 | flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) { | |
b5ae8913 | 652 | nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_ND_TARGET : NXM_NX_ND_TARGET, |
81a76618 | 653 | &flow->nd_target, &match->wc.masks.nd_target); |
e878338b SH |
654 | if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) { |
655 | nxm_put_eth_masked(b, oxm ? OXM_OF_IPV6_ND_SLL : NXM_NX_ND_SLL, | |
81a76618 | 656 | flow->arp_sha, match->wc.masks.arp_sha); |
8e7082b0 | 657 | } |
e878338b SH |
658 | if (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) { |
659 | nxm_put_eth_masked(b, oxm ? OXM_OF_IPV6_ND_TLL : NXM_NX_ND_TLL, | |
81a76618 | 660 | flow->arp_tha, match->wc.masks.arp_tha); |
d31f1109 JP |
661 | } |
662 | } | |
8087f5ff MM |
663 | } else if (flow->dl_type == htons(ETH_TYPE_ARP) || |
664 | flow->dl_type == htons(ETH_TYPE_RARP)) { | |
09246b99 | 665 | /* ARP. */ |
81a76618 | 666 | if (match->wc.masks.nw_proto) { |
b5ae8913 SH |
667 | nxm_put_16(b, oxm ? OXM_OF_ARP_OP : NXM_OF_ARP_OP, |
668 | htons(flow->nw_proto)); | |
09246b99 | 669 | } |
b5ae8913 | 670 | nxm_put_32m(b, oxm ? OXM_OF_ARP_SPA : NXM_OF_ARP_SPA, |
81a76618 | 671 | flow->nw_src, match->wc.masks.nw_src); |
b5ae8913 | 672 | nxm_put_32m(b, oxm ? OXM_OF_ARP_TPA : NXM_OF_ARP_TPA, |
81a76618 | 673 | flow->nw_dst, match->wc.masks.nw_dst); |
e878338b | 674 | nxm_put_eth_masked(b, oxm ? OXM_OF_ARP_SHA : NXM_NX_ARP_SHA, |
81a76618 | 675 | flow->arp_sha, match->wc.masks.arp_sha); |
e878338b | 676 | nxm_put_eth_masked(b, oxm ? OXM_OF_ARP_THA : NXM_NX_ARP_THA, |
81a76618 | 677 | flow->arp_tha, match->wc.masks.arp_tha); |
09246b99 BP |
678 | } |
679 | ||
680 | /* Tunnel ID. */ | |
2f2eb46c | 681 | nxm_put_64m(b, oxm ? OXM_OF_TUNNEL_ID : NXM_NX_TUN_ID, |
0ad90c84 JR |
682 | flow->tunnel.tun_id, match->wc.masks.tunnel.tun_id); |
683 | ||
684 | /* Other tunnel metadata. */ | |
685 | nxm_put_32m(b, NXM_NX_TUN_IPV4_SRC, | |
686 | flow->tunnel.ip_src, match->wc.masks.tunnel.ip_src); | |
687 | nxm_put_32m(b, NXM_NX_TUN_IPV4_DST, | |
688 | flow->tunnel.ip_dst, match->wc.masks.tunnel.ip_dst); | |
09246b99 | 689 | |
b6c9e612 BP |
690 | /* Registers. */ |
691 | for (i = 0; i < FLOW_N_REGS; i++) { | |
692 | nxm_put_32m(b, NXM_NX_REG(i), | |
81a76618 | 693 | htonl(flow->regs[i]), htonl(match->wc.masks.regs[i])); |
b6c9e612 BP |
694 | } |
695 | ||
969fc56c | 696 | /* OpenFlow 1.1+ Metadata. */ |
81a76618 | 697 | nxm_put_64m(b, OXM_OF_METADATA, flow->metadata, match->wc.masks.metadata); |
969fc56c | 698 | |
e729e793 JP |
699 | /* Cookie. */ |
700 | nxm_put_64m(b, NXM_NX_COOKIE, cookie, cookie_mask); | |
701 | ||
09246b99 | 702 | match_len = b->size - start_len; |
7623f4dd SH |
703 | return match_len; |
704 | } | |
705 | ||
81a76618 | 706 | /* Appends to 'b' the nx_match format that expresses 'match', plus enough zero |
7623f4dd SH |
707 | * bytes to pad the nx_match out to a multiple of 8. For Flow Mod and Flow |
708 | * Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied. | |
709 | * Otherwise, 'cookie_mask' should be zero. | |
710 | * | |
711 | * This function can cause 'b''s data to be reallocated. | |
712 | * | |
713 | * Returns the number of bytes appended to 'b', excluding padding. The return | |
714 | * value can be zero if it appended nothing at all to 'b' (which happens if | |
715 | * 'cr' is a catch-all rule that matches every packet). */ | |
716 | int | |
81a76618 | 717 | nx_put_match(struct ofpbuf *b, const struct match *match, |
7623f4dd SH |
718 | ovs_be64 cookie, ovs_be64 cookie_mask) |
719 | { | |
81a76618 | 720 | int match_len = nx_put_raw(b, false, match, cookie, cookie_mask); |
7623f4dd SH |
721 | |
722 | ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len); | |
723 | return match_len; | |
724 | } | |
725 | ||
726 | ||
727 | /* Appends to 'b' an struct ofp11_match_header followed by the oxm format that | |
81a76618 BP |
728 | * expresses 'cr', plus enough zero bytes to pad the data appended out to a |
729 | * multiple of 8. | |
7623f4dd SH |
730 | * |
731 | * This function can cause 'b''s data to be reallocated. | |
732 | * | |
733 | * Returns the number of bytes appended to 'b', excluding the padding. Never | |
734 | * returns zero. */ | |
735 | int | |
81a76618 | 736 | oxm_put_match(struct ofpbuf *b, const struct match *match) |
7623f4dd SH |
737 | { |
738 | int match_len; | |
739 | struct ofp11_match_header *omh; | |
740 | size_t start_len = b->size; | |
741 | ovs_be64 cookie = htonll(0), cookie_mask = htonll(0); | |
742 | ||
743 | ofpbuf_put_uninit(b, sizeof *omh); | |
81a76618 | 744 | match_len = nx_put_raw(b, true, match, cookie, cookie_mask) + sizeof *omh; |
09246b99 | 745 | ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len); |
7623f4dd SH |
746 | |
747 | omh = (struct ofp11_match_header *)((char *)b->data + start_len); | |
748 | omh->type = htons(OFPMT_OXM); | |
749 | omh->length = htons(match_len); | |
750 | ||
09246b99 BP |
751 | return match_len; |
752 | } | |
753 | \f | |
754 | /* nx_match_to_string() and helpers. */ | |
755 | ||
f393f81e BP |
756 | static void format_nxm_field_name(struct ds *, uint32_t header); |
757 | ||
09246b99 BP |
758 | char * |
759 | nx_match_to_string(const uint8_t *p, unsigned int match_len) | |
760 | { | |
761 | uint32_t header; | |
762 | struct ds s; | |
763 | ||
764 | if (!match_len) { | |
765 | return xstrdup("<any>"); | |
766 | } | |
767 | ||
768 | ds_init(&s); | |
769 | while ((header = nx_entry_ok(p, match_len)) != 0) { | |
770 | unsigned int length = NXM_LENGTH(header); | |
771 | unsigned int value_len = nxm_field_bytes(header); | |
772 | const uint8_t *value = p + 4; | |
773 | const uint8_t *mask = value + value_len; | |
09246b99 BP |
774 | unsigned int i; |
775 | ||
776 | if (s.length) { | |
777 | ds_put_cstr(&s, ", "); | |
778 | } | |
779 | ||
f393f81e | 780 | format_nxm_field_name(&s, header); |
09246b99 BP |
781 | ds_put_char(&s, '('); |
782 | ||
783 | for (i = 0; i < value_len; i++) { | |
784 | ds_put_format(&s, "%02x", value[i]); | |
785 | } | |
786 | if (NXM_HASMASK(header)) { | |
787 | ds_put_char(&s, '/'); | |
788 | for (i = 0; i < value_len; i++) { | |
789 | ds_put_format(&s, "%02x", mask[i]); | |
790 | } | |
791 | } | |
792 | ds_put_char(&s, ')'); | |
793 | ||
794 | p += 4 + length; | |
795 | match_len -= 4 + length; | |
796 | } | |
797 | ||
798 | if (match_len) { | |
799 | if (s.length) { | |
800 | ds_put_cstr(&s, ", "); | |
801 | } | |
802 | ||
803 | ds_put_format(&s, "<%u invalid bytes>", match_len); | |
804 | } | |
805 | ||
806 | return ds_steal_cstr(&s); | |
807 | } | |
808 | ||
7623f4dd SH |
809 | char * |
810 | oxm_match_to_string(const uint8_t *p, unsigned int match_len) | |
811 | { | |
812 | const struct ofp11_match_header *omh = (struct ofp11_match_header *)p; | |
813 | uint16_t match_len_; | |
814 | struct ds s; | |
815 | ||
816 | ds_init(&s); | |
817 | ||
818 | if (match_len < sizeof *omh) { | |
819 | ds_put_format(&s, "<match too short: %u>", match_len); | |
820 | goto err; | |
821 | } | |
822 | ||
823 | if (omh->type != htons(OFPMT_OXM)) { | |
824 | ds_put_format(&s, "<bad match type field: %u>", ntohs(omh->type)); | |
825 | goto err; | |
826 | } | |
827 | ||
828 | match_len_ = ntohs(omh->length); | |
829 | if (match_len_ < sizeof *omh) { | |
830 | ds_put_format(&s, "<match length field too short: %u>", match_len_); | |
831 | goto err; | |
832 | } | |
833 | ||
834 | if (match_len_ != match_len) { | |
835 | ds_put_format(&s, "<match length field incorrect: %u != %u>", | |
836 | match_len_, match_len); | |
837 | goto err; | |
838 | } | |
839 | ||
840 | return nx_match_to_string(p + sizeof *omh, match_len - sizeof *omh); | |
841 | ||
842 | err: | |
843 | return ds_steal_cstr(&s); | |
844 | } | |
845 | ||
f393f81e BP |
846 | static void |
847 | format_nxm_field_name(struct ds *s, uint32_t header) | |
848 | { | |
28da1f8f BP |
849 | const struct mf_field *mf = mf_from_nxm_header(header); |
850 | if (mf) { | |
b5ae8913 | 851 | ds_put_cstr(s, IS_OXM_HEADER(header) ? mf->oxm_name : mf->nxm_name); |
28da1f8f BP |
852 | if (NXM_HASMASK(header)) { |
853 | ds_put_cstr(s, "_W"); | |
854 | } | |
e729e793 JP |
855 | } else if (header == NXM_NX_COOKIE) { |
856 | ds_put_cstr(s, "NXM_NX_COOKIE"); | |
857 | } else if (header == NXM_NX_COOKIE_W) { | |
858 | ds_put_cstr(s, "NXM_NX_COOKIE_W"); | |
f393f81e BP |
859 | } else { |
860 | ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header)); | |
861 | } | |
862 | } | |
863 | ||
558d80cb BP |
864 | static uint32_t |
865 | parse_nxm_field_name(const char *name, int name_len) | |
09246b99 | 866 | { |
28da1f8f BP |
867 | bool wild; |
868 | int i; | |
09246b99 | 869 | |
558d80cb | 870 | /* Check whether it's a field name. */ |
28da1f8f BP |
871 | wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2); |
872 | if (wild) { | |
873 | name_len -= 2; | |
874 | } | |
e729e793 | 875 | |
28da1f8f BP |
876 | for (i = 0; i < MFF_N_IDS; i++) { |
877 | const struct mf_field *mf = mf_from_id(i); | |
b5ae8913 SH |
878 | uint32_t header; |
879 | ||
880 | if (mf->nxm_name && | |
881 | !strncmp(mf->nxm_name, name, name_len) && | |
882 | mf->nxm_name[name_len] == '\0') { | |
883 | header = mf->nxm_header; | |
884 | } else if (mf->oxm_name && | |
885 | !strncmp(mf->oxm_name, name, name_len) && | |
886 | mf->oxm_name[name_len] == '\0') { | |
887 | header = mf->oxm_header; | |
888 | } else { | |
889 | continue; | |
890 | } | |
28da1f8f | 891 | |
b5ae8913 SH |
892 | if (!wild) { |
893 | return header; | |
894 | } else if (mf->maskable != MFM_NONE) { | |
895 | return NXM_MAKE_WILD_HEADER(header); | |
09246b99 BP |
896 | } |
897 | } | |
898 | ||
b5ae8913 SH |
899 | if (!strncmp("NXM_NX_COOKIE", name, name_len) && |
900 | (name_len == strlen("NXM_NX_COOKIE"))) { | |
e729e793 JP |
901 | if (!wild) { |
902 | return NXM_NX_COOKIE; | |
903 | } else { | |
904 | return NXM_NX_COOKIE_W; | |
905 | } | |
906 | } | |
907 | ||
558d80cb BP |
908 | /* Check whether it's a 32-bit field header value as hex. |
909 | * (This isn't ordinarily useful except for testing error behavior.) */ | |
910 | if (name_len == 8) { | |
911 | uint32_t header = hexits_value(name, name_len, NULL); | |
912 | if (header != UINT_MAX) { | |
913 | return header; | |
914 | } | |
915 | } | |
916 | ||
917 | return 0; | |
09246b99 | 918 | } |
09246b99 BP |
919 | \f |
920 | /* nx_match_from_string(). */ | |
921 | ||
7623f4dd SH |
922 | static int |
923 | nx_match_from_string_raw(const char *s, struct ofpbuf *b) | |
09246b99 BP |
924 | { |
925 | const char *full_s = s; | |
926 | const size_t start_len = b->size; | |
09246b99 BP |
927 | |
928 | if (!strcmp(s, "<any>")) { | |
929 | /* Ensure that 'b->data' isn't actually null. */ | |
930 | ofpbuf_prealloc_tailroom(b, 1); | |
931 | return 0; | |
932 | } | |
933 | ||
934 | for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) { | |
558d80cb BP |
935 | const char *name; |
936 | uint32_t header; | |
09246b99 | 937 | int name_len; |
78090f63 | 938 | size_t n; |
09246b99 | 939 | |
558d80cb | 940 | name = s; |
09246b99 BP |
941 | name_len = strcspn(s, "("); |
942 | if (s[name_len] != '(') { | |
943 | ovs_fatal(0, "%s: missing ( at end of nx_match", full_s); | |
944 | } | |
945 | ||
558d80cb BP |
946 | header = parse_nxm_field_name(name, name_len); |
947 | if (!header) { | |
09246b99 BP |
948 | ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s); |
949 | } | |
950 | ||
951 | s += name_len + 1; | |
952 | ||
558d80cb | 953 | nxm_put_header(b, header); |
78090f63 BP |
954 | s = ofpbuf_put_hex(b, s, &n); |
955 | if (n != nxm_field_bytes(header)) { | |
956 | ovs_fatal(0, "%.2s: hex digits expected", s); | |
957 | } | |
558d80cb | 958 | if (NXM_HASMASK(header)) { |
09246b99 BP |
959 | s += strspn(s, " "); |
960 | if (*s != '/') { | |
558d80cb BP |
961 | ovs_fatal(0, "%s: missing / in masked field %.*s", |
962 | full_s, name_len, name); | |
09246b99 | 963 | } |
78090f63 BP |
964 | s = ofpbuf_put_hex(b, s + 1, &n); |
965 | if (n != nxm_field_bytes(header)) { | |
966 | ovs_fatal(0, "%.2s: hex digits expected", s); | |
967 | } | |
09246b99 BP |
968 | } |
969 | ||
970 | s += strspn(s, " "); | |
971 | if (*s != ')') { | |
558d80cb BP |
972 | ovs_fatal(0, "%s: missing ) following field %.*s", |
973 | full_s, name_len, name); | |
09246b99 BP |
974 | } |
975 | s++; | |
976 | } | |
977 | ||
7623f4dd SH |
978 | return b->size - start_len; |
979 | } | |
980 | ||
981 | int | |
982 | nx_match_from_string(const char *s, struct ofpbuf *b) | |
983 | { | |
984 | int match_len = nx_match_from_string_raw(s, b); | |
985 | ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len); | |
986 | return match_len; | |
987 | } | |
988 | ||
989 | int | |
990 | oxm_match_from_string(const char *s, struct ofpbuf *b) | |
991 | { | |
992 | int match_len; | |
993 | struct ofp11_match_header *omh; | |
994 | size_t start_len = b->size; | |
995 | ||
996 | ofpbuf_put_uninit(b, sizeof *omh); | |
997 | match_len = nx_match_from_string_raw(s, b) + sizeof *omh; | |
09246b99 | 998 | ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len); |
7623f4dd SH |
999 | |
1000 | omh = (struct ofp11_match_header *)((char *)b->data + start_len); | |
1001 | omh->type = htons(OFPMT_OXM); | |
1002 | omh->length = htons(match_len); | |
1003 | ||
09246b99 BP |
1004 | return match_len; |
1005 | } | |
b6c9e612 | 1006 | \f |
f393f81e | 1007 | void |
f25d0cf3 | 1008 | nxm_parse_reg_move(struct ofpact_reg_move *move, const char *s) |
f393f81e BP |
1009 | { |
1010 | const char *full_s = s; | |
f393f81e | 1011 | |
f25d0cf3 | 1012 | s = mf_parse_subfield(&move->src, s); |
f393f81e BP |
1013 | if (strncmp(s, "->", 2)) { |
1014 | ovs_fatal(0, "%s: missing `->' following source", full_s); | |
1015 | } | |
1016 | s += 2; | |
f25d0cf3 | 1017 | s = mf_parse_subfield(&move->dst, s); |
f393f81e BP |
1018 | if (*s != '\0') { |
1019 | ovs_fatal(0, "%s: trailing garbage following destination", full_s); | |
1020 | } | |
1021 | ||
f25d0cf3 | 1022 | if (move->src.n_bits != move->dst.n_bits) { |
f393f81e | 1023 | ovs_fatal(0, "%s: source field is %d bits wide but destination is " |
f25d0cf3 BP |
1024 | "%d bits wide", full_s, |
1025 | move->src.n_bits, move->dst.n_bits); | |
f393f81e | 1026 | } |
f393f81e BP |
1027 | } |
1028 | ||
1029 | void | |
f25d0cf3 | 1030 | nxm_parse_reg_load(struct ofpact_reg_load *load, const char *s) |
f393f81e BP |
1031 | { |
1032 | const char *full_s = s; | |
9bab681f | 1033 | uint64_t value = strtoull(s, (char **) &s, 0); |
f393f81e | 1034 | |
f393f81e BP |
1035 | if (strncmp(s, "->", 2)) { |
1036 | ovs_fatal(0, "%s: missing `->' following value", full_s); | |
1037 | } | |
1038 | s += 2; | |
f25d0cf3 | 1039 | s = mf_parse_subfield(&load->dst, s); |
f393f81e BP |
1040 | if (*s != '\0') { |
1041 | ovs_fatal(0, "%s: trailing garbage following destination", full_s); | |
1042 | } | |
1043 | ||
9bab681f | 1044 | if (load->dst.n_bits < 64 && (value >> load->dst.n_bits) != 0) { |
f25d0cf3 | 1045 | ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits", |
9bab681f | 1046 | full_s, value, load->dst.n_bits); |
f393f81e | 1047 | } |
9bab681f | 1048 | |
158edc8d BP |
1049 | load->subvalue.be64[0] = htonll(0); |
1050 | load->subvalue.be64[1] = htonll(value); | |
f393f81e BP |
1051 | } |
1052 | \f | |
1053 | /* nxm_format_reg_move(), nxm_format_reg_load(). */ | |
1054 | ||
f393f81e | 1055 | void |
f25d0cf3 | 1056 | nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s) |
f393f81e | 1057 | { |
f393f81e | 1058 | ds_put_format(s, "move:"); |
f25d0cf3 | 1059 | mf_format_subfield(&move->src, s); |
f393f81e | 1060 | ds_put_cstr(s, "->"); |
f25d0cf3 | 1061 | mf_format_subfield(&move->dst, s); |
f393f81e BP |
1062 | } |
1063 | ||
0291c1c6 SH |
1064 | static void |
1065 | set_field_format(const struct ofpact_reg_load *load, struct ds *s) | |
1066 | { | |
1067 | const struct mf_field *mf = load->dst.field; | |
1068 | union mf_value value; | |
1069 | ||
cb22974d | 1070 | ovs_assert(load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD); |
0291c1c6 SH |
1071 | ds_put_format(s, "set_field:"); |
1072 | memset(&value, 0, sizeof value); | |
1073 | bitwise_copy(&load->subvalue, sizeof load->subvalue, 0, | |
1074 | &value, mf->n_bytes, 0, load->dst.n_bits); | |
1075 | mf_format(mf, &value, NULL, s); | |
1076 | ds_put_format(s, "->%s", mf->name); | |
1077 | } | |
1078 | ||
1079 | static void | |
1080 | load_format(const struct ofpact_reg_load *load, struct ds *s) | |
f25d0cf3 | 1081 | { |
9bab681f | 1082 | ds_put_cstr(s, "load:"); |
158edc8d | 1083 | mf_format_subvalue(&load->subvalue, s); |
9bab681f | 1084 | ds_put_cstr(s, "->"); |
f25d0cf3 BP |
1085 | mf_format_subfield(&load->dst, s); |
1086 | } | |
0291c1c6 SH |
1087 | |
1088 | void | |
1089 | nxm_format_reg_load(const struct ofpact_reg_load *load, struct ds *s) | |
1090 | { | |
1091 | if (load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD) { | |
1092 | set_field_format(load, s); | |
1093 | } else { | |
1094 | load_format(load, s); | |
1095 | } | |
1096 | } | |
f25d0cf3 BP |
1097 | \f |
1098 | enum ofperr | |
1099 | nxm_reg_move_from_openflow(const struct nx_action_reg_move *narm, | |
1100 | struct ofpbuf *ofpacts) | |
f393f81e | 1101 | { |
f25d0cf3 | 1102 | struct ofpact_reg_move *move; |
f393f81e | 1103 | |
f25d0cf3 BP |
1104 | move = ofpact_put_REG_MOVE(ofpacts); |
1105 | move->src.field = mf_from_nxm_header(ntohl(narm->src)); | |
1106 | move->src.ofs = ntohs(narm->src_ofs); | |
1107 | move->src.n_bits = ntohs(narm->n_bits); | |
1108 | move->dst.field = mf_from_nxm_header(ntohl(narm->dst)); | |
1109 | move->dst.ofs = ntohs(narm->dst_ofs); | |
1110 | move->dst.n_bits = ntohs(narm->n_bits); | |
816fd533 | 1111 | |
f25d0cf3 | 1112 | return nxm_reg_move_check(move, NULL); |
f393f81e | 1113 | } |
b6c9e612 | 1114 | |
816fd533 | 1115 | enum ofperr |
f25d0cf3 BP |
1116 | nxm_reg_load_from_openflow(const struct nx_action_reg_load *narl, |
1117 | struct ofpbuf *ofpacts) | |
b6c9e612 | 1118 | { |
f25d0cf3 | 1119 | struct ofpact_reg_load *load; |
b6c9e612 | 1120 | |
f25d0cf3 BP |
1121 | load = ofpact_put_REG_LOAD(ofpacts); |
1122 | load->dst.field = mf_from_nxm_header(ntohl(narl->dst)); | |
1123 | load->dst.ofs = nxm_decode_ofs(narl->ofs_nbits); | |
1124 | load->dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits); | |
158edc8d | 1125 | load->subvalue.be64[1] = narl->value; |
f25d0cf3 BP |
1126 | |
1127 | /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in | |
1128 | * narl->value. */ | |
9bab681f IY |
1129 | if (load->dst.n_bits < 64 && |
1130 | ntohll(narl->value) >> load->dst.n_bits) { | |
f25d0cf3 | 1131 | return OFPERR_OFPBAC_BAD_ARGUMENT; |
b6c9e612 BP |
1132 | } |
1133 | ||
f25d0cf3 | 1134 | return nxm_reg_load_check(load, NULL); |
43edca57 | 1135 | } |
0291c1c6 SH |
1136 | |
1137 | enum ofperr | |
1138 | nxm_reg_load_from_openflow12_set_field( | |
1139 | const struct ofp12_action_set_field * oasf, struct ofpbuf *ofpacts) | |
1140 | { | |
1141 | uint16_t oasf_len = ntohs(oasf->len); | |
1142 | uint32_t oxm_header = ntohl(oasf->dst); | |
1143 | uint8_t oxm_length = NXM_LENGTH(oxm_header); | |
1144 | struct ofpact_reg_load *load; | |
1145 | const struct mf_field *mf; | |
1146 | ||
1147 | /* ofp12_action_set_field is padded to 64 bits by zero */ | |
1148 | if (oasf_len != ROUND_UP(sizeof(*oasf) + oxm_length, 8)) { | |
1149 | return OFPERR_OFPBAC_BAD_ARGUMENT; | |
1150 | } | |
1151 | if (!is_all_zeros((const uint8_t *)(oasf) + sizeof *oasf + oxm_length, | |
1152 | oasf_len - oxm_length - sizeof *oasf)) { | |
1153 | return OFPERR_OFPBAC_BAD_ARGUMENT; | |
1154 | } | |
1155 | ||
1156 | if (NXM_HASMASK(oxm_header)) { | |
1157 | return OFPERR_OFPBAC_BAD_ARGUMENT; | |
1158 | } | |
1159 | mf = mf_from_nxm_header(oxm_header); | |
1160 | if (!mf) { | |
1161 | return OFPERR_OFPBAC_BAD_ARGUMENT; | |
1162 | } | |
1163 | load = ofpact_put_REG_LOAD(ofpacts); | |
f5c45121 | 1164 | ofpact_set_field_init(load, mf, oasf + 1); |
0291c1c6 SH |
1165 | |
1166 | return nxm_reg_load_check(load, NULL); | |
1167 | } | |
f25d0cf3 | 1168 | \f |
90bf1e07 | 1169 | enum ofperr |
f25d0cf3 | 1170 | nxm_reg_move_check(const struct ofpact_reg_move *move, const struct flow *flow) |
43edca57 | 1171 | { |
90bf1e07 | 1172 | enum ofperr error; |
43edca57 | 1173 | |
f25d0cf3 | 1174 | error = mf_check_src(&move->src, flow); |
43edca57 EJ |
1175 | if (error) { |
1176 | return error; | |
b6c9e612 BP |
1177 | } |
1178 | ||
f25d0cf3 BP |
1179 | return mf_check_dst(&move->dst, NULL); |
1180 | } | |
b6c9e612 | 1181 | |
f25d0cf3 BP |
1182 | enum ofperr |
1183 | nxm_reg_load_check(const struct ofpact_reg_load *load, const struct flow *flow) | |
1184 | { | |
1185 | return mf_check_dst(&load->dst, flow); | |
b6c9e612 BP |
1186 | } |
1187 | \f | |
b6c9e612 | 1188 | void |
f25d0cf3 BP |
1189 | nxm_reg_move_to_nxast(const struct ofpact_reg_move *move, |
1190 | struct ofpbuf *openflow) | |
b6c9e612 | 1191 | { |
f25d0cf3 | 1192 | struct nx_action_reg_move *narm; |
28da1f8f | 1193 | |
f25d0cf3 BP |
1194 | narm = ofputil_put_NXAST_REG_MOVE(openflow); |
1195 | narm->n_bits = htons(move->dst.n_bits); | |
1196 | narm->src_ofs = htons(move->src.ofs); | |
1197 | narm->dst_ofs = htons(move->dst.ofs); | |
1198 | narm->src = htonl(move->src.field->nxm_header); | |
1199 | narm->dst = htonl(move->dst.field->nxm_header); | |
1200 | } | |
816fd533 | 1201 | |
0291c1c6 SH |
1202 | static void |
1203 | reg_load_to_nxast(const struct ofpact_reg_load *load, struct ofpbuf *openflow) | |
f25d0cf3 BP |
1204 | { |
1205 | struct nx_action_reg_load *narl; | |
1206 | ||
1207 | narl = ofputil_put_NXAST_REG_LOAD(openflow); | |
1208 | narl->ofs_nbits = nxm_encode_ofs_nbits(load->dst.ofs, load->dst.n_bits); | |
1209 | narl->dst = htonl(load->dst.field->nxm_header); | |
158edc8d | 1210 | narl->value = load->subvalue.be64[1]; |
b6c9e612 | 1211 | } |
0291c1c6 SH |
1212 | |
1213 | static void | |
1214 | set_field_to_ofast(const struct ofpact_reg_load *load, | |
1215 | struct ofpbuf *openflow) | |
1216 | { | |
1217 | const struct mf_field *mf = load->dst.field; | |
c93f303a | 1218 | uint16_t padded_value_len = ROUND_UP(mf->n_bytes, 8); |
0291c1c6 | 1219 | struct ofp12_action_set_field *oasf; |
c93f303a | 1220 | char *value; |
0291c1c6 SH |
1221 | |
1222 | /* Set field is the only action of variable length (so far), | |
1223 | * so handling the variable length portion is open-coded here */ | |
c93f303a SH |
1224 | oasf = ofputil_put_OFPAT12_SET_FIELD(openflow); |
1225 | oasf->dst = htonl(mf->oxm_header); | |
0291c1c6 | 1226 | oasf->len = htons(ntohs(oasf->len) + padded_value_len); |
0291c1c6 | 1227 | |
c93f303a | 1228 | value = ofpbuf_put_zeros(openflow, padded_value_len); |
0291c1c6 | 1229 | bitwise_copy(&load->subvalue, sizeof load->subvalue, load->dst.ofs, |
c93f303a | 1230 | value, mf->n_bytes, load->dst.ofs, load->dst.n_bits); |
0291c1c6 SH |
1231 | } |
1232 | ||
1233 | void | |
1234 | nxm_reg_load_to_nxast(const struct ofpact_reg_load *load, | |
1235 | struct ofpbuf *openflow) | |
1236 | { | |
1237 | ||
1238 | if (load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD) { | |
1239 | struct ofp_header *oh = (struct ofp_header *)openflow->l2; | |
1240 | ||
1241 | switch(oh->version) { | |
2e1ae200 | 1242 | case OFP13_VERSION: |
0291c1c6 SH |
1243 | case OFP12_VERSION: |
1244 | set_field_to_ofast(load, openflow); | |
1245 | break; | |
1246 | ||
1247 | case OFP11_VERSION: | |
1248 | case OFP10_VERSION: | |
1249 | if (load->dst.n_bits < 64) { | |
1250 | reg_load_to_nxast(load, openflow); | |
1251 | } else { | |
1252 | /* Split into 64bit chunks */ | |
1253 | int chunk, ofs; | |
1254 | for (ofs = 0; ofs < load->dst.n_bits; ofs += chunk) { | |
1255 | struct ofpact_reg_load subload = *load; | |
1256 | ||
1257 | chunk = MIN(load->dst.n_bits - ofs, 64); | |
1258 | ||
1259 | subload.dst.field = load->dst.field; | |
1260 | subload.dst.ofs = load->dst.ofs + ofs; | |
1261 | subload.dst.n_bits = chunk; | |
1262 | bitwise_copy(&load->subvalue, sizeof load->subvalue, ofs, | |
1263 | &subload.subvalue, sizeof subload.subvalue, 0, | |
1264 | chunk); | |
1265 | reg_load_to_nxast(&subload, openflow); | |
1266 | } | |
1267 | } | |
1268 | break; | |
1269 | ||
1270 | default: | |
1271 | NOT_REACHED(); | |
1272 | } | |
1273 | } else { | |
1274 | reg_load_to_nxast(load, openflow); | |
1275 | } | |
1276 | } | |
f25d0cf3 BP |
1277 | \f |
1278 | /* nxm_execute_reg_move(), nxm_execute_reg_load(). */ | |
b6c9e612 BP |
1279 | |
1280 | void | |
f25d0cf3 | 1281 | nxm_execute_reg_move(const struct ofpact_reg_move *move, |
b6c9e612 BP |
1282 | struct flow *flow) |
1283 | { | |
f25d0cf3 BP |
1284 | union mf_value src_value; |
1285 | union mf_value dst_value; | |
816fd533 | 1286 | |
f25d0cf3 BP |
1287 | mf_get_value(move->dst.field, flow, &dst_value); |
1288 | mf_get_value(move->src.field, flow, &src_value); | |
1289 | bitwise_copy(&src_value, move->src.field->n_bytes, move->src.ofs, | |
1290 | &dst_value, move->dst.field->n_bytes, move->dst.ofs, | |
1291 | move->src.n_bits); | |
1292 | mf_set_flow_value(move->dst.field, &dst_value, flow); | |
43edca57 | 1293 | } |
b6c9e612 | 1294 | |
43edca57 | 1295 | void |
f25d0cf3 | 1296 | nxm_execute_reg_load(const struct ofpact_reg_load *load, struct flow *flow) |
43edca57 | 1297 | { |
9bab681f | 1298 | mf_write_subfield_flow(&load->dst, &load->subvalue, flow); |
816fd533 | 1299 | } |
28da1f8f | 1300 | |
816fd533 | 1301 | void |
f25d0cf3 BP |
1302 | nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data, |
1303 | struct flow *flow) | |
816fd533 | 1304 | { |
9bab681f IY |
1305 | union mf_subvalue src_subvalue; |
1306 | ovs_be64 src_data_be = htonll(src_data); | |
f25d0cf3 | 1307 | |
9bab681f IY |
1308 | bitwise_copy(&src_data_be, sizeof src_data_be, 0, |
1309 | &src_subvalue, sizeof src_subvalue, 0, | |
1310 | sizeof src_data_be * 8); | |
1311 | mf_write_subfield_flow(dst, &src_subvalue, flow); | |
b6c9e612 | 1312 | } |
bd85dac1 AZ |
1313 | \f |
1314 | /* nxm_parse_stack_action, works for both push() and pop(). */ | |
1315 | void | |
1316 | nxm_parse_stack_action(struct ofpact_stack *stack_action, const char *s) | |
1317 | { | |
1318 | s = mf_parse_subfield(&stack_action->subfield, s); | |
1319 | if (*s != '\0') { | |
1320 | ovs_fatal(0, "%s: trailing garbage following push or pop", s); | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | void | |
1325 | nxm_format_stack_push(const struct ofpact_stack *push, struct ds *s) | |
1326 | { | |
1327 | ds_put_cstr(s, "push:"); | |
1328 | mf_format_subfield(&push->subfield, s); | |
1329 | } | |
1330 | ||
1331 | void | |
1332 | nxm_format_stack_pop(const struct ofpact_stack *pop, struct ds *s) | |
1333 | { | |
1334 | ds_put_cstr(s, "pop:"); | |
1335 | mf_format_subfield(&pop->subfield, s); | |
1336 | } | |
1337 | ||
1338 | /* Common set for both push and pop actions. */ | |
1339 | static void | |
1340 | stack_action_from_openflow__(const struct nx_action_stack *nasp, | |
1341 | struct ofpact_stack *stack_action) | |
1342 | { | |
1343 | stack_action->subfield.field = mf_from_nxm_header(ntohl(nasp->field)); | |
1344 | stack_action->subfield.ofs = ntohs(nasp->offset); | |
1345 | stack_action->subfield.n_bits = ntohs(nasp->n_bits); | |
1346 | } | |
1347 | ||
1348 | static void | |
1349 | nxm_stack_to_nxast__(const struct ofpact_stack *stack_action, | |
1350 | struct nx_action_stack *nasp) | |
1351 | { | |
1352 | nasp->offset = htons(stack_action->subfield.ofs); | |
1353 | nasp->n_bits = htons(stack_action->subfield.n_bits); | |
1354 | nasp->field = htonl(stack_action->subfield.field->nxm_header); | |
1355 | } | |
1356 | ||
1357 | enum ofperr | |
1358 | nxm_stack_push_from_openflow(const struct nx_action_stack *nasp, | |
1359 | struct ofpbuf *ofpacts) | |
1360 | { | |
1361 | struct ofpact_stack *push; | |
1362 | ||
1363 | push = ofpact_put_STACK_PUSH(ofpacts); | |
1364 | stack_action_from_openflow__(nasp, push); | |
1365 | ||
1366 | return nxm_stack_push_check(push, NULL); | |
1367 | } | |
1368 | ||
1369 | enum ofperr | |
1370 | nxm_stack_pop_from_openflow(const struct nx_action_stack *nasp, | |
1371 | struct ofpbuf *ofpacts) | |
1372 | { | |
1373 | struct ofpact_stack *pop; | |
1374 | ||
1375 | pop = ofpact_put_STACK_POP(ofpacts); | |
1376 | stack_action_from_openflow__(nasp, pop); | |
1377 | ||
1378 | return nxm_stack_pop_check(pop, NULL); | |
1379 | } | |
1380 | ||
1381 | enum ofperr | |
1382 | nxm_stack_push_check(const struct ofpact_stack *push, | |
1383 | const struct flow *flow) | |
1384 | { | |
1385 | return mf_check_src(&push->subfield, flow); | |
1386 | } | |
1387 | ||
1388 | enum ofperr | |
1389 | nxm_stack_pop_check(const struct ofpact_stack *pop, | |
1390 | const struct flow *flow) | |
1391 | { | |
1392 | return mf_check_dst(&pop->subfield, flow); | |
1393 | } | |
1394 | ||
1395 | void | |
1396 | nxm_stack_push_to_nxast(const struct ofpact_stack *stack, | |
1397 | struct ofpbuf *openflow) | |
1398 | { | |
1399 | nxm_stack_to_nxast__(stack, ofputil_put_NXAST_STACK_PUSH(openflow)); | |
1400 | } | |
1401 | ||
1402 | void | |
1403 | nxm_stack_pop_to_nxast(const struct ofpact_stack *stack, | |
1404 | struct ofpbuf *openflow) | |
1405 | { | |
1406 | nxm_stack_to_nxast__(stack, ofputil_put_NXAST_STACK_POP(openflow)); | |
1407 | } | |
1408 | ||
1409 | /* nxm_execute_stack_push(), nxm_execute_stack_pop(). */ | |
1410 | static void | |
1411 | nx_stack_push(struct ofpbuf *stack, union mf_subvalue *v) | |
1412 | { | |
1413 | ofpbuf_put(stack, v, sizeof *v); | |
1414 | } | |
1415 | ||
1416 | static union mf_subvalue * | |
1417 | nx_stack_pop(struct ofpbuf *stack) | |
1418 | { | |
1419 | union mf_subvalue *v = NULL; | |
1420 | ||
1421 | if (stack->size) { | |
1422 | stack->size -= sizeof *v; | |
1423 | v = (union mf_subvalue *) ofpbuf_tail(stack); | |
1424 | } | |
1425 | ||
1426 | return v; | |
1427 | } | |
1428 | ||
1429 | void | |
1430 | nxm_execute_stack_push(const struct ofpact_stack *push, | |
1431 | const struct flow *flow, struct ofpbuf *stack) | |
1432 | { | |
1433 | union mf_subvalue dst_value; | |
1434 | ||
1435 | mf_read_subfield(&push->subfield, flow, &dst_value); | |
1436 | nx_stack_push(stack, &dst_value); | |
1437 | } | |
1438 | ||
1439 | void | |
1440 | nxm_execute_stack_pop(const struct ofpact_stack *pop, | |
1441 | struct flow *flow, struct ofpbuf *stack) | |
1442 | { | |
1443 | union mf_subvalue *src_value; | |
1444 | ||
1445 | src_value = nx_stack_pop(stack); | |
1446 | ||
1447 | /* Only pop if stack is not empty. Otherwise, give warning. */ | |
1448 | if (src_value) { | |
1449 | mf_write_subfield_flow(&pop->subfield, src_value, flow); | |
1450 | } else { | |
1451 | if (!VLOG_DROP_WARN(&rl)) { | |
1452 | char *flow_str = flow_to_string(flow); | |
1453 | VLOG_WARN_RL(&rl, "Failed to pop from an empty stack. On flow \n" | |
1454 | " %s", flow_str); | |
1455 | free(flow_str); | |
1456 | } | |
1457 | } | |
1458 | } |