]>
Commit | Line | Data |
---|---|---|
09246b99 | 1 | /* |
f6e984d7 | 2 | * Copyright (c) 2010, 2011, 2012, 2013, 2014 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" | |
178742f9 | 25 | #include "hmap.h" |
6a885fd0 | 26 | #include "meta-flow.h" |
f25d0cf3 | 27 | #include "ofp-actions.h" |
90bf1e07 | 28 | #include "ofp-errors.h" |
09246b99 BP |
29 | #include "ofp-util.h" |
30 | #include "ofpbuf.h" | |
31 | #include "openflow/nicira-ext.h" | |
32 | #include "packets.h" | |
178742f9 | 33 | #include "shash.h" |
09246b99 | 34 | #include "unaligned.h" |
ddc4f8e2 | 35 | #include "util.h" |
09246b99 BP |
36 | #include "vlog.h" |
37 | ||
38 | VLOG_DEFINE_THIS_MODULE(nx_match); | |
39 | ||
508a9338 BP |
40 | /* OXM headers. |
41 | * | |
42 | * | |
43 | * Standard OXM/NXM | |
44 | * ================ | |
45 | * | |
46 | * The header is 32 bits long. It looks like this: | |
47 | * | |
48 | * |31 16 15 9| 8 7 0 | |
49 | * +----------------------------------+---------------+--+------------------+ | |
50 | * | oxm_class | oxm_field |hm| oxm_length | | |
51 | * +----------------------------------+---------------+--+------------------+ | |
52 | * | |
53 | * where hm stands for oxm_hasmask. It is followed by oxm_length bytes of | |
54 | * payload. When oxm_hasmask is 0, the payload is the value of the field | |
55 | * identified by the header; when oxm_hasmask is 1, the payload is a value for | |
56 | * the field followed by a mask of equal length. | |
57 | * | |
58 | * Internally, we represent a standard OXM header as a 64-bit integer with the | |
59 | * above information in the most-significant bits. | |
60 | * | |
61 | * | |
62 | * Experimenter OXM | |
63 | * ================ | |
64 | * | |
65 | * The header is 64 bits long. It looks like the diagram above except that a | |
66 | * 32-bit experimenter ID, which we call oxm_vendor and which identifies a | |
67 | * vendor, is inserted just before the payload. Experimenter OXMs are | |
68 | * identified by an all-1-bits oxm_class (OFPXMC12_EXPERIMENTER). The | |
69 | * oxm_length value *includes* the experimenter ID, so that the real payload is | |
70 | * only oxm_length - 4 bytes long. | |
71 | * | |
72 | * Internally, we represent an experimenter OXM header as a 64-bit integer with | |
73 | * the standard header in the upper 32 bits and the experimenter ID in the | |
74 | * lower 32 bits. (It would be more convenient to swap the positions of the | |
75 | * two 32-bit words, but this would be more error-prone because experimenter | |
76 | * OXMs are very rarely used, so accidentally passing one through a 32-bit type | |
77 | * somewhere in the OVS code would be hard to find.) | |
78 | */ | |
79 | ||
178742f9 BP |
80 | /* |
81 | * OXM Class IDs. | |
82 | * The high order bit differentiate reserved classes from member classes. | |
83 | * Classes 0x0000 to 0x7FFF are member classes, allocated by ONF. | |
84 | * Classes 0x8000 to 0xFFFE are reserved classes, reserved for standardisation. | |
85 | */ | |
86 | enum ofp12_oxm_class { | |
87 | OFPXMC12_NXM_0 = 0x0000, /* Backward compatibility with NXM */ | |
88 | OFPXMC12_NXM_1 = 0x0001, /* Backward compatibility with NXM */ | |
89 | OFPXMC12_OPENFLOW_BASIC = 0x8000, /* Basic class for OpenFlow */ | |
90 | OFPXMC15_PACKET_REGS = 0x8001, /* Packet registers (pipeline fields). */ | |
91 | OFPXMC12_EXPERIMENTER = 0xffff, /* Experimenter class */ | |
92 | }; | |
93 | ||
508a9338 BP |
94 | /* Functions for extracting raw field values from OXM/NXM headers. */ |
95 | static uint32_t nxm_vendor(uint64_t header) { return header; } | |
96 | static int nxm_class(uint64_t header) { return header >> 48; } | |
97 | static int nxm_field(uint64_t header) { return (header >> 41) & 0x7f; } | |
98 | static bool nxm_hasmask(uint64_t header) { return (header >> 40) & 1; } | |
99 | static int nxm_length(uint64_t header) { return (header >> 32) & 0xff; } | |
100 | ||
101 | static bool | |
102 | is_experimenter_oxm(uint64_t header) | |
103 | { | |
104 | return nxm_class(header) == OFPXMC12_EXPERIMENTER; | |
105 | } | |
106 | ||
107 | /* The OXM header "length" field is somewhat tricky: | |
108 | * | |
109 | * - For a standard OXM header, the length is the number of bytes of the | |
110 | * payload, and the payload consists of just the value (and mask, if | |
111 | * present). | |
112 | * | |
113 | * - For an experimenter OXM header, the length is the number of bytes in | |
114 | * the payload plus 4 (the length of the experimenter ID). That is, the | |
115 | * experimenter ID is included in oxm_length. | |
116 | * | |
117 | * This function returns the length of the experimenter ID field in 'header'. | |
118 | * That is, for an experimenter OXM (when an experimenter ID is present), it | |
119 | * returns 4, and for a standard OXM (when no experimenter ID is present), it | |
120 | * returns 0. */ | |
121 | static int | |
122 | nxm_experimenter_len(uint64_t header) | |
123 | { | |
124 | return is_experimenter_oxm(header) ? 4 : 0; | |
125 | } | |
126 | ||
127 | /* Returns the number of bytes that follow the header for an NXM/OXM entry | |
128 | * with the given 'header'. */ | |
129 | static int | |
130 | nxm_payload_len(uint64_t header) | |
131 | { | |
132 | return nxm_length(header) - nxm_experimenter_len(header); | |
133 | } | |
134 | ||
135 | /* Returns the number of bytes in the header for an NXM/OXM entry with the | |
136 | * given 'header'. */ | |
137 | static int | |
138 | nxm_header_len(uint64_t header) | |
139 | { | |
140 | return 4 + nxm_experimenter_len(header); | |
141 | } | |
178742f9 | 142 | |
508a9338 BP |
143 | #define NXM_HEADER(VENDOR, CLASS, FIELD, HASMASK, LENGTH) \ |
144 | (((uint64_t) (CLASS) << 48) | \ | |
145 | ((uint64_t) (FIELD) << 41) | \ | |
146 | ((uint64_t) (HASMASK) << 40) | \ | |
147 | ((uint64_t) (LENGTH) << 32) | \ | |
148 | (VENDOR)) | |
178742f9 | 149 | |
508a9338 BP |
150 | #define NXM_HEADER_FMT "%#"PRIx32":%d:%d:%d:%d" |
151 | #define NXM_HEADER_ARGS(HEADER) \ | |
152 | nxm_vendor(HEADER), nxm_class(HEADER), nxm_field(HEADER), \ | |
178742f9 BP |
153 | nxm_hasmask(HEADER), nxm_length(HEADER) |
154 | ||
155 | /* Functions for turning the "hasmask" bit on or off. (This also requires | |
156 | * adjusting the length.) */ | |
508a9338 BP |
157 | static uint64_t |
158 | nxm_make_exact_header(uint64_t header) | |
178742f9 | 159 | { |
508a9338 BP |
160 | int new_len = nxm_payload_len(header) / 2 + nxm_experimenter_len(header); |
161 | return NXM_HEADER(nxm_vendor(header), nxm_class(header), | |
162 | nxm_field(header), 0, new_len); | |
178742f9 | 163 | } |
508a9338 BP |
164 | static uint64_t |
165 | nxm_make_wild_header(uint64_t header) | |
178742f9 | 166 | { |
508a9338 BP |
167 | int new_len = nxm_payload_len(header) * 2 + nxm_experimenter_len(header); |
168 | return NXM_HEADER(nxm_vendor(header), nxm_class(header), | |
169 | nxm_field(header), 1, new_len); | |
178742f9 BP |
170 | } |
171 | ||
172 | /* Flow cookie. | |
173 | * | |
174 | * This may be used to gain the OpenFlow 1.1-like ability to restrict | |
175 | * certain NXM-based Flow Mod and Flow Stats Request messages to flows | |
176 | * with specific cookies. See the "nx_flow_mod" and "nx_flow_stats_request" | |
177 | * structure definitions for more details. This match is otherwise not | |
178 | * allowed. */ | |
508a9338 | 179 | #define NXM_NX_COOKIE NXM_HEADER (0, 0x0001, 30, 0, 8) |
178742f9 BP |
180 | #define NXM_NX_COOKIE_W nxm_make_wild_header(NXM_NX_COOKIE) |
181 | ||
182 | struct nxm_field { | |
508a9338 | 183 | uint64_t header; |
178742f9 BP |
184 | enum ofp_version version; |
185 | const char *name; /* e.g. "NXM_OF_IN_PORT". */ | |
186 | ||
187 | enum mf_field_id id; | |
188 | }; | |
189 | ||
508a9338 | 190 | static const struct nxm_field *nxm_field_by_header(uint64_t header); |
178742f9 | 191 | static const struct nxm_field *nxm_field_by_name(const char *name, size_t len); |
e6556fe3 BP |
192 | static const struct nxm_field *nxm_field_by_mf_id(enum mf_field_id, |
193 | enum ofp_version); | |
178742f9 | 194 | |
508a9338 | 195 | static void nx_put_header__(struct ofpbuf *, uint64_t header, bool masked); |
f8047558 | 196 | |
09246b99 BP |
197 | /* Rate limit for nx_match parse errors. These always indicate a bug in the |
198 | * peer and so there's not much point in showing a lot of them. */ | |
199 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
200 | ||
178742f9 BP |
201 | static const struct nxm_field * |
202 | mf_parse_subfield_name(const char *name, int name_len, bool *wild); | |
203 | ||
178742f9 BP |
204 | /* Returns the preferred OXM header to use for field 'id' in OpenFlow version |
205 | * 'version'. Specify 0 for 'version' if an NXM legacy header should be | |
206 | * preferred over any standardized OXM header. Returns 0 if field 'id' cannot | |
207 | * be expressed in NXM or OXM. */ | |
508a9338 | 208 | static uint64_t |
178742f9 BP |
209 | mf_oxm_header(enum mf_field_id id, enum ofp_version version) |
210 | { | |
e6556fe3 | 211 | const struct nxm_field *f = nxm_field_by_mf_id(id, version); |
178742f9 BP |
212 | return f ? f->header : 0; |
213 | } | |
214 | ||
508a9338 BP |
215 | /* Returns the 32-bit OXM or NXM header to use for field 'id', preferring an |
216 | * NXM legacy header over any standardized OXM header. Returns 0 if field 'id' | |
217 | * cannot be expressed with a 32-bit NXM or OXM header. | |
218 | * | |
219 | * Whenever possible, use nx_pull_header() instead of this function, because | |
220 | * this function cannot support 64-bit experimenter OXM headers. */ | |
221 | uint32_t | |
222 | mf_nxm_header(enum mf_field_id id) | |
223 | { | |
224 | uint64_t oxm = mf_oxm_header(id, 0); | |
225 | return is_experimenter_oxm(oxm) ? 0 : oxm >> 32; | |
226 | } | |
227 | ||
228 | static const struct mf_field * | |
229 | mf_from_oxm_header(uint64_t header) | |
230 | { | |
231 | const struct nxm_field *f = nxm_field_by_header(header); | |
232 | return f ? mf_from_id(f->id) : NULL; | |
233 | } | |
234 | ||
178742f9 BP |
235 | /* Returns the "struct mf_field" that corresponds to NXM or OXM header |
236 | * 'header', or NULL if 'header' doesn't correspond to any known field. */ | |
237 | const struct mf_field * | |
238 | mf_from_nxm_header(uint32_t header) | |
239 | { | |
508a9338 | 240 | return mf_from_oxm_header((uint64_t) header << 32); |
178742f9 BP |
241 | } |
242 | ||
09246b99 BP |
243 | /* Returns the width of the data for a field with the given 'header', in |
244 | * bytes. */ | |
178742f9 | 245 | static int |
508a9338 | 246 | nxm_field_bytes(uint64_t header) |
09246b99 | 247 | { |
508a9338 | 248 | unsigned int length = nxm_payload_len(header); |
178742f9 | 249 | return nxm_hasmask(header) ? length / 2 : length; |
09246b99 | 250 | } |
e6556fe3 | 251 | \f |
6a885fd0 | 252 | /* nx_pull_match() and helpers. */ |
09246b99 | 253 | |
178742f9 BP |
254 | /* Given NXM/OXM value 'value' and mask 'mask' associated with 'header', checks |
255 | * for any 1-bit in the value where there is a 0-bit in the mask. Returns 0 if | |
256 | * none, otherwise an error code. */ | |
257 | static bool | |
508a9338 | 258 | is_mask_consistent(uint64_t header, const uint8_t *value, const uint8_t *mask) |
09246b99 | 259 | { |
178742f9 BP |
260 | unsigned int width = nxm_field_bytes(header); |
261 | unsigned int i; | |
09246b99 | 262 | |
178742f9 BP |
263 | for (i = 0; i < width; i++) { |
264 | if (value[i] & ~mask[i]) { | |
265 | if (!VLOG_DROP_WARN(&rl)) { | |
266 | VLOG_WARN_RL(&rl, "Rejecting NXM/OXM entry "NXM_HEADER_FMT " " | |
267 | "with 1-bits in value for bits wildcarded by the " | |
268 | "mask.", NXM_HEADER_ARGS(header)); | |
269 | } | |
270 | return false; | |
09246b99 | 271 | } |
09246b99 | 272 | } |
178742f9 BP |
273 | return true; |
274 | } | |
09246b99 | 275 | |
178742f9 | 276 | static bool |
508a9338 | 277 | is_cookie_pseudoheader(uint64_t header) |
178742f9 BP |
278 | { |
279 | return header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W; | |
280 | } | |
281 | ||
282 | static enum ofperr | |
508a9338 | 283 | nx_pull_header__(struct ofpbuf *b, bool allow_cookie, uint64_t *header, |
178742f9 BP |
284 | const struct mf_field **field) |
285 | { | |
286 | if (ofpbuf_size(b) < 4) { | |
508a9338 | 287 | goto bad_len; |
09246b99 | 288 | } |
508a9338 BP |
289 | |
290 | *header = ((uint64_t) ntohl(get_unaligned_be32(ofpbuf_data(b)))) << 32; | |
291 | if (is_experimenter_oxm(*header)) { | |
292 | if (ofpbuf_size(b) < 8) { | |
293 | goto bad_len; | |
294 | } | |
295 | *header = ntohll(get_unaligned_be64(ofpbuf_data(b))); | |
296 | } | |
297 | if (nxm_length(*header) <= nxm_experimenter_len(*header)) { | |
298 | VLOG_WARN_RL(&rl, "OXM header "NXM_HEADER_FMT" has invalid length %d " | |
299 | "(minimum is %d)", | |
300 | NXM_HEADER_ARGS(*header), nxm_length(*header), | |
301 | nxm_header_len(*header) + 1); | |
178742f9 BP |
302 | goto error; |
303 | } | |
508a9338 BP |
304 | ofpbuf_pull(b, nxm_header_len(*header)); |
305 | ||
178742f9 | 306 | if (field) { |
508a9338 | 307 | *field = mf_from_oxm_header(*header); |
178742f9 BP |
308 | if (!*field && !(allow_cookie && is_cookie_pseudoheader(*header))) { |
309 | VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" is unknown", | |
310 | NXM_HEADER_ARGS(*header)); | |
311 | return OFPERR_OFPBMC_BAD_FIELD; | |
312 | } | |
09246b99 BP |
313 | } |
314 | ||
178742f9 BP |
315 | return 0; |
316 | ||
508a9338 BP |
317 | bad_len: |
318 | VLOG_DBG_RL(&rl, "encountered partial (%"PRIu32"-byte) OXM entry", | |
319 | ofpbuf_size(b)); | |
178742f9 BP |
320 | error: |
321 | *header = 0; | |
322 | *field = NULL; | |
323 | return OFPERR_OFPBMC_BAD_LEN; | |
09246b99 BP |
324 | } |
325 | ||
3947cc76 | 326 | static enum ofperr |
508a9338 | 327 | nx_pull_entry__(struct ofpbuf *b, bool allow_cookie, uint64_t *header, |
178742f9 BP |
328 | const struct mf_field **field, |
329 | union mf_value *value, union mf_value *mask) | |
e1cfc4e4 | 330 | { |
178742f9 BP |
331 | enum ofperr header_error; |
332 | unsigned int payload_len; | |
333 | const uint8_t *payload; | |
334 | int width; | |
e1cfc4e4 | 335 | |
178742f9 BP |
336 | header_error = nx_pull_header__(b, allow_cookie, header, field); |
337 | if (header_error && header_error != OFPERR_OFPBMC_BAD_FIELD) { | |
338 | return header_error; | |
339 | } | |
340 | ||
508a9338 | 341 | payload_len = nxm_payload_len(*header); |
178742f9 BP |
342 | payload = ofpbuf_try_pull(b, payload_len); |
343 | if (!payload) { | |
344 | VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" calls for %u-byte " | |
345 | "payload but only %"PRIu32" bytes follow OXM header", | |
346 | NXM_HEADER_ARGS(*header), payload_len, ofpbuf_size(b)); | |
347 | return OFPERR_OFPBMC_BAD_LEN; | |
348 | } | |
349 | ||
350 | width = nxm_field_bytes(*header); | |
351 | if (nxm_hasmask(*header) | |
352 | && !is_mask_consistent(*header, payload, payload + width)) { | |
353 | return OFPERR_OFPBMC_BAD_WILDCARDS; | |
354 | } | |
355 | ||
356 | memcpy(value, payload, MIN(width, sizeof *value)); | |
357 | if (mask) { | |
358 | if (nxm_hasmask(*header)) { | |
359 | memcpy(mask, payload + width, MIN(width, sizeof *mask)); | |
360 | } else { | |
361 | memset(mask, 0xff, MIN(width, sizeof *mask)); | |
362 | } | |
363 | } else if (nxm_hasmask(*header)) { | |
364 | VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" includes mask but " | |
365 | "masked OXMs are not allowed here", | |
366 | NXM_HEADER_ARGS(*header)); | |
367 | return OFPERR_OFPBMC_BAD_MASK; | |
368 | } | |
369 | ||
370 | return header_error; | |
371 | } | |
372 | ||
373 | /* Attempts to pull an NXM or OXM header, value, and mask (if present) from the | |
374 | * beginning of 'b'. If successful, stores a pointer to the "struct mf_field" | |
375 | * corresponding to the pulled header in '*field', the value into '*value', | |
376 | * and the mask into '*mask', and returns 0. On error, returns an OpenFlow | |
377 | * error; in this case, some bytes might have been pulled off 'b' anyhow, and | |
378 | * the output parameters might have been modified. | |
379 | * | |
380 | * If a NULL 'mask' is supplied, masked OXM or NXM entries are treated as | |
381 | * errors (with OFPERR_OFPBMC_BAD_MASK). | |
382 | */ | |
383 | enum ofperr | |
384 | nx_pull_entry(struct ofpbuf *b, const struct mf_field **field, | |
385 | union mf_value *value, union mf_value *mask) | |
386 | { | |
508a9338 | 387 | uint64_t header; |
178742f9 BP |
388 | |
389 | return nx_pull_entry__(b, false, &header, field, value, mask); | |
390 | } | |
391 | ||
392 | /* Attempts to pull an NXM or OXM header from the beginning of 'b'. If | |
393 | * successful, stores a pointer to the "struct mf_field" corresponding to the | |
394 | * pulled header in '*field', stores the header's hasmask bit in '*masked' | |
395 | * (true if hasmask=1, false if hasmask=0), and returns 0. On error, returns | |
396 | * an OpenFlow error; in this case, some bytes might have been pulled off 'b' | |
397 | * anyhow, and the output parameters might have been modified. | |
398 | * | |
399 | * If NULL 'masked' is supplied, masked OXM or NXM headers are treated as | |
400 | * errors (with OFPERR_OFPBMC_BAD_MASK). | |
401 | */ | |
402 | enum ofperr | |
403 | nx_pull_header(struct ofpbuf *b, const struct mf_field **field, bool *masked) | |
404 | { | |
405 | enum ofperr error; | |
508a9338 | 406 | uint64_t header; |
178742f9 BP |
407 | |
408 | error = nx_pull_header__(b, false, &header, field); | |
409 | if (masked) { | |
410 | *masked = !error && nxm_hasmask(header); | |
411 | } else if (!error && nxm_hasmask(header)) { | |
412 | error = OFPERR_OFPBMC_BAD_MASK; | |
413 | } | |
414 | return error; | |
415 | } | |
416 | ||
417 | static enum ofperr | |
418 | nx_pull_match_entry(struct ofpbuf *b, bool allow_cookie, | |
419 | const struct mf_field **field, | |
420 | union mf_value *value, union mf_value *mask) | |
421 | { | |
422 | enum ofperr error; | |
508a9338 | 423 | uint64_t header; |
178742f9 BP |
424 | |
425 | error = nx_pull_entry__(b, allow_cookie, &header, field, value, mask); | |
426 | if (error) { | |
427 | return error; | |
428 | } | |
429 | if (field && *field) { | |
430 | if (!mf_is_mask_valid(*field, mask)) { | |
431 | VLOG_DBG_RL(&rl, "bad mask for field %s", (*field)->name); | |
432 | return OFPERR_OFPBMC_BAD_MASK; | |
433 | } | |
434 | if (!mf_is_value_valid(*field, value)) { | |
435 | VLOG_DBG_RL(&rl, "bad value for field %s", (*field)->name); | |
436 | return OFPERR_OFPBMC_BAD_VALUE; | |
e1cfc4e4 BP |
437 | } |
438 | } | |
3947cc76 | 439 | return 0; |
e1cfc4e4 BP |
440 | } |
441 | ||
90bf1e07 | 442 | static enum ofperr |
7623f4dd | 443 | nx_pull_raw(const uint8_t *p, unsigned int match_len, bool strict, |
81a76618 | 444 | struct match *match, ovs_be64 *cookie, ovs_be64 *cookie_mask) |
09246b99 | 445 | { |
178742f9 | 446 | struct ofpbuf b; |
09246b99 | 447 | |
cb22974d | 448 | ovs_assert((cookie != NULL) == (cookie_mask != NULL)); |
e729e793 | 449 | |
81a76618 | 450 | match_init_catchall(match); |
a3a0c29e BP |
451 | if (cookie) { |
452 | *cookie = *cookie_mask = htonll(0); | |
453 | } | |
a3a0c29e | 454 | |
178742f9 BP |
455 | ofpbuf_use_const(&b, p, match_len); |
456 | while (ofpbuf_size(&b)) { | |
457 | const uint8_t *pos = ofpbuf_data(&b); | |
458 | const struct mf_field *field; | |
459 | union mf_value value; | |
460 | union mf_value mask; | |
90bf1e07 | 461 | enum ofperr error; |
09246b99 | 462 | |
178742f9 BP |
463 | error = nx_pull_match_entry(&b, cookie != NULL, &field, &value, &mask); |
464 | if (error) { | |
465 | if (error == OFPERR_OFPBMC_BAD_FIELD && !strict) { | |
466 | continue; | |
467 | } | |
468 | } else if (!field) { | |
469 | if (!cookie) { | |
2e0525bc | 470 | error = OFPERR_OFPBMC_BAD_FIELD; |
178742f9 BP |
471 | } else if (*cookie_mask) { |
472 | error = OFPERR_OFPBMC_DUP_FIELD; | |
102ce766 | 473 | } else { |
178742f9 BP |
474 | *cookie = value.be64; |
475 | *cookie_mask = mask.be64; | |
102ce766 | 476 | } |
178742f9 | 477 | } else if (!mf_are_prereqs_ok(field, &match->flow)) { |
2e0525bc | 478 | error = OFPERR_OFPBMC_BAD_PREREQ; |
178742f9 | 479 | } else if (!mf_is_all_wild(field, &match->wc)) { |
2e0525bc | 480 | error = OFPERR_OFPBMC_DUP_FIELD; |
72333065 | 481 | } else { |
178742f9 | 482 | mf_set(field, &value, &mask, match); |
e729e793 JP |
483 | } |
484 | ||
09246b99 | 485 | if (error) { |
178742f9 BP |
486 | VLOG_DBG_RL(&rl, "error parsing OXM at offset %"PRIdPTR" " |
487 | "within match (%s)", pos - | |
488 | p, ofperr_to_string(error)); | |
09246b99 BP |
489 | return error; |
490 | } | |
09246b99 BP |
491 | } |
492 | ||
178742f9 | 493 | return 0; |
09246b99 | 494 | } |
102ce766 | 495 | |
7623f4dd SH |
496 | static enum ofperr |
497 | nx_pull_match__(struct ofpbuf *b, unsigned int match_len, bool strict, | |
81a76618 | 498 | struct match *match, |
7623f4dd SH |
499 | ovs_be64 *cookie, ovs_be64 *cookie_mask) |
500 | { | |
501 | uint8_t *p = NULL; | |
502 | ||
503 | if (match_len) { | |
504 | p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8)); | |
505 | if (!p) { | |
506 | VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a " | |
507 | "multiple of 8, is longer than space in message (max " | |
1f317cb5 | 508 | "length %"PRIu32")", match_len, ofpbuf_size(b)); |
7623f4dd SH |
509 | return OFPERR_OFPBMC_BAD_LEN; |
510 | } | |
511 | } | |
512 | ||
81a76618 | 513 | return nx_pull_raw(p, match_len, strict, match, cookie, cookie_mask); |
7623f4dd SH |
514 | } |
515 | ||
102ce766 | 516 | /* Parses the nx_match formatted match description in 'b' with length |
81a76618 BP |
517 | * 'match_len'. Stores the results in 'match'. If 'cookie' and 'cookie_mask' |
518 | * are valid pointers, then stores the cookie and mask in them if 'b' contains | |
519 | * a "NXM_NX_COOKIE*" match. Otherwise, stores 0 in both. | |
102ce766 | 520 | * |
81a76618 | 521 | * Fails with an error upon encountering an unknown NXM header. |
102ce766 EJ |
522 | * |
523 | * Returns 0 if successful, otherwise an OpenFlow error code. */ | |
90bf1e07 | 524 | enum ofperr |
81a76618 | 525 | nx_pull_match(struct ofpbuf *b, unsigned int match_len, struct match *match, |
102ce766 EJ |
526 | ovs_be64 *cookie, ovs_be64 *cookie_mask) |
527 | { | |
81a76618 | 528 | return nx_pull_match__(b, match_len, true, match, cookie, cookie_mask); |
102ce766 EJ |
529 | } |
530 | ||
81a76618 BP |
531 | /* Behaves the same as nx_pull_match(), but skips over unknown NXM headers, |
532 | * instead of failing with an error. */ | |
90bf1e07 | 533 | enum ofperr |
102ce766 | 534 | nx_pull_match_loose(struct ofpbuf *b, unsigned int match_len, |
81a76618 | 535 | struct match *match, |
102ce766 EJ |
536 | ovs_be64 *cookie, ovs_be64 *cookie_mask) |
537 | { | |
81a76618 | 538 | return nx_pull_match__(b, match_len, false, match, cookie, cookie_mask); |
102ce766 | 539 | } |
7623f4dd SH |
540 | |
541 | static enum ofperr | |
81a76618 | 542 | oxm_pull_match__(struct ofpbuf *b, bool strict, struct match *match) |
7623f4dd | 543 | { |
1f317cb5 | 544 | struct ofp11_match_header *omh = ofpbuf_data(b); |
7623f4dd SH |
545 | uint8_t *p; |
546 | uint16_t match_len; | |
547 | ||
1f317cb5 | 548 | if (ofpbuf_size(b) < sizeof *omh) { |
7623f4dd SH |
549 | return OFPERR_OFPBMC_BAD_LEN; |
550 | } | |
551 | ||
552 | match_len = ntohs(omh->length); | |
553 | if (match_len < sizeof *omh) { | |
554 | return OFPERR_OFPBMC_BAD_LEN; | |
555 | } | |
556 | ||
557 | if (omh->type != htons(OFPMT_OXM)) { | |
558 | return OFPERR_OFPBMC_BAD_TYPE; | |
559 | } | |
560 | ||
561 | p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8)); | |
562 | if (!p) { | |
563 | VLOG_DBG_RL(&rl, "oxm length %u, rounded up to a " | |
564 | "multiple of 8, is longer than space in message (max " | |
1f317cb5 | 565 | "length %"PRIu32")", match_len, ofpbuf_size(b)); |
7623f4dd SH |
566 | return OFPERR_OFPBMC_BAD_LEN; |
567 | } | |
568 | ||
569 | return nx_pull_raw(p + sizeof *omh, match_len - sizeof *omh, | |
81a76618 | 570 | strict, match, NULL, NULL); |
7623f4dd SH |
571 | } |
572 | ||
aa0667bc YT |
573 | /* Parses the oxm formatted match description preceded by a struct |
574 | * ofp11_match_header in 'b'. Stores the result in 'match'. | |
7623f4dd SH |
575 | * |
576 | * Fails with an error when encountering unknown OXM headers. | |
577 | * | |
578 | * Returns 0 if successful, otherwise an OpenFlow error code. */ | |
579 | enum ofperr | |
81a76618 | 580 | oxm_pull_match(struct ofpbuf *b, struct match *match) |
7623f4dd | 581 | { |
81a76618 | 582 | return oxm_pull_match__(b, true, match); |
7623f4dd SH |
583 | } |
584 | ||
585 | /* Behaves the same as oxm_pull_match() with one exception. Skips over unknown | |
aa0667bc | 586 | * OXM headers instead of failing with an error when they are encountered. */ |
7623f4dd | 587 | enum ofperr |
81a76618 | 588 | oxm_pull_match_loose(struct ofpbuf *b, struct match *match) |
7623f4dd | 589 | { |
81a76618 | 590 | return oxm_pull_match__(b, false, match); |
7623f4dd | 591 | } |
09246b99 BP |
592 | \f |
593 | /* nx_put_match() and helpers. | |
594 | * | |
595 | * 'put' functions whose names end in 'w' add a wildcarded field. | |
596 | * 'put' functions whose names end in 'm' add a field that might be wildcarded. | |
597 | * Other 'put' functions add exact-match fields. | |
598 | */ | |
599 | ||
600 | static void | |
f8047558 BP |
601 | nxm_put_unmasked(struct ofpbuf *b, enum mf_field_id field, |
602 | enum ofp_version version, const void *value, size_t n_bytes) | |
09246b99 | 603 | { |
f8047558 BP |
604 | nx_put_header(b, field, version, false); |
605 | ofpbuf_put(b, value, n_bytes); | |
09246b99 BP |
606 | } |
607 | ||
66642cb4 | 608 | static void |
f8047558 BP |
609 | nxm_put(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version, |
610 | const void *value, const void *mask, size_t n_bytes) | |
66642cb4 | 611 | { |
f8047558 BP |
612 | if (!is_all_zeros(mask, n_bytes)) { |
613 | bool masked = !is_all_ones(mask, n_bytes); | |
614 | nx_put_header(b, field, version, masked); | |
615 | ofpbuf_put(b, value, n_bytes); | |
616 | if (masked) { | |
617 | ofpbuf_put(b, mask, n_bytes); | |
618 | } | |
66642cb4 BP |
619 | } |
620 | } | |
621 | ||
09246b99 | 622 | static void |
f8047558 BP |
623 | nxm_put_8m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version, |
624 | uint8_t value, uint8_t mask) | |
09246b99 | 625 | { |
f8047558 | 626 | nxm_put(b, field, version, &value, &mask, sizeof value); |
09246b99 BP |
627 | } |
628 | ||
629 | static void | |
f8047558 BP |
630 | nxm_put_8(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version, |
631 | uint8_t value) | |
09246b99 | 632 | { |
f8047558 | 633 | nxm_put_unmasked(b, field, version, &value, sizeof value); |
09246b99 BP |
634 | } |
635 | ||
636 | static void | |
f8047558 BP |
637 | nxm_put_16m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version, |
638 | ovs_be16 value, ovs_be16 mask) | |
09246b99 | 639 | { |
f8047558 | 640 | nxm_put(b, field, version, &value, &mask, sizeof value); |
09246b99 BP |
641 | } |
642 | ||
643 | static void | |
f8047558 BP |
644 | nxm_put_16(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version, |
645 | ovs_be16 value) | |
09246b99 | 646 | { |
f8047558 | 647 | nxm_put_unmasked(b, field, version, &value, sizeof value); |
09246b99 BP |
648 | } |
649 | ||
8368c090 | 650 | static void |
f8047558 BP |
651 | nxm_put_32m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version, |
652 | ovs_be32 value, ovs_be32 mask) | |
8368c090 | 653 | { |
f8047558 | 654 | nxm_put(b, field, version, &value, &mask, sizeof value); |
8368c090 BP |
655 | } |
656 | ||
657 | static void | |
f8047558 BP |
658 | nxm_put_32(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version, |
659 | ovs_be32 value) | |
8368c090 | 660 | { |
f8047558 | 661 | nxm_put_unmasked(b, field, version, &value, sizeof value); |
8368c090 BP |
662 | } |
663 | ||
09246b99 | 664 | static void |
f8047558 BP |
665 | nxm_put_64m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version, |
666 | ovs_be64 value, ovs_be64 mask) | |
09246b99 | 667 | { |
f8047558 | 668 | nxm_put(b, field, version, &value, &mask, sizeof value); |
09246b99 BP |
669 | } |
670 | ||
1e37a2d7 | 671 | static void |
f8047558 BP |
672 | nxm_put_eth_masked(struct ofpbuf *b, |
673 | enum mf_field_id field, enum ofp_version version, | |
73c0ce34 JS |
674 | const uint8_t value[ETH_ADDR_LEN], |
675 | const uint8_t mask[ETH_ADDR_LEN]) | |
1e37a2d7 | 676 | { |
f8047558 | 677 | nxm_put(b, field, version, value, mask, ETH_ADDR_LEN); |
1e37a2d7 BP |
678 | } |
679 | ||
d31f1109 | 680 | static void |
f8047558 BP |
681 | nxm_put_ipv6(struct ofpbuf *b, |
682 | enum mf_field_id field, enum ofp_version version, | |
d31f1109 JP |
683 | const struct in6_addr *value, const struct in6_addr *mask) |
684 | { | |
f8047558 BP |
685 | nxm_put(b, field, version, value->s6_addr, mask->s6_addr, |
686 | sizeof value->s6_addr); | |
d31f1109 JP |
687 | } |
688 | ||
7257b535 | 689 | static void |
f8047558 BP |
690 | nxm_put_frag(struct ofpbuf *b, const struct match *match, |
691 | enum ofp_version version) | |
7257b535 | 692 | { |
f8047558 BP |
693 | uint8_t nw_frag = match->flow.nw_frag & FLOW_NW_FRAG_MASK; |
694 | uint8_t nw_frag_mask = match->wc.masks.nw_frag & FLOW_NW_FRAG_MASK; | |
7257b535 | 695 | |
f8047558 BP |
696 | nxm_put_8m(b, MFF_IP_FRAG, version, nw_frag, |
697 | nw_frag_mask == FLOW_NW_FRAG_MASK ? UINT8_MAX : nw_frag_mask); | |
7257b535 BP |
698 | } |
699 | ||
5e10215d BP |
700 | /* Appends to 'b' a set of OXM or NXM matches for the IPv4 or IPv6 fields in |
701 | * 'match'. */ | |
8e7082b0 | 702 | static void |
9d84066c | 703 | nxm_put_ip(struct ofpbuf *b, const struct match *match, enum ofp_version oxm) |
8e7082b0 | 704 | { |
81a76618 | 705 | const struct flow *flow = &match->flow; |
8e7082b0 | 706 | |
5e10215d | 707 | if (flow->dl_type == htons(ETH_TYPE_IP)) { |
f8047558 | 708 | nxm_put_32m(b, MFF_IPV4_SRC, oxm, |
5e10215d | 709 | flow->nw_src, match->wc.masks.nw_src); |
f8047558 | 710 | nxm_put_32m(b, MFF_IPV4_DST, oxm, |
5e10215d BP |
711 | flow->nw_dst, match->wc.masks.nw_dst); |
712 | } else { | |
f8047558 | 713 | nxm_put_ipv6(b, MFF_IPV6_SRC, oxm, |
5e10215d | 714 | &flow->ipv6_src, &match->wc.masks.ipv6_src); |
f8047558 | 715 | nxm_put_ipv6(b, MFF_IPV6_DST, oxm, |
5e10215d BP |
716 | &flow->ipv6_dst, &match->wc.masks.ipv6_dst); |
717 | } | |
718 | ||
21119b3e | 719 | nxm_put_frag(b, match, oxm); |
8e7082b0 | 720 | |
81a76618 | 721 | if (match->wc.masks.nw_tos & IP_DSCP_MASK) { |
1638b906 | 722 | if (oxm) { |
f8047558 | 723 | nxm_put_8(b, MFF_IP_DSCP_SHIFTED, oxm, |
9d84066c | 724 | flow->nw_tos >> 2); |
1638b906 | 725 | } else { |
f8047558 | 726 | nxm_put_8(b, MFF_IP_DSCP, oxm, |
9d84066c | 727 | flow->nw_tos & IP_DSCP_MASK); |
1638b906 | 728 | } |
8e7082b0 BP |
729 | } |
730 | ||
81a76618 | 731 | if (match->wc.masks.nw_tos & IP_ECN_MASK) { |
f8047558 | 732 | nxm_put_8(b, MFF_IP_ECN, oxm, |
b5ae8913 | 733 | flow->nw_tos & IP_ECN_MASK); |
8e7082b0 BP |
734 | } |
735 | ||
81a76618 | 736 | if (!oxm && match->wc.masks.nw_ttl) { |
f8047558 | 737 | nxm_put_8(b, MFF_IP_TTL, oxm, flow->nw_ttl); |
8e7082b0 BP |
738 | } |
739 | ||
f8047558 | 740 | nxm_put_32m(b, MFF_IPV6_LABEL, oxm, |
5e10215d BP |
741 | flow->ipv6_label, match->wc.masks.ipv6_label); |
742 | ||
81a76618 | 743 | if (match->wc.masks.nw_proto) { |
f8047558 | 744 | nxm_put_8(b, MFF_IP_PROTO, oxm, flow->nw_proto); |
8e7082b0 BP |
745 | |
746 | if (flow->nw_proto == IPPROTO_TCP) { | |
f8047558 | 747 | nxm_put_16m(b, MFF_TCP_SRC, oxm, |
81a76618 | 748 | flow->tp_src, match->wc.masks.tp_src); |
f8047558 | 749 | nxm_put_16m(b, MFF_TCP_DST, oxm, |
81a76618 | 750 | flow->tp_dst, match->wc.masks.tp_dst); |
f8047558 | 751 | nxm_put_16m(b, MFF_TCP_FLAGS, oxm, |
dc235f7f | 752 | flow->tcp_flags, match->wc.masks.tcp_flags); |
8e7082b0 | 753 | } else if (flow->nw_proto == IPPROTO_UDP) { |
f8047558 | 754 | nxm_put_16m(b, MFF_UDP_SRC, oxm, |
81a76618 | 755 | flow->tp_src, match->wc.masks.tp_src); |
f8047558 | 756 | nxm_put_16m(b, MFF_UDP_DST, oxm, |
81a76618 | 757 | flow->tp_dst, match->wc.masks.tp_dst); |
0d56eaf2 | 758 | } else if (flow->nw_proto == IPPROTO_SCTP) { |
f8047558 | 759 | nxm_put_16m(b, MFF_SCTP_SRC, oxm, flow->tp_src, |
0d56eaf2 | 760 | match->wc.masks.tp_src); |
f8047558 | 761 | nxm_put_16m(b, MFF_SCTP_DST, oxm, flow->tp_dst, |
0d56eaf2 | 762 | match->wc.masks.tp_dst); |
5e10215d BP |
763 | } else if (is_icmpv4(flow)) { |
764 | if (match->wc.masks.tp_src) { | |
f8047558 | 765 | nxm_put_8(b, MFF_ICMPV4_TYPE, oxm, |
5e10215d BP |
766 | ntohs(flow->tp_src)); |
767 | } | |
768 | if (match->wc.masks.tp_dst) { | |
f8047558 | 769 | nxm_put_8(b, MFF_ICMPV4_CODE, oxm, |
5e10215d BP |
770 | ntohs(flow->tp_dst)); |
771 | } | |
772 | } else if (is_icmpv6(flow)) { | |
81a76618 | 773 | if (match->wc.masks.tp_src) { |
f8047558 | 774 | nxm_put_8(b, MFF_ICMPV6_TYPE, oxm, |
5e10215d | 775 | ntohs(flow->tp_src)); |
8e7082b0 | 776 | } |
81a76618 | 777 | if (match->wc.masks.tp_dst) { |
f8047558 | 778 | nxm_put_8(b, MFF_ICMPV6_CODE, oxm, |
5e10215d BP |
779 | ntohs(flow->tp_dst)); |
780 | } | |
781 | if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) || | |
782 | flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) { | |
f8047558 | 783 | nxm_put_ipv6(b, MFF_ND_TARGET, oxm, |
5e10215d BP |
784 | &flow->nd_target, &match->wc.masks.nd_target); |
785 | if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) { | |
f8047558 | 786 | nxm_put_eth_masked(b, MFF_ND_SLL, oxm, |
5e10215d BP |
787 | flow->arp_sha, match->wc.masks.arp_sha); |
788 | } | |
789 | if (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) { | |
f8047558 | 790 | nxm_put_eth_masked(b, MFF_ND_TLL, oxm, |
5e10215d BP |
791 | flow->arp_tha, match->wc.masks.arp_tha); |
792 | } | |
8e7082b0 BP |
793 | } |
794 | } | |
795 | } | |
796 | } | |
797 | ||
81a76618 | 798 | /* Appends to 'b' the nx_match format that expresses 'match'. For Flow Mod and |
7623f4dd SH |
799 | * Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied. |
800 | * Otherwise, 'cookie_mask' should be zero. | |
4d0ed519 | 801 | * |
9d84066c BP |
802 | * Specify 'oxm' as 0 to express the match in NXM format; otherwise, specify |
803 | * 'oxm' as the OpenFlow version number for the OXM format to use. | |
804 | * | |
4d0ed519 BP |
805 | * This function can cause 'b''s data to be reallocated. |
806 | * | |
807 | * Returns the number of bytes appended to 'b', excluding padding. | |
808 | * | |
81a76618 | 809 | * If 'match' is a catch-all rule that matches every packet, then this function |
4d0ed519 | 810 | * appends nothing to 'b' and returns 0. */ |
7623f4dd | 811 | static int |
9d84066c | 812 | nx_put_raw(struct ofpbuf *b, enum ofp_version oxm, const struct match *match, |
7623f4dd | 813 | ovs_be64 cookie, ovs_be64 cookie_mask) |
09246b99 | 814 | { |
81a76618 | 815 | const struct flow *flow = &match->flow; |
1f317cb5 | 816 | const size_t start_len = ofpbuf_size(b); |
09246b99 | 817 | int match_len; |
b6c9e612 | 818 | int i; |
09246b99 | 819 | |
c61f3870 | 820 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 28); |
a877206f | 821 | |
09246b99 | 822 | /* Metadata. */ |
a79f29f2 | 823 | if (match->wc.masks.dp_hash) { |
f8047558 | 824 | nxm_put_32m(b, MFF_DP_HASH, oxm, |
447b6582 | 825 | htonl(flow->dp_hash), htonl(match->wc.masks.dp_hash)); |
a79f29f2 AZ |
826 | } |
827 | ||
828 | if (match->wc.masks.recirc_id) { | |
f8047558 | 829 | nxm_put_32(b, MFF_RECIRC_ID, oxm, htonl(flow->recirc_id)); |
a79f29f2 AZ |
830 | } |
831 | ||
4e022ec0 AW |
832 | if (match->wc.masks.in_port.ofp_port) { |
833 | ofp_port_t in_port = flow->in_port.ofp_port; | |
b5ae8913 | 834 | if (oxm) { |
f8047558 | 835 | nxm_put_32(b, MFF_IN_PORT_OXM, oxm, |
9d84066c | 836 | ofputil_port_to_ofp11(in_port)); |
b5ae8913 | 837 | } else { |
f8047558 | 838 | nxm_put_16(b, MFF_IN_PORT, oxm, |
9d84066c | 839 | htons(ofp_to_u16(in_port))); |
b5ae8913 | 840 | } |
09246b99 | 841 | } |
c61f3870 BP |
842 | if (match->wc.masks.actset_output) { |
843 | nxm_put_32(b, MFF_ACTSET_OUTPUT, oxm, | |
844 | ofputil_port_to_ofp11(flow->actset_output)); | |
845 | } | |
09246b99 BP |
846 | |
847 | /* Ethernet. */ | |
f8047558 | 848 | nxm_put_eth_masked(b, MFF_ETH_SRC, oxm, |
81a76618 | 849 | flow->dl_src, match->wc.masks.dl_src); |
f8047558 | 850 | nxm_put_eth_masked(b, MFF_ETH_DST, oxm, |
81a76618 | 851 | flow->dl_dst, match->wc.masks.dl_dst); |
f8047558 | 852 | nxm_put_16m(b, MFF_ETH_TYPE, oxm, |
e2170cff | 853 | ofputil_dl_type_to_openflow(flow->dl_type), |
81a76618 | 854 | match->wc.masks.dl_type); |
09246b99 | 855 | |
95f61ba8 SH |
856 | /* 802.1Q. */ |
857 | if (oxm) { | |
26720e24 BP |
858 | ovs_be16 VID_CFI_MASK = htons(VLAN_VID_MASK | VLAN_CFI); |
859 | ovs_be16 vid = flow->vlan_tci & VID_CFI_MASK; | |
81a76618 | 860 | ovs_be16 mask = match->wc.masks.vlan_tci & VID_CFI_MASK; |
95f61ba8 SH |
861 | |
862 | if (mask == htons(VLAN_VID_MASK | VLAN_CFI)) { | |
f8047558 | 863 | nxm_put_16(b, MFF_VLAN_VID, oxm, vid); |
95f61ba8 | 864 | } else if (mask) { |
f8047558 | 865 | nxm_put_16m(b, MFF_VLAN_VID, oxm, vid, mask); |
95f61ba8 SH |
866 | } |
867 | ||
81a76618 | 868 | if (vid && vlan_tci_to_pcp(match->wc.masks.vlan_tci)) { |
f8047558 | 869 | nxm_put_8(b, MFF_VLAN_PCP, oxm, |
9d84066c | 870 | vlan_tci_to_pcp(flow->vlan_tci)); |
95f61ba8 SH |
871 | } |
872 | ||
873 | } else { | |
f8047558 | 874 | nxm_put_16m(b, MFF_VLAN_TCI, oxm, flow->vlan_tci, |
81a76618 | 875 | match->wc.masks.vlan_tci); |
95f61ba8 | 876 | } |
09246b99 | 877 | |
b02475c5 SH |
878 | /* MPLS. */ |
879 | if (eth_type_mpls(flow->dl_type)) { | |
8bfd0fda | 880 | if (match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) { |
f8047558 | 881 | nxm_put_8(b, MFF_MPLS_TC, oxm, |
9d84066c | 882 | mpls_lse_to_tc(flow->mpls_lse[0])); |
b02475c5 SH |
883 | } |
884 | ||
8bfd0fda | 885 | if (match->wc.masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) { |
f8047558 | 886 | nxm_put_8(b, MFF_MPLS_BOS, oxm, |
9d84066c | 887 | mpls_lse_to_bos(flow->mpls_lse[0])); |
b02475c5 SH |
888 | } |
889 | ||
8bfd0fda | 890 | if (match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) { |
f8047558 | 891 | nxm_put_32(b, MFF_MPLS_LABEL, oxm, |
8bfd0fda | 892 | htonl(mpls_lse_to_label(flow->mpls_lse[0]))); |
b02475c5 SH |
893 | } |
894 | } | |
895 | ||
66642cb4 | 896 | /* L3. */ |
5e10215d BP |
897 | if (is_ip_any(flow)) { |
898 | nxm_put_ip(b, match, oxm); | |
8087f5ff MM |
899 | } else if (flow->dl_type == htons(ETH_TYPE_ARP) || |
900 | flow->dl_type == htons(ETH_TYPE_RARP)) { | |
09246b99 | 901 | /* ARP. */ |
81a76618 | 902 | if (match->wc.masks.nw_proto) { |
f8047558 | 903 | nxm_put_16(b, MFF_ARP_OP, oxm, |
b5ae8913 | 904 | htons(flow->nw_proto)); |
09246b99 | 905 | } |
f8047558 | 906 | nxm_put_32m(b, MFF_ARP_SPA, oxm, |
81a76618 | 907 | flow->nw_src, match->wc.masks.nw_src); |
f8047558 | 908 | nxm_put_32m(b, MFF_ARP_TPA, oxm, |
81a76618 | 909 | flow->nw_dst, match->wc.masks.nw_dst); |
f8047558 | 910 | nxm_put_eth_masked(b, MFF_ARP_SHA, oxm, |
81a76618 | 911 | flow->arp_sha, match->wc.masks.arp_sha); |
f8047558 | 912 | nxm_put_eth_masked(b, MFF_ARP_THA, oxm, |
81a76618 | 913 | flow->arp_tha, match->wc.masks.arp_tha); |
09246b99 BP |
914 | } |
915 | ||
916 | /* Tunnel ID. */ | |
f8047558 | 917 | nxm_put_64m(b, MFF_TUN_ID, oxm, |
0ad90c84 JR |
918 | flow->tunnel.tun_id, match->wc.masks.tunnel.tun_id); |
919 | ||
920 | /* Other tunnel metadata. */ | |
f8047558 | 921 | nxm_put_32m(b, MFF_TUN_SRC, oxm, |
0ad90c84 | 922 | flow->tunnel.ip_src, match->wc.masks.tunnel.ip_src); |
f8047558 | 923 | nxm_put_32m(b, MFF_TUN_DST, oxm, |
0ad90c84 | 924 | flow->tunnel.ip_dst, match->wc.masks.tunnel.ip_dst); |
09246b99 | 925 | |
b6c9e612 | 926 | /* Registers. */ |
a678b23e BP |
927 | if (oxm < OFP15_VERSION) { |
928 | for (i = 0; i < FLOW_N_REGS; i++) { | |
f8047558 | 929 | nxm_put_32m(b, MFF_REG0 + i, oxm, |
a678b23e BP |
930 | htonl(flow->regs[i]), htonl(match->wc.masks.regs[i])); |
931 | } | |
932 | } else { | |
933 | for (i = 0; i < FLOW_N_XREGS; i++) { | |
f8047558 | 934 | nxm_put_64m(b, MFF_XREG0 + i, oxm, |
a678b23e BP |
935 | htonll(flow_get_xreg(flow, i)), |
936 | htonll(flow_get_xreg(&match->wc.masks, i))); | |
937 | } | |
b6c9e612 BP |
938 | } |
939 | ||
ac923e91 | 940 | /* Mark. */ |
f8047558 | 941 | nxm_put_32m(b, MFF_PKT_MARK, oxm, htonl(flow->pkt_mark), |
ac923e91 JG |
942 | htonl(match->wc.masks.pkt_mark)); |
943 | ||
969fc56c | 944 | /* OpenFlow 1.1+ Metadata. */ |
f8047558 | 945 | nxm_put_64m(b, MFF_METADATA, oxm, |
9d84066c | 946 | flow->metadata, match->wc.masks.metadata); |
969fc56c | 947 | |
e729e793 | 948 | /* Cookie. */ |
f8047558 BP |
949 | if (cookie_mask) { |
950 | bool masked = cookie_mask != OVS_BE64_MAX; | |
951 | ||
952 | cookie &= cookie_mask; | |
953 | nx_put_header__(b, NXM_NX_COOKIE, masked); | |
954 | ofpbuf_put(b, &cookie, sizeof cookie); | |
955 | if (masked) { | |
956 | ofpbuf_put(b, &cookie_mask, sizeof cookie_mask); | |
957 | } | |
958 | } | |
e729e793 | 959 | |
1f317cb5 | 960 | match_len = ofpbuf_size(b) - start_len; |
7623f4dd SH |
961 | return match_len; |
962 | } | |
963 | ||
81a76618 | 964 | /* Appends to 'b' the nx_match format that expresses 'match', plus enough zero |
7623f4dd SH |
965 | * bytes to pad the nx_match out to a multiple of 8. For Flow Mod and Flow |
966 | * Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied. | |
967 | * Otherwise, 'cookie_mask' should be zero. | |
968 | * | |
969 | * This function can cause 'b''s data to be reallocated. | |
970 | * | |
971 | * Returns the number of bytes appended to 'b', excluding padding. The return | |
972 | * value can be zero if it appended nothing at all to 'b' (which happens if | |
973 | * 'cr' is a catch-all rule that matches every packet). */ | |
974 | int | |
81a76618 | 975 | nx_put_match(struct ofpbuf *b, const struct match *match, |
7623f4dd SH |
976 | ovs_be64 cookie, ovs_be64 cookie_mask) |
977 | { | |
9d84066c | 978 | int match_len = nx_put_raw(b, 0, match, cookie, cookie_mask); |
7623f4dd | 979 | |
f6e984d7 | 980 | ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8)); |
7623f4dd SH |
981 | return match_len; |
982 | } | |
983 | ||
9d84066c | 984 | /* Appends to 'b' an struct ofp11_match_header followed by the OXM format that |
81a76618 BP |
985 | * expresses 'cr', plus enough zero bytes to pad the data appended out to a |
986 | * multiple of 8. | |
7623f4dd | 987 | * |
9d84066c BP |
988 | * OXM differs slightly among versions of OpenFlow. Specify the OpenFlow |
989 | * version in use as 'version'. | |
990 | * | |
7623f4dd SH |
991 | * This function can cause 'b''s data to be reallocated. |
992 | * | |
993 | * Returns the number of bytes appended to 'b', excluding the padding. Never | |
994 | * returns zero. */ | |
995 | int | |
9d84066c BP |
996 | oxm_put_match(struct ofpbuf *b, const struct match *match, |
997 | enum ofp_version version) | |
7623f4dd SH |
998 | { |
999 | int match_len; | |
1000 | struct ofp11_match_header *omh; | |
1f317cb5 | 1001 | size_t start_len = ofpbuf_size(b); |
7623f4dd SH |
1002 | ovs_be64 cookie = htonll(0), cookie_mask = htonll(0); |
1003 | ||
1004 | ofpbuf_put_uninit(b, sizeof *omh); | |
9d84066c BP |
1005 | match_len = (nx_put_raw(b, version, match, cookie, cookie_mask) |
1006 | + sizeof *omh); | |
f6e984d7 | 1007 | ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8)); |
7623f4dd | 1008 | |
db5a1019 | 1009 | omh = ofpbuf_at(b, start_len, sizeof *omh); |
7623f4dd SH |
1010 | omh->type = htons(OFPMT_OXM); |
1011 | omh->length = htons(match_len); | |
1012 | ||
09246b99 BP |
1013 | return match_len; |
1014 | } | |
178742f9 | 1015 | |
f8047558 | 1016 | static void |
508a9338 | 1017 | nx_put_header__(struct ofpbuf *b, uint64_t header, bool masked) |
f8047558 | 1018 | { |
508a9338 BP |
1019 | uint64_t masked_header = masked ? nxm_make_wild_header(header) : header; |
1020 | ovs_be64 network_header = htonll(masked_header); | |
f8047558 | 1021 | |
508a9338 | 1022 | ofpbuf_put(b, &network_header, nxm_header_len(header)); |
f8047558 BP |
1023 | } |
1024 | ||
178742f9 BP |
1025 | void |
1026 | nx_put_header(struct ofpbuf *b, enum mf_field_id field, | |
1027 | enum ofp_version version, bool masked) | |
1028 | { | |
f8047558 | 1029 | nx_put_header__(b, mf_oxm_header(field, version), masked); |
178742f9 BP |
1030 | } |
1031 | ||
1032 | void | |
1033 | nx_put_entry(struct ofpbuf *b, | |
1034 | enum mf_field_id field, enum ofp_version version, | |
1035 | const union mf_value *value, const union mf_value *mask) | |
1036 | { | |
1037 | int n_bytes = mf_from_id(field)->n_bytes; | |
1038 | bool masked = mask && !is_all_ones(mask, n_bytes); | |
1039 | ||
1040 | nx_put_header(b, field, version, masked); | |
1041 | ofpbuf_put(b, value, n_bytes); | |
1042 | if (masked) { | |
1043 | ofpbuf_put(b, mask, n_bytes); | |
1044 | } | |
1045 | } | |
09246b99 BP |
1046 | \f |
1047 | /* nx_match_to_string() and helpers. */ | |
1048 | ||
508a9338 | 1049 | static void format_nxm_field_name(struct ds *, uint64_t header); |
f393f81e | 1050 | |
09246b99 BP |
1051 | char * |
1052 | nx_match_to_string(const uint8_t *p, unsigned int match_len) | |
1053 | { | |
178742f9 | 1054 | struct ofpbuf b; |
09246b99 BP |
1055 | struct ds s; |
1056 | ||
1057 | if (!match_len) { | |
1058 | return xstrdup("<any>"); | |
1059 | } | |
1060 | ||
178742f9 | 1061 | ofpbuf_use_const(&b, p, match_len); |
09246b99 | 1062 | ds_init(&s); |
178742f9 BP |
1063 | while (ofpbuf_size(&b)) { |
1064 | union mf_value value; | |
1065 | union mf_value mask; | |
1066 | enum ofperr error; | |
508a9338 | 1067 | uint64_t header; |
178742f9 BP |
1068 | int value_len; |
1069 | ||
1070 | error = nx_pull_entry__(&b, true, &header, NULL, &value, &mask); | |
1071 | if (error) { | |
1072 | break; | |
1073 | } | |
1074 | value_len = MIN(sizeof value, nxm_field_bytes(header)); | |
09246b99 BP |
1075 | |
1076 | if (s.length) { | |
1077 | ds_put_cstr(&s, ", "); | |
1078 | } | |
1079 | ||
f393f81e | 1080 | format_nxm_field_name(&s, header); |
09246b99 BP |
1081 | ds_put_char(&s, '('); |
1082 | ||
178742f9 BP |
1083 | for (int i = 0; i < value_len; i++) { |
1084 | ds_put_format(&s, "%02x", ((const uint8_t *) &value)[i]); | |
09246b99 | 1085 | } |
178742f9 | 1086 | if (nxm_hasmask(header)) { |
09246b99 | 1087 | ds_put_char(&s, '/'); |
178742f9 BP |
1088 | for (int i = 0; i < value_len; i++) { |
1089 | ds_put_format(&s, "%02x", ((const uint8_t *) &mask)[i]); | |
09246b99 BP |
1090 | } |
1091 | } | |
1092 | ds_put_char(&s, ')'); | |
09246b99 BP |
1093 | } |
1094 | ||
178742f9 | 1095 | if (ofpbuf_size(&b)) { |
09246b99 BP |
1096 | if (s.length) { |
1097 | ds_put_cstr(&s, ", "); | |
1098 | } | |
1099 | ||
178742f9 | 1100 | ds_put_format(&s, "<%u invalid bytes>", ofpbuf_size(&b)); |
09246b99 BP |
1101 | } |
1102 | ||
1103 | return ds_steal_cstr(&s); | |
1104 | } | |
1105 | ||
7623f4dd | 1106 | char * |
db5a1019 | 1107 | oxm_match_to_string(const struct ofpbuf *p, unsigned int match_len) |
7623f4dd | 1108 | { |
1f317cb5 | 1109 | const struct ofp11_match_header *omh = ofpbuf_data(p); |
7623f4dd SH |
1110 | uint16_t match_len_; |
1111 | struct ds s; | |
1112 | ||
1113 | ds_init(&s); | |
1114 | ||
1115 | if (match_len < sizeof *omh) { | |
1116 | ds_put_format(&s, "<match too short: %u>", match_len); | |
1117 | goto err; | |
1118 | } | |
1119 | ||
1120 | if (omh->type != htons(OFPMT_OXM)) { | |
1121 | ds_put_format(&s, "<bad match type field: %u>", ntohs(omh->type)); | |
1122 | goto err; | |
1123 | } | |
1124 | ||
1125 | match_len_ = ntohs(omh->length); | |
1126 | if (match_len_ < sizeof *omh) { | |
1127 | ds_put_format(&s, "<match length field too short: %u>", match_len_); | |
1128 | goto err; | |
1129 | } | |
1130 | ||
1131 | if (match_len_ != match_len) { | |
1132 | ds_put_format(&s, "<match length field incorrect: %u != %u>", | |
1133 | match_len_, match_len); | |
1134 | goto err; | |
1135 | } | |
1136 | ||
db5a1019 AW |
1137 | return nx_match_to_string(ofpbuf_at(p, sizeof *omh, 0), |
1138 | match_len - sizeof *omh); | |
7623f4dd SH |
1139 | |
1140 | err: | |
1141 | return ds_steal_cstr(&s); | |
1142 | } | |
1143 | ||
178742f9 BP |
1144 | void |
1145 | nx_format_field_name(enum mf_field_id id, enum ofp_version version, | |
1146 | struct ds *s) | |
1147 | { | |
1148 | format_nxm_field_name(s, mf_oxm_header(id, version)); | |
1149 | } | |
1150 | ||
f393f81e | 1151 | static void |
508a9338 | 1152 | format_nxm_field_name(struct ds *s, uint64_t header) |
f393f81e | 1153 | { |
178742f9 BP |
1154 | const struct nxm_field *f = nxm_field_by_header(header); |
1155 | if (f) { | |
1156 | ds_put_cstr(s, f->name); | |
1157 | if (nxm_hasmask(header)) { | |
28da1f8f BP |
1158 | ds_put_cstr(s, "_W"); |
1159 | } | |
e729e793 JP |
1160 | } else if (header == NXM_NX_COOKIE) { |
1161 | ds_put_cstr(s, "NXM_NX_COOKIE"); | |
1162 | } else if (header == NXM_NX_COOKIE_W) { | |
1163 | ds_put_cstr(s, "NXM_NX_COOKIE_W"); | |
f393f81e | 1164 | } else { |
c8058af7 | 1165 | ds_put_format(s, "%d:%d", nxm_class(header), nxm_field(header)); |
f393f81e BP |
1166 | } |
1167 | } | |
1168 | ||
178742f9 BP |
1169 | static bool |
1170 | streq_len(const char *a, size_t a_len, const char *b) | |
1171 | { | |
1172 | return strlen(b) == a_len && !memcmp(a, b, a_len); | |
1173 | } | |
1174 | ||
508a9338 | 1175 | static uint64_t |
558d80cb | 1176 | parse_nxm_field_name(const char *name, int name_len) |
09246b99 | 1177 | { |
178742f9 | 1178 | const struct nxm_field *f; |
28da1f8f | 1179 | bool wild; |
28da1f8f | 1180 | |
178742f9 BP |
1181 | f = mf_parse_subfield_name(name, name_len, &wild); |
1182 | if (f) { | |
b5ae8913 | 1183 | if (!wild) { |
178742f9 BP |
1184 | return f->header; |
1185 | } else if (mf_from_id(f->id)->maskable != MFM_NONE) { | |
1186 | return nxm_make_wild_header(f->header); | |
09246b99 BP |
1187 | } |
1188 | } | |
1189 | ||
178742f9 BP |
1190 | if (streq_len(name, name_len, "NXM_NX_COOKIE")) { |
1191 | return NXM_NX_COOKIE; | |
1192 | } else if (streq_len(name, name_len, "NXM_NX_COOKIE_W")) { | |
1193 | return NXM_NX_COOKIE_W; | |
e729e793 JP |
1194 | } |
1195 | ||
508a9338 | 1196 | /* Check whether it's a field header value as hex. |
558d80cb BP |
1197 | * (This isn't ordinarily useful except for testing error behavior.) */ |
1198 | if (name_len == 8) { | |
508a9338 | 1199 | uint64_t header; |
0429d959 BP |
1200 | bool ok; |
1201 | ||
508a9338 | 1202 | header = hexits_value(name, name_len, &ok) << 32; |
0429d959 | 1203 | if (ok) { |
558d80cb BP |
1204 | return header; |
1205 | } | |
508a9338 BP |
1206 | } else if (name_len == 16) { |
1207 | uint64_t header; | |
1208 | bool ok; | |
1209 | ||
1210 | header = hexits_value(name, name_len, &ok); | |
1211 | if (ok && is_experimenter_oxm(header)) { | |
1212 | return header; | |
1213 | } | |
558d80cb BP |
1214 | } |
1215 | ||
1216 | return 0; | |
09246b99 | 1217 | } |
09246b99 BP |
1218 | \f |
1219 | /* nx_match_from_string(). */ | |
1220 | ||
7623f4dd SH |
1221 | static int |
1222 | nx_match_from_string_raw(const char *s, struct ofpbuf *b) | |
09246b99 BP |
1223 | { |
1224 | const char *full_s = s; | |
1f317cb5 | 1225 | const size_t start_len = ofpbuf_size(b); |
09246b99 BP |
1226 | |
1227 | if (!strcmp(s, "<any>")) { | |
1f317cb5 | 1228 | /* Ensure that 'ofpbuf_data(b)' isn't actually null. */ |
09246b99 BP |
1229 | ofpbuf_prealloc_tailroom(b, 1); |
1230 | return 0; | |
1231 | } | |
1232 | ||
1233 | for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) { | |
558d80cb | 1234 | const char *name; |
508a9338 | 1235 | uint64_t header; |
09246b99 | 1236 | int name_len; |
78090f63 | 1237 | size_t n; |
09246b99 | 1238 | |
558d80cb | 1239 | name = s; |
09246b99 BP |
1240 | name_len = strcspn(s, "("); |
1241 | if (s[name_len] != '(') { | |
1242 | ovs_fatal(0, "%s: missing ( at end of nx_match", full_s); | |
1243 | } | |
1244 | ||
558d80cb BP |
1245 | header = parse_nxm_field_name(name, name_len); |
1246 | if (!header) { | |
09246b99 BP |
1247 | ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s); |
1248 | } | |
1249 | ||
1250 | s += name_len + 1; | |
1251 | ||
f8047558 | 1252 | nx_put_header__(b, header, false); |
78090f63 BP |
1253 | s = ofpbuf_put_hex(b, s, &n); |
1254 | if (n != nxm_field_bytes(header)) { | |
1255 | ovs_fatal(0, "%.2s: hex digits expected", s); | |
1256 | } | |
178742f9 | 1257 | if (nxm_hasmask(header)) { |
09246b99 BP |
1258 | s += strspn(s, " "); |
1259 | if (*s != '/') { | |
558d80cb BP |
1260 | ovs_fatal(0, "%s: missing / in masked field %.*s", |
1261 | full_s, name_len, name); | |
09246b99 | 1262 | } |
78090f63 BP |
1263 | s = ofpbuf_put_hex(b, s + 1, &n); |
1264 | if (n != nxm_field_bytes(header)) { | |
1265 | ovs_fatal(0, "%.2s: hex digits expected", s); | |
1266 | } | |
09246b99 BP |
1267 | } |
1268 | ||
1269 | s += strspn(s, " "); | |
1270 | if (*s != ')') { | |
558d80cb BP |
1271 | ovs_fatal(0, "%s: missing ) following field %.*s", |
1272 | full_s, name_len, name); | |
09246b99 BP |
1273 | } |
1274 | s++; | |
1275 | } | |
1276 | ||
1f317cb5 | 1277 | return ofpbuf_size(b) - start_len; |
7623f4dd SH |
1278 | } |
1279 | ||
1280 | int | |
1281 | nx_match_from_string(const char *s, struct ofpbuf *b) | |
1282 | { | |
1283 | int match_len = nx_match_from_string_raw(s, b); | |
f6e984d7 | 1284 | ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8)); |
7623f4dd SH |
1285 | return match_len; |
1286 | } | |
1287 | ||
1288 | int | |
1289 | oxm_match_from_string(const char *s, struct ofpbuf *b) | |
1290 | { | |
1291 | int match_len; | |
1292 | struct ofp11_match_header *omh; | |
1f317cb5 | 1293 | size_t start_len = ofpbuf_size(b); |
7623f4dd SH |
1294 | |
1295 | ofpbuf_put_uninit(b, sizeof *omh); | |
1296 | match_len = nx_match_from_string_raw(s, b) + sizeof *omh; | |
f6e984d7 | 1297 | ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8)); |
7623f4dd | 1298 | |
db5a1019 | 1299 | omh = ofpbuf_at(b, start_len, sizeof *omh); |
7623f4dd SH |
1300 | omh->type = htons(OFPMT_OXM); |
1301 | omh->length = htons(match_len); | |
1302 | ||
09246b99 BP |
1303 | return match_len; |
1304 | } | |
b6c9e612 | 1305 | \f |
bdda5aca BP |
1306 | /* Parses 's' as a "move" action, in the form described in ovs-ofctl(8), into |
1307 | * '*move'. | |
1308 | * | |
1309 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
1310 | * error. The caller is responsible for freeing the returned string. */ | |
1311 | char * WARN_UNUSED_RESULT | |
f25d0cf3 | 1312 | nxm_parse_reg_move(struct ofpact_reg_move *move, const char *s) |
f393f81e BP |
1313 | { |
1314 | const char *full_s = s; | |
bdda5aca | 1315 | char *error; |
f393f81e | 1316 | |
bdda5aca BP |
1317 | error = mf_parse_subfield__(&move->src, &s); |
1318 | if (error) { | |
1319 | return error; | |
1320 | } | |
f393f81e | 1321 | if (strncmp(s, "->", 2)) { |
bdda5aca | 1322 | return xasprintf("%s: missing `->' following source", full_s); |
f393f81e BP |
1323 | } |
1324 | s += 2; | |
bdda5aca BP |
1325 | error = mf_parse_subfield(&move->dst, s); |
1326 | if (error) { | |
1327 | return error; | |
f393f81e BP |
1328 | } |
1329 | ||
f25d0cf3 | 1330 | if (move->src.n_bits != move->dst.n_bits) { |
bdda5aca BP |
1331 | return xasprintf("%s: source field is %d bits wide but destination is " |
1332 | "%d bits wide", full_s, | |
1333 | move->src.n_bits, move->dst.n_bits); | |
f393f81e | 1334 | } |
bdda5aca | 1335 | return NULL; |
f393f81e | 1336 | } |
f393f81e | 1337 | \f |
7eb4b1f1 | 1338 | /* nxm_format_reg_move(). */ |
f393f81e | 1339 | |
f393f81e | 1340 | void |
f25d0cf3 | 1341 | nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s) |
f393f81e | 1342 | { |
f393f81e | 1343 | ds_put_format(s, "move:"); |
f25d0cf3 | 1344 | mf_format_subfield(&move->src, s); |
f393f81e | 1345 | ds_put_cstr(s, "->"); |
f25d0cf3 | 1346 | mf_format_subfield(&move->dst, s); |
f393f81e BP |
1347 | } |
1348 | ||
f25d0cf3 BP |
1349 | \f |
1350 | enum ofperr | |
f25d0cf3 | 1351 | nxm_reg_move_check(const struct ofpact_reg_move *move, const struct flow *flow) |
43edca57 | 1352 | { |
90bf1e07 | 1353 | enum ofperr error; |
43edca57 | 1354 | |
f25d0cf3 | 1355 | error = mf_check_src(&move->src, flow); |
43edca57 EJ |
1356 | if (error) { |
1357 | return error; | |
b6c9e612 BP |
1358 | } |
1359 | ||
f25d0cf3 BP |
1360 | return mf_check_dst(&move->dst, NULL); |
1361 | } | |
f25d0cf3 | 1362 | \f |
7eb4b1f1 | 1363 | /* nxm_execute_reg_move(). */ |
b6c9e612 BP |
1364 | |
1365 | void | |
f25d0cf3 | 1366 | nxm_execute_reg_move(const struct ofpact_reg_move *move, |
bcd2633a | 1367 | struct flow *flow, struct flow_wildcards *wc) |
b6c9e612 | 1368 | { |
f25d0cf3 BP |
1369 | union mf_value src_value; |
1370 | union mf_value dst_value; | |
816fd533 | 1371 | |
f47ea021 JR |
1372 | mf_mask_field_and_prereqs(move->dst.field, &wc->masks); |
1373 | mf_mask_field_and_prereqs(move->src.field, &wc->masks); | |
bcd2633a | 1374 | |
f25d0cf3 BP |
1375 | mf_get_value(move->dst.field, flow, &dst_value); |
1376 | mf_get_value(move->src.field, flow, &src_value); | |
1377 | bitwise_copy(&src_value, move->src.field->n_bytes, move->src.ofs, | |
1378 | &dst_value, move->dst.field->n_bytes, move->dst.ofs, | |
1379 | move->src.n_bits); | |
1380 | mf_set_flow_value(move->dst.field, &dst_value, flow); | |
43edca57 | 1381 | } |
b6c9e612 | 1382 | |
816fd533 | 1383 | void |
f25d0cf3 | 1384 | nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data, |
f74e7df7 | 1385 | struct flow *flow, struct flow_wildcards *wc) |
816fd533 | 1386 | { |
9bab681f | 1387 | union mf_subvalue src_subvalue; |
f74e7df7 | 1388 | union mf_subvalue mask_value; |
9bab681f | 1389 | ovs_be64 src_data_be = htonll(src_data); |
f25d0cf3 | 1390 | |
f74e7df7 JP |
1391 | memset(&mask_value, 0xff, sizeof mask_value); |
1392 | mf_write_subfield_flow(dst, &mask_value, &wc->masks); | |
1393 | ||
9bab681f IY |
1394 | bitwise_copy(&src_data_be, sizeof src_data_be, 0, |
1395 | &src_subvalue, sizeof src_subvalue, 0, | |
1396 | sizeof src_data_be * 8); | |
1397 | mf_write_subfield_flow(dst, &src_subvalue, flow); | |
b6c9e612 | 1398 | } |
bd85dac1 AZ |
1399 | \f |
1400 | /* nxm_parse_stack_action, works for both push() and pop(). */ | |
bdda5aca BP |
1401 | |
1402 | /* Parses 's' as a "push" or "pop" action, in the form described in | |
1403 | * ovs-ofctl(8), into '*stack_action'. | |
1404 | * | |
1405 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
1406 | * error. The caller is responsible for freeing the returned string. */ | |
1407 | char * WARN_UNUSED_RESULT | |
bd85dac1 AZ |
1408 | nxm_parse_stack_action(struct ofpact_stack *stack_action, const char *s) |
1409 | { | |
bdda5aca BP |
1410 | char *error; |
1411 | ||
1412 | error = mf_parse_subfield__(&stack_action->subfield, &s); | |
1413 | if (error) { | |
1414 | return error; | |
1415 | } | |
1416 | ||
bd85dac1 | 1417 | if (*s != '\0') { |
bdda5aca | 1418 | return xasprintf("%s: trailing garbage following push or pop", s); |
bd85dac1 | 1419 | } |
bdda5aca BP |
1420 | |
1421 | return NULL; | |
bd85dac1 AZ |
1422 | } |
1423 | ||
1424 | void | |
1425 | nxm_format_stack_push(const struct ofpact_stack *push, struct ds *s) | |
1426 | { | |
1427 | ds_put_cstr(s, "push:"); | |
1428 | mf_format_subfield(&push->subfield, s); | |
1429 | } | |
1430 | ||
1431 | void | |
1432 | nxm_format_stack_pop(const struct ofpact_stack *pop, struct ds *s) | |
1433 | { | |
1434 | ds_put_cstr(s, "pop:"); | |
1435 | mf_format_subfield(&pop->subfield, s); | |
1436 | } | |
1437 | ||
bd85dac1 AZ |
1438 | enum ofperr |
1439 | nxm_stack_push_check(const struct ofpact_stack *push, | |
1440 | const struct flow *flow) | |
1441 | { | |
1442 | return mf_check_src(&push->subfield, flow); | |
1443 | } | |
1444 | ||
1445 | enum ofperr | |
1446 | nxm_stack_pop_check(const struct ofpact_stack *pop, | |
1447 | const struct flow *flow) | |
1448 | { | |
1449 | return mf_check_dst(&pop->subfield, flow); | |
1450 | } | |
1451 | ||
bd85dac1 AZ |
1452 | /* nxm_execute_stack_push(), nxm_execute_stack_pop(). */ |
1453 | static void | |
1454 | nx_stack_push(struct ofpbuf *stack, union mf_subvalue *v) | |
1455 | { | |
1456 | ofpbuf_put(stack, v, sizeof *v); | |
1457 | } | |
1458 | ||
1459 | static union mf_subvalue * | |
1460 | nx_stack_pop(struct ofpbuf *stack) | |
1461 | { | |
1462 | union mf_subvalue *v = NULL; | |
1463 | ||
1f317cb5 PS |
1464 | if (ofpbuf_size(stack)) { |
1465 | ||
1466 | ofpbuf_set_size(stack, ofpbuf_size(stack) - sizeof *v); | |
bd85dac1 AZ |
1467 | v = (union mf_subvalue *) ofpbuf_tail(stack); |
1468 | } | |
1469 | ||
1470 | return v; | |
1471 | } | |
1472 | ||
1473 | void | |
1474 | nxm_execute_stack_push(const struct ofpact_stack *push, | |
bcd2633a JP |
1475 | const struct flow *flow, struct flow_wildcards *wc, |
1476 | struct ofpbuf *stack) | |
bd85dac1 | 1477 | { |
bcd2633a | 1478 | union mf_subvalue mask_value; |
bd85dac1 AZ |
1479 | union mf_subvalue dst_value; |
1480 | ||
bcd2633a JP |
1481 | memset(&mask_value, 0xff, sizeof mask_value); |
1482 | mf_write_subfield_flow(&push->subfield, &mask_value, &wc->masks); | |
1483 | ||
bd85dac1 AZ |
1484 | mf_read_subfield(&push->subfield, flow, &dst_value); |
1485 | nx_stack_push(stack, &dst_value); | |
1486 | } | |
1487 | ||
1488 | void | |
1489 | nxm_execute_stack_pop(const struct ofpact_stack *pop, | |
f74e7df7 JP |
1490 | struct flow *flow, struct flow_wildcards *wc, |
1491 | struct ofpbuf *stack) | |
bd85dac1 AZ |
1492 | { |
1493 | union mf_subvalue *src_value; | |
1494 | ||
1495 | src_value = nx_stack_pop(stack); | |
1496 | ||
1497 | /* Only pop if stack is not empty. Otherwise, give warning. */ | |
1498 | if (src_value) { | |
f74e7df7 JP |
1499 | union mf_subvalue mask_value; |
1500 | ||
1501 | memset(&mask_value, 0xff, sizeof mask_value); | |
1502 | mf_write_subfield_flow(&pop->subfield, &mask_value, &wc->masks); | |
bd85dac1 AZ |
1503 | mf_write_subfield_flow(&pop->subfield, src_value, flow); |
1504 | } else { | |
1505 | if (!VLOG_DROP_WARN(&rl)) { | |
1506 | char *flow_str = flow_to_string(flow); | |
1507 | VLOG_WARN_RL(&rl, "Failed to pop from an empty stack. On flow \n" | |
1508 | " %s", flow_str); | |
1509 | free(flow_str); | |
1510 | } | |
1511 | } | |
1512 | } | |
178742f9 BP |
1513 | \f |
1514 | /* Formats 'sf' into 's' in a format normally acceptable to | |
1515 | * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if | |
1516 | * sf->field has no NXM name.) */ | |
1517 | void | |
1518 | mf_format_subfield(const struct mf_subfield *sf, struct ds *s) | |
1519 | { | |
1520 | if (!sf->field) { | |
1521 | ds_put_cstr(s, "<unknown>"); | |
1522 | } else { | |
e6556fe3 | 1523 | const struct nxm_field *f = nxm_field_by_mf_id(sf->field->id, 0); |
178742f9 BP |
1524 | ds_put_cstr(s, f ? f->name : sf->field->name); |
1525 | } | |
1526 | ||
1527 | if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) { | |
1528 | ds_put_cstr(s, "[]"); | |
1529 | } else if (sf->n_bits == 1) { | |
1530 | ds_put_format(s, "[%d]", sf->ofs); | |
1531 | } else { | |
1532 | ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1); | |
1533 | } | |
1534 | } | |
1535 | ||
1536 | static const struct nxm_field * | |
1537 | mf_parse_subfield_name(const char *name, int name_len, bool *wild) | |
1538 | { | |
1539 | *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2); | |
1540 | if (*wild) { | |
1541 | name_len -= 2; | |
1542 | } | |
1543 | ||
1544 | return nxm_field_by_name(name, name_len); | |
1545 | } | |
1546 | ||
1547 | /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful, | |
1548 | * returns NULL and advances '*sp' to the first byte following the parsed | |
1549 | * string. On failure, returns a malloc()'d error message, does not modify | |
1550 | * '*sp', and does not properly initialize 'sf'. | |
1551 | * | |
1552 | * The syntax parsed from '*sp' takes the form "header[start..end]" where | |
1553 | * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive) | |
1554 | * bit indexes. "..end" may be omitted to indicate a single bit. "start..end" | |
1555 | * may both be omitted (the [] are still required) to indicate an entire | |
1556 | * field. */ | |
1557 | char * WARN_UNUSED_RESULT | |
1558 | mf_parse_subfield__(struct mf_subfield *sf, const char **sp) | |
1559 | { | |
1560 | const struct mf_field *field; | |
1561 | const struct nxm_field *f; | |
1562 | const char *name; | |
1563 | int start, end; | |
1564 | const char *s; | |
1565 | int name_len; | |
1566 | bool wild; | |
1567 | ||
1568 | s = *sp; | |
1569 | name = s; | |
1570 | name_len = strcspn(s, "["); | |
1571 | if (s[name_len] != '[') { | |
1572 | return xasprintf("%s: missing [ looking for field name", *sp); | |
1573 | } | |
1574 | ||
1575 | f = mf_parse_subfield_name(name, name_len, &wild); | |
1576 | if (!f) { | |
1577 | return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s); | |
1578 | } | |
1579 | field = mf_from_id(f->id); | |
1580 | ||
1581 | s += name_len; | |
1582 | if (ovs_scan(s, "[%d..%d]", &start, &end)) { | |
1583 | /* Nothing to do. */ | |
1584 | } else if (ovs_scan(s, "[%d]", &start)) { | |
1585 | end = start; | |
1586 | } else if (!strncmp(s, "[]", 2)) { | |
1587 | start = 0; | |
1588 | end = field->n_bits - 1; | |
1589 | } else { | |
1590 | return xasprintf("%s: syntax error expecting [] or [<bit>] or " | |
1591 | "[<start>..<end>]", *sp); | |
1592 | } | |
1593 | s = strchr(s, ']') + 1; | |
1594 | ||
1595 | if (start > end) { | |
1596 | return xasprintf("%s: starting bit %d is after ending bit %d", | |
1597 | *sp, start, end); | |
1598 | } else if (start >= field->n_bits) { | |
1599 | return xasprintf("%s: starting bit %d is not valid because field is " | |
1600 | "only %d bits wide", *sp, start, field->n_bits); | |
1601 | } else if (end >= field->n_bits){ | |
1602 | return xasprintf("%s: ending bit %d is not valid because field is " | |
1603 | "only %d bits wide", *sp, end, field->n_bits); | |
1604 | } | |
1605 | ||
1606 | sf->field = field; | |
1607 | sf->ofs = start; | |
1608 | sf->n_bits = end - start + 1; | |
1609 | ||
1610 | *sp = s; | |
1611 | return NULL; | |
1612 | } | |
1613 | ||
1614 | /* Parses a subfield from the entirety of 's' into 'sf'. Returns NULL if | |
1615 | * successful, otherwise a malloc()'d string describing the error. The caller | |
1616 | * is responsible for freeing the returned string. | |
1617 | * | |
1618 | * The syntax parsed from 's' takes the form "header[start..end]" where | |
1619 | * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive) | |
1620 | * bit indexes. "..end" may be omitted to indicate a single bit. "start..end" | |
1621 | * may both be omitted (the [] are still required) to indicate an entire | |
1622 | * field. */ | |
1623 | char * WARN_UNUSED_RESULT | |
1624 | mf_parse_subfield(struct mf_subfield *sf, const char *s) | |
1625 | { | |
1626 | char *error = mf_parse_subfield__(sf, &s); | |
1627 | if (!error && s[0]) { | |
1628 | error = xstrdup("unexpected input following field syntax"); | |
1629 | } | |
1630 | return error; | |
1631 | } | |
1632 | \f | |
1633 | /* Returns an bitmap in which each bit corresponds to the like-numbered field | |
1634 | * in the OFPXMC12_OPENFLOW_BASIC OXM class, in which the bit values are taken | |
1635 | * from the 'fields' bitmap. Only fields defined in OpenFlow 'version' are | |
1636 | * considered. | |
1637 | * | |
1638 | * This is useful for encoding OpenFlow 1.2 table stats messages. */ | |
1639 | ovs_be64 | |
1640 | oxm_bitmap_from_mf_bitmap(const struct mf_bitmap *fields, | |
1641 | enum ofp_version version) | |
1642 | { | |
1643 | uint64_t oxm_bitmap = 0; | |
1644 | int i; | |
1645 | ||
1646 | BITMAP_FOR_EACH_1 (i, MFF_N_IDS, fields->bm) { | |
508a9338 | 1647 | uint64_t oxm = mf_oxm_header(i, version); |
c8058af7 | 1648 | uint32_t class = nxm_class(oxm); |
178742f9 BP |
1649 | int field = nxm_field(oxm); |
1650 | ||
c8058af7 | 1651 | if (class == OFPXMC12_OPENFLOW_BASIC && field < 64) { |
178742f9 BP |
1652 | oxm_bitmap |= UINT64_C(1) << field; |
1653 | } | |
1654 | } | |
1655 | return htonll(oxm_bitmap); | |
1656 | } | |
1657 | ||
1658 | /* Opposite conversion from oxm_bitmap_from_mf_bitmap(). | |
1659 | * | |
1660 | * This is useful for decoding OpenFlow 1.2 table stats messages. */ | |
1661 | struct mf_bitmap | |
1662 | oxm_bitmap_to_mf_bitmap(ovs_be64 oxm_bitmap, enum ofp_version version) | |
1663 | { | |
1664 | struct mf_bitmap fields = MF_BITMAP_INITIALIZER; | |
1665 | ||
1666 | for (enum mf_field_id id = 0; id < MFF_N_IDS; id++) { | |
e6556fe3 BP |
1667 | uint64_t oxm = mf_oxm_header(id, version); |
1668 | if (oxm && version >= nxm_field_by_header(oxm)->version) { | |
c8058af7 | 1669 | uint32_t class = nxm_class(oxm); |
178742f9 BP |
1670 | int field = nxm_field(oxm); |
1671 | ||
c8058af7 | 1672 | if (class == OFPXMC12_OPENFLOW_BASIC |
178742f9 BP |
1673 | && field < 64 |
1674 | && oxm_bitmap & htonll(UINT64_C(1) << field)) { | |
1675 | bitmap_set1(fields.bm, id); | |
1676 | } | |
1677 | } | |
1678 | } | |
1679 | return fields; | |
1680 | } | |
1681 | ||
1682 | /* Returns a bitmap of fields that can be encoded in OXM and that can be | |
1683 | * modified with a "set_field" action. */ | |
1684 | struct mf_bitmap | |
1685 | oxm_writable_fields(void) | |
1686 | { | |
1687 | struct mf_bitmap b = MF_BITMAP_INITIALIZER; | |
1688 | int i; | |
1689 | ||
1690 | for (i = 0; i < MFF_N_IDS; i++) { | |
1691 | if (mf_oxm_header(i, 0) && mf_from_id(i)->writable) { | |
1692 | bitmap_set1(b.bm, i); | |
1693 | } | |
1694 | } | |
1695 | return b; | |
1696 | } | |
1697 | ||
1698 | /* Returns a bitmap of fields that can be encoded in OXM and that can be | |
1699 | * matched in a flow table. */ | |
1700 | struct mf_bitmap | |
1701 | oxm_matchable_fields(void) | |
1702 | { | |
1703 | struct mf_bitmap b = MF_BITMAP_INITIALIZER; | |
1704 | int i; | |
1705 | ||
1706 | for (i = 0; i < MFF_N_IDS; i++) { | |
1707 | if (mf_oxm_header(i, 0)) { | |
1708 | bitmap_set1(b.bm, i); | |
1709 | } | |
1710 | } | |
1711 | return b; | |
1712 | } | |
1713 | ||
1714 | /* Returns a bitmap of fields that can be encoded in OXM and that can be | |
1715 | * matched in a flow table with an arbitrary bitmask. */ | |
1716 | struct mf_bitmap | |
1717 | oxm_maskable_fields(void) | |
1718 | { | |
1719 | struct mf_bitmap b = MF_BITMAP_INITIALIZER; | |
1720 | int i; | |
1721 | ||
1722 | for (i = 0; i < MFF_N_IDS; i++) { | |
1723 | if (mf_oxm_header(i, 0) && mf_from_id(i)->maskable == MFM_FULLY) { | |
1724 | bitmap_set1(b.bm, i); | |
1725 | } | |
1726 | } | |
1727 | return b; | |
1728 | } | |
1729 | \f | |
1730 | struct nxm_field_index { | |
e6556fe3 BP |
1731 | struct hmap_node header_node; /* In nxm_header_map. */ |
1732 | struct hmap_node name_node; /* In nxm_name_map. */ | |
1733 | struct list mf_node; /* In mf_mf_map[nf.id]. */ | |
1734 | const struct nxm_field nf; | |
178742f9 BP |
1735 | }; |
1736 | ||
1737 | #include "nx-match.inc" | |
1738 | ||
1739 | static struct hmap nxm_header_map; | |
1740 | static struct hmap nxm_name_map; | |
e6556fe3 | 1741 | static struct list nxm_mf_map[MFF_N_IDS]; |
178742f9 BP |
1742 | |
1743 | static void | |
1744 | nxm_init(void) | |
1745 | { | |
1746 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; | |
1747 | if (ovsthread_once_start(&once)) { | |
1748 | hmap_init(&nxm_header_map); | |
1749 | hmap_init(&nxm_name_map); | |
e6556fe3 BP |
1750 | for (int i = 0; i < MFF_N_IDS; i++) { |
1751 | list_init(&nxm_mf_map[i]); | |
1752 | } | |
178742f9 BP |
1753 | for (struct nxm_field_index *nfi = all_nxm_fields; |
1754 | nfi < &all_nxm_fields[ARRAY_SIZE(all_nxm_fields)]; nfi++) { | |
1755 | hmap_insert(&nxm_header_map, &nfi->header_node, | |
1756 | hash_int(nfi->nf.header, 0)); | |
1757 | hmap_insert(&nxm_name_map, &nfi->name_node, | |
1758 | hash_string(nfi->nf.name, 0)); | |
e6556fe3 | 1759 | list_push_back(&nxm_mf_map[nfi->nf.id], &nfi->mf_node); |
178742f9 BP |
1760 | } |
1761 | ovsthread_once_done(&once); | |
1762 | } | |
1763 | } | |
1764 | ||
1765 | static const struct nxm_field * | |
508a9338 | 1766 | nxm_field_by_header(uint64_t header) |
178742f9 BP |
1767 | { |
1768 | const struct nxm_field_index *nfi; | |
1769 | ||
1770 | nxm_init(); | |
1771 | if (nxm_hasmask(header)) { | |
1772 | header = nxm_make_exact_header(header); | |
1773 | } | |
1774 | ||
1775 | HMAP_FOR_EACH_IN_BUCKET (nfi, header_node, hash_int(header, 0), | |
1776 | &nxm_header_map) { | |
1777 | if (header == nfi->nf.header) { | |
1778 | return &nfi->nf; | |
1779 | } | |
1780 | } | |
1781 | return NULL; | |
1782 | } | |
1783 | ||
1784 | static const struct nxm_field * | |
1785 | nxm_field_by_name(const char *name, size_t len) | |
1786 | { | |
1787 | const struct nxm_field_index *nfi; | |
1788 | ||
1789 | nxm_init(); | |
1790 | HMAP_FOR_EACH_WITH_HASH (nfi, name_node, hash_bytes(name, len, 0), | |
1791 | &nxm_name_map) { | |
1792 | if (strlen(nfi->nf.name) == len && !memcmp(nfi->nf.name, name, len)) { | |
1793 | return &nfi->nf; | |
1794 | } | |
1795 | } | |
1796 | return NULL; | |
1797 | } | |
1798 | ||
1799 | static const struct nxm_field * | |
e6556fe3 | 1800 | nxm_field_by_mf_id(enum mf_field_id id, enum ofp_version version) |
178742f9 | 1801 | { |
e6556fe3 BP |
1802 | const struct nxm_field_index *nfi; |
1803 | const struct nxm_field *f; | |
178742f9 | 1804 | |
178742f9 | 1805 | nxm_init(); |
178742f9 | 1806 | |
e6556fe3 BP |
1807 | f = NULL; |
1808 | LIST_FOR_EACH (nfi, mf_node, &nxm_mf_map[id]) { | |
1809 | if (!f || version >= nfi->nf.version) { | |
1810 | f = &nfi->nf; | |
1811 | } | |
1812 | } | |
1813 | return f; | |
1814 | } |