]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ofp-actions.c
dpif-netdev: Fix typo in copyright header.
[mirror_ovs.git] / lib / ofp-actions.c
1 /*
2 * Copyright (c) 2008-2017, 2019-2020 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18
19 #include <sys/types.h>
20 #include <netinet/in.h>
21
22 #include "bundle.h"
23 #include "byte-order.h"
24 #include "colors.h"
25 #include "compiler.h"
26 #include "dummy.h"
27 #include "openvswitch/hmap.h"
28 #include "learn.h"
29 #include "multipath.h"
30 #include "nx-match.h"
31 #include "odp-netlink.h"
32 #include "openvswitch/dynamic-string.h"
33 #include "openvswitch/meta-flow.h"
34 #include "openvswitch/ofp-actions.h"
35 #include "openvswitch/ofp-packet.h"
36 #include "openvswitch/ofp-parse.h"
37 #include "openvswitch/ofp-port.h"
38 #include "openvswitch/ofp-prop.h"
39 #include "openvswitch/ofp-table.h"
40 #include "openvswitch/ofpbuf.h"
41 #include "openvswitch/vlog.h"
42 #include "unaligned.h"
43 #include "util.h"
44 #include "vl-mff-map.h"
45
46 VLOG_DEFINE_THIS_MODULE(ofp_actions);
47
48 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
49
50 struct ofp_action_header;
51
52 /* Header for Open vSwitch and ONF vendor extension actions.
53 *
54 * This is the entire header for a few Open vSwitch vendor extension actions,
55 * the ones that either have no arguments or for which variable-length
56 * arguments follow the header.
57 *
58 * This cannot be used as an entirely generic vendor extension action header,
59 * because OpenFlow does not specify the location or size of the action
60 * subtype; it just happens that ONF extensions and Nicira extensions share
61 * this format. */
62 struct ext_action_header {
63 ovs_be16 type; /* OFPAT_VENDOR. */
64 ovs_be16 len; /* At least 16. */
65 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
66 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
67 uint8_t pad[6];
68 };
69 OFP_ASSERT(sizeof(struct ext_action_header) == 16);
70
71 /* Raw identifiers for OpenFlow actions.
72 *
73 * Decoding and encoding OpenFlow actions across multiple versions is difficult
74 * to do in a clean, consistent way. This enumeration lays out all of the
75 * forms of actions that Open vSwitch supports.
76 *
77 * The comments here must follow a stylized form because the
78 * "extract-ofp-actions" program parses them at build time to generate data
79 * tables.
80 *
81 * - The first part of each comment specifies the vendor, OpenFlow versions,
82 * and type for each protocol that supports the action:
83 *
84 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
85 * extension actions. (Support for other vendors can be added, but
86 * it can't be done just based on a vendor ID definition alone
87 * because OpenFlow doesn't define a standard way to specify a
88 * subtype for vendor actions, so other vendors might do it different
89 * from Nicira.)
90 *
91 * # The version can specify a specific OpenFlow version, a version
92 * range delimited by "-", or an open-ended range with "+".
93 *
94 * # The type, in parentheses, is the action type number (for standard
95 * OpenFlow actions) or subtype (for vendor extension actions).
96 *
97 * # Optionally one may add "is deprecated" followed by a
98 * human-readable reason in parentheses (which will be used in log
99 * messages), if a particular action should no longer be used.
100 *
101 * Multiple such specifications may be separated by commas.
102 *
103 * - The second part describes the action's wire format. It may be:
104 *
105 * # "struct <name>": The struct fully specifies the wire format. The
106 * action is exactly the size of the struct. (Thus, the struct must
107 * be an exact multiple of 8 bytes in size.)
108 *
109 * # "struct <name>, ...": The struct specifies the beginning of the
110 * wire format. An instance of the action is either the struct's
111 * exact size, or a multiple of 8 bytes longer.
112 *
113 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
114 * vendor extension) header, followed by 0 or more pad bytes to align
115 * to a multiple of <N> bits, followed by an argument of the given
116 * type, followed by 0 or more pad bytes to bring the total action up
117 * to a multiple of 8 bytes.
118 *
119 * # "void": The action is just a (standard or vendor extension)
120 * header.
121 *
122 * # Optionally, one may add "VLMFF" in the end of the second part if
123 * the Openflow action may use a variable length meta-flow field
124 * (i.e. tun_metadata). Adding "VLMFF" will pass the per-switch based
125 * variable length meta-flow field mapping map (struct vl_mff_map) to
126 * the corresponding action decoding function.
127 *
128 * - Optional additional text enclosed in square brackets is commentary for
129 * the human reader.
130 */
131 enum ofp_raw_action_type {
132 /* ## ----------------- ## */
133 /* ## Standard actions. ## */
134 /* ## ----------------- ## */
135
136 /* OF1.0(0): struct ofp10_action_output. */
137 OFPAT_RAW10_OUTPUT,
138 /* OF1.1+(0): struct ofp11_action_output. */
139 OFPAT_RAW11_OUTPUT,
140
141 /* OF1.0(1): uint16_t. */
142 OFPAT_RAW10_SET_VLAN_VID,
143 /* OF1.0(2): uint8_t. */
144 OFPAT_RAW10_SET_VLAN_PCP,
145
146 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
147 *
148 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
149 * modification actions: the 1.0 versions push a VLAN header if none is
150 * present, but the 1.1 versions do not. That is the only reason that we
151 * distinguish their raw action types.] */
152 OFPAT_RAW11_SET_VLAN_VID,
153 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
154 OFPAT_RAW11_SET_VLAN_PCP,
155
156 /* OF1.1+(17): ovs_be16.
157 *
158 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
159 * TCI.] */
160 OFPAT_RAW11_PUSH_VLAN,
161
162 /* OF1.0(3): void. */
163 OFPAT_RAW10_STRIP_VLAN,
164 /* OF1.1+(18): void. */
165 OFPAT_RAW11_POP_VLAN,
166
167 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
168 * ofp_action_dl_addr. */
169 OFPAT_RAW_SET_DL_SRC,
170
171 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
172 * ofp_action_dl_addr. */
173 OFPAT_RAW_SET_DL_DST,
174
175 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
176 * ovs_be32. */
177 OFPAT_RAW_SET_NW_SRC,
178
179 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
180 * ovs_be32. */
181 OFPAT_RAW_SET_NW_DST,
182
183 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
184 OFPAT_RAW_SET_NW_TOS,
185
186 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
187 OFPAT_RAW11_SET_NW_ECN,
188
189 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
190 * ovs_be16. */
191 OFPAT_RAW_SET_TP_SRC,
192
193 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
194 * ovs_be16. */
195 OFPAT_RAW_SET_TP_DST,
196
197 /* OF1.0(11): struct ofp10_action_enqueue. */
198 OFPAT_RAW10_ENQUEUE,
199
200 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
201 * ovs_be32. */
202 OFPAT_RAW_SET_MPLS_LABEL,
203
204 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
205 * uint8_t. */
206 OFPAT_RAW_SET_MPLS_TC,
207
208 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
209 * uint8_t. */
210 OFPAT_RAW_SET_MPLS_TTL,
211
212 /* NX1.0(26), OF1.1+(16): void. */
213 OFPAT_RAW_DEC_MPLS_TTL,
214
215 /* NX1.0(23), OF1.1+(19): ovs_be16.
216 *
217 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
218 OFPAT_RAW_PUSH_MPLS,
219
220 /* NX1.0(24), OF1.1+(20): ovs_be16.
221 *
222 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
223 * ETH_TYPE_MPLS otherwise, not the label.] */
224 OFPAT_RAW_POP_MPLS,
225
226 /* NX1.0(4), OF1.1+(21): uint32_t. */
227 OFPAT_RAW_SET_QUEUE,
228
229 /* NX1.0(40), OF1.1+(22): uint32_t. */
230 OFPAT_RAW_GROUP,
231
232 /* OF1.1+(23): uint8_t. */
233 OFPAT_RAW11_SET_NW_TTL,
234
235 /* NX1.0(18), OF1.1+(24): void. */
236 OFPAT_RAW_DEC_NW_TTL,
237 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
238 NXAST_RAW_DEC_TTL_CNT_IDS,
239
240 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... VLMFF */
241 OFPAT_RAW12_SET_FIELD,
242 /* OF1.5+(25): struct ofp12_action_set_field, ... VLMFF */
243 OFPAT_RAW15_SET_FIELD,
244 /* NX1.0-1.4(7): struct nx_action_reg_load. VLMFF
245 *
246 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
247 * we drop reg_load.] */
248 NXAST_RAW_REG_LOAD,
249 /* NX1.0-1.4(33): struct ext_action_header, ... VLMFF
250 *
251 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
252 * we drop reg_load2.] */
253 NXAST_RAW_REG_LOAD2,
254
255 /* OF1.5+(28): struct ofp15_action_copy_field, ... VLMFF */
256 OFPAT_RAW15_COPY_FIELD,
257 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... VLMFF */
258 ONFACT_RAW13_COPY_FIELD,
259 /* NX1.0-1.4(6): struct nx_action_reg_move, ... VLMFF */
260 NXAST_RAW_REG_MOVE,
261
262 /* OF1.5+(29): uint32_t. */
263 OFPAT_RAW15_METER,
264
265 /* ## ------------------------- ## */
266 /* ## Nicira extension actions. ## */
267 /* ## ------------------------- ## */
268
269 /* Actions similar to standard actions are listed with the standard actions. */
270
271 /* NX1.0+(1): uint16_t. */
272 NXAST_RAW_RESUBMIT,
273 /* NX1.0+(14): struct nx_action_resubmit. */
274 NXAST_RAW_RESUBMIT_TABLE,
275 /* NX1.0+(44): struct nx_action_resubmit. */
276 NXAST_RAW_RESUBMIT_TABLE_CT,
277
278 /* NX1.0+(2): uint32_t. */
279 NXAST_RAW_SET_TUNNEL,
280 /* NX1.0+(9): uint64_t. */
281 NXAST_RAW_SET_TUNNEL64,
282
283 /* NX1.0+(5): void. */
284 NXAST_RAW_POP_QUEUE,
285
286 /* NX1.0+(8): struct nx_action_note, ... */
287 NXAST_RAW_NOTE,
288
289 /* NX1.0+(10): struct nx_action_multipath. VLMFF */
290 NXAST_RAW_MULTIPATH,
291
292 /* NX1.0+(12): struct nx_action_bundle, ... */
293 NXAST_RAW_BUNDLE,
294 /* NX1.0+(13): struct nx_action_bundle, ... VLMFF */
295 NXAST_RAW_BUNDLE_LOAD,
296
297 /* NX1.0+(15): struct nx_action_output_reg. VLMFF */
298 NXAST_RAW_OUTPUT_REG,
299 /* NX1.0+(32): struct nx_action_output_reg2. VLMFF */
300 NXAST_RAW_OUTPUT_REG2,
301
302 /* NX1.0+(16): struct nx_action_learn, ... VLMFF */
303 NXAST_RAW_LEARN,
304 /* NX1.0+(45): struct nx_action_learn2, ... VLMFF */
305 NXAST_RAW_LEARN2,
306
307 /* NX1.0+(17): void. */
308 NXAST_RAW_EXIT,
309
310 /* NX1.0+(19): struct nx_action_fin_timeout. */
311 NXAST_RAW_FIN_TIMEOUT,
312
313 /* NX1.0+(20): struct nx_action_controller. */
314 NXAST_RAW_CONTROLLER,
315 /* NX1.0+(37): struct ext_action_header, ... */
316 NXAST_RAW_CONTROLLER2,
317
318 /* NX1.0+(22): struct nx_action_write_metadata. */
319 NXAST_RAW_WRITE_METADATA,
320
321 /* NX1.0+(27): struct nx_action_stack. VLMFF */
322 NXAST_RAW_STACK_PUSH,
323
324 /* NX1.0+(28): struct nx_action_stack. VLMFF */
325 NXAST_RAW_STACK_POP,
326
327 /* NX1.0+(29): struct nx_action_sample. */
328 NXAST_RAW_SAMPLE,
329 /* NX1.0+(38): struct nx_action_sample2. */
330 NXAST_RAW_SAMPLE2,
331 /* NX1.0+(41): struct nx_action_sample2. */
332 NXAST_RAW_SAMPLE3,
333
334 /* NX1.0+(34): struct nx_action_conjunction. */
335 NXAST_RAW_CONJUNCTION,
336
337 /* NX1.0+(35): struct nx_action_conntrack, ... VLMFF */
338 NXAST_RAW_CT,
339
340 /* NX1.0+(36): struct nx_action_nat, ... */
341 NXAST_RAW_NAT,
342
343 /* NX1.0+(39): struct nx_action_output_trunc. */
344 NXAST_RAW_OUTPUT_TRUNC,
345
346 /* NX1.0+(42): struct ext_action_header, ... VLMFF */
347 NXAST_RAW_CLONE,
348
349 /* NX1.0+(43): void. */
350 NXAST_RAW_CT_CLEAR,
351
352 /* NX1.3+(46): struct nx_action_encap, ... */
353 NXAST_RAW_ENCAP,
354
355 /* NX1.3+(47): struct nx_action_decap, ... */
356 NXAST_RAW_DECAP,
357
358 /* NX1.3+(48): void. */
359 NXAST_RAW_DEC_NSH_TTL,
360
361 /* NX1.0+(49): struct nx_action_check_pkt_larger, ... VLMFF */
362 NXAST_RAW_CHECK_PKT_LARGER,
363
364 /* NX1.0+(50): struct nx_action_delete_field. VLMFF */
365 NXAST_RAW_DELETE_FIELD,
366
367 /* ## ------------------ ## */
368 /* ## Debugging actions. ## */
369 /* ## ------------------ ## */
370
371 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
372 /* accepts them only if started with --enable-dummy. */
373
374 /* NX1.0+(254): void. */
375 NXAST_RAW_DEBUG_SLOW,
376
377 /* NX1.0+(255): void. */
378 NXAST_RAW_DEBUG_RECIRC,
379 };
380
381 /* OpenFlow actions are always a multiple of 8 bytes in length. */
382 #define OFP_ACTION_ALIGN 8
383
384 /* Define a few functions for working with instructions. */
385 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
386 static inline const struct STRUCT * OVS_UNUSED \
387 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
388 { \
389 ovs_assert(inst->type == htons(ENUM)); \
390 return ALIGNED_CAST(struct STRUCT *, inst); \
391 } \
392 \
393 static inline void OVS_UNUSED \
394 instruction_init_##ENUM(struct STRUCT *s) \
395 { \
396 memset(s, 0, sizeof *s); \
397 s->type = htons(ENUM); \
398 s->len = htons(sizeof *s); \
399 } \
400 \
401 static inline struct STRUCT * OVS_UNUSED \
402 instruction_put_##ENUM(struct ofpbuf *buf) \
403 { \
404 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
405 instruction_init_##ENUM(s); \
406 return s; \
407 }
408 OVS_INSTRUCTIONS
409 #undef DEFINE_INST
410
411 static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
412 size_t ofs);
413 static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
414
415 static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
416 enum ofp_version, uint32_t allowed_ovsinsts,
417 enum ofpact_type outer_action,
418 char **errorp);
419
420 static void put_set_field(struct ofpbuf *openflow, enum ofp_version,
421 enum mf_field_id, uint64_t value);
422
423 static void put_reg_load(struct ofpbuf *openflow,
424 const struct mf_subfield *, uint64_t value);
425
426 static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
427 enum ofp_raw_action_type *, uint64_t *arg);
428 static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
429 enum ofp_raw_action_type, uint64_t arg);
430
431 static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
432 char *str, const struct ofpact_parse_params *pp,
433 bool allow_instructions, enum ofpact_type outer_action);
434 static enum ofperr ofpacts_pull_openflow_actions__(
435 struct ofpbuf *openflow, unsigned int actions_len,
436 enum ofp_version version, uint32_t allowed_ovsinsts,
437 struct ofpbuf *ofpacts, enum ofpact_type outer_action,
438 const struct vl_mff_map *vl_mff_map, uint64_t *ofpacts_tlv_bitmap);
439 static char * OVS_WARN_UNUSED_RESULT ofpacts_parse_copy(
440 const char *s_, const struct ofpact_parse_params *pp,
441 bool allow_instructions, enum ofpact_type outer_action);
442
443 static void inconsistent_match(enum ofputil_protocol *usable_protocols);
444
445 /* Returns the ofpact following 'ofpact', except that if 'ofpact' contains
446 * nested ofpacts it returns the first one. */
447 struct ofpact *
448 ofpact_next_flattened(const struct ofpact *ofpact)
449 {
450 switch (ofpact->type) {
451 case OFPACT_OUTPUT:
452 case OFPACT_GROUP:
453 case OFPACT_CONTROLLER:
454 case OFPACT_ENQUEUE:
455 case OFPACT_OUTPUT_REG:
456 case OFPACT_OUTPUT_TRUNC:
457 case OFPACT_BUNDLE:
458 case OFPACT_SET_FIELD:
459 case OFPACT_SET_VLAN_VID:
460 case OFPACT_SET_VLAN_PCP:
461 case OFPACT_STRIP_VLAN:
462 case OFPACT_PUSH_VLAN:
463 case OFPACT_SET_ETH_SRC:
464 case OFPACT_SET_ETH_DST:
465 case OFPACT_SET_IPV4_SRC:
466 case OFPACT_SET_IPV4_DST:
467 case OFPACT_SET_IP_DSCP:
468 case OFPACT_SET_IP_ECN:
469 case OFPACT_SET_IP_TTL:
470 case OFPACT_SET_L4_SRC_PORT:
471 case OFPACT_SET_L4_DST_PORT:
472 case OFPACT_REG_MOVE:
473 case OFPACT_STACK_PUSH:
474 case OFPACT_STACK_POP:
475 case OFPACT_DEC_TTL:
476 case OFPACT_SET_MPLS_LABEL:
477 case OFPACT_SET_MPLS_TC:
478 case OFPACT_SET_MPLS_TTL:
479 case OFPACT_DEC_MPLS_TTL:
480 case OFPACT_PUSH_MPLS:
481 case OFPACT_POP_MPLS:
482 case OFPACT_SET_TUNNEL:
483 case OFPACT_SET_QUEUE:
484 case OFPACT_POP_QUEUE:
485 case OFPACT_FIN_TIMEOUT:
486 case OFPACT_RESUBMIT:
487 case OFPACT_LEARN:
488 case OFPACT_CONJUNCTION:
489 case OFPACT_MULTIPATH:
490 case OFPACT_NOTE:
491 case OFPACT_EXIT:
492 case OFPACT_SAMPLE:
493 case OFPACT_UNROLL_XLATE:
494 case OFPACT_CT_CLEAR:
495 case OFPACT_DEBUG_RECIRC:
496 case OFPACT_DEBUG_SLOW:
497 case OFPACT_METER:
498 case OFPACT_CLEAR_ACTIONS:
499 case OFPACT_WRITE_METADATA:
500 case OFPACT_GOTO_TABLE:
501 case OFPACT_NAT:
502 case OFPACT_ENCAP:
503 case OFPACT_DECAP:
504 case OFPACT_DEC_NSH_TTL:
505 case OFPACT_CHECK_PKT_LARGER:
506 case OFPACT_DELETE_FIELD:
507 return ofpact_next(ofpact);
508
509 case OFPACT_CLONE:
510 return ofpact_get_CLONE(ofpact)->actions;
511
512 case OFPACT_CT:
513 return ofpact_get_CT(ofpact)->actions;
514
515 case OFPACT_WRITE_ACTIONS:
516 return ofpact_get_WRITE_ACTIONS(ofpact)->actions;
517 }
518
519 OVS_NOT_REACHED();
520 }
521
522 /* Pull off existing actions or instructions. Used by nesting actions to keep
523 * ofpacts_parse() oblivious of actions nesting.
524 *
525 * Push the actions back on after nested parsing, e.g.:
526 *
527 * size_t ofs = ofpacts_pull(ofpacts);
528 * ...nested parsing...
529 * ofpbuf_push_uninit(ofpacts, ofs);
530 */
531 static size_t
532 ofpacts_pull(struct ofpbuf *ofpacts)
533 {
534 size_t ofs;
535
536 ofs = ofpacts->size;
537 ofpbuf_pull(ofpacts, ofs);
538
539 return ofs;
540 }
541
542 #include "ofp-actions.inc1"
543 \f
544 /* Output actions. */
545
546 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
547 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
548 * number of bytes to send. A 'max_len' of zero means no bytes of the
549 * packet should be sent. */
550 struct ofp10_action_output {
551 ovs_be16 type; /* OFPAT10_OUTPUT. */
552 ovs_be16 len; /* Length is 8. */
553 ovs_be16 port; /* Output port. */
554 ovs_be16 max_len; /* Max length to send to controller. */
555 };
556 OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
557
558 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
559 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
560 * number of bytes to send. A 'max_len' of zero means no bytes of the
561 * packet should be sent.*/
562 struct ofp11_action_output {
563 ovs_be16 type; /* OFPAT11_OUTPUT. */
564 ovs_be16 len; /* Length is 16. */
565 ovs_be32 port; /* Output port. */
566 ovs_be16 max_len; /* Max length to send to controller. */
567 uint8_t pad[6]; /* Pad to 64 bits. */
568 };
569 OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
570
571 static enum ofperr
572 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
573 enum ofp_version ofp_version OVS_UNUSED,
574 struct ofpbuf *out)
575 {
576 struct ofpact_output *output;
577
578 output = ofpact_put_OUTPUT(out);
579 output->port = u16_to_ofp(ntohs(oao->port));
580 output->max_len = ntohs(oao->max_len);
581
582 return ofpact_check_output_port(output->port, OFPP_MAX);
583 }
584
585 static enum ofperr
586 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
587 enum ofp_version ofp_version OVS_UNUSED,
588 struct ofpbuf *out)
589 {
590 struct ofpact_output *output;
591 enum ofperr error;
592
593 output = ofpact_put_OUTPUT(out);
594 output->max_len = ntohs(oao->max_len);
595
596 error = ofputil_port_from_ofp11(oao->port, &output->port);
597 if (error) {
598 return error;
599 }
600
601 return ofpact_check_output_port(output->port, OFPP_MAX);
602 }
603
604 static void
605 encode_OUTPUT(const struct ofpact_output *output,
606 enum ofp_version ofp_version, struct ofpbuf *out)
607 {
608 if (ofp_version == OFP10_VERSION) {
609 struct ofp10_action_output *oao;
610
611 oao = put_OFPAT10_OUTPUT(out);
612 oao->port = htons(ofp_to_u16(output->port));
613 oao->max_len = htons(output->max_len);
614 } else {
615 struct ofp11_action_output *oao;
616
617 oao = put_OFPAT11_OUTPUT(out);
618 oao->port = ofputil_port_to_ofp11(output->port);
619 oao->max_len = htons(output->max_len);
620 }
621 }
622
623 static char * OVS_WARN_UNUSED_RESULT
624 parse_truncate_subfield(const char *arg_,
625 const struct ofpact_parse_params *pp,
626 struct ofpact_output_trunc *output_trunc)
627 {
628 char *key, *value;
629 char *arg = CONST_CAST(char *, arg_);
630
631 while (ofputil_parse_key_value(&arg, &key, &value)) {
632 if (!strcmp(key, "port")) {
633 if (!ofputil_port_from_string(value, pp->port_map,
634 &output_trunc->port)) {
635 return xasprintf("output to unknown truncate port: %s",
636 value);
637 }
638 if (ofp_to_u16(output_trunc->port) > ofp_to_u16(OFPP_MAX)) {
639 if (output_trunc->port != OFPP_LOCAL &&
640 output_trunc->port != OFPP_IN_PORT)
641 return xasprintf("output to unsupported truncate port: %s",
642 value);
643 }
644 } else if (!strcmp(key, "max_len")) {
645 char *err;
646
647 err = str_to_u32(value, &output_trunc->max_len);
648 if (err) {
649 return err;
650 }
651
652 if (output_trunc->max_len < ETH_HEADER_LEN) {
653 return xasprintf("max_len %"PRIu32" is less than the minimum "
654 "value %d",
655 output_trunc->max_len, ETH_HEADER_LEN);
656 }
657 } else {
658 return xasprintf("invalid key '%s' in output_trunc argument",
659 key);
660 }
661 }
662 return NULL;
663 }
664
665 static char * OVS_WARN_UNUSED_RESULT
666 parse_OUTPUT(const char *arg, const struct ofpact_parse_params *pp)
667 {
668 if (strstr(arg, "port") && strstr(arg, "max_len")) {
669 struct ofpact_output_trunc *output_trunc;
670
671 output_trunc = ofpact_put_OUTPUT_TRUNC(pp->ofpacts);
672 return parse_truncate_subfield(arg, pp, output_trunc);
673 }
674
675 ofp_port_t port;
676 if (ofputil_port_from_string(arg, pp->port_map, &port)) {
677 struct ofpact_output *output = ofpact_put_OUTPUT(pp->ofpacts);
678 output->port = port;
679 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
680 return NULL;
681 }
682
683 struct mf_subfield src;
684 char *error = mf_parse_subfield(&src, arg);
685 if (!error) {
686 struct ofpact_output_reg *output_reg;
687
688 output_reg = ofpact_put_OUTPUT_REG(pp->ofpacts);
689 output_reg->max_len = UINT16_MAX;
690 output_reg->src = src;
691 return NULL;
692 }
693 free(error);
694
695 return xasprintf("%s: output to unknown port", arg);
696 }
697
698 static void
699 format_OUTPUT(const struct ofpact_output *a,
700 const struct ofpact_format_params *fp)
701 {
702 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
703 ds_put_format(fp->s, "%soutput:%s", colors.special, colors.end);
704 }
705 ofputil_format_port(a->port, fp->port_map, fp->s);
706 if (a->port == OFPP_CONTROLLER) {
707 ds_put_format(fp->s, ":%"PRIu16, a->max_len);
708 }
709 }
710
711 static enum ofperr
712 check_OUTPUT(const struct ofpact_output *a,
713 const struct ofpact_check_params *cp)
714 {
715 return ofpact_check_output_port(a->port, cp->max_ports);
716 }
717 \f
718 /* Group actions. */
719
720 static enum ofperr
721 decode_OFPAT_RAW_GROUP(uint32_t group_id,
722 enum ofp_version ofp_version OVS_UNUSED,
723 struct ofpbuf *out)
724 {
725 ofpact_put_GROUP(out)->group_id = group_id;
726 return 0;
727 }
728
729 static void
730 encode_GROUP(const struct ofpact_group *group,
731 enum ofp_version ofp_version, struct ofpbuf *out)
732 {
733 put_OFPAT_GROUP(out, ofp_version, group->group_id);
734 }
735
736 static char * OVS_WARN_UNUSED_RESULT
737 parse_GROUP(char *arg, const struct ofpact_parse_params *pp)
738 {
739 return str_to_u32(arg, &ofpact_put_GROUP(pp->ofpacts)->group_id);
740 }
741
742 static void
743 format_GROUP(const struct ofpact_group *a,
744 const struct ofpact_format_params *fp)
745 {
746 ds_put_format(fp->s, "%sgroup:%s%"PRIu32,
747 colors.special, colors.end, a->group_id);
748 }
749
750 static enum ofperr
751 check_GROUP(const struct ofpact_group *a OVS_UNUSED,
752 const struct ofpact_check_params *cp OVS_UNUSED)
753 {
754 return 0;
755 }
756 \f
757 /* Action structure for NXAST_CONTROLLER.
758 *
759 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
760 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
761 * specifying:
762 *
763 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
764 *
765 * - 'controller_id': The ID of the controller connection to which the
766 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
767 * sent only to controllers that have the specified controller connection
768 * ID. See "struct nx_controller_id" for more information. */
769 struct nx_action_controller {
770 ovs_be16 type; /* OFPAT_VENDOR. */
771 ovs_be16 len; /* Length is 16. */
772 ovs_be32 vendor; /* NX_VENDOR_ID. */
773 ovs_be16 subtype; /* NXAST_CONTROLLER. */
774 ovs_be16 max_len; /* Maximum length to send to controller. */
775 ovs_be16 controller_id; /* Controller ID to send packet-in. */
776 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
777 uint8_t zero; /* Must be zero. */
778 };
779 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
780
781 /* Properties for NXAST_CONTROLLER2.
782 *
783 * For more information on the effect of NXAC2PT_PAUSE, see the large comment
784 * on NXT_PACKET_IN2 in nicira-ext.h */
785 enum nx_action_controller2_prop_type {
786 NXAC2PT_MAX_LEN, /* ovs_be16 max bytes to send (default all). */
787 NXAC2PT_CONTROLLER_ID, /* ovs_be16 dest controller ID (default 0). */
788 NXAC2PT_REASON, /* uint8_t reason (OFPR_*), default 0. */
789 NXAC2PT_USERDATA, /* Data to copy into NXPINT_USERDATA. */
790 NXAC2PT_PAUSE, /* Flag to pause pipeline to resume later. */
791 NXAC2PT_METER_ID, /* ovs_b32 meter (default NX_CTLR_NO_METER). */
792 };
793
794 /* The action structure for NXAST_CONTROLLER2 is "struct ext_action_header",
795 * followed by NXAC2PT_* properties. */
796
797 static enum ofperr
798 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
799 enum ofp_version ofp_version OVS_UNUSED,
800 struct ofpbuf *out)
801 {
802 struct ofpact_controller *oc;
803
804 oc = ofpact_put_CONTROLLER(out);
805 oc->ofpact.raw = NXAST_RAW_CONTROLLER;
806 oc->max_len = ntohs(nac->max_len);
807 oc->controller_id = ntohs(nac->controller_id);
808 oc->reason = nac->reason;
809 oc->meter_id = NX_CTLR_NO_METER;
810 ofpact_finish_CONTROLLER(out, &oc);
811
812 return 0;
813 }
814
815 static enum ofperr
816 decode_NXAST_RAW_CONTROLLER2(const struct ext_action_header *eah,
817 enum ofp_version ofp_version OVS_UNUSED,
818 struct ofpbuf *out)
819 {
820 if (!is_all_zeros(eah->pad, sizeof eah->pad)) {
821 return OFPERR_NXBRC_MUST_BE_ZERO;
822 }
823
824 size_t start_ofs = out->size;
825 struct ofpact_controller *oc = ofpact_put_CONTROLLER(out);
826 oc->ofpact.raw = NXAST_RAW_CONTROLLER2;
827 oc->max_len = UINT16_MAX;
828 oc->reason = OFPR_ACTION;
829 oc->meter_id = NX_CTLR_NO_METER;
830
831 struct ofpbuf properties;
832 ofpbuf_use_const(&properties, eah, ntohs(eah->len));
833 ofpbuf_pull(&properties, sizeof *eah);
834
835 while (properties.size > 0) {
836 struct ofpbuf payload;
837 uint64_t type;
838
839 enum ofperr error = ofpprop_pull(&properties, &payload, &type);
840 if (error) {
841 return error;
842 }
843
844 switch (type) {
845 case NXAC2PT_MAX_LEN:
846 error = ofpprop_parse_u16(&payload, &oc->max_len);
847 break;
848
849 case NXAC2PT_CONTROLLER_ID:
850 error = ofpprop_parse_u16(&payload, &oc->controller_id);
851 break;
852
853 case NXAC2PT_REASON: {
854 uint8_t u8;
855 error = ofpprop_parse_u8(&payload, &u8);
856 oc->reason = u8;
857 break;
858 }
859
860 case NXAC2PT_USERDATA:
861 out->size = start_ofs + sizeof(struct ofpact_controller);
862 ofpbuf_put(out, payload.msg, ofpbuf_msgsize(&payload));
863 oc = ofpbuf_at_assert(out, start_ofs, sizeof *oc);
864 oc->userdata_len = ofpbuf_msgsize(&payload);
865 break;
866
867 case NXAC2PT_PAUSE:
868 oc->pause = true;
869 break;
870
871 case NXAC2PT_METER_ID:
872 error = ofpprop_parse_u32(&payload, &oc->meter_id);
873 break;
874
875 default:
876 error = OFPPROP_UNKNOWN(false, "NXAST_RAW_CONTROLLER2", type);
877 break;
878 }
879 if (error) {
880 return error;
881 }
882 }
883
884 ofpact_finish_CONTROLLER(out, &oc);
885
886 return 0;
887 }
888
889 static void
890 encode_CONTROLLER(const struct ofpact_controller *controller,
891 enum ofp_version ofp_version OVS_UNUSED,
892 struct ofpbuf *out)
893 {
894 if (controller->userdata_len
895 || controller->pause
896 || controller->meter_id != NX_CTLR_NO_METER
897 || controller->ofpact.raw == NXAST_RAW_CONTROLLER2) {
898 size_t start_ofs = out->size;
899 put_NXAST_CONTROLLER2(out);
900 if (controller->max_len != UINT16_MAX) {
901 ofpprop_put_u16(out, NXAC2PT_MAX_LEN, controller->max_len);
902 }
903 if (controller->controller_id != 0) {
904 ofpprop_put_u16(out, NXAC2PT_CONTROLLER_ID,
905 controller->controller_id);
906 }
907 if (controller->reason != OFPR_ACTION) {
908 ofpprop_put_u8(out, NXAC2PT_REASON, controller->reason);
909 }
910 if (controller->userdata_len != 0) {
911 ofpprop_put(out, NXAC2PT_USERDATA, controller->userdata,
912 controller->userdata_len);
913 }
914 if (controller->pause) {
915 ofpprop_put_flag(out, NXAC2PT_PAUSE);
916 }
917 if (controller->meter_id != NX_CTLR_NO_METER) {
918 ofpprop_put_u32(out, NXAC2PT_METER_ID, controller->meter_id);
919 }
920 pad_ofpat(out, start_ofs);
921 } else {
922 struct nx_action_controller *nac;
923
924 nac = put_NXAST_CONTROLLER(out);
925 nac->max_len = htons(controller->max_len);
926 nac->controller_id = htons(controller->controller_id);
927 nac->reason = controller->reason;
928 }
929 }
930
931 static char * OVS_WARN_UNUSED_RESULT
932 parse_CONTROLLER(char *arg, const struct ofpact_parse_params *pp)
933 {
934 enum ofp_packet_in_reason reason = OFPR_ACTION;
935 uint16_t controller_id = 0;
936 uint16_t max_len = UINT16_MAX;
937 uint32_t meter_id = NX_CTLR_NO_METER;
938 const char *userdata = NULL;
939 bool pause = false;
940
941 if (!arg[0]) {
942 /* Use defaults. */
943 } else if (strspn(arg, "0123456789") == strlen(arg)) {
944 char *error = str_to_u16(arg, "max_len", &max_len);
945 if (error) {
946 return error;
947 }
948 } else {
949 char *name, *value;
950
951 while (ofputil_parse_key_value(&arg, &name, &value)) {
952 if (!strcmp(name, "reason")) {
953 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
954 return xasprintf("unknown reason \"%s\"", value);
955 }
956 } else if (!strcmp(name, "max_len")) {
957 char *error = str_to_u16(value, "max_len", &max_len);
958 if (error) {
959 return error;
960 }
961 } else if (!strcmp(name, "id")) {
962 char *error = str_to_u16(value, "id", &controller_id);
963 if (error) {
964 return error;
965 }
966 } else if (!strcmp(name, "userdata")) {
967 userdata = value;
968 } else if (!strcmp(name, "pause")) {
969 pause = true;
970 } else if (!strcmp(name, "meter_id")) {
971 char *error = str_to_u32(value, &meter_id);
972 if (error) {
973 return error;
974 }
975 } else {
976 return xasprintf("unknown key \"%s\" parsing controller "
977 "action", name);
978 }
979 }
980 }
981
982 if (reason == OFPR_ACTION && controller_id == 0 && !userdata && !pause
983 && meter_id == NX_CTLR_NO_METER) {
984 struct ofpact_output *output;
985
986 output = ofpact_put_OUTPUT(pp->ofpacts);
987 output->port = OFPP_CONTROLLER;
988 output->max_len = max_len;
989 } else {
990 struct ofpact_controller *controller;
991
992 controller = ofpact_put_CONTROLLER(pp->ofpacts);
993 controller->max_len = max_len;
994 controller->reason = reason;
995 controller->controller_id = controller_id;
996 controller->pause = pause;
997 controller->meter_id = meter_id;
998
999 if (userdata) {
1000 size_t start_ofs = pp->ofpacts->size;
1001 const char *end = ofpbuf_put_hex(pp->ofpacts, userdata, NULL);
1002 if (*end) {
1003 return xstrdup("bad hex digit in `controller' "
1004 "action `userdata'");
1005 }
1006 size_t userdata_len = pp->ofpacts->size - start_ofs;
1007 controller = pp->ofpacts->header;
1008 controller->userdata_len = userdata_len;
1009 }
1010
1011 if (ofpbuf_oversized(pp->ofpacts)) {
1012 return xasprintf("input too big");
1013 }
1014
1015 ofpact_finish_CONTROLLER(pp->ofpacts, &controller);
1016 }
1017
1018 return NULL;
1019 }
1020
1021 static void
1022 format_hex_arg(struct ds *s, const uint8_t *data, size_t len)
1023 {
1024 for (size_t i = 0; i < len; i++) {
1025 if (i) {
1026 ds_put_char(s, '.');
1027 }
1028 ds_put_format(s, "%02"PRIx8, data[i]);
1029 }
1030 }
1031
1032 static void
1033 format_CONTROLLER(const struct ofpact_controller *a,
1034 const struct ofpact_format_params *fp)
1035 {
1036 if (a->reason == OFPR_ACTION && !a->controller_id && !a->userdata_len
1037 && !a->pause && a->meter_id == NX_CTLR_NO_METER) {
1038 ds_put_format(fp->s, "%sCONTROLLER:%s%"PRIu16,
1039 colors.special, colors.end, a->max_len);
1040 } else {
1041 enum ofp_packet_in_reason reason = a->reason;
1042
1043 ds_put_format(fp->s, "%scontroller(%s", colors.paren, colors.end);
1044 if (reason != OFPR_ACTION) {
1045 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
1046
1047 ds_put_format(fp->s, "%sreason=%s%s,", colors.param, colors.end,
1048 ofputil_packet_in_reason_to_string(
1049 reason, reasonbuf, sizeof reasonbuf));
1050 }
1051 if (a->max_len != UINT16_MAX) {
1052 ds_put_format(fp->s, "%smax_len=%s%"PRIu16",",
1053 colors.param, colors.end, a->max_len);
1054 }
1055 if (a->controller_id != 0) {
1056 ds_put_format(fp->s, "%sid=%s%"PRIu16",",
1057 colors.param, colors.end, a->controller_id);
1058 }
1059 if (a->userdata_len) {
1060 ds_put_format(fp->s, "%suserdata=%s", colors.param, colors.end);
1061 format_hex_arg(fp->s, a->userdata, a->userdata_len);
1062 ds_put_char(fp->s, ',');
1063 }
1064 if (a->pause) {
1065 ds_put_format(fp->s, "%spause%s,", colors.value, colors.end);
1066 }
1067 if (a->meter_id != NX_CTLR_NO_METER) {
1068 ds_put_format(fp->s, "%smeter_id=%s%"PRIu32",",
1069 colors.param, colors.end, a->meter_id);
1070 }
1071 ds_chomp(fp->s, ',');
1072 ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
1073 }
1074 }
1075
1076 static enum ofperr
1077 check_CONTROLLER(const struct ofpact_controller *a OVS_UNUSED,
1078 const struct ofpact_check_params *cp OVS_UNUSED)
1079 {
1080 return 0;
1081 }
1082 \f
1083 /* Enqueue action. */
1084 struct ofp10_action_enqueue {
1085 ovs_be16 type; /* OFPAT10_ENQUEUE. */
1086 ovs_be16 len; /* Len is 16. */
1087 ovs_be16 port; /* Port that queue belongs. Should
1088 refer to a valid physical port
1089 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
1090 uint8_t pad[6]; /* Pad for 64-bit alignment. */
1091 ovs_be32 queue_id; /* Where to enqueue the packets. */
1092 };
1093 OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
1094
1095 static enum ofperr
1096 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
1097 enum ofp_version ofp_version OVS_UNUSED,
1098 struct ofpbuf *out)
1099 {
1100 struct ofpact_enqueue *enqueue;
1101
1102 enqueue = ofpact_put_ENQUEUE(out);
1103 enqueue->port = u16_to_ofp(ntohs(oae->port));
1104 enqueue->queue = ntohl(oae->queue_id);
1105 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
1106 && enqueue->port != OFPP_IN_PORT
1107 && enqueue->port != OFPP_LOCAL) {
1108 return OFPERR_OFPBAC_BAD_OUT_PORT;
1109 }
1110 return 0;
1111 }
1112
1113 static void
1114 encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
1115 enum ofp_version ofp_version, struct ofpbuf *out)
1116 {
1117 if (ofp_version == OFP10_VERSION) {
1118 struct ofp10_action_enqueue *oae;
1119
1120 oae = put_OFPAT10_ENQUEUE(out);
1121 oae->port = htons(ofp_to_u16(enqueue->port));
1122 oae->queue_id = htonl(enqueue->queue);
1123 } else {
1124 put_OFPAT_SET_QUEUE(out, ofp_version, enqueue->queue);
1125
1126 struct ofp11_action_output *oao = put_OFPAT11_OUTPUT(out);
1127 oao->port = ofputil_port_to_ofp11(enqueue->port);
1128 oao->max_len = OVS_BE16_MAX;
1129
1130 put_NXAST_POP_QUEUE(out);
1131 }
1132 }
1133
1134 static char * OVS_WARN_UNUSED_RESULT
1135 parse_ENQUEUE(char *arg, const struct ofpact_parse_params *pp)
1136 {
1137 char *sp = NULL;
1138 char *port = strtok_r(arg, ":q,", &sp);
1139 char *queue = strtok_r(NULL, "", &sp);
1140 struct ofpact_enqueue *enqueue;
1141
1142 if (port == NULL || queue == NULL) {
1143 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
1144 "\"enqueue(PORT,QUEUE)\"");
1145 }
1146
1147 enqueue = ofpact_put_ENQUEUE(pp->ofpacts);
1148 if (!ofputil_port_from_string(port, pp->port_map, &enqueue->port)) {
1149 return xasprintf("%s: enqueue to unknown port", port);
1150 }
1151 return str_to_u32(queue, &enqueue->queue);
1152 }
1153
1154 static void
1155 format_ENQUEUE(const struct ofpact_enqueue *a,
1156 const struct ofpact_format_params *fp)
1157 {
1158 ds_put_format(fp->s, "%senqueue:%s", colors.param, colors.end);
1159 ofputil_format_port(a->port, fp->port_map, fp->s);
1160 ds_put_format(fp->s, ":%"PRIu32, a->queue);
1161 }
1162
1163 static enum ofperr
1164 check_ENQUEUE(const struct ofpact_enqueue *a,
1165 const struct ofpact_check_params *cp)
1166 {
1167 if (ofp_to_u16(a->port) >= ofp_to_u16(cp->max_ports)
1168 && a->port != OFPP_IN_PORT
1169 && a->port != OFPP_LOCAL) {
1170 return OFPERR_OFPBAC_BAD_OUT_PORT;
1171 }
1172 return 0;
1173 }
1174 \f
1175 /* Action structure for NXAST_OUTPUT_REG.
1176 *
1177 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
1178 *
1179 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
1180 * the NXAST_REG_LOAD action.
1181 *
1182 * The acceptable nxm_header values for 'src' are the same as the acceptable
1183 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
1184 *
1185 * The 'max_len' field indicates the number of bytes to send when the chosen
1186 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
1187 * field of OFPAT_OUTPUT.
1188 *
1189 * The 'zero' field is required to be zeroed for forward compatibility. */
1190 struct nx_action_output_reg {
1191 ovs_be16 type; /* OFPAT_VENDOR. */
1192 ovs_be16 len; /* 24. */
1193 ovs_be32 vendor; /* NX_VENDOR_ID. */
1194 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
1195
1196 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
1197 ovs_be32 src; /* Source. */
1198
1199 ovs_be16 max_len; /* Max length to send to controller. */
1200
1201 uint8_t zero[6]; /* Reserved, must be zero. */
1202 };
1203 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
1204
1205 /* Action structure for NXAST_OUTPUT_REG2.
1206 *
1207 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
1208 * experimenter OXM as 'src'.
1209 */
1210 struct nx_action_output_reg2 {
1211 ovs_be16 type; /* OFPAT_VENDOR. */
1212 ovs_be16 len; /* 24. */
1213 ovs_be32 vendor; /* NX_VENDOR_ID. */
1214 ovs_be16 subtype; /* NXAST_OUTPUT_REG2. */
1215
1216 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
1217 ovs_be16 max_len; /* Max length to send to controller. */
1218
1219 /* Followed by:
1220 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
1221 * - Enough 0-bytes to pad the action out to 24 bytes. */
1222 uint8_t pad[10];
1223 };
1224 OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
1225
1226 static enum ofperr
1227 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
1228 enum ofp_version ofp_version OVS_UNUSED,
1229 const struct vl_mff_map *vl_mff_map,
1230 uint64_t *tlv_bitmap, struct ofpbuf *out)
1231 {
1232 struct ofpact_output_reg *output_reg;
1233 enum ofperr error;
1234
1235 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
1236 return OFPERR_OFPBAC_BAD_ARGUMENT;
1237 }
1238
1239 output_reg = ofpact_put_OUTPUT_REG(out);
1240 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
1241 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
1242 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
1243 output_reg->max_len = ntohs(naor->max_len);
1244 error = mf_vl_mff_mf_from_nxm_header(ntohl(naor->src), vl_mff_map,
1245 &output_reg->src.field, tlv_bitmap);
1246 if (error) {
1247 return error;
1248 }
1249
1250 return mf_check_src(&output_reg->src, NULL);
1251 }
1252
1253 static enum ofperr
1254 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
1255 enum ofp_version ofp_version OVS_UNUSED,
1256 const struct vl_mff_map *vl_mff_map,
1257 uint64_t *tlv_bitmap, struct ofpbuf *out)
1258 {
1259 struct ofpact_output_reg *output_reg;
1260 enum ofperr error;
1261
1262 output_reg = ofpact_put_OUTPUT_REG(out);
1263 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
1264 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
1265 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
1266 output_reg->max_len = ntohs(naor->max_len);
1267
1268 struct ofpbuf b = ofpbuf_const_initializer(naor, ntohs(naor->len));
1269 ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
1270
1271 error = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &output_reg->src.field,
1272 NULL, tlv_bitmap);
1273 if (error) {
1274 return error;
1275 }
1276
1277 if (!is_all_zeros(b.data, b.size)) {
1278 return OFPERR_NXBRC_MUST_BE_ZERO;
1279 }
1280
1281 return mf_check_src(&output_reg->src, NULL);
1282 }
1283
1284 static void
1285 encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
1286 enum ofp_version ofp_version OVS_UNUSED,
1287 struct ofpbuf *out)
1288 {
1289 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
1290 * cannot be encoded in the older form, encode it as
1291 * NXAST_RAW_OUTPUT_REG2. */
1292 if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
1293 || !mf_nxm_header(output_reg->src.field->id)) {
1294 struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
1295 size_t size = out->size;
1296
1297 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1298 output_reg->src.n_bits);
1299 naor->max_len = htons(output_reg->max_len);
1300
1301 out->size = size - sizeof naor->pad;
1302 nx_put_mff_header(out, output_reg->src.field, 0, false);
1303 out->size = size;
1304 } else {
1305 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
1306
1307 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1308 output_reg->src.n_bits);
1309 naor->src = htonl(nxm_header_from_mff(output_reg->src.field));
1310 naor->max_len = htons(output_reg->max_len);
1311 }
1312 }
1313
1314 static char * OVS_WARN_UNUSED_RESULT
1315 parse_OUTPUT_REG(const char *arg, const struct ofpact_parse_params *pp)
1316 {
1317 return parse_OUTPUT(arg, pp);
1318 }
1319
1320 static void
1321 format_OUTPUT_REG(const struct ofpact_output_reg *a,
1322 const struct ofpact_format_params *fp)
1323 {
1324 ds_put_format(fp->s, "%soutput:%s", colors.special, colors.end);
1325 mf_format_subfield(&a->src, fp->s);
1326 }
1327
1328 static enum ofperr
1329 check_OUTPUT_REG(const struct ofpact_output_reg *a,
1330 const struct ofpact_check_params *cp)
1331 {
1332 return mf_check_src(&a->src, cp->match);
1333 }
1334 \f
1335 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
1336 *
1337 * The bundle actions choose a slave from a supplied list of options.
1338 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
1339 * selection to a register.
1340 *
1341 * The list of possible slaves follows the nx_action_bundle structure. The size
1342 * of each slave is governed by its type as indicated by the 'slave_type'
1343 * parameter. The list of slaves should be padded at its end with zeros to make
1344 * the total length of the action a multiple of 8.
1345 *
1346 * Switches infer from the 'slave_type' parameter the size of each slave. All
1347 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
1348 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
1349 * 2 byte width. Switches should reject actions which indicate unknown or
1350 * unsupported slave types.
1351 *
1352 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
1353 * slave. If the switch does not support the specified 'algorithm' parameter,
1354 * it should reject the action.
1355 *
1356 * Several algorithms take into account liveness when selecting slaves. The
1357 * liveness of a slave is implementation defined (with one exception), but will
1358 * generally take into account things like its carrier status and the results
1359 * of any link monitoring protocols which happen to be running on it. In order
1360 * to give controllers a place-holder value, the OFPP_NONE port is always
1361 * considered live, that is, NXAST_BUNDLE_LOAD stores OFPP_NONE in the output
1362 * register if no slave is live.
1363 *
1364 * Some slave selection strategies require the use of a hash function, in which
1365 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
1366 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
1367 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
1368 * 'basis' parameter is used as a universal hash parameter. Different values
1369 * of 'basis' yield different hash results.
1370 *
1371 * The 'zero' parameter at the end of the action structure is reserved for
1372 * future use. Switches are required to reject actions which have nonzero
1373 * bytes in the 'zero' field.
1374 *
1375 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
1376 * should reject actions which have nonzero bytes in either of these fields.
1377 *
1378 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
1379 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
1380 * similar to those for the NXAST_REG_LOAD action. */
1381 struct nx_action_bundle {
1382 ovs_be16 type; /* OFPAT_VENDOR. */
1383 ovs_be16 len; /* Length including slaves. */
1384 ovs_be32 vendor; /* NX_VENDOR_ID. */
1385 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
1386
1387 /* Slave choice algorithm to apply to hash value. */
1388 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
1389
1390 /* What fields to hash and how. */
1391 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
1392 ovs_be16 basis; /* Universal hash parameter. */
1393
1394 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
1395 ovs_be16 n_slaves; /* Number of slaves. */
1396
1397 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
1398 ovs_be32 dst; /* Destination. */
1399
1400 uint8_t zero[4]; /* Reserved. Must be zero. */
1401 };
1402 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
1403
1404 static enum ofperr
1405 decode_bundle(bool load, const struct nx_action_bundle *nab,
1406 const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap,
1407 struct ofpbuf *ofpacts)
1408 {
1409 static struct vlog_rate_limit rll = VLOG_RATE_LIMIT_INIT(1, 5);
1410 struct ofpact_bundle *bundle;
1411 uint32_t slave_type;
1412 size_t slaves_size, i;
1413 enum ofperr error;
1414
1415 bundle = ofpact_put_BUNDLE(ofpacts);
1416
1417 bundle->n_slaves = ntohs(nab->n_slaves);
1418 bundle->basis = ntohs(nab->basis);
1419 bundle->fields = ntohs(nab->fields);
1420 bundle->algorithm = ntohs(nab->algorithm);
1421 slave_type = ntohl(nab->slave_type);
1422 slaves_size = ntohs(nab->len) - sizeof *nab;
1423
1424 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1425 if (!flow_hash_fields_valid(bundle->fields)) {
1426 VLOG_WARN_RL(&rll, "unsupported fields %d", (int) bundle->fields);
1427 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
1428 VLOG_WARN_RL(&rll, "too many slaves");
1429 } else if (bundle->algorithm != NX_BD_ALG_HRW
1430 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
1431 VLOG_WARN_RL(&rll, "unsupported algorithm %d", (int) bundle->algorithm);
1432 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
1433 VLOG_WARN_RL(&rll, "unsupported slave type %"PRIu32, slave_type);
1434 } else {
1435 error = 0;
1436 }
1437
1438 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
1439 VLOG_WARN_RL(&rll, "reserved field is nonzero");
1440 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1441 }
1442
1443 if (load) {
1444 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
1445 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
1446 error = mf_vl_mff_mf_from_nxm_header(ntohl(nab->dst), vl_mff_map,
1447 &bundle->dst.field, tlv_bitmap);
1448 if (error) {
1449 return error;
1450 }
1451
1452 if (bundle->dst.n_bits < 16) {
1453 VLOG_WARN_RL(&rll, "bundle_load action requires at least 16 bit "
1454 "destination.");
1455 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1456 }
1457 } else {
1458 if (nab->ofs_nbits || nab->dst) {
1459 VLOG_WARN_RL(&rll, "bundle action has nonzero reserved fields");
1460 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1461 }
1462 }
1463
1464 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
1465 VLOG_WARN_RL(&rll, "Nicira action %s only has %"PRIuSIZE" bytes "
1466 "allocated for slaves. %"PRIuSIZE" bytes are required "
1467 "for %u slaves.",
1468 load ? "bundle_load" : "bundle", slaves_size,
1469 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
1470 error = OFPERR_OFPBAC_BAD_LEN;
1471 } else {
1472 for (i = 0; i < bundle->n_slaves; i++) {
1473 ofp_port_t ofp_port
1474 = u16_to_ofp(ntohs(((ovs_be16 *)(nab + 1))[i]));
1475 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
1476 bundle = ofpacts->header;
1477 }
1478 }
1479
1480 ofpact_finish_BUNDLE(ofpacts, &bundle);
1481 if (!error) {
1482 error = bundle_check(bundle, OFPP_MAX, NULL);
1483 }
1484 return error;
1485 }
1486
1487 static enum ofperr
1488 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab,
1489 enum ofp_version ofp_version OVS_UNUSED,
1490 struct ofpbuf *out)
1491 {
1492 return decode_bundle(false, nab, NULL, NULL, out);
1493 }
1494
1495 static enum ofperr
1496 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
1497 enum ofp_version ofp_version OVS_UNUSED,
1498 const struct vl_mff_map *vl_mff_map,
1499 uint64_t *tlv_bitmap, struct ofpbuf *out)
1500 {
1501 return decode_bundle(true, nab, vl_mff_map, tlv_bitmap, out);
1502 }
1503
1504 static void
1505 encode_BUNDLE(const struct ofpact_bundle *bundle,
1506 enum ofp_version ofp_version OVS_UNUSED,
1507 struct ofpbuf *out)
1508 {
1509 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
1510 struct nx_action_bundle *nab;
1511 ovs_be16 *slaves;
1512 size_t i;
1513
1514 nab = (bundle->dst.field
1515 ? put_NXAST_BUNDLE_LOAD(out)
1516 : put_NXAST_BUNDLE(out));
1517 nab->len = htons(ntohs(nab->len) + slaves_len);
1518 nab->algorithm = htons(bundle->algorithm);
1519 nab->fields = htons(bundle->fields);
1520 nab->basis = htons(bundle->basis);
1521 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
1522 nab->n_slaves = htons(bundle->n_slaves);
1523 if (bundle->dst.field) {
1524 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1525 bundle->dst.n_bits);
1526 nab->dst = htonl(nxm_header_from_mff(bundle->dst.field));
1527 }
1528
1529 slaves = ofpbuf_put_zeros(out, slaves_len);
1530 for (i = 0; i < bundle->n_slaves; i++) {
1531 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1532 }
1533 }
1534
1535 static char * OVS_WARN_UNUSED_RESULT
1536 parse_BUNDLE(const char *arg, const struct ofpact_parse_params *pp)
1537 {
1538 return bundle_parse(arg, pp->port_map, pp->ofpacts);
1539 }
1540
1541 static char * OVS_WARN_UNUSED_RESULT
1542 parse_bundle_load(const char *arg, const struct ofpact_parse_params *pp)
1543 {
1544 return bundle_parse_load(arg, pp->port_map, pp->ofpacts);
1545 }
1546
1547 static void
1548 format_BUNDLE(const struct ofpact_bundle *a,
1549 const struct ofpact_format_params *fp)
1550 {
1551 bundle_format(a, fp->port_map, fp->s);
1552 }
1553
1554 static enum ofperr
1555 check_BUNDLE(const struct ofpact_bundle *a,
1556 const struct ofpact_check_params *cp)
1557 {
1558 return bundle_check(a, cp->max_ports, cp->match);
1559 }
1560 \f
1561 /* Set VLAN actions. */
1562
1563 static enum ofperr
1564 decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1565 {
1566 if (vid & ~0xfff) {
1567 return OFPERR_OFPBAC_BAD_ARGUMENT;
1568 } else {
1569 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1570 vlan_vid->vlan_vid = vid;
1571 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1572 return 0;
1573 }
1574 }
1575
1576 static enum ofperr
1577 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid,
1578 enum ofp_version ofp_version OVS_UNUSED,
1579 struct ofpbuf *out)
1580 {
1581 return decode_set_vlan_vid(vid, true, out);
1582 }
1583
1584 static enum ofperr
1585 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid,
1586 enum ofp_version ofp_version OVS_UNUSED,
1587 struct ofpbuf *out)
1588 {
1589 return decode_set_vlan_vid(vid, false, out);
1590 }
1591
1592 static void
1593 encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1594 enum ofp_version ofp_version, struct ofpbuf *out)
1595 {
1596 uint16_t vid = vlan_vid->vlan_vid;
1597
1598 /* Push a VLAN tag, if none is present and this form of the action calls
1599 * for such a feature. */
1600 if (ofp_version > OFP10_VERSION
1601 && vlan_vid->push_vlan_if_needed
1602 && !vlan_vid->flow_has_vlan) {
1603 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1604 }
1605
1606 if (ofp_version == OFP10_VERSION) {
1607 put_OFPAT10_SET_VLAN_VID(out, vid);
1608 } else if (ofp_version == OFP11_VERSION) {
1609 put_OFPAT11_SET_VLAN_VID(out, vid);
1610 } else {
1611 put_set_field(out, ofp_version, MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1612 }
1613 }
1614
1615 static char * OVS_WARN_UNUSED_RESULT
1616 parse_set_vlan_vid(char *arg, bool push_vlan_if_needed,
1617 const struct ofpact_parse_params *pp)
1618 {
1619 struct ofpact_vlan_vid *vlan_vid;
1620 uint16_t vid;
1621 char *error;
1622
1623 error = str_to_u16(arg, "VLAN VID", &vid);
1624 if (error) {
1625 return error;
1626 }
1627
1628 if (vid & ~VLAN_VID_MASK) {
1629 return xasprintf("%s: not a valid VLAN VID", arg);
1630 }
1631 vlan_vid = ofpact_put_SET_VLAN_VID(pp->ofpacts);
1632 vlan_vid->vlan_vid = vid;
1633 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1634 return NULL;
1635 }
1636
1637 static char * OVS_WARN_UNUSED_RESULT
1638 parse_SET_VLAN_VID(char *arg, const struct ofpact_parse_params *pp)
1639 {
1640 return parse_set_vlan_vid(arg, false, pp);
1641 }
1642
1643 static void
1644 format_SET_VLAN_VID(const struct ofpact_vlan_vid *a,
1645 const struct ofpact_format_params *fp)
1646 {
1647 ds_put_format(fp->s, "%s%s:%s%"PRIu16, colors.param,
1648 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1649 colors.end, a->vlan_vid);
1650 }
1651
1652 static enum ofperr
1653 check_SET_VLAN_VID(struct ofpact_vlan_vid *a, struct ofpact_check_params *cp)
1654 {
1655 /* Remember if we saw a vlan tag in the flow to aid translating to OpenFlow
1656 * 1.1+ if need be. */
1657 ovs_be16 *tci = &cp->match->flow.vlans[0].tci;
1658 a->flow_has_vlan = (*tci & htons(VLAN_CFI)) != 0;
1659 if (!a->flow_has_vlan && !a->push_vlan_if_needed) {
1660 inconsistent_match(&cp->usable_protocols);
1661 }
1662
1663 /* Temporarily mark that we have a vlan tag. */
1664 *tci |= htons(VLAN_CFI);
1665
1666 return 0;
1667 }
1668 \f
1669 /* Set PCP actions. */
1670
1671 static enum ofperr
1672 decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1673 {
1674 if (pcp & ~7) {
1675 return OFPERR_OFPBAC_BAD_ARGUMENT;
1676 } else {
1677 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1678 vlan_pcp->vlan_pcp = pcp;
1679 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1680 return 0;
1681 }
1682 }
1683
1684 static enum ofperr
1685 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp,
1686 enum ofp_version ofp_version OVS_UNUSED,
1687 struct ofpbuf *out)
1688 {
1689 return decode_set_vlan_pcp(pcp, true, out);
1690 }
1691
1692 static enum ofperr
1693 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp,
1694 enum ofp_version ofp_version OVS_UNUSED,
1695 struct ofpbuf *out)
1696 {
1697 return decode_set_vlan_pcp(pcp, false, out);
1698 }
1699
1700 static void
1701 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1702 enum ofp_version ofp_version, struct ofpbuf *out)
1703 {
1704 uint8_t pcp = vlan_pcp->vlan_pcp;
1705
1706 /* Push a VLAN tag, if none is present and this form of the action calls
1707 * for such a feature. */
1708 if (ofp_version > OFP10_VERSION
1709 && vlan_pcp->push_vlan_if_needed
1710 && !vlan_pcp->flow_has_vlan) {
1711 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1712 }
1713
1714 if (ofp_version == OFP10_VERSION) {
1715 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1716 } else if (ofp_version == OFP11_VERSION) {
1717 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1718 } else {
1719 put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1720 }
1721 }
1722
1723 static char * OVS_WARN_UNUSED_RESULT
1724 parse_set_vlan_pcp(char *arg, bool push_vlan_if_needed,
1725 const struct ofpact_parse_params *pp)
1726 {
1727 struct ofpact_vlan_pcp *vlan_pcp;
1728 uint8_t pcp;
1729 char *error;
1730
1731 error = str_to_u8(arg, "VLAN PCP", &pcp);
1732 if (error) {
1733 return error;
1734 }
1735
1736 if (pcp & ~7) {
1737 return xasprintf("%s: not a valid VLAN PCP", arg);
1738 }
1739 vlan_pcp = ofpact_put_SET_VLAN_PCP(pp->ofpacts);
1740 vlan_pcp->vlan_pcp = pcp;
1741 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1742 return NULL;
1743 }
1744
1745 static char * OVS_WARN_UNUSED_RESULT
1746 parse_SET_VLAN_PCP(char *arg, const struct ofpact_parse_params *pp)
1747 {
1748 return parse_set_vlan_pcp(arg, false, pp);
1749 }
1750
1751 static void
1752 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a,
1753 const struct ofpact_format_params *fp)
1754 {
1755 ds_put_format(fp->s, "%s%s:%s%"PRIu8, colors.param,
1756 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1757 colors.end, a->vlan_pcp);
1758 }
1759
1760 static enum ofperr
1761 check_SET_VLAN_PCP(struct ofpact_vlan_pcp *a, struct ofpact_check_params *cp)
1762 {
1763 /* Remember if we saw a vlan tag in the flow to aid translating to OpenFlow
1764 * 1.1+ if need be. */
1765 ovs_be16 *tci = &cp->match->flow.vlans[0].tci;
1766 a->flow_has_vlan = (*tci & htons(VLAN_CFI)) != 0;
1767 if (!a->flow_has_vlan && !a->push_vlan_if_needed) {
1768 inconsistent_match(&cp->usable_protocols);
1769 }
1770
1771 /* Temporarily mark that we have a vlan tag. */
1772 *tci |= htons(VLAN_CFI);
1773
1774 return 0;
1775 }
1776 \f
1777 /* Strip VLAN actions. */
1778
1779 static enum ofperr
1780 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1781 {
1782 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1783 return 0;
1784 }
1785
1786 static enum ofperr
1787 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1788 {
1789 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1790 return 0;
1791 }
1792
1793 static void
1794 encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1795 enum ofp_version ofp_version, struct ofpbuf *out)
1796 {
1797 if (ofp_version == OFP10_VERSION) {
1798 put_OFPAT10_STRIP_VLAN(out);
1799 } else {
1800 put_OFPAT11_POP_VLAN(out);
1801 }
1802 }
1803
1804 static char * OVS_WARN_UNUSED_RESULT
1805 parse_STRIP_VLAN(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
1806 {
1807 ofpact_put_STRIP_VLAN(pp->ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1808 return NULL;
1809 }
1810
1811 static char * OVS_WARN_UNUSED_RESULT
1812 parse_pop_vlan(const struct ofpact_parse_params *pp)
1813 {
1814 ofpact_put_STRIP_VLAN(pp->ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1815 return NULL;
1816 }
1817
1818 static void
1819 format_STRIP_VLAN(const struct ofpact_null *a,
1820 const struct ofpact_format_params *fp)
1821 {
1822 ds_put_format(fp->s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1823 ? "%spop_vlan%s"
1824 : "%sstrip_vlan%s"),
1825 colors.value, colors.end);
1826 }
1827
1828 static enum ofperr
1829 check_STRIP_VLAN(const struct ofpact_null *a OVS_UNUSED,
1830 struct ofpact_check_params *cp)
1831 {
1832 if (!(cp->match->flow.vlans[0].tci & htons(VLAN_CFI))) {
1833 inconsistent_match(&cp->usable_protocols);
1834 }
1835 flow_pop_vlan(&cp->match->flow, NULL);
1836 return 0;
1837 }
1838 \f
1839 /* Push VLAN action. */
1840
1841 static enum ofperr
1842 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type,
1843 enum ofp_version ofp_version OVS_UNUSED,
1844 struct ofpbuf *out)
1845 {
1846 struct ofpact_push_vlan *push_vlan;
1847 if (!eth_type_vlan(eth_type)) {
1848 return OFPERR_OFPBAC_BAD_ARGUMENT;
1849 }
1850 push_vlan = ofpact_put_PUSH_VLAN(out);
1851 push_vlan->ethertype = eth_type;
1852 return 0;
1853 }
1854
1855 static void
1856 encode_PUSH_VLAN(const struct ofpact_push_vlan *push_vlan,
1857 enum ofp_version ofp_version, struct ofpbuf *out)
1858 {
1859 if (ofp_version == OFP10_VERSION) {
1860 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1861 * follow this action. */
1862 } else {
1863 put_OFPAT11_PUSH_VLAN(out, push_vlan->ethertype);
1864 }
1865 }
1866
1867 static char * OVS_WARN_UNUSED_RESULT
1868 parse_PUSH_VLAN(char *arg, const struct ofpact_parse_params *pp)
1869 {
1870 struct ofpact_push_vlan *push_vlan;
1871 uint16_t ethertype;
1872 char *error;
1873
1874 *pp->usable_protocols &= OFPUTIL_P_OF11_UP;
1875 error = str_to_u16(arg, "ethertype", &ethertype);
1876 if (error) {
1877 return error;
1878 }
1879
1880 if (!eth_type_vlan(htons(ethertype))) {
1881 return xasprintf("%s: not a valid VLAN ethertype", arg);
1882 }
1883 push_vlan = ofpact_put_PUSH_VLAN(pp->ofpacts);
1884 push_vlan->ethertype = htons(ethertype);
1885 return NULL;
1886 }
1887
1888 static void
1889 format_PUSH_VLAN(const struct ofpact_push_vlan *push_vlan,
1890 const struct ofpact_format_params *fp)
1891 {
1892 ds_put_format(fp->s, "%spush_vlan:%s%#"PRIx16,
1893 colors.param, colors.end, ntohs(push_vlan->ethertype));
1894 }
1895
1896 static enum ofperr
1897 check_PUSH_VLAN(const struct ofpact_push_vlan *a OVS_UNUSED,
1898 struct ofpact_check_params *cp)
1899 {
1900 struct flow *flow = &cp->match->flow;
1901 if (flow->vlans[FLOW_MAX_VLAN_HEADERS - 1].tci & htons(VLAN_CFI)) {
1902 /* Support maximum (FLOW_MAX_VLAN_HEADERS) VLAN headers. */
1903 return OFPERR_OFPBAC_BAD_TAG;
1904 }
1905 /* Temporary mark that we have a vlan tag. */
1906 flow_push_vlan_uninit(flow, NULL);
1907 flow->vlans[0].tci |= htons(VLAN_CFI);
1908 return 0;
1909 }
1910 \f
1911 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1912 struct ofp_action_dl_addr {
1913 ovs_be16 type; /* Type. */
1914 ovs_be16 len; /* Length is 16. */
1915 struct eth_addr dl_addr; /* Ethernet address. */
1916 uint8_t pad[6];
1917 };
1918 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1919
1920 static enum ofperr
1921 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1922 enum ofp_version ofp_version OVS_UNUSED,
1923 struct ofpbuf *out)
1924 {
1925 ofpact_put_SET_ETH_SRC(out)->mac = a->dl_addr;
1926 return 0;
1927 }
1928
1929 static enum ofperr
1930 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1931 enum ofp_version ofp_version OVS_UNUSED,
1932 struct ofpbuf *out)
1933 {
1934 ofpact_put_SET_ETH_DST(out)->mac = a->dl_addr;
1935 return 0;
1936 }
1937
1938 static void
1939 encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1940 enum ofp_raw_action_type raw, enum mf_field_id field,
1941 struct ofpbuf *out)
1942 {
1943 if (ofp_version < OFP12_VERSION) {
1944 struct ofp_action_dl_addr *oada;
1945
1946 oada = ofpact_put_raw(out, ofp_version, raw, 0);
1947 oada->dl_addr = mac->mac;
1948 } else {
1949 put_set_field(out, ofp_version, field, eth_addr_to_uint64(mac->mac));
1950 }
1951 }
1952
1953 static void
1954 encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1955 struct ofpbuf *out)
1956 {
1957 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1958 out);
1959
1960 }
1961
1962 static void
1963 encode_SET_ETH_DST(const struct ofpact_mac *mac,
1964 enum ofp_version ofp_version,
1965 struct ofpbuf *out)
1966 {
1967 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1968 out);
1969
1970 }
1971
1972 static char * OVS_WARN_UNUSED_RESULT
1973 parse_SET_ETH_SRC(char *arg, const struct ofpact_parse_params *pp)
1974 {
1975 return str_to_mac(arg, &ofpact_put_SET_ETH_SRC(pp->ofpacts)->mac);
1976 }
1977
1978 static char * OVS_WARN_UNUSED_RESULT
1979 parse_SET_ETH_DST(char *arg, const struct ofpact_parse_params *pp)
1980 {
1981 return str_to_mac(arg, &ofpact_put_SET_ETH_DST(pp->ofpacts)->mac);
1982 }
1983
1984 static void
1985 format_SET_ETH_SRC(const struct ofpact_mac *a,
1986 const struct ofpact_format_params *fp)
1987 {
1988 ds_put_format(fp->s, "%smod_dl_src:%s"ETH_ADDR_FMT,
1989 colors.param, colors.end, ETH_ADDR_ARGS(a->mac));
1990 }
1991
1992 static void
1993 format_SET_ETH_DST(const struct ofpact_mac *a,
1994 const struct ofpact_format_params *fp)
1995 {
1996 ds_put_format(fp->s, "%smod_dl_dst:%s"ETH_ADDR_FMT,
1997 colors.param, colors.end, ETH_ADDR_ARGS(a->mac));
1998 }
1999
2000 static enum ofperr
2001 check_SET_ETH_SRC(const struct ofpact_mac *a OVS_UNUSED,
2002 const struct ofpact_check_params *cp OVS_UNUSED)
2003 {
2004 return 0;
2005 }
2006
2007 static enum ofperr
2008 check_SET_ETH_DST(const struct ofpact_mac *a OVS_UNUSED,
2009 const struct ofpact_check_params *cp OVS_UNUSED)
2010 {
2011 return 0;
2012 }
2013 \f
2014 /* Set IPv4 address actions. */
2015
2016 static enum ofperr
2017 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4,
2018 enum ofp_version ofp_version OVS_UNUSED,
2019 struct ofpbuf *out)
2020 {
2021 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
2022 return 0;
2023 }
2024
2025 static enum ofperr
2026 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4,
2027 enum ofp_version ofp_version OVS_UNUSED,
2028 struct ofpbuf *out)
2029 {
2030 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
2031 return 0;
2032 }
2033
2034 static void
2035 encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
2036 enum ofp_version ofp_version,
2037 enum ofp_raw_action_type raw, enum mf_field_id field,
2038 struct ofpbuf *out)
2039 {
2040 ovs_be32 addr = ipv4->ipv4;
2041 if (ofp_version < OFP12_VERSION) {
2042 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
2043 } else {
2044 put_set_field(out, ofp_version, field, ntohl(addr));
2045 }
2046 }
2047
2048 static void
2049 encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
2050 enum ofp_version ofp_version, struct ofpbuf *out)
2051 {
2052 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
2053 out);
2054 }
2055
2056 static void
2057 encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
2058 enum ofp_version ofp_version, struct ofpbuf *out)
2059 {
2060 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
2061 out);
2062 }
2063
2064 static char * OVS_WARN_UNUSED_RESULT
2065 parse_SET_IPV4_SRC(char *arg, const struct ofpact_parse_params *pp)
2066 {
2067 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(pp->ofpacts)->ipv4);
2068 }
2069
2070 static char * OVS_WARN_UNUSED_RESULT
2071 parse_SET_IPV4_DST(char *arg, const struct ofpact_parse_params *pp)
2072 {
2073 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(pp->ofpacts)->ipv4);
2074 }
2075
2076 static void
2077 format_SET_IPV4_SRC(const struct ofpact_ipv4 *a,
2078 const struct ofpact_format_params *fp)
2079 {
2080 ds_put_format(fp->s, "%smod_nw_src:%s"IP_FMT,
2081 colors.param, colors.end, IP_ARGS(a->ipv4));
2082 }
2083
2084 static void
2085 format_SET_IPV4_DST(const struct ofpact_ipv4 *a,
2086 const struct ofpact_format_params *fp)
2087 {
2088 ds_put_format(fp->s, "%smod_nw_dst:%s"IP_FMT,
2089 colors.param, colors.end, IP_ARGS(a->ipv4));
2090 }
2091
2092 static enum ofperr
2093 check_set_ipv4(struct ofpact_check_params *cp)
2094 {
2095 ovs_be16 dl_type = get_dl_type(&cp->match->flow);
2096 if (dl_type != htons(ETH_TYPE_IP)) {
2097 inconsistent_match(&cp->usable_protocols);
2098 }
2099 return 0;
2100 }
2101
2102 static enum ofperr
2103 check_SET_IPV4_SRC(const struct ofpact_ipv4 *a OVS_UNUSED,
2104 struct ofpact_check_params *cp)
2105 {
2106 return check_set_ipv4(cp);
2107 }
2108
2109 static enum ofperr
2110 check_SET_IPV4_DST(const struct ofpact_ipv4 *a OVS_UNUSED,
2111 struct ofpact_check_params *cp)
2112 {
2113 return check_set_ipv4(cp);
2114 }
2115 \f
2116 /* Set IPv4/v6 TOS actions. */
2117
2118 static enum ofperr
2119 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp,
2120 enum ofp_version ofp_version OVS_UNUSED,
2121 struct ofpbuf *out)
2122 {
2123 if (dscp & ~IP_DSCP_MASK) {
2124 return OFPERR_OFPBAC_BAD_ARGUMENT;
2125 } else {
2126 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
2127 return 0;
2128 }
2129 }
2130
2131 static void
2132 encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
2133 enum ofp_version ofp_version, struct ofpbuf *out)
2134 {
2135 if (ofp_version < OFP12_VERSION) {
2136 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
2137 } else {
2138 put_set_field(out, ofp_version, MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
2139 }
2140 }
2141
2142 static char * OVS_WARN_UNUSED_RESULT
2143 parse_SET_IP_DSCP(char *arg, const struct ofpact_parse_params *pp)
2144
2145 {
2146 uint8_t tos;
2147 char *error;
2148
2149 error = str_to_u8(arg, "TOS", &tos);
2150 if (error) {
2151 return error;
2152 }
2153
2154 if (tos & ~IP_DSCP_MASK) {
2155 return xasprintf("%s: not a valid TOS", arg);
2156 }
2157 ofpact_put_SET_IP_DSCP(pp->ofpacts)->dscp = tos;
2158 return NULL;
2159 }
2160
2161 static void
2162 format_SET_IP_DSCP(const struct ofpact_dscp *a,
2163 const struct ofpact_format_params *fp)
2164 {
2165 ds_put_format(fp->s, "%smod_nw_tos:%s%d",
2166 colors.param, colors.end, a->dscp);
2167 }
2168
2169 static enum ofperr
2170 check_set_ip(struct ofpact_check_params *cp)
2171 {
2172 if (!is_ip_any(&cp->match->flow)) {
2173 inconsistent_match(&cp->usable_protocols);
2174 }
2175 return 0;
2176 }
2177
2178 static enum ofperr
2179 check_SET_IP_DSCP(const struct ofpact_dscp *a OVS_UNUSED,
2180 struct ofpact_check_params *cp)
2181 {
2182 return check_set_ip(cp);
2183 }
2184 \f
2185 /* Set IPv4/v6 ECN actions. */
2186
2187 static enum ofperr
2188 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn,
2189 enum ofp_version ofp_version OVS_UNUSED,
2190 struct ofpbuf *out)
2191 {
2192 if (ecn & ~IP_ECN_MASK) {
2193 return OFPERR_OFPBAC_BAD_ARGUMENT;
2194 } else {
2195 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
2196 return 0;
2197 }
2198 }
2199
2200 static void
2201 encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
2202 enum ofp_version ofp_version, struct ofpbuf *out)
2203 {
2204 uint8_t ecn = ip_ecn->ecn;
2205 if (ofp_version == OFP10_VERSION) {
2206 struct mf_subfield dst = { .field = mf_from_id(MFF_IP_ECN),
2207 .ofs = 0, .n_bits = 2 };
2208 put_reg_load(out, &dst, ecn);
2209 } else if (ofp_version == OFP11_VERSION) {
2210 put_OFPAT11_SET_NW_ECN(out, ecn);
2211 } else {
2212 put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
2213 }
2214 }
2215
2216 static char * OVS_WARN_UNUSED_RESULT
2217 parse_SET_IP_ECN(char *arg, const struct ofpact_parse_params *pp)
2218 {
2219 uint8_t ecn;
2220 char *error;
2221
2222 error = str_to_u8(arg, "ECN", &ecn);
2223 if (error) {
2224 return error;
2225 }
2226
2227 if (ecn & ~IP_ECN_MASK) {
2228 return xasprintf("%s: not a valid ECN", arg);
2229 }
2230 ofpact_put_SET_IP_ECN(pp->ofpacts)->ecn = ecn;
2231 return NULL;
2232 }
2233
2234 static void
2235 format_SET_IP_ECN(const struct ofpact_ecn *a,
2236 const struct ofpact_format_params *fp)
2237 {
2238 ds_put_format(fp->s, "%smod_nw_ecn:%s%d",
2239 colors.param, colors.end, a->ecn);
2240 }
2241
2242 static enum ofperr
2243 check_SET_IP_ECN(const struct ofpact_ecn *a OVS_UNUSED,
2244 struct ofpact_check_params *cp)
2245 {
2246 return check_set_ip(cp);
2247 }
2248 \f
2249 /* Set IPv4/v6 TTL actions. */
2250
2251 static enum ofperr
2252 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl,
2253 enum ofp_version ofp_version OVS_UNUSED,
2254 struct ofpbuf *out)
2255 {
2256 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
2257 return 0;
2258 }
2259
2260 static void
2261 encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
2262 enum ofp_version ofp_version, struct ofpbuf *out)
2263 {
2264 if (ofp_version >= OFP11_VERSION) {
2265 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
2266 } else {
2267 struct mf_subfield dst = { .field = mf_from_id(MFF_IP_TTL),
2268 .ofs = 0, .n_bits = 8 };
2269 put_reg_load(out, &dst, ttl->ttl);
2270 }
2271 }
2272
2273 static char * OVS_WARN_UNUSED_RESULT
2274 parse_SET_IP_TTL(char *arg, const struct ofpact_parse_params *pp)
2275
2276 {
2277 uint8_t ttl;
2278 char *error;
2279
2280 error = str_to_u8(arg, "TTL", &ttl);
2281 if (error) {
2282 return error;
2283 }
2284
2285 ofpact_put_SET_IP_TTL(pp->ofpacts)->ttl = ttl;
2286 return NULL;
2287 }
2288
2289 static void
2290 format_SET_IP_TTL(const struct ofpact_ip_ttl *a,
2291 const struct ofpact_format_params *fp)
2292 {
2293 ds_put_format(fp->s, "%smod_nw_ttl:%s%d",
2294 colors.param, colors.end, a->ttl);
2295 }
2296
2297 static enum ofperr
2298 check_SET_IP_TTL(const struct ofpact_ip_ttl *a OVS_UNUSED,
2299 struct ofpact_check_params *cp)
2300 {
2301 return check_set_ip(cp);
2302 }
2303 \f
2304 /* Set TCP/UDP/SCTP port actions. */
2305
2306 static enum ofperr
2307 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port,
2308 enum ofp_version ofp_version OVS_UNUSED,
2309 struct ofpbuf *out)
2310 {
2311 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
2312 return 0;
2313 }
2314
2315 static enum ofperr
2316 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port,
2317 enum ofp_version ofp_version OVS_UNUSED,
2318 struct ofpbuf *out)
2319 {
2320 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
2321 return 0;
2322 }
2323
2324 static void
2325 encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
2326 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
2327 enum mf_field_id field, struct ofpbuf *out)
2328 {
2329 uint16_t port = l4_port->port;
2330
2331 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
2332 put_set_field(out, ofp_version, field, port);
2333 } else {
2334 ofpact_put_raw(out, ofp_version, raw, port);
2335 }
2336 }
2337
2338 static void
2339 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
2340 enum ofp_version ofp_version, struct ofpbuf *out)
2341 {
2342 uint8_t proto = l4_port->flow_ip_proto;
2343 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
2344 : proto == IPPROTO_UDP ? MFF_UDP_SRC
2345 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
2346 : MFF_N_IDS);
2347
2348 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
2349 }
2350
2351 static void
2352 encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
2353 enum ofp_version ofp_version,
2354 struct ofpbuf *out)
2355 {
2356 uint8_t proto = l4_port->flow_ip_proto;
2357 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
2358 : proto == IPPROTO_UDP ? MFF_UDP_DST
2359 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
2360 : MFF_N_IDS);
2361
2362 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
2363 }
2364
2365 static char * OVS_WARN_UNUSED_RESULT
2366 parse_SET_L4_SRC_PORT(char *arg, const struct ofpact_parse_params *pp)
2367 {
2368 return str_to_u16(arg, "source port",
2369 &ofpact_put_SET_L4_SRC_PORT(pp->ofpacts)->port);
2370 }
2371
2372 static char * OVS_WARN_UNUSED_RESULT
2373 parse_SET_L4_DST_PORT(char *arg, const struct ofpact_parse_params *pp)
2374 {
2375 return str_to_u16(arg, "destination port",
2376 &ofpact_put_SET_L4_DST_PORT(pp->ofpacts)->port);
2377 }
2378
2379 static void
2380 format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a,
2381 const struct ofpact_format_params *fp)
2382 {
2383 ds_put_format(fp->s, "%smod_tp_src:%s%d",
2384 colors.param, colors.end, a->port);
2385 }
2386
2387 static void
2388 format_SET_L4_DST_PORT(const struct ofpact_l4_port *a,
2389 const struct ofpact_format_params *fp)
2390 {
2391 ds_put_format(fp->s, "%smod_tp_dst:%s%d",
2392 colors.param, colors.end, a->port);
2393 }
2394
2395 static enum ofperr
2396 check_set_l4_port(struct ofpact_l4_port *a, struct ofpact_check_params *cp)
2397 {
2398 const struct flow *flow = &cp->match->flow;
2399 if (!is_ip_any(flow)
2400 || flow->nw_frag & FLOW_NW_FRAG_LATER
2401 || (flow->nw_proto != IPPROTO_TCP &&
2402 flow->nw_proto != IPPROTO_UDP &&
2403 flow->nw_proto != IPPROTO_SCTP)) {
2404 inconsistent_match(&cp->usable_protocols);
2405 }
2406
2407 /* Note the transport protocol in use, to allow this action to be converted
2408 * to an OF1.2 set_field action later if necessary. */
2409 a->flow_ip_proto = flow->nw_proto;
2410
2411 return 0;
2412 }
2413
2414 static enum ofperr
2415 check_SET_L4_SRC_PORT(struct ofpact_l4_port *a, struct ofpact_check_params *cp)
2416 {
2417 return check_set_l4_port(a, cp);
2418 }
2419
2420 static enum ofperr
2421 check_SET_L4_DST_PORT(struct ofpact_l4_port *a, struct ofpact_check_params *cp)
2422 {
2423 return check_set_l4_port(a, cp);
2424 }
2425 \f
2426 /* Action structure for OFPAT_COPY_FIELD. */
2427 struct ofp15_action_copy_field {
2428 ovs_be16 type; /* OFPAT_COPY_FIELD. */
2429 ovs_be16 len; /* Length is padded to 64 bits. */
2430 ovs_be16 n_bits; /* Number of bits to copy. */
2431 ovs_be16 src_offset; /* Starting bit offset in source. */
2432 ovs_be16 dst_offset; /* Starting bit offset in destination. */
2433 uint8_t pad[2];
2434 /* Followed by:
2435 * - OXM header for source field.
2436 * - OXM header for destination field.
2437 * - Padding with 0-bytes to a multiple of 8 bytes.
2438 * The "pad2" member is the beginning of the above. */
2439 uint8_t pad2[4];
2440 };
2441 OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
2442
2443 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
2444 struct onf_action_copy_field {
2445 ovs_be16 type; /* OFPAT_EXPERIMENTER. */
2446 ovs_be16 len; /* Length is padded to 64 bits. */
2447 ovs_be32 experimenter; /* ONF_VENDOR_ID. */
2448 ovs_be16 exp_type; /* 3200. */
2449 uint8_t pad[2]; /* Not used. */
2450 ovs_be16 n_bits; /* Number of bits to copy. */
2451 ovs_be16 src_offset; /* Starting bit offset in source. */
2452 ovs_be16 dst_offset; /* Starting bit offset in destination. */
2453 uint8_t pad2[2]; /* Not used. */
2454 /* Followed by:
2455 * - OXM header for source field.
2456 * - OXM header for destination field.
2457 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
2458 * The "pad3" member is the beginning of the above. */
2459 uint8_t pad3[4]; /* Not used. */
2460 };
2461 OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
2462
2463 /* Action structure for NXAST_REG_MOVE.
2464 *
2465 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
2466 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
2467 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
2468 * the next most significant bit, and so on.
2469 *
2470 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
2471 * sense to use nxm_hasmask=1 because the action does not do any kind of
2472 * matching; it uses the actual value of a field.)
2473 *
2474 * The following nxm_header values are potentially acceptable as 'src':
2475 *
2476 * - NXM_OF_IN_PORT
2477 * - NXM_OF_ETH_DST
2478 * - NXM_OF_ETH_SRC
2479 * - NXM_OF_ETH_TYPE
2480 * - NXM_OF_VLAN_TCI
2481 * - NXM_OF_IP_TOS
2482 * - NXM_OF_IP_PROTO
2483 * - NXM_OF_IP_SRC
2484 * - NXM_OF_IP_DST
2485 * - NXM_OF_TCP_SRC
2486 * - NXM_OF_TCP_DST
2487 * - NXM_OF_UDP_SRC
2488 * - NXM_OF_UDP_DST
2489 * - NXM_OF_ICMP_TYPE
2490 * - NXM_OF_ICMP_CODE
2491 * - NXM_OF_ARP_OP
2492 * - NXM_OF_ARP_SPA
2493 * - NXM_OF_ARP_TPA
2494 * - NXM_NX_TUN_ID
2495 * - NXM_NX_ARP_SHA
2496 * - NXM_NX_ARP_THA
2497 * - NXM_NX_ICMPV6_TYPE
2498 * - NXM_NX_ICMPV6_CODE
2499 * - NXM_NX_ND_SLL
2500 * - NXM_NX_ND_TLL
2501 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2502 * - NXM_NX_PKT_MARK
2503 * - NXM_NX_TUN_IPV4_SRC
2504 * - NXM_NX_TUN_IPV4_DST
2505 *
2506 * The following nxm_header values are potentially acceptable as 'dst':
2507 *
2508 * - NXM_OF_ETH_DST
2509 * - NXM_OF_ETH_SRC
2510 * - NXM_OF_IP_TOS
2511 * - NXM_OF_IP_SRC
2512 * - NXM_OF_IP_DST
2513 * - NXM_OF_TCP_SRC
2514 * - NXM_OF_TCP_DST
2515 * - NXM_OF_UDP_SRC
2516 * - NXM_OF_UDP_DST
2517 * - NXM_OF_ICMP_TYPE
2518 * - NXM_OF_ICMP_CODE
2519 * - NXM_NX_ICMPV6_TYPE
2520 * - NXM_NX_ICMPV6_CODE
2521 * - NXM_NX_ARP_SHA
2522 * - NXM_NX_ARP_THA
2523 * - NXM_OF_ARP_OP
2524 * - NXM_OF_ARP_SPA
2525 * - NXM_OF_ARP_TPA
2526 * Modifying any of the above fields changes the corresponding packet
2527 * header.
2528 *
2529 * - NXM_OF_IN_PORT
2530 *
2531 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2532 *
2533 * - NXM_NX_PKT_MARK
2534 *
2535 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
2536 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
2537 * header (if any), ignoring the other bits. Setting a value with CFI=1
2538 * adds or modifies the 802.1Q header appropriately, setting the TCI field
2539 * to the field's new value (with the CFI bit masked out).
2540 *
2541 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
2542 * any of these values modifies the corresponding tunnel header field used
2543 * for the packet's next tunnel encapsulation, if allowed by the
2544 * configuration of the output tunnel port.
2545 *
2546 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
2547 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
2548 * used only if the flow's nx_match includes an nxm_entry that specifies
2549 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
2550 *
2551 * The switch will reject actions for which src_ofs+n_bits is greater than the
2552 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
2553 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
2554 *
2555 * This action behaves properly when 'src' overlaps with 'dst', that is, it
2556 * behaves as if 'src' were copied out to a temporary buffer, then the
2557 * temporary buffer copied to 'dst'.
2558 */
2559 struct nx_action_reg_move {
2560 ovs_be16 type; /* OFPAT_VENDOR. */
2561 ovs_be16 len; /* Length is 24. */
2562 ovs_be32 vendor; /* NX_VENDOR_ID. */
2563 ovs_be16 subtype; /* NXAST_REG_MOVE. */
2564 ovs_be16 n_bits; /* Number of bits. */
2565 ovs_be16 src_ofs; /* Starting bit offset in source. */
2566 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
2567 /* Followed by:
2568 * - OXM/NXM header for source field (4 or 8 bytes).
2569 * - OXM/NXM header for destination field (4 or 8 bytes).
2570 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
2571 };
2572 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
2573
2574 static enum ofperr
2575 decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
2576 const void *action, ovs_be16 action_len, size_t oxm_offset,
2577 const struct vl_mff_map *vl_mff_map,
2578 uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2579 {
2580 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2581 enum ofperr error;
2582
2583 move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
2584 move->src.ofs = ntohs(src_offset);
2585 move->src.n_bits = ntohs(n_bits);
2586 move->dst.ofs = ntohs(dst_offset);
2587 move->dst.n_bits = ntohs(n_bits);
2588
2589 struct ofpbuf b = ofpbuf_const_initializer(action, ntohs(action_len));
2590 ofpbuf_pull(&b, oxm_offset);
2591
2592 error = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &move->src.field, NULL,
2593 tlv_bitmap);
2594 if (error) {
2595 return error;
2596 }
2597 error = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &move->dst.field, NULL,
2598 tlv_bitmap);
2599 if (error) {
2600 return error;
2601 }
2602
2603 if (!is_all_zeros(b.data, b.size)) {
2604 return OFPERR_NXBRC_MUST_BE_ZERO;
2605 }
2606
2607 return nxm_reg_move_check(move, NULL);
2608 }
2609
2610 static enum ofperr
2611 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
2612 enum ofp_version ofp_version OVS_UNUSED,
2613 const struct vl_mff_map *vl_mff_map,
2614 uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2615 {
2616 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2617 oacf->n_bits, oacf, oacf->len,
2618 OBJECT_OFFSETOF(oacf, pad2), vl_mff_map,
2619 tlv_bitmap, ofpacts);
2620 }
2621
2622 static enum ofperr
2623 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
2624 enum ofp_version ofp_version OVS_UNUSED,
2625 const struct vl_mff_map *vl_mff_map,
2626 uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2627 {
2628 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2629 oacf->n_bits, oacf, oacf->len,
2630 OBJECT_OFFSETOF(oacf, pad3), vl_mff_map,
2631 tlv_bitmap, ofpacts);
2632 }
2633
2634 static enum ofperr
2635 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
2636 enum ofp_version ofp_version OVS_UNUSED,
2637 const struct vl_mff_map *vl_mff_map,
2638 uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2639 {
2640 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2641 enum ofperr error;
2642
2643 move->ofpact.raw = NXAST_RAW_REG_MOVE;
2644 move->src.ofs = ntohs(narm->src_ofs);
2645 move->src.n_bits = ntohs(narm->n_bits);
2646 move->dst.ofs = ntohs(narm->dst_ofs);
2647 move->dst.n_bits = ntohs(narm->n_bits);
2648
2649 struct ofpbuf b = ofpbuf_const_initializer(narm, ntohs(narm->len));
2650 ofpbuf_pull(&b, sizeof *narm);
2651
2652 error = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &move->src.field, NULL,
2653 tlv_bitmap);
2654 if (error) {
2655 return error;
2656 }
2657
2658 error = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &move->dst.field, NULL,
2659 tlv_bitmap);
2660 if (error) {
2661 return error;
2662 }
2663
2664 if (!is_all_zeros(b.data, b.size)) {
2665 return OFPERR_NXBRC_MUST_BE_ZERO;
2666 }
2667
2668 return nxm_reg_move_check(move, NULL);
2669 }
2670
2671 static void
2672 encode_REG_MOVE(const struct ofpact_reg_move *move,
2673 enum ofp_version ofp_version, struct ofpbuf *out)
2674 {
2675 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2676 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2677 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2678 * probe for support. Until we have that ability, we currently prefer
2679 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2680 * versions. */
2681 size_t start_ofs = out->size;
2682 if (ofp_version >= OFP15_VERSION) {
2683 struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
2684 copy->n_bits = htons(move->dst.n_bits);
2685 copy->src_offset = htons(move->src.ofs);
2686 copy->dst_offset = htons(move->dst.ofs);
2687 out->size = out->size - sizeof copy->pad2;
2688 nx_put_mff_header(out, move->src.field, ofp_version, false);
2689 nx_put_mff_header(out, move->dst.field, ofp_version, false);
2690 } else if (ofp_version == OFP13_VERSION
2691 && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
2692 struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
2693 copy->n_bits = htons(move->dst.n_bits);
2694 copy->src_offset = htons(move->src.ofs);
2695 copy->dst_offset = htons(move->dst.ofs);
2696 out->size = out->size - sizeof copy->pad3;
2697 nx_put_mff_header(out, move->src.field, ofp_version, false);
2698 nx_put_mff_header(out, move->dst.field, ofp_version, false);
2699 } else {
2700 struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
2701 narm->n_bits = htons(move->dst.n_bits);
2702 narm->src_ofs = htons(move->src.ofs);
2703 narm->dst_ofs = htons(move->dst.ofs);
2704 nx_put_mff_header(out, move->src.field, 0, false);
2705 nx_put_mff_header(out, move->dst.field, 0, false);
2706 }
2707 pad_ofpat(out, start_ofs);
2708 }
2709
2710 static char * OVS_WARN_UNUSED_RESULT
2711 parse_REG_MOVE(const char *arg, const struct ofpact_parse_params *pp)
2712 {
2713 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(pp->ofpacts);
2714 return nxm_parse_reg_move(move, arg);
2715 }
2716
2717 static void
2718 format_REG_MOVE(const struct ofpact_reg_move *a,
2719 const struct ofpact_format_params *fp)
2720 {
2721 nxm_format_reg_move(a, fp->s);
2722 }
2723
2724 static enum ofperr
2725 check_REG_MOVE(const struct ofpact_reg_move *a,
2726 const struct ofpact_check_params *cp)
2727 {
2728 return nxm_reg_move_check(a, cp->match);
2729 }
2730 \f
2731 /* Action structure for OFPAT12_SET_FIELD. */
2732 struct ofp12_action_set_field {
2733 ovs_be16 type; /* OFPAT12_SET_FIELD. */
2734 ovs_be16 len; /* Length is padded to 64 bits. */
2735
2736 /* Followed by:
2737 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2738 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2739 *
2740 * The "pad" member is the beginning of the above. */
2741 uint8_t pad[4];
2742 };
2743 OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2744
2745 /* Action structure for NXAST_REG_LOAD.
2746 *
2747 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2748 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2749 * starts at 0 for the least-significant bit, 1 for the next most significant
2750 * bit, and so on.
2751 *
2752 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2753 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2754 * loading them.
2755 *
2756 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2757 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2758 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2759 * also stored as one less than its true value:
2760 *
2761 * 15 6 5 0
2762 * +------------------------------+------------------+
2763 * | ofs | n_bits - 1 |
2764 * +------------------------------+------------------+
2765 *
2766 * The switch will reject actions for which ofs+n_bits is greater than the
2767 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2768 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2769 * OFPBAC_BAD_ARGUMENT.
2770 */
2771 struct nx_action_reg_load {
2772 ovs_be16 type; /* OFPAT_VENDOR. */
2773 ovs_be16 len; /* Length is 24. */
2774 ovs_be32 vendor; /* NX_VENDOR_ID. */
2775 ovs_be16 subtype; /* NXAST_REG_LOAD. */
2776 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
2777 ovs_be32 dst; /* Destination register. */
2778 ovs_be64 value; /* Immediate value. */
2779 };
2780 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2781
2782 /* The NXAST_REG_LOAD2 action structure is "struct ext_action_header",
2783 * followed by:
2784 *
2785 * - An NXM/OXM header, value, and optionally a mask.
2786 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2787 *
2788 * The "pad" member is the beginning of the above. */
2789
2790 static enum ofperr
2791 decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2792 bool may_mask, const struct vl_mff_map *vl_mff_map,
2793 uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2794 {
2795 struct ofpbuf b = ofpbuf_const_initializer(oasf, ntohs(oasf->len));
2796 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2797
2798 union mf_value value, mask;
2799 const struct mf_field *field;
2800 enum ofperr error;
2801 error = mf_vl_mff_nx_pull_entry(&b, vl_mff_map, &field, &value,
2802 may_mask ? &mask : NULL, tlv_bitmap);
2803 if (error) {
2804 return (error == OFPERR_OFPBMC_BAD_MASK
2805 ? OFPERR_OFPBAC_BAD_SET_MASK
2806 : error);
2807 }
2808
2809 if (!may_mask) {
2810 memset(&mask, 0xff, field->n_bytes);
2811 }
2812
2813 if (!is_all_zeros(b.data, b.size)) {
2814 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2815 }
2816
2817 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2818 * Set-Field. */
2819 if (field->id == MFF_IN_PORT_OXM) {
2820 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2821 }
2822
2823 /* oxm_length is now validated to be compatible with mf_value. */
2824 if (!field->writable) {
2825 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2826 field->name);
2827 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2828 }
2829
2830 /* The value must be valid for match. OpenFlow 1.5 also says,
2831 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2832 * a 1-bit in oxm_value and in oxm_mask." */
2833 if (!mf_is_value_valid(field, &value)
2834 || (field->id == MFF_VLAN_VID
2835 && (!(mask.be16 & htons(OFPVID12_PRESENT))
2836 || !(value.be16 & htons(OFPVID12_PRESENT))))) {
2837 struct ds ds = DS_EMPTY_INITIALIZER;
2838 mf_format(field, &value, NULL, NULL, &ds);
2839 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2840 field->name, ds_cstr(&ds));
2841 ds_destroy(&ds);
2842
2843 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2844 }
2845
2846 ofpact_put_set_field(ofpacts, field, &value, &mask);
2847 return 0;
2848 }
2849
2850 static enum ofperr
2851 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2852 enum ofp_version ofp_version OVS_UNUSED,
2853 const struct vl_mff_map *vl_mff_map,
2854 uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2855 {
2856 return decode_ofpat_set_field(oasf, false, vl_mff_map, tlv_bitmap,
2857 ofpacts);
2858 }
2859
2860 static enum ofperr
2861 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2862 enum ofp_version ofp_version OVS_UNUSED,
2863 const struct vl_mff_map *vl_mff_map,
2864 uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2865 {
2866 return decode_ofpat_set_field(oasf, true, vl_mff_map, tlv_bitmap, ofpacts);
2867 }
2868
2869 static enum ofperr
2870 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2871 enum ofp_version ofp_version OVS_UNUSED,
2872 const struct vl_mff_map *vl_mff_map,
2873 uint64_t *tlv_bitmap, struct ofpbuf *out)
2874 {
2875 struct mf_subfield dst;
2876 enum ofperr error;
2877
2878 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2879 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2880 error = mf_vl_mff_mf_from_nxm_header(ntohl(narl->dst), vl_mff_map,
2881 &dst.field, tlv_bitmap);
2882 if (error) {
2883 return error;
2884 }
2885
2886 error = mf_check_dst(&dst, NULL);
2887 if (error) {
2888 return error;
2889 }
2890
2891 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2892 * narl->value. */
2893 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2894 return OFPERR_OFPBAC_BAD_ARGUMENT;
2895 }
2896
2897 struct ofpact_set_field *sf = ofpact_put_reg_load(out, dst.field, NULL,
2898 NULL);
2899 bitwise_put(ntohll(narl->value),
2900 sf->value, dst.field->n_bytes, dst.ofs,
2901 dst.n_bits);
2902 bitwise_put(UINT64_MAX,
2903 ofpact_set_field_mask(sf), dst.field->n_bytes, dst.ofs,
2904 dst.n_bits);
2905 return 0;
2906 }
2907
2908 static enum ofperr
2909 decode_NXAST_RAW_REG_LOAD2(const struct ext_action_header *eah,
2910 enum ofp_version ofp_version OVS_UNUSED,
2911 const struct vl_mff_map *vl_mff_map,
2912 uint64_t *tlv_bitmap, struct ofpbuf *out)
2913 {
2914 struct ofpbuf b = ofpbuf_const_initializer(eah, ntohs(eah->len));
2915 ofpbuf_pull(&b, OBJECT_OFFSETOF(eah, pad));
2916
2917 union mf_value value, mask;
2918 const struct mf_field *field;
2919 enum ofperr error;
2920 error = mf_vl_mff_nx_pull_entry(&b, vl_mff_map, &field, &value, &mask,
2921 tlv_bitmap);
2922 if (error) {
2923 return error;
2924 }
2925
2926 if (!is_all_zeros(b.data, b.size)) {
2927 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2928 }
2929
2930 if (!field->writable) {
2931 VLOG_WARN_RL(&rl, "destination field %s is not writable", field->name);
2932 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2933 }
2934
2935 /* Put value and mask. */
2936 ofpact_put_reg_load2(out, field, &value, &mask);
2937 return 0;
2938 }
2939
2940 static void
2941 put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2942 enum mf_field_id field, uint64_t value_)
2943 {
2944 struct ofp12_action_set_field *oasf OVS_UNUSED;
2945 int n_bytes = mf_from_id(field)->n_bytes;
2946 size_t start_ofs = openflow->size;
2947 union mf_value value;
2948
2949 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2950
2951 oasf = put_OFPAT12_SET_FIELD(openflow);
2952 openflow->size = openflow->size - sizeof oasf->pad;
2953 nx_put_entry(openflow, mf_from_id(field), ofp_version, &value, NULL);
2954 pad_ofpat(openflow, start_ofs);
2955 }
2956
2957 static void
2958 put_reg_load(struct ofpbuf *openflow,
2959 const struct mf_subfield *dst, uint64_t value)
2960 {
2961 ovs_assert(dst->n_bits <= 64);
2962
2963 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2964 narl->ofs_nbits = nxm_encode_ofs_nbits(dst->ofs, dst->n_bits);
2965 narl->dst = htonl(nxm_header_from_mff(dst->field));
2966 narl->value = htonll(value);
2967 }
2968
2969 static bool
2970 next_load_segment(const struct ofpact_set_field *sf,
2971 struct mf_subfield *dst, uint64_t *value)
2972 {
2973 int n_bits = sf->field->n_bits;
2974 int n_bytes = sf->field->n_bytes;
2975 int start = dst->ofs + dst->n_bits;
2976
2977 if (start < n_bits) {
2978 dst->field = sf->field;
2979 dst->ofs = bitwise_scan(ofpact_set_field_mask(sf), n_bytes, 1, start,
2980 n_bits);
2981 if (dst->ofs < n_bits) {
2982 dst->n_bits = bitwise_scan(ofpact_set_field_mask(sf), n_bytes, 0,
2983 dst->ofs + 1,
2984 MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2985 *value = bitwise_get(sf->value, n_bytes, dst->ofs, dst->n_bits);
2986 return true;
2987 }
2988 }
2989 return false;
2990 }
2991
2992 /* Convert 'sf' to a series of REG_LOADs. */
2993 static void
2994 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2995 {
2996 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2997 * experimenter OXM or is variable length (or if it came in as
2998 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2999 * NXAST_REG_LOAD, which is backward compatible. */
3000 if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
3001 || !mf_nxm_header(sf->field->id) || sf->field->variable_len) {
3002 struct ext_action_header *eah OVS_UNUSED;
3003 size_t start_ofs = openflow->size;
3004
3005 eah = put_NXAST_REG_LOAD2(openflow);
3006 openflow->size = openflow->size - sizeof eah->pad;
3007 nx_put_entry(openflow, sf->field, 0, sf->value,
3008 ofpact_set_field_mask(sf));
3009 pad_ofpat(openflow, start_ofs);
3010 } else {
3011 struct mf_subfield dst;
3012 uint64_t value;
3013
3014 dst.ofs = dst.n_bits = 0;
3015 while (next_load_segment(sf, &dst, &value)) {
3016 put_reg_load(openflow, &dst, value);
3017 }
3018 }
3019 }
3020
3021 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
3022 * actions, if we can, falling back to Nicira extensions if we must.
3023 *
3024 * We check only meta-flow types that can appear within set field actions and
3025 * that have a mapping to compatible action types. These struct mf_field
3026 * definitions have a defined OXM or NXM header value and specify the field as
3027 * writable. */
3028 static void
3029 set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
3030 enum ofp_version ofp_version,
3031 struct ofpbuf *out)
3032 {
3033 switch ((int) sf->field->id) {
3034 case MFF_VLAN_TCI: {
3035 ovs_be16 tci = sf->value->be16;
3036 bool cfi = (tci & htons(VLAN_CFI)) != 0;
3037 uint16_t vid = vlan_tci_to_vid(tci);
3038 uint8_t pcp = vlan_tci_to_pcp(tci);
3039
3040 if (ofp_version < OFP11_VERSION) {
3041 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
3042 *
3043 * If CFI=1, Add or modify VLAN VID & PCP.
3044 * If CFI=0, strip VLAN header, if any.
3045 */
3046 if (cfi) {
3047 put_OFPAT10_SET_VLAN_VID(out, vid);
3048 put_OFPAT10_SET_VLAN_PCP(out, pcp);
3049 } else {
3050 put_OFPAT10_STRIP_VLAN(out);
3051 }
3052 } else {
3053 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
3054 *
3055 * If CFI=1, Add or modify VLAN VID & PCP.
3056 * OpenFlow 1.1 set actions only apply if the packet
3057 * already has VLAN tags. To be sure that is the case
3058 * we have to push a VLAN header. As we do not support
3059 * multiple layers of VLANs, this is a no-op, if a VLAN
3060 * header already exists. This may backfire, however,
3061 * when we start supporting multiple layers of VLANs.
3062 * If CFI=0, strip VLAN header, if any.
3063 */
3064 if (cfi) {
3065 /* Push a VLAN tag, if one was not seen at action validation
3066 * time. */
3067 if (!sf->flow_has_vlan) {
3068 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
3069 }
3070 put_OFPAT11_SET_VLAN_VID(out, vid);
3071 put_OFPAT11_SET_VLAN_PCP(out, pcp);
3072 } else {
3073 /* If the flow did not match on vlan, we have no way of
3074 * knowing if the vlan tag exists, so we must POP just to be
3075 * sure. */
3076 put_OFPAT11_POP_VLAN(out);
3077 }
3078 }
3079 break;
3080 }
3081
3082 case MFF_VLAN_VID: {
3083 uint16_t vid = ntohs(sf->value->be16) & VLAN_VID_MASK;
3084 if (ofp_version == OFP10_VERSION) {
3085 put_OFPAT10_SET_VLAN_VID(out, vid);
3086 } else {
3087 put_OFPAT11_SET_VLAN_VID(out, vid);
3088 }
3089 break;
3090 }
3091
3092 case MFF_VLAN_PCP:
3093 if (ofp_version == OFP10_VERSION) {
3094 put_OFPAT10_SET_VLAN_PCP(out, sf->value->u8);
3095 } else {
3096 put_OFPAT11_SET_VLAN_PCP(out, sf->value->u8);
3097 }
3098 break;
3099
3100 case MFF_ETH_SRC:
3101 put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr = sf->value->mac;
3102 break;
3103
3104 case MFF_ETH_DST:
3105 put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr = sf->value->mac;
3106 break;
3107
3108 case MFF_IPV4_SRC:
3109 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value->be32);
3110 break;
3111
3112 case MFF_IPV4_DST:
3113 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value->be32);
3114 break;
3115
3116 case MFF_IP_DSCP:
3117 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value->u8);
3118 break;
3119
3120 case MFF_IP_DSCP_SHIFTED:
3121 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value->u8 << 2);
3122 break;
3123
3124 case MFF_IP_ECN: {
3125 struct ofpact_ecn ip_ecn = { .ecn = sf->value->u8 };
3126 encode_SET_IP_ECN(&ip_ecn, ofp_version, out);
3127 break;
3128 }
3129
3130 case MFF_TCP_SRC:
3131 case MFF_UDP_SRC:
3132 put_OFPAT_SET_TP_SRC(out, sf->value->be16);
3133 break;
3134
3135 case MFF_TCP_DST:
3136 case MFF_UDP_DST:
3137 put_OFPAT_SET_TP_DST(out, sf->value->be16);
3138 break;
3139
3140 default:
3141 set_field_to_nxast(sf, out);
3142 break;
3143 }
3144 }
3145
3146 static void
3147 set_field_to_set_field(const struct ofpact_set_field *sf,
3148 enum ofp_version ofp_version, struct ofpbuf *out)
3149 {
3150 struct ofp12_action_set_field *oasf OVS_UNUSED;
3151 size_t start_ofs = out->size;
3152
3153 oasf = put_OFPAT12_SET_FIELD(out);
3154 out->size = out->size - sizeof oasf->pad;
3155 nx_put_entry(out, sf->field, ofp_version, sf->value,
3156 ofpact_set_field_mask(sf));
3157 pad_ofpat(out, start_ofs);
3158 }
3159
3160 static void
3161 encode_SET_FIELD(const struct ofpact_set_field *sf,
3162 enum ofp_version ofp_version, struct ofpbuf *out)
3163 {
3164 if (ofp_version >= OFP15_VERSION) {
3165 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
3166 * entirely). */
3167 set_field_to_set_field(sf, ofp_version, out);
3168 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
3169 sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
3170 /* It came in as reg_load, send it out the same way. */
3171 set_field_to_nxast(sf, out);
3172 } else if (ofp_version < OFP12_VERSION) {
3173 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
3174 set_field_to_legacy_openflow(sf, ofp_version, out);
3175 } else if (is_all_ones(ofpact_set_field_mask(sf), sf->field->n_bytes)) {
3176 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
3177 * entire field, so encode it as OFPAT_SET_FIELD. */
3178 set_field_to_set_field(sf, ofp_version, out);
3179 } else {
3180 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
3181 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
3182 * so encode it as reg_load. */
3183 set_field_to_nxast(sf, out);
3184 }
3185 }
3186
3187 /* Parses the input argument 'arg' into the key, value, and delimiter
3188 * components that are common across the reg_load and set_field action format.
3189 *
3190 * With an argument like "1->metadata", sets the following pointers to
3191 * point within 'arg':
3192 * key: "metadata"
3193 * value: "1"
3194 * delim: "->"
3195 *
3196 * Returns NULL if successful, otherwise a malloc()'d string describing the
3197 * error. The caller is responsible for freeing the returned string. */
3198 static char * OVS_WARN_UNUSED_RESULT
3199 set_field_split_str(char *arg, char **key, char **value, char **delim)
3200 {
3201 char *value_end;
3202
3203 *value = arg;
3204 value_end = strstr(arg, "->");
3205 *key = value_end + strlen("->");
3206 if (delim) {
3207 *delim = value_end;
3208 }
3209
3210 if (!value_end) {
3211 return xasprintf("%s: missing `->'", arg);
3212 }
3213 if (strlen(value_end) <= strlen("->")) {
3214 return xasprintf("%s: missing field name following `->'", arg);
3215 }
3216
3217 return NULL;
3218 }
3219
3220 /* Parses a "set_field" action with argument 'arg', appending the parsed
3221 * action to 'pp->ofpacts'.
3222 *
3223 * Returns NULL if successful, otherwise a malloc()'d string describing the
3224 * error. The caller is responsible for freeing the returned string. */
3225 static char * OVS_WARN_UNUSED_RESULT
3226 set_field_parse__(char *arg, const struct ofpact_parse_params *pp)
3227 {
3228 char *value;
3229 char *delim;
3230 char *key;
3231 const struct mf_field *mf;
3232 union mf_value sf_value, sf_mask;
3233 char *error;
3234
3235 error = set_field_split_str(arg, &key, &value, &delim);
3236 if (error) {
3237 return error;
3238 }
3239
3240 mf = mf_from_name(key);
3241 if (!mf) {
3242 return xasprintf("%s is not a valid OXM field name", key);
3243 }
3244 if (!mf->writable) {
3245 return xasprintf("%s is read-only", key);
3246 }
3247
3248 delim[0] = '\0';
3249 error = mf_parse(mf, value, pp->port_map, &sf_value, &sf_mask);
3250 if (error) {
3251 return error;
3252 }
3253
3254 if (!mf_is_value_valid(mf, &sf_value)) {
3255 return xasprintf("%s is not a valid value for field %s", value, key);
3256 }
3257
3258 *pp->usable_protocols &= mf->usable_protocols_exact;
3259
3260 ofpact_put_set_field(pp->ofpacts, mf, &sf_value, &sf_mask);
3261 return NULL;
3262 }
3263
3264 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
3265 * action to 'pp->ofpacts'.
3266 *
3267 * Returns NULL if successful, otherwise a malloc()'d string describing the
3268 * error. The caller is responsible for freeing the returned string. */
3269 static char * OVS_WARN_UNUSED_RESULT
3270 parse_SET_FIELD(const char *arg, const struct ofpact_parse_params *pp)
3271 {
3272 char *copy = xstrdup(arg);
3273 char *error = set_field_parse__(copy, pp);
3274 free(copy);
3275 return error;
3276 }
3277
3278 static char * OVS_WARN_UNUSED_RESULT
3279 parse_reg_load(char *arg, const struct ofpact_parse_params *pp)
3280 {
3281 struct mf_subfield dst;
3282 char *key, *value_str;
3283 union mf_value value;
3284 char *error;
3285
3286 error = set_field_split_str(arg, &key, &value_str, NULL);
3287 if (error) {
3288 return error;
3289 }
3290
3291 error = mf_parse_subfield(&dst, key);
3292 if (error) {
3293 return error;
3294 }
3295
3296 if (parse_int_string(value_str, (uint8_t *)&value, dst.field->n_bytes,
3297 &key)) {
3298 return xasprintf("%s: cannot parse integer value", arg);
3299 }
3300
3301 if (!bitwise_is_all_zeros(&value, dst.field->n_bytes, dst.n_bits,
3302 dst.field->n_bytes * 8 - dst.n_bits)) {
3303 struct ds ds;
3304
3305 ds_init(&ds);
3306 mf_format(dst.field, &value, NULL, NULL, &ds);
3307 error = xasprintf("%s: value %s does not fit into %d bits",
3308 arg, ds_cstr(&ds), dst.n_bits);
3309 ds_destroy(&ds);
3310 return error;
3311 }
3312
3313 struct ofpact_set_field *sf = ofpact_put_reg_load(pp->ofpacts, dst.field,
3314 NULL, NULL);
3315
3316 bitwise_copy(&value, dst.field->n_bytes, 0, sf->value,
3317 dst.field->n_bytes, dst.ofs, dst.n_bits);
3318 bitwise_one(ofpact_set_field_mask(sf), dst.field->n_bytes, dst.ofs,
3319 dst.n_bits);
3320 return NULL;
3321 }
3322
3323 static void
3324 format_SET_FIELD(const struct ofpact_set_field *a,
3325 const struct ofpact_format_params *fp)
3326 {
3327 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
3328 struct mf_subfield dst;
3329 uint64_t value;
3330
3331 dst.ofs = dst.n_bits = 0;
3332 while (next_load_segment(a, &dst, &value)) {
3333 ds_put_format(fp->s, "%sload:%s%#"PRIx64"%s->%s",
3334 colors.special, colors.end, value,
3335 colors.special, colors.end);
3336 mf_format_subfield(&dst, fp->s);
3337 ds_put_char(fp->s, ',');
3338 }
3339 ds_chomp(fp->s, ',');
3340 } else {
3341 ds_put_format(fp->s, "%sset_field:%s", colors.special, colors.end);
3342 mf_format(a->field, a->value, ofpact_set_field_mask(a),
3343 fp->port_map, fp->s);
3344 ds_put_format(fp->s, "%s->%s%s",
3345 colors.special, colors.end, a->field->name);
3346 }
3347 }
3348
3349 static enum ofperr
3350 check_SET_FIELD(struct ofpact_set_field *a,
3351 const struct ofpact_check_params *cp)
3352 {
3353 const struct mf_field *mf = a->field;
3354 struct flow *flow = &cp->match->flow;
3355 ovs_be16 *tci = &flow->vlans[0].tci;
3356
3357 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
3358 if (!mf_are_prereqs_ok(mf, flow, NULL)
3359 || (mf->id == MFF_VLAN_VID && !(*tci & htons(VLAN_CFI)))) {
3360 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisites",
3361 mf->name);
3362 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
3363 }
3364
3365 /* Remember if we saw a VLAN tag in the flow, to aid translating to
3366 * OpenFlow 1.1 if need be. */
3367 a->flow_has_vlan = (*tci & htons(VLAN_CFI)) != 0;
3368 if (mf->id == MFF_VLAN_TCI) {
3369 /* The set field may add or remove the VLAN tag,
3370 * Mark the status temporarily. */
3371 *tci = a->value->be16;
3372 }
3373
3374 return 0;
3375 }
3376
3377 /* Appends an OFPACT_SET_FIELD ofpact with enough space for the value and mask
3378 * for the 'field' to 'ofpacts' and returns it. Fills in the value from
3379 * 'value', if non-NULL, and mask from 'mask' if non-NULL. If 'value' is
3380 * non-NULL and 'mask' is NULL, an all-ones mask will be filled in. */
3381 struct ofpact_set_field *
3382 ofpact_put_set_field(struct ofpbuf *ofpacts, const struct mf_field *field,
3383 const void *value, const void *mask)
3384 {
3385 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
3386 sf->field = field;
3387
3388 /* Fill in the value and mask if given, otherwise put zeroes so that the
3389 * caller may fill in the value and mask itself. */
3390 if (value) {
3391 ofpbuf_put_uninit(ofpacts, 2 * field->n_bytes);
3392 sf = ofpacts->header;
3393 memcpy(sf->value, value, field->n_bytes);
3394 if (mask) {
3395 memcpy(ofpact_set_field_mask(sf), mask, field->n_bytes);
3396 } else {
3397 memset(ofpact_set_field_mask(sf), 0xff, field->n_bytes);
3398 }
3399 } else {
3400 ofpbuf_put_zeros(ofpacts, 2 * field->n_bytes);
3401 sf = ofpacts->header;
3402 }
3403 /* Update length. */
3404 ofpact_finish_SET_FIELD(ofpacts, &sf);
3405
3406 return sf;
3407 }
3408
3409 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
3410 * is marked such that, if possible, it will be translated to OpenFlow as
3411 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
3412 * that was the way that the action was expressed when it came into OVS or for
3413 * backward compatibility. */
3414 struct ofpact_set_field *
3415 ofpact_put_reg_load(struct ofpbuf *ofpacts, const struct mf_field *field,
3416 const void *value, const void *mask)
3417 {
3418 struct ofpact_set_field *sf = ofpact_put_set_field(ofpacts, field, value,
3419 mask);
3420 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
3421
3422 return sf;
3423 }
3424
3425 struct ofpact_set_field *
3426 ofpact_put_reg_load2(struct ofpbuf *ofpacts, const struct mf_field *field,
3427 const void *value, const void *mask)
3428 {
3429 struct ofpact_set_field *sf = ofpact_put_set_field(ofpacts, field, value,
3430 mask);
3431 sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
3432
3433 return sf;
3434 }
3435
3436 \f
3437 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
3438 *
3439 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
3440 * top of the stack.
3441 */
3442 struct nx_action_stack {
3443 ovs_be16 type; /* OFPAT_VENDOR. */
3444 ovs_be16 len; /* Length is 16. */
3445 ovs_be32 vendor; /* NX_VENDOR_ID. */
3446 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
3447 ovs_be16 offset; /* Bit offset into the field. */
3448 /* Followed by:
3449 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
3450 * - ovs_be16 'n_bits', the number of bits to extract from the field.
3451 * - Enough 0-bytes to pad out the action to 24 bytes. */
3452 uint8_t pad[12]; /* See above. */
3453 };
3454 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
3455
3456 static enum ofperr
3457 decode_stack_action(const struct nx_action_stack *nasp,
3458 const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap,
3459 struct ofpact_stack *stack_action)
3460 {
3461 enum ofperr error;
3462 stack_action->subfield.ofs = ntohs(nasp->offset);
3463
3464 struct ofpbuf b = ofpbuf_const_initializer(nasp, sizeof *nasp);
3465 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
3466 error = mf_vl_mff_nx_pull_header(&b, vl_mff_map,
3467 &stack_action->subfield.field, NULL,
3468 tlv_bitmap);
3469 if (error) {
3470 return error;
3471 }
3472
3473 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
3474 ofpbuf_pull(&b, 2);
3475 if (!is_all_zeros(b.data, b.size)) {
3476 return OFPERR_NXBRC_MUST_BE_ZERO;
3477 }
3478
3479 return 0;
3480 }
3481
3482 static enum ofperr
3483 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
3484 enum ofp_version ofp_version OVS_UNUSED,
3485 const struct vl_mff_map *vl_mff_map,
3486 uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
3487 {
3488 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
3489 enum ofperr error = decode_stack_action(nasp, vl_mff_map, tlv_bitmap,
3490 push);
3491 return error ? error : nxm_stack_push_check(push, NULL);
3492 }
3493
3494 static enum ofperr
3495 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
3496 enum ofp_version ofp_version OVS_UNUSED,
3497 const struct vl_mff_map *vl_mff_map,
3498 uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
3499 {
3500 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
3501 enum ofperr error = decode_stack_action(nasp, vl_mff_map, tlv_bitmap,
3502 pop);
3503 return error ? error : nxm_stack_pop_check(pop, NULL);
3504 }
3505
3506 static void
3507 encode_STACK_op(const struct ofpact_stack *stack_action,
3508 struct nx_action_stack *nasp)
3509 {
3510 struct ofpbuf b;
3511 ovs_be16 n_bits;
3512
3513 nasp->offset = htons(stack_action->subfield.ofs);
3514
3515 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
3516 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
3517 nx_put_mff_header(&b, stack_action->subfield.field, 0, false);
3518 n_bits = htons(stack_action->subfield.n_bits);
3519 ofpbuf_put(&b, &n_bits, sizeof n_bits);
3520 }
3521
3522 static void
3523 encode_STACK_PUSH(const struct ofpact_stack *stack,
3524 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3525 {
3526 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
3527 }
3528
3529 static void
3530 encode_STACK_POP(const struct ofpact_stack *stack,
3531 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3532 {
3533 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
3534 }
3535
3536 static char * OVS_WARN_UNUSED_RESULT
3537 parse_STACK_PUSH(char *arg, const struct ofpact_parse_params *pp)
3538 {
3539 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(pp->ofpacts), arg);
3540 }
3541
3542 static char * OVS_WARN_UNUSED_RESULT
3543 parse_STACK_POP(char *arg, const struct ofpact_parse_params *pp)
3544 {
3545 return nxm_parse_stack_action(ofpact_put_STACK_POP(pp->ofpacts), arg);
3546 }
3547
3548 static void
3549 format_STACK_PUSH(const struct ofpact_stack *a,
3550 const struct ofpact_format_params *fp)
3551 {
3552 nxm_format_stack_push(a, fp->s);
3553 }
3554
3555 static void
3556 format_STACK_POP(const struct ofpact_stack *a,
3557 const struct ofpact_format_params *fp)
3558 {
3559 nxm_format_stack_pop(a, fp->s);
3560 }
3561
3562 static enum ofperr
3563 check_STACK_PUSH(const struct ofpact_stack *a,
3564 const struct ofpact_check_params *cp)
3565 {
3566 return nxm_stack_push_check(a, cp->match);
3567 }
3568
3569 static enum ofperr
3570 check_STACK_POP(const struct ofpact_stack *a,
3571 const struct ofpact_check_params *cp)
3572 {
3573 return nxm_stack_pop_check(a, cp->match);
3574 }
3575 \f
3576 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
3577 *
3578 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
3579 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
3580 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
3581 * 'n_controllers' controller IDs specified in 'cnt_ids'.
3582 *
3583 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
3584 * sent only to controllers with id 0.)
3585 */
3586 struct nx_action_cnt_ids {
3587 ovs_be16 type; /* OFPAT_VENDOR. */
3588 ovs_be16 len; /* Length including slaves. */
3589 ovs_be32 vendor; /* NX_VENDOR_ID. */
3590 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
3591
3592 ovs_be16 n_controllers; /* Number of controllers. */
3593 uint8_t zeros[4]; /* Must be zero. */
3594
3595 /* Followed by 1 or more controller ids:
3596 *
3597 * uint16_t cnt_ids[]; -- Controller ids.
3598 * uint8_t pad[]; -- Must be 0 to 8-byte align cnt_ids[].
3599 */
3600 };
3601 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
3602
3603 static enum ofperr
3604 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
3605 {
3606 uint16_t id = 0;
3607 struct ofpact_cnt_ids *ids;
3608 enum ofperr error = 0;
3609
3610 ids = ofpact_put_DEC_TTL(out);
3611 ids->n_controllers = 1;
3612 ofpbuf_put(out, &id, sizeof id);
3613 ids = out->header;
3614 ofpact_finish_DEC_TTL(out, &ids);
3615 return error;
3616 }
3617
3618 static enum ofperr
3619 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
3620 enum ofp_version ofp_version OVS_UNUSED,
3621 struct ofpbuf *out)
3622 {
3623 struct ofpact_cnt_ids *ids;
3624 size_t ids_size;
3625 int i;
3626
3627 ids = ofpact_put_DEC_TTL(out);
3628 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
3629 ids->n_controllers = ntohs(nac_ids->n_controllers);
3630 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
3631
3632 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
3633 return OFPERR_NXBRC_MUST_BE_ZERO;
3634 }
3635
3636 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
3637 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
3638 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
3639 "are required for %u controllers.",
3640 ids_size, ids->n_controllers * sizeof(ovs_be16),
3641 ids->n_controllers);
3642 return OFPERR_OFPBAC_BAD_LEN;
3643 }
3644
3645 for (i = 0; i < ids->n_controllers; i++) {
3646 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
3647 ofpbuf_put(out, &id, sizeof id);
3648 ids = out->header;
3649 }
3650
3651 ofpact_finish_DEC_TTL(out, &ids);
3652
3653 return 0;
3654 }
3655
3656 static void
3657 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
3658 enum ofp_version ofp_version, struct ofpbuf *out)
3659 {
3660 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
3661 || dec_ttl->n_controllers != 1
3662 || dec_ttl->cnt_ids[0] != 0) {
3663 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
3664 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
3665 ovs_be16 *ids;
3666 size_t i;
3667
3668 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
3669 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
3670
3671 ids = ofpbuf_put_zeros(out, ids_len);
3672 for (i = 0; i < dec_ttl->n_controllers; i++) {
3673 ids[i] = htons(dec_ttl->cnt_ids[i]);
3674 }
3675 } else {
3676 put_OFPAT_DEC_NW_TTL(out, ofp_version);
3677 }
3678 }
3679
3680 static void
3681 parse_noargs_dec_ttl(const struct ofpact_parse_params *pp)
3682 {
3683 struct ofpact_cnt_ids *ids;
3684 uint16_t id = 0;
3685
3686 ofpact_put_DEC_TTL(pp->ofpacts);
3687 ofpbuf_put(pp->ofpacts, &id, sizeof id);
3688 ids = pp->ofpacts->header;
3689 ids->n_controllers++;
3690 ofpact_finish_DEC_TTL(pp->ofpacts, &ids);
3691 }
3692
3693 static char * OVS_WARN_UNUSED_RESULT
3694 parse_DEC_TTL(char *arg, const struct ofpact_parse_params *pp)
3695 {
3696 if (*arg == '\0') {
3697 parse_noargs_dec_ttl(pp);
3698 } else {
3699 struct ofpact_cnt_ids *ids;
3700 char *cntr;
3701
3702 ids = ofpact_put_DEC_TTL(pp->ofpacts);
3703 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
3704 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
3705 cntr = strtok_r(NULL, ", ", &arg)) {
3706 uint16_t id = atoi(cntr);
3707
3708 ofpbuf_put(pp->ofpacts, &id, sizeof id);
3709 ids = pp->ofpacts->header;
3710 ids->n_controllers++;
3711 }
3712 if (!ids->n_controllers) {
3713 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
3714 "id.");
3715 }
3716
3717 if (ofpbuf_oversized(pp->ofpacts)) {
3718 return xasprintf("input too big");
3719 }
3720
3721 ofpact_finish_DEC_TTL(pp->ofpacts, &ids);
3722 }
3723 return NULL;
3724 }
3725
3726 static void
3727 format_DEC_TTL(const struct ofpact_cnt_ids *a,
3728 const struct ofpact_format_params *fp)
3729 {
3730 size_t i;
3731
3732 ds_put_format(fp->s, "%sdec_ttl%s", colors.paren, colors.end);
3733 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
3734 ds_put_format(fp->s, "%s(%s", colors.paren, colors.end);
3735 for (i = 0; i < a->n_controllers; i++) {
3736 if (i) {
3737 ds_put_cstr(fp->s, ",");
3738 }
3739 ds_put_format(fp->s, "%"PRIu16, a->cnt_ids[i]);
3740 }
3741 ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
3742 }
3743 }
3744
3745 static enum ofperr
3746 check_DEC_TTL(const struct ofpact_cnt_ids *a OVS_UNUSED,
3747 struct ofpact_check_params *cp)
3748 {
3749 return check_set_ip(cp);
3750 }
3751 \f
3752 /* Set MPLS label actions. */
3753
3754 static enum ofperr
3755 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label,
3756 enum ofp_version ofp_version OVS_UNUSED,
3757 struct ofpbuf *out)
3758 {
3759 ofpact_put_SET_MPLS_LABEL(out)->label = label;
3760 return 0;
3761 }
3762
3763 static void
3764 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
3765 enum ofp_version ofp_version,
3766 struct ofpbuf *out)
3767 {
3768 if (ofp_version < OFP12_VERSION) {
3769 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
3770 } else {
3771 put_set_field(out, ofp_version, MFF_MPLS_LABEL, ntohl(label->label));
3772 }
3773 }
3774
3775 static char * OVS_WARN_UNUSED_RESULT
3776 parse_SET_MPLS_LABEL(char *arg, const struct ofpact_parse_params *pp)
3777 {
3778 struct ofpact_mpls_label *mpls_label
3779 = ofpact_put_SET_MPLS_LABEL(pp->ofpacts);
3780 if (*arg == '\0') {
3781 return xstrdup("set_mpls_label: expected label.");
3782 }
3783
3784 mpls_label->label = htonl(atoi(arg));
3785 return NULL;
3786 }
3787
3788 static void
3789 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a,
3790 const struct ofpact_format_params *fp)
3791 {
3792 ds_put_format(fp->s, "%sset_mpls_label(%s%"PRIu32"%s)%s",
3793 colors.paren, colors.end, ntohl(a->label),
3794 colors.paren, colors.end);
3795 }
3796
3797 static enum ofperr
3798 check_set_mpls(struct ofpact_check_params *cp)
3799 {
3800 ovs_be16 dl_type = get_dl_type(&cp->match->flow);
3801 if (!eth_type_mpls(dl_type)) {
3802 inconsistent_match(&cp->usable_protocols);
3803 }
3804 return 0;
3805 }
3806
3807 static enum ofperr
3808 check_SET_MPLS_LABEL(const struct ofpact_mpls_label *a OVS_UNUSED,
3809 struct ofpact_check_params *cp)
3810 {
3811 return check_set_mpls(cp);
3812 }
3813 \f
3814 /* Set MPLS TC actions. */
3815
3816 static enum ofperr
3817 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc,
3818 enum ofp_version ofp_version OVS_UNUSED,
3819 struct ofpbuf *out)
3820 {
3821 ofpact_put_SET_MPLS_TC(out)->tc = tc;
3822 return 0;
3823 }
3824
3825 static void
3826 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
3827 enum ofp_version ofp_version, struct ofpbuf *out)
3828 {
3829 if (ofp_version < OFP12_VERSION) {
3830 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
3831 } else {
3832 put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
3833 }
3834 }
3835
3836 static char * OVS_WARN_UNUSED_RESULT
3837 parse_SET_MPLS_TC(char *arg, const struct ofpact_parse_params *pp)
3838 {
3839 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(pp->ofpacts);
3840
3841 if (*arg == '\0') {
3842 return xstrdup("set_mpls_tc: expected tc.");
3843 }
3844
3845 mpls_tc->tc = atoi(arg);
3846 return NULL;
3847 }
3848
3849 static void
3850 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a,
3851 const struct ofpact_format_params *fp)
3852 {
3853 ds_put_format(fp->s, "%sset_mpls_ttl(%s%"PRIu8"%s)%s",
3854 colors.paren, colors.end, a->tc,
3855 colors.paren, colors.end);
3856 }
3857
3858 static enum ofperr
3859 check_SET_MPLS_TC(const struct ofpact_mpls_tc *a OVS_UNUSED,
3860 struct ofpact_check_params *cp)
3861 {
3862 return check_set_mpls(cp);
3863 }
3864 \f
3865 /* Set MPLS TTL actions. */
3866
3867 static enum ofperr
3868 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl,
3869 enum ofp_version ofp_version OVS_UNUSED,
3870 struct ofpbuf *out)
3871 {
3872 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3873 return 0;
3874 }
3875
3876 static void
3877 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3878 enum ofp_version ofp_version, struct ofpbuf *out)
3879 {
3880 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3881 }
3882
3883 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3884 * action to 'pp->ofpacts'.
3885 *
3886 * Returns NULL if successful, otherwise a malloc()'d string describing the
3887 * error. The caller is responsible for freeing the returned string. */
3888 static char * OVS_WARN_UNUSED_RESULT
3889 parse_SET_MPLS_TTL(char *arg, const struct ofpact_parse_params *pp)
3890 {
3891 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(pp->ofpacts);
3892
3893 if (*arg == '\0') {
3894 return xstrdup("set_mpls_ttl: expected ttl.");
3895 }
3896
3897 mpls_ttl->ttl = atoi(arg);
3898 return NULL;
3899 }
3900
3901 static void
3902 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a,
3903 const struct ofpact_format_params *fp)
3904 {
3905 ds_put_format(fp->s, "%sset_mpls_ttl(%s%"PRIu8"%s)%s",
3906 colors.paren, colors.end, a->ttl,
3907 colors.paren, colors.end);
3908 }
3909
3910 static enum ofperr
3911 check_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a OVS_UNUSED,
3912 struct ofpact_check_params *cp)
3913 {
3914 return check_set_mpls(cp);
3915 }
3916 \f
3917 /* Decrement MPLS TTL actions. */
3918
3919 static enum ofperr
3920 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3921 {
3922 ofpact_put_DEC_MPLS_TTL(out);
3923 return 0;
3924 }
3925
3926 static void
3927 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3928 enum ofp_version ofp_version, struct ofpbuf *out)
3929 {
3930 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3931 }
3932
3933 static char * OVS_WARN_UNUSED_RESULT
3934 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
3935 {
3936 ofpact_put_DEC_MPLS_TTL(pp->ofpacts);
3937 return NULL;
3938 }
3939
3940 static void
3941 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED,
3942 const struct ofpact_format_params *fp)
3943 {
3944 ds_put_format(fp->s, "%sdec_mpls_ttl%s", colors.value, colors.end);
3945 }
3946
3947 static enum ofperr
3948 check_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED,
3949 struct ofpact_check_params *cp)
3950 {
3951 return check_set_mpls(cp);
3952 }
3953 \f
3954 /* Push MPLS label action. */
3955
3956 static enum ofperr
3957 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype,
3958 enum ofp_version ofp_version OVS_UNUSED,
3959 struct ofpbuf *out)
3960 {
3961 struct ofpact_push_mpls *oam;
3962
3963 if (!eth_type_mpls(ethertype)) {
3964 return OFPERR_OFPBAC_BAD_ARGUMENT;
3965 }
3966 oam = ofpact_put_PUSH_MPLS(out);
3967 oam->ethertype = ethertype;
3968
3969 return 0;
3970 }
3971
3972 static void
3973 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3974 enum ofp_version ofp_version, struct ofpbuf *out)
3975 {
3976 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3977 }
3978
3979 static char * OVS_WARN_UNUSED_RESULT
3980 parse_PUSH_MPLS(char *arg, const struct ofpact_parse_params *pp)
3981 {
3982 uint16_t ethertype;
3983 char *error;
3984
3985 error = str_to_u16(arg, "push_mpls", &ethertype);
3986 if (!error) {
3987 ofpact_put_PUSH_MPLS(pp->ofpacts)->ethertype = htons(ethertype);
3988 }
3989 return error;
3990 }
3991
3992 static void
3993 format_PUSH_MPLS(const struct ofpact_push_mpls *a,
3994 const struct ofpact_format_params *fp)
3995 {
3996 ds_put_format(fp->s, "%spush_mpls:%s0x%04"PRIx16,
3997 colors.param, colors.end, ntohs(a->ethertype));
3998 }
3999
4000 static enum ofperr
4001 check_PUSH_MPLS(const struct ofpact_push_mpls *a,
4002 struct ofpact_check_params *cp)
4003 {
4004 struct flow *flow = &cp->match->flow;
4005
4006 if (flow->packet_type != htonl(PT_ETH)) {
4007 inconsistent_match(&cp->usable_protocols);
4008 }
4009 flow->dl_type = a->ethertype;
4010
4011 /* The packet is now MPLS and the MPLS payload is opaque.
4012 * Thus nothing can be assumed about the network protocol.
4013 * Temporarily mark that we have no nw_proto. */
4014 flow->nw_proto = 0;
4015
4016 return 0;
4017 }
4018 \f
4019 /* Pop MPLS label action. */
4020
4021 static enum ofperr
4022 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype,
4023 enum ofp_version ofp_version OVS_UNUSED,
4024 struct ofpbuf *out)
4025 {
4026 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
4027 return 0;
4028 }
4029
4030 static void
4031 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
4032 enum ofp_version ofp_version, struct ofpbuf *out)
4033 {
4034 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
4035 }
4036
4037 static char * OVS_WARN_UNUSED_RESULT
4038 parse_POP_MPLS(char *arg, const struct ofpact_parse_params *pp)
4039 {
4040 uint16_t ethertype;
4041 char *error;
4042
4043 error = str_to_u16(arg, "pop_mpls", &ethertype);
4044 if (!error) {
4045 ofpact_put_POP_MPLS(pp->ofpacts)->ethertype = htons(ethertype);
4046 }
4047 return error;
4048 }
4049
4050 static void
4051 format_POP_MPLS(const struct ofpact_pop_mpls *a,
4052 const struct ofpact_format_params *fp)
4053 {
4054 ds_put_format(fp->s, "%spop_mpls:%s0x%04"PRIx16,
4055 colors.param, colors.end, ntohs(a->ethertype));
4056 }
4057
4058 static enum ofperr
4059 check_POP_MPLS(const struct ofpact_pop_mpls *a, struct ofpact_check_params *cp)
4060 {
4061 struct flow *flow = &cp->match->flow;
4062 ovs_be16 dl_type = get_dl_type(flow);
4063
4064 if (flow->packet_type != htonl(PT_ETH) || !eth_type_mpls(dl_type)) {
4065 inconsistent_match(&cp->usable_protocols);
4066 }
4067 flow->dl_type = a->ethertype;
4068 return 0;
4069 }
4070 \f
4071 /* Set tunnel ID actions. */
4072
4073 static enum ofperr
4074 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id,
4075 enum ofp_version ofp_version OVS_UNUSED,
4076 struct ofpbuf *out)
4077 {
4078 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
4079 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
4080 tunnel->tun_id = tun_id;
4081 return 0;
4082 }
4083
4084 static enum ofperr
4085 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id,
4086 enum ofp_version ofp_version OVS_UNUSED,
4087 struct ofpbuf *out)
4088 {
4089 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
4090 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
4091 tunnel->tun_id = tun_id;
4092 return 0;
4093 }
4094
4095 static void
4096 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
4097 enum ofp_version ofp_version, struct ofpbuf *out)
4098 {
4099 uint64_t tun_id = tunnel->tun_id;
4100
4101 if (ofp_version < OFP12_VERSION) {
4102 if (tun_id <= UINT32_MAX
4103 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
4104 put_NXAST_SET_TUNNEL(out, tun_id);
4105 } else {
4106 put_NXAST_SET_TUNNEL64(out, tun_id);
4107 }
4108 } else {
4109 put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
4110 }
4111 }
4112
4113 static char * OVS_WARN_UNUSED_RESULT
4114 parse_set_tunnel(char *arg, enum ofp_raw_action_type raw,
4115 const struct ofpact_parse_params *pp)
4116 {
4117 struct ofpact_tunnel *tunnel;
4118
4119 tunnel = ofpact_put_SET_TUNNEL(pp->ofpacts);
4120 tunnel->ofpact.raw = raw;
4121 return str_to_u64(arg, &tunnel->tun_id);
4122 }
4123
4124 static char * OVS_WARN_UNUSED_RESULT
4125 parse_SET_TUNNEL(char *arg, const struct ofpact_parse_params *pp)
4126 {
4127 return parse_set_tunnel(arg, NXAST_RAW_SET_TUNNEL, pp);
4128 }
4129
4130 static void
4131 format_SET_TUNNEL(const struct ofpact_tunnel *a,
4132 const struct ofpact_format_params *fp)
4133 {
4134 ds_put_format(fp->s, "%sset_tunnel%s:%s%#"PRIx64, colors.param,
4135 (a->tun_id > UINT32_MAX
4136 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
4137 colors.end, a->tun_id);
4138 }
4139
4140 static enum ofperr
4141 check_SET_TUNNEL(const struct ofpact_tunnel *a OVS_UNUSED,
4142 const struct ofpact_check_params *cp OVS_UNUSED)
4143 {
4144 return 0;
4145 }
4146 \f
4147 /* Delete field action. */
4148
4149 /* Action structure for DELETE_FIELD */
4150 struct nx_action_delete_field {
4151 ovs_be16 type; /* OFPAT_VENDOR */
4152 ovs_be16 len; /* Length is 24. */
4153 ovs_be32 vendor; /* NX_VENDOR_ID. */
4154 ovs_be16 subtype; /* NXAST_DELETE_FIELD. */
4155 /* Followed by:
4156 * - OXM/NXM header for field to delete (4 or 8 bytes).
4157 * - Enough 0-bytes to pad out the action to 24 bytes. */
4158 uint8_t pad[14];
4159 };
4160 OFP_ASSERT(sizeof(struct nx_action_delete_field ) == 24);
4161
4162 static enum ofperr
4163 decode_NXAST_RAW_DELETE_FIELD(const struct nx_action_delete_field *nadf,
4164 enum ofp_version ofp_version OVS_UNUSED,
4165 const struct vl_mff_map *vl_mff_map,
4166 uint64_t *tlv_bitmap, struct ofpbuf *out)
4167 {
4168 struct ofpact_delete_field *delete_field;
4169 enum ofperr err;
4170
4171 delete_field = ofpact_put_DELETE_FIELD(out);
4172 delete_field->ofpact.raw = NXAST_RAW_DELETE_FIELD;
4173
4174 struct ofpbuf b = ofpbuf_const_initializer(nadf, ntohs(nadf->len));
4175 ofpbuf_pull(&b, OBJECT_OFFSETOF(nadf, pad));
4176
4177 err = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &delete_field->field,
4178 NULL, tlv_bitmap);
4179 if (err) {
4180 return err;
4181 }
4182
4183 return 0;
4184 }
4185
4186 static void
4187 encode_DELETE_FIELD(const struct ofpact_delete_field *delete_field,
4188 enum ofp_version ofp_version OVS_UNUSED,
4189 struct ofpbuf *out)
4190 {
4191 struct nx_action_delete_field *nadf = put_NXAST_DELETE_FIELD(out);
4192 size_t size = out->size;
4193
4194 out->size = size - sizeof nadf->pad;
4195 nx_put_mff_header(out, delete_field->field, 0, false);
4196 out->size = size;
4197 }
4198
4199 static char * OVS_WARN_UNUSED_RESULT
4200 parse_DELETE_FIELD(char *arg, const struct ofpact_parse_params *pp)
4201 {
4202 struct ofpact_delete_field *delete_field;
4203
4204 delete_field = ofpact_put_DELETE_FIELD(pp->ofpacts);
4205 return mf_parse_field(&delete_field->field, arg);
4206 }
4207
4208 static void
4209 format_DELETE_FIELD(const struct ofpact_delete_field *odf,
4210 const struct ofpact_format_params *fp)
4211 {
4212 ds_put_format(fp->s, "%sdelete_field:%s", colors.param,
4213 colors.end);
4214 ds_put_format(fp->s, "%s", odf->field->name);
4215 }
4216
4217 static enum ofperr
4218 check_DELETE_FIELD(const struct ofpact_delete_field *odf,
4219 struct ofpact_check_params *cp OVS_UNUSED)
4220 {
4221 if (odf->field->id < MFF_TUN_METADATA0 ||
4222 odf->field->id > MFF_TUN_METADATA63) {
4223 return OFPERR_OFPBAC_BAD_ARGUMENT;
4224 }
4225 return 0;
4226 }
4227 \f
4228 /* Set queue action. */
4229
4230 static enum ofperr
4231 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id,
4232 enum ofp_version ofp_version OVS_UNUSED,
4233 struct ofpbuf *out)
4234 {
4235 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
4236 return 0;
4237 }
4238
4239 static void
4240 encode_SET_QUEUE(const struct ofpact_queue *queue,
4241 enum ofp_version ofp_version, struct ofpbuf *out)
4242 {
4243 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
4244 }
4245
4246 static char * OVS_WARN_UNUSED_RESULT
4247 parse_SET_QUEUE(char *arg, const struct ofpact_parse_params *pp)
4248 {
4249 return str_to_u32(arg, &ofpact_put_SET_QUEUE(pp->ofpacts)->queue_id);
4250 }
4251
4252 static void
4253 format_SET_QUEUE(const struct ofpact_queue *a,
4254 const struct ofpact_format_params *fp)
4255 {
4256 ds_put_format(fp->s, "%sset_queue:%s%"PRIu32,
4257 colors.param, colors.end, a->queue_id);
4258 }
4259
4260 static enum ofperr
4261 check_SET_QUEUE(const struct ofpact_queue *a OVS_UNUSED,
4262 const struct ofpact_check_params *cp OVS_UNUSED)
4263 {
4264 return 0;
4265 }
4266 \f
4267 /* Pop queue action. */
4268
4269 static enum ofperr
4270 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
4271 {
4272 ofpact_put_POP_QUEUE(out);
4273 return 0;
4274 }
4275
4276 static void
4277 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
4278 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4279 {
4280 put_NXAST_POP_QUEUE(out);
4281 }
4282
4283 static char * OVS_WARN_UNUSED_RESULT
4284 parse_POP_QUEUE(const char *arg OVS_UNUSED,
4285 const struct ofpact_parse_params *pp)
4286 {
4287 ofpact_put_POP_QUEUE(pp->ofpacts);
4288 return NULL;
4289 }
4290
4291 static void
4292 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED,
4293 const struct ofpact_format_params *fp)
4294 {
4295 ds_put_format(fp->s, "%spop_queue%s", colors.value, colors.end);
4296 }
4297
4298 static enum ofperr
4299 check_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED,
4300 const struct ofpact_check_params *cp OVS_UNUSED)
4301 {
4302 return 0;
4303 }
4304 \f
4305 /* Action structure for NXAST_FIN_TIMEOUT.
4306 *
4307 * This action changes the idle timeout or hard timeout, or both, of this
4308 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
4309 * When such a packet is observed, the action reduces the rule's idle timeout
4310 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
4311 * action has no effect on an existing timeout that is already shorter than the
4312 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
4313 * of zero has no effect on the respective timeout.
4314 *
4315 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
4316 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
4317 * receipt of the FIN or RST.
4318 *
4319 * This is useful for quickly discarding learned TCP flows that otherwise will
4320 * take a long time to expire.
4321 *
4322 * This action is intended for use with an OpenFlow rule that matches only a
4323 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
4324 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
4325 * RST in any of those flows will cause the entire OpenFlow rule to expire
4326 * early, which is not normally desirable.
4327 */
4328 struct nx_action_fin_timeout {
4329 ovs_be16 type; /* OFPAT_VENDOR. */
4330 ovs_be16 len; /* 16. */
4331 ovs_be32 vendor; /* NX_VENDOR_ID. */
4332 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
4333 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
4334 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
4335 ovs_be16 pad; /* Must be zero. */
4336 };
4337 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
4338
4339 static enum ofperr
4340 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
4341 enum ofp_version ofp_version OVS_UNUSED,
4342 struct ofpbuf *out)
4343 {
4344 struct ofpact_fin_timeout *oft;
4345
4346 oft = ofpact_put_FIN_TIMEOUT(out);
4347 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
4348 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
4349 return 0;
4350 }
4351
4352 static void
4353 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
4354 enum ofp_version ofp_version OVS_UNUSED,
4355 struct ofpbuf *out)
4356 {
4357 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
4358 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
4359 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
4360 }
4361
4362 static char * OVS_WARN_UNUSED_RESULT
4363 parse_FIN_TIMEOUT(char *arg, const struct ofpact_parse_params *pp)
4364 {
4365 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(pp->ofpacts);
4366 char *key, *value;
4367
4368 while (ofputil_parse_key_value(&arg, &key, &value)) {
4369 char *error;
4370
4371 if (!strcmp(key, "idle_timeout")) {
4372 error = str_to_u16(value, key, &oft->fin_idle_timeout);
4373 } else if (!strcmp(key, "hard_timeout")) {
4374 error = str_to_u16(value, key, &oft->fin_hard_timeout);
4375 } else {
4376 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
4377 key);
4378 }
4379
4380 if (error) {
4381 return error;
4382 }
4383 }
4384 return NULL;
4385 }
4386
4387 static void
4388 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a,
4389 const struct ofpact_format_params *fp)
4390 {
4391 ds_put_format(fp->s, "%sfin_timeout(%s", colors.paren, colors.end);
4392 if (a->fin_idle_timeout) {
4393 ds_put_format(fp->s, "%sidle_timeout=%s%"PRIu16",",
4394 colors.param, colors.end, a->fin_idle_timeout);
4395 }
4396 if (a->fin_hard_timeout) {
4397 ds_put_format(fp->s, "%shard_timeout=%s%"PRIu16",",
4398 colors.param, colors.end, a->fin_hard_timeout);
4399 }
4400 ds_chomp(fp->s, ',');
4401 ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
4402 }
4403
4404
4405 static enum ofperr
4406 check_FIN_TIMEOUT(const struct ofpact_fin_timeout *a OVS_UNUSED,
4407 struct ofpact_check_params *cp)
4408 {
4409 if (cp->match->flow.nw_proto != IPPROTO_TCP) {
4410 inconsistent_match(&cp->usable_protocols);
4411 }
4412 return 0;
4413 }
4414 \f
4415 /* Action structure for NXAST_ENCAP */
4416 struct nx_action_encap {
4417 ovs_be16 type; /* OFPAT_VENDOR. */
4418 ovs_be16 len; /* Total size including any property TLVs. */
4419 ovs_be32 vendor; /* NX_VENDOR_ID. */
4420 ovs_be16 subtype; /* NXAST_ENCAP. */
4421 ovs_be16 hdr_size; /* Header size in bytes, 0 = 'not specified'.*/
4422 ovs_be32 new_pkt_type; /* Header type to add and PACKET_TYPE of result. */
4423 struct ofp_ed_prop_header props[]; /* Encap TLV properties. */
4424 };
4425 OFP_ASSERT(sizeof(struct nx_action_encap) == 16);
4426
4427 static enum ofperr
4428 decode_NXAST_RAW_ENCAP(const struct nx_action_encap *nae,
4429 enum ofp_version ofp_version OVS_UNUSED,
4430 struct ofpbuf *out)
4431 {
4432 struct ofpact_encap *encap;
4433 const struct ofp_ed_prop_header *ofp_prop;
4434 size_t props_len;
4435 uint16_t n_props = 0;
4436 int err;
4437
4438 encap = ofpact_put_ENCAP(out);
4439 encap->ofpact.raw = NXAST_RAW_ENCAP;
4440 switch (ntohl(nae->new_pkt_type)) {
4441 case PT_ETH:
4442 case PT_NSH:
4443 /* Add supported encap header types here. */
4444 break;
4445 default:
4446 return OFPERR_NXBAC_BAD_HEADER_TYPE;
4447 }
4448 encap->new_pkt_type = nae->new_pkt_type;
4449 encap->hdr_size = ntohs(nae->hdr_size);
4450
4451 ofp_prop = nae->props;
4452 props_len = ntohs(nae->len) - offsetof(struct nx_action_encap, props);
4453 n_props = 0;
4454 while (props_len > 0) {
4455 err = decode_ed_prop(&ofp_prop, out, &props_len);
4456 if (err) {
4457 return err;
4458 }
4459 n_props++;
4460 }
4461 encap->n_props = n_props;
4462 out->header = &encap->ofpact;
4463 ofpact_finish_ENCAP(out, &encap);
4464
4465 return 0;
4466 }
4467
4468 static void
4469 encode_ENCAP(const struct ofpact_encap *encap,
4470 enum ofp_version ofp_version OVS_UNUSED,
4471 struct ofpbuf *out)
4472 {
4473 size_t start_ofs = out->size;
4474 struct nx_action_encap *nae = put_NXAST_ENCAP(out);
4475 int i;
4476
4477 nae->new_pkt_type = encap->new_pkt_type;
4478 nae->hdr_size = htons(encap->hdr_size);
4479 const struct ofpact_ed_prop *prop = encap->props;
4480 for (i = 0; i < encap->n_props; i++) {
4481 encode_ed_prop(&prop, out);
4482 }
4483 pad_ofpat(out, start_ofs);
4484 }
4485
4486 static bool
4487 parse_encap_header(const char *hdr, ovs_be32 *packet_type)
4488 {
4489 if (strcmp(hdr, "ethernet") == 0) {
4490 *packet_type = htonl(PT_ETH);
4491 } else if (strcmp(hdr, "nsh") == 0) {
4492 *packet_type = htonl(PT_NSH);
4493 } else {
4494 return false;
4495 }
4496 return true;
4497 }
4498
4499 static char *
4500 parse_ed_props(const uint16_t prop_class, char **arg, int *n_props, struct ofpbuf *out)
4501 {
4502 char *key, *value, *err;
4503 uint8_t prop_type;
4504
4505 while (ofputil_parse_key_value(arg, &key, &value)) {
4506 if (!parse_ed_prop_type(prop_class, key, &prop_type)) {
4507 return xasprintf("Invalid property: %s", key);
4508 }
4509 if (value == NULL) {
4510 return xasprintf("Missing the value for property: %s", key);
4511 }
4512 err = parse_ed_prop_value(prop_class, prop_type, value, out);
4513 if (err != NULL) {
4514 return err;
4515 }
4516 (*n_props)++;
4517 }
4518 return NULL;
4519 }
4520
4521 /* The string representation of the encap action is
4522 * encap(header_type(prop=<value>,tlv(<class>,<type>,<value>),...))
4523 */
4524
4525 static char * OVS_WARN_UNUSED_RESULT
4526 parse_ENCAP(char *arg, const struct ofpact_parse_params *pp)
4527 {
4528 *pp->usable_protocols &= OFPUTIL_P_OF13_UP;
4529
4530 struct ofpact_encap *encap;
4531 char *key, *value, *str;
4532 char *error = NULL;
4533 uint16_t prop_class;
4534 int n_props = 0;
4535
4536 encap = ofpact_put_ENCAP(pp->ofpacts);
4537 encap->hdr_size = 0;
4538 /* Parse encap header type. */
4539 str = arg;
4540 if (!ofputil_parse_key_value(&arg, &key, &value)) {
4541 return xasprintf("Missing encap hdr: %s", str);
4542 }
4543 if (!parse_encap_header(key, &encap->new_pkt_type)) {
4544 return xasprintf("Encap hdr not supported: %s", value);
4545 }
4546 if (!parse_ed_prop_class(key, &prop_class)) {
4547 return xasprintf("Invalid encap prop class: %s", key);
4548 }
4549 /* Parse encap properties. */
4550 error = parse_ed_props(prop_class, &value, &n_props, pp->ofpacts);
4551 if (error != NULL) {
4552 return error;
4553 }
4554 /* ofpbuf may have been re-allocated. */
4555 encap = pp->ofpacts->header;
4556 encap->n_props = n_props;
4557
4558 if (ofpbuf_oversized(pp->ofpacts)) {
4559 return xasprintf("input too big");
4560 }
4561
4562 ofpact_finish_ENCAP(pp->ofpacts, &encap);
4563 return NULL;
4564 }
4565
4566 static char *
4567 format_encap_pkt_type(const ovs_be32 pkt_type)
4568 {
4569 switch (ntohl(pkt_type)) {
4570 case PT_ETH:
4571 return "ethernet";
4572 case PT_NSH:
4573 return "nsh";
4574 default:
4575 return "UNKNOWN";
4576 }
4577 }
4578
4579 static void
4580 format_ed_props(struct ds *s, uint16_t n_props,
4581 const struct ofpact_ed_prop *prop)
4582 {
4583 const uint8_t *p = (uint8_t *) prop;
4584 int i;
4585
4586 if (n_props == 0) {
4587 return;
4588 }
4589 for (i = 0; i < n_props; i++) {
4590 format_ed_prop(s, prop);
4591 ds_put_char(s, ',');
4592 p += ROUND_UP(prop->len, 8);
4593 prop = ALIGNED_CAST(const struct ofpact_ed_prop *, p);
4594 }
4595 if (n_props > 0) {
4596 ds_chomp(s, ',');
4597 }
4598 }
4599
4600 static void
4601 format_ENCAP(const struct ofpact_encap *a,
4602 const struct ofpact_format_params *fp)
4603 {
4604 ds_put_format(fp->s, "%sencap(%s", colors.paren, colors.end);
4605 ds_put_format(fp->s, "%s", format_encap_pkt_type(a->new_pkt_type));
4606 if (a->n_props > 0) {
4607 ds_put_format(fp->s, "%s(%s", colors.paren, colors.end);
4608 format_ed_props(fp->s, a->n_props, a->props);
4609 ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
4610 }
4611 ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
4612 }
4613
4614 static enum ofperr
4615 check_ENCAP(const struct ofpact_encap *a, struct ofpact_check_params *cp)
4616 {
4617 struct flow *flow = &cp->match->flow;
4618 flow->packet_type = a->new_pkt_type;
4619 if (pt_ns(flow->packet_type) == OFPHTN_ETHERTYPE) {
4620 flow->dl_type = htons(pt_ns_type(flow->packet_type));
4621 }
4622 if (!is_ip_any(flow)) {
4623 flow->nw_proto = 0;
4624 }
4625 return 0;
4626 }
4627 \f
4628 /* Action structure for NXAST_DECAP */
4629 struct nx_action_decap {
4630 ovs_be16 type; /* OFPAT_VENDOR. */
4631 ovs_be16 len; /* Total size including any property TLVs. */
4632 ovs_be32 vendor; /* NX_VENDOR_ID. */
4633 ovs_be16 subtype; /* NXAST_DECAP. */
4634 uint8_t pad[2]; /* 2 bytes padding */
4635
4636 /* Packet type or result.
4637 *
4638 * The special value (0,0xFFFE) "Use next proto"
4639 * is used to request OVS to automatically set the new packet type based
4640 * on the decap'ed header's next protocol.
4641 */
4642 ovs_be32 new_pkt_type;
4643 };
4644 OFP_ASSERT(sizeof(struct nx_action_decap) == 16);
4645
4646 static enum ofperr
4647 decode_NXAST_RAW_DECAP(const struct nx_action_decap *nad,
4648 enum ofp_version ofp_version OVS_UNUSED,
4649 struct ofpbuf *ofpacts)
4650 {
4651 struct ofpact_decap * decap;
4652
4653 if (ntohs(nad->len) > sizeof *nad) {
4654 /* No properties supported yet. */
4655 return OFPERR_NXBAC_UNKNOWN_ED_PROP;
4656 }
4657
4658 decap = ofpact_put_DECAP(ofpacts);
4659 decap->ofpact.raw = NXAST_RAW_DECAP;
4660 decap->new_pkt_type = nad->new_pkt_type;
4661 return 0;
4662 }
4663
4664 static void
4665 encode_DECAP(const struct ofpact_decap *decap,
4666 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4667 {
4668 struct nx_action_decap *nad = put_NXAST_DECAP(out);
4669
4670 nad->len = htons(sizeof(struct nx_action_decap));
4671 nad->new_pkt_type = decap->new_pkt_type;
4672 }
4673
4674 static char * OVS_WARN_UNUSED_RESULT
4675 parse_DECAP(char *arg, const struct ofpact_parse_params *pp)
4676 {
4677 struct ofpact_decap *decap;
4678 char *key, *value, *pos;
4679 char *error = NULL;
4680 uint16_t ns, type;
4681
4682 decap = ofpact_put_DECAP(pp->ofpacts);
4683 /* Default next packet_type is PT_USE_NEXT_PROTO. */
4684 decap->new_pkt_type = htonl(PT_USE_NEXT_PROTO);
4685
4686 /* Parse decap packet_type if given. */
4687 if (ofputil_parse_key_value(&arg, &key, &value)) {
4688 if (strcmp(key, "packet_type") == 0) {
4689 pos = value;
4690 if (!ofputil_parse_key_value(&pos, &key, &value)
4691 || strcmp(key, "ns") != 0) {
4692 return xstrdup("Missing packet_type attribute ns");
4693 }
4694 error = str_to_u16(value, "ns", &ns);
4695 if (error) {
4696 return error;
4697 }
4698 if (ns >= OFPHTN_N_TYPES) {
4699 return xasprintf("Unsupported ns value: %"PRIu16, ns);
4700 }
4701 if (!ofputil_parse_key_value(&pos, &key, &value)
4702 || strcmp(key, "type") != 0) {
4703 return xstrdup("Missing packet_type attribute type");
4704 }
4705 error = str_to_u16(value, "type", &type);
4706 if (error) {
4707 return error;
4708 }
4709 decap->new_pkt_type = htonl(PACKET_TYPE(ns, type));
4710 } else {
4711 return xasprintf("Invalid decap argument: %s", key);
4712 }
4713 }
4714 return NULL;
4715 }
4716
4717 static void
4718 format_DECAP(const struct ofpact_decap *a,
4719 const struct ofpact_format_params *fp)
4720 {
4721 ds_put_format(fp->s, "%sdecap(%s", colors.paren, colors.end);
4722 if (a->new_pkt_type != htonl(PT_USE_NEXT_PROTO)) {
4723 ds_put_format(fp->s, "packet_type(ns=%"PRIu16",type=%#"PRIx16")",
4724 pt_ns(a->new_pkt_type),
4725 pt_ns_type(a->new_pkt_type));
4726 }
4727 ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
4728 }
4729
4730 static enum ofperr
4731 check_DECAP(const struct ofpact_decap *a OVS_UNUSED,
4732 struct ofpact_check_params *cp)
4733 {
4734 struct flow *flow = &cp->match->flow;
4735 if (flow->packet_type == htonl(PT_ETH)) {
4736 /* Adjust the packet_type to allow subsequent actions. */
4737 flow->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
4738 ntohs(flow->dl_type));
4739 } else {
4740 /* The actual packet_type is only known after decapsulation.
4741 * Do not allow subsequent actions that depend on packet headers. */
4742 flow->packet_type = htonl(PT_UNKNOWN);
4743 flow->dl_type = OVS_BE16_MAX;
4744 }
4745 return 0;
4746 }
4747
4748 /* Action dec_nsh_ttl */
4749
4750 static enum ofperr
4751 decode_NXAST_RAW_DEC_NSH_TTL(struct ofpbuf *out)
4752 {
4753 ofpact_put_DEC_NSH_TTL(out);
4754 return 0;
4755 }
4756
4757 static void
4758 encode_DEC_NSH_TTL(const struct ofpact_null *null OVS_UNUSED,
4759 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4760 {
4761 put_NXAST_DEC_NSH_TTL(out);
4762 }
4763
4764 static char * OVS_WARN_UNUSED_RESULT
4765 parse_DEC_NSH_TTL(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
4766 {
4767 ofpact_put_DEC_NSH_TTL(pp->ofpacts);
4768 return NULL;
4769 }
4770
4771 static void
4772 format_DEC_NSH_TTL(const struct ofpact_null *a OVS_UNUSED,
4773 const struct ofpact_format_params *fp)
4774 {
4775 ds_put_format(fp->s, "%sdec_nsh_ttl%s", colors.special, colors.end);
4776 }
4777
4778 static enum ofperr
4779 check_DEC_NSH_TTL(const struct ofpact_null *a OVS_UNUSED,
4780 struct ofpact_check_params *cp)
4781 {
4782 struct flow *flow = &cp->match->flow;
4783 if (flow->packet_type != htonl(PT_NSH) &&
4784 flow->dl_type != htons(ETH_TYPE_NSH)) {
4785 inconsistent_match(&cp->usable_protocols);
4786 }
4787 return 0;
4788 }
4789 \f
4790 /* Action structures for NXAST_RESUBMIT, NXAST_RESUBMIT_TABLE, and
4791 * NXAST_RESUBMIT_TABLE_CT.
4792 *
4793 * These actions search one of the switch's flow tables:
4794 *
4795 * - For NXAST_RESUBMIT_TABLE and NXAST_RESUBMIT_TABLE_CT, if the
4796 * 'table' member is not 255, then it specifies the table to search.
4797 *
4798 * - Otherwise (for NXAST_RESUBMIT_TABLE or NXAST_RESUBMIT_TABLE_CT with a
4799 * 'table' of 255, or for NXAST_RESUBMIT regardless of 'table'), it
4800 * searches the current flow table, that is, the OpenFlow flow table that
4801 * contains the flow from which this action was obtained. If this action
4802 * did not come from a flow table (e.g. it came from an OFPT_PACKET_OUT
4803 * message), then table 0 is the current table.
4804 *
4805 * The flow table lookup uses a flow that may be slightly modified from the
4806 * original lookup:
4807 *
4808 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
4809 * is used as the flow's in_port.
4810 *
4811 * - For NXAST_RESUBMIT_TABLE and NXAST_RESUBMIT_TABLE_CT, if the 'in_port'
4812 * member is not OFPP_IN_PORT, then its value is used as the flow's
4813 * in_port. Otherwise, the original in_port is used.
4814 *
4815 * - For NXAST_RESUBMIT_TABLE_CT the Conntrack 5-tuple fields are used as
4816 * the packets IP header fields during the lookup.
4817 *
4818 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
4819 * resubmit action, then the flow is updated with the new values.
4820 *
4821 * Following the lookup, the original in_port is restored.
4822 *
4823 * If the modified flow matched in the flow table, then the corresponding
4824 * actions are executed. Afterward, actions following the resubmit in the
4825 * original set of actions, if any, are executed; any changes made to the
4826 * packet (e.g. changes to VLAN) by secondary actions persist when those
4827 * actions are executed, although the original in_port is restored.
4828 *
4829 * Resubmit actions may be used any number of times within a set of actions.
4830 *
4831 * Resubmit actions may nest. To prevent infinite loops and excessive resource
4832 * use, the implementation may limit nesting depth and the total number of
4833 * resubmits:
4834 *
4835 * - Open vSwitch 1.0.1 and earlier did not support recursion.
4836 *
4837 * - Open vSwitch 1.0.2 and 1.0.3 limited recursion to 8 levels.
4838 *
4839 * - Open vSwitch 1.1 and 1.2 limited recursion to 16 levels.
4840 *
4841 * - Open vSwitch 1.2 through 1.8 limited recursion to 32 levels.
4842 *
4843 * - Open vSwitch 1.9 through 2.0 limited recursion to 64 levels.
4844 *
4845 * - Open vSwitch 2.1 through 2.5 limited recursion to 64 levels and impose
4846 * a total limit of 4,096 resubmits per flow translation (earlier versions
4847 * did not impose any total limit).
4848 *
4849 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE and
4850 * NXAST_RESUBMIT_TABLE_CT require 'pad' to be all-bits-zero.
4851 *
4852 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
4853 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE. Open vSwitch before
4854 * 2.8.0 did not support NXAST_RESUBMIT_TABLE_CT.
4855 */
4856 struct nx_action_resubmit {
4857 ovs_be16 type; /* OFPAT_VENDOR. */
4858 ovs_be16 len; /* Length is 16. */
4859 ovs_be32 vendor; /* NX_VENDOR_ID. */
4860 ovs_be16 subtype; /* NXAST_RESUBMIT. */
4861 ovs_be16 in_port; /* New in_port for checking flow table. */
4862 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
4863 uint8_t pad[3];
4864 };
4865 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
4866
4867 static enum ofperr
4868 decode_NXAST_RAW_RESUBMIT(uint16_t port,
4869 enum ofp_version ofp_version OVS_UNUSED,
4870 struct ofpbuf *out)
4871 {
4872 struct ofpact_resubmit *resubmit;
4873
4874 resubmit = ofpact_put_RESUBMIT(out);
4875 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
4876 resubmit->in_port = u16_to_ofp(port);
4877 resubmit->table_id = 0xff;
4878 return 0;
4879 }
4880
4881 static enum ofperr
4882 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
4883 enum ofp_version ofp_version OVS_UNUSED,
4884 struct ofpbuf *out)
4885 {
4886 struct ofpact_resubmit *resubmit;
4887
4888 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
4889 return OFPERR_OFPBAC_BAD_ARGUMENT;
4890 }
4891
4892 resubmit = ofpact_put_RESUBMIT(out);
4893 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
4894 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
4895 resubmit->table_id = nar->table;
4896 return 0;
4897 }
4898
4899 static enum ofperr
4900 decode_NXAST_RAW_RESUBMIT_TABLE_CT(const struct nx_action_resubmit *nar,
4901 enum ofp_version ofp_version OVS_UNUSED,
4902 struct ofpbuf *out)
4903 {
4904 enum ofperr error = decode_NXAST_RAW_RESUBMIT_TABLE(nar, ofp_version, out);
4905 if (error) {
4906 return error;
4907 }
4908 struct ofpact_resubmit *resubmit = out->header;
4909 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE_CT;
4910 resubmit->with_ct_orig = true;
4911 return 0;
4912 }
4913
4914 static void
4915 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
4916 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4917 {
4918 uint16_t in_port = ofp_to_u16(resubmit->in_port);
4919
4920 if (resubmit->table_id == 0xff
4921 && resubmit->ofpact.raw == NXAST_RAW_RESUBMIT) {
4922 put_NXAST_RESUBMIT(out, in_port);
4923 } else {
4924 struct nx_action_resubmit *nar;
4925 nar = resubmit->with_ct_orig
4926 ? put_NXAST_RESUBMIT_TABLE_CT(out) : put_NXAST_RESUBMIT_TABLE(out);
4927 nar->table = resubmit->table_id;
4928 nar->in_port = htons(in_port);
4929 }
4930 }
4931
4932 static char * OVS_WARN_UNUSED_RESULT
4933 parse_RESUBMIT(char *arg, const struct ofpact_parse_params *pp)
4934 {
4935 struct ofpact_resubmit *resubmit;
4936 char *in_port_s, *table_s, *ct_s;
4937
4938 resubmit = ofpact_put_RESUBMIT(pp->ofpacts);
4939
4940 in_port_s = strsep(&arg, ",");
4941 if (in_port_s && in_port_s[0]) {
4942 if (!ofputil_port_from_string(in_port_s, pp->port_map,
4943 &resubmit->in_port)) {
4944 return xasprintf("%s: resubmit to unknown port", in_port_s);
4945 }
4946 } else {
4947 resubmit->in_port = OFPP_IN_PORT;
4948 }
4949
4950 table_s = strsep(&arg, ",");
4951 if (table_s && table_s[0]) {
4952 if (!ofputil_table_from_string(table_s, pp->table_map,
4953 &resubmit->table_id)) {
4954 return xasprintf("%s: resubmit to unknown table", table_s);
4955 }
4956 } else {
4957 resubmit->table_id = 255;
4958 }
4959
4960 ct_s = strsep(&arg, ",");
4961 if (ct_s && ct_s[0]) {
4962 if (strcmp(ct_s, "ct")) {
4963 return xasprintf("%s: unknown parameter", ct_s);
4964 }
4965 resubmit->with_ct_orig = true;
4966 } else {
4967 resubmit->with_ct_orig = false;
4968 }
4969
4970 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
4971 return xstrdup("at least one \"in_port\" or \"table\" must be "
4972 "specified on resubmit");
4973 }
4974 return NULL;
4975 }
4976
4977 static void
4978 format_RESUBMIT(const struct ofpact_resubmit *a,
4979 const struct ofpact_format_params *fp)
4980 {
4981 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
4982 ds_put_format(fp->s, "%sresubmit:%s", colors.special, colors.end);
4983 ofputil_format_port(a->in_port, fp->port_map, fp->s);
4984 } else {
4985 ds_put_format(fp->s, "%sresubmit(%s", colors.paren, colors.end);
4986 if (a->in_port != OFPP_IN_PORT) {
4987 ofputil_format_port(a->in_port, fp->port_map, fp->s);
4988 }
4989 ds_put_char(fp->s, ',');
4990 if (a->table_id != 255) {
4991 ofputil_format_table(a->table_id, fp->table_map, fp->s);
4992 }
4993 if (a->with_ct_orig) {
4994 ds_put_cstr(fp->s, ",ct");
4995 }
4996 ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
4997 }
4998 }
4999
5000 static enum ofperr
5001 check_RESUBMIT(const struct ofpact_resubmit *a,
5002 const struct ofpact_check_params *cp)
5003 {
5004 if (a->with_ct_orig && !is_ct_valid(&cp->match->flow, &cp->match->wc,
5005 NULL)) {
5006 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
5007 }
5008 return 0;
5009 }
5010 \f
5011 /* Action structure for NXAST_LEARN and NXAST_LEARN2.
5012 *
5013 * This action adds or modifies a flow in an OpenFlow table, similar to
5014 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
5015 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
5016 * flow's match criteria and actions are built by applying each of the series
5017 * of flow_mod_spec elements included as part of the action.
5018 *
5019 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
5020 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
5021 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
5022 * bits from a source to a destination. In this case, the header contains
5023 * multiple fields:
5024 *
5025 * 15 14 13 12 11 10 0
5026 * +------+---+------+---------------------------------+
5027 * | 0 |src| dst | n_bits |
5028 * +------+---+------+---------------------------------+
5029 *
5030 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
5031 * following table summarizes the meaning of each possible combination.
5032 * Details follow the table:
5033 *
5034 * src dst meaning
5035 * --- --- ----------------------------------------------------------
5036 * 0 0 Add match criteria based on value in a field.
5037 * 1 0 Add match criteria based on an immediate value.
5038 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
5039 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
5040 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
5041 * All other combinations are undefined and not allowed.
5042 *
5043 * The flow_mod_spec header is followed by a source specification and a
5044 * destination specification. The format and meaning of the source
5045 * specification depends on 'src':
5046 *
5047 * - If 'src' is 0, the source bits are taken from a field in the flow to
5048 * which this action is attached. (This should be a wildcarded field. If
5049 * its value is fully specified then the source bits being copied have
5050 * constant values.)
5051 *
5052 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
5053 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
5054 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
5055 * 'field' and 'ofs' are subject to the same restrictions as the source
5056 * field in NXAST_REG_MOVE.
5057 *
5058 * - If 'src' is 1, the source bits are a constant value. The source
5059 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
5060 * number in network order, the source bits are the 'n_bits'
5061 * least-significant bits. The switch will report an error if other bits
5062 * in the constant are nonzero.
5063 *
5064 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
5065 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
5066 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
5067 * meaning of the flow_mod_spec depends on 'dst':
5068 *
5069 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
5070 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
5071 * packet equal the source bits. 'field' may be any nxm_header with
5072 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
5073 *
5074 * Order is significant. Earlier flow_mod_specs must satisfy any
5075 * prerequisites for matching fields specified later, by copying constant
5076 * values into prerequisite fields.
5077 *
5078 * The switch will reject flow_mod_specs that do not satisfy NXM masking
5079 * restrictions.
5080 *
5081 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
5082 * the new flow. The new flow copies the source bits into
5083 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
5084 * flow_mod_specs.
5085 *
5086 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
5087 * greater than 64 yields multiple NXAST_REG_LOAD actions.
5088 *
5089 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
5090 * empty. It has the following meaning:
5091 *
5092 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
5093 * The new flow outputs to the OpenFlow port specified by the source field.
5094 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
5095 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
5096 * may not be used.
5097 *
5098 * Resource Management
5099 * -------------------
5100 *
5101 * A switch has a finite amount of flow table space available for learning.
5102 * When this space is exhausted, no new learning table entries will be learned
5103 * until some existing flow table entries expire. The controller should be
5104 * prepared to handle this by flooding (which can be implemented as a
5105 * low-priority flow).
5106 *
5107 * If a learned flow matches a single TCP stream with a relatively long
5108 * timeout, one may make the best of resource constraints by setting
5109 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
5110 * both, to shorter timeouts. When either of these is specified as a nonzero
5111 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
5112 * the learned flow.
5113 *
5114 * Examples
5115 * --------
5116 *
5117 * The following examples give a prose description of the flow_mod_specs along
5118 * with informal notation for how those would be represented and a hex dump of
5119 * the bytes that would be required.
5120 *
5121 * These examples could work with various nx_action_learn parameters. Typical
5122 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
5123 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
5124 *
5125 * 1. Learn input port based on the source MAC, with lookup into
5126 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
5127 *
5128 * Match on in_port=99:
5129 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
5130 * ovs_be16(99), 00 63
5131 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
5132 *
5133 * Match Ethernet destination on Ethernet source from packet:
5134 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
5135 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
5136 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
5137 *
5138 * Set NXM_NX_REG1[16:31] to the packet's input port:
5139 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
5140 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
5141 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
5142 *
5143 * Given a packet that arrived on port A with Ethernet source address B,
5144 * this would set up the flow "in_port=99, dl_dst=B,
5145 * actions=load:A->NXM_NX_REG1[16..31]".
5146 *
5147 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
5148 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
5149 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
5150 *
5151 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
5152 * into NXM_NX_REG1[16:31]:
5153 *
5154 * Match on same VLAN ID as packet:
5155 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
5156 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
5157 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
5158 *
5159 * Match Ethernet destination on Ethernet source from packet:
5160 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
5161 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
5162 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
5163 *
5164 * Output to the packet's input port:
5165 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
5166 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
5167 *
5168 * Given a packet that arrived on port A with Ethernet source address B in
5169 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
5170 * actions=output:A".
5171 *
5172 * In syntax accepted by ovs-ofctl, this action is:
5173 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
5174 * output:NXM_OF_IN_PORT[])
5175 *
5176 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
5177 * 10-second MAC expiration time to make it easier to see what's going on
5178 *
5179 * ovs-vsctl del-controller br0
5180 * ovs-ofctl del-flows br0
5181 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
5182 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
5183 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
5184 output:NXM_OF_IN_PORT[]), resubmit(,1)"
5185 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
5186 *
5187 * You can then dump the MAC learning table with:
5188 *
5189 * ovs-ofctl dump-flows br0 table=1
5190 *
5191 * Usage Advice
5192 * ------------
5193 *
5194 * For best performance, segregate learned flows into a table that is not used
5195 * for any other flows except possibly for a lowest-priority "catch-all" flow
5196 * (a flow with no match criteria). If different learning actions specify
5197 * different match criteria, use different tables for the learned flows.
5198 *
5199 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
5200 * These timeouts apply to the flow that is added, which means that a flow with
5201 * an idle timeout will expire when no traffic has been sent *to* the learned
5202 * address. This is not usually the intent in MAC learning; instead, we want
5203 * the MAC learn entry to expire when no traffic has been sent *from* the
5204 * learned address. Use a hard timeout for that.
5205 *
5206 *
5207 * Visibility of Changes
5208 * ---------------------
5209 *
5210 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
5211 * flow translation are visible to flow table lookups made later in the flow
5212 * translation. This means that, in the example above, a MAC learned by the
5213 * learn action in table 0 would be found in table 1 (if the packet being
5214 * processed had the same source and destination MAC address).
5215 *
5216 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
5217 * modify a flow) by a "learn" action are visible only for later flow
5218 * translations, not for later lookups within the same flow translation. In
5219 * the MAC learning example, a MAC learned by the learn action in table 0 would
5220 * not be found in table 1 if the flow translation would resubmit to table 1
5221 * after the processing of the learn action, meaning that if this MAC had not
5222 * been learned before then the packet would be flooded. */
5223 struct nx_action_learn {
5224 ovs_be16 type; /* OFPAT_VENDOR. */
5225 ovs_be16 len; /* At least 24. */
5226 ovs_be32 vendor; /* NX_VENDOR_ID. */
5227 ovs_be16 subtype; /* NXAST_LEARN. */
5228 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
5229 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
5230 ovs_be16 priority; /* Priority level of flow entry. */
5231 ovs_be64 cookie; /* Cookie for new flow. */
5232 ovs_be16 flags; /* NX_LEARN_F_*. */
5233 uint8_t table_id; /* Table to insert flow entry. */
5234 uint8_t pad; /* Must be zero. */
5235 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
5236 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
5237 /* Followed by a sequence of flow_mod_spec elements, as described above,
5238 * until the end of the action is reached. */
5239 };
5240 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
5241
5242 struct nx_action_learn2 {
5243 struct nx_action_learn up; /* The wire format includes nx_action_learn. */
5244 ovs_be32 limit; /* Maximum number of learned flows.
5245 * 0 indicates unlimited. */
5246
5247 /* Where to store the result. */
5248 ovs_be16 result_dst_ofs; /* Starting bit offset in destination. */
5249
5250 ovs_be16 pad2; /* Must be zero. */
5251 /* Followed by:
5252 * - OXM/NXM header for destination field (4 or 8 bytes),
5253 * if NX_LEARN_F_WRITE_RESULT is set in 'flags'
5254 * - a sequence of flow_mod_spec elements, as described above,
5255 * until the end of the action is reached. */
5256 };
5257 OFP_ASSERT(sizeof(struct nx_action_learn2) == 40);
5258
5259 static ovs_be16
5260 get_be16(const void **pp)
5261 {
5262 const ovs_be16 *p = *pp;
5263 ovs_be16 value = *p;
5264 *pp = p + 1;
5265 return value;
5266 }
5267
5268 static ovs_be32
5269 get_be32(const void **pp)
5270 {
5271 const ovs_be32 *p = *pp;
5272 ovs_be32 value = get_unaligned_be32(p);
5273 *pp = p + 1;
5274 return value;
5275 }
5276
5277 static enum ofperr
5278 get_subfield(int n_bits, const void **p, struct mf_subfield *sf,
5279 const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap)
5280 {
5281 enum ofperr error;
5282
5283 error = mf_vl_mff_mf_from_nxm_header(ntohl(get_be32(p)), vl_mff_map,
5284 &sf->field, tlv_bitmap);
5285 sf->ofs = ntohs(get_be16(p));
5286 sf->n_bits = n_bits;
5287 return error;
5288 }
5289
5290 static unsigned int
5291 learn_min_len(uint16_t header)
5292 {
5293 int n_bits = header & NX_LEARN_N_BITS_MASK;
5294 int src_type = header & NX_LEARN_SRC_MASK;
5295 int dst_type = header & NX_LEARN_DST_MASK;
5296 unsigned int min_len;
5297
5298 min_len = 0;
5299 if (src_type == NX_LEARN_SRC_FIELD) {
5300 min_len += sizeof(ovs_be32); /* src_field */
5301 min_len += sizeof(ovs_be16); /* src_ofs */
5302 } else {
5303 min_len += 2 * DIV_ROUND_UP(n_bits, 16);
5304 }
5305 if (dst_type == NX_LEARN_DST_MATCH ||
5306 dst_type == NX_LEARN_DST_LOAD) {
5307 min_len += sizeof(ovs_be32); /* dst_field */
5308 min_len += sizeof(ovs_be16); /* dst_ofs */
5309 }
5310 return min_len;
5311 }
5312
5313 static enum ofperr
5314 decode_LEARN_common(const struct nx_action_learn *nal,
5315 enum ofp_raw_action_type raw,
5316 struct ofpact_learn *learn)
5317 {
5318 if (nal->pad) {
5319 return OFPERR_OFPBAC_BAD_ARGUMENT;
5320 }
5321
5322 learn->ofpact.raw = raw;
5323 learn->idle_timeout = ntohs(nal->idle_timeout);
5324 learn->hard_timeout = ntohs(nal->hard_timeout);
5325 learn->priority = ntohs(nal->priority);
5326 learn->cookie = nal->cookie;
5327 learn->table_id = nal->table_id;
5328 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
5329 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
5330 learn->flags = ntohs(nal->flags);
5331
5332 if (learn->table_id == 0xff) {
5333 return OFPERR_OFPBAC_BAD_ARGUMENT;
5334 }
5335
5336 return 0;
5337 }
5338
5339 static enum ofperr
5340 decode_LEARN_specs(const void *p, const void *end,
5341 const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap,
5342 struct ofpbuf *ofpacts)
5343 {
5344 struct ofpact_learn *learn = ofpacts->header;
5345
5346 while (p != end) {
5347 struct ofpact_learn_spec *spec;
5348 uint16_t header = ntohs(get_be16(&p));
5349
5350 if (!header) {
5351 break;
5352 }
5353
5354 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
5355 learn = ofpacts->header;
5356
5357 spec->src_type = header & NX_LEARN_SRC_MASK;
5358 spec->dst_type = header & NX_LEARN_DST_MASK;
5359 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
5360
5361 /* Check for valid src and dst type combination. */
5362 if (spec->dst_type == NX_LEARN_DST_MATCH ||
5363 spec->dst_type == NX_LEARN_DST_LOAD ||
5364 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
5365 spec->src_type == NX_LEARN_SRC_FIELD)) {
5366 /* OK. */
5367 } else {
5368 return OFPERR_OFPBAC_BAD_ARGUMENT;
5369 }
5370
5371 /* Check that the arguments don't overrun the end of the action. */
5372 if ((char *) end - (char *) p < learn_min_len(header)) {
5373 return OFPERR_OFPBAC_BAD_LEN;
5374 }
5375
5376 /* Get the source. */
5377 const uint8_t *imm = NULL;
5378 unsigned int imm_bytes = 0;
5379 enum ofperr error;
5380 if (spec->src_type == NX_LEARN_SRC_FIELD) {
5381 error = get_subfield(spec->n_bits, &p, &spec->src, vl_mff_map,
5382 tlv_bitmap);
5383 if (error) {
5384 return error;
5385 }
5386 } else {
5387 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
5388 p = (const uint8_t *) p + p_bytes;
5389
5390 imm_bytes = DIV_ROUND_UP(spec->n_bits, 8);
5391 imm = (const uint8_t *) p - imm_bytes;
5392 }
5393
5394 /* Get the destination. */
5395 if (spec->dst_type == NX_LEARN_DST_MATCH ||
5396 spec->dst_type == NX_LEARN_DST_LOAD) {
5397 error = get_subfield(spec->n_bits, &p, &spec->dst, vl_mff_map,
5398 tlv_bitmap);
5399 if (error) {
5400 return error;
5401 }
5402 }
5403
5404 if (imm) {
5405 uint8_t *src_imm = ofpbuf_put_zeros(ofpacts,
5406 OFPACT_ALIGN(imm_bytes));
5407 memcpy(src_imm, imm, imm_bytes);
5408
5409 learn = ofpacts->header;
5410 }
5411 }
5412 ofpact_finish_LEARN(ofpacts, &learn);
5413
5414 if (!is_all_zeros(p, (char *) end - (char *) p)) {
5415 return OFPERR_OFPBAC_BAD_ARGUMENT;
5416 }
5417
5418 return 0;
5419 }
5420
5421 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
5422 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
5423 static enum ofperr
5424 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
5425 enum ofp_version ofp_version OVS_UNUSED,
5426 const struct vl_mff_map *vl_mff_map,
5427 uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
5428 {
5429 struct ofpact_learn *learn;
5430 enum ofperr error;
5431
5432 learn = ofpact_put_LEARN(ofpacts);
5433
5434 error = decode_LEARN_common(nal, NXAST_RAW_LEARN, learn);
5435 if (error) {
5436 return error;
5437 }
5438
5439 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
5440 NX_LEARN_F_DELETE_LEARNED)) {
5441 return OFPERR_OFPBAC_BAD_ARGUMENT;
5442 }
5443
5444 return decode_LEARN_specs(nal + 1, (char *) nal + ntohs(nal->len),
5445 vl_mff_map, tlv_bitmap, ofpacts);
5446 }
5447
5448 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
5449 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
5450 static enum ofperr
5451 decode_NXAST_RAW_LEARN2(const struct nx_action_learn2 *nal,
5452 enum ofp_version ofp_version OVS_UNUSED,
5453 const struct vl_mff_map *vl_mff_map,
5454 uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
5455 {
5456 struct ofpbuf b = ofpbuf_const_initializer(nal, ntohs(nal->up.len));
5457 struct ofpact_learn *learn;
5458 enum ofperr error;
5459
5460 if (nal->pad2) {
5461 return OFPERR_NXBAC_MUST_BE_ZERO;
5462 }
5463
5464 learn = ofpact_put_LEARN(ofpacts);
5465 error = decode_LEARN_common(&nal->up, NXAST_RAW_LEARN2, learn);
5466 if (error) {
5467 return error;
5468 }
5469
5470 learn->limit = ntohl(nal->limit);
5471
5472 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
5473 NX_LEARN_F_DELETE_LEARNED |
5474 NX_LEARN_F_WRITE_RESULT)) {
5475 return OFPERR_OFPBAC_BAD_ARGUMENT;
5476 }
5477
5478 ofpbuf_pull(&b, sizeof *nal);
5479
5480 if (learn->flags & NX_LEARN_F_WRITE_RESULT) {
5481 error = nx_pull_header(&b, vl_mff_map, &learn->result_dst.field, NULL);
5482 if (error) {
5483 return error;
5484 }
5485 if (!learn->result_dst.field->writable) {
5486 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
5487 }
5488 learn->result_dst.ofs = ntohs(nal->result_dst_ofs);
5489 learn->result_dst.n_bits = 1;
5490 } else if (nal->result_dst_ofs) {
5491 return OFPERR_OFPBAC_BAD_ARGUMENT;
5492 }
5493
5494 return decode_LEARN_specs(b.data, (char *) nal + ntohs(nal->up.len),
5495 vl_mff_map, tlv_bitmap, ofpacts);
5496 }
5497
5498 static void
5499 put_be16(struct ofpbuf *b, ovs_be16 x)
5500 {
5501 ofpbuf_put(b, &x, sizeof x);
5502 }
5503
5504 static void
5505 put_be32(struct ofpbuf *b, ovs_be32 x)
5506 {
5507 ofpbuf_put(b, &x, sizeof x);
5508 }
5509
5510 static void
5511 put_u16(struct ofpbuf *b, uint16_t x)
5512 {
5513 put_be16(b, htons(x));
5514 }
5515
5516 static void
5517 put_u32(struct ofpbuf *b, uint32_t x)
5518 {
5519 put_be32(b, htonl(x));
5520 }
5521
5522 static void
5523 encode_LEARN(const struct ofpact_learn *learn,
5524 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
5525 {
5526 const struct ofpact_learn_spec *spec;
5527 struct nx_action_learn *nal;
5528 size_t start_ofs;
5529
5530 start_ofs = out->size;
5531
5532 if (learn->ofpact.raw == NXAST_RAW_LEARN2
5533 || learn->limit != 0
5534 || learn->flags & NX_LEARN_F_WRITE_RESULT) {
5535 struct nx_action_learn2 *nal2;
5536
5537 nal2 = put_NXAST_LEARN2(out);
5538 nal2->limit = htonl(learn->limit);
5539 nal2->result_dst_ofs = htons(learn->result_dst.ofs);
5540 nal = &nal2->up;
5541 } else {
5542 nal = put_NXAST_LEARN(out);
5543 }
5544 nal->idle_timeout = htons(learn->idle_timeout);
5545 nal->hard_timeout = htons(learn->hard_timeout);
5546 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
5547 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
5548 nal->priority = htons(learn->priority);
5549 nal->cookie = learn->cookie;
5550 nal->flags = htons(learn->flags);
5551 nal->table_id = learn->table_id;
5552
5553 if (learn->flags & NX_LEARN_F_WRITE_RESULT) {
5554 nx_put_header(out, learn->result_dst.field->id, 0, false);
5555 }
5556
5557 OFPACT_LEARN_SPEC_FOR_EACH (spec, learn) {
5558 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
5559
5560 if (spec->src_type == NX_LEARN_SRC_FIELD) {
5561 put_u32(out, nxm_header_from_mff(spec->src.field));
5562 put_u16(out, spec->src.ofs);
5563 } else {
5564 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
5565 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
5566 unsigned int n_bytes = DIV_ROUND_UP(spec->n_bits, 8);
5567
5568 memcpy(bits + n_dst_bytes - n_bytes, ofpact_learn_spec_imm(spec),
5569 n_bytes);
5570 }
5571
5572 if (spec->dst_type == NX_LEARN_DST_MATCH ||
5573 spec->dst_type == NX_LEARN_DST_LOAD) {
5574 put_u32(out, nxm_header_from_mff(spec->dst.field));
5575 put_u16(out, spec->dst.ofs);
5576 }
5577 }
5578
5579 pad_ofpat(out, start_ofs);
5580 }
5581
5582 static char * OVS_WARN_UNUSED_RESULT
5583 parse_LEARN(char *arg, const struct ofpact_parse_params *pp)
5584 {
5585 return learn_parse(arg, pp->port_map, pp->table_map, pp->ofpacts);
5586 }
5587
5588 static void
5589 format_LEARN(const struct ofpact_learn *a,
5590 const struct ofpact_format_params *fp)
5591 {
5592 learn_format(a, fp->port_map, fp->table_map, fp->s);
5593 }
5594
5595 static enum ofperr
5596 check_LEARN(const struct ofpact_learn *a,
5597 const struct ofpact_check_params *cp)
5598 {
5599 return learn_check(a, cp->match);
5600 }
5601 \f
5602 /* Action structure for NXAST_CONJUNCTION. */
5603 struct nx_action_conjunction {
5604 ovs_be16 type; /* OFPAT_VENDOR. */
5605 ovs_be16 len; /* At least 16. */
5606 ovs_be32 vendor; /* NX_VENDOR_ID. */
5607 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
5608 uint8_t clause;
5609 uint8_t n_clauses;
5610 ovs_be32 id;
5611 };
5612 OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
5613
5614 static void
5615 add_conjunction(struct ofpbuf *out,
5616 uint32_t id, uint8_t clause, uint8_t n_clauses)
5617 {
5618 struct ofpact_conjunction *oc;
5619
5620 oc = ofpact_put_CONJUNCTION(out);
5621 oc->id = id;
5622 oc->clause = clause;
5623 oc->n_clauses = n_clauses;
5624 }
5625
5626 static enum ofperr
5627 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
5628 enum ofp_version ofp_version OVS_UNUSED,
5629 struct ofpbuf *out)
5630 {
5631 if (nac->n_clauses < 2 || nac->n_clauses > 64
5632 || nac->clause >= nac->n_clauses) {
5633 return OFPERR_NXBAC_BAD_CONJUNCTION;
5634 } else {
5635 add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
5636 return 0;
5637 }
5638 }
5639
5640 static void
5641 encode_CONJUNCTION(const struct ofpact_conjunction *oc,
5642 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
5643 {
5644 struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
5645 nac->clause = oc->clause;
5646 nac->n_clauses = oc->n_clauses;
5647 nac->id = htonl(oc->id);
5648 }
5649
5650 static void
5651 format_CONJUNCTION(const struct ofpact_conjunction *oc,
5652 const struct ofpact_format_params *fp)
5653 {
5654 ds_put_format(fp->s, "%sconjunction(%s%"PRIu32",%d/%"PRIu8"%s)%s",
5655 colors.paren, colors.end,
5656 oc->id, oc->clause + 1, oc->n_clauses,
5657 colors.paren, colors.end);
5658 }
5659
5660 static char * OVS_WARN_UNUSED_RESULT
5661 parse_CONJUNCTION(const char *arg, const struct ofpact_parse_params *pp)
5662 {
5663 uint8_t n_clauses;
5664 uint8_t clause;
5665 uint32_t id;
5666 int n;
5667
5668 if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
5669 &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
5670 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
5671 }
5672
5673 if (n_clauses < 2) {
5674 return xstrdup("conjunction must have at least 2 clauses");
5675 } else if (n_clauses > 64) {
5676 return xstrdup("conjunction must have at most 64 clauses");
5677 } else if (clause < 1) {
5678 return xstrdup("clause index must be positive");
5679 } else if (clause > n_clauses) {
5680 return xstrdup("clause index must be less than or equal to "
5681 "number of clauses");
5682 }
5683
5684 add_conjunction(pp->ofpacts, id, clause - 1, n_clauses);
5685 return NULL;
5686 }
5687
5688 static enum ofperr
5689 check_CONJUNCTION(const struct ofpact_conjunction *a OVS_UNUSED,
5690 const struct ofpact_check_params *cp OVS_UNUSED)
5691 {
5692 return 0;
5693 }
5694 \f
5695 /* Action structure for NXAST_MULTIPATH.
5696 *
5697 * This action performs the following steps in sequence:
5698 *
5699 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
5700 * Refer to the definition of "enum nx_mp_fields" for details.
5701 *
5702 * The 'basis' value is used as a universal hash parameter, that is,
5703 * different values of 'basis' yield different hash functions. The
5704 * particular universal hash function used is implementation-defined.
5705 *
5706 * The hashed fields' values are drawn from the current state of the
5707 * flow, including all modifications that have been made by actions up to
5708 * this point.
5709 *
5710 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
5711 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
5712 * for details.
5713 *
5714 * The output of the algorithm is 'link', an unsigned integer less than
5715 * or equal to 'max_link'.
5716 *
5717 * Some algorithms use 'arg' as an additional argument.
5718 *
5719 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
5720 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
5721 * action.
5722 *
5723 * The switch will reject actions that have an unknown 'fields', or an unknown
5724 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
5725 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
5726 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
5727 */
5728 struct nx_action_multipath {
5729 ovs_be16 type; /* OFPAT_VENDOR. */
5730 ovs_be16 len; /* Length is 32. */
5731 ovs_be32 vendor; /* NX_VENDOR_ID. */
5732 ovs_be16 subtype; /* NXAST_MULTIPATH. */
5733
5734 /* What fields to hash and how. */
5735 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
5736 ovs_be16 basis; /* Universal hash parameter. */
5737 ovs_be16 pad0;
5738
5739 /* Multipath link choice algorithm to apply to hash value. */
5740 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
5741 ovs_be16 max_link; /* Number of output links, minus 1. */
5742 ovs_be32 arg; /* Algorithm-specific argument. */
5743 ovs_be16 pad1;
5744
5745 /* Where to store the result. */
5746 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
5747 ovs_be32 dst; /* Destination. */
5748 };
5749 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
5750
5751 static enum ofperr
5752 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
5753 enum ofp_version ofp_version OVS_UNUSED,
5754 const struct vl_mff_map *vl_mff_map,
5755 uint64_t *tlv_bitmap, struct ofpbuf *out)
5756 {
5757 uint32_t n_links = ntohs(nam->max_link) + 1;
5758 size_t min_n_bits = log_2_ceil(n_links);
5759 struct ofpact_multipath *mp;
5760 enum ofperr error;
5761
5762 mp = ofpact_put_MULTIPATH(out);
5763 mp->fields = ntohs(nam->fields);
5764 mp->basis = ntohs(nam->basis);
5765 mp->algorithm = ntohs(nam->algorithm);
5766 mp->max_link = ntohs(nam->max_link);
5767 mp->arg = ntohl(nam->arg);
5768 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
5769 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
5770 error = mf_vl_mff_mf_from_nxm_header(ntohl(nam->dst), vl_mff_map,
5771 &mp->dst.field, tlv_bitmap);
5772 if (error) {
5773 return error;
5774 }
5775
5776 if (!flow_hash_fields_valid(mp->fields)) {
5777 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
5778 return OFPERR_OFPBAC_BAD_ARGUMENT;
5779 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
5780 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
5781 && mp->algorithm != NX_MP_ALG_HRW
5782 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
5783 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
5784 return OFPERR_OFPBAC_BAD_ARGUMENT;
5785 } else if (mp->dst.n_bits < min_n_bits) {
5786 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
5787 "%"PRIu32" links", min_n_bits, n_links);
5788 return OFPERR_OFPBAC_BAD_ARGUMENT;
5789 }
5790
5791 return multipath_check(mp, NULL);
5792 }
5793
5794 static void
5795 encode_MULTIPATH(const struct ofpact_multipath *mp,
5796 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
5797 {
5798 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
5799
5800 nam->fields = htons(mp->fields);
5801 nam->basis = htons(mp->basis);
5802 nam->algorithm = htons(mp->algorithm);
5803 nam->max_link = htons(mp->max_link);
5804 nam->arg = htonl(mp->arg);
5805 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
5806 nam->dst = htonl(nxm_header_from_mff(mp->dst.field));
5807 }
5808
5809 static char * OVS_WARN_UNUSED_RESULT
5810 parse_MULTIPATH(const char *arg, const struct ofpact_parse_params *pp)
5811 {
5812 return multipath_parse(ofpact_put_MULTIPATH(pp->ofpacts), arg);
5813 }
5814
5815 static void
5816 format_MULTIPATH(const struct ofpact_multipath *a,
5817 const struct ofpact_format_params *fp)
5818 {
5819 multipath_format(a, fp->s);
5820 }
5821
5822 static enum ofperr
5823 check_MULTIPATH(const struct ofpact_multipath *a,
5824 const struct ofpact_check_params *cp)
5825 {
5826 return multipath_check(a, cp->match);
5827 }
5828 \f
5829 /* Action structure for NXAST_NOTE.
5830 *
5831 * This action has no effect. It is variable length. The switch does not
5832 * attempt to interpret the user-defined 'note' data in any way. A controller
5833 * can use this action to attach arbitrary metadata to a flow.
5834 *
5835 * This action might go away in the future.
5836 */
5837 struct nx_action_note {
5838 ovs_be16 type; /* OFPAT_VENDOR. */
5839 ovs_be16 len; /* A multiple of 8, but at least 16. */
5840 ovs_be32 vendor; /* NX_VENDOR_ID. */
5841 ovs_be16 subtype; /* NXAST_NOTE. */
5842 uint8_t note[6]; /* Start of user-defined data. */
5843 /* Possibly followed by additional user-defined data. */
5844 };
5845 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
5846
5847 static enum ofperr
5848 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan,
5849 enum ofp_version ofp_version OVS_UNUSED,
5850 struct ofpbuf *out)
5851 {
5852 struct ofpact_note *note;
5853 unsigned int length;
5854
5855 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
5856 note = ofpact_put_NOTE(out);
5857 note->length = length;
5858 ofpbuf_put(out, nan->note, length);
5859 note = out->header;
5860 ofpact_finish_NOTE(out, &note);
5861
5862 return 0;
5863 }
5864
5865 static void
5866 encode_NOTE(const struct ofpact_note *note,
5867 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
5868 {
5869 size_t start_ofs = out->size;
5870 struct nx_action_note *nan;
5871
5872 put_NXAST_NOTE(out);
5873 out->size = out->size - sizeof nan->note;
5874
5875 ofpbuf_put(out, note->data, note->length);
5876 pad_ofpat(out, start_ofs);
5877 }
5878
5879 static char * OVS_WARN_UNUSED_RESULT
5880 parse_NOTE(const char *arg, const struct ofpact_parse_params *pp)
5881 {
5882 size_t start_ofs = pp->ofpacts->size;
5883 ofpact_put_NOTE(pp->ofpacts);
5884 arg = ofpbuf_put_hex(pp->ofpacts, arg, NULL);
5885 if (arg[0]) {
5886 return xstrdup("bad hex digit in `note' argument");
5887 }
5888 struct ofpact_note *note = ofpbuf_at_assert(pp->ofpacts, start_ofs,
5889 sizeof *note);
5890 note->length = pp->ofpacts->size - (start_ofs + sizeof *note);
5891
5892 if (ofpbuf_oversized(pp->ofpacts)) {
5893 return xasprintf("input too big");
5894 }
5895
5896 ofpact_finish_NOTE(pp->ofpacts, &note);
5897 return NULL;
5898 }
5899
5900 static void
5901 format_NOTE(const struct ofpact_note *a,
5902 const struct ofpact_format_params *fp)
5903 {
5904 ds_put_format(fp->s, "%snote:%s", colors.param, colors.end);
5905 format_hex_arg(fp->s, a->data, a->length);
5906 }
5907
5908 static enum ofperr
5909 check_NOTE(const struct ofpact_note *a OVS_UNUSED,
5910 const struct ofpact_check_params *cp OVS_UNUSED)
5911 {
5912 return 0;
5913 }
5914 \f
5915 /* Exit action. */
5916
5917 static enum ofperr
5918 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
5919 {
5920 ofpact_put_EXIT(out);
5921 return 0;
5922 }
5923
5924 static void
5925 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
5926 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
5927 {
5928 put_NXAST_EXIT(out);
5929 }
5930
5931 static char * OVS_WARN_UNUSED_RESULT
5932 parse_EXIT(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
5933 {
5934 ofpact_put_EXIT(pp->ofpacts);
5935 return NULL;
5936 }
5937
5938 static void
5939 format_EXIT(const struct ofpact_null *a OVS_UNUSED,
5940 const struct ofpact_format_params *fp)
5941 {
5942 ds_put_format(fp->s, "%sexit%s", colors.special, colors.end);
5943 }
5944
5945 static enum ofperr
5946 check_EXIT(const struct ofpact_null *a OVS_UNUSED,
5947 const struct ofpact_check_params *cp OVS_UNUSED)
5948 {
5949 return 0;
5950 }
5951 \f
5952 /* Unroll xlate action. */
5953
5954 static void
5955 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
5956 enum ofp_version ofp_version OVS_UNUSED,
5957 struct ofpbuf *out OVS_UNUSED)
5958 {
5959 OVS_NOT_REACHED();
5960 }
5961
5962 static char * OVS_WARN_UNUSED_RESULT
5963 parse_UNROLL_XLATE(char *arg OVS_UNUSED,
5964 const struct ofpact_parse_params *pp OVS_UNUSED)
5965 {
5966 return xasprintf("UNROLL is an internal action "
5967 "that shouldn't be used via OpenFlow");
5968 }
5969
5970 static void
5971 format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a,
5972 const struct ofpact_format_params *fp)
5973 {
5974 ds_put_format(fp->s, "%sunroll_xlate(%s%stable=%s",
5975 colors.paren, colors.end,
5976 colors.special, colors.end);
5977 ofputil_format_table(a->rule_table_id, fp->table_map, fp->s);
5978 ds_put_format(fp->s, ", %scookie=%s%"PRIu64"%s)%s",
5979 colors.param, colors.end, ntohll(a->rule_cookie),
5980 colors.paren, colors.end);
5981 }
5982
5983 static enum ofperr
5984 check_UNROLL_XLATE(const struct ofpact_unroll_xlate *a OVS_UNUSED,
5985 const struct ofpact_check_params *cp OVS_UNUSED)
5986 {
5987 /* UNROLL is an internal action that should never be seen via OpenFlow. */
5988 return OFPERR_OFPBAC_BAD_TYPE;
5989 }
5990 \f
5991 /* The NXAST_CLONE action is "struct ext_action_header", followed by zero or
5992 * more embedded OpenFlow actions. */
5993
5994 static enum ofperr
5995 decode_NXAST_RAW_CLONE(const struct ext_action_header *eah,
5996 enum ofp_version ofp_version,
5997 const struct vl_mff_map *vl_mff_map,
5998 uint64_t *tlv_bitmap, struct ofpbuf *out)
5999 {
6000 int error;
6001 struct ofpbuf openflow;
6002 const size_t clone_offset = ofpacts_pull(out);
6003 struct ofpact_nest *clone = ofpact_put_CLONE(out);
6004
6005 /* decode action list */
6006 ofpbuf_pull(out, sizeof(*clone));
6007 openflow = ofpbuf_const_initializer(
6008 eah + 1, ntohs(eah->len) - sizeof *eah);
6009 error = ofpacts_pull_openflow_actions__(&openflow, openflow.size,
6010 ofp_version,
6011 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
6012 out, 0, vl_mff_map, tlv_bitmap);
6013 if (error) {
6014 return error;
6015 }
6016 clone = ofpbuf_push_uninit(out, sizeof *clone);
6017 out->header = &clone->ofpact;
6018 ofpact_finish_CLONE(out, &clone);
6019 ofpbuf_push_uninit(out, clone_offset);
6020 return error;
6021 }
6022
6023 static void
6024 encode_CLONE(const struct ofpact_nest *clone,
6025 enum ofp_version ofp_version, struct ofpbuf *out)
6026 {
6027 size_t len;
6028 const size_t ofs = out->size;
6029 struct ext_action_header *eah;
6030
6031 put_NXAST_CLONE(out);
6032 len = ofpacts_put_openflow_actions(clone->actions,
6033 ofpact_nest_get_action_len(clone),
6034 out, ofp_version);
6035 len += sizeof *eah;
6036 eah = ofpbuf_at(out, ofs, sizeof *eah);
6037 eah->len = htons(len);
6038 }
6039
6040 static char * OVS_WARN_UNUSED_RESULT
6041 parse_CLONE(char *arg, const struct ofpact_parse_params *pp)
6042 {
6043 const size_t clone_offset = ofpacts_pull(pp->ofpacts);
6044 struct ofpact_nest *clone = ofpact_put_CLONE(pp->ofpacts);
6045 char *error;
6046
6047 ofpbuf_pull(pp->ofpacts, sizeof *clone);
6048 error = ofpacts_parse_copy(arg, pp, false, OFPACT_CLONE);
6049 /* header points to the action list */
6050 pp->ofpacts->header = ofpbuf_push_uninit(pp->ofpacts, sizeof *clone);
6051 clone = pp->ofpacts->header;
6052
6053 if (ofpbuf_oversized(pp->ofpacts)) {
6054 free(error);
6055 return xasprintf("input too big");
6056 }
6057
6058 ofpact_finish_CLONE(pp->ofpacts, &clone);
6059 ofpbuf_push_uninit(pp->ofpacts, clone_offset);
6060 return error;
6061 }
6062
6063 static void
6064 format_CLONE(const struct ofpact_nest *a,
6065 const struct ofpact_format_params *fp)
6066 {
6067 ds_put_format(fp->s, "%sclone(%s", colors.paren, colors.end);
6068 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), fp);
6069 ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
6070 }
6071
6072 static enum ofperr
6073 check_subactions(struct ofpact *ofpacts, size_t ofpacts_len,
6074 struct ofpact_check_params *cp)
6075 {
6076 struct ofpact_check_params sub = *cp;
6077 enum ofperr error = ofpacts_check(ofpacts, ofpacts_len, &sub);
6078 cp->usable_protocols &= sub.usable_protocols;
6079 return error;
6080 }
6081
6082 static enum ofperr
6083 check_CLONE(struct ofpact_nest *a, struct ofpact_check_params *cp)
6084 {
6085 return check_subactions(a->actions, ofpact_nest_get_action_len(a), cp);
6086 }
6087 \f
6088 /* Action structure for NXAST_SAMPLE.
6089 *
6090 * Samples matching packets with the given probability and sends them
6091 * each to the set of collectors identified with the given ID. The
6092 * probability is expressed as a number of packets to be sampled out
6093 * of USHRT_MAX packets, and must be >0.
6094 *
6095 * When sending packet samples to IPFIX collectors, the IPFIX flow
6096 * record sent for each sampled packet is associated with the given
6097 * observation domain ID and observation point ID. Each IPFIX flow
6098 * record contain the sampled packet's headers when executing this
6099 * rule. If a sampled packet's headers are modified by previous
6100 * actions in the flow, those modified headers are sent. */
6101 struct nx_action_sample {
6102 ovs_be16 type; /* OFPAT_VENDOR. */
6103 ovs_be16 len; /* Length is 24. */
6104 ovs_be32 vendor; /* NX_VENDOR_ID. */
6105 ovs_be16 subtype; /* NXAST_SAMPLE. */
6106 ovs_be16 probability; /* Fraction of packets to sample. */
6107 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
6108 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
6109 ovs_be32 obs_point_id; /* ID of sampling observation point. */
6110 };
6111 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
6112
6113 /* Action structure for NXAST_SAMPLE2 and NXAST_SAMPLE3.
6114 *
6115 * NXAST_SAMPLE2 was added in Open vSwitch 2.5.90. Compared to NXAST_SAMPLE,
6116 * it adds support for exporting egress tunnel information.
6117 *
6118 * NXAST_SAMPLE3 was added in Open vSwitch 2.6.90. Compared to NXAST_SAMPLE2,
6119 * it adds support for the 'direction' field. */
6120 struct nx_action_sample2 {
6121 ovs_be16 type; /* OFPAT_VENDOR. */
6122 ovs_be16 len; /* Length is 32. */
6123 ovs_be32 vendor; /* NX_VENDOR_ID. */
6124 ovs_be16 subtype; /* NXAST_SAMPLE. */
6125 ovs_be16 probability; /* Fraction of packets to sample. */
6126 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
6127 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
6128 ovs_be32 obs_point_id; /* ID of sampling observation point. */
6129 ovs_be16 sampling_port; /* Sampling port. */
6130 uint8_t direction; /* NXAST_SAMPLE3 only. */
6131 uint8_t zeros[5]; /* Pad to a multiple of 8 bytes */
6132 };
6133 OFP_ASSERT(sizeof(struct nx_action_sample2) == 32);
6134
6135 static enum ofperr
6136 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas,
6137 enum ofp_version ofp_version OVS_UNUSED,
6138 struct ofpbuf *out)
6139 {
6140 struct ofpact_sample *sample;
6141
6142 sample = ofpact_put_SAMPLE(out);
6143 sample->ofpact.raw = NXAST_RAW_SAMPLE;
6144 sample->probability = ntohs(nas->probability);
6145 sample->collector_set_id = ntohl(nas->collector_set_id);
6146 sample->obs_domain_id = ntohl(nas->obs_domain_id);
6147 sample->obs_point_id = ntohl(nas->obs_point_id);
6148 sample->sampling_port = OFPP_NONE;
6149 sample->direction = NX_ACTION_SAMPLE_DEFAULT;
6150
6151 if (sample->probability == 0) {
6152 return OFPERR_OFPBAC_BAD_ARGUMENT;
6153 }
6154
6155 return 0;
6156 }
6157
6158 static enum ofperr
6159 decode_SAMPLE2(const struct nx_action_sample2 *nas,
6160 enum ofp_raw_action_type raw,
6161 enum nx_action_sample_direction direction,
6162 struct ofpact_sample *sample)
6163 {
6164 sample->ofpact.raw = raw;
6165 sample->probability = ntohs(nas->probability);
6166 sample->collector_set_id = ntohl(nas->collector_set_id);
6167 sample->obs_domain_id = ntohl(nas->obs_domain_id);
6168 sample->obs_point_id = ntohl(nas->obs_point_id);
6169 sample->sampling_port = u16_to_ofp(ntohs(nas->sampling_port));
6170 sample->direction = direction;
6171
6172 if (sample->probability == 0) {
6173 return OFPERR_OFPBAC_BAD_ARGUMENT;
6174 }
6175
6176 return 0;
6177 }
6178
6179 static enum ofperr
6180 decode_NXAST_RAW_SAMPLE2(const struct nx_action_sample2 *nas,
6181 enum ofp_version ofp_version OVS_UNUSED,
6182 struct ofpbuf *out)
6183 {
6184 return decode_SAMPLE2(nas, NXAST_RAW_SAMPLE2, NX_ACTION_SAMPLE_DEFAULT,
6185 ofpact_put_SAMPLE(out));
6186 }
6187
6188 static enum ofperr
6189 decode_NXAST_RAW_SAMPLE3(const struct nx_action_sample2 *nas,
6190 enum ofp_version ofp_version OVS_UNUSED,
6191 struct ofpbuf *out)
6192 {
6193 struct ofpact_sample *sample = ofpact_put_SAMPLE(out);
6194 if (!is_all_zeros(nas->zeros, sizeof nas->zeros)) {
6195 return OFPERR_NXBRC_MUST_BE_ZERO;
6196 }
6197 if (nas->direction != NX_ACTION_SAMPLE_DEFAULT &&
6198 nas->direction != NX_ACTION_SAMPLE_INGRESS &&
6199 nas->direction != NX_ACTION_SAMPLE_EGRESS) {
6200 VLOG_WARN_RL(&rl, "invalid sample direction %"PRIu8, nas->direction);
6201 return OFPERR_OFPBAC_BAD_ARGUMENT;
6202 }
6203 return decode_SAMPLE2(nas, NXAST_RAW_SAMPLE3, nas->direction, sample);
6204 }
6205
6206 static void
6207 encode_SAMPLE2(const struct ofpact_sample *sample,
6208 struct nx_action_sample2 *nas)
6209 {
6210 nas->probability = htons(sample->probability);
6211 nas->collector_set_id = htonl(sample->collector_set_id);
6212 nas->obs_domain_id = htonl(sample->obs_domain_id);
6213 nas->obs_point_id = htonl(sample->obs_point_id);
6214 nas->sampling_port = htons(ofp_to_u16(sample->sampling_port));
6215 nas->direction = sample->direction;
6216 }
6217
6218 static void
6219 encode_SAMPLE(const struct ofpact_sample *sample,
6220 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
6221 {
6222 if (sample->ofpact.raw == NXAST_RAW_SAMPLE3
6223 || sample->direction != NX_ACTION_SAMPLE_DEFAULT) {
6224 encode_SAMPLE2(sample, put_NXAST_SAMPLE3(out));
6225 } else if (sample->ofpact.raw == NXAST_RAW_SAMPLE2
6226 || sample->sampling_port != OFPP_NONE) {
6227 encode_SAMPLE2(sample, put_NXAST_SAMPLE2(out));
6228 } else {
6229 struct nx_action_sample *nas = put_NXAST_SAMPLE(out);
6230 nas->probability = htons(sample->probability);
6231 nas->collector_set_id = htonl(sample->collector_set_id);
6232 nas->obs_domain_id = htonl(sample->obs_domain_id);
6233 nas->obs_point_id = htonl(sample->obs_point_id);
6234 }
6235 }
6236
6237 /* Parses 'arg' as the argument to a "sample" action, and appends such an
6238 * action to 'pp->ofpacts'.
6239 *
6240 * Returns NULL if successful, otherwise a malloc()'d string describing the
6241 * error. The caller is responsible for freeing the returned string. */
6242 static char * OVS_WARN_UNUSED_RESULT
6243 parse_SAMPLE(char *arg, const struct ofpact_parse_params *pp)
6244 {
6245 struct ofpact_sample *os = ofpact_put_SAMPLE(pp->ofpacts);
6246 os->sampling_port = OFPP_NONE;
6247 os->direction = NX_ACTION_SAMPLE_DEFAULT;
6248
6249 char *key, *value;
6250 while (ofputil_parse_key_value(&arg, &key, &value)) {
6251 char *error = NULL;
6252
6253 if (!strcmp(key, "probability")) {
6254 error = str_to_u16(value, "probability", &os->probability);
6255 if (!error && os->probability == 0) {
6256 error = xasprintf("invalid probability value \"%s\"", value);
6257 }
6258 } else if (!strcmp(key, "collector_set_id")) {
6259 error = str_to_u32(value, &os->collector_set_id);
6260 } else if (!strcmp(key, "obs_domain_id")) {
6261 error = str_to_u32(value, &os->obs_domain_id);
6262 } else if (!strcmp(key, "obs_point_id")) {
6263 error = str_to_u32(value, &os->obs_point_id);
6264 } else if (!strcmp(key, "sampling_port")) {
6265 if (!ofputil_port_from_string(value, pp->port_map,
6266 &os->sampling_port)) {
6267 error = xasprintf("%s: unknown port", value);
6268 }
6269 } else if (!strcmp(key, "ingress")) {
6270 os->direction = NX_ACTION_SAMPLE_INGRESS;
6271 } else if (!strcmp(key, "egress")) {
6272 os->direction = NX_ACTION_SAMPLE_EGRESS;
6273 } else {
6274 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
6275 key);
6276 }
6277 if (error) {
6278 return error;
6279 }
6280 }
6281 if (os->probability == 0) {
6282 return xstrdup("non-zero \"probability\" must be specified on sample");
6283 }
6284
6285 return NULL;
6286 }
6287
6288 static void
6289 format_SAMPLE(const struct ofpact_sample *a,
6290 const struct ofpact_format_params *fp)
6291 {
6292 ds_put_format(fp->s, "%ssample(%s%sprobability=%s%"PRIu16
6293 ",%scollector_set_id=%s%"PRIu32
6294 ",%sobs_domain_id=%s%"PRIu32
6295 ",%sobs_point_id=%s%"PRIu32,
6296 colors.paren, colors.end,
6297 colors.param, colors.end, a->probability,
6298 colors.param, colors.end, a->collector_set_id,
6299 colors.param, colors.end, a->obs_domain_id,
6300 colors.param, colors.end, a->obs_point_id);
6301 if (a->sampling_port != OFPP_NONE) {
6302 ds_put_format(fp->s, ",%ssampling_port=%s", colors.param, colors.end);
6303 ofputil_format_port(a->sampling_port, fp->port_map, fp->s);
6304 }
6305 if (a->direction == NX_ACTION_SAMPLE_INGRESS) {
6306 ds_put_format(fp->s, ",%singress%s", colors.param, colors.end);
6307 } else if (a->direction == NX_ACTION_SAMPLE_EGRESS) {
6308 ds_put_format(fp->s, ",%segress%s", colors.param, colors.end);
6309 }
6310 ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
6311 }
6312
6313 static enum ofperr
6314 check_SAMPLE(const struct ofpact_sample *a OVS_UNUSED,
6315 const struct ofpact_check_params *cp OVS_UNUSED)
6316 {
6317 return 0;
6318 }
6319 \f
6320 /* debug instructions. */
6321
6322 static bool enable_debug;
6323
6324 void
6325 ofpact_dummy_enable(void)
6326 {
6327 enable_debug = true;
6328 }
6329
6330 static enum ofperr
6331 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
6332 {
6333 if (!enable_debug) {
6334 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
6335 }
6336
6337 ofpact_put_DEBUG_RECIRC(out);
6338 return 0;
6339 }
6340
6341 static void
6342 encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
6343 enum ofp_version ofp_version OVS_UNUSED,
6344 struct ofpbuf *out)
6345 {
6346 put_NXAST_DEBUG_RECIRC(out);
6347 }
6348
6349 static char * OVS_WARN_UNUSED_RESULT
6350 parse_DEBUG_RECIRC(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
6351 {
6352 ofpact_put_DEBUG_RECIRC(pp->ofpacts);
6353 return NULL;
6354 }
6355
6356 static void
6357 format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED,
6358 const struct ofpact_format_params *fp)
6359 {
6360 ds_put_format(fp->s, "%sdebug_recirc%s", colors.value, colors.end);
6361 }
6362
6363 static enum ofperr
6364 check_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED,
6365 const struct ofpact_check_params *cp OVS_UNUSED)
6366 {
6367 return 0;
6368 }
6369
6370 static enum ofperr
6371 decode_NXAST_RAW_DEBUG_SLOW(struct ofpbuf *out)
6372 {
6373 if (!enable_debug) {
6374 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
6375 }
6376
6377 ofpact_put_DEBUG_SLOW(out);
6378 return 0;
6379 }
6380
6381 static void
6382 encode_DEBUG_SLOW(const struct ofpact_null *n OVS_UNUSED,
6383 enum ofp_version ofp_version OVS_UNUSED,
6384 struct ofpbuf *out)
6385 {
6386 put_NXAST_DEBUG_SLOW(out);
6387 }
6388
6389 static char * OVS_WARN_UNUSED_RESULT
6390 parse_DEBUG_SLOW(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
6391 {
6392 ofpact_put_DEBUG_SLOW(pp->ofpacts);
6393 return NULL;
6394 }
6395
6396 static void
6397 format_DEBUG_SLOW(const struct ofpact_null *a OVS_UNUSED,
6398 const struct ofpact_format_params *fp)
6399 {
6400 ds_put_format(fp->s, "%sdebug_slow%s", colors.value, colors.end);
6401 }
6402
6403 static enum ofperr
6404 check_DEBUG_SLOW(const struct ofpact_null *a OVS_UNUSED,
6405 const struct ofpact_check_params *cp OVS_UNUSED)
6406 {
6407 return 0;
6408 }
6409
6410 /* Action structure for NXAST_CT.
6411 *
6412 * Pass traffic to the connection tracker.
6413 *
6414 * There are two important concepts to understanding the connection tracking
6415 * interface: Packet state and Connection state. Packets may be "Untracked" or
6416 * "Tracked". Connections may be "Uncommitted" or "Committed".
6417 *
6418 * - Packet State:
6419 *
6420 * Untracked packets have an unknown connection state. In most
6421 * cases, packets entering the OpenFlow pipeline will initially be
6422 * in the untracked state. Untracked packets may become tracked by
6423 * executing NXAST_CT with a "recirc_table" specified. This makes
6424 * various aspects about the connection available, in particular
6425 * the connection state.
6426 *
6427 * An NXAST_CT action always puts the packet into an untracked
6428 * state for the current processing path. If "recirc_table" is
6429 * set, execution is forked and the packet passes through the
6430 * connection tracker. The specified table's processing path is
6431 * able to match on Connection state until the end of the OpenFlow
6432 * pipeline or NXAST_CT is called again.
6433 *
6434 * - Connection State:
6435 *
6436 * Multiple packets may be associated with a single connection. Initially,
6437 * all connections are uncommitted. The connection state corresponding to
6438 * a packet is available in the NXM_NX_CT_STATE field for tracked packets.
6439 *
6440 * Uncommitted connections have no state stored about them. Uncommitted
6441 * connections may transition into the committed state by executing
6442 * NXAST_CT with the NX_CT_F_COMMIT flag.
6443 *
6444 * Once a connection becomes committed, information may be gathered about
6445 * the connection by passing subsequent packets through the connection
6446 * tracker, and the state of the connection will be stored beyond the
6447 * lifetime of packet processing.
6448 *
6449 * A committed connection always has the directionality of the packet that
6450 * caused the connection to be committed in the first place. This is the
6451 * "original direction" of the connection, and the opposite direction is
6452 * the "reply direction". If a connection is already committed, but it is
6453 * then decided that the original direction should be the opposite of the
6454 * existing connection, NX_CT_F_FORCE flag may be used in addition to
6455 * NX_CT_F_COMMIT flag to in effect terminate the existing connection and
6456 * start a new one in the current direction.
6457 *
6458 * Connections may transition back into the uncommitted state due to
6459 * external timers, or due to the contents of packets that are sent to the
6460 * connection tracker. This behaviour is outside of the scope of the
6461 * OpenFlow interface.
6462 *
6463 * The "zone" specifies a context within which the tracking is done:
6464 *
6465 * The connection tracking zone is a 16-bit number. Each zone is an
6466 * independent connection tracking context. The connection state for each
6467 * connection is completely separate for each zone, so if a connection
6468 * is committed to zone A, then it will remain uncommitted in zone B.
6469 * If NXAST_CT is executed with the same zone multiple times, later
6470 * executions have no effect.
6471 *
6472 * If 'zone_src' is nonzero, this specifies that the zone should be
6473 * sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
6474 * of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
6475 * NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
6476 * are the same as the acceptable nxm_header values for the 'src' field of
6477 * NXAST_REG_MOVE.
6478 *
6479 * If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
6480 * connection tracking zone.
6481 *
6482 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
6483 *
6484 * If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
6485 * packet will be logically cloned prior to executing this action. One
6486 * copy will be sent to the connection tracker, then will be re-injected
6487 * into the OpenFlow pipeline beginning at the OpenFlow table specified in
6488 * this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
6489 * fields will be populated. The original instance of the packet will
6490 * continue the current actions list. This can be thought of as similar to
6491 * the effect of the "output" action: One copy is sent out (in this case,
6492 * to the connection tracker), but the current copy continues processing.
6493 *
6494 * It is strongly recommended that this table is later than the current
6495 * table, to prevent loops.
6496 *
6497 * The "alg" attaches protocol-specific behaviour to this action:
6498 *
6499 * The ALG is a 16-bit number which specifies that additional
6500 * processing should be applied to this traffic.
6501 *
6502 * Protocol | Value | Meaning
6503 * --------------------------------------------------------------------
6504 * None | 0 | No protocol-specific behaviour.
6505 * FTP | 21 | Parse FTP control connections and observe the
6506 * | | negotiation of related data connections.
6507 * Other | Other | Unsupported protocols.
6508 *
6509 * By way of example, if FTP control connections have this action applied
6510 * with the ALG set to FTP (21), then the connection tracker will observe
6511 * the negotiation of data connections. This allows the connection
6512 * tracker to identify subsequent data connections as "related" to this
6513 * existing connection. The "related" flag will be populated in the
6514 * NXM_NX_CT_STATE field for such connections if the 'recirc_table' is
6515 * specified.
6516 *
6517 * Zero or more actions may immediately follow this action. These actions will
6518 * be executed within the context of the connection tracker, and they require
6519 * NX_CT_F_COMMIT flag be set.
6520 */
6521 struct nx_action_conntrack {
6522 ovs_be16 type; /* OFPAT_VENDOR. */
6523 ovs_be16 len; /* At least 24. */
6524 ovs_be32 vendor; /* NX_VENDOR_ID. */
6525 ovs_be16 subtype; /* NXAST_CT. */
6526 ovs_be16 flags; /* Zero or more NX_CT_F_* flags.
6527 * Unspecified flag bits must be zero. */
6528 ovs_be32 zone_src; /* Connection tracking context. */
6529 union {
6530 ovs_be16 zone_ofs_nbits;/* Range to use from source field. */
6531 ovs_be16 zone_imm; /* Immediate value for zone. */
6532 };
6533 uint8_t recirc_table; /* Recirculate to a specific table, or
6534 NX_CT_RECIRC_NONE for no recirculation. */
6535 uint8_t pad[3]; /* Zeroes */
6536 ovs_be16 alg; /* Well-known port number for the protocol.
6537 * 0 indicates no ALG is required. */
6538 /* Followed by a sequence of zero or more OpenFlow actions. The length of
6539 * these is included in 'len'. */
6540 };
6541 OFP_ASSERT(sizeof(struct nx_action_conntrack) == 24);
6542
6543 static enum ofperr
6544 decode_ct_zone(const struct nx_action_conntrack *nac,
6545 struct ofpact_conntrack *out,
6546 const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap)
6547 {
6548 if (nac->zone_src) {
6549 enum ofperr error;
6550
6551 out->zone_src.ofs = nxm_decode_ofs(nac->zone_ofs_nbits);
6552 out->zone_src.n_bits = nxm_decode_n_bits(nac->zone_ofs_nbits);
6553 error = mf_vl_mff_mf_from_nxm_header(ntohl(nac->zone_src),
6554 vl_mff_map, &out->zone_src.field,
6555 tlv_bitmap);
6556 if (error) {
6557 return error;
6558 }
6559
6560 error = mf_check_src(&out->zone_src, NULL);
6561 if (error) {
6562 return error;
6563 }
6564
6565 if (out->zone_src.n_bits != 16) {
6566 VLOG_WARN_RL(&rl, "zone n_bits %d not within valid range [16..16]",
6567 out->zone_src.n_bits);
6568 return OFPERR_OFPBAC_BAD_SET_LEN;
6569 }
6570 } else {
6571 out->zone_src.field = NULL;
6572 out->zone_imm = ntohs(nac->zone_imm);
6573 }
6574
6575 return 0;
6576 }
6577
6578 static enum ofperr
6579 decode_NXAST_RAW_CT(const struct nx_action_conntrack *nac,
6580 enum ofp_version ofp_version,
6581 const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap,
6582 struct ofpbuf *out)
6583 {
6584 const size_t ct_offset = ofpacts_pull(out);
6585 struct ofpact_conntrack *conntrack = ofpact_put_CT(out);
6586 int error;
6587
6588 conntrack->flags = ntohs(nac->flags);
6589 if (conntrack->flags & NX_CT_F_FORCE &&
6590 !(conntrack->flags & NX_CT_F_COMMIT)) {
6591 error = OFPERR_OFPBAC_BAD_ARGUMENT;
6592 goto out;
6593 }
6594
6595 error = decode_ct_zone(nac, conntrack, vl_mff_map, tlv_bitmap);
6596 if (error) {
6597 goto out;
6598 }
6599 conntrack->recirc_table = nac->recirc_table;
6600 conntrack->alg = ntohs(nac->alg);
6601
6602 ofpbuf_pull(out, sizeof(*conntrack));
6603
6604 struct ofpbuf openflow = ofpbuf_const_initializer(
6605 nac + 1, ntohs(nac->len) - sizeof(*nac));
6606 error = ofpacts_pull_openflow_actions__(&openflow, openflow.size,
6607 ofp_version,
6608 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
6609 out, OFPACT_CT, vl_mff_map,
6610 tlv_bitmap);
6611 if (error) {
6612 return error;
6613 }
6614
6615 conntrack = ofpbuf_push_uninit(out, sizeof(*conntrack));
6616 out->header = &conntrack->ofpact;
6617 ofpact_finish_CT(out, &conntrack);
6618
6619 if (conntrack->ofpact.len > sizeof(*conntrack)
6620 && !(conntrack->flags & NX_CT_F_COMMIT)) {
6621 const struct ofpact *a;
6622 size_t ofpacts_len = conntrack->ofpact.len - sizeof(*conntrack);
6623
6624 OFPACT_FOR_EACH (a, conntrack->actions, ofpacts_len) {
6625 if (a->type != OFPACT_NAT || ofpact_get_NAT(a)->flags
6626 || ofpact_get_NAT(a)->range_af != AF_UNSPEC) {
6627 VLOG_WARN_RL(&rl, "CT action requires commit flag if actions "
6628 "other than NAT without arguments are specified.");
6629 error = OFPERR_OFPBAC_BAD_ARGUMENT;
6630 goto out;
6631 }
6632 }
6633 }
6634
6635 out:
6636 ofpbuf_push_uninit(out, ct_offset);
6637 return error;
6638 }
6639
6640 static void
6641 encode_CT(const struct ofpact_conntrack *conntrack,
6642 enum ofp_version ofp_version, struct ofpbuf *out)
6643 {
6644 struct nx_action_conntrack *nac;
6645 const size_t ofs = out->size;
6646 size_t len;
6647
6648 nac = put_NXAST_CT(out);
6649 nac->flags = htons(conntrack->flags);
6650 if (conntrack->zone_src.field) {
6651 nac->zone_src = htonl(nxm_header_from_mff(conntrack->zone_src.field));
6652 nac->zone_ofs_nbits = nxm_encode_ofs_nbits(conntrack->zone_src.ofs,
6653 conntrack->zone_src.n_bits);
6654 } else {
6655 nac->zone_src = htonl(0);
6656 nac->zone_imm = htons(conntrack->zone_imm);
6657 }
6658 nac->recirc_table = conntrack->recirc_table;
6659 nac->alg = htons(conntrack->alg);
6660
6661 len = ofpacts_put_openflow_actions(conntrack->actions,
6662 ofpact_ct_get_action_len(conntrack),
6663 out, ofp_version);
6664 len += sizeof(*nac);
6665 nac = ofpbuf_at(out, ofs, sizeof(*nac));
6666 nac->len = htons(len);
6667 }
6668
6669 static char *OVS_WARN_UNUSED_RESULT
6670 parse_NAT(char *arg, const struct ofpact_parse_params *pp);
6671
6672 /* Parses 'arg' as the argument to a "ct" action, and appends such an
6673 * action to 'pp->ofpacts'.
6674 *
6675 * Returns NULL if successful, otherwise a malloc()'d string describing the
6676 * error. The caller is responsible for freeing the returned string. */
6677 static char * OVS_WARN_UNUSED_RESULT
6678 parse_CT(char *arg, const struct ofpact_parse_params *pp)
6679 {
6680 const size_t ct_offset = ofpacts_pull(pp->ofpacts);
6681 struct ofpact_conntrack *oc;
6682 char *error = NULL;
6683 char *key, *value;
6684
6685 oc = ofpact_put_CT(pp->ofpacts);
6686 oc->flags = 0;
6687 oc->recirc_table = NX_CT_RECIRC_NONE;
6688 while (ofputil_parse_key_value(&arg, &key, &value)) {
6689 if (!strcmp(key, "commit")) {
6690 oc->flags |= NX_CT_F_COMMIT;
6691 } else if (!strcmp(key, "force")) {
6692 oc->flags |= NX_CT_F_FORCE;
6693 } else if (!strcmp(key, "table")) {
6694 if (!ofputil_table_from_string(value, pp->table_map,
6695 &oc->recirc_table)) {
6696 error = xasprintf("unknown table %s", value);
6697 } else if (oc->recirc_table == NX_CT_RECIRC_NONE) {
6698 error = xasprintf("invalid table %#"PRIx8, oc->recirc_table);
6699 }
6700 } else if (!strcmp(key, "zone")) {
6701 error = str_to_u16(value, "zone", &oc->zone_imm);
6702
6703 if (error) {
6704 free(error);
6705 error = mf_parse_subfield(&oc->zone_src, value);
6706 if (error) {
6707 return error;
6708 }
6709 }
6710 } else if (!strcmp(key, "alg")) {
6711 error = str_to_connhelper(value, &oc->alg);
6712 } else if (!strcmp(key, "nat")) {
6713 const size_t nat_offset = ofpacts_pull(pp->ofpacts);
6714
6715 error = parse_NAT(value, pp);
6716 /* Update CT action pointer and length. */
6717 pp->ofpacts->header = ofpbuf_push_uninit(pp->ofpacts, nat_offset);
6718 oc = pp->ofpacts->header;
6719 } else if (!strcmp(key, "exec")) {
6720 /* Hide existing actions from ofpacts_parse_copy(), so the
6721 * nesting can be handled transparently. */
6722 enum ofputil_protocol usable_protocols2;
6723 const size_t exec_offset = ofpacts_pull(pp->ofpacts);
6724
6725 /* Initializes 'usable_protocol2', fold it back to
6726 * '*usable_protocols' afterwards, so that we do not lose
6727 * restrictions already in there. */
6728 struct ofpact_parse_params pp2 = *pp;
6729 pp2.usable_protocols = &usable_protocols2;
6730 error = ofpacts_parse_copy(value, &pp2, false, OFPACT_CT);
6731 *pp->usable_protocols &= usable_protocols2;
6732 pp->ofpacts->header = ofpbuf_push_uninit(pp->ofpacts, exec_offset);
6733 oc = pp->ofpacts->header;
6734 } else {
6735 error = xasprintf("invalid argument to \"ct\" action: `%s'", key);
6736 }
6737 if (error) {
6738 break;
6739 }
6740 }
6741 if (!error && oc->flags & NX_CT_F_FORCE && !(oc->flags & NX_CT_F_COMMIT)) {
6742 error = xasprintf("\"force\" flag requires \"commit\" flag.");
6743 }
6744
6745 if (ofpbuf_oversized(pp->ofpacts)) {
6746 free(error);
6747 return xasprintf("input too big");
6748 }
6749
6750 ofpact_finish_CT(pp->ofpacts, &oc);
6751 ofpbuf_push_uninit(pp->ofpacts, ct_offset);
6752 return error;
6753 }
6754
6755 static void
6756 format_alg(int port, struct ds *s)
6757 {
6758 switch(port) {
6759 case IPPORT_FTP:
6760 ds_put_format(s, "%salg=%sftp,", colors.param, colors.end);
6761 break;
6762 case IPPORT_TFTP:
6763 ds_put_format(s, "%salg=%stftp,", colors.param, colors.end);
6764 break;
6765 case 0:
6766 /* Don't print. */
6767 break;
6768 default:
6769 ds_put_format(s, "%salg=%s%d,", colors.param, colors.end, port);
6770 break;
6771 }
6772 }
6773
6774 static void format_NAT(const struct ofpact_nat *,
6775 const struct ofpact_format_params *fp);
6776
6777 static void
6778 format_CT(const struct ofpact_conntrack *a,
6779 const struct ofpact_format_params *fp)
6780 {
6781 ds_put_format(fp->s, "%sct(%s", colors.paren, colors.end);
6782 if (a->flags & NX_CT_F_COMMIT) {
6783 ds_put_format(fp->s, "%scommit%s,", colors.value, colors.end);
6784 }
6785 if (a->flags & NX_CT_F_FORCE) {
6786 ds_put_format(fp->s, "%sforce%s,", colors.value, colors.end);
6787 }
6788 if (a->recirc_table != NX_CT_RECIRC_NONE) {
6789 ds_put_format(fp->s, "%stable=%s", colors.special, colors.end);
6790 ofputil_format_table(a->recirc_table, fp->table_map, fp->s);
6791 ds_put_char(fp->s, ',');
6792 }
6793 if (a->zone_src.field) {
6794 ds_put_format(fp->s, "%szone=%s", colors.param, colors.end);
6795 mf_format_subfield(&a->zone_src, fp->s);
6796 ds_put_char(fp->s, ',');
6797 } else if (a->zone_imm) {
6798 ds_put_format(fp->s, "%szone=%s%"PRIu16",",
6799 colors.param, colors.end, a->zone_imm);
6800 }
6801 /* If the first action is a NAT action, format it outside of the 'exec'
6802 * envelope. */
6803 const struct ofpact *action = a->actions;
6804 size_t actions_len = ofpact_ct_get_action_len(a);
6805 if (actions_len && action->type == OFPACT_NAT) {
6806 format_NAT(ofpact_get_NAT(action), fp);
6807 ds_put_char(fp->s, ',');
6808 actions_len -= OFPACT_ALIGN(action->len);
6809 action = ofpact_next(action);
6810 }
6811 if (actions_len) {
6812 ds_put_format(fp->s, "%sexec(%s", colors.paren, colors.end);
6813 ofpacts_format(action, actions_len, fp);
6814 ds_put_format(fp->s, "%s),%s", colors.paren, colors.end);
6815 }
6816 format_alg(a->alg, fp->s);
6817 ds_chomp(fp->s, ',');
6818 ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
6819 }
6820
6821 static enum ofperr
6822 check_CT(struct ofpact_conntrack *a, struct ofpact_check_params *cp)
6823 {
6824 struct flow *flow = &cp->match->flow;
6825
6826 if (!dl_type_is_ip_any(get_dl_type(flow))
6827 || (flow->ct_state & CS_INVALID && a->flags & NX_CT_F_COMMIT)
6828 || (a->alg == IPPORT_FTP && flow->nw_proto != IPPROTO_TCP)
6829 || (a->alg == IPPORT_TFTP && flow->nw_proto != IPPROTO_UDP)) {
6830 /* We can't downgrade to OF1.0 and expect inconsistent CT actions
6831 * be silently discarded. Instead, datapath flow install fails, so
6832 * it is better to flag inconsistent CT actions as hard errors. */
6833 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
6834 }
6835
6836 if (a->zone_src.field) {
6837 return mf_check_src(&a->zone_src, cp->match);
6838 }
6839
6840 return check_subactions(a->actions, ofpact_ct_get_action_len(a), cp);
6841 }
6842 \f
6843 /* ct_clear action. */
6844
6845 static enum ofperr
6846 decode_NXAST_RAW_CT_CLEAR(struct ofpbuf *out)
6847 {
6848 ofpact_put_CT_CLEAR(out);
6849 return 0;
6850 }
6851
6852 static void
6853 encode_CT_CLEAR(const struct ofpact_null *null OVS_UNUSED,
6854 enum ofp_version ofp_version OVS_UNUSED,
6855 struct ofpbuf *out)
6856 {
6857 put_NXAST_CT_CLEAR(out);
6858 }
6859
6860 static char * OVS_WARN_UNUSED_RESULT
6861 parse_CT_CLEAR(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
6862 {
6863 ofpact_put_CT_CLEAR(pp->ofpacts);
6864 return NULL;
6865 }
6866
6867 static void
6868 format_CT_CLEAR(const struct ofpact_null *a OVS_UNUSED,
6869 const struct ofpact_format_params *fp)
6870 {
6871 ds_put_format(fp->s, "%sct_clear%s", colors.value, colors.end);
6872 }
6873
6874 static enum ofperr
6875 check_CT_CLEAR(const struct ofpact_null *a OVS_UNUSED,
6876 const struct ofpact_check_params *cp OVS_UNUSED)
6877 {
6878 return 0;
6879 }
6880 \f
6881 /* NAT action. */
6882
6883 /* Which optional fields are present? */
6884 enum nx_nat_range {
6885 NX_NAT_RANGE_IPV4_MIN = 1 << 0, /* ovs_be32 */
6886 NX_NAT_RANGE_IPV4_MAX = 1 << 1, /* ovs_be32 */
6887 NX_NAT_RANGE_IPV6_MIN = 1 << 2, /* struct in6_addr */
6888 NX_NAT_RANGE_IPV6_MAX = 1 << 3, /* struct in6_addr */
6889 NX_NAT_RANGE_PROTO_MIN = 1 << 4, /* ovs_be16 */
6890 NX_NAT_RANGE_PROTO_MAX = 1 << 5, /* ovs_be16 */
6891 };
6892
6893 /* Action structure for NXAST_NAT. */
6894 struct nx_action_nat {
6895 ovs_be16 type; /* OFPAT_VENDOR. */
6896 ovs_be16 len; /* At least 16. */
6897 ovs_be32 vendor; /* NX_VENDOR_ID. */
6898 ovs_be16 subtype; /* NXAST_NAT. */
6899 uint8_t pad[2]; /* Must be zero. */
6900 ovs_be16 flags; /* Zero or more NX_NAT_F_* flags.
6901 * Unspecified flag bits must be zero. */
6902 ovs_be16 range_present; /* NX_NAT_RANGE_* */
6903 /* Followed by optional parameters as specified by 'range_present' */
6904 };
6905 OFP_ASSERT(sizeof(struct nx_action_nat) == 16);
6906
6907 static void
6908 encode_NAT(const struct ofpact_nat *nat,
6909 enum ofp_version ofp_version OVS_UNUSED,
6910 struct ofpbuf *out)
6911 {
6912 struct nx_action_nat *nan;
6913 const size_t ofs = out->size;
6914 uint16_t range_present = 0;
6915
6916 nan = put_NXAST_NAT(out);
6917 nan->flags = htons(nat->flags);
6918 if (nat->range_af == AF_INET) {
6919 if (nat->range.addr.ipv4.min) {
6920 ovs_be32 *min = ofpbuf_put_uninit(out, sizeof *min);
6921 *min = nat->range.addr.ipv4.min;
6922 range_present |= NX_NAT_RANGE_IPV4_MIN;
6923 }
6924 if (nat->range.addr.ipv4.max) {
6925 ovs_be32 *max = ofpbuf_put_uninit(out, sizeof *max);
6926 *max = nat->range.addr.ipv4.max;
6927 range_present |= NX_NAT_RANGE_IPV4_MAX;
6928 }
6929 } else if (nat->range_af == AF_INET6) {
6930 if (!ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
6931 struct in6_addr *min = ofpbuf_put_uninit(out, sizeof *min);
6932 *min = nat->range.addr.ipv6.min;
6933 range_present |= NX_NAT_RANGE_IPV6_MIN;
6934 }
6935 if (!ipv6_mask_is_any(&nat->range.addr.ipv6.max)) {
6936 struct in6_addr *max = ofpbuf_put_uninit(out, sizeof *max);
6937 *max = nat->range.addr.ipv6.max;
6938 range_present |= NX_NAT_RANGE_IPV6_MAX;
6939 }
6940 }
6941 if (nat->range_af != AF_UNSPEC) {
6942 if (nat->range.proto.min) {
6943 ovs_be16 *min = ofpbuf_put_uninit(out, sizeof *min);
6944 *min = htons(nat->range.proto.min);
6945 range_present |= NX_NAT_RANGE_PROTO_MIN;
6946 }
6947 if (nat->range.proto.max) {
6948 ovs_be16 *max = ofpbuf_put_uninit(out, sizeof *max);
6949 *max = htons(nat->range.proto.max);
6950 range_present |= NX_NAT_RANGE_PROTO_MAX;
6951 }
6952 }
6953 pad_ofpat(out, ofs);
6954 nan = ofpbuf_at(out, ofs, sizeof *nan);
6955 nan->range_present = htons(range_present);
6956 }
6957
6958 static enum ofperr
6959 decode_NXAST_RAW_NAT(const struct nx_action_nat *nan,
6960 enum ofp_version ofp_version OVS_UNUSED,
6961 struct ofpbuf *out)
6962 {
6963 struct ofpact_nat *nat;
6964 uint16_t range_present = ntohs(nan->range_present);
6965 const char *opts = (char *)(nan + 1);
6966 uint16_t len = ntohs(nan->len) - sizeof *nan;
6967
6968 nat = ofpact_put_NAT(out);
6969 nat->flags = ntohs(nan->flags);
6970
6971 /* Check for unknown or mutually exclusive flags. */
6972 if ((nat->flags & ~NX_NAT_F_MASK)
6973 || (nat->flags & NX_NAT_F_SRC && nat->flags & NX_NAT_F_DST)
6974 || (nat->flags & NX_NAT_F_PROTO_HASH
6975 && nat->flags & NX_NAT_F_PROTO_RANDOM)) {
6976 return OFPERR_OFPBAC_BAD_ARGUMENT;
6977 }
6978
6979 #define NX_NAT_GET_OPT(DST, SRC, LEN, TYPE) \
6980 (LEN >= sizeof(TYPE) \
6981 ? (memcpy(DST, SRC, sizeof(TYPE)), LEN -= sizeof(TYPE), \
6982 SRC += sizeof(TYPE)) \
6983 : NULL)
6984
6985 nat->range_af = AF_UNSPEC;
6986 if (range_present & NX_NAT_RANGE_IPV4_MIN) {
6987 if (range_present & (NX_NAT_RANGE_IPV6_MIN | NX_NAT_RANGE_IPV6_MAX)) {
6988 return OFPERR_OFPBAC_BAD_ARGUMENT;
6989 }
6990
6991 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.min, opts, len, ovs_be32)
6992 || !nat->range.addr.ipv4.min) {
6993 return OFPERR_OFPBAC_BAD_ARGUMENT;
6994 }
6995
6996 nat->range_af = AF_INET;
6997
6998 if (range_present & NX_NAT_RANGE_IPV4_MAX) {
6999 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.max, opts, len,
7000 ovs_be32)) {
7001 return OFPERR_OFPBAC_BAD_ARGUMENT;
7002 }
7003 if (ntohl(nat->range.addr.ipv4.max)
7004 < ntohl(nat->range.addr.ipv4.min)) {
7005 return OFPERR_OFPBAC_BAD_ARGUMENT;
7006 }
7007 }
7008 } else if (range_present & NX_NAT_RANGE_IPV4_MAX) {
7009 return OFPERR_OFPBAC_BAD_ARGUMENT;
7010 } else if (range_present & NX_NAT_RANGE_IPV6_MIN) {
7011 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.min, opts, len,
7012 struct in6_addr)
7013 || ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
7014 return OFPERR_OFPBAC_BAD_ARGUMENT;
7015 }
7016
7017 nat->range_af = AF_INET6;
7018
7019 if (range_present & NX_NAT_RANGE_IPV6_MAX) {
7020 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.max, opts, len,
7021 struct in6_addr)) {
7022 return OFPERR_OFPBAC_BAD_ARGUMENT;
7023 }
7024 if (memcmp(&nat->range.addr.ipv6.max, &nat->range.addr.ipv6.min,
7025 sizeof(struct in6_addr)) < 0) {
7026 return OFPERR_OFPBAC_BAD_ARGUMENT;
7027 }
7028 }
7029 } else if (range_present & NX_NAT_RANGE_IPV6_MAX) {
7030 return OFPERR_OFPBAC_BAD_ARGUMENT;
7031 }
7032
7033 if (range_present & NX_NAT_RANGE_PROTO_MIN) {
7034 ovs_be16 proto;
7035
7036 if (nat->range_af == AF_UNSPEC) {
7037 return OFPERR_OFPBAC_BAD_ARGUMENT;
7038 }
7039 if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16) || proto == 0) {
7040 return OFPERR_OFPBAC_BAD_ARGUMENT;
7041 }
7042 nat->range.proto.min = ntohs(proto);
7043 if (range_present & NX_NAT_RANGE_PROTO_MAX) {
7044 if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16)) {
7045 return OFPERR_OFPBAC_BAD_ARGUMENT;
7046 }
7047 nat->range.proto.max = ntohs(proto);
7048 if (nat->range.proto.max < nat->range.proto.min) {
7049 return OFPERR_OFPBAC_BAD_ARGUMENT;
7050 }
7051 }
7052 } else if (range_present & NX_NAT_RANGE_PROTO_MAX) {
7053 return OFPERR_OFPBAC_BAD_ARGUMENT;
7054 }
7055
7056 return 0;
7057 }
7058
7059 static void
7060 format_NAT(const struct ofpact_nat *a, const struct ofpact_format_params *fp)
7061 {
7062 ds_put_format(fp->s, "%snat%s", colors.paren, colors.end);
7063
7064 if (a->flags & (NX_NAT_F_SRC | NX_NAT_F_DST)) {
7065 ds_put_format(fp->s, "%s(%s", colors.paren, colors.end);
7066 ds_put_format(fp->s, a->flags & NX_NAT_F_SRC ? "%ssrc%s" : "%sdst%s",
7067 colors.param, colors.end);
7068
7069 if (a->range_af != AF_UNSPEC) {
7070 ds_put_format(fp->s, "%s=%s", colors.param, colors.end);
7071
7072 if (a->range_af == AF_INET) {
7073 ds_put_format(fp->s, IP_FMT, IP_ARGS(a->range.addr.ipv4.min));
7074
7075 if (a->range.addr.ipv4.max
7076 && a->range.addr.ipv4.max != a->range.addr.ipv4.min) {
7077 ds_put_format(fp->s, "-"IP_FMT,
7078 IP_ARGS(a->range.addr.ipv4.max));
7079 }
7080 } else if (a->range_af == AF_INET6) {
7081 ipv6_format_addr_bracket(&a->range.addr.ipv6.min, fp->s,
7082 a->range.proto.min);
7083
7084 if (!ipv6_mask_is_any(&a->range.addr.ipv6.max)
7085 && memcmp(&a->range.addr.ipv6.max, &a->range.addr.ipv6.min,
7086 sizeof(struct in6_addr)) != 0) {
7087 ds_put_char(fp->s, '-');
7088 ipv6_format_addr_bracket(&a->range.addr.ipv6.max, fp->s,
7089 a->range.proto.min);
7090 }
7091 }
7092 if (a->range.proto.min) {
7093 ds_put_char(fp->s, ':');
7094 ds_put_format(fp->s, "%"PRIu16, a->range.proto.min);
7095
7096 if (a->range.proto.max
7097 && a->range.proto.max != a->range.proto.min) {
7098 ds_put_format(fp->s, "-%"PRIu16, a->range.proto.max);
7099 }
7100 }
7101 ds_put_char(fp->s, ',');
7102
7103 if (a->flags & NX_NAT_F_PERSISTENT) {
7104 ds_put_format(fp->s, "%spersistent%s,",
7105 colors.value, colors.end);
7106 }
7107 if (a->flags & NX_NAT_F_PROTO_HASH) {
7108 ds_put_format(fp->s, "%shash%s,", colors.value, colors.end);
7109 }
7110 if (a->flags & NX_NAT_F_PROTO_RANDOM) {
7111 ds_put_format(fp->s, "%srandom%s,", colors.value, colors.end);
7112 }
7113 }
7114 ds_chomp(fp->s, ',');
7115 ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
7116 }
7117 }
7118
7119 static char * OVS_WARN_UNUSED_RESULT
7120 str_to_nat_range(const char *s, struct ofpact_nat *on)
7121 {
7122 char ipv6_s[IPV6_SCAN_LEN + 1];
7123 int n = 0;
7124
7125 on->range_af = AF_UNSPEC;
7126 if (ovs_scan_len(s, &n, IP_SCAN_FMT,
7127 IP_SCAN_ARGS(&on->range.addr.ipv4.min))) {
7128 on->range_af = AF_INET;
7129
7130 if (s[n] == '-') {
7131 n++;
7132 if (!ovs_scan_len(s, &n, IP_SCAN_FMT,
7133 IP_SCAN_ARGS(&on->range.addr.ipv4.max))
7134 || (ntohl(on->range.addr.ipv4.max)
7135 < ntohl(on->range.addr.ipv4.min))) {
7136 goto error;
7137 }
7138 }
7139 } else if ((ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
7140 || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
7141 && inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.min) == 1) {
7142 on->range_af = AF_INET6;
7143
7144 if (s[n] == '-') {
7145 n++;
7146 if (!(ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
7147 || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
7148 || inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.max) != 1
7149 || memcmp(&on->range.addr.ipv6.max, &on->range.addr.ipv6.min,
7150 sizeof on->range.addr.ipv6.max) < 0) {
7151 goto error;
7152 }
7153 }
7154 }
7155 if (on->range_af != AF_UNSPEC && s[n] == ':') {
7156 n++;
7157 if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.min)) {
7158 goto error;
7159 }
7160 if (s[n] == '-') {
7161 n++;
7162 if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.max)
7163 || on->range.proto.max < on->range.proto.min) {
7164 goto error;
7165 }
7166 }
7167 }
7168 if (strlen(s) != n) {
7169 return xasprintf("garbage (%s) after nat range \"%s\" (pos: %d)",
7170 &s[n], s, n);
7171 }
7172 return NULL;
7173 error:
7174 return xasprintf("invalid nat range \"%s\"", s);
7175 }
7176
7177
7178 /* Parses 'arg' as the argument to a "nat" action, and appends such an
7179 * action to 'pp->ofpacts'.
7180 *
7181 * Returns NULL if successful, otherwise a malloc()'d string describing the
7182 * error. The caller is responsible for freeing the returned string. */
7183 static char * OVS_WARN_UNUSED_RESULT
7184 parse_NAT(char *arg, const struct ofpact_parse_params *pp)
7185 {
7186 struct ofpact_nat *on = ofpact_put_NAT(pp->ofpacts);
7187 char *key, *value;
7188
7189 on->flags = 0;
7190 on->range_af = AF_UNSPEC;
7191
7192 while (ofputil_parse_key_value(&arg, &key, &value)) {
7193 char *error = NULL;
7194
7195 if (!strcmp(key, "src")) {
7196 on->flags |= NX_NAT_F_SRC;
7197 error = str_to_nat_range(value, on);
7198 } else if (!strcmp(key, "dst")) {
7199 on->flags |= NX_NAT_F_DST;
7200 error = str_to_nat_range(value, on);
7201 } else if (!strcmp(key, "persistent")) {
7202 on->flags |= NX_NAT_F_PERSISTENT;
7203 } else if (!strcmp(key, "hash")) {
7204 on->flags |= NX_NAT_F_PROTO_HASH;
7205 } else if (!strcmp(key, "random")) {
7206 on->flags |= NX_NAT_F_PROTO_RANDOM;
7207 } else {
7208 error = xasprintf("invalid key \"%s\" in \"nat\" argument",
7209 key);
7210 }
7211 if (error) {
7212 return error;
7213 }
7214 }
7215 if (on->flags & NX_NAT_F_SRC && on->flags & NX_NAT_F_DST) {
7216 return xasprintf("May only specify one of \"src\" or \"dst\".");
7217 }
7218 if (!(on->flags & NX_NAT_F_SRC || on->flags & NX_NAT_F_DST)) {
7219 if (on->flags) {
7220 return xasprintf("Flags allowed only with \"src\" or \"dst\".");
7221 }
7222 if (on->range_af != AF_UNSPEC) {
7223 return xasprintf("Range allowed only with \"src\" or \"dst\".");
7224 }
7225 }
7226 if (on->flags & NX_NAT_F_PROTO_HASH && on->flags & NX_NAT_F_PROTO_RANDOM) {
7227 return xasprintf("Both \"hash\" and \"random\" are not allowed.");
7228 }
7229
7230 return NULL;
7231 }
7232
7233 static enum ofperr
7234 check_NAT(const struct ofpact_nat *a, const struct ofpact_check_params *cp)
7235 {
7236 ovs_be16 dl_type = get_dl_type(&cp->match->flow);
7237 if (!dl_type_is_ip_any(dl_type) ||
7238 (a->range_af == AF_INET && dl_type != htons(ETH_TYPE_IP)) ||
7239 (a->range_af == AF_INET6 && dl_type != htons(ETH_TYPE_IPV6))) {
7240 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
7241 }
7242 return 0;
7243 }
7244 \f
7245 /* Truncate output action. */
7246 struct nx_action_output_trunc {
7247 ovs_be16 type; /* OFPAT_VENDOR. */
7248 ovs_be16 len; /* At least 16. */
7249 ovs_be32 vendor; /* NX_VENDOR_ID. */
7250 ovs_be16 subtype; /* NXAST_OUTPUT_TRUNC. */
7251 ovs_be16 port; /* Output port */
7252 ovs_be32 max_len; /* Truncate packet to size bytes */
7253 };
7254 OFP_ASSERT(sizeof(struct nx_action_output_trunc) == 16);
7255
7256 static enum ofperr
7257 decode_NXAST_RAW_OUTPUT_TRUNC(const struct nx_action_output_trunc *natrc,
7258 enum ofp_version ofp_version OVS_UNUSED,
7259 struct ofpbuf *out)
7260 {
7261 struct ofpact_output_trunc *output_trunc;
7262
7263 output_trunc = ofpact_put_OUTPUT_TRUNC(out);
7264 output_trunc->max_len = ntohl(natrc->max_len);
7265 output_trunc->port = u16_to_ofp(ntohs(natrc->port));
7266
7267 if (output_trunc->max_len < ETH_HEADER_LEN) {
7268 return OFPERR_OFPBAC_BAD_ARGUMENT;
7269 }
7270 return 0;
7271 }
7272
7273 static void
7274 encode_OUTPUT_TRUNC(const struct ofpact_output_trunc *output_trunc,
7275 enum ofp_version ofp_version OVS_UNUSED,
7276 struct ofpbuf *out)
7277 {
7278 struct nx_action_output_trunc *natrc = put_NXAST_OUTPUT_TRUNC(out);
7279
7280 natrc->max_len = htonl(output_trunc->max_len);
7281 natrc->port = htons(ofp_to_u16(output_trunc->port));
7282 }
7283
7284 static char * OVS_WARN_UNUSED_RESULT
7285 parse_OUTPUT_TRUNC(const char *arg,
7286 const struct ofpact_parse_params *pp OVS_UNUSED)
7287 {
7288 /* Disable output_trunc parsing. Expose as output(port=N,max_len=M) and
7289 * reuse parse_OUTPUT to parse output_trunc action. */
7290 return xasprintf("unknown action %s", arg);
7291 }
7292
7293 static void
7294 format_OUTPUT_TRUNC(const struct ofpact_output_trunc *a,
7295 const struct ofpact_format_params *fp)
7296 {
7297 ds_put_format(fp->s, "%soutput%s(port=", colors.special, colors.end);
7298 ofputil_format_port(a->port, fp->port_map, fp->s);
7299 ds_put_format(fp->s, ",max_len=%"PRIu32")", a->max_len);
7300 }
7301
7302 static enum ofperr
7303 check_OUTPUT_TRUNC(const struct ofpact_output_trunc *a,
7304 const struct ofpact_check_params *cp)
7305 {
7306 return ofpact_check_output_port(a->port, cp->max_ports);
7307 }
7308 \f
7309 /* Meter.
7310 *
7311 * In OpenFlow 1.3 and 1.4, "meter" is an instruction.
7312 * In OpenFlow 1.5 and later, "meter" is an action.
7313 *
7314 * OpenFlow 1.5 */
7315
7316 static enum ofperr
7317 decode_OFPAT_RAW15_METER(uint32_t meter_id,
7318 enum ofp_version ofp_version OVS_UNUSED,
7319 struct ofpbuf *out)
7320 {
7321 struct ofpact_meter *om = ofpact_put_METER(out);
7322 om->meter_id = meter_id;
7323 om->provider_meter_id = UINT32_MAX; /* No provider meter ID. */
7324 return 0;
7325 }
7326
7327 static void
7328 encode_METER(const struct ofpact_meter *meter,
7329 enum ofp_version ofp_version, struct ofpbuf *out)
7330 {
7331 if (ofp_version == OFP13_VERSION || ofp_version == OFP14_VERSION) {
7332 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
7333 } else if (ofp_version >= OFP15_VERSION) {
7334 put_OFPAT15_METER(out, meter->meter_id);
7335 }
7336 }
7337
7338 static char * OVS_WARN_UNUSED_RESULT
7339 parse_METER(char *arg, const struct ofpact_parse_params *pp)
7340 {
7341 *pp->usable_protocols &= OFPUTIL_P_OF13_UP;
7342 return str_to_u32(arg, &ofpact_put_METER(pp->ofpacts)->meter_id);
7343 }
7344
7345 static void
7346 format_METER(const struct ofpact_meter *a,
7347 const struct ofpact_format_params *fp)
7348 {
7349 ds_put_format(fp->s, "%smeter:%s%"PRIu32,
7350 colors.param, colors.end, a->meter_id);
7351 }
7352
7353 static enum ofperr
7354 check_METER(const struct ofpact_meter *a,
7355 const struct ofpact_check_params *cp OVS_UNUSED)
7356 {
7357 uint32_t mid = a->meter_id;
7358 return mid == 0 || mid > OFPM13_MAX ? OFPERR_OFPMMFC_INVALID_METER : 0;
7359 }
7360 \f
7361 /* Clear-Actions instruction. */
7362
7363 static void
7364 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
7365 enum ofp_version ofp_version OVS_UNUSED,
7366 struct ofpbuf *out OVS_UNUSED)
7367 {
7368 if (ofp_version > OFP10_VERSION) {
7369 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
7370 }
7371 }
7372
7373 static char * OVS_WARN_UNUSED_RESULT
7374 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
7375 {
7376 ofpact_put_CLEAR_ACTIONS(pp->ofpacts);
7377 return NULL;
7378 }
7379
7380 static void
7381 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED,
7382 const struct ofpact_format_params *fp)
7383 {
7384 ds_put_format(fp->s, "%sclear_actions%s", colors.value, colors.end);
7385 }
7386
7387 static enum ofperr
7388 check_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED,
7389 const struct ofpact_check_params *cp OVS_UNUSED)
7390 {
7391 return 0;
7392 }
7393 \f
7394 /* Write-Actions instruction. */
7395
7396 static void
7397 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
7398 enum ofp_version ofp_version, struct ofpbuf *out)
7399 {
7400 if (ofp_version > OFP10_VERSION) {
7401 const size_t ofs = out->size;
7402
7403 instruction_put_OFPIT11_WRITE_ACTIONS(out);
7404 ofpacts_put_openflow_actions(actions->actions,
7405 ofpact_nest_get_action_len(actions),
7406 out, ofp_version);
7407 ofpacts_update_instruction_actions(out, ofs);
7408 }
7409 }
7410
7411 static char * OVS_WARN_UNUSED_RESULT
7412 parse_WRITE_ACTIONS(char *arg, const struct ofpact_parse_params *pp)
7413 {
7414 size_t ofs = ofpacts_pull(pp->ofpacts);
7415 struct ofpact_nest *on;
7416 char *error;
7417
7418 /* Add a Write-Actions instruction and then pull it off. */
7419 ofpact_put(pp->ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
7420 ofpbuf_pull(pp->ofpacts, sizeof *on);
7421
7422 /* Parse nested actions.
7423 *
7424 * We pulled off "write-actions" and the previous actions because the
7425 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
7426 * that it doesn't actually include the nested actions. That means that
7427 * ofpacts_parse() would reject them as being part of an Apply-Actions that
7428 * follows a Write-Actions, which is an invalid order. */
7429 error = ofpacts_parse(arg, pp, false, OFPACT_WRITE_ACTIONS);
7430
7431 /* Put the Write-Actions back on and update its length. */
7432 on = ofpbuf_push_uninit(pp->ofpacts, sizeof *on);
7433 on->ofpact.len = pp->ofpacts->size;
7434
7435 /* Put any previous actions or instructions back on. */
7436 ofpbuf_push_uninit(pp->ofpacts, ofs);
7437
7438 return error;
7439 }
7440
7441 static void
7442 format_WRITE_ACTIONS(const struct ofpact_nest *a,
7443 const struct ofpact_format_params *fp)
7444 {
7445 ds_put_format(fp->s, "%swrite_actions(%s", colors.paren, colors.end);
7446 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), fp);
7447 ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
7448 }
7449
7450 static enum ofperr
7451 check_WRITE_ACTIONS(struct ofpact_nest *a,
7452 const struct ofpact_check_params *cp)
7453 {
7454 /* Use a temporary copy of 'cp' to avoid updating 'cp->usable_protocols',
7455 * since we can't check consistency of an action set. */
7456 struct ofpact_check_params tmp = *cp;
7457 return ofpacts_check(a->actions, ofpact_nest_get_action_len(a), &tmp);
7458 }
7459 \f
7460 /* Action structure for NXAST_WRITE_METADATA.
7461 *
7462 * Modifies the 'mask' bits of the metadata value. */
7463 struct nx_action_write_metadata {
7464 ovs_be16 type; /* OFPAT_VENDOR. */
7465 ovs_be16 len; /* Length is 32. */
7466 ovs_be32 vendor; /* NX_VENDOR_ID. */
7467 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
7468 uint8_t zeros[6]; /* Must be zero. */
7469 ovs_be64 metadata; /* Metadata register. */
7470 ovs_be64 mask; /* Metadata mask. */
7471 };
7472 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
7473
7474 static enum ofperr
7475 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
7476 enum ofp_version ofp_version OVS_UNUSED,
7477 struct ofpbuf *out)
7478 {
7479 struct ofpact_metadata *om;
7480
7481 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
7482 return OFPERR_NXBRC_MUST_BE_ZERO;
7483 }
7484
7485 om = ofpact_put_WRITE_METADATA(out);
7486 om->metadata = nawm->metadata;
7487 om->mask = nawm->mask;
7488
7489 return 0;
7490 }
7491
7492 static void
7493 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
7494 enum ofp_version ofp_version, struct ofpbuf *out)
7495 {
7496 if (ofp_version == OFP10_VERSION) {
7497 struct nx_action_write_metadata *nawm;
7498
7499 nawm = put_NXAST_WRITE_METADATA(out);
7500 nawm->metadata = metadata->metadata;
7501 nawm->mask = metadata->mask;
7502 } else {
7503 struct ofp11_instruction_write_metadata *oiwm;
7504
7505 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
7506 oiwm->metadata = metadata->metadata;
7507 oiwm->metadata_mask = metadata->mask;
7508 }
7509 }
7510
7511 static char * OVS_WARN_UNUSED_RESULT
7512 parse_WRITE_METADATA(char *arg, const struct ofpact_parse_params *pp)
7513 {
7514 struct ofpact_metadata *om;
7515 char *mask = strchr(arg, '/');
7516
7517 *pp->usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
7518
7519 om = ofpact_put_WRITE_METADATA(pp->ofpacts);
7520 if (mask) {
7521 char *error;
7522
7523 *mask = '\0';
7524 error = str_to_be64(mask + 1, &om->mask);
7525 if (error) {
7526 return error;
7527 }
7528 } else {
7529 om->mask = OVS_BE64_MAX;
7530 }
7531
7532 return str_to_be64(arg, &om->metadata);
7533 }
7534
7535 static void
7536 format_WRITE_METADATA(const struct ofpact_metadata *a,
7537 const struct ofpact_format_params *fp)
7538 {
7539 ds_put_format(fp->s, "%swrite_metadata:%s%#"PRIx64,
7540 colors.param, colors.end, ntohll(a->metadata));
7541 if (a->mask != OVS_BE64_MAX) {
7542 ds_put_format(fp->s, "/%#"PRIx64, ntohll(a->mask));
7543 }
7544 }
7545
7546 static enum ofperr
7547 check_WRITE_METADATA(const struct ofpact_metadata *a OVS_UNUSED,
7548 const struct ofpact_check_params *cp OVS_UNUSED)
7549 {
7550 return 0;
7551 }
7552 \f
7553 /* Check packet length action. */
7554
7555 struct nx_action_check_pkt_larger {
7556 ovs_be16 type; /* OFPAT_VENDOR. */
7557 ovs_be16 len; /* 24. */
7558 ovs_be32 vendor; /* NX_VENDOR_ID. */
7559 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
7560 ovs_be16 pkt_len; /* Length of the packet to check. */
7561 ovs_be16 offset; /* Result bit offset in destination. */
7562 /* Followed by:
7563 * - 'dst', as an OXM/NXM header (either 4 or 8 bytes).
7564 * - Enough 0-bytes to pad the action out to 24 bytes. */
7565 uint8_t pad[10];
7566 };
7567
7568 OFP_ASSERT(sizeof(struct nx_action_check_pkt_larger) == 24);
7569
7570 static enum ofperr
7571 decode_NXAST_RAW_CHECK_PKT_LARGER(
7572 const struct nx_action_check_pkt_larger *ncpl,
7573 enum ofp_version ofp_version OVS_UNUSED,
7574 const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap,
7575 struct ofpbuf *out)
7576 {
7577 struct ofpact_check_pkt_larger *check_pkt_larger;
7578 enum ofperr error;
7579
7580 check_pkt_larger = ofpact_put_CHECK_PKT_LARGER(out);
7581 check_pkt_larger->pkt_len = ntohs(ncpl->pkt_len);
7582 check_pkt_larger->dst.ofs = ntohs(ncpl->offset);
7583 check_pkt_larger->dst.n_bits = 1;
7584
7585 struct ofpbuf b = ofpbuf_const_initializer(ncpl, ntohs(ncpl->len));
7586 ofpbuf_pull(&b, OBJECT_OFFSETOF(ncpl, pad));
7587
7588 error = mf_vl_mff_nx_pull_header(&b, vl_mff_map,
7589 &check_pkt_larger->dst.field,
7590 NULL, tlv_bitmap);
7591 if (error) {
7592 return error;
7593 }
7594
7595 if (!is_all_zeros(b.data, b.size)) {
7596 return OFPERR_NXBRC_MUST_BE_ZERO;
7597 }
7598
7599 return mf_check_dst(&check_pkt_larger->dst, NULL);
7600 }
7601
7602 static void
7603 encode_CHECK_PKT_LARGER(const struct ofpact_check_pkt_larger *check_pkt_larger,
7604 enum ofp_version ofp_version OVS_UNUSED,
7605 struct ofpbuf *out)
7606 {
7607 struct nx_action_check_pkt_larger *ncpl = put_NXAST_CHECK_PKT_LARGER(out);
7608 ncpl->pkt_len = htons(check_pkt_larger->pkt_len);
7609 ncpl->offset = htons(check_pkt_larger->dst.ofs);
7610
7611 if (check_pkt_larger->dst.field) {
7612 size_t size = out->size;
7613 out->size = size - sizeof ncpl->pad;
7614 nx_put_mff_header(out, check_pkt_larger->dst.field, 0, false);
7615 out->size = size;
7616 }
7617 }
7618
7619 static char * OVS_WARN_UNUSED_RESULT
7620 parse_CHECK_PKT_LARGER(char *arg, const struct ofpact_parse_params *pp)
7621 {
7622 char *value;
7623 char *delim;
7624 char *key;
7625 char *error = set_field_split_str(arg, &key, &value, &delim);
7626 if (error) {
7627 return error;
7628 }
7629
7630 delim[0] = '\0';
7631 if (value[strlen(value) - 1] == ')') {
7632 value[strlen(value) - 1] = '\0';
7633 }
7634 struct mf_subfield dst;
7635 error = mf_parse_subfield(&dst, key);
7636 if (error) {
7637 return error;
7638 }
7639
7640 if (dst.n_bits != 1) {
7641 return xstrdup("Only 1-bit destination field is allowed");
7642 }
7643
7644 struct ofpact_check_pkt_larger *check_pkt_larger =
7645 ofpact_put_CHECK_PKT_LARGER(pp->ofpacts);
7646 error = str_to_u16(value, NULL, &check_pkt_larger->pkt_len);
7647 if (error) {
7648 return error;
7649 }
7650 check_pkt_larger->dst = dst;
7651 return NULL;
7652 }
7653
7654 static void
7655 format_CHECK_PKT_LARGER(const struct ofpact_check_pkt_larger *a,
7656 const struct ofpact_format_params *fp)
7657 {
7658 ds_put_format(fp->s, "%scheck_pkt_larger(%s%"PRIu32")->",
7659 colors.param, colors.end, a->pkt_len);
7660 mf_format_subfield(&a->dst, fp->s);
7661 }
7662
7663 static enum ofperr
7664 check_CHECK_PKT_LARGER(const struct ofpact_check_pkt_larger *a OVS_UNUSED,
7665 const struct ofpact_check_params *cp OVS_UNUSED)
7666 {
7667 return 0;
7668 }
7669 \f
7670
7671 /* Goto-Table instruction. */
7672
7673 static void
7674 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
7675 enum ofp_version ofp_version, struct ofpbuf *out)
7676 {
7677 if (ofp_version == OFP10_VERSION) {
7678 struct nx_action_resubmit *nar;
7679
7680 nar = put_NXAST_RESUBMIT_TABLE(out);
7681 nar->table = goto_table->table_id;
7682 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
7683 } else {
7684 struct ofp11_instruction_goto_table *oigt;
7685
7686 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
7687 oigt->table_id = goto_table->table_id;
7688 memset(oigt->pad, 0, sizeof oigt->pad);
7689 }
7690 }
7691
7692 static char * OVS_WARN_UNUSED_RESULT
7693 parse_GOTO_TABLE(char *arg, const struct ofpact_parse_params *pp)
7694 {
7695 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(pp->ofpacts);
7696 if (!ofputil_table_from_string(arg, pp->table_map, &ogt->table_id)) {
7697 return xasprintf("unknown table \"%s\"", arg);
7698 }
7699 return NULL;
7700 }
7701
7702 static void
7703 format_GOTO_TABLE(const struct ofpact_goto_table *a,
7704 const struct ofpact_format_params *fp)
7705 {
7706 ds_put_format(fp->s, "%sgoto_table:%s", colors.param, colors.end);
7707 ofputil_format_table(a->table_id, fp->table_map, fp->s);
7708 }
7709
7710 static enum ofperr
7711 check_GOTO_TABLE(const struct ofpact_goto_table *a,
7712 const struct ofpact_check_params *cp)
7713 {
7714 if ((cp->table_id != 255 && a->table_id <= cp->table_id)
7715 || (cp->n_tables != 255 && a->table_id >= cp->n_tables)) {
7716 return OFPERR_OFPBIC_BAD_TABLE_ID;
7717 }
7718 return 0;
7719 }
7720 \f
7721 static void
7722 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
7723 const struct ofp_action_header *bad_action, enum ofperr error)
7724 {
7725 if (!VLOG_DROP_WARN(&rl)) {
7726 struct ds s;
7727
7728 ds_init(&s);
7729 ds_put_hex_dump(&s, actions, actions_len, 0, false);
7730 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
7731 (char *)bad_action - (char *)actions,
7732 ofperr_get_name(error), ds_cstr(&s));
7733 ds_destroy(&s);
7734 }
7735 }
7736
7737 static enum ofperr
7738 ofpacts_decode(const void *actions, size_t actions_len,
7739 enum ofp_version ofp_version,
7740 const struct vl_mff_map *vl_mff_map,
7741 uint64_t *ofpacts_tlv_bitmap, struct ofpbuf *ofpacts)
7742 {
7743 struct ofpbuf openflow = ofpbuf_const_initializer(actions, actions_len);
7744 while (openflow.size) {
7745 const struct ofp_action_header *action = openflow.data;
7746 enum ofp_raw_action_type raw;
7747 enum ofperr error;
7748 uint64_t arg;
7749
7750 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
7751 if (!error) {
7752 error = ofpact_decode(action, raw, ofp_version, arg, vl_mff_map,
7753 ofpacts_tlv_bitmap, ofpacts);
7754 }
7755
7756 if (error) {
7757 log_bad_action(actions, actions_len, action, error);
7758 return error;
7759 }
7760 }
7761 return 0;
7762 }
7763
7764 static enum ofperr
7765 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
7766 unsigned int actions_len,
7767 enum ofp_version version,
7768 uint32_t allowed_ovsinsts,
7769 struct ofpbuf *ofpacts,
7770 enum ofpact_type outer_action,
7771 const struct vl_mff_map *vl_mff_map,
7772 uint64_t *ofpacts_tlv_bitmap)
7773 {
7774 const struct ofp_action_header *actions;
7775 enum ofperr error;
7776
7777 if (actions_len % OFP_ACTION_ALIGN != 0) {
7778 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
7779 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
7780 return OFPERR_OFPBRC_BAD_LEN;
7781 }
7782
7783 actions = ofpbuf_try_pull(openflow, actions_len);
7784 if (actions == NULL) {
7785 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
7786 "remaining message length (%"PRIu32")",
7787 actions_len, openflow->size);
7788 return OFPERR_OFPBRC_BAD_LEN;
7789 }
7790
7791 error = ofpacts_decode(actions, actions_len, version, vl_mff_map,
7792 ofpacts_tlv_bitmap, ofpacts);
7793 if (!error) {
7794 error = ofpacts_verify(ofpacts->data, ofpacts->size, version,
7795 allowed_ovsinsts, outer_action, NULL);
7796 }
7797 if (error) {
7798 ofpbuf_clear(ofpacts);
7799 }
7800 return error;
7801 }
7802
7803 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the front
7804 * of 'openflow' into ofpacts. On success, appends the converted actions to
7805 * 'ofpacts'; on failure, clears 'ofpacts'. Returns 0 if successful, otherwise
7806 * an OpenFlow error.
7807 *
7808 * Actions are processed according to their OpenFlow version which
7809 * is provided in the 'version' parameter.
7810 *
7811 * In most places in OpenFlow, actions appear encapsulated in instructions, so
7812 * you should call ofpacts_pull_openflow_instructions() instead of this
7813 * function.
7814 *
7815 * 'vl_mff_map' and 'ofpacts_tlv_bitmap' are optional. If 'vl_mff_map' is
7816 * provided, it is used to get variable length mf_fields with configured
7817 * length in the actions. If an action uses a variable length mf_field,
7818 * 'ofpacts_tlv_bitmap' is updated accordingly for ref counting. If
7819 * 'vl_mff_map' is not provided, the default mf_fields with maximum length
7820 * will be used.
7821 *
7822 * The parsed actions are valid generically, but they may not be valid in a
7823 * specific context. For example, port numbers up to OFPP_MAX are valid
7824 * generically, but specific datapaths may only support port numbers in a
7825 * smaller range. Use ofpacts_check() to additional check whether actions are
7826 * valid in a specific context. */
7827 enum ofperr
7828 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
7829 unsigned int actions_len,
7830 enum ofp_version version,
7831 const struct vl_mff_map *vl_mff_map,
7832 uint64_t *ofpacts_tlv_bitmap,
7833 struct ofpbuf *ofpacts)
7834 {
7835 return ofpacts_pull_openflow_actions__(
7836 openflow, actions_len, version,
7837 (1u << OVSINST_OFPIT11_APPLY_ACTIONS) | (1u << OVSINST_OFPIT13_METER),
7838 ofpacts, 0, vl_mff_map, ofpacts_tlv_bitmap);
7839 }
7840 \f
7841 /* OpenFlow 1.1 action sets. */
7842
7843 /* Append ofpact 'a' onto the tail of 'out' */
7844 static void
7845 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
7846 {
7847 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
7848 }
7849
7850 /* The order in which actions in an action set get executed. This is only for
7851 * the actions where only the last instance added is used. */
7852 #define ACTION_SET_ORDER \
7853 SLOT(OFPACT_STRIP_VLAN) \
7854 SLOT(OFPACT_POP_MPLS) \
7855 SLOT(OFPACT_DECAP) \
7856 SLOT(OFPACT_ENCAP) \
7857 SLOT(OFPACT_PUSH_MPLS) \
7858 SLOT(OFPACT_PUSH_VLAN) \
7859 SLOT(OFPACT_DEC_TTL) \
7860 SLOT(OFPACT_DEC_MPLS_TTL) \
7861 SLOT(OFPACT_DEC_NSH_TTL)
7862
7863 /* Priority for "final actions" in an action set. An action set only gets
7864 * executed at all if at least one of these actions is present. If more than
7865 * one is present, then only the one later in this list is executed (and if
7866 * more than one of a given type, the one later in the action set). */
7867 #define ACTION_SET_FINAL_PRIORITY \
7868 FINAL(OFPACT_CT) \
7869 FINAL(OFPACT_CT_CLEAR) \
7870 FINAL(OFPACT_RESUBMIT) \
7871 FINAL(OFPACT_OUTPUT) \
7872 FINAL(OFPACT_GROUP)
7873
7874 enum action_set_class {
7875 /* Actions that individually can usefully appear only once in an action
7876 * set. If they do appear more than once, then only the last instance is
7877 * honored. */
7878 #define SLOT(OFPACT) ACTION_SLOT_##OFPACT,
7879 ACTION_SET_ORDER
7880 #undef SLOT
7881
7882 /* Final actions. */
7883 #define FINAL(OFPACT) ACTION_SLOT_##OFPACT,
7884 ACTION_SET_FINAL_PRIORITY
7885 #undef FINAL
7886
7887 /* Actions that can appear in an action set more than once and are executed
7888 * in order. */
7889 ACTION_SLOT_SET_OR_MOVE,
7890
7891 /* Actions that shouldn't appear in the action set at all. */
7892 ACTION_SLOT_INVALID
7893 };
7894
7895 /* Count the action set slots. */
7896 #define SLOT(OFPACT) +1
7897 enum { N_ACTION_SLOTS = ACTION_SET_ORDER };
7898 #undef SLOT
7899
7900 static enum action_set_class
7901 action_set_classify(const struct ofpact *a)
7902 {
7903 switch (a->type) {
7904 #define SLOT(OFPACT) case OFPACT: return ACTION_SLOT_##OFPACT;
7905 ACTION_SET_ORDER
7906 #undef SLOT
7907
7908 #define FINAL(OFPACT) case OFPACT: return ACTION_SLOT_##OFPACT;
7909 ACTION_SET_FINAL_PRIORITY
7910 #undef FINAL
7911
7912 case OFPACT_SET_FIELD:
7913 case OFPACT_REG_MOVE:
7914 case OFPACT_SET_ETH_DST:
7915 case OFPACT_SET_ETH_SRC:
7916 case OFPACT_SET_IP_DSCP:
7917 case OFPACT_SET_IP_ECN:
7918 case OFPACT_SET_IP_TTL:
7919 case OFPACT_SET_IPV4_DST:
7920 case OFPACT_SET_IPV4_SRC:
7921 case OFPACT_SET_L4_DST_PORT:
7922 case OFPACT_SET_L4_SRC_PORT:
7923 case OFPACT_SET_MPLS_LABEL:
7924 case OFPACT_SET_MPLS_TC:
7925 case OFPACT_SET_MPLS_TTL:
7926 case OFPACT_SET_QUEUE:
7927 case OFPACT_SET_TUNNEL:
7928 case OFPACT_SET_VLAN_PCP:
7929 case OFPACT_SET_VLAN_VID:
7930 return ACTION_SLOT_SET_OR_MOVE;
7931
7932 case OFPACT_BUNDLE:
7933 case OFPACT_CLEAR_ACTIONS:
7934 case OFPACT_CLONE:
7935 case OFPACT_NAT:
7936 case OFPACT_CONTROLLER:
7937 case OFPACT_ENQUEUE:
7938 case OFPACT_EXIT:
7939 case OFPACT_UNROLL_XLATE:
7940 case OFPACT_FIN_TIMEOUT:
7941 case OFPACT_GOTO_TABLE:
7942 case OFPACT_LEARN:
7943 case OFPACT_CONJUNCTION:
7944 case OFPACT_METER:
7945 case OFPACT_MULTIPATH:
7946 case OFPACT_NOTE:
7947 case OFPACT_OUTPUT_REG:
7948 case OFPACT_OUTPUT_TRUNC:
7949 case OFPACT_POP_QUEUE:
7950 case OFPACT_SAMPLE:
7951 case OFPACT_STACK_POP:
7952 case OFPACT_STACK_PUSH:
7953 case OFPACT_WRITE_ACTIONS:
7954 case OFPACT_WRITE_METADATA:
7955 case OFPACT_DEBUG_RECIRC:
7956 case OFPACT_DEBUG_SLOW:
7957 case OFPACT_CHECK_PKT_LARGER:
7958 case OFPACT_DELETE_FIELD:
7959 return ACTION_SLOT_INVALID;
7960
7961 default:
7962 OVS_NOT_REACHED();
7963 }
7964 }
7965
7966 /* True if an action is allowed in the action set.
7967 * False otherwise. */
7968 static bool
7969 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
7970 {
7971 return action_set_classify(a) != ACTION_SLOT_INVALID;
7972 }
7973
7974 /* Reads 'action_set', which contains ofpacts accumulated by
7975 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
7976 * executed directly into 'action_list'. (These names correspond to the
7977 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
7978 *
7979 * In general this involves appending the last instance of each action that is
7980 * admissible in the action set in the order described in the OpenFlow
7981 * specification.
7982 *
7983 * Exceptions:
7984 * + output action is only appended if no group action was present in 'in'.
7985 * + As a simplification all set actions are copied in the order the are
7986 * provided in 'in' as many set actions applied to a field has the same
7987 * affect as only applying the last action that sets a field and
7988 * duplicates are removed by do_xlate_actions().
7989 * This has an unwanted side-effect of compsoting multiple
7990 * LOAD_REG actions that touch different regions of the same field. */
7991 void
7992 ofpacts_execute_action_set(struct ofpbuf *action_list,
7993 const struct ofpbuf *action_set)
7994 {
7995 const struct ofpact *slots[N_ACTION_SLOTS] = {NULL, };
7996
7997 struct ofpbuf set_or_move;
7998 ofpbuf_init(&set_or_move, 0);
7999
8000 const struct ofpact *final_action = NULL;
8001 enum action_set_class final_class = 0;
8002
8003 const struct ofpact *cursor;
8004 OFPACT_FOR_EACH (cursor, action_set->data, action_set->size) {
8005 int class = action_set_classify(cursor);
8006 if (class < N_ACTION_SLOTS) {
8007 slots[class] = cursor;
8008 } else if (class < ACTION_SLOT_SET_OR_MOVE) {
8009 if (class >= final_class) {
8010 final_action = cursor;
8011 final_class = class;
8012 }
8013 } else if (class == ACTION_SLOT_SET_OR_MOVE) {
8014 ofpact_copy(&set_or_move, cursor);
8015 } else {
8016 ovs_assert(class == ACTION_SLOT_INVALID);
8017 }
8018 }
8019
8020 if (final_action) {
8021 for (int i = 0; i < N_ACTION_SLOTS; i++) {
8022 if (slots[i]) {
8023 ofpact_copy(action_list, slots[i]);
8024 }
8025 }
8026 ofpbuf_put(action_list, set_or_move.data, set_or_move.size);
8027 ofpact_copy(action_list, final_action);
8028 }
8029 ofpbuf_uninit(&set_or_move);
8030 }
8031
8032
8033 static enum ofperr
8034 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
8035 size_t n_in, enum ofp_version version,
8036 const struct vl_mff_map *vl_mff_map,
8037 uint64_t *ofpacts_tlv_bitmap,
8038 struct ofpbuf *out)
8039 {
8040 enum ofperr error;
8041 struct ofpact *a;
8042 size_t start = out->size;
8043
8044 error = ofpacts_decode(in, n_in, version, vl_mff_map, ofpacts_tlv_bitmap,
8045 out);
8046
8047 if (error) {
8048 return error;
8049 }
8050
8051 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
8052 if (!ofpact_is_allowed_in_actions_set(a)) {
8053 VLOG_WARN_RL(&rl, "disallowed action in action set");
8054 return OFPERR_OFPBAC_BAD_TYPE;
8055 }
8056 }
8057
8058 return 0;
8059 }
8060 \f
8061 /* OpenFlow 1.1 instructions. */
8062
8063 struct instruction_type_info {
8064 enum ovs_instruction_type type;
8065 const char *name;
8066 };
8067
8068 static const struct instruction_type_info inst_info[] = {
8069 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
8070 OVS_INSTRUCTIONS
8071 #undef DEFINE_INST
8072 };
8073
8074 const char *
8075 ovs_instruction_name_from_type(enum ovs_instruction_type type)
8076 {
8077 return type < ARRAY_SIZE(inst_info) ? inst_info[type].name : NULL;
8078 }
8079
8080 int
8081 ovs_instruction_type_from_name(const char *name)
8082 {
8083 const struct instruction_type_info *p;
8084 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
8085 if (!strcasecmp(name, p->name)) {
8086 return p->type;
8087 }
8088 }
8089 return -1;
8090 }
8091
8092 enum ovs_instruction_type
8093 ovs_instruction_type_from_ofpact_type(enum ofpact_type type,
8094 enum ofp_version version)
8095 {
8096 switch (type) {
8097 case OFPACT_METER:
8098 return (version >= OFP15_VERSION
8099 ? OVSINST_OFPIT11_APPLY_ACTIONS
8100 : OVSINST_OFPIT13_METER);
8101 case OFPACT_CLEAR_ACTIONS:
8102 return OVSINST_OFPIT11_CLEAR_ACTIONS;
8103 case OFPACT_WRITE_ACTIONS:
8104 return OVSINST_OFPIT11_WRITE_ACTIONS;
8105 case OFPACT_WRITE_METADATA:
8106 return OVSINST_OFPIT11_WRITE_METADATA;
8107 case OFPACT_GOTO_TABLE:
8108 return OVSINST_OFPIT11_GOTO_TABLE;
8109 case OFPACT_OUTPUT:
8110 case OFPACT_GROUP:
8111 case OFPACT_CLONE:
8112 case OFPACT_CONTROLLER:
8113 case OFPACT_ENQUEUE:
8114 case OFPACT_OUTPUT_REG:
8115 case OFPACT_OUTPUT_TRUNC:
8116 case OFPACT_BUNDLE:
8117 case OFPACT_SET_VLAN_VID:
8118 case OFPACT_SET_VLAN_PCP:
8119 case OFPACT_STRIP_VLAN:
8120 case OFPACT_PUSH_VLAN:
8121 case OFPACT_SET_ETH_SRC:
8122 case OFPACT_SET_ETH_DST:
8123 case OFPACT_SET_IPV4_SRC:
8124 case OFPACT_SET_IPV4_DST:
8125 case OFPACT_SET_IP_DSCP:
8126 case OFPACT_SET_IP_ECN:
8127 case OFPACT_SET_IP_TTL:
8128 case OFPACT_SET_L4_SRC_PORT:
8129 case OFPACT_SET_L4_DST_PORT:
8130 case OFPACT_REG_MOVE:
8131 case OFPACT_SET_FIELD:
8132 case OFPACT_STACK_PUSH:
8133 case OFPACT_STACK_POP:
8134 case OFPACT_DEC_TTL:
8135 case OFPACT_SET_MPLS_LABEL:
8136 case OFPACT_SET_MPLS_TC:
8137 case OFPACT_SET_MPLS_TTL:
8138 case OFPACT_DEC_MPLS_TTL:
8139 case OFPACT_PUSH_MPLS:
8140 case OFPACT_POP_MPLS:
8141 case OFPACT_SET_TUNNEL:
8142 case OFPACT_SET_QUEUE:
8143 case OFPACT_POP_QUEUE:
8144 case OFPACT_FIN_TIMEOUT:
8145 case OFPACT_RESUBMIT:
8146 case OFPACT_LEARN:
8147 case OFPACT_CONJUNCTION:
8148 case OFPACT_MULTIPATH:
8149 case OFPACT_NOTE:
8150 case OFPACT_EXIT:
8151 case OFPACT_UNROLL_XLATE:
8152 case OFPACT_SAMPLE:
8153 case OFPACT_DEBUG_RECIRC:
8154 case OFPACT_DEBUG_SLOW:
8155 case OFPACT_CT:
8156 case OFPACT_CT_CLEAR:
8157 case OFPACT_NAT:
8158 case OFPACT_ENCAP:
8159 case OFPACT_DECAP:
8160 case OFPACT_DEC_NSH_TTL:
8161 case OFPACT_CHECK_PKT_LARGER:
8162 case OFPACT_DELETE_FIELD:
8163 default:
8164 return OVSINST_OFPIT11_APPLY_ACTIONS;
8165 }
8166 }
8167
8168 enum ofperr
8169 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
8170 const uint16_t inst_type)
8171 {
8172 switch (inst_type) {
8173
8174 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
8175 case ENUM: \
8176 *instruction_type = OVSINST_##ENUM; \
8177 return 0;
8178 OVS_INSTRUCTIONS
8179 #undef DEFINE_INST
8180
8181 default:
8182 return OFPERR_OFPBIC_UNKNOWN_INST;
8183 }
8184 }
8185
8186 /* Two-way translation between OVS's internal "OVSINST_*" representation of
8187 * instructions and the "OFPIT_*" representation used in OpenFlow. */
8188 struct ovsinst_map {
8189 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
8190 int ofpit; /* OFPIT_* number from OpenFlow spec. */
8191 };
8192
8193 static const struct ovsinst_map *
8194 get_ovsinst_map(enum ofp_version version)
8195 {
8196 /* OpenFlow 1.1, 1.2, and 1.5 instructions. */
8197 static const struct ovsinst_map of11[] = {
8198 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
8199 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
8200 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
8201 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
8202 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
8203 { 0, -1 },
8204 };
8205
8206 /* OpenFlow 1.3 and 1.4 instructions. */
8207 static const struct ovsinst_map of13[] = {
8208 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
8209 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
8210 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
8211 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
8212 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
8213 { OVSINST_OFPIT13_METER, 6 },
8214 { 0, -1 },
8215 };
8216
8217 return version == OFP13_VERSION || version == OFP14_VERSION ? of13 : of11;
8218 }
8219
8220 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
8221 * values, into a bitmap of instructions suitable for OpenFlow 'version'
8222 * (OFP11_VERSION or later), and returns the result. */
8223 ovs_be32
8224 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
8225 {
8226 uint32_t ofpit_bitmap = 0;
8227 const struct ovsinst_map *x;
8228
8229 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
8230 if (ovsinst_bitmap & (1u << x->ovsinst)) {
8231 ofpit_bitmap |= 1u << x->ofpit;
8232 }
8233 }
8234 return htonl(ofpit_bitmap);
8235 }
8236
8237 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
8238 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
8239 * correspond to OVSINST_* values, and returns the result. */
8240 uint32_t
8241 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
8242 {
8243 uint32_t ovsinst_bitmap = 0;
8244 const struct ovsinst_map *x;
8245
8246 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
8247 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
8248 ovsinst_bitmap |= 1u << x->ovsinst;
8249 }
8250 }
8251 return ovsinst_bitmap;
8252 }
8253
8254 static inline struct ofp11_instruction *
8255 instruction_next(const struct ofp11_instruction *inst)
8256 {
8257 return ((struct ofp11_instruction *) (void *)
8258 ((uint8_t *) inst + ntohs(inst->len)));
8259 }
8260
8261 static inline bool
8262 instruction_is_valid(const struct ofp11_instruction *inst,
8263 size_t n_instructions)
8264 {
8265 uint16_t len = ntohs(inst->len);
8266 return (!(len % OFP11_INSTRUCTION_ALIGN)
8267 && len >= sizeof *inst
8268 && len / sizeof *inst <= n_instructions);
8269 }
8270
8271 /* This macro is careful to check for instructions with bad lengths. */
8272 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
8273 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
8274 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
8275 ((LEFT) -= (ntohs((ITER)->len) \
8276 / sizeof(struct ofp11_instruction)), \
8277 (ITER) = instruction_next(ITER)))
8278
8279 static enum ofperr
8280 decode_openflow11_instruction(const struct ofp11_instruction *inst,
8281 enum ovs_instruction_type *type)
8282 {
8283 uint16_t len = ntohs(inst->len);
8284
8285 switch (inst->type) {
8286 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
8287 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
8288
8289 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
8290 case CONSTANT_HTONS(ENUM): \
8291 if (EXTENSIBLE \
8292 ? len >= sizeof(struct STRUCT) \
8293 : len == sizeof(struct STRUCT)) { \
8294 *type = OVSINST_##ENUM; \
8295 return 0; \
8296 } else { \
8297 return OFPERR_OFPBIC_BAD_LEN; \
8298 }
8299 OVS_INSTRUCTIONS
8300 #undef DEFINE_INST
8301
8302 default:
8303 return OFPERR_OFPBIC_UNKNOWN_INST;
8304 }
8305 }
8306
8307 static enum ofperr
8308 decode_openflow11_instructions(const struct ofp11_instruction insts[],
8309 size_t n_insts, enum ofp_version version,
8310 const struct ofp11_instruction *out[])
8311 {
8312 const struct ofp11_instruction *inst;
8313 size_t left;
8314
8315 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
8316 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
8317 enum ovs_instruction_type type;
8318 enum ofperr error;
8319
8320 error = decode_openflow11_instruction(inst, &type);
8321 if (error) {
8322 return error;
8323 }
8324
8325 if (type == OVSINST_OFPIT13_METER && version >= OFP15_VERSION) {
8326 /* "meter" is an action, not an instruction, in OpenFlow 1.5. */
8327 return OFPERR_OFPBIC_UNKNOWN_INST;
8328 }
8329
8330 if (out[type]) {
8331 return OFPERR_OFPBIC_DUP_INST;
8332 }
8333 out[type] = inst;
8334 }
8335
8336 if (left) {
8337 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
8338 (n_insts - left) * sizeof *inst);
8339 return OFPERR_OFPBIC_BAD_LEN;
8340 }
8341 return 0;
8342 }
8343
8344 static void
8345 get_actions_from_instruction(const struct ofp11_instruction *inst,
8346 const struct ofp_action_header **actions,
8347 size_t *actions_len)
8348 {
8349 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
8350 *actions_len = ntohs(inst->len) - sizeof *inst;
8351 }
8352
8353 enum ofperr
8354 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
8355 unsigned int instructions_len,
8356 enum ofp_version version,
8357 const struct vl_mff_map *vl_mff_map,
8358 uint64_t *ofpacts_tlv_bitmap,
8359 struct ofpbuf *ofpacts)
8360 {
8361 const struct ofp11_instruction *instructions;
8362 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
8363 enum ofperr error;
8364
8365 ofpbuf_clear(ofpacts);
8366 if (version == OFP10_VERSION) {
8367 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
8368 version,
8369 (1u << N_OVS_INSTRUCTIONS) - 1,
8370 ofpacts, 0, vl_mff_map,
8371 ofpacts_tlv_bitmap);
8372 }
8373
8374 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
8375 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
8376 "multiple of %d",
8377 instructions_len, OFP11_INSTRUCTION_ALIGN);
8378 error = OFPERR_OFPBIC_BAD_LEN;
8379 goto exit;
8380 }
8381
8382 instructions = ofpbuf_try_pull(openflow, instructions_len);
8383 if (instructions == NULL) {
8384 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
8385 "remaining message length (%"PRIu32")",
8386 instructions_len, openflow->size);
8387 error = OFPERR_OFPBIC_BAD_LEN;
8388 goto exit;
8389 }
8390
8391 error = decode_openflow11_instructions(
8392 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN, version,
8393 insts);
8394 if (error) {
8395 goto exit;
8396 }
8397
8398 if (insts[OVSINST_OFPIT13_METER]) {
8399 const struct ofp13_instruction_meter *oim;
8400 struct ofpact_meter *om;
8401
8402 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
8403 insts[OVSINST_OFPIT13_METER]);
8404
8405 om = ofpact_put_METER(ofpacts);
8406 om->meter_id = ntohl(oim->meter_id);
8407 om->provider_meter_id = UINT32_MAX; /* No provider meter ID. */
8408 }
8409 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
8410 const struct ofp_action_header *actions;
8411 size_t actions_len;
8412
8413 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
8414 &actions, &actions_len);
8415 error = ofpacts_decode(actions, actions_len, version, vl_mff_map,
8416 ofpacts_tlv_bitmap, ofpacts);
8417 if (error) {
8418 goto exit;
8419 }
8420 }
8421 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
8422 instruction_get_OFPIT11_CLEAR_ACTIONS(
8423 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
8424 ofpact_put_CLEAR_ACTIONS(ofpacts);
8425 }
8426 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
8427 struct ofpact_nest *on;
8428 const struct ofp_action_header *actions;
8429 size_t actions_len;
8430 size_t start = ofpacts->size;
8431 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
8432 offsetof(struct ofpact_nest, actions));
8433 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
8434 &actions, &actions_len);
8435 error = ofpacts_decode_for_action_set(actions, actions_len,
8436 version, vl_mff_map,
8437 ofpacts_tlv_bitmap, ofpacts);
8438 if (error) {
8439 goto exit;
8440 }
8441 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
8442 on->ofpact.len = ofpacts->size - start;
8443 }
8444 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
8445 const struct ofp11_instruction_write_metadata *oiwm;
8446 struct ofpact_metadata *om;
8447
8448 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
8449 insts[OVSINST_OFPIT11_WRITE_METADATA]);
8450
8451 om = ofpact_put_WRITE_METADATA(ofpacts);
8452 om->metadata = oiwm->metadata;
8453 om->mask = oiwm->metadata_mask;
8454 }
8455 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
8456 const struct ofp11_instruction_goto_table *oigt;
8457 struct ofpact_goto_table *ogt;
8458
8459 oigt = instruction_get_OFPIT11_GOTO_TABLE(
8460 insts[OVSINST_OFPIT11_GOTO_TABLE]);
8461 ogt = ofpact_put_GOTO_TABLE(ofpacts);
8462 ogt->table_id = oigt->table_id;
8463 }
8464
8465 error = ofpacts_verify(ofpacts->data, ofpacts->size, version,
8466 (1u << N_OVS_INSTRUCTIONS) - 1, 0, NULL);
8467 exit:
8468 if (error) {
8469 ofpbuf_clear(ofpacts);
8470 }
8471 return error;
8472 }
8473
8474 /* Update the length of the instruction that begins at offset 'ofs' within
8475 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
8476 * If the instruction contains no nested actions, deletes it entirely. */
8477 static void
8478 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
8479 {
8480 struct ofp11_instruction_actions *oia;
8481
8482 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
8483 if (openflow->size > ofs + sizeof *oia) {
8484 oia->len = htons(openflow->size - ofs);
8485 } else {
8486 openflow->size = ofs;
8487 }
8488 }
8489 \f
8490 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
8491 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
8492 * valid, otherwise an OpenFlow error code. */
8493 enum ofperr
8494 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
8495 {
8496 switch (port) {
8497 case OFPP_IN_PORT:
8498 case OFPP_TABLE:
8499 case OFPP_NORMAL:
8500 case OFPP_FLOOD:
8501 case OFPP_ALL:
8502 case OFPP_CONTROLLER:
8503 case OFPP_LOCAL:
8504 return 0;
8505
8506 case OFPP_NONE:
8507 return OFPERR_OFPBAC_BAD_OUT_PORT;
8508
8509 default:
8510 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
8511 return 0;
8512 }
8513 return OFPERR_OFPBAC_BAD_OUT_PORT;
8514 }
8515 }
8516
8517 /* Removes the protocols that require consistency between match and actions
8518 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
8519 *
8520 * (An example of an inconsistency between match and actions is a flow that
8521 * does not match on an MPLS Ethertype but has an action that pops an MPLS
8522 * label.) */
8523 static void
8524 inconsistent_match(enum ofputil_protocol *usable_protocols)
8525 {
8526 *usable_protocols &= OFPUTIL_P_OF10_ANY;
8527 }
8528
8529 /* May modify flow->packet_type, flow->dl_type, flow->nw_proto and
8530 * flow->vlan_tci, caller must restore them.
8531 *
8532 * Modifies some actions, filling in fields that could not be properly set
8533 * without context. */
8534 static enum ofperr
8535 ofpact_check__(struct ofpact *a, struct ofpact_check_params *cp)
8536 {
8537 switch (a->type) {
8538 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
8539 case OFPACT_##ENUM: \
8540 return check_##ENUM(ofpact_get_##ENUM(a), cp);
8541 OFPACTS
8542 #undef OFPACT
8543 default:
8544 OVS_NOT_REACHED();
8545 }
8546 }
8547
8548 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
8549 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
8550 * switch with no more than 'max_ports' ports.
8551 *
8552 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
8553 * '*usable_protocols' the protocols that forbid the inconsistency. (An
8554 * example of an inconsistency between match and actions is a flow that does
8555 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
8556 *
8557 * May annotate ofpacts with information gathered from the 'match'.
8558 *
8559 * May temporarily modify 'match', but restores the changes before
8560 * returning. */
8561 enum ofperr
8562 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
8563 struct ofpact_check_params *cp)
8564 {
8565 /* Save fields that might temporarily be modified. */
8566 struct flow *flow = &cp->match->flow;
8567 ovs_be32 packet_type = flow->packet_type;
8568 ovs_be16 dl_type = flow->dl_type;
8569 uint8_t nw_proto = flow->nw_proto;
8570 union flow_vlan_hdr vlans[FLOW_MAX_VLAN_HEADERS];
8571 memcpy(vlans, flow->vlans, sizeof vlans);
8572
8573 /* Check all the actions. */
8574 cp->usable_protocols = OFPUTIL_P_ANY;
8575 enum ofperr error = 0;
8576 struct ofpact *a;
8577 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
8578 error = ofpact_check__(a, cp);
8579 if (error) {
8580 break;
8581 }
8582 }
8583
8584 /* Restore fields that may have been modified. */
8585 flow->packet_type = packet_type;
8586 flow->dl_type = dl_type;
8587 memcpy(flow->vlans, vlans, sizeof vlans);
8588 flow->nw_proto = nw_proto;
8589
8590 return error;
8591 }
8592
8593 /* Like ofpacts_check(), but reports inconsistencies as
8594 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
8595 enum ofperr
8596 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
8597 enum ofputil_protocol needed_protocols,
8598 struct ofpact_check_params *cp)
8599 {
8600 enum ofperr error = ofpacts_check(ofpacts, ofpacts_len, cp);
8601 if (!error && needed_protocols & ~cp->usable_protocols) {
8602 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
8603 }
8604 return error;
8605 }
8606
8607 /* Returns the destination field that 'ofpact' would write to, or NULL
8608 * if the action would not write to an mf_field. */
8609 const struct mf_field *
8610 ofpact_get_mf_dst(const struct ofpact *ofpact)
8611 {
8612 if (ofpact->type == OFPACT_SET_FIELD) {
8613 const struct ofpact_set_field *orl;
8614
8615 orl = CONTAINER_OF(ofpact, struct ofpact_set_field, ofpact);
8616 return orl->field;
8617 } else if (ofpact->type == OFPACT_REG_MOVE) {
8618 const struct ofpact_reg_move *orm;
8619
8620 orm = CONTAINER_OF(ofpact, struct ofpact_reg_move, ofpact);
8621 return orm->dst.field;
8622 }
8623
8624 return NULL;
8625 }
8626
8627 static void OVS_PRINTF_FORMAT(2, 3)
8628 verify_error(char **errorp, const char *format, ...)
8629 {
8630 va_list args;
8631 va_start(args, format);
8632 char *error = xvasprintf(format, args);
8633 va_end(args);
8634
8635 if (errorp) {
8636 *errorp = error;
8637 } else {
8638 VLOG_WARN("%s", error);
8639 free(error);
8640 }
8641 }
8642
8643 static enum ofperr
8644 unsupported_nesting(enum ofpact_type action, enum ofpact_type outer_action,
8645 char **errorp)
8646 {
8647 verify_error(errorp, "%s action doesn't support nested action %s",
8648 ofpact_name(outer_action), ofpact_name(action));
8649 return OFPERR_OFPBAC_BAD_ARGUMENT;
8650 }
8651
8652 static bool
8653 field_requires_ct(enum mf_field_id field)
8654 {
8655 return field == MFF_CT_MARK || field == MFF_CT_LABEL;
8656 }
8657
8658 /* Apply nesting constraints for actions */
8659 static enum ofperr
8660 ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action,
8661 char **errorp)
8662 {
8663 const struct mf_field *field = ofpact_get_mf_dst(a);
8664
8665 if (field && field_requires_ct(field->id) && outer_action != OFPACT_CT) {
8666 verify_error(errorp, "cannot set CT fields outside of ct action");
8667 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
8668 }
8669 if (a->type == OFPACT_NAT) {
8670 if (outer_action != OFPACT_CT) {
8671 verify_error(errorp,
8672 "Cannot have NAT action outside of \"ct\" action");
8673 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
8674 }
8675 return 0;
8676 }
8677
8678 if (outer_action) {
8679 ovs_assert(outer_action == OFPACT_WRITE_ACTIONS
8680 || outer_action == OFPACT_CT
8681 || outer_action == OFPACT_CLONE);
8682
8683 if (outer_action == OFPACT_CT) {
8684 if (!field) {
8685 return unsupported_nesting(a->type, outer_action, errorp);
8686 } else if (!field_requires_ct(field->id)) {
8687 verify_error(errorp,
8688 "%s action doesn't support nested modification "
8689 "of %s", ofpact_name(outer_action), field->name);
8690 return OFPERR_OFPBAC_BAD_ARGUMENT;
8691 }
8692 }
8693
8694 if (a->type == OFPACT_METER) {
8695 return unsupported_nesting(a->type, outer_action, errorp);
8696 }
8697 }
8698
8699 return 0;
8700 }
8701
8702 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
8703 * appropriate order as defined by the OpenFlow spec and as required by Open
8704 * vSwitch.
8705 *
8706 * The 'version' is relevant only for error reporting: Open vSwitch enforces
8707 * the same rules for every version of OpenFlow, but different versions require
8708 * different error codes.
8709 *
8710 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
8711 * instructions that are allowed within 'ofpacts[]'.
8712 *
8713 * If 'outer_action' is not zero, it specifies that the actions are nested
8714 * within another action of type 'outer_action'. */
8715 static enum ofperr
8716 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
8717 enum ofp_version version, uint32_t allowed_ovsinsts,
8718 enum ofpact_type outer_action, char **errorp)
8719 {
8720 const struct ofpact *a;
8721 enum ovs_instruction_type inst;
8722
8723 inst = OVSINST_OFPIT13_METER;
8724 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
8725 enum ovs_instruction_type next;
8726 enum ofperr error;
8727
8728 if (a->type == OFPACT_CONJUNCTION) {
8729 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
8730 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
8731 verify_error(errorp, "\"conjunction\" actions may be used "
8732 "along with \"note\" but not any other kind "
8733 "of action (such as the \"%s\" action used "
8734 "here)", ofpact_name(a->type));
8735 return OFPERR_NXBAC_BAD_CONJUNCTION;
8736 }
8737 }
8738 return 0;
8739 }
8740
8741 error = ofpacts_verify_nested(a, outer_action, errorp);
8742 if (error) {
8743 return error;
8744 }
8745
8746 next = ovs_instruction_type_from_ofpact_type(a->type, version);
8747 if (a > ofpacts
8748 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
8749 ? next < inst
8750 : next <= inst)) {
8751 const char *name = ovs_instruction_name_from_type(inst);
8752 const char *next_name = ovs_instruction_name_from_type(next);
8753
8754 if (next == inst) {
8755 verify_error(errorp, "duplicate %s instruction not allowed, "
8756 "for OpenFlow 1.1+ compatibility", name);
8757 } else {
8758 verify_error(errorp, "invalid instruction ordering: "
8759 "%s must appear before %s, "
8760 "for OpenFlow 1.1+ compatibility",
8761 next_name, name);
8762 }
8763 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
8764 }
8765 if (!((1u << next) & allowed_ovsinsts)) {
8766 const char *name = ovs_instruction_name_from_type(next);
8767
8768 if (next == OVSINST_OFPIT13_METER && version >= OFP15_VERSION) {
8769 verify_error(errorp, "%s action not allowed here", name);
8770 return OFPERR_OFPBAC_BAD_TYPE;
8771 } else {
8772 verify_error(errorp, "%s instruction not allowed here", name);
8773 return OFPERR_OFPBIC_UNSUP_INST;
8774 }
8775 }
8776
8777 inst = next;
8778 }
8779
8780 return 0;
8781 }
8782 \f
8783 /* Converting ofpacts to OpenFlow. */
8784
8785 static void
8786 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
8787 struct ofpbuf *out)
8788 {
8789 switch (a->type) {
8790 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
8791 case OFPACT_##ENUM: \
8792 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
8793 return;
8794 OFPACTS
8795 #undef OFPACT
8796 default:
8797 OVS_NOT_REACHED();
8798 }
8799 }
8800
8801 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
8802 * actions in 'openflow', appending the actions to any existing data in
8803 * 'openflow'. */
8804 size_t
8805 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
8806 struct ofpbuf *openflow,
8807 enum ofp_version ofp_version)
8808 {
8809 const struct ofpact *a;
8810 size_t start_size = openflow->size;
8811
8812 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
8813 encode_ofpact(a, ofp_version, openflow);
8814 }
8815 return openflow->size - start_size;
8816 }
8817
8818 static enum ovs_instruction_type
8819 ofpact_is_apply_actions(const struct ofpact *a, enum ofp_version version)
8820 {
8821 return (ovs_instruction_type_from_ofpact_type(a->type, version)
8822 == OVSINST_OFPIT11_APPLY_ACTIONS);
8823 }
8824
8825 void
8826 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
8827 size_t ofpacts_len,
8828 struct ofpbuf *openflow,
8829 enum ofp_version ofp_version)
8830 {
8831 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
8832 const struct ofpact *a;
8833
8834 if (ofp_version == OFP10_VERSION) {
8835 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
8836 ofp_version);
8837 return;
8838 }
8839
8840 a = ofpacts;
8841 while (a < end) {
8842 if (ofpact_is_apply_actions(a, ofp_version)) {
8843 size_t ofs = openflow->size;
8844
8845 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
8846 do {
8847 encode_ofpact(a, ofp_version, openflow);
8848 a = ofpact_next(a);
8849 } while (a < end && ofpact_is_apply_actions(a, ofp_version));
8850 ofpacts_update_instruction_actions(openflow, ofs);
8851 } else {
8852 encode_ofpact(a, ofp_version, openflow);
8853 a = ofpact_next(a);
8854 }
8855 }
8856 }
8857 \f
8858 /* Sets of supported actions. */
8859
8860 /* Two-way translation between OVS's internal "OFPACT_*" representation of
8861 * actions and the "OFPAT_*" representation used in some OpenFlow version.
8862 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
8863 * instance is specific to one OpenFlow version.) */
8864 struct ofpact_map {
8865 enum ofpact_type ofpact; /* Internal name for action type. */
8866 int ofpat; /* OFPAT_* number from OpenFlow spec. */
8867 };
8868
8869 static const struct ofpact_map *
8870 get_ofpact_map(enum ofp_version version)
8871 {
8872 /* OpenFlow 1.0 actions. */
8873 static const struct ofpact_map of10[] = {
8874 { OFPACT_OUTPUT, 0 },
8875 { OFPACT_SET_VLAN_VID, 1 },
8876 { OFPACT_SET_VLAN_PCP, 2 },
8877 { OFPACT_STRIP_VLAN, 3 },
8878 { OFPACT_SET_ETH_SRC, 4 },
8879 { OFPACT_SET_ETH_DST, 5 },
8880 { OFPACT_SET_IPV4_SRC, 6 },
8881 { OFPACT_SET_IPV4_DST, 7 },
8882 { OFPACT_SET_IP_DSCP, 8 },
8883 { OFPACT_SET_L4_SRC_PORT, 9 },
8884 { OFPACT_SET_L4_DST_PORT, 10 },
8885 { OFPACT_ENQUEUE, 11 },
8886 { 0, -1 },
8887 };
8888
8889 /* OpenFlow 1.1 actions. */
8890 static const struct ofpact_map of11[] = {
8891 { OFPACT_OUTPUT, 0 },
8892 { OFPACT_SET_VLAN_VID, 1 },
8893 { OFPACT_SET_VLAN_PCP, 2 },
8894 { OFPACT_SET_ETH_SRC, 3 },
8895 { OFPACT_SET_ETH_DST, 4 },
8896 { OFPACT_SET_IPV4_SRC, 5 },
8897 { OFPACT_SET_IPV4_DST, 6 },
8898 { OFPACT_SET_IP_DSCP, 7 },
8899 { OFPACT_SET_IP_ECN, 8 },
8900 { OFPACT_SET_L4_SRC_PORT, 9 },
8901 { OFPACT_SET_L4_DST_PORT, 10 },
8902 /* OFPAT_COPY_TTL_OUT (11) not supported. */
8903 /* OFPAT_COPY_TTL_IN (12) not supported. */
8904 { OFPACT_SET_MPLS_LABEL, 13 },
8905 { OFPACT_SET_MPLS_TC, 14 },
8906 { OFPACT_SET_MPLS_TTL, 15 },
8907 { OFPACT_DEC_MPLS_TTL, 16 },
8908 { OFPACT_PUSH_VLAN, 17 },
8909 { OFPACT_STRIP_VLAN, 18 },
8910 { OFPACT_PUSH_MPLS, 19 },
8911 { OFPACT_POP_MPLS, 20 },
8912 { OFPACT_SET_QUEUE, 21 },
8913 { OFPACT_GROUP, 22 },
8914 { OFPACT_SET_IP_TTL, 23 },
8915 { OFPACT_DEC_TTL, 24 },
8916 { 0, -1 },
8917 };
8918
8919 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
8920 static const struct ofpact_map of12[] = {
8921 { OFPACT_OUTPUT, 0 },
8922 /* OFPAT_COPY_TTL_OUT (11) not supported. */
8923 /* OFPAT_COPY_TTL_IN (12) not supported. */
8924 { OFPACT_SET_MPLS_TTL, 15 },
8925 { OFPACT_DEC_MPLS_TTL, 16 },
8926 { OFPACT_PUSH_VLAN, 17 },
8927 { OFPACT_STRIP_VLAN, 18 },
8928 { OFPACT_PUSH_MPLS, 19 },
8929 { OFPACT_POP_MPLS, 20 },
8930 { OFPACT_SET_QUEUE, 21 },
8931 { OFPACT_GROUP, 22 },
8932 { OFPACT_SET_IP_TTL, 23 },
8933 { OFPACT_DEC_TTL, 24 },
8934 { OFPACT_SET_FIELD, 25 },
8935 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
8936 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
8937 { 0, -1 },
8938 };
8939
8940 switch (version) {
8941 case OFP10_VERSION:
8942 return of10;
8943
8944 case OFP11_VERSION:
8945 return of11;
8946
8947 case OFP12_VERSION:
8948 case OFP13_VERSION:
8949 case OFP14_VERSION:
8950 case OFP15_VERSION:
8951 default:
8952 return of12;
8953 }
8954 }
8955
8956 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
8957 * values, into a bitmap of actions suitable for OpenFlow 'version', and
8958 * returns the result. */
8959 ovs_be32
8960 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
8961 {
8962 uint32_t openflow_bitmap = 0;
8963 const struct ofpact_map *x;
8964
8965 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
8966 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
8967 openflow_bitmap |= 1u << x->ofpat;
8968 }
8969 }
8970 return htonl(openflow_bitmap);
8971 }
8972
8973 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
8974 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
8975 * and returns the result. */
8976 uint64_t
8977 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
8978 {
8979 uint64_t ofpact_bitmap = 0;
8980 const struct ofpact_map *x;
8981
8982 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
8983 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
8984 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
8985 }
8986 }
8987 return ofpact_bitmap;
8988 }
8989
8990 /* Appends to 's' a string representation of the set of OFPACT_* represented
8991 * by 'ofpacts_bitmap'. */
8992 void
8993 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
8994 {
8995 if (!ofpacts_bitmap) {
8996 ds_put_cstr(s, "<none>");
8997 } else {
8998 while (ofpacts_bitmap) {
8999 ds_put_format(s, "%s ",
9000 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
9001 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
9002 }
9003 ds_chomp(s, ' ');
9004 }
9005 }
9006 \f
9007 /* Returns true if 'action' outputs to 'port', false otherwise. */
9008 static bool
9009 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
9010 {
9011 switch (ofpact->type) {
9012 case OFPACT_OUTPUT:
9013 return ofpact_get_OUTPUT(ofpact)->port == port;
9014 case OFPACT_ENQUEUE:
9015 return ofpact_get_ENQUEUE(ofpact)->port == port;
9016 case OFPACT_CONTROLLER:
9017 return port == OFPP_CONTROLLER;
9018
9019 case OFPACT_OUTPUT_REG:
9020 case OFPACT_OUTPUT_TRUNC:
9021 case OFPACT_BUNDLE:
9022 case OFPACT_SET_VLAN_VID:
9023 case OFPACT_SET_VLAN_PCP:
9024 case OFPACT_STRIP_VLAN:
9025 case OFPACT_PUSH_VLAN:
9026 case OFPACT_SET_ETH_SRC:
9027 case OFPACT_SET_ETH_DST:
9028 case OFPACT_SET_IPV4_SRC:
9029 case OFPACT_SET_IPV4_DST:
9030 case OFPACT_SET_IP_DSCP:
9031 case OFPACT_SET_IP_ECN:
9032 case OFPACT_SET_IP_TTL:
9033 case OFPACT_SET_L4_SRC_PORT:
9034 case OFPACT_SET_L4_DST_PORT:
9035 case OFPACT_REG_MOVE:
9036 case OFPACT_SET_FIELD:
9037 case OFPACT_STACK_PUSH:
9038 case OFPACT_STACK_POP:
9039 case OFPACT_DEC_TTL:
9040 case OFPACT_SET_MPLS_LABEL:
9041 case OFPACT_SET_MPLS_TC:
9042 case OFPACT_SET_MPLS_TTL:
9043 case OFPACT_DEC_MPLS_TTL:
9044 case OFPACT_SET_TUNNEL:
9045 case OFPACT_WRITE_METADATA:
9046 case OFPACT_SET_QUEUE:
9047 case OFPACT_POP_QUEUE:
9048 case OFPACT_FIN_TIMEOUT:
9049 case OFPACT_RESUBMIT:
9050 case OFPACT_LEARN:
9051 case OFPACT_CONJUNCTION:
9052 case OFPACT_MULTIPATH:
9053 case OFPACT_NOTE:
9054 case OFPACT_EXIT:
9055 case OFPACT_UNROLL_XLATE:
9056 case OFPACT_PUSH_MPLS:
9057 case OFPACT_POP_MPLS:
9058 case OFPACT_SAMPLE:
9059 case OFPACT_CLEAR_ACTIONS:
9060 case OFPACT_CLONE:
9061 case OFPACT_WRITE_ACTIONS:
9062 case OFPACT_GOTO_TABLE:
9063 case OFPACT_METER:
9064 case OFPACT_GROUP:
9065 case OFPACT_DEBUG_RECIRC:
9066 case OFPACT_DEBUG_SLOW:
9067 case OFPACT_CT:
9068 case OFPACT_CT_CLEAR:
9069 case OFPACT_NAT:
9070 case OFPACT_ENCAP:
9071 case OFPACT_DECAP:
9072 case OFPACT_DEC_NSH_TTL:
9073 case OFPACT_CHECK_PKT_LARGER:
9074 case OFPACT_DELETE_FIELD:
9075 default:
9076 return false;
9077 }
9078 }
9079
9080 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
9081 * to 'port', false otherwise. */
9082 bool
9083 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
9084 ofp_port_t port)
9085 {
9086 const struct ofpact *a;
9087
9088 OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
9089 if (ofpact_outputs_to_port(a, port)) {
9090 return true;
9091 }
9092 }
9093
9094 return false;
9095 }
9096
9097 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
9098 * to 'group', false otherwise. */
9099 bool
9100 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
9101 uint32_t group_id)
9102 {
9103 const struct ofpact *a;
9104
9105 OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
9106 if (a->type == OFPACT_GROUP
9107 && ofpact_get_GROUP(a)->group_id == group_id) {
9108 return true;
9109 }
9110 }
9111
9112 return false;
9113 }
9114
9115 /* Returns true if the 'a_len' bytes of actions in 'a' and the 'b_len' bytes of
9116 * actions in 'b' are bytewise identical. */
9117 bool
9118 ofpacts_equal(const struct ofpact *a, size_t a_len,
9119 const struct ofpact *b, size_t b_len)
9120 {
9121 return a_len == b_len && !memcmp(a, b, a_len);
9122 }
9123
9124 /* Returns true if the 'a_len' bytes of actions in 'a' and the 'b_len' bytes of
9125 * actions in 'b' are identical when formatted as strings. (Converting actions
9126 * to string form suppresses some rarely meaningful differences, such as the
9127 * 'compat' member of actions.) */
9128 bool
9129 ofpacts_equal_stringwise(const struct ofpact *a, size_t a_len,
9130 const struct ofpact *b, size_t b_len)
9131 {
9132 struct ds a_s = DS_EMPTY_INITIALIZER;
9133 struct ofpact_format_params a_fp = { .s = &a_s };
9134 ofpacts_format(a, a_len, &a_fp);
9135
9136 struct ds b_s = DS_EMPTY_INITIALIZER;
9137 struct ofpact_format_params b_fp = { .s = &b_s };
9138 ofpacts_format(b, b_len, &b_fp);
9139
9140 bool equal = !strcmp(ds_cstr(&a_s), ds_cstr(&b_s));
9141
9142 ds_destroy(&a_s);
9143 ds_destroy(&b_s);
9144
9145 return equal;
9146 }
9147
9148 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
9149 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
9150 *
9151 * This function relies on the order of 'ofpacts' being correct (as checked by
9152 * ofpacts_verify()). */
9153 uint32_t
9154 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
9155 {
9156 const struct ofpact *a;
9157
9158 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
9159 if (a->type == OFPACT_METER) {
9160 return ofpact_get_METER(a)->meter_id;
9161 }
9162
9163 enum ovs_instruction_type inst
9164 = ovs_instruction_type_from_ofpact_type(a->type, 0);
9165 if (inst > OVSINST_OFPIT13_METER) {
9166 break;
9167 }
9168 }
9169
9170 return 0;
9171 }
9172 \f
9173 /* Formatting ofpacts. */
9174
9175 static void
9176 ofpact_format(const struct ofpact *a,
9177 const struct ofpact_format_params *fp)
9178 {
9179 switch (a->type) {
9180 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
9181 case OFPACT_##ENUM: \
9182 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), fp); \
9183 break;
9184 OFPACTS
9185 #undef OFPACT
9186 default:
9187 OVS_NOT_REACHED();
9188 }
9189 }
9190
9191 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
9192 * 'ofpacts' to 'fp->s'. If 'port_map' is nonnull, uses it to translate port
9193 * numbers to names in output. */
9194 void
9195 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
9196 const struct ofpact_format_params *fp)
9197 {
9198 if (!ofpacts_len) {
9199 ds_put_format(fp->s, "%sdrop%s", colors.drop, colors.end);
9200 } else {
9201 const struct ofpact *a;
9202
9203 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
9204 if (a != ofpacts) {
9205 ds_put_char(fp->s, ',');
9206 }
9207
9208 ofpact_format(a, fp);
9209 }
9210 }
9211 }
9212 \f
9213 /* Internal use by helpers. */
9214
9215 /* Implementation of ofpact_put_<ENUM>(). */
9216 void *
9217 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
9218 {
9219 struct ofpact *ofpact;
9220
9221 ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
9222 ofpact = ofpacts->header;
9223 ofpact_init(ofpact, type, len);
9224 return ofpact;
9225 }
9226
9227 /* Implementation of ofpact_init_<ENUM>(). */
9228 void
9229 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
9230 {
9231 memset(ofpact, 0, len);
9232 ofpact->type = type;
9233 ofpact->raw = -1;
9234 ofpact->len = len;
9235 }
9236
9237 /* Implementation of ofpact_finish_<ENUM>().
9238 *
9239 * Finishes composing a variable-length action (begun using
9240 * ofpact_put_<NAME>()), by padding the action to a multiple of OFPACT_ALIGNTO
9241 * bytes and updating its embedded length field. See the large comment near
9242 * the end of ofp-actions.h for more information.
9243 *
9244 * May reallocate 'ofpacts'. Callers should consider updating their 'ofpact'
9245 * pointer to the return value of this function. */
9246 void *
9247 ofpact_finish(struct ofpbuf *ofpacts, struct ofpact *ofpact)
9248 {
9249 ptrdiff_t len;
9250
9251 ovs_assert(ofpact == ofpacts->header);
9252 len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
9253 ovs_assert(len > 0 && len <= UINT16_MAX);
9254 ofpact->len = len;
9255 ofpbuf_padto(ofpacts, OFPACT_ALIGN(ofpacts->size));
9256
9257 return ofpacts->header;
9258 }
9259 \f
9260 static char * OVS_WARN_UNUSED_RESULT
9261 ofpact_parse(enum ofpact_type type, char *value,
9262 const struct ofpact_parse_params *pp)
9263 {
9264 switch (type) {
9265 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
9266 case OFPACT_##ENUM: \
9267 return parse_##ENUM(value, pp);
9268 OFPACTS
9269 #undef OFPACT
9270 default:
9271 OVS_NOT_REACHED();
9272 }
9273 }
9274
9275 static bool
9276 ofpact_type_from_name(const char *name, enum ofpact_type *type)
9277 {
9278 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
9279 if (!strcasecmp(name, NAME)) { \
9280 *type = OFPACT_##ENUM; \
9281 return true; \
9282 }
9283 OFPACTS
9284 #undef OFPACT
9285
9286 return false;
9287 }
9288
9289 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
9290 *
9291 * Returns NULL if successful, otherwise a malloc()'d string describing the
9292 * error. The caller is responsible for freeing the returned string.
9293 *
9294 * If 'outer_action' is specified, indicates that the actions being parsed
9295 * are nested within another action of the type specified in 'outer_action'. */
9296 static char * OVS_WARN_UNUSED_RESULT
9297 ofpacts_parse__(char *str, const struct ofpact_parse_params *pp,
9298 bool allow_instructions, enum ofpact_type outer_action)
9299 {
9300 char *key, *value;
9301 bool drop = false;
9302 char *pos;
9303
9304 pos = str;
9305 while (ofputil_parse_key_value(&pos, &key, &value)) {
9306 enum ofpact_type type;
9307 char *error = NULL;
9308 ofp_port_t port;
9309 if (ofpact_type_from_name(key, &type)) {
9310 error = ofpact_parse(type, value, pp);
9311
9312 if (type == OFPACT_METER && !allow_instructions) {
9313 /* Meter is an action in OF1.5 and it's being used in a
9314 * context where instructions aren't allowed. Therefore,
9315 * this must be OF1.5+. */
9316 *pp->usable_protocols &= OFPUTIL_P_OF15_UP;
9317 }
9318 } else if (!strcasecmp(key, "mod_vlan_vid")) {
9319 error = parse_set_vlan_vid(value, true, pp);
9320 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
9321 error = parse_set_vlan_pcp(value, true, pp);
9322 } else if (!strcasecmp(key, "set_nw_ttl")) {
9323 error = parse_SET_IP_TTL(value, pp);
9324 } else if (!strcasecmp(key, "pop_vlan")) {
9325 error = parse_pop_vlan(pp);
9326 } else if (!strcasecmp(key, "set_tunnel64")) {
9327 error = parse_set_tunnel(value, NXAST_RAW_SET_TUNNEL64, pp);
9328 } else if (!strcasecmp(key, "load")) {
9329 error = parse_reg_load(value, pp);
9330 } else if (!strcasecmp(key, "bundle_load")) {
9331 error = parse_bundle_load(value, pp);
9332 } else if (!strcasecmp(key, "drop")) {
9333 drop = true;
9334 } else if (!strcasecmp(key, "apply_actions")) {
9335 return xstrdup("apply_actions is the default instruction");
9336 } else if (ofputil_port_from_string(key, pp->port_map, &port)) {
9337 ofpact_put_OUTPUT(pp->ofpacts)->port = port;
9338 } else {
9339 return xasprintf("unknown action %s", key);
9340 }
9341 if (error) {
9342 return error;
9343 }
9344 }
9345
9346 if (drop && pp->ofpacts->size) {
9347 return xstrdup("\"drop\" must not be accompanied by any other action "
9348 "or instruction");
9349 }
9350
9351 char *error = NULL;
9352 ofpacts_verify(pp->ofpacts->data, pp->ofpacts->size, OFP11_VERSION,
9353 (allow_instructions
9354 ? (1u << N_OVS_INSTRUCTIONS) - 1
9355 : ((1u << OVSINST_OFPIT11_APPLY_ACTIONS)
9356 | (1u << OVSINST_OFPIT13_METER))),
9357 outer_action, &error);
9358 if (error) {
9359 return error;
9360 }
9361
9362 return NULL;
9363 }
9364
9365 static char * OVS_WARN_UNUSED_RESULT
9366 ofpacts_parse(char *str, const struct ofpact_parse_params *pp,
9367 bool allow_instructions, enum ofpact_type outer_action)
9368 {
9369 if (pp->depth >= MAX_OFPACT_PARSE_DEPTH) {
9370 return xstrdup("Action nested too deeply");
9371 }
9372 CONST_CAST(struct ofpact_parse_params *, pp)->depth++;
9373 uint32_t orig_size = pp->ofpacts->size;
9374 char *error = ofpacts_parse__(str, pp, allow_instructions, outer_action);
9375 if (error) {
9376 pp->ofpacts->size = orig_size;
9377 }
9378 CONST_CAST(struct ofpact_parse_params *, pp)->depth--;
9379 return error;
9380 }
9381
9382 static char * OVS_WARN_UNUSED_RESULT
9383 ofpacts_parse_copy(const char *s_, const struct ofpact_parse_params *pp,
9384 bool allow_instructions, enum ofpact_type outer_action)
9385 {
9386 char *error, *s;
9387
9388 *pp->usable_protocols = OFPUTIL_P_ANY;
9389
9390 s = xstrdup(s_);
9391 error = ofpacts_parse(s, pp, allow_instructions, outer_action);
9392 free(s);
9393
9394 return error;
9395 }
9396
9397 /* Parses 's' as a set of OpenFlow actions and appends the actions to
9398 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
9399 * that are nested within the action of type 'outer_action'.
9400 *
9401 * Returns NULL if successful, otherwise a malloc()'d string describing the
9402 * error. The caller is responsible for freeing the returned string. */
9403 char * OVS_WARN_UNUSED_RESULT
9404 ofpacts_parse_actions(const char *s, const struct ofpact_parse_params *pp)
9405 {
9406 return ofpacts_parse_copy(s, pp, false, 0);
9407 }
9408
9409 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
9410 * 'ofpacts'.
9411 *
9412 * Returns NULL if successful, otherwise a malloc()'d string describing the
9413 * error. The caller is responsible for freeing the returned string. */
9414 char * OVS_WARN_UNUSED_RESULT
9415 ofpacts_parse_instructions(const char *s, const struct ofpact_parse_params *pp)
9416 {
9417 return ofpacts_parse_copy(s, pp, true, 0);
9418 }
9419
9420 const char *
9421 ofpact_name(enum ofpact_type type)
9422 {
9423 switch (type) {
9424 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
9425 OFPACTS
9426 #undef OFPACT
9427 }
9428 return "<unknown>";
9429 }
9430 \f
9431 /* Low-level action decoding and encoding functions. */
9432
9433 /* Everything needed to identify a particular OpenFlow action. */
9434 struct ofpact_hdrs {
9435 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
9436 uint16_t type; /* Type if standard, otherwise subtype. */
9437 uint8_t ofp_version; /* From ofp_header. */
9438 };
9439
9440 /* Information about a particular OpenFlow action. */
9441 struct ofpact_raw_instance {
9442 /* The action's identity. */
9443 struct ofpact_hdrs hdrs;
9444 enum ofp_raw_action_type raw;
9445
9446 /* Looking up the action. */
9447 struct hmap_node decode_node; /* Based on 'hdrs'. */
9448 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
9449
9450 /* The action's encoded size.
9451 *
9452 * If this action is fixed-length, 'min_length' == 'max_length'.
9453 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
9454 * OFP_ACTION_ALIGN) == 65528. */
9455 unsigned short int min_length;
9456 unsigned short int max_length;
9457
9458 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
9459 * offset of that argument from the beginning of the action and 'arg_len'
9460 * its length, both in bytes.
9461 *
9462 * For actions that take other forms, these are both zero. */
9463 unsigned short int arg_ofs;
9464 unsigned short int arg_len;
9465
9466 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
9467 const char *name;
9468
9469 /* If this action is deprecated, a human-readable string with a brief
9470 * explanation. */
9471 const char *deprecation;
9472 };
9473
9474 /* Action header. */
9475 struct ofp_action_header {
9476 /* The meaning of other values of 'type' generally depends on the OpenFlow
9477 * version (see enum ofp_raw_action_type).
9478 *
9479 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
9480 * designates an OpenFlow vendor ID and that the remainder of the action
9481 * structure has a vendor-defined meaning.
9482 */
9483 #define OFPAT_VENDOR 0xffff
9484 ovs_be16 type;
9485
9486 /* Always a multiple of 8. */
9487 ovs_be16 len;
9488
9489 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
9490 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
9491 ovs_be32 vendor;
9492 };
9493 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
9494
9495 static bool
9496 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
9497 const struct ofpact_hdrs *b)
9498 {
9499 return (a->vendor == b->vendor
9500 && a->type == b->type
9501 && a->ofp_version == b->ofp_version);
9502 }
9503
9504 static uint32_t
9505 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
9506 {
9507 return hash_2words(hdrs->vendor,
9508 ((uint32_t) hdrs->type << 16) | hdrs->ofp_version);
9509 }
9510
9511 #include "ofp-actions.inc2"
9512
9513 static struct hmap *
9514 ofpact_decode_hmap(void)
9515 {
9516 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
9517 static struct hmap hmap;
9518
9519 if (ovsthread_once_start(&once)) {
9520 struct ofpact_raw_instance *inst;
9521
9522 hmap_init(&hmap);
9523 for (inst = all_raw_instances;
9524 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
9525 inst++) {
9526 hmap_insert(&hmap, &inst->decode_node,
9527 ofpact_hdrs_hash(&inst->hdrs));
9528 }
9529 ovsthread_once_done(&once);
9530 }
9531 return &hmap;
9532 }
9533
9534 static struct hmap *
9535 ofpact_encode_hmap(void)
9536 {
9537 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
9538 static struct hmap hmap;
9539
9540 if (ovsthread_once_start(&once)) {
9541 struct ofpact_raw_instance *inst;
9542
9543 hmap_init(&hmap);
9544 for (inst = all_raw_instances;
9545 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
9546 inst++) {
9547 hmap_insert(&hmap, &inst->encode_node,
9548 hash_2words(inst->raw, inst->hdrs.ofp_version));
9549 }
9550 ovsthread_once_done(&once);
9551 }
9552 return &hmap;
9553 }
9554
9555 static enum ofperr
9556 ofpact_decode_raw(enum ofp_version ofp_version,
9557 const struct ofp_action_header *oah, size_t length,
9558 const struct ofpact_raw_instance **instp)
9559 {
9560 const struct ofpact_raw_instance *inst;
9561 struct ofpact_hdrs hdrs;
9562
9563 *instp = NULL;
9564 if (length < sizeof *oah) {
9565 return OFPERR_OFPBAC_BAD_LEN;
9566 }
9567
9568 /* Get base action type. */
9569 if (oah->type == htons(OFPAT_VENDOR)) {
9570 /* Get vendor. */
9571 hdrs.vendor = ntohl(oah->vendor);
9572 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
9573 /* Get extension subtype. */
9574 const struct ext_action_header *nah;
9575
9576 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
9577 if (length < sizeof *nah) {
9578 return OFPERR_OFPBAC_BAD_LEN;
9579 }
9580 hdrs.type = ntohs(nah->subtype);
9581 } else {
9582 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
9583 hdrs.vendor);
9584 return OFPERR_OFPBAC_BAD_VENDOR;
9585 }
9586 } else {
9587 hdrs.vendor = 0;
9588 hdrs.type = ntohs(oah->type);
9589 }
9590
9591 hdrs.ofp_version = ofp_version;
9592 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
9593 ofpact_decode_hmap()) {
9594 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
9595 *instp = inst;
9596 return 0;
9597 }
9598 }
9599
9600 VLOG_WARN_RL(&rl, "unknown %s action for vendor %#"PRIx32" and "
9601 "type %"PRIu16, ofputil_version_to_string(ofp_version),
9602 hdrs.vendor, hdrs.type);
9603 return (hdrs.vendor
9604 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
9605 : OFPERR_OFPBAC_BAD_TYPE);
9606 }
9607
9608 static enum ofperr
9609 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
9610 enum ofp_raw_action_type *raw, uint64_t *arg)
9611 {
9612 const struct ofp_action_header *oah = buf->data;
9613 const struct ofpact_raw_instance *action;
9614 unsigned int length;
9615 enum ofperr error;
9616
9617 *raw = *arg = 0;
9618 error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
9619 if (error) {
9620 return error;
9621 }
9622
9623 if (action->deprecation) {
9624 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
9625 action->name, ofputil_version_to_string(ofp_version),
9626 action->deprecation);
9627 }
9628
9629 length = ntohs(oah->len);
9630 if (length > buf->size) {
9631 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
9632 "length %"PRIu32, action->name, length, buf->size);
9633 return OFPERR_OFPBAC_BAD_LEN;
9634 }
9635 if (length < action->min_length || length > action->max_length) {
9636 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
9637 "[%hu,%hu]", action->name, length,
9638 action->min_length, action->max_length);
9639 return OFPERR_OFPBAC_BAD_LEN;
9640 }
9641 if (length % 8) {
9642 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
9643 "of 8", action->name, length);
9644 return OFPERR_OFPBAC_BAD_LEN;
9645 }
9646
9647 *raw = action->raw;
9648 *arg = 0;
9649 if (action->arg_len) {
9650 const uint8_t *p;
9651 int i;
9652
9653 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
9654 for (i = 0; i < action->arg_len; i++) {
9655 *arg = (*arg << 8) | p[i];
9656 }
9657 }
9658
9659 ofpbuf_pull(buf, length);
9660
9661 return 0;
9662 }
9663
9664 static const struct ofpact_raw_instance *
9665 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
9666 {
9667 const struct ofpact_raw_instance *inst;
9668
9669 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
9670 ofpact_encode_hmap()) {
9671 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
9672 return inst;
9673 }
9674 }
9675 OVS_NOT_REACHED();
9676 }
9677
9678 static void *
9679 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
9680 enum ofp_raw_action_type raw, uint64_t arg)
9681 {
9682 const struct ofpact_raw_instance *inst;
9683 struct ofp_action_header *oah;
9684 const struct ofpact_hdrs *hdrs;
9685
9686 inst = ofpact_raw_lookup(ofp_version, raw);
9687 hdrs = &inst->hdrs;
9688
9689 oah = ofpbuf_put_zeros(buf, inst->min_length);
9690 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
9691 oah->len = htons(inst->min_length);
9692 oah->vendor = htonl(hdrs->vendor);
9693
9694 switch (hdrs->vendor) {
9695 case 0:
9696 break;
9697
9698 case NX_VENDOR_ID:
9699 case ONF_VENDOR_ID: {
9700 struct ext_action_header *nah = (struct ext_action_header *) oah;
9701 nah->subtype = htons(hdrs->type);
9702 break;
9703 }
9704
9705 default:
9706 OVS_NOT_REACHED();
9707 }
9708
9709 if (inst->arg_len) {
9710 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
9711 int i;
9712
9713 for (i = 0; i < inst->arg_len; i++) {
9714 *--p = arg;
9715 arg >>= 8;
9716 }
9717 } else {
9718 ovs_assert(!arg);
9719 }
9720
9721 return oah;
9722 }
9723
9724 static void
9725 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
9726 {
9727 struct ofp_action_header *oah;
9728
9729 ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs,
9730 OFP_ACTION_ALIGN));
9731
9732 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
9733 oah->len = htons(openflow->size - start_ofs);
9734 }
9735