]>
Commit | Line | Data |
---|---|---|
09246b99 | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2010, 2011, 2012 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; |
b5ae8913 | 151 | } else if (header != OXM_OF_IN_PORT) { |
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 | } | |
b5ae8913 SH |
173 | } else { |
174 | /* Special case for 32bit ports when using OXM, | |
175 | * ports are 16 bits wide otherwise. */ | |
176 | ovs_be32 port_of11; | |
177 | uint16_t port; | |
178 | ||
179 | memcpy(&port_of11, p + 4, sizeof port_of11); | |
180 | error = ofputil_port_from_ofp11(port_of11, &port); | |
181 | if (!error) { | |
81a76618 | 182 | match_set_in_port(match, port); |
b5ae8913 | 183 | } |
09246b99 | 184 | } |
6a885fd0 | 185 | |
e729e793 JP |
186 | /* Check if the match is for a cookie rather than a classifier rule. */ |
187 | if ((header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W) && cookie) { | |
188 | if (*cookie_mask) { | |
2e0525bc | 189 | error = OFPERR_OFPBMC_DUP_FIELD; |
e729e793 JP |
190 | } else { |
191 | unsigned int width = sizeof *cookie; | |
192 | ||
193 | memcpy(cookie, p + 4, width); | |
194 | if (NXM_HASMASK(header)) { | |
195 | memcpy(cookie_mask, p + 4 + width, width); | |
196 | } else { | |
197 | *cookie_mask = htonll(UINT64_MAX); | |
198 | } | |
199 | error = 0; | |
200 | } | |
201 | } | |
202 | ||
09246b99 | 203 | if (error) { |
28da1f8f BP |
204 | VLOG_DBG_RL(&rl, "bad nxm_entry %#08"PRIx32" (vendor=%"PRIu32", " |
205 | "field=%"PRIu32", hasmask=%"PRIu32", len=%"PRIu32"), " | |
206 | "(%s)", header, | |
09246b99 | 207 | NXM_VENDOR(header), NXM_FIELD(header), |
28da1f8f | 208 | NXM_HASMASK(header), NXM_LENGTH(header), |
90bf1e07 | 209 | ofperr_to_string(error)); |
09246b99 BP |
210 | return error; |
211 | } | |
09246b99 BP |
212 | } |
213 | ||
2e0525bc | 214 | return match_len ? OFPERR_OFPBMC_BAD_LEN : 0; |
09246b99 | 215 | } |
102ce766 | 216 | |
7623f4dd SH |
217 | static enum ofperr |
218 | nx_pull_match__(struct ofpbuf *b, unsigned int match_len, bool strict, | |
81a76618 | 219 | struct match *match, |
7623f4dd SH |
220 | ovs_be64 *cookie, ovs_be64 *cookie_mask) |
221 | { | |
222 | uint8_t *p = NULL; | |
223 | ||
224 | if (match_len) { | |
225 | p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8)); | |
226 | if (!p) { | |
227 | VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a " | |
228 | "multiple of 8, is longer than space in message (max " | |
229 | "length %zu)", match_len, b->size); | |
230 | return OFPERR_OFPBMC_BAD_LEN; | |
231 | } | |
232 | } | |
233 | ||
81a76618 | 234 | return nx_pull_raw(p, match_len, strict, match, cookie, cookie_mask); |
7623f4dd SH |
235 | } |
236 | ||
102ce766 | 237 | /* Parses the nx_match formatted match description in 'b' with length |
81a76618 BP |
238 | * 'match_len'. Stores the results in 'match'. If 'cookie' and 'cookie_mask' |
239 | * are valid pointers, then stores the cookie and mask in them if 'b' contains | |
240 | * a "NXM_NX_COOKIE*" match. Otherwise, stores 0 in both. | |
102ce766 | 241 | * |
81a76618 | 242 | * Fails with an error upon encountering an unknown NXM header. |
102ce766 EJ |
243 | * |
244 | * Returns 0 if successful, otherwise an OpenFlow error code. */ | |
90bf1e07 | 245 | enum ofperr |
81a76618 | 246 | nx_pull_match(struct ofpbuf *b, unsigned int match_len, struct match *match, |
102ce766 EJ |
247 | ovs_be64 *cookie, ovs_be64 *cookie_mask) |
248 | { | |
81a76618 | 249 | return nx_pull_match__(b, match_len, true, match, cookie, cookie_mask); |
102ce766 EJ |
250 | } |
251 | ||
81a76618 BP |
252 | /* Behaves the same as nx_pull_match(), but skips over unknown NXM headers, |
253 | * instead of failing with an error. */ | |
90bf1e07 | 254 | enum ofperr |
102ce766 | 255 | nx_pull_match_loose(struct ofpbuf *b, unsigned int match_len, |
81a76618 | 256 | struct match *match, |
102ce766 EJ |
257 | ovs_be64 *cookie, ovs_be64 *cookie_mask) |
258 | { | |
81a76618 | 259 | return nx_pull_match__(b, match_len, false, match, cookie, cookie_mask); |
102ce766 | 260 | } |
7623f4dd SH |
261 | |
262 | static enum ofperr | |
81a76618 | 263 | oxm_pull_match__(struct ofpbuf *b, bool strict, struct match *match) |
7623f4dd SH |
264 | { |
265 | struct ofp11_match_header *omh = b->data; | |
266 | uint8_t *p; | |
267 | uint16_t match_len; | |
268 | ||
269 | if (b->size < sizeof *omh) { | |
270 | return OFPERR_OFPBMC_BAD_LEN; | |
271 | } | |
272 | ||
273 | match_len = ntohs(omh->length); | |
274 | if (match_len < sizeof *omh) { | |
275 | return OFPERR_OFPBMC_BAD_LEN; | |
276 | } | |
277 | ||
278 | if (omh->type != htons(OFPMT_OXM)) { | |
279 | return OFPERR_OFPBMC_BAD_TYPE; | |
280 | } | |
281 | ||
282 | p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8)); | |
283 | if (!p) { | |
284 | VLOG_DBG_RL(&rl, "oxm length %u, rounded up to a " | |
285 | "multiple of 8, is longer than space in message (max " | |
286 | "length %zu)", match_len, b->size); | |
287 | return OFPERR_OFPBMC_BAD_LEN; | |
288 | } | |
289 | ||
290 | return nx_pull_raw(p + sizeof *omh, match_len - sizeof *omh, | |
81a76618 | 291 | strict, match, NULL, NULL); |
7623f4dd SH |
292 | } |
293 | ||
81a76618 BP |
294 | /* Parses the oxm formatted match description preceeded by a struct ofp11_match |
295 | * in 'b' with length 'match_len'. Stores the result in 'match'. | |
7623f4dd SH |
296 | * |
297 | * Fails with an error when encountering unknown OXM headers. | |
298 | * | |
299 | * Returns 0 if successful, otherwise an OpenFlow error code. */ | |
300 | enum ofperr | |
81a76618 | 301 | oxm_pull_match(struct ofpbuf *b, struct match *match) |
7623f4dd | 302 | { |
81a76618 | 303 | return oxm_pull_match__(b, true, match); |
7623f4dd SH |
304 | } |
305 | ||
306 | /* Behaves the same as oxm_pull_match() with one exception. Skips over unknown | |
307 | * PXM headers instead of failing with an error when they are encountered. */ | |
308 | enum ofperr | |
81a76618 | 309 | oxm_pull_match_loose(struct ofpbuf *b, struct match *match) |
7623f4dd | 310 | { |
81a76618 | 311 | return oxm_pull_match__(b, false, match); |
7623f4dd | 312 | } |
09246b99 BP |
313 | \f |
314 | /* nx_put_match() and helpers. | |
315 | * | |
316 | * 'put' functions whose names end in 'w' add a wildcarded field. | |
317 | * 'put' functions whose names end in 'm' add a field that might be wildcarded. | |
318 | * Other 'put' functions add exact-match fields. | |
319 | */ | |
320 | ||
321 | static void | |
322 | nxm_put_header(struct ofpbuf *b, uint32_t header) | |
323 | { | |
324 | ovs_be32 n_header = htonl(header); | |
325 | ofpbuf_put(b, &n_header, sizeof n_header); | |
326 | } | |
327 | ||
328 | static void | |
329 | nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value) | |
330 | { | |
331 | nxm_put_header(b, header); | |
332 | ofpbuf_put(b, &value, sizeof value); | |
333 | } | |
334 | ||
7257b535 BP |
335 | static void |
336 | nxm_put_8m(struct ofpbuf *b, uint32_t header, uint8_t value, uint8_t mask) | |
337 | { | |
338 | switch (mask) { | |
339 | case 0: | |
340 | break; | |
341 | ||
342 | case UINT8_MAX: | |
343 | nxm_put_8(b, header, value); | |
344 | break; | |
345 | ||
346 | default: | |
347 | nxm_put_header(b, NXM_MAKE_WILD_HEADER(header)); | |
348 | ofpbuf_put(b, &value, sizeof value); | |
349 | ofpbuf_put(b, &mask, sizeof mask); | |
350 | } | |
351 | } | |
352 | ||
09246b99 BP |
353 | static void |
354 | nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value) | |
355 | { | |
356 | nxm_put_header(b, header); | |
357 | ofpbuf_put(b, &value, sizeof value); | |
358 | } | |
359 | ||
360 | static void | |
361 | nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask) | |
362 | { | |
363 | nxm_put_header(b, header); | |
364 | ofpbuf_put(b, &value, sizeof value); | |
365 | ofpbuf_put(b, &mask, sizeof mask); | |
366 | } | |
367 | ||
66642cb4 BP |
368 | static void |
369 | nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask) | |
370 | { | |
371 | switch (mask) { | |
372 | case 0: | |
373 | break; | |
374 | ||
375 | case CONSTANT_HTONS(UINT16_MAX): | |
376 | nxm_put_16(b, header, value); | |
377 | break; | |
378 | ||
379 | default: | |
380 | nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask); | |
381 | break; | |
382 | } | |
383 | } | |
384 | ||
09246b99 BP |
385 | static void |
386 | nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value) | |
387 | { | |
388 | nxm_put_header(b, header); | |
389 | ofpbuf_put(b, &value, sizeof value); | |
390 | } | |
391 | ||
392 | static void | |
393 | nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask) | |
394 | { | |
395 | nxm_put_header(b, header); | |
396 | ofpbuf_put(b, &value, sizeof value); | |
397 | ofpbuf_put(b, &mask, sizeof mask); | |
398 | } | |
399 | ||
400 | static void | |
401 | nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask) | |
402 | { | |
403 | switch (mask) { | |
404 | case 0: | |
405 | break; | |
406 | ||
66642cb4 | 407 | case CONSTANT_HTONL(UINT32_MAX): |
09246b99 BP |
408 | nxm_put_32(b, header, value); |
409 | break; | |
410 | ||
411 | default: | |
412 | nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask); | |
413 | break; | |
414 | } | |
415 | } | |
416 | ||
417 | static void | |
418 | nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value) | |
419 | { | |
420 | nxm_put_header(b, header); | |
421 | ofpbuf_put(b, &value, sizeof value); | |
422 | } | |
423 | ||
8368c090 BP |
424 | static void |
425 | nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask) | |
426 | { | |
427 | nxm_put_header(b, header); | |
428 | ofpbuf_put(b, &value, sizeof value); | |
429 | ofpbuf_put(b, &mask, sizeof mask); | |
430 | } | |
431 | ||
432 | static void | |
433 | nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask) | |
434 | { | |
435 | switch (mask) { | |
436 | case 0: | |
437 | break; | |
438 | ||
439 | case CONSTANT_HTONLL(UINT64_MAX): | |
440 | nxm_put_64(b, header, value); | |
441 | break; | |
442 | ||
443 | default: | |
444 | nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask); | |
445 | break; | |
446 | } | |
447 | } | |
448 | ||
09246b99 BP |
449 | static void |
450 | nxm_put_eth(struct ofpbuf *b, uint32_t header, | |
451 | const uint8_t value[ETH_ADDR_LEN]) | |
452 | { | |
453 | nxm_put_header(b, header); | |
454 | ofpbuf_put(b, value, ETH_ADDR_LEN); | |
455 | } | |
456 | ||
1e37a2d7 | 457 | static void |
73c0ce34 JS |
458 | nxm_put_eth_masked(struct ofpbuf *b, uint32_t header, |
459 | const uint8_t value[ETH_ADDR_LEN], | |
460 | const uint8_t mask[ETH_ADDR_LEN]) | |
1e37a2d7 | 461 | { |
73c0ce34 JS |
462 | if (!eth_addr_is_zero(mask)) { |
463 | if (eth_mask_is_exact(mask)) { | |
464 | nxm_put_eth(b, header, value); | |
465 | } else { | |
466 | nxm_put_header(b, NXM_MAKE_WILD_HEADER(header)); | |
467 | ofpbuf_put(b, value, ETH_ADDR_LEN); | |
468 | ofpbuf_put(b, mask, ETH_ADDR_LEN); | |
469 | } | |
1e37a2d7 BP |
470 | } |
471 | } | |
472 | ||
d31f1109 JP |
473 | static void |
474 | nxm_put_ipv6(struct ofpbuf *b, uint32_t header, | |
475 | const struct in6_addr *value, const struct in6_addr *mask) | |
476 | { | |
477 | if (ipv6_mask_is_any(mask)) { | |
478 | return; | |
479 | } else if (ipv6_mask_is_exact(mask)) { | |
480 | nxm_put_header(b, header); | |
481 | ofpbuf_put(b, value, sizeof *value); | |
482 | } else { | |
483 | nxm_put_header(b, NXM_MAKE_WILD_HEADER(header)); | |
484 | ofpbuf_put(b, value, sizeof *value); | |
485 | ofpbuf_put(b, mask, sizeof *mask); | |
486 | } | |
487 | } | |
488 | ||
7257b535 | 489 | static void |
81a76618 | 490 | nxm_put_frag(struct ofpbuf *b, const struct match *match) |
7257b535 | 491 | { |
81a76618 BP |
492 | uint8_t nw_frag = match->flow.nw_frag; |
493 | uint8_t nw_frag_mask = match->wc.masks.nw_frag; | |
7257b535 | 494 | |
eadef313 | 495 | switch (nw_frag_mask) { |
7257b535 BP |
496 | case 0: |
497 | break; | |
498 | ||
eadef313 JP |
499 | case FLOW_NW_FRAG_MASK: |
500 | nxm_put_8(b, NXM_NX_IP_FRAG, nw_frag); | |
7257b535 BP |
501 | break; |
502 | ||
503 | default: | |
eadef313 JP |
504 | nxm_put_8m(b, NXM_NX_IP_FRAG, nw_frag, |
505 | nw_frag_mask & FLOW_NW_FRAG_MASK); | |
7257b535 BP |
506 | break; |
507 | } | |
508 | } | |
509 | ||
8e7082b0 | 510 | static void |
81a76618 | 511 | nxm_put_ip(struct ofpbuf *b, const struct match *match, |
b5ae8913 SH |
512 | uint8_t icmp_proto, uint32_t icmp_type, uint32_t icmp_code, |
513 | bool oxm) | |
8e7082b0 | 514 | { |
81a76618 | 515 | const struct flow *flow = &match->flow; |
8e7082b0 | 516 | |
81a76618 | 517 | nxm_put_frag(b, match); |
8e7082b0 | 518 | |
81a76618 | 519 | if (match->wc.masks.nw_tos & IP_DSCP_MASK) { |
b5ae8913 SH |
520 | nxm_put_8(b, oxm ? OXM_OF_IP_DSCP : NXM_OF_IP_TOS, |
521 | flow->nw_tos & IP_DSCP_MASK); | |
8e7082b0 BP |
522 | } |
523 | ||
81a76618 | 524 | if (match->wc.masks.nw_tos & IP_ECN_MASK) { |
b5ae8913 SH |
525 | nxm_put_8(b, oxm ? OXM_OF_IP_ECN : NXM_NX_IP_ECN, |
526 | flow->nw_tos & IP_ECN_MASK); | |
8e7082b0 BP |
527 | } |
528 | ||
81a76618 | 529 | if (!oxm && match->wc.masks.nw_ttl) { |
8e7082b0 BP |
530 | nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl); |
531 | } | |
532 | ||
81a76618 | 533 | if (match->wc.masks.nw_proto) { |
b5ae8913 | 534 | nxm_put_8(b, oxm ? OXM_OF_IP_PROTO : NXM_OF_IP_PROTO, flow->nw_proto); |
8e7082b0 BP |
535 | |
536 | if (flow->nw_proto == IPPROTO_TCP) { | |
b5ae8913 | 537 | nxm_put_16m(b, oxm ? OXM_OF_TCP_SRC : NXM_OF_TCP_SRC, |
81a76618 | 538 | flow->tp_src, match->wc.masks.tp_src); |
b5ae8913 | 539 | nxm_put_16m(b, oxm ? OXM_OF_TCP_DST : NXM_OF_TCP_DST, |
81a76618 | 540 | flow->tp_dst, match->wc.masks.tp_dst); |
8e7082b0 | 541 | } else if (flow->nw_proto == IPPROTO_UDP) { |
b5ae8913 | 542 | nxm_put_16m(b, oxm ? OXM_OF_UDP_SRC : NXM_OF_UDP_SRC, |
81a76618 | 543 | flow->tp_src, match->wc.masks.tp_src); |
b5ae8913 | 544 | nxm_put_16m(b, oxm ? OXM_OF_UDP_DST : NXM_OF_UDP_DST, |
81a76618 | 545 | flow->tp_dst, match->wc.masks.tp_dst); |
8e7082b0 | 546 | } else if (flow->nw_proto == icmp_proto) { |
81a76618 | 547 | if (match->wc.masks.tp_src) { |
8e7082b0 BP |
548 | nxm_put_8(b, icmp_type, ntohs(flow->tp_src)); |
549 | } | |
81a76618 | 550 | if (match->wc.masks.tp_dst) { |
8e7082b0 BP |
551 | nxm_put_8(b, icmp_code, ntohs(flow->tp_dst)); |
552 | } | |
553 | } | |
554 | } | |
555 | } | |
556 | ||
81a76618 | 557 | /* Appends to 'b' the nx_match format that expresses 'match'. For Flow Mod and |
7623f4dd SH |
558 | * Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied. |
559 | * Otherwise, 'cookie_mask' should be zero. | |
4d0ed519 BP |
560 | * |
561 | * This function can cause 'b''s data to be reallocated. | |
562 | * | |
563 | * Returns the number of bytes appended to 'b', excluding padding. | |
564 | * | |
81a76618 | 565 | * If 'match' is a catch-all rule that matches every packet, then this function |
4d0ed519 | 566 | * appends nothing to 'b' and returns 0. */ |
7623f4dd | 567 | static int |
81a76618 | 568 | nx_put_raw(struct ofpbuf *b, bool oxm, const struct match *match, |
7623f4dd | 569 | ovs_be64 cookie, ovs_be64 cookie_mask) |
09246b99 | 570 | { |
81a76618 | 571 | const struct flow *flow = &match->flow; |
09246b99 | 572 | const size_t start_len = b->size; |
09246b99 | 573 | int match_len; |
b6c9e612 | 574 | int i; |
09246b99 | 575 | |
b02475c5 | 576 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 19); |
a877206f | 577 | |
09246b99 | 578 | /* Metadata. */ |
81a76618 | 579 | if (match->wc.masks.in_port) { |
09246b99 | 580 | uint16_t in_port = flow->in_port; |
b5ae8913 SH |
581 | if (oxm) { |
582 | nxm_put_32(b, OXM_OF_IN_PORT, ofputil_port_to_ofp11(in_port)); | |
583 | } else { | |
584 | nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port)); | |
585 | } | |
09246b99 BP |
586 | } |
587 | ||
588 | /* Ethernet. */ | |
b5ae8913 | 589 | nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_SRC : NXM_OF_ETH_SRC, |
81a76618 | 590 | flow->dl_src, match->wc.masks.dl_src); |
b5ae8913 | 591 | nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_DST : NXM_OF_ETH_DST, |
81a76618 | 592 | flow->dl_dst, match->wc.masks.dl_dst); |
e2170cff BP |
593 | nxm_put_16m(b, oxm ? OXM_OF_ETH_TYPE : NXM_OF_ETH_TYPE, |
594 | ofputil_dl_type_to_openflow(flow->dl_type), | |
81a76618 | 595 | match->wc.masks.dl_type); |
09246b99 | 596 | |
95f61ba8 SH |
597 | /* 802.1Q. */ |
598 | if (oxm) { | |
26720e24 BP |
599 | ovs_be16 VID_CFI_MASK = htons(VLAN_VID_MASK | VLAN_CFI); |
600 | ovs_be16 vid = flow->vlan_tci & VID_CFI_MASK; | |
81a76618 | 601 | ovs_be16 mask = match->wc.masks.vlan_tci & VID_CFI_MASK; |
95f61ba8 SH |
602 | |
603 | if (mask == htons(VLAN_VID_MASK | VLAN_CFI)) { | |
604 | nxm_put_16(b, OXM_OF_VLAN_VID, vid); | |
605 | } else if (mask) { | |
606 | nxm_put_16m(b, OXM_OF_VLAN_VID, vid, mask); | |
607 | } | |
608 | ||
81a76618 | 609 | if (vid && vlan_tci_to_pcp(match->wc.masks.vlan_tci)) { |
95f61ba8 SH |
610 | nxm_put_8(b, OXM_OF_VLAN_PCP, vlan_tci_to_pcp(flow->vlan_tci)); |
611 | } | |
612 | ||
613 | } else { | |
81a76618 BP |
614 | nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, |
615 | match->wc.masks.vlan_tci); | |
95f61ba8 | 616 | } |
09246b99 | 617 | |
b02475c5 SH |
618 | /* MPLS. */ |
619 | if (eth_type_mpls(flow->dl_type)) { | |
620 | if (match->wc.masks.mpls_lse & htonl(MPLS_TC_MASK)) { | |
621 | nxm_put_8(b, OXM_OF_MPLS_TC, mpls_lse_to_tc(flow->mpls_lse)); | |
622 | } | |
623 | ||
624 | if (match->wc.masks.mpls_lse & htonl(MPLS_BOS_MASK)) { | |
625 | nxm_put_8(b, OXM_OF_MPLS_BOS, mpls_lse_to_bos(flow->mpls_lse)); | |
626 | } | |
627 | ||
628 | if (match->wc.masks.mpls_lse & htonl(MPLS_LABEL_MASK)) { | |
629 | nxm_put_32(b, OXM_OF_MPLS_LABEL, | |
630 | htonl(mpls_lse_to_label(flow->mpls_lse))); | |
631 | } | |
632 | } | |
633 | ||
66642cb4 | 634 | /* L3. */ |
e2170cff | 635 | if (flow->dl_type == htons(ETH_TYPE_IP)) { |
09246b99 | 636 | /* IP. */ |
b5ae8913 | 637 | nxm_put_32m(b, oxm ? OXM_OF_IPV4_SRC : NXM_OF_IP_SRC, |
81a76618 | 638 | flow->nw_src, match->wc.masks.nw_src); |
b5ae8913 | 639 | nxm_put_32m(b, oxm ? OXM_OF_IPV4_DST : NXM_OF_IP_DST, |
81a76618 BP |
640 | flow->nw_dst, match->wc.masks.nw_dst); |
641 | nxm_put_ip(b, match, IPPROTO_ICMP, | |
b5ae8913 SH |
642 | oxm ? OXM_OF_ICMPV4_TYPE : NXM_OF_ICMP_TYPE, |
643 | oxm ? OXM_OF_ICMPV4_CODE : NXM_OF_ICMP_CODE, oxm); | |
e2170cff | 644 | } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) { |
d31f1109 | 645 | /* IPv6. */ |
b5ae8913 | 646 | nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_SRC : NXM_NX_IPV6_SRC, |
81a76618 | 647 | &flow->ipv6_src, &match->wc.masks.ipv6_src); |
b5ae8913 | 648 | nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_DST : NXM_NX_IPV6_DST, |
81a76618 BP |
649 | &flow->ipv6_dst, &match->wc.masks.ipv6_dst); |
650 | nxm_put_ip(b, match, IPPROTO_ICMPV6, | |
b5ae8913 SH |
651 | oxm ? OXM_OF_ICMPV6_TYPE : NXM_NX_ICMPV6_TYPE, |
652 | oxm ? OXM_OF_ICMPV6_CODE : NXM_NX_ICMPV6_CODE, oxm); | |
9e44d715 | 653 | |
32455024 | 654 | nxm_put_32m(b, oxm ? OXM_OF_IPV6_FLABEL : NXM_NX_IPV6_LABEL, |
81a76618 | 655 | flow->ipv6_label, match->wc.masks.ipv6_label); |
d31f1109 | 656 | |
8e7082b0 BP |
657 | if (flow->nw_proto == IPPROTO_ICMPV6 |
658 | && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) || | |
659 | flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) { | |
b5ae8913 | 660 | nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_ND_TARGET : NXM_NX_ND_TARGET, |
81a76618 | 661 | &flow->nd_target, &match->wc.masks.nd_target); |
e878338b SH |
662 | if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) { |
663 | nxm_put_eth_masked(b, oxm ? OXM_OF_IPV6_ND_SLL : NXM_NX_ND_SLL, | |
81a76618 | 664 | flow->arp_sha, match->wc.masks.arp_sha); |
8e7082b0 | 665 | } |
e878338b SH |
666 | if (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) { |
667 | nxm_put_eth_masked(b, oxm ? OXM_OF_IPV6_ND_TLL : NXM_NX_ND_TLL, | |
81a76618 | 668 | flow->arp_tha, match->wc.masks.arp_tha); |
d31f1109 JP |
669 | } |
670 | } | |
8087f5ff MM |
671 | } else if (flow->dl_type == htons(ETH_TYPE_ARP) || |
672 | flow->dl_type == htons(ETH_TYPE_RARP)) { | |
09246b99 | 673 | /* ARP. */ |
81a76618 | 674 | if (match->wc.masks.nw_proto) { |
b5ae8913 SH |
675 | nxm_put_16(b, oxm ? OXM_OF_ARP_OP : NXM_OF_ARP_OP, |
676 | htons(flow->nw_proto)); | |
09246b99 | 677 | } |
b5ae8913 | 678 | nxm_put_32m(b, oxm ? OXM_OF_ARP_SPA : NXM_OF_ARP_SPA, |
81a76618 | 679 | flow->nw_src, match->wc.masks.nw_src); |
b5ae8913 | 680 | nxm_put_32m(b, oxm ? OXM_OF_ARP_TPA : NXM_OF_ARP_TPA, |
81a76618 | 681 | flow->nw_dst, match->wc.masks.nw_dst); |
e878338b | 682 | nxm_put_eth_masked(b, oxm ? OXM_OF_ARP_SHA : NXM_NX_ARP_SHA, |
81a76618 | 683 | flow->arp_sha, match->wc.masks.arp_sha); |
e878338b | 684 | nxm_put_eth_masked(b, oxm ? OXM_OF_ARP_THA : NXM_NX_ARP_THA, |
81a76618 | 685 | flow->arp_tha, match->wc.masks.arp_tha); |
09246b99 BP |
686 | } |
687 | ||
688 | /* Tunnel ID. */ | |
2f2eb46c JR |
689 | nxm_put_64m(b, oxm ? OXM_OF_TUNNEL_ID : NXM_NX_TUN_ID, |
690 | flow->tunnel.tun_id, match->wc.masks.tunnel.tun_id); | |
09246b99 | 691 | |
b6c9e612 BP |
692 | /* Registers. */ |
693 | for (i = 0; i < FLOW_N_REGS; i++) { | |
694 | nxm_put_32m(b, NXM_NX_REG(i), | |
81a76618 | 695 | htonl(flow->regs[i]), htonl(match->wc.masks.regs[i])); |
b6c9e612 BP |
696 | } |
697 | ||
969fc56c | 698 | /* OpenFlow 1.1+ Metadata. */ |
81a76618 | 699 | nxm_put_64m(b, OXM_OF_METADATA, flow->metadata, match->wc.masks.metadata); |
969fc56c | 700 | |
e729e793 JP |
701 | /* Cookie. */ |
702 | nxm_put_64m(b, NXM_NX_COOKIE, cookie, cookie_mask); | |
703 | ||
09246b99 | 704 | match_len = b->size - start_len; |
7623f4dd SH |
705 | return match_len; |
706 | } | |
707 | ||
81a76618 | 708 | /* Appends to 'b' the nx_match format that expresses 'match', plus enough zero |
7623f4dd SH |
709 | * bytes to pad the nx_match out to a multiple of 8. For Flow Mod and Flow |
710 | * Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied. | |
711 | * Otherwise, 'cookie_mask' should be zero. | |
712 | * | |
713 | * This function can cause 'b''s data to be reallocated. | |
714 | * | |
715 | * Returns the number of bytes appended to 'b', excluding padding. The return | |
716 | * value can be zero if it appended nothing at all to 'b' (which happens if | |
717 | * 'cr' is a catch-all rule that matches every packet). */ | |
718 | int | |
81a76618 | 719 | nx_put_match(struct ofpbuf *b, const struct match *match, |
7623f4dd SH |
720 | ovs_be64 cookie, ovs_be64 cookie_mask) |
721 | { | |
81a76618 | 722 | int match_len = nx_put_raw(b, false, match, cookie, cookie_mask); |
7623f4dd SH |
723 | |
724 | ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len); | |
725 | return match_len; | |
726 | } | |
727 | ||
728 | ||
729 | /* Appends to 'b' an struct ofp11_match_header followed by the oxm format that | |
81a76618 BP |
730 | * expresses 'cr', plus enough zero bytes to pad the data appended out to a |
731 | * multiple of 8. | |
7623f4dd SH |
732 | * |
733 | * This function can cause 'b''s data to be reallocated. | |
734 | * | |
735 | * Returns the number of bytes appended to 'b', excluding the padding. Never | |
736 | * returns zero. */ | |
737 | int | |
81a76618 | 738 | oxm_put_match(struct ofpbuf *b, const struct match *match) |
7623f4dd SH |
739 | { |
740 | int match_len; | |
741 | struct ofp11_match_header *omh; | |
742 | size_t start_len = b->size; | |
743 | ovs_be64 cookie = htonll(0), cookie_mask = htonll(0); | |
744 | ||
745 | ofpbuf_put_uninit(b, sizeof *omh); | |
81a76618 | 746 | match_len = nx_put_raw(b, true, match, cookie, cookie_mask) + sizeof *omh; |
09246b99 | 747 | ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len); |
7623f4dd SH |
748 | |
749 | omh = (struct ofp11_match_header *)((char *)b->data + start_len); | |
750 | omh->type = htons(OFPMT_OXM); | |
751 | omh->length = htons(match_len); | |
752 | ||
09246b99 BP |
753 | return match_len; |
754 | } | |
755 | \f | |
756 | /* nx_match_to_string() and helpers. */ | |
757 | ||
f393f81e BP |
758 | static void format_nxm_field_name(struct ds *, uint32_t header); |
759 | ||
09246b99 BP |
760 | char * |
761 | nx_match_to_string(const uint8_t *p, unsigned int match_len) | |
762 | { | |
763 | uint32_t header; | |
764 | struct ds s; | |
765 | ||
766 | if (!match_len) { | |
767 | return xstrdup("<any>"); | |
768 | } | |
769 | ||
770 | ds_init(&s); | |
771 | while ((header = nx_entry_ok(p, match_len)) != 0) { | |
772 | unsigned int length = NXM_LENGTH(header); | |
773 | unsigned int value_len = nxm_field_bytes(header); | |
774 | const uint8_t *value = p + 4; | |
775 | const uint8_t *mask = value + value_len; | |
09246b99 BP |
776 | unsigned int i; |
777 | ||
778 | if (s.length) { | |
779 | ds_put_cstr(&s, ", "); | |
780 | } | |
781 | ||
f393f81e | 782 | format_nxm_field_name(&s, header); |
09246b99 BP |
783 | ds_put_char(&s, '('); |
784 | ||
785 | for (i = 0; i < value_len; i++) { | |
786 | ds_put_format(&s, "%02x", value[i]); | |
787 | } | |
788 | if (NXM_HASMASK(header)) { | |
789 | ds_put_char(&s, '/'); | |
790 | for (i = 0; i < value_len; i++) { | |
791 | ds_put_format(&s, "%02x", mask[i]); | |
792 | } | |
793 | } | |
794 | ds_put_char(&s, ')'); | |
795 | ||
796 | p += 4 + length; | |
797 | match_len -= 4 + length; | |
798 | } | |
799 | ||
800 | if (match_len) { | |
801 | if (s.length) { | |
802 | ds_put_cstr(&s, ", "); | |
803 | } | |
804 | ||
805 | ds_put_format(&s, "<%u invalid bytes>", match_len); | |
806 | } | |
807 | ||
808 | return ds_steal_cstr(&s); | |
809 | } | |
810 | ||
7623f4dd SH |
811 | char * |
812 | oxm_match_to_string(const uint8_t *p, unsigned int match_len) | |
813 | { | |
814 | const struct ofp11_match_header *omh = (struct ofp11_match_header *)p; | |
815 | uint16_t match_len_; | |
816 | struct ds s; | |
817 | ||
818 | ds_init(&s); | |
819 | ||
820 | if (match_len < sizeof *omh) { | |
821 | ds_put_format(&s, "<match too short: %u>", match_len); | |
822 | goto err; | |
823 | } | |
824 | ||
825 | if (omh->type != htons(OFPMT_OXM)) { | |
826 | ds_put_format(&s, "<bad match type field: %u>", ntohs(omh->type)); | |
827 | goto err; | |
828 | } | |
829 | ||
830 | match_len_ = ntohs(omh->length); | |
831 | if (match_len_ < sizeof *omh) { | |
832 | ds_put_format(&s, "<match length field too short: %u>", match_len_); | |
833 | goto err; | |
834 | } | |
835 | ||
836 | if (match_len_ != match_len) { | |
837 | ds_put_format(&s, "<match length field incorrect: %u != %u>", | |
838 | match_len_, match_len); | |
839 | goto err; | |
840 | } | |
841 | ||
842 | return nx_match_to_string(p + sizeof *omh, match_len - sizeof *omh); | |
843 | ||
844 | err: | |
845 | return ds_steal_cstr(&s); | |
846 | } | |
847 | ||
f393f81e BP |
848 | static void |
849 | format_nxm_field_name(struct ds *s, uint32_t header) | |
850 | { | |
28da1f8f BP |
851 | const struct mf_field *mf = mf_from_nxm_header(header); |
852 | if (mf) { | |
b5ae8913 | 853 | ds_put_cstr(s, IS_OXM_HEADER(header) ? mf->oxm_name : mf->nxm_name); |
28da1f8f BP |
854 | if (NXM_HASMASK(header)) { |
855 | ds_put_cstr(s, "_W"); | |
856 | } | |
e729e793 JP |
857 | } else if (header == NXM_NX_COOKIE) { |
858 | ds_put_cstr(s, "NXM_NX_COOKIE"); | |
859 | } else if (header == NXM_NX_COOKIE_W) { | |
860 | ds_put_cstr(s, "NXM_NX_COOKIE_W"); | |
f393f81e BP |
861 | } else { |
862 | ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header)); | |
863 | } | |
864 | } | |
865 | ||
558d80cb BP |
866 | static uint32_t |
867 | parse_nxm_field_name(const char *name, int name_len) | |
09246b99 | 868 | { |
28da1f8f BP |
869 | bool wild; |
870 | int i; | |
09246b99 | 871 | |
558d80cb | 872 | /* Check whether it's a field name. */ |
28da1f8f BP |
873 | wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2); |
874 | if (wild) { | |
875 | name_len -= 2; | |
876 | } | |
e729e793 | 877 | |
28da1f8f BP |
878 | for (i = 0; i < MFF_N_IDS; i++) { |
879 | const struct mf_field *mf = mf_from_id(i); | |
b5ae8913 SH |
880 | uint32_t header; |
881 | ||
882 | if (mf->nxm_name && | |
883 | !strncmp(mf->nxm_name, name, name_len) && | |
884 | mf->nxm_name[name_len] == '\0') { | |
885 | header = mf->nxm_header; | |
886 | } else if (mf->oxm_name && | |
887 | !strncmp(mf->oxm_name, name, name_len) && | |
888 | mf->oxm_name[name_len] == '\0') { | |
889 | header = mf->oxm_header; | |
890 | } else { | |
891 | continue; | |
892 | } | |
28da1f8f | 893 | |
b5ae8913 SH |
894 | if (!wild) { |
895 | return header; | |
896 | } else if (mf->maskable != MFM_NONE) { | |
897 | return NXM_MAKE_WILD_HEADER(header); | |
09246b99 BP |
898 | } |
899 | } | |
900 | ||
b5ae8913 SH |
901 | if (!strncmp("NXM_NX_COOKIE", name, name_len) && |
902 | (name_len == strlen("NXM_NX_COOKIE"))) { | |
e729e793 JP |
903 | if (!wild) { |
904 | return NXM_NX_COOKIE; | |
905 | } else { | |
906 | return NXM_NX_COOKIE_W; | |
907 | } | |
908 | } | |
909 | ||
558d80cb BP |
910 | /* Check whether it's a 32-bit field header value as hex. |
911 | * (This isn't ordinarily useful except for testing error behavior.) */ | |
912 | if (name_len == 8) { | |
913 | uint32_t header = hexits_value(name, name_len, NULL); | |
914 | if (header != UINT_MAX) { | |
915 | return header; | |
916 | } | |
917 | } | |
918 | ||
919 | return 0; | |
09246b99 | 920 | } |
09246b99 BP |
921 | \f |
922 | /* nx_match_from_string(). */ | |
923 | ||
7623f4dd SH |
924 | static int |
925 | nx_match_from_string_raw(const char *s, struct ofpbuf *b) | |
09246b99 BP |
926 | { |
927 | const char *full_s = s; | |
928 | const size_t start_len = b->size; | |
09246b99 BP |
929 | |
930 | if (!strcmp(s, "<any>")) { | |
931 | /* Ensure that 'b->data' isn't actually null. */ | |
932 | ofpbuf_prealloc_tailroom(b, 1); | |
933 | return 0; | |
934 | } | |
935 | ||
936 | for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) { | |
558d80cb BP |
937 | const char *name; |
938 | uint32_t header; | |
09246b99 | 939 | int name_len; |
78090f63 | 940 | size_t n; |
09246b99 | 941 | |
558d80cb | 942 | name = s; |
09246b99 BP |
943 | name_len = strcspn(s, "("); |
944 | if (s[name_len] != '(') { | |
945 | ovs_fatal(0, "%s: missing ( at end of nx_match", full_s); | |
946 | } | |
947 | ||
558d80cb BP |
948 | header = parse_nxm_field_name(name, name_len); |
949 | if (!header) { | |
09246b99 BP |
950 | ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s); |
951 | } | |
952 | ||
953 | s += name_len + 1; | |
954 | ||
558d80cb | 955 | nxm_put_header(b, header); |
78090f63 BP |
956 | s = ofpbuf_put_hex(b, s, &n); |
957 | if (n != nxm_field_bytes(header)) { | |
958 | ovs_fatal(0, "%.2s: hex digits expected", s); | |
959 | } | |
558d80cb | 960 | if (NXM_HASMASK(header)) { |
09246b99 BP |
961 | s += strspn(s, " "); |
962 | if (*s != '/') { | |
558d80cb BP |
963 | ovs_fatal(0, "%s: missing / in masked field %.*s", |
964 | full_s, name_len, name); | |
09246b99 | 965 | } |
78090f63 BP |
966 | s = ofpbuf_put_hex(b, s + 1, &n); |
967 | if (n != nxm_field_bytes(header)) { | |
968 | ovs_fatal(0, "%.2s: hex digits expected", s); | |
969 | } | |
09246b99 BP |
970 | } |
971 | ||
972 | s += strspn(s, " "); | |
973 | if (*s != ')') { | |
558d80cb BP |
974 | ovs_fatal(0, "%s: missing ) following field %.*s", |
975 | full_s, name_len, name); | |
09246b99 BP |
976 | } |
977 | s++; | |
978 | } | |
979 | ||
7623f4dd SH |
980 | return b->size - start_len; |
981 | } | |
982 | ||
983 | int | |
984 | nx_match_from_string(const char *s, struct ofpbuf *b) | |
985 | { | |
986 | int match_len = nx_match_from_string_raw(s, b); | |
987 | ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len); | |
988 | return match_len; | |
989 | } | |
990 | ||
991 | int | |
992 | oxm_match_from_string(const char *s, struct ofpbuf *b) | |
993 | { | |
994 | int match_len; | |
995 | struct ofp11_match_header *omh; | |
996 | size_t start_len = b->size; | |
997 | ||
998 | ofpbuf_put_uninit(b, sizeof *omh); | |
999 | match_len = nx_match_from_string_raw(s, b) + sizeof *omh; | |
09246b99 | 1000 | ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len); |
7623f4dd SH |
1001 | |
1002 | omh = (struct ofp11_match_header *)((char *)b->data + start_len); | |
1003 | omh->type = htons(OFPMT_OXM); | |
1004 | omh->length = htons(match_len); | |
1005 | ||
09246b99 BP |
1006 | return match_len; |
1007 | } | |
b6c9e612 | 1008 | \f |
f393f81e | 1009 | void |
f25d0cf3 | 1010 | nxm_parse_reg_move(struct ofpact_reg_move *move, const char *s) |
f393f81e BP |
1011 | { |
1012 | const char *full_s = s; | |
f393f81e | 1013 | |
f25d0cf3 | 1014 | s = mf_parse_subfield(&move->src, s); |
f393f81e BP |
1015 | if (strncmp(s, "->", 2)) { |
1016 | ovs_fatal(0, "%s: missing `->' following source", full_s); | |
1017 | } | |
1018 | s += 2; | |
f25d0cf3 | 1019 | s = mf_parse_subfield(&move->dst, s); |
f393f81e BP |
1020 | if (*s != '\0') { |
1021 | ovs_fatal(0, "%s: trailing garbage following destination", full_s); | |
1022 | } | |
1023 | ||
f25d0cf3 | 1024 | if (move->src.n_bits != move->dst.n_bits) { |
f393f81e | 1025 | ovs_fatal(0, "%s: source field is %d bits wide but destination is " |
f25d0cf3 BP |
1026 | "%d bits wide", full_s, |
1027 | move->src.n_bits, move->dst.n_bits); | |
f393f81e | 1028 | } |
f393f81e BP |
1029 | } |
1030 | ||
1031 | void | |
f25d0cf3 | 1032 | nxm_parse_reg_load(struct ofpact_reg_load *load, const char *s) |
f393f81e BP |
1033 | { |
1034 | const char *full_s = s; | |
9bab681f | 1035 | uint64_t value = strtoull(s, (char **) &s, 0); |
f393f81e | 1036 | |
f393f81e BP |
1037 | if (strncmp(s, "->", 2)) { |
1038 | ovs_fatal(0, "%s: missing `->' following value", full_s); | |
1039 | } | |
1040 | s += 2; | |
f25d0cf3 | 1041 | s = mf_parse_subfield(&load->dst, s); |
f393f81e BP |
1042 | if (*s != '\0') { |
1043 | ovs_fatal(0, "%s: trailing garbage following destination", full_s); | |
1044 | } | |
1045 | ||
9bab681f | 1046 | if (load->dst.n_bits < 64 && (value >> load->dst.n_bits) != 0) { |
f25d0cf3 | 1047 | ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits", |
9bab681f | 1048 | full_s, value, load->dst.n_bits); |
f393f81e | 1049 | } |
9bab681f | 1050 | |
158edc8d BP |
1051 | load->subvalue.be64[0] = htonll(0); |
1052 | load->subvalue.be64[1] = htonll(value); | |
f393f81e BP |
1053 | } |
1054 | \f | |
1055 | /* nxm_format_reg_move(), nxm_format_reg_load(). */ | |
1056 | ||
f393f81e | 1057 | void |
f25d0cf3 | 1058 | nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s) |
f393f81e | 1059 | { |
f393f81e | 1060 | ds_put_format(s, "move:"); |
f25d0cf3 | 1061 | mf_format_subfield(&move->src, s); |
f393f81e | 1062 | ds_put_cstr(s, "->"); |
f25d0cf3 | 1063 | mf_format_subfield(&move->dst, s); |
f393f81e BP |
1064 | } |
1065 | ||
0291c1c6 SH |
1066 | static void |
1067 | set_field_format(const struct ofpact_reg_load *load, struct ds *s) | |
1068 | { | |
1069 | const struct mf_field *mf = load->dst.field; | |
1070 | union mf_value value; | |
1071 | ||
cb22974d | 1072 | ovs_assert(load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD); |
0291c1c6 SH |
1073 | ds_put_format(s, "set_field:"); |
1074 | memset(&value, 0, sizeof value); | |
1075 | bitwise_copy(&load->subvalue, sizeof load->subvalue, 0, | |
1076 | &value, mf->n_bytes, 0, load->dst.n_bits); | |
1077 | mf_format(mf, &value, NULL, s); | |
1078 | ds_put_format(s, "->%s", mf->name); | |
1079 | } | |
1080 | ||
1081 | static void | |
1082 | load_format(const struct ofpact_reg_load *load, struct ds *s) | |
f25d0cf3 | 1083 | { |
9bab681f | 1084 | ds_put_cstr(s, "load:"); |
158edc8d | 1085 | mf_format_subvalue(&load->subvalue, s); |
9bab681f | 1086 | ds_put_cstr(s, "->"); |
f25d0cf3 BP |
1087 | mf_format_subfield(&load->dst, s); |
1088 | } | |
0291c1c6 SH |
1089 | |
1090 | void | |
1091 | nxm_format_reg_load(const struct ofpact_reg_load *load, struct ds *s) | |
1092 | { | |
1093 | if (load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD) { | |
1094 | set_field_format(load, s); | |
1095 | } else { | |
1096 | load_format(load, s); | |
1097 | } | |
1098 | } | |
f25d0cf3 BP |
1099 | \f |
1100 | enum ofperr | |
1101 | nxm_reg_move_from_openflow(const struct nx_action_reg_move *narm, | |
1102 | struct ofpbuf *ofpacts) | |
f393f81e | 1103 | { |
f25d0cf3 | 1104 | struct ofpact_reg_move *move; |
f393f81e | 1105 | |
f25d0cf3 BP |
1106 | move = ofpact_put_REG_MOVE(ofpacts); |
1107 | move->src.field = mf_from_nxm_header(ntohl(narm->src)); | |
1108 | move->src.ofs = ntohs(narm->src_ofs); | |
1109 | move->src.n_bits = ntohs(narm->n_bits); | |
1110 | move->dst.field = mf_from_nxm_header(ntohl(narm->dst)); | |
1111 | move->dst.ofs = ntohs(narm->dst_ofs); | |
1112 | move->dst.n_bits = ntohs(narm->n_bits); | |
816fd533 | 1113 | |
f25d0cf3 | 1114 | return nxm_reg_move_check(move, NULL); |
f393f81e | 1115 | } |
b6c9e612 | 1116 | |
816fd533 | 1117 | enum ofperr |
f25d0cf3 BP |
1118 | nxm_reg_load_from_openflow(const struct nx_action_reg_load *narl, |
1119 | struct ofpbuf *ofpacts) | |
b6c9e612 | 1120 | { |
f25d0cf3 | 1121 | struct ofpact_reg_load *load; |
b6c9e612 | 1122 | |
f25d0cf3 BP |
1123 | load = ofpact_put_REG_LOAD(ofpacts); |
1124 | load->dst.field = mf_from_nxm_header(ntohl(narl->dst)); | |
1125 | load->dst.ofs = nxm_decode_ofs(narl->ofs_nbits); | |
1126 | load->dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits); | |
158edc8d | 1127 | load->subvalue.be64[1] = narl->value; |
f25d0cf3 BP |
1128 | |
1129 | /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in | |
1130 | * narl->value. */ | |
9bab681f IY |
1131 | if (load->dst.n_bits < 64 && |
1132 | ntohll(narl->value) >> load->dst.n_bits) { | |
f25d0cf3 | 1133 | return OFPERR_OFPBAC_BAD_ARGUMENT; |
b6c9e612 BP |
1134 | } |
1135 | ||
f25d0cf3 | 1136 | return nxm_reg_load_check(load, NULL); |
43edca57 | 1137 | } |
0291c1c6 SH |
1138 | |
1139 | enum ofperr | |
1140 | nxm_reg_load_from_openflow12_set_field( | |
1141 | const struct ofp12_action_set_field * oasf, struct ofpbuf *ofpacts) | |
1142 | { | |
1143 | uint16_t oasf_len = ntohs(oasf->len); | |
1144 | uint32_t oxm_header = ntohl(oasf->dst); | |
1145 | uint8_t oxm_length = NXM_LENGTH(oxm_header); | |
1146 | struct ofpact_reg_load *load; | |
1147 | const struct mf_field *mf; | |
1148 | ||
1149 | /* ofp12_action_set_field is padded to 64 bits by zero */ | |
1150 | if (oasf_len != ROUND_UP(sizeof(*oasf) + oxm_length, 8)) { | |
1151 | return OFPERR_OFPBAC_BAD_ARGUMENT; | |
1152 | } | |
1153 | if (!is_all_zeros((const uint8_t *)(oasf) + sizeof *oasf + oxm_length, | |
1154 | oasf_len - oxm_length - sizeof *oasf)) { | |
1155 | return OFPERR_OFPBAC_BAD_ARGUMENT; | |
1156 | } | |
1157 | ||
1158 | if (NXM_HASMASK(oxm_header)) { | |
1159 | return OFPERR_OFPBAC_BAD_ARGUMENT; | |
1160 | } | |
1161 | mf = mf_from_nxm_header(oxm_header); | |
1162 | if (!mf) { | |
1163 | return OFPERR_OFPBAC_BAD_ARGUMENT; | |
1164 | } | |
1165 | load = ofpact_put_REG_LOAD(ofpacts); | |
f5c45121 | 1166 | ofpact_set_field_init(load, mf, oasf + 1); |
0291c1c6 SH |
1167 | |
1168 | return nxm_reg_load_check(load, NULL); | |
1169 | } | |
f25d0cf3 | 1170 | \f |
90bf1e07 | 1171 | enum ofperr |
f25d0cf3 | 1172 | nxm_reg_move_check(const struct ofpact_reg_move *move, const struct flow *flow) |
43edca57 | 1173 | { |
90bf1e07 | 1174 | enum ofperr error; |
43edca57 | 1175 | |
f25d0cf3 | 1176 | error = mf_check_src(&move->src, flow); |
43edca57 EJ |
1177 | if (error) { |
1178 | return error; | |
b6c9e612 BP |
1179 | } |
1180 | ||
f25d0cf3 BP |
1181 | return mf_check_dst(&move->dst, NULL); |
1182 | } | |
b6c9e612 | 1183 | |
f25d0cf3 BP |
1184 | enum ofperr |
1185 | nxm_reg_load_check(const struct ofpact_reg_load *load, const struct flow *flow) | |
1186 | { | |
1187 | return mf_check_dst(&load->dst, flow); | |
b6c9e612 BP |
1188 | } |
1189 | \f | |
b6c9e612 | 1190 | void |
f25d0cf3 BP |
1191 | nxm_reg_move_to_nxast(const struct ofpact_reg_move *move, |
1192 | struct ofpbuf *openflow) | |
b6c9e612 | 1193 | { |
f25d0cf3 | 1194 | struct nx_action_reg_move *narm; |
28da1f8f | 1195 | |
f25d0cf3 BP |
1196 | narm = ofputil_put_NXAST_REG_MOVE(openflow); |
1197 | narm->n_bits = htons(move->dst.n_bits); | |
1198 | narm->src_ofs = htons(move->src.ofs); | |
1199 | narm->dst_ofs = htons(move->dst.ofs); | |
1200 | narm->src = htonl(move->src.field->nxm_header); | |
1201 | narm->dst = htonl(move->dst.field->nxm_header); | |
1202 | } | |
816fd533 | 1203 | |
0291c1c6 SH |
1204 | static void |
1205 | reg_load_to_nxast(const struct ofpact_reg_load *load, struct ofpbuf *openflow) | |
f25d0cf3 BP |
1206 | { |
1207 | struct nx_action_reg_load *narl; | |
1208 | ||
1209 | narl = ofputil_put_NXAST_REG_LOAD(openflow); | |
1210 | narl->ofs_nbits = nxm_encode_ofs_nbits(load->dst.ofs, load->dst.n_bits); | |
1211 | narl->dst = htonl(load->dst.field->nxm_header); | |
158edc8d | 1212 | narl->value = load->subvalue.be64[1]; |
b6c9e612 | 1213 | } |
0291c1c6 SH |
1214 | |
1215 | static void | |
1216 | set_field_to_ofast(const struct ofpact_reg_load *load, | |
1217 | struct ofpbuf *openflow) | |
1218 | { | |
1219 | const struct mf_field *mf = load->dst.field; | |
1220 | struct ofp12_action_set_field *oasf; | |
1221 | uint16_t padded_value_len; | |
1222 | ||
1223 | oasf = ofputil_put_OFPAT12_SET_FIELD(openflow); | |
1224 | oasf->dst = htonl(mf->oxm_header); | |
1225 | ||
1226 | /* Set field is the only action of variable length (so far), | |
1227 | * so handling the variable length portion is open-coded here */ | |
1228 | padded_value_len = ROUND_UP(mf->n_bytes, 8); | |
1229 | ofpbuf_put_uninit(openflow, padded_value_len); | |
1230 | oasf->len = htons(ntohs(oasf->len) + padded_value_len); | |
1231 | memset(oasf + 1, 0, padded_value_len); | |
1232 | ||
1233 | bitwise_copy(&load->subvalue, sizeof load->subvalue, load->dst.ofs, | |
1234 | oasf + 1, mf->n_bytes, load->dst.ofs, load->dst.n_bits); | |
1235 | return; | |
1236 | } | |
1237 | ||
1238 | void | |
1239 | nxm_reg_load_to_nxast(const struct ofpact_reg_load *load, | |
1240 | struct ofpbuf *openflow) | |
1241 | { | |
1242 | ||
1243 | if (load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD) { | |
1244 | struct ofp_header *oh = (struct ofp_header *)openflow->l2; | |
1245 | ||
1246 | switch(oh->version) { | |
2e1ae200 | 1247 | case OFP13_VERSION: |
0291c1c6 SH |
1248 | case OFP12_VERSION: |
1249 | set_field_to_ofast(load, openflow); | |
1250 | break; | |
1251 | ||
1252 | case OFP11_VERSION: | |
1253 | case OFP10_VERSION: | |
1254 | if (load->dst.n_bits < 64) { | |
1255 | reg_load_to_nxast(load, openflow); | |
1256 | } else { | |
1257 | /* Split into 64bit chunks */ | |
1258 | int chunk, ofs; | |
1259 | for (ofs = 0; ofs < load->dst.n_bits; ofs += chunk) { | |
1260 | struct ofpact_reg_load subload = *load; | |
1261 | ||
1262 | chunk = MIN(load->dst.n_bits - ofs, 64); | |
1263 | ||
1264 | subload.dst.field = load->dst.field; | |
1265 | subload.dst.ofs = load->dst.ofs + ofs; | |
1266 | subload.dst.n_bits = chunk; | |
1267 | bitwise_copy(&load->subvalue, sizeof load->subvalue, ofs, | |
1268 | &subload.subvalue, sizeof subload.subvalue, 0, | |
1269 | chunk); | |
1270 | reg_load_to_nxast(&subload, openflow); | |
1271 | } | |
1272 | } | |
1273 | break; | |
1274 | ||
1275 | default: | |
1276 | NOT_REACHED(); | |
1277 | } | |
1278 | } else { | |
1279 | reg_load_to_nxast(load, openflow); | |
1280 | } | |
1281 | } | |
f25d0cf3 BP |
1282 | \f |
1283 | /* nxm_execute_reg_move(), nxm_execute_reg_load(). */ | |
b6c9e612 BP |
1284 | |
1285 | void | |
f25d0cf3 | 1286 | nxm_execute_reg_move(const struct ofpact_reg_move *move, |
b6c9e612 BP |
1287 | struct flow *flow) |
1288 | { | |
f25d0cf3 BP |
1289 | union mf_value src_value; |
1290 | union mf_value dst_value; | |
816fd533 | 1291 | |
f25d0cf3 BP |
1292 | mf_get_value(move->dst.field, flow, &dst_value); |
1293 | mf_get_value(move->src.field, flow, &src_value); | |
1294 | bitwise_copy(&src_value, move->src.field->n_bytes, move->src.ofs, | |
1295 | &dst_value, move->dst.field->n_bytes, move->dst.ofs, | |
1296 | move->src.n_bits); | |
1297 | mf_set_flow_value(move->dst.field, &dst_value, flow); | |
43edca57 | 1298 | } |
b6c9e612 | 1299 | |
43edca57 | 1300 | void |
f25d0cf3 | 1301 | nxm_execute_reg_load(const struct ofpact_reg_load *load, struct flow *flow) |
43edca57 | 1302 | { |
9bab681f | 1303 | mf_write_subfield_flow(&load->dst, &load->subvalue, flow); |
816fd533 | 1304 | } |
28da1f8f | 1305 | |
816fd533 | 1306 | void |
f25d0cf3 BP |
1307 | nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data, |
1308 | struct flow *flow) | |
816fd533 | 1309 | { |
9bab681f IY |
1310 | union mf_subvalue src_subvalue; |
1311 | ovs_be64 src_data_be = htonll(src_data); | |
f25d0cf3 | 1312 | |
9bab681f IY |
1313 | bitwise_copy(&src_data_be, sizeof src_data_be, 0, |
1314 | &src_subvalue, sizeof src_subvalue, 0, | |
1315 | sizeof src_data_be * 8); | |
1316 | mf_write_subfield_flow(dst, &src_subvalue, flow); | |
b6c9e612 | 1317 | } |