2 * Copyright (c) 2012 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #define OFP_ACTIONS_H 1
21 #include "meta-flow.h"
22 #include "ofp-errors.h"
24 #include "openflow/openflow.h"
25 #include "openflow/nicira-ext.h"
26 #include "openvswitch/types.h"
28 /* List of OVS abstracted actions.
30 * This macro is used directly only internally by this header, but the list is
31 * still of interest to developers.
33 * Each DEFINE_OFPACT invocation has the following parameters:
35 * 1. <ENUM>, used below in the enum definition of OFPACT_<ENUM>, and
38 * 2. <STRUCT> corresponding to a structure "struct <STRUCT>", that must be
39 * defined below. This structure must be an abstract definition of the
40 * action. Its first member must have type "struct ofpact" and name
41 * "ofpact". It may be fixed length or end with a flexible array member
42 * (e.g. "int member[];").
44 * 3. <MEMBER>, which has one of two possible values:
46 * - If "struct <STRUCT>" is fixed-length, it must be "ofpact".
48 * - If "struct <STRUCT>" is variable-length, it must be the name of the
49 * flexible array member.
53 DEFINE_OFPACT(OUTPUT, ofpact_output, ofpact) \
54 DEFINE_OFPACT(CONTROLLER, ofpact_controller, ofpact) \
55 DEFINE_OFPACT(ENQUEUE, ofpact_enqueue, ofpact) \
56 DEFINE_OFPACT(OUTPUT_REG, ofpact_output_reg, ofpact) \
57 DEFINE_OFPACT(BUNDLE, ofpact_bundle, slaves) \
59 /* Header changes. */ \
60 DEFINE_OFPACT(SET_VLAN_VID, ofpact_vlan_vid, ofpact) \
61 DEFINE_OFPACT(SET_VLAN_PCP, ofpact_vlan_pcp, ofpact) \
62 DEFINE_OFPACT(STRIP_VLAN, ofpact_null, ofpact) \
63 DEFINE_OFPACT(SET_ETH_SRC, ofpact_mac, ofpact) \
64 DEFINE_OFPACT(SET_ETH_DST, ofpact_mac, ofpact) \
65 DEFINE_OFPACT(SET_IPV4_SRC, ofpact_ipv4, ofpact) \
66 DEFINE_OFPACT(SET_IPV4_DST, ofpact_ipv4, ofpact) \
67 DEFINE_OFPACT(SET_IPV4_DSCP, ofpact_dscp, ofpact) \
68 DEFINE_OFPACT(SET_L4_SRC_PORT, ofpact_l4_port, ofpact) \
69 DEFINE_OFPACT(SET_L4_DST_PORT, ofpact_l4_port, ofpact) \
70 DEFINE_OFPACT(REG_MOVE, ofpact_reg_move, ofpact) \
71 DEFINE_OFPACT(REG_LOAD, ofpact_reg_load, ofpact) \
72 DEFINE_OFPACT(DEC_TTL, ofpact_cnt_ids, cnt_ids) \
75 DEFINE_OFPACT(SET_TUNNEL, ofpact_tunnel, ofpact) \
76 DEFINE_OFPACT(SET_QUEUE, ofpact_queue, ofpact) \
77 DEFINE_OFPACT(POP_QUEUE, ofpact_null, ofpact) \
78 DEFINE_OFPACT(FIN_TIMEOUT, ofpact_fin_timeout, ofpact) \
80 /* Flow table interaction. */ \
81 DEFINE_OFPACT(RESUBMIT, ofpact_resubmit, ofpact) \
82 DEFINE_OFPACT(LEARN, ofpact_learn, specs) \
85 DEFINE_OFPACT(MULTIPATH, ofpact_multipath, ofpact) \
86 DEFINE_OFPACT(AUTOPATH, ofpact_autopath, ofpact) \
89 DEFINE_OFPACT(NOTE, ofpact_note, data) \
90 DEFINE_OFPACT(EXIT, ofpact_null, ofpact)
92 /* enum ofpact_type, with a member OFPACT_<ENUM> for each action. */
93 enum OVS_PACKED_ENUM ofpact_type {
94 #define DEFINE_OFPACT(ENUM, STRUCT, MEMBER) OFPACT_##ENUM,
99 /* N_OFPACTS, the number of values of "enum ofpact_type". */
102 #define DEFINE_OFPACT(ENUM, STRUCT, MEMBER) + 1
107 /* Header for an action.
109 * Each action is a structure "struct ofpact_*" that begins with "struct
110 * ofpact", usually followed by other data that describes the action. Actions
111 * are padded out to a multiple of OFPACT_ALIGNTO bytes in length.
113 * The 'compat' member is special:
115 * - Most "struct ofpact"s correspond to one particular kind of OpenFlow
116 * action, at least in a given OpenFlow version. For example,
117 * OFPACT_SET_VLAN_VID corresponds to OFPAT10_SET_VLAN_VID in OpenFlow
120 * For such actions, the 'compat' member is not meaningful and generally
123 * - A few "struct ofpact"s correspond to multiple OpenFlow actions. For
124 * example, OFPACT_SET_TUNNEL can be NXAST_SET_TUNNEL or
125 * NXAST_SET_TUNNEL64. In these cases, if the "struct ofpact" originated
126 * from OpenFlow, then we want to make sure that, if it gets translated
127 * back to OpenFlow later, it is translated back to the same action type.
128 * (Otherwise, we'd violate the promise made in DESIGN, in the "Action
129 * Reproduction" section.)
131 * For such actions, the 'compat' member should be the original action
132 * type. (If the action didn't originate from OpenFlow, then setting
133 * 'compat' to zero should be fine: code to translate the ofpact to
134 * OpenFlow must tolerate this case.)
137 enum ofpact_type type; /* OFPACT_*. */
138 enum ofputil_action_code compat; /* Original type when added, if any. */
139 uint16_t len; /* Length of the action, in bytes, including
140 * struct ofpact, excluding padding. */
144 /* Make sure that OVS_PACKED_ENUM really worked. */
145 BUILD_ASSERT_DECL(sizeof(struct ofpact) == 4);
149 #define OFPACT_ALIGNTO 8
150 #define OFPACT_ALIGN(SIZE) ROUND_UP(SIZE, OFPACT_ALIGNTO)
152 static inline struct ofpact *
153 ofpact_next(const struct ofpact *ofpact)
155 return (void *) ((uint8_t *) ofpact + OFPACT_ALIGN(ofpact->len));
158 static inline struct ofpact *
159 ofpact_end(const struct ofpact *ofpacts, size_t ofpacts_len)
161 return (void *) ((uint8_t *) ofpacts + ofpacts_len);
164 /* Assigns POS to each ofpact, in turn, in the OFPACTS_LEN bytes of ofpacts
165 * starting at OFPACTS. */
166 #define OFPACT_FOR_EACH(POS, OFPACTS, OFPACTS_LEN) \
167 for ((POS) = (OFPACTS); (POS) < ofpact_end(OFPACTS, OFPACTS_LEN); \
168 (POS) = ofpact_next(POS))
170 /* Action structure for each OFPACT_*. */
172 /* OFPACT_STRIP_VLAN, OFPACT_POP_QUEUE, OFPACT_EXIT.
174 * Used for OFPAT10_STRIP_VLAN, NXAST_DEC_TTL, NXAST_POP_QUEUE, NXAST_EXIT.
176 * Action structure for actions that do not have any extra data beyond the
179 struct ofpact ofpact;
184 * Used for OFPAT10_OUTPUT. */
185 struct ofpact_output {
186 struct ofpact ofpact;
187 uint16_t port; /* Output port. */
188 uint16_t max_len; /* Max send len, for port OFPP_CONTROLLER. */
191 /* OFPACT_CONTROLLER.
193 * Used for NXAST_CONTROLLER. */
194 struct ofpact_controller {
195 struct ofpact ofpact;
196 uint16_t max_len; /* Maximum length to send to controller. */
197 uint16_t controller_id; /* Controller ID to send packet-in. */
198 enum ofp_packet_in_reason reason; /* Reason to put in packet-in. */
203 * Used for OFPAT10_ENQUEUE. */
204 struct ofpact_enqueue {
205 struct ofpact ofpact;
210 /* OFPACT_OUTPUT_REG.
212 * Used for NXAST_OUTPUT_REG. */
213 struct ofpact_output_reg {
214 struct ofpact ofpact;
215 struct mf_subfield src;
221 * Used for NXAST_BUNDLE. */
222 struct ofpact_bundle {
223 struct ofpact ofpact;
225 /* Slave choice algorithm to apply to hash value. */
226 enum nx_bd_algorithm algorithm;
228 /* What fields to hash and how. */
229 enum nx_hash_fields fields;
230 uint16_t basis; /* Universal hash parameter. */
232 struct mf_subfield dst;
234 /* Slaves for output. */
235 unsigned int n_slaves;
239 /* OFPACT_SET_VLAN_VID.
241 * Used for OFPAT10_SET_VLAN_VID. */
242 struct ofpact_vlan_vid {
243 struct ofpact ofpact;
244 uint16_t vlan_vid; /* VLAN VID in low 12 bits, 0 in other bits. */
247 /* OFPACT_SET_VLAN_PCP.
249 * Used for OFPAT10_SET_VLAN_PCP. */
250 struct ofpact_vlan_pcp {
251 struct ofpact ofpact;
252 uint8_t vlan_pcp; /* VLAN PCP in low 3 bits, 0 in other bits. */
255 /* OFPACT_SET_ETH_SRC, OFPACT_SET_ETH_DST.
257 * Used for OFPAT10_SET_DL_SRC, OFPAT10_SET_DL_DST. */
259 struct ofpact ofpact;
260 uint8_t mac[ETH_ADDR_LEN];
263 /* OFPACT_SET_IPV4_SRC, OFPACT_SET_IPV4_DST.
265 * Used for OFPAT10_SET_NW_SRC, OFPAT10_SET_NW_DST. */
267 struct ofpact ofpact;
271 /* OFPACT_SET_IPV4_DSCP.
273 * Used for OFPAT10_SET_NW_TOS. */
275 struct ofpact ofpact;
276 uint8_t dscp; /* DSCP in high 6 bits, rest ignored. */
279 /* OFPACT_SET_L4_SRC_PORT, OFPACT_SET_L4_DST_PORT.
281 * Used for OFPAT10_SET_TP_SRC, OFPAT10_SET_TP_DST. */
282 struct ofpact_l4_port {
283 struct ofpact ofpact;
284 uint16_t port; /* TCP or UDP port number. */
289 * Used for NXAST_REG_MOVE. */
290 struct ofpact_reg_move {
291 struct ofpact ofpact;
292 struct mf_subfield src;
293 struct mf_subfield dst;
298 * Used for NXAST_REG_LOAD, OFPAT12_SET_FIELD. */
299 struct ofpact_reg_load {
300 struct ofpact ofpact;
301 struct mf_subfield dst;
302 union mf_subvalue subvalue; /* Least-significant bits are used. */
305 /* OFPACT_SET_TUNNEL.
307 * Used for NXAST_SET_TUNNEL, NXAST_SET_TUNNEL64. */
308 struct ofpact_tunnel {
309 struct ofpact ofpact;
315 * Used for NXAST_SET_QUEUE. */
316 struct ofpact_queue {
317 struct ofpact ofpact;
321 /* OFPACT_FIN_TIMEOUT.
323 * Used for NXAST_FIN_TIMEOUT. */
324 struct ofpact_fin_timeout {
325 struct ofpact ofpact;
326 uint16_t fin_idle_timeout;
327 uint16_t fin_hard_timeout;
332 * Used for NXAST_RESUBMIT, NXAST_RESUBMIT_TABLE. */
333 struct ofpact_resubmit {
334 struct ofpact ofpact;
339 /* Part of struct ofpact_learn, below. */
340 struct ofpact_learn_spec {
344 struct mf_subfield src;
345 union mf_subvalue src_imm;
348 struct mf_subfield dst;
353 * Used for NXAST_LEARN. */
354 struct ofpact_learn {
355 struct ofpact ofpact;
357 uint16_t idle_timeout; /* Idle time before discarding (seconds). */
358 uint16_t hard_timeout; /* Max time before discarding (seconds). */
359 uint16_t priority; /* Priority level of flow entry. */
360 uint64_t cookie; /* Cookie for new flow. */
361 uint16_t flags; /* Either 0 or OFPFF_SEND_FLOW_REM. */
362 uint8_t table_id; /* Table to insert flow entry. */
363 uint16_t fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
364 uint16_t fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
366 unsigned int n_specs;
367 struct ofpact_learn_spec specs[];
372 * Used for NXAST_MULTIPATH. */
373 struct ofpact_multipath {
374 struct ofpact ofpact;
376 /* What fields to hash and how. */
377 enum nx_hash_fields fields;
378 uint16_t basis; /* Universal hash parameter. */
380 /* Multipath link choice algorithm to apply to hash value. */
381 enum nx_mp_algorithm algorithm;
382 uint16_t max_link; /* Number of output links, minus 1. */
383 uint32_t arg; /* Algorithm-specific argument. */
385 /* Where to store the result. */
386 struct mf_subfield dst;
391 * Used for NXAST_AUTOPATH. */
392 struct ofpact_autopath {
393 struct ofpact ofpact;
394 struct mf_subfield dst;
400 * Used for NXAST_NOTE. */
402 struct ofpact ofpact;
409 * Used for NXAST_DEC_TTL and NXAST_DEC_TTL_CNT_IDS. */
410 struct ofpact_cnt_ids {
411 struct ofpact ofpact;
413 /* Controller ids. */
414 unsigned int n_controllers;
419 /* Converting OpenFlow to ofpacts. */
420 enum ofperr ofpacts_pull_openflow10(struct ofpbuf *openflow,
421 unsigned int actions_len,
422 struct ofpbuf *ofpacts);
423 enum ofperr ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
424 unsigned int actions_len,
425 struct ofpbuf *ofpacts);
426 enum ofperr ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
427 unsigned int instructions_len,
428 struct ofpbuf *ofpacts);
429 enum ofperr ofpacts_check(const struct ofpact[], size_t ofpacts_len,
430 const struct flow *, int max_ports);
432 /* Converting ofpacts to OpenFlow. */
433 void ofpacts_put_openflow10(const struct ofpact[], size_t ofpacts_len,
434 struct ofpbuf *openflow);
435 size_t ofpacts_put_openflow11_actions(const struct ofpact[], size_t ofpacts_len,
436 struct ofpbuf *openflow);
437 void ofpacts_put_openflow11_instructions(const struct ofpact[],
439 struct ofpbuf *openflow);
441 /* Working with ofpacts. */
442 bool ofpacts_output_to_port(const struct ofpact[], size_t ofpacts_len,
444 bool ofpacts_equal(const struct ofpact a[], size_t a_len,
445 const struct ofpact b[], size_t b_len);
447 /* Formatting ofpacts.
449 * (For parsing ofpacts, see ofp-parse.h.) */
450 void ofpacts_format(const struct ofpact[], size_t ofpacts_len, struct ds *);
452 /* Internal use by the helpers below. */
453 void ofpact_init(struct ofpact *, enum ofpact_type, size_t len);
454 void *ofpact_put(struct ofpbuf *, enum ofpact_type, size_t len);
456 /* For each OFPACT_<ENUM> with a corresponding struct <STRUCT>, this defines
457 * the following commonly useful functions:
459 * struct <STRUCT> *ofpact_put_<ENUM>(struct ofpbuf *ofpacts);
461 * Appends a new 'ofpact', of length OFPACT_<ENUM>_RAW_SIZE, to 'ofpacts',
462 * initializes it with ofpact_init_<ENUM>(), and returns it. Also sets
463 * 'ofpacts->l2' to the returned action.
465 * After using this function to add a variable-length action, add the
466 * elements of the flexible array (e.g. with ofpbuf_put()), then use
467 * ofpact_update_len() to update the length embedded into the action.
468 * (Keep in mind the need to refresh the structure from 'ofpacts->l2' after
469 * adding data to 'ofpacts'.)
471 * struct <STRUCT> *ofpact_get_<ENUM>(const struct ofpact *ofpact);
473 * Returns 'ofpact' cast to "struct <STRUCT> *". 'ofpact->type' must be
476 * as well as the following more rarely useful definitions:
478 * void ofpact_init_<ENUM>(struct <STRUCT> *ofpact);
480 * Initializes the parts of 'ofpact' that identify it as having type
481 * OFPACT_<ENUM> and length OFPACT_<ENUM>_RAW_SIZE and zeros the rest.
485 * The size of the action structure. For a fixed-length action, this is
486 * sizeof(struct <STRUCT>). For a variable-length action, this is the
487 * offset to the variable-length part.
491 * An integer constant, the value of OFPACT_<ENUM>_RAW_SIZE rounded up to a
492 * multiple of OFPACT_ALIGNTO.
494 #define DEFINE_OFPACT(ENUM, STRUCT, MEMBER) \
495 BUILD_ASSERT_DECL(offsetof(struct STRUCT, ofpact) == 0); \
497 enum { OFPACT_##ENUM##_RAW_SIZE \
498 = (offsetof(struct STRUCT, MEMBER) \
499 ? offsetof(struct STRUCT, MEMBER) \
500 : sizeof(struct STRUCT)) }; \
502 enum { OFPACT_##ENUM##_SIZE \
503 = ROUND_UP(OFPACT_##ENUM##_RAW_SIZE, OFPACT_ALIGNTO) }; \
505 static inline struct STRUCT * \
506 ofpact_get_##ENUM(const struct ofpact *ofpact) \
508 assert(ofpact->type == OFPACT_##ENUM); \
509 return (struct STRUCT *) ofpact; \
512 static inline struct STRUCT * \
513 ofpact_put_##ENUM(struct ofpbuf *ofpacts) \
515 return ofpact_put(ofpacts, OFPACT_##ENUM, \
516 OFPACT_##ENUM##_RAW_SIZE); \
520 ofpact_init_##ENUM(struct STRUCT *ofpact) \
522 ofpact_init(&ofpact->ofpact, OFPACT_##ENUM, \
523 OFPACT_##ENUM##_RAW_SIZE); \
528 /* Functions to use after adding ofpacts to a buffer. */
529 void ofpact_update_len(struct ofpbuf *, struct ofpact *);
530 void ofpact_pad(struct ofpbuf *);
532 /* OpenFlow 1.1 instructions.
533 * The order is sorted in execution order. Not in the value of OFPIT11_xxx.
534 * It is enforced on parser from text string.
536 #define OVS_INSTRUCTIONS \
537 DEFINE_INST(OFPIT11_APPLY_ACTIONS, \
538 ofp11_instruction_actions, true, \
541 DEFINE_INST(OFPIT11_CLEAR_ACTIONS, \
542 ofp11_instruction, false, \
545 DEFINE_INST(OFPIT11_WRITE_ACTIONS, \
546 ofp11_instruction_actions, true, \
549 DEFINE_INST(OFPIT11_WRITE_METADATA, \
550 ofp11_instruction_write_metadata, false, \
553 DEFINE_INST(OFPIT11_GOTO_TABLE, \
554 ofp11_instruction_goto_table, false, \
557 enum ovs_instruction_type {
558 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) OVSINST_##ENUM,
564 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
565 N_OVS_INSTRUCTIONS = OVS_INSTRUCTIONS
569 const char *ofpact_instruction_name_from_type(enum ovs_instruction_type type);
570 int ofpact_instruction_type_from_name(const char *name);
572 void ofpact_set_field_init(struct ofpact_reg_load *load,
573 const struct mf_field *mf, const void *src);
575 #endif /* ofp-actions.h */