1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. */
17 #include "ofproto/ofproto-dpif-xlate.h"
20 #include <sys/types.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
24 #include <sys/socket.h>
30 #include "byte-order.h"
35 #include "dp-packet.h"
40 #include "mac-learning.h"
41 #include "mcast-snooping.h"
42 #include "multipath.h"
43 #include "netdev-vport.h"
46 #include "odp-execute.h"
47 #include "ofproto/ofproto-dpif-ipfix.h"
48 #include "ofproto/ofproto-dpif-mirror.h"
49 #include "ofproto/ofproto-dpif-monitor.h"
50 #include "ofproto/ofproto-dpif-sflow.h"
51 #include "ofproto/ofproto-dpif-trace.h"
52 #include "ofproto/ofproto-dpif-xlate-cache.h"
53 #include "ofproto/ofproto-dpif.h"
54 #include "ofproto/ofproto-provider.h"
55 #include "openvswitch/dynamic-string.h"
56 #include "openvswitch/meta-flow.h"
57 #include "openvswitch/list.h"
58 #include "openvswitch/ofp-actions.h"
59 #include "openvswitch/ofp-ed-props.h"
60 #include "openvswitch/vlog.h"
62 #include "ovs-router.h"
64 #include "tnl-neigh-cache.h"
65 #include "tnl-ports.h"
70 COVERAGE_DEFINE(xlate_actions
);
71 COVERAGE_DEFINE(xlate_actions_oversize
);
72 COVERAGE_DEFINE(xlate_actions_too_many_output
);
74 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate
);
76 /* Maximum depth of flow table recursion (due to resubmit actions) in a
79 * The goal of limiting the depth of resubmits is to ensure that flow
80 * translation eventually terminates. Only resubmits to the same table or an
81 * earlier table count against the maximum depth. This is because resubmits to
82 * strictly monotonically increasing table IDs will eventually terminate, since
83 * any OpenFlow switch has a finite number of tables. OpenFlow tables are most
84 * commonly traversed in numerically increasing order, so this limit has little
85 * effect on conventionally designed OpenFlow pipelines.
87 * Outputs to patch ports and to groups also count against the depth limit. */
90 /* Maximum number of resubmit actions in a flow translation, whether they are
91 * recursive or not. */
92 #define MAX_RESUBMITS (MAX_DEPTH * MAX_DEPTH)
95 struct hmap_node hmap_node
; /* Node in global 'xbridges' map. */
96 struct ofproto_dpif
*ofproto
; /* Key in global 'xbridges' map. */
98 struct ovs_list xbundles
; /* Owned xbundles. */
99 struct hmap xports
; /* Indexed by ofp_port. */
101 char *name
; /* Name used in log messages. */
102 struct dpif
*dpif
; /* Datapath interface. */
103 struct mac_learning
*ml
; /* Mac learning handle. */
104 struct mcast_snooping
*ms
; /* Multicast Snooping handle. */
105 struct mbridge
*mbridge
; /* Mirroring. */
106 struct dpif_sflow
*sflow
; /* SFlow handle, or null. */
107 struct dpif_ipfix
*ipfix
; /* Ipfix handle, or null. */
108 struct netflow
*netflow
; /* Netflow handle, or null. */
109 struct stp
*stp
; /* STP or null if disabled. */
110 struct rstp
*rstp
; /* RSTP or null if disabled. */
112 bool has_in_band
; /* Bridge has in band control? */
113 bool forward_bpdu
; /* Bridge forwards STP BPDUs? */
115 /* Datapath feature support. */
116 struct dpif_backer_support support
;
120 struct hmap_node hmap_node
; /* In global 'xbundles' map. */
121 struct ofbundle
*ofbundle
; /* Key in global 'xbundles' map. */
123 struct ovs_list list_node
; /* In parent 'xbridges' list. */
124 struct xbridge
*xbridge
; /* Parent xbridge. */
126 struct ovs_list xports
; /* Contains "struct xport"s. */
128 char *name
; /* Name used in log messages. */
129 struct bond
*bond
; /* Nonnull iff more than one port. */
130 struct lacp
*lacp
; /* LACP handle or null. */
132 enum port_vlan_mode vlan_mode
; /* VLAN mode. */
133 uint16_t qinq_ethtype
; /* Ethertype of dot1q-tunnel interface
134 * either 0x8100 or 0x88a8. */
135 int vlan
; /* -1=trunk port, else a 12-bit VLAN ID. */
136 unsigned long *trunks
; /* Bitmap of trunked VLANs, if 'vlan' == -1.
137 * NULL if all VLANs are trunked. */
138 unsigned long *cvlans
; /* Bitmap of allowed customer vlans,
139 * NULL if all VLANs are allowed */
140 bool use_priority_tags
; /* Use 802.1p tag for frames in VLAN 0? */
141 bool floodable
; /* No port has OFPUTIL_PC_NO_FLOOD set? */
142 bool protected; /* Protected port mode */
146 struct hmap_node hmap_node
; /* Node in global 'xports' map. */
147 struct ofport_dpif
*ofport
; /* Key in global 'xports map. */
149 struct hmap_node ofp_node
; /* Node in parent xbridge 'xports' map. */
150 ofp_port_t ofp_port
; /* Key in parent xbridge 'xports' map. */
152 struct hmap_node uuid_node
; /* Node in global 'xports_uuid' map. */
153 struct uuid uuid
; /* Key in global 'xports_uuid' map. */
155 odp_port_t odp_port
; /* Datapath port number or ODPP_NONE. */
157 struct ovs_list bundle_node
; /* In parent xbundle (if it exists). */
158 struct xbundle
*xbundle
; /* Parent xbundle or null. */
160 struct netdev
*netdev
; /* 'ofport''s netdev. */
162 struct xbridge
*xbridge
; /* Parent bridge. */
163 struct xport
*peer
; /* Patch port peer or null. */
165 enum ofputil_port_config config
; /* OpenFlow port configuration. */
166 enum ofputil_port_state state
; /* OpenFlow port state. */
167 int stp_port_no
; /* STP port number or -1 if not in use. */
168 struct rstp_port
*rstp_port
; /* RSTP port or null. */
170 struct hmap skb_priorities
; /* Map of 'skb_priority_to_dscp's. */
172 bool may_enable
; /* May be enabled in bonds. */
173 bool is_tunnel
; /* Is a tunnel port. */
174 enum netdev_pt_mode pt_mode
; /* packet_type handling. */
176 struct cfm
*cfm
; /* CFM handle or null. */
177 struct bfd
*bfd
; /* BFD handle or null. */
178 struct lldp
*lldp
; /* LLDP handle or null. */
182 struct xlate_in
*xin
;
183 struct xlate_out
*xout
;
185 struct xlate_cfg
*xcfg
;
186 const struct xbridge
*xbridge
;
188 /* Flow at the last commit. */
189 struct flow base_flow
;
191 /* Tunnel IP destination address as received. This is stored separately
192 * as the base_flow.tunnel is cleared on init to reflect the datapath
193 * behavior. Used to make sure not to send tunneled output to ourselves,
194 * which might lead to an infinite loop. This could happen easily
195 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
196 * actually set the tun_dst field. */
197 struct in6_addr orig_tunnel_ipv6_dst
;
199 /* Stack for the push and pop actions. See comment above nx_stack_push()
200 * in nx-match.c for info on how the stack is stored. */
203 /* The rule that we are currently translating, or NULL. */
204 struct rule_dpif
*rule
;
206 /* Flow translation populates this with wildcards relevant in translation.
207 * When 'xin->wc' is nonnull, this is the same pointer. When 'xin->wc' is
208 * null, this is a pointer to a temporary buffer. */
209 struct flow_wildcards
*wc
;
211 /* Output buffer for datapath actions. When 'xin->odp_actions' is nonnull,
212 * this is the same pointer. When 'xin->odp_actions' is null, this points
213 * to a scratch ofpbuf. This allows code to add actions to
214 * 'ctx->odp_actions' without worrying about whether the caller really
216 struct ofpbuf
*odp_actions
;
218 /* Statistics maintained by xlate_table_action().
220 * These statistics limit the amount of work that a single flow
221 * translation can perform. The goal of the first of these, 'depth', is
222 * primarily to prevent translation from performing an infinite amount of
223 * work. It counts the current depth of nested "resubmit"s (and a few
224 * other activities); when a resubmit returns, it decreases. Resubmits to
225 * tables in strictly monotonically increasing order don't contribute to
226 * 'depth' because they cannot cause a flow translation to take an infinite
227 * amount of time (because the number of tables is finite). Translation
228 * aborts when 'depth' exceeds MAX_DEPTH.
230 * 'resubmits', on the other hand, prevents flow translation from
231 * performing an extraordinarily large while still finite amount of work.
232 * It counts the total number of resubmits (and a few other activities)
233 * that have been executed. Returning from a resubmit does not affect this
234 * counter. Thus, this limits the amount of work that a particular
235 * translation can perform. Translation aborts when 'resubmits' exceeds
236 * MAX_RESUBMITS (which is much larger than MAX_DEPTH).
238 int depth
; /* Current resubmit nesting depth. */
239 int resubmits
; /* Total number of resubmits. */
240 bool in_group
; /* Currently translating ofgroup, if true. */
241 bool in_action_set
; /* Currently translating action_set, if true. */
242 bool in_packet_out
; /* Currently translating a packet_out msg, if
244 bool pending_encap
; /* True when waiting to commit a pending
246 bool pending_decap
; /* True when waiting to commit a pending
248 struct ofpbuf
*encap_data
; /* May contain a pointer to an ofpbuf with
249 * context for the datapath encap action.*/
251 uint8_t table_id
; /* OpenFlow table ID where flow was found. */
252 ovs_be64 rule_cookie
; /* Cookie of the rule being translated. */
253 uint32_t orig_skb_priority
; /* Priority when packet arrived. */
254 uint32_t sflow_n_outputs
; /* Number of output ports. */
255 odp_port_t sflow_odp_port
; /* Output port for composing sFlow action. */
256 ofp_port_t nf_output_iface
; /* Output interface index for NetFlow. */
257 bool exit
; /* No further actions should be processed. */
258 mirror_mask_t mirrors
; /* Bitmap of associated mirrors. */
259 int mirror_snaplen
; /* Max size of a mirror packet in byte. */
261 /* Freezing Translation
262 * ====================
264 * At some point during translation, the code may recognize the need to halt
265 * and checkpoint the translation in a way that it can be restarted again
266 * later. We call the checkpointing process "freezing" and the restarting
269 * The use cases for freezing are:
271 * - "Recirculation", where the translation process discovers that it
272 * doesn't have enough information to complete translation without
273 * actually executing the actions that have already been translated,
274 * which provides the additionally needed information. In these
275 * situations, translation freezes translation and assigns the frozen
276 * data a unique "recirculation ID", which it associates with the data
277 * in a table in userspace (see ofproto-dpif-rid.h). It also adds a
278 * OVS_ACTION_ATTR_RECIRC action specifying that ID to the datapath
279 * actions. When a packet hits that action, the datapath looks its
280 * flow up again using the ID. If there's a miss, it comes back to
281 * userspace, which find the recirculation table entry for the ID,
282 * thaws the associated frozen data, and continues translation from
283 * that point given the additional information that is now known.
285 * The archetypal example is MPLS. As MPLS is implemented in
286 * OpenFlow, the protocol that follows the last MPLS label becomes
287 * known only when that label is popped by an OpenFlow action. That
288 * means that Open vSwitch can't extract the headers beyond the MPLS
289 * labels until the pop action is executed. Thus, at that point
290 * translation uses the recirculation process to extract the headers
291 * beyond the MPLS labels.
293 * (OVS also uses OVS_ACTION_ATTR_RECIRC to implement hashing for
294 * output to bonds. OVS pre-populates all the datapath flows for bond
295 * output in the datapath, though, which means that the elaborate
296 * process of coming back to userspace for a second round of
297 * translation isn't needed, and so bonds don't follow the above
300 * - "Continuation". A continuation is a way for an OpenFlow controller
301 * to interpose on a packet's traversal of the OpenFlow tables. When
302 * the translation process encounters a "controller" action with the
303 * "pause" flag, it freezes translation, serializes the frozen data,
304 * and sends it to an OpenFlow controller. The controller then
305 * examines and possibly modifies the frozen data and eventually sends
306 * it back to the switch, which thaws it and continues translation.
308 * The main problem of freezing translation is preserving state, so that
309 * when the translation is thawed later it resumes from where it left off,
310 * without disruption. In particular, actions must be preserved as follows:
312 * - If we're freezing because an action needed more information, the
313 * action that prompted it.
315 * - Any actions remaining to be translated within the current flow.
317 * - If translation was frozen within a NXAST_RESUBMIT, then any actions
318 * following the resubmit action. Resubmit actions can be nested, so
319 * this has to go all the way up the control stack.
321 * - The OpenFlow 1.1+ action set.
323 * State that actions and flow table lookups can depend on, such as the
324 * following, must also be preserved:
326 * - Metadata fields (input port, registers, OF1.1+ metadata, ...).
328 * - The stack used by NXAST_STACK_PUSH and NXAST_STACK_POP actions.
330 * - The table ID and cookie of the flow being translated at each level
331 * of the control stack, because these can become visible through
332 * OFPAT_CONTROLLER actions (and other ways).
334 * Translation allows for the control of this state preservation via these
335 * members. When a need to freeze translation is identified, the
336 * translation process:
338 * 1. Sets 'freezing' to true.
340 * 2. Sets 'exit' to true to tell later steps that we're exiting from the
341 * translation process.
343 * 3. Adds an OFPACT_UNROLL_XLATE action to 'frozen_actions', and points
344 * frozen_actions.header to the action to make it easy to find it later.
345 * This action holds the current table ID and cookie so that they can be
346 * restored during a post-recirculation upcall translation.
348 * 4. Adds the action that prompted recirculation and any actions following
349 * it within the same flow to 'frozen_actions', so that they can be
350 * executed during a post-recirculation upcall translation.
354 * 6. The action that prompted recirculation might be nested in a stack of
355 * nested "resubmit"s that have actions remaining. Each of these notices
356 * that we're exiting and freezing and responds by adding more
357 * OFPACT_UNROLL_XLATE actions to 'frozen_actions', as necessary,
358 * followed by any actions that were yet unprocessed.
360 * If we're freezing because of recirculation, the caller generates a
361 * recirculation ID and associates all the state produced by this process
362 * with it. For post-recirculation upcall translation, the caller passes it
363 * back in for the new translation to execute. The process yielded a set of
364 * ofpacts that can be translated directly, so it is not much of a special
365 * case at that point.
368 bool recirc_update_dp_hash
; /* Generated recirculation will be preceded
369 * by datapath HASH action to get an updated
370 * dp_hash after recirculation. */
371 uint32_t dp_hash_alg
;
372 uint32_t dp_hash_basis
;
373 struct ofpbuf frozen_actions
;
374 const struct ofpact_controller
*pause
;
376 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
377 * This is a trigger for recirculation in cases where translating an action
378 * or looking up a flow requires access to the fields of the packet after
379 * the MPLS label stack that was originally present. */
382 /* True if conntrack has been performed on this packet during processing
383 * on the current bridge. This is used to determine whether conntrack
384 * state from the datapath should be honored after thawing. */
387 /* Pointer to an embedded NAT action in a conntrack action, or NULL. */
388 struct ofpact_nat
*ct_nat_action
;
390 /* OpenFlow 1.1+ action set.
392 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
393 * When translation is otherwise complete, ofpacts_execute_action_set()
394 * converts it to a set of "struct ofpact"s that can be translated into
395 * datapath actions. */
396 bool action_set_has_group
; /* Action set contains OFPACT_GROUP? */
397 struct ofpbuf action_set
; /* Action set. */
399 enum xlate_error error
; /* Translation failed. */
402 /* Structure to track VLAN manipulation */
403 struct xvlan_single
{
410 struct xvlan_single v
[FLOW_MAX_VLAN_HEADERS
];
413 const char *xlate_strerror(enum xlate_error error
)
418 case XLATE_BRIDGE_NOT_FOUND
:
419 return "Bridge not found";
420 case XLATE_RECURSION_TOO_DEEP
:
421 return "Recursion too deep";
422 case XLATE_TOO_MANY_RESUBMITS
:
423 return "Too many resubmits";
424 case XLATE_STACK_TOO_DEEP
:
425 return "Stack too deep";
426 case XLATE_NO_RECIRCULATION_CONTEXT
:
427 return "No recirculation context";
428 case XLATE_RECIRCULATION_CONFLICT
:
429 return "Recirculation conflict";
430 case XLATE_TOO_MANY_MPLS_LABELS
:
431 return "Too many MPLS labels";
432 case XLATE_INVALID_TUNNEL_METADATA
:
433 return "Invalid tunnel metadata";
434 case XLATE_UNSUPPORTED_PACKET_TYPE
:
435 return "Unsupported packet type";
437 return "Unknown error";
440 static void xlate_action_set(struct xlate_ctx
*ctx
);
441 static void xlate_commit_actions(struct xlate_ctx
*ctx
);
444 patch_port_output(struct xlate_ctx
*ctx
, const struct xport
*in_dev
,
445 struct xport
*out_dev
);
448 ctx_trigger_freeze(struct xlate_ctx
*ctx
)
451 ctx
->freezing
= true;
455 ctx_trigger_recirculate_with_hash(struct xlate_ctx
*ctx
, uint32_t type
,
459 ctx
->freezing
= true;
460 ctx
->recirc_update_dp_hash
= true;
461 ctx
->dp_hash_alg
= type
;
462 ctx
->dp_hash_basis
= basis
;
466 ctx_first_frozen_action(const struct xlate_ctx
*ctx
)
468 return !ctx
->frozen_actions
.size
;
472 ctx_cancel_freeze(struct xlate_ctx
*ctx
)
475 ctx
->freezing
= false;
476 ctx
->recirc_update_dp_hash
= false;
477 ofpbuf_clear(&ctx
->frozen_actions
);
478 ctx
->frozen_actions
.header
= NULL
;
482 static void finish_freezing(struct xlate_ctx
*ctx
);
484 /* A controller may use OFPP_NONE as the ingress port to indicate that
485 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
486 * when an input bundle is needed for validation (e.g., mirroring or
487 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
488 * any 'port' structs, so care must be taken when dealing with it. */
489 static struct xbundle ofpp_none_bundle
= {
491 .vlan_mode
= PORT_VLAN_TRUNK
494 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
495 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
496 * traffic egressing the 'ofport' with that priority should be marked with. */
497 struct skb_priority_to_dscp
{
498 struct hmap_node hmap_node
; /* Node in 'ofport_dpif''s 'skb_priorities'. */
499 uint32_t skb_priority
; /* Priority of this queue (see struct flow). */
501 uint8_t dscp
; /* DSCP bits to mark outgoing traffic with. */
504 /* Xlate config contains hash maps of all bridges, bundles and ports.
505 * Xcfgp contains the pointer to the current xlate configuration.
506 * When the main thread needs to change the configuration, it copies xcfgp to
507 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
508 * does not block handler and revalidator threads. */
510 struct hmap xbridges
;
511 struct hmap xbundles
;
513 struct hmap xports_uuid
;
515 static OVSRCU_TYPE(struct xlate_cfg
*) xcfgp
= OVSRCU_INITIALIZER(NULL
);
516 static struct xlate_cfg
*new_xcfg
= NULL
;
518 typedef void xlate_actions_handler(const struct ofpact
*, size_t ofpacts_len
,
519 struct xlate_ctx
*, bool);
520 static bool may_receive(const struct xport
*, struct xlate_ctx
*);
521 static void do_xlate_actions(const struct ofpact
*, size_t ofpacts_len
,
522 struct xlate_ctx
*, bool);
523 static void clone_xlate_actions(const struct ofpact
*, size_t ofpacts_len
,
524 struct xlate_ctx
*, bool);
525 static void xlate_normal(struct xlate_ctx
*);
526 static void xlate_table_action(struct xlate_ctx
*, ofp_port_t in_port
,
527 uint8_t table_id
, bool may_packet_in
,
528 bool honor_table_miss
, bool with_ct_orig
,
529 bool is_last_action
, xlate_actions_handler
*);
531 static bool input_vid_is_valid(const struct xlate_ctx
*,
532 uint16_t vid
, struct xbundle
*);
533 static void xvlan_copy(struct xvlan
*dst
, const struct xvlan
*src
);
534 static void xvlan_pop(struct xvlan
*src
);
535 static void xvlan_push_uninit(struct xvlan
*src
);
536 static void xvlan_extract(const struct flow
*, struct xvlan
*);
537 static void xvlan_put(struct flow
*, const struct xvlan
*);
538 static void xvlan_input_translate(const struct xbundle
*,
539 const struct xvlan
*in
,
540 struct xvlan
*xvlan
);
541 static void xvlan_output_translate(const struct xbundle
*,
542 const struct xvlan
*xvlan
,
544 static void output_normal(struct xlate_ctx
*, const struct xbundle
*,
545 const struct xvlan
*);
547 /* Optional bond recirculation parameter to compose_output_action(). */
548 struct xlate_bond_recirc
{
549 uint32_t recirc_id
; /* !0 Use recirculation instead of output. */
550 uint8_t hash_alg
; /* !0 Compute hash for recirc before. */
551 uint32_t hash_basis
; /* Compute hash for recirc before. */
554 static void compose_output_action(struct xlate_ctx
*, ofp_port_t ofp_port
,
555 const struct xlate_bond_recirc
*xr
,
556 bool is_last_action
, bool truncate
);
558 static struct xbridge
*xbridge_lookup(struct xlate_cfg
*,
559 const struct ofproto_dpif
*);
560 static struct xbridge
*xbridge_lookup_by_uuid(struct xlate_cfg
*,
561 const struct uuid
*);
562 static struct xbundle
*xbundle_lookup(struct xlate_cfg
*,
563 const struct ofbundle
*);
564 static struct xport
*xport_lookup(struct xlate_cfg
*,
565 const struct ofport_dpif
*);
566 static struct xport
*xport_lookup_by_uuid(struct xlate_cfg
*,
567 const struct uuid
*);
568 static struct xport
*get_ofp_port(const struct xbridge
*, ofp_port_t ofp_port
);
569 static struct skb_priority_to_dscp
*get_skb_priority(const struct xport
*,
570 uint32_t skb_priority
);
571 static void clear_skb_priorities(struct xport
*);
572 static size_t count_skb_priorities(const struct xport
*);
573 static bool dscp_from_skb_priority(const struct xport
*, uint32_t skb_priority
,
576 static void xlate_xbridge_init(struct xlate_cfg
*, struct xbridge
*);
577 static void xlate_xbundle_init(struct xlate_cfg
*, struct xbundle
*);
578 static void xlate_xport_init(struct xlate_cfg
*, struct xport
*);
579 static void xlate_xbridge_set(struct xbridge
*, struct dpif
*,
580 const struct mac_learning
*, struct stp
*,
581 struct rstp
*, const struct mcast_snooping
*,
582 const struct mbridge
*,
583 const struct dpif_sflow
*,
584 const struct dpif_ipfix
*,
585 const struct netflow
*,
586 bool forward_bpdu
, bool has_in_band
,
587 const struct dpif_backer_support
*);
588 static void xlate_xbundle_set(struct xbundle
*xbundle
,
589 enum port_vlan_mode vlan_mode
,
590 uint16_t qinq_ethtype
, int vlan
,
591 unsigned long *trunks
, unsigned long *cvlans
,
592 bool use_priority_tags
,
593 const struct bond
*bond
, const struct lacp
*lacp
,
594 bool floodable
, bool protected);
595 static void xlate_xport_set(struct xport
*xport
, odp_port_t odp_port
,
596 const struct netdev
*netdev
, const struct cfm
*cfm
,
597 const struct bfd
*bfd
, const struct lldp
*lldp
,
598 int stp_port_no
, const struct rstp_port
*rstp_port
,
599 enum ofputil_port_config config
,
600 enum ofputil_port_state state
, bool is_tunnel
,
602 static void xlate_xbridge_remove(struct xlate_cfg
*, struct xbridge
*);
603 static void xlate_xbundle_remove(struct xlate_cfg
*, struct xbundle
*);
604 static void xlate_xport_remove(struct xlate_cfg
*, struct xport
*);
605 static void xlate_xbridge_copy(struct xbridge
*);
606 static void xlate_xbundle_copy(struct xbridge
*, struct xbundle
*);
607 static void xlate_xport_copy(struct xbridge
*, struct xbundle
*,
609 static void xlate_xcfg_free(struct xlate_cfg
*);
611 /* Tracing helpers. */
613 /* If tracing is enabled in 'ctx', creates a new trace node and appends it to
614 * the list of nodes maintained in ctx->xin. The new node has type 'type' and
615 * its text is created from 'format' by treating it as a printf format string.
616 * Returns the list of nodes embedded within the new trace node; ordinarily,
617 * the calleer can ignore this, but it is useful if the caller needs to nest
618 * more trace nodes within the new node.
620 * If tracing is not enabled, does nothing and returns NULL. */
621 static struct ovs_list
* OVS_PRINTF_FORMAT(3, 4)
622 xlate_report(const struct xlate_ctx
*ctx
, enum oftrace_node_type type
,
623 const char *format
, ...)
625 struct ovs_list
*subtrace
= NULL
;
626 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
628 va_start(args
, format
);
629 char *text
= xvasprintf(format
, args
);
630 subtrace
= &oftrace_report(ctx
->xin
->trace
, type
, text
)->subs
;
637 /* This is like xlate_report() for errors that are serious enough that we
638 * should log them even if we are not tracing. */
639 static void OVS_PRINTF_FORMAT(2, 3)
640 xlate_report_error(const struct xlate_ctx
*ctx
, const char *format
, ...)
642 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
643 if (!OVS_UNLIKELY(ctx
->xin
->trace
)
644 && (!ctx
->xin
->packet
|| VLOG_DROP_WARN(&rl
))) {
648 struct ds s
= DS_EMPTY_INITIALIZER
;
650 va_start(args
, format
);
651 ds_put_format_valist(&s
, format
, args
);
654 if (ctx
->xin
->trace
) {
655 oftrace_report(ctx
->xin
->trace
, OFT_ERROR
, ds_cstr(&s
));
657 ds_put_cstr(&s
, " while processing ");
658 flow_format(&s
, &ctx
->base_flow
, NULL
);
659 ds_put_format(&s
, " on bridge %s", ctx
->xbridge
->name
);
660 VLOG_WARN("%s", ds_cstr(&s
));
665 /* This is like xlate_report() for messages that should be logged at debug
666 * level (even if we are not tracing) because they can be valuable for
668 static void OVS_PRINTF_FORMAT(3, 4)
669 xlate_report_debug(const struct xlate_ctx
*ctx
, enum oftrace_node_type type
,
670 const char *format
, ...)
672 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(30, 300);
673 if (!OVS_UNLIKELY(ctx
->xin
->trace
)
674 && (!ctx
->xin
->packet
|| VLOG_DROP_DBG(&rl
))) {
678 struct ds s
= DS_EMPTY_INITIALIZER
;
680 va_start(args
, format
);
681 ds_put_format_valist(&s
, format
, args
);
684 if (ctx
->xin
->trace
) {
685 oftrace_report(ctx
->xin
->trace
, type
, ds_cstr(&s
));
687 VLOG_DBG("bridge %s: %s", ctx
->xbridge
->name
, ds_cstr(&s
));
692 /* If tracing is enabled in 'ctx', appends a node of the given 'type' to the
693 * trace, whose text is 'title' followed by a formatted version of the
694 * 'ofpacts_len' OpenFlow actions in 'ofpacts'.
696 * If tracing is not enabled, does nothing. */
698 xlate_report_actions(const struct xlate_ctx
*ctx
, enum oftrace_node_type type
,
700 const struct ofpact
*ofpacts
, size_t ofpacts_len
)
702 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
703 struct ds s
= DS_EMPTY_INITIALIZER
;
704 ds_put_format(&s
, "%s: ", title
);
705 ofpacts_format(ofpacts
, ofpacts_len
, NULL
, &s
);
706 oftrace_report(ctx
->xin
->trace
, type
, ds_cstr(&s
));
711 /* If tracing is enabled in 'ctx', appends a node of type OFT_DETAIL to the
712 * trace, whose the message is a formatted version of the OpenFlow action set.
713 * 'verb' should be "was" or "is", depending on whether the action set reported
714 * is the new action set or the old one.
716 * If tracing is not enabled, does nothing. */
718 xlate_report_action_set(const struct xlate_ctx
*ctx
, const char *verb
)
720 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
721 struct ofpbuf action_list
;
722 ofpbuf_init(&action_list
, 0);
723 ofpacts_execute_action_set(&action_list
, &ctx
->action_set
);
724 if (action_list
.size
) {
725 struct ds s
= DS_EMPTY_INITIALIZER
;
726 ofpacts_format(action_list
.data
, action_list
.size
, NULL
, &s
);
727 xlate_report(ctx
, OFT_DETAIL
, "action set %s: %s",
731 xlate_report(ctx
, OFT_DETAIL
, "action set %s empty", verb
);
733 ofpbuf_uninit(&action_list
);
738 /* If tracing is enabled in 'ctx', appends a node representing 'rule' (in
739 * OpenFlow table 'table_id') to the trace and makes this node the parent for
740 * future trace nodes. The caller should save ctx->xin->trace before calling
741 * this function, then after tracing all of the activities under the table,
742 * restore its previous value.
744 * If tracing is not enabled, does nothing. */
746 xlate_report_table(const struct xlate_ctx
*ctx
, struct rule_dpif
*rule
,
749 if (OVS_LIKELY(!ctx
->xin
->trace
)) {
753 struct ds s
= DS_EMPTY_INITIALIZER
;
754 ds_put_format(&s
, "%2d. ", table_id
);
755 if (rule
== ctx
->xin
->ofproto
->miss_rule
) {
756 ds_put_cstr(&s
, "No match, and a \"packet-in\" is called for.");
757 } else if (rule
== ctx
->xin
->ofproto
->no_packet_in_rule
) {
758 ds_put_cstr(&s
, "No match.");
759 } else if (rule
== ctx
->xin
->ofproto
->drop_frags_rule
) {
760 ds_put_cstr(&s
, "Packets are IP fragments and "
761 "the fragment handling mode is \"drop\".");
763 minimatch_format(&rule
->up
.cr
.match
,
764 ofproto_get_tun_tab(&ctx
->xin
->ofproto
->up
),
765 NULL
, &s
, OFP_DEFAULT_PRIORITY
);
766 if (ds_last(&s
) != ' ') {
767 ds_put_cstr(&s
, ", ");
769 ds_put_format(&s
, "priority %d", rule
->up
.cr
.priority
);
770 if (rule
->up
.flow_cookie
) {
771 ds_put_format(&s
, ", cookie %#"PRIx64
,
772 ntohll(rule
->up
.flow_cookie
));
775 ctx
->xin
->trace
= &oftrace_report(ctx
->xin
->trace
, OFT_TABLE
,
780 /* If tracing is enabled in 'ctx', adds an OFT_DETAIL trace node to 'ctx'
781 * reporting the value of subfield 'sf'.
783 * If tracing is not enabled, does nothing. */
785 xlate_report_subfield(const struct xlate_ctx
*ctx
,
786 const struct mf_subfield
*sf
)
788 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
789 struct ds s
= DS_EMPTY_INITIALIZER
;
790 mf_format_subfield(sf
, &s
);
791 ds_put_cstr(&s
, " is now ");
793 if (sf
->ofs
== 0 && sf
->n_bits
>= sf
->field
->n_bits
) {
794 union mf_value value
;
795 mf_get_value(sf
->field
, &ctx
->xin
->flow
, &value
);
796 mf_format(sf
->field
, &value
, NULL
, NULL
, &s
);
798 union mf_subvalue cst
;
799 mf_read_subfield(sf
, &ctx
->xin
->flow
, &cst
);
800 ds_put_hex(&s
, &cst
, sizeof cst
);
803 xlate_report(ctx
, OFT_DETAIL
, "%s", ds_cstr(&s
));
810 xlate_xbridge_init(struct xlate_cfg
*xcfg
, struct xbridge
*xbridge
)
812 ovs_list_init(&xbridge
->xbundles
);
813 hmap_init(&xbridge
->xports
);
814 hmap_insert(&xcfg
->xbridges
, &xbridge
->hmap_node
,
815 hash_pointer(xbridge
->ofproto
, 0));
819 xlate_xbundle_init(struct xlate_cfg
*xcfg
, struct xbundle
*xbundle
)
821 ovs_list_init(&xbundle
->xports
);
822 ovs_list_insert(&xbundle
->xbridge
->xbundles
, &xbundle
->list_node
);
823 hmap_insert(&xcfg
->xbundles
, &xbundle
->hmap_node
,
824 hash_pointer(xbundle
->ofbundle
, 0));
828 xlate_xport_init(struct xlate_cfg
*xcfg
, struct xport
*xport
)
830 hmap_init(&xport
->skb_priorities
);
831 hmap_insert(&xcfg
->xports
, &xport
->hmap_node
,
832 hash_pointer(xport
->ofport
, 0));
833 hmap_insert(&xport
->xbridge
->xports
, &xport
->ofp_node
,
834 hash_ofp_port(xport
->ofp_port
));
835 hmap_insert(&xcfg
->xports_uuid
, &xport
->uuid_node
,
836 uuid_hash(&xport
->uuid
));
840 xlate_xbridge_set(struct xbridge
*xbridge
,
842 const struct mac_learning
*ml
, struct stp
*stp
,
843 struct rstp
*rstp
, const struct mcast_snooping
*ms
,
844 const struct mbridge
*mbridge
,
845 const struct dpif_sflow
*sflow
,
846 const struct dpif_ipfix
*ipfix
,
847 const struct netflow
*netflow
,
848 bool forward_bpdu
, bool has_in_band
,
849 const struct dpif_backer_support
*support
)
851 if (xbridge
->ml
!= ml
) {
852 mac_learning_unref(xbridge
->ml
);
853 xbridge
->ml
= mac_learning_ref(ml
);
856 if (xbridge
->ms
!= ms
) {
857 mcast_snooping_unref(xbridge
->ms
);
858 xbridge
->ms
= mcast_snooping_ref(ms
);
861 if (xbridge
->mbridge
!= mbridge
) {
862 mbridge_unref(xbridge
->mbridge
);
863 xbridge
->mbridge
= mbridge_ref(mbridge
);
866 if (xbridge
->sflow
!= sflow
) {
867 dpif_sflow_unref(xbridge
->sflow
);
868 xbridge
->sflow
= dpif_sflow_ref(sflow
);
871 if (xbridge
->ipfix
!= ipfix
) {
872 dpif_ipfix_unref(xbridge
->ipfix
);
873 xbridge
->ipfix
= dpif_ipfix_ref(ipfix
);
876 if (xbridge
->stp
!= stp
) {
877 stp_unref(xbridge
->stp
);
878 xbridge
->stp
= stp_ref(stp
);
881 if (xbridge
->rstp
!= rstp
) {
882 rstp_unref(xbridge
->rstp
);
883 xbridge
->rstp
= rstp_ref(rstp
);
886 if (xbridge
->netflow
!= netflow
) {
887 netflow_unref(xbridge
->netflow
);
888 xbridge
->netflow
= netflow_ref(netflow
);
891 xbridge
->dpif
= dpif
;
892 xbridge
->forward_bpdu
= forward_bpdu
;
893 xbridge
->has_in_band
= has_in_band
;
894 xbridge
->support
= *support
;
898 xlate_xbundle_set(struct xbundle
*xbundle
,
899 enum port_vlan_mode vlan_mode
, uint16_t qinq_ethtype
,
900 int vlan
, unsigned long *trunks
, unsigned long *cvlans
,
901 bool use_priority_tags
,
902 const struct bond
*bond
, const struct lacp
*lacp
,
903 bool floodable
, bool protected)
905 ovs_assert(xbundle
->xbridge
);
907 xbundle
->vlan_mode
= vlan_mode
;
908 xbundle
->qinq_ethtype
= qinq_ethtype
;
909 xbundle
->vlan
= vlan
;
910 xbundle
->trunks
= trunks
;
911 xbundle
->cvlans
= cvlans
;
912 xbundle
->use_priority_tags
= use_priority_tags
;
913 xbundle
->floodable
= floodable
;
914 xbundle
->protected = protected;
916 if (xbundle
->bond
!= bond
) {
917 bond_unref(xbundle
->bond
);
918 xbundle
->bond
= bond_ref(bond
);
921 if (xbundle
->lacp
!= lacp
) {
922 lacp_unref(xbundle
->lacp
);
923 xbundle
->lacp
= lacp_ref(lacp
);
928 xlate_xport_set(struct xport
*xport
, odp_port_t odp_port
,
929 const struct netdev
*netdev
, const struct cfm
*cfm
,
930 const struct bfd
*bfd
, const struct lldp
*lldp
, int stp_port_no
,
931 const struct rstp_port
* rstp_port
,
932 enum ofputil_port_config config
, enum ofputil_port_state state
,
933 bool is_tunnel
, bool may_enable
)
935 xport
->config
= config
;
936 xport
->state
= state
;
937 xport
->stp_port_no
= stp_port_no
;
938 xport
->is_tunnel
= is_tunnel
;
939 xport
->pt_mode
= netdev_get_pt_mode(netdev
);
940 xport
->may_enable
= may_enable
;
941 xport
->odp_port
= odp_port
;
943 if (xport
->rstp_port
!= rstp_port
) {
944 rstp_port_unref(xport
->rstp_port
);
945 xport
->rstp_port
= rstp_port_ref(rstp_port
);
948 if (xport
->cfm
!= cfm
) {
949 cfm_unref(xport
->cfm
);
950 xport
->cfm
= cfm_ref(cfm
);
953 if (xport
->bfd
!= bfd
) {
954 bfd_unref(xport
->bfd
);
955 xport
->bfd
= bfd_ref(bfd
);
958 if (xport
->lldp
!= lldp
) {
959 lldp_unref(xport
->lldp
);
960 xport
->lldp
= lldp_ref(lldp
);
963 if (xport
->netdev
!= netdev
) {
964 netdev_close(xport
->netdev
);
965 xport
->netdev
= netdev_ref(netdev
);
970 xlate_xbridge_copy(struct xbridge
*xbridge
)
972 struct xbundle
*xbundle
;
974 struct xbridge
*new_xbridge
= xzalloc(sizeof *xbridge
);
975 new_xbridge
->ofproto
= xbridge
->ofproto
;
976 new_xbridge
->name
= xstrdup(xbridge
->name
);
977 xlate_xbridge_init(new_xcfg
, new_xbridge
);
979 xlate_xbridge_set(new_xbridge
,
980 xbridge
->dpif
, xbridge
->ml
, xbridge
->stp
,
981 xbridge
->rstp
, xbridge
->ms
, xbridge
->mbridge
,
982 xbridge
->sflow
, xbridge
->ipfix
, xbridge
->netflow
,
983 xbridge
->forward_bpdu
, xbridge
->has_in_band
,
985 LIST_FOR_EACH (xbundle
, list_node
, &xbridge
->xbundles
) {
986 xlate_xbundle_copy(new_xbridge
, xbundle
);
989 /* Copy xports which are not part of a xbundle */
990 HMAP_FOR_EACH (xport
, ofp_node
, &xbridge
->xports
) {
991 if (!xport
->xbundle
) {
992 xlate_xport_copy(new_xbridge
, NULL
, xport
);
998 xlate_xbundle_copy(struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1000 struct xport
*xport
;
1001 struct xbundle
*new_xbundle
= xzalloc(sizeof *xbundle
);
1002 new_xbundle
->ofbundle
= xbundle
->ofbundle
;
1003 new_xbundle
->xbridge
= xbridge
;
1004 new_xbundle
->name
= xstrdup(xbundle
->name
);
1005 xlate_xbundle_init(new_xcfg
, new_xbundle
);
1007 xlate_xbundle_set(new_xbundle
, xbundle
->vlan_mode
, xbundle
->qinq_ethtype
,
1008 xbundle
->vlan
, xbundle
->trunks
, xbundle
->cvlans
,
1009 xbundle
->use_priority_tags
, xbundle
->bond
, xbundle
->lacp
,
1010 xbundle
->floodable
, xbundle
->protected);
1011 LIST_FOR_EACH (xport
, bundle_node
, &xbundle
->xports
) {
1012 xlate_xport_copy(xbridge
, new_xbundle
, xport
);
1017 xlate_xport_copy(struct xbridge
*xbridge
, struct xbundle
*xbundle
,
1018 struct xport
*xport
)
1020 struct skb_priority_to_dscp
*pdscp
, *new_pdscp
;
1021 struct xport
*new_xport
= xzalloc(sizeof *xport
);
1022 new_xport
->ofport
= xport
->ofport
;
1023 new_xport
->ofp_port
= xport
->ofp_port
;
1024 new_xport
->xbridge
= xbridge
;
1025 new_xport
->uuid
= xport
->uuid
;
1026 xlate_xport_init(new_xcfg
, new_xport
);
1028 xlate_xport_set(new_xport
, xport
->odp_port
, xport
->netdev
, xport
->cfm
,
1029 xport
->bfd
, xport
->lldp
, xport
->stp_port_no
,
1030 xport
->rstp_port
, xport
->config
, xport
->state
,
1031 xport
->is_tunnel
, xport
->may_enable
);
1034 struct xport
*peer
= xport_lookup(new_xcfg
, xport
->peer
->ofport
);
1036 new_xport
->peer
= peer
;
1037 new_xport
->peer
->peer
= new_xport
;
1042 new_xport
->xbundle
= xbundle
;
1043 ovs_list_insert(&new_xport
->xbundle
->xports
, &new_xport
->bundle_node
);
1046 HMAP_FOR_EACH (pdscp
, hmap_node
, &xport
->skb_priorities
) {
1047 new_pdscp
= xmalloc(sizeof *pdscp
);
1048 new_pdscp
->skb_priority
= pdscp
->skb_priority
;
1049 new_pdscp
->dscp
= pdscp
->dscp
;
1050 hmap_insert(&new_xport
->skb_priorities
, &new_pdscp
->hmap_node
,
1051 hash_int(new_pdscp
->skb_priority
, 0));
1055 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
1056 * configuration in xcfgp.
1058 * This needs to be called after editing the xlate configuration.
1060 * Functions that edit the new xlate configuration are
1061 * xlate_<ofproto/bundle/ofport>_set and xlate_<ofproto/bundle/ofport>_remove.
1063 * A sample workflow:
1065 * xlate_txn_start();
1067 * edit_xlate_configuration();
1069 * xlate_txn_commit(); */
1071 xlate_txn_commit(void)
1073 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1075 ovsrcu_set(&xcfgp
, new_xcfg
);
1076 ovsrcu_synchronize();
1077 xlate_xcfg_free(xcfg
);
1081 /* Copies the current xlate configuration in xcfgp to new_xcfg.
1083 * This needs to be called prior to editing the xlate configuration. */
1085 xlate_txn_start(void)
1087 struct xbridge
*xbridge
;
1088 struct xlate_cfg
*xcfg
;
1090 ovs_assert(!new_xcfg
);
1092 new_xcfg
= xmalloc(sizeof *new_xcfg
);
1093 hmap_init(&new_xcfg
->xbridges
);
1094 hmap_init(&new_xcfg
->xbundles
);
1095 hmap_init(&new_xcfg
->xports
);
1096 hmap_init(&new_xcfg
->xports_uuid
);
1098 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1103 HMAP_FOR_EACH (xbridge
, hmap_node
, &xcfg
->xbridges
) {
1104 xlate_xbridge_copy(xbridge
);
1110 xlate_xcfg_free(struct xlate_cfg
*xcfg
)
1112 struct xbridge
*xbridge
, *next_xbridge
;
1118 HMAP_FOR_EACH_SAFE (xbridge
, next_xbridge
, hmap_node
, &xcfg
->xbridges
) {
1119 xlate_xbridge_remove(xcfg
, xbridge
);
1122 hmap_destroy(&xcfg
->xbridges
);
1123 hmap_destroy(&xcfg
->xbundles
);
1124 hmap_destroy(&xcfg
->xports
);
1125 hmap_destroy(&xcfg
->xports_uuid
);
1130 xlate_ofproto_set(struct ofproto_dpif
*ofproto
, const char *name
,
1132 const struct mac_learning
*ml
, struct stp
*stp
,
1133 struct rstp
*rstp
, const struct mcast_snooping
*ms
,
1134 const struct mbridge
*mbridge
,
1135 const struct dpif_sflow
*sflow
,
1136 const struct dpif_ipfix
*ipfix
,
1137 const struct netflow
*netflow
,
1138 bool forward_bpdu
, bool has_in_band
,
1139 const struct dpif_backer_support
*support
)
1141 struct xbridge
*xbridge
;
1143 ovs_assert(new_xcfg
);
1145 xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
1147 xbridge
= xzalloc(sizeof *xbridge
);
1148 xbridge
->ofproto
= ofproto
;
1150 xlate_xbridge_init(new_xcfg
, xbridge
);
1153 free(xbridge
->name
);
1154 xbridge
->name
= xstrdup(name
);
1156 xlate_xbridge_set(xbridge
, dpif
, ml
, stp
, rstp
, ms
, mbridge
, sflow
, ipfix
,
1157 netflow
, forward_bpdu
, has_in_band
, support
);
1161 xlate_xbridge_remove(struct xlate_cfg
*xcfg
, struct xbridge
*xbridge
)
1163 struct xbundle
*xbundle
, *next_xbundle
;
1164 struct xport
*xport
, *next_xport
;
1170 HMAP_FOR_EACH_SAFE (xport
, next_xport
, ofp_node
, &xbridge
->xports
) {
1171 xlate_xport_remove(xcfg
, xport
);
1174 LIST_FOR_EACH_SAFE (xbundle
, next_xbundle
, list_node
, &xbridge
->xbundles
) {
1175 xlate_xbundle_remove(xcfg
, xbundle
);
1178 hmap_remove(&xcfg
->xbridges
, &xbridge
->hmap_node
);
1179 mac_learning_unref(xbridge
->ml
);
1180 mcast_snooping_unref(xbridge
->ms
);
1181 mbridge_unref(xbridge
->mbridge
);
1182 dpif_sflow_unref(xbridge
->sflow
);
1183 dpif_ipfix_unref(xbridge
->ipfix
);
1184 netflow_unref(xbridge
->netflow
);
1185 stp_unref(xbridge
->stp
);
1186 rstp_unref(xbridge
->rstp
);
1187 hmap_destroy(&xbridge
->xports
);
1188 free(xbridge
->name
);
1193 xlate_remove_ofproto(struct ofproto_dpif
*ofproto
)
1195 struct xbridge
*xbridge
;
1197 ovs_assert(new_xcfg
);
1199 xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
1200 xlate_xbridge_remove(new_xcfg
, xbridge
);
1204 xlate_bundle_set(struct ofproto_dpif
*ofproto
, struct ofbundle
*ofbundle
,
1205 const char *name
, enum port_vlan_mode vlan_mode
,
1206 uint16_t qinq_ethtype
, int vlan
,
1207 unsigned long *trunks
, unsigned long *cvlans
,
1208 bool use_priority_tags
,
1209 const struct bond
*bond
, const struct lacp
*lacp
,
1210 bool floodable
, bool protected)
1212 struct xbundle
*xbundle
;
1214 ovs_assert(new_xcfg
);
1216 xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
1218 xbundle
= xzalloc(sizeof *xbundle
);
1219 xbundle
->ofbundle
= ofbundle
;
1220 xbundle
->xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
1222 xlate_xbundle_init(new_xcfg
, xbundle
);
1225 free(xbundle
->name
);
1226 xbundle
->name
= xstrdup(name
);
1228 xlate_xbundle_set(xbundle
, vlan_mode
, qinq_ethtype
, vlan
, trunks
, cvlans
,
1229 use_priority_tags
, bond
, lacp
, floodable
, protected);
1233 xlate_xbundle_remove(struct xlate_cfg
*xcfg
, struct xbundle
*xbundle
)
1235 struct xport
*xport
;
1241 LIST_FOR_EACH_POP (xport
, bundle_node
, &xbundle
->xports
) {
1242 xport
->xbundle
= NULL
;
1245 hmap_remove(&xcfg
->xbundles
, &xbundle
->hmap_node
);
1246 ovs_list_remove(&xbundle
->list_node
);
1247 bond_unref(xbundle
->bond
);
1248 lacp_unref(xbundle
->lacp
);
1249 free(xbundle
->name
);
1254 xlate_bundle_remove(struct ofbundle
*ofbundle
)
1256 struct xbundle
*xbundle
;
1258 ovs_assert(new_xcfg
);
1260 xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
1261 xlate_xbundle_remove(new_xcfg
, xbundle
);
1265 xlate_ofport_set(struct ofproto_dpif
*ofproto
, struct ofbundle
*ofbundle
,
1266 struct ofport_dpif
*ofport
, ofp_port_t ofp_port
,
1267 odp_port_t odp_port
, const struct netdev
*netdev
,
1268 const struct cfm
*cfm
, const struct bfd
*bfd
,
1269 const struct lldp
*lldp
, struct ofport_dpif
*peer
,
1270 int stp_port_no
, const struct rstp_port
*rstp_port
,
1271 const struct ofproto_port_queue
*qdscp_list
, size_t n_qdscp
,
1272 enum ofputil_port_config config
,
1273 enum ofputil_port_state state
, bool is_tunnel
,
1277 struct xport
*xport
;
1279 ovs_assert(new_xcfg
);
1281 xport
= xport_lookup(new_xcfg
, ofport
);
1283 xport
= xzalloc(sizeof *xport
);
1284 xport
->ofport
= ofport
;
1285 xport
->xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
1286 xport
->ofp_port
= ofp_port
;
1287 uuid_generate(&xport
->uuid
);
1289 xlate_xport_init(new_xcfg
, xport
);
1292 ovs_assert(xport
->ofp_port
== ofp_port
);
1294 xlate_xport_set(xport
, odp_port
, netdev
, cfm
, bfd
, lldp
,
1295 stp_port_no
, rstp_port
, config
, state
, is_tunnel
,
1299 xport
->peer
->peer
= NULL
;
1301 xport
->peer
= xport_lookup(new_xcfg
, peer
);
1303 xport
->peer
->peer
= xport
;
1306 if (xport
->xbundle
) {
1307 ovs_list_remove(&xport
->bundle_node
);
1309 xport
->xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
1310 if (xport
->xbundle
) {
1311 ovs_list_insert(&xport
->xbundle
->xports
, &xport
->bundle_node
);
1314 clear_skb_priorities(xport
);
1315 for (i
= 0; i
< n_qdscp
; i
++) {
1316 struct skb_priority_to_dscp
*pdscp
;
1317 uint32_t skb_priority
;
1319 if (dpif_queue_to_priority(xport
->xbridge
->dpif
, qdscp_list
[i
].queue
,
1324 pdscp
= xmalloc(sizeof *pdscp
);
1325 pdscp
->skb_priority
= skb_priority
;
1326 pdscp
->dscp
= (qdscp_list
[i
].dscp
<< 2) & IP_DSCP_MASK
;
1327 hmap_insert(&xport
->skb_priorities
, &pdscp
->hmap_node
,
1328 hash_int(pdscp
->skb_priority
, 0));
1333 xlate_xport_remove(struct xlate_cfg
*xcfg
, struct xport
*xport
)
1340 xport
->peer
->peer
= NULL
;
1344 if (xport
->xbundle
) {
1345 ovs_list_remove(&xport
->bundle_node
);
1348 clear_skb_priorities(xport
);
1349 hmap_destroy(&xport
->skb_priorities
);
1351 hmap_remove(&xcfg
->xports
, &xport
->hmap_node
);
1352 hmap_remove(&xcfg
->xports_uuid
, &xport
->uuid_node
);
1353 hmap_remove(&xport
->xbridge
->xports
, &xport
->ofp_node
);
1355 netdev_close(xport
->netdev
);
1356 rstp_port_unref(xport
->rstp_port
);
1357 cfm_unref(xport
->cfm
);
1358 bfd_unref(xport
->bfd
);
1359 lldp_unref(xport
->lldp
);
1364 xlate_ofport_remove(struct ofport_dpif
*ofport
)
1366 struct xport
*xport
;
1368 ovs_assert(new_xcfg
);
1370 xport
= xport_lookup(new_xcfg
, ofport
);
1371 xlate_xport_remove(new_xcfg
, xport
);
1374 static struct ofproto_dpif
*
1375 xlate_lookup_ofproto_(const struct dpif_backer
*backer
, const struct flow
*flow
,
1376 ofp_port_t
*ofp_in_port
, const struct xport
**xportp
)
1378 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1379 const struct xport
*xport
;
1381 /* If packet is recirculated, xport can be retrieved from frozen state. */
1382 if (flow
->recirc_id
) {
1383 const struct recirc_id_node
*recirc_id_node
;
1385 recirc_id_node
= recirc_id_node_find(flow
->recirc_id
);
1387 if (OVS_UNLIKELY(!recirc_id_node
)) {
1391 /* If recirculation was initiated due to bond (in_port = OFPP_NONE)
1392 * then frozen state is static and xport_uuid is not defined, so xport
1393 * cannot be restored from frozen state. */
1394 if (recirc_id_node
->state
.metadata
.in_port
!= OFPP_NONE
) {
1395 struct uuid xport_uuid
= recirc_id_node
->state
.xport_uuid
;
1396 xport
= xport_lookup_by_uuid(xcfg
, &xport_uuid
);
1397 if (xport
&& xport
->xbridge
&& xport
->xbridge
->ofproto
) {
1403 xport
= xport_lookup(xcfg
, tnl_port_should_receive(flow
)
1404 ? tnl_port_receive(flow
)
1405 : odp_port_to_ofport(backer
, flow
->in_port
.odp_port
));
1406 if (OVS_UNLIKELY(!xport
)) {
1413 *ofp_in_port
= xport
->ofp_port
;
1415 return xport
->xbridge
->ofproto
;
1418 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1419 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1420 struct ofproto_dpif
*
1421 xlate_lookup_ofproto(const struct dpif_backer
*backer
, const struct flow
*flow
,
1422 ofp_port_t
*ofp_in_port
)
1424 const struct xport
*xport
;
1426 return xlate_lookup_ofproto_(backer
, flow
, ofp_in_port
, &xport
);
1429 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1430 * optionally populates 'ofprotop' with the ofproto_dpif, 'ofp_in_port' with the
1431 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1432 * handles for those protocols if they're enabled. Caller may use the returned
1433 * pointers until quiescing, for longer term use additional references must
1436 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1439 xlate_lookup(const struct dpif_backer
*backer
, const struct flow
*flow
,
1440 struct ofproto_dpif
**ofprotop
, struct dpif_ipfix
**ipfix
,
1441 struct dpif_sflow
**sflow
, struct netflow
**netflow
,
1442 ofp_port_t
*ofp_in_port
)
1444 struct ofproto_dpif
*ofproto
;
1445 const struct xport
*xport
;
1447 ofproto
= xlate_lookup_ofproto_(backer
, flow
, ofp_in_port
, &xport
);
1454 *ofprotop
= ofproto
;
1458 *ipfix
= xport
? xport
->xbridge
->ipfix
: NULL
;
1462 *sflow
= xport
? xport
->xbridge
->sflow
: NULL
;
1466 *netflow
= xport
? xport
->xbridge
->netflow
: NULL
;
1472 static struct xbridge
*
1473 xbridge_lookup(struct xlate_cfg
*xcfg
, const struct ofproto_dpif
*ofproto
)
1475 struct hmap
*xbridges
;
1476 struct xbridge
*xbridge
;
1478 if (!ofproto
|| !xcfg
) {
1482 xbridges
= &xcfg
->xbridges
;
1484 HMAP_FOR_EACH_IN_BUCKET (xbridge
, hmap_node
, hash_pointer(ofproto
, 0),
1486 if (xbridge
->ofproto
== ofproto
) {
1493 static struct xbridge
*
1494 xbridge_lookup_by_uuid(struct xlate_cfg
*xcfg
, const struct uuid
*uuid
)
1496 struct xbridge
*xbridge
;
1498 HMAP_FOR_EACH (xbridge
, hmap_node
, &xcfg
->xbridges
) {
1499 if (uuid_equals(&xbridge
->ofproto
->uuid
, uuid
)) {
1506 static struct xbundle
*
1507 xbundle_lookup(struct xlate_cfg
*xcfg
, const struct ofbundle
*ofbundle
)
1509 struct hmap
*xbundles
;
1510 struct xbundle
*xbundle
;
1512 if (!ofbundle
|| !xcfg
) {
1516 xbundles
= &xcfg
->xbundles
;
1518 HMAP_FOR_EACH_IN_BUCKET (xbundle
, hmap_node
, hash_pointer(ofbundle
, 0),
1520 if (xbundle
->ofbundle
== ofbundle
) {
1527 static struct xport
*
1528 xport_lookup(struct xlate_cfg
*xcfg
, const struct ofport_dpif
*ofport
)
1530 struct hmap
*xports
;
1531 struct xport
*xport
;
1533 if (!ofport
|| !xcfg
) {
1537 xports
= &xcfg
->xports
;
1539 HMAP_FOR_EACH_IN_BUCKET (xport
, hmap_node
, hash_pointer(ofport
, 0),
1541 if (xport
->ofport
== ofport
) {
1548 static struct xport
*
1549 xport_lookup_by_uuid(struct xlate_cfg
*xcfg
, const struct uuid
*uuid
)
1551 struct hmap
*xports
;
1552 struct xport
*xport
;
1554 if (uuid_is_zero(uuid
) || !xcfg
) {
1558 xports
= &xcfg
->xports_uuid
;
1560 HMAP_FOR_EACH_IN_BUCKET (xport
, uuid_node
, uuid_hash(uuid
), xports
) {
1561 if (uuid_equals(&xport
->uuid
, uuid
)) {
1568 static struct stp_port
*
1569 xport_get_stp_port(const struct xport
*xport
)
1571 return xport
->xbridge
->stp
&& xport
->stp_port_no
!= -1
1572 ? stp_get_port(xport
->xbridge
->stp
, xport
->stp_port_no
)
1577 xport_stp_learn_state(const struct xport
*xport
)
1579 struct stp_port
*sp
= xport_get_stp_port(xport
);
1581 ? stp_learn_in_state(stp_port_get_state(sp
))
1586 xport_stp_forward_state(const struct xport
*xport
)
1588 struct stp_port
*sp
= xport_get_stp_port(xport
);
1590 ? stp_forward_in_state(stp_port_get_state(sp
))
1595 xport_stp_should_forward_bpdu(const struct xport
*xport
)
1597 struct stp_port
*sp
= xport_get_stp_port(xport
);
1598 return stp_should_forward_bpdu(sp
? stp_port_get_state(sp
) : STP_DISABLED
);
1601 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1602 * were used to make the determination.*/
1604 stp_should_process_flow(const struct flow
*flow
, struct flow_wildcards
*wc
)
1606 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1607 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
1608 return is_stp(flow
);
1612 stp_process_packet(const struct xport
*xport
, const struct dp_packet
*packet
)
1614 struct stp_port
*sp
= xport_get_stp_port(xport
);
1615 struct dp_packet payload
= *packet
;
1616 struct eth_header
*eth
= dp_packet_data(&payload
);
1618 /* Sink packets on ports that have STP disabled when the bridge has
1620 if (!sp
|| stp_port_get_state(sp
) == STP_DISABLED
) {
1624 /* Trim off padding on payload. */
1625 if (dp_packet_size(&payload
) > ntohs(eth
->eth_type
) + ETH_HEADER_LEN
) {
1626 dp_packet_set_size(&payload
, ntohs(eth
->eth_type
) + ETH_HEADER_LEN
);
1629 if (dp_packet_try_pull(&payload
, ETH_HEADER_LEN
+ LLC_HEADER_LEN
)) {
1630 stp_received_bpdu(sp
, dp_packet_data(&payload
), dp_packet_size(&payload
));
1634 static enum rstp_state
1635 xport_get_rstp_port_state(const struct xport
*xport
)
1637 return xport
->rstp_port
1638 ? rstp_port_get_state(xport
->rstp_port
)
1643 xport_rstp_learn_state(const struct xport
*xport
)
1645 return xport
->xbridge
->rstp
&& xport
->rstp_port
1646 ? rstp_learn_in_state(xport_get_rstp_port_state(xport
))
1651 xport_rstp_forward_state(const struct xport
*xport
)
1653 return xport
->xbridge
->rstp
&& xport
->rstp_port
1654 ? rstp_forward_in_state(xport_get_rstp_port_state(xport
))
1659 xport_rstp_should_manage_bpdu(const struct xport
*xport
)
1661 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport
));
1665 rstp_process_packet(const struct xport
*xport
, const struct dp_packet
*packet
)
1667 struct dp_packet payload
= *packet
;
1668 struct eth_header
*eth
= dp_packet_data(&payload
);
1670 /* Sink packets on ports that have no RSTP. */
1671 if (!xport
->rstp_port
) {
1675 /* Trim off padding on payload. */
1676 if (dp_packet_size(&payload
) > ntohs(eth
->eth_type
) + ETH_HEADER_LEN
) {
1677 dp_packet_set_size(&payload
, ntohs(eth
->eth_type
) + ETH_HEADER_LEN
);
1680 if (dp_packet_try_pull(&payload
, ETH_HEADER_LEN
+ LLC_HEADER_LEN
)) {
1681 rstp_port_received_bpdu(xport
->rstp_port
, dp_packet_data(&payload
),
1682 dp_packet_size(&payload
));
1686 static struct xport
*
1687 get_ofp_port(const struct xbridge
*xbridge
, ofp_port_t ofp_port
)
1689 struct xport
*xport
;
1691 HMAP_FOR_EACH_IN_BUCKET (xport
, ofp_node
, hash_ofp_port(ofp_port
),
1693 if (xport
->ofp_port
== ofp_port
) {
1701 ofp_port_to_odp_port(const struct xbridge
*xbridge
, ofp_port_t ofp_port
)
1703 const struct xport
*xport
= get_ofp_port(xbridge
, ofp_port
);
1704 return xport
? xport
->odp_port
: ODPP_NONE
;
1708 odp_port_is_alive(const struct xlate_ctx
*ctx
, ofp_port_t ofp_port
)
1710 struct xport
*xport
= get_ofp_port(ctx
->xbridge
, ofp_port
);
1711 return xport
&& xport
->may_enable
;
1714 static struct ofputil_bucket
*
1715 group_first_live_bucket(const struct xlate_ctx
*, const struct group_dpif
*,
1719 group_is_alive(const struct xlate_ctx
*ctx
, uint32_t group_id
, int depth
)
1721 struct group_dpif
*group
;
1723 group
= group_dpif_lookup(ctx
->xbridge
->ofproto
, group_id
,
1724 ctx
->xin
->tables_version
, false);
1726 return group_first_live_bucket(ctx
, group
, depth
) != NULL
;
1732 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1735 bucket_is_alive(const struct xlate_ctx
*ctx
,
1736 struct ofputil_bucket
*bucket
, int depth
)
1738 if (depth
>= MAX_LIVENESS_RECURSION
) {
1739 xlate_report_error(ctx
, "bucket chaining exceeded %d links",
1740 MAX_LIVENESS_RECURSION
);
1744 return (!ofputil_bucket_has_liveness(bucket
)
1745 || (bucket
->watch_port
!= OFPP_ANY
1746 && odp_port_is_alive(ctx
, bucket
->watch_port
))
1747 || (bucket
->watch_group
!= OFPG_ANY
1748 && group_is_alive(ctx
, bucket
->watch_group
, depth
+ 1)));
1751 static struct ofputil_bucket
*
1752 group_first_live_bucket(const struct xlate_ctx
*ctx
,
1753 const struct group_dpif
*group
, int depth
)
1755 struct ofputil_bucket
*bucket
;
1756 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
1757 if (bucket_is_alive(ctx
, bucket
, depth
)) {
1765 static struct ofputil_bucket
*
1766 group_best_live_bucket(const struct xlate_ctx
*ctx
,
1767 const struct group_dpif
*group
,
1770 struct ofputil_bucket
*best_bucket
= NULL
;
1771 uint32_t best_score
= 0;
1773 struct ofputil_bucket
*bucket
;
1774 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
1775 if (bucket_is_alive(ctx
, bucket
, 0)) {
1777 (hash_int(bucket
->bucket_id
, basis
) & 0xffff) * bucket
->weight
;
1778 if (score
>= best_score
) {
1779 best_bucket
= bucket
;
1789 xbundle_trunks_vlan(const struct xbundle
*bundle
, uint16_t vlan
)
1791 return (bundle
->vlan_mode
!= PORT_VLAN_ACCESS
1792 && (!bundle
->trunks
|| bitmap_is_set(bundle
->trunks
, vlan
)));
1796 xbundle_allows_cvlan(const struct xbundle
*bundle
, uint16_t vlan
)
1798 return (!bundle
->cvlans
|| bitmap_is_set(bundle
->cvlans
, vlan
));
1802 xbundle_includes_vlan(const struct xbundle
*xbundle
, const struct xvlan
*xvlan
)
1804 switch (xbundle
->vlan_mode
) {
1805 case PORT_VLAN_ACCESS
:
1806 return xvlan
->v
[0].vid
== xbundle
->vlan
&& xvlan
->v
[1].vid
== 0;
1808 case PORT_VLAN_TRUNK
:
1809 case PORT_VLAN_NATIVE_UNTAGGED
:
1810 case PORT_VLAN_NATIVE_TAGGED
:
1811 return xbundle_trunks_vlan(xbundle
, xvlan
->v
[0].vid
);
1813 case PORT_VLAN_DOT1Q_TUNNEL
:
1814 return xvlan
->v
[0].vid
== xbundle
->vlan
&&
1815 xbundle_allows_cvlan(xbundle
, xvlan
->v
[1].vid
);
1822 static mirror_mask_t
1823 xbundle_mirror_out(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1825 return xbundle
!= &ofpp_none_bundle
1826 ? mirror_bundle_out(xbridge
->mbridge
, xbundle
->ofbundle
)
1830 static mirror_mask_t
1831 xbundle_mirror_src(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1833 return xbundle
!= &ofpp_none_bundle
1834 ? mirror_bundle_src(xbridge
->mbridge
, xbundle
->ofbundle
)
1838 static mirror_mask_t
1839 xbundle_mirror_dst(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1841 return xbundle
!= &ofpp_none_bundle
1842 ? mirror_bundle_dst(xbridge
->mbridge
, xbundle
->ofbundle
)
1846 static struct xbundle
*
1847 lookup_input_bundle__(const struct xbridge
*xbridge
,
1848 ofp_port_t in_port
, struct xport
**in_xportp
)
1850 struct xport
*xport
;
1852 /* Find the port and bundle for the received packet. */
1853 xport
= get_ofp_port(xbridge
, in_port
);
1857 if (xport
&& xport
->xbundle
) {
1858 return xport
->xbundle
;
1861 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1862 * which a controller may use as the ingress port for traffic that
1863 * it is sourcing. */
1864 if (in_port
== OFPP_CONTROLLER
|| in_port
== OFPP_NONE
) {
1865 return &ofpp_none_bundle
;
1870 static struct xbundle
*
1871 lookup_input_bundle(const struct xlate_ctx
*ctx
,
1872 ofp_port_t in_port
, struct xport
**in_xportp
)
1874 struct xbundle
*xbundle
= lookup_input_bundle__(ctx
->xbridge
,
1875 in_port
, in_xportp
);
1877 /* Odd. A few possible reasons here:
1879 * - We deleted a port but there are still a few packets queued up
1882 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1883 * we don't know about.
1885 * - The ofproto client didn't configure the port as part of a bundle.
1886 * This is particularly likely to happen if a packet was received on
1887 * the port after it was created, but before the client had a chance
1888 * to configure its bundle.
1890 xlate_report_error(ctx
, "received packet on unknown port %"PRIu32
,
1896 /* Mirrors the packet represented by 'ctx' to appropriate mirror destinations,
1897 * given the packet is ingressing or egressing on 'xbundle', which has ingress
1898 * or egress (as appropriate) mirrors 'mirrors'. */
1900 mirror_packet(struct xlate_ctx
*ctx
, struct xbundle
*xbundle
,
1901 mirror_mask_t mirrors
)
1903 struct xvlan in_xvlan
;
1906 /* Figure out what VLAN the packet is in (because mirrors can select
1907 * packets on basis of VLAN). */
1908 xvlan_extract(&ctx
->xin
->flow
, &in_xvlan
);
1909 if (!input_vid_is_valid(ctx
, in_xvlan
.v
[0].vid
, xbundle
)) {
1912 xvlan_input_translate(xbundle
, &in_xvlan
, &xvlan
);
1914 const struct xbridge
*xbridge
= ctx
->xbridge
;
1916 /* Don't mirror to destinations that we've already mirrored to. */
1917 mirrors
&= ~ctx
->mirrors
;
1922 if (ctx
->xin
->resubmit_stats
) {
1923 mirror_update_stats(xbridge
->mbridge
, mirrors
,
1924 ctx
->xin
->resubmit_stats
->n_packets
,
1925 ctx
->xin
->resubmit_stats
->n_bytes
);
1927 if (ctx
->xin
->xcache
) {
1928 struct xc_entry
*entry
;
1930 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_MIRROR
);
1931 entry
->mirror
.mbridge
= mbridge_ref(xbridge
->mbridge
);
1932 entry
->mirror
.mirrors
= mirrors
;
1935 /* 'mirrors' is a bit-mask of candidates for mirroring. Iterate as long as
1936 * some candidates remain. */
1938 const unsigned long *vlans
;
1939 mirror_mask_t dup_mirrors
;
1940 struct ofbundle
*out
;
1944 /* Get the details of the mirror represented by the rightmost 1-bit. */
1945 bool has_mirror
= mirror_get(xbridge
->mbridge
, raw_ctz(mirrors
),
1946 &vlans
, &dup_mirrors
,
1947 &out
, &snaplen
, &out_vlan
);
1948 ovs_assert(has_mirror
);
1951 /* If this mirror selects on the basis of VLAN, and it does not select
1952 * 'vlan', then discard this mirror and go on to the next one. */
1954 ctx
->wc
->masks
.vlans
[0].tci
|= htons(VLAN_CFI
| VLAN_VID_MASK
);
1956 if (vlans
&& !bitmap_is_set(vlans
, xvlan
.v
[0].vid
)) {
1957 mirrors
= zero_rightmost_1bit(mirrors
);
1961 /* Record the mirror, and the mirrors that output to the same
1962 * destination, so that we don't mirror to them again. This must be
1963 * done now to ensure that output_normal(), below, doesn't recursively
1964 * output to the same mirrors. */
1965 ctx
->mirrors
|= dup_mirrors
;
1966 ctx
->mirror_snaplen
= snaplen
;
1968 /* Send the packet to the mirror. */
1970 struct xbundle
*out_xbundle
= xbundle_lookup(ctx
->xcfg
, out
);
1972 output_normal(ctx
, out_xbundle
, &xvlan
);
1974 } else if (xvlan
.v
[0].vid
!= out_vlan
1975 && !eth_addr_is_reserved(ctx
->xin
->flow
.dl_dst
)) {
1977 uint16_t old_vid
= xvlan
.v
[0].vid
;
1979 xvlan
.v
[0].vid
= out_vlan
;
1980 LIST_FOR_EACH (xb
, list_node
, &xbridge
->xbundles
) {
1981 if (xbundle_includes_vlan(xb
, &xvlan
)
1982 && !xbundle_mirror_out(xbridge
, xb
)) {
1983 output_normal(ctx
, xb
, &xvlan
);
1986 xvlan
.v
[0].vid
= old_vid
;
1989 /* output_normal() could have recursively output (to different
1990 * mirrors), so make sure that we don't send duplicates. */
1991 mirrors
&= ~ctx
->mirrors
;
1992 ctx
->mirror_snaplen
= 0;
1997 mirror_ingress_packet(struct xlate_ctx
*ctx
)
1999 if (mbridge_has_mirrors(ctx
->xbridge
->mbridge
)) {
2000 struct xbundle
*xbundle
= lookup_input_bundle(
2001 ctx
, ctx
->xin
->flow
.in_port
.ofp_port
, NULL
);
2003 mirror_packet(ctx
, xbundle
,
2004 xbundle_mirror_src(ctx
->xbridge
, xbundle
));
2009 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
2010 * If so, returns true. Otherwise, returns false.
2012 * 'vid' should be the VID obtained from the 802.1Q header that was received as
2013 * part of a packet (specify 0 if there was no 802.1Q header), in the range
2016 input_vid_is_valid(const struct xlate_ctx
*ctx
,
2017 uint16_t vid
, struct xbundle
*in_xbundle
)
2019 /* Allow any VID on the OFPP_NONE port. */
2020 if (in_xbundle
== &ofpp_none_bundle
) {
2024 switch (in_xbundle
->vlan_mode
) {
2025 case PORT_VLAN_ACCESS
:
2027 xlate_report_error(ctx
, "dropping VLAN %"PRIu16
" tagged "
2028 "packet received on port %s configured as VLAN "
2029 "%d access port", vid
, in_xbundle
->name
,
2035 case PORT_VLAN_NATIVE_UNTAGGED
:
2036 case PORT_VLAN_NATIVE_TAGGED
:
2038 /* Port must always carry its native VLAN. */
2042 case PORT_VLAN_TRUNK
:
2043 if (!xbundle_trunks_vlan(in_xbundle
, vid
)) {
2044 xlate_report_error(ctx
, "dropping VLAN %"PRIu16
" packet "
2045 "received on port %s not configured for "
2046 "trunking VLAN %"PRIu16
,
2047 vid
, in_xbundle
->name
, vid
);
2052 case PORT_VLAN_DOT1Q_TUNNEL
:
2053 if (!xbundle_allows_cvlan(in_xbundle
, vid
)) {
2054 xlate_report_error(ctx
, "dropping VLAN %"PRIu16
" packet received "
2055 "on dot1q-tunnel port %s that excludes this "
2056 "VLAN", vid
, in_xbundle
->name
);
2068 xvlan_copy(struct xvlan
*dst
, const struct xvlan
*src
)
2074 xvlan_pop(struct xvlan
*src
)
2076 memmove(&src
->v
[0], &src
->v
[1], sizeof(src
->v
) - sizeof(src
->v
[0]));
2077 memset(&src
->v
[FLOW_MAX_VLAN_HEADERS
- 1], 0,
2078 sizeof(src
->v
[FLOW_MAX_VLAN_HEADERS
- 1]));
2082 xvlan_push_uninit(struct xvlan
*src
)
2084 memmove(&src
->v
[1], &src
->v
[0], sizeof(src
->v
) - sizeof(src
->v
[0]));
2085 memset(&src
->v
[0], 0, sizeof(src
->v
[0]));
2088 /* Extract VLAN information (headers) from flow */
2090 xvlan_extract(const struct flow
*flow
, struct xvlan
*xvlan
)
2093 memset(xvlan
, 0, sizeof(*xvlan
));
2094 for (i
= 0; i
< FLOW_MAX_VLAN_HEADERS
; i
++) {
2095 if (!eth_type_vlan(flow
->vlans
[i
].tpid
) ||
2096 !(flow
->vlans
[i
].tci
& htons(VLAN_CFI
))) {
2099 xvlan
->v
[i
].tpid
= ntohs(flow
->vlans
[i
].tpid
);
2100 xvlan
->v
[i
].vid
= vlan_tci_to_vid(flow
->vlans
[i
].tci
);
2101 xvlan
->v
[i
].pcp
= ntohs(flow
->vlans
[i
].tci
) & VLAN_PCP_MASK
;
2105 /* Put VLAN information (headers) to flow */
2107 xvlan_put(struct flow
*flow
, const struct xvlan
*xvlan
)
2111 for (i
= 0; i
< FLOW_MAX_VLAN_HEADERS
; i
++) {
2112 tci
= htons(xvlan
->v
[i
].vid
| (xvlan
->v
[i
].pcp
& VLAN_PCP_MASK
));
2114 tci
|= htons(VLAN_CFI
);
2115 flow
->vlans
[i
].tpid
= xvlan
->v
[i
].tpid
?
2116 htons(xvlan
->v
[i
].tpid
) :
2117 htons(ETH_TYPE_VLAN_8021Q
);
2119 flow
->vlans
[i
].tci
= tci
;
2123 /* Given 'in_xvlan', extracted from the input 802.1Q headers received as part
2124 * of a packet, and 'in_xbundle', the bundle on which the packet was received,
2125 * returns the VLANs of the packet during bridge internal processing. */
2127 xvlan_input_translate(const struct xbundle
*in_xbundle
,
2128 const struct xvlan
*in_xvlan
, struct xvlan
*xvlan
)
2131 switch (in_xbundle
->vlan_mode
) {
2132 case PORT_VLAN_ACCESS
:
2133 memset(xvlan
, 0, sizeof(*xvlan
));
2134 xvlan
->v
[0].tpid
= in_xvlan
->v
[0].tpid
? in_xvlan
->v
[0].tpid
:
2135 ETH_TYPE_VLAN_8021Q
;
2136 xvlan
->v
[0].vid
= in_xbundle
->vlan
;
2137 xvlan
->v
[0].pcp
= in_xvlan
->v
[0].pcp
;
2140 case PORT_VLAN_TRUNK
:
2141 xvlan_copy(xvlan
, in_xvlan
);
2144 case PORT_VLAN_NATIVE_UNTAGGED
:
2145 case PORT_VLAN_NATIVE_TAGGED
:
2146 xvlan_copy(xvlan
, in_xvlan
);
2147 if (!in_xvlan
->v
[0].vid
) {
2148 xvlan
->v
[0].tpid
= in_xvlan
->v
[0].tpid
? in_xvlan
->v
[0].tpid
:
2149 ETH_TYPE_VLAN_8021Q
;
2150 xvlan
->v
[0].vid
= in_xbundle
->vlan
;
2151 xvlan
->v
[0].pcp
= in_xvlan
->v
[0].pcp
;
2155 case PORT_VLAN_DOT1Q_TUNNEL
:
2156 xvlan_copy(xvlan
, in_xvlan
);
2157 xvlan_push_uninit(xvlan
);
2158 xvlan
->v
[0].tpid
= in_xbundle
->qinq_ethtype
;
2159 xvlan
->v
[0].vid
= in_xbundle
->vlan
;
2160 xvlan
->v
[0].pcp
= 0;
2168 /* Given 'xvlan', the VLANs of a packet during internal processing, and
2169 * 'out_xbundle', a bundle on which the packet is to be output, returns the
2170 * VLANs that should be included in output packet. */
2172 xvlan_output_translate(const struct xbundle
*out_xbundle
,
2173 const struct xvlan
*xvlan
, struct xvlan
*out_xvlan
)
2175 switch (out_xbundle
->vlan_mode
) {
2176 case PORT_VLAN_ACCESS
:
2177 memset(out_xvlan
, 0, sizeof(*out_xvlan
));
2180 case PORT_VLAN_TRUNK
:
2181 case PORT_VLAN_NATIVE_TAGGED
:
2182 xvlan_copy(out_xvlan
, xvlan
);
2185 case PORT_VLAN_NATIVE_UNTAGGED
:
2186 xvlan_copy(out_xvlan
, xvlan
);
2187 if (xvlan
->v
[0].vid
== out_xbundle
->vlan
) {
2188 xvlan_pop(out_xvlan
);
2192 case PORT_VLAN_DOT1Q_TUNNEL
:
2193 xvlan_copy(out_xvlan
, xvlan
);
2194 xvlan_pop(out_xvlan
);
2202 /* If output xbundle is dot1q-tunnel, set mask bits of cvlan */
2204 check_and_set_cvlan_mask(struct flow_wildcards
*wc
,
2205 const struct xbundle
*xbundle
)
2207 if (xbundle
->vlan_mode
== PORT_VLAN_DOT1Q_TUNNEL
&& xbundle
->cvlans
) {
2208 wc
->masks
.vlans
[1].tci
= htons(0xffff);
2213 output_normal(struct xlate_ctx
*ctx
, const struct xbundle
*out_xbundle
,
2214 const struct xvlan
*xvlan
)
2217 union flow_vlan_hdr old_vlans
[FLOW_MAX_VLAN_HEADERS
];
2218 struct xport
*xport
;
2219 struct xlate_bond_recirc xr
;
2220 bool use_recirc
= false;
2221 struct xvlan out_xvlan
;
2223 check_and_set_cvlan_mask(ctx
->wc
, out_xbundle
);
2225 xvlan_output_translate(out_xbundle
, xvlan
, &out_xvlan
);
2226 if (out_xbundle
->use_priority_tags
) {
2227 out_xvlan
.v
[0].pcp
= ntohs(ctx
->xin
->flow
.vlans
[0].tci
) &
2230 vid
= out_xvlan
.v
[0].vid
;
2231 if (ovs_list_is_empty(&out_xbundle
->xports
)) {
2232 /* Partially configured bundle with no slaves. Drop the packet. */
2234 } else if (!out_xbundle
->bond
) {
2235 xport
= CONTAINER_OF(ovs_list_front(&out_xbundle
->xports
), struct xport
,
2238 struct flow_wildcards
*wc
= ctx
->wc
;
2239 struct ofport_dpif
*ofport
;
2241 if (ctx
->xbridge
->support
.odp
.recirc
) {
2242 /* In case recirculation is not actually in use, 'xr.recirc_id'
2243 * will be set to '0', since a valid 'recirc_id' can
2245 bond_update_post_recirc_rules(out_xbundle
->bond
,
2249 /* Use recirculation instead of output. */
2251 xr
.hash_alg
= OVS_HASH_ALG_L4
;
2252 /* Recirculation does not require unmasking hash fields. */
2257 ofport
= bond_choose_output_slave(out_xbundle
->bond
,
2258 &ctx
->xin
->flow
, wc
, vid
);
2259 xport
= xport_lookup(ctx
->xcfg
, ofport
);
2262 /* No slaves enabled, so drop packet. */
2266 /* If use_recirc is set, the main thread will handle stats
2267 * accounting for this bond. */
2269 if (ctx
->xin
->resubmit_stats
) {
2270 bond_account(out_xbundle
->bond
, &ctx
->xin
->flow
, vid
,
2271 ctx
->xin
->resubmit_stats
->n_bytes
);
2273 if (ctx
->xin
->xcache
) {
2274 struct xc_entry
*entry
;
2277 flow
= &ctx
->xin
->flow
;
2278 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_BOND
);
2279 entry
->bond
.bond
= bond_ref(out_xbundle
->bond
);
2280 entry
->bond
.flow
= xmemdup(flow
, sizeof *flow
);
2281 entry
->bond
.vid
= vid
;
2286 memcpy(&old_vlans
, &ctx
->xin
->flow
.vlans
, sizeof(old_vlans
));
2287 xvlan_put(&ctx
->xin
->flow
, &out_xvlan
);
2289 compose_output_action(ctx
, xport
->ofp_port
, use_recirc
? &xr
: NULL
,
2291 memcpy(&ctx
->xin
->flow
.vlans
, &old_vlans
, sizeof(old_vlans
));
2294 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
2295 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
2296 * indicate this; newer upstream kernels use gratuitous ARP requests. */
2298 is_gratuitous_arp(const struct flow
*flow
, struct flow_wildcards
*wc
)
2300 if (flow
->dl_type
!= htons(ETH_TYPE_ARP
)) {
2304 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
2305 if (!eth_addr_is_broadcast(flow
->dl_dst
)) {
2309 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
2310 if (flow
->nw_proto
== ARP_OP_REPLY
) {
2312 } else if (flow
->nw_proto
== ARP_OP_REQUEST
) {
2313 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
2314 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
2316 return flow
->nw_src
== flow
->nw_dst
;
2322 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
2323 * dropped. Returns true if they may be forwarded, false if they should be
2326 * 'in_port' must be the xport that corresponds to flow->in_port.
2327 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
2329 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
2330 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
2331 * checked by input_vid_is_valid().
2333 * May also add tags to '*tags', although the current implementation only does
2334 * so in one special case.
2337 is_admissible(struct xlate_ctx
*ctx
, struct xport
*in_port
,
2340 struct xbundle
*in_xbundle
= in_port
->xbundle
;
2341 const struct xbridge
*xbridge
= ctx
->xbridge
;
2342 struct flow
*flow
= &ctx
->xin
->flow
;
2344 /* Drop frames for reserved multicast addresses
2345 * only if forward_bpdu option is absent. */
2346 if (!xbridge
->forward_bpdu
&& eth_addr_is_reserved(flow
->dl_dst
)) {
2347 xlate_report(ctx
, OFT_DETAIL
,
2348 "packet has reserved destination MAC, dropping");
2352 if (in_xbundle
->bond
) {
2353 struct mac_entry
*mac
;
2355 switch (bond_check_admissibility(in_xbundle
->bond
, in_port
->ofport
,
2361 xlate_report(ctx
, OFT_DETAIL
,
2362 "bonding refused admissibility, dropping");
2365 case BV_DROP_IF_MOVED
:
2366 ovs_rwlock_rdlock(&xbridge
->ml
->rwlock
);
2367 mac
= mac_learning_lookup(xbridge
->ml
, flow
->dl_src
, vlan
);
2369 && mac_entry_get_port(xbridge
->ml
, mac
) != in_xbundle
->ofbundle
2370 && (!is_gratuitous_arp(flow
, ctx
->wc
)
2371 || mac_entry_is_grat_arp_locked(mac
))) {
2372 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
2373 xlate_report(ctx
, OFT_DETAIL
,
2374 "SLB bond thinks this packet looped back, "
2378 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
2387 update_learning_table__(const struct xbridge
*xbridge
,
2388 struct xbundle
*in_xbundle
, struct eth_addr dl_src
,
2389 int vlan
, bool is_grat_arp
)
2391 return (in_xbundle
== &ofpp_none_bundle
2392 || !mac_learning_update(xbridge
->ml
, dl_src
, vlan
,
2394 in_xbundle
->bond
!= NULL
,
2395 in_xbundle
->ofbundle
));
2399 update_learning_table(const struct xlate_ctx
*ctx
,
2400 struct xbundle
*in_xbundle
, struct eth_addr dl_src
,
2401 int vlan
, bool is_grat_arp
)
2403 if (!update_learning_table__(ctx
->xbridge
, in_xbundle
, dl_src
, vlan
,
2405 xlate_report_debug(ctx
, OFT_DETAIL
, "learned that "ETH_ADDR_FMT
" is "
2406 "on port %s in VLAN %d",
2407 ETH_ADDR_ARGS(dl_src
), in_xbundle
->name
, vlan
);
2411 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2412 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
2414 update_mcast_snooping_table4__(const struct xlate_ctx
*ctx
,
2415 const struct flow
*flow
,
2416 struct mcast_snooping
*ms
, int vlan
,
2417 struct xbundle
*in_xbundle
,
2418 const struct dp_packet
*packet
)
2419 OVS_REQ_WRLOCK(ms
->rwlock
)
2421 const struct igmp_header
*igmp
;
2424 ovs_be32 ip4
= flow
->igmp_group_ip4
;
2426 offset
= (char *) dp_packet_l4(packet
) - (char *) dp_packet_data(packet
);
2427 igmp
= dp_packet_at(packet
, offset
, IGMP_HEADER_LEN
);
2428 if (!igmp
|| csum(igmp
, dp_packet_l4_size(packet
)) != 0) {
2429 xlate_report_debug(ctx
, OFT_DETAIL
,
2430 "multicast snooping received bad IGMP "
2431 "checksum on port %s in VLAN %d",
2432 in_xbundle
->name
, vlan
);
2436 switch (ntohs(flow
->tp_src
)) {
2437 case IGMP_HOST_MEMBERSHIP_REPORT
:
2438 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
2439 if (mcast_snooping_add_group4(ms
, ip4
, vlan
, in_xbundle
->ofbundle
)) {
2440 xlate_report_debug(ctx
, OFT_DETAIL
,
2441 "multicast snooping learned that "
2442 IP_FMT
" is on port %s in VLAN %d",
2443 IP_ARGS(ip4
), in_xbundle
->name
, vlan
);
2446 case IGMP_HOST_LEAVE_MESSAGE
:
2447 if (mcast_snooping_leave_group4(ms
, ip4
, vlan
, in_xbundle
->ofbundle
)) {
2448 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping leaving "
2449 IP_FMT
" is on port %s in VLAN %d",
2450 IP_ARGS(ip4
), in_xbundle
->name
, vlan
);
2453 case IGMP_HOST_MEMBERSHIP_QUERY
:
2454 if (flow
->nw_src
&& mcast_snooping_add_mrouter(ms
, vlan
,
2455 in_xbundle
->ofbundle
)) {
2456 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping query "
2457 "from "IP_FMT
" is on port %s in VLAN %d",
2458 IP_ARGS(flow
->nw_src
), in_xbundle
->name
, vlan
);
2461 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
2462 count
= mcast_snooping_add_report(ms
, packet
, vlan
,
2463 in_xbundle
->ofbundle
);
2465 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping processed "
2466 "%d addresses on port %s in VLAN %d",
2467 count
, in_xbundle
->name
, vlan
);
2474 update_mcast_snooping_table6__(const struct xlate_ctx
*ctx
,
2475 const struct flow
*flow
,
2476 struct mcast_snooping
*ms
, int vlan
,
2477 struct xbundle
*in_xbundle
,
2478 const struct dp_packet
*packet
)
2479 OVS_REQ_WRLOCK(ms
->rwlock
)
2481 const struct mld_header
*mld
;
2485 offset
= (char *) dp_packet_l4(packet
) - (char *) dp_packet_data(packet
);
2486 mld
= dp_packet_at(packet
, offset
, MLD_HEADER_LEN
);
2489 packet_csum_upperlayer6(dp_packet_l3(packet
),
2490 mld
, IPPROTO_ICMPV6
,
2491 dp_packet_l4_size(packet
)) != 0) {
2492 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping received "
2493 "bad MLD checksum on port %s in VLAN %d",
2494 in_xbundle
->name
, vlan
);
2498 switch (ntohs(flow
->tp_src
)) {
2500 if (!ipv6_addr_equals(&flow
->ipv6_src
, &in6addr_any
)
2501 && mcast_snooping_add_mrouter(ms
, vlan
, in_xbundle
->ofbundle
)) {
2502 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping query on "
2503 "port %s in VLAN %d", in_xbundle
->name
, vlan
);
2509 count
= mcast_snooping_add_mld(ms
, packet
, vlan
, in_xbundle
->ofbundle
);
2511 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping processed "
2512 "%d addresses on port %s in VLAN %d",
2513 count
, in_xbundle
->name
, vlan
);
2519 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2520 * was received on 'in_xbundle' in 'vlan'. */
2522 update_mcast_snooping_table(const struct xlate_ctx
*ctx
,
2523 const struct flow
*flow
, int vlan
,
2524 struct xbundle
*in_xbundle
,
2525 const struct dp_packet
*packet
)
2527 struct mcast_snooping
*ms
= ctx
->xbridge
->ms
;
2528 struct xbundle
*mcast_xbundle
;
2529 struct mcast_port_bundle
*fport
;
2531 /* Don't learn the OFPP_NONE port. */
2532 if (in_xbundle
== &ofpp_none_bundle
) {
2536 /* Don't learn from flood ports */
2537 mcast_xbundle
= NULL
;
2538 ovs_rwlock_wrlock(&ms
->rwlock
);
2539 LIST_FOR_EACH(fport
, node
, &ms
->fport_list
) {
2540 mcast_xbundle
= xbundle_lookup(ctx
->xcfg
, fport
->port
);
2541 if (mcast_xbundle
== in_xbundle
) {
2546 if (!mcast_xbundle
|| mcast_xbundle
!= in_xbundle
) {
2547 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2548 update_mcast_snooping_table4__(ctx
, flow
, ms
, vlan
,
2549 in_xbundle
, packet
);
2551 update_mcast_snooping_table6__(ctx
, flow
, ms
, vlan
,
2552 in_xbundle
, packet
);
2555 ovs_rwlock_unlock(&ms
->rwlock
);
2558 /* send the packet to ports having the multicast group learned */
2560 xlate_normal_mcast_send_group(struct xlate_ctx
*ctx
,
2561 struct mcast_snooping
*ms OVS_UNUSED
,
2562 struct mcast_group
*grp
,
2563 struct xbundle
*in_xbundle
,
2564 const struct xvlan
*xvlan
)
2565 OVS_REQ_RDLOCK(ms
->rwlock
)
2567 struct mcast_group_bundle
*b
;
2568 struct xbundle
*mcast_xbundle
;
2570 LIST_FOR_EACH(b
, bundle_node
, &grp
->bundle_lru
) {
2571 mcast_xbundle
= xbundle_lookup(ctx
->xcfg
, b
->port
);
2572 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
) {
2573 xlate_report(ctx
, OFT_DETAIL
, "forwarding to mcast group port");
2574 output_normal(ctx
, mcast_xbundle
, xvlan
);
2575 } else if (!mcast_xbundle
) {
2576 xlate_report(ctx
, OFT_WARN
,
2577 "mcast group port is unknown, dropping");
2579 xlate_report(ctx
, OFT_DETAIL
,
2580 "mcast group port is input port, dropping");
2585 /* send the packet to ports connected to multicast routers */
2587 xlate_normal_mcast_send_mrouters(struct xlate_ctx
*ctx
,
2588 struct mcast_snooping
*ms
,
2589 struct xbundle
*in_xbundle
,
2590 const struct xvlan
*xvlan
)
2591 OVS_REQ_RDLOCK(ms
->rwlock
)
2593 struct mcast_mrouter_bundle
*mrouter
;
2594 struct xbundle
*mcast_xbundle
;
2596 LIST_FOR_EACH(mrouter
, mrouter_node
, &ms
->mrouter_lru
) {
2597 mcast_xbundle
= xbundle_lookup(ctx
->xcfg
, mrouter
->port
);
2598 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
2599 && mrouter
->vlan
== xvlan
->v
[0].vid
) {
2600 xlate_report(ctx
, OFT_DETAIL
, "forwarding to mcast router port");
2601 output_normal(ctx
, mcast_xbundle
, xvlan
);
2602 } else if (!mcast_xbundle
) {
2603 xlate_report(ctx
, OFT_WARN
,
2604 "mcast router port is unknown, dropping");
2605 } else if (mrouter
->vlan
!= xvlan
->v
[0].vid
) {
2606 xlate_report(ctx
, OFT_DETAIL
,
2607 "mcast router is on another vlan, dropping");
2609 xlate_report(ctx
, OFT_DETAIL
,
2610 "mcast router port is input port, dropping");
2615 /* send the packet to ports flagged to be flooded */
2617 xlate_normal_mcast_send_fports(struct xlate_ctx
*ctx
,
2618 struct mcast_snooping
*ms
,
2619 struct xbundle
*in_xbundle
,
2620 const struct xvlan
*xvlan
)
2621 OVS_REQ_RDLOCK(ms
->rwlock
)
2623 struct mcast_port_bundle
*fport
;
2624 struct xbundle
*mcast_xbundle
;
2626 LIST_FOR_EACH(fport
, node
, &ms
->fport_list
) {
2627 mcast_xbundle
= xbundle_lookup(ctx
->xcfg
, fport
->port
);
2628 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
) {
2629 xlate_report(ctx
, OFT_DETAIL
, "forwarding to mcast flood port");
2630 output_normal(ctx
, mcast_xbundle
, xvlan
);
2631 } else if (!mcast_xbundle
) {
2632 xlate_report(ctx
, OFT_WARN
,
2633 "mcast flood port is unknown, dropping");
2635 xlate_report(ctx
, OFT_DETAIL
,
2636 "mcast flood port is input port, dropping");
2641 /* forward the Reports to configured ports */
2643 xlate_normal_mcast_send_rports(struct xlate_ctx
*ctx
,
2644 struct mcast_snooping
*ms
,
2645 struct xbundle
*in_xbundle
,
2646 const struct xvlan
*xvlan
)
2647 OVS_REQ_RDLOCK(ms
->rwlock
)
2649 struct mcast_port_bundle
*rport
;
2650 struct xbundle
*mcast_xbundle
;
2652 LIST_FOR_EACH(rport
, node
, &ms
->rport_list
) {
2653 mcast_xbundle
= xbundle_lookup(ctx
->xcfg
, rport
->port
);
2655 && mcast_xbundle
!= in_xbundle
2656 && mcast_xbundle
->ofbundle
!= in_xbundle
->ofbundle
) {
2657 xlate_report(ctx
, OFT_DETAIL
,
2658 "forwarding report to mcast flagged port");
2659 output_normal(ctx
, mcast_xbundle
, xvlan
);
2660 } else if (!mcast_xbundle
) {
2661 xlate_report(ctx
, OFT_WARN
,
2662 "mcast port is unknown, dropping the report");
2664 xlate_report(ctx
, OFT_DETAIL
,
2665 "mcast port is input port, dropping the Report");
2671 xlate_normal_flood(struct xlate_ctx
*ctx
, struct xbundle
*in_xbundle
,
2672 struct xvlan
*xvlan
)
2674 struct xbundle
*xbundle
;
2676 LIST_FOR_EACH (xbundle
, list_node
, &ctx
->xbridge
->xbundles
) {
2677 if (xbundle
!= in_xbundle
2678 && xbundle
->ofbundle
!= in_xbundle
->ofbundle
2679 && xbundle_includes_vlan(xbundle
, xvlan
)
2680 && xbundle
->floodable
2681 && !xbundle_mirror_out(ctx
->xbridge
, xbundle
)) {
2682 output_normal(ctx
, xbundle
, xvlan
);
2685 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
2689 is_ip_local_multicast(const struct flow
*flow
, struct flow_wildcards
*wc
)
2691 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2692 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
2693 return ip_is_local_multicast(flow
->nw_dst
);
2694 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
2695 memset(&wc
->masks
.ipv6_dst
, 0xff, sizeof wc
->masks
.ipv6_dst
);
2696 return ipv6_is_all_hosts(&flow
->ipv6_dst
);
2703 xlate_normal(struct xlate_ctx
*ctx
)
2705 struct flow_wildcards
*wc
= ctx
->wc
;
2706 struct flow
*flow
= &ctx
->xin
->flow
;
2707 struct xbundle
*in_xbundle
;
2708 struct xport
*in_port
;
2709 struct mac_entry
*mac
;
2711 struct xvlan in_xvlan
;
2715 memset(&wc
->masks
.dl_src
, 0xff, sizeof wc
->masks
.dl_src
);
2716 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
2717 wc
->masks
.vlans
[0].tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
2719 in_xbundle
= lookup_input_bundle(ctx
, flow
->in_port
.ofp_port
, &in_port
);
2721 xlate_report(ctx
, OFT_WARN
, "no input bundle, dropping");
2725 /* Drop malformed frames. */
2726 if (eth_type_vlan(flow
->dl_type
) &&
2727 !(flow
->vlans
[0].tci
& htons(VLAN_CFI
))) {
2728 if (ctx
->xin
->packet
!= NULL
) {
2729 xlate_report_error(ctx
, "dropping packet with partial "
2730 "VLAN tag received on port %s",
2733 xlate_report(ctx
, OFT_WARN
, "partial VLAN tag, dropping");
2737 /* Drop frames on bundles reserved for mirroring. */
2738 if (xbundle_mirror_out(ctx
->xbridge
, in_xbundle
)) {
2739 if (ctx
->xin
->packet
!= NULL
) {
2740 xlate_report_error(ctx
, "dropping packet received on port %s, "
2741 "which is reserved exclusively for mirroring",
2744 xlate_report(ctx
, OFT_WARN
,
2745 "input port is mirror output port, dropping");
2750 xvlan_extract(flow
, &in_xvlan
);
2751 if (!input_vid_is_valid(ctx
, in_xvlan
.v
[0].vid
, in_xbundle
)) {
2752 xlate_report(ctx
, OFT_WARN
,
2753 "disallowed VLAN VID for this input port, dropping");
2756 xvlan_input_translate(in_xbundle
, &in_xvlan
, &xvlan
);
2757 vlan
= xvlan
.v
[0].vid
;
2759 /* Check other admissibility requirements. */
2760 if (in_port
&& !is_admissible(ctx
, in_port
, vlan
)) {
2764 /* Learn source MAC. */
2765 bool is_grat_arp
= is_gratuitous_arp(flow
, wc
);
2766 if (ctx
->xin
->allow_side_effects
2767 && flow
->packet_type
== htonl(PT_ETH
)
2768 && in_port
->pt_mode
!= NETDEV_PT_LEGACY_L3
2770 update_learning_table(ctx
, in_xbundle
, flow
->dl_src
, vlan
,
2773 if (ctx
->xin
->xcache
&& in_xbundle
!= &ofpp_none_bundle
) {
2774 struct xc_entry
*entry
;
2776 /* Save just enough info to update mac learning table later. */
2777 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NORMAL
);
2778 entry
->normal
.ofproto
= ctx
->xbridge
->ofproto
;
2779 entry
->normal
.in_port
= flow
->in_port
.ofp_port
;
2780 entry
->normal
.dl_src
= flow
->dl_src
;
2781 entry
->normal
.vlan
= vlan
;
2782 entry
->normal
.is_gratuitous_arp
= is_grat_arp
;
2785 /* Determine output bundle. */
2786 if (mcast_snooping_enabled(ctx
->xbridge
->ms
)
2787 && !eth_addr_is_broadcast(flow
->dl_dst
)
2788 && eth_addr_is_multicast(flow
->dl_dst
)
2789 && is_ip_any(flow
)) {
2790 struct mcast_snooping
*ms
= ctx
->xbridge
->ms
;
2791 struct mcast_group
*grp
= NULL
;
2793 if (is_igmp(flow
, wc
)) {
2795 * IGMP packets need to take the slow path, in order to be
2796 * processed for mdb updates. That will prevent expires
2797 * firing off even after hosts have sent reports.
2799 ctx
->xout
->slow
|= SLOW_ACTION
;
2801 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
2802 if (mcast_snooping_is_membership(flow
->tp_src
) ||
2803 mcast_snooping_is_query(flow
->tp_src
)) {
2804 if (ctx
->xin
->allow_side_effects
&& ctx
->xin
->packet
) {
2805 update_mcast_snooping_table(ctx
, flow
, vlan
,
2806 in_xbundle
, ctx
->xin
->packet
);
2810 if (mcast_snooping_is_membership(flow
->tp_src
)) {
2811 ovs_rwlock_rdlock(&ms
->rwlock
);
2812 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, &xvlan
);
2813 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2814 * forward IGMP Membership Reports only to those ports where
2815 * multicast routers are attached. Alternatively stated: a
2816 * snooping switch should not forward IGMP Membership Reports
2817 * to ports on which only hosts are attached.
2818 * An administrative control may be provided to override this
2819 * restriction, allowing the report messages to be flooded to
2821 xlate_normal_mcast_send_rports(ctx
, ms
, in_xbundle
, &xvlan
);
2822 ovs_rwlock_unlock(&ms
->rwlock
);
2824 xlate_report(ctx
, OFT_DETAIL
, "multicast traffic, flooding");
2825 xlate_normal_flood(ctx
, in_xbundle
, &xvlan
);
2828 } else if (is_mld(flow
, wc
)) {
2829 ctx
->xout
->slow
|= SLOW_ACTION
;
2830 if (ctx
->xin
->allow_side_effects
&& ctx
->xin
->packet
) {
2831 update_mcast_snooping_table(ctx
, flow
, vlan
,
2832 in_xbundle
, ctx
->xin
->packet
);
2834 if (is_mld_report(flow
, wc
)) {
2835 ovs_rwlock_rdlock(&ms
->rwlock
);
2836 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, &xvlan
);
2837 xlate_normal_mcast_send_rports(ctx
, ms
, in_xbundle
, &xvlan
);
2838 ovs_rwlock_unlock(&ms
->rwlock
);
2840 xlate_report(ctx
, OFT_DETAIL
, "MLD query, flooding");
2841 xlate_normal_flood(ctx
, in_xbundle
, &xvlan
);
2844 if (is_ip_local_multicast(flow
, wc
)) {
2845 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2846 * address in the 224.0.0.x range which are not IGMP must
2847 * be forwarded on all ports */
2848 xlate_report(ctx
, OFT_DETAIL
,
2849 "RFC4541: section 2.1.2, item 2, flooding");
2850 xlate_normal_flood(ctx
, in_xbundle
, &xvlan
);
2855 /* forwarding to group base ports */
2856 ovs_rwlock_rdlock(&ms
->rwlock
);
2857 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2858 grp
= mcast_snooping_lookup4(ms
, flow
->nw_dst
, vlan
);
2859 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
2860 grp
= mcast_snooping_lookup(ms
, &flow
->ipv6_dst
, vlan
);
2863 xlate_normal_mcast_send_group(ctx
, ms
, grp
, in_xbundle
, &xvlan
);
2864 xlate_normal_mcast_send_fports(ctx
, ms
, in_xbundle
, &xvlan
);
2865 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, &xvlan
);
2867 if (mcast_snooping_flood_unreg(ms
)) {
2868 xlate_report(ctx
, OFT_DETAIL
,
2869 "unregistered multicast, flooding");
2870 xlate_normal_flood(ctx
, in_xbundle
, &xvlan
);
2872 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, &xvlan
);
2873 xlate_normal_mcast_send_fports(ctx
, ms
, in_xbundle
, &xvlan
);
2876 ovs_rwlock_unlock(&ms
->rwlock
);
2878 ovs_rwlock_rdlock(&ctx
->xbridge
->ml
->rwlock
);
2879 mac
= mac_learning_lookup(ctx
->xbridge
->ml
, flow
->dl_dst
, vlan
);
2880 mac_port
= mac
? mac_entry_get_port(ctx
->xbridge
->ml
, mac
) : NULL
;
2881 ovs_rwlock_unlock(&ctx
->xbridge
->ml
->rwlock
);
2884 struct xbundle
*mac_xbundle
= xbundle_lookup(ctx
->xcfg
, mac_port
);
2886 && mac_xbundle
!= in_xbundle
2887 && mac_xbundle
->ofbundle
!= in_xbundle
->ofbundle
) {
2888 xlate_report(ctx
, OFT_DETAIL
, "forwarding to learned port");
2889 output_normal(ctx
, mac_xbundle
, &xvlan
);
2890 } else if (!mac_xbundle
) {
2891 xlate_report(ctx
, OFT_WARN
,
2892 "learned port is unknown, dropping");
2894 xlate_report(ctx
, OFT_DETAIL
,
2895 "learned port is input port, dropping");
2898 xlate_report(ctx
, OFT_DETAIL
,
2899 "no learned MAC for destination, flooding");
2900 xlate_normal_flood(ctx
, in_xbundle
, &xvlan
);
2905 /* Appends a "sample" action for sFlow or IPFIX to 'ctx->odp_actions'. The
2906 * 'probability' is the number of packets out of UINT32_MAX to sample. The
2907 * 'cookie' is passed back in the callback for each sampled packet.
2908 * 'tunnel_out_port', if not ODPP_NONE, is added as the
2909 * OVS_USERSPACE_ATTR_EGRESS_TUN_PORT attribute. If 'include_actions',
2910 * an OVS_USERSPACE_ATTR_ACTIONS attribute is added. If
2911 * 'emit_set_tunnel', sample(sampling_port=1) would translate into
2912 * datapath sample action set(tunnel(...)), sample(...) and it is used
2913 * for sampling egress tunnel information.
2916 compose_sample_action(struct xlate_ctx
*ctx
,
2917 const uint32_t probability
,
2918 const struct user_action_cookie
*cookie
,
2919 const odp_port_t tunnel_out_port
,
2920 bool include_actions
)
2922 if (probability
== 0) {
2923 /* No need to generate sampling or the inner action. */
2927 /* If the slow path meter is configured by the controller,
2928 * insert a meter action before the user space action. */
2929 struct ofproto
*ofproto
= &ctx
->xin
->ofproto
->up
;
2930 uint32_t meter_id
= ofproto
->slowpath_meter_id
;
2932 /* When meter action is not required, avoid generate sample action
2933 * for 100% sampling rate. */
2934 bool is_sample
= probability
< UINT32_MAX
|| meter_id
!= UINT32_MAX
;
2935 size_t sample_offset
, actions_offset
;
2937 sample_offset
= nl_msg_start_nested(ctx
->odp_actions
,
2938 OVS_ACTION_ATTR_SAMPLE
);
2939 nl_msg_put_u32(ctx
->odp_actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
2941 actions_offset
= nl_msg_start_nested(ctx
->odp_actions
,
2942 OVS_SAMPLE_ATTR_ACTIONS
);
2945 if (meter_id
!= UINT32_MAX
) {
2946 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_METER
, meter_id
);
2949 odp_port_t odp_port
= ofp_port_to_odp_port(
2950 ctx
->xbridge
, ctx
->xin
->flow
.in_port
.ofp_port
);
2951 uint32_t pid
= dpif_port_get_pid(ctx
->xbridge
->dpif
, odp_port
,
2952 flow_hash_5tuple(&ctx
->xin
->flow
, 0));
2953 int cookie_offset
= odp_put_userspace_action(pid
, cookie
, sizeof *cookie
,
2959 nl_msg_end_nested(ctx
->odp_actions
, actions_offset
);
2960 nl_msg_end_nested(ctx
->odp_actions
, sample_offset
);
2963 return cookie_offset
;
2966 /* If sFLow is not enabled, returns 0 without doing anything.
2968 * If sFlow is enabled, appends a template "sample" action to the ODP actions
2969 * in 'ctx'. This action is a template because some of the information needed
2970 * to fill it out is not available until flow translation is complete. In this
2971 * case, this functions returns an offset, which is always nonzero, to pass
2972 * later to fix_sflow_action() to fill in the rest of the template. */
2974 compose_sflow_action(struct xlate_ctx
*ctx
)
2976 struct dpif_sflow
*sflow
= ctx
->xbridge
->sflow
;
2977 if (!sflow
|| ctx
->xin
->flow
.in_port
.ofp_port
== OFPP_NONE
) {
2981 struct user_action_cookie cookie
= {
2982 .type
= USER_ACTION_COOKIE_SFLOW
,
2983 .ofp_in_port
= ctx
->xin
->flow
.in_port
.ofp_port
,
2984 .ofproto_uuid
= ctx
->xbridge
->ofproto
->uuid
2986 return compose_sample_action(ctx
, dpif_sflow_get_probability(sflow
),
2987 &cookie
, ODPP_NONE
, true);
2990 /* If flow IPFIX is enabled, make sure IPFIX flow sample action
2991 * at egress point of tunnel port is just in front of corresponding
2992 * output action. If bridge IPFIX is enabled, this appends an IPFIX
2993 * sample action to 'ctx->odp_actions'. */
2995 compose_ipfix_action(struct xlate_ctx
*ctx
, odp_port_t output_odp_port
)
2997 struct dpif_ipfix
*ipfix
= ctx
->xbridge
->ipfix
;
2998 odp_port_t tunnel_out_port
= ODPP_NONE
;
3000 if (!ipfix
|| ctx
->xin
->flow
.in_port
.ofp_port
== OFPP_NONE
) {
3004 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
3006 if (output_odp_port
== ODPP_NONE
&&
3007 !dpif_ipfix_get_bridge_exporter_input_sampling(ipfix
)) {
3011 /* For output case, output_odp_port is valid. */
3012 if (output_odp_port
!= ODPP_NONE
) {
3013 if (!dpif_ipfix_get_bridge_exporter_output_sampling(ipfix
)) {
3016 /* If tunnel sampling is enabled, put an additional option attribute:
3017 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
3019 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(ipfix
) &&
3020 dpif_ipfix_is_tunnel_port(ipfix
, output_odp_port
) ) {
3021 tunnel_out_port
= output_odp_port
;
3025 struct user_action_cookie cookie
= {
3026 .type
= USER_ACTION_COOKIE_IPFIX
,
3027 .ofp_in_port
= ctx
->xin
->flow
.in_port
.ofp_port
,
3028 .ofproto_uuid
= ctx
->xbridge
->ofproto
->uuid
,
3029 .ipfix
.output_odp_port
= output_odp_port
3031 compose_sample_action(ctx
,
3032 dpif_ipfix_get_bridge_exporter_probability(ipfix
),
3033 &cookie
, tunnel_out_port
, false);
3036 /* Fix "sample" action according to data collected while composing ODP actions,
3037 * as described in compose_sflow_action().
3039 * 'user_cookie_offset' must be the offset returned by
3040 * compose_sflow_action(). */
3042 fix_sflow_action(struct xlate_ctx
*ctx
, unsigned int user_cookie_offset
)
3044 const struct flow
*base
= &ctx
->base_flow
;
3045 struct user_action_cookie
*cookie
;
3047 cookie
= ofpbuf_at(ctx
->odp_actions
, user_cookie_offset
, sizeof *cookie
);
3048 ovs_assert(cookie
->type
== USER_ACTION_COOKIE_SFLOW
);
3050 cookie
->sflow
.vlan_tci
= base
->vlans
[0].tci
;
3052 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
3053 * port information") for the interpretation of cookie->output. */
3054 switch (ctx
->sflow_n_outputs
) {
3056 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
3057 cookie
->sflow
.output
= 0x40000000 | 256;
3061 cookie
->sflow
.output
= dpif_sflow_odp_port_to_ifindex(
3062 ctx
->xbridge
->sflow
, ctx
->sflow_odp_port
);
3063 if (cookie
->sflow
.output
) {
3068 /* 0x80000000 means "multiple output ports. */
3069 cookie
->sflow
.output
= 0x80000000 | ctx
->sflow_n_outputs
;
3075 process_special(struct xlate_ctx
*ctx
, const struct xport
*xport
)
3077 const struct flow
*flow
= &ctx
->xin
->flow
;
3078 struct flow_wildcards
*wc
= ctx
->wc
;
3079 const struct xbridge
*xbridge
= ctx
->xbridge
;
3080 const struct dp_packet
*packet
= ctx
->xin
->packet
;
3081 enum slow_path_reason slow
;
3085 } else if (xport
->cfm
&& cfm_should_process_flow(xport
->cfm
, flow
, wc
)) {
3087 cfm_process_heartbeat(xport
->cfm
, packet
);
3090 } else if (xport
->bfd
&& bfd_should_process_flow(xport
->bfd
, flow
, wc
)) {
3092 bfd_process_packet(xport
->bfd
, flow
, packet
);
3093 /* If POLL received, immediately sends FINAL back. */
3094 if (bfd_should_send_packet(xport
->bfd
)) {
3095 ofproto_dpif_monitor_port_send_soon(xport
->ofport
);
3099 } else if (xport
->xbundle
&& xport
->xbundle
->lacp
3100 && flow
->dl_type
== htons(ETH_TYPE_LACP
)) {
3102 lacp_process_packet(xport
->xbundle
->lacp
, xport
->ofport
, packet
);
3105 } else if ((xbridge
->stp
|| xbridge
->rstp
) &&
3106 stp_should_process_flow(flow
, wc
)) {
3109 ? stp_process_packet(xport
, packet
)
3110 : rstp_process_packet(xport
, packet
);
3113 } else if (xport
->lldp
&& lldp_should_process_flow(xport
->lldp
, flow
)) {
3115 lldp_process_packet(xport
->lldp
, packet
);
3123 ctx
->xout
->slow
|= slow
;
3131 tnl_route_lookup_flow(const struct xlate_ctx
*ctx
,
3132 const struct flow
*oflow
,
3133 struct in6_addr
*ip
, struct in6_addr
*src
,
3134 struct xport
**out_port
)
3136 char out_dev
[IFNAMSIZ
];
3137 struct xbridge
*xbridge
;
3139 struct in6_addr dst
;
3141 dst
= flow_tnl_dst(&oflow
->tunnel
);
3142 if (!ovs_router_lookup(oflow
->pkt_mark
, &dst
, out_dev
, src
, &gw
)) {
3146 if (ipv6_addr_is_set(&gw
) &&
3147 (!IN6_IS_ADDR_V4MAPPED(&gw
) || in6_addr_get_mapped_ipv4(&gw
))) {
3153 HMAP_FOR_EACH (xbridge
, hmap_node
, &ctx
->xcfg
->xbridges
) {
3154 if (!strncmp(xbridge
->name
, out_dev
, IFNAMSIZ
)) {
3157 HMAP_FOR_EACH (port
, ofp_node
, &xbridge
->xports
) {
3158 if (!strncmp(netdev_get_name(port
->netdev
), out_dev
, IFNAMSIZ
)) {
3169 compose_table_xlate(struct xlate_ctx
*ctx
, const struct xport
*out_dev
,
3170 struct dp_packet
*packet
)
3172 struct xbridge
*xbridge
= out_dev
->xbridge
;
3173 struct ofpact_output output
;
3176 ofpact_init(&output
.ofpact
, OFPACT_OUTPUT
, sizeof output
);
3177 flow_extract(packet
, &flow
);
3178 flow
.in_port
.ofp_port
= out_dev
->ofp_port
;
3179 output
.port
= OFPP_TABLE
;
3182 return ofproto_dpif_execute_actions__(xbridge
->ofproto
,
3183 ctx
->xin
->tables_version
, &flow
,
3184 NULL
, &output
.ofpact
, sizeof output
,
3185 ctx
->depth
, ctx
->resubmits
, packet
);
3189 tnl_send_nd_request(struct xlate_ctx
*ctx
, const struct xport
*out_dev
,
3190 const struct eth_addr eth_src
,
3191 struct in6_addr
* ipv6_src
, struct in6_addr
* ipv6_dst
)
3193 struct dp_packet packet
;
3195 dp_packet_init(&packet
, 0);
3196 compose_nd_ns(&packet
, eth_src
, ipv6_src
, ipv6_dst
);
3197 compose_table_xlate(ctx
, out_dev
, &packet
);
3198 dp_packet_uninit(&packet
);
3202 tnl_send_arp_request(struct xlate_ctx
*ctx
, const struct xport
*out_dev
,
3203 const struct eth_addr eth_src
,
3204 ovs_be32 ip_src
, ovs_be32 ip_dst
)
3206 struct dp_packet packet
;
3208 dp_packet_init(&packet
, 0);
3209 compose_arp(&packet
, ARP_OP_REQUEST
,
3210 eth_src
, eth_addr_zero
, true, ip_src
, ip_dst
);
3212 compose_table_xlate(ctx
, out_dev
, &packet
);
3213 dp_packet_uninit(&packet
);
3217 propagate_tunnel_data_to_flow__(struct flow
*dst_flow
,
3218 const struct flow
*src_flow
,
3219 struct eth_addr dmac
, struct eth_addr smac
,
3220 struct in6_addr s_ip6
, ovs_be32 s_ip
,
3221 bool is_tnl_ipv6
, uint8_t nw_proto
)
3223 dst_flow
->dl_dst
= dmac
;
3224 dst_flow
->dl_src
= smac
;
3226 dst_flow
->packet_type
= htonl(PT_ETH
);
3227 dst_flow
->nw_dst
= src_flow
->tunnel
.ip_dst
;
3228 dst_flow
->nw_src
= src_flow
->tunnel
.ip_src
;
3229 dst_flow
->ipv6_dst
= src_flow
->tunnel
.ipv6_dst
;
3230 dst_flow
->ipv6_src
= src_flow
->tunnel
.ipv6_src
;
3232 dst_flow
->nw_frag
= 0; /* Tunnel packets are unfragmented. */
3233 dst_flow
->nw_tos
= src_flow
->tunnel
.ip_tos
;
3234 dst_flow
->nw_ttl
= src_flow
->tunnel
.ip_ttl
;
3235 dst_flow
->tp_dst
= src_flow
->tunnel
.tp_dst
;
3236 dst_flow
->tp_src
= src_flow
->tunnel
.tp_src
;
3239 dst_flow
->dl_type
= htons(ETH_TYPE_IPV6
);
3240 if (ipv6_mask_is_any(&dst_flow
->ipv6_src
)
3241 && !ipv6_mask_is_any(&s_ip6
)) {
3242 dst_flow
->ipv6_src
= s_ip6
;
3245 dst_flow
->dl_type
= htons(ETH_TYPE_IP
);
3246 if (dst_flow
->nw_src
== 0 && s_ip
) {
3247 dst_flow
->nw_src
= s_ip
;
3250 dst_flow
->nw_proto
= nw_proto
;
3254 * Populate the 'flow' and 'base_flow' L3 fields to do the post tunnel push
3258 propagate_tunnel_data_to_flow(struct xlate_ctx
*ctx
, struct eth_addr dmac
,
3259 struct eth_addr smac
, struct in6_addr s_ip6
,
3260 ovs_be32 s_ip
, bool is_tnl_ipv6
,
3261 enum ovs_vport_type tnl_type
)
3263 struct flow
*base_flow
, *flow
;
3264 flow
= &ctx
->xin
->flow
;
3265 base_flow
= &ctx
->base_flow
;
3266 uint8_t nw_proto
= 0;
3269 case OVS_VPORT_TYPE_GRE
:
3270 nw_proto
= IPPROTO_GRE
;
3272 case OVS_VPORT_TYPE_VXLAN
:
3273 case OVS_VPORT_TYPE_GENEVE
:
3274 nw_proto
= IPPROTO_UDP
;
3276 case OVS_VPORT_TYPE_LISP
:
3277 case OVS_VPORT_TYPE_STT
:
3278 case OVS_VPORT_TYPE_UNSPEC
:
3279 case OVS_VPORT_TYPE_NETDEV
:
3280 case OVS_VPORT_TYPE_INTERNAL
:
3281 case __OVS_VPORT_TYPE_MAX
:
3286 * Update base_flow first followed by flow as the dst_flow gets modified
3289 propagate_tunnel_data_to_flow__(base_flow
, flow
, dmac
, smac
, s_ip6
, s_ip
,
3290 is_tnl_ipv6
, nw_proto
);
3291 propagate_tunnel_data_to_flow__(flow
, flow
, dmac
, smac
, s_ip6
, s_ip
,
3292 is_tnl_ipv6
, nw_proto
);
3296 native_tunnel_output(struct xlate_ctx
*ctx
, const struct xport
*xport
,
3297 const struct flow
*flow
, odp_port_t tunnel_odp_port
,
3300 struct netdev_tnl_build_header_params tnl_params
;
3301 struct ovs_action_push_tnl tnl_push_data
;
3302 struct xport
*out_dev
= NULL
;
3303 ovs_be32 s_ip
= 0, d_ip
= 0;
3304 struct in6_addr s_ip6
= in6addr_any
;
3305 struct in6_addr d_ip6
= in6addr_any
;
3306 struct eth_addr smac
;
3307 struct eth_addr dmac
;
3309 char buf_sip6
[INET6_ADDRSTRLEN
];
3310 char buf_dip6
[INET6_ADDRSTRLEN
];
3312 /* Store sFlow data. */
3313 uint32_t sflow_n_outputs
= ctx
->sflow_n_outputs
;
3315 /* Structures to backup Ethernet and IP of base_flow. */
3316 struct flow old_base_flow
;
3317 struct flow old_flow
;
3319 /* Backup flow & base_flow data. */
3320 memcpy(&old_base_flow
, &ctx
->base_flow
, sizeof old_base_flow
);
3321 memcpy(&old_flow
, &ctx
->xin
->flow
, sizeof old_flow
);
3323 err
= tnl_route_lookup_flow(ctx
, flow
, &d_ip6
, &s_ip6
, &out_dev
);
3325 xlate_report(ctx
, OFT_WARN
, "native tunnel routing failed");
3329 xlate_report(ctx
, OFT_DETAIL
, "tunneling to %s via %s",
3330 ipv6_string_mapped(buf_dip6
, &d_ip6
),
3331 netdev_get_name(out_dev
->netdev
));
3333 /* Use mac addr of bridge port of the peer. */
3334 err
= netdev_get_etheraddr(out_dev
->netdev
, &smac
);
3336 xlate_report(ctx
, OFT_WARN
,
3337 "tunnel output device lacks Ethernet address");
3341 d_ip
= in6_addr_get_mapped_ipv4(&d_ip6
);
3343 s_ip
= in6_addr_get_mapped_ipv4(&s_ip6
);
3346 err
= tnl_neigh_lookup(out_dev
->xbridge
->name
, &d_ip6
, &dmac
);
3348 xlate_report(ctx
, OFT_DETAIL
,
3349 "neighbor cache miss for %s on bridge %s, "
3350 "sending %s request",
3351 buf_dip6
, out_dev
->xbridge
->name
, d_ip
? "ARP" : "ND");
3353 tnl_send_arp_request(ctx
, out_dev
, smac
, s_ip
, d_ip
);
3355 tnl_send_nd_request(ctx
, out_dev
, smac
, &s_ip6
, &d_ip6
);
3360 if (ctx
->xin
->xcache
) {
3361 struct xc_entry
*entry
;
3363 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_TNL_NEIGH
);
3364 ovs_strlcpy(entry
->tnl_neigh_cache
.br_name
, out_dev
->xbridge
->name
,
3365 sizeof entry
->tnl_neigh_cache
.br_name
);
3366 entry
->tnl_neigh_cache
.d_ipv6
= d_ip6
;
3369 xlate_report(ctx
, OFT_DETAIL
, "tunneling from "ETH_ADDR_FMT
" %s"
3370 " to "ETH_ADDR_FMT
" %s",
3371 ETH_ADDR_ARGS(smac
), ipv6_string_mapped(buf_sip6
, &s_ip6
),
3372 ETH_ADDR_ARGS(dmac
), buf_dip6
);
3374 netdev_init_tnl_build_header_params(&tnl_params
, flow
, &s_ip6
, dmac
, smac
);
3375 err
= tnl_port_build_header(xport
->ofport
, &tnl_push_data
, &tnl_params
);
3379 tnl_push_data
.tnl_port
= tunnel_odp_port
;
3380 tnl_push_data
.out_port
= out_dev
->odp_port
;
3382 /* After tunnel header has been added, MAC and IP data of flow and
3383 * base_flow need to be set properly, since there is not recirculation
3384 * any more when sending packet to tunnel. */
3386 propagate_tunnel_data_to_flow(ctx
, dmac
, smac
, s_ip6
,
3387 s_ip
, tnl_params
.is_ipv6
,
3388 tnl_push_data
.tnl_type
);
3390 size_t clone_ofs
= 0;
3391 size_t push_action_size
;
3393 clone_ofs
= nl_msg_start_nested(ctx
->odp_actions
, OVS_ACTION_ATTR_CLONE
);
3394 odp_put_tnl_push_action(ctx
->odp_actions
, &tnl_push_data
);
3395 push_action_size
= ctx
->odp_actions
->size
;
3398 const struct dpif_flow_stats
*backup_resubmit_stats
;
3399 struct xlate_cache
*backup_xcache
;
3400 struct flow_wildcards
*backup_wc
, wc
;
3401 bool backup_side_effects
;
3402 const struct dp_packet
*backup_packet
;
3404 memset(&wc
, 0 , sizeof wc
);
3405 backup_wc
= ctx
->wc
;
3407 ctx
->xin
->wc
= NULL
;
3408 backup_resubmit_stats
= ctx
->xin
->resubmit_stats
;
3409 backup_xcache
= ctx
->xin
->xcache
;
3410 backup_side_effects
= ctx
->xin
->allow_side_effects
;
3411 backup_packet
= ctx
->xin
->packet
;
3413 ctx
->xin
->resubmit_stats
= NULL
;
3414 ctx
->xin
->xcache
= xlate_cache_new(); /* Use new temporary cache. */
3415 ctx
->xin
->allow_side_effects
= false;
3416 ctx
->xin
->packet
= NULL
;
3418 /* Push the cache entry for the tunnel first. */
3419 struct xc_entry
*entry
;
3420 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_TUNNEL_HEADER
);
3421 entry
->tunnel_hdr
.hdr_size
= tnl_push_data
.header_len
;
3422 entry
->tunnel_hdr
.operation
= ADD
;
3424 patch_port_output(ctx
, xport
, out_dev
);
3426 /* Similar to the stats update in revalidation, the x_cache entries
3427 * are populated by the previous translation are used to update the
3430 if (backup_resubmit_stats
) {
3431 struct dpif_flow_stats stats
= *backup_resubmit_stats
;
3432 xlate_push_stats(ctx
->xin
->xcache
, &stats
);
3434 xlate_cache_steal_entries(backup_xcache
, ctx
->xin
->xcache
);
3436 if (ctx
->odp_actions
->size
> push_action_size
) {
3437 nl_msg_end_non_empty_nested(ctx
->odp_actions
, clone_ofs
);
3439 nl_msg_cancel_nested(ctx
->odp_actions
, clone_ofs
);
3440 /* XXX : There is no real use-case for a tunnel push without
3441 * any post actions. However keeping it now
3442 * as is to make the 'make check' happy. Should remove when all the
3443 * make check tunnel test case does something meaningful on a
3444 * tunnel encap packets.
3446 odp_put_tnl_push_action(ctx
->odp_actions
, &tnl_push_data
);
3449 /* Restore context status. */
3450 ctx
->xin
->resubmit_stats
= backup_resubmit_stats
;
3451 xlate_cache_delete(ctx
->xin
->xcache
);
3452 ctx
->xin
->xcache
= backup_xcache
;
3453 ctx
->xin
->allow_side_effects
= backup_side_effects
;
3454 ctx
->xin
->packet
= backup_packet
;
3455 ctx
->wc
= backup_wc
;
3457 /* In order to maintain accurate stats, use recirc for
3458 * natvie tunneling. */
3459 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_RECIRC
, 0);
3460 nl_msg_end_nested(ctx
->odp_actions
, clone_ofs
);
3463 /* Restore the flows after the translation. */
3464 memcpy(&ctx
->xin
->flow
, &old_flow
, sizeof ctx
->xin
->flow
);
3465 memcpy(&ctx
->base_flow
, &old_base_flow
, sizeof ctx
->base_flow
);
3467 /* Restore sFlow data. */
3468 ctx
->sflow_n_outputs
= sflow_n_outputs
;
3474 xlate_commit_actions(struct xlate_ctx
*ctx
)
3476 bool use_masked
= ctx
->xbridge
->support
.masked_set_action
;
3478 ctx
->xout
->slow
|= commit_odp_actions(&ctx
->xin
->flow
, &ctx
->base_flow
,
3479 ctx
->odp_actions
, ctx
->wc
,
3480 use_masked
, ctx
->pending_encap
,
3481 ctx
->pending_decap
, ctx
->encap_data
);
3482 ctx
->pending_encap
= false;
3483 ctx
->pending_decap
= false;
3484 ofpbuf_delete(ctx
->encap_data
);
3485 ctx
->encap_data
= NULL
;
3489 clear_conntrack(struct xlate_ctx
*ctx
)
3491 ctx
->conntracked
= false;
3492 flow_clear_conntrack(&ctx
->xin
->flow
);
3496 xlate_flow_is_protected(const struct xlate_ctx
*ctx
, const struct flow
*flow
, const struct xport
*xport_out
)
3498 const struct xport
*xport_in
;
3504 xport_in
= get_ofp_port(ctx
->xbridge
, flow
->in_port
.ofp_port
);
3506 return (xport_in
&& xport_in
->xbundle
&& xport_out
->xbundle
&&
3507 xport_in
->xbundle
->protected && xport_out
->xbundle
->protected);
3510 /* Function handles when a packet is sent from one bridge to another bridge.
3512 * The bridges are internally connected, either with patch ports or with
3515 * The output action to another bridge causes translation to continue within
3516 * the next bridge. This process can be recursive; the next bridge can
3517 * output yet to another bridge.
3519 * The translated actions from the second bridge onwards are enclosed within
3520 * the clone action, so that any modification to the packet will not be visible
3521 * to the remaining actions of the originating bridge.
3524 patch_port_output(struct xlate_ctx
*ctx
, const struct xport
*in_dev
,
3525 struct xport
*out_dev
)
3527 struct flow
*flow
= &ctx
->xin
->flow
;
3528 struct flow old_flow
= ctx
->xin
->flow
;
3529 struct flow_tnl old_flow_tnl_wc
= ctx
->wc
->masks
.tunnel
;
3530 bool old_conntrack
= ctx
->conntracked
;
3531 bool old_was_mpls
= ctx
->was_mpls
;
3532 ovs_version_t old_version
= ctx
->xin
->tables_version
;
3533 struct ofpbuf old_stack
= ctx
->stack
;
3534 uint8_t new_stack
[1024];
3535 struct ofpbuf old_action_set
= ctx
->action_set
;
3536 struct ovs_list
*old_trace
= ctx
->xin
->trace
;
3537 uint64_t actset_stub
[1024 / 8];
3539 ofpbuf_use_stub(&ctx
->stack
, new_stack
, sizeof new_stack
);
3540 ofpbuf_use_stub(&ctx
->action_set
, actset_stub
, sizeof actset_stub
);
3541 flow
->in_port
.ofp_port
= out_dev
->ofp_port
;
3542 flow
->metadata
= htonll(0);
3543 memset(&flow
->tunnel
, 0, sizeof flow
->tunnel
);
3544 memset(&ctx
->wc
->masks
.tunnel
, 0, sizeof ctx
->wc
->masks
.tunnel
);
3545 flow
->tunnel
.metadata
.tab
=
3546 ofproto_get_tun_tab(&out_dev
->xbridge
->ofproto
->up
);
3547 ctx
->wc
->masks
.tunnel
.metadata
.tab
= flow
->tunnel
.metadata
.tab
;
3548 memset(flow
->regs
, 0, sizeof flow
->regs
);
3549 flow
->actset_output
= OFPP_UNSET
;
3550 clear_conntrack(ctx
);
3551 ctx
->xin
->trace
= xlate_report(ctx
, OFT_BRIDGE
, "bridge(\"%s\")",
3552 out_dev
->xbridge
->name
);
3553 mirror_mask_t old_mirrors
= ctx
->mirrors
;
3554 bool independent_mirrors
= out_dev
->xbridge
!= ctx
->xbridge
;
3555 if (independent_mirrors
) {
3558 ctx
->xbridge
= out_dev
->xbridge
;
3560 /* The bridge is now known so obtain its table version. */
3561 ctx
->xin
->tables_version
3562 = ofproto_dpif_get_tables_version(ctx
->xbridge
->ofproto
);
3564 if (!process_special(ctx
, out_dev
) && may_receive(out_dev
, ctx
)) {
3565 if (xport_stp_forward_state(out_dev
) &&
3566 xport_rstp_forward_state(out_dev
)) {
3567 xlate_table_action(ctx
, flow
->in_port
.ofp_port
, 0, true, true,
3568 false, true, clone_xlate_actions
);
3569 if (!ctx
->freezing
) {
3570 xlate_action_set(ctx
);
3572 if (ctx
->freezing
) {
3573 finish_freezing(ctx
);
3576 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
3577 * the learning action look at the packet, then drop it. */
3578 struct flow old_base_flow
= ctx
->base_flow
;
3579 size_t old_size
= ctx
->odp_actions
->size
;
3580 mirror_mask_t old_mirrors2
= ctx
->mirrors
;
3582 xlate_table_action(ctx
, flow
->in_port
.ofp_port
, 0, true, true,
3583 false, true, clone_xlate_actions
);
3584 ctx
->mirrors
= old_mirrors2
;
3585 ctx
->base_flow
= old_base_flow
;
3586 ctx
->odp_actions
->size
= old_size
;
3588 /* Undo changes that may have been done for freezing. */
3589 ctx_cancel_freeze(ctx
);
3593 ctx
->xin
->trace
= old_trace
;
3594 if (independent_mirrors
) {
3595 ctx
->mirrors
= old_mirrors
;
3597 ctx
->xin
->flow
= old_flow
;
3598 ctx
->xbridge
= in_dev
->xbridge
;
3599 ofpbuf_uninit(&ctx
->action_set
);
3600 ctx
->action_set
= old_action_set
;
3601 ofpbuf_uninit(&ctx
->stack
);
3602 ctx
->stack
= old_stack
;
3604 /* Restore calling bridge's lookup version. */
3605 ctx
->xin
->tables_version
= old_version
;
3607 /* Restore to calling bridge tunneling information */
3608 ctx
->wc
->masks
.tunnel
= old_flow_tnl_wc
;
3610 /* The out bridge popping MPLS should have no effect on the original
3612 ctx
->was_mpls
= old_was_mpls
;
3614 /* The out bridge's conntrack execution should have no effect on the
3615 * original bridge. */
3616 ctx
->conntracked
= old_conntrack
;
3618 /* The fact that the out bridge exits (for any reason) does not mean
3619 * that the original bridge should exit. Specifically, if the out
3620 * bridge freezes translation, the original bridge must continue
3621 * processing with the original, not the frozen packet! */
3624 /* Out bridge errors do not propagate back. */
3625 ctx
->error
= XLATE_OK
;
3627 if (ctx
->xin
->resubmit_stats
) {
3628 netdev_vport_inc_tx(in_dev
->netdev
, ctx
->xin
->resubmit_stats
);
3629 netdev_vport_inc_rx(out_dev
->netdev
, ctx
->xin
->resubmit_stats
);
3631 bfd_account_rx(out_dev
->bfd
, ctx
->xin
->resubmit_stats
);
3634 if (ctx
->xin
->xcache
) {
3635 struct xc_entry
*entry
;
3637 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NETDEV
);
3638 entry
->dev
.tx
= netdev_ref(in_dev
->netdev
);
3639 entry
->dev
.rx
= netdev_ref(out_dev
->netdev
);
3640 entry
->dev
.bfd
= bfd_ref(out_dev
->bfd
);
3645 check_output_prerequisites(struct xlate_ctx
*ctx
,
3646 const struct xport
*xport
,
3650 struct flow_wildcards
*wc
= ctx
->wc
;
3653 xlate_report(ctx
, OFT_WARN
, "Nonexistent output port");
3655 } else if (xport
->config
& OFPUTIL_PC_NO_FWD
) {
3656 xlate_report(ctx
, OFT_DETAIL
, "OFPPC_NO_FWD set, skipping output");
3658 } else if (ctx
->mirror_snaplen
!= 0 && xport
->odp_port
== ODPP_NONE
) {
3659 xlate_report(ctx
, OFT_WARN
,
3660 "Mirror truncate to ODPP_NONE, skipping output");
3662 } else if (xlate_flow_is_protected(ctx
, flow
, xport
)) {
3663 xlate_report(ctx
, OFT_WARN
,
3664 "Flow is between protected ports, skipping output.");
3666 } else if (check_stp
) {
3667 if (is_stp(&ctx
->base_flow
)) {
3668 if (!xport_stp_should_forward_bpdu(xport
) &&
3669 !xport_rstp_should_manage_bpdu(xport
)) {
3670 if (ctx
->xbridge
->stp
!= NULL
) {
3671 xlate_report(ctx
, OFT_WARN
,
3672 "STP not in listening state, "
3673 "skipping bpdu output");
3674 } else if (ctx
->xbridge
->rstp
!= NULL
) {
3675 xlate_report(ctx
, OFT_WARN
,
3676 "RSTP not managing BPDU in this state, "
3677 "skipping bpdu output");
3681 } else if ((xport
->cfm
&& cfm_should_process_flow(xport
->cfm
, flow
, wc
))
3682 || (xport
->bfd
&& bfd_should_process_flow(xport
->bfd
, flow
,
3684 /* Pass; STP should not block link health detection. */
3685 } else if (!xport_stp_forward_state(xport
) ||
3686 !xport_rstp_forward_state(xport
)) {
3687 if (ctx
->xbridge
->stp
!= NULL
) {
3688 xlate_report(ctx
, OFT_WARN
,
3689 "STP not in forwarding state, skipping output");
3690 } else if (ctx
->xbridge
->rstp
!= NULL
) {
3691 xlate_report(ctx
, OFT_WARN
,
3692 "RSTP not in forwarding state, skipping output");
3698 if (xport
->pt_mode
== NETDEV_PT_LEGACY_L2
&&
3699 flow
->packet_type
!= htonl(PT_ETH
)) {
3700 xlate_report(ctx
, OFT_WARN
, "Trying to send non-Ethernet packet "
3701 "through legacy L2 port. Dropping packet.");
3709 terminate_native_tunnel(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
,
3710 struct flow
*flow
, struct flow_wildcards
*wc
,
3711 odp_port_t
*tnl_port
)
3713 *tnl_port
= ODPP_NONE
;
3715 /* XXX: Write better Filter for tunnel port. We can use in_port
3716 * in tunnel-port flow to avoid these checks completely. */
3717 if (ofp_port
== OFPP_LOCAL
&&
3718 ovs_native_tunneling_is_on(ctx
->xbridge
->ofproto
)) {
3719 *tnl_port
= tnl_port_map_lookup(flow
, wc
);
3722 return *tnl_port
!= ODPP_NONE
;
3726 compose_output_action__(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
,
3727 const struct xlate_bond_recirc
*xr
, bool check_stp
,
3728 bool is_last_action OVS_UNUSED
, bool truncate
)
3730 const struct xport
*xport
= get_ofp_port(ctx
->xbridge
, ofp_port
);
3731 struct flow_wildcards
*wc
= ctx
->wc
;
3732 struct flow
*flow
= &ctx
->xin
->flow
;
3733 struct flow_tnl flow_tnl
;
3734 union flow_vlan_hdr flow_vlans
[FLOW_MAX_VLAN_HEADERS
];
3735 uint8_t flow_nw_tos
;
3736 odp_port_t out_port
, odp_port
, odp_tnl_port
;
3737 bool is_native_tunnel
= false;
3739 struct eth_addr flow_dl_dst
= flow
->dl_dst
;
3740 struct eth_addr flow_dl_src
= flow
->dl_src
;
3741 ovs_be32 flow_packet_type
= flow
->packet_type
;
3742 ovs_be16 flow_dl_type
= flow
->dl_type
;
3744 /* If 'struct flow' gets additional metadata, we'll need to zero it out
3745 * before traversing a patch port. */
3746 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 40);
3747 memset(&flow_tnl
, 0, sizeof flow_tnl
);
3749 if (!check_output_prerequisites(ctx
, xport
, flow
, check_stp
)) {
3753 if (flow
->packet_type
== htonl(PT_ETH
)) {
3754 /* Strip Ethernet header for legacy L3 port. */
3755 if (xport
->pt_mode
== NETDEV_PT_LEGACY_L3
) {
3756 flow
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
3757 ntohs(flow
->dl_type
));
3763 xlate_report_error(ctx
, "Cannot truncate output to patch port");
3765 patch_port_output(ctx
, xport
, xport
->peer
);
3769 memcpy(flow_vlans
, flow
->vlans
, sizeof flow_vlans
);
3770 flow_nw_tos
= flow
->nw_tos
;
3772 if (count_skb_priorities(xport
)) {
3773 memset(&wc
->masks
.skb_priority
, 0xff, sizeof wc
->masks
.skb_priority
);
3774 if (dscp_from_skb_priority(xport
, flow
->skb_priority
, &dscp
)) {
3775 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
3776 flow
->nw_tos
&= ~IP_DSCP_MASK
;
3777 flow
->nw_tos
|= dscp
;
3781 if (xport
->is_tunnel
) {
3782 struct in6_addr dst
;
3783 /* Save tunnel metadata so that changes made due to
3784 * the Logical (tunnel) Port are not visible for any further
3785 * matches, while explicit set actions on tunnel metadata are.
3787 flow_tnl
= flow
->tunnel
;
3788 odp_port
= tnl_port_send(xport
->ofport
, flow
, ctx
->wc
);
3789 if (odp_port
== ODPP_NONE
) {
3790 xlate_report(ctx
, OFT_WARN
, "Tunneling decided against output");
3791 goto out
; /* restore flow_nw_tos */
3793 dst
= flow_tnl_dst(&flow
->tunnel
);
3794 if (ipv6_addr_equals(&dst
, &ctx
->orig_tunnel_ipv6_dst
)) {
3795 xlate_report(ctx
, OFT_WARN
, "Not tunneling to our own address");
3796 goto out
; /* restore flow_nw_tos */
3798 if (ctx
->xin
->resubmit_stats
) {
3799 netdev_vport_inc_tx(xport
->netdev
, ctx
->xin
->resubmit_stats
);
3801 if (ctx
->xin
->xcache
) {
3802 struct xc_entry
*entry
;
3804 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NETDEV
);
3805 entry
->dev
.tx
= netdev_ref(xport
->netdev
);
3807 out_port
= odp_port
;
3808 if (ovs_native_tunneling_is_on(ctx
->xbridge
->ofproto
)) {
3809 xlate_report(ctx
, OFT_DETAIL
, "output to native tunnel");
3810 is_native_tunnel
= true;
3812 xlate_report(ctx
, OFT_DETAIL
, "output to kernel tunnel");
3813 commit_odp_tunnel_action(flow
, &ctx
->base_flow
, ctx
->odp_actions
);
3814 flow
->tunnel
= flow_tnl
; /* Restore tunnel metadata */
3817 odp_port
= xport
->odp_port
;
3818 out_port
= odp_port
;
3821 if (out_port
!= ODPP_NONE
) {
3822 /* Commit accumulated flow updates before output. */
3823 xlate_commit_actions(ctx
);
3826 /* Recirculate the packet. */
3827 struct ovs_action_hash
*act_hash
;
3830 act_hash
= nl_msg_put_unspec_uninit(ctx
->odp_actions
,
3831 OVS_ACTION_ATTR_HASH
,
3833 act_hash
->hash_alg
= xr
->hash_alg
;
3834 act_hash
->hash_basis
= xr
->hash_basis
;
3836 /* Recirc action. */
3837 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_RECIRC
,
3839 } else if (is_native_tunnel
) {
3840 /* Output to native tunnel port. */
3841 native_tunnel_output(ctx
, xport
, flow
, odp_port
, truncate
);
3842 flow
->tunnel
= flow_tnl
; /* Restore tunnel metadata */
3844 } else if (terminate_native_tunnel(ctx
, ofp_port
, flow
, wc
,
3846 /* Intercept packet to be received on native tunnel port. */
3847 nl_msg_put_odp_port(ctx
->odp_actions
, OVS_ACTION_ATTR_TUNNEL_POP
,
3851 /* Tunnel push-pop action is not compatible with
3853 compose_ipfix_action(ctx
, out_port
);
3855 /* Handle truncation of the mirrored packet. */
3856 if (ctx
->mirror_snaplen
> 0 &&
3857 ctx
->mirror_snaplen
< UINT16_MAX
) {
3858 struct ovs_action_trunc
*trunc
;
3860 trunc
= nl_msg_put_unspec_uninit(ctx
->odp_actions
,
3861 OVS_ACTION_ATTR_TRUNC
,
3863 trunc
->max_len
= ctx
->mirror_snaplen
;
3864 if (!ctx
->xbridge
->support
.trunc
) {
3865 ctx
->xout
->slow
|= SLOW_ACTION
;
3869 nl_msg_put_odp_port(ctx
->odp_actions
,
3870 OVS_ACTION_ATTR_OUTPUT
,
3874 ctx
->sflow_odp_port
= odp_port
;
3875 ctx
->sflow_n_outputs
++;
3876 ctx
->nf_output_iface
= ofp_port
;
3879 if (mbridge_has_mirrors(ctx
->xbridge
->mbridge
) && xport
->xbundle
) {
3880 mirror_packet(ctx
, xport
->xbundle
,
3881 xbundle_mirror_dst(xport
->xbundle
->xbridge
,
3887 memcpy(flow
->vlans
, flow_vlans
, sizeof flow
->vlans
);
3888 flow
->nw_tos
= flow_nw_tos
;
3889 flow
->dl_dst
= flow_dl_dst
;
3890 flow
->dl_src
= flow_dl_src
;
3891 flow
->packet_type
= flow_packet_type
;
3892 flow
->dl_type
= flow_dl_type
;
3896 compose_output_action(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
,
3897 const struct xlate_bond_recirc
*xr
,
3898 bool is_last_action
, bool truncate
)
3900 compose_output_action__(ctx
, ofp_port
, xr
, true,
3901 is_last_action
, truncate
);
3905 xlate_recursively(struct xlate_ctx
*ctx
, struct rule_dpif
*rule
,
3906 bool deepens
, bool is_last_action
,
3907 xlate_actions_handler
*actions_xlator
)
3909 struct rule_dpif
*old_rule
= ctx
->rule
;
3910 ovs_be64 old_cookie
= ctx
->rule_cookie
;
3911 const struct rule_actions
*actions
;
3913 if (ctx
->xin
->resubmit_stats
) {
3914 rule_dpif_credit_stats(rule
, ctx
->xin
->resubmit_stats
);
3919 ctx
->depth
+= deepens
;
3921 ctx
->rule_cookie
= rule
->up
.flow_cookie
;
3922 actions
= rule_get_actions(&rule
->up
);
3923 actions_xlator(actions
->ofpacts
, actions
->ofpacts_len
, ctx
,
3925 ctx
->rule_cookie
= old_cookie
;
3926 ctx
->rule
= old_rule
;
3927 ctx
->depth
-= deepens
;
3931 xlate_resubmit_resource_check(struct xlate_ctx
*ctx
)
3933 if (ctx
->depth
>= MAX_DEPTH
) {
3934 xlate_report_error(ctx
, "over max translation depth %d", MAX_DEPTH
);
3935 ctx
->error
= XLATE_RECURSION_TOO_DEEP
;
3936 } else if (ctx
->resubmits
>= MAX_RESUBMITS
) {
3937 xlate_report_error(ctx
, "over %d resubmit actions", MAX_RESUBMITS
);
3938 ctx
->error
= XLATE_TOO_MANY_RESUBMITS
;
3939 } else if (ctx
->odp_actions
->size
> UINT16_MAX
) {
3940 xlate_report_error(ctx
, "resubmits yielded over 64 kB of actions");
3941 /* NOT an error, as we'll be slow-pathing the flow in this case? */
3942 ctx
->exit
= true; /* XXX: translation still terminated! */
3943 } else if (ctx
->stack
.size
>= 65536) {
3944 xlate_report_error(ctx
, "resubmits yielded over 64 kB of stack");
3945 ctx
->error
= XLATE_STACK_TOO_DEEP
;
3954 tuple_swap_flow(struct flow
*flow
, bool ipv4
)
3956 uint8_t nw_proto
= flow
->nw_proto
;
3957 flow
->nw_proto
= flow
->ct_nw_proto
;
3958 flow
->ct_nw_proto
= nw_proto
;
3961 ovs_be32 nw_src
= flow
->nw_src
;
3962 flow
->nw_src
= flow
->ct_nw_src
;
3963 flow
->ct_nw_src
= nw_src
;
3965 ovs_be32 nw_dst
= flow
->nw_dst
;
3966 flow
->nw_dst
= flow
->ct_nw_dst
;
3967 flow
->ct_nw_dst
= nw_dst
;
3969 struct in6_addr ipv6_src
= flow
->ipv6_src
;
3970 flow
->ipv6_src
= flow
->ct_ipv6_src
;
3971 flow
->ct_ipv6_src
= ipv6_src
;
3973 struct in6_addr ipv6_dst
= flow
->ipv6_dst
;
3974 flow
->ipv6_dst
= flow
->ct_ipv6_dst
;
3975 flow
->ct_ipv6_dst
= ipv6_dst
;
3978 ovs_be16 tp_src
= flow
->tp_src
;
3979 flow
->tp_src
= flow
->ct_tp_src
;
3980 flow
->ct_tp_src
= tp_src
;
3982 ovs_be16 tp_dst
= flow
->tp_dst
;
3983 flow
->tp_dst
= flow
->ct_tp_dst
;
3984 flow
->ct_tp_dst
= tp_dst
;
3988 tuple_swap(struct flow
*flow
, struct flow_wildcards
*wc
)
3990 bool ipv4
= (flow
->dl_type
== htons(ETH_TYPE_IP
));
3992 tuple_swap_flow(flow
, ipv4
);
3993 tuple_swap_flow(&wc
->masks
, ipv4
);
3997 xlate_table_action(struct xlate_ctx
*ctx
, ofp_port_t in_port
, uint8_t table_id
,
3998 bool may_packet_in
, bool honor_table_miss
,
3999 bool with_ct_orig
, bool is_last_action
,
4000 xlate_actions_handler
*xlator
)
4002 /* Check if we need to recirculate before matching in a table. */
4003 if (ctx
->was_mpls
) {
4004 ctx_trigger_freeze(ctx
);
4007 if (xlate_resubmit_resource_check(ctx
)) {
4008 uint8_t old_table_id
= ctx
->table_id
;
4009 struct rule_dpif
*rule
;
4011 ctx
->table_id
= table_id
;
4013 /* Swap packet fields with CT 5-tuple if requested. */
4015 /* Do not swap if there is no CT tuple, or if key is not IP. */
4016 if (ctx
->xin
->flow
.ct_nw_proto
== 0 ||
4017 !is_ip_any(&ctx
->xin
->flow
)) {
4018 xlate_report_error(ctx
,
4019 "resubmit(ct) with non-tracked or non-IP packet!");
4022 tuple_swap(&ctx
->xin
->flow
, ctx
->wc
);
4024 rule
= rule_dpif_lookup_from_table(ctx
->xbridge
->ofproto
,
4025 ctx
->xin
->tables_version
,
4026 &ctx
->xin
->flow
, ctx
->wc
,
4027 ctx
->xin
->resubmit_stats
,
4028 &ctx
->table_id
, in_port
,
4029 may_packet_in
, honor_table_miss
,
4033 tuple_swap(&ctx
->xin
->flow
, ctx
->wc
);
4037 /* Fill in the cache entry here instead of xlate_recursively
4038 * to make the reference counting more explicit. We take a
4039 * reference in the lookups above if we are going to cache the
4041 if (ctx
->xin
->xcache
) {
4042 struct xc_entry
*entry
;
4044 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_RULE
);
4046 ofproto_rule_ref(&rule
->up
);
4049 struct ovs_list
*old_trace
= ctx
->xin
->trace
;
4050 xlate_report_table(ctx
, rule
, table_id
);
4051 xlate_recursively(ctx
, rule
, table_id
<= old_table_id
,
4052 is_last_action
, xlator
);
4053 ctx
->xin
->trace
= old_trace
;
4056 ctx
->table_id
= old_table_id
;
4061 /* Consumes the group reference, which is only taken if xcache exists. */
4063 xlate_group_stats(struct xlate_ctx
*ctx
, struct group_dpif
*group
,
4064 struct ofputil_bucket
*bucket
)
4066 if (ctx
->xin
->resubmit_stats
) {
4067 group_dpif_credit_stats(group
, bucket
, ctx
->xin
->resubmit_stats
);
4069 if (ctx
->xin
->xcache
) {
4070 struct xc_entry
*entry
;
4072 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_GROUP
);
4073 entry
->group
.group
= group
;
4074 entry
->group
.bucket
= bucket
;
4079 xlate_group_bucket(struct xlate_ctx
*ctx
, struct ofputil_bucket
*bucket
,
4080 bool is_last_action
)
4082 uint64_t action_list_stub
[1024 / 8];
4083 struct ofpbuf action_list
= OFPBUF_STUB_INITIALIZER(action_list_stub
);
4084 struct ofpbuf action_set
= ofpbuf_const_initializer(bucket
->ofpacts
,
4085 bucket
->ofpacts_len
);
4086 struct flow old_flow
= ctx
->xin
->flow
;
4087 bool old_was_mpls
= ctx
->was_mpls
;
4089 ofpacts_execute_action_set(&action_list
, &action_set
);
4091 do_xlate_actions(action_list
.data
, action_list
.size
, ctx
, is_last_action
);
4094 ofpbuf_uninit(&action_list
);
4096 /* Check if need to freeze. */
4097 if (ctx
->freezing
) {
4098 finish_freezing(ctx
);
4101 /* Roll back flow to previous state.
4102 * This is equivalent to cloning the packet for each bucket.
4104 * As a side effect any subsequently applied actions will
4105 * also effectively be applied to a clone of the packet taken
4106 * just before applying the all or indirect group.
4108 * Note that group buckets are action sets, hence they cannot modify the
4109 * main action set. Also any stack actions are ignored when executing an
4110 * action set, so group buckets cannot change the stack either.
4111 * However, we do allow resubmit actions in group buckets, which could
4112 * break the above assumptions. It is up to the controller to not mess up
4113 * with the action_set and stack in the tables resubmitted to from
4115 ctx
->xin
->flow
= old_flow
;
4117 /* The group bucket popping MPLS should have no effect after bucket
4119 ctx
->was_mpls
= old_was_mpls
;
4121 /* The fact that the group bucket exits (for any reason) does not mean that
4122 * the translation after the group action should exit. Specifically, if
4123 * the group bucket freezes translation, the actions after the group action
4124 * must continue processing with the original, not the frozen packet! */
4127 /* Context error in a bucket should not impact processing of other buckets
4128 * or actions. This is similar to cloning a packet for group buckets.
4129 * There is no need to restore the error back to old value due to the fact
4130 * that we actually processed group action which can happen only when there
4131 * is no previous context error.
4133 * Exception to above is errors which are system limits to protect
4134 * translation from running too long or occupy too much space. These errors
4135 * should not be masked. XLATE_RECURSION_TOO_DEEP, XLATE_TOO_MANY_RESUBMITS
4136 * and XLATE_STACK_TOO_DEEP fall in this category. */
4137 if (ctx
->error
== XLATE_TOO_MANY_MPLS_LABELS
||
4138 ctx
->error
== XLATE_UNSUPPORTED_PACKET_TYPE
) {
4139 /* reset the error and continue processing other buckets */
4140 ctx
->error
= XLATE_OK
;
4145 xlate_all_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
,
4146 bool is_last_action
)
4148 struct ofputil_bucket
*bucket
;
4149 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
4150 bool last
= is_last_action
&& !bucket
->list_node
.next
;
4151 xlate_group_bucket(ctx
, bucket
, last
);
4153 xlate_group_stats(ctx
, group
, NULL
);
4157 xlate_ff_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
,
4158 bool is_last_action
)
4160 struct ofputil_bucket
*bucket
;
4162 bucket
= group_first_live_bucket(ctx
, group
, 0);
4164 xlate_group_bucket(ctx
, bucket
, is_last_action
);
4165 xlate_group_stats(ctx
, group
, bucket
);
4166 } else if (ctx
->xin
->xcache
) {
4167 ofproto_group_unref(&group
->up
);
4172 xlate_default_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
,
4173 bool is_last_action
)
4175 struct flow_wildcards
*wc
= ctx
->wc
;
4176 struct ofputil_bucket
*bucket
;
4179 basis
= flow_hash_symmetric_l4(&ctx
->xin
->flow
, 0);
4180 flow_mask_hash_fields(&ctx
->xin
->flow
, wc
, NX_HASH_FIELDS_SYMMETRIC_L4
);
4181 bucket
= group_best_live_bucket(ctx
, group
, basis
);
4183 xlate_group_bucket(ctx
, bucket
, is_last_action
);
4184 xlate_group_stats(ctx
, group
, bucket
);
4185 } else if (ctx
->xin
->xcache
) {
4186 ofproto_group_unref(&group
->up
);
4191 xlate_hash_fields_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
,
4192 bool is_last_action
)
4194 const struct field_array
*fields
= &group
->up
.props
.fields
;
4195 const uint8_t *mask_values
= fields
->values
;
4196 uint32_t basis
= hash_uint64(group
->up
.props
.selection_method_param
);
4199 BITMAP_FOR_EACH_1 (i
, MFF_N_IDS
, fields
->used
.bm
) {
4200 const struct mf_field
*mf
= mf_from_id(i
);
4202 /* Skip fields for which prerequisites are not met. */
4203 if (!mf_are_prereqs_ok(mf
, &ctx
->xin
->flow
, ctx
->wc
)) {
4204 /* Skip the mask bytes for this field. */
4205 mask_values
+= mf
->n_bytes
;
4209 union mf_value value
;
4210 union mf_value mask
;
4212 mf_get_value(mf
, &ctx
->xin
->flow
, &value
);
4213 /* Mask the value. */
4214 for (int j
= 0; j
< mf
->n_bytes
; j
++) {
4215 mask
.b
[j
] = *mask_values
++;
4216 value
.b
[j
] &= mask
.b
[j
];
4218 basis
= hash_bytes(&value
, mf
->n_bytes
, basis
);
4220 /* For tunnels, hash in whether the field is present. */
4221 if (mf_is_tun_metadata(mf
)) {
4222 basis
= hash_boolean(mf_is_set(mf
, &ctx
->xin
->flow
), basis
);
4225 mf_mask_field_masked(mf
, &mask
, ctx
->wc
);
4228 struct ofputil_bucket
*bucket
= group_best_live_bucket(ctx
, group
, basis
);
4230 xlate_group_bucket(ctx
, bucket
, is_last_action
);
4231 xlate_group_stats(ctx
, group
, bucket
);
4232 } else if (ctx
->xin
->xcache
) {
4233 ofproto_group_unref(&group
->up
);
4238 xlate_dp_hash_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
,
4239 bool is_last_action
)
4241 struct ofputil_bucket
*bucket
;
4243 /* dp_hash value 0 is special since it means that the dp_hash has not been
4244 * computed, as all computed dp_hash values are non-zero. Therefore
4245 * compare to zero can be used to decide if the dp_hash value is valid
4246 * without masking the dp_hash field. */
4247 if (!ctx
->xin
->flow
.dp_hash
) {
4248 uint64_t param
= group
->up
.props
.selection_method_param
;
4250 ctx_trigger_recirculate_with_hash(ctx
, param
>> 32, (uint32_t)param
);
4252 uint32_t n_buckets
= group
->up
.n_buckets
;
4254 /* Minimal mask to cover the number of buckets. */
4255 uint32_t mask
= (1 << log_2_ceil(n_buckets
)) - 1;
4256 /* Multiplier chosen to make the trivial 1 bit case to
4257 * actually distribute amongst two equal weight buckets. */
4258 uint32_t basis
= 0xc2b73583 * (ctx
->xin
->flow
.dp_hash
& mask
);
4260 ctx
->wc
->masks
.dp_hash
|= mask
;
4261 bucket
= group_best_live_bucket(ctx
, group
, basis
);
4263 xlate_group_bucket(ctx
, bucket
, is_last_action
);
4264 xlate_group_stats(ctx
, group
, bucket
);
4271 xlate_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
,
4272 bool is_last_action
)
4274 const char *selection_method
= group
->up
.props
.selection_method
;
4276 /* Select groups may access flow keys beyond L2 in order to
4277 * select a bucket. Recirculate as appropriate to make this possible.
4279 if (ctx
->was_mpls
) {
4280 ctx_trigger_freeze(ctx
);
4283 if (selection_method
[0] == '\0') {
4284 xlate_default_select_group(ctx
, group
, is_last_action
);
4285 } else if (!strcasecmp("hash", selection_method
)) {
4286 xlate_hash_fields_select_group(ctx
, group
, is_last_action
);
4287 } else if (!strcasecmp("dp_hash", selection_method
)) {
4288 xlate_dp_hash_select_group(ctx
, group
, is_last_action
);
4290 /* Parsing of groups should ensure this never happens */
4296 xlate_group_action__(struct xlate_ctx
*ctx
, struct group_dpif
*group
,
4297 bool is_last_action
)
4299 bool was_in_group
= ctx
->in_group
;
4300 ctx
->in_group
= true;
4302 switch (group
->up
.type
) {
4304 case OFPGT11_INDIRECT
:
4305 xlate_all_group(ctx
, group
, is_last_action
);
4307 case OFPGT11_SELECT
:
4308 xlate_select_group(ctx
, group
, is_last_action
);
4311 xlate_ff_group(ctx
, group
, is_last_action
);
4317 ctx
->in_group
= was_in_group
;
4321 xlate_group_action(struct xlate_ctx
*ctx
, uint32_t group_id
,
4322 bool is_last_action
)
4324 if (xlate_resubmit_resource_check(ctx
)) {
4325 struct group_dpif
*group
;
4327 /* Take ref only if xcache exists. */
4328 group
= group_dpif_lookup(ctx
->xbridge
->ofproto
, group_id
,
4329 ctx
->xin
->tables_version
, ctx
->xin
->xcache
);
4331 /* XXX: Should set ctx->error ? */
4332 xlate_report(ctx
, OFT_WARN
, "output to nonexistent group %"PRIu32
,
4336 xlate_group_action__(ctx
, group
, is_last_action
);
4343 xlate_ofpact_resubmit(struct xlate_ctx
*ctx
,
4344 const struct ofpact_resubmit
*resubmit
,
4345 bool is_last_action
)
4349 bool may_packet_in
= false;
4350 bool honor_table_miss
= false;
4352 if (ctx
->rule
&& rule_dpif_is_internal(ctx
->rule
)) {
4353 /* Still allow missed packets to be sent to the controller
4354 * if resubmitting from an internal table. */
4355 may_packet_in
= true;
4356 honor_table_miss
= true;
4359 in_port
= resubmit
->in_port
;
4360 if (in_port
== OFPP_IN_PORT
) {
4361 in_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
4364 table_id
= resubmit
->table_id
;
4365 if (table_id
== 255) {
4366 table_id
= ctx
->table_id
;
4369 xlate_table_action(ctx
, in_port
, table_id
, may_packet_in
,
4370 honor_table_miss
, resubmit
->with_ct_orig
,
4371 is_last_action
, do_xlate_actions
);
4375 flood_packet_to_port(struct xlate_ctx
*ctx
, const struct xport
*xport
,
4376 bool all
, bool is_last_action
)
4383 compose_output_action__(ctx
, xport
->ofp_port
, NULL
, false,
4384 is_last_action
, false);
4386 compose_output_action(ctx
, xport
->ofp_port
, NULL
, is_last_action
,
4392 flood_packets(struct xlate_ctx
*ctx
, bool all
, bool is_last_action
)
4394 const struct xport
*xport
, *last
= NULL
;
4396 /* Use 'last' the keep track of the last output port. */
4397 HMAP_FOR_EACH (xport
, ofp_node
, &ctx
->xbridge
->xports
) {
4398 if (xport
->ofp_port
== ctx
->xin
->flow
.in_port
.ofp_port
) {
4402 if (all
|| !(xport
->config
& OFPUTIL_PC_NO_FLOOD
)) {
4403 /* 'last' is not the last port, send a packet out, and
4405 flood_packet_to_port(ctx
, last
, all
, false);
4410 /* Send the packet to the 'last' port. */
4411 flood_packet_to_port(ctx
, last
, all
, is_last_action
);
4412 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
4416 put_controller_user_action(struct xlate_ctx
*ctx
,
4417 bool dont_send
, bool continuation
,
4418 uint32_t recirc_id
, int len
,
4419 enum ofp_packet_in_reason reason
,
4420 uint16_t controller_id
)
4422 struct user_action_cookie cookie
;
4424 memset(&cookie
, 0, sizeof cookie
);
4425 cookie
.type
= USER_ACTION_COOKIE_CONTROLLER
;
4426 cookie
.ofp_in_port
= OFPP_NONE
,
4427 cookie
.ofproto_uuid
= ctx
->xbridge
->ofproto
->uuid
;
4428 cookie
.controller
.dont_send
= dont_send
;
4429 cookie
.controller
.continuation
= continuation
;
4430 cookie
.controller
.reason
= reason
;
4431 cookie
.controller
.recirc_id
= recirc_id
;
4432 put_32aligned_be64(&cookie
.controller
.rule_cookie
, ctx
->rule_cookie
);
4433 cookie
.controller
.controller_id
= controller_id
;
4434 cookie
.controller
.max_len
= len
;
4436 odp_port_t odp_port
= ofp_port_to_odp_port(ctx
->xbridge
,
4437 ctx
->xin
->flow
.in_port
.ofp_port
);
4438 uint32_t pid
= dpif_port_get_pid(ctx
->xbridge
->dpif
, odp_port
,
4439 flow_hash_5tuple(&ctx
->xin
->flow
, 0));
4440 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
, ODPP_NONE
,
4441 false, ctx
->odp_actions
);
4445 xlate_controller_action(struct xlate_ctx
*ctx
, int len
,
4446 enum ofp_packet_in_reason reason
,
4447 uint16_t controller_id
,
4448 const uint8_t *userdata
, size_t userdata_len
)
4450 xlate_commit_actions(ctx
);
4452 /* A packet sent by an action in a table-miss rule is considered an
4453 * explicit table miss. OpenFlow before 1.3 doesn't have that concept so
4454 * it will get translated back to OFPR_ACTION for those versions. */
4455 if (reason
== OFPR_ACTION
4456 && ctx
->rule
&& rule_is_table_miss(&ctx
->rule
->up
)) {
4457 reason
= OFPR_EXPLICIT_MISS
;
4460 struct frozen_state state
= {
4461 .table_id
= ctx
->table_id
,
4462 .ofproto_uuid
= ctx
->xbridge
->ofproto
->uuid
,
4463 .stack
= ctx
->stack
.data
,
4464 .stack_size
= ctx
->stack
.size
,
4465 .mirrors
= ctx
->mirrors
,
4466 .conntracked
= ctx
->conntracked
,
4470 .action_set_len
= 0,
4471 .userdata
= CONST_CAST(uint8_t *, userdata
),
4472 .userdata_len
= userdata_len
,
4474 frozen_metadata_from_flow(&state
.metadata
, &ctx
->xin
->flow
);
4476 uint32_t recirc_id
= recirc_alloc_id_ctx(&state
);
4478 xlate_report_error(ctx
, "Failed to allocate recirculation id");
4479 ctx
->error
= XLATE_NO_RECIRCULATION_CONTEXT
;
4482 recirc_refs_add(&ctx
->xout
->recircs
, recirc_id
);
4486 uint32_t meter_id
= ctx
->xbridge
->ofproto
->up
.controller_meter_id
;
4487 if (meter_id
!= UINT32_MAX
) {
4488 /* If controller meter is configured, generate clone(meter, userspace)
4490 offset
= nl_msg_start_nested(ctx
->odp_actions
, OVS_ACTION_ATTR_SAMPLE
);
4491 nl_msg_put_u32(ctx
->odp_actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
4493 ac_offset
= nl_msg_start_nested(ctx
->odp_actions
,
4494 OVS_SAMPLE_ATTR_ACTIONS
);
4495 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_METER
, meter_id
);
4498 /* Generate the datapath flows even if we don't send the packet-in
4499 * so that debugging more closely represents normal state. */
4500 bool dont_send
= false;
4501 if (!ctx
->xin
->allow_side_effects
&& !ctx
->xin
->xcache
) {
4504 put_controller_user_action(ctx
, dont_send
, false, recirc_id
, len
,
4505 reason
, controller_id
);
4507 if (meter_id
!= UINT32_MAX
) {
4508 nl_msg_end_nested(ctx
->odp_actions
, ac_offset
);
4509 nl_msg_end_nested(ctx
->odp_actions
, offset
);
4513 /* Creates a frozen state, and allocates a unique recirc id for the given
4514 * state. Returns a non-zero recirc id if it is allocated successfully.
4515 * Returns 0 otherwise.
4518 finish_freezing__(struct xlate_ctx
*ctx
, uint8_t table
)
4520 ovs_assert(ctx
->freezing
);
4522 struct frozen_state state
= {
4524 .ofproto_uuid
= ctx
->xbridge
->ofproto
->uuid
,
4525 .stack
= ctx
->stack
.data
,
4526 .stack_size
= ctx
->stack
.size
,
4527 .mirrors
= ctx
->mirrors
,
4528 .conntracked
= ctx
->conntracked
,
4529 .xport_uuid
= ctx
->xin
->xport_uuid
,
4530 .ofpacts
= ctx
->frozen_actions
.data
,
4531 .ofpacts_len
= ctx
->frozen_actions
.size
,
4532 .action_set
= ctx
->action_set
.data
,
4533 .action_set_len
= ctx
->action_set
.size
,
4534 .userdata
= ctx
->pause
? CONST_CAST(uint8_t *,ctx
->pause
->userdata
)
4536 .userdata_len
= ctx
->pause
? ctx
->pause
->userdata_len
: 0,
4538 frozen_metadata_from_flow(&state
.metadata
, &ctx
->xin
->flow
);
4540 /* Allocate a unique recirc id for the given metadata state in the
4541 * flow. An existing id, with a new reference to the corresponding
4542 * recirculation context, will be returned if possible.
4543 * The life-cycle of this recirc id is managed by associating it
4544 * with the udpif key ('ukey') created for each new datapath flow. */
4545 uint32_t recirc_id
= recirc_alloc_id_ctx(&state
);
4547 xlate_report_error(ctx
, "Failed to allocate recirculation id");
4548 ctx
->error
= XLATE_NO_RECIRCULATION_CONTEXT
;
4551 recirc_refs_add(&ctx
->xout
->recircs
, recirc_id
);
4554 if (!ctx
->xin
->allow_side_effects
&& !ctx
->xin
->xcache
) {
4558 put_controller_user_action(ctx
, false, true, recirc_id
,
4559 ctx
->pause
->max_len
,
4561 ctx
->pause
->controller_id
);
4563 if (ctx
->recirc_update_dp_hash
) {
4564 struct ovs_action_hash
*act_hash
;
4567 act_hash
= nl_msg_put_unspec_uninit(ctx
->odp_actions
,
4568 OVS_ACTION_ATTR_HASH
,
4570 act_hash
->hash_alg
= OVS_HASH_ALG_L4
; /* Make configurable. */
4571 act_hash
->hash_basis
= 0; /* Make configurable. */
4573 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_RECIRC
, recirc_id
);
4576 /* Undo changes done by freezing. */
4577 ctx_cancel_freeze(ctx
);
4581 /* Called only when we're freezing. */
4583 finish_freezing(struct xlate_ctx
*ctx
)
4585 xlate_commit_actions(ctx
);
4586 finish_freezing__(ctx
, 0);
4589 /* Fork the pipeline here. The current packet will continue processing the
4590 * current action list. A clone of the current packet will recirculate, skip
4591 * the remainder of the current action list and asynchronously resume pipeline
4592 * processing in 'table' with the current metadata and action set. */
4594 compose_recirculate_and_fork(struct xlate_ctx
*ctx
, uint8_t table
,
4595 const uint16_t zone
)
4598 ctx
->freezing
= true;
4599 recirc_id
= finish_freezing__(ctx
, table
);
4601 if (OVS_UNLIKELY(ctx
->xin
->trace
) && recirc_id
) {
4602 if (oftrace_add_recirc_node(ctx
->xin
->recirc_queue
,
4603 OFT_RECIRC_CONNTRACK
, &ctx
->xin
->flow
,
4604 ctx
->xin
->packet
, recirc_id
, zone
)) {
4605 xlate_report(ctx
, OFT_DETAIL
, "A clone of the packet is forked to "
4606 "recirculate. The forked pipeline will be resumed at "
4607 "table %u.", table
);
4609 xlate_report(ctx
, OFT_DETAIL
, "Failed to trace the conntrack "
4610 "forked pipeline with recirc_id = %d.", recirc_id
);
4616 compose_mpls_push_action(struct xlate_ctx
*ctx
, struct ofpact_push_mpls
*mpls
)
4618 struct flow
*flow
= &ctx
->xin
->flow
;
4621 ovs_assert(eth_type_mpls(mpls
->ethertype
));
4623 n
= flow_count_mpls_labels(flow
, ctx
->wc
);
4625 xlate_commit_actions(ctx
);
4626 } else if (n
>= FLOW_MAX_MPLS_LABELS
) {
4627 if (ctx
->xin
->packet
!= NULL
) {
4628 xlate_report_error(ctx
, "dropping packet on which an MPLS push "
4629 "action can't be performed as it would have "
4630 "more MPLS LSEs than the %d supported.",
4631 FLOW_MAX_MPLS_LABELS
);
4633 ctx
->error
= XLATE_TOO_MANY_MPLS_LABELS
;
4637 /* Update flow's MPLS stack, and clear L3/4 fields to mark them invalid. */
4638 flow_push_mpls(flow
, n
, mpls
->ethertype
, ctx
->wc
, true);
4642 compose_mpls_pop_action(struct xlate_ctx
*ctx
, ovs_be16 eth_type
)
4644 struct flow
*flow
= &ctx
->xin
->flow
;
4645 int n
= flow_count_mpls_labels(flow
, ctx
->wc
);
4647 if (flow_pop_mpls(flow
, n
, eth_type
, ctx
->wc
)) {
4648 if (!eth_type_mpls(eth_type
) && ctx
->xbridge
->support
.odp
.recirc
) {
4649 ctx
->was_mpls
= true;
4651 } else if (n
>= FLOW_MAX_MPLS_LABELS
) {
4652 if (ctx
->xin
->packet
!= NULL
) {
4653 xlate_report_error(ctx
, "dropping packet on which an "
4654 "MPLS pop action can't be performed as it has "
4655 "more MPLS LSEs than the %d supported.",
4656 FLOW_MAX_MPLS_LABELS
);
4658 ctx
->error
= XLATE_TOO_MANY_MPLS_LABELS
;
4659 ofpbuf_clear(ctx
->odp_actions
);
4664 compose_dec_ttl(struct xlate_ctx
*ctx
, struct ofpact_cnt_ids
*ids
)
4666 struct flow
*flow
= &ctx
->xin
->flow
;
4668 if (!is_ip_any(flow
)) {
4672 ctx
->wc
->masks
.nw_ttl
= 0xff;
4673 if (flow
->nw_ttl
> 1) {
4679 for (i
= 0; i
< ids
->n_controllers
; i
++) {
4680 xlate_controller_action(ctx
, UINT16_MAX
, OFPR_INVALID_TTL
,
4681 ids
->cnt_ids
[i
], NULL
, 0);
4684 /* Stop processing for current table. */
4685 xlate_report(ctx
, OFT_WARN
, "IPv%d decrement TTL exception",
4686 flow
->dl_type
== htons(ETH_TYPE_IP
) ? 4 : 6);
4692 compose_set_mpls_label_action(struct xlate_ctx
*ctx
, ovs_be32 label
)
4694 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
4695 ctx
->wc
->masks
.mpls_lse
[0] |= htonl(MPLS_LABEL_MASK
);
4696 set_mpls_lse_label(&ctx
->xin
->flow
.mpls_lse
[0], label
);
4701 compose_set_mpls_tc_action(struct xlate_ctx
*ctx
, uint8_t tc
)
4703 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
4704 ctx
->wc
->masks
.mpls_lse
[0] |= htonl(MPLS_TC_MASK
);
4705 set_mpls_lse_tc(&ctx
->xin
->flow
.mpls_lse
[0], tc
);
4710 compose_dec_nsh_ttl_action(struct xlate_ctx
*ctx
)
4712 struct flow
*flow
= &ctx
->xin
->flow
;
4714 if ((flow
->packet_type
== htonl(PT_NSH
)) ||
4715 (flow
->dl_type
== htons(ETH_TYPE_NSH
))) {
4716 ctx
->wc
->masks
.nsh
.ttl
= 0xff;
4717 if (flow
->nsh
.ttl
> 1) {
4721 xlate_controller_action(ctx
, UINT16_MAX
, OFPR_INVALID_TTL
,
4726 /* Stop processing for current table. */
4727 xlate_report(ctx
, OFT_WARN
, "NSH decrement TTL exception");
4732 compose_set_mpls_ttl_action(struct xlate_ctx
*ctx
, uint8_t ttl
)
4734 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
4735 ctx
->wc
->masks
.mpls_lse
[0] |= htonl(MPLS_TTL_MASK
);
4736 set_mpls_lse_ttl(&ctx
->xin
->flow
.mpls_lse
[0], ttl
);
4741 compose_dec_mpls_ttl_action(struct xlate_ctx
*ctx
)
4743 struct flow
*flow
= &ctx
->xin
->flow
;
4745 if (eth_type_mpls(flow
->dl_type
)) {
4746 uint8_t ttl
= mpls_lse_to_ttl(flow
->mpls_lse
[0]);
4748 ctx
->wc
->masks
.mpls_lse
[0] |= htonl(MPLS_TTL_MASK
);
4751 set_mpls_lse_ttl(&flow
->mpls_lse
[0], ttl
);
4754 xlate_controller_action(ctx
, UINT16_MAX
, OFPR_INVALID_TTL
, 0,
4759 /* Stop processing for current table. */
4760 xlate_report(ctx
, OFT_WARN
, "MPLS decrement TTL exception");
4764 /* Emits an action that outputs to 'port', within 'ctx'.
4766 * 'controller_len' affects only packets sent to an OpenFlow controller. It
4767 * is the maximum number of bytes of the packet to send. UINT16_MAX means to
4768 * send the whole packet (and 0 means to omit the packet entirely).
4770 * 'may_packet_in' determines whether the packet may be sent to an OpenFlow
4771 * controller. If it is false, then the packet is never sent to the OpenFlow
4774 * 'is_last_action' should be true if this output is the last OpenFlow action
4775 * to be processed, which enables certain optimizations.
4777 * 'truncate' should be true if the packet to be output is being truncated,
4778 * which suppresses certain optimizations. */
4780 xlate_output_action(struct xlate_ctx
*ctx
, ofp_port_t port
,
4781 uint16_t controller_len
, bool may_packet_in
,
4782 bool is_last_action
, bool truncate
)
4784 ofp_port_t prev_nf_output_iface
= ctx
->nf_output_iface
;
4786 ctx
->nf_output_iface
= NF_OUT_DROP
;
4790 compose_output_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
, NULL
,
4791 is_last_action
, truncate
);
4794 xlate_table_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
,
4795 0, may_packet_in
, true, false, false,
4802 flood_packets(ctx
, false, is_last_action
);
4805 flood_packets(ctx
, true, is_last_action
);
4807 case OFPP_CONTROLLER
:
4808 xlate_controller_action(ctx
, controller_len
,
4809 (ctx
->in_packet_out
? OFPR_PACKET_OUT
4810 : ctx
->in_group
? OFPR_GROUP
4811 : ctx
->in_action_set
? OFPR_ACTION_SET
4819 if (port
!= ctx
->xin
->flow
.in_port
.ofp_port
) {
4820 compose_output_action(ctx
, port
, NULL
, is_last_action
, truncate
);
4822 xlate_report(ctx
, OFT_WARN
, "skipping output to input port");
4827 if (prev_nf_output_iface
== NF_OUT_FLOOD
) {
4828 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
4829 } else if (ctx
->nf_output_iface
== NF_OUT_DROP
) {
4830 ctx
->nf_output_iface
= prev_nf_output_iface
;
4831 } else if (prev_nf_output_iface
!= NF_OUT_DROP
&&
4832 ctx
->nf_output_iface
!= NF_OUT_FLOOD
) {
4833 ctx
->nf_output_iface
= NF_OUT_MULTI
;
4838 xlate_output_reg_action(struct xlate_ctx
*ctx
,
4839 const struct ofpact_output_reg
*or,
4840 bool is_last_action
)
4842 uint64_t port
= mf_get_subfield(&or->src
, &ctx
->xin
->flow
);
4843 if (port
<= UINT16_MAX
) {
4844 xlate_report(ctx
, OFT_DETAIL
, "output port is %"PRIu64
, port
);
4846 union mf_subvalue value
;
4848 memset(&value
, 0xff, sizeof value
);
4849 mf_write_subfield_flow(&or->src
, &value
, &ctx
->wc
->masks
);
4850 xlate_output_action(ctx
, u16_to_ofp(port
), or->max_len
,
4851 false, is_last_action
, false);
4853 xlate_report(ctx
, OFT_WARN
, "output port %"PRIu64
" is out of range",
4859 xlate_output_trunc_action(struct xlate_ctx
*ctx
,
4860 ofp_port_t port
, uint32_t max_len
,
4861 bool is_last_action
)
4863 bool support_trunc
= ctx
->xbridge
->support
.trunc
;
4864 struct ovs_action_trunc
*trunc
;
4865 char name
[OFP10_MAX_PORT_NAME_LEN
];
4872 case OFPP_CONTROLLER
:
4874 ofputil_port_to_string(port
, NULL
, name
, sizeof name
);
4875 xlate_report(ctx
, OFT_WARN
,
4876 "output_trunc does not support port: %s", name
);
4881 if (port
!= ctx
->xin
->flow
.in_port
.ofp_port
) {
4882 const struct xport
*xport
= get_ofp_port(ctx
->xbridge
, port
);
4884 if (xport
== NULL
|| xport
->odp_port
== ODPP_NONE
) {
4885 /* Since truncate happens at its following output action, if
4886 * the output port is a patch port, the behavior is somehow
4887 * unpredictable. For simplicity, disallow this case. */
4888 ofputil_port_to_string(port
, NULL
, name
, sizeof name
);
4889 xlate_report_error(ctx
, "output_trunc does not support "
4890 "patch port %s", name
);
4894 trunc
= nl_msg_put_unspec_uninit(ctx
->odp_actions
,
4895 OVS_ACTION_ATTR_TRUNC
,
4897 trunc
->max_len
= max_len
;
4898 xlate_output_action(ctx
, port
, 0, false, is_last_action
, true);
4899 if (!support_trunc
) {
4900 ctx
->xout
->slow
|= SLOW_ACTION
;
4903 xlate_report(ctx
, OFT_WARN
, "skipping output to input port");
4910 xlate_enqueue_action(struct xlate_ctx
*ctx
,
4911 const struct ofpact_enqueue
*enqueue
,
4912 bool is_last_action
)
4914 ofp_port_t ofp_port
= enqueue
->port
;
4915 uint32_t queue_id
= enqueue
->queue
;
4916 uint32_t flow_priority
, priority
;
4919 /* Translate queue to priority. */
4920 error
= dpif_queue_to_priority(ctx
->xbridge
->dpif
, queue_id
, &priority
);
4922 /* Fall back to ordinary output action. */
4923 xlate_output_action(ctx
, enqueue
->port
, 0, false,
4924 is_last_action
, false);
4928 /* Check output port. */
4929 if (ofp_port
== OFPP_IN_PORT
) {
4930 ofp_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
4931 } else if (ofp_port
== ctx
->xin
->flow
.in_port
.ofp_port
) {
4935 /* Add datapath actions. */
4936 flow_priority
= ctx
->xin
->flow
.skb_priority
;
4937 ctx
->xin
->flow
.skb_priority
= priority
;
4938 compose_output_action(ctx
, ofp_port
, NULL
, is_last_action
, false);
4939 ctx
->xin
->flow
.skb_priority
= flow_priority
;
4941 /* Update NetFlow output port. */
4942 if (ctx
->nf_output_iface
== NF_OUT_DROP
) {
4943 ctx
->nf_output_iface
= ofp_port
;
4944 } else if (ctx
->nf_output_iface
!= NF_OUT_FLOOD
) {
4945 ctx
->nf_output_iface
= NF_OUT_MULTI
;
4950 xlate_set_queue_action(struct xlate_ctx
*ctx
, uint32_t queue_id
)
4952 uint32_t skb_priority
;
4954 if (!dpif_queue_to_priority(ctx
->xbridge
->dpif
, queue_id
, &skb_priority
)) {
4955 ctx
->xin
->flow
.skb_priority
= skb_priority
;
4957 /* Couldn't translate queue to a priority. Nothing to do. A warning
4958 * has already been logged. */
4963 slave_enabled_cb(ofp_port_t ofp_port
, void *xbridge_
)
4965 const struct xbridge
*xbridge
= xbridge_
;
4976 case OFPP_CONTROLLER
: /* Not supported by the bundle action. */
4979 port
= get_ofp_port(xbridge
, ofp_port
);
4980 return port
? port
->may_enable
: false;
4985 xlate_bundle_action(struct xlate_ctx
*ctx
,
4986 const struct ofpact_bundle
*bundle
,
4987 bool is_last_action
)
4991 port
= bundle_execute(bundle
, &ctx
->xin
->flow
, ctx
->wc
, slave_enabled_cb
,
4992 CONST_CAST(struct xbridge
*, ctx
->xbridge
));
4993 if (bundle
->dst
.field
) {
4994 nxm_reg_load(&bundle
->dst
, ofp_to_u16(port
), &ctx
->xin
->flow
, ctx
->wc
);
4995 xlate_report_subfield(ctx
, &bundle
->dst
);
4997 xlate_output_action(ctx
, port
, 0, false, is_last_action
, false);
5002 xlate_learn_action(struct xlate_ctx
*ctx
, const struct ofpact_learn
*learn
)
5004 learn_mask(learn
, ctx
->wc
);
5006 if (ctx
->xin
->xcache
|| ctx
->xin
->allow_side_effects
) {
5007 uint64_t ofpacts_stub
[1024 / 8];
5008 struct ofputil_flow_mod fm
;
5009 struct ofproto_flow_mod ofm__
, *ofm
;
5010 struct ofpbuf ofpacts
;
5013 if (ctx
->xin
->xcache
) {
5014 ofm
= xmalloc(sizeof *ofm
);
5019 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
5020 learn_execute(learn
, &ctx
->xin
->flow
, &fm
, &ofpacts
);
5021 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
5022 struct ds s
= DS_EMPTY_INITIALIZER
;
5023 ds_put_format(&s
, "table=%"PRIu8
" ", fm
.table_id
);
5024 match_format(&fm
.match
, NULL
, &s
, OFP_DEFAULT_PRIORITY
);
5026 ds_put_format(&s
, " priority=%d", fm
.priority
);
5027 if (fm
.new_cookie
) {
5028 ds_put_format(&s
, " cookie=%#"PRIx64
, ntohll(fm
.new_cookie
));
5030 if (fm
.idle_timeout
!= OFP_FLOW_PERMANENT
) {
5031 ds_put_format(&s
, " idle=%"PRIu16
, fm
.idle_timeout
);
5033 if (fm
.hard_timeout
!= OFP_FLOW_PERMANENT
) {
5034 ds_put_format(&s
, " hard=%"PRIu16
, fm
.hard_timeout
);
5036 if (fm
.flags
& NX_LEARN_F_SEND_FLOW_REM
) {
5037 ds_put_cstr(&s
, " send_flow_rem");
5039 ds_put_cstr(&s
, " actions=");
5040 ofpacts_format(fm
.ofpacts
, fm
.ofpacts_len
, NULL
, &s
);
5041 xlate_report(ctx
, OFT_DETAIL
, "%s", ds_cstr(&s
));
5044 error
= ofproto_dpif_flow_mod_init_for_learn(ctx
->xbridge
->ofproto
,
5046 ofpbuf_uninit(&ofpacts
);
5049 bool success
= true;
5050 if (ctx
->xin
->allow_side_effects
) {
5051 error
= ofproto_flow_mod_learn(ofm
, ctx
->xin
->xcache
!= NULL
,
5052 learn
->limit
, &success
);
5053 } else if (learn
->limit
) {
5055 || ofm
->temp_rule
->state
!= RULE_INSERTED
) {
5056 /* The learned rule expired and there are no packets, so
5057 * we cannot learn again. Since the translated actions
5058 * depend on the result of learning, we tell the caller
5059 * that there's no point in caching this result. */
5060 ctx
->xout
->avoid_caching
= true;
5064 if (learn
->flags
& NX_LEARN_F_WRITE_RESULT
) {
5065 nxm_reg_load(&learn
->result_dst
, success
? 1 : 0,
5066 &ctx
->xin
->flow
, ctx
->wc
);
5067 xlate_report_subfield(ctx
, &learn
->result_dst
);
5070 if (success
&& ctx
->xin
->xcache
) {
5071 struct xc_entry
*entry
;
5073 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_LEARN
);
5074 entry
->learn
.ofm
= ofm
;
5075 entry
->learn
.limit
= learn
->limit
;
5078 ofproto_flow_mod_uninit(ofm
);
5081 if (OVS_UNLIKELY(ctx
->xin
->trace
&& !success
)) {
5082 xlate_report(ctx
, OFT_DETAIL
, "Limit exceeded, learn failed");
5086 if (ofm
!= &ofm__
) {
5091 xlate_report_error(ctx
, "LEARN action execution failed (%s).",
5092 ofperr_to_string(error
));
5095 xlate_report(ctx
, OFT_WARN
,
5096 "suppressing side effects, so learn action ignored");
5101 xlate_fin_timeout__(struct rule_dpif
*rule
, uint16_t tcp_flags
,
5102 uint16_t idle_timeout
, uint16_t hard_timeout
)
5104 if (tcp_flags
& (TCP_FIN
| TCP_RST
)) {
5105 ofproto_rule_reduce_timeouts(&rule
->up
, idle_timeout
, hard_timeout
);
5110 xlate_fin_timeout(struct xlate_ctx
*ctx
,
5111 const struct ofpact_fin_timeout
*oft
)
5114 if (ctx
->xin
->allow_side_effects
) {
5115 xlate_fin_timeout__(ctx
->rule
, ctx
->xin
->tcp_flags
,
5116 oft
->fin_idle_timeout
, oft
->fin_hard_timeout
);
5118 if (ctx
->xin
->xcache
) {
5119 struct xc_entry
*entry
;
5121 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_FIN_TIMEOUT
);
5122 /* XC_RULE already holds a reference on the rule, none is taken
5124 entry
->fin
.rule
= ctx
->rule
;
5125 entry
->fin
.idle
= oft
->fin_idle_timeout
;
5126 entry
->fin
.hard
= oft
->fin_hard_timeout
;
5132 xlate_sample_action(struct xlate_ctx
*ctx
,
5133 const struct ofpact_sample
*os
)
5135 odp_port_t output_odp_port
= ODPP_NONE
;
5136 odp_port_t tunnel_out_port
= ODPP_NONE
;
5137 struct dpif_ipfix
*ipfix
= ctx
->xbridge
->ipfix
;
5138 bool emit_set_tunnel
= false;
5140 if (!ipfix
|| ctx
->xin
->flow
.in_port
.ofp_port
== OFPP_NONE
) {
5144 /* Scale the probability from 16-bit to 32-bit while representing
5145 * the same percentage. */
5146 uint32_t probability
= (os
->probability
<< 16) | os
->probability
;
5148 /* If ofp_port in flow sample action is equel to ofp_port,
5149 * this sample action is a input port action. */
5150 if (os
->sampling_port
!= OFPP_NONE
&&
5151 os
->sampling_port
!= ctx
->xin
->flow
.in_port
.ofp_port
) {
5152 output_odp_port
= ofp_port_to_odp_port(ctx
->xbridge
,
5154 if (output_odp_port
== ODPP_NONE
) {
5155 xlate_report_error(ctx
, "can't use unknown port %d in flow sample "
5156 "action", os
->sampling_port
);
5160 if (dpif_ipfix_get_flow_exporter_tunnel_sampling(ipfix
,
5161 os
->collector_set_id
)
5162 && dpif_ipfix_is_tunnel_port(ipfix
, output_odp_port
)) {
5163 tunnel_out_port
= output_odp_port
;
5164 emit_set_tunnel
= true;
5168 xlate_commit_actions(ctx
);
5169 /* If 'emit_set_tunnel', sample(sampling_port=1) would translate
5170 * into datapath sample action set(tunnel(...)), sample(...) and
5171 * it is used for sampling egress tunnel information. */
5172 if (emit_set_tunnel
) {
5173 const struct xport
*xport
= get_ofp_port(ctx
->xbridge
,
5176 if (xport
&& xport
->is_tunnel
) {
5177 struct flow
*flow
= &ctx
->xin
->flow
;
5178 tnl_port_send(xport
->ofport
, flow
, ctx
->wc
);
5179 if (!ovs_native_tunneling_is_on(ctx
->xbridge
->ofproto
)) {
5180 struct flow_tnl flow_tnl
= flow
->tunnel
;
5182 commit_odp_tunnel_action(flow
, &ctx
->base_flow
,
5184 flow
->tunnel
= flow_tnl
;
5187 xlate_report_error(ctx
,
5188 "sampling_port:%d should be a tunnel port.",
5193 struct user_action_cookie cookie
= {
5194 .type
= USER_ACTION_COOKIE_FLOW_SAMPLE
,
5195 .ofp_in_port
= ctx
->xin
->flow
.in_port
.ofp_port
,
5196 .ofproto_uuid
= ctx
->xbridge
->ofproto
->uuid
,
5198 .probability
= os
->probability
,
5199 .collector_set_id
= os
->collector_set_id
,
5200 .obs_domain_id
= os
->obs_domain_id
,
5201 .obs_point_id
= os
->obs_point_id
,
5202 .output_odp_port
= output_odp_port
,
5203 .direction
= os
->direction
,
5206 compose_sample_action(ctx
, probability
, &cookie
, tunnel_out_port
, false);
5209 /* Determine if an datapath action translated from the openflow action
5210 * can be reversed by another datapath action.
5212 * Openflow actions that do not emit datapath actions are trivially
5213 * reversible. Reversiblity of other actions depends on nature of
5214 * action and their translation. */
5216 reversible_actions(const struct ofpact
*ofpacts
, size_t ofpacts_len
)
5218 const struct ofpact
*a
;
5220 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
5223 case OFPACT_CLEAR_ACTIONS
:
5225 case OFPACT_CONJUNCTION
:
5226 case OFPACT_CONTROLLER
:
5227 case OFPACT_CT_CLEAR
:
5228 case OFPACT_DEBUG_RECIRC
:
5229 case OFPACT_DEBUG_SLOW
:
5230 case OFPACT_DEC_MPLS_TTL
:
5231 case OFPACT_DEC_TTL
:
5232 case OFPACT_ENQUEUE
:
5234 case OFPACT_FIN_TIMEOUT
:
5235 case OFPACT_GOTO_TABLE
:
5238 case OFPACT_MULTIPATH
:
5241 case OFPACT_OUTPUT_REG
:
5242 case OFPACT_POP_MPLS
:
5243 case OFPACT_POP_QUEUE
:
5244 case OFPACT_PUSH_MPLS
:
5245 case OFPACT_PUSH_VLAN
:
5246 case OFPACT_REG_MOVE
:
5247 case OFPACT_RESUBMIT
:
5249 case OFPACT_SET_ETH_DST
:
5250 case OFPACT_SET_ETH_SRC
:
5251 case OFPACT_SET_FIELD
:
5252 case OFPACT_SET_IP_DSCP
:
5253 case OFPACT_SET_IP_ECN
:
5254 case OFPACT_SET_IP_TTL
:
5255 case OFPACT_SET_IPV4_DST
:
5256 case OFPACT_SET_IPV4_SRC
:
5257 case OFPACT_SET_L4_DST_PORT
:
5258 case OFPACT_SET_L4_SRC_PORT
:
5259 case OFPACT_SET_MPLS_LABEL
:
5260 case OFPACT_SET_MPLS_TC
:
5261 case OFPACT_SET_MPLS_TTL
:
5262 case OFPACT_SET_QUEUE
:
5263 case OFPACT_SET_TUNNEL
:
5264 case OFPACT_SET_VLAN_PCP
:
5265 case OFPACT_SET_VLAN_VID
:
5266 case OFPACT_STACK_POP
:
5267 case OFPACT_STACK_PUSH
:
5268 case OFPACT_STRIP_VLAN
:
5269 case OFPACT_UNROLL_XLATE
:
5270 case OFPACT_WRITE_ACTIONS
:
5271 case OFPACT_WRITE_METADATA
:
5277 case OFPACT_OUTPUT_TRUNC
:
5280 case OFPACT_DEC_NSH_TTL
:
5288 clone_xlate_actions(const struct ofpact
*actions
, size_t actions_len
,
5289 struct xlate_ctx
*ctx
, bool is_last_action
)
5291 struct ofpbuf old_stack
= ctx
->stack
;
5292 union mf_subvalue new_stack
[1024 / sizeof(union mf_subvalue
)];
5293 ofpbuf_use_stub(&ctx
->stack
, new_stack
, sizeof new_stack
);
5294 ofpbuf_put(&ctx
->stack
, old_stack
.data
, old_stack
.size
);
5296 struct ofpbuf old_action_set
= ctx
->action_set
;
5297 uint64_t actset_stub
[1024 / 8];
5298 ofpbuf_use_stub(&ctx
->action_set
, actset_stub
, sizeof actset_stub
);
5299 ofpbuf_put(&ctx
->action_set
, old_action_set
.data
, old_action_set
.size
);
5301 size_t offset
, ac_offset
;
5302 struct flow old_flow
= ctx
->xin
->flow
;
5304 if (reversible_actions(actions
, actions_len
) || is_last_action
) {
5305 old_flow
= ctx
->xin
->flow
;
5306 do_xlate_actions(actions
, actions_len
, ctx
, is_last_action
);
5307 if (!ctx
->freezing
) {
5308 xlate_action_set(ctx
);
5310 if (ctx
->freezing
) {
5311 finish_freezing(ctx
);
5316 /* Commit datapath actions before emitting the clone action to
5317 * avoid emitting those actions twice. Once inside
5318 * the clone, another time for the action after clone. */
5319 xlate_commit_actions(ctx
);
5320 struct flow old_base
= ctx
->base_flow
;
5321 bool old_was_mpls
= ctx
->was_mpls
;
5322 bool old_conntracked
= ctx
->conntracked
;
5324 /* The actions are not reversible, a datapath clone action is
5325 * required to encode the translation. Select the clone action
5326 * based on datapath capabilities. */
5327 if (ctx
->xbridge
->support
.clone
) { /* Use clone action */
5328 /* Use clone action as datapath clone. */
5329 offset
= nl_msg_start_nested(ctx
->odp_actions
, OVS_ACTION_ATTR_CLONE
);
5330 do_xlate_actions(actions
, actions_len
, ctx
, true);
5331 if (!ctx
->freezing
) {
5332 xlate_action_set(ctx
);
5334 if (ctx
->freezing
) {
5335 finish_freezing(ctx
);
5337 nl_msg_end_non_empty_nested(ctx
->odp_actions
, offset
);
5341 if (ctx
->xbridge
->support
.sample_nesting
> 3) {
5342 /* Use sample action as datapath clone. */
5343 offset
= nl_msg_start_nested(ctx
->odp_actions
, OVS_ACTION_ATTR_SAMPLE
);
5344 ac_offset
= nl_msg_start_nested(ctx
->odp_actions
,
5345 OVS_SAMPLE_ATTR_ACTIONS
);
5346 do_xlate_actions(actions
, actions_len
, ctx
, true);
5347 if (!ctx
->freezing
) {
5348 xlate_action_set(ctx
);
5350 if (ctx
->freezing
) {
5351 finish_freezing(ctx
);
5353 if (nl_msg_end_non_empty_nested(ctx
->odp_actions
, ac_offset
)) {
5354 nl_msg_cancel_nested(ctx
->odp_actions
, offset
);
5356 nl_msg_put_u32(ctx
->odp_actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
5357 UINT32_MAX
); /* 100% probability. */
5358 nl_msg_end_nested(ctx
->odp_actions
, offset
);
5363 /* Datapath does not support clone, skip xlate 'oc' and
5364 * report an error */
5365 xlate_report_error(ctx
, "Failed to compose clone action");
5368 /* The clone's conntrack execution should have no effect on the original
5370 ctx
->conntracked
= old_conntracked
;
5372 /* Popping MPLS from the clone should have no effect on the original
5374 ctx
->was_mpls
= old_was_mpls
;
5376 /* Restore the 'base_flow' for the next action. */
5377 ctx
->base_flow
= old_base
;
5380 ofpbuf_uninit(&ctx
->action_set
);
5381 ctx
->action_set
= old_action_set
;
5382 ofpbuf_uninit(&ctx
->stack
);
5383 ctx
->stack
= old_stack
;
5384 ctx
->xin
->flow
= old_flow
;
5388 compose_clone(struct xlate_ctx
*ctx
, const struct ofpact_nest
*oc
,
5389 bool is_last_action
)
5391 size_t oc_actions_len
= ofpact_nest_get_action_len(oc
);
5393 clone_xlate_actions(oc
->actions
, oc_actions_len
, ctx
, is_last_action
);
5397 xlate_meter_action(struct xlate_ctx
*ctx
, const struct ofpact_meter
*meter
)
5399 if (meter
->provider_meter_id
!= UINT32_MAX
) {
5400 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_METER
,
5401 meter
->provider_meter_id
);
5406 may_receive(const struct xport
*xport
, struct xlate_ctx
*ctx
)
5408 if (xport
->config
& (is_stp(&ctx
->xin
->flow
)
5409 ? OFPUTIL_PC_NO_RECV_STP
5410 : OFPUTIL_PC_NO_RECV
)) {
5414 /* Only drop packets here if both forwarding and learning are
5415 * disabled. If just learning is enabled, we need to have
5416 * OFPP_NORMAL and the learning action have a look at the packet
5417 * before we can drop it. */
5418 if ((!xport_stp_forward_state(xport
) && !xport_stp_learn_state(xport
)) ||
5419 (!xport_rstp_forward_state(xport
) && !xport_rstp_learn_state(xport
))) {
5427 xlate_write_actions__(struct xlate_ctx
*ctx
,
5428 const struct ofpact
*ofpacts
, size_t ofpacts_len
)
5430 /* Maintain actset_output depending on the contents of the action set:
5432 * - OFPP_UNSET, if there is no "output" action.
5434 * - The output port, if there is an "output" action and no "group"
5437 * - OFPP_UNSET, if there is a "group" action.
5439 if (!ctx
->action_set_has_group
) {
5440 const struct ofpact
*a
;
5441 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
5442 if (a
->type
== OFPACT_OUTPUT
) {
5443 ctx
->xin
->flow
.actset_output
= ofpact_get_OUTPUT(a
)->port
;
5444 } else if (a
->type
== OFPACT_GROUP
) {
5445 ctx
->xin
->flow
.actset_output
= OFPP_UNSET
;
5446 ctx
->action_set_has_group
= true;
5452 ofpbuf_put(&ctx
->action_set
, ofpacts
, ofpacts_len
);
5456 xlate_write_actions(struct xlate_ctx
*ctx
, const struct ofpact_nest
*a
)
5458 xlate_write_actions__(ctx
, a
->actions
, ofpact_nest_get_action_len(a
));
5462 xlate_action_set(struct xlate_ctx
*ctx
)
5464 uint64_t action_list_stub
[1024 / 8];
5465 struct ofpbuf action_list
= OFPBUF_STUB_INITIALIZER(action_list_stub
);
5466 ofpacts_execute_action_set(&action_list
, &ctx
->action_set
);
5467 /* Clear the action set, as it is not needed any more. */
5468 ofpbuf_clear(&ctx
->action_set
);
5469 if (action_list
.size
) {
5470 ctx
->in_action_set
= true;
5472 struct ovs_list
*old_trace
= ctx
->xin
->trace
;
5473 ctx
->xin
->trace
= xlate_report(ctx
, OFT_TABLE
,
5474 "--. Executing action set:");
5475 do_xlate_actions(action_list
.data
, action_list
.size
, ctx
, true);
5476 ctx
->xin
->trace
= old_trace
;
5478 ctx
->in_action_set
= false;
5480 ofpbuf_uninit(&action_list
);
5484 freeze_put_unroll_xlate(struct xlate_ctx
*ctx
)
5486 struct ofpact_unroll_xlate
*unroll
= ctx
->frozen_actions
.header
;
5488 /* Restore the table_id and rule cookie for a potential PACKET
5491 (ctx
->table_id
!= unroll
->rule_table_id
5492 || ctx
->rule_cookie
!= unroll
->rule_cookie
)) {
5493 unroll
= ofpact_put_UNROLL_XLATE(&ctx
->frozen_actions
);
5494 unroll
->rule_table_id
= ctx
->table_id
;
5495 unroll
->rule_cookie
= ctx
->rule_cookie
;
5496 ctx
->frozen_actions
.header
= unroll
;
5501 /* Copy actions 'a' through 'end' to ctx->frozen_actions, which will be
5502 * executed after thawing. Inserts an UNROLL_XLATE action, if none is already
5503 * present, before any action that may depend on the current table ID or flow
5506 freeze_unroll_actions(const struct ofpact
*a
, const struct ofpact
*end
,
5507 struct xlate_ctx
*ctx
)
5509 for (; a
< end
; a
= ofpact_next(a
)) {
5511 case OFPACT_OUTPUT_REG
:
5512 case OFPACT_OUTPUT_TRUNC
:
5515 case OFPACT_CONTROLLER
:
5516 case OFPACT_DEC_MPLS_TTL
:
5517 case OFPACT_DEC_NSH_TTL
:
5518 case OFPACT_DEC_TTL
:
5519 /* These actions may generate asynchronous messages, which include
5520 * table ID and flow cookie information. */
5521 freeze_put_unroll_xlate(ctx
);
5524 case OFPACT_RESUBMIT
:
5525 if (ofpact_get_RESUBMIT(a
)->table_id
== 0xff) {
5526 /* This resubmit action is relative to the current table, so we
5527 * need to track what table that is.*/
5528 freeze_put_unroll_xlate(ctx
);
5532 case OFPACT_SET_TUNNEL
:
5533 case OFPACT_REG_MOVE
:
5534 case OFPACT_SET_FIELD
:
5535 case OFPACT_STACK_PUSH
:
5536 case OFPACT_STACK_POP
:
5538 case OFPACT_WRITE_METADATA
:
5539 case OFPACT_GOTO_TABLE
:
5540 case OFPACT_ENQUEUE
:
5541 case OFPACT_SET_VLAN_VID
:
5542 case OFPACT_SET_VLAN_PCP
:
5543 case OFPACT_STRIP_VLAN
:
5544 case OFPACT_PUSH_VLAN
:
5545 case OFPACT_SET_ETH_SRC
:
5546 case OFPACT_SET_ETH_DST
:
5547 case OFPACT_SET_IPV4_SRC
:
5548 case OFPACT_SET_IPV4_DST
:
5549 case OFPACT_SET_IP_DSCP
:
5550 case OFPACT_SET_IP_ECN
:
5551 case OFPACT_SET_IP_TTL
:
5552 case OFPACT_SET_L4_SRC_PORT
:
5553 case OFPACT_SET_L4_DST_PORT
:
5554 case OFPACT_SET_QUEUE
:
5555 case OFPACT_POP_QUEUE
:
5556 case OFPACT_PUSH_MPLS
:
5557 case OFPACT_POP_MPLS
:
5558 case OFPACT_SET_MPLS_LABEL
:
5559 case OFPACT_SET_MPLS_TC
:
5560 case OFPACT_SET_MPLS_TTL
:
5561 case OFPACT_MULTIPATH
:
5564 case OFPACT_UNROLL_XLATE
:
5565 case OFPACT_FIN_TIMEOUT
:
5566 case OFPACT_CLEAR_ACTIONS
:
5567 case OFPACT_WRITE_ACTIONS
:
5573 case OFPACT_DEBUG_RECIRC
:
5574 case OFPACT_DEBUG_SLOW
:
5576 case OFPACT_CT_CLEAR
:
5578 /* These may not generate PACKET INs. */
5582 case OFPACT_CONJUNCTION
:
5583 /* These need not be copied for restoration. */
5586 /* Copy the action over. */
5587 ofpbuf_put(&ctx
->frozen_actions
, a
, OFPACT_ALIGN(a
->len
));
5592 put_ct_mark(const struct flow
*flow
, struct ofpbuf
*odp_actions
,
5593 struct flow_wildcards
*wc
)
5595 if (wc
->masks
.ct_mark
) {
5601 odp_ct_mark
= nl_msg_put_unspec_uninit(odp_actions
, OVS_CT_ATTR_MARK
,
5602 sizeof(*odp_ct_mark
));
5603 odp_ct_mark
->key
= flow
->ct_mark
& wc
->masks
.ct_mark
;
5604 odp_ct_mark
->mask
= wc
->masks
.ct_mark
;
5609 put_ct_label(const struct flow
*flow
, struct ofpbuf
*odp_actions
,
5610 struct flow_wildcards
*wc
)
5612 if (!ovs_u128_is_zero(wc
->masks
.ct_label
)) {
5618 odp_ct_label
.key
= ovs_u128_and(flow
->ct_label
, wc
->masks
.ct_label
);
5619 odp_ct_label
.mask
= wc
->masks
.ct_label
;
5620 nl_msg_put_unspec(odp_actions
, OVS_CT_ATTR_LABELS
,
5621 &odp_ct_label
, sizeof odp_ct_label
);
5626 put_ct_helper(struct xlate_ctx
*ctx
,
5627 struct ofpbuf
*odp_actions
, struct ofpact_conntrack
*ofc
)
5632 nl_msg_put_string(odp_actions
, OVS_CT_ATTR_HELPER
, "ftp");
5635 nl_msg_put_string(odp_actions
, OVS_CT_ATTR_HELPER
, "tftp");
5638 xlate_report_error(ctx
, "cannot serialize ct_helper %d", ofc
->alg
);
5645 put_ct_nat(struct xlate_ctx
*ctx
)
5647 struct ofpact_nat
*ofn
= ctx
->ct_nat_action
;
5654 nat_offset
= nl_msg_start_nested(ctx
->odp_actions
, OVS_CT_ATTR_NAT
);
5655 if (ofn
->flags
& NX_NAT_F_SRC
|| ofn
->flags
& NX_NAT_F_DST
) {
5656 nl_msg_put_flag(ctx
->odp_actions
, ofn
->flags
& NX_NAT_F_SRC
5657 ? OVS_NAT_ATTR_SRC
: OVS_NAT_ATTR_DST
);
5658 if (ofn
->flags
& NX_NAT_F_PERSISTENT
) {
5659 nl_msg_put_flag(ctx
->odp_actions
, OVS_NAT_ATTR_PERSISTENT
);
5661 if (ofn
->flags
& NX_NAT_F_PROTO_HASH
) {
5662 nl_msg_put_flag(ctx
->odp_actions
, OVS_NAT_ATTR_PROTO_HASH
);
5663 } else if (ofn
->flags
& NX_NAT_F_PROTO_RANDOM
) {
5664 nl_msg_put_flag(ctx
->odp_actions
, OVS_NAT_ATTR_PROTO_RANDOM
);
5666 if (ofn
->range_af
== AF_INET
) {
5667 nl_msg_put_be32(ctx
->odp_actions
, OVS_NAT_ATTR_IP_MIN
,
5668 ofn
->range
.addr
.ipv4
.min
);
5669 if (ofn
->range
.addr
.ipv4
.max
&&
5670 (ntohl(ofn
->range
.addr
.ipv4
.max
)
5671 > ntohl(ofn
->range
.addr
.ipv4
.min
))) {
5672 nl_msg_put_be32(ctx
->odp_actions
, OVS_NAT_ATTR_IP_MAX
,
5673 ofn
->range
.addr
.ipv4
.max
);
5675 } else if (ofn
->range_af
== AF_INET6
) {
5676 nl_msg_put_unspec(ctx
->odp_actions
, OVS_NAT_ATTR_IP_MIN
,
5677 &ofn
->range
.addr
.ipv6
.min
,
5678 sizeof ofn
->range
.addr
.ipv6
.min
);
5679 if (!ipv6_mask_is_any(&ofn
->range
.addr
.ipv6
.max
) &&
5680 memcmp(&ofn
->range
.addr
.ipv6
.max
, &ofn
->range
.addr
.ipv6
.min
,
5681 sizeof ofn
->range
.addr
.ipv6
.max
) > 0) {
5682 nl_msg_put_unspec(ctx
->odp_actions
, OVS_NAT_ATTR_IP_MAX
,
5683 &ofn
->range
.addr
.ipv6
.max
,
5684 sizeof ofn
->range
.addr
.ipv6
.max
);
5687 if (ofn
->range_af
!= AF_UNSPEC
&& ofn
->range
.proto
.min
) {
5688 nl_msg_put_u16(ctx
->odp_actions
, OVS_NAT_ATTR_PROTO_MIN
,
5689 ofn
->range
.proto
.min
);
5690 if (ofn
->range
.proto
.max
&&
5691 ofn
->range
.proto
.max
> ofn
->range
.proto
.min
) {
5692 nl_msg_put_u16(ctx
->odp_actions
, OVS_NAT_ATTR_PROTO_MAX
,
5693 ofn
->range
.proto
.max
);
5697 nl_msg_end_nested(ctx
->odp_actions
, nat_offset
);
5701 compose_conntrack_action(struct xlate_ctx
*ctx
, struct ofpact_conntrack
*ofc
,
5702 bool is_last_action
)
5704 ovs_u128 old_ct_label_mask
= ctx
->wc
->masks
.ct_label
;
5705 uint32_t old_ct_mark_mask
= ctx
->wc
->masks
.ct_mark
;
5709 /* Ensure that any prior actions are applied before composing the new
5710 * conntrack action. */
5711 xlate_commit_actions(ctx
);
5713 /* Process nested actions first, to populate the key. */
5714 ctx
->ct_nat_action
= NULL
;
5715 ctx
->wc
->masks
.ct_mark
= 0;
5716 ctx
->wc
->masks
.ct_label
= OVS_U128_ZERO
;
5717 do_xlate_actions(ofc
->actions
, ofpact_ct_get_action_len(ofc
), ctx
,
5720 if (ofc
->zone_src
.field
) {
5721 zone
= mf_get_subfield(&ofc
->zone_src
, &ctx
->xin
->flow
);
5723 zone
= ofc
->zone_imm
;
5726 ct_offset
= nl_msg_start_nested(ctx
->odp_actions
, OVS_ACTION_ATTR_CT
);
5727 if (ofc
->flags
& NX_CT_F_COMMIT
) {
5728 nl_msg_put_flag(ctx
->odp_actions
, ofc
->flags
& NX_CT_F_FORCE
?
5729 OVS_CT_ATTR_FORCE_COMMIT
: OVS_CT_ATTR_COMMIT
);
5730 if (ctx
->xbridge
->support
.ct_eventmask
) {
5731 nl_msg_put_u32(ctx
->odp_actions
, OVS_CT_ATTR_EVENTMASK
,
5732 OVS_CT_EVENTMASK_DEFAULT
);
5735 nl_msg_put_u16(ctx
->odp_actions
, OVS_CT_ATTR_ZONE
, zone
);
5736 put_ct_mark(&ctx
->xin
->flow
, ctx
->odp_actions
, ctx
->wc
);
5737 put_ct_label(&ctx
->xin
->flow
, ctx
->odp_actions
, ctx
->wc
);
5738 put_ct_helper(ctx
, ctx
->odp_actions
, ofc
);
5740 ctx
->ct_nat_action
= NULL
;
5741 nl_msg_end_nested(ctx
->odp_actions
, ct_offset
);
5743 ctx
->wc
->masks
.ct_mark
= old_ct_mark_mask
;
5744 ctx
->wc
->masks
.ct_label
= old_ct_label_mask
;
5746 if (ofc
->recirc_table
!= NX_CT_RECIRC_NONE
) {
5747 ctx
->conntracked
= true;
5748 compose_recirculate_and_fork(ctx
, ofc
->recirc_table
, zone
);
5751 /* The ct_* fields are only available in the scope of the 'recirc_table'
5753 flow_clear_conntrack(&ctx
->xin
->flow
);
5754 ctx
->conntracked
= false;
5758 compose_ct_clear_action(struct xlate_ctx
*ctx
)
5760 clear_conntrack(ctx
);
5761 /* This action originally existed without dpif support. So to preserve
5762 * compatibility, only append it if the dpif supports it. */
5763 if (ctx
->xbridge
->support
.ct_clear
) {
5764 nl_msg_put_flag(ctx
->odp_actions
, OVS_ACTION_ATTR_CT_CLEAR
);
5769 rewrite_flow_encap_ethernet(struct xlate_ctx
*ctx
,
5771 struct flow_wildcards
*wc
)
5773 wc
->masks
.packet_type
= OVS_BE32_MAX
;
5774 if (pt_ns(flow
->packet_type
) == OFPHTN_ETHERTYPE
) {
5775 /* Only adjust the packet_type and zero the dummy Ethernet addresses. */
5776 ovs_be16 ethertype
= pt_ns_type_be(flow
->packet_type
);
5777 flow
->packet_type
= htonl(PT_ETH
);
5778 flow
->dl_src
= eth_addr_zero
;
5779 flow
->dl_dst
= eth_addr_zero
;
5780 flow
->dl_type
= ethertype
;
5782 /* Error handling: drop packet. */
5783 xlate_report_debug(ctx
, OFT_ACTION
,
5784 "Dropping packet as encap(ethernet) is not "
5785 "supported for packet type ethernet.");
5786 ctx
->error
= XLATE_UNSUPPORTED_PACKET_TYPE
;
5790 /* For an MD2 NSH header returns a pointer to an ofpbuf with the encoded
5791 * MD2 TLVs provided as encap properties to the encap operation. This
5792 * will be stored as encap_data in the ctx and copied into the push_nsh
5793 * action at the next commit. */
5794 static struct ofpbuf
*
5795 rewrite_flow_push_nsh(struct xlate_ctx
*ctx
,
5796 const struct ofpact_encap
*encap
,
5798 struct flow_wildcards
*wc
)
5800 ovs_be32 packet_type
= flow
->packet_type
;
5801 const char *ptr
= (char *) encap
->props
;
5802 struct ofpbuf
*buf
= ofpbuf_new(NSH_CTX_HDRS_MAX_LEN
);
5803 uint8_t md_type
= NSH_M_TYPE1
;
5807 /* Scan the optional NSH encap TLV properties, if any. */
5808 for (i
= 0; i
< encap
->n_props
; i
++) {
5809 struct ofpact_ed_prop
*prop_ptr
=
5810 ALIGNED_CAST(struct ofpact_ed_prop
*, ptr
);
5811 if (prop_ptr
->prop_class
== OFPPPC_NSH
) {
5812 switch (prop_ptr
->type
) {
5813 case OFPPPT_PROP_NSH_MDTYPE
: {
5814 struct ofpact_ed_prop_nsh_md_type
*prop_md_type
=
5815 ALIGNED_CAST(struct ofpact_ed_prop_nsh_md_type
*,
5817 md_type
= prop_md_type
->md_type
;
5820 case OFPPPT_PROP_NSH_TLV
: {
5821 struct ofpact_ed_prop_nsh_tlv
*tlv_prop
=
5822 ALIGNED_CAST(struct ofpact_ed_prop_nsh_tlv
*,
5824 struct nsh_md2_tlv
*md2_ctx
=
5825 ofpbuf_put_uninit(buf
, sizeof(*md2_ctx
));
5826 md2_ctx
->md_class
= tlv_prop
->tlv_class
;
5827 md2_ctx
->type
= tlv_prop
->tlv_type
;
5828 md2_ctx
->length
= tlv_prop
->tlv_len
;
5829 size_t len
= ROUND_UP(md2_ctx
->length
, 4);
5830 size_t padding
= len
- md2_ctx
->length
;
5831 ofpbuf_put(buf
, tlv_prop
->data
, md2_ctx
->length
);
5832 ofpbuf_put_zeros(buf
, padding
);
5836 /* No other NSH encap properties defined yet. */
5840 ptr
+= ROUND_UP(prop_ptr
->len
, 8);
5842 if (buf
->size
== 0 || buf
->size
> NSH_CTX_HDRS_MAX_LEN
) {
5847 /* Determine the Next Protocol field for NSH header. */
5848 switch (ntohl(packet_type
)) {
5850 np
= NSH_P_ETHERNET
;
5862 /* Error handling: drop packet. */
5863 xlate_report_debug(ctx
, OFT_ACTION
,
5864 "Dropping packet as encap(nsh) is not "
5865 "supported for packet type (%d,0x%x)",
5866 pt_ns(packet_type
), pt_ns_type(packet_type
));
5867 ctx
->error
= XLATE_UNSUPPORTED_PACKET_TYPE
;
5870 /* Note that we have matched on packet_type! */
5871 wc
->masks
.packet_type
= OVS_BE32_MAX
;
5873 /* Reset all current flow packet headers. */
5874 memset(&flow
->dl_dst
, 0,
5875 sizeof(struct flow
) - offsetof(struct flow
, dl_dst
));
5877 /* Populate the flow with the new NSH header. */
5878 flow
->packet_type
= htonl(PT_NSH
);
5879 flow
->dl_type
= htons(ETH_TYPE_NSH
);
5880 flow
->nsh
.flags
= 0;
5883 flow
->nsh
.path_hdr
= htonl(255);
5885 if (md_type
== NSH_M_TYPE1
) {
5886 flow
->nsh
.mdtype
= NSH_M_TYPE1
;
5887 memset(flow
->nsh
.context
, 0, sizeof flow
->nsh
.context
);
5889 /* Drop any MD2 context TLVs. */
5893 } else if (md_type
== NSH_M_TYPE2
) {
5894 flow
->nsh
.mdtype
= NSH_M_TYPE2
;
5896 flow
->nsh
.mdtype
&= NSH_MDTYPE_MASK
;
5902 xlate_generic_encap_action(struct xlate_ctx
*ctx
,
5903 const struct ofpact_encap
*encap
)
5905 struct flow
*flow
= &ctx
->xin
->flow
;
5906 struct flow_wildcards
*wc
= ctx
->wc
;
5907 struct ofpbuf
*encap_data
= NULL
;
5909 /* Ensure that any pending actions on the inner packet are applied before
5910 * rewriting the flow */
5911 xlate_commit_actions(ctx
);
5913 /* Rewrite the flow to reflect the effect of pushing the new encap header. */
5914 switch (ntohl(encap
->new_pkt_type
)) {
5916 rewrite_flow_encap_ethernet(ctx
, flow
, wc
);
5919 encap_data
= rewrite_flow_push_nsh(ctx
, encap
, flow
, wc
);
5922 /* New packet type was checked during decoding. */
5927 /* The actual encap datapath action will be generated at next commit. */
5928 ctx
->pending_encap
= true;
5929 ctx
->encap_data
= encap_data
;
5933 /* Returns true if packet must be recirculated after decapsulation. */
5935 xlate_generic_decap_action(struct xlate_ctx
*ctx
,
5936 const struct ofpact_decap
*decap OVS_UNUSED
)
5938 struct flow
*flow
= &ctx
->xin
->flow
;
5940 /* Ensure that any pending actions on the current packet are applied
5941 * before generating the decap action. */
5942 xlate_commit_actions(ctx
);
5944 /* We assume for now that the new_pkt_type is PT_USE_NEXT_PROTO. */
5945 switch (ntohl(flow
->packet_type
)) {
5947 if (flow
->vlans
[0].tci
& htons(VLAN_CFI
)) {
5948 /* Error handling: drop packet. */
5949 xlate_report_debug(ctx
, OFT_ACTION
, "Dropping packet, cannot "
5950 "decap Ethernet if VLAN is present.");
5951 ctx
->error
= XLATE_UNSUPPORTED_PACKET_TYPE
;
5953 /* Just change the packet_type.
5954 * Delay generating pop_eth to the next commit. */
5955 flow
->packet_type
= htonl(PACKET_TYPE(OFPHTN_ETHERTYPE
,
5956 ntohs(flow
->dl_type
)));
5957 ctx
->wc
->masks
.dl_type
= OVS_BE16_MAX
;
5961 /* The pop_nsh action is generated at the commit executed as
5962 * part of freezing the ctx for recirculation. Here we just set
5963 * the new packet type based on the NSH next protocol field. */
5964 switch (flow
->nsh
.np
) {
5965 case NSH_P_ETHERNET
:
5966 flow
->packet_type
= htonl(PT_ETH
);
5969 flow
->packet_type
= htonl(PT_IPV4
);
5972 flow
->packet_type
= htonl(PT_IPV6
);
5975 flow
->packet_type
= htonl(PT_NSH
);
5978 /* Error handling: drop packet. */
5979 xlate_report_debug(ctx
, OFT_ACTION
,
5980 "Dropping packet as NSH next protocol %d "
5981 "is not supported", flow
->nsh
.np
);
5982 ctx
->error
= XLATE_UNSUPPORTED_PACKET_TYPE
;
5986 ctx
->wc
->masks
.nsh
.np
= UINT8_MAX
;
5987 ctx
->pending_decap
= true;
5988 /* Trigger recirculation. */
5991 /* Error handling: drop packet. */
5994 "Dropping packet as the decap() does not support "
5995 "packet type (%d,0x%x)",
5996 pt_ns(flow
->packet_type
), pt_ns_type(flow
->packet_type
));
5997 ctx
->error
= XLATE_UNSUPPORTED_PACKET_TYPE
;
6003 recirc_for_mpls(const struct ofpact
*a
, struct xlate_ctx
*ctx
)
6005 /* No need to recirculate if already exiting. */
6010 /* Do not consider recirculating unless the packet was previously MPLS. */
6011 if (!ctx
->was_mpls
) {
6015 /* Special case these actions, only recirculating if necessary.
6016 * This avoids the overhead of recirculation in common use-cases.
6020 /* Output actions do not require recirculation. */
6022 case OFPACT_OUTPUT_TRUNC
:
6023 case OFPACT_ENQUEUE
:
6024 case OFPACT_OUTPUT_REG
:
6025 /* Set actions that don't touch L3+ fields do not require recirculation. */
6026 case OFPACT_SET_VLAN_VID
:
6027 case OFPACT_SET_VLAN_PCP
:
6028 case OFPACT_SET_ETH_SRC
:
6029 case OFPACT_SET_ETH_DST
:
6030 case OFPACT_SET_TUNNEL
:
6031 case OFPACT_SET_QUEUE
:
6032 /* If actions of a group require recirculation that can be detected
6033 * when translating them. */
6037 /* Set field that don't touch L3+ fields don't require recirculation. */
6038 case OFPACT_SET_FIELD
:
6039 if (mf_is_l3_or_higher(ofpact_get_SET_FIELD(a
)->field
)) {
6044 /* For simplicity, recirculate in all other cases. */
6045 case OFPACT_CONTROLLER
:
6047 case OFPACT_STRIP_VLAN
:
6048 case OFPACT_PUSH_VLAN
:
6049 case OFPACT_SET_IPV4_SRC
:
6050 case OFPACT_SET_IPV4_DST
:
6051 case OFPACT_SET_IP_DSCP
:
6052 case OFPACT_SET_IP_ECN
:
6053 case OFPACT_SET_IP_TTL
:
6054 case OFPACT_SET_L4_SRC_PORT
:
6055 case OFPACT_SET_L4_DST_PORT
:
6056 case OFPACT_REG_MOVE
:
6057 case OFPACT_STACK_PUSH
:
6058 case OFPACT_STACK_POP
:
6059 case OFPACT_DEC_TTL
:
6060 case OFPACT_SET_MPLS_LABEL
:
6061 case OFPACT_SET_MPLS_TC
:
6062 case OFPACT_SET_MPLS_TTL
:
6063 case OFPACT_DEC_MPLS_TTL
:
6064 case OFPACT_PUSH_MPLS
:
6065 case OFPACT_POP_MPLS
:
6066 case OFPACT_POP_QUEUE
:
6067 case OFPACT_FIN_TIMEOUT
:
6068 case OFPACT_RESUBMIT
:
6070 case OFPACT_CONJUNCTION
:
6071 case OFPACT_MULTIPATH
:
6078 case OFPACT_DEC_NSH_TTL
:
6079 case OFPACT_UNROLL_XLATE
:
6081 case OFPACT_CT_CLEAR
:
6083 case OFPACT_DEBUG_RECIRC
:
6084 case OFPACT_DEBUG_SLOW
:
6086 case OFPACT_CLEAR_ACTIONS
:
6087 case OFPACT_WRITE_ACTIONS
:
6088 case OFPACT_WRITE_METADATA
:
6089 case OFPACT_GOTO_TABLE
:
6095 ctx_trigger_freeze(ctx
);
6099 xlate_ofpact_reg_move(struct xlate_ctx
*ctx
, const struct ofpact_reg_move
*a
)
6101 mf_subfield_copy(&a
->src
, &a
->dst
, &ctx
->xin
->flow
, ctx
->wc
);
6102 xlate_report_subfield(ctx
, &a
->dst
);
6106 xlate_ofpact_stack_pop(struct xlate_ctx
*ctx
, const struct ofpact_stack
*a
)
6108 if (nxm_execute_stack_pop(a
, &ctx
->xin
->flow
, ctx
->wc
, &ctx
->stack
)) {
6109 xlate_report_subfield(ctx
, &a
->subfield
);
6111 xlate_report_error(ctx
, "stack underflow");
6115 /* Restore translation context data that was stored earlier. */
6117 xlate_ofpact_unroll_xlate(struct xlate_ctx
*ctx
,
6118 const struct ofpact_unroll_xlate
*a
)
6120 ctx
->table_id
= a
->rule_table_id
;
6121 ctx
->rule_cookie
= a
->rule_cookie
;
6122 xlate_report(ctx
, OFT_THAW
, "restored state: table=%"PRIu8
", "
6123 "cookie=%#"PRIx64
, a
->rule_table_id
, a
->rule_cookie
);
6127 do_xlate_actions(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
6128 struct xlate_ctx
*ctx
, bool is_last_action
)
6130 struct flow_wildcards
*wc
= ctx
->wc
;
6131 struct flow
*flow
= &ctx
->xin
->flow
;
6132 const struct ofpact
*a
;
6134 if (ovs_native_tunneling_is_on(ctx
->xbridge
->ofproto
)) {
6135 tnl_neigh_snoop(flow
, wc
, ctx
->xbridge
->name
);
6137 /* dl_type already in the mask, not set below. */
6140 xlate_report(ctx
, OFT_ACTION
, "drop");
6144 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
6145 struct ofpact_controller
*controller
;
6146 const struct ofpact_metadata
*metadata
;
6147 const struct ofpact_set_field
*set_field
;
6148 const struct mf_field
*mf
;
6149 bool last
= is_last_action
&& ofpact_last(a
, ofpacts
, ofpacts_len
)
6150 && ctx
->action_set
.size
;
6156 recirc_for_mpls(a
, ctx
);
6159 /* Check if need to store the remaining actions for later
6161 if (ctx
->freezing
) {
6162 freeze_unroll_actions(a
, ofpact_end(ofpacts
, ofpacts_len
),
6168 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
6169 struct ds s
= DS_EMPTY_INITIALIZER
;
6170 ofpacts_format(a
, OFPACT_ALIGN(a
->len
), NULL
, &s
);
6171 xlate_report(ctx
, OFT_ACTION
, "%s", ds_cstr(&s
));
6177 xlate_output_action(ctx
, ofpact_get_OUTPUT(a
)->port
,
6178 ofpact_get_OUTPUT(a
)->max_len
, true, last
,
6183 if (xlate_group_action(ctx
, ofpact_get_GROUP(a
)->group_id
, last
)) {
6184 /* Group could not be found. */
6186 /* XXX: Terminates action list translation, but does not
6187 * terminate the pipeline. */
6192 case OFPACT_CONTROLLER
:
6193 controller
= ofpact_get_CONTROLLER(a
);
6194 if (controller
->pause
) {
6195 ctx
->pause
= controller
;
6196 ctx_trigger_freeze(ctx
);
6199 xlate_controller_action(ctx
, controller
->max_len
,
6201 controller
->controller_id
,
6202 controller
->userdata
,
6203 controller
->userdata_len
);
6207 case OFPACT_ENQUEUE
:
6208 memset(&wc
->masks
.skb_priority
, 0xff,
6209 sizeof wc
->masks
.skb_priority
);
6210 xlate_enqueue_action(ctx
, ofpact_get_ENQUEUE(a
), last
);
6213 case OFPACT_SET_VLAN_VID
:
6214 wc
->masks
.vlans
[0].tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
6215 if (flow
->vlans
[0].tci
& htons(VLAN_CFI
) ||
6216 ofpact_get_SET_VLAN_VID(a
)->push_vlan_if_needed
) {
6217 if (!flow
->vlans
[0].tpid
) {
6218 flow
->vlans
[0].tpid
= htons(ETH_TYPE_VLAN
);
6220 flow
->vlans
[0].tci
&= ~htons(VLAN_VID_MASK
);
6221 flow
->vlans
[0].tci
|=
6222 (htons(ofpact_get_SET_VLAN_VID(a
)->vlan_vid
) |
6227 case OFPACT_SET_VLAN_PCP
:
6228 wc
->masks
.vlans
[0].tci
|= htons(VLAN_PCP_MASK
| VLAN_CFI
);
6229 if (flow
->vlans
[0].tci
& htons(VLAN_CFI
) ||
6230 ofpact_get_SET_VLAN_PCP(a
)->push_vlan_if_needed
) {
6231 if (!flow
->vlans
[0].tpid
) {
6232 flow
->vlans
[0].tpid
= htons(ETH_TYPE_VLAN
);
6234 flow
->vlans
[0].tci
&= ~htons(VLAN_PCP_MASK
);
6235 flow
->vlans
[0].tci
|=
6236 htons((ofpact_get_SET_VLAN_PCP(a
)->vlan_pcp
6237 << VLAN_PCP_SHIFT
) | VLAN_CFI
);
6241 case OFPACT_STRIP_VLAN
:
6242 flow_pop_vlan(flow
, wc
);
6245 case OFPACT_PUSH_VLAN
:
6246 flow_push_vlan_uninit(flow
, wc
);
6247 flow
->vlans
[0].tpid
= ofpact_get_PUSH_VLAN(a
)->ethertype
;
6248 flow
->vlans
[0].tci
= htons(VLAN_CFI
);
6251 case OFPACT_SET_ETH_SRC
:
6252 WC_MASK_FIELD(wc
, dl_src
);
6253 flow
->dl_src
= ofpact_get_SET_ETH_SRC(a
)->mac
;
6256 case OFPACT_SET_ETH_DST
:
6257 WC_MASK_FIELD(wc
, dl_dst
);
6258 flow
->dl_dst
= ofpact_get_SET_ETH_DST(a
)->mac
;
6261 case OFPACT_SET_IPV4_SRC
:
6262 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6263 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
6264 flow
->nw_src
= ofpact_get_SET_IPV4_SRC(a
)->ipv4
;
6268 case OFPACT_SET_IPV4_DST
:
6269 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6270 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
6271 flow
->nw_dst
= ofpact_get_SET_IPV4_DST(a
)->ipv4
;
6275 case OFPACT_SET_IP_DSCP
:
6276 if (is_ip_any(flow
)) {
6277 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
6278 flow
->nw_tos
&= ~IP_DSCP_MASK
;
6279 flow
->nw_tos
|= ofpact_get_SET_IP_DSCP(a
)->dscp
;
6283 case OFPACT_SET_IP_ECN
:
6284 if (is_ip_any(flow
)) {
6285 wc
->masks
.nw_tos
|= IP_ECN_MASK
;
6286 flow
->nw_tos
&= ~IP_ECN_MASK
;
6287 flow
->nw_tos
|= ofpact_get_SET_IP_ECN(a
)->ecn
;
6291 case OFPACT_SET_IP_TTL
:
6292 if (is_ip_any(flow
)) {
6293 wc
->masks
.nw_ttl
= 0xff;
6294 flow
->nw_ttl
= ofpact_get_SET_IP_TTL(a
)->ttl
;
6298 case OFPACT_SET_L4_SRC_PORT
:
6299 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6300 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
6301 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
6302 flow
->tp_src
= htons(ofpact_get_SET_L4_SRC_PORT(a
)->port
);
6306 case OFPACT_SET_L4_DST_PORT
:
6307 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6308 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
6309 memset(&wc
->masks
.tp_dst
, 0xff, sizeof wc
->masks
.tp_dst
);
6310 flow
->tp_dst
= htons(ofpact_get_SET_L4_DST_PORT(a
)->port
);
6314 case OFPACT_RESUBMIT
:
6315 /* Freezing complicates resubmit. Some action in the flow
6316 * entry found by resubmit might trigger freezing. If that
6317 * happens, then we do not want to execute the resubmit again after
6318 * during thawing, so we want to skip back to the head of the loop
6319 * to avoid that, only adding any actions that follow the resubmit
6320 * to the frozen actions.
6322 xlate_ofpact_resubmit(ctx
, ofpact_get_RESUBMIT(a
), last
);
6325 case OFPACT_SET_TUNNEL
:
6326 flow
->tunnel
.tun_id
= htonll(ofpact_get_SET_TUNNEL(a
)->tun_id
);
6329 case OFPACT_SET_QUEUE
:
6330 memset(&wc
->masks
.skb_priority
, 0xff,
6331 sizeof wc
->masks
.skb_priority
);
6332 xlate_set_queue_action(ctx
, ofpact_get_SET_QUEUE(a
)->queue_id
);
6335 case OFPACT_POP_QUEUE
:
6336 memset(&wc
->masks
.skb_priority
, 0xff,
6337 sizeof wc
->masks
.skb_priority
);
6338 if (flow
->skb_priority
!= ctx
->orig_skb_priority
) {
6339 flow
->skb_priority
= ctx
->orig_skb_priority
;
6340 xlate_report(ctx
, OFT_DETAIL
, "queue = %#"PRIx32
,
6341 flow
->skb_priority
);
6345 case OFPACT_REG_MOVE
:
6346 xlate_ofpact_reg_move(ctx
, ofpact_get_REG_MOVE(a
));
6349 case OFPACT_SET_FIELD
:
6350 set_field
= ofpact_get_SET_FIELD(a
);
6351 mf
= set_field
->field
;
6353 /* Set the field only if the packet actually has it. */
6354 if (mf_are_prereqs_ok(mf
, flow
, wc
)) {
6355 mf_mask_field_masked(mf
, ofpact_set_field_mask(set_field
), wc
);
6356 mf_set_flow_value_masked(mf
, set_field
->value
,
6357 ofpact_set_field_mask(set_field
),
6360 xlate_report(ctx
, OFT_WARN
,
6361 "unmet prerequisites for %s, set_field ignored",
6367 case OFPACT_STACK_PUSH
:
6368 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a
), flow
, wc
,
6372 case OFPACT_STACK_POP
:
6373 xlate_ofpact_stack_pop(ctx
, ofpact_get_STACK_POP(a
));
6376 case OFPACT_PUSH_MPLS
:
6377 compose_mpls_push_action(ctx
, ofpact_get_PUSH_MPLS(a
));
6380 case OFPACT_POP_MPLS
:
6381 compose_mpls_pop_action(ctx
, ofpact_get_POP_MPLS(a
)->ethertype
);
6384 case OFPACT_SET_MPLS_LABEL
:
6385 compose_set_mpls_label_action(
6386 ctx
, ofpact_get_SET_MPLS_LABEL(a
)->label
);
6389 case OFPACT_SET_MPLS_TC
:
6390 compose_set_mpls_tc_action(ctx
, ofpact_get_SET_MPLS_TC(a
)->tc
);
6393 case OFPACT_SET_MPLS_TTL
:
6394 compose_set_mpls_ttl_action(ctx
, ofpact_get_SET_MPLS_TTL(a
)->ttl
);
6397 case OFPACT_DEC_MPLS_TTL
:
6398 if (compose_dec_mpls_ttl_action(ctx
)) {
6403 case OFPACT_DEC_NSH_TTL
:
6404 if (compose_dec_nsh_ttl_action(ctx
)) {
6409 case OFPACT_DEC_TTL
:
6410 wc
->masks
.nw_ttl
= 0xff;
6411 if (compose_dec_ttl(ctx
, ofpact_get_DEC_TTL(a
))) {
6417 /* Nothing to do. */
6420 case OFPACT_MULTIPATH
:
6421 multipath_execute(ofpact_get_MULTIPATH(a
), flow
, wc
);
6422 xlate_report_subfield(ctx
, &ofpact_get_MULTIPATH(a
)->dst
);
6426 xlate_bundle_action(ctx
, ofpact_get_BUNDLE(a
), last
);
6429 case OFPACT_OUTPUT_REG
:
6430 xlate_output_reg_action(ctx
, ofpact_get_OUTPUT_REG(a
), last
);
6433 case OFPACT_OUTPUT_TRUNC
:
6434 xlate_output_trunc_action(ctx
, ofpact_get_OUTPUT_TRUNC(a
)->port
,
6435 ofpact_get_OUTPUT_TRUNC(a
)->max_len
, last
);
6439 xlate_learn_action(ctx
, ofpact_get_LEARN(a
));
6442 case OFPACT_CONJUNCTION
:
6443 /* A flow with a "conjunction" action represents part of a special
6444 * kind of "set membership match". Such a flow should not actually
6445 * get executed, but it could via, say, a "packet-out", even though
6446 * that wouldn't be useful. Log it to help debugging. */
6447 xlate_report_error(ctx
, "executing no-op conjunction action");
6454 case OFPACT_UNROLL_XLATE
:
6455 xlate_ofpact_unroll_xlate(ctx
, ofpact_get_UNROLL_XLATE(a
));
6458 case OFPACT_FIN_TIMEOUT
:
6459 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
6460 xlate_fin_timeout(ctx
, ofpact_get_FIN_TIMEOUT(a
));
6463 case OFPACT_CLEAR_ACTIONS
:
6464 xlate_report_action_set(ctx
, "was");
6465 ofpbuf_clear(&ctx
->action_set
);
6466 ctx
->xin
->flow
.actset_output
= OFPP_UNSET
;
6467 ctx
->action_set_has_group
= false;
6470 case OFPACT_WRITE_ACTIONS
:
6471 xlate_write_actions(ctx
, ofpact_get_WRITE_ACTIONS(a
));
6472 xlate_report_action_set(ctx
, "is");
6475 case OFPACT_WRITE_METADATA
:
6476 metadata
= ofpact_get_WRITE_METADATA(a
);
6477 flow
->metadata
&= ~metadata
->mask
;
6478 flow
->metadata
|= metadata
->metadata
& metadata
->mask
;
6482 xlate_meter_action(ctx
, ofpact_get_METER(a
));
6485 case OFPACT_GOTO_TABLE
: {
6486 struct ofpact_goto_table
*ogt
= ofpact_get_GOTO_TABLE(a
);
6488 ovs_assert(ctx
->table_id
< ogt
->table_id
);
6490 xlate_table_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
,
6491 ogt
->table_id
, true, true, false, last
,
6497 xlate_sample_action(ctx
, ofpact_get_SAMPLE(a
));
6501 compose_clone(ctx
, ofpact_get_CLONE(a
), last
);
6505 xlate_generic_encap_action(ctx
, ofpact_get_ENCAP(a
));
6508 case OFPACT_DECAP
: {
6509 bool recirc_needed
=
6510 xlate_generic_decap_action(ctx
, ofpact_get_DECAP(a
));
6511 if (!ctx
->error
&& recirc_needed
) {
6512 /* Recirculate for parsing of inner packet. */
6513 ctx_trigger_freeze(ctx
);
6514 /* Then continue with next action. */
6521 compose_conntrack_action(ctx
, ofpact_get_CT(a
), last
);
6524 case OFPACT_CT_CLEAR
:
6525 compose_ct_clear_action(ctx
);
6529 /* This will be processed by compose_conntrack_action(). */
6530 ctx
->ct_nat_action
= ofpact_get_NAT(a
);
6533 case OFPACT_DEBUG_RECIRC
:
6534 ctx_trigger_freeze(ctx
);
6538 case OFPACT_DEBUG_SLOW
:
6539 ctx
->xout
->slow
|= SLOW_ACTION
;
6543 /* Check if need to store this and the remaining actions for later
6545 if (!ctx
->error
&& ctx
->exit
&& ctx_first_frozen_action(ctx
)) {
6546 freeze_unroll_actions(a
, ofpact_end(ofpacts
, ofpacts_len
), ctx
);
6553 xlate_in_init(struct xlate_in
*xin
, struct ofproto_dpif
*ofproto
,
6554 ovs_version_t version
, const struct flow
*flow
,
6555 ofp_port_t in_port
, struct rule_dpif
*rule
, uint16_t tcp_flags
,
6556 const struct dp_packet
*packet
, struct flow_wildcards
*wc
,
6557 struct ofpbuf
*odp_actions
)
6559 xin
->ofproto
= ofproto
;
6560 xin
->tables_version
= version
;
6562 xin
->upcall_flow
= flow
;
6563 xin
->flow
.in_port
.ofp_port
= in_port
;
6564 xin
->flow
.actset_output
= OFPP_UNSET
;
6565 xin
->packet
= packet
;
6566 xin
->allow_side_effects
= packet
!= NULL
;
6569 xin
->ofpacts
= NULL
;
6570 xin
->ofpacts_len
= 0;
6571 xin
->tcp_flags
= tcp_flags
;
6573 xin
->resubmit_stats
= NULL
;
6577 xin
->odp_actions
= odp_actions
;
6578 xin
->in_packet_out
= false;
6579 xin
->recirc_queue
= NULL
;
6580 xin
->xport_uuid
= UUID_ZERO
;
6582 /* Do recirc lookup. */
6583 xin
->frozen_state
= NULL
;
6584 if (flow
->recirc_id
) {
6585 const struct recirc_id_node
*node
6586 = recirc_id_node_find(flow
->recirc_id
);
6588 xin
->frozen_state
= &node
->state
;
6594 xlate_out_uninit(struct xlate_out
*xout
)
6597 recirc_refs_unref(&xout
->recircs
);
6601 static struct skb_priority_to_dscp
*
6602 get_skb_priority(const struct xport
*xport
, uint32_t skb_priority
)
6604 struct skb_priority_to_dscp
*pdscp
;
6607 hash
= hash_int(skb_priority
, 0);
6608 HMAP_FOR_EACH_IN_BUCKET (pdscp
, hmap_node
, hash
, &xport
->skb_priorities
) {
6609 if (pdscp
->skb_priority
== skb_priority
) {
6617 dscp_from_skb_priority(const struct xport
*xport
, uint32_t skb_priority
,
6620 struct skb_priority_to_dscp
*pdscp
= get_skb_priority(xport
, skb_priority
);
6621 *dscp
= pdscp
? pdscp
->dscp
: 0;
6622 return pdscp
!= NULL
;
6626 count_skb_priorities(const struct xport
*xport
)
6628 return hmap_count(&xport
->skb_priorities
);
6632 clear_skb_priorities(struct xport
*xport
)
6634 struct skb_priority_to_dscp
*pdscp
;
6636 HMAP_FOR_EACH_POP (pdscp
, hmap_node
, &xport
->skb_priorities
) {
6642 actions_output_to_local_port(const struct xlate_ctx
*ctx
)
6644 odp_port_t local_odp_port
= ofp_port_to_odp_port(ctx
->xbridge
, OFPP_LOCAL
);
6645 const struct nlattr
*a
;
6648 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, ctx
->odp_actions
->data
,
6649 ctx
->odp_actions
->size
) {
6650 if (nl_attr_type(a
) == OVS_ACTION_ATTR_OUTPUT
6651 && nl_attr_get_odp_port(a
) == local_odp_port
) {
6658 #if defined(__linux__)
6659 /* Returns the maximum number of packets that the Linux kernel is willing to
6660 * queue up internally to certain kinds of software-implemented ports, or the
6661 * default (and rarely modified) value if it cannot be determined. */
6663 netdev_max_backlog(void)
6665 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
6666 static int max_backlog
= 1000; /* The normal default value. */
6668 if (ovsthread_once_start(&once
)) {
6669 static const char filename
[] = "/proc/sys/net/core/netdev_max_backlog";
6673 stream
= fopen(filename
, "r");
6675 VLOG_INFO("%s: open failed (%s)", filename
, ovs_strerror(errno
));
6677 if (fscanf(stream
, "%d", &n
) != 1) {
6678 VLOG_WARN("%s: read error", filename
);
6679 } else if (n
<= 100) {
6680 VLOG_WARN("%s: unexpectedly small value %d", filename
, n
);
6686 ovsthread_once_done(&once
);
6688 VLOG_DBG("%s: using %d max_backlog", filename
, max_backlog
);
6694 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
6697 count_output_actions(const struct ofpbuf
*odp_actions
)
6699 const struct nlattr
*a
;
6703 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, odp_actions
->data
, odp_actions
->size
) {
6704 if (a
->nla_type
== OVS_ACTION_ATTR_OUTPUT
) {
6710 #endif /* defined(__linux__) */
6712 /* Returns true if 'odp_actions' contains more output actions than the datapath
6713 * can reliably handle in one go. On Linux, this is the value of the
6714 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
6715 * packets that the kernel is willing to queue up for processing while the
6716 * datapath is processing a set of actions. */
6718 too_many_output_actions(const struct ofpbuf
*odp_actions OVS_UNUSED
)
6721 return (odp_actions
->size
/ NL_A_U32_SIZE
> netdev_max_backlog()
6722 && count_output_actions(odp_actions
) > netdev_max_backlog());
6724 /* OSes other than Linux might have similar limits, but we don't know how
6725 * to determine them.*/
6731 xlate_wc_init(struct xlate_ctx
*ctx
)
6733 flow_wildcards_init_catchall(ctx
->wc
);
6735 /* Some fields we consider to always be examined. */
6736 WC_MASK_FIELD(ctx
->wc
, packet_type
);
6737 WC_MASK_FIELD(ctx
->wc
, in_port
);
6738 if (is_ethernet(&ctx
->xin
->flow
, NULL
)) {
6739 WC_MASK_FIELD(ctx
->wc
, dl_type
);
6741 if (is_ip_any(&ctx
->xin
->flow
)) {
6742 WC_MASK_FIELD_MASK(ctx
->wc
, nw_frag
, FLOW_NW_FRAG_MASK
);
6745 if (ctx
->xbridge
->support
.odp
.recirc
) {
6746 /* Always exactly match recirc_id when datapath supports
6748 WC_MASK_FIELD(ctx
->wc
, recirc_id
);
6751 if (ctx
->xbridge
->netflow
) {
6752 netflow_mask_wc(&ctx
->xin
->flow
, ctx
->wc
);
6755 tnl_wc_init(&ctx
->xin
->flow
, ctx
->wc
);
6759 xlate_wc_finish(struct xlate_ctx
*ctx
)
6763 /* Clear the metadata and register wildcard masks, because we won't
6764 * use non-header fields as part of the cache. */
6765 flow_wildcards_clear_non_packet_fields(ctx
->wc
);
6767 /* Wildcard ethernet fields if the original packet type was not
6769 if (ctx
->xin
->upcall_flow
->packet_type
!= htonl(PT_ETH
)) {
6770 ctx
->wc
->masks
.dl_dst
= eth_addr_zero
;
6771 ctx
->wc
->masks
.dl_src
= eth_addr_zero
;
6772 ctx
->wc
->masks
.dl_type
= 0;
6775 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
6776 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
6777 * represent these fields. The datapath interface, on the other hand,
6778 * represents them with just 8 bits each. This means that if the high
6779 * 8 bits of the masks for these fields somehow become set, then they
6780 * will get chopped off by a round trip through the datapath, and
6781 * revalidation will spot that as an inconsistency and delete the flow.
6782 * Avoid the problem here by making sure that only the low 8 bits of
6783 * either field can be unwildcarded for ICMP.
6785 if (is_icmpv4(&ctx
->xin
->flow
, NULL
) || is_icmpv6(&ctx
->xin
->flow
, NULL
)) {
6786 ctx
->wc
->masks
.tp_src
&= htons(UINT8_MAX
);
6787 ctx
->wc
->masks
.tp_dst
&= htons(UINT8_MAX
);
6789 /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
6790 for (i
= 0; i
< FLOW_MAX_VLAN_HEADERS
; i
++) {
6791 if (ctx
->wc
->masks
.vlans
[i
].tci
) {
6792 ctx
->wc
->masks
.vlans
[i
].tci
|= htons(VLAN_CFI
);
6796 /* The classifier might return masks that match on tp_src and tp_dst even
6797 * for later fragments. This happens because there might be flows that
6798 * match on tp_src or tp_dst without matching on the frag bits, because
6799 * it is not a prerequisite for OpenFlow. Since it is a prerequisite for
6800 * datapath flows and since tp_src and tp_dst are always going to be 0,
6801 * wildcard the fields here. */
6802 if (ctx
->xin
->flow
.nw_frag
& FLOW_NW_FRAG_LATER
) {
6803 ctx
->wc
->masks
.tp_src
= 0;
6804 ctx
->wc
->masks
.tp_dst
= 0;
6808 /* Translates the flow, actions, or rule in 'xin' into datapath actions in
6810 * The caller must take responsibility for eventually freeing 'xout', with
6811 * xlate_out_uninit().
6812 * Returns 'XLATE_OK' if translation was successful. In case of an error an
6813 * empty set of actions will be returned in 'xin->odp_actions' (if non-NULL),
6814 * so that most callers may ignore the return value and transparently install a
6815 * drop flow when the translation fails. */
6817 xlate_actions(struct xlate_in
*xin
, struct xlate_out
*xout
)
6819 *xout
= (struct xlate_out
) {
6821 .recircs
= RECIRC_REFS_EMPTY_INITIALIZER
,
6824 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
6825 struct xbridge
*xbridge
= xbridge_lookup(xcfg
, xin
->ofproto
);
6827 return XLATE_BRIDGE_NOT_FOUND
;
6830 struct flow
*flow
= &xin
->flow
;
6832 uint8_t stack_stub
[1024];
6833 uint64_t action_set_stub
[1024 / 8];
6834 uint64_t frozen_actions_stub
[1024 / 8];
6835 uint64_t actions_stub
[256 / 8];
6836 struct ofpbuf scratch_actions
= OFPBUF_STUB_INITIALIZER(actions_stub
);
6837 struct xlate_ctx ctx
= {
6841 .orig_tunnel_ipv6_dst
= flow_tnl_dst(&flow
->tunnel
),
6844 .stack
= OFPBUF_STUB_INITIALIZER(stack_stub
),
6848 : &(struct flow_wildcards
) { .masks
= { .dl_type
= 0 } }),
6849 .odp_actions
= xin
->odp_actions
? xin
->odp_actions
: &scratch_actions
,
6851 .depth
= xin
->depth
,
6852 .resubmits
= xin
->resubmits
,
6854 .in_action_set
= false,
6855 .in_packet_out
= xin
->in_packet_out
,
6856 .pending_encap
= false,
6857 .pending_decap
= false,
6861 .rule_cookie
= OVS_BE64_MAX
,
6862 .orig_skb_priority
= flow
->skb_priority
,
6863 .sflow_n_outputs
= 0,
6864 .sflow_odp_port
= 0,
6865 .nf_output_iface
= NF_OUT_DROP
,
6871 .recirc_update_dp_hash
= false,
6872 .frozen_actions
= OFPBUF_STUB_INITIALIZER(frozen_actions_stub
),
6876 .conntracked
= false,
6878 .ct_nat_action
= NULL
,
6880 .action_set_has_group
= false,
6881 .action_set
= OFPBUF_STUB_INITIALIZER(action_set_stub
),
6884 /* 'base_flow' reflects the packet as it came in, but we need it to reflect
6885 * the packet as the datapath will treat it for output actions. Our
6886 * datapath doesn't retain tunneling information without us re-setting
6887 * it, so clear the tunnel data.
6890 memset(&ctx
.base_flow
.tunnel
, 0, sizeof ctx
.base_flow
.tunnel
);
6892 ofpbuf_reserve(ctx
.odp_actions
, NL_A_U32_SIZE
);
6893 xlate_wc_init(&ctx
);
6895 COVERAGE_INC(xlate_actions
);
6897 xin
->trace
= xlate_report(&ctx
, OFT_BRIDGE
, "bridge(\"%s\")",
6899 if (xin
->frozen_state
) {
6900 const struct frozen_state
*state
= xin
->frozen_state
;
6902 struct ovs_list
*old_trace
= xin
->trace
;
6903 xin
->trace
= xlate_report(&ctx
, OFT_THAW
, "thaw");
6905 if (xin
->ofpacts_len
> 0 || ctx
.rule
) {
6906 xlate_report_error(&ctx
, "Recirculation conflict (%s)!",
6907 xin
->ofpacts_len
? "actions" : "rule");
6908 ctx
.error
= XLATE_RECIRCULATION_CONFLICT
;
6912 /* Set the bridge for post-recirculation processing if needed. */
6913 if (!uuid_equals(&ctx
.xbridge
->ofproto
->uuid
, &state
->ofproto_uuid
)) {
6914 const struct xbridge
*new_bridge
6915 = xbridge_lookup_by_uuid(xcfg
, &state
->ofproto_uuid
);
6917 if (OVS_UNLIKELY(!new_bridge
)) {
6918 /* Drop the packet if the bridge cannot be found. */
6919 xlate_report_error(&ctx
, "Frozen bridge no longer exists.");
6920 ctx
.error
= XLATE_BRIDGE_NOT_FOUND
;
6921 xin
->trace
= old_trace
;
6924 ctx
.xbridge
= new_bridge
;
6925 /* The bridge is now known so obtain its table version. */
6926 ctx
.xin
->tables_version
6927 = ofproto_dpif_get_tables_version(ctx
.xbridge
->ofproto
);
6930 /* Set the thawed table id. Note: A table lookup is done only if there
6931 * are no frozen actions. */
6932 ctx
.table_id
= state
->table_id
;
6933 xlate_report(&ctx
, OFT_THAW
,
6934 "Resuming from table %"PRIu8
, ctx
.table_id
);
6936 ctx
.conntracked
= state
->conntracked
;
6937 if (!state
->conntracked
) {
6938 clear_conntrack(&ctx
);
6941 /* Restore pipeline metadata. May change flow's in_port and other
6942 * metadata to the values that existed when freezing was triggered. */
6943 frozen_metadata_to_flow(&state
->metadata
, flow
);
6945 /* Restore stack, if any. */
6947 ofpbuf_put(&ctx
.stack
, state
->stack
, state
->stack_size
);
6950 /* Restore mirror state. */
6951 ctx
.mirrors
= state
->mirrors
;
6953 /* Restore action set, if any. */
6954 if (state
->action_set_len
) {
6955 xlate_report_actions(&ctx
, OFT_THAW
, "Restoring action set",
6956 state
->action_set
, state
->action_set_len
);
6958 flow
->actset_output
= OFPP_UNSET
;
6959 xlate_write_actions__(&ctx
, state
->action_set
,
6960 state
->action_set_len
);
6963 /* Restore frozen actions. If there are no actions, processing will
6964 * start with a lookup in the table set above. */
6965 xin
->ofpacts
= state
->ofpacts
;
6966 xin
->ofpacts_len
= state
->ofpacts_len
;
6967 if (state
->ofpacts_len
) {
6968 xlate_report_actions(&ctx
, OFT_THAW
, "Restoring actions",
6969 xin
->ofpacts
, xin
->ofpacts_len
);
6972 xin
->trace
= old_trace
;
6973 } else if (OVS_UNLIKELY(flow
->recirc_id
)) {
6974 xlate_report_error(&ctx
,
6975 "Recirculation context not found for ID %"PRIx32
,
6977 ctx
.error
= XLATE_NO_RECIRCULATION_CONTEXT
;
6981 /* Tunnel metadata in udpif format must be normalized before translation. */
6982 if (flow
->tunnel
.flags
& FLOW_TNL_F_UDPIF
) {
6983 const struct tun_table
*tun_tab
= ofproto_get_tun_tab(
6984 &ctx
.xbridge
->ofproto
->up
);
6987 err
= tun_metadata_from_geneve_udpif(tun_tab
, &xin
->upcall_flow
->tunnel
,
6988 &xin
->upcall_flow
->tunnel
,
6991 xlate_report_error(&ctx
, "Invalid Geneve tunnel metadata");
6992 ctx
.error
= XLATE_INVALID_TUNNEL_METADATA
;
6995 } else if (!flow
->tunnel
.metadata
.tab
|| xin
->frozen_state
) {
6996 /* If the original flow did not come in on a tunnel, then it won't have
6997 * FLOW_TNL_F_UDPIF set. However, we still need to have a metadata
6998 * table in case we generate tunnel actions. */
6999 /* If the translation is from a frozen state, we use the latest
7000 * TLV map to avoid segmentation fault in case the old TLV map is
7001 * replaced by a new one.
7002 * XXX: It is better to abort translation if the table is changed. */
7003 flow
->tunnel
.metadata
.tab
= ofproto_get_tun_tab(
7004 &ctx
.xbridge
->ofproto
->up
);
7006 ctx
.wc
->masks
.tunnel
.metadata
.tab
= flow
->tunnel
.metadata
.tab
;
7008 /* Get the proximate input port of the packet. (If xin->frozen_state,
7009 * flow->in_port is the ultimate input port of the packet.) */
7010 struct xport
*in_port
= get_ofp_port(xbridge
,
7011 ctx
.base_flow
.in_port
.ofp_port
);
7012 if (in_port
&& !in_port
->peer
) {
7013 ctx
.xin
->xport_uuid
= in_port
->uuid
;
7016 if (flow
->packet_type
!= htonl(PT_ETH
) && in_port
&&
7017 in_port
->pt_mode
== NETDEV_PT_LEGACY_L3
&& ctx
.table_id
== 0) {
7018 /* Add dummy Ethernet header to non-L2 packet if it's coming from a
7019 * L3 port. So all packets will be L2 packets for lookup.
7020 * The dl_type has already been set from the packet_type. */
7021 flow
->packet_type
= htonl(PT_ETH
);
7022 flow
->dl_src
= eth_addr_zero
;
7023 flow
->dl_dst
= eth_addr_zero
;
7024 ctx
.pending_encap
= true;
7027 if (!xin
->ofpacts
&& !ctx
.rule
) {
7028 ctx
.rule
= rule_dpif_lookup_from_table(
7029 ctx
.xbridge
->ofproto
, ctx
.xin
->tables_version
, flow
, ctx
.wc
,
7030 ctx
.xin
->resubmit_stats
, &ctx
.table_id
,
7031 flow
->in_port
.ofp_port
, true, true, ctx
.xin
->xcache
);
7032 if (ctx
.xin
->resubmit_stats
) {
7033 rule_dpif_credit_stats(ctx
.rule
, ctx
.xin
->resubmit_stats
);
7035 if (ctx
.xin
->xcache
) {
7036 struct xc_entry
*entry
;
7038 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_RULE
);
7039 entry
->rule
= ctx
.rule
;
7040 ofproto_rule_ref(&ctx
.rule
->up
);
7043 xlate_report_table(&ctx
, ctx
.rule
, ctx
.table_id
);
7046 /* Tunnel stats only for not-thawed packets. */
7047 if (!xin
->frozen_state
&& in_port
&& in_port
->is_tunnel
) {
7048 if (ctx
.xin
->resubmit_stats
) {
7049 netdev_vport_inc_rx(in_port
->netdev
, ctx
.xin
->resubmit_stats
);
7051 bfd_account_rx(in_port
->bfd
, ctx
.xin
->resubmit_stats
);
7054 if (ctx
.xin
->xcache
) {
7055 struct xc_entry
*entry
;
7057 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_NETDEV
);
7058 entry
->dev
.rx
= netdev_ref(in_port
->netdev
);
7059 entry
->dev
.bfd
= bfd_ref(in_port
->bfd
);
7063 if (!xin
->frozen_state
&& process_special(&ctx
, in_port
)) {
7064 /* process_special() did all the processing for this packet.
7066 * We do not perform special processing on thawed packets, since that
7067 * was done before they were frozen and should not be redone. */
7068 } else if (in_port
&& in_port
->xbundle
7069 && xbundle_mirror_out(xbridge
, in_port
->xbundle
)) {
7070 xlate_report_error(&ctx
, "dropping packet received on port "
7071 "%s, which is reserved exclusively for mirroring",
7072 in_port
->xbundle
->name
);
7074 /* Sampling is done on initial reception; don't redo after thawing. */
7075 unsigned int user_cookie_offset
= 0;
7076 if (!xin
->frozen_state
) {
7077 user_cookie_offset
= compose_sflow_action(&ctx
);
7078 compose_ipfix_action(&ctx
, ODPP_NONE
);
7080 size_t sample_actions_len
= ctx
.odp_actions
->size
;
7082 if (tnl_process_ecn(flow
)
7083 && (!in_port
|| may_receive(in_port
, &ctx
))) {
7084 const struct ofpact
*ofpacts
;
7088 ofpacts
= xin
->ofpacts
;
7089 ofpacts_len
= xin
->ofpacts_len
;
7090 } else if (ctx
.rule
) {
7091 const struct rule_actions
*actions
7092 = rule_get_actions(&ctx
.rule
->up
);
7093 ofpacts
= actions
->ofpacts
;
7094 ofpacts_len
= actions
->ofpacts_len
;
7095 ctx
.rule_cookie
= ctx
.rule
->up
.flow_cookie
;
7100 mirror_ingress_packet(&ctx
);
7101 do_xlate_actions(ofpacts
, ofpacts_len
, &ctx
, true);
7106 /* We've let OFPP_NORMAL and the learning action look at the
7107 * packet, so cancel all actions and freezing if forwarding is
7109 if (in_port
&& (!xport_stp_forward_state(in_port
) ||
7110 !xport_rstp_forward_state(in_port
))) {
7111 ctx
.odp_actions
->size
= sample_actions_len
;
7112 ctx_cancel_freeze(&ctx
);
7113 ofpbuf_clear(&ctx
.action_set
);
7116 if (!ctx
.freezing
) {
7117 xlate_action_set(&ctx
);
7120 finish_freezing(&ctx
);
7124 /* Output only fully processed packets. */
7126 && xbridge
->has_in_band
7127 && in_band_must_output_to_local_port(flow
)
7128 && !actions_output_to_local_port(&ctx
)) {
7129 compose_output_action(&ctx
, OFPP_LOCAL
, NULL
, false, false);
7132 if (user_cookie_offset
) {
7133 fix_sflow_action(&ctx
, user_cookie_offset
);
7137 if (nl_attr_oversized(ctx
.odp_actions
->size
)) {
7138 /* These datapath actions are too big for a Netlink attribute, so we
7139 * can't hand them to the kernel directly. dpif_execute() can execute
7140 * them one by one with help, so just mark the result as SLOW_ACTION to
7141 * prevent the flow from being installed. */
7142 COVERAGE_INC(xlate_actions_oversize
);
7143 ctx
.xout
->slow
|= SLOW_ACTION
;
7144 } else if (too_many_output_actions(ctx
.odp_actions
)) {
7145 COVERAGE_INC(xlate_actions_too_many_output
);
7146 ctx
.xout
->slow
|= SLOW_ACTION
;
7149 /* Update NetFlow for non-frozen traffic. */
7150 if (xbridge
->netflow
&& !xin
->frozen_state
) {
7151 if (ctx
.xin
->resubmit_stats
) {
7152 netflow_flow_update(xbridge
->netflow
, flow
,
7153 ctx
.nf_output_iface
,
7154 ctx
.xin
->resubmit_stats
);
7156 if (ctx
.xin
->xcache
) {
7157 struct xc_entry
*entry
;
7159 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_NETFLOW
);
7160 entry
->nf
.netflow
= netflow_ref(xbridge
->netflow
);
7161 entry
->nf
.flow
= xmemdup(flow
, sizeof *flow
);
7162 entry
->nf
.iface
= ctx
.nf_output_iface
;
7166 /* Translate tunnel metadata masks to udpif format if necessary. */
7167 if (xin
->upcall_flow
->tunnel
.flags
& FLOW_TNL_F_UDPIF
) {
7168 if (ctx
.wc
->masks
.tunnel
.metadata
.present
.map
) {
7169 const struct flow_tnl
*upcall_tnl
= &xin
->upcall_flow
->tunnel
;
7170 struct geneve_opt opts
[TLV_TOT_OPT_SIZE
/
7171 sizeof(struct geneve_opt
)];
7173 tun_metadata_to_geneve_udpif_mask(&flow
->tunnel
,
7174 &ctx
.wc
->masks
.tunnel
,
7175 upcall_tnl
->metadata
.opts
.gnv
,
7176 upcall_tnl
->metadata
.present
.len
,
7178 memset(&ctx
.wc
->masks
.tunnel
.metadata
, 0,
7179 sizeof ctx
.wc
->masks
.tunnel
.metadata
);
7180 memcpy(&ctx
.wc
->masks
.tunnel
.metadata
.opts
.gnv
, opts
,
7181 upcall_tnl
->metadata
.present
.len
);
7183 ctx
.wc
->masks
.tunnel
.metadata
.present
.len
= 0xff;
7184 ctx
.wc
->masks
.tunnel
.metadata
.tab
= NULL
;
7185 ctx
.wc
->masks
.tunnel
.flags
|= FLOW_TNL_F_UDPIF
;
7186 } else if (!xin
->upcall_flow
->tunnel
.metadata
.tab
) {
7187 /* If we didn't have options in UDPIF format and didn't have an existing
7188 * metadata table, then it means that there were no options at all when
7189 * we started processing and any wildcards we picked up were from
7190 * action generation. Without options on the incoming packet, wildcards
7191 * aren't meaningful. To avoid them possibly getting misinterpreted,
7192 * just clear everything. */
7193 if (ctx
.wc
->masks
.tunnel
.metadata
.present
.map
) {
7194 memset(&ctx
.wc
->masks
.tunnel
.metadata
, 0,
7195 sizeof ctx
.wc
->masks
.tunnel
.metadata
);
7197 ctx
.wc
->masks
.tunnel
.metadata
.tab
= NULL
;
7201 xlate_wc_finish(&ctx
);
7204 /* Reset the table to what it was when we came in. If we only fetched
7205 * it locally, then it has no meaning outside of flow translation. */
7206 flow
->tunnel
.metadata
.tab
= xin
->upcall_flow
->tunnel
.metadata
.tab
;
7208 ofpbuf_uninit(&ctx
.stack
);
7209 ofpbuf_uninit(&ctx
.action_set
);
7210 ofpbuf_uninit(&ctx
.frozen_actions
);
7211 ofpbuf_uninit(&scratch_actions
);
7212 ofpbuf_delete(ctx
.encap_data
);
7214 /* Make sure we return a "drop flow" in case of an error. */
7217 if (xin
->odp_actions
) {
7218 ofpbuf_clear(xin
->odp_actions
);
7225 xlate_resume(struct ofproto_dpif
*ofproto
,
7226 const struct ofputil_packet_in_private
*pin
,
7227 struct ofpbuf
*odp_actions
,
7228 enum slow_path_reason
*slow
)
7230 struct dp_packet packet
;
7231 dp_packet_use_const(&packet
, pin
->base
.packet
,
7232 pin
->base
.packet_len
);
7235 flow_extract(&packet
, &flow
);
7237 struct xlate_in xin
;
7238 xlate_in_init(&xin
, ofproto
, ofproto_dpif_get_tables_version(ofproto
),
7239 &flow
, 0, NULL
, ntohs(flow
.tcp_flags
),
7240 &packet
, NULL
, odp_actions
);
7242 struct ofpact_note noop
;
7243 ofpact_init_NOTE(&noop
);
7246 bool any_actions
= pin
->actions_len
> 0;
7247 struct frozen_state state
= {
7248 .table_id
= 0, /* Not the table where NXAST_PAUSE was executed. */
7249 .ofproto_uuid
= pin
->bridge
,
7250 .stack
= pin
->stack
,
7251 .stack_size
= pin
->stack_size
,
7252 .mirrors
= pin
->mirrors
,
7253 .conntracked
= pin
->conntracked
,
7254 .xport_uuid
= UUID_ZERO
,
7256 /* When there are no actions, xlate_actions() will search the flow
7257 * table. We don't want it to do that (we want it to resume), so
7258 * supply a no-op action if there aren't any.
7260 * (We can't necessarily avoid translating actions entirely if there
7261 * aren't any actions, because there might be some finishing-up to do
7262 * at the end of the pipeline, and we don't check for those
7264 .ofpacts
= any_actions
? pin
->actions
: &noop
.ofpact
,
7265 .ofpacts_len
= any_actions
? pin
->actions_len
: sizeof noop
,
7267 .action_set
= pin
->action_set
,
7268 .action_set_len
= pin
->action_set_len
,
7270 frozen_metadata_from_flow(&state
.metadata
,
7271 &pin
->base
.flow_metadata
.flow
);
7272 xin
.frozen_state
= &state
;
7274 struct xlate_out xout
;
7275 enum xlate_error error
= xlate_actions(&xin
, &xout
);
7277 xlate_out_uninit(&xout
);
7279 /* xlate_actions() can generate a number of errors, but only
7280 * XLATE_BRIDGE_NOT_FOUND really stands out to me as one that we should be
7281 * sure to report over OpenFlow. The others could come up in packet-outs
7282 * or regular flow translation and I don't think that it's going to be too
7283 * useful to report them to the controller. */
7284 return error
== XLATE_BRIDGE_NOT_FOUND
? OFPERR_NXR_STALE
: 0;
7287 /* Sends 'packet' out 'ofport'. If 'port' is a tunnel and that tunnel type
7288 * supports a notion of an OAM flag, sets it if 'oam' is true.
7289 * May modify 'packet'.
7290 * Returns 0 if successful, otherwise a positive errno value. */
7292 xlate_send_packet(const struct ofport_dpif
*ofport
, bool oam
,
7293 struct dp_packet
*packet
)
7295 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
7296 struct xport
*xport
;
7297 uint64_t ofpacts_stub
[1024 / 8];
7298 struct ofpbuf ofpacts
;
7301 ofpbuf_use_stack(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
7302 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
7303 flow_extract(packet
, &flow
);
7304 flow
.in_port
.ofp_port
= OFPP_NONE
;
7306 xport
= xport_lookup(xcfg
, ofport
);
7312 const ovs_be16 flag
= htons(NX_TUN_FLAG_OAM
);
7313 ofpact_put_set_field(&ofpacts
, mf_from_id(MFF_TUN_FLAGS
),
7317 ofpact_put_OUTPUT(&ofpacts
)->port
= xport
->ofp_port
;
7319 /* Actions here are not referring to anything versionable (flow tables or
7320 * groups) so we don't need to worry about the version here. */
7321 return ofproto_dpif_execute_actions(xport
->xbridge
->ofproto
,
7322 OVS_VERSION_MAX
, &flow
, NULL
,
7323 ofpacts
.data
, ofpacts
.size
, packet
);
7327 xlate_mac_learning_update(const struct ofproto_dpif
*ofproto
,
7328 ofp_port_t in_port
, struct eth_addr dl_src
,
7329 int vlan
, bool is_grat_arp
)
7331 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
7332 struct xbridge
*xbridge
;
7333 struct xbundle
*xbundle
;
7335 xbridge
= xbridge_lookup(xcfg
, ofproto
);
7340 xbundle
= lookup_input_bundle__(xbridge
, in_port
, NULL
);
7345 update_learning_table__(xbridge
, xbundle
, dl_src
, vlan
, is_grat_arp
);
7349 xlate_set_support(const struct ofproto_dpif
*ofproto
,
7350 const struct dpif_backer_support
*support
)
7352 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
7353 struct xbridge
*xbridge
= xbridge_lookup(xcfg
, ofproto
);
7356 xbridge
->support
= *support
;