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 <arpa/inet.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
29 #include "byte-order.h"
34 #include "dp-packet.h"
39 #include "mac-learning.h"
40 #include "mcast-snooping.h"
41 #include "multipath.h"
42 #include "netdev-vport.h"
45 #include "odp-execute.h"
46 #include "ofproto/ofproto-dpif-ipfix.h"
47 #include "ofproto/ofproto-dpif-mirror.h"
48 #include "ofproto/ofproto-dpif-monitor.h"
49 #include "ofproto/ofproto-dpif-sflow.h"
50 #include "ofproto/ofproto-dpif-trace.h"
51 #include "ofproto/ofproto-dpif-xlate-cache.h"
52 #include "ofproto/ofproto-dpif.h"
53 #include "ofproto/ofproto-provider.h"
54 #include "openvswitch/dynamic-string.h"
55 #include "openvswitch/meta-flow.h"
56 #include "openvswitch/list.h"
57 #include "openvswitch/ofp-actions.h"
58 #include "openvswitch/ofp-ed-props.h"
59 #include "openvswitch/vlog.h"
61 #include "ovs-router.h"
63 #include "tnl-neigh-cache.h"
64 #include "tnl-ports.h"
68 COVERAGE_DEFINE(xlate_actions
);
69 COVERAGE_DEFINE(xlate_actions_oversize
);
70 COVERAGE_DEFINE(xlate_actions_too_many_output
);
72 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate
);
74 /* Maximum depth of flow table recursion (due to resubmit actions) in a
77 * The goal of limiting the depth of resubmits is to ensure that flow
78 * translation eventually terminates. Only resubmits to the same table or an
79 * earlier table count against the maximum depth. This is because resubmits to
80 * strictly monotonically increasing table IDs will eventually terminate, since
81 * any OpenFlow switch has a finite number of tables. OpenFlow tables are most
82 * commonly traversed in numerically increasing order, so this limit has little
83 * effect on conventionally designed OpenFlow pipelines.
85 * Outputs to patch ports and to groups also count against the depth limit. */
88 /* Maximum number of resubmit actions in a flow translation, whether they are
89 * recursive or not. */
90 #define MAX_RESUBMITS (MAX_DEPTH * MAX_DEPTH)
93 struct hmap_node hmap_node
; /* Node in global 'xbridges' map. */
94 struct ofproto_dpif
*ofproto
; /* Key in global 'xbridges' map. */
96 struct ovs_list xbundles
; /* Owned xbundles. */
97 struct hmap xports
; /* Indexed by ofp_port. */
99 char *name
; /* Name used in log messages. */
100 struct dpif
*dpif
; /* Datapath interface. */
101 struct mac_learning
*ml
; /* Mac learning handle. */
102 struct mcast_snooping
*ms
; /* Multicast Snooping handle. */
103 struct mbridge
*mbridge
; /* Mirroring. */
104 struct dpif_sflow
*sflow
; /* SFlow handle, or null. */
105 struct dpif_ipfix
*ipfix
; /* Ipfix handle, or null. */
106 struct netflow
*netflow
; /* Netflow handle, or null. */
107 struct stp
*stp
; /* STP or null if disabled. */
108 struct rstp
*rstp
; /* RSTP or null if disabled. */
110 bool has_in_band
; /* Bridge has in band control? */
111 bool forward_bpdu
; /* Bridge forwards STP BPDUs? */
113 /* Datapath feature support. */
114 struct dpif_backer_support support
;
118 struct hmap_node hmap_node
; /* In global 'xbundles' map. */
119 struct ofbundle
*ofbundle
; /* Key in global 'xbundles' map. */
121 struct ovs_list list_node
; /* In parent 'xbridges' list. */
122 struct xbridge
*xbridge
; /* Parent xbridge. */
124 struct ovs_list xports
; /* Contains "struct xport"s. */
126 char *name
; /* Name used in log messages. */
127 struct bond
*bond
; /* Nonnull iff more than one port. */
128 struct lacp
*lacp
; /* LACP handle or null. */
130 enum port_vlan_mode vlan_mode
; /* VLAN mode. */
131 uint16_t qinq_ethtype
; /* Ethertype of dot1q-tunnel interface
132 * either 0x8100 or 0x88a8. */
133 int vlan
; /* -1=trunk port, else a 12-bit VLAN ID. */
134 unsigned long *trunks
; /* Bitmap of trunked VLANs, if 'vlan' == -1.
135 * NULL if all VLANs are trunked. */
136 unsigned long *cvlans
; /* Bitmap of allowed customer vlans,
137 * NULL if all VLANs are allowed */
138 bool use_priority_tags
; /* Use 802.1p tag for frames in VLAN 0? */
139 bool floodable
; /* No port has OFPUTIL_PC_NO_FLOOD set? */
140 bool protected; /* Protected port mode */
144 struct hmap_node hmap_node
; /* Node in global 'xports' map. */
145 struct ofport_dpif
*ofport
; /* Key in global 'xports map. */
147 struct hmap_node ofp_node
; /* Node in parent xbridge 'xports' map. */
148 ofp_port_t ofp_port
; /* Key in parent xbridge 'xports' map. */
150 odp_port_t odp_port
; /* Datapath port number or ODPP_NONE. */
152 struct ovs_list bundle_node
; /* In parent xbundle (if it exists). */
153 struct xbundle
*xbundle
; /* Parent xbundle or null. */
155 struct netdev
*netdev
; /* 'ofport''s netdev. */
157 struct xbridge
*xbridge
; /* Parent bridge. */
158 struct xport
*peer
; /* Patch port peer or null. */
160 enum ofputil_port_config config
; /* OpenFlow port configuration. */
161 enum ofputil_port_state state
; /* OpenFlow port state. */
162 int stp_port_no
; /* STP port number or -1 if not in use. */
163 struct rstp_port
*rstp_port
; /* RSTP port or null. */
165 struct hmap skb_priorities
; /* Map of 'skb_priority_to_dscp's. */
167 bool may_enable
; /* May be enabled in bonds. */
168 bool is_tunnel
; /* Is a tunnel port. */
169 enum netdev_pt_mode pt_mode
; /* packet_type handling. */
171 struct cfm
*cfm
; /* CFM handle or null. */
172 struct bfd
*bfd
; /* BFD handle or null. */
173 struct lldp
*lldp
; /* LLDP handle or null. */
177 struct xlate_in
*xin
;
178 struct xlate_out
*xout
;
180 const struct xbridge
*xbridge
;
182 /* Flow at the last commit. */
183 struct flow base_flow
;
185 /* Tunnel IP destination address as received. This is stored separately
186 * as the base_flow.tunnel is cleared on init to reflect the datapath
187 * behavior. Used to make sure not to send tunneled output to ourselves,
188 * which might lead to an infinite loop. This could happen easily
189 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
190 * actually set the tun_dst field. */
191 struct in6_addr orig_tunnel_ipv6_dst
;
193 /* Stack for the push and pop actions. See comment above nx_stack_push()
194 * in nx-match.c for info on how the stack is stored. */
197 /* The rule that we are currently translating, or NULL. */
198 struct rule_dpif
*rule
;
200 /* Flow translation populates this with wildcards relevant in translation.
201 * When 'xin->wc' is nonnull, this is the same pointer. When 'xin->wc' is
202 * null, this is a pointer to a temporary buffer. */
203 struct flow_wildcards
*wc
;
205 /* Output buffer for datapath actions. When 'xin->odp_actions' is nonnull,
206 * this is the same pointer. When 'xin->odp_actions' is null, this points
207 * to a scratch ofpbuf. This allows code to add actions to
208 * 'ctx->odp_actions' without worrying about whether the caller really
210 struct ofpbuf
*odp_actions
;
212 /* Statistics maintained by xlate_table_action().
214 * These statistics limit the amount of work that a single flow
215 * translation can perform. The goal of the first of these, 'depth', is
216 * primarily to prevent translation from performing an infinite amount of
217 * work. It counts the current depth of nested "resubmit"s (and a few
218 * other activities); when a resubmit returns, it decreases. Resubmits to
219 * tables in strictly monotonically increasing order don't contribute to
220 * 'depth' because they cannot cause a flow translation to take an infinite
221 * amount of time (because the number of tables is finite). Translation
222 * aborts when 'depth' exceeds MAX_DEPTH.
224 * 'resubmits', on the other hand, prevents flow translation from
225 * performing an extraordinarily large while still finite amount of work.
226 * It counts the total number of resubmits (and a few other activities)
227 * that have been executed. Returning from a resubmit does not affect this
228 * counter. Thus, this limits the amount of work that a particular
229 * translation can perform. Translation aborts when 'resubmits' exceeds
230 * MAX_RESUBMITS (which is much larger than MAX_DEPTH).
232 int depth
; /* Current resubmit nesting depth. */
233 int resubmits
; /* Total number of resubmits. */
234 bool in_group
; /* Currently translating ofgroup, if true. */
235 bool in_action_set
; /* Currently translating action_set, if true. */
236 bool in_packet_out
; /* Currently translating a packet_out msg, if
238 bool pending_encap
; /* True when waiting to commit a pending
240 struct ofpbuf
*encap_data
; /* May contain a pointer to an ofpbuf with
241 * context for the datapath encap action.*/
243 uint8_t table_id
; /* OpenFlow table ID where flow was found. */
244 ovs_be64 rule_cookie
; /* Cookie of the rule being translated. */
245 uint32_t orig_skb_priority
; /* Priority when packet arrived. */
246 uint32_t sflow_n_outputs
; /* Number of output ports. */
247 odp_port_t sflow_odp_port
; /* Output port for composing sFlow action. */
248 ofp_port_t nf_output_iface
; /* Output interface index for NetFlow. */
249 bool exit
; /* No further actions should be processed. */
250 mirror_mask_t mirrors
; /* Bitmap of associated mirrors. */
251 int mirror_snaplen
; /* Max size of a mirror packet in byte. */
253 /* Freezing Translation
254 * ====================
256 * At some point during translation, the code may recognize the need to halt
257 * and checkpoint the translation in a way that it can be restarted again
258 * later. We call the checkpointing process "freezing" and the restarting
261 * The use cases for freezing are:
263 * - "Recirculation", where the translation process discovers that it
264 * doesn't have enough information to complete translation without
265 * actually executing the actions that have already been translated,
266 * which provides the additionally needed information. In these
267 * situations, translation freezes translation and assigns the frozen
268 * data a unique "recirculation ID", which it associates with the data
269 * in a table in userspace (see ofproto-dpif-rid.h). It also adds a
270 * OVS_ACTION_ATTR_RECIRC action specifying that ID to the datapath
271 * actions. When a packet hits that action, the datapath looks its
272 * flow up again using the ID. If there's a miss, it comes back to
273 * userspace, which find the recirculation table entry for the ID,
274 * thaws the associated frozen data, and continues translation from
275 * that point given the additional information that is now known.
277 * The archetypal example is MPLS. As MPLS is implemented in
278 * OpenFlow, the protocol that follows the last MPLS label becomes
279 * known only when that label is popped by an OpenFlow action. That
280 * means that Open vSwitch can't extract the headers beyond the MPLS
281 * labels until the pop action is executed. Thus, at that point
282 * translation uses the recirculation process to extract the headers
283 * beyond the MPLS labels.
285 * (OVS also uses OVS_ACTION_ATTR_RECIRC to implement hashing for
286 * output to bonds. OVS pre-populates all the datapath flows for bond
287 * output in the datapath, though, which means that the elaborate
288 * process of coming back to userspace for a second round of
289 * translation isn't needed, and so bonds don't follow the above
292 * - "Continuation". A continuation is a way for an OpenFlow controller
293 * to interpose on a packet's traversal of the OpenFlow tables. When
294 * the translation process encounters a "controller" action with the
295 * "pause" flag, it freezes translation, serializes the frozen data,
296 * and sends it to an OpenFlow controller. The controller then
297 * examines and possibly modifies the frozen data and eventually sends
298 * it back to the switch, which thaws it and continues translation.
300 * The main problem of freezing translation is preserving state, so that
301 * when the translation is thawed later it resumes from where it left off,
302 * without disruption. In particular, actions must be preserved as follows:
304 * - If we're freezing because an action needed more information, the
305 * action that prompted it.
307 * - Any actions remaining to be translated within the current flow.
309 * - If translation was frozen within a NXAST_RESUBMIT, then any actions
310 * following the resubmit action. Resubmit actions can be nested, so
311 * this has to go all the way up the control stack.
313 * - The OpenFlow 1.1+ action set.
315 * State that actions and flow table lookups can depend on, such as the
316 * following, must also be preserved:
318 * - Metadata fields (input port, registers, OF1.1+ metadata, ...).
320 * - The stack used by NXAST_STACK_PUSH and NXAST_STACK_POP actions.
322 * - The table ID and cookie of the flow being translated at each level
323 * of the control stack, because these can become visible through
324 * OFPAT_CONTROLLER actions (and other ways).
326 * Translation allows for the control of this state preservation via these
327 * members. When a need to freeze translation is identified, the
328 * translation process:
330 * 1. Sets 'freezing' to true.
332 * 2. Sets 'exit' to true to tell later steps that we're exiting from the
333 * translation process.
335 * 3. Adds an OFPACT_UNROLL_XLATE action to 'frozen_actions', and points
336 * frozen_actions.header to the action to make it easy to find it later.
337 * This action holds the current table ID and cookie so that they can be
338 * restored during a post-recirculation upcall translation.
340 * 4. Adds the action that prompted recirculation and any actions following
341 * it within the same flow to 'frozen_actions', so that they can be
342 * executed during a post-recirculation upcall translation.
346 * 6. The action that prompted recirculation might be nested in a stack of
347 * nested "resubmit"s that have actions remaining. Each of these notices
348 * that we're exiting and freezing and responds by adding more
349 * OFPACT_UNROLL_XLATE actions to 'frozen_actions', as necessary,
350 * followed by any actions that were yet unprocessed.
352 * If we're freezing because of recirculation, the caller generates a
353 * recirculation ID and associates all the state produced by this process
354 * with it. For post-recirculation upcall translation, the caller passes it
355 * back in for the new translation to execute. The process yielded a set of
356 * ofpacts that can be translated directly, so it is not much of a special
357 * case at that point.
360 bool recirc_update_dp_hash
; /* Generated recirculation will be preceded
361 * by datapath HASH action to get an updated
362 * dp_hash after recirculation. */
363 uint32_t dp_hash_alg
;
364 uint32_t dp_hash_basis
;
365 struct ofpbuf frozen_actions
;
366 const struct ofpact_controller
*pause
;
367 struct flow
*paused_flow
;
369 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
370 * This is a trigger for recirculation in cases where translating an action
371 * or looking up a flow requires access to the fields of the packet after
372 * the MPLS label stack that was originally present. */
375 /* True if conntrack has been performed on this packet during processing
376 * on the current bridge. This is used to determine whether conntrack
377 * state from the datapath should be honored after thawing. */
380 /* Pointer to an embedded NAT action in a conntrack action, or NULL. */
381 struct ofpact_nat
*ct_nat_action
;
383 /* OpenFlow 1.1+ action set.
385 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
386 * When translation is otherwise complete, ofpacts_execute_action_set()
387 * converts it to a set of "struct ofpact"s that can be translated into
388 * datapath actions. */
389 bool action_set_has_group
; /* Action set contains OFPACT_GROUP? */
390 struct ofpbuf action_set
; /* Action set. */
392 enum xlate_error error
; /* Translation failed. */
395 /* Structure to track VLAN manipulation */
396 struct xvlan_single
{
403 struct xvlan_single v
[FLOW_MAX_VLAN_HEADERS
];
406 const char *xlate_strerror(enum xlate_error error
)
411 case XLATE_BRIDGE_NOT_FOUND
:
412 return "Bridge not found";
413 case XLATE_RECURSION_TOO_DEEP
:
414 return "Recursion too deep";
415 case XLATE_TOO_MANY_RESUBMITS
:
416 return "Too many resubmits";
417 case XLATE_STACK_TOO_DEEP
:
418 return "Stack too deep";
419 case XLATE_NO_RECIRCULATION_CONTEXT
:
420 return "No recirculation context";
421 case XLATE_RECIRCULATION_CONFLICT
:
422 return "Recirculation conflict";
423 case XLATE_TOO_MANY_MPLS_LABELS
:
424 return "Too many MPLS labels";
425 case XLATE_INVALID_TUNNEL_METADATA
:
426 return "Invalid tunnel metadata";
428 return "Unknown error";
431 static void xlate_action_set(struct xlate_ctx
*ctx
);
432 static void xlate_commit_actions(struct xlate_ctx
*ctx
);
435 apply_nested_clone_actions(struct xlate_ctx
*ctx
, const struct xport
*in_dev
,
436 struct xport
*out_dev
);
439 ctx_trigger_freeze(struct xlate_ctx
*ctx
)
442 ctx
->freezing
= true;
446 ctx_trigger_recirculate_with_hash(struct xlate_ctx
*ctx
, uint32_t type
,
450 ctx
->freezing
= true;
451 ctx
->recirc_update_dp_hash
= true;
452 ctx
->dp_hash_alg
= type
;
453 ctx
->dp_hash_basis
= basis
;
457 ctx_first_frozen_action(const struct xlate_ctx
*ctx
)
459 return !ctx
->frozen_actions
.size
;
463 ctx_cancel_freeze(struct xlate_ctx
*ctx
)
466 ctx
->freezing
= false;
467 ctx
->recirc_update_dp_hash
= false;
468 ofpbuf_clear(&ctx
->frozen_actions
);
469 ctx
->frozen_actions
.header
= NULL
;
473 static void finish_freezing(struct xlate_ctx
*ctx
);
475 /* A controller may use OFPP_NONE as the ingress port to indicate that
476 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
477 * when an input bundle is needed for validation (e.g., mirroring or
478 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
479 * any 'port' structs, so care must be taken when dealing with it. */
480 static struct xbundle ofpp_none_bundle
= {
482 .vlan_mode
= PORT_VLAN_TRUNK
485 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
486 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
487 * traffic egressing the 'ofport' with that priority should be marked with. */
488 struct skb_priority_to_dscp
{
489 struct hmap_node hmap_node
; /* Node in 'ofport_dpif''s 'skb_priorities'. */
490 uint32_t skb_priority
; /* Priority of this queue (see struct flow). */
492 uint8_t dscp
; /* DSCP bits to mark outgoing traffic with. */
495 /* Xlate config contains hash maps of all bridges, bundles and ports.
496 * Xcfgp contains the pointer to the current xlate configuration.
497 * When the main thread needs to change the configuration, it copies xcfgp to
498 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
499 * does not block handler and revalidator threads. */
501 struct hmap xbridges
;
502 struct hmap xbundles
;
505 static OVSRCU_TYPE(struct xlate_cfg
*) xcfgp
= OVSRCU_INITIALIZER(NULL
);
506 static struct xlate_cfg
*new_xcfg
= NULL
;
508 static bool may_receive(const struct xport
*, struct xlate_ctx
*);
509 static void do_xlate_actions(const struct ofpact
*, size_t ofpacts_len
,
511 static void xlate_normal(struct xlate_ctx
*);
512 static void xlate_table_action(struct xlate_ctx
*, ofp_port_t in_port
,
513 uint8_t table_id
, bool may_packet_in
,
514 bool honor_table_miss
, bool with_ct_orig
);
515 static bool input_vid_is_valid(const struct xlate_ctx
*,
516 uint16_t vid
, struct xbundle
*);
517 static void xvlan_copy(struct xvlan
*dst
, const struct xvlan
*src
);
518 static void xvlan_pop(struct xvlan
*src
);
519 static void xvlan_push_uninit(struct xvlan
*src
);
520 static void xvlan_extract(const struct flow
*, struct xvlan
*);
521 static void xvlan_put(struct flow
*, const struct xvlan
*);
522 static void xvlan_input_translate(const struct xbundle
*,
523 const struct xvlan
*in
,
524 struct xvlan
*xvlan
);
525 static void xvlan_output_translate(const struct xbundle
*,
526 const struct xvlan
*xvlan
,
528 static void output_normal(struct xlate_ctx
*, const struct xbundle
*,
529 const struct xvlan
*);
531 /* Optional bond recirculation parameter to compose_output_action(). */
532 struct xlate_bond_recirc
{
533 uint32_t recirc_id
; /* !0 Use recirculation instead of output. */
534 uint8_t hash_alg
; /* !0 Compute hash for recirc before. */
535 uint32_t hash_basis
; /* Compute hash for recirc before. */
538 static void compose_output_action(struct xlate_ctx
*, ofp_port_t ofp_port
,
539 const struct xlate_bond_recirc
*xr
);
541 static struct xbridge
*xbridge_lookup(struct xlate_cfg
*,
542 const struct ofproto_dpif
*);
543 static struct xbridge
*xbridge_lookup_by_uuid(struct xlate_cfg
*,
544 const struct uuid
*);
545 static struct xbundle
*xbundle_lookup(struct xlate_cfg
*,
546 const struct ofbundle
*);
547 static struct xport
*xport_lookup(struct xlate_cfg
*,
548 const struct ofport_dpif
*);
549 static struct xport
*get_ofp_port(const struct xbridge
*, ofp_port_t ofp_port
);
550 static struct skb_priority_to_dscp
*get_skb_priority(const struct xport
*,
551 uint32_t skb_priority
);
552 static void clear_skb_priorities(struct xport
*);
553 static size_t count_skb_priorities(const struct xport
*);
554 static bool dscp_from_skb_priority(const struct xport
*, uint32_t skb_priority
,
557 static void xlate_xbridge_init(struct xlate_cfg
*, struct xbridge
*);
558 static void xlate_xbundle_init(struct xlate_cfg
*, struct xbundle
*);
559 static void xlate_xport_init(struct xlate_cfg
*, struct xport
*);
560 static void xlate_xbridge_set(struct xbridge
*, struct dpif
*,
561 const struct mac_learning
*, struct stp
*,
562 struct rstp
*, const struct mcast_snooping
*,
563 const struct mbridge
*,
564 const struct dpif_sflow
*,
565 const struct dpif_ipfix
*,
566 const struct netflow
*,
567 bool forward_bpdu
, bool has_in_band
,
568 const struct dpif_backer_support
*);
569 static void xlate_xbundle_set(struct xbundle
*xbundle
,
570 enum port_vlan_mode vlan_mode
,
571 uint16_t qinq_ethtype
, int vlan
,
572 unsigned long *trunks
, unsigned long *cvlans
,
573 bool use_priority_tags
,
574 const struct bond
*bond
, const struct lacp
*lacp
,
575 bool floodable
, bool protected);
576 static void xlate_xport_set(struct xport
*xport
, odp_port_t odp_port
,
577 const struct netdev
*netdev
, const struct cfm
*cfm
,
578 const struct bfd
*bfd
, const struct lldp
*lldp
,
579 int stp_port_no
, const struct rstp_port
*rstp_port
,
580 enum ofputil_port_config config
,
581 enum ofputil_port_state state
, bool is_tunnel
,
583 static void xlate_xbridge_remove(struct xlate_cfg
*, struct xbridge
*);
584 static void xlate_xbundle_remove(struct xlate_cfg
*, struct xbundle
*);
585 static void xlate_xport_remove(struct xlate_cfg
*, struct xport
*);
586 static void xlate_xbridge_copy(struct xbridge
*);
587 static void xlate_xbundle_copy(struct xbridge
*, struct xbundle
*);
588 static void xlate_xport_copy(struct xbridge
*, struct xbundle
*,
590 static void xlate_xcfg_free(struct xlate_cfg
*);
592 /* Tracing helpers. */
594 /* If tracing is enabled in 'ctx', creates a new trace node and appends it to
595 * the list of nodes maintained in ctx->xin. The new node has type 'type' and
596 * its text is created from 'format' by treating it as a printf format string.
597 * Returns the list of nodes embedded within the new trace node; ordinarily,
598 * the calleer can ignore this, but it is useful if the caller needs to nest
599 * more trace nodes within the new node.
601 * If tracing is not enabled, does nothing and returns NULL. */
602 static struct ovs_list
* OVS_PRINTF_FORMAT(3, 4)
603 xlate_report(const struct xlate_ctx
*ctx
, enum oftrace_node_type type
,
604 const char *format
, ...)
606 struct ovs_list
*subtrace
= NULL
;
607 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
609 va_start(args
, format
);
610 char *text
= xvasprintf(format
, args
);
611 subtrace
= &oftrace_report(ctx
->xin
->trace
, type
, text
)->subs
;
618 /* This is like xlate_report() for errors that are serious enough that we
619 * should log them even if we are not tracing. */
620 static void OVS_PRINTF_FORMAT(2, 3)
621 xlate_report_error(const struct xlate_ctx
*ctx
, const char *format
, ...)
623 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
624 if (!OVS_UNLIKELY(ctx
->xin
->trace
)
625 && (!ctx
->xin
->packet
|| VLOG_DROP_WARN(&rl
))) {
629 struct ds s
= DS_EMPTY_INITIALIZER
;
631 va_start(args
, format
);
632 ds_put_format_valist(&s
, format
, args
);
635 if (ctx
->xin
->trace
) {
636 oftrace_report(ctx
->xin
->trace
, OFT_ERROR
, ds_cstr(&s
));
638 ds_put_cstr(&s
, " while processing ");
639 flow_format(&s
, &ctx
->base_flow
, NULL
);
640 ds_put_format(&s
, " on bridge %s", ctx
->xbridge
->name
);
641 VLOG_WARN("%s", ds_cstr(&s
));
646 /* This is like xlate_report() for messages that should be logged at debug
647 * level (even if we are not tracing) because they can be valuable for
649 static void OVS_PRINTF_FORMAT(3, 4)
650 xlate_report_debug(const struct xlate_ctx
*ctx
, enum oftrace_node_type type
,
651 const char *format
, ...)
653 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(30, 300);
654 if (!OVS_UNLIKELY(ctx
->xin
->trace
)
655 && (!ctx
->xin
->packet
|| VLOG_DROP_DBG(&rl
))) {
659 struct ds s
= DS_EMPTY_INITIALIZER
;
661 va_start(args
, format
);
662 ds_put_format_valist(&s
, format
, args
);
665 if (ctx
->xin
->trace
) {
666 oftrace_report(ctx
->xin
->trace
, type
, ds_cstr(&s
));
668 VLOG_DBG("bridge %s: %s", ctx
->xbridge
->name
, ds_cstr(&s
));
673 /* If tracing is enabled in 'ctx', appends a node of the given 'type' to the
674 * trace, whose text is 'title' followed by a formatted version of the
675 * 'ofpacts_len' OpenFlow actions in 'ofpacts'.
677 * If tracing is not enabled, does nothing. */
679 xlate_report_actions(const struct xlate_ctx
*ctx
, enum oftrace_node_type type
,
681 const struct ofpact
*ofpacts
, size_t ofpacts_len
)
683 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
684 struct ds s
= DS_EMPTY_INITIALIZER
;
685 ds_put_format(&s
, "%s: ", title
);
686 ofpacts_format(ofpacts
, ofpacts_len
, NULL
, &s
);
687 oftrace_report(ctx
->xin
->trace
, type
, ds_cstr(&s
));
692 /* If tracing is enabled in 'ctx', appends a node of type OFT_DETAIL to the
693 * trace, whose the message is a formatted version of the OpenFlow action set.
694 * 'verb' should be "was" or "is", depending on whether the action set reported
695 * is the new action set or the old one.
697 * If tracing is not enabled, does nothing. */
699 xlate_report_action_set(const struct xlate_ctx
*ctx
, const char *verb
)
701 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
702 struct ofpbuf action_list
;
703 ofpbuf_init(&action_list
, 0);
704 ofpacts_execute_action_set(&action_list
, &ctx
->action_set
);
705 if (action_list
.size
) {
706 struct ds s
= DS_EMPTY_INITIALIZER
;
707 ofpacts_format(action_list
.data
, action_list
.size
, NULL
, &s
);
708 xlate_report(ctx
, OFT_DETAIL
, "action set %s: %s",
712 xlate_report(ctx
, OFT_DETAIL
, "action set %s empty", verb
);
714 ofpbuf_uninit(&action_list
);
719 /* If tracing is enabled in 'ctx', appends a node representing 'rule' (in
720 * OpenFlow table 'table_id') to the trace and makes this node the parent for
721 * future trace nodes. The caller should save ctx->xin->trace before calling
722 * this function, then after tracing all of the activities under the table,
723 * restore its previous value.
725 * If tracing is not enabled, does nothing. */
727 xlate_report_table(const struct xlate_ctx
*ctx
, struct rule_dpif
*rule
,
730 if (OVS_LIKELY(!ctx
->xin
->trace
)) {
734 struct ds s
= DS_EMPTY_INITIALIZER
;
735 ds_put_format(&s
, "%2d. ", table_id
);
736 if (rule
== ctx
->xin
->ofproto
->miss_rule
) {
737 ds_put_cstr(&s
, "No match, and a \"packet-in\" is called for.");
738 } else if (rule
== ctx
->xin
->ofproto
->no_packet_in_rule
) {
739 ds_put_cstr(&s
, "No match.");
740 } else if (rule
== ctx
->xin
->ofproto
->drop_frags_rule
) {
741 ds_put_cstr(&s
, "Packets are IP fragments and "
742 "the fragment handling mode is \"drop\".");
744 minimatch_format(&rule
->up
.cr
.match
,
745 ofproto_get_tun_tab(&ctx
->xin
->ofproto
->up
),
746 NULL
, &s
, OFP_DEFAULT_PRIORITY
);
747 if (ds_last(&s
) != ' ') {
748 ds_put_cstr(&s
, ", ");
750 ds_put_format(&s
, "priority %d", rule
->up
.cr
.priority
);
751 if (rule
->up
.flow_cookie
) {
752 ds_put_format(&s
, ", cookie %#"PRIx64
,
753 ntohll(rule
->up
.flow_cookie
));
756 ctx
->xin
->trace
= &oftrace_report(ctx
->xin
->trace
, OFT_TABLE
,
761 /* If tracing is enabled in 'ctx', adds an OFT_DETAIL trace node to 'ctx'
762 * reporting the value of subfield 'sf'.
764 * If tracing is not enabled, does nothing. */
766 xlate_report_subfield(const struct xlate_ctx
*ctx
,
767 const struct mf_subfield
*sf
)
769 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
770 struct ds s
= DS_EMPTY_INITIALIZER
;
771 mf_format_subfield(sf
, &s
);
772 ds_put_cstr(&s
, " is now ");
774 if (sf
->ofs
== 0 && sf
->n_bits
>= sf
->field
->n_bits
) {
775 union mf_value value
;
776 mf_get_value(sf
->field
, &ctx
->xin
->flow
, &value
);
777 mf_format(sf
->field
, &value
, NULL
, NULL
, &s
);
779 union mf_subvalue cst
;
780 mf_read_subfield(sf
, &ctx
->xin
->flow
, &cst
);
781 ds_put_hex(&s
, &cst
, sizeof cst
);
784 xlate_report(ctx
, OFT_DETAIL
, "%s", ds_cstr(&s
));
791 xlate_xbridge_init(struct xlate_cfg
*xcfg
, struct xbridge
*xbridge
)
793 ovs_list_init(&xbridge
->xbundles
);
794 hmap_init(&xbridge
->xports
);
795 hmap_insert(&xcfg
->xbridges
, &xbridge
->hmap_node
,
796 hash_pointer(xbridge
->ofproto
, 0));
800 xlate_xbundle_init(struct xlate_cfg
*xcfg
, struct xbundle
*xbundle
)
802 ovs_list_init(&xbundle
->xports
);
803 ovs_list_insert(&xbundle
->xbridge
->xbundles
, &xbundle
->list_node
);
804 hmap_insert(&xcfg
->xbundles
, &xbundle
->hmap_node
,
805 hash_pointer(xbundle
->ofbundle
, 0));
809 xlate_xport_init(struct xlate_cfg
*xcfg
, struct xport
*xport
)
811 hmap_init(&xport
->skb_priorities
);
812 hmap_insert(&xcfg
->xports
, &xport
->hmap_node
,
813 hash_pointer(xport
->ofport
, 0));
814 hmap_insert(&xport
->xbridge
->xports
, &xport
->ofp_node
,
815 hash_ofp_port(xport
->ofp_port
));
819 xlate_xbridge_set(struct xbridge
*xbridge
,
821 const struct mac_learning
*ml
, struct stp
*stp
,
822 struct rstp
*rstp
, const struct mcast_snooping
*ms
,
823 const struct mbridge
*mbridge
,
824 const struct dpif_sflow
*sflow
,
825 const struct dpif_ipfix
*ipfix
,
826 const struct netflow
*netflow
,
827 bool forward_bpdu
, bool has_in_band
,
828 const struct dpif_backer_support
*support
)
830 if (xbridge
->ml
!= ml
) {
831 mac_learning_unref(xbridge
->ml
);
832 xbridge
->ml
= mac_learning_ref(ml
);
835 if (xbridge
->ms
!= ms
) {
836 mcast_snooping_unref(xbridge
->ms
);
837 xbridge
->ms
= mcast_snooping_ref(ms
);
840 if (xbridge
->mbridge
!= mbridge
) {
841 mbridge_unref(xbridge
->mbridge
);
842 xbridge
->mbridge
= mbridge_ref(mbridge
);
845 if (xbridge
->sflow
!= sflow
) {
846 dpif_sflow_unref(xbridge
->sflow
);
847 xbridge
->sflow
= dpif_sflow_ref(sflow
);
850 if (xbridge
->ipfix
!= ipfix
) {
851 dpif_ipfix_unref(xbridge
->ipfix
);
852 xbridge
->ipfix
= dpif_ipfix_ref(ipfix
);
855 if (xbridge
->stp
!= stp
) {
856 stp_unref(xbridge
->stp
);
857 xbridge
->stp
= stp_ref(stp
);
860 if (xbridge
->rstp
!= rstp
) {
861 rstp_unref(xbridge
->rstp
);
862 xbridge
->rstp
= rstp_ref(rstp
);
865 if (xbridge
->netflow
!= netflow
) {
866 netflow_unref(xbridge
->netflow
);
867 xbridge
->netflow
= netflow_ref(netflow
);
870 xbridge
->dpif
= dpif
;
871 xbridge
->forward_bpdu
= forward_bpdu
;
872 xbridge
->has_in_band
= has_in_band
;
873 xbridge
->support
= *support
;
877 xlate_xbundle_set(struct xbundle
*xbundle
,
878 enum port_vlan_mode vlan_mode
, uint16_t qinq_ethtype
,
879 int vlan
, unsigned long *trunks
, unsigned long *cvlans
,
880 bool use_priority_tags
,
881 const struct bond
*bond
, const struct lacp
*lacp
,
882 bool floodable
, bool protected)
884 ovs_assert(xbundle
->xbridge
);
886 xbundle
->vlan_mode
= vlan_mode
;
887 xbundle
->qinq_ethtype
= qinq_ethtype
;
888 xbundle
->vlan
= vlan
;
889 xbundle
->trunks
= trunks
;
890 xbundle
->cvlans
= cvlans
;
891 xbundle
->use_priority_tags
= use_priority_tags
;
892 xbundle
->floodable
= floodable
;
893 xbundle
->protected = protected;
895 if (xbundle
->bond
!= bond
) {
896 bond_unref(xbundle
->bond
);
897 xbundle
->bond
= bond_ref(bond
);
900 if (xbundle
->lacp
!= lacp
) {
901 lacp_unref(xbundle
->lacp
);
902 xbundle
->lacp
= lacp_ref(lacp
);
907 xlate_xport_set(struct xport
*xport
, odp_port_t odp_port
,
908 const struct netdev
*netdev
, const struct cfm
*cfm
,
909 const struct bfd
*bfd
, const struct lldp
*lldp
, int stp_port_no
,
910 const struct rstp_port
* rstp_port
,
911 enum ofputil_port_config config
, enum ofputil_port_state state
,
912 bool is_tunnel
, bool may_enable
)
914 xport
->config
= config
;
915 xport
->state
= state
;
916 xport
->stp_port_no
= stp_port_no
;
917 xport
->is_tunnel
= is_tunnel
;
918 xport
->pt_mode
= netdev_get_pt_mode(netdev
);
919 xport
->may_enable
= may_enable
;
920 xport
->odp_port
= odp_port
;
922 if (xport
->rstp_port
!= rstp_port
) {
923 rstp_port_unref(xport
->rstp_port
);
924 xport
->rstp_port
= rstp_port_ref(rstp_port
);
927 if (xport
->cfm
!= cfm
) {
928 cfm_unref(xport
->cfm
);
929 xport
->cfm
= cfm_ref(cfm
);
932 if (xport
->bfd
!= bfd
) {
933 bfd_unref(xport
->bfd
);
934 xport
->bfd
= bfd_ref(bfd
);
937 if (xport
->lldp
!= lldp
) {
938 lldp_unref(xport
->lldp
);
939 xport
->lldp
= lldp_ref(lldp
);
942 if (xport
->netdev
!= netdev
) {
943 netdev_close(xport
->netdev
);
944 xport
->netdev
= netdev_ref(netdev
);
949 xlate_xbridge_copy(struct xbridge
*xbridge
)
951 struct xbundle
*xbundle
;
953 struct xbridge
*new_xbridge
= xzalloc(sizeof *xbridge
);
954 new_xbridge
->ofproto
= xbridge
->ofproto
;
955 new_xbridge
->name
= xstrdup(xbridge
->name
);
956 xlate_xbridge_init(new_xcfg
, new_xbridge
);
958 xlate_xbridge_set(new_xbridge
,
959 xbridge
->dpif
, xbridge
->ml
, xbridge
->stp
,
960 xbridge
->rstp
, xbridge
->ms
, xbridge
->mbridge
,
961 xbridge
->sflow
, xbridge
->ipfix
, xbridge
->netflow
,
962 xbridge
->forward_bpdu
, xbridge
->has_in_band
,
964 LIST_FOR_EACH (xbundle
, list_node
, &xbridge
->xbundles
) {
965 xlate_xbundle_copy(new_xbridge
, xbundle
);
968 /* Copy xports which are not part of a xbundle */
969 HMAP_FOR_EACH (xport
, ofp_node
, &xbridge
->xports
) {
970 if (!xport
->xbundle
) {
971 xlate_xport_copy(new_xbridge
, NULL
, xport
);
977 xlate_xbundle_copy(struct xbridge
*xbridge
, struct xbundle
*xbundle
)
980 struct xbundle
*new_xbundle
= xzalloc(sizeof *xbundle
);
981 new_xbundle
->ofbundle
= xbundle
->ofbundle
;
982 new_xbundle
->xbridge
= xbridge
;
983 new_xbundle
->name
= xstrdup(xbundle
->name
);
984 xlate_xbundle_init(new_xcfg
, new_xbundle
);
986 xlate_xbundle_set(new_xbundle
, xbundle
->vlan_mode
, xbundle
->qinq_ethtype
,
987 xbundle
->vlan
, xbundle
->trunks
, xbundle
->cvlans
,
988 xbundle
->use_priority_tags
, xbundle
->bond
, xbundle
->lacp
,
989 xbundle
->floodable
, xbundle
->protected);
990 LIST_FOR_EACH (xport
, bundle_node
, &xbundle
->xports
) {
991 xlate_xport_copy(xbridge
, new_xbundle
, xport
);
996 xlate_xport_copy(struct xbridge
*xbridge
, struct xbundle
*xbundle
,
999 struct skb_priority_to_dscp
*pdscp
, *new_pdscp
;
1000 struct xport
*new_xport
= xzalloc(sizeof *xport
);
1001 new_xport
->ofport
= xport
->ofport
;
1002 new_xport
->ofp_port
= xport
->ofp_port
;
1003 new_xport
->xbridge
= xbridge
;
1004 xlate_xport_init(new_xcfg
, new_xport
);
1006 xlate_xport_set(new_xport
, xport
->odp_port
, xport
->netdev
, xport
->cfm
,
1007 xport
->bfd
, xport
->lldp
, xport
->stp_port_no
,
1008 xport
->rstp_port
, xport
->config
, xport
->state
,
1009 xport
->is_tunnel
, xport
->may_enable
);
1012 struct xport
*peer
= xport_lookup(new_xcfg
, xport
->peer
->ofport
);
1014 new_xport
->peer
= peer
;
1015 new_xport
->peer
->peer
= new_xport
;
1020 new_xport
->xbundle
= xbundle
;
1021 ovs_list_insert(&new_xport
->xbundle
->xports
, &new_xport
->bundle_node
);
1024 HMAP_FOR_EACH (pdscp
, hmap_node
, &xport
->skb_priorities
) {
1025 new_pdscp
= xmalloc(sizeof *pdscp
);
1026 new_pdscp
->skb_priority
= pdscp
->skb_priority
;
1027 new_pdscp
->dscp
= pdscp
->dscp
;
1028 hmap_insert(&new_xport
->skb_priorities
, &new_pdscp
->hmap_node
,
1029 hash_int(new_pdscp
->skb_priority
, 0));
1033 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
1034 * configuration in xcfgp.
1036 * This needs to be called after editing the xlate configuration.
1038 * Functions that edit the new xlate configuration are
1039 * xlate_<ofproto/bundle/ofport>_set and xlate_<ofproto/bundle/ofport>_remove.
1041 * A sample workflow:
1043 * xlate_txn_start();
1045 * edit_xlate_configuration();
1047 * xlate_txn_commit(); */
1049 xlate_txn_commit(void)
1051 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1053 ovsrcu_set(&xcfgp
, new_xcfg
);
1054 ovsrcu_synchronize();
1055 xlate_xcfg_free(xcfg
);
1059 /* Copies the current xlate configuration in xcfgp to new_xcfg.
1061 * This needs to be called prior to editing the xlate configuration. */
1063 xlate_txn_start(void)
1065 struct xbridge
*xbridge
;
1066 struct xlate_cfg
*xcfg
;
1068 ovs_assert(!new_xcfg
);
1070 new_xcfg
= xmalloc(sizeof *new_xcfg
);
1071 hmap_init(&new_xcfg
->xbridges
);
1072 hmap_init(&new_xcfg
->xbundles
);
1073 hmap_init(&new_xcfg
->xports
);
1075 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1080 HMAP_FOR_EACH (xbridge
, hmap_node
, &xcfg
->xbridges
) {
1081 xlate_xbridge_copy(xbridge
);
1087 xlate_xcfg_free(struct xlate_cfg
*xcfg
)
1089 struct xbridge
*xbridge
, *next_xbridge
;
1095 HMAP_FOR_EACH_SAFE (xbridge
, next_xbridge
, hmap_node
, &xcfg
->xbridges
) {
1096 xlate_xbridge_remove(xcfg
, xbridge
);
1099 hmap_destroy(&xcfg
->xbridges
);
1100 hmap_destroy(&xcfg
->xbundles
);
1101 hmap_destroy(&xcfg
->xports
);
1106 xlate_ofproto_set(struct ofproto_dpif
*ofproto
, const char *name
,
1108 const struct mac_learning
*ml
, struct stp
*stp
,
1109 struct rstp
*rstp
, const struct mcast_snooping
*ms
,
1110 const struct mbridge
*mbridge
,
1111 const struct dpif_sflow
*sflow
,
1112 const struct dpif_ipfix
*ipfix
,
1113 const struct netflow
*netflow
,
1114 bool forward_bpdu
, bool has_in_band
,
1115 const struct dpif_backer_support
*support
)
1117 struct xbridge
*xbridge
;
1119 ovs_assert(new_xcfg
);
1121 xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
1123 xbridge
= xzalloc(sizeof *xbridge
);
1124 xbridge
->ofproto
= ofproto
;
1126 xlate_xbridge_init(new_xcfg
, xbridge
);
1129 free(xbridge
->name
);
1130 xbridge
->name
= xstrdup(name
);
1132 xlate_xbridge_set(xbridge
, dpif
, ml
, stp
, rstp
, ms
, mbridge
, sflow
, ipfix
,
1133 netflow
, forward_bpdu
, has_in_band
, support
);
1137 xlate_xbridge_remove(struct xlate_cfg
*xcfg
, struct xbridge
*xbridge
)
1139 struct xbundle
*xbundle
, *next_xbundle
;
1140 struct xport
*xport
, *next_xport
;
1146 HMAP_FOR_EACH_SAFE (xport
, next_xport
, ofp_node
, &xbridge
->xports
) {
1147 xlate_xport_remove(xcfg
, xport
);
1150 LIST_FOR_EACH_SAFE (xbundle
, next_xbundle
, list_node
, &xbridge
->xbundles
) {
1151 xlate_xbundle_remove(xcfg
, xbundle
);
1154 hmap_remove(&xcfg
->xbridges
, &xbridge
->hmap_node
);
1155 mac_learning_unref(xbridge
->ml
);
1156 mcast_snooping_unref(xbridge
->ms
);
1157 mbridge_unref(xbridge
->mbridge
);
1158 dpif_sflow_unref(xbridge
->sflow
);
1159 dpif_ipfix_unref(xbridge
->ipfix
);
1160 netflow_unref(xbridge
->netflow
);
1161 stp_unref(xbridge
->stp
);
1162 rstp_unref(xbridge
->rstp
);
1163 hmap_destroy(&xbridge
->xports
);
1164 free(xbridge
->name
);
1169 xlate_remove_ofproto(struct ofproto_dpif
*ofproto
)
1171 struct xbridge
*xbridge
;
1173 ovs_assert(new_xcfg
);
1175 xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
1176 xlate_xbridge_remove(new_xcfg
, xbridge
);
1180 xlate_bundle_set(struct ofproto_dpif
*ofproto
, struct ofbundle
*ofbundle
,
1181 const char *name
, enum port_vlan_mode vlan_mode
,
1182 uint16_t qinq_ethtype
, int vlan
,
1183 unsigned long *trunks
, unsigned long *cvlans
,
1184 bool use_priority_tags
,
1185 const struct bond
*bond
, const struct lacp
*lacp
,
1186 bool floodable
, bool protected)
1188 struct xbundle
*xbundle
;
1190 ovs_assert(new_xcfg
);
1192 xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
1194 xbundle
= xzalloc(sizeof *xbundle
);
1195 xbundle
->ofbundle
= ofbundle
;
1196 xbundle
->xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
1198 xlate_xbundle_init(new_xcfg
, xbundle
);
1201 free(xbundle
->name
);
1202 xbundle
->name
= xstrdup(name
);
1204 xlate_xbundle_set(xbundle
, vlan_mode
, qinq_ethtype
, vlan
, trunks
, cvlans
,
1205 use_priority_tags
, bond
, lacp
, floodable
, protected);
1209 xlate_xbundle_remove(struct xlate_cfg
*xcfg
, struct xbundle
*xbundle
)
1211 struct xport
*xport
;
1217 LIST_FOR_EACH_POP (xport
, bundle_node
, &xbundle
->xports
) {
1218 xport
->xbundle
= NULL
;
1221 hmap_remove(&xcfg
->xbundles
, &xbundle
->hmap_node
);
1222 ovs_list_remove(&xbundle
->list_node
);
1223 bond_unref(xbundle
->bond
);
1224 lacp_unref(xbundle
->lacp
);
1225 free(xbundle
->name
);
1230 xlate_bundle_remove(struct ofbundle
*ofbundle
)
1232 struct xbundle
*xbundle
;
1234 ovs_assert(new_xcfg
);
1236 xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
1237 xlate_xbundle_remove(new_xcfg
, xbundle
);
1241 xlate_ofport_set(struct ofproto_dpif
*ofproto
, struct ofbundle
*ofbundle
,
1242 struct ofport_dpif
*ofport
, ofp_port_t ofp_port
,
1243 odp_port_t odp_port
, const struct netdev
*netdev
,
1244 const struct cfm
*cfm
, const struct bfd
*bfd
,
1245 const struct lldp
*lldp
, struct ofport_dpif
*peer
,
1246 int stp_port_no
, const struct rstp_port
*rstp_port
,
1247 const struct ofproto_port_queue
*qdscp_list
, size_t n_qdscp
,
1248 enum ofputil_port_config config
,
1249 enum ofputil_port_state state
, bool is_tunnel
,
1253 struct xport
*xport
;
1255 ovs_assert(new_xcfg
);
1257 xport
= xport_lookup(new_xcfg
, ofport
);
1259 xport
= xzalloc(sizeof *xport
);
1260 xport
->ofport
= ofport
;
1261 xport
->xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
1262 xport
->ofp_port
= ofp_port
;
1264 xlate_xport_init(new_xcfg
, xport
);
1267 ovs_assert(xport
->ofp_port
== ofp_port
);
1269 xlate_xport_set(xport
, odp_port
, netdev
, cfm
, bfd
, lldp
,
1270 stp_port_no
, rstp_port
, config
, state
, is_tunnel
,
1274 xport
->peer
->peer
= NULL
;
1276 xport
->peer
= xport_lookup(new_xcfg
, peer
);
1278 xport
->peer
->peer
= xport
;
1281 if (xport
->xbundle
) {
1282 ovs_list_remove(&xport
->bundle_node
);
1284 xport
->xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
1285 if (xport
->xbundle
) {
1286 ovs_list_insert(&xport
->xbundle
->xports
, &xport
->bundle_node
);
1289 clear_skb_priorities(xport
);
1290 for (i
= 0; i
< n_qdscp
; i
++) {
1291 struct skb_priority_to_dscp
*pdscp
;
1292 uint32_t skb_priority
;
1294 if (dpif_queue_to_priority(xport
->xbridge
->dpif
, qdscp_list
[i
].queue
,
1299 pdscp
= xmalloc(sizeof *pdscp
);
1300 pdscp
->skb_priority
= skb_priority
;
1301 pdscp
->dscp
= (qdscp_list
[i
].dscp
<< 2) & IP_DSCP_MASK
;
1302 hmap_insert(&xport
->skb_priorities
, &pdscp
->hmap_node
,
1303 hash_int(pdscp
->skb_priority
, 0));
1308 xlate_xport_remove(struct xlate_cfg
*xcfg
, struct xport
*xport
)
1315 xport
->peer
->peer
= NULL
;
1319 if (xport
->xbundle
) {
1320 ovs_list_remove(&xport
->bundle_node
);
1323 clear_skb_priorities(xport
);
1324 hmap_destroy(&xport
->skb_priorities
);
1326 hmap_remove(&xcfg
->xports
, &xport
->hmap_node
);
1327 hmap_remove(&xport
->xbridge
->xports
, &xport
->ofp_node
);
1329 netdev_close(xport
->netdev
);
1330 rstp_port_unref(xport
->rstp_port
);
1331 cfm_unref(xport
->cfm
);
1332 bfd_unref(xport
->bfd
);
1333 lldp_unref(xport
->lldp
);
1338 xlate_ofport_remove(struct ofport_dpif
*ofport
)
1340 struct xport
*xport
;
1342 ovs_assert(new_xcfg
);
1344 xport
= xport_lookup(new_xcfg
, ofport
);
1345 xlate_xport_remove(new_xcfg
, xport
);
1348 static struct ofproto_dpif
*
1349 xlate_lookup_ofproto_(const struct dpif_backer
*backer
, const struct flow
*flow
,
1350 ofp_port_t
*ofp_in_port
, const struct xport
**xportp
)
1352 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1353 const struct xport
*xport
;
1355 xport
= xport_lookup(xcfg
, tnl_port_should_receive(flow
)
1356 ? tnl_port_receive(flow
)
1357 : odp_port_to_ofport(backer
, flow
->in_port
.odp_port
));
1358 if (OVS_UNLIKELY(!xport
)) {
1363 *ofp_in_port
= xport
->ofp_port
;
1365 return xport
->xbridge
->ofproto
;
1368 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1369 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1370 struct ofproto_dpif
*
1371 xlate_lookup_ofproto(const struct dpif_backer
*backer
, const struct flow
*flow
,
1372 ofp_port_t
*ofp_in_port
)
1374 const struct xport
*xport
;
1376 return xlate_lookup_ofproto_(backer
, flow
, ofp_in_port
, &xport
);
1379 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1380 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
1381 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1382 * handles for those protocols if they're enabled. Caller may use the returned
1383 * pointers until quiescing, for longer term use additional references must
1386 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1389 xlate_lookup(const struct dpif_backer
*backer
, const struct flow
*flow
,
1390 struct ofproto_dpif
**ofprotop
, struct dpif_ipfix
**ipfix
,
1391 struct dpif_sflow
**sflow
, struct netflow
**netflow
,
1392 ofp_port_t
*ofp_in_port
)
1394 struct ofproto_dpif
*ofproto
;
1395 const struct xport
*xport
;
1397 ofproto
= xlate_lookup_ofproto_(backer
, flow
, ofp_in_port
, &xport
);
1404 *ofprotop
= ofproto
;
1408 *ipfix
= xport
? xport
->xbridge
->ipfix
: NULL
;
1412 *sflow
= xport
? xport
->xbridge
->sflow
: NULL
;
1416 *netflow
= xport
? xport
->xbridge
->netflow
: NULL
;
1422 static struct xbridge
*
1423 xbridge_lookup(struct xlate_cfg
*xcfg
, const struct ofproto_dpif
*ofproto
)
1425 struct hmap
*xbridges
;
1426 struct xbridge
*xbridge
;
1428 if (!ofproto
|| !xcfg
) {
1432 xbridges
= &xcfg
->xbridges
;
1434 HMAP_FOR_EACH_IN_BUCKET (xbridge
, hmap_node
, hash_pointer(ofproto
, 0),
1436 if (xbridge
->ofproto
== ofproto
) {
1443 static struct xbridge
*
1444 xbridge_lookup_by_uuid(struct xlate_cfg
*xcfg
, const struct uuid
*uuid
)
1446 struct xbridge
*xbridge
;
1448 HMAP_FOR_EACH (xbridge
, hmap_node
, &xcfg
->xbridges
) {
1449 if (uuid_equals(&xbridge
->ofproto
->uuid
, uuid
)) {
1456 static struct xbundle
*
1457 xbundle_lookup(struct xlate_cfg
*xcfg
, const struct ofbundle
*ofbundle
)
1459 struct hmap
*xbundles
;
1460 struct xbundle
*xbundle
;
1462 if (!ofbundle
|| !xcfg
) {
1466 xbundles
= &xcfg
->xbundles
;
1468 HMAP_FOR_EACH_IN_BUCKET (xbundle
, hmap_node
, hash_pointer(ofbundle
, 0),
1470 if (xbundle
->ofbundle
== ofbundle
) {
1477 static struct xport
*
1478 xport_lookup(struct xlate_cfg
*xcfg
, const struct ofport_dpif
*ofport
)
1480 struct hmap
*xports
;
1481 struct xport
*xport
;
1483 if (!ofport
|| !xcfg
) {
1487 xports
= &xcfg
->xports
;
1489 HMAP_FOR_EACH_IN_BUCKET (xport
, hmap_node
, hash_pointer(ofport
, 0),
1491 if (xport
->ofport
== ofport
) {
1498 static struct stp_port
*
1499 xport_get_stp_port(const struct xport
*xport
)
1501 return xport
->xbridge
->stp
&& xport
->stp_port_no
!= -1
1502 ? stp_get_port(xport
->xbridge
->stp
, xport
->stp_port_no
)
1507 xport_stp_learn_state(const struct xport
*xport
)
1509 struct stp_port
*sp
= xport_get_stp_port(xport
);
1511 ? stp_learn_in_state(stp_port_get_state(sp
))
1516 xport_stp_forward_state(const struct xport
*xport
)
1518 struct stp_port
*sp
= xport_get_stp_port(xport
);
1520 ? stp_forward_in_state(stp_port_get_state(sp
))
1525 xport_stp_should_forward_bpdu(const struct xport
*xport
)
1527 struct stp_port
*sp
= xport_get_stp_port(xport
);
1528 return stp_should_forward_bpdu(sp
? stp_port_get_state(sp
) : STP_DISABLED
);
1531 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1532 * were used to make the determination.*/
1534 stp_should_process_flow(const struct flow
*flow
, struct flow_wildcards
*wc
)
1536 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1537 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
1538 return is_stp(flow
);
1542 stp_process_packet(const struct xport
*xport
, const struct dp_packet
*packet
)
1544 struct stp_port
*sp
= xport_get_stp_port(xport
);
1545 struct dp_packet payload
= *packet
;
1546 struct eth_header
*eth
= dp_packet_data(&payload
);
1548 /* Sink packets on ports that have STP disabled when the bridge has
1550 if (!sp
|| stp_port_get_state(sp
) == STP_DISABLED
) {
1554 /* Trim off padding on payload. */
1555 if (dp_packet_size(&payload
) > ntohs(eth
->eth_type
) + ETH_HEADER_LEN
) {
1556 dp_packet_set_size(&payload
, ntohs(eth
->eth_type
) + ETH_HEADER_LEN
);
1559 if (dp_packet_try_pull(&payload
, ETH_HEADER_LEN
+ LLC_HEADER_LEN
)) {
1560 stp_received_bpdu(sp
, dp_packet_data(&payload
), dp_packet_size(&payload
));
1564 static enum rstp_state
1565 xport_get_rstp_port_state(const struct xport
*xport
)
1567 return xport
->rstp_port
1568 ? rstp_port_get_state(xport
->rstp_port
)
1573 xport_rstp_learn_state(const struct xport
*xport
)
1575 return xport
->xbridge
->rstp
&& xport
->rstp_port
1576 ? rstp_learn_in_state(xport_get_rstp_port_state(xport
))
1581 xport_rstp_forward_state(const struct xport
*xport
)
1583 return xport
->xbridge
->rstp
&& xport
->rstp_port
1584 ? rstp_forward_in_state(xport_get_rstp_port_state(xport
))
1589 xport_rstp_should_manage_bpdu(const struct xport
*xport
)
1591 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport
));
1595 rstp_process_packet(const struct xport
*xport
, const struct dp_packet
*packet
)
1597 struct dp_packet payload
= *packet
;
1598 struct eth_header
*eth
= dp_packet_data(&payload
);
1600 /* Sink packets on ports that have no RSTP. */
1601 if (!xport
->rstp_port
) {
1605 /* Trim off padding on payload. */
1606 if (dp_packet_size(&payload
) > ntohs(eth
->eth_type
) + ETH_HEADER_LEN
) {
1607 dp_packet_set_size(&payload
, ntohs(eth
->eth_type
) + ETH_HEADER_LEN
);
1610 if (dp_packet_try_pull(&payload
, ETH_HEADER_LEN
+ LLC_HEADER_LEN
)) {
1611 rstp_port_received_bpdu(xport
->rstp_port
, dp_packet_data(&payload
),
1612 dp_packet_size(&payload
));
1616 static struct xport
*
1617 get_ofp_port(const struct xbridge
*xbridge
, ofp_port_t ofp_port
)
1619 struct xport
*xport
;
1621 HMAP_FOR_EACH_IN_BUCKET (xport
, ofp_node
, hash_ofp_port(ofp_port
),
1623 if (xport
->ofp_port
== ofp_port
) {
1631 ofp_port_to_odp_port(const struct xbridge
*xbridge
, ofp_port_t ofp_port
)
1633 const struct xport
*xport
= get_ofp_port(xbridge
, ofp_port
);
1634 return xport
? xport
->odp_port
: ODPP_NONE
;
1638 odp_port_is_alive(const struct xlate_ctx
*ctx
, ofp_port_t ofp_port
)
1640 struct xport
*xport
= get_ofp_port(ctx
->xbridge
, ofp_port
);
1641 return xport
&& xport
->may_enable
;
1644 static struct ofputil_bucket
*
1645 group_first_live_bucket(const struct xlate_ctx
*, const struct group_dpif
*,
1649 group_is_alive(const struct xlate_ctx
*ctx
, uint32_t group_id
, int depth
)
1651 struct group_dpif
*group
;
1653 group
= group_dpif_lookup(ctx
->xbridge
->ofproto
, group_id
,
1654 ctx
->xin
->tables_version
, false);
1656 return group_first_live_bucket(ctx
, group
, depth
) != NULL
;
1662 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1665 bucket_is_alive(const struct xlate_ctx
*ctx
,
1666 struct ofputil_bucket
*bucket
, int depth
)
1668 if (depth
>= MAX_LIVENESS_RECURSION
) {
1669 xlate_report_error(ctx
, "bucket chaining exceeded %d links",
1670 MAX_LIVENESS_RECURSION
);
1674 return (!ofputil_bucket_has_liveness(bucket
)
1675 || (bucket
->watch_port
!= OFPP_ANY
1676 && odp_port_is_alive(ctx
, bucket
->watch_port
))
1677 || (bucket
->watch_group
!= OFPG_ANY
1678 && group_is_alive(ctx
, bucket
->watch_group
, depth
+ 1)));
1681 static struct ofputil_bucket
*
1682 group_first_live_bucket(const struct xlate_ctx
*ctx
,
1683 const struct group_dpif
*group
, int depth
)
1685 struct ofputil_bucket
*bucket
;
1686 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
1687 if (bucket_is_alive(ctx
, bucket
, depth
)) {
1695 static struct ofputil_bucket
*
1696 group_best_live_bucket(const struct xlate_ctx
*ctx
,
1697 const struct group_dpif
*group
,
1700 struct ofputil_bucket
*best_bucket
= NULL
;
1701 uint32_t best_score
= 0;
1703 struct ofputil_bucket
*bucket
;
1704 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
1705 if (bucket_is_alive(ctx
, bucket
, 0)) {
1707 (hash_int(bucket
->bucket_id
, basis
) & 0xffff) * bucket
->weight
;
1708 if (score
>= best_score
) {
1709 best_bucket
= bucket
;
1719 xbundle_trunks_vlan(const struct xbundle
*bundle
, uint16_t vlan
)
1721 return (bundle
->vlan_mode
!= PORT_VLAN_ACCESS
1722 && (!bundle
->trunks
|| bitmap_is_set(bundle
->trunks
, vlan
)));
1726 xbundle_allows_cvlan(const struct xbundle
*bundle
, uint16_t vlan
)
1728 return (!bundle
->cvlans
|| bitmap_is_set(bundle
->cvlans
, vlan
));
1732 xbundle_includes_vlan(const struct xbundle
*xbundle
, const struct xvlan
*xvlan
)
1734 switch (xbundle
->vlan_mode
) {
1735 case PORT_VLAN_ACCESS
:
1736 return xvlan
->v
[0].vid
== xbundle
->vlan
&& xvlan
->v
[1].vid
== 0;
1738 case PORT_VLAN_TRUNK
:
1739 case PORT_VLAN_NATIVE_UNTAGGED
:
1740 case PORT_VLAN_NATIVE_TAGGED
:
1741 return xbundle_trunks_vlan(xbundle
, xvlan
->v
[0].vid
);
1743 case PORT_VLAN_DOT1Q_TUNNEL
:
1744 return xvlan
->v
[0].vid
== xbundle
->vlan
&&
1745 xbundle_allows_cvlan(xbundle
, xvlan
->v
[1].vid
);
1752 static mirror_mask_t
1753 xbundle_mirror_out(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1755 return xbundle
!= &ofpp_none_bundle
1756 ? mirror_bundle_out(xbridge
->mbridge
, xbundle
->ofbundle
)
1760 static mirror_mask_t
1761 xbundle_mirror_src(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1763 return xbundle
!= &ofpp_none_bundle
1764 ? mirror_bundle_src(xbridge
->mbridge
, xbundle
->ofbundle
)
1768 static mirror_mask_t
1769 xbundle_mirror_dst(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1771 return xbundle
!= &ofpp_none_bundle
1772 ? mirror_bundle_dst(xbridge
->mbridge
, xbundle
->ofbundle
)
1776 static struct xbundle
*
1777 lookup_input_bundle__(const struct xbridge
*xbridge
,
1778 ofp_port_t in_port
, struct xport
**in_xportp
)
1780 struct xport
*xport
;
1782 /* Find the port and bundle for the received packet. */
1783 xport
= get_ofp_port(xbridge
, in_port
);
1787 if (xport
&& xport
->xbundle
) {
1788 return xport
->xbundle
;
1791 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1792 * which a controller may use as the ingress port for traffic that
1793 * it is sourcing. */
1794 if (in_port
== OFPP_CONTROLLER
|| in_port
== OFPP_NONE
) {
1795 return &ofpp_none_bundle
;
1800 static struct xbundle
*
1801 lookup_input_bundle(const struct xlate_ctx
*ctx
,
1802 ofp_port_t in_port
, struct xport
**in_xportp
)
1804 struct xbundle
*xbundle
= lookup_input_bundle__(ctx
->xbridge
,
1805 in_port
, in_xportp
);
1807 /* Odd. A few possible reasons here:
1809 * - We deleted a port but there are still a few packets queued up
1812 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1813 * we don't know about.
1815 * - The ofproto client didn't configure the port as part of a bundle.
1816 * This is particularly likely to happen if a packet was received on
1817 * the port after it was created, but before the client had a chance
1818 * to configure its bundle.
1820 xlate_report_error(ctx
, "received packet on unknown port %"PRIu32
,
1826 /* Mirrors the packet represented by 'ctx' to appropriate mirror destinations,
1827 * given the packet is ingressing or egressing on 'xbundle', which has ingress
1828 * or egress (as appropriate) mirrors 'mirrors'. */
1830 mirror_packet(struct xlate_ctx
*ctx
, struct xbundle
*xbundle
,
1831 mirror_mask_t mirrors
)
1833 struct xvlan in_xvlan
;
1836 /* Figure out what VLAN the packet is in (because mirrors can select
1837 * packets on basis of VLAN). */
1838 xvlan_extract(&ctx
->xin
->flow
, &in_xvlan
);
1839 if (!input_vid_is_valid(ctx
, in_xvlan
.v
[0].vid
, xbundle
)) {
1842 xvlan_input_translate(xbundle
, &in_xvlan
, &xvlan
);
1844 const struct xbridge
*xbridge
= ctx
->xbridge
;
1846 /* Don't mirror to destinations that we've already mirrored to. */
1847 mirrors
&= ~ctx
->mirrors
;
1852 if (ctx
->xin
->resubmit_stats
) {
1853 mirror_update_stats(xbridge
->mbridge
, mirrors
,
1854 ctx
->xin
->resubmit_stats
->n_packets
,
1855 ctx
->xin
->resubmit_stats
->n_bytes
);
1857 if (ctx
->xin
->xcache
) {
1858 struct xc_entry
*entry
;
1860 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_MIRROR
);
1861 entry
->mirror
.mbridge
= mbridge_ref(xbridge
->mbridge
);
1862 entry
->mirror
.mirrors
= mirrors
;
1865 /* 'mirrors' is a bit-mask of candidates for mirroring. Iterate as long as
1866 * some candidates remain. */
1868 const unsigned long *vlans
;
1869 mirror_mask_t dup_mirrors
;
1870 struct ofbundle
*out
;
1874 /* Get the details of the mirror represented by the rightmost 1-bit. */
1875 bool has_mirror
= mirror_get(xbridge
->mbridge
, raw_ctz(mirrors
),
1876 &vlans
, &dup_mirrors
,
1877 &out
, &snaplen
, &out_vlan
);
1878 ovs_assert(has_mirror
);
1881 /* If this mirror selects on the basis of VLAN, and it does not select
1882 * 'vlan', then discard this mirror and go on to the next one. */
1884 ctx
->wc
->masks
.vlans
[0].tci
|= htons(VLAN_CFI
| VLAN_VID_MASK
);
1886 if (vlans
&& !bitmap_is_set(vlans
, xvlan
.v
[0].vid
)) {
1887 mirrors
= zero_rightmost_1bit(mirrors
);
1891 /* Record the mirror, and the mirrors that output to the same
1892 * destination, so that we don't mirror to them again. This must be
1893 * done now to ensure that output_normal(), below, doesn't recursively
1894 * output to the same mirrors. */
1895 ctx
->mirrors
|= dup_mirrors
;
1896 ctx
->mirror_snaplen
= snaplen
;
1898 /* Send the packet to the mirror. */
1900 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1901 struct xbundle
*out_xbundle
= xbundle_lookup(xcfg
, out
);
1903 output_normal(ctx
, out_xbundle
, &xvlan
);
1905 } else if (xvlan
.v
[0].vid
!= out_vlan
1906 && !eth_addr_is_reserved(ctx
->xin
->flow
.dl_dst
)) {
1908 uint16_t old_vid
= xvlan
.v
[0].vid
;
1910 xvlan
.v
[0].vid
= out_vlan
;
1911 LIST_FOR_EACH (xb
, list_node
, &xbridge
->xbundles
) {
1912 if (xbundle_includes_vlan(xb
, &xvlan
)
1913 && !xbundle_mirror_out(xbridge
, xb
)) {
1914 output_normal(ctx
, xb
, &xvlan
);
1917 xvlan
.v
[0].vid
= old_vid
;
1920 /* output_normal() could have recursively output (to different
1921 * mirrors), so make sure that we don't send duplicates. */
1922 mirrors
&= ~ctx
->mirrors
;
1923 ctx
->mirror_snaplen
= 0;
1928 mirror_ingress_packet(struct xlate_ctx
*ctx
)
1930 if (mbridge_has_mirrors(ctx
->xbridge
->mbridge
)) {
1931 struct xbundle
*xbundle
= lookup_input_bundle(
1932 ctx
, ctx
->xin
->flow
.in_port
.ofp_port
, NULL
);
1934 mirror_packet(ctx
, xbundle
,
1935 xbundle_mirror_src(ctx
->xbridge
, xbundle
));
1940 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1941 * If so, returns true. Otherwise, returns false.
1943 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1944 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1947 input_vid_is_valid(const struct xlate_ctx
*ctx
,
1948 uint16_t vid
, struct xbundle
*in_xbundle
)
1950 /* Allow any VID on the OFPP_NONE port. */
1951 if (in_xbundle
== &ofpp_none_bundle
) {
1955 switch (in_xbundle
->vlan_mode
) {
1956 case PORT_VLAN_ACCESS
:
1958 xlate_report_error(ctx
, "dropping VLAN %"PRIu16
" tagged "
1959 "packet received on port %s configured as VLAN "
1960 "%d access port", vid
, in_xbundle
->name
,
1966 case PORT_VLAN_NATIVE_UNTAGGED
:
1967 case PORT_VLAN_NATIVE_TAGGED
:
1969 /* Port must always carry its native VLAN. */
1973 case PORT_VLAN_TRUNK
:
1974 if (!xbundle_trunks_vlan(in_xbundle
, vid
)) {
1975 xlate_report_error(ctx
, "dropping VLAN %"PRIu16
" packet "
1976 "received on port %s not configured for "
1977 "trunking VLAN %"PRIu16
,
1978 vid
, in_xbundle
->name
, vid
);
1983 case PORT_VLAN_DOT1Q_TUNNEL
:
1984 if (!xbundle_allows_cvlan(in_xbundle
, vid
)) {
1985 xlate_report_error(ctx
, "dropping VLAN %"PRIu16
" packet received "
1986 "on dot1q-tunnel port %s that excludes this "
1987 "VLAN", vid
, in_xbundle
->name
);
1999 xvlan_copy(struct xvlan
*dst
, const struct xvlan
*src
)
2005 xvlan_pop(struct xvlan
*src
)
2007 memmove(&src
->v
[0], &src
->v
[1], sizeof(src
->v
) - sizeof(src
->v
[0]));
2008 memset(&src
->v
[FLOW_MAX_VLAN_HEADERS
- 1], 0,
2009 sizeof(src
->v
[FLOW_MAX_VLAN_HEADERS
- 1]));
2013 xvlan_push_uninit(struct xvlan
*src
)
2015 memmove(&src
->v
[1], &src
->v
[0], sizeof(src
->v
) - sizeof(src
->v
[0]));
2016 memset(&src
->v
[0], 0, sizeof(src
->v
[0]));
2019 /* Extract VLAN information (headers) from flow */
2021 xvlan_extract(const struct flow
*flow
, struct xvlan
*xvlan
)
2024 memset(xvlan
, 0, sizeof(*xvlan
));
2025 for (i
= 0; i
< FLOW_MAX_VLAN_HEADERS
; i
++) {
2026 if (!eth_type_vlan(flow
->vlans
[i
].tpid
) ||
2027 !(flow
->vlans
[i
].tci
& htons(VLAN_CFI
))) {
2030 xvlan
->v
[i
].tpid
= ntohs(flow
->vlans
[i
].tpid
);
2031 xvlan
->v
[i
].vid
= vlan_tci_to_vid(flow
->vlans
[i
].tci
);
2032 xvlan
->v
[i
].pcp
= ntohs(flow
->vlans
[i
].tci
) & VLAN_PCP_MASK
;
2036 /* Put VLAN information (headers) to flow */
2038 xvlan_put(struct flow
*flow
, const struct xvlan
*xvlan
)
2042 for (i
= 0; i
< FLOW_MAX_VLAN_HEADERS
; i
++) {
2043 tci
= htons(xvlan
->v
[i
].vid
| (xvlan
->v
[i
].pcp
& VLAN_PCP_MASK
));
2045 tci
|= htons(VLAN_CFI
);
2046 flow
->vlans
[i
].tpid
= xvlan
->v
[i
].tpid
?
2047 htons(xvlan
->v
[i
].tpid
) :
2048 htons(ETH_TYPE_VLAN_8021Q
);
2050 flow
->vlans
[i
].tci
= tci
;
2054 /* Given 'in_xvlan', extracted from the input 802.1Q headers received as part
2055 * of a packet, and 'in_xbundle', the bundle on which the packet was received,
2056 * returns the VLANs of the packet during bridge internal processing. */
2058 xvlan_input_translate(const struct xbundle
*in_xbundle
,
2059 const struct xvlan
*in_xvlan
, struct xvlan
*xvlan
)
2062 switch (in_xbundle
->vlan_mode
) {
2063 case PORT_VLAN_ACCESS
:
2064 memset(xvlan
, 0, sizeof(*xvlan
));
2065 xvlan
->v
[0].tpid
= in_xvlan
->v
[0].tpid
? in_xvlan
->v
[0].tpid
:
2066 ETH_TYPE_VLAN_8021Q
;
2067 xvlan
->v
[0].vid
= in_xbundle
->vlan
;
2068 xvlan
->v
[0].pcp
= in_xvlan
->v
[0].pcp
;
2071 case PORT_VLAN_TRUNK
:
2072 xvlan_copy(xvlan
, in_xvlan
);
2075 case PORT_VLAN_NATIVE_UNTAGGED
:
2076 case PORT_VLAN_NATIVE_TAGGED
:
2077 xvlan_copy(xvlan
, in_xvlan
);
2078 if (!in_xvlan
->v
[0].vid
) {
2079 xvlan
->v
[0].tpid
= in_xvlan
->v
[0].tpid
? in_xvlan
->v
[0].tpid
:
2080 ETH_TYPE_VLAN_8021Q
;
2081 xvlan
->v
[0].vid
= in_xbundle
->vlan
;
2082 xvlan
->v
[0].pcp
= in_xvlan
->v
[0].pcp
;
2086 case PORT_VLAN_DOT1Q_TUNNEL
:
2087 xvlan_copy(xvlan
, in_xvlan
);
2088 xvlan_push_uninit(xvlan
);
2089 xvlan
->v
[0].tpid
= in_xbundle
->qinq_ethtype
;
2090 xvlan
->v
[0].vid
= in_xbundle
->vlan
;
2091 xvlan
->v
[0].pcp
= 0;
2099 /* Given 'xvlan', the VLANs of a packet during internal processing, and
2100 * 'out_xbundle', a bundle on which the packet is to be output, returns the
2101 * VLANs that should be included in output packet. */
2103 xvlan_output_translate(const struct xbundle
*out_xbundle
,
2104 const struct xvlan
*xvlan
, struct xvlan
*out_xvlan
)
2106 switch (out_xbundle
->vlan_mode
) {
2107 case PORT_VLAN_ACCESS
:
2108 memset(out_xvlan
, 0, sizeof(*out_xvlan
));
2111 case PORT_VLAN_TRUNK
:
2112 case PORT_VLAN_NATIVE_TAGGED
:
2113 xvlan_copy(out_xvlan
, xvlan
);
2116 case PORT_VLAN_NATIVE_UNTAGGED
:
2117 xvlan_copy(out_xvlan
, xvlan
);
2118 if (xvlan
->v
[0].vid
== out_xbundle
->vlan
) {
2119 xvlan_pop(out_xvlan
);
2123 case PORT_VLAN_DOT1Q_TUNNEL
:
2124 xvlan_copy(out_xvlan
, xvlan
);
2125 xvlan_pop(out_xvlan
);
2133 /* If output xbundle is dot1q-tunnel, set mask bits of cvlan */
2135 check_and_set_cvlan_mask(struct flow_wildcards
*wc
,
2136 const struct xbundle
*xbundle
)
2138 if (xbundle
->vlan_mode
== PORT_VLAN_DOT1Q_TUNNEL
&& xbundle
->cvlans
) {
2139 wc
->masks
.vlans
[1].tci
= htons(0xffff);
2144 output_normal(struct xlate_ctx
*ctx
, const struct xbundle
*out_xbundle
,
2145 const struct xvlan
*xvlan
)
2148 union flow_vlan_hdr old_vlans
[FLOW_MAX_VLAN_HEADERS
];
2149 struct xport
*xport
;
2150 struct xlate_bond_recirc xr
;
2151 bool use_recirc
= false;
2152 struct xvlan out_xvlan
;
2154 check_and_set_cvlan_mask(ctx
->wc
, out_xbundle
);
2156 xvlan_output_translate(out_xbundle
, xvlan
, &out_xvlan
);
2157 if (out_xbundle
->use_priority_tags
) {
2158 out_xvlan
.v
[0].pcp
= ntohs(ctx
->xin
->flow
.vlans
[0].tci
) &
2161 vid
= out_xvlan
.v
[0].vid
;
2162 if (ovs_list_is_empty(&out_xbundle
->xports
)) {
2163 /* Partially configured bundle with no slaves. Drop the packet. */
2165 } else if (!out_xbundle
->bond
) {
2166 xport
= CONTAINER_OF(ovs_list_front(&out_xbundle
->xports
), struct xport
,
2169 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2170 struct flow_wildcards
*wc
= ctx
->wc
;
2171 struct ofport_dpif
*ofport
;
2173 if (ctx
->xbridge
->support
.odp
.recirc
) {
2174 /* In case recirculation is not actually in use, 'xr.recirc_id'
2175 * will be set to '0', since a valid 'recirc_id' can
2177 bond_update_post_recirc_rules(out_xbundle
->bond
,
2181 /* Use recirculation instead of output. */
2183 xr
.hash_alg
= OVS_HASH_ALG_L4
;
2184 /* Recirculation does not require unmasking hash fields. */
2189 ofport
= bond_choose_output_slave(out_xbundle
->bond
,
2190 &ctx
->xin
->flow
, wc
, vid
);
2191 xport
= xport_lookup(xcfg
, ofport
);
2194 /* No slaves enabled, so drop packet. */
2198 /* If use_recirc is set, the main thread will handle stats
2199 * accounting for this bond. */
2201 if (ctx
->xin
->resubmit_stats
) {
2202 bond_account(out_xbundle
->bond
, &ctx
->xin
->flow
, vid
,
2203 ctx
->xin
->resubmit_stats
->n_bytes
);
2205 if (ctx
->xin
->xcache
) {
2206 struct xc_entry
*entry
;
2209 flow
= &ctx
->xin
->flow
;
2210 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_BOND
);
2211 entry
->bond
.bond
= bond_ref(out_xbundle
->bond
);
2212 entry
->bond
.flow
= xmemdup(flow
, sizeof *flow
);
2213 entry
->bond
.vid
= vid
;
2218 memcpy(&old_vlans
, &ctx
->xin
->flow
.vlans
, sizeof(old_vlans
));
2219 xvlan_put(&ctx
->xin
->flow
, &out_xvlan
);
2221 compose_output_action(ctx
, xport
->ofp_port
, use_recirc
? &xr
: NULL
);
2222 memcpy(&ctx
->xin
->flow
.vlans
, &old_vlans
, sizeof(old_vlans
));
2225 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
2226 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
2227 * indicate this; newer upstream kernels use gratuitous ARP requests. */
2229 is_gratuitous_arp(const struct flow
*flow
, struct flow_wildcards
*wc
)
2231 if (flow
->dl_type
!= htons(ETH_TYPE_ARP
)) {
2235 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
2236 if (!eth_addr_is_broadcast(flow
->dl_dst
)) {
2240 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
2241 if (flow
->nw_proto
== ARP_OP_REPLY
) {
2243 } else if (flow
->nw_proto
== ARP_OP_REQUEST
) {
2244 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
2245 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
2247 return flow
->nw_src
== flow
->nw_dst
;
2253 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
2254 * dropped. Returns true if they may be forwarded, false if they should be
2257 * 'in_port' must be the xport that corresponds to flow->in_port.
2258 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
2260 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
2261 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
2262 * checked by input_vid_is_valid().
2264 * May also add tags to '*tags', although the current implementation only does
2265 * so in one special case.
2268 is_admissible(struct xlate_ctx
*ctx
, struct xport
*in_port
,
2271 struct xbundle
*in_xbundle
= in_port
->xbundle
;
2272 const struct xbridge
*xbridge
= ctx
->xbridge
;
2273 struct flow
*flow
= &ctx
->xin
->flow
;
2275 /* Drop frames for reserved multicast addresses
2276 * only if forward_bpdu option is absent. */
2277 if (!xbridge
->forward_bpdu
&& eth_addr_is_reserved(flow
->dl_dst
)) {
2278 xlate_report(ctx
, OFT_DETAIL
,
2279 "packet has reserved destination MAC, dropping");
2283 if (in_xbundle
->bond
) {
2284 struct mac_entry
*mac
;
2286 switch (bond_check_admissibility(in_xbundle
->bond
, in_port
->ofport
,
2292 xlate_report(ctx
, OFT_DETAIL
,
2293 "bonding refused admissibility, dropping");
2296 case BV_DROP_IF_MOVED
:
2297 ovs_rwlock_rdlock(&xbridge
->ml
->rwlock
);
2298 mac
= mac_learning_lookup(xbridge
->ml
, flow
->dl_src
, vlan
);
2300 && mac_entry_get_port(xbridge
->ml
, mac
) != in_xbundle
->ofbundle
2301 && (!is_gratuitous_arp(flow
, ctx
->wc
)
2302 || mac_entry_is_grat_arp_locked(mac
))) {
2303 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
2304 xlate_report(ctx
, OFT_DETAIL
,
2305 "SLB bond thinks this packet looped back, "
2309 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
2318 update_learning_table__(const struct xbridge
*xbridge
,
2319 struct xbundle
*in_xbundle
, struct eth_addr dl_src
,
2320 int vlan
, bool is_grat_arp
)
2322 return (in_xbundle
== &ofpp_none_bundle
2323 || !mac_learning_update(xbridge
->ml
, dl_src
, vlan
,
2325 in_xbundle
->bond
!= NULL
,
2326 in_xbundle
->ofbundle
));
2330 update_learning_table(const struct xlate_ctx
*ctx
,
2331 struct xbundle
*in_xbundle
, struct eth_addr dl_src
,
2332 int vlan
, bool is_grat_arp
)
2334 if (!update_learning_table__(ctx
->xbridge
, in_xbundle
, dl_src
, vlan
,
2336 xlate_report_debug(ctx
, OFT_DETAIL
, "learned that "ETH_ADDR_FMT
" is "
2337 "on port %s in VLAN %d",
2338 ETH_ADDR_ARGS(dl_src
), in_xbundle
->name
, vlan
);
2342 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2343 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
2345 update_mcast_snooping_table4__(const struct xlate_ctx
*ctx
,
2346 const struct flow
*flow
,
2347 struct mcast_snooping
*ms
, int vlan
,
2348 struct xbundle
*in_xbundle
,
2349 const struct dp_packet
*packet
)
2350 OVS_REQ_WRLOCK(ms
->rwlock
)
2352 const struct igmp_header
*igmp
;
2355 ovs_be32 ip4
= flow
->igmp_group_ip4
;
2357 offset
= (char *) dp_packet_l4(packet
) - (char *) dp_packet_data(packet
);
2358 igmp
= dp_packet_at(packet
, offset
, IGMP_HEADER_LEN
);
2359 if (!igmp
|| csum(igmp
, dp_packet_l4_size(packet
)) != 0) {
2360 xlate_report_debug(ctx
, OFT_DETAIL
,
2361 "multicast snooping received bad IGMP "
2362 "checksum on port %s in VLAN %d",
2363 in_xbundle
->name
, vlan
);
2367 switch (ntohs(flow
->tp_src
)) {
2368 case IGMP_HOST_MEMBERSHIP_REPORT
:
2369 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
2370 if (mcast_snooping_add_group4(ms
, ip4
, vlan
, in_xbundle
->ofbundle
)) {
2371 xlate_report_debug(ctx
, OFT_DETAIL
,
2372 "multicast snooping learned that "
2373 IP_FMT
" is on port %s in VLAN %d",
2374 IP_ARGS(ip4
), in_xbundle
->name
, vlan
);
2377 case IGMP_HOST_LEAVE_MESSAGE
:
2378 if (mcast_snooping_leave_group4(ms
, ip4
, vlan
, in_xbundle
->ofbundle
)) {
2379 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping leaving "
2380 IP_FMT
" is on port %s in VLAN %d",
2381 IP_ARGS(ip4
), in_xbundle
->name
, vlan
);
2384 case IGMP_HOST_MEMBERSHIP_QUERY
:
2385 if (flow
->nw_src
&& mcast_snooping_add_mrouter(ms
, vlan
,
2386 in_xbundle
->ofbundle
)) {
2387 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping query "
2388 "from "IP_FMT
" is on port %s in VLAN %d",
2389 IP_ARGS(flow
->nw_src
), in_xbundle
->name
, vlan
);
2392 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
2393 count
= mcast_snooping_add_report(ms
, packet
, vlan
,
2394 in_xbundle
->ofbundle
);
2396 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping processed "
2397 "%d addresses on port %s in VLAN %d",
2398 count
, in_xbundle
->name
, vlan
);
2405 update_mcast_snooping_table6__(const struct xlate_ctx
*ctx
,
2406 const struct flow
*flow
,
2407 struct mcast_snooping
*ms
, int vlan
,
2408 struct xbundle
*in_xbundle
,
2409 const struct dp_packet
*packet
)
2410 OVS_REQ_WRLOCK(ms
->rwlock
)
2412 const struct mld_header
*mld
;
2416 offset
= (char *) dp_packet_l4(packet
) - (char *) dp_packet_data(packet
);
2417 mld
= dp_packet_at(packet
, offset
, MLD_HEADER_LEN
);
2420 packet_csum_upperlayer6(dp_packet_l3(packet
),
2421 mld
, IPPROTO_ICMPV6
,
2422 dp_packet_l4_size(packet
)) != 0) {
2423 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping received "
2424 "bad MLD checksum on port %s in VLAN %d",
2425 in_xbundle
->name
, vlan
);
2429 switch (ntohs(flow
->tp_src
)) {
2431 if (!ipv6_addr_equals(&flow
->ipv6_src
, &in6addr_any
)
2432 && mcast_snooping_add_mrouter(ms
, vlan
, in_xbundle
->ofbundle
)) {
2433 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping query on "
2434 "port %s in VLAN %d", in_xbundle
->name
, vlan
);
2440 count
= mcast_snooping_add_mld(ms
, packet
, vlan
, in_xbundle
->ofbundle
);
2442 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping processed "
2443 "%d addresses on port %s in VLAN %d",
2444 count
, in_xbundle
->name
, vlan
);
2450 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2451 * was received on 'in_xbundle' in 'vlan'. */
2453 update_mcast_snooping_table(const struct xlate_ctx
*ctx
,
2454 const struct flow
*flow
, int vlan
,
2455 struct xbundle
*in_xbundle
,
2456 const struct dp_packet
*packet
)
2458 struct mcast_snooping
*ms
= ctx
->xbridge
->ms
;
2459 struct xlate_cfg
*xcfg
;
2460 struct xbundle
*mcast_xbundle
;
2461 struct mcast_port_bundle
*fport
;
2463 /* Don't learn the OFPP_NONE port. */
2464 if (in_xbundle
== &ofpp_none_bundle
) {
2468 /* Don't learn from flood ports */
2469 mcast_xbundle
= NULL
;
2470 ovs_rwlock_wrlock(&ms
->rwlock
);
2471 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2472 LIST_FOR_EACH(fport
, node
, &ms
->fport_list
) {
2473 mcast_xbundle
= xbundle_lookup(xcfg
, fport
->port
);
2474 if (mcast_xbundle
== in_xbundle
) {
2479 if (!mcast_xbundle
|| mcast_xbundle
!= in_xbundle
) {
2480 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2481 update_mcast_snooping_table4__(ctx
, flow
, ms
, vlan
,
2482 in_xbundle
, packet
);
2484 update_mcast_snooping_table6__(ctx
, flow
, ms
, vlan
,
2485 in_xbundle
, packet
);
2488 ovs_rwlock_unlock(&ms
->rwlock
);
2491 /* send the packet to ports having the multicast group learned */
2493 xlate_normal_mcast_send_group(struct xlate_ctx
*ctx
,
2494 struct mcast_snooping
*ms OVS_UNUSED
,
2495 struct mcast_group
*grp
,
2496 struct xbundle
*in_xbundle
,
2497 const struct xvlan
*xvlan
)
2498 OVS_REQ_RDLOCK(ms
->rwlock
)
2500 struct xlate_cfg
*xcfg
;
2501 struct mcast_group_bundle
*b
;
2502 struct xbundle
*mcast_xbundle
;
2504 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2505 LIST_FOR_EACH(b
, bundle_node
, &grp
->bundle_lru
) {
2506 mcast_xbundle
= xbundle_lookup(xcfg
, b
->port
);
2507 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
) {
2508 xlate_report(ctx
, OFT_DETAIL
, "forwarding to mcast group port");
2509 output_normal(ctx
, mcast_xbundle
, xvlan
);
2510 } else if (!mcast_xbundle
) {
2511 xlate_report(ctx
, OFT_WARN
,
2512 "mcast group port is unknown, dropping");
2514 xlate_report(ctx
, OFT_DETAIL
,
2515 "mcast group port is input port, dropping");
2520 /* send the packet to ports connected to multicast routers */
2522 xlate_normal_mcast_send_mrouters(struct xlate_ctx
*ctx
,
2523 struct mcast_snooping
*ms
,
2524 struct xbundle
*in_xbundle
,
2525 const struct xvlan
*xvlan
)
2526 OVS_REQ_RDLOCK(ms
->rwlock
)
2528 struct xlate_cfg
*xcfg
;
2529 struct mcast_mrouter_bundle
*mrouter
;
2530 struct xbundle
*mcast_xbundle
;
2532 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2533 LIST_FOR_EACH(mrouter
, mrouter_node
, &ms
->mrouter_lru
) {
2534 mcast_xbundle
= xbundle_lookup(xcfg
, mrouter
->port
);
2535 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
2536 && mrouter
->vlan
== xvlan
->v
[0].vid
) {
2537 xlate_report(ctx
, OFT_DETAIL
, "forwarding to mcast router port");
2538 output_normal(ctx
, mcast_xbundle
, xvlan
);
2539 } else if (!mcast_xbundle
) {
2540 xlate_report(ctx
, OFT_WARN
,
2541 "mcast router port is unknown, dropping");
2542 } else if (mrouter
->vlan
!= xvlan
->v
[0].vid
) {
2543 xlate_report(ctx
, OFT_DETAIL
,
2544 "mcast router is on another vlan, dropping");
2546 xlate_report(ctx
, OFT_DETAIL
,
2547 "mcast router port is input port, dropping");
2552 /* send the packet to ports flagged to be flooded */
2554 xlate_normal_mcast_send_fports(struct xlate_ctx
*ctx
,
2555 struct mcast_snooping
*ms
,
2556 struct xbundle
*in_xbundle
,
2557 const struct xvlan
*xvlan
)
2558 OVS_REQ_RDLOCK(ms
->rwlock
)
2560 struct xlate_cfg
*xcfg
;
2561 struct mcast_port_bundle
*fport
;
2562 struct xbundle
*mcast_xbundle
;
2564 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2565 LIST_FOR_EACH(fport
, node
, &ms
->fport_list
) {
2566 mcast_xbundle
= xbundle_lookup(xcfg
, fport
->port
);
2567 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
) {
2568 xlate_report(ctx
, OFT_DETAIL
, "forwarding to mcast flood port");
2569 output_normal(ctx
, mcast_xbundle
, xvlan
);
2570 } else if (!mcast_xbundle
) {
2571 xlate_report(ctx
, OFT_WARN
,
2572 "mcast flood port is unknown, dropping");
2574 xlate_report(ctx
, OFT_DETAIL
,
2575 "mcast flood port is input port, dropping");
2580 /* forward the Reports to configured ports */
2582 xlate_normal_mcast_send_rports(struct xlate_ctx
*ctx
,
2583 struct mcast_snooping
*ms
,
2584 struct xbundle
*in_xbundle
,
2585 const struct xvlan
*xvlan
)
2586 OVS_REQ_RDLOCK(ms
->rwlock
)
2588 struct xlate_cfg
*xcfg
;
2589 struct mcast_port_bundle
*rport
;
2590 struct xbundle
*mcast_xbundle
;
2592 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2593 LIST_FOR_EACH(rport
, node
, &ms
->rport_list
) {
2594 mcast_xbundle
= xbundle_lookup(xcfg
, rport
->port
);
2595 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
) {
2596 xlate_report(ctx
, OFT_DETAIL
,
2597 "forwarding report to mcast flagged port");
2598 output_normal(ctx
, mcast_xbundle
, xvlan
);
2599 } else if (!mcast_xbundle
) {
2600 xlate_report(ctx
, OFT_WARN
,
2601 "mcast port is unknown, dropping the report");
2603 xlate_report(ctx
, OFT_DETAIL
,
2604 "mcast port is input port, dropping the Report");
2610 xlate_normal_flood(struct xlate_ctx
*ctx
, struct xbundle
*in_xbundle
,
2611 struct xvlan
*xvlan
)
2613 struct xbundle
*xbundle
;
2615 LIST_FOR_EACH (xbundle
, list_node
, &ctx
->xbridge
->xbundles
) {
2616 if (xbundle
!= in_xbundle
2617 && xbundle_includes_vlan(xbundle
, xvlan
)
2618 && xbundle
->floodable
2619 && !xbundle_mirror_out(ctx
->xbridge
, xbundle
)) {
2620 output_normal(ctx
, xbundle
, xvlan
);
2623 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
2627 is_ip_local_multicast(const struct flow
*flow
, struct flow_wildcards
*wc
)
2629 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2630 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
2631 return ip_is_local_multicast(flow
->nw_dst
);
2632 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
2633 memset(&wc
->masks
.ipv6_dst
, 0xff, sizeof wc
->masks
.ipv6_dst
);
2634 return ipv6_is_all_hosts(&flow
->ipv6_dst
);
2641 xlate_normal(struct xlate_ctx
*ctx
)
2643 struct flow_wildcards
*wc
= ctx
->wc
;
2644 struct flow
*flow
= &ctx
->xin
->flow
;
2645 struct xbundle
*in_xbundle
;
2646 struct xport
*in_port
;
2647 struct mac_entry
*mac
;
2649 struct xvlan in_xvlan
;
2653 memset(&wc
->masks
.dl_src
, 0xff, sizeof wc
->masks
.dl_src
);
2654 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
2655 wc
->masks
.vlans
[0].tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
2657 in_xbundle
= lookup_input_bundle(ctx
, flow
->in_port
.ofp_port
, &in_port
);
2659 xlate_report(ctx
, OFT_WARN
, "no input bundle, dropping");
2663 /* Drop malformed frames. */
2664 if (eth_type_vlan(flow
->dl_type
) &&
2665 !(flow
->vlans
[0].tci
& htons(VLAN_CFI
))) {
2666 if (ctx
->xin
->packet
!= NULL
) {
2667 xlate_report_error(ctx
, "dropping packet with partial "
2668 "VLAN tag received on port %s",
2671 xlate_report(ctx
, OFT_WARN
, "partial VLAN tag, dropping");
2675 /* Drop frames on bundles reserved for mirroring. */
2676 if (xbundle_mirror_out(ctx
->xbridge
, in_xbundle
)) {
2677 if (ctx
->xin
->packet
!= NULL
) {
2678 xlate_report_error(ctx
, "dropping packet received on port %s, "
2679 "which is reserved exclusively for mirroring",
2682 xlate_report(ctx
, OFT_WARN
,
2683 "input port is mirror output port, dropping");
2688 xvlan_extract(flow
, &in_xvlan
);
2689 if (!input_vid_is_valid(ctx
, in_xvlan
.v
[0].vid
, in_xbundle
)) {
2690 xlate_report(ctx
, OFT_WARN
,
2691 "disallowed VLAN VID for this input port, dropping");
2694 xvlan_input_translate(in_xbundle
, &in_xvlan
, &xvlan
);
2695 vlan
= xvlan
.v
[0].vid
;
2697 /* Check other admissibility requirements. */
2698 if (in_port
&& !is_admissible(ctx
, in_port
, vlan
)) {
2702 /* Learn source MAC. */
2703 bool is_grat_arp
= is_gratuitous_arp(flow
, wc
);
2704 if (ctx
->xin
->allow_side_effects
2705 && flow
->packet_type
== htonl(PT_ETH
)
2706 && in_port
->pt_mode
!= NETDEV_PT_LEGACY_L3
2708 update_learning_table(ctx
, in_xbundle
, flow
->dl_src
, vlan
,
2711 if (ctx
->xin
->xcache
&& in_xbundle
!= &ofpp_none_bundle
) {
2712 struct xc_entry
*entry
;
2714 /* Save just enough info to update mac learning table later. */
2715 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NORMAL
);
2716 entry
->normal
.ofproto
= ctx
->xbridge
->ofproto
;
2717 entry
->normal
.in_port
= flow
->in_port
.ofp_port
;
2718 entry
->normal
.dl_src
= flow
->dl_src
;
2719 entry
->normal
.vlan
= vlan
;
2720 entry
->normal
.is_gratuitous_arp
= is_grat_arp
;
2723 /* Determine output bundle. */
2724 if (mcast_snooping_enabled(ctx
->xbridge
->ms
)
2725 && !eth_addr_is_broadcast(flow
->dl_dst
)
2726 && eth_addr_is_multicast(flow
->dl_dst
)
2727 && is_ip_any(flow
)) {
2728 struct mcast_snooping
*ms
= ctx
->xbridge
->ms
;
2729 struct mcast_group
*grp
= NULL
;
2731 if (is_igmp(flow
, wc
)) {
2733 * IGMP packets need to take the slow path, in order to be
2734 * processed for mdb updates. That will prevent expires
2735 * firing off even after hosts have sent reports.
2737 ctx
->xout
->slow
|= SLOW_ACTION
;
2739 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
2740 if (mcast_snooping_is_membership(flow
->tp_src
) ||
2741 mcast_snooping_is_query(flow
->tp_src
)) {
2742 if (ctx
->xin
->allow_side_effects
&& ctx
->xin
->packet
) {
2743 update_mcast_snooping_table(ctx
, flow
, vlan
,
2744 in_xbundle
, ctx
->xin
->packet
);
2748 if (mcast_snooping_is_membership(flow
->tp_src
)) {
2749 ovs_rwlock_rdlock(&ms
->rwlock
);
2750 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, &xvlan
);
2751 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2752 * forward IGMP Membership Reports only to those ports where
2753 * multicast routers are attached. Alternatively stated: a
2754 * snooping switch should not forward IGMP Membership Reports
2755 * to ports on which only hosts are attached.
2756 * An administrative control may be provided to override this
2757 * restriction, allowing the report messages to be flooded to
2759 xlate_normal_mcast_send_rports(ctx
, ms
, in_xbundle
, &xvlan
);
2760 ovs_rwlock_unlock(&ms
->rwlock
);
2762 xlate_report(ctx
, OFT_DETAIL
, "multicast traffic, flooding");
2763 xlate_normal_flood(ctx
, in_xbundle
, &xvlan
);
2766 } else if (is_mld(flow
, wc
)) {
2767 ctx
->xout
->slow
|= SLOW_ACTION
;
2768 if (ctx
->xin
->allow_side_effects
&& ctx
->xin
->packet
) {
2769 update_mcast_snooping_table(ctx
, flow
, vlan
,
2770 in_xbundle
, ctx
->xin
->packet
);
2772 if (is_mld_report(flow
, wc
)) {
2773 ovs_rwlock_rdlock(&ms
->rwlock
);
2774 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, &xvlan
);
2775 xlate_normal_mcast_send_rports(ctx
, ms
, in_xbundle
, &xvlan
);
2776 ovs_rwlock_unlock(&ms
->rwlock
);
2778 xlate_report(ctx
, OFT_DETAIL
, "MLD query, flooding");
2779 xlate_normal_flood(ctx
, in_xbundle
, &xvlan
);
2782 if (is_ip_local_multicast(flow
, wc
)) {
2783 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2784 * address in the 224.0.0.x range which are not IGMP must
2785 * be forwarded on all ports */
2786 xlate_report(ctx
, OFT_DETAIL
,
2787 "RFC4541: section 2.1.2, item 2, flooding");
2788 xlate_normal_flood(ctx
, in_xbundle
, &xvlan
);
2793 /* forwarding to group base ports */
2794 ovs_rwlock_rdlock(&ms
->rwlock
);
2795 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2796 grp
= mcast_snooping_lookup4(ms
, flow
->nw_dst
, vlan
);
2797 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
2798 grp
= mcast_snooping_lookup(ms
, &flow
->ipv6_dst
, vlan
);
2801 xlate_normal_mcast_send_group(ctx
, ms
, grp
, in_xbundle
, &xvlan
);
2802 xlate_normal_mcast_send_fports(ctx
, ms
, in_xbundle
, &xvlan
);
2803 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, &xvlan
);
2805 if (mcast_snooping_flood_unreg(ms
)) {
2806 xlate_report(ctx
, OFT_DETAIL
,
2807 "unregistered multicast, flooding");
2808 xlate_normal_flood(ctx
, in_xbundle
, &xvlan
);
2810 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, &xvlan
);
2811 xlate_normal_mcast_send_fports(ctx
, ms
, in_xbundle
, &xvlan
);
2814 ovs_rwlock_unlock(&ms
->rwlock
);
2816 ovs_rwlock_rdlock(&ctx
->xbridge
->ml
->rwlock
);
2817 mac
= mac_learning_lookup(ctx
->xbridge
->ml
, flow
->dl_dst
, vlan
);
2818 mac_port
= mac
? mac_entry_get_port(ctx
->xbridge
->ml
, mac
) : NULL
;
2819 ovs_rwlock_unlock(&ctx
->xbridge
->ml
->rwlock
);
2822 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2823 struct xbundle
*mac_xbundle
= xbundle_lookup(xcfg
, mac_port
);
2824 if (mac_xbundle
&& mac_xbundle
!= in_xbundle
) {
2825 xlate_report(ctx
, OFT_DETAIL
, "forwarding to learned port");
2826 output_normal(ctx
, mac_xbundle
, &xvlan
);
2827 } else if (!mac_xbundle
) {
2828 xlate_report(ctx
, OFT_WARN
,
2829 "learned port is unknown, dropping");
2831 xlate_report(ctx
, OFT_DETAIL
,
2832 "learned port is input port, dropping");
2835 xlate_report(ctx
, OFT_DETAIL
,
2836 "no learned MAC for destination, flooding");
2837 xlate_normal_flood(ctx
, in_xbundle
, &xvlan
);
2842 /* Appends a "sample" action for sFlow or IPFIX to 'ctx->odp_actions'. The
2843 * 'probability' is the number of packets out of UINT32_MAX to sample. The
2844 * 'cookie' (of length 'cookie_size' bytes) is passed back in the callback for
2845 * each sampled packet. 'tunnel_out_port', if not ODPP_NONE, is added as the
2846 * OVS_USERSPACE_ATTR_EGRESS_TUN_PORT attribute. If 'include_actions', an
2847 * OVS_USERSPACE_ATTR_ACTIONS attribute is added. If 'emit_set_tunnel',
2848 * sample(sampling_port=1) would translate into datapath sample action
2849 * set(tunnel(...)), sample(...) and it is used for sampling egress tunnel
2853 compose_sample_action(struct xlate_ctx
*ctx
,
2854 const uint32_t probability
,
2855 const union user_action_cookie
*cookie
,
2856 const size_t cookie_size
,
2857 const odp_port_t tunnel_out_port
,
2858 bool include_actions
)
2860 if (probability
== 0) {
2861 /* No need to generate sampling or the inner action. */
2865 /* If the slow path meter is configured by the controller,
2866 * insert a meter action before the user space action. */
2867 struct ofproto
*ofproto
= &ctx
->xin
->ofproto
->up
;
2868 uint32_t meter_id
= ofproto
->slowpath_meter_id
;
2870 /* When meter action is not required, avoid generate sample action
2871 * for 100% sampling rate. */
2872 bool is_sample
= probability
< UINT32_MAX
|| meter_id
!= UINT32_MAX
;
2873 size_t sample_offset
, actions_offset
;
2875 sample_offset
= nl_msg_start_nested(ctx
->odp_actions
,
2876 OVS_ACTION_ATTR_SAMPLE
);
2877 nl_msg_put_u32(ctx
->odp_actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
2879 actions_offset
= nl_msg_start_nested(ctx
->odp_actions
,
2880 OVS_SAMPLE_ATTR_ACTIONS
);
2883 if (meter_id
!= UINT32_MAX
) {
2884 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_METER
, meter_id
);
2887 odp_port_t odp_port
= ofp_port_to_odp_port(
2888 ctx
->xbridge
, ctx
->xin
->flow
.in_port
.ofp_port
);
2889 uint32_t pid
= dpif_port_get_pid(ctx
->xbridge
->dpif
, odp_port
,
2890 flow_hash_5tuple(&ctx
->xin
->flow
, 0));
2891 int cookie_offset
= odp_put_userspace_action(pid
, cookie
, cookie_size
,
2897 nl_msg_end_nested(ctx
->odp_actions
, actions_offset
);
2898 nl_msg_end_nested(ctx
->odp_actions
, sample_offset
);
2901 return cookie_offset
;
2904 /* If sFLow is not enabled, returns 0 without doing anything.
2906 * If sFlow is enabled, appends a template "sample" action to the ODP actions
2907 * in 'ctx'. This action is a template because some of the information needed
2908 * to fill it out is not available until flow translation is complete. In this
2909 * case, this functions returns an offset, which is always nonzero, to pass
2910 * later to fix_sflow_action() to fill in the rest of the template. */
2912 compose_sflow_action(struct xlate_ctx
*ctx
)
2914 struct dpif_sflow
*sflow
= ctx
->xbridge
->sflow
;
2915 if (!sflow
|| ctx
->xin
->flow
.in_port
.ofp_port
== OFPP_NONE
) {
2919 union user_action_cookie cookie
= { .type
= USER_ACTION_COOKIE_SFLOW
};
2920 return compose_sample_action(ctx
, dpif_sflow_get_probability(sflow
),
2921 &cookie
, sizeof cookie
.sflow
, ODPP_NONE
,
2925 /* If flow IPFIX is enabled, make sure IPFIX flow sample action
2926 * at egress point of tunnel port is just in front of corresponding
2927 * output action. If bridge IPFIX is enabled, this appends an IPFIX
2928 * sample action to 'ctx->odp_actions'. */
2930 compose_ipfix_action(struct xlate_ctx
*ctx
, odp_port_t output_odp_port
)
2932 struct dpif_ipfix
*ipfix
= ctx
->xbridge
->ipfix
;
2933 odp_port_t tunnel_out_port
= ODPP_NONE
;
2935 if (!ipfix
|| ctx
->xin
->flow
.in_port
.ofp_port
== OFPP_NONE
) {
2939 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2941 if (output_odp_port
== ODPP_NONE
&&
2942 !dpif_ipfix_get_bridge_exporter_input_sampling(ipfix
)) {
2946 /* For output case, output_odp_port is valid. */
2947 if (output_odp_port
!= ODPP_NONE
) {
2948 if (!dpif_ipfix_get_bridge_exporter_output_sampling(ipfix
)) {
2951 /* If tunnel sampling is enabled, put an additional option attribute:
2952 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2954 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(ipfix
) &&
2955 dpif_ipfix_get_tunnel_port(ipfix
, output_odp_port
) ) {
2956 tunnel_out_port
= output_odp_port
;
2960 union user_action_cookie cookie
= {
2962 .type
= USER_ACTION_COOKIE_IPFIX
,
2963 .output_odp_port
= output_odp_port
,
2966 compose_sample_action(ctx
,
2967 dpif_ipfix_get_bridge_exporter_probability(ipfix
),
2968 &cookie
, sizeof cookie
.ipfix
, tunnel_out_port
,
2972 /* Fix "sample" action according to data collected while composing ODP actions,
2973 * as described in compose_sflow_action().
2975 * 'user_cookie_offset' must be the offset returned by add_sflow_action(). */
2977 fix_sflow_action(struct xlate_ctx
*ctx
, unsigned int user_cookie_offset
)
2979 const struct flow
*base
= &ctx
->base_flow
;
2980 union user_action_cookie
*cookie
;
2982 cookie
= ofpbuf_at(ctx
->odp_actions
, user_cookie_offset
,
2983 sizeof cookie
->sflow
);
2984 ovs_assert(cookie
->type
== USER_ACTION_COOKIE_SFLOW
);
2986 cookie
->type
= USER_ACTION_COOKIE_SFLOW
;
2987 cookie
->sflow
.vlan_tci
= base
->vlans
[0].tci
;
2989 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2990 * port information") for the interpretation of cookie->output. */
2991 switch (ctx
->sflow_n_outputs
) {
2993 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2994 cookie
->sflow
.output
= 0x40000000 | 256;
2998 cookie
->sflow
.output
= dpif_sflow_odp_port_to_ifindex(
2999 ctx
->xbridge
->sflow
, ctx
->sflow_odp_port
);
3000 if (cookie
->sflow
.output
) {
3005 /* 0x80000000 means "multiple output ports. */
3006 cookie
->sflow
.output
= 0x80000000 | ctx
->sflow_n_outputs
;
3012 process_special(struct xlate_ctx
*ctx
, const struct xport
*xport
)
3014 const struct flow
*flow
= &ctx
->xin
->flow
;
3015 struct flow_wildcards
*wc
= ctx
->wc
;
3016 const struct xbridge
*xbridge
= ctx
->xbridge
;
3017 const struct dp_packet
*packet
= ctx
->xin
->packet
;
3018 enum slow_path_reason slow
;
3022 } else if (xport
->cfm
&& cfm_should_process_flow(xport
->cfm
, flow
, wc
)) {
3024 cfm_process_heartbeat(xport
->cfm
, packet
);
3027 } else if (xport
->bfd
&& bfd_should_process_flow(xport
->bfd
, flow
, wc
)) {
3029 bfd_process_packet(xport
->bfd
, flow
, packet
);
3030 /* If POLL received, immediately sends FINAL back. */
3031 if (bfd_should_send_packet(xport
->bfd
)) {
3032 ofproto_dpif_monitor_port_send_soon(xport
->ofport
);
3036 } else if (xport
->xbundle
&& xport
->xbundle
->lacp
3037 && flow
->dl_type
== htons(ETH_TYPE_LACP
)) {
3039 lacp_process_packet(xport
->xbundle
->lacp
, xport
->ofport
, packet
);
3042 } else if ((xbridge
->stp
|| xbridge
->rstp
) &&
3043 stp_should_process_flow(flow
, wc
)) {
3046 ? stp_process_packet(xport
, packet
)
3047 : rstp_process_packet(xport
, packet
);
3050 } else if (xport
->lldp
&& lldp_should_process_flow(xport
->lldp
, flow
)) {
3052 lldp_process_packet(xport
->lldp
, packet
);
3060 ctx
->xout
->slow
|= slow
;
3068 tnl_route_lookup_flow(const struct flow
*oflow
,
3069 struct in6_addr
*ip
, struct in6_addr
*src
,
3070 struct xport
**out_port
)
3072 char out_dev
[IFNAMSIZ
];
3073 struct xbridge
*xbridge
;
3074 struct xlate_cfg
*xcfg
;
3076 struct in6_addr dst
;
3078 dst
= flow_tnl_dst(&oflow
->tunnel
);
3079 if (!ovs_router_lookup(oflow
->pkt_mark
, &dst
, out_dev
, src
, &gw
)) {
3083 if (ipv6_addr_is_set(&gw
) &&
3084 (!IN6_IS_ADDR_V4MAPPED(&gw
) || in6_addr_get_mapped_ipv4(&gw
))) {
3090 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
3093 HMAP_FOR_EACH (xbridge
, hmap_node
, &xcfg
->xbridges
) {
3094 if (!strncmp(xbridge
->name
, out_dev
, IFNAMSIZ
)) {
3097 HMAP_FOR_EACH (port
, ofp_node
, &xbridge
->xports
) {
3098 if (!strncmp(netdev_get_name(port
->netdev
), out_dev
, IFNAMSIZ
)) {
3109 compose_table_xlate(struct xlate_ctx
*ctx
, const struct xport
*out_dev
,
3110 struct dp_packet
*packet
)
3112 struct xbridge
*xbridge
= out_dev
->xbridge
;
3113 struct ofpact_output output
;
3116 ofpact_init(&output
.ofpact
, OFPACT_OUTPUT
, sizeof output
);
3117 flow_extract(packet
, &flow
);
3118 flow
.in_port
.ofp_port
= out_dev
->ofp_port
;
3119 output
.port
= OFPP_TABLE
;
3122 return ofproto_dpif_execute_actions__(xbridge
->ofproto
,
3123 ctx
->xin
->tables_version
, &flow
,
3124 NULL
, &output
.ofpact
, sizeof output
,
3125 ctx
->depth
, ctx
->resubmits
, packet
);
3129 tnl_send_nd_request(struct xlate_ctx
*ctx
, const struct xport
*out_dev
,
3130 const struct eth_addr eth_src
,
3131 struct in6_addr
* ipv6_src
, struct in6_addr
* ipv6_dst
)
3133 struct dp_packet packet
;
3135 dp_packet_init(&packet
, 0);
3136 compose_nd_ns(&packet
, eth_src
, ipv6_src
, ipv6_dst
);
3137 compose_table_xlate(ctx
, out_dev
, &packet
);
3138 dp_packet_uninit(&packet
);
3142 tnl_send_arp_request(struct xlate_ctx
*ctx
, const struct xport
*out_dev
,
3143 const struct eth_addr eth_src
,
3144 ovs_be32 ip_src
, ovs_be32 ip_dst
)
3146 struct dp_packet packet
;
3148 dp_packet_init(&packet
, 0);
3149 compose_arp(&packet
, ARP_OP_REQUEST
,
3150 eth_src
, eth_addr_zero
, true, ip_src
, ip_dst
);
3152 compose_table_xlate(ctx
, out_dev
, &packet
);
3153 dp_packet_uninit(&packet
);
3157 propagate_tunnel_data_to_flow__(struct flow
*dst_flow
,
3158 const struct flow
*src_flow
,
3159 struct eth_addr dmac
, struct eth_addr smac
,
3160 struct in6_addr s_ip6
, ovs_be32 s_ip
,
3161 bool is_tnl_ipv6
, uint8_t nw_proto
)
3163 dst_flow
->dl_dst
= dmac
;
3164 dst_flow
->dl_src
= smac
;
3166 dst_flow
->packet_type
= htonl(PT_ETH
);
3167 dst_flow
->nw_dst
= src_flow
->tunnel
.ip_dst
;
3168 dst_flow
->nw_src
= src_flow
->tunnel
.ip_src
;
3169 dst_flow
->ipv6_dst
= src_flow
->tunnel
.ipv6_dst
;
3170 dst_flow
->ipv6_src
= src_flow
->tunnel
.ipv6_src
;
3172 dst_flow
->nw_tos
= src_flow
->tunnel
.ip_tos
;
3173 dst_flow
->nw_ttl
= src_flow
->tunnel
.ip_ttl
;
3174 dst_flow
->tp_dst
= src_flow
->tunnel
.tp_dst
;
3175 dst_flow
->tp_src
= src_flow
->tunnel
.tp_src
;
3178 dst_flow
->dl_type
= htons(ETH_TYPE_IPV6
);
3179 if (ipv6_mask_is_any(&dst_flow
->ipv6_src
)
3180 && !ipv6_mask_is_any(&s_ip6
)) {
3181 dst_flow
->ipv6_src
= s_ip6
;
3184 dst_flow
->dl_type
= htons(ETH_TYPE_IP
);
3185 if (dst_flow
->nw_src
== 0 && s_ip
) {
3186 dst_flow
->nw_src
= s_ip
;
3189 dst_flow
->nw_proto
= nw_proto
;
3193 * Populate the 'flow' and 'base_flow' L3 fields to do the post tunnel push
3197 propagate_tunnel_data_to_flow(struct xlate_ctx
*ctx
, struct eth_addr dmac
,
3198 struct eth_addr smac
, struct in6_addr s_ip6
,
3199 ovs_be32 s_ip
, bool is_tnl_ipv6
,
3200 enum ovs_vport_type tnl_type
)
3202 struct flow
*base_flow
, *flow
;
3203 flow
= &ctx
->xin
->flow
;
3204 base_flow
= &ctx
->base_flow
;
3205 uint8_t nw_proto
= 0;
3208 case OVS_VPORT_TYPE_GRE
:
3209 nw_proto
= IPPROTO_GRE
;
3211 case OVS_VPORT_TYPE_VXLAN
:
3212 case OVS_VPORT_TYPE_GENEVE
:
3213 nw_proto
= IPPROTO_UDP
;
3215 case OVS_VPORT_TYPE_LISP
:
3216 case OVS_VPORT_TYPE_STT
:
3217 case OVS_VPORT_TYPE_UNSPEC
:
3218 case OVS_VPORT_TYPE_NETDEV
:
3219 case OVS_VPORT_TYPE_INTERNAL
:
3220 case __OVS_VPORT_TYPE_MAX
:
3226 * Update base_flow first followed by flow as the dst_flow gets modified
3229 propagate_tunnel_data_to_flow__(base_flow
, flow
, dmac
, smac
, s_ip6
, s_ip
,
3230 is_tnl_ipv6
, nw_proto
);
3231 propagate_tunnel_data_to_flow__(flow
, flow
, dmac
, smac
, s_ip6
, s_ip
,
3232 is_tnl_ipv6
, nw_proto
);
3235 /* Validate if the transalated combined actions are OK to proceed.
3236 * If actions consist of TRUNC action, it is not allowed to do the
3237 * tunnel_push combine as it cannot update stats correctly.
3240 is_tunnel_actions_clone_ready(struct xlate_ctx
*ctx
)
3242 struct nlattr
*tnl_actions
;
3243 const struct nlattr
*a
;
3246 struct ofpbuf
*actions
= ctx
->odp_actions
;
3249 /* No actions, no harm in doing combine. */
3253 /* Cannot perform tunnel push on slow path action CONTROLLER_OUTPUT. */
3254 if (ctx
->xout
->slow
& SLOW_CONTROLLER
) {
3257 actions_len
= actions
->size
;
3259 tnl_actions
=(struct nlattr
*)(actions
->data
);
3260 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, tnl_actions
, actions_len
) {
3261 int type
= nl_attr_type(a
);
3262 if (type
== OVS_ACTION_ATTR_TRUNC
) {
3263 VLOG_DBG("Cannot do tunnel action-combine on trunc action");
3272 validate_and_combine_post_tnl_actions(struct xlate_ctx
*ctx
,
3273 const struct xport
*xport
,
3274 struct xport
*out_dev
,
3275 struct ovs_action_push_tnl tnl_push_data
)
3277 const struct dpif_flow_stats
*backup_resubmit_stats
;
3278 struct xlate_cache
*backup_xcache
;
3279 bool nested_act_flag
= false;
3280 struct flow_wildcards tmp_flow_wc
;
3281 struct flow_wildcards
*backup_flow_wc_ptr
;
3282 bool backup_side_effects
;
3283 const struct dp_packet
*backup_pkt
;
3285 memset(&tmp_flow_wc
, 0 , sizeof tmp_flow_wc
);
3286 backup_flow_wc_ptr
= ctx
->wc
;
3287 ctx
->wc
= &tmp_flow_wc
;
3288 ctx
->xin
->wc
= NULL
;
3289 backup_resubmit_stats
= ctx
->xin
->resubmit_stats
;
3290 backup_xcache
= ctx
->xin
->xcache
;
3291 backup_side_effects
= ctx
->xin
->allow_side_effects
;
3292 backup_pkt
= ctx
->xin
->packet
;
3294 size_t push_action_size
= 0;
3295 size_t clone_ofs
= nl_msg_start_nested(ctx
->odp_actions
,
3296 OVS_ACTION_ATTR_CLONE
);
3297 odp_put_tnl_push_action(ctx
->odp_actions
, &tnl_push_data
);
3298 push_action_size
= ctx
->odp_actions
->size
;
3300 ctx
->xin
->resubmit_stats
= NULL
;
3301 ctx
->xin
->xcache
= xlate_cache_new(); /* Use new temporary cache. */
3302 ctx
->xin
->allow_side_effects
= false;
3303 ctx
->xin
->packet
= NULL
;
3305 /* Push the cache entry for the tunnel first. */
3306 struct xc_entry
*entry
;
3307 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_TUNNEL_HEADER
);
3308 entry
->tunnel_hdr
.hdr_size
= tnl_push_data
.header_len
;
3309 entry
->tunnel_hdr
.operation
= ADD
;
3311 apply_nested_clone_actions(ctx
, xport
, out_dev
);
3312 nested_act_flag
= is_tunnel_actions_clone_ready(ctx
);
3314 if (nested_act_flag
) {
3315 /* Similar to the stats update in revalidation, the x_cache entries
3316 * are populated by the previous translation are used to update the
3319 if (backup_resubmit_stats
) {
3320 struct dpif_flow_stats tmp_resubmit_stats
;
3321 memcpy(&tmp_resubmit_stats
, backup_resubmit_stats
,
3322 sizeof tmp_resubmit_stats
);
3323 xlate_push_stats(ctx
->xin
->xcache
, &tmp_resubmit_stats
);
3325 xlate_cache_steal_entries(backup_xcache
, ctx
->xin
->xcache
);
3327 /* Combine is not valid. */
3328 nl_msg_cancel_nested(ctx
->odp_actions
, clone_ofs
);
3331 if (ctx
->odp_actions
->size
> push_action_size
) {
3332 /* Update the CLONE action only when combined. */
3333 nl_msg_end_nested(ctx
->odp_actions
, clone_ofs
);
3335 nl_msg_cancel_nested(ctx
->odp_actions
, clone_ofs
);
3336 /* XXX : There is no real use-case for a tunnel push without
3337 * any post actions. However keeping it now
3338 * as is to make the 'make check' happy. Should remove when all the
3339 * make check tunnel test case does something meaningful on a
3340 * tunnel encap packets.
3342 odp_put_tnl_push_action(ctx
->odp_actions
, &tnl_push_data
);
3346 /* Restore context status. */
3347 ctx
->xin
->resubmit_stats
= backup_resubmit_stats
;
3348 xlate_cache_delete(ctx
->xin
->xcache
);
3349 ctx
->xin
->xcache
= backup_xcache
;
3350 ctx
->xin
->allow_side_effects
= backup_side_effects
;
3351 ctx
->xin
->packet
= backup_pkt
;
3352 ctx
->wc
= backup_flow_wc_ptr
;
3353 return nested_act_flag
;
3357 build_tunnel_send(struct xlate_ctx
*ctx
, const struct xport
*xport
,
3358 const struct flow
*flow
, odp_port_t tunnel_odp_port
)
3360 struct netdev_tnl_build_header_params tnl_params
;
3361 struct ovs_action_push_tnl tnl_push_data
;
3362 struct xport
*out_dev
= NULL
;
3363 ovs_be32 s_ip
= 0, d_ip
= 0;
3364 struct in6_addr s_ip6
= in6addr_any
;
3365 struct in6_addr d_ip6
= in6addr_any
;
3366 struct eth_addr smac
;
3367 struct eth_addr dmac
;
3369 char buf_sip6
[INET6_ADDRSTRLEN
];
3370 char buf_dip6
[INET6_ADDRSTRLEN
];
3372 /* Structures to backup Ethernet and IP of base_flow. */
3373 struct flow old_base_flow
;
3374 struct flow old_flow
;
3376 /* Backup flow & base_flow data. */
3377 memcpy(&old_base_flow
, &ctx
->base_flow
, sizeof old_base_flow
);
3378 memcpy(&old_flow
, &ctx
->xin
->flow
, sizeof old_flow
);
3380 err
= tnl_route_lookup_flow(flow
, &d_ip6
, &s_ip6
, &out_dev
);
3382 xlate_report(ctx
, OFT_WARN
, "native tunnel routing failed");
3386 xlate_report(ctx
, OFT_DETAIL
, "tunneling to %s via %s",
3387 ipv6_string_mapped(buf_dip6
, &d_ip6
),
3388 netdev_get_name(out_dev
->netdev
));
3390 /* Use mac addr of bridge port of the peer. */
3391 err
= netdev_get_etheraddr(out_dev
->netdev
, &smac
);
3393 xlate_report(ctx
, OFT_WARN
,
3394 "tunnel output device lacks Ethernet address");
3398 d_ip
= in6_addr_get_mapped_ipv4(&d_ip6
);
3400 s_ip
= in6_addr_get_mapped_ipv4(&s_ip6
);
3403 err
= tnl_neigh_lookup(out_dev
->xbridge
->name
, &d_ip6
, &dmac
);
3405 xlate_report(ctx
, OFT_DETAIL
,
3406 "neighbor cache miss for %s on bridge %s, "
3407 "sending %s request",
3408 buf_dip6
, out_dev
->xbridge
->name
, d_ip
? "ARP" : "ND");
3410 tnl_send_arp_request(ctx
, out_dev
, smac
, s_ip
, d_ip
);
3412 tnl_send_nd_request(ctx
, out_dev
, smac
, &s_ip6
, &d_ip6
);
3417 if (ctx
->xin
->xcache
) {
3418 struct xc_entry
*entry
;
3420 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_TNL_NEIGH
);
3421 ovs_strlcpy(entry
->tnl_neigh_cache
.br_name
, out_dev
->xbridge
->name
,
3422 sizeof entry
->tnl_neigh_cache
.br_name
);
3423 entry
->tnl_neigh_cache
.d_ipv6
= d_ip6
;
3426 xlate_report(ctx
, OFT_DETAIL
, "tunneling from "ETH_ADDR_FMT
" %s"
3427 " to "ETH_ADDR_FMT
" %s",
3428 ETH_ADDR_ARGS(smac
), ipv6_string_mapped(buf_sip6
, &s_ip6
),
3429 ETH_ADDR_ARGS(dmac
), buf_dip6
);
3431 netdev_init_tnl_build_header_params(&tnl_params
, flow
, &s_ip6
, dmac
, smac
);
3432 err
= tnl_port_build_header(xport
->ofport
, &tnl_push_data
, &tnl_params
);
3436 tnl_push_data
.tnl_port
= tunnel_odp_port
;
3437 tnl_push_data
.out_port
= out_dev
->odp_port
;
3439 /* After tunnel header has been added, MAC and IP data of flow and
3440 * base_flow need to be set properly, since there is not recirculation
3441 * any more when sending packet to tunnel. */
3443 propagate_tunnel_data_to_flow(ctx
, dmac
, smac
, s_ip6
, s_ip
,
3444 tnl_params
.is_ipv6
, tnl_push_data
.tnl_type
);
3447 /* Try to see if its possible to apply nested clone actions on tunnel.
3448 * Revert the combined actions on tunnel if its not valid.
3450 if (!validate_and_combine_post_tnl_actions(ctx
, xport
, out_dev
,
3452 /* Datapath is not doing the recirculation now, so lets make it
3453 * happen explicitly.
3455 size_t clone_ofs
= nl_msg_start_nested(ctx
->odp_actions
,
3456 OVS_ACTION_ATTR_CLONE
);
3457 odp_put_tnl_push_action(ctx
->odp_actions
, &tnl_push_data
);
3458 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_RECIRC
, 0);
3459 nl_msg_end_nested(ctx
->odp_actions
, clone_ofs
);
3461 /* Restore the flows after the translation. */
3462 memcpy(&ctx
->xin
->flow
, &old_flow
, sizeof ctx
->xin
->flow
);
3463 memcpy(&ctx
->base_flow
, &old_base_flow
, sizeof ctx
->base_flow
);
3468 xlate_commit_actions(struct xlate_ctx
*ctx
)
3470 bool use_masked
= ctx
->xbridge
->support
.masked_set_action
;
3472 ctx
->xout
->slow
|= commit_odp_actions(&ctx
->xin
->flow
, &ctx
->base_flow
,
3473 ctx
->odp_actions
, ctx
->wc
,
3474 use_masked
, ctx
->pending_encap
,
3476 ctx
->pending_encap
= false;
3477 ofpbuf_delete(ctx
->encap_data
);
3478 ctx
->encap_data
= NULL
;
3482 clear_conntrack(struct xlate_ctx
*ctx
)
3484 ctx
->conntracked
= false;
3485 flow_clear_conntrack(&ctx
->xin
->flow
);
3489 xlate_flow_is_protected(const struct xlate_ctx
*ctx
, const struct flow
*flow
, const struct xport
*xport_out
)
3491 const struct xport
*xport_in
;
3497 xport_in
= get_ofp_port(ctx
->xbridge
, flow
->in_port
.ofp_port
);
3499 return (xport_in
&& xport_in
->xbundle
&& xport_out
->xbundle
&&
3500 xport_in
->xbundle
->protected && xport_out
->xbundle
->protected);
3503 /* Function to combine actions from following device/port with the current
3504 * device actions in openflow pipeline. Mainly used for the translation of
3505 * patch/tunnel port output actions. It pushes the openflow state into a stack
3506 * first, clear out to execute the packet through the device and finally pop
3507 * the openflow state back from the stack. This is equivalent to cloning
3508 * a packet in translation for the duration of execution.
3510 * On output to a patch port, the output action will be replaced with set of
3511 * nested actions on the peer patch port.
3512 * Similarly on output to a tunnel port, the post nested actions on
3513 * tunnel are chained up with the tunnel-push action.
3516 apply_nested_clone_actions(struct xlate_ctx
*ctx
, const struct xport
*in_dev
,
3517 struct xport
*out_dev
)
3519 struct flow
*flow
= &ctx
->xin
->flow
;
3520 struct flow old_flow
= ctx
->xin
->flow
;
3521 struct flow_tnl old_flow_tnl_wc
= ctx
->wc
->masks
.tunnel
;
3522 bool old_conntrack
= ctx
->conntracked
;
3523 bool old_was_mpls
= ctx
->was_mpls
;
3524 ovs_version_t old_version
= ctx
->xin
->tables_version
;
3525 struct ofpbuf old_stack
= ctx
->stack
;
3526 uint8_t new_stack
[1024];
3527 struct ofpbuf old_action_set
= ctx
->action_set
;
3528 struct ovs_list
*old_trace
= ctx
->xin
->trace
;
3529 uint64_t actset_stub
[1024 / 8];
3531 ofpbuf_use_stub(&ctx
->stack
, new_stack
, sizeof new_stack
);
3532 ofpbuf_use_stub(&ctx
->action_set
, actset_stub
, sizeof actset_stub
);
3533 flow
->in_port
.ofp_port
= out_dev
->ofp_port
;
3534 flow
->metadata
= htonll(0);
3535 memset(&flow
->tunnel
, 0, sizeof flow
->tunnel
);
3536 memset(&ctx
->wc
->masks
.tunnel
, 0, sizeof ctx
->wc
->masks
.tunnel
);
3537 flow
->tunnel
.metadata
.tab
=
3538 ofproto_get_tun_tab(&out_dev
->xbridge
->ofproto
->up
);
3539 ctx
->wc
->masks
.tunnel
.metadata
.tab
= flow
->tunnel
.metadata
.tab
;
3540 memset(flow
->regs
, 0, sizeof flow
->regs
);
3541 flow
->actset_output
= OFPP_UNSET
;
3542 clear_conntrack(ctx
);
3543 ctx
->xin
->trace
= xlate_report(ctx
, OFT_BRIDGE
, "bridge(\"%s\")",
3544 out_dev
->xbridge
->name
);
3545 mirror_mask_t old_mirrors
= ctx
->mirrors
;
3546 bool independent_mirrors
= out_dev
->xbridge
!= ctx
->xbridge
;
3547 if (independent_mirrors
) {
3550 ctx
->xbridge
= out_dev
->xbridge
;
3552 /* The bridge is now known so obtain its table version. */
3553 ctx
->xin
->tables_version
3554 = ofproto_dpif_get_tables_version(ctx
->xbridge
->ofproto
);
3556 if (!process_special(ctx
, out_dev
) && may_receive(out_dev
, ctx
)) {
3557 if (xport_stp_forward_state(out_dev
) &&
3558 xport_rstp_forward_state(out_dev
)) {
3559 xlate_table_action(ctx
, flow
->in_port
.ofp_port
, 0, true, true,
3561 if (!ctx
->freezing
) {
3562 xlate_action_set(ctx
);
3564 if (ctx
->freezing
) {
3565 finish_freezing(ctx
);
3568 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
3569 * the learning action look at the packet, then drop it. */
3570 struct flow old_base_flow
= ctx
->base_flow
;
3571 size_t old_size
= ctx
->odp_actions
->size
;
3572 mirror_mask_t old_mirrors2
= ctx
->mirrors
;
3574 xlate_table_action(ctx
, flow
->in_port
.ofp_port
, 0, true, true,
3576 ctx
->mirrors
= old_mirrors2
;
3577 ctx
->base_flow
= old_base_flow
;
3578 ctx
->odp_actions
->size
= old_size
;
3580 /* Undo changes that may have been done for freezing. */
3581 ctx_cancel_freeze(ctx
);
3585 ctx
->xin
->trace
= old_trace
;
3586 if (independent_mirrors
) {
3587 ctx
->mirrors
= old_mirrors
;
3589 ctx
->xin
->flow
= old_flow
;
3590 ctx
->xbridge
= in_dev
->xbridge
;
3591 ofpbuf_uninit(&ctx
->action_set
);
3592 ctx
->action_set
= old_action_set
;
3593 ofpbuf_uninit(&ctx
->stack
);
3594 ctx
->stack
= old_stack
;
3596 /* Restore calling bridge's lookup version. */
3597 ctx
->xin
->tables_version
= old_version
;
3599 /* Restore to calling bridge tunneling information */
3600 ctx
->wc
->masks
.tunnel
= old_flow_tnl_wc
;
3602 /* The out bridge popping MPLS should have no effect on the original
3604 ctx
->was_mpls
= old_was_mpls
;
3606 /* The out bridge's conntrack execution should have no effect on the
3607 * original bridge. */
3608 ctx
->conntracked
= old_conntrack
;
3610 /* The fact that the out bridge exits (for any reason) does not mean
3611 * that the original bridge should exit. Specifically, if the out
3612 * bridge freezes translation, the original bridge must continue
3613 * processing with the original, not the frozen packet! */
3616 /* Out bridge errors do not propagate back. */
3617 ctx
->error
= XLATE_OK
;
3619 if (ctx
->xin
->resubmit_stats
) {
3620 netdev_vport_inc_tx(in_dev
->netdev
, ctx
->xin
->resubmit_stats
);
3621 netdev_vport_inc_rx(out_dev
->netdev
, ctx
->xin
->resubmit_stats
);
3623 bfd_account_rx(out_dev
->bfd
, ctx
->xin
->resubmit_stats
);
3626 if (ctx
->xin
->xcache
) {
3627 struct xc_entry
*entry
;
3629 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NETDEV
);
3630 entry
->dev
.tx
= netdev_ref(in_dev
->netdev
);
3631 entry
->dev
.rx
= netdev_ref(out_dev
->netdev
);
3632 entry
->dev
.bfd
= bfd_ref(out_dev
->bfd
);
3637 check_output_prerequisites(struct xlate_ctx
*ctx
,
3638 const struct xport
*xport
,
3642 struct flow_wildcards
*wc
= ctx
->wc
;
3645 xlate_report(ctx
, OFT_WARN
, "Nonexistent output port");
3647 } else if (xport
->config
& OFPUTIL_PC_NO_FWD
) {
3648 xlate_report(ctx
, OFT_DETAIL
, "OFPPC_NO_FWD set, skipping output");
3650 } else if (ctx
->mirror_snaplen
!= 0 && xport
->odp_port
== ODPP_NONE
) {
3651 xlate_report(ctx
, OFT_WARN
,
3652 "Mirror truncate to ODPP_NONE, skipping output");
3654 } else if (xlate_flow_is_protected(ctx
, flow
, xport
)) {
3655 xlate_report(ctx
, OFT_WARN
,
3656 "Flow is between protected ports, skipping output.");
3658 } else if (check_stp
) {
3659 if (is_stp(&ctx
->base_flow
)) {
3660 if (!xport_stp_should_forward_bpdu(xport
) &&
3661 !xport_rstp_should_manage_bpdu(xport
)) {
3662 if (ctx
->xbridge
->stp
!= NULL
) {
3663 xlate_report(ctx
, OFT_WARN
,
3664 "STP not in listening state, "
3665 "skipping bpdu output");
3666 } else if (ctx
->xbridge
->rstp
!= NULL
) {
3667 xlate_report(ctx
, OFT_WARN
,
3668 "RSTP not managing BPDU in this state, "
3669 "skipping bpdu output");
3673 } else if ((xport
->cfm
&& cfm_should_process_flow(xport
->cfm
, flow
, wc
))
3674 || (xport
->bfd
&& bfd_should_process_flow(xport
->bfd
, flow
,
3676 /* Pass; STP should not block link health detection. */
3677 } else if (!xport_stp_forward_state(xport
) ||
3678 !xport_rstp_forward_state(xport
)) {
3679 if (ctx
->xbridge
->stp
!= NULL
) {
3680 xlate_report(ctx
, OFT_WARN
,
3681 "STP not in forwarding state, skipping output");
3682 } else if (ctx
->xbridge
->rstp
!= NULL
) {
3683 xlate_report(ctx
, OFT_WARN
,
3684 "RSTP not in forwarding state, skipping output");
3690 if (xport
->pt_mode
== NETDEV_PT_LEGACY_L2
&&
3691 flow
->packet_type
!= htonl(PT_ETH
)) {
3692 xlate_report(ctx
, OFT_WARN
, "Trying to send non-Ethernet packet "
3693 "through legacy L2 port. Dropping packet.");
3701 terminate_native_tunnel(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
,
3702 struct flow
*flow
, struct flow_wildcards
*wc
,
3703 odp_port_t
*tnl_port
)
3705 *tnl_port
= ODPP_NONE
;
3707 /* XXX: Write better Filter for tunnel port. We can use in_port
3708 * in tunnel-port flow to avoid these checks completely. */
3709 if (ofp_port
== OFPP_LOCAL
&&
3710 ovs_native_tunneling_is_on(ctx
->xbridge
->ofproto
)) {
3711 *tnl_port
= tnl_port_map_lookup(flow
, wc
);
3714 return *tnl_port
!= ODPP_NONE
;
3718 compose_output_action__(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
,
3719 const struct xlate_bond_recirc
*xr
, bool check_stp
)
3721 const struct xport
*xport
= get_ofp_port(ctx
->xbridge
, ofp_port
);
3722 struct flow_wildcards
*wc
= ctx
->wc
;
3723 struct flow
*flow
= &ctx
->xin
->flow
;
3724 struct flow_tnl flow_tnl
;
3725 union flow_vlan_hdr flow_vlans
[FLOW_MAX_VLAN_HEADERS
];
3726 uint8_t flow_nw_tos
;
3727 odp_port_t out_port
, odp_port
, odp_tnl_port
;
3728 bool is_native_tunnel
= false;
3730 struct eth_addr flow_dl_dst
= flow
->dl_dst
;
3731 struct eth_addr flow_dl_src
= flow
->dl_src
;
3732 ovs_be32 flow_packet_type
= flow
->packet_type
;
3733 ovs_be16 flow_dl_type
= flow
->dl_type
;
3735 /* If 'struct flow' gets additional metadata, we'll need to zero it out
3736 * before traversing a patch port. */
3737 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 40);
3738 memset(&flow_tnl
, 0, sizeof flow_tnl
);
3740 if (!check_output_prerequisites(ctx
, xport
, flow
, check_stp
)) {
3744 if (flow
->packet_type
== htonl(PT_ETH
)) {
3745 /* Strip Ethernet header for legacy L3 port. */
3746 if (xport
->pt_mode
== NETDEV_PT_LEGACY_L3
) {
3747 flow
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
3748 ntohs(flow
->dl_type
));
3753 apply_nested_clone_actions(ctx
, xport
, xport
->peer
);
3757 memcpy(flow_vlans
, flow
->vlans
, sizeof flow_vlans
);
3758 flow_nw_tos
= flow
->nw_tos
;
3760 if (count_skb_priorities(xport
)) {
3761 memset(&wc
->masks
.skb_priority
, 0xff, sizeof wc
->masks
.skb_priority
);
3762 if (dscp_from_skb_priority(xport
, flow
->skb_priority
, &dscp
)) {
3763 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
3764 flow
->nw_tos
&= ~IP_DSCP_MASK
;
3765 flow
->nw_tos
|= dscp
;
3769 if (xport
->is_tunnel
) {
3770 struct in6_addr dst
;
3771 /* Save tunnel metadata so that changes made due to
3772 * the Logical (tunnel) Port are not visible for any further
3773 * matches, while explicit set actions on tunnel metadata are.
3775 flow_tnl
= flow
->tunnel
;
3776 odp_port
= tnl_port_send(xport
->ofport
, flow
, ctx
->wc
);
3777 if (odp_port
== ODPP_NONE
) {
3778 xlate_report(ctx
, OFT_WARN
, "Tunneling decided against output");
3779 goto out
; /* restore flow_nw_tos */
3781 dst
= flow_tnl_dst(&flow
->tunnel
);
3782 if (ipv6_addr_equals(&dst
, &ctx
->orig_tunnel_ipv6_dst
)) {
3783 xlate_report(ctx
, OFT_WARN
, "Not tunneling to our own address");
3784 goto out
; /* restore flow_nw_tos */
3786 if (ctx
->xin
->resubmit_stats
) {
3787 netdev_vport_inc_tx(xport
->netdev
, ctx
->xin
->resubmit_stats
);
3789 if (ctx
->xin
->xcache
) {
3790 struct xc_entry
*entry
;
3792 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NETDEV
);
3793 entry
->dev
.tx
= netdev_ref(xport
->netdev
);
3795 out_port
= odp_port
;
3796 if (ovs_native_tunneling_is_on(ctx
->xbridge
->ofproto
)) {
3797 xlate_report(ctx
, OFT_DETAIL
, "output to native tunnel");
3798 is_native_tunnel
= true;
3800 xlate_report(ctx
, OFT_DETAIL
, "output to kernel tunnel");
3801 commit_odp_tunnel_action(flow
, &ctx
->base_flow
, ctx
->odp_actions
);
3802 flow
->tunnel
= flow_tnl
; /* Restore tunnel metadata */
3805 odp_port
= xport
->odp_port
;
3806 out_port
= odp_port
;
3809 if (out_port
!= ODPP_NONE
) {
3810 /* Commit accumulated flow updates before output. */
3811 xlate_commit_actions(ctx
);
3814 /* Recirculate the packet. */
3815 struct ovs_action_hash
*act_hash
;
3818 act_hash
= nl_msg_put_unspec_uninit(ctx
->odp_actions
,
3819 OVS_ACTION_ATTR_HASH
,
3821 act_hash
->hash_alg
= xr
->hash_alg
;
3822 act_hash
->hash_basis
= xr
->hash_basis
;
3824 /* Recirc action. */
3825 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_RECIRC
,
3827 } else if (is_native_tunnel
) {
3828 /* Output to native tunnel port. */
3829 build_tunnel_send(ctx
, xport
, flow
, odp_port
);
3830 flow
->tunnel
= flow_tnl
; /* Restore tunnel metadata */
3832 } else if (terminate_native_tunnel(ctx
, ofp_port
, flow
, wc
,
3834 /* Intercept packet to be received on native tunnel port. */
3835 nl_msg_put_odp_port(ctx
->odp_actions
, OVS_ACTION_ATTR_TUNNEL_POP
,
3839 /* Tunnel push-pop action is not compatible with
3841 compose_ipfix_action(ctx
, out_port
);
3843 /* Handle truncation of the mirrored packet. */
3844 if (ctx
->mirror_snaplen
> 0 &&
3845 ctx
->mirror_snaplen
< UINT16_MAX
) {
3846 struct ovs_action_trunc
*trunc
;
3848 trunc
= nl_msg_put_unspec_uninit(ctx
->odp_actions
,
3849 OVS_ACTION_ATTR_TRUNC
,
3851 trunc
->max_len
= ctx
->mirror_snaplen
;
3852 if (!ctx
->xbridge
->support
.trunc
) {
3853 ctx
->xout
->slow
|= SLOW_ACTION
;
3857 nl_msg_put_odp_port(ctx
->odp_actions
,
3858 OVS_ACTION_ATTR_OUTPUT
,
3862 ctx
->sflow_odp_port
= odp_port
;
3863 ctx
->sflow_n_outputs
++;
3864 ctx
->nf_output_iface
= ofp_port
;
3867 if (mbridge_has_mirrors(ctx
->xbridge
->mbridge
) && xport
->xbundle
) {
3868 mirror_packet(ctx
, xport
->xbundle
,
3869 xbundle_mirror_dst(xport
->xbundle
->xbridge
,
3875 memcpy(flow
->vlans
, flow_vlans
, sizeof flow
->vlans
);
3876 flow
->nw_tos
= flow_nw_tos
;
3877 flow
->dl_dst
= flow_dl_dst
;
3878 flow
->dl_src
= flow_dl_src
;
3879 flow
->packet_type
= flow_packet_type
;
3880 flow
->dl_type
= flow_dl_type
;
3884 compose_output_action(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
,
3885 const struct xlate_bond_recirc
*xr
)
3887 compose_output_action__(ctx
, ofp_port
, xr
, true);
3891 xlate_recursively(struct xlate_ctx
*ctx
, struct rule_dpif
*rule
, bool deepens
)
3893 struct rule_dpif
*old_rule
= ctx
->rule
;
3894 ovs_be64 old_cookie
= ctx
->rule_cookie
;
3895 const struct rule_actions
*actions
;
3897 if (ctx
->xin
->resubmit_stats
) {
3898 rule_dpif_credit_stats(rule
, ctx
->xin
->resubmit_stats
);
3903 ctx
->depth
+= deepens
;
3905 ctx
->rule_cookie
= rule
->up
.flow_cookie
;
3906 actions
= rule_get_actions(&rule
->up
);
3907 do_xlate_actions(actions
->ofpacts
, actions
->ofpacts_len
, ctx
);
3908 ctx
->rule_cookie
= old_cookie
;
3909 ctx
->rule
= old_rule
;
3910 ctx
->depth
-= deepens
;
3914 xlate_resubmit_resource_check(struct xlate_ctx
*ctx
)
3916 if (ctx
->depth
>= MAX_DEPTH
) {
3917 xlate_report_error(ctx
, "over max translation depth %d", MAX_DEPTH
);
3918 ctx
->error
= XLATE_RECURSION_TOO_DEEP
;
3919 } else if (ctx
->resubmits
>= MAX_RESUBMITS
) {
3920 xlate_report_error(ctx
, "over %d resubmit actions", MAX_RESUBMITS
);
3921 ctx
->error
= XLATE_TOO_MANY_RESUBMITS
;
3922 } else if (ctx
->odp_actions
->size
> UINT16_MAX
) {
3923 xlate_report_error(ctx
, "resubmits yielded over 64 kB of actions");
3924 /* NOT an error, as we'll be slow-pathing the flow in this case? */
3925 ctx
->exit
= true; /* XXX: translation still terminated! */
3926 } else if (ctx
->stack
.size
>= 65536) {
3927 xlate_report_error(ctx
, "resubmits yielded over 64 kB of stack");
3928 ctx
->error
= XLATE_STACK_TOO_DEEP
;
3937 tuple_swap_flow(struct flow
*flow
, bool ipv4
)
3939 uint8_t nw_proto
= flow
->nw_proto
;
3940 flow
->nw_proto
= flow
->ct_nw_proto
;
3941 flow
->ct_nw_proto
= nw_proto
;
3944 ovs_be32 nw_src
= flow
->nw_src
;
3945 flow
->nw_src
= flow
->ct_nw_src
;
3946 flow
->ct_nw_src
= nw_src
;
3948 ovs_be32 nw_dst
= flow
->nw_dst
;
3949 flow
->nw_dst
= flow
->ct_nw_dst
;
3950 flow
->ct_nw_dst
= nw_dst
;
3952 struct in6_addr ipv6_src
= flow
->ipv6_src
;
3953 flow
->ipv6_src
= flow
->ct_ipv6_src
;
3954 flow
->ct_ipv6_src
= ipv6_src
;
3956 struct in6_addr ipv6_dst
= flow
->ipv6_dst
;
3957 flow
->ipv6_dst
= flow
->ct_ipv6_dst
;
3958 flow
->ct_ipv6_dst
= ipv6_dst
;
3961 ovs_be16 tp_src
= flow
->tp_src
;
3962 flow
->tp_src
= flow
->ct_tp_src
;
3963 flow
->ct_tp_src
= tp_src
;
3965 ovs_be16 tp_dst
= flow
->tp_dst
;
3966 flow
->tp_dst
= flow
->ct_tp_dst
;
3967 flow
->ct_tp_dst
= tp_dst
;
3971 tuple_swap(struct flow
*flow
, struct flow_wildcards
*wc
)
3973 bool ipv4
= (flow
->dl_type
== htons(ETH_TYPE_IP
));
3975 tuple_swap_flow(flow
, ipv4
);
3976 tuple_swap_flow(&wc
->masks
, ipv4
);
3980 xlate_table_action(struct xlate_ctx
*ctx
, ofp_port_t in_port
, uint8_t table_id
,
3981 bool may_packet_in
, bool honor_table_miss
,
3984 /* Check if we need to recirculate before matching in a table. */
3985 if (ctx
->was_mpls
) {
3986 ctx_trigger_freeze(ctx
);
3989 if (xlate_resubmit_resource_check(ctx
)) {
3990 uint8_t old_table_id
= ctx
->table_id
;
3991 struct rule_dpif
*rule
;
3993 ctx
->table_id
= table_id
;
3995 /* Swap packet fields with CT 5-tuple if requested. */
3997 /* Do not swap if there is no CT tuple, or if key is not IP. */
3998 if (ctx
->xin
->flow
.ct_nw_proto
== 0 ||
3999 !is_ip_any(&ctx
->xin
->flow
)) {
4000 xlate_report_error(ctx
,
4001 "resubmit(ct) with non-tracked or non-IP packet!");
4004 tuple_swap(&ctx
->xin
->flow
, ctx
->wc
);
4006 rule
= rule_dpif_lookup_from_table(ctx
->xbridge
->ofproto
,
4007 ctx
->xin
->tables_version
,
4008 &ctx
->xin
->flow
, ctx
->wc
,
4009 ctx
->xin
->resubmit_stats
,
4010 &ctx
->table_id
, in_port
,
4011 may_packet_in
, honor_table_miss
,
4015 tuple_swap(&ctx
->xin
->flow
, ctx
->wc
);
4019 /* Fill in the cache entry here instead of xlate_recursively
4020 * to make the reference counting more explicit. We take a
4021 * reference in the lookups above if we are going to cache the
4023 if (ctx
->xin
->xcache
) {
4024 struct xc_entry
*entry
;
4026 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_RULE
);
4028 ofproto_rule_ref(&rule
->up
);
4031 struct ovs_list
*old_trace
= ctx
->xin
->trace
;
4032 xlate_report_table(ctx
, rule
, table_id
);
4033 xlate_recursively(ctx
, rule
, table_id
<= old_table_id
);
4034 ctx
->xin
->trace
= old_trace
;
4037 ctx
->table_id
= old_table_id
;
4042 /* Consumes the group reference, which is only taken if xcache exists. */
4044 xlate_group_stats(struct xlate_ctx
*ctx
, struct group_dpif
*group
,
4045 struct ofputil_bucket
*bucket
)
4047 if (ctx
->xin
->resubmit_stats
) {
4048 group_dpif_credit_stats(group
, bucket
, ctx
->xin
->resubmit_stats
);
4050 if (ctx
->xin
->xcache
) {
4051 struct xc_entry
*entry
;
4053 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_GROUP
);
4054 entry
->group
.group
= group
;
4055 entry
->group
.bucket
= bucket
;
4060 xlate_group_bucket(struct xlate_ctx
*ctx
, struct ofputil_bucket
*bucket
)
4062 uint64_t action_list_stub
[1024 / 8];
4063 struct ofpbuf action_list
= OFPBUF_STUB_INITIALIZER(action_list_stub
);
4064 struct ofpbuf action_set
= ofpbuf_const_initializer(bucket
->ofpacts
,
4065 bucket
->ofpacts_len
);
4066 struct flow old_flow
= ctx
->xin
->flow
;
4067 bool old_was_mpls
= ctx
->was_mpls
;
4069 ofpacts_execute_action_set(&action_list
, &action_set
);
4071 do_xlate_actions(action_list
.data
, action_list
.size
, ctx
);
4074 ofpbuf_uninit(&action_list
);
4076 /* Check if need to freeze. */
4077 if (ctx
->freezing
) {
4078 finish_freezing(ctx
);
4081 /* Roll back flow to previous state.
4082 * This is equivalent to cloning the packet for each bucket.
4084 * As a side effect any subsequently applied actions will
4085 * also effectively be applied to a clone of the packet taken
4086 * just before applying the all or indirect group.
4088 * Note that group buckets are action sets, hence they cannot modify the
4089 * main action set. Also any stack actions are ignored when executing an
4090 * action set, so group buckets cannot change the stack either.
4091 * However, we do allow resubmit actions in group buckets, which could
4092 * break the above assumptions. It is up to the controller to not mess up
4093 * with the action_set and stack in the tables resubmitted to from
4095 ctx
->xin
->flow
= old_flow
;
4097 /* The group bucket popping MPLS should have no effect after bucket
4099 ctx
->was_mpls
= old_was_mpls
;
4101 /* The fact that the group bucket exits (for any reason) does not mean that
4102 * the translation after the group action should exit. Specifically, if
4103 * the group bucket freezes translation, the actions after the group action
4104 * must continue processing with the original, not the frozen packet! */
4109 xlate_all_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
4111 struct ofputil_bucket
*bucket
;
4112 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
4113 xlate_group_bucket(ctx
, bucket
);
4115 xlate_group_stats(ctx
, group
, NULL
);
4119 xlate_ff_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
4121 struct ofputil_bucket
*bucket
;
4123 bucket
= group_first_live_bucket(ctx
, group
, 0);
4125 xlate_group_bucket(ctx
, bucket
);
4126 xlate_group_stats(ctx
, group
, bucket
);
4127 } else if (ctx
->xin
->xcache
) {
4128 ofproto_group_unref(&group
->up
);
4133 xlate_default_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
4135 struct flow_wildcards
*wc
= ctx
->wc
;
4136 struct ofputil_bucket
*bucket
;
4139 basis
= flow_hash_symmetric_l4(&ctx
->xin
->flow
, 0);
4140 flow_mask_hash_fields(&ctx
->xin
->flow
, wc
, NX_HASH_FIELDS_SYMMETRIC_L4
);
4141 bucket
= group_best_live_bucket(ctx
, group
, basis
);
4143 xlate_group_bucket(ctx
, bucket
);
4144 xlate_group_stats(ctx
, group
, bucket
);
4145 } else if (ctx
->xin
->xcache
) {
4146 ofproto_group_unref(&group
->up
);
4151 xlate_hash_fields_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
4153 const struct field_array
*fields
= &group
->up
.props
.fields
;
4154 const uint8_t *mask_values
= fields
->values
;
4155 uint32_t basis
= hash_uint64(group
->up
.props
.selection_method_param
);
4158 BITMAP_FOR_EACH_1 (i
, MFF_N_IDS
, fields
->used
.bm
) {
4159 const struct mf_field
*mf
= mf_from_id(i
);
4161 /* Skip fields for which prerequisites are not met. */
4162 if (!mf_are_prereqs_ok(mf
, &ctx
->xin
->flow
, ctx
->wc
)) {
4163 /* Skip the mask bytes for this field. */
4164 mask_values
+= mf
->n_bytes
;
4168 union mf_value value
;
4169 union mf_value mask
;
4171 mf_get_value(mf
, &ctx
->xin
->flow
, &value
);
4172 /* Mask the value. */
4173 for (int j
= 0; j
< mf
->n_bytes
; j
++) {
4174 mask
.b
[j
] = *mask_values
++;
4175 value
.b
[j
] &= mask
.b
[j
];
4177 basis
= hash_bytes(&value
, mf
->n_bytes
, basis
);
4179 /* For tunnels, hash in whether the field is present. */
4180 if (mf_is_tun_metadata(mf
)) {
4181 basis
= hash_boolean(mf_is_set(mf
, &ctx
->xin
->flow
), basis
);
4184 mf_mask_field_masked(mf
, &mask
, ctx
->wc
);
4187 struct ofputil_bucket
*bucket
= group_best_live_bucket(ctx
, group
, basis
);
4189 xlate_group_bucket(ctx
, bucket
);
4190 xlate_group_stats(ctx
, group
, bucket
);
4191 } else if (ctx
->xin
->xcache
) {
4192 ofproto_group_unref(&group
->up
);
4197 xlate_dp_hash_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
4199 struct ofputil_bucket
*bucket
;
4201 /* dp_hash value 0 is special since it means that the dp_hash has not been
4202 * computed, as all computed dp_hash values are non-zero. Therefore
4203 * compare to zero can be used to decide if the dp_hash value is valid
4204 * without masking the dp_hash field. */
4205 if (!ctx
->xin
->flow
.dp_hash
) {
4206 uint64_t param
= group
->up
.props
.selection_method_param
;
4208 ctx_trigger_recirculate_with_hash(ctx
, param
>> 32, (uint32_t)param
);
4210 uint32_t n_buckets
= group
->up
.n_buckets
;
4212 /* Minimal mask to cover the number of buckets. */
4213 uint32_t mask
= (1 << log_2_ceil(n_buckets
)) - 1;
4214 /* Multiplier chosen to make the trivial 1 bit case to
4215 * actually distribute amongst two equal weight buckets. */
4216 uint32_t basis
= 0xc2b73583 * (ctx
->xin
->flow
.dp_hash
& mask
);
4218 ctx
->wc
->masks
.dp_hash
|= mask
;
4219 bucket
= group_best_live_bucket(ctx
, group
, basis
);
4221 xlate_group_bucket(ctx
, bucket
);
4222 xlate_group_stats(ctx
, group
, bucket
);
4229 xlate_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
4231 const char *selection_method
= group
->up
.props
.selection_method
;
4233 /* Select groups may access flow keys beyond L2 in order to
4234 * select a bucket. Recirculate as appropriate to make this possible.
4236 if (ctx
->was_mpls
) {
4237 ctx_trigger_freeze(ctx
);
4240 if (selection_method
[0] == '\0') {
4241 xlate_default_select_group(ctx
, group
);
4242 } else if (!strcasecmp("hash", selection_method
)) {
4243 xlate_hash_fields_select_group(ctx
, group
);
4244 } else if (!strcasecmp("dp_hash", selection_method
)) {
4245 xlate_dp_hash_select_group(ctx
, group
);
4247 /* Parsing of groups should ensure this never happens */
4253 xlate_group_action__(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
4255 bool was_in_group
= ctx
->in_group
;
4256 ctx
->in_group
= true;
4258 switch (group
->up
.type
) {
4260 case OFPGT11_INDIRECT
:
4261 xlate_all_group(ctx
, group
);
4263 case OFPGT11_SELECT
:
4264 xlate_select_group(ctx
, group
);
4267 xlate_ff_group(ctx
, group
);
4273 ctx
->in_group
= was_in_group
;
4277 xlate_group_action(struct xlate_ctx
*ctx
, uint32_t group_id
)
4279 if (xlate_resubmit_resource_check(ctx
)) {
4280 struct group_dpif
*group
;
4282 /* Take ref only if xcache exists. */
4283 group
= group_dpif_lookup(ctx
->xbridge
->ofproto
, group_id
,
4284 ctx
->xin
->tables_version
, ctx
->xin
->xcache
);
4286 /* XXX: Should set ctx->error ? */
4287 xlate_report(ctx
, OFT_WARN
, "output to nonexistent group %"PRIu32
,
4291 xlate_group_action__(ctx
, group
);
4298 xlate_ofpact_resubmit(struct xlate_ctx
*ctx
,
4299 const struct ofpact_resubmit
*resubmit
)
4303 bool may_packet_in
= false;
4304 bool honor_table_miss
= false;
4306 if (ctx
->rule
&& rule_dpif_is_internal(ctx
->rule
)) {
4307 /* Still allow missed packets to be sent to the controller
4308 * if resubmitting from an internal table. */
4309 may_packet_in
= true;
4310 honor_table_miss
= true;
4313 in_port
= resubmit
->in_port
;
4314 if (in_port
== OFPP_IN_PORT
) {
4315 in_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
4318 table_id
= resubmit
->table_id
;
4319 if (table_id
== 255) {
4320 table_id
= ctx
->table_id
;
4323 xlate_table_action(ctx
, in_port
, table_id
, may_packet_in
,
4324 honor_table_miss
, resubmit
->with_ct_orig
);
4328 flood_packets(struct xlate_ctx
*ctx
, bool all
)
4330 const struct xport
*xport
;
4332 HMAP_FOR_EACH (xport
, ofp_node
, &ctx
->xbridge
->xports
) {
4333 if (xport
->ofp_port
== ctx
->xin
->flow
.in_port
.ofp_port
) {
4338 compose_output_action__(ctx
, xport
->ofp_port
, NULL
, false);
4339 } else if (!(xport
->config
& OFPUTIL_PC_NO_FLOOD
)) {
4340 compose_output_action(ctx
, xport
->ofp_port
, NULL
);
4344 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
4347 /* Copy and reformat a partially xlated odp actions to a new
4348 * odp actions list in 'b', so that the new actions list
4349 * can be executed by odp_execute_actions.
4351 * When xlate using nested odp actions, such as sample and clone,
4352 * the nested action created by nl_msg_start_nested() may not
4353 * have been properly closed yet, thus can not be executed
4356 * Since unclosed nested action has to be last action, it can be
4357 * fixed by skipping the outer header, and treating the actions within
4358 * as if they are outside the nested attribute since the effect
4359 * of executing them on packet is the same.
4361 * As an optimization, a fully closed 'sample' or 'clone' action
4362 * is skipped since their execution has no effect to the packet.
4364 * Returns true if success. 'b' contains the new actions list.
4365 * The caller is responsible for disposing 'b'.
4367 * Returns false if error, 'b' has been freed already. */
4369 xlate_fixup_actions(struct ofpbuf
*b
, const struct nlattr
*actions
,
4372 const struct nlattr
*a
;
4375 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, actions
, actions_len
) {
4376 int type
= nl_attr_type(a
);
4378 switch ((enum ovs_action_attr
) type
) {
4379 case OVS_ACTION_ATTR_HASH
:
4380 case OVS_ACTION_ATTR_PUSH_VLAN
:
4381 case OVS_ACTION_ATTR_POP_VLAN
:
4382 case OVS_ACTION_ATTR_PUSH_MPLS
:
4383 case OVS_ACTION_ATTR_POP_MPLS
:
4384 case OVS_ACTION_ATTR_SET
:
4385 case OVS_ACTION_ATTR_SET_MASKED
:
4386 case OVS_ACTION_ATTR_TRUNC
:
4387 case OVS_ACTION_ATTR_OUTPUT
:
4388 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
4389 case OVS_ACTION_ATTR_TUNNEL_POP
:
4390 case OVS_ACTION_ATTR_USERSPACE
:
4391 case OVS_ACTION_ATTR_RECIRC
:
4392 case OVS_ACTION_ATTR_CT
:
4393 case OVS_ACTION_ATTR_PUSH_ETH
:
4394 case OVS_ACTION_ATTR_POP_ETH
:
4395 case OVS_ACTION_ATTR_ENCAP_NSH
:
4396 case OVS_ACTION_ATTR_DECAP_NSH
:
4397 case OVS_ACTION_ATTR_METER
:
4398 ofpbuf_put(b
, a
, nl_attr_len_pad(a
, left
));
4401 case OVS_ACTION_ATTR_CLONE
:
4402 /* If the clone action has been fully xlated, it can
4403 * be skipped, since any actions executed within clone
4404 * do not affect the current packet.
4406 * When xlating actions within clone, the clone action,
4407 * because it is an nested netlink attribute, do not have
4408 * a valid 'nla_len'; it will be zero instead. Skip
4409 * the clone header to find the start of the actions
4410 * enclosed. Treat those actions as if they are written
4411 * outside of clone. */
4414 if (left
< NLA_HDRLEN
) {
4418 ok
= xlate_fixup_actions(b
, nl_attr_get_unspec(a
, 0),
4426 case OVS_ACTION_ATTR_SAMPLE
:
4429 if (left
< NLA_HDRLEN
) {
4432 const struct nlattr
*attr
= nl_attr_get_unspec(a
, 0);
4436 nl_attr_type(attr
) != OVS_SAMPLE_ATTR_ACTIONS
) {
4437 /* Only OVS_SAMPLE_ATTR_ACTIONS can have unclosed
4438 * nested netlink attribute. */
4439 if (!attr
->nla_len
) {
4443 left
-= NLA_ALIGN(attr
->nla_len
);
4444 attr
= nl_attr_next(attr
);
4447 if (left
< NLA_HDRLEN
) {
4451 ok
= xlate_fixup_actions(b
, nl_attr_get_unspec(attr
, 0),
4459 case OVS_ACTION_ATTR_UNSPEC
:
4460 case __OVS_ACTION_ATTR_MAX
:
4473 xlate_execute_odp_actions(struct dp_packet
*packet
,
4474 const struct nlattr
*actions
, int actions_len
)
4476 struct dp_packet_batch batch
;
4477 struct ofpbuf
*b
= ofpbuf_new(actions_len
);
4479 if (!xlate_fixup_actions(b
, actions
, actions_len
)) {
4483 dp_packet_batch_init_packet(&batch
, packet
);
4484 odp_execute_actions(NULL
, &batch
, false, b
->data
, b
->size
, NULL
);
4491 execute_controller_action(struct xlate_ctx
*ctx
, int len
,
4492 enum ofp_packet_in_reason reason
,
4493 uint16_t controller_id
,
4494 const uint8_t *userdata
, size_t userdata_len
)
4496 struct dp_packet
*packet
;
4498 ctx
->xout
->slow
|= SLOW_CONTROLLER
;
4499 xlate_commit_actions(ctx
);
4500 if (!ctx
->xin
->packet
) {
4504 if (!ctx
->xin
->allow_side_effects
&& !ctx
->xin
->xcache
) {
4508 packet
= dp_packet_clone(ctx
->xin
->packet
);
4509 if (!xlate_execute_odp_actions(packet
, ctx
->odp_actions
->data
,
4510 ctx
->odp_actions
->size
)) {
4511 xlate_report_error(ctx
, "Failed to execute controller action");
4512 dp_packet_delete(packet
);
4516 /* A packet sent by an action in a table-miss rule is considered an
4517 * explicit table miss. OpenFlow before 1.3 doesn't have that concept so
4518 * it will get translated back to OFPR_ACTION for those versions. */
4519 if (reason
== OFPR_ACTION
4520 && ctx
->rule
&& rule_is_table_miss(&ctx
->rule
->up
)) {
4521 reason
= OFPR_EXPLICIT_MISS
;
4524 size_t packet_len
= dp_packet_size(packet
);
4526 struct ofproto_async_msg
*am
= xmalloc(sizeof *am
);
4527 *am
= (struct ofproto_async_msg
) {
4528 .controller_id
= controller_id
,
4529 .oam
= OAM_PACKET_IN
,
4533 .packet
= dp_packet_steal_data(packet
),
4534 .packet_len
= packet_len
,
4536 .table_id
= ctx
->table_id
,
4537 .cookie
= ctx
->rule_cookie
,
4538 .userdata
= (userdata_len
4539 ? xmemdup(userdata
, userdata_len
)
4541 .userdata_len
= userdata_len
,
4547 flow_get_metadata(&ctx
->xin
->flow
, &am
->pin
.up
.base
.flow_metadata
);
4549 /* Async messages are only sent once, so if we send one now, no
4550 * xlate cache entry is created. */
4551 if (ctx
->xin
->allow_side_effects
) {
4552 ofproto_dpif_send_async_msg(ctx
->xbridge
->ofproto
, am
);
4553 } else /* xcache */ {
4554 struct xc_entry
*entry
;
4556 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_CONTROLLER
);
4557 entry
->controller
.ofproto
= ctx
->xbridge
->ofproto
;
4558 entry
->controller
.am
= am
;
4561 dp_packet_delete(packet
);
4565 emit_continuation(struct xlate_ctx
*ctx
, const struct frozen_state
*state
)
4567 if (!ctx
->xin
->allow_side_effects
&& !ctx
->xin
->xcache
) {
4571 struct ofproto_async_msg
*am
= xmalloc(sizeof *am
);
4572 *am
= (struct ofproto_async_msg
) {
4573 .controller_id
= ctx
->pause
->controller_id
,
4574 .oam
= OAM_PACKET_IN
,
4578 .userdata
= xmemdup(ctx
->pause
->userdata
,
4579 ctx
->pause
->userdata_len
),
4580 .userdata_len
= ctx
->pause
->userdata_len
,
4581 .packet
= xmemdup(dp_packet_data(ctx
->xin
->packet
),
4582 dp_packet_size(ctx
->xin
->packet
)),
4583 .packet_len
= dp_packet_size(ctx
->xin
->packet
),
4584 .reason
= ctx
->pause
->reason
,
4586 .bridge
= ctx
->xbridge
->ofproto
->uuid
,
4587 .stack
= xmemdup(state
->stack
, state
->stack_size
),
4588 .stack_size
= state
->stack_size
,
4589 .mirrors
= state
->mirrors
,
4590 .conntracked
= state
->conntracked
,
4591 .actions
= xmemdup(state
->ofpacts
, state
->ofpacts_len
),
4592 .actions_len
= state
->ofpacts_len
,
4593 .action_set
= xmemdup(state
->action_set
,
4594 state
->action_set_len
),
4595 .action_set_len
= state
->action_set_len
,
4597 .max_len
= UINT16_MAX
,
4600 flow_get_metadata(ctx
->paused_flow
, &am
->pin
.up
.base
.flow_metadata
);
4602 /* Async messages are only sent once, so if we send one now, no
4603 * xlate cache entry is created. */
4604 if (ctx
->xin
->allow_side_effects
) {
4605 ofproto_dpif_send_async_msg(ctx
->xbridge
->ofproto
, am
);
4606 } else /* xcache */ {
4607 struct xc_entry
*entry
;
4609 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_CONTROLLER
);
4610 entry
->controller
.ofproto
= ctx
->xbridge
->ofproto
;
4611 entry
->controller
.am
= am
;
4615 /* Creates a frozen state, and allocates a unique recirc id for the given
4616 * state. Returns a non-zero recirc id if it is allocated successfully.
4617 * Returns 0 otherwise.
4620 finish_freezing__(struct xlate_ctx
*ctx
, uint8_t table
)
4623 ovs_assert(ctx
->freezing
);
4625 struct frozen_state state
= {
4627 .ofproto_uuid
= ctx
->xbridge
->ofproto
->uuid
,
4628 .stack
= ctx
->stack
.data
,
4629 .stack_size
= ctx
->stack
.size
,
4630 .mirrors
= ctx
->mirrors
,
4631 .conntracked
= ctx
->conntracked
,
4632 .ofpacts
= ctx
->frozen_actions
.data
,
4633 .ofpacts_len
= ctx
->frozen_actions
.size
,
4634 .action_set
= ctx
->action_set
.data
,
4635 .action_set_len
= ctx
->action_set
.size
,
4637 frozen_metadata_from_flow(&state
.metadata
, &ctx
->xin
->flow
);
4640 if (ctx
->xin
->packet
) {
4641 emit_continuation(ctx
, &state
);
4644 /* Allocate a unique recirc id for the given metadata state in the
4645 * flow. An existing id, with a new reference to the corresponding
4646 * recirculation context, will be returned if possible.
4647 * The life-cycle of this recirc id is managed by associating it
4648 * with the udpif key ('ukey') created for each new datapath flow. */
4649 id
= recirc_alloc_id_ctx(&state
);
4651 xlate_report_error(ctx
, "Failed to allocate recirculation id");
4652 ctx
->error
= XLATE_NO_RECIRCULATION_CONTEXT
;
4655 recirc_refs_add(&ctx
->xout
->recircs
, id
);
4657 if (ctx
->recirc_update_dp_hash
) {
4658 struct ovs_action_hash
*act_hash
;
4661 act_hash
= nl_msg_put_unspec_uninit(ctx
->odp_actions
,
4662 OVS_ACTION_ATTR_HASH
,
4664 act_hash
->hash_alg
= OVS_HASH_ALG_L4
; /* Make configurable. */
4665 act_hash
->hash_basis
= 0; /* Make configurable. */
4667 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_RECIRC
, id
);
4670 /* Undo changes done by freezing. */
4671 ctx_cancel_freeze(ctx
);
4675 /* Called only when we're freezing. */
4677 finish_freezing(struct xlate_ctx
*ctx
)
4679 xlate_commit_actions(ctx
);
4680 finish_freezing__(ctx
, 0);
4683 /* Fork the pipeline here. The current packet will continue processing the
4684 * current action list. A clone of the current packet will recirculate, skip
4685 * the remainder of the current action list and asynchronously resume pipeline
4686 * processing in 'table' with the current metadata and action set. */
4688 compose_recirculate_and_fork(struct xlate_ctx
*ctx
, uint8_t table
)
4691 ctx
->freezing
= true;
4692 recirc_id
= finish_freezing__(ctx
, table
);
4694 if (OVS_UNLIKELY(ctx
->xin
->trace
) && recirc_id
) {
4695 if (oftrace_add_recirc_node(ctx
->xin
->recirc_queue
,
4696 OFT_RECIRC_CONNTRACK
, &ctx
->xin
->flow
,
4697 ctx
->xin
->packet
, recirc_id
)) {
4698 xlate_report(ctx
, OFT_DETAIL
, "A clone of the packet is forked to "
4699 "recirculate. The forked pipeline will be resumed at "
4700 "table %u.", table
);
4702 xlate_report(ctx
, OFT_DETAIL
, "Failed to trace the conntrack "
4703 "forked pipeline with recirc_id = %d.", recirc_id
);
4709 compose_mpls_push_action(struct xlate_ctx
*ctx
, struct ofpact_push_mpls
*mpls
)
4711 struct flow
*flow
= &ctx
->xin
->flow
;
4714 ovs_assert(eth_type_mpls(mpls
->ethertype
));
4716 n
= flow_count_mpls_labels(flow
, ctx
->wc
);
4718 xlate_commit_actions(ctx
);
4719 } else if (n
>= FLOW_MAX_MPLS_LABELS
) {
4720 if (ctx
->xin
->packet
!= NULL
) {
4721 xlate_report_error(ctx
, "dropping packet on which an MPLS push "
4722 "action can't be performed as it would have "
4723 "more MPLS LSEs than the %d supported.",
4724 FLOW_MAX_MPLS_LABELS
);
4726 ctx
->error
= XLATE_TOO_MANY_MPLS_LABELS
;
4730 /* Update flow's MPLS stack, and clear L3/4 fields to mark them invalid. */
4731 flow_push_mpls(flow
, n
, mpls
->ethertype
, ctx
->wc
, true);
4735 compose_mpls_pop_action(struct xlate_ctx
*ctx
, ovs_be16 eth_type
)
4737 struct flow
*flow
= &ctx
->xin
->flow
;
4738 int n
= flow_count_mpls_labels(flow
, ctx
->wc
);
4740 if (flow_pop_mpls(flow
, n
, eth_type
, ctx
->wc
)) {
4741 if (!eth_type_mpls(eth_type
) && ctx
->xbridge
->support
.odp
.recirc
) {
4742 ctx
->was_mpls
= true;
4744 } else if (n
>= FLOW_MAX_MPLS_LABELS
) {
4745 if (ctx
->xin
->packet
!= NULL
) {
4746 xlate_report_error(ctx
, "dropping packet on which an "
4747 "MPLS pop action can't be performed as it has "
4748 "more MPLS LSEs than the %d supported.",
4749 FLOW_MAX_MPLS_LABELS
);
4751 ctx
->error
= XLATE_TOO_MANY_MPLS_LABELS
;
4752 ofpbuf_clear(ctx
->odp_actions
);
4757 compose_dec_ttl(struct xlate_ctx
*ctx
, struct ofpact_cnt_ids
*ids
)
4759 struct flow
*flow
= &ctx
->xin
->flow
;
4761 if (!is_ip_any(flow
)) {
4765 ctx
->wc
->masks
.nw_ttl
= 0xff;
4766 if (flow
->nw_ttl
> 1) {
4772 for (i
= 0; i
< ids
->n_controllers
; i
++) {
4773 execute_controller_action(ctx
, UINT16_MAX
, OFPR_INVALID_TTL
,
4774 ids
->cnt_ids
[i
], NULL
, 0);
4777 /* Stop processing for current table. */
4778 xlate_report(ctx
, OFT_WARN
, "IPv%d decrement TTL exception",
4779 flow
->dl_type
== htons(ETH_TYPE_IP
) ? 4 : 6);
4785 compose_set_mpls_label_action(struct xlate_ctx
*ctx
, ovs_be32 label
)
4787 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
4788 ctx
->wc
->masks
.mpls_lse
[0] |= htonl(MPLS_LABEL_MASK
);
4789 set_mpls_lse_label(&ctx
->xin
->flow
.mpls_lse
[0], label
);
4794 compose_set_mpls_tc_action(struct xlate_ctx
*ctx
, uint8_t tc
)
4796 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
4797 ctx
->wc
->masks
.mpls_lse
[0] |= htonl(MPLS_TC_MASK
);
4798 set_mpls_lse_tc(&ctx
->xin
->flow
.mpls_lse
[0], tc
);
4803 compose_set_mpls_ttl_action(struct xlate_ctx
*ctx
, uint8_t ttl
)
4805 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
4806 ctx
->wc
->masks
.mpls_lse
[0] |= htonl(MPLS_TTL_MASK
);
4807 set_mpls_lse_ttl(&ctx
->xin
->flow
.mpls_lse
[0], ttl
);
4812 compose_dec_mpls_ttl_action(struct xlate_ctx
*ctx
)
4814 struct flow
*flow
= &ctx
->xin
->flow
;
4816 if (eth_type_mpls(flow
->dl_type
)) {
4817 uint8_t ttl
= mpls_lse_to_ttl(flow
->mpls_lse
[0]);
4819 ctx
->wc
->masks
.mpls_lse
[0] |= htonl(MPLS_TTL_MASK
);
4822 set_mpls_lse_ttl(&flow
->mpls_lse
[0], ttl
);
4825 execute_controller_action(ctx
, UINT16_MAX
, OFPR_INVALID_TTL
, 0,
4830 /* Stop processing for current table. */
4831 xlate_report(ctx
, OFT_WARN
, "MPLS decrement TTL exception");
4836 xlate_output_action(struct xlate_ctx
*ctx
,
4837 ofp_port_t port
, uint16_t max_len
, bool may_packet_in
)
4839 ofp_port_t prev_nf_output_iface
= ctx
->nf_output_iface
;
4841 ctx
->nf_output_iface
= NF_OUT_DROP
;
4845 compose_output_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
, NULL
);
4848 xlate_table_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
,
4849 0, may_packet_in
, true, false);
4855 flood_packets(ctx
, false);
4858 flood_packets(ctx
, true);
4860 case OFPP_CONTROLLER
:
4861 execute_controller_action(ctx
, max_len
,
4862 (ctx
->in_packet_out
? OFPR_PACKET_OUT
4863 : ctx
->in_group
? OFPR_GROUP
4864 : ctx
->in_action_set
? OFPR_ACTION_SET
4872 if (port
!= ctx
->xin
->flow
.in_port
.ofp_port
) {
4873 compose_output_action(ctx
, port
, NULL
);
4875 xlate_report(ctx
, OFT_WARN
, "skipping output to input port");
4880 if (prev_nf_output_iface
== NF_OUT_FLOOD
) {
4881 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
4882 } else if (ctx
->nf_output_iface
== NF_OUT_DROP
) {
4883 ctx
->nf_output_iface
= prev_nf_output_iface
;
4884 } else if (prev_nf_output_iface
!= NF_OUT_DROP
&&
4885 ctx
->nf_output_iface
!= NF_OUT_FLOOD
) {
4886 ctx
->nf_output_iface
= NF_OUT_MULTI
;
4891 xlate_output_reg_action(struct xlate_ctx
*ctx
,
4892 const struct ofpact_output_reg
*or)
4894 uint64_t port
= mf_get_subfield(&or->src
, &ctx
->xin
->flow
);
4895 if (port
<= UINT16_MAX
) {
4896 xlate_report(ctx
, OFT_DETAIL
, "output port is %"PRIu64
, port
);
4898 union mf_subvalue value
;
4900 memset(&value
, 0xff, sizeof value
);
4901 mf_write_subfield_flow(&or->src
, &value
, &ctx
->wc
->masks
);
4902 xlate_output_action(ctx
, u16_to_ofp(port
), or->max_len
, false);
4904 xlate_report(ctx
, OFT_WARN
, "output port %"PRIu64
" is out of range",
4910 xlate_output_trunc_action(struct xlate_ctx
*ctx
,
4911 ofp_port_t port
, uint32_t max_len
)
4913 bool support_trunc
= ctx
->xbridge
->support
.trunc
;
4914 struct ovs_action_trunc
*trunc
;
4915 char name
[OFP10_MAX_PORT_NAME_LEN
];
4922 case OFPP_CONTROLLER
:
4924 ofputil_port_to_string(port
, NULL
, name
, sizeof name
);
4925 xlate_report(ctx
, OFT_WARN
,
4926 "output_trunc does not support port: %s", name
);
4931 if (port
!= ctx
->xin
->flow
.in_port
.ofp_port
) {
4932 const struct xport
*xport
= get_ofp_port(ctx
->xbridge
, port
);
4934 if (xport
== NULL
|| xport
->odp_port
== ODPP_NONE
) {
4935 /* Since truncate happens at its following output action, if
4936 * the output port is a patch port, the behavior is somehow
4937 * unpredictable. For simplicity, disallow this case. */
4938 ofputil_port_to_string(port
, NULL
, name
, sizeof name
);
4939 xlate_report_error(ctx
, "output_trunc does not support "
4940 "patch port %s", name
);
4944 trunc
= nl_msg_put_unspec_uninit(ctx
->odp_actions
,
4945 OVS_ACTION_ATTR_TRUNC
,
4947 trunc
->max_len
= max_len
;
4948 xlate_output_action(ctx
, port
, max_len
, false);
4949 if (!support_trunc
) {
4950 ctx
->xout
->slow
|= SLOW_ACTION
;
4953 xlate_report(ctx
, OFT_WARN
, "skipping output to input port");
4960 xlate_enqueue_action(struct xlate_ctx
*ctx
,
4961 const struct ofpact_enqueue
*enqueue
)
4963 ofp_port_t ofp_port
= enqueue
->port
;
4964 uint32_t queue_id
= enqueue
->queue
;
4965 uint32_t flow_priority
, priority
;
4968 /* Translate queue to priority. */
4969 error
= dpif_queue_to_priority(ctx
->xbridge
->dpif
, queue_id
, &priority
);
4971 /* Fall back to ordinary output action. */
4972 xlate_output_action(ctx
, enqueue
->port
, 0, false);
4976 /* Check output port. */
4977 if (ofp_port
== OFPP_IN_PORT
) {
4978 ofp_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
4979 } else if (ofp_port
== ctx
->xin
->flow
.in_port
.ofp_port
) {
4983 /* Add datapath actions. */
4984 flow_priority
= ctx
->xin
->flow
.skb_priority
;
4985 ctx
->xin
->flow
.skb_priority
= priority
;
4986 compose_output_action(ctx
, ofp_port
, NULL
);
4987 ctx
->xin
->flow
.skb_priority
= flow_priority
;
4989 /* Update NetFlow output port. */
4990 if (ctx
->nf_output_iface
== NF_OUT_DROP
) {
4991 ctx
->nf_output_iface
= ofp_port
;
4992 } else if (ctx
->nf_output_iface
!= NF_OUT_FLOOD
) {
4993 ctx
->nf_output_iface
= NF_OUT_MULTI
;
4998 xlate_set_queue_action(struct xlate_ctx
*ctx
, uint32_t queue_id
)
5000 uint32_t skb_priority
;
5002 if (!dpif_queue_to_priority(ctx
->xbridge
->dpif
, queue_id
, &skb_priority
)) {
5003 ctx
->xin
->flow
.skb_priority
= skb_priority
;
5005 /* Couldn't translate queue to a priority. Nothing to do. A warning
5006 * has already been logged. */
5011 slave_enabled_cb(ofp_port_t ofp_port
, void *xbridge_
)
5013 const struct xbridge
*xbridge
= xbridge_
;
5024 case OFPP_CONTROLLER
: /* Not supported by the bundle action. */
5027 port
= get_ofp_port(xbridge
, ofp_port
);
5028 return port
? port
->may_enable
: false;
5033 xlate_bundle_action(struct xlate_ctx
*ctx
,
5034 const struct ofpact_bundle
*bundle
)
5038 port
= bundle_execute(bundle
, &ctx
->xin
->flow
, ctx
->wc
, slave_enabled_cb
,
5039 CONST_CAST(struct xbridge
*, ctx
->xbridge
));
5040 if (bundle
->dst
.field
) {
5041 nxm_reg_load(&bundle
->dst
, ofp_to_u16(port
), &ctx
->xin
->flow
, ctx
->wc
);
5042 xlate_report_subfield(ctx
, &bundle
->dst
);
5044 xlate_output_action(ctx
, port
, 0, false);
5049 xlate_learn_action(struct xlate_ctx
*ctx
, const struct ofpact_learn
*learn
)
5051 learn_mask(learn
, ctx
->wc
);
5053 if (ctx
->xin
->xcache
|| ctx
->xin
->allow_side_effects
) {
5054 uint64_t ofpacts_stub
[1024 / 8];
5055 struct ofputil_flow_mod fm
;
5056 struct ofproto_flow_mod ofm__
, *ofm
;
5057 struct ofpbuf ofpacts
;
5060 if (ctx
->xin
->xcache
) {
5061 ofm
= xmalloc(sizeof *ofm
);
5066 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
5067 learn_execute(learn
, &ctx
->xin
->flow
, &fm
, &ofpacts
);
5068 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
5069 struct ds s
= DS_EMPTY_INITIALIZER
;
5070 ds_put_format(&s
, "table=%"PRIu8
" ", fm
.table_id
);
5071 match_format(&fm
.match
, NULL
, &s
, OFP_DEFAULT_PRIORITY
);
5073 ds_put_format(&s
, " priority=%d", fm
.priority
);
5074 if (fm
.new_cookie
) {
5075 ds_put_format(&s
, " cookie=%#"PRIx64
, ntohll(fm
.new_cookie
));
5077 if (fm
.idle_timeout
!= OFP_FLOW_PERMANENT
) {
5078 ds_put_format(&s
, " idle=%"PRIu16
, fm
.idle_timeout
);
5080 if (fm
.hard_timeout
!= OFP_FLOW_PERMANENT
) {
5081 ds_put_format(&s
, " hard=%"PRIu16
, fm
.hard_timeout
);
5083 if (fm
.flags
& NX_LEARN_F_SEND_FLOW_REM
) {
5084 ds_put_cstr(&s
, " send_flow_rem");
5086 ds_put_cstr(&s
, " actions=");
5087 ofpacts_format(fm
.ofpacts
, fm
.ofpacts_len
, NULL
, &s
);
5088 xlate_report(ctx
, OFT_DETAIL
, "%s", ds_cstr(&s
));
5091 error
= ofproto_dpif_flow_mod_init_for_learn(ctx
->xbridge
->ofproto
,
5093 ofpbuf_uninit(&ofpacts
);
5096 bool success
= true;
5097 if (ctx
->xin
->allow_side_effects
) {
5098 error
= ofproto_flow_mod_learn(ofm
, ctx
->xin
->xcache
!= NULL
,
5099 learn
->limit
, &success
);
5100 } else if (learn
->limit
) {
5102 || ofm
->temp_rule
->state
!= RULE_INSERTED
) {
5103 /* The learned rule expired and there are no packets, so
5104 * we cannot learn again. Since the translated actions
5105 * depend on the result of learning, we tell the caller
5106 * that there's no point in caching this result. */
5107 ctx
->xout
->avoid_caching
= true;
5111 if (learn
->flags
& NX_LEARN_F_WRITE_RESULT
) {
5112 nxm_reg_load(&learn
->result_dst
, success
? 1 : 0,
5113 &ctx
->xin
->flow
, ctx
->wc
);
5114 xlate_report_subfield(ctx
, &learn
->result_dst
);
5117 if (success
&& ctx
->xin
->xcache
) {
5118 struct xc_entry
*entry
;
5120 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_LEARN
);
5121 entry
->learn
.ofm
= ofm
;
5122 entry
->learn
.limit
= learn
->limit
;
5126 if (OVS_UNLIKELY(ctx
->xin
->trace
&& !success
)) {
5127 xlate_report(ctx
, OFT_DETAIL
, "Limit exceeded, learn failed");
5131 if (ctx
->xin
->xcache
) {
5136 xlate_report_error(ctx
, "LEARN action execution failed (%s).",
5137 ofperr_to_string(error
));
5140 xlate_report(ctx
, OFT_WARN
,
5141 "suppressing side effects, so learn action ignored");
5146 xlate_fin_timeout__(struct rule_dpif
*rule
, uint16_t tcp_flags
,
5147 uint16_t idle_timeout
, uint16_t hard_timeout
)
5149 if (tcp_flags
& (TCP_FIN
| TCP_RST
)) {
5150 ofproto_rule_reduce_timeouts(&rule
->up
, idle_timeout
, hard_timeout
);
5155 xlate_fin_timeout(struct xlate_ctx
*ctx
,
5156 const struct ofpact_fin_timeout
*oft
)
5159 if (ctx
->xin
->allow_side_effects
) {
5160 xlate_fin_timeout__(ctx
->rule
, ctx
->xin
->tcp_flags
,
5161 oft
->fin_idle_timeout
, oft
->fin_hard_timeout
);
5163 if (ctx
->xin
->xcache
) {
5164 struct xc_entry
*entry
;
5166 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_FIN_TIMEOUT
);
5167 /* XC_RULE already holds a reference on the rule, none is taken
5169 entry
->fin
.rule
= ctx
->rule
;
5170 entry
->fin
.idle
= oft
->fin_idle_timeout
;
5171 entry
->fin
.hard
= oft
->fin_hard_timeout
;
5177 xlate_sample_action(struct xlate_ctx
*ctx
,
5178 const struct ofpact_sample
*os
)
5180 odp_port_t output_odp_port
= ODPP_NONE
;
5181 odp_port_t tunnel_out_port
= ODPP_NONE
;
5182 struct dpif_ipfix
*ipfix
= ctx
->xbridge
->ipfix
;
5183 bool emit_set_tunnel
= false;
5185 if (!ipfix
|| ctx
->xin
->flow
.in_port
.ofp_port
== OFPP_NONE
) {
5189 /* Scale the probability from 16-bit to 32-bit while representing
5190 * the same percentage. */
5191 uint32_t probability
= (os
->probability
<< 16) | os
->probability
;
5193 if (!ctx
->xbridge
->support
.variable_length_userdata
) {
5194 xlate_report_error(ctx
, "ignoring NXAST_SAMPLE action because "
5195 "datapath lacks support (needs Linux 3.10+ or "
5196 "kernel module from OVS 1.11+)");
5200 /* If ofp_port in flow sample action is equel to ofp_port,
5201 * this sample action is a input port action. */
5202 if (os
->sampling_port
!= OFPP_NONE
&&
5203 os
->sampling_port
!= ctx
->xin
->flow
.in_port
.ofp_port
) {
5204 output_odp_port
= ofp_port_to_odp_port(ctx
->xbridge
,
5206 if (output_odp_port
== ODPP_NONE
) {
5207 xlate_report_error(ctx
, "can't use unknown port %d in flow sample "
5208 "action", os
->sampling_port
);
5212 if (dpif_ipfix_get_flow_exporter_tunnel_sampling(ipfix
,
5213 os
->collector_set_id
)
5214 && dpif_ipfix_get_tunnel_port(ipfix
, output_odp_port
)) {
5215 tunnel_out_port
= output_odp_port
;
5216 emit_set_tunnel
= true;
5220 xlate_commit_actions(ctx
);
5221 /* If 'emit_set_tunnel', sample(sampling_port=1) would translate
5222 * into datapath sample action set(tunnel(...)), sample(...) and
5223 * it is used for sampling egress tunnel information. */
5224 if (emit_set_tunnel
) {
5225 const struct xport
*xport
= get_ofp_port(ctx
->xbridge
,
5228 if (xport
&& xport
->is_tunnel
) {
5229 struct flow
*flow
= &ctx
->xin
->flow
;
5230 tnl_port_send(xport
->ofport
, flow
, ctx
->wc
);
5231 if (!ovs_native_tunneling_is_on(ctx
->xbridge
->ofproto
)) {
5232 struct flow_tnl flow_tnl
= flow
->tunnel
;
5234 commit_odp_tunnel_action(flow
, &ctx
->base_flow
,
5236 flow
->tunnel
= flow_tnl
;
5239 xlate_report_error(ctx
,
5240 "sampling_port:%d should be a tunnel port.",
5245 union user_action_cookie cookie
= {
5247 .type
= USER_ACTION_COOKIE_FLOW_SAMPLE
,
5248 .probability
= os
->probability
,
5249 .collector_set_id
= os
->collector_set_id
,
5250 .obs_domain_id
= os
->obs_domain_id
,
5251 .obs_point_id
= os
->obs_point_id
,
5252 .output_odp_port
= output_odp_port
,
5253 .direction
= os
->direction
,
5256 compose_sample_action(ctx
, probability
, &cookie
, sizeof cookie
.flow_sample
,
5257 tunnel_out_port
, false);
5260 /* Determine if an datapath action translated from the openflow action
5261 * can be reversed by another datapath action.
5263 * Openflow actions that do not emit datapath actions are trivially
5264 * reversible. Reversiblity of other actions depends on nature of
5265 * action and their translation. */
5267 reversible_actions(const struct ofpact
*ofpacts
, size_t ofpacts_len
)
5269 const struct ofpact
*a
;
5271 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
5274 case OFPACT_CLEAR_ACTIONS
:
5276 case OFPACT_CONJUNCTION
:
5277 case OFPACT_CONTROLLER
:
5278 case OFPACT_CT_CLEAR
:
5279 case OFPACT_DEBUG_RECIRC
:
5280 case OFPACT_DEC_MPLS_TTL
:
5281 case OFPACT_DEC_TTL
:
5282 case OFPACT_ENQUEUE
:
5284 case OFPACT_FIN_TIMEOUT
:
5285 case OFPACT_GOTO_TABLE
:
5288 case OFPACT_MULTIPATH
:
5291 case OFPACT_OUTPUT_REG
:
5292 case OFPACT_POP_MPLS
:
5293 case OFPACT_POP_QUEUE
:
5294 case OFPACT_PUSH_MPLS
:
5295 case OFPACT_PUSH_VLAN
:
5296 case OFPACT_REG_MOVE
:
5297 case OFPACT_RESUBMIT
:
5299 case OFPACT_SET_ETH_DST
:
5300 case OFPACT_SET_ETH_SRC
:
5301 case OFPACT_SET_FIELD
:
5302 case OFPACT_SET_IP_DSCP
:
5303 case OFPACT_SET_IP_ECN
:
5304 case OFPACT_SET_IP_TTL
:
5305 case OFPACT_SET_IPV4_DST
:
5306 case OFPACT_SET_IPV4_SRC
:
5307 case OFPACT_SET_L4_DST_PORT
:
5308 case OFPACT_SET_L4_SRC_PORT
:
5309 case OFPACT_SET_MPLS_LABEL
:
5310 case OFPACT_SET_MPLS_TC
:
5311 case OFPACT_SET_MPLS_TTL
:
5312 case OFPACT_SET_QUEUE
:
5313 case OFPACT_SET_TUNNEL
:
5314 case OFPACT_SET_VLAN_PCP
:
5315 case OFPACT_SET_VLAN_VID
:
5316 case OFPACT_STACK_POP
:
5317 case OFPACT_STACK_PUSH
:
5318 case OFPACT_STRIP_VLAN
:
5319 case OFPACT_UNROLL_XLATE
:
5320 case OFPACT_WRITE_ACTIONS
:
5321 case OFPACT_WRITE_METADATA
:
5327 case OFPACT_OUTPUT_TRUNC
:
5337 compose_clone(struct xlate_ctx
*ctx
, const struct ofpact_nest
*oc
)
5339 struct ofpbuf old_stack
= ctx
->stack
;
5340 union mf_subvalue new_stack
[1024 / sizeof(union mf_subvalue
)];
5341 ofpbuf_use_stub(&ctx
->stack
, new_stack
, sizeof new_stack
);
5342 ofpbuf_put(&ctx
->stack
, old_stack
.data
, old_stack
.size
);
5344 struct ofpbuf old_action_set
= ctx
->action_set
;
5345 uint64_t actset_stub
[1024 / 8];
5346 ofpbuf_use_stub(&ctx
->action_set
, actset_stub
, sizeof actset_stub
);
5347 ofpbuf_put(&ctx
->action_set
, old_action_set
.data
, old_action_set
.size
);
5349 size_t offset
, ac_offset
;
5350 size_t oc_actions_len
= ofpact_nest_get_action_len(oc
);
5351 struct flow old_flow
= ctx
->xin
->flow
;
5353 if (reversible_actions(oc
->actions
, oc_actions_len
)) {
5354 old_flow
= ctx
->xin
->flow
;
5355 do_xlate_actions(oc
->actions
, oc_actions_len
, ctx
);
5359 /* Commit datapath actions before emitting the clone action to
5360 * avoid emitting those actions twice. Once inside
5361 * the clone, another time for the action after clone. */
5362 xlate_commit_actions(ctx
);
5363 struct flow old_base
= ctx
->base_flow
;
5364 bool old_was_mpls
= ctx
->was_mpls
;
5365 bool old_conntracked
= ctx
->conntracked
;
5367 /* The actions are not reversible, a datapath clone action is
5368 * required to encode the translation. Select the clone action
5369 * based on datapath capabilities. */
5370 if (ctx
->xbridge
->support
.clone
) { /* Use clone action */
5371 /* Use clone action as datapath clone. */
5372 offset
= nl_msg_start_nested(ctx
->odp_actions
, OVS_ACTION_ATTR_CLONE
);
5373 ac_offset
= ctx
->odp_actions
->size
;
5374 do_xlate_actions(oc
->actions
, oc_actions_len
, ctx
);
5375 nl_msg_end_non_empty_nested(ctx
->odp_actions
, offset
);
5379 if (ctx
->xbridge
->support
.sample_nesting
> 3) {
5380 /* Use sample action as datapath clone. */
5381 offset
= nl_msg_start_nested(ctx
->odp_actions
, OVS_ACTION_ATTR_SAMPLE
);
5382 ac_offset
= nl_msg_start_nested(ctx
->odp_actions
,
5383 OVS_SAMPLE_ATTR_ACTIONS
);
5384 do_xlate_actions(oc
->actions
, oc_actions_len
, ctx
);
5385 if (nl_msg_end_non_empty_nested(ctx
->odp_actions
, ac_offset
)) {
5386 nl_msg_cancel_nested(ctx
->odp_actions
, offset
);
5388 nl_msg_put_u32(ctx
->odp_actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
5389 UINT32_MAX
); /* 100% probability. */
5390 nl_msg_end_nested(ctx
->odp_actions
, offset
);
5395 /* Datapath does not support clone, skip xlate 'oc' and
5396 * report an error */
5397 xlate_report_error(ctx
, "Failed to compose clone action");
5400 /* The clone's conntrack execution should have no effect on the original
5402 ctx
->conntracked
= old_conntracked
;
5404 /* Popping MPLS from the clone should have no effect on the original
5406 ctx
->was_mpls
= old_was_mpls
;
5408 /* Restore the 'base_flow' for the next action. */
5409 ctx
->base_flow
= old_base
;
5412 ofpbuf_uninit(&ctx
->action_set
);
5413 ctx
->action_set
= old_action_set
;
5414 ofpbuf_uninit(&ctx
->stack
);
5415 ctx
->stack
= old_stack
;
5416 ctx
->xin
->flow
= old_flow
;
5420 xlate_meter_action(struct xlate_ctx
*ctx
, const struct ofpact_meter
*meter
)
5422 if (meter
->provider_meter_id
!= UINT32_MAX
) {
5423 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_METER
,
5424 meter
->provider_meter_id
);
5429 may_receive(const struct xport
*xport
, struct xlate_ctx
*ctx
)
5431 if (xport
->config
& (is_stp(&ctx
->xin
->flow
)
5432 ? OFPUTIL_PC_NO_RECV_STP
5433 : OFPUTIL_PC_NO_RECV
)) {
5437 /* Only drop packets here if both forwarding and learning are
5438 * disabled. If just learning is enabled, we need to have
5439 * OFPP_NORMAL and the learning action have a look at the packet
5440 * before we can drop it. */
5441 if ((!xport_stp_forward_state(xport
) && !xport_stp_learn_state(xport
)) ||
5442 (!xport_rstp_forward_state(xport
) && !xport_rstp_learn_state(xport
))) {
5450 xlate_write_actions__(struct xlate_ctx
*ctx
,
5451 const struct ofpact
*ofpacts
, size_t ofpacts_len
)
5453 /* Maintain actset_output depending on the contents of the action set:
5455 * - OFPP_UNSET, if there is no "output" action.
5457 * - The output port, if there is an "output" action and no "group"
5460 * - OFPP_UNSET, if there is a "group" action.
5462 if (!ctx
->action_set_has_group
) {
5463 const struct ofpact
*a
;
5464 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
5465 if (a
->type
== OFPACT_OUTPUT
) {
5466 ctx
->xin
->flow
.actset_output
= ofpact_get_OUTPUT(a
)->port
;
5467 } else if (a
->type
== OFPACT_GROUP
) {
5468 ctx
->xin
->flow
.actset_output
= OFPP_UNSET
;
5469 ctx
->action_set_has_group
= true;
5475 ofpbuf_put(&ctx
->action_set
, ofpacts
, ofpacts_len
);
5479 xlate_write_actions(struct xlate_ctx
*ctx
, const struct ofpact_nest
*a
)
5481 xlate_write_actions__(ctx
, a
->actions
, ofpact_nest_get_action_len(a
));
5485 xlate_action_set(struct xlate_ctx
*ctx
)
5487 uint64_t action_list_stub
[1024 / 8];
5488 struct ofpbuf action_list
= OFPBUF_STUB_INITIALIZER(action_list_stub
);
5489 ofpacts_execute_action_set(&action_list
, &ctx
->action_set
);
5490 /* Clear the action set, as it is not needed any more. */
5491 ofpbuf_clear(&ctx
->action_set
);
5492 if (action_list
.size
) {
5493 ctx
->in_action_set
= true;
5495 struct ovs_list
*old_trace
= ctx
->xin
->trace
;
5496 ctx
->xin
->trace
= xlate_report(ctx
, OFT_TABLE
,
5497 "--. Executing action set:");
5498 do_xlate_actions(action_list
.data
, action_list
.size
, ctx
);
5499 ctx
->xin
->trace
= old_trace
;
5501 ctx
->in_action_set
= false;
5503 ofpbuf_uninit(&action_list
);
5507 freeze_put_unroll_xlate(struct xlate_ctx
*ctx
)
5509 struct ofpact_unroll_xlate
*unroll
= ctx
->frozen_actions
.header
;
5511 /* Restore the table_id and rule cookie for a potential PACKET
5514 (ctx
->table_id
!= unroll
->rule_table_id
5515 || ctx
->rule_cookie
!= unroll
->rule_cookie
)) {
5516 unroll
= ofpact_put_UNROLL_XLATE(&ctx
->frozen_actions
);
5517 unroll
->rule_table_id
= ctx
->table_id
;
5518 unroll
->rule_cookie
= ctx
->rule_cookie
;
5519 ctx
->frozen_actions
.header
= unroll
;
5524 /* Copy actions 'a' through 'end' to ctx->frozen_actions, which will be
5525 * executed after thawing. Inserts an UNROLL_XLATE action, if none is already
5526 * present, before any action that may depend on the current table ID or flow
5529 freeze_unroll_actions(const struct ofpact
*a
, const struct ofpact
*end
,
5530 struct xlate_ctx
*ctx
)
5532 for (; a
< end
; a
= ofpact_next(a
)) {
5534 case OFPACT_OUTPUT_REG
:
5535 case OFPACT_OUTPUT_TRUNC
:
5538 case OFPACT_CONTROLLER
:
5539 case OFPACT_DEC_MPLS_TTL
:
5540 case OFPACT_DEC_TTL
:
5541 /* These actions may generate asynchronous messages, which include
5542 * table ID and flow cookie information. */
5543 freeze_put_unroll_xlate(ctx
);
5546 case OFPACT_RESUBMIT
:
5547 if (ofpact_get_RESUBMIT(a
)->table_id
== 0xff) {
5548 /* This resubmit action is relative to the current table, so we
5549 * need to track what table that is.*/
5550 freeze_put_unroll_xlate(ctx
);
5554 case OFPACT_SET_TUNNEL
:
5555 case OFPACT_REG_MOVE
:
5556 case OFPACT_SET_FIELD
:
5557 case OFPACT_STACK_PUSH
:
5558 case OFPACT_STACK_POP
:
5560 case OFPACT_WRITE_METADATA
:
5561 case OFPACT_GOTO_TABLE
:
5562 case OFPACT_ENQUEUE
:
5563 case OFPACT_SET_VLAN_VID
:
5564 case OFPACT_SET_VLAN_PCP
:
5565 case OFPACT_STRIP_VLAN
:
5566 case OFPACT_PUSH_VLAN
:
5567 case OFPACT_SET_ETH_SRC
:
5568 case OFPACT_SET_ETH_DST
:
5569 case OFPACT_SET_IPV4_SRC
:
5570 case OFPACT_SET_IPV4_DST
:
5571 case OFPACT_SET_IP_DSCP
:
5572 case OFPACT_SET_IP_ECN
:
5573 case OFPACT_SET_IP_TTL
:
5574 case OFPACT_SET_L4_SRC_PORT
:
5575 case OFPACT_SET_L4_DST_PORT
:
5576 case OFPACT_SET_QUEUE
:
5577 case OFPACT_POP_QUEUE
:
5578 case OFPACT_PUSH_MPLS
:
5579 case OFPACT_POP_MPLS
:
5580 case OFPACT_SET_MPLS_LABEL
:
5581 case OFPACT_SET_MPLS_TC
:
5582 case OFPACT_SET_MPLS_TTL
:
5583 case OFPACT_MULTIPATH
:
5586 case OFPACT_UNROLL_XLATE
:
5587 case OFPACT_FIN_TIMEOUT
:
5588 case OFPACT_CLEAR_ACTIONS
:
5589 case OFPACT_WRITE_ACTIONS
:
5595 case OFPACT_DEBUG_RECIRC
:
5597 case OFPACT_CT_CLEAR
:
5599 /* These may not generate PACKET INs. */
5603 case OFPACT_CONJUNCTION
:
5604 /* These need not be copied for restoration. */
5607 /* Copy the action over. */
5608 ofpbuf_put(&ctx
->frozen_actions
, a
, OFPACT_ALIGN(a
->len
));
5613 put_ct_mark(const struct flow
*flow
, struct ofpbuf
*odp_actions
,
5614 struct flow_wildcards
*wc
)
5616 if (wc
->masks
.ct_mark
) {
5622 odp_ct_mark
= nl_msg_put_unspec_uninit(odp_actions
, OVS_CT_ATTR_MARK
,
5623 sizeof(*odp_ct_mark
));
5624 odp_ct_mark
->key
= flow
->ct_mark
& wc
->masks
.ct_mark
;
5625 odp_ct_mark
->mask
= wc
->masks
.ct_mark
;
5630 put_ct_label(const struct flow
*flow
, struct ofpbuf
*odp_actions
,
5631 struct flow_wildcards
*wc
)
5633 if (!ovs_u128_is_zero(wc
->masks
.ct_label
)) {
5639 odp_ct_label
.key
= ovs_u128_and(flow
->ct_label
, wc
->masks
.ct_label
);
5640 odp_ct_label
.mask
= wc
->masks
.ct_label
;
5641 nl_msg_put_unspec(odp_actions
, OVS_CT_ATTR_LABELS
,
5642 &odp_ct_label
, sizeof odp_ct_label
);
5647 put_ct_helper(struct xlate_ctx
*ctx
,
5648 struct ofpbuf
*odp_actions
, struct ofpact_conntrack
*ofc
)
5653 nl_msg_put_string(odp_actions
, OVS_CT_ATTR_HELPER
, "ftp");
5656 nl_msg_put_string(odp_actions
, OVS_CT_ATTR_HELPER
, "tftp");
5659 xlate_report_error(ctx
, "cannot serialize ct_helper %d", ofc
->alg
);
5666 put_ct_nat(struct xlate_ctx
*ctx
)
5668 struct ofpact_nat
*ofn
= ctx
->ct_nat_action
;
5675 nat_offset
= nl_msg_start_nested(ctx
->odp_actions
, OVS_CT_ATTR_NAT
);
5676 if (ofn
->flags
& NX_NAT_F_SRC
|| ofn
->flags
& NX_NAT_F_DST
) {
5677 nl_msg_put_flag(ctx
->odp_actions
, ofn
->flags
& NX_NAT_F_SRC
5678 ? OVS_NAT_ATTR_SRC
: OVS_NAT_ATTR_DST
);
5679 if (ofn
->flags
& NX_NAT_F_PERSISTENT
) {
5680 nl_msg_put_flag(ctx
->odp_actions
, OVS_NAT_ATTR_PERSISTENT
);
5682 if (ofn
->flags
& NX_NAT_F_PROTO_HASH
) {
5683 nl_msg_put_flag(ctx
->odp_actions
, OVS_NAT_ATTR_PROTO_HASH
);
5684 } else if (ofn
->flags
& NX_NAT_F_PROTO_RANDOM
) {
5685 nl_msg_put_flag(ctx
->odp_actions
, OVS_NAT_ATTR_PROTO_RANDOM
);
5687 if (ofn
->range_af
== AF_INET
) {
5688 nl_msg_put_be32(ctx
->odp_actions
, OVS_NAT_ATTR_IP_MIN
,
5689 ofn
->range
.addr
.ipv4
.min
);
5690 if (ofn
->range
.addr
.ipv4
.max
&&
5691 (ntohl(ofn
->range
.addr
.ipv4
.max
)
5692 > ntohl(ofn
->range
.addr
.ipv4
.min
))) {
5693 nl_msg_put_be32(ctx
->odp_actions
, OVS_NAT_ATTR_IP_MAX
,
5694 ofn
->range
.addr
.ipv4
.max
);
5696 } else if (ofn
->range_af
== AF_INET6
) {
5697 nl_msg_put_unspec(ctx
->odp_actions
, OVS_NAT_ATTR_IP_MIN
,
5698 &ofn
->range
.addr
.ipv6
.min
,
5699 sizeof ofn
->range
.addr
.ipv6
.min
);
5700 if (!ipv6_mask_is_any(&ofn
->range
.addr
.ipv6
.max
) &&
5701 memcmp(&ofn
->range
.addr
.ipv6
.max
, &ofn
->range
.addr
.ipv6
.min
,
5702 sizeof ofn
->range
.addr
.ipv6
.max
) > 0) {
5703 nl_msg_put_unspec(ctx
->odp_actions
, OVS_NAT_ATTR_IP_MAX
,
5704 &ofn
->range
.addr
.ipv6
.max
,
5705 sizeof ofn
->range
.addr
.ipv6
.max
);
5708 if (ofn
->range_af
!= AF_UNSPEC
&& ofn
->range
.proto
.min
) {
5709 nl_msg_put_u16(ctx
->odp_actions
, OVS_NAT_ATTR_PROTO_MIN
,
5710 ofn
->range
.proto
.min
);
5711 if (ofn
->range
.proto
.max
&&
5712 ofn
->range
.proto
.max
> ofn
->range
.proto
.min
) {
5713 nl_msg_put_u16(ctx
->odp_actions
, OVS_NAT_ATTR_PROTO_MAX
,
5714 ofn
->range
.proto
.max
);
5718 nl_msg_end_nested(ctx
->odp_actions
, nat_offset
);
5722 compose_conntrack_action(struct xlate_ctx
*ctx
, struct ofpact_conntrack
*ofc
)
5724 ovs_u128 old_ct_label_mask
= ctx
->wc
->masks
.ct_label
;
5725 uint32_t old_ct_mark_mask
= ctx
->wc
->masks
.ct_mark
;
5729 /* Ensure that any prior actions are applied before composing the new
5730 * conntrack action. */
5731 xlate_commit_actions(ctx
);
5733 /* Process nested actions first, to populate the key. */
5734 ctx
->ct_nat_action
= NULL
;
5735 ctx
->wc
->masks
.ct_mark
= 0;
5736 ctx
->wc
->masks
.ct_label
= OVS_U128_ZERO
;
5737 do_xlate_actions(ofc
->actions
, ofpact_ct_get_action_len(ofc
), ctx
);
5739 if (ofc
->zone_src
.field
) {
5740 zone
= mf_get_subfield(&ofc
->zone_src
, &ctx
->xin
->flow
);
5742 zone
= ofc
->zone_imm
;
5745 ct_offset
= nl_msg_start_nested(ctx
->odp_actions
, OVS_ACTION_ATTR_CT
);
5746 if (ofc
->flags
& NX_CT_F_COMMIT
) {
5747 nl_msg_put_flag(ctx
->odp_actions
, ofc
->flags
& NX_CT_F_FORCE
?
5748 OVS_CT_ATTR_FORCE_COMMIT
: OVS_CT_ATTR_COMMIT
);
5749 if (ctx
->xbridge
->support
.ct_eventmask
) {
5750 nl_msg_put_u32(ctx
->odp_actions
, OVS_CT_ATTR_EVENTMASK
,
5751 OVS_CT_EVENTMASK_DEFAULT
);
5754 nl_msg_put_u16(ctx
->odp_actions
, OVS_CT_ATTR_ZONE
, zone
);
5755 put_ct_mark(&ctx
->xin
->flow
, ctx
->odp_actions
, ctx
->wc
);
5756 put_ct_label(&ctx
->xin
->flow
, ctx
->odp_actions
, ctx
->wc
);
5757 put_ct_helper(ctx
, ctx
->odp_actions
, ofc
);
5759 ctx
->ct_nat_action
= NULL
;
5760 nl_msg_end_nested(ctx
->odp_actions
, ct_offset
);
5762 ctx
->wc
->masks
.ct_mark
= old_ct_mark_mask
;
5763 ctx
->wc
->masks
.ct_label
= old_ct_label_mask
;
5765 if (ofc
->recirc_table
!= NX_CT_RECIRC_NONE
) {
5766 ctx
->conntracked
= true;
5767 compose_recirculate_and_fork(ctx
, ofc
->recirc_table
);
5770 /* The ct_* fields are only available in the scope of the 'recirc_table'
5772 flow_clear_conntrack(&ctx
->xin
->flow
);
5773 ctx
->conntracked
= false;
5777 rewrite_flow_encap_ethernet(struct xlate_ctx
*ctx
,
5779 struct flow_wildcards
*wc
)
5781 wc
->masks
.packet_type
= OVS_BE32_MAX
;
5782 if (pt_ns(flow
->packet_type
) == OFPHTN_ETHERTYPE
) {
5783 /* Only adjust the packet_type and zero the dummy Ethernet addresses. */
5784 ovs_be16 ethertype
= pt_ns_type_be(flow
->packet_type
);
5785 flow
->packet_type
= htonl(PT_ETH
);
5786 flow
->dl_src
= eth_addr_zero
;
5787 flow
->dl_dst
= eth_addr_zero
;
5788 flow
->dl_type
= ethertype
;
5790 /* Error handling: drop packet. */
5791 xlate_report_debug(ctx
, OFT_ACTION
,
5792 "Dropping packet as encap(ethernet) is not "
5793 "supported for packet type ethernet.");
5798 /* For an MD2 NSH header returns a pointer to an ofpbuf with the encoded
5799 * MD2 TLVs provided as encap properties to the encap operation. This
5800 * will be stored as encap_data in the ctx and copied into the encap_nsh
5801 * action at the next commit. */
5802 static struct ofpbuf
*
5803 rewrite_flow_encap_nsh(struct xlate_ctx
*ctx
,
5804 const struct ofpact_encap
*encap
,
5806 struct flow_wildcards
*wc
)
5808 ovs_be32 packet_type
= flow
->packet_type
;
5809 const char *ptr
= (char *) encap
->props
;
5810 struct ofpbuf
*buf
= ofpbuf_new(OVS_ENCAP_NSH_MAX_MD_LEN
);
5811 uint8_t md_type
= NSH_M_TYPE1
;
5815 /* Scan the optional NSH encap TLV properties, if any. */
5816 for (i
= 0; i
< encap
->n_props
; i
++) {
5817 struct ofpact_ed_prop
*prop_ptr
=
5818 ALIGNED_CAST(struct ofpact_ed_prop
*, ptr
);
5819 if (prop_ptr
->prop_class
== OFPPPC_NSH
) {
5820 switch (prop_ptr
->type
) {
5821 case OFPPPT_PROP_NSH_MDTYPE
: {
5822 struct ofpact_ed_prop_nsh_md_type
*prop_md_type
=
5823 ALIGNED_CAST(struct ofpact_ed_prop_nsh_md_type
*,
5825 md_type
= prop_md_type
->md_type
;
5828 case OFPPPT_PROP_NSH_TLV
: {
5829 struct ofpact_ed_prop_nsh_tlv
*tlv_prop
=
5830 ALIGNED_CAST(struct ofpact_ed_prop_nsh_tlv
*,
5832 struct nsh_md2_tlv
*md2_ctx
=
5833 ofpbuf_put_uninit(buf
, sizeof(*md2_ctx
));
5834 md2_ctx
->md_class
= tlv_prop
->tlv_class
;
5835 md2_ctx
->type
= tlv_prop
->tlv_type
;
5836 md2_ctx
->length
= tlv_prop
->tlv_len
;
5837 size_t len
= ROUND_UP(md2_ctx
->length
, 4);
5838 size_t padding
= len
- md2_ctx
->length
;
5839 ofpbuf_put(buf
, tlv_prop
->data
, md2_ctx
->length
);
5840 ofpbuf_put_zeros(buf
, padding
);
5844 /* No other NSH encap properties defined yet. */
5848 ptr
+= ROUND_UP(prop_ptr
->len
, 8);
5850 if (buf
->size
== 0 || buf
->size
> OVS_ENCAP_NSH_MAX_MD_LEN
) {
5855 /* Determine the Next Protocol field for NSH header. */
5856 switch (ntohl(packet_type
)) {
5858 np
= NSH_P_ETHERNET
;
5870 /* Error handling: drop packet. */
5871 xlate_report_debug(ctx
, OFT_ACTION
,
5872 "Dropping packet as encap(nsh) is not "
5873 "supported for packet type (%d,0x%x)",
5874 pt_ns(packet_type
), pt_ns_type(packet_type
));
5878 /* Note that we have matched on packet_type! */
5879 wc
->masks
.packet_type
= OVS_BE32_MAX
;
5881 /* Reset all current flow packet headers. */
5882 memset(&flow
->dl_dst
, 0,
5883 sizeof(struct flow
) - offsetof(struct flow
, dl_dst
));
5885 /* Populate the flow with the new NSH header. */
5886 flow
->packet_type
= htonl(PT_NSH
);
5887 flow
->dl_type
= htons(ETH_TYPE_NSH
);
5888 flow
->nsh
.flags
= 0; /* */
5893 if (md_type
== NSH_M_TYPE1
) {
5894 flow
->nsh
.mdtype
= NSH_M_TYPE1
;
5895 memset(flow
->nsh
.c
, 0, sizeof flow
->nsh
.c
);
5897 /* Drop any MD2 context TLVs. */
5901 } else if (md_type
== NSH_M_TYPE2
) {
5902 flow
->nsh
.mdtype
= NSH_M_TYPE2
;
5909 xlate_generic_encap_action(struct xlate_ctx
*ctx
,
5910 const struct ofpact_encap
*encap
)
5912 struct flow
*flow
= &ctx
->xin
->flow
;
5913 struct flow_wildcards
*wc
= ctx
->wc
;
5914 struct ofpbuf
*encap_data
= NULL
;
5916 /* Ensure that any pending actions on the inner packet are applied before
5917 * rewriting the flow */
5918 xlate_commit_actions(ctx
);
5920 /* Rewrite the flow to reflect the effect of pushing the new encap header. */
5921 switch (ntohl(encap
->new_pkt_type
)) {
5923 rewrite_flow_encap_ethernet(ctx
, flow
, wc
);
5926 encap_data
= rewrite_flow_encap_nsh(ctx
, encap
, flow
, wc
);
5929 /* New packet type was checked during decoding. */
5935 /* The actual encap datapath action will be generated at next commit. */
5936 ctx
->pending_encap
= true;
5937 ctx
->encap_data
= encap_data
;
5941 /* Returns true if packet must be recirculated after decapsulation. */
5943 xlate_generic_decap_action(struct xlate_ctx
*ctx
,
5944 const struct ofpact_decap
*decap OVS_UNUSED
)
5946 struct flow
*flow
= &ctx
->xin
->flow
;
5948 /* Ensure that any pending actions on the current packet are applied
5949 * before generating the decap action. */
5950 xlate_commit_actions(ctx
);
5952 /* We assume for now that the new_pkt_type is PT_USE_NEXT_PROTO. */
5953 switch (ntohl(flow
->packet_type
)) {
5955 if (flow
->vlans
[0].tci
& htons(VLAN_CFI
)) {
5956 /* Error handling: drop packet. */
5957 xlate_report_debug(ctx
, OFT_ACTION
, "Dropping packet, cannot "
5958 "decap Ethernet if VLAN is present.");
5961 /* Just change the packet_type.
5962 * Delay generating pop_eth to the next commit. */
5963 flow
->packet_type
= htonl(PACKET_TYPE(OFPHTN_ETHERTYPE
,
5964 ntohs(flow
->dl_type
)));
5965 ctx
->wc
->masks
.dl_type
= OVS_BE16_MAX
;
5969 /* The decap_nsh action is generated at the commit executed as
5970 * part of freezing the ctx for recirculation. Here we just set
5971 * the new packet type based on the NSH next protocol field. */
5972 switch (flow
->nsh
.np
) {
5973 case NSH_P_ETHERNET
:
5974 flow
->packet_type
= htonl(PT_ETH
);
5977 flow
->packet_type
= htonl(PT_IPV4
);
5980 flow
->packet_type
= htonl(PT_IPV6
);
5983 flow
->packet_type
= htonl(PT_NSH
);
5986 /* Error handling: drop packet. */
5987 xlate_report_debug(ctx
, OFT_ACTION
,
5988 "Dropping packet as NSH next protocol %d "
5989 "is not supported", flow
->nsh
.np
);
5994 ctx
->wc
->masks
.nsh
.np
= UINT8_MAX
;
5995 /* Trigger recirculation. */
5998 /* Error handling: drop packet. */
6001 "Dropping packet as the decap() does not support "
6002 "packet type (%d,0x%x)",
6003 pt_ns(flow
->packet_type
), pt_ns_type(flow
->packet_type
));
6010 recirc_for_mpls(const struct ofpact
*a
, struct xlate_ctx
*ctx
)
6012 /* No need to recirculate if already exiting. */
6017 /* Do not consider recirculating unless the packet was previously MPLS. */
6018 if (!ctx
->was_mpls
) {
6022 /* Special case these actions, only recirculating if necessary.
6023 * This avoids the overhead of recirculation in common use-cases.
6027 /* Output actions do not require recirculation. */
6029 case OFPACT_OUTPUT_TRUNC
:
6030 case OFPACT_ENQUEUE
:
6031 case OFPACT_OUTPUT_REG
:
6032 /* Set actions that don't touch L3+ fields do not require recirculation. */
6033 case OFPACT_SET_VLAN_VID
:
6034 case OFPACT_SET_VLAN_PCP
:
6035 case OFPACT_SET_ETH_SRC
:
6036 case OFPACT_SET_ETH_DST
:
6037 case OFPACT_SET_TUNNEL
:
6038 case OFPACT_SET_QUEUE
:
6039 /* If actions of a group require recirculation that can be detected
6040 * when translating them. */
6044 /* Set field that don't touch L3+ fields don't require recirculation. */
6045 case OFPACT_SET_FIELD
:
6046 if (mf_is_l3_or_higher(ofpact_get_SET_FIELD(a
)->field
)) {
6051 /* For simplicity, recirculate in all other cases. */
6052 case OFPACT_CONTROLLER
:
6054 case OFPACT_STRIP_VLAN
:
6055 case OFPACT_PUSH_VLAN
:
6056 case OFPACT_SET_IPV4_SRC
:
6057 case OFPACT_SET_IPV4_DST
:
6058 case OFPACT_SET_IP_DSCP
:
6059 case OFPACT_SET_IP_ECN
:
6060 case OFPACT_SET_IP_TTL
:
6061 case OFPACT_SET_L4_SRC_PORT
:
6062 case OFPACT_SET_L4_DST_PORT
:
6063 case OFPACT_REG_MOVE
:
6064 case OFPACT_STACK_PUSH
:
6065 case OFPACT_STACK_POP
:
6066 case OFPACT_DEC_TTL
:
6067 case OFPACT_SET_MPLS_LABEL
:
6068 case OFPACT_SET_MPLS_TC
:
6069 case OFPACT_SET_MPLS_TTL
:
6070 case OFPACT_DEC_MPLS_TTL
:
6071 case OFPACT_PUSH_MPLS
:
6072 case OFPACT_POP_MPLS
:
6073 case OFPACT_POP_QUEUE
:
6074 case OFPACT_FIN_TIMEOUT
:
6075 case OFPACT_RESUBMIT
:
6077 case OFPACT_CONJUNCTION
:
6078 case OFPACT_MULTIPATH
:
6085 case OFPACT_UNROLL_XLATE
:
6087 case OFPACT_CT_CLEAR
:
6089 case OFPACT_DEBUG_RECIRC
:
6091 case OFPACT_CLEAR_ACTIONS
:
6092 case OFPACT_WRITE_ACTIONS
:
6093 case OFPACT_WRITE_METADATA
:
6094 case OFPACT_GOTO_TABLE
:
6100 ctx_trigger_freeze(ctx
);
6104 xlate_ofpact_reg_move(struct xlate_ctx
*ctx
, const struct ofpact_reg_move
*a
)
6106 mf_subfield_copy(&a
->src
, &a
->dst
, &ctx
->xin
->flow
, ctx
->wc
);
6107 xlate_report_subfield(ctx
, &a
->dst
);
6111 xlate_ofpact_stack_pop(struct xlate_ctx
*ctx
, const struct ofpact_stack
*a
)
6113 if (nxm_execute_stack_pop(a
, &ctx
->xin
->flow
, ctx
->wc
, &ctx
->stack
)) {
6114 xlate_report_subfield(ctx
, &a
->subfield
);
6116 xlate_report_error(ctx
, "stack underflow");
6120 /* Restore translation context data that was stored earlier. */
6122 xlate_ofpact_unroll_xlate(struct xlate_ctx
*ctx
,
6123 const struct ofpact_unroll_xlate
*a
)
6125 ctx
->table_id
= a
->rule_table_id
;
6126 ctx
->rule_cookie
= a
->rule_cookie
;
6127 xlate_report(ctx
, OFT_THAW
, "restored state: table=%"PRIu8
", "
6128 "cookie=%#"PRIx64
, a
->rule_table_id
, a
->rule_cookie
);
6132 do_xlate_actions(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
6133 struct xlate_ctx
*ctx
)
6135 struct flow_wildcards
*wc
= ctx
->wc
;
6136 struct flow
*flow
= &ctx
->xin
->flow
;
6137 const struct ofpact
*a
;
6139 if (ovs_native_tunneling_is_on(ctx
->xbridge
->ofproto
)) {
6140 tnl_neigh_snoop(flow
, wc
, ctx
->xbridge
->name
);
6142 /* dl_type already in the mask, not set below. */
6145 xlate_report(ctx
, OFT_ACTION
, "drop");
6149 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
6150 struct ofpact_controller
*controller
;
6151 const struct ofpact_metadata
*metadata
;
6152 const struct ofpact_set_field
*set_field
;
6153 const struct mf_field
*mf
;
6159 recirc_for_mpls(a
, ctx
);
6162 /* Check if need to store the remaining actions for later
6164 if (ctx
->freezing
) {
6165 freeze_unroll_actions(a
, ofpact_end(ofpacts
, ofpacts_len
),
6171 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
6172 struct ds s
= DS_EMPTY_INITIALIZER
;
6173 ofpacts_format(a
, OFPACT_ALIGN(a
->len
), NULL
, &s
);
6174 xlate_report(ctx
, OFT_ACTION
, "%s", ds_cstr(&s
));
6180 xlate_output_action(ctx
, ofpact_get_OUTPUT(a
)->port
,
6181 ofpact_get_OUTPUT(a
)->max_len
, true);
6185 if (xlate_group_action(ctx
, ofpact_get_GROUP(a
)->group_id
)) {
6186 /* Group could not be found. */
6188 /* XXX: Terminates action list translation, but does not
6189 * terminate the pipeline. */
6194 case OFPACT_CONTROLLER
:
6195 controller
= ofpact_get_CONTROLLER(a
);
6196 if (controller
->pause
) {
6197 ctx
->pause
= controller
;
6198 ctx
->xout
->slow
|= SLOW_CONTROLLER
;
6199 *ctx
->paused_flow
= ctx
->xin
->flow
;
6200 ctx_trigger_freeze(ctx
);
6203 execute_controller_action(ctx
, controller
->max_len
,
6205 controller
->controller_id
,
6206 controller
->userdata
,
6207 controller
->userdata_len
);
6211 case OFPACT_ENQUEUE
:
6212 memset(&wc
->masks
.skb_priority
, 0xff,
6213 sizeof wc
->masks
.skb_priority
);
6214 xlate_enqueue_action(ctx
, ofpact_get_ENQUEUE(a
));
6217 case OFPACT_SET_VLAN_VID
:
6218 wc
->masks
.vlans
[0].tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
6219 if (flow
->vlans
[0].tci
& htons(VLAN_CFI
) ||
6220 ofpact_get_SET_VLAN_VID(a
)->push_vlan_if_needed
) {
6221 if (!flow
->vlans
[0].tpid
) {
6222 flow
->vlans
[0].tpid
= htons(ETH_TYPE_VLAN
);
6224 flow
->vlans
[0].tci
&= ~htons(VLAN_VID_MASK
);
6225 flow
->vlans
[0].tci
|=
6226 (htons(ofpact_get_SET_VLAN_VID(a
)->vlan_vid
) |
6231 case OFPACT_SET_VLAN_PCP
:
6232 wc
->masks
.vlans
[0].tci
|= htons(VLAN_PCP_MASK
| VLAN_CFI
);
6233 if (flow
->vlans
[0].tci
& htons(VLAN_CFI
) ||
6234 ofpact_get_SET_VLAN_PCP(a
)->push_vlan_if_needed
) {
6235 if (!flow
->vlans
[0].tpid
) {
6236 flow
->vlans
[0].tpid
= htons(ETH_TYPE_VLAN
);
6238 flow
->vlans
[0].tci
&= ~htons(VLAN_PCP_MASK
);
6239 flow
->vlans
[0].tci
|=
6240 htons((ofpact_get_SET_VLAN_PCP(a
)->vlan_pcp
6241 << VLAN_PCP_SHIFT
) | VLAN_CFI
);
6245 case OFPACT_STRIP_VLAN
:
6246 flow_pop_vlan(flow
, wc
);
6249 case OFPACT_PUSH_VLAN
:
6250 flow_push_vlan_uninit(flow
, wc
);
6251 flow
->vlans
[0].tpid
= ofpact_get_PUSH_VLAN(a
)->ethertype
;
6252 flow
->vlans
[0].tci
= htons(VLAN_CFI
);
6255 case OFPACT_SET_ETH_SRC
:
6256 WC_MASK_FIELD(wc
, dl_src
);
6257 flow
->dl_src
= ofpact_get_SET_ETH_SRC(a
)->mac
;
6260 case OFPACT_SET_ETH_DST
:
6261 WC_MASK_FIELD(wc
, dl_dst
);
6262 flow
->dl_dst
= ofpact_get_SET_ETH_DST(a
)->mac
;
6265 case OFPACT_SET_IPV4_SRC
:
6266 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6267 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
6268 flow
->nw_src
= ofpact_get_SET_IPV4_SRC(a
)->ipv4
;
6272 case OFPACT_SET_IPV4_DST
:
6273 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6274 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
6275 flow
->nw_dst
= ofpact_get_SET_IPV4_DST(a
)->ipv4
;
6279 case OFPACT_SET_IP_DSCP
:
6280 if (is_ip_any(flow
)) {
6281 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
6282 flow
->nw_tos
&= ~IP_DSCP_MASK
;
6283 flow
->nw_tos
|= ofpact_get_SET_IP_DSCP(a
)->dscp
;
6287 case OFPACT_SET_IP_ECN
:
6288 if (is_ip_any(flow
)) {
6289 wc
->masks
.nw_tos
|= IP_ECN_MASK
;
6290 flow
->nw_tos
&= ~IP_ECN_MASK
;
6291 flow
->nw_tos
|= ofpact_get_SET_IP_ECN(a
)->ecn
;
6295 case OFPACT_SET_IP_TTL
:
6296 if (is_ip_any(flow
)) {
6297 wc
->masks
.nw_ttl
= 0xff;
6298 flow
->nw_ttl
= ofpact_get_SET_IP_TTL(a
)->ttl
;
6302 case OFPACT_SET_L4_SRC_PORT
:
6303 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6304 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
6305 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
6306 flow
->tp_src
= htons(ofpact_get_SET_L4_SRC_PORT(a
)->port
);
6310 case OFPACT_SET_L4_DST_PORT
:
6311 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6312 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
6313 memset(&wc
->masks
.tp_dst
, 0xff, sizeof wc
->masks
.tp_dst
);
6314 flow
->tp_dst
= htons(ofpact_get_SET_L4_DST_PORT(a
)->port
);
6318 case OFPACT_RESUBMIT
:
6319 /* Freezing complicates resubmit. Some action in the flow
6320 * entry found by resubmit might trigger freezing. If that
6321 * happens, then we do not want to execute the resubmit again after
6322 * during thawing, so we want to skip back to the head of the loop
6323 * to avoid that, only adding any actions that follow the resubmit
6324 * to the frozen actions.
6326 xlate_ofpact_resubmit(ctx
, ofpact_get_RESUBMIT(a
));
6329 case OFPACT_SET_TUNNEL
:
6330 flow
->tunnel
.tun_id
= htonll(ofpact_get_SET_TUNNEL(a
)->tun_id
);
6333 case OFPACT_SET_QUEUE
:
6334 memset(&wc
->masks
.skb_priority
, 0xff,
6335 sizeof wc
->masks
.skb_priority
);
6336 xlate_set_queue_action(ctx
, ofpact_get_SET_QUEUE(a
)->queue_id
);
6339 case OFPACT_POP_QUEUE
:
6340 memset(&wc
->masks
.skb_priority
, 0xff,
6341 sizeof wc
->masks
.skb_priority
);
6342 if (flow
->skb_priority
!= ctx
->orig_skb_priority
) {
6343 flow
->skb_priority
= ctx
->orig_skb_priority
;
6344 xlate_report(ctx
, OFT_DETAIL
, "queue = %#"PRIx32
,
6345 flow
->skb_priority
);
6349 case OFPACT_REG_MOVE
:
6350 xlate_ofpact_reg_move(ctx
, ofpact_get_REG_MOVE(a
));
6353 case OFPACT_SET_FIELD
:
6354 set_field
= ofpact_get_SET_FIELD(a
);
6355 mf
= set_field
->field
;
6357 /* Set the field only if the packet actually has it. */
6358 if (mf_are_prereqs_ok(mf
, flow
, wc
)) {
6359 mf_mask_field_masked(mf
, ofpact_set_field_mask(set_field
), wc
);
6360 mf_set_flow_value_masked(mf
, set_field
->value
,
6361 ofpact_set_field_mask(set_field
),
6364 xlate_report(ctx
, OFT_WARN
,
6365 "unmet prerequisites for %s, set_field ignored",
6371 case OFPACT_STACK_PUSH
:
6372 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a
), flow
, wc
,
6376 case OFPACT_STACK_POP
:
6377 xlate_ofpact_stack_pop(ctx
, ofpact_get_STACK_POP(a
));
6380 case OFPACT_PUSH_MPLS
:
6381 compose_mpls_push_action(ctx
, ofpact_get_PUSH_MPLS(a
));
6384 case OFPACT_POP_MPLS
:
6385 compose_mpls_pop_action(ctx
, ofpact_get_POP_MPLS(a
)->ethertype
);
6388 case OFPACT_SET_MPLS_LABEL
:
6389 compose_set_mpls_label_action(
6390 ctx
, ofpact_get_SET_MPLS_LABEL(a
)->label
);
6393 case OFPACT_SET_MPLS_TC
:
6394 compose_set_mpls_tc_action(ctx
, ofpact_get_SET_MPLS_TC(a
)->tc
);
6397 case OFPACT_SET_MPLS_TTL
:
6398 compose_set_mpls_ttl_action(ctx
, ofpact_get_SET_MPLS_TTL(a
)->ttl
);
6401 case OFPACT_DEC_MPLS_TTL
:
6402 if (compose_dec_mpls_ttl_action(ctx
)) {
6407 case OFPACT_DEC_TTL
:
6408 wc
->masks
.nw_ttl
= 0xff;
6409 if (compose_dec_ttl(ctx
, ofpact_get_DEC_TTL(a
))) {
6415 /* Nothing to do. */
6418 case OFPACT_MULTIPATH
:
6419 multipath_execute(ofpact_get_MULTIPATH(a
), flow
, wc
);
6420 xlate_report_subfield(ctx
, &ofpact_get_MULTIPATH(a
)->dst
);
6424 xlate_bundle_action(ctx
, ofpact_get_BUNDLE(a
));
6427 case OFPACT_OUTPUT_REG
:
6428 xlate_output_reg_action(ctx
, ofpact_get_OUTPUT_REG(a
));
6431 case OFPACT_OUTPUT_TRUNC
:
6432 xlate_output_trunc_action(ctx
, ofpact_get_OUTPUT_TRUNC(a
)->port
,
6433 ofpact_get_OUTPUT_TRUNC(a
)->max_len
);
6437 xlate_learn_action(ctx
, ofpact_get_LEARN(a
));
6440 case OFPACT_CONJUNCTION
:
6441 /* A flow with a "conjunction" action represents part of a special
6442 * kind of "set membership match". Such a flow should not actually
6443 * get executed, but it could via, say, a "packet-out", even though
6444 * that wouldn't be useful. Log it to help debugging. */
6445 xlate_report_error(ctx
, "executing no-op conjunction action");
6452 case OFPACT_UNROLL_XLATE
:
6453 xlate_ofpact_unroll_xlate(ctx
, ofpact_get_UNROLL_XLATE(a
));
6456 case OFPACT_FIN_TIMEOUT
:
6457 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
6458 xlate_fin_timeout(ctx
, ofpact_get_FIN_TIMEOUT(a
));
6461 case OFPACT_CLEAR_ACTIONS
:
6462 xlate_report_action_set(ctx
, "was");
6463 ofpbuf_clear(&ctx
->action_set
);
6464 ctx
->xin
->flow
.actset_output
= OFPP_UNSET
;
6465 ctx
->action_set_has_group
= false;
6468 case OFPACT_WRITE_ACTIONS
:
6469 xlate_write_actions(ctx
, ofpact_get_WRITE_ACTIONS(a
));
6470 xlate_report_action_set(ctx
, "is");
6473 case OFPACT_WRITE_METADATA
:
6474 metadata
= ofpact_get_WRITE_METADATA(a
);
6475 flow
->metadata
&= ~metadata
->mask
;
6476 flow
->metadata
|= metadata
->metadata
& metadata
->mask
;
6480 xlate_meter_action(ctx
, ofpact_get_METER(a
));
6483 case OFPACT_GOTO_TABLE
: {
6484 struct ofpact_goto_table
*ogt
= ofpact_get_GOTO_TABLE(a
);
6486 ovs_assert(ctx
->table_id
< ogt
->table_id
);
6488 xlate_table_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
,
6489 ogt
->table_id
, true, true, false);
6494 xlate_sample_action(ctx
, ofpact_get_SAMPLE(a
));
6498 compose_clone(ctx
, ofpact_get_CLONE(a
));
6502 xlate_generic_encap_action(ctx
, ofpact_get_ENCAP(a
));
6505 case OFPACT_DECAP
: {
6506 bool recirc_needed
=
6507 xlate_generic_decap_action(ctx
, ofpact_get_DECAP(a
));
6508 if (!ctx
->error
&& recirc_needed
) {
6509 /* Recirculate for parsing of inner packet. */
6510 ctx_trigger_freeze(ctx
);
6511 /* Then continue with next action. */
6518 compose_conntrack_action(ctx
, ofpact_get_CT(a
));
6521 case OFPACT_CT_CLEAR
:
6522 clear_conntrack(ctx
);
6526 /* This will be processed by compose_conntrack_action(). */
6527 ctx
->ct_nat_action
= ofpact_get_NAT(a
);
6530 case OFPACT_DEBUG_RECIRC
:
6531 ctx_trigger_freeze(ctx
);
6536 /* Check if need to store this and the remaining actions for later
6538 if (!ctx
->error
&& ctx
->exit
&& ctx_first_frozen_action(ctx
)) {
6539 freeze_unroll_actions(a
, ofpact_end(ofpacts
, ofpacts_len
), ctx
);
6546 xlate_in_init(struct xlate_in
*xin
, struct ofproto_dpif
*ofproto
,
6547 ovs_version_t version
, const struct flow
*flow
,
6548 ofp_port_t in_port
, struct rule_dpif
*rule
, uint16_t tcp_flags
,
6549 const struct dp_packet
*packet
, struct flow_wildcards
*wc
,
6550 struct ofpbuf
*odp_actions
)
6552 xin
->ofproto
= ofproto
;
6553 xin
->tables_version
= version
;
6555 xin
->upcall_flow
= flow
;
6556 xin
->flow
.in_port
.ofp_port
= in_port
;
6557 xin
->flow
.actset_output
= OFPP_UNSET
;
6558 xin
->packet
= packet
;
6559 xin
->allow_side_effects
= packet
!= NULL
;
6562 xin
->ofpacts
= NULL
;
6563 xin
->ofpacts_len
= 0;
6564 xin
->tcp_flags
= tcp_flags
;
6566 xin
->resubmit_stats
= NULL
;
6570 xin
->odp_actions
= odp_actions
;
6571 xin
->in_packet_out
= false;
6572 xin
->recirc_queue
= NULL
;
6574 /* Do recirc lookup. */
6575 xin
->frozen_state
= NULL
;
6576 if (flow
->recirc_id
) {
6577 const struct recirc_id_node
*node
6578 = recirc_id_node_find(flow
->recirc_id
);
6580 xin
->frozen_state
= &node
->state
;
6586 xlate_out_uninit(struct xlate_out
*xout
)
6589 recirc_refs_unref(&xout
->recircs
);
6593 static struct skb_priority_to_dscp
*
6594 get_skb_priority(const struct xport
*xport
, uint32_t skb_priority
)
6596 struct skb_priority_to_dscp
*pdscp
;
6599 hash
= hash_int(skb_priority
, 0);
6600 HMAP_FOR_EACH_IN_BUCKET (pdscp
, hmap_node
, hash
, &xport
->skb_priorities
) {
6601 if (pdscp
->skb_priority
== skb_priority
) {
6609 dscp_from_skb_priority(const struct xport
*xport
, uint32_t skb_priority
,
6612 struct skb_priority_to_dscp
*pdscp
= get_skb_priority(xport
, skb_priority
);
6613 *dscp
= pdscp
? pdscp
->dscp
: 0;
6614 return pdscp
!= NULL
;
6618 count_skb_priorities(const struct xport
*xport
)
6620 return hmap_count(&xport
->skb_priorities
);
6624 clear_skb_priorities(struct xport
*xport
)
6626 struct skb_priority_to_dscp
*pdscp
;
6628 HMAP_FOR_EACH_POP (pdscp
, hmap_node
, &xport
->skb_priorities
) {
6634 actions_output_to_local_port(const struct xlate_ctx
*ctx
)
6636 odp_port_t local_odp_port
= ofp_port_to_odp_port(ctx
->xbridge
, OFPP_LOCAL
);
6637 const struct nlattr
*a
;
6640 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, ctx
->odp_actions
->data
,
6641 ctx
->odp_actions
->size
) {
6642 if (nl_attr_type(a
) == OVS_ACTION_ATTR_OUTPUT
6643 && nl_attr_get_odp_port(a
) == local_odp_port
) {
6650 #if defined(__linux__)
6651 /* Returns the maximum number of packets that the Linux kernel is willing to
6652 * queue up internally to certain kinds of software-implemented ports, or the
6653 * default (and rarely modified) value if it cannot be determined. */
6655 netdev_max_backlog(void)
6657 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
6658 static int max_backlog
= 1000; /* The normal default value. */
6660 if (ovsthread_once_start(&once
)) {
6661 static const char filename
[] = "/proc/sys/net/core/netdev_max_backlog";
6665 stream
= fopen(filename
, "r");
6667 VLOG_INFO("%s: open failed (%s)", filename
, ovs_strerror(errno
));
6669 if (fscanf(stream
, "%d", &n
) != 1) {
6670 VLOG_WARN("%s: read error", filename
);
6671 } else if (n
<= 100) {
6672 VLOG_WARN("%s: unexpectedly small value %d", filename
, n
);
6678 ovsthread_once_done(&once
);
6680 VLOG_DBG("%s: using %d max_backlog", filename
, max_backlog
);
6686 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
6689 count_output_actions(const struct ofpbuf
*odp_actions
)
6691 const struct nlattr
*a
;
6695 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, odp_actions
->data
, odp_actions
->size
) {
6696 if (a
->nla_type
== OVS_ACTION_ATTR_OUTPUT
) {
6702 #endif /* defined(__linux__) */
6704 /* Returns true if 'odp_actions' contains more output actions than the datapath
6705 * can reliably handle in one go. On Linux, this is the value of the
6706 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
6707 * packets that the kernel is willing to queue up for processing while the
6708 * datapath is processing a set of actions. */
6710 too_many_output_actions(const struct ofpbuf
*odp_actions OVS_UNUSED
)
6713 return (odp_actions
->size
/ NL_A_U32_SIZE
> netdev_max_backlog()
6714 && count_output_actions(odp_actions
) > netdev_max_backlog());
6716 /* OSes other than Linux might have similar limits, but we don't know how
6717 * to determine them.*/
6723 xlate_wc_init(struct xlate_ctx
*ctx
)
6725 flow_wildcards_init_catchall(ctx
->wc
);
6727 /* Some fields we consider to always be examined. */
6728 WC_MASK_FIELD(ctx
->wc
, packet_type
);
6729 WC_MASK_FIELD(ctx
->wc
, in_port
);
6730 if (is_ethernet(&ctx
->xin
->flow
, NULL
)) {
6731 WC_MASK_FIELD(ctx
->wc
, dl_type
);
6733 if (is_ip_any(&ctx
->xin
->flow
)) {
6734 WC_MASK_FIELD_MASK(ctx
->wc
, nw_frag
, FLOW_NW_FRAG_MASK
);
6737 if (ctx
->xbridge
->support
.odp
.recirc
) {
6738 /* Always exactly match recirc_id when datapath supports
6740 WC_MASK_FIELD(ctx
->wc
, recirc_id
);
6743 if (ctx
->xbridge
->netflow
) {
6744 netflow_mask_wc(&ctx
->xin
->flow
, ctx
->wc
);
6747 tnl_wc_init(&ctx
->xin
->flow
, ctx
->wc
);
6751 xlate_wc_finish(struct xlate_ctx
*ctx
)
6755 /* Clear the metadata and register wildcard masks, because we won't
6756 * use non-header fields as part of the cache. */
6757 flow_wildcards_clear_non_packet_fields(ctx
->wc
);
6759 /* Wildcard ethernet fields if the original packet type was not
6761 if (ctx
->xin
->upcall_flow
->packet_type
!= htonl(PT_ETH
)) {
6762 ctx
->wc
->masks
.dl_dst
= eth_addr_zero
;
6763 ctx
->wc
->masks
.dl_src
= eth_addr_zero
;
6764 ctx
->wc
->masks
.dl_type
= 0;
6767 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
6768 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
6769 * represent these fields. The datapath interface, on the other hand,
6770 * represents them with just 8 bits each. This means that if the high
6771 * 8 bits of the masks for these fields somehow become set, then they
6772 * will get chopped off by a round trip through the datapath, and
6773 * revalidation will spot that as an inconsistency and delete the flow.
6774 * Avoid the problem here by making sure that only the low 8 bits of
6775 * either field can be unwildcarded for ICMP.
6777 if (is_icmpv4(&ctx
->xin
->flow
, NULL
) || is_icmpv6(&ctx
->xin
->flow
, NULL
)) {
6778 ctx
->wc
->masks
.tp_src
&= htons(UINT8_MAX
);
6779 ctx
->wc
->masks
.tp_dst
&= htons(UINT8_MAX
);
6781 /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
6782 for (i
= 0; i
< FLOW_MAX_VLAN_HEADERS
; i
++) {
6783 if (ctx
->wc
->masks
.vlans
[i
].tci
) {
6784 ctx
->wc
->masks
.vlans
[i
].tci
|= htons(VLAN_CFI
);
6788 /* The classifier might return masks that match on tp_src and tp_dst even
6789 * for later fragments. This happens because there might be flows that
6790 * match on tp_src or tp_dst without matching on the frag bits, because
6791 * it is not a prerequisite for OpenFlow. Since it is a prerequisite for
6792 * datapath flows and since tp_src and tp_dst are always going to be 0,
6793 * wildcard the fields here. */
6794 if (ctx
->xin
->flow
.nw_frag
& FLOW_NW_FRAG_LATER
) {
6795 ctx
->wc
->masks
.tp_src
= 0;
6796 ctx
->wc
->masks
.tp_dst
= 0;
6800 /* Translates the flow, actions, or rule in 'xin' into datapath actions in
6802 * The caller must take responsibility for eventually freeing 'xout', with
6803 * xlate_out_uninit().
6804 * Returns 'XLATE_OK' if translation was successful. In case of an error an
6805 * empty set of actions will be returned in 'xin->odp_actions' (if non-NULL),
6806 * so that most callers may ignore the return value and transparently install a
6807 * drop flow when the translation fails. */
6809 xlate_actions(struct xlate_in
*xin
, struct xlate_out
*xout
)
6811 *xout
= (struct xlate_out
) {
6813 .recircs
= RECIRC_REFS_EMPTY_INITIALIZER
,
6816 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
6817 struct xbridge
*xbridge
= xbridge_lookup(xcfg
, xin
->ofproto
);
6819 return XLATE_BRIDGE_NOT_FOUND
;
6822 struct flow
*flow
= &xin
->flow
;
6824 uint8_t stack_stub
[1024];
6825 uint64_t action_set_stub
[1024 / 8];
6826 uint64_t frozen_actions_stub
[1024 / 8];
6827 uint64_t actions_stub
[256 / 8];
6828 struct ofpbuf scratch_actions
= OFPBUF_STUB_INITIALIZER(actions_stub
);
6829 struct flow paused_flow
;
6830 struct xlate_ctx ctx
= {
6834 .orig_tunnel_ipv6_dst
= flow_tnl_dst(&flow
->tunnel
),
6836 .stack
= OFPBUF_STUB_INITIALIZER(stack_stub
),
6840 : &(struct flow_wildcards
) { .masks
= { .dl_type
= 0 } }),
6841 .odp_actions
= xin
->odp_actions
? xin
->odp_actions
: &scratch_actions
,
6843 .depth
= xin
->depth
,
6844 .resubmits
= xin
->resubmits
,
6846 .in_action_set
= false,
6847 .in_packet_out
= xin
->in_packet_out
,
6848 .pending_encap
= false,
6852 .rule_cookie
= OVS_BE64_MAX
,
6853 .orig_skb_priority
= flow
->skb_priority
,
6854 .sflow_n_outputs
= 0,
6855 .sflow_odp_port
= 0,
6856 .nf_output_iface
= NF_OUT_DROP
,
6862 .recirc_update_dp_hash
= false,
6863 .frozen_actions
= OFPBUF_STUB_INITIALIZER(frozen_actions_stub
),
6865 .paused_flow
= &paused_flow
,
6868 .conntracked
= false,
6870 .ct_nat_action
= NULL
,
6872 .action_set_has_group
= false,
6873 .action_set
= OFPBUF_STUB_INITIALIZER(action_set_stub
),
6876 /* 'base_flow' reflects the packet as it came in, but we need it to reflect
6877 * the packet as the datapath will treat it for output actions. Our
6878 * datapath doesn't retain tunneling information without us re-setting
6879 * it, so clear the tunnel data.
6882 memset(&ctx
.base_flow
.tunnel
, 0, sizeof ctx
.base_flow
.tunnel
);
6884 ofpbuf_reserve(ctx
.odp_actions
, NL_A_U32_SIZE
);
6885 xlate_wc_init(&ctx
);
6887 COVERAGE_INC(xlate_actions
);
6889 xin
->trace
= xlate_report(&ctx
, OFT_BRIDGE
, "bridge(\"%s\")",
6891 if (xin
->frozen_state
) {
6892 const struct frozen_state
*state
= xin
->frozen_state
;
6894 struct ovs_list
*old_trace
= xin
->trace
;
6895 xin
->trace
= xlate_report(&ctx
, OFT_THAW
, "thaw");
6897 if (xin
->ofpacts_len
> 0 || ctx
.rule
) {
6898 xlate_report_error(&ctx
, "Recirculation conflict (%s)!",
6899 xin
->ofpacts_len
? "actions" : "rule");
6900 ctx
.error
= XLATE_RECIRCULATION_CONFLICT
;
6904 /* Set the bridge for post-recirculation processing if needed. */
6905 if (!uuid_equals(&ctx
.xbridge
->ofproto
->uuid
, &state
->ofproto_uuid
)) {
6906 const struct xbridge
*new_bridge
6907 = xbridge_lookup_by_uuid(xcfg
, &state
->ofproto_uuid
);
6909 if (OVS_UNLIKELY(!new_bridge
)) {
6910 /* Drop the packet if the bridge cannot be found. */
6911 xlate_report_error(&ctx
, "Frozen bridge no longer exists.");
6912 ctx
.error
= XLATE_BRIDGE_NOT_FOUND
;
6913 xin
->trace
= old_trace
;
6916 ctx
.xbridge
= new_bridge
;
6917 /* The bridge is now known so obtain its table version. */
6918 ctx
.xin
->tables_version
6919 = ofproto_dpif_get_tables_version(ctx
.xbridge
->ofproto
);
6922 /* Set the thawed table id. Note: A table lookup is done only if there
6923 * are no frozen actions. */
6924 ctx
.table_id
= state
->table_id
;
6925 xlate_report(&ctx
, OFT_THAW
,
6926 "Resuming from table %"PRIu8
, ctx
.table_id
);
6928 ctx
.conntracked
= state
->conntracked
;
6929 if (!state
->conntracked
) {
6930 clear_conntrack(&ctx
);
6933 /* Restore pipeline metadata. May change flow's in_port and other
6934 * metadata to the values that existed when freezing was triggered. */
6935 frozen_metadata_to_flow(&state
->metadata
, flow
);
6937 /* Restore stack, if any. */
6939 ofpbuf_put(&ctx
.stack
, state
->stack
, state
->stack_size
);
6942 /* Restore mirror state. */
6943 ctx
.mirrors
= state
->mirrors
;
6945 /* Restore action set, if any. */
6946 if (state
->action_set_len
) {
6947 xlate_report_actions(&ctx
, OFT_THAW
, "Restoring action set",
6948 state
->action_set
, state
->action_set_len
);
6950 flow
->actset_output
= OFPP_UNSET
;
6951 xlate_write_actions__(&ctx
, state
->action_set
,
6952 state
->action_set_len
);
6955 /* Restore frozen actions. If there are no actions, processing will
6956 * start with a lookup in the table set above. */
6957 xin
->ofpacts
= state
->ofpacts
;
6958 xin
->ofpacts_len
= state
->ofpacts_len
;
6959 if (state
->ofpacts_len
) {
6960 xlate_report_actions(&ctx
, OFT_THAW
, "Restoring actions",
6961 xin
->ofpacts
, xin
->ofpacts_len
);
6964 xin
->trace
= old_trace
;
6965 } else if (OVS_UNLIKELY(flow
->recirc_id
)) {
6966 xlate_report_error(&ctx
,
6967 "Recirculation context not found for ID %"PRIx32
,
6969 ctx
.error
= XLATE_NO_RECIRCULATION_CONTEXT
;
6973 /* Tunnel metadata in udpif format must be normalized before translation. */
6974 if (flow
->tunnel
.flags
& FLOW_TNL_F_UDPIF
) {
6975 const struct tun_table
*tun_tab
= ofproto_get_tun_tab(
6976 &ctx
.xbridge
->ofproto
->up
);
6979 err
= tun_metadata_from_geneve_udpif(tun_tab
, &xin
->upcall_flow
->tunnel
,
6980 &xin
->upcall_flow
->tunnel
,
6983 xlate_report_error(&ctx
, "Invalid Geneve tunnel metadata");
6984 ctx
.error
= XLATE_INVALID_TUNNEL_METADATA
;
6987 } else if (!flow
->tunnel
.metadata
.tab
) {
6988 /* If the original flow did not come in on a tunnel, then it won't have
6989 * FLOW_TNL_F_UDPIF set. However, we still need to have a metadata
6990 * table in case we generate tunnel actions. */
6991 flow
->tunnel
.metadata
.tab
= ofproto_get_tun_tab(
6992 &ctx
.xbridge
->ofproto
->up
);
6994 ctx
.wc
->masks
.tunnel
.metadata
.tab
= flow
->tunnel
.metadata
.tab
;
6996 /* Get the proximate input port of the packet. (If xin->frozen_state,
6997 * flow->in_port is the ultimate input port of the packet.) */
6998 struct xport
*in_port
= get_ofp_port(xbridge
,
6999 ctx
.base_flow
.in_port
.ofp_port
);
7001 if (flow
->packet_type
!= htonl(PT_ETH
) && in_port
&&
7002 in_port
->pt_mode
== NETDEV_PT_LEGACY_L3
&& ctx
.table_id
== 0) {
7003 /* Add dummy Ethernet header to non-L2 packet if it's coming from a
7004 * L3 port. So all packets will be L2 packets for lookup.
7005 * The dl_type has already been set from the packet_type. */
7006 flow
->packet_type
= htonl(PT_ETH
);
7007 flow
->dl_src
= eth_addr_zero
;
7008 flow
->dl_dst
= eth_addr_zero
;
7009 ctx
.pending_encap
= true;
7012 if (!xin
->ofpacts
&& !ctx
.rule
) {
7013 ctx
.rule
= rule_dpif_lookup_from_table(
7014 ctx
.xbridge
->ofproto
, ctx
.xin
->tables_version
, flow
, ctx
.wc
,
7015 ctx
.xin
->resubmit_stats
, &ctx
.table_id
,
7016 flow
->in_port
.ofp_port
, true, true, ctx
.xin
->xcache
);
7017 if (ctx
.xin
->resubmit_stats
) {
7018 rule_dpif_credit_stats(ctx
.rule
, ctx
.xin
->resubmit_stats
);
7020 if (ctx
.xin
->xcache
) {
7021 struct xc_entry
*entry
;
7023 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_RULE
);
7024 entry
->rule
= ctx
.rule
;
7025 ofproto_rule_ref(&ctx
.rule
->up
);
7028 xlate_report_table(&ctx
, ctx
.rule
, ctx
.table_id
);
7031 /* Tunnel stats only for not-thawed packets. */
7032 if (!xin
->frozen_state
&& in_port
&& in_port
->is_tunnel
) {
7033 if (ctx
.xin
->resubmit_stats
) {
7034 netdev_vport_inc_rx(in_port
->netdev
, ctx
.xin
->resubmit_stats
);
7036 bfd_account_rx(in_port
->bfd
, ctx
.xin
->resubmit_stats
);
7039 if (ctx
.xin
->xcache
) {
7040 struct xc_entry
*entry
;
7042 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_NETDEV
);
7043 entry
->dev
.rx
= netdev_ref(in_port
->netdev
);
7044 entry
->dev
.bfd
= bfd_ref(in_port
->bfd
);
7048 if (!xin
->frozen_state
&& process_special(&ctx
, in_port
)) {
7049 /* process_special() did all the processing for this packet.
7051 * We do not perform special processing on thawed packets, since that
7052 * was done before they were frozen and should not be redone. */
7053 } else if (in_port
&& in_port
->xbundle
7054 && xbundle_mirror_out(xbridge
, in_port
->xbundle
)) {
7055 xlate_report_error(&ctx
, "dropping packet received on port "
7056 "%s, which is reserved exclusively for mirroring",
7057 in_port
->xbundle
->name
);
7059 /* Sampling is done on initial reception; don't redo after thawing. */
7060 unsigned int user_cookie_offset
= 0;
7061 if (!xin
->frozen_state
) {
7062 user_cookie_offset
= compose_sflow_action(&ctx
);
7063 compose_ipfix_action(&ctx
, ODPP_NONE
);
7065 size_t sample_actions_len
= ctx
.odp_actions
->size
;
7067 if (tnl_process_ecn(flow
)
7068 && (!in_port
|| may_receive(in_port
, &ctx
))) {
7069 const struct ofpact
*ofpacts
;
7073 ofpacts
= xin
->ofpacts
;
7074 ofpacts_len
= xin
->ofpacts_len
;
7075 } else if (ctx
.rule
) {
7076 const struct rule_actions
*actions
7077 = rule_get_actions(&ctx
.rule
->up
);
7078 ofpacts
= actions
->ofpacts
;
7079 ofpacts_len
= actions
->ofpacts_len
;
7080 ctx
.rule_cookie
= ctx
.rule
->up
.flow_cookie
;
7085 mirror_ingress_packet(&ctx
);
7086 do_xlate_actions(ofpacts
, ofpacts_len
, &ctx
);
7091 /* We've let OFPP_NORMAL and the learning action look at the
7092 * packet, so cancel all actions and freezing if forwarding is
7094 if (in_port
&& (!xport_stp_forward_state(in_port
) ||
7095 !xport_rstp_forward_state(in_port
))) {
7096 ctx
.odp_actions
->size
= sample_actions_len
;
7097 ctx_cancel_freeze(&ctx
);
7098 ofpbuf_clear(&ctx
.action_set
);
7101 if (!ctx
.freezing
) {
7102 xlate_action_set(&ctx
);
7105 finish_freezing(&ctx
);
7109 /* Output only fully processed packets. */
7111 && xbridge
->has_in_band
7112 && in_band_must_output_to_local_port(flow
)
7113 && !actions_output_to_local_port(&ctx
)) {
7114 compose_output_action(&ctx
, OFPP_LOCAL
, NULL
);
7117 if (user_cookie_offset
) {
7118 fix_sflow_action(&ctx
, user_cookie_offset
);
7122 if (nl_attr_oversized(ctx
.odp_actions
->size
)) {
7123 /* These datapath actions are too big for a Netlink attribute, so we
7124 * can't hand them to the kernel directly. dpif_execute() can execute
7125 * them one by one with help, so just mark the result as SLOW_ACTION to
7126 * prevent the flow from being installed. */
7127 COVERAGE_INC(xlate_actions_oversize
);
7128 ctx
.xout
->slow
|= SLOW_ACTION
;
7129 } else if (too_many_output_actions(ctx
.odp_actions
)) {
7130 COVERAGE_INC(xlate_actions_too_many_output
);
7131 ctx
.xout
->slow
|= SLOW_ACTION
;
7134 /* Update NetFlow for non-frozen traffic. */
7135 if (xbridge
->netflow
&& !xin
->frozen_state
) {
7136 if (ctx
.xin
->resubmit_stats
) {
7137 netflow_flow_update(xbridge
->netflow
, flow
,
7138 ctx
.nf_output_iface
,
7139 ctx
.xin
->resubmit_stats
);
7141 if (ctx
.xin
->xcache
) {
7142 struct xc_entry
*entry
;
7144 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_NETFLOW
);
7145 entry
->nf
.netflow
= netflow_ref(xbridge
->netflow
);
7146 entry
->nf
.flow
= xmemdup(flow
, sizeof *flow
);
7147 entry
->nf
.iface
= ctx
.nf_output_iface
;
7151 /* Translate tunnel metadata masks to udpif format if necessary. */
7152 if (xin
->upcall_flow
->tunnel
.flags
& FLOW_TNL_F_UDPIF
) {
7153 if (ctx
.wc
->masks
.tunnel
.metadata
.present
.map
) {
7154 const struct flow_tnl
*upcall_tnl
= &xin
->upcall_flow
->tunnel
;
7155 struct geneve_opt opts
[TLV_TOT_OPT_SIZE
/
7156 sizeof(struct geneve_opt
)];
7158 tun_metadata_to_geneve_udpif_mask(&flow
->tunnel
,
7159 &ctx
.wc
->masks
.tunnel
,
7160 upcall_tnl
->metadata
.opts
.gnv
,
7161 upcall_tnl
->metadata
.present
.len
,
7163 memset(&ctx
.wc
->masks
.tunnel
.metadata
, 0,
7164 sizeof ctx
.wc
->masks
.tunnel
.metadata
);
7165 memcpy(&ctx
.wc
->masks
.tunnel
.metadata
.opts
.gnv
, opts
,
7166 upcall_tnl
->metadata
.present
.len
);
7168 ctx
.wc
->masks
.tunnel
.metadata
.present
.len
= 0xff;
7169 ctx
.wc
->masks
.tunnel
.metadata
.tab
= NULL
;
7170 ctx
.wc
->masks
.tunnel
.flags
|= FLOW_TNL_F_UDPIF
;
7171 } else if (!xin
->upcall_flow
->tunnel
.metadata
.tab
) {
7172 /* If we didn't have options in UDPIF format and didn't have an existing
7173 * metadata table, then it means that there were no options at all when
7174 * we started processing and any wildcards we picked up were from
7175 * action generation. Without options on the incoming packet, wildcards
7176 * aren't meaningful. To avoid them possibly getting misinterpreted,
7177 * just clear everything. */
7178 if (ctx
.wc
->masks
.tunnel
.metadata
.present
.map
) {
7179 memset(&ctx
.wc
->masks
.tunnel
.metadata
, 0,
7180 sizeof ctx
.wc
->masks
.tunnel
.metadata
);
7182 ctx
.wc
->masks
.tunnel
.metadata
.tab
= NULL
;
7186 xlate_wc_finish(&ctx
);
7189 /* Reset the table to what it was when we came in. If we only fetched
7190 * it locally, then it has no meaning outside of flow translation. */
7191 flow
->tunnel
.metadata
.tab
= xin
->upcall_flow
->tunnel
.metadata
.tab
;
7193 ofpbuf_uninit(&ctx
.stack
);
7194 ofpbuf_uninit(&ctx
.action_set
);
7195 ofpbuf_uninit(&ctx
.frozen_actions
);
7196 ofpbuf_uninit(&scratch_actions
);
7197 ofpbuf_delete(ctx
.encap_data
);
7199 /* Make sure we return a "drop flow" in case of an error. */
7202 if (xin
->odp_actions
) {
7203 ofpbuf_clear(xin
->odp_actions
);
7210 xlate_resume(struct ofproto_dpif
*ofproto
,
7211 const struct ofputil_packet_in_private
*pin
,
7212 struct ofpbuf
*odp_actions
,
7213 enum slow_path_reason
*slow
)
7215 struct dp_packet packet
;
7216 dp_packet_use_const(&packet
, pin
->base
.packet
,
7217 pin
->base
.packet_len
);
7220 flow_extract(&packet
, &flow
);
7222 struct xlate_in xin
;
7223 xlate_in_init(&xin
, ofproto
, ofproto_dpif_get_tables_version(ofproto
),
7224 &flow
, 0, NULL
, ntohs(flow
.tcp_flags
),
7225 &packet
, NULL
, odp_actions
);
7227 struct ofpact_note noop
;
7228 ofpact_init_NOTE(&noop
);
7231 bool any_actions
= pin
->actions_len
> 0;
7232 struct frozen_state state
= {
7233 .table_id
= 0, /* Not the table where NXAST_PAUSE was executed. */
7234 .ofproto_uuid
= pin
->bridge
,
7235 .stack
= pin
->stack
,
7236 .stack_size
= pin
->stack_size
,
7237 .mirrors
= pin
->mirrors
,
7238 .conntracked
= pin
->conntracked
,
7240 /* When there are no actions, xlate_actions() will search the flow
7241 * table. We don't want it to do that (we want it to resume), so
7242 * supply a no-op action if there aren't any.
7244 * (We can't necessarily avoid translating actions entirely if there
7245 * aren't any actions, because there might be some finishing-up to do
7246 * at the end of the pipeline, and we don't check for those
7248 .ofpacts
= any_actions
? pin
->actions
: &noop
.ofpact
,
7249 .ofpacts_len
= any_actions
? pin
->actions_len
: sizeof noop
,
7251 .action_set
= pin
->action_set
,
7252 .action_set_len
= pin
->action_set_len
,
7254 frozen_metadata_from_flow(&state
.metadata
,
7255 &pin
->base
.flow_metadata
.flow
);
7256 xin
.frozen_state
= &state
;
7258 struct xlate_out xout
;
7259 enum xlate_error error
= xlate_actions(&xin
, &xout
);
7261 xlate_out_uninit(&xout
);
7263 /* xlate_actions() can generate a number of errors, but only
7264 * XLATE_BRIDGE_NOT_FOUND really stands out to me as one that we should be
7265 * sure to report over OpenFlow. The others could come up in packet-outs
7266 * or regular flow translation and I don't think that it's going to be too
7267 * useful to report them to the controller. */
7268 return error
== XLATE_BRIDGE_NOT_FOUND
? OFPERR_NXR_STALE
: 0;
7271 /* Sends 'packet' out 'ofport'. If 'port' is a tunnel and that tunnel type
7272 * supports a notion of an OAM flag, sets it if 'oam' is true.
7273 * May modify 'packet'.
7274 * Returns 0 if successful, otherwise a positive errno value. */
7276 xlate_send_packet(const struct ofport_dpif
*ofport
, bool oam
,
7277 struct dp_packet
*packet
)
7279 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
7280 struct xport
*xport
;
7281 uint64_t ofpacts_stub
[1024 / 8];
7282 struct ofpbuf ofpacts
;
7285 ofpbuf_use_stack(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
7286 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
7287 flow_extract(packet
, &flow
);
7288 flow
.in_port
.ofp_port
= OFPP_NONE
;
7290 xport
= xport_lookup(xcfg
, ofport
);
7296 const ovs_be16 flag
= htons(NX_TUN_FLAG_OAM
);
7297 ofpact_put_set_field(&ofpacts
, mf_from_id(MFF_TUN_FLAGS
),
7301 ofpact_put_OUTPUT(&ofpacts
)->port
= xport
->ofp_port
;
7303 /* Actions here are not referring to anything versionable (flow tables or
7304 * groups) so we don't need to worry about the version here. */
7305 return ofproto_dpif_execute_actions(xport
->xbridge
->ofproto
,
7306 OVS_VERSION_MAX
, &flow
, NULL
,
7307 ofpacts
.data
, ofpacts
.size
, packet
);
7311 xlate_mac_learning_update(const struct ofproto_dpif
*ofproto
,
7312 ofp_port_t in_port
, struct eth_addr dl_src
,
7313 int vlan
, bool is_grat_arp
)
7315 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
7316 struct xbridge
*xbridge
;
7317 struct xbundle
*xbundle
;
7319 xbridge
= xbridge_lookup(xcfg
, ofproto
);
7324 xbundle
= lookup_input_bundle__(xbridge
, in_port
, NULL
);
7329 update_learning_table__(xbridge
, xbundle
, dl_src
, vlan
, is_grat_arp
);
7333 xlate_set_support(const struct ofproto_dpif
*ofproto
,
7334 const struct dpif_backer_support
*support
)
7336 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
7337 struct xbridge
*xbridge
= xbridge_lookup(xcfg
, ofproto
);
7340 xbridge
->support
= *support
;