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/vlog.h"
60 #include "ovs-router.h"
62 #include "tnl-neigh-cache.h"
63 #include "tnl-ports.h"
67 COVERAGE_DEFINE(xlate_actions
);
68 COVERAGE_DEFINE(xlate_actions_oversize
);
69 COVERAGE_DEFINE(xlate_actions_too_many_output
);
71 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate
);
73 /* Maximum depth of flow table recursion (due to resubmit actions) in a
76 * The goal of limiting the depth of resubmits is to ensure that flow
77 * translation eventually terminates. Only resubmits to the same table or an
78 * earlier table count against the maximum depth. This is because resubmits to
79 * strictly monotonically increasing table IDs will eventually terminate, since
80 * any OpenFlow switch has a finite number of tables. OpenFlow tables are most
81 * commonly traversed in numerically increasing order, so this limit has little
82 * effect on conventionally designed OpenFlow pipelines.
84 * Outputs to patch ports and to groups also count against the depth limit. */
87 /* Maximum number of resubmit actions in a flow translation, whether they are
88 * recursive or not. */
89 #define MAX_RESUBMITS (MAX_DEPTH * MAX_DEPTH)
92 struct hmap_node hmap_node
; /* Node in global 'xbridges' map. */
93 struct ofproto_dpif
*ofproto
; /* Key in global 'xbridges' map. */
95 struct ovs_list xbundles
; /* Owned xbundles. */
96 struct hmap xports
; /* Indexed by ofp_port. */
98 char *name
; /* Name used in log messages. */
99 struct dpif
*dpif
; /* Datapath interface. */
100 struct mac_learning
*ml
; /* Mac learning handle. */
101 struct mcast_snooping
*ms
; /* Multicast Snooping handle. */
102 struct mbridge
*mbridge
; /* Mirroring. */
103 struct dpif_sflow
*sflow
; /* SFlow handle, or null. */
104 struct dpif_ipfix
*ipfix
; /* Ipfix handle, or null. */
105 struct netflow
*netflow
; /* Netflow handle, or null. */
106 struct stp
*stp
; /* STP or null if disabled. */
107 struct rstp
*rstp
; /* RSTP or null if disabled. */
109 bool has_in_band
; /* Bridge has in band control? */
110 bool forward_bpdu
; /* Bridge forwards STP BPDUs? */
112 /* Datapath feature support. */
113 struct dpif_backer_support support
;
117 struct hmap_node hmap_node
; /* In global 'xbundles' map. */
118 struct ofbundle
*ofbundle
; /* Key in global 'xbundles' map. */
120 struct ovs_list list_node
; /* In parent 'xbridges' list. */
121 struct xbridge
*xbridge
; /* Parent xbridge. */
123 struct ovs_list xports
; /* Contains "struct xport"s. */
125 char *name
; /* Name used in log messages. */
126 struct bond
*bond
; /* Nonnull iff more than one port. */
127 struct lacp
*lacp
; /* LACP handle or null. */
129 enum port_vlan_mode vlan_mode
; /* VLAN mode. */
130 uint16_t qinq_ethtype
; /* Ethertype of dot1q-tunnel interface
131 * either 0x8100 or 0x88a8. */
132 int vlan
; /* -1=trunk port, else a 12-bit VLAN ID. */
133 unsigned long *trunks
; /* Bitmap of trunked VLANs, if 'vlan' == -1.
134 * NULL if all VLANs are trunked. */
135 unsigned long *cvlans
; /* Bitmap of allowed customer vlans,
136 * NULL if all VLANs are allowed */
137 bool use_priority_tags
; /* Use 802.1p tag for frames in VLAN 0? */
138 bool floodable
; /* No port has OFPUTIL_PC_NO_FLOOD set? */
139 bool protected; /* Protected port mode */
143 struct hmap_node hmap_node
; /* Node in global 'xports' map. */
144 struct ofport_dpif
*ofport
; /* Key in global 'xports map. */
146 struct hmap_node ofp_node
; /* Node in parent xbridge 'xports' map. */
147 ofp_port_t ofp_port
; /* Key in parent xbridge 'xports' map. */
149 odp_port_t odp_port
; /* Datapath port number or ODPP_NONE. */
151 struct ovs_list bundle_node
; /* In parent xbundle (if it exists). */
152 struct xbundle
*xbundle
; /* Parent xbundle or null. */
154 struct netdev
*netdev
; /* 'ofport''s netdev. */
156 struct xbridge
*xbridge
; /* Parent bridge. */
157 struct xport
*peer
; /* Patch port peer or null. */
159 enum ofputil_port_config config
; /* OpenFlow port configuration. */
160 enum ofputil_port_state state
; /* OpenFlow port state. */
161 int stp_port_no
; /* STP port number or -1 if not in use. */
162 struct rstp_port
*rstp_port
; /* RSTP port or null. */
164 struct hmap skb_priorities
; /* Map of 'skb_priority_to_dscp's. */
166 bool may_enable
; /* May be enabled in bonds. */
167 bool is_tunnel
; /* Is a tunnel port. */
169 struct cfm
*cfm
; /* CFM handle or null. */
170 struct bfd
*bfd
; /* BFD handle or null. */
171 struct lldp
*lldp
; /* LLDP handle or null. */
175 struct xlate_in
*xin
;
176 struct xlate_out
*xout
;
178 const struct xbridge
*xbridge
;
180 /* Flow at the last commit. */
181 struct flow base_flow
;
183 /* Tunnel IP destination address as received. This is stored separately
184 * as the base_flow.tunnel is cleared on init to reflect the datapath
185 * behavior. Used to make sure not to send tunneled output to ourselves,
186 * which might lead to an infinite loop. This could happen easily
187 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
188 * actually set the tun_dst field. */
189 struct in6_addr orig_tunnel_ipv6_dst
;
191 /* Stack for the push and pop actions. See comment above nx_stack_push()
192 * in nx-match.c for info on how the stack is stored. */
195 /* The rule that we are currently translating, or NULL. */
196 struct rule_dpif
*rule
;
198 /* Flow translation populates this with wildcards relevant in translation.
199 * When 'xin->wc' is nonnull, this is the same pointer. When 'xin->wc' is
200 * null, this is a pointer to a temporary buffer. */
201 struct flow_wildcards
*wc
;
203 /* Output buffer for datapath actions. When 'xin->odp_actions' is nonnull,
204 * this is the same pointer. When 'xin->odp_actions' is null, this points
205 * to a scratch ofpbuf. This allows code to add actions to
206 * 'ctx->odp_actions' without worrying about whether the caller really
208 struct ofpbuf
*odp_actions
;
210 /* Statistics maintained by xlate_table_action().
212 * These statistics limit the amount of work that a single flow
213 * translation can perform. The goal of the first of these, 'depth', is
214 * primarily to prevent translation from performing an infinite amount of
215 * work. It counts the current depth of nested "resubmit"s (and a few
216 * other activities); when a resubmit returns, it decreases. Resubmits to
217 * tables in strictly monotonically increasing order don't contribute to
218 * 'depth' because they cannot cause a flow translation to take an infinite
219 * amount of time (because the number of tables is finite). Translation
220 * aborts when 'depth' exceeds MAX_DEPTH.
222 * 'resubmits', on the other hand, prevents flow translation from
223 * performing an extraordinarily large while still finite amount of work.
224 * It counts the total number of resubmits (and a few other activities)
225 * that have been executed. Returning from a resubmit does not affect this
226 * counter. Thus, this limits the amount of work that a particular
227 * translation can perform. Translation aborts when 'resubmits' exceeds
228 * MAX_RESUBMITS (which is much larger than MAX_DEPTH).
230 int depth
; /* Current resubmit nesting depth. */
231 int resubmits
; /* Total number of resubmits. */
232 bool in_group
; /* Currently translating ofgroup, if true. */
233 bool in_action_set
; /* Currently translating action_set, if true. */
234 bool in_packet_out
; /* Currently translating a packet_out msg, if
237 uint8_t table_id
; /* OpenFlow table ID where flow was found. */
238 ovs_be64 rule_cookie
; /* Cookie of the rule being translated. */
239 uint32_t orig_skb_priority
; /* Priority when packet arrived. */
240 uint32_t sflow_n_outputs
; /* Number of output ports. */
241 odp_port_t sflow_odp_port
; /* Output port for composing sFlow action. */
242 ofp_port_t nf_output_iface
; /* Output interface index for NetFlow. */
243 bool exit
; /* No further actions should be processed. */
244 mirror_mask_t mirrors
; /* Bitmap of associated mirrors. */
245 int mirror_snaplen
; /* Max size of a mirror packet in byte. */
247 /* Freezing Translation
248 * ====================
250 * At some point during translation, the code may recognize the need to halt
251 * and checkpoint the translation in a way that it can be restarted again
252 * later. We call the checkpointing process "freezing" and the restarting
255 * The use cases for freezing are:
257 * - "Recirculation", where the translation process discovers that it
258 * doesn't have enough information to complete translation without
259 * actually executing the actions that have already been translated,
260 * which provides the additionally needed information. In these
261 * situations, translation freezes translation and assigns the frozen
262 * data a unique "recirculation ID", which it associates with the data
263 * in a table in userspace (see ofproto-dpif-rid.h). It also adds a
264 * OVS_ACTION_ATTR_RECIRC action specifying that ID to the datapath
265 * actions. When a packet hits that action, the datapath looks its
266 * flow up again using the ID. If there's a miss, it comes back to
267 * userspace, which find the recirculation table entry for the ID,
268 * thaws the associated frozen data, and continues translation from
269 * that point given the additional information that is now known.
271 * The archetypal example is MPLS. As MPLS is implemented in
272 * OpenFlow, the protocol that follows the last MPLS label becomes
273 * known only when that label is popped by an OpenFlow action. That
274 * means that Open vSwitch can't extract the headers beyond the MPLS
275 * labels until the pop action is executed. Thus, at that point
276 * translation uses the recirculation process to extract the headers
277 * beyond the MPLS labels.
279 * (OVS also uses OVS_ACTION_ATTR_RECIRC to implement hashing for
280 * output to bonds. OVS pre-populates all the datapath flows for bond
281 * output in the datapath, though, which means that the elaborate
282 * process of coming back to userspace for a second round of
283 * translation isn't needed, and so bonds don't follow the above
286 * - "Continuation". A continuation is a way for an OpenFlow controller
287 * to interpose on a packet's traversal of the OpenFlow tables. When
288 * the translation process encounters a "controller" action with the
289 * "pause" flag, it freezes translation, serializes the frozen data,
290 * and sends it to an OpenFlow controller. The controller then
291 * examines and possibly modifies the frozen data and eventually sends
292 * it back to the switch, which thaws it and continues translation.
294 * The main problem of freezing translation is preserving state, so that
295 * when the translation is thawed later it resumes from where it left off,
296 * without disruption. In particular, actions must be preserved as follows:
298 * - If we're freezing because an action needed more information, the
299 * action that prompted it.
301 * - Any actions remaining to be translated within the current flow.
303 * - If translation was frozen within a NXAST_RESUBMIT, then any actions
304 * following the resubmit action. Resubmit actions can be nested, so
305 * this has to go all the way up the control stack.
307 * - The OpenFlow 1.1+ action set.
309 * State that actions and flow table lookups can depend on, such as the
310 * following, must also be preserved:
312 * - Metadata fields (input port, registers, OF1.1+ metadata, ...).
314 * - The stack used by NXAST_STACK_PUSH and NXAST_STACK_POP actions.
316 * - The table ID and cookie of the flow being translated at each level
317 * of the control stack, because these can become visible through
318 * OFPAT_CONTROLLER actions (and other ways).
320 * Translation allows for the control of this state preservation via these
321 * members. When a need to freeze translation is identified, the
322 * translation process:
324 * 1. Sets 'freezing' to true.
326 * 2. Sets 'exit' to true to tell later steps that we're exiting from the
327 * translation process.
329 * 3. Adds an OFPACT_UNROLL_XLATE action to 'frozen_actions', and points
330 * frozen_actions.header to the action to make it easy to find it later.
331 * This action holds the current table ID and cookie so that they can be
332 * restored during a post-recirculation upcall translation.
334 * 4. Adds the action that prompted recirculation and any actions following
335 * it within the same flow to 'frozen_actions', so that they can be
336 * executed during a post-recirculation upcall translation.
340 * 6. The action that prompted recirculation might be nested in a stack of
341 * nested "resubmit"s that have actions remaining. Each of these notices
342 * that we're exiting and freezing and responds by adding more
343 * OFPACT_UNROLL_XLATE actions to 'frozen_actions', as necessary,
344 * followed by any actions that were yet unprocessed.
346 * If we're freezing because of recirculation, the caller generates a
347 * recirculation ID and associates all the state produced by this process
348 * with it. For post-recirculation upcall translation, the caller passes it
349 * back in for the new translation to execute. The process yielded a set of
350 * ofpacts that can be translated directly, so it is not much of a special
351 * case at that point.
354 bool recirc_update_dp_hash
; /* Generated recirculation will be preceded
355 * by datapath HASH action to get an updated
356 * dp_hash after recirculation. */
357 uint32_t dp_hash_alg
;
358 uint32_t dp_hash_basis
;
359 struct ofpbuf frozen_actions
;
360 const struct ofpact_controller
*pause
;
362 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
363 * This is a trigger for recirculation in cases where translating an action
364 * or looking up a flow requires access to the fields of the packet after
365 * the MPLS label stack that was originally present. */
368 /* True if conntrack has been performed on this packet during processing
369 * on the current bridge. This is used to determine whether conntrack
370 * state from the datapath should be honored after thawing. */
373 /* Pointer to an embedded NAT action in a conntrack action, or NULL. */
374 struct ofpact_nat
*ct_nat_action
;
376 /* OpenFlow 1.1+ action set.
378 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
379 * When translation is otherwise complete, ofpacts_execute_action_set()
380 * converts it to a set of "struct ofpact"s that can be translated into
381 * datapath actions. */
382 bool action_set_has_group
; /* Action set contains OFPACT_GROUP? */
383 struct ofpbuf action_set
; /* Action set. */
385 enum xlate_error error
; /* Translation failed. */
388 /* Structure to track VLAN manipulation */
389 struct xvlan_single
{
396 struct xvlan_single v
[FLOW_MAX_VLAN_HEADERS
];
399 const char *xlate_strerror(enum xlate_error error
)
404 case XLATE_BRIDGE_NOT_FOUND
:
405 return "Bridge not found";
406 case XLATE_RECURSION_TOO_DEEP
:
407 return "Recursion too deep";
408 case XLATE_TOO_MANY_RESUBMITS
:
409 return "Too many resubmits";
410 case XLATE_STACK_TOO_DEEP
:
411 return "Stack too deep";
412 case XLATE_NO_RECIRCULATION_CONTEXT
:
413 return "No recirculation context";
414 case XLATE_RECIRCULATION_CONFLICT
:
415 return "Recirculation conflict";
416 case XLATE_TOO_MANY_MPLS_LABELS
:
417 return "Too many MPLS labels";
418 case XLATE_INVALID_TUNNEL_METADATA
:
419 return "Invalid tunnel metadata";
421 return "Unknown error";
424 static void xlate_action_set(struct xlate_ctx
*ctx
);
425 static void xlate_commit_actions(struct xlate_ctx
*ctx
);
428 apply_nested_clone_actions(struct xlate_ctx
*ctx
, const struct xport
*in_dev
,
429 struct xport
*out_dev
);
432 ctx_trigger_freeze(struct xlate_ctx
*ctx
)
435 ctx
->freezing
= true;
439 ctx_trigger_recirculate_with_hash(struct xlate_ctx
*ctx
, uint32_t type
,
443 ctx
->freezing
= true;
444 ctx
->recirc_update_dp_hash
= true;
445 ctx
->dp_hash_alg
= type
;
446 ctx
->dp_hash_basis
= basis
;
450 ctx_first_frozen_action(const struct xlate_ctx
*ctx
)
452 return !ctx
->frozen_actions
.size
;
456 ctx_cancel_freeze(struct xlate_ctx
*ctx
)
459 ctx
->freezing
= false;
460 ctx
->recirc_update_dp_hash
= false;
461 ofpbuf_clear(&ctx
->frozen_actions
);
462 ctx
->frozen_actions
.header
= NULL
;
466 static void finish_freezing(struct xlate_ctx
*ctx
);
468 /* A controller may use OFPP_NONE as the ingress port to indicate that
469 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
470 * when an input bundle is needed for validation (e.g., mirroring or
471 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
472 * any 'port' structs, so care must be taken when dealing with it. */
473 static struct xbundle ofpp_none_bundle
= {
475 .vlan_mode
= PORT_VLAN_TRUNK
478 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
479 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
480 * traffic egressing the 'ofport' with that priority should be marked with. */
481 struct skb_priority_to_dscp
{
482 struct hmap_node hmap_node
; /* Node in 'ofport_dpif''s 'skb_priorities'. */
483 uint32_t skb_priority
; /* Priority of this queue (see struct flow). */
485 uint8_t dscp
; /* DSCP bits to mark outgoing traffic with. */
488 /* Xlate config contains hash maps of all bridges, bundles and ports.
489 * Xcfgp contains the pointer to the current xlate configuration.
490 * When the main thread needs to change the configuration, it copies xcfgp to
491 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
492 * does not block handler and revalidator threads. */
494 struct hmap xbridges
;
495 struct hmap xbundles
;
498 static OVSRCU_TYPE(struct xlate_cfg
*) xcfgp
= OVSRCU_INITIALIZER(NULL
);
499 static struct xlate_cfg
*new_xcfg
= NULL
;
501 static bool may_receive(const struct xport
*, struct xlate_ctx
*);
502 static void do_xlate_actions(const struct ofpact
*, size_t ofpacts_len
,
504 static void xlate_normal(struct xlate_ctx
*);
505 static void xlate_table_action(struct xlate_ctx
*, ofp_port_t in_port
,
506 uint8_t table_id
, bool may_packet_in
,
507 bool honor_table_miss
, bool with_ct_orig
);
508 static bool input_vid_is_valid(const struct xlate_ctx
*,
509 uint16_t vid
, struct xbundle
*);
510 static void xvlan_copy(struct xvlan
*dst
, const struct xvlan
*src
);
511 static void xvlan_pop(struct xvlan
*src
);
512 static void xvlan_push_uninit(struct xvlan
*src
);
513 static void xvlan_extract(const struct flow
*, struct xvlan
*);
514 static void xvlan_put(struct flow
*, const struct xvlan
*);
515 static void xvlan_input_translate(const struct xbundle
*,
516 const struct xvlan
*in
,
517 struct xvlan
*xvlan
);
518 static void xvlan_output_translate(const struct xbundle
*,
519 const struct xvlan
*xvlan
,
521 static void output_normal(struct xlate_ctx
*, const struct xbundle
*,
522 const struct xvlan
*);
524 /* Optional bond recirculation parameter to compose_output_action(). */
525 struct xlate_bond_recirc
{
526 uint32_t recirc_id
; /* !0 Use recirculation instead of output. */
527 uint8_t hash_alg
; /* !0 Compute hash for recirc before. */
528 uint32_t hash_basis
; /* Compute hash for recirc before. */
531 static void compose_output_action(struct xlate_ctx
*, ofp_port_t ofp_port
,
532 const struct xlate_bond_recirc
*xr
);
534 static struct xbridge
*xbridge_lookup(struct xlate_cfg
*,
535 const struct ofproto_dpif
*);
536 static struct xbridge
*xbridge_lookup_by_uuid(struct xlate_cfg
*,
537 const struct uuid
*);
538 static struct xbundle
*xbundle_lookup(struct xlate_cfg
*,
539 const struct ofbundle
*);
540 static struct xport
*xport_lookup(struct xlate_cfg
*,
541 const struct ofport_dpif
*);
542 static struct xport
*get_ofp_port(const struct xbridge
*, ofp_port_t ofp_port
);
543 static struct skb_priority_to_dscp
*get_skb_priority(const struct xport
*,
544 uint32_t skb_priority
);
545 static void clear_skb_priorities(struct xport
*);
546 static size_t count_skb_priorities(const struct xport
*);
547 static bool dscp_from_skb_priority(const struct xport
*, uint32_t skb_priority
,
550 static void xlate_xbridge_init(struct xlate_cfg
*, struct xbridge
*);
551 static void xlate_xbundle_init(struct xlate_cfg
*, struct xbundle
*);
552 static void xlate_xport_init(struct xlate_cfg
*, struct xport
*);
553 static void xlate_xbridge_set(struct xbridge
*, struct dpif
*,
554 const struct mac_learning
*, struct stp
*,
555 struct rstp
*, const struct mcast_snooping
*,
556 const struct mbridge
*,
557 const struct dpif_sflow
*,
558 const struct dpif_ipfix
*,
559 const struct netflow
*,
560 bool forward_bpdu
, bool has_in_band
,
561 const struct dpif_backer_support
*);
562 static void xlate_xbundle_set(struct xbundle
*xbundle
,
563 enum port_vlan_mode vlan_mode
,
564 uint16_t qinq_ethtype
, int vlan
,
565 unsigned long *trunks
, unsigned long *cvlans
,
566 bool use_priority_tags
,
567 const struct bond
*bond
, const struct lacp
*lacp
,
568 bool floodable
, bool protected);
569 static void xlate_xport_set(struct xport
*xport
, odp_port_t odp_port
,
570 const struct netdev
*netdev
, const struct cfm
*cfm
,
571 const struct bfd
*bfd
, const struct lldp
*lldp
,
572 int stp_port_no
, const struct rstp_port
*rstp_port
,
573 enum ofputil_port_config config
,
574 enum ofputil_port_state state
, bool is_tunnel
,
576 static void xlate_xbridge_remove(struct xlate_cfg
*, struct xbridge
*);
577 static void xlate_xbundle_remove(struct xlate_cfg
*, struct xbundle
*);
578 static void xlate_xport_remove(struct xlate_cfg
*, struct xport
*);
579 static void xlate_xbridge_copy(struct xbridge
*);
580 static void xlate_xbundle_copy(struct xbridge
*, struct xbundle
*);
581 static void xlate_xport_copy(struct xbridge
*, struct xbundle
*,
583 static void xlate_xcfg_free(struct xlate_cfg
*);
585 /* Tracing helpers. */
587 /* If tracing is enabled in 'ctx', creates a new trace node and appends it to
588 * the list of nodes maintained in ctx->xin. The new node has type 'type' and
589 * its text is created from 'format' by treating it as a printf format string.
590 * Returns the list of nodes embedded within the new trace node; ordinarily,
591 * the calleer can ignore this, but it is useful if the caller needs to nest
592 * more trace nodes within the new node.
594 * If tracing is not enabled, does nothing and returns NULL. */
595 static struct ovs_list
* OVS_PRINTF_FORMAT(3, 4)
596 xlate_report(const struct xlate_ctx
*ctx
, enum oftrace_node_type type
,
597 const char *format
, ...)
599 struct ovs_list
*subtrace
= NULL
;
600 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
602 va_start(args
, format
);
603 char *text
= xvasprintf(format
, args
);
604 subtrace
= &oftrace_report(ctx
->xin
->trace
, type
, text
)->subs
;
611 /* This is like xlate_report() for errors that are serious enough that we
612 * should log them even if we are not tracing. */
613 static void OVS_PRINTF_FORMAT(2, 3)
614 xlate_report_error(const struct xlate_ctx
*ctx
, const char *format
, ...)
616 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
617 if (!OVS_UNLIKELY(ctx
->xin
->trace
)
618 && (!ctx
->xin
->packet
|| VLOG_DROP_WARN(&rl
))) {
622 struct ds s
= DS_EMPTY_INITIALIZER
;
624 va_start(args
, format
);
625 ds_put_format_valist(&s
, format
, args
);
628 if (ctx
->xin
->trace
) {
629 oftrace_report(ctx
->xin
->trace
, OFT_ERROR
, ds_cstr(&s
));
631 ds_put_cstr(&s
, " while processing ");
632 flow_format(&s
, &ctx
->base_flow
);
633 ds_put_format(&s
, " on bridge %s", ctx
->xbridge
->name
);
634 VLOG_WARN("%s", ds_cstr(&s
));
639 /* This is like xlate_report() for messages that should be logged at debug
640 * level (even if we are not tracing) because they can be valuable for
642 static void OVS_PRINTF_FORMAT(3, 4)
643 xlate_report_debug(const struct xlate_ctx
*ctx
, enum oftrace_node_type type
,
644 const char *format
, ...)
646 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(30, 300);
647 if (!OVS_UNLIKELY(ctx
->xin
->trace
)
648 && (!ctx
->xin
->packet
|| VLOG_DROP_DBG(&rl
))) {
652 struct ds s
= DS_EMPTY_INITIALIZER
;
654 va_start(args
, format
);
655 ds_put_format_valist(&s
, format
, args
);
658 if (ctx
->xin
->trace
) {
659 oftrace_report(ctx
->xin
->trace
, type
, ds_cstr(&s
));
661 VLOG_DBG("bridge %s: %s", ctx
->xbridge
->name
, ds_cstr(&s
));
666 /* If tracing is enabled in 'ctx', appends a node of the given 'type' to the
667 * trace, whose text is 'title' followed by a formatted version of the
668 * 'ofpacts_len' OpenFlow actions in 'ofpacts'.
670 * If tracing is not enabled, does nothing. */
672 xlate_report_actions(const struct xlate_ctx
*ctx
, enum oftrace_node_type type
,
674 const struct ofpact
*ofpacts
, size_t ofpacts_len
)
676 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
677 struct ds s
= DS_EMPTY_INITIALIZER
;
678 ds_put_format(&s
, "%s: ", title
);
679 ofpacts_format(ofpacts
, ofpacts_len
, &s
);
680 oftrace_report(ctx
->xin
->trace
, type
, ds_cstr(&s
));
685 /* If tracing is enabled in 'ctx', appends a node of type OFT_DETAIL to the
686 * trace, whose the message is a formatted version of the OpenFlow action set.
687 * 'verb' should be "was" or "is", depending on whether the action set reported
688 * is the new action set or the old one.
690 * If tracing is not enabled, does nothing. */
692 xlate_report_action_set(const struct xlate_ctx
*ctx
, const char *verb
)
694 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
695 struct ofpbuf action_list
;
696 ofpbuf_init(&action_list
, 0);
697 ofpacts_execute_action_set(&action_list
, &ctx
->action_set
);
698 if (action_list
.size
) {
699 struct ds s
= DS_EMPTY_INITIALIZER
;
700 ofpacts_format(action_list
.data
, action_list
.size
, &s
);
701 xlate_report(ctx
, OFT_DETAIL
, "action set %s: %s",
705 xlate_report(ctx
, OFT_DETAIL
, "action set %s empty", verb
);
707 ofpbuf_uninit(&action_list
);
712 /* If tracing is enabled in 'ctx', appends a node representing 'rule' (in
713 * OpenFlow table 'table_id') to the trace and makes this node the parent for
714 * future trace nodes. The caller should save ctx->xin->trace before calling
715 * this function, then after tracing all of the activities under the table,
716 * restore its previous value.
718 * If tracing is not enabled, does nothing. */
720 xlate_report_table(const struct xlate_ctx
*ctx
, struct rule_dpif
*rule
,
723 if (OVS_LIKELY(!ctx
->xin
->trace
)) {
727 struct ds s
= DS_EMPTY_INITIALIZER
;
728 ds_put_format(&s
, "%2d. ", table_id
);
729 if (rule
== ctx
->xin
->ofproto
->miss_rule
) {
730 ds_put_cstr(&s
, "No match, and a \"packet-in\" is called for.");
731 } else if (rule
== ctx
->xin
->ofproto
->no_packet_in_rule
) {
732 ds_put_cstr(&s
, "No match.");
733 } else if (rule
== ctx
->xin
->ofproto
->drop_frags_rule
) {
734 ds_put_cstr(&s
, "Packets are IP fragments and "
735 "the fragment handling mode is \"drop\".");
737 minimatch_format(&rule
->up
.cr
.match
,
738 ofproto_get_tun_tab(&ctx
->xin
->ofproto
->up
),
739 &s
, OFP_DEFAULT_PRIORITY
);
740 if (ds_last(&s
) != ' ') {
741 ds_put_cstr(&s
, ", ");
743 ds_put_format(&s
, "priority %d", rule
->up
.cr
.priority
);
744 if (rule
->up
.flow_cookie
) {
745 ds_put_format(&s
, ", cookie %#"PRIx64
,
746 ntohll(rule
->up
.flow_cookie
));
749 ctx
->xin
->trace
= &oftrace_report(ctx
->xin
->trace
, OFT_TABLE
,
754 /* If tracing is enabled in 'ctx', adds an OFT_DETAIL trace node to 'ctx'
755 * reporting the value of subfield 'sf'.
757 * If tracing is not enabled, does nothing. */
759 xlate_report_subfield(const struct xlate_ctx
*ctx
,
760 const struct mf_subfield
*sf
)
762 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
763 struct ds s
= DS_EMPTY_INITIALIZER
;
764 mf_format_subfield(sf
, &s
);
765 ds_put_cstr(&s
, " is now ");
767 if (sf
->ofs
== 0 && sf
->n_bits
>= sf
->field
->n_bits
) {
768 union mf_value value
;
769 mf_get_value(sf
->field
, &ctx
->xin
->flow
, &value
);
770 mf_format(sf
->field
, &value
, NULL
, &s
);
772 union mf_subvalue cst
;
773 mf_read_subfield(sf
, &ctx
->xin
->flow
, &cst
);
774 ds_put_hex(&s
, &cst
, sizeof cst
);
777 xlate_report(ctx
, OFT_DETAIL
, "%s", ds_cstr(&s
));
784 xlate_xbridge_init(struct xlate_cfg
*xcfg
, struct xbridge
*xbridge
)
786 ovs_list_init(&xbridge
->xbundles
);
787 hmap_init(&xbridge
->xports
);
788 hmap_insert(&xcfg
->xbridges
, &xbridge
->hmap_node
,
789 hash_pointer(xbridge
->ofproto
, 0));
793 xlate_xbundle_init(struct xlate_cfg
*xcfg
, struct xbundle
*xbundle
)
795 ovs_list_init(&xbundle
->xports
);
796 ovs_list_insert(&xbundle
->xbridge
->xbundles
, &xbundle
->list_node
);
797 hmap_insert(&xcfg
->xbundles
, &xbundle
->hmap_node
,
798 hash_pointer(xbundle
->ofbundle
, 0));
802 xlate_xport_init(struct xlate_cfg
*xcfg
, struct xport
*xport
)
804 hmap_init(&xport
->skb_priorities
);
805 hmap_insert(&xcfg
->xports
, &xport
->hmap_node
,
806 hash_pointer(xport
->ofport
, 0));
807 hmap_insert(&xport
->xbridge
->xports
, &xport
->ofp_node
,
808 hash_ofp_port(xport
->ofp_port
));
812 xlate_xbridge_set(struct xbridge
*xbridge
,
814 const struct mac_learning
*ml
, struct stp
*stp
,
815 struct rstp
*rstp
, const struct mcast_snooping
*ms
,
816 const struct mbridge
*mbridge
,
817 const struct dpif_sflow
*sflow
,
818 const struct dpif_ipfix
*ipfix
,
819 const struct netflow
*netflow
,
820 bool forward_bpdu
, bool has_in_band
,
821 const struct dpif_backer_support
*support
)
823 if (xbridge
->ml
!= ml
) {
824 mac_learning_unref(xbridge
->ml
);
825 xbridge
->ml
= mac_learning_ref(ml
);
828 if (xbridge
->ms
!= ms
) {
829 mcast_snooping_unref(xbridge
->ms
);
830 xbridge
->ms
= mcast_snooping_ref(ms
);
833 if (xbridge
->mbridge
!= mbridge
) {
834 mbridge_unref(xbridge
->mbridge
);
835 xbridge
->mbridge
= mbridge_ref(mbridge
);
838 if (xbridge
->sflow
!= sflow
) {
839 dpif_sflow_unref(xbridge
->sflow
);
840 xbridge
->sflow
= dpif_sflow_ref(sflow
);
843 if (xbridge
->ipfix
!= ipfix
) {
844 dpif_ipfix_unref(xbridge
->ipfix
);
845 xbridge
->ipfix
= dpif_ipfix_ref(ipfix
);
848 if (xbridge
->stp
!= stp
) {
849 stp_unref(xbridge
->stp
);
850 xbridge
->stp
= stp_ref(stp
);
853 if (xbridge
->rstp
!= rstp
) {
854 rstp_unref(xbridge
->rstp
);
855 xbridge
->rstp
= rstp_ref(rstp
);
858 if (xbridge
->netflow
!= netflow
) {
859 netflow_unref(xbridge
->netflow
);
860 xbridge
->netflow
= netflow_ref(netflow
);
863 xbridge
->dpif
= dpif
;
864 xbridge
->forward_bpdu
= forward_bpdu
;
865 xbridge
->has_in_band
= has_in_band
;
866 xbridge
->support
= *support
;
870 xlate_xbundle_set(struct xbundle
*xbundle
,
871 enum port_vlan_mode vlan_mode
, uint16_t qinq_ethtype
,
872 int vlan
, unsigned long *trunks
, unsigned long *cvlans
,
873 bool use_priority_tags
,
874 const struct bond
*bond
, const struct lacp
*lacp
,
875 bool floodable
, bool protected)
877 ovs_assert(xbundle
->xbridge
);
879 xbundle
->vlan_mode
= vlan_mode
;
880 xbundle
->qinq_ethtype
= qinq_ethtype
;
881 xbundle
->vlan
= vlan
;
882 xbundle
->trunks
= trunks
;
883 xbundle
->cvlans
= cvlans
;
884 xbundle
->use_priority_tags
= use_priority_tags
;
885 xbundle
->floodable
= floodable
;
886 xbundle
->protected = protected;
888 if (xbundle
->bond
!= bond
) {
889 bond_unref(xbundle
->bond
);
890 xbundle
->bond
= bond_ref(bond
);
893 if (xbundle
->lacp
!= lacp
) {
894 lacp_unref(xbundle
->lacp
);
895 xbundle
->lacp
= lacp_ref(lacp
);
900 xlate_xport_set(struct xport
*xport
, odp_port_t odp_port
,
901 const struct netdev
*netdev
, const struct cfm
*cfm
,
902 const struct bfd
*bfd
, const struct lldp
*lldp
, int stp_port_no
,
903 const struct rstp_port
* rstp_port
,
904 enum ofputil_port_config config
, enum ofputil_port_state state
,
905 bool is_tunnel
, bool may_enable
)
907 xport
->config
= config
;
908 xport
->state
= state
;
909 xport
->stp_port_no
= stp_port_no
;
910 xport
->is_tunnel
= is_tunnel
;
911 xport
->may_enable
= may_enable
;
912 xport
->odp_port
= odp_port
;
914 if (xport
->rstp_port
!= rstp_port
) {
915 rstp_port_unref(xport
->rstp_port
);
916 xport
->rstp_port
= rstp_port_ref(rstp_port
);
919 if (xport
->cfm
!= cfm
) {
920 cfm_unref(xport
->cfm
);
921 xport
->cfm
= cfm_ref(cfm
);
924 if (xport
->bfd
!= bfd
) {
925 bfd_unref(xport
->bfd
);
926 xport
->bfd
= bfd_ref(bfd
);
929 if (xport
->lldp
!= lldp
) {
930 lldp_unref(xport
->lldp
);
931 xport
->lldp
= lldp_ref(lldp
);
934 if (xport
->netdev
!= netdev
) {
935 netdev_close(xport
->netdev
);
936 xport
->netdev
= netdev_ref(netdev
);
941 xlate_xbridge_copy(struct xbridge
*xbridge
)
943 struct xbundle
*xbundle
;
945 struct xbridge
*new_xbridge
= xzalloc(sizeof *xbridge
);
946 new_xbridge
->ofproto
= xbridge
->ofproto
;
947 new_xbridge
->name
= xstrdup(xbridge
->name
);
948 xlate_xbridge_init(new_xcfg
, new_xbridge
);
950 xlate_xbridge_set(new_xbridge
,
951 xbridge
->dpif
, xbridge
->ml
, xbridge
->stp
,
952 xbridge
->rstp
, xbridge
->ms
, xbridge
->mbridge
,
953 xbridge
->sflow
, xbridge
->ipfix
, xbridge
->netflow
,
954 xbridge
->forward_bpdu
, xbridge
->has_in_band
,
956 LIST_FOR_EACH (xbundle
, list_node
, &xbridge
->xbundles
) {
957 xlate_xbundle_copy(new_xbridge
, xbundle
);
960 /* Copy xports which are not part of a xbundle */
961 HMAP_FOR_EACH (xport
, ofp_node
, &xbridge
->xports
) {
962 if (!xport
->xbundle
) {
963 xlate_xport_copy(new_xbridge
, NULL
, xport
);
969 xlate_xbundle_copy(struct xbridge
*xbridge
, struct xbundle
*xbundle
)
972 struct xbundle
*new_xbundle
= xzalloc(sizeof *xbundle
);
973 new_xbundle
->ofbundle
= xbundle
->ofbundle
;
974 new_xbundle
->xbridge
= xbridge
;
975 new_xbundle
->name
= xstrdup(xbundle
->name
);
976 xlate_xbundle_init(new_xcfg
, new_xbundle
);
978 xlate_xbundle_set(new_xbundle
, xbundle
->vlan_mode
, xbundle
->qinq_ethtype
,
979 xbundle
->vlan
, xbundle
->trunks
, xbundle
->cvlans
,
980 xbundle
->use_priority_tags
, xbundle
->bond
, xbundle
->lacp
,
981 xbundle
->floodable
, xbundle
->protected);
982 LIST_FOR_EACH (xport
, bundle_node
, &xbundle
->xports
) {
983 xlate_xport_copy(xbridge
, new_xbundle
, xport
);
988 xlate_xport_copy(struct xbridge
*xbridge
, struct xbundle
*xbundle
,
991 struct skb_priority_to_dscp
*pdscp
, *new_pdscp
;
992 struct xport
*new_xport
= xzalloc(sizeof *xport
);
993 new_xport
->ofport
= xport
->ofport
;
994 new_xport
->ofp_port
= xport
->ofp_port
;
995 new_xport
->xbridge
= xbridge
;
996 xlate_xport_init(new_xcfg
, new_xport
);
998 xlate_xport_set(new_xport
, xport
->odp_port
, xport
->netdev
, xport
->cfm
,
999 xport
->bfd
, xport
->lldp
, xport
->stp_port_no
,
1000 xport
->rstp_port
, xport
->config
, xport
->state
,
1001 xport
->is_tunnel
, xport
->may_enable
);
1004 struct xport
*peer
= xport_lookup(new_xcfg
, xport
->peer
->ofport
);
1006 new_xport
->peer
= peer
;
1007 new_xport
->peer
->peer
= new_xport
;
1012 new_xport
->xbundle
= xbundle
;
1013 ovs_list_insert(&new_xport
->xbundle
->xports
, &new_xport
->bundle_node
);
1016 HMAP_FOR_EACH (pdscp
, hmap_node
, &xport
->skb_priorities
) {
1017 new_pdscp
= xmalloc(sizeof *pdscp
);
1018 new_pdscp
->skb_priority
= pdscp
->skb_priority
;
1019 new_pdscp
->dscp
= pdscp
->dscp
;
1020 hmap_insert(&new_xport
->skb_priorities
, &new_pdscp
->hmap_node
,
1021 hash_int(new_pdscp
->skb_priority
, 0));
1025 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
1026 * configuration in xcfgp.
1028 * This needs to be called after editing the xlate configuration.
1030 * Functions that edit the new xlate configuration are
1031 * xlate_<ofproto/bundle/ofport>_set and xlate_<ofproto/bundle/ofport>_remove.
1033 * A sample workflow:
1035 * xlate_txn_start();
1037 * edit_xlate_configuration();
1039 * xlate_txn_commit(); */
1041 xlate_txn_commit(void)
1043 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1045 ovsrcu_set(&xcfgp
, new_xcfg
);
1046 ovsrcu_synchronize();
1047 xlate_xcfg_free(xcfg
);
1051 /* Copies the current xlate configuration in xcfgp to new_xcfg.
1053 * This needs to be called prior to editing the xlate configuration. */
1055 xlate_txn_start(void)
1057 struct xbridge
*xbridge
;
1058 struct xlate_cfg
*xcfg
;
1060 ovs_assert(!new_xcfg
);
1062 new_xcfg
= xmalloc(sizeof *new_xcfg
);
1063 hmap_init(&new_xcfg
->xbridges
);
1064 hmap_init(&new_xcfg
->xbundles
);
1065 hmap_init(&new_xcfg
->xports
);
1067 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1072 HMAP_FOR_EACH (xbridge
, hmap_node
, &xcfg
->xbridges
) {
1073 xlate_xbridge_copy(xbridge
);
1079 xlate_xcfg_free(struct xlate_cfg
*xcfg
)
1081 struct xbridge
*xbridge
, *next_xbridge
;
1087 HMAP_FOR_EACH_SAFE (xbridge
, next_xbridge
, hmap_node
, &xcfg
->xbridges
) {
1088 xlate_xbridge_remove(xcfg
, xbridge
);
1091 hmap_destroy(&xcfg
->xbridges
);
1092 hmap_destroy(&xcfg
->xbundles
);
1093 hmap_destroy(&xcfg
->xports
);
1098 xlate_ofproto_set(struct ofproto_dpif
*ofproto
, const char *name
,
1100 const struct mac_learning
*ml
, struct stp
*stp
,
1101 struct rstp
*rstp
, const struct mcast_snooping
*ms
,
1102 const struct mbridge
*mbridge
,
1103 const struct dpif_sflow
*sflow
,
1104 const struct dpif_ipfix
*ipfix
,
1105 const struct netflow
*netflow
,
1106 bool forward_bpdu
, bool has_in_band
,
1107 const struct dpif_backer_support
*support
)
1109 struct xbridge
*xbridge
;
1111 ovs_assert(new_xcfg
);
1113 xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
1115 xbridge
= xzalloc(sizeof *xbridge
);
1116 xbridge
->ofproto
= ofproto
;
1118 xlate_xbridge_init(new_xcfg
, xbridge
);
1121 free(xbridge
->name
);
1122 xbridge
->name
= xstrdup(name
);
1124 xlate_xbridge_set(xbridge
, dpif
, ml
, stp
, rstp
, ms
, mbridge
, sflow
, ipfix
,
1125 netflow
, forward_bpdu
, has_in_band
, support
);
1129 xlate_xbridge_remove(struct xlate_cfg
*xcfg
, struct xbridge
*xbridge
)
1131 struct xbundle
*xbundle
, *next_xbundle
;
1132 struct xport
*xport
, *next_xport
;
1138 HMAP_FOR_EACH_SAFE (xport
, next_xport
, ofp_node
, &xbridge
->xports
) {
1139 xlate_xport_remove(xcfg
, xport
);
1142 LIST_FOR_EACH_SAFE (xbundle
, next_xbundle
, list_node
, &xbridge
->xbundles
) {
1143 xlate_xbundle_remove(xcfg
, xbundle
);
1146 hmap_remove(&xcfg
->xbridges
, &xbridge
->hmap_node
);
1147 mac_learning_unref(xbridge
->ml
);
1148 mcast_snooping_unref(xbridge
->ms
);
1149 mbridge_unref(xbridge
->mbridge
);
1150 dpif_sflow_unref(xbridge
->sflow
);
1151 dpif_ipfix_unref(xbridge
->ipfix
);
1152 netflow_unref(xbridge
->netflow
);
1153 stp_unref(xbridge
->stp
);
1154 rstp_unref(xbridge
->rstp
);
1155 hmap_destroy(&xbridge
->xports
);
1156 free(xbridge
->name
);
1161 xlate_remove_ofproto(struct ofproto_dpif
*ofproto
)
1163 struct xbridge
*xbridge
;
1165 ovs_assert(new_xcfg
);
1167 xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
1168 xlate_xbridge_remove(new_xcfg
, xbridge
);
1172 xlate_bundle_set(struct ofproto_dpif
*ofproto
, struct ofbundle
*ofbundle
,
1173 const char *name
, enum port_vlan_mode vlan_mode
,
1174 uint16_t qinq_ethtype
, int vlan
,
1175 unsigned long *trunks
, unsigned long *cvlans
,
1176 bool use_priority_tags
,
1177 const struct bond
*bond
, const struct lacp
*lacp
,
1178 bool floodable
, bool protected)
1180 struct xbundle
*xbundle
;
1182 ovs_assert(new_xcfg
);
1184 xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
1186 xbundle
= xzalloc(sizeof *xbundle
);
1187 xbundle
->ofbundle
= ofbundle
;
1188 xbundle
->xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
1190 xlate_xbundle_init(new_xcfg
, xbundle
);
1193 free(xbundle
->name
);
1194 xbundle
->name
= xstrdup(name
);
1196 xlate_xbundle_set(xbundle
, vlan_mode
, qinq_ethtype
, vlan
, trunks
, cvlans
,
1197 use_priority_tags
, bond
, lacp
, floodable
, protected);
1201 xlate_xbundle_remove(struct xlate_cfg
*xcfg
, struct xbundle
*xbundle
)
1203 struct xport
*xport
;
1209 LIST_FOR_EACH_POP (xport
, bundle_node
, &xbundle
->xports
) {
1210 xport
->xbundle
= NULL
;
1213 hmap_remove(&xcfg
->xbundles
, &xbundle
->hmap_node
);
1214 ovs_list_remove(&xbundle
->list_node
);
1215 bond_unref(xbundle
->bond
);
1216 lacp_unref(xbundle
->lacp
);
1217 free(xbundle
->name
);
1222 xlate_bundle_remove(struct ofbundle
*ofbundle
)
1224 struct xbundle
*xbundle
;
1226 ovs_assert(new_xcfg
);
1228 xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
1229 xlate_xbundle_remove(new_xcfg
, xbundle
);
1233 xlate_ofport_set(struct ofproto_dpif
*ofproto
, struct ofbundle
*ofbundle
,
1234 struct ofport_dpif
*ofport
, ofp_port_t ofp_port
,
1235 odp_port_t odp_port
, const struct netdev
*netdev
,
1236 const struct cfm
*cfm
, const struct bfd
*bfd
,
1237 const struct lldp
*lldp
, struct ofport_dpif
*peer
,
1238 int stp_port_no
, const struct rstp_port
*rstp_port
,
1239 const struct ofproto_port_queue
*qdscp_list
, size_t n_qdscp
,
1240 enum ofputil_port_config config
,
1241 enum ofputil_port_state state
, bool is_tunnel
,
1245 struct xport
*xport
;
1247 ovs_assert(new_xcfg
);
1249 xport
= xport_lookup(new_xcfg
, ofport
);
1251 xport
= xzalloc(sizeof *xport
);
1252 xport
->ofport
= ofport
;
1253 xport
->xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
1254 xport
->ofp_port
= ofp_port
;
1256 xlate_xport_init(new_xcfg
, xport
);
1259 ovs_assert(xport
->ofp_port
== ofp_port
);
1261 xlate_xport_set(xport
, odp_port
, netdev
, cfm
, bfd
, lldp
,
1262 stp_port_no
, rstp_port
, config
, state
, is_tunnel
,
1266 xport
->peer
->peer
= NULL
;
1268 xport
->peer
= xport_lookup(new_xcfg
, peer
);
1270 xport
->peer
->peer
= xport
;
1273 if (xport
->xbundle
) {
1274 ovs_list_remove(&xport
->bundle_node
);
1276 xport
->xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
1277 if (xport
->xbundle
) {
1278 ovs_list_insert(&xport
->xbundle
->xports
, &xport
->bundle_node
);
1281 clear_skb_priorities(xport
);
1282 for (i
= 0; i
< n_qdscp
; i
++) {
1283 struct skb_priority_to_dscp
*pdscp
;
1284 uint32_t skb_priority
;
1286 if (dpif_queue_to_priority(xport
->xbridge
->dpif
, qdscp_list
[i
].queue
,
1291 pdscp
= xmalloc(sizeof *pdscp
);
1292 pdscp
->skb_priority
= skb_priority
;
1293 pdscp
->dscp
= (qdscp_list
[i
].dscp
<< 2) & IP_DSCP_MASK
;
1294 hmap_insert(&xport
->skb_priorities
, &pdscp
->hmap_node
,
1295 hash_int(pdscp
->skb_priority
, 0));
1300 xlate_xport_remove(struct xlate_cfg
*xcfg
, struct xport
*xport
)
1307 xport
->peer
->peer
= NULL
;
1311 if (xport
->xbundle
) {
1312 ovs_list_remove(&xport
->bundle_node
);
1315 clear_skb_priorities(xport
);
1316 hmap_destroy(&xport
->skb_priorities
);
1318 hmap_remove(&xcfg
->xports
, &xport
->hmap_node
);
1319 hmap_remove(&xport
->xbridge
->xports
, &xport
->ofp_node
);
1321 netdev_close(xport
->netdev
);
1322 rstp_port_unref(xport
->rstp_port
);
1323 cfm_unref(xport
->cfm
);
1324 bfd_unref(xport
->bfd
);
1325 lldp_unref(xport
->lldp
);
1330 xlate_ofport_remove(struct ofport_dpif
*ofport
)
1332 struct xport
*xport
;
1334 ovs_assert(new_xcfg
);
1336 xport
= xport_lookup(new_xcfg
, ofport
);
1337 xlate_xport_remove(new_xcfg
, xport
);
1340 static struct ofproto_dpif
*
1341 xlate_lookup_ofproto_(const struct dpif_backer
*backer
, const struct flow
*flow
,
1342 ofp_port_t
*ofp_in_port
, const struct xport
**xportp
)
1344 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1345 const struct xport
*xport
;
1347 xport
= xport_lookup(xcfg
, tnl_port_should_receive(flow
)
1348 ? tnl_port_receive(flow
)
1349 : odp_port_to_ofport(backer
, flow
->in_port
.odp_port
));
1350 if (OVS_UNLIKELY(!xport
)) {
1355 *ofp_in_port
= xport
->ofp_port
;
1357 return xport
->xbridge
->ofproto
;
1360 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1361 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1362 struct ofproto_dpif
*
1363 xlate_lookup_ofproto(const struct dpif_backer
*backer
, const struct flow
*flow
,
1364 ofp_port_t
*ofp_in_port
)
1366 const struct xport
*xport
;
1368 return xlate_lookup_ofproto_(backer
, flow
, ofp_in_port
, &xport
);
1371 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1372 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
1373 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1374 * handles for those protocols if they're enabled. Caller may use the returned
1375 * pointers until quiescing, for longer term use additional references must
1378 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1381 xlate_lookup(const struct dpif_backer
*backer
, const struct flow
*flow
,
1382 struct ofproto_dpif
**ofprotop
, struct dpif_ipfix
**ipfix
,
1383 struct dpif_sflow
**sflow
, struct netflow
**netflow
,
1384 ofp_port_t
*ofp_in_port
)
1386 struct ofproto_dpif
*ofproto
;
1387 const struct xport
*xport
;
1389 ofproto
= xlate_lookup_ofproto_(backer
, flow
, ofp_in_port
, &xport
);
1396 *ofprotop
= ofproto
;
1400 *ipfix
= xport
? xport
->xbridge
->ipfix
: NULL
;
1404 *sflow
= xport
? xport
->xbridge
->sflow
: NULL
;
1408 *netflow
= xport
? xport
->xbridge
->netflow
: NULL
;
1414 static struct xbridge
*
1415 xbridge_lookup(struct xlate_cfg
*xcfg
, const struct ofproto_dpif
*ofproto
)
1417 struct hmap
*xbridges
;
1418 struct xbridge
*xbridge
;
1420 if (!ofproto
|| !xcfg
) {
1424 xbridges
= &xcfg
->xbridges
;
1426 HMAP_FOR_EACH_IN_BUCKET (xbridge
, hmap_node
, hash_pointer(ofproto
, 0),
1428 if (xbridge
->ofproto
== ofproto
) {
1435 static struct xbridge
*
1436 xbridge_lookup_by_uuid(struct xlate_cfg
*xcfg
, const struct uuid
*uuid
)
1438 struct xbridge
*xbridge
;
1440 HMAP_FOR_EACH (xbridge
, hmap_node
, &xcfg
->xbridges
) {
1441 if (uuid_equals(&xbridge
->ofproto
->uuid
, uuid
)) {
1448 static struct xbundle
*
1449 xbundle_lookup(struct xlate_cfg
*xcfg
, const struct ofbundle
*ofbundle
)
1451 struct hmap
*xbundles
;
1452 struct xbundle
*xbundle
;
1454 if (!ofbundle
|| !xcfg
) {
1458 xbundles
= &xcfg
->xbundles
;
1460 HMAP_FOR_EACH_IN_BUCKET (xbundle
, hmap_node
, hash_pointer(ofbundle
, 0),
1462 if (xbundle
->ofbundle
== ofbundle
) {
1469 static struct xport
*
1470 xport_lookup(struct xlate_cfg
*xcfg
, const struct ofport_dpif
*ofport
)
1472 struct hmap
*xports
;
1473 struct xport
*xport
;
1475 if (!ofport
|| !xcfg
) {
1479 xports
= &xcfg
->xports
;
1481 HMAP_FOR_EACH_IN_BUCKET (xport
, hmap_node
, hash_pointer(ofport
, 0),
1483 if (xport
->ofport
== ofport
) {
1490 static struct stp_port
*
1491 xport_get_stp_port(const struct xport
*xport
)
1493 return xport
->xbridge
->stp
&& xport
->stp_port_no
!= -1
1494 ? stp_get_port(xport
->xbridge
->stp
, xport
->stp_port_no
)
1499 xport_stp_learn_state(const struct xport
*xport
)
1501 struct stp_port
*sp
= xport_get_stp_port(xport
);
1503 ? stp_learn_in_state(stp_port_get_state(sp
))
1508 xport_stp_forward_state(const struct xport
*xport
)
1510 struct stp_port
*sp
= xport_get_stp_port(xport
);
1512 ? stp_forward_in_state(stp_port_get_state(sp
))
1517 xport_stp_should_forward_bpdu(const struct xport
*xport
)
1519 struct stp_port
*sp
= xport_get_stp_port(xport
);
1520 return stp_should_forward_bpdu(sp
? stp_port_get_state(sp
) : STP_DISABLED
);
1523 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1524 * were used to make the determination.*/
1526 stp_should_process_flow(const struct flow
*flow
, struct flow_wildcards
*wc
)
1528 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1529 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
1530 return is_stp(flow
);
1534 stp_process_packet(const struct xport
*xport
, const struct dp_packet
*packet
)
1536 struct stp_port
*sp
= xport_get_stp_port(xport
);
1537 struct dp_packet payload
= *packet
;
1538 struct eth_header
*eth
= dp_packet_data(&payload
);
1540 /* Sink packets on ports that have STP disabled when the bridge has
1542 if (!sp
|| stp_port_get_state(sp
) == STP_DISABLED
) {
1546 /* Trim off padding on payload. */
1547 if (dp_packet_size(&payload
) > ntohs(eth
->eth_type
) + ETH_HEADER_LEN
) {
1548 dp_packet_set_size(&payload
, ntohs(eth
->eth_type
) + ETH_HEADER_LEN
);
1551 if (dp_packet_try_pull(&payload
, ETH_HEADER_LEN
+ LLC_HEADER_LEN
)) {
1552 stp_received_bpdu(sp
, dp_packet_data(&payload
), dp_packet_size(&payload
));
1556 static enum rstp_state
1557 xport_get_rstp_port_state(const struct xport
*xport
)
1559 return xport
->rstp_port
1560 ? rstp_port_get_state(xport
->rstp_port
)
1565 xport_rstp_learn_state(const struct xport
*xport
)
1567 return xport
->xbridge
->rstp
&& xport
->rstp_port
1568 ? rstp_learn_in_state(xport_get_rstp_port_state(xport
))
1573 xport_rstp_forward_state(const struct xport
*xport
)
1575 return xport
->xbridge
->rstp
&& xport
->rstp_port
1576 ? rstp_forward_in_state(xport_get_rstp_port_state(xport
))
1581 xport_rstp_should_manage_bpdu(const struct xport
*xport
)
1583 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport
));
1587 rstp_process_packet(const struct xport
*xport
, const struct dp_packet
*packet
)
1589 struct dp_packet payload
= *packet
;
1590 struct eth_header
*eth
= dp_packet_data(&payload
);
1592 /* Sink packets on ports that have no RSTP. */
1593 if (!xport
->rstp_port
) {
1597 /* Trim off padding on payload. */
1598 if (dp_packet_size(&payload
) > ntohs(eth
->eth_type
) + ETH_HEADER_LEN
) {
1599 dp_packet_set_size(&payload
, ntohs(eth
->eth_type
) + ETH_HEADER_LEN
);
1602 if (dp_packet_try_pull(&payload
, ETH_HEADER_LEN
+ LLC_HEADER_LEN
)) {
1603 rstp_port_received_bpdu(xport
->rstp_port
, dp_packet_data(&payload
),
1604 dp_packet_size(&payload
));
1608 static struct xport
*
1609 get_ofp_port(const struct xbridge
*xbridge
, ofp_port_t ofp_port
)
1611 struct xport
*xport
;
1613 HMAP_FOR_EACH_IN_BUCKET (xport
, ofp_node
, hash_ofp_port(ofp_port
),
1615 if (xport
->ofp_port
== ofp_port
) {
1623 ofp_port_to_odp_port(const struct xbridge
*xbridge
, ofp_port_t ofp_port
)
1625 const struct xport
*xport
= get_ofp_port(xbridge
, ofp_port
);
1626 return xport
? xport
->odp_port
: ODPP_NONE
;
1630 odp_port_is_alive(const struct xlate_ctx
*ctx
, ofp_port_t ofp_port
)
1632 struct xport
*xport
= get_ofp_port(ctx
->xbridge
, ofp_port
);
1633 return xport
&& xport
->may_enable
;
1636 static struct ofputil_bucket
*
1637 group_first_live_bucket(const struct xlate_ctx
*, const struct group_dpif
*,
1641 group_is_alive(const struct xlate_ctx
*ctx
, uint32_t group_id
, int depth
)
1643 struct group_dpif
*group
;
1645 group
= group_dpif_lookup(ctx
->xbridge
->ofproto
, group_id
,
1646 ctx
->xin
->tables_version
, false);
1648 return group_first_live_bucket(ctx
, group
, depth
) != NULL
;
1654 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1657 bucket_is_alive(const struct xlate_ctx
*ctx
,
1658 struct ofputil_bucket
*bucket
, int depth
)
1660 if (depth
>= MAX_LIVENESS_RECURSION
) {
1661 xlate_report_error(ctx
, "bucket chaining exceeded %d links",
1662 MAX_LIVENESS_RECURSION
);
1666 return (!ofputil_bucket_has_liveness(bucket
)
1667 || (bucket
->watch_port
!= OFPP_ANY
1668 && odp_port_is_alive(ctx
, bucket
->watch_port
))
1669 || (bucket
->watch_group
!= OFPG_ANY
1670 && group_is_alive(ctx
, bucket
->watch_group
, depth
+ 1)));
1673 static struct ofputil_bucket
*
1674 group_first_live_bucket(const struct xlate_ctx
*ctx
,
1675 const struct group_dpif
*group
, int depth
)
1677 struct ofputil_bucket
*bucket
;
1678 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
1679 if (bucket_is_alive(ctx
, bucket
, depth
)) {
1687 static struct ofputil_bucket
*
1688 group_best_live_bucket(const struct xlate_ctx
*ctx
,
1689 const struct group_dpif
*group
,
1692 struct ofputil_bucket
*best_bucket
= NULL
;
1693 uint32_t best_score
= 0;
1695 struct ofputil_bucket
*bucket
;
1696 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
1697 if (bucket_is_alive(ctx
, bucket
, 0)) {
1699 (hash_int(bucket
->bucket_id
, basis
) & 0xffff) * bucket
->weight
;
1700 if (score
>= best_score
) {
1701 best_bucket
= bucket
;
1711 xbundle_trunks_vlan(const struct xbundle
*bundle
, uint16_t vlan
)
1713 return (bundle
->vlan_mode
!= PORT_VLAN_ACCESS
1714 && (!bundle
->trunks
|| bitmap_is_set(bundle
->trunks
, vlan
)));
1718 xbundle_allows_cvlan(const struct xbundle
*bundle
, uint16_t vlan
)
1720 return (!bundle
->cvlans
|| bitmap_is_set(bundle
->cvlans
, vlan
));
1724 xbundle_includes_vlan(const struct xbundle
*xbundle
, const struct xvlan
*xvlan
)
1726 switch (xbundle
->vlan_mode
) {
1727 case PORT_VLAN_ACCESS
:
1728 return xvlan
->v
[0].vid
== xbundle
->vlan
&& xvlan
->v
[1].vid
== 0;
1730 case PORT_VLAN_TRUNK
:
1731 case PORT_VLAN_NATIVE_UNTAGGED
:
1732 case PORT_VLAN_NATIVE_TAGGED
:
1733 return xbundle_trunks_vlan(xbundle
, xvlan
->v
[0].vid
);
1735 case PORT_VLAN_DOT1Q_TUNNEL
:
1736 return xvlan
->v
[0].vid
== xbundle
->vlan
&&
1737 xbundle_allows_cvlan(xbundle
, xvlan
->v
[1].vid
);
1744 static mirror_mask_t
1745 xbundle_mirror_out(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1747 return xbundle
!= &ofpp_none_bundle
1748 ? mirror_bundle_out(xbridge
->mbridge
, xbundle
->ofbundle
)
1752 static mirror_mask_t
1753 xbundle_mirror_src(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1755 return xbundle
!= &ofpp_none_bundle
1756 ? mirror_bundle_src(xbridge
->mbridge
, xbundle
->ofbundle
)
1760 static mirror_mask_t
1761 xbundle_mirror_dst(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1763 return xbundle
!= &ofpp_none_bundle
1764 ? mirror_bundle_dst(xbridge
->mbridge
, xbundle
->ofbundle
)
1768 static struct xbundle
*
1769 lookup_input_bundle__(const struct xbridge
*xbridge
,
1770 ofp_port_t in_port
, struct xport
**in_xportp
)
1772 struct xport
*xport
;
1774 /* Find the port and bundle for the received packet. */
1775 xport
= get_ofp_port(xbridge
, in_port
);
1779 if (xport
&& xport
->xbundle
) {
1780 return xport
->xbundle
;
1783 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1784 * which a controller may use as the ingress port for traffic that
1785 * it is sourcing. */
1786 if (in_port
== OFPP_CONTROLLER
|| in_port
== OFPP_NONE
) {
1787 return &ofpp_none_bundle
;
1792 static struct xbundle
*
1793 lookup_input_bundle(const struct xlate_ctx
*ctx
,
1794 ofp_port_t in_port
, struct xport
**in_xportp
)
1796 struct xbundle
*xbundle
= lookup_input_bundle__(ctx
->xbridge
,
1797 in_port
, in_xportp
);
1799 /* Odd. A few possible reasons here:
1801 * - We deleted a port but there are still a few packets queued up
1804 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1805 * we don't know about.
1807 * - The ofproto client didn't configure the port as part of a bundle.
1808 * This is particularly likely to happen if a packet was received on
1809 * the port after it was created, but before the client had a chance
1810 * to configure its bundle.
1812 xlate_report_error(ctx
, "received packet on unknown port %"PRIu32
,
1818 /* Mirrors the packet represented by 'ctx' to appropriate mirror destinations,
1819 * given the packet is ingressing or egressing on 'xbundle', which has ingress
1820 * or egress (as appropriate) mirrors 'mirrors'. */
1822 mirror_packet(struct xlate_ctx
*ctx
, struct xbundle
*xbundle
,
1823 mirror_mask_t mirrors
)
1825 struct xvlan in_xvlan
;
1828 /* Figure out what VLAN the packet is in (because mirrors can select
1829 * packets on basis of VLAN). */
1830 xvlan_extract(&ctx
->xin
->flow
, &in_xvlan
);
1831 if (!input_vid_is_valid(ctx
, in_xvlan
.v
[0].vid
, xbundle
)) {
1834 xvlan_input_translate(xbundle
, &in_xvlan
, &xvlan
);
1836 const struct xbridge
*xbridge
= ctx
->xbridge
;
1838 /* Don't mirror to destinations that we've already mirrored to. */
1839 mirrors
&= ~ctx
->mirrors
;
1844 if (ctx
->xin
->resubmit_stats
) {
1845 mirror_update_stats(xbridge
->mbridge
, mirrors
,
1846 ctx
->xin
->resubmit_stats
->n_packets
,
1847 ctx
->xin
->resubmit_stats
->n_bytes
);
1849 if (ctx
->xin
->xcache
) {
1850 struct xc_entry
*entry
;
1852 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_MIRROR
);
1853 entry
->mirror
.mbridge
= mbridge_ref(xbridge
->mbridge
);
1854 entry
->mirror
.mirrors
= mirrors
;
1857 /* 'mirrors' is a bit-mask of candidates for mirroring. Iterate as long as
1858 * some candidates remain. */
1860 const unsigned long *vlans
;
1861 mirror_mask_t dup_mirrors
;
1862 struct ofbundle
*out
;
1866 /* Get the details of the mirror represented by the rightmost 1-bit. */
1867 bool has_mirror
= mirror_get(xbridge
->mbridge
, raw_ctz(mirrors
),
1868 &vlans
, &dup_mirrors
,
1869 &out
, &snaplen
, &out_vlan
);
1870 ovs_assert(has_mirror
);
1873 /* If this mirror selects on the basis of VLAN, and it does not select
1874 * 'vlan', then discard this mirror and go on to the next one. */
1876 ctx
->wc
->masks
.vlans
[0].tci
|= htons(VLAN_CFI
| VLAN_VID_MASK
);
1878 if (vlans
&& !bitmap_is_set(vlans
, xvlan
.v
[0].vid
)) {
1879 mirrors
= zero_rightmost_1bit(mirrors
);
1883 /* Record the mirror, and the mirrors that output to the same
1884 * destination, so that we don't mirror to them again. This must be
1885 * done now to ensure that output_normal(), below, doesn't recursively
1886 * output to the same mirrors. */
1887 ctx
->mirrors
|= dup_mirrors
;
1888 ctx
->mirror_snaplen
= snaplen
;
1890 /* Send the packet to the mirror. */
1892 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1893 struct xbundle
*out_xbundle
= xbundle_lookup(xcfg
, out
);
1895 output_normal(ctx
, out_xbundle
, &xvlan
);
1897 } else if (xvlan
.v
[0].vid
!= out_vlan
1898 && !eth_addr_is_reserved(ctx
->xin
->flow
.dl_dst
)) {
1899 struct xbundle
*xbundle
;
1900 uint16_t old_vid
= xvlan
.v
[0].vid
;
1902 xvlan
.v
[0].vid
= out_vlan
;
1903 LIST_FOR_EACH (xbundle
, list_node
, &xbridge
->xbundles
) {
1904 if (xbundle_includes_vlan(xbundle
, &xvlan
)
1905 && !xbundle_mirror_out(xbridge
, xbundle
)) {
1906 output_normal(ctx
, xbundle
, &xvlan
);
1909 xvlan
.v
[0].vid
= old_vid
;
1912 /* output_normal() could have recursively output (to different
1913 * mirrors), so make sure that we don't send duplicates. */
1914 mirrors
&= ~ctx
->mirrors
;
1915 ctx
->mirror_snaplen
= 0;
1920 mirror_ingress_packet(struct xlate_ctx
*ctx
)
1922 if (mbridge_has_mirrors(ctx
->xbridge
->mbridge
)) {
1923 struct xbundle
*xbundle
= lookup_input_bundle(
1924 ctx
, ctx
->xin
->flow
.in_port
.ofp_port
, NULL
);
1926 mirror_packet(ctx
, xbundle
,
1927 xbundle_mirror_src(ctx
->xbridge
, xbundle
));
1932 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1933 * If so, returns true. Otherwise, returns false.
1935 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1936 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1939 input_vid_is_valid(const struct xlate_ctx
*ctx
,
1940 uint16_t vid
, struct xbundle
*in_xbundle
)
1942 /* Allow any VID on the OFPP_NONE port. */
1943 if (in_xbundle
== &ofpp_none_bundle
) {
1947 switch (in_xbundle
->vlan_mode
) {
1948 case PORT_VLAN_ACCESS
:
1950 xlate_report_error(ctx
, "dropping VLAN %"PRIu16
" tagged "
1951 "packet received on port %s configured as VLAN "
1952 "%d access port", vid
, in_xbundle
->name
,
1958 case PORT_VLAN_NATIVE_UNTAGGED
:
1959 case PORT_VLAN_NATIVE_TAGGED
:
1961 /* Port must always carry its native VLAN. */
1965 case PORT_VLAN_TRUNK
:
1966 if (!xbundle_trunks_vlan(in_xbundle
, vid
)) {
1967 xlate_report_error(ctx
, "dropping VLAN %"PRIu16
" packet "
1968 "received on port %s not configured for "
1969 "trunking VLAN %"PRIu16
,
1970 vid
, in_xbundle
->name
, vid
);
1975 case PORT_VLAN_DOT1Q_TUNNEL
:
1976 if (!xbundle_allows_cvlan(in_xbundle
, vid
)) {
1977 xlate_report_error(ctx
, "dropping VLAN %"PRIu16
" packet received "
1978 "on dot1q-tunnel port %s that excludes this "
1979 "VLAN", vid
, in_xbundle
->name
);
1991 xvlan_copy(struct xvlan
*dst
, const struct xvlan
*src
)
1997 xvlan_pop(struct xvlan
*src
)
1999 memmove(&src
->v
[0], &src
->v
[1], sizeof(src
->v
) - sizeof(src
->v
[0]));
2000 memset(&src
->v
[FLOW_MAX_VLAN_HEADERS
- 1], 0,
2001 sizeof(src
->v
[FLOW_MAX_VLAN_HEADERS
- 1]));
2005 xvlan_push_uninit(struct xvlan
*src
)
2007 memmove(&src
->v
[1], &src
->v
[0], sizeof(src
->v
) - sizeof(src
->v
[0]));
2008 memset(&src
->v
[0], 0, sizeof(src
->v
[0]));
2011 /* Extract VLAN information (headers) from flow */
2013 xvlan_extract(const struct flow
*flow
, struct xvlan
*xvlan
)
2016 memset(xvlan
, 0, sizeof(*xvlan
));
2017 for (i
= 0; i
< FLOW_MAX_VLAN_HEADERS
; i
++) {
2018 if (!eth_type_vlan(flow
->vlans
[i
].tpid
) ||
2019 !(flow
->vlans
[i
].tci
& htons(VLAN_CFI
))) {
2022 xvlan
->v
[i
].tpid
= ntohs(flow
->vlans
[i
].tpid
);
2023 xvlan
->v
[i
].vid
= vlan_tci_to_vid(flow
->vlans
[i
].tci
);
2024 xvlan
->v
[i
].pcp
= ntohs(flow
->vlans
[i
].tci
) & VLAN_PCP_MASK
;
2028 /* Put VLAN information (headers) to flow */
2030 xvlan_put(struct flow
*flow
, const struct xvlan
*xvlan
)
2034 for (i
= 0; i
< FLOW_MAX_VLAN_HEADERS
; i
++) {
2035 tci
= htons(xvlan
->v
[i
].vid
| (xvlan
->v
[i
].pcp
& VLAN_PCP_MASK
));
2037 tci
|= htons(VLAN_CFI
);
2038 flow
->vlans
[i
].tpid
= xvlan
->v
[i
].tpid
?
2039 htons(xvlan
->v
[i
].tpid
) :
2040 htons(ETH_TYPE_VLAN_8021Q
);
2042 flow
->vlans
[i
].tci
= tci
;
2046 /* Given 'in_xvlan', extracted from the input 802.1Q headers received as part
2047 * of a packet, and 'in_xbundle', the bundle on which the packet was received,
2048 * returns the VLANs of the packet during bridge internal processing. */
2050 xvlan_input_translate(const struct xbundle
*in_xbundle
,
2051 const struct xvlan
*in_xvlan
, struct xvlan
*xvlan
)
2054 switch (in_xbundle
->vlan_mode
) {
2055 case PORT_VLAN_ACCESS
:
2056 memset(xvlan
, 0, sizeof(*xvlan
));
2057 xvlan
->v
[0].tpid
= in_xvlan
->v
[0].tpid
? in_xvlan
->v
[0].tpid
:
2058 ETH_TYPE_VLAN_8021Q
;
2059 xvlan
->v
[0].vid
= in_xbundle
->vlan
;
2060 xvlan
->v
[0].pcp
= in_xvlan
->v
[0].pcp
;
2063 case PORT_VLAN_TRUNK
:
2064 xvlan_copy(xvlan
, in_xvlan
);
2067 case PORT_VLAN_NATIVE_UNTAGGED
:
2068 case PORT_VLAN_NATIVE_TAGGED
:
2069 xvlan_copy(xvlan
, in_xvlan
);
2070 if (!in_xvlan
->v
[0].vid
) {
2071 xvlan
->v
[0].tpid
= in_xvlan
->v
[0].tpid
? in_xvlan
->v
[0].tpid
:
2072 ETH_TYPE_VLAN_8021Q
;
2073 xvlan
->v
[0].vid
= in_xbundle
->vlan
;
2074 xvlan
->v
[0].pcp
= in_xvlan
->v
[0].pcp
;
2078 case PORT_VLAN_DOT1Q_TUNNEL
:
2079 xvlan_copy(xvlan
, in_xvlan
);
2080 xvlan_push_uninit(xvlan
);
2081 xvlan
->v
[0].tpid
= in_xbundle
->qinq_ethtype
;
2082 xvlan
->v
[0].vid
= in_xbundle
->vlan
;
2083 xvlan
->v
[0].pcp
= 0;
2091 /* Given 'xvlan', the VLANs of a packet during internal processing, and
2092 * 'out_xbundle', a bundle on which the packet is to be output, returns the
2093 * VLANs that should be included in output packet. */
2095 xvlan_output_translate(const struct xbundle
*out_xbundle
,
2096 const struct xvlan
*xvlan
, struct xvlan
*out_xvlan
)
2098 switch (out_xbundle
->vlan_mode
) {
2099 case PORT_VLAN_ACCESS
:
2100 memset(out_xvlan
, 0, sizeof(*out_xvlan
));
2103 case PORT_VLAN_TRUNK
:
2104 case PORT_VLAN_NATIVE_TAGGED
:
2105 xvlan_copy(out_xvlan
, xvlan
);
2108 case PORT_VLAN_NATIVE_UNTAGGED
:
2109 xvlan_copy(out_xvlan
, xvlan
);
2110 if (xvlan
->v
[0].vid
== out_xbundle
->vlan
) {
2111 xvlan_pop(out_xvlan
);
2115 case PORT_VLAN_DOT1Q_TUNNEL
:
2116 xvlan_copy(out_xvlan
, xvlan
);
2117 xvlan_pop(out_xvlan
);
2125 /* If output xbundle is dot1q-tunnel, set mask bits of cvlan */
2127 check_and_set_cvlan_mask(struct flow_wildcards
*wc
,
2128 const struct xbundle
*xbundle
)
2130 if (xbundle
->vlan_mode
== PORT_VLAN_DOT1Q_TUNNEL
&& xbundle
->cvlans
) {
2131 wc
->masks
.vlans
[1].tci
= htons(0xffff);
2136 output_normal(struct xlate_ctx
*ctx
, const struct xbundle
*out_xbundle
,
2137 const struct xvlan
*xvlan
)
2140 union flow_vlan_hdr old_vlans
[FLOW_MAX_VLAN_HEADERS
];
2141 struct xport
*xport
;
2142 struct xlate_bond_recirc xr
;
2143 bool use_recirc
= false;
2144 struct xvlan out_xvlan
;
2146 check_and_set_cvlan_mask(ctx
->wc
, out_xbundle
);
2148 xvlan_output_translate(out_xbundle
, xvlan
, &out_xvlan
);
2149 if (out_xbundle
->use_priority_tags
) {
2150 out_xvlan
.v
[0].pcp
= ntohs(ctx
->xin
->flow
.vlans
[0].tci
) &
2153 vid
= out_xvlan
.v
[0].vid
;
2154 if (ovs_list_is_empty(&out_xbundle
->xports
)) {
2155 /* Partially configured bundle with no slaves. Drop the packet. */
2157 } else if (!out_xbundle
->bond
) {
2158 xport
= CONTAINER_OF(ovs_list_front(&out_xbundle
->xports
), struct xport
,
2161 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2162 struct flow_wildcards
*wc
= ctx
->wc
;
2163 struct ofport_dpif
*ofport
;
2165 if (ctx
->xbridge
->support
.odp
.recirc
) {
2166 /* In case recirculation is not actually in use, 'xr.recirc_id'
2167 * will be set to '0', since a valid 'recirc_id' can
2169 bond_update_post_recirc_rules(out_xbundle
->bond
,
2173 /* Use recirculation instead of output. */
2175 xr
.hash_alg
= OVS_HASH_ALG_L4
;
2176 /* Recirculation does not require unmasking hash fields. */
2181 ofport
= bond_choose_output_slave(out_xbundle
->bond
,
2182 &ctx
->xin
->flow
, wc
, vid
);
2183 xport
= xport_lookup(xcfg
, ofport
);
2186 /* No slaves enabled, so drop packet. */
2190 /* If use_recirc is set, the main thread will handle stats
2191 * accounting for this bond. */
2193 if (ctx
->xin
->resubmit_stats
) {
2194 bond_account(out_xbundle
->bond
, &ctx
->xin
->flow
, vid
,
2195 ctx
->xin
->resubmit_stats
->n_bytes
);
2197 if (ctx
->xin
->xcache
) {
2198 struct xc_entry
*entry
;
2201 flow
= &ctx
->xin
->flow
;
2202 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_BOND
);
2203 entry
->bond
.bond
= bond_ref(out_xbundle
->bond
);
2204 entry
->bond
.flow
= xmemdup(flow
, sizeof *flow
);
2205 entry
->bond
.vid
= vid
;
2210 memcpy(&old_vlans
, &ctx
->xin
->flow
.vlans
, sizeof(old_vlans
));
2211 xvlan_put(&ctx
->xin
->flow
, &out_xvlan
);
2213 compose_output_action(ctx
, xport
->ofp_port
, use_recirc
? &xr
: NULL
);
2214 memcpy(&ctx
->xin
->flow
.vlans
, &old_vlans
, sizeof(old_vlans
));
2217 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
2218 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
2219 * indicate this; newer upstream kernels use gratuitous ARP requests. */
2221 is_gratuitous_arp(const struct flow
*flow
, struct flow_wildcards
*wc
)
2223 if (flow
->dl_type
!= htons(ETH_TYPE_ARP
)) {
2227 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
2228 if (!eth_addr_is_broadcast(flow
->dl_dst
)) {
2232 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
2233 if (flow
->nw_proto
== ARP_OP_REPLY
) {
2235 } else if (flow
->nw_proto
== ARP_OP_REQUEST
) {
2236 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
2237 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
2239 return flow
->nw_src
== flow
->nw_dst
;
2245 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
2246 * dropped. Returns true if they may be forwarded, false if they should be
2249 * 'in_port' must be the xport that corresponds to flow->in_port.
2250 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
2252 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
2253 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
2254 * checked by input_vid_is_valid().
2256 * May also add tags to '*tags', although the current implementation only does
2257 * so in one special case.
2260 is_admissible(struct xlate_ctx
*ctx
, struct xport
*in_port
,
2263 struct xbundle
*in_xbundle
= in_port
->xbundle
;
2264 const struct xbridge
*xbridge
= ctx
->xbridge
;
2265 struct flow
*flow
= &ctx
->xin
->flow
;
2267 /* Drop frames for reserved multicast addresses
2268 * only if forward_bpdu option is absent. */
2269 if (!xbridge
->forward_bpdu
&& eth_addr_is_reserved(flow
->dl_dst
)) {
2270 xlate_report(ctx
, OFT_DETAIL
,
2271 "packet has reserved destination MAC, dropping");
2275 if (in_xbundle
->bond
) {
2276 struct mac_entry
*mac
;
2278 switch (bond_check_admissibility(in_xbundle
->bond
, in_port
->ofport
,
2284 xlate_report(ctx
, OFT_DETAIL
,
2285 "bonding refused admissibility, dropping");
2288 case BV_DROP_IF_MOVED
:
2289 ovs_rwlock_rdlock(&xbridge
->ml
->rwlock
);
2290 mac
= mac_learning_lookup(xbridge
->ml
, flow
->dl_src
, vlan
);
2292 && mac_entry_get_port(xbridge
->ml
, mac
) != in_xbundle
->ofbundle
2293 && (!is_gratuitous_arp(flow
, ctx
->wc
)
2294 || mac_entry_is_grat_arp_locked(mac
))) {
2295 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
2296 xlate_report(ctx
, OFT_DETAIL
,
2297 "SLB bond thinks this packet looped back, "
2301 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
2310 update_learning_table__(const struct xbridge
*xbridge
,
2311 struct xbundle
*in_xbundle
, struct eth_addr dl_src
,
2312 int vlan
, bool is_grat_arp
)
2314 return (in_xbundle
== &ofpp_none_bundle
2315 || !mac_learning_update(xbridge
->ml
, dl_src
, vlan
,
2317 in_xbundle
->bond
!= NULL
,
2318 in_xbundle
->ofbundle
));
2322 update_learning_table(const struct xlate_ctx
*ctx
,
2323 struct xbundle
*in_xbundle
, struct eth_addr dl_src
,
2324 int vlan
, bool is_grat_arp
)
2326 if (!update_learning_table__(ctx
->xbridge
, in_xbundle
, dl_src
, vlan
,
2328 xlate_report_debug(ctx
, OFT_DETAIL
, "learned that "ETH_ADDR_FMT
" is "
2329 "on port %s in VLAN %d",
2330 ETH_ADDR_ARGS(dl_src
), in_xbundle
->name
, vlan
);
2334 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2335 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
2337 update_mcast_snooping_table4__(const struct xlate_ctx
*ctx
,
2338 const struct flow
*flow
,
2339 struct mcast_snooping
*ms
, int vlan
,
2340 struct xbundle
*in_xbundle
,
2341 const struct dp_packet
*packet
)
2342 OVS_REQ_WRLOCK(ms
->rwlock
)
2344 const struct igmp_header
*igmp
;
2347 ovs_be32 ip4
= flow
->igmp_group_ip4
;
2349 offset
= (char *) dp_packet_l4(packet
) - (char *) dp_packet_data(packet
);
2350 igmp
= dp_packet_at(packet
, offset
, IGMP_HEADER_LEN
);
2351 if (!igmp
|| csum(igmp
, dp_packet_l4_size(packet
)) != 0) {
2352 xlate_report_debug(ctx
, OFT_DETAIL
,
2353 "multicast snooping received bad IGMP "
2354 "checksum on port %s in VLAN %d",
2355 in_xbundle
->name
, vlan
);
2359 switch (ntohs(flow
->tp_src
)) {
2360 case IGMP_HOST_MEMBERSHIP_REPORT
:
2361 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
2362 if (mcast_snooping_add_group4(ms
, ip4
, vlan
, in_xbundle
->ofbundle
)) {
2363 xlate_report_debug(ctx
, OFT_DETAIL
,
2364 "multicast snooping learned that "
2365 IP_FMT
" is on port %s in VLAN %d",
2366 IP_ARGS(ip4
), in_xbundle
->name
, vlan
);
2369 case IGMP_HOST_LEAVE_MESSAGE
:
2370 if (mcast_snooping_leave_group4(ms
, ip4
, vlan
, in_xbundle
->ofbundle
)) {
2371 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping leaving "
2372 IP_FMT
" is on port %s in VLAN %d",
2373 IP_ARGS(ip4
), in_xbundle
->name
, vlan
);
2376 case IGMP_HOST_MEMBERSHIP_QUERY
:
2377 if (flow
->nw_src
&& mcast_snooping_add_mrouter(ms
, vlan
,
2378 in_xbundle
->ofbundle
)) {
2379 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping query "
2380 "from "IP_FMT
" is on port %s in VLAN %d",
2381 IP_ARGS(flow
->nw_src
), in_xbundle
->name
, vlan
);
2384 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
2385 count
= mcast_snooping_add_report(ms
, packet
, vlan
,
2386 in_xbundle
->ofbundle
);
2388 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping processed "
2389 "%d addresses on port %s in VLAN %d",
2390 count
, in_xbundle
->name
, vlan
);
2397 update_mcast_snooping_table6__(const struct xlate_ctx
*ctx
,
2398 const struct flow
*flow
,
2399 struct mcast_snooping
*ms
, int vlan
,
2400 struct xbundle
*in_xbundle
,
2401 const struct dp_packet
*packet
)
2402 OVS_REQ_WRLOCK(ms
->rwlock
)
2404 const struct mld_header
*mld
;
2408 offset
= (char *) dp_packet_l4(packet
) - (char *) dp_packet_data(packet
);
2409 mld
= dp_packet_at(packet
, offset
, MLD_HEADER_LEN
);
2412 packet_csum_upperlayer6(dp_packet_l3(packet
),
2413 mld
, IPPROTO_ICMPV6
,
2414 dp_packet_l4_size(packet
)) != 0) {
2415 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping received "
2416 "bad MLD checksum on port %s in VLAN %d",
2417 in_xbundle
->name
, vlan
);
2421 switch (ntohs(flow
->tp_src
)) {
2423 if (!ipv6_addr_equals(&flow
->ipv6_src
, &in6addr_any
)
2424 && mcast_snooping_add_mrouter(ms
, vlan
, in_xbundle
->ofbundle
)) {
2425 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping query on "
2426 "port %s in VLAN %d", in_xbundle
->name
, vlan
);
2432 count
= mcast_snooping_add_mld(ms
, packet
, vlan
, in_xbundle
->ofbundle
);
2434 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping processed "
2435 "%d addresses on port %s in VLAN %d",
2436 count
, in_xbundle
->name
, vlan
);
2442 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2443 * was received on 'in_xbundle' in 'vlan'. */
2445 update_mcast_snooping_table(const struct xlate_ctx
*ctx
,
2446 const struct flow
*flow
, int vlan
,
2447 struct xbundle
*in_xbundle
,
2448 const struct dp_packet
*packet
)
2450 struct mcast_snooping
*ms
= ctx
->xbridge
->ms
;
2451 struct xlate_cfg
*xcfg
;
2452 struct xbundle
*mcast_xbundle
;
2453 struct mcast_port_bundle
*fport
;
2455 /* Don't learn the OFPP_NONE port. */
2456 if (in_xbundle
== &ofpp_none_bundle
) {
2460 /* Don't learn from flood ports */
2461 mcast_xbundle
= NULL
;
2462 ovs_rwlock_wrlock(&ms
->rwlock
);
2463 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2464 LIST_FOR_EACH(fport
, node
, &ms
->fport_list
) {
2465 mcast_xbundle
= xbundle_lookup(xcfg
, fport
->port
);
2466 if (mcast_xbundle
== in_xbundle
) {
2471 if (!mcast_xbundle
|| mcast_xbundle
!= in_xbundle
) {
2472 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2473 update_mcast_snooping_table4__(ctx
, flow
, ms
, vlan
,
2474 in_xbundle
, packet
);
2476 update_mcast_snooping_table6__(ctx
, flow
, ms
, vlan
,
2477 in_xbundle
, packet
);
2480 ovs_rwlock_unlock(&ms
->rwlock
);
2483 /* send the packet to ports having the multicast group learned */
2485 xlate_normal_mcast_send_group(struct xlate_ctx
*ctx
,
2486 struct mcast_snooping
*ms OVS_UNUSED
,
2487 struct mcast_group
*grp
,
2488 struct xbundle
*in_xbundle
,
2489 const struct xvlan
*xvlan
)
2490 OVS_REQ_RDLOCK(ms
->rwlock
)
2492 struct xlate_cfg
*xcfg
;
2493 struct mcast_group_bundle
*b
;
2494 struct xbundle
*mcast_xbundle
;
2496 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2497 LIST_FOR_EACH(b
, bundle_node
, &grp
->bundle_lru
) {
2498 mcast_xbundle
= xbundle_lookup(xcfg
, b
->port
);
2499 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
) {
2500 xlate_report(ctx
, OFT_DETAIL
, "forwarding to mcast group port");
2501 output_normal(ctx
, mcast_xbundle
, xvlan
);
2502 } else if (!mcast_xbundle
) {
2503 xlate_report(ctx
, OFT_WARN
,
2504 "mcast group port is unknown, dropping");
2506 xlate_report(ctx
, OFT_DETAIL
,
2507 "mcast group port is input port, dropping");
2512 /* send the packet to ports connected to multicast routers */
2514 xlate_normal_mcast_send_mrouters(struct xlate_ctx
*ctx
,
2515 struct mcast_snooping
*ms
,
2516 struct xbundle
*in_xbundle
,
2517 const struct xvlan
*xvlan
)
2518 OVS_REQ_RDLOCK(ms
->rwlock
)
2520 struct xlate_cfg
*xcfg
;
2521 struct mcast_mrouter_bundle
*mrouter
;
2522 struct xbundle
*mcast_xbundle
;
2524 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2525 LIST_FOR_EACH(mrouter
, mrouter_node
, &ms
->mrouter_lru
) {
2526 mcast_xbundle
= xbundle_lookup(xcfg
, mrouter
->port
);
2527 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
2528 && mrouter
->vlan
== xvlan
->v
[0].vid
) {
2529 xlate_report(ctx
, OFT_DETAIL
, "forwarding to mcast router port");
2530 output_normal(ctx
, mcast_xbundle
, xvlan
);
2531 } else if (!mcast_xbundle
) {
2532 xlate_report(ctx
, OFT_WARN
,
2533 "mcast router port is unknown, dropping");
2534 } else if (mrouter
->vlan
!= xvlan
->v
[0].vid
) {
2535 xlate_report(ctx
, OFT_DETAIL
,
2536 "mcast router is on another vlan, dropping");
2538 xlate_report(ctx
, OFT_DETAIL
,
2539 "mcast router port is input port, dropping");
2544 /* send the packet to ports flagged to be flooded */
2546 xlate_normal_mcast_send_fports(struct xlate_ctx
*ctx
,
2547 struct mcast_snooping
*ms
,
2548 struct xbundle
*in_xbundle
,
2549 const struct xvlan
*xvlan
)
2550 OVS_REQ_RDLOCK(ms
->rwlock
)
2552 struct xlate_cfg
*xcfg
;
2553 struct mcast_port_bundle
*fport
;
2554 struct xbundle
*mcast_xbundle
;
2556 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2557 LIST_FOR_EACH(fport
, node
, &ms
->fport_list
) {
2558 mcast_xbundle
= xbundle_lookup(xcfg
, fport
->port
);
2559 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
) {
2560 xlate_report(ctx
, OFT_DETAIL
, "forwarding to mcast flood port");
2561 output_normal(ctx
, mcast_xbundle
, xvlan
);
2562 } else if (!mcast_xbundle
) {
2563 xlate_report(ctx
, OFT_WARN
,
2564 "mcast flood port is unknown, dropping");
2566 xlate_report(ctx
, OFT_DETAIL
,
2567 "mcast flood port is input port, dropping");
2572 /* forward the Reports to configured ports */
2574 xlate_normal_mcast_send_rports(struct xlate_ctx
*ctx
,
2575 struct mcast_snooping
*ms
,
2576 struct xbundle
*in_xbundle
,
2577 const struct xvlan
*xvlan
)
2578 OVS_REQ_RDLOCK(ms
->rwlock
)
2580 struct xlate_cfg
*xcfg
;
2581 struct mcast_port_bundle
*rport
;
2582 struct xbundle
*mcast_xbundle
;
2584 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2585 LIST_FOR_EACH(rport
, node
, &ms
->rport_list
) {
2586 mcast_xbundle
= xbundle_lookup(xcfg
, rport
->port
);
2587 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
) {
2588 xlate_report(ctx
, OFT_DETAIL
,
2589 "forwarding report to mcast flagged port");
2590 output_normal(ctx
, mcast_xbundle
, xvlan
);
2591 } else if (!mcast_xbundle
) {
2592 xlate_report(ctx
, OFT_WARN
,
2593 "mcast port is unknown, dropping the report");
2595 xlate_report(ctx
, OFT_DETAIL
,
2596 "mcast port is input port, dropping the Report");
2602 xlate_normal_flood(struct xlate_ctx
*ctx
, struct xbundle
*in_xbundle
,
2603 struct xvlan
*xvlan
)
2605 struct xbundle
*xbundle
;
2607 LIST_FOR_EACH (xbundle
, list_node
, &ctx
->xbridge
->xbundles
) {
2608 if (xbundle
!= in_xbundle
2609 && xbundle_includes_vlan(xbundle
, xvlan
)
2610 && xbundle
->floodable
2611 && !xbundle_mirror_out(ctx
->xbridge
, xbundle
)) {
2612 output_normal(ctx
, xbundle
, xvlan
);
2615 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
2619 is_ip_local_multicast(const struct flow
*flow
, struct flow_wildcards
*wc
)
2621 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2622 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
2623 return ip_is_local_multicast(flow
->nw_dst
);
2624 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
2625 memset(&wc
->masks
.ipv6_dst
, 0xff, sizeof wc
->masks
.ipv6_dst
);
2626 return ipv6_is_all_hosts(&flow
->ipv6_dst
);
2633 xlate_normal(struct xlate_ctx
*ctx
)
2635 struct flow_wildcards
*wc
= ctx
->wc
;
2636 struct flow
*flow
= &ctx
->xin
->flow
;
2637 struct xbundle
*in_xbundle
;
2638 struct xport
*in_port
;
2639 struct mac_entry
*mac
;
2641 struct xvlan in_xvlan
;
2645 memset(&wc
->masks
.dl_src
, 0xff, sizeof wc
->masks
.dl_src
);
2646 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
2647 wc
->masks
.vlans
[0].tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
2649 in_xbundle
= lookup_input_bundle(ctx
, flow
->in_port
.ofp_port
, &in_port
);
2651 xlate_report(ctx
, OFT_WARN
, "no input bundle, dropping");
2655 /* Drop malformed frames. */
2656 if (eth_type_vlan(flow
->dl_type
) &&
2657 !(flow
->vlans
[0].tci
& htons(VLAN_CFI
))) {
2658 if (ctx
->xin
->packet
!= NULL
) {
2659 xlate_report_error(ctx
, "dropping packet with partial "
2660 "VLAN tag received on port %s",
2663 xlate_report(ctx
, OFT_WARN
, "partial VLAN tag, dropping");
2667 /* Drop frames on bundles reserved for mirroring. */
2668 if (xbundle_mirror_out(ctx
->xbridge
, in_xbundle
)) {
2669 if (ctx
->xin
->packet
!= NULL
) {
2670 xlate_report_error(ctx
, "dropping packet received on port %s, "
2671 "which is reserved exclusively for mirroring",
2674 xlate_report(ctx
, OFT_WARN
,
2675 "input port is mirror output port, dropping");
2680 xvlan_extract(flow
, &in_xvlan
);
2681 if (!input_vid_is_valid(ctx
, in_xvlan
.v
[0].vid
, in_xbundle
)) {
2682 xlate_report(ctx
, OFT_WARN
,
2683 "disallowed VLAN VID for this input port, dropping");
2686 xvlan_input_translate(in_xbundle
, &in_xvlan
, &xvlan
);
2687 vlan
= xvlan
.v
[0].vid
;
2689 /* Check other admissibility requirements. */
2690 if (in_port
&& !is_admissible(ctx
, in_port
, vlan
)) {
2694 /* Learn source MAC. */
2695 bool is_grat_arp
= is_gratuitous_arp(flow
, wc
);
2696 if (ctx
->xin
->allow_side_effects
) {
2697 update_learning_table(ctx
, in_xbundle
, flow
->dl_src
, vlan
,
2700 if (ctx
->xin
->xcache
&& in_xbundle
!= &ofpp_none_bundle
) {
2701 struct xc_entry
*entry
;
2703 /* Save just enough info to update mac learning table later. */
2704 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NORMAL
);
2705 entry
->normal
.ofproto
= ctx
->xbridge
->ofproto
;
2706 entry
->normal
.in_port
= flow
->in_port
.ofp_port
;
2707 entry
->normal
.dl_src
= flow
->dl_src
;
2708 entry
->normal
.vlan
= vlan
;
2709 entry
->normal
.is_gratuitous_arp
= is_grat_arp
;
2712 /* Determine output bundle. */
2713 if (mcast_snooping_enabled(ctx
->xbridge
->ms
)
2714 && !eth_addr_is_broadcast(flow
->dl_dst
)
2715 && eth_addr_is_multicast(flow
->dl_dst
)
2716 && is_ip_any(flow
)) {
2717 struct mcast_snooping
*ms
= ctx
->xbridge
->ms
;
2718 struct mcast_group
*grp
= NULL
;
2720 if (is_igmp(flow
, wc
)) {
2721 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
2722 if (mcast_snooping_is_membership(flow
->tp_src
) ||
2723 mcast_snooping_is_query(flow
->tp_src
)) {
2724 if (ctx
->xin
->allow_side_effects
&& ctx
->xin
->packet
) {
2725 update_mcast_snooping_table(ctx
, flow
, vlan
,
2726 in_xbundle
, ctx
->xin
->packet
);
2729 * IGMP packets need to take the slow path, in order to be
2730 * processed for mdb updates. That will prevent expires
2731 * firing off even after hosts have sent reports.
2733 ctx
->xout
->slow
|= SLOW_ACTION
;
2736 if (mcast_snooping_is_membership(flow
->tp_src
)) {
2737 ovs_rwlock_rdlock(&ms
->rwlock
);
2738 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, &xvlan
);
2739 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2740 * forward IGMP Membership Reports only to those ports where
2741 * multicast routers are attached. Alternatively stated: a
2742 * snooping switch should not forward IGMP Membership Reports
2743 * to ports on which only hosts are attached.
2744 * An administrative control may be provided to override this
2745 * restriction, allowing the report messages to be flooded to
2747 xlate_normal_mcast_send_rports(ctx
, ms
, in_xbundle
, &xvlan
);
2748 ovs_rwlock_unlock(&ms
->rwlock
);
2750 xlate_report(ctx
, OFT_DETAIL
, "multicast traffic, flooding");
2751 xlate_normal_flood(ctx
, in_xbundle
, &xvlan
);
2754 } else if (is_mld(flow
, wc
)) {
2755 ctx
->xout
->slow
|= SLOW_ACTION
;
2756 if (ctx
->xin
->allow_side_effects
&& ctx
->xin
->packet
) {
2757 update_mcast_snooping_table(ctx
, flow
, vlan
,
2758 in_xbundle
, ctx
->xin
->packet
);
2760 if (is_mld_report(flow
, wc
)) {
2761 ovs_rwlock_rdlock(&ms
->rwlock
);
2762 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, &xvlan
);
2763 xlate_normal_mcast_send_rports(ctx
, ms
, in_xbundle
, &xvlan
);
2764 ovs_rwlock_unlock(&ms
->rwlock
);
2766 xlate_report(ctx
, OFT_DETAIL
, "MLD query, flooding");
2767 xlate_normal_flood(ctx
, in_xbundle
, &xvlan
);
2770 if (is_ip_local_multicast(flow
, wc
)) {
2771 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2772 * address in the 224.0.0.x range which are not IGMP must
2773 * be forwarded on all ports */
2774 xlate_report(ctx
, OFT_DETAIL
,
2775 "RFC4541: section 2.1.2, item 2, flooding");
2776 xlate_normal_flood(ctx
, in_xbundle
, &xvlan
);
2781 /* forwarding to group base ports */
2782 ovs_rwlock_rdlock(&ms
->rwlock
);
2783 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2784 grp
= mcast_snooping_lookup4(ms
, flow
->nw_dst
, vlan
);
2785 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
2786 grp
= mcast_snooping_lookup(ms
, &flow
->ipv6_dst
, vlan
);
2789 xlate_normal_mcast_send_group(ctx
, ms
, grp
, in_xbundle
, &xvlan
);
2790 xlate_normal_mcast_send_fports(ctx
, ms
, in_xbundle
, &xvlan
);
2791 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, &xvlan
);
2793 if (mcast_snooping_flood_unreg(ms
)) {
2794 xlate_report(ctx
, OFT_DETAIL
,
2795 "unregistered multicast, flooding");
2796 xlate_normal_flood(ctx
, in_xbundle
, &xvlan
);
2798 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, &xvlan
);
2799 xlate_normal_mcast_send_fports(ctx
, ms
, in_xbundle
, &xvlan
);
2802 ovs_rwlock_unlock(&ms
->rwlock
);
2804 ovs_rwlock_rdlock(&ctx
->xbridge
->ml
->rwlock
);
2805 mac
= mac_learning_lookup(ctx
->xbridge
->ml
, flow
->dl_dst
, vlan
);
2806 mac_port
= mac
? mac_entry_get_port(ctx
->xbridge
->ml
, mac
) : NULL
;
2807 ovs_rwlock_unlock(&ctx
->xbridge
->ml
->rwlock
);
2810 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2811 struct xbundle
*mac_xbundle
= xbundle_lookup(xcfg
, mac_port
);
2812 if (mac_xbundle
&& mac_xbundle
!= in_xbundle
) {
2813 xlate_report(ctx
, OFT_DETAIL
, "forwarding to learned port");
2814 output_normal(ctx
, mac_xbundle
, &xvlan
);
2815 } else if (!mac_xbundle
) {
2816 xlate_report(ctx
, OFT_WARN
,
2817 "learned port is unknown, dropping");
2819 xlate_report(ctx
, OFT_DETAIL
,
2820 "learned port is input port, dropping");
2823 xlate_report(ctx
, OFT_DETAIL
,
2824 "no learned MAC for destination, flooding");
2825 xlate_normal_flood(ctx
, in_xbundle
, &xvlan
);
2830 /* Appends a "sample" action for sFlow or IPFIX to 'ctx->odp_actions'. The
2831 * 'probability' is the number of packets out of UINT32_MAX to sample. The
2832 * 'cookie' (of length 'cookie_size' bytes) is passed back in the callback for
2833 * each sampled packet. 'tunnel_out_port', if not ODPP_NONE, is added as the
2834 * OVS_USERSPACE_ATTR_EGRESS_TUN_PORT attribute. If 'include_actions', an
2835 * OVS_USERSPACE_ATTR_ACTIONS attribute is added. If 'emit_set_tunnel',
2836 * sample(sampling_port=1) would translate into datapath sample action
2837 * set(tunnel(...)), sample(...) and it is used for sampling egress tunnel
2841 compose_sample_action(struct xlate_ctx
*ctx
,
2842 const uint32_t probability
,
2843 const union user_action_cookie
*cookie
,
2844 const size_t cookie_size
,
2845 const odp_port_t tunnel_out_port
,
2846 bool include_actions
)
2848 if (probability
== 0) {
2849 /* No need to generate sampling or the inner action. */
2853 /* If the slow path meter is configured by the controller,
2854 * insert a meter action before the user space action. */
2855 struct ofproto
*ofproto
= &ctx
->xin
->ofproto
->up
;
2856 uint32_t meter_id
= ofproto
->slowpath_meter_id
;
2858 /* When meter action is not required, avoid generate sample action
2859 * for 100% sampling rate. */
2860 bool is_sample
= probability
< UINT32_MAX
|| meter_id
!= UINT32_MAX
;
2861 size_t sample_offset
, actions_offset
;
2863 sample_offset
= nl_msg_start_nested(ctx
->odp_actions
,
2864 OVS_ACTION_ATTR_SAMPLE
);
2865 nl_msg_put_u32(ctx
->odp_actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
2867 actions_offset
= nl_msg_start_nested(ctx
->odp_actions
,
2868 OVS_SAMPLE_ATTR_ACTIONS
);
2871 if (meter_id
!= UINT32_MAX
) {
2872 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_METER
, meter_id
);
2875 odp_port_t odp_port
= ofp_port_to_odp_port(
2876 ctx
->xbridge
, ctx
->xin
->flow
.in_port
.ofp_port
);
2877 uint32_t pid
= dpif_port_get_pid(ctx
->xbridge
->dpif
, odp_port
,
2878 flow_hash_5tuple(&ctx
->xin
->flow
, 0));
2879 int cookie_offset
= odp_put_userspace_action(pid
, cookie
, cookie_size
,
2885 nl_msg_end_nested(ctx
->odp_actions
, actions_offset
);
2886 nl_msg_end_nested(ctx
->odp_actions
, sample_offset
);
2889 return cookie_offset
;
2892 /* If sFLow is not enabled, returns 0 without doing anything.
2894 * If sFlow is enabled, appends a template "sample" action to the ODP actions
2895 * in 'ctx'. This action is a template because some of the information needed
2896 * to fill it out is not available until flow translation is complete. In this
2897 * case, this functions returns an offset, which is always nonzero, to pass
2898 * later to fix_sflow_action() to fill in the rest of the template. */
2900 compose_sflow_action(struct xlate_ctx
*ctx
)
2902 struct dpif_sflow
*sflow
= ctx
->xbridge
->sflow
;
2903 if (!sflow
|| ctx
->xin
->flow
.in_port
.ofp_port
== OFPP_NONE
) {
2907 union user_action_cookie cookie
= { .type
= USER_ACTION_COOKIE_SFLOW
};
2908 return compose_sample_action(ctx
, dpif_sflow_get_probability(sflow
),
2909 &cookie
, sizeof cookie
.sflow
, ODPP_NONE
,
2913 /* If flow IPFIX is enabled, make sure IPFIX flow sample action
2914 * at egress point of tunnel port is just in front of corresponding
2915 * output action. If bridge IPFIX is enabled, this appends an IPFIX
2916 * sample action to 'ctx->odp_actions'. */
2918 compose_ipfix_action(struct xlate_ctx
*ctx
, odp_port_t output_odp_port
)
2920 struct dpif_ipfix
*ipfix
= ctx
->xbridge
->ipfix
;
2921 odp_port_t tunnel_out_port
= ODPP_NONE
;
2923 if (!ipfix
|| ctx
->xin
->flow
.in_port
.ofp_port
== OFPP_NONE
) {
2927 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2929 if (output_odp_port
== ODPP_NONE
&&
2930 !dpif_ipfix_get_bridge_exporter_input_sampling(ipfix
)) {
2934 /* For output case, output_odp_port is valid. */
2935 if (output_odp_port
!= ODPP_NONE
) {
2936 if (!dpif_ipfix_get_bridge_exporter_output_sampling(ipfix
)) {
2939 /* If tunnel sampling is enabled, put an additional option attribute:
2940 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2942 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(ipfix
) &&
2943 dpif_ipfix_get_tunnel_port(ipfix
, output_odp_port
) ) {
2944 tunnel_out_port
= output_odp_port
;
2948 union user_action_cookie cookie
= {
2950 .type
= USER_ACTION_COOKIE_IPFIX
,
2951 .output_odp_port
= output_odp_port
,
2954 compose_sample_action(ctx
,
2955 dpif_ipfix_get_bridge_exporter_probability(ipfix
),
2956 &cookie
, sizeof cookie
.ipfix
, tunnel_out_port
,
2960 /* Fix "sample" action according to data collected while composing ODP actions,
2961 * as described in compose_sflow_action().
2963 * 'user_cookie_offset' must be the offset returned by add_sflow_action(). */
2965 fix_sflow_action(struct xlate_ctx
*ctx
, unsigned int user_cookie_offset
)
2967 const struct flow
*base
= &ctx
->base_flow
;
2968 union user_action_cookie
*cookie
;
2970 cookie
= ofpbuf_at(ctx
->odp_actions
, user_cookie_offset
,
2971 sizeof cookie
->sflow
);
2972 ovs_assert(cookie
->type
== USER_ACTION_COOKIE_SFLOW
);
2974 cookie
->type
= USER_ACTION_COOKIE_SFLOW
;
2975 cookie
->sflow
.vlan_tci
= base
->vlans
[0].tci
;
2977 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2978 * port information") for the interpretation of cookie->output. */
2979 switch (ctx
->sflow_n_outputs
) {
2981 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2982 cookie
->sflow
.output
= 0x40000000 | 256;
2986 cookie
->sflow
.output
= dpif_sflow_odp_port_to_ifindex(
2987 ctx
->xbridge
->sflow
, ctx
->sflow_odp_port
);
2988 if (cookie
->sflow
.output
) {
2993 /* 0x80000000 means "multiple output ports. */
2994 cookie
->sflow
.output
= 0x80000000 | ctx
->sflow_n_outputs
;
3000 process_special(struct xlate_ctx
*ctx
, const struct xport
*xport
)
3002 const struct flow
*flow
= &ctx
->xin
->flow
;
3003 struct flow_wildcards
*wc
= ctx
->wc
;
3004 const struct xbridge
*xbridge
= ctx
->xbridge
;
3005 const struct dp_packet
*packet
= ctx
->xin
->packet
;
3006 enum slow_path_reason slow
;
3010 } else if (xport
->cfm
&& cfm_should_process_flow(xport
->cfm
, flow
, wc
)) {
3012 cfm_process_heartbeat(xport
->cfm
, packet
);
3015 } else if (xport
->bfd
&& bfd_should_process_flow(xport
->bfd
, flow
, wc
)) {
3017 bfd_process_packet(xport
->bfd
, flow
, packet
);
3018 /* If POLL received, immediately sends FINAL back. */
3019 if (bfd_should_send_packet(xport
->bfd
)) {
3020 ofproto_dpif_monitor_port_send_soon(xport
->ofport
);
3024 } else if (xport
->xbundle
&& xport
->xbundle
->lacp
3025 && flow
->dl_type
== htons(ETH_TYPE_LACP
)) {
3027 lacp_process_packet(xport
->xbundle
->lacp
, xport
->ofport
, packet
);
3030 } else if ((xbridge
->stp
|| xbridge
->rstp
) &&
3031 stp_should_process_flow(flow
, wc
)) {
3034 ? stp_process_packet(xport
, packet
)
3035 : rstp_process_packet(xport
, packet
);
3038 } else if (xport
->lldp
&& lldp_should_process_flow(xport
->lldp
, flow
)) {
3040 lldp_process_packet(xport
->lldp
, packet
);
3048 ctx
->xout
->slow
|= slow
;
3056 tnl_route_lookup_flow(const struct flow
*oflow
,
3057 struct in6_addr
*ip
, struct in6_addr
*src
,
3058 struct xport
**out_port
)
3060 char out_dev
[IFNAMSIZ
];
3061 struct xbridge
*xbridge
;
3062 struct xlate_cfg
*xcfg
;
3064 struct in6_addr dst
;
3066 dst
= flow_tnl_dst(&oflow
->tunnel
);
3067 if (!ovs_router_lookup(oflow
->pkt_mark
, &dst
, out_dev
, src
, &gw
)) {
3071 if (ipv6_addr_is_set(&gw
) &&
3072 (!IN6_IS_ADDR_V4MAPPED(&gw
) || in6_addr_get_mapped_ipv4(&gw
))) {
3078 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
3081 HMAP_FOR_EACH (xbridge
, hmap_node
, &xcfg
->xbridges
) {
3082 if (!strncmp(xbridge
->name
, out_dev
, IFNAMSIZ
)) {
3085 HMAP_FOR_EACH (port
, ofp_node
, &xbridge
->xports
) {
3086 if (!strncmp(netdev_get_name(port
->netdev
), out_dev
, IFNAMSIZ
)) {
3097 compose_table_xlate(struct xlate_ctx
*ctx
, const struct xport
*out_dev
,
3098 struct dp_packet
*packet
)
3100 struct xbridge
*xbridge
= out_dev
->xbridge
;
3101 struct ofpact_output output
;
3104 ofpact_init(&output
.ofpact
, OFPACT_OUTPUT
, sizeof output
);
3105 flow_extract(packet
, &flow
);
3106 flow
.in_port
.ofp_port
= out_dev
->ofp_port
;
3107 output
.port
= OFPP_TABLE
;
3110 return ofproto_dpif_execute_actions__(xbridge
->ofproto
,
3111 ctx
->xin
->tables_version
, &flow
,
3112 NULL
, &output
.ofpact
, sizeof output
,
3113 ctx
->depth
, ctx
->resubmits
, packet
);
3117 tnl_send_nd_request(struct xlate_ctx
*ctx
, const struct xport
*out_dev
,
3118 const struct eth_addr eth_src
,
3119 struct in6_addr
* ipv6_src
, struct in6_addr
* ipv6_dst
)
3121 struct dp_packet packet
;
3123 dp_packet_init(&packet
, 0);
3124 compose_nd_ns(&packet
, eth_src
, ipv6_src
, ipv6_dst
);
3125 compose_table_xlate(ctx
, out_dev
, &packet
);
3126 dp_packet_uninit(&packet
);
3130 tnl_send_arp_request(struct xlate_ctx
*ctx
, const struct xport
*out_dev
,
3131 const struct eth_addr eth_src
,
3132 ovs_be32 ip_src
, ovs_be32 ip_dst
)
3134 struct dp_packet packet
;
3136 dp_packet_init(&packet
, 0);
3137 compose_arp(&packet
, ARP_OP_REQUEST
,
3138 eth_src
, eth_addr_zero
, true, ip_src
, ip_dst
);
3140 compose_table_xlate(ctx
, out_dev
, &packet
);
3141 dp_packet_uninit(&packet
);
3145 build_tunnel_send(struct xlate_ctx
*ctx
, const struct xport
*xport
,
3146 const struct flow
*flow
, odp_port_t tunnel_odp_port
)
3148 struct netdev_tnl_build_header_params tnl_params
;
3149 struct ovs_action_push_tnl tnl_push_data
;
3150 struct xport
*out_dev
= NULL
;
3151 ovs_be32 s_ip
= 0, d_ip
= 0;
3152 struct in6_addr s_ip6
= in6addr_any
;
3153 struct in6_addr d_ip6
= in6addr_any
;
3154 struct eth_addr smac
;
3155 struct eth_addr dmac
;
3157 char buf_sip6
[INET6_ADDRSTRLEN
];
3158 char buf_dip6
[INET6_ADDRSTRLEN
];
3160 err
= tnl_route_lookup_flow(flow
, &d_ip6
, &s_ip6
, &out_dev
);
3162 xlate_report(ctx
, OFT_WARN
, "native tunnel routing failed");
3166 xlate_report(ctx
, OFT_DETAIL
, "tunneling to %s via %s",
3167 ipv6_string_mapped(buf_dip6
, &d_ip6
),
3168 netdev_get_name(out_dev
->netdev
));
3170 /* Use mac addr of bridge port of the peer. */
3171 err
= netdev_get_etheraddr(out_dev
->netdev
, &smac
);
3173 xlate_report(ctx
, OFT_WARN
,
3174 "tunnel output device lacks Ethernet address");
3178 d_ip
= in6_addr_get_mapped_ipv4(&d_ip6
);
3180 s_ip
= in6_addr_get_mapped_ipv4(&s_ip6
);
3183 err
= tnl_neigh_lookup(out_dev
->xbridge
->name
, &d_ip6
, &dmac
);
3185 xlate_report(ctx
, OFT_DETAIL
,
3186 "neighbor cache miss for %s on bridge %s, "
3187 "sending %s request",
3188 buf_dip6
, out_dev
->xbridge
->name
, d_ip
? "ARP" : "ND");
3190 tnl_send_arp_request(ctx
, out_dev
, smac
, s_ip
, d_ip
);
3192 tnl_send_nd_request(ctx
, out_dev
, smac
, &s_ip6
, &d_ip6
);
3197 if (ctx
->xin
->xcache
) {
3198 struct xc_entry
*entry
;
3200 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_TNL_NEIGH
);
3201 ovs_strlcpy(entry
->tnl_neigh_cache
.br_name
, out_dev
->xbridge
->name
,
3202 sizeof entry
->tnl_neigh_cache
.br_name
);
3203 entry
->tnl_neigh_cache
.d_ipv6
= d_ip6
;
3206 xlate_report(ctx
, OFT_DETAIL
, "tunneling from "ETH_ADDR_FMT
" %s"
3207 " to "ETH_ADDR_FMT
" %s",
3208 ETH_ADDR_ARGS(smac
), ipv6_string_mapped(buf_sip6
, &s_ip6
),
3209 ETH_ADDR_ARGS(dmac
), buf_dip6
);
3211 netdev_init_tnl_build_header_params(&tnl_params
, flow
, &s_ip6
, dmac
, smac
);
3212 err
= tnl_port_build_header(xport
->ofport
, &tnl_push_data
, &tnl_params
);
3216 tnl_push_data
.tnl_port
= odp_to_u32(tunnel_odp_port
);
3217 tnl_push_data
.out_port
= odp_to_u32(out_dev
->odp_port
);
3219 size_t push_action_size
= 0;
3220 size_t clone_ofs
= nl_msg_start_nested(ctx
->odp_actions
,
3221 OVS_ACTION_ATTR_CLONE
);
3222 odp_put_tnl_push_action(ctx
->odp_actions
, &tnl_push_data
);
3223 push_action_size
= ctx
->odp_actions
->size
;
3224 apply_nested_clone_actions(ctx
, xport
, out_dev
);
3225 if (ctx
->odp_actions
->size
> push_action_size
) {
3226 /* Update the CLONE action only when combined */
3227 nl_msg_end_nested(ctx
->odp_actions
, clone_ofs
);
3233 xlate_commit_actions(struct xlate_ctx
*ctx
)
3235 bool use_masked
= ctx
->xbridge
->support
.masked_set_action
;
3237 ctx
->xout
->slow
|= commit_odp_actions(&ctx
->xin
->flow
, &ctx
->base_flow
,
3238 ctx
->odp_actions
, ctx
->wc
,
3243 clear_conntrack(struct xlate_ctx
*ctx
)
3245 ctx
->conntracked
= false;
3246 flow_clear_conntrack(&ctx
->xin
->flow
);
3250 xlate_flow_is_protected(const struct xlate_ctx
*ctx
, const struct flow
*flow
, const struct xport
*xport_out
)
3252 const struct xport
*xport_in
;
3258 xport_in
= get_ofp_port(ctx
->xbridge
, flow
->in_port
.ofp_port
);
3260 return (xport_in
&& xport_in
->xbundle
&& xport_out
->xbundle
&&
3261 xport_in
->xbundle
->protected && xport_out
->xbundle
->protected);
3264 /* Populate and apply nested actions on 'out_dev'.
3265 * The nested actions are applied on cloned packets in dp while outputting to
3266 * either patch or tunnel ports.
3267 * On output to a patch port, the output action will be replaced with set of
3268 * nested actions on the peer patch port.
3269 * Similarly on output to a tunnel port, the post nested actions on
3270 * tunnel are chained up with the tunnel-push action.
3273 apply_nested_clone_actions(struct xlate_ctx
*ctx
, const struct xport
*in_dev
,
3274 struct xport
*out_dev
)
3276 struct flow
*flow
= &ctx
->xin
->flow
;
3277 struct flow old_flow
= ctx
->xin
->flow
;
3278 struct flow_tnl old_flow_tnl_wc
= ctx
->wc
->masks
.tunnel
;
3279 bool old_conntrack
= ctx
->conntracked
;
3280 bool old_was_mpls
= ctx
->was_mpls
;
3281 ovs_version_t old_version
= ctx
->xin
->tables_version
;
3282 struct ofpbuf old_stack
= ctx
->stack
;
3283 union mf_subvalue new_stack
[1024 / sizeof(union mf_subvalue
)];
3284 struct ofpbuf old_action_set
= ctx
->action_set
;
3285 struct ovs_list
*old_trace
= ctx
->xin
->trace
;
3286 uint64_t actset_stub
[1024 / 8];
3288 ofpbuf_use_stub(&ctx
->stack
, new_stack
, sizeof new_stack
);
3289 ofpbuf_use_stub(&ctx
->action_set
, actset_stub
, sizeof actset_stub
);
3290 flow
->in_port
.ofp_port
= out_dev
->ofp_port
;
3291 flow
->metadata
= htonll(0);
3292 memset(&flow
->tunnel
, 0, sizeof flow
->tunnel
);
3293 memset(&ctx
->wc
->masks
.tunnel
, 0, sizeof ctx
->wc
->masks
.tunnel
);
3294 flow
->tunnel
.metadata
.tab
=
3295 ofproto_get_tun_tab(&out_dev
->xbridge
->ofproto
->up
);
3296 ctx
->wc
->masks
.tunnel
.metadata
.tab
= flow
->tunnel
.metadata
.tab
;
3297 memset(flow
->regs
, 0, sizeof flow
->regs
);
3298 flow
->actset_output
= OFPP_UNSET
;
3299 ctx
->conntracked
= false;
3300 clear_conntrack(ctx
);
3301 ctx
->xin
->trace
= xlate_report(ctx
, OFT_BRIDGE
,
3303 out_dev
->xbridge
->name
);
3304 mirror_mask_t old_mirrors
= ctx
->mirrors
;
3305 bool independent_mirrors
= out_dev
->xbridge
!= ctx
->xbridge
;
3306 if (independent_mirrors
) {
3309 ctx
->xbridge
= out_dev
->xbridge
;
3311 /* The bridge is now known so obtain its table version. */
3312 ctx
->xin
->tables_version
3313 = ofproto_dpif_get_tables_version(ctx
->xbridge
->ofproto
);
3315 if (!process_special(ctx
, out_dev
) && may_receive(out_dev
, ctx
)) {
3316 if (xport_stp_forward_state(out_dev
) &&
3317 xport_rstp_forward_state(out_dev
)) {
3318 xlate_table_action(ctx
, flow
->in_port
.ofp_port
, 0, true, true,
3320 if (!ctx
->freezing
) {
3321 xlate_action_set(ctx
);
3323 if (ctx
->freezing
) {
3324 finish_freezing(ctx
);
3327 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
3328 * the learning action look at the packet, then drop it. */
3329 struct flow old_base_flow
= ctx
->base_flow
;
3330 size_t old_size
= ctx
->odp_actions
->size
;
3331 mirror_mask_t old_mirrors2
= ctx
->mirrors
;
3333 xlate_table_action(ctx
, flow
->in_port
.ofp_port
, 0, true, true,
3335 ctx
->mirrors
= old_mirrors2
;
3336 ctx
->base_flow
= old_base_flow
;
3337 ctx
->odp_actions
->size
= old_size
;
3339 /* Undo changes that may have been done for freezing. */
3340 ctx_cancel_freeze(ctx
);
3344 ctx
->xin
->trace
= old_trace
;
3345 if (independent_mirrors
) {
3346 ctx
->mirrors
= old_mirrors
;
3348 ctx
->xin
->flow
= old_flow
;
3349 ctx
->xbridge
= in_dev
->xbridge
;
3350 ofpbuf_uninit(&ctx
->action_set
);
3351 ctx
->action_set
= old_action_set
;
3352 ofpbuf_uninit(&ctx
->stack
);
3353 ctx
->stack
= old_stack
;
3355 /* Restore calling bridge's lookup version. */
3356 ctx
->xin
->tables_version
= old_version
;
3358 /* Restore to calling bridge tunneling information */
3359 ctx
->wc
->masks
.tunnel
= old_flow_tnl_wc
;
3361 /* The out bridge popping MPLS should have no effect on the original
3363 ctx
->was_mpls
= old_was_mpls
;
3365 /* The out bridge's conntrack execution should have no effect on the
3366 * original bridge. */
3367 ctx
->conntracked
= old_conntrack
;
3369 /* The fact that the out bridge exits (for any reason) does not mean
3370 * that the original bridge should exit. Specifically, if the out
3371 * bridge freezes translation, the original bridge must continue
3372 * processing with the original, not the frozen packet! */
3375 /* Out bridge errors do not propagate back. */
3376 ctx
->error
= XLATE_OK
;
3378 if (ctx
->xin
->resubmit_stats
) {
3379 netdev_vport_inc_tx(in_dev
->netdev
, ctx
->xin
->resubmit_stats
);
3380 netdev_vport_inc_rx(out_dev
->netdev
, ctx
->xin
->resubmit_stats
);
3382 bfd_account_rx(out_dev
->bfd
, ctx
->xin
->resubmit_stats
);
3385 if (ctx
->xin
->xcache
) {
3386 struct xc_entry
*entry
;
3387 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NETDEV
);
3388 entry
->dev
.tx
= netdev_ref(in_dev
->netdev
);
3389 entry
->dev
.rx
= netdev_ref(out_dev
->netdev
);
3390 entry
->dev
.bfd
= bfd_ref(out_dev
->bfd
);
3395 compose_output_action__(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
,
3396 const struct xlate_bond_recirc
*xr
, bool check_stp
)
3398 const struct xport
*xport
= get_ofp_port(ctx
->xbridge
, ofp_port
);
3399 struct flow_wildcards
*wc
= ctx
->wc
;
3400 struct flow
*flow
= &ctx
->xin
->flow
;
3401 struct flow_tnl flow_tnl
;
3402 union flow_vlan_hdr flow_vlans
[FLOW_MAX_VLAN_HEADERS
];
3403 uint8_t flow_nw_tos
;
3404 odp_port_t out_port
, odp_port
;
3405 bool tnl_push_pop_send
= false;
3408 /* If 'struct flow' gets additional metadata, we'll need to zero it out
3409 * before traversing a patch port. */
3410 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 39);
3411 memset(&flow_tnl
, 0, sizeof flow_tnl
);
3414 xlate_report(ctx
, OFT_WARN
, "Nonexistent output port");
3416 } else if (xport
->config
& OFPUTIL_PC_NO_FWD
) {
3417 xlate_report(ctx
, OFT_DETAIL
, "OFPPC_NO_FWD set, skipping output");
3419 } else if (ctx
->mirror_snaplen
!= 0 && xport
->odp_port
== ODPP_NONE
) {
3420 xlate_report(ctx
, OFT_WARN
,
3421 "Mirror truncate to ODPP_NONE, skipping output");
3423 } else if (xlate_flow_is_protected(ctx
, flow
, xport
)) {
3424 xlate_report(ctx
, OFT_WARN
,
3425 "Flow is between protected ports, skipping output.");
3427 } else if (check_stp
) {
3428 if (is_stp(&ctx
->base_flow
)) {
3429 if (!xport_stp_should_forward_bpdu(xport
) &&
3430 !xport_rstp_should_manage_bpdu(xport
)) {
3431 if (ctx
->xbridge
->stp
!= NULL
) {
3432 xlate_report(ctx
, OFT_WARN
,
3433 "STP not in listening state, "
3434 "skipping bpdu output");
3435 } else if (ctx
->xbridge
->rstp
!= NULL
) {
3436 xlate_report(ctx
, OFT_WARN
,
3437 "RSTP not managing BPDU in this state, "
3438 "skipping bpdu output");
3442 } else if ((xport
->cfm
&& cfm_should_process_flow(xport
->cfm
, flow
, wc
))
3443 || (xport
->bfd
&& bfd_should_process_flow(xport
->bfd
, flow
,
3445 /* Pass; STP should not block link health detection. */
3446 } else if (!xport_stp_forward_state(xport
) ||
3447 !xport_rstp_forward_state(xport
)) {
3448 if (ctx
->xbridge
->stp
!= NULL
) {
3449 xlate_report(ctx
, OFT_WARN
,
3450 "STP not in forwarding state, skipping output");
3451 } else if (ctx
->xbridge
->rstp
!= NULL
) {
3452 xlate_report(ctx
, OFT_WARN
,
3453 "RSTP not in forwarding state, skipping output");
3460 apply_nested_clone_actions(ctx
, xport
, xport
->peer
);
3464 memcpy(flow_vlans
, flow
->vlans
, sizeof flow_vlans
);
3465 flow_nw_tos
= flow
->nw_tos
;
3467 if (count_skb_priorities(xport
)) {
3468 memset(&wc
->masks
.skb_priority
, 0xff, sizeof wc
->masks
.skb_priority
);
3469 if (dscp_from_skb_priority(xport
, flow
->skb_priority
, &dscp
)) {
3470 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
3471 flow
->nw_tos
&= ~IP_DSCP_MASK
;
3472 flow
->nw_tos
|= dscp
;
3476 if (xport
->is_tunnel
) {
3477 struct in6_addr dst
;
3478 /* Save tunnel metadata so that changes made due to
3479 * the Logical (tunnel) Port are not visible for any further
3480 * matches, while explicit set actions on tunnel metadata are.
3482 flow_tnl
= flow
->tunnel
;
3483 odp_port
= tnl_port_send(xport
->ofport
, flow
, ctx
->wc
);
3484 if (odp_port
== ODPP_NONE
) {
3485 xlate_report(ctx
, OFT_WARN
, "Tunneling decided against output");
3486 goto out
; /* restore flow_nw_tos */
3488 dst
= flow_tnl_dst(&flow
->tunnel
);
3489 if (ipv6_addr_equals(&dst
, &ctx
->orig_tunnel_ipv6_dst
)) {
3490 xlate_report(ctx
, OFT_WARN
, "Not tunneling to our own address");
3491 goto out
; /* restore flow_nw_tos */
3493 if (ctx
->xin
->resubmit_stats
) {
3494 netdev_vport_inc_tx(xport
->netdev
, ctx
->xin
->resubmit_stats
);
3496 if (ctx
->xin
->xcache
) {
3497 struct xc_entry
*entry
;
3499 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NETDEV
);
3500 entry
->dev
.tx
= netdev_ref(xport
->netdev
);
3502 out_port
= odp_port
;
3503 if (ovs_native_tunneling_is_on(ctx
->xbridge
->ofproto
)) {
3504 xlate_report(ctx
, OFT_DETAIL
, "output to native tunnel");
3505 tnl_push_pop_send
= true;
3507 xlate_report(ctx
, OFT_DETAIL
, "output to kernel tunnel");
3508 commit_odp_tunnel_action(flow
, &ctx
->base_flow
, ctx
->odp_actions
);
3509 flow
->tunnel
= flow_tnl
; /* Restore tunnel metadata */
3512 odp_port
= xport
->odp_port
;
3513 out_port
= odp_port
;
3516 if (out_port
!= ODPP_NONE
) {
3517 xlate_commit_actions(ctx
);
3520 struct ovs_action_hash
*act_hash
;
3523 act_hash
= nl_msg_put_unspec_uninit(ctx
->odp_actions
,
3524 OVS_ACTION_ATTR_HASH
,
3526 act_hash
->hash_alg
= xr
->hash_alg
;
3527 act_hash
->hash_basis
= xr
->hash_basis
;
3529 /* Recirc action. */
3530 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_RECIRC
,
3534 if (tnl_push_pop_send
) {
3535 build_tunnel_send(ctx
, xport
, flow
, odp_port
);
3536 flow
->tunnel
= flow_tnl
; /* Restore tunnel metadata */
3538 odp_port_t odp_tnl_port
= ODPP_NONE
;
3540 /* XXX: Write better Filter for tunnel port. We can use inport
3541 * int tunnel-port flow to avoid these checks completely. */
3542 if (ofp_port
== OFPP_LOCAL
&&
3543 ovs_native_tunneling_is_on(ctx
->xbridge
->ofproto
)) {
3545 odp_tnl_port
= tnl_port_map_lookup(flow
, wc
);
3548 if (odp_tnl_port
!= ODPP_NONE
) {
3549 nl_msg_put_odp_port(ctx
->odp_actions
,
3550 OVS_ACTION_ATTR_TUNNEL_POP
,
3553 /* Tunnel push-pop action is not compatible with
3555 compose_ipfix_action(ctx
, out_port
);
3557 /* Handle truncation of the mirrored packet. */
3558 if (ctx
->mirror_snaplen
> 0 &&
3559 ctx
->mirror_snaplen
< UINT16_MAX
) {
3560 struct ovs_action_trunc
*trunc
;
3562 trunc
= nl_msg_put_unspec_uninit(ctx
->odp_actions
,
3563 OVS_ACTION_ATTR_TRUNC
,
3565 trunc
->max_len
= ctx
->mirror_snaplen
;
3566 if (!ctx
->xbridge
->support
.trunc
) {
3567 ctx
->xout
->slow
|= SLOW_ACTION
;
3571 nl_msg_put_odp_port(ctx
->odp_actions
,
3572 OVS_ACTION_ATTR_OUTPUT
,
3578 ctx
->sflow_odp_port
= odp_port
;
3579 ctx
->sflow_n_outputs
++;
3580 ctx
->nf_output_iface
= ofp_port
;
3583 if (mbridge_has_mirrors(ctx
->xbridge
->mbridge
) && xport
->xbundle
) {
3584 mirror_packet(ctx
, xport
->xbundle
,
3585 xbundle_mirror_dst(xport
->xbundle
->xbridge
,
3591 memcpy(flow
->vlans
, flow_vlans
, sizeof flow
->vlans
);
3592 flow
->nw_tos
= flow_nw_tos
;
3596 compose_output_action(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
,
3597 const struct xlate_bond_recirc
*xr
)
3599 compose_output_action__(ctx
, ofp_port
, xr
, true);
3603 xlate_recursively(struct xlate_ctx
*ctx
, struct rule_dpif
*rule
, bool deepens
)
3605 struct rule_dpif
*old_rule
= ctx
->rule
;
3606 ovs_be64 old_cookie
= ctx
->rule_cookie
;
3607 const struct rule_actions
*actions
;
3609 if (ctx
->xin
->resubmit_stats
) {
3610 rule_dpif_credit_stats(rule
, ctx
->xin
->resubmit_stats
);
3615 ctx
->depth
+= deepens
;
3617 ctx
->rule_cookie
= rule
->up
.flow_cookie
;
3618 actions
= rule_get_actions(&rule
->up
);
3619 do_xlate_actions(actions
->ofpacts
, actions
->ofpacts_len
, ctx
);
3620 ctx
->rule_cookie
= old_cookie
;
3621 ctx
->rule
= old_rule
;
3622 ctx
->depth
-= deepens
;
3626 xlate_resubmit_resource_check(struct xlate_ctx
*ctx
)
3628 if (ctx
->depth
>= MAX_DEPTH
) {
3629 xlate_report_error(ctx
, "over max translation depth %d", MAX_DEPTH
);
3630 ctx
->error
= XLATE_RECURSION_TOO_DEEP
;
3631 } else if (ctx
->resubmits
>= MAX_RESUBMITS
) {
3632 xlate_report_error(ctx
, "over %d resubmit actions", MAX_RESUBMITS
);
3633 ctx
->error
= XLATE_TOO_MANY_RESUBMITS
;
3634 } else if (ctx
->odp_actions
->size
> UINT16_MAX
) {
3635 xlate_report_error(ctx
, "resubmits yielded over 64 kB of actions");
3636 /* NOT an error, as we'll be slow-pathing the flow in this case? */
3637 ctx
->exit
= true; /* XXX: translation still terminated! */
3638 } else if (ctx
->stack
.size
>= 65536) {
3639 xlate_report_error(ctx
, "resubmits yielded over 64 kB of stack");
3640 ctx
->error
= XLATE_STACK_TOO_DEEP
;
3649 tuple_swap_flow(struct flow
*flow
, bool ipv4
)
3651 uint8_t nw_proto
= flow
->nw_proto
;
3652 flow
->nw_proto
= flow
->ct_nw_proto
;
3653 flow
->ct_nw_proto
= nw_proto
;
3656 ovs_be32 nw_src
= flow
->nw_src
;
3657 flow
->nw_src
= flow
->ct_nw_src
;
3658 flow
->ct_nw_src
= nw_src
;
3660 ovs_be32 nw_dst
= flow
->nw_dst
;
3661 flow
->nw_dst
= flow
->ct_nw_dst
;
3662 flow
->ct_nw_dst
= nw_dst
;
3664 struct in6_addr ipv6_src
= flow
->ipv6_src
;
3665 flow
->ipv6_src
= flow
->ct_ipv6_src
;
3666 flow
->ct_ipv6_src
= ipv6_src
;
3668 struct in6_addr ipv6_dst
= flow
->ipv6_dst
;
3669 flow
->ipv6_dst
= flow
->ct_ipv6_dst
;
3670 flow
->ct_ipv6_dst
= ipv6_dst
;
3673 ovs_be16 tp_src
= flow
->tp_src
;
3674 flow
->tp_src
= flow
->ct_tp_src
;
3675 flow
->ct_tp_src
= tp_src
;
3677 ovs_be16 tp_dst
= flow
->tp_dst
;
3678 flow
->tp_dst
= flow
->ct_tp_dst
;
3679 flow
->ct_tp_dst
= tp_dst
;
3683 tuple_swap(struct flow
*flow
, struct flow_wildcards
*wc
)
3685 bool ipv4
= (flow
->dl_type
== htons(ETH_TYPE_IP
));
3687 tuple_swap_flow(flow
, ipv4
);
3688 tuple_swap_flow(&wc
->masks
, ipv4
);
3692 xlate_table_action(struct xlate_ctx
*ctx
, ofp_port_t in_port
, uint8_t table_id
,
3693 bool may_packet_in
, bool honor_table_miss
,
3696 /* Check if we need to recirculate before matching in a table. */
3697 if (ctx
->was_mpls
) {
3698 ctx_trigger_freeze(ctx
);
3701 if (xlate_resubmit_resource_check(ctx
)) {
3702 uint8_t old_table_id
= ctx
->table_id
;
3703 struct rule_dpif
*rule
;
3705 ctx
->table_id
= table_id
;
3707 /* Swap packet fields with CT 5-tuple if requested. */
3709 /* Do not swap if there is no CT tuple, or if key is not IP. */
3710 if (ctx
->xin
->flow
.ct_nw_proto
== 0 ||
3711 !is_ip_any(&ctx
->xin
->flow
)) {
3712 xlate_report_error(ctx
,
3713 "resubmit(ct) with non-tracked or non-IP packet!");
3716 tuple_swap(&ctx
->xin
->flow
, ctx
->wc
);
3718 rule
= rule_dpif_lookup_from_table(ctx
->xbridge
->ofproto
,
3719 ctx
->xin
->tables_version
,
3720 &ctx
->xin
->flow
, ctx
->wc
,
3721 ctx
->xin
->resubmit_stats
,
3722 &ctx
->table_id
, in_port
,
3723 may_packet_in
, honor_table_miss
,
3727 tuple_swap(&ctx
->xin
->flow
, ctx
->wc
);
3731 /* Fill in the cache entry here instead of xlate_recursively
3732 * to make the reference counting more explicit. We take a
3733 * reference in the lookups above if we are going to cache the
3735 if (ctx
->xin
->xcache
) {
3736 struct xc_entry
*entry
;
3738 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_RULE
);
3740 ofproto_rule_ref(&rule
->up
);
3743 struct ovs_list
*old_trace
= ctx
->xin
->trace
;
3744 xlate_report_table(ctx
, rule
, table_id
);
3745 xlate_recursively(ctx
, rule
, table_id
<= old_table_id
);
3746 ctx
->xin
->trace
= old_trace
;
3749 ctx
->table_id
= old_table_id
;
3754 /* Consumes the group reference, which is only taken if xcache exists. */
3756 xlate_group_stats(struct xlate_ctx
*ctx
, struct group_dpif
*group
,
3757 struct ofputil_bucket
*bucket
)
3759 if (ctx
->xin
->resubmit_stats
) {
3760 group_dpif_credit_stats(group
, bucket
, ctx
->xin
->resubmit_stats
);
3762 if (ctx
->xin
->xcache
) {
3763 struct xc_entry
*entry
;
3765 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_GROUP
);
3766 entry
->group
.group
= group
;
3767 entry
->group
.bucket
= bucket
;
3772 xlate_group_bucket(struct xlate_ctx
*ctx
, struct ofputil_bucket
*bucket
)
3774 uint64_t action_list_stub
[1024 / 8];
3775 struct ofpbuf action_list
= OFPBUF_STUB_INITIALIZER(action_list_stub
);
3776 struct ofpbuf action_set
= ofpbuf_const_initializer(bucket
->ofpacts
,
3777 bucket
->ofpacts_len
);
3778 struct flow old_flow
= ctx
->xin
->flow
;
3779 bool old_was_mpls
= ctx
->was_mpls
;
3781 ofpacts_execute_action_set(&action_list
, &action_set
);
3783 do_xlate_actions(action_list
.data
, action_list
.size
, ctx
);
3786 ofpbuf_uninit(&action_list
);
3788 /* Check if need to freeze. */
3789 if (ctx
->freezing
) {
3790 finish_freezing(ctx
);
3793 /* Roll back flow to previous state.
3794 * This is equivalent to cloning the packet for each bucket.
3796 * As a side effect any subsequently applied actions will
3797 * also effectively be applied to a clone of the packet taken
3798 * just before applying the all or indirect group.
3800 * Note that group buckets are action sets, hence they cannot modify the
3801 * main action set. Also any stack actions are ignored when executing an
3802 * action set, so group buckets cannot change the stack either.
3803 * However, we do allow resubmit actions in group buckets, which could
3804 * break the above assumptions. It is up to the controller to not mess up
3805 * with the action_set and stack in the tables resubmitted to from
3807 ctx
->xin
->flow
= old_flow
;
3809 /* The group bucket popping MPLS should have no effect after bucket
3811 ctx
->was_mpls
= old_was_mpls
;
3813 /* The fact that the group bucket exits (for any reason) does not mean that
3814 * the translation after the group action should exit. Specifically, if
3815 * the group bucket freezes translation, the actions after the group action
3816 * must continue processing with the original, not the frozen packet! */
3821 xlate_all_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
3823 struct ofputil_bucket
*bucket
;
3824 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
3825 xlate_group_bucket(ctx
, bucket
);
3827 xlate_group_stats(ctx
, group
, NULL
);
3831 xlate_ff_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
3833 struct ofputil_bucket
*bucket
;
3835 bucket
= group_first_live_bucket(ctx
, group
, 0);
3837 xlate_group_bucket(ctx
, bucket
);
3838 xlate_group_stats(ctx
, group
, bucket
);
3839 } else if (ctx
->xin
->xcache
) {
3840 ofproto_group_unref(&group
->up
);
3845 xlate_default_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
3847 struct flow_wildcards
*wc
= ctx
->wc
;
3848 struct ofputil_bucket
*bucket
;
3851 basis
= flow_hash_symmetric_l4(&ctx
->xin
->flow
, 0);
3852 flow_mask_hash_fields(&ctx
->xin
->flow
, wc
, NX_HASH_FIELDS_SYMMETRIC_L4
);
3853 bucket
= group_best_live_bucket(ctx
, group
, basis
);
3855 xlate_group_bucket(ctx
, bucket
);
3856 xlate_group_stats(ctx
, group
, bucket
);
3857 } else if (ctx
->xin
->xcache
) {
3858 ofproto_group_unref(&group
->up
);
3863 xlate_hash_fields_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
3865 const struct field_array
*fields
= &group
->up
.props
.fields
;
3866 const uint8_t *mask_values
= fields
->values
;
3867 uint32_t basis
= hash_uint64(group
->up
.props
.selection_method_param
);
3870 BITMAP_FOR_EACH_1 (i
, MFF_N_IDS
, fields
->used
.bm
) {
3871 const struct mf_field
*mf
= mf_from_id(i
);
3873 /* Skip fields for which prerequisities are not met. */
3874 if (!mf_are_prereqs_ok(mf
, &ctx
->xin
->flow
, ctx
->wc
)) {
3875 /* Skip the mask bytes for this field. */
3876 mask_values
+= mf
->n_bytes
;
3880 union mf_value value
;
3881 union mf_value mask
;
3883 mf_get_value(mf
, &ctx
->xin
->flow
, &value
);
3884 /* Mask the value. */
3885 for (int j
= 0; j
< mf
->n_bytes
; j
++) {
3886 mask
.b
[j
] = *mask_values
++;
3887 value
.b
[j
] &= mask
.b
[j
];
3889 basis
= hash_bytes(&value
, mf
->n_bytes
, basis
);
3891 /* For tunnels, hash in whether the field is present. */
3892 if (mf_is_tun_metadata(mf
)) {
3893 basis
= hash_boolean(mf_is_set(mf
, &ctx
->xin
->flow
), basis
);
3896 mf_mask_field_masked(mf
, &mask
, ctx
->wc
);
3899 struct ofputil_bucket
*bucket
= group_best_live_bucket(ctx
, group
, basis
);
3901 xlate_group_bucket(ctx
, bucket
);
3902 xlate_group_stats(ctx
, group
, bucket
);
3903 } else if (ctx
->xin
->xcache
) {
3904 ofproto_group_unref(&group
->up
);
3909 xlate_dp_hash_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
3911 struct ofputil_bucket
*bucket
;
3913 /* dp_hash value 0 is special since it means that the dp_hash has not been
3914 * computed, as all computed dp_hash values are non-zero. Therefore
3915 * compare to zero can be used to decide if the dp_hash value is valid
3916 * without masking the dp_hash field. */
3917 if (!ctx
->xin
->flow
.dp_hash
) {
3918 uint64_t param
= group
->up
.props
.selection_method_param
;
3920 ctx_trigger_recirculate_with_hash(ctx
, param
>> 32, (uint32_t)param
);
3922 uint32_t n_buckets
= group
->up
.n_buckets
;
3924 /* Minimal mask to cover the number of buckets. */
3925 uint32_t mask
= (1 << log_2_ceil(n_buckets
)) - 1;
3926 /* Multiplier chosen to make the trivial 1 bit case to
3927 * actually distribute amongst two equal weight buckets. */
3928 uint32_t basis
= 0xc2b73583 * (ctx
->xin
->flow
.dp_hash
& mask
);
3930 ctx
->wc
->masks
.dp_hash
|= mask
;
3931 bucket
= group_best_live_bucket(ctx
, group
, basis
);
3933 xlate_group_bucket(ctx
, bucket
);
3934 xlate_group_stats(ctx
, group
, bucket
);
3941 xlate_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
3943 const char *selection_method
= group
->up
.props
.selection_method
;
3945 /* Select groups may access flow keys beyond L2 in order to
3946 * select a bucket. Recirculate as appropriate to make this possible.
3948 if (ctx
->was_mpls
) {
3949 ctx_trigger_freeze(ctx
);
3952 if (selection_method
[0] == '\0') {
3953 xlate_default_select_group(ctx
, group
);
3954 } else if (!strcasecmp("hash", selection_method
)) {
3955 xlate_hash_fields_select_group(ctx
, group
);
3956 } else if (!strcasecmp("dp_hash", selection_method
)) {
3957 xlate_dp_hash_select_group(ctx
, group
);
3959 /* Parsing of groups should ensure this never happens */
3965 xlate_group_action__(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
3967 bool was_in_group
= ctx
->in_group
;
3968 ctx
->in_group
= true;
3970 switch (group
->up
.type
) {
3972 case OFPGT11_INDIRECT
:
3973 xlate_all_group(ctx
, group
);
3975 case OFPGT11_SELECT
:
3976 xlate_select_group(ctx
, group
);
3979 xlate_ff_group(ctx
, group
);
3985 ctx
->in_group
= was_in_group
;
3989 xlate_group_action(struct xlate_ctx
*ctx
, uint32_t group_id
)
3991 if (xlate_resubmit_resource_check(ctx
)) {
3992 struct group_dpif
*group
;
3994 /* Take ref only if xcache exists. */
3995 group
= group_dpif_lookup(ctx
->xbridge
->ofproto
, group_id
,
3996 ctx
->xin
->tables_version
, ctx
->xin
->xcache
);
3998 /* XXX: Should set ctx->error ? */
3999 xlate_report(ctx
, OFT_WARN
, "output to nonexistent group %"PRIu32
,
4003 xlate_group_action__(ctx
, group
);
4010 xlate_ofpact_resubmit(struct xlate_ctx
*ctx
,
4011 const struct ofpact_resubmit
*resubmit
)
4015 bool may_packet_in
= false;
4016 bool honor_table_miss
= false;
4018 if (ctx
->rule
&& rule_dpif_is_internal(ctx
->rule
)) {
4019 /* Still allow missed packets to be sent to the controller
4020 * if resubmitting from an internal table. */
4021 may_packet_in
= true;
4022 honor_table_miss
= true;
4025 in_port
= resubmit
->in_port
;
4026 if (in_port
== OFPP_IN_PORT
) {
4027 in_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
4030 table_id
= resubmit
->table_id
;
4031 if (table_id
== 255) {
4032 table_id
= ctx
->table_id
;
4035 xlate_table_action(ctx
, in_port
, table_id
, may_packet_in
,
4036 honor_table_miss
, resubmit
->with_ct_orig
);
4040 flood_packets(struct xlate_ctx
*ctx
, bool all
)
4042 const struct xport
*xport
;
4044 HMAP_FOR_EACH (xport
, ofp_node
, &ctx
->xbridge
->xports
) {
4045 if (xport
->ofp_port
== ctx
->xin
->flow
.in_port
.ofp_port
) {
4050 compose_output_action__(ctx
, xport
->ofp_port
, NULL
, false);
4051 } else if (!(xport
->config
& OFPUTIL_PC_NO_FLOOD
)) {
4052 compose_output_action(ctx
, xport
->ofp_port
, NULL
);
4056 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
4059 /* Copy and reformat a partially xlated odp actions to a new
4060 * odp actions list in 'b', so that the new actions list
4061 * can be executed by odp_execute_actions.
4063 * When xlate using nested odp actions, such as sample and clone,
4064 * the nested action created by nl_msg_start_nested() may not
4065 * have been properly closed yet, thus can not be executed
4068 * Since unclosed nested action has to be last action, it can be
4069 * fixed by skipping the outer header, and treating the actions within
4070 * as if they are outside the nested attribute since the effect
4071 * of executing them on packet is the same.
4073 * As an optimization, a fully closed 'sample' or 'clone' action
4074 * is skipped since their execution has no effect to the packet.
4076 * Returns true if success. 'b' contains the new actions list.
4077 * The caller is responsible for disposing 'b'.
4079 * Returns false if error, 'b' has been freed already. */
4081 xlate_fixup_actions(struct ofpbuf
*b
, const struct nlattr
*actions
,
4084 const struct nlattr
*a
;
4087 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, actions
, actions_len
) {
4088 int type
= nl_attr_type(a
);
4090 switch ((enum ovs_action_attr
) type
) {
4091 case OVS_ACTION_ATTR_HASH
:
4092 case OVS_ACTION_ATTR_PUSH_VLAN
:
4093 case OVS_ACTION_ATTR_POP_VLAN
:
4094 case OVS_ACTION_ATTR_PUSH_MPLS
:
4095 case OVS_ACTION_ATTR_POP_MPLS
:
4096 case OVS_ACTION_ATTR_SET
:
4097 case OVS_ACTION_ATTR_SET_MASKED
:
4098 case OVS_ACTION_ATTR_TRUNC
:
4099 case OVS_ACTION_ATTR_OUTPUT
:
4100 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
4101 case OVS_ACTION_ATTR_TUNNEL_POP
:
4102 case OVS_ACTION_ATTR_USERSPACE
:
4103 case OVS_ACTION_ATTR_RECIRC
:
4104 case OVS_ACTION_ATTR_CT
:
4105 case OVS_ACTION_ATTR_PUSH_ETH
:
4106 case OVS_ACTION_ATTR_POP_ETH
:
4107 case OVS_ACTION_ATTR_METER
:
4108 ofpbuf_put(b
, a
, nl_attr_len_pad(a
, left
));
4111 case OVS_ACTION_ATTR_CLONE
:
4112 /* If the clone action has been fully xlated, it can
4113 * be skipped, since any actions executed within clone
4114 * do not affect the current packet.
4116 * When xlating actions within clone, the clone action,
4117 * because it is an nested netlink attribute, do not have
4118 * a valid 'nla_len'; it will be zero instead. Skip
4119 * the clone header to find the start of the actions
4120 * enclosed. Treat those actions as if they are written
4121 * outside of clone. */
4124 if (left
< NLA_HDRLEN
) {
4128 ok
= xlate_fixup_actions(b
, nl_attr_get_unspec(a
, 0),
4136 case OVS_ACTION_ATTR_SAMPLE
:
4139 if (left
< NLA_HDRLEN
) {
4142 const struct nlattr
*attr
= nl_attr_get_unspec(a
, 0);
4146 nl_attr_type(attr
) != OVS_SAMPLE_ATTR_ACTIONS
) {
4147 /* Only OVS_SAMPLE_ATTR_ACTIONS can have unclosed
4148 * nested netlink attribute. */
4149 if (!attr
->nla_len
) {
4153 left
-= NLA_ALIGN(attr
->nla_len
);
4154 attr
= nl_attr_next(attr
);
4157 if (left
< NLA_HDRLEN
) {
4161 ok
= xlate_fixup_actions(b
, nl_attr_get_unspec(attr
, 0),
4169 case OVS_ACTION_ATTR_UNSPEC
:
4170 case __OVS_ACTION_ATTR_MAX
:
4183 xlate_execute_odp_actions(struct dp_packet
*packet
,
4184 const struct nlattr
*actions
, int actions_len
)
4186 struct dp_packet_batch batch
;
4187 struct ofpbuf
*b
= ofpbuf_new(actions_len
);
4189 if (!xlate_fixup_actions(b
, actions
, actions_len
)) {
4193 dp_packet_batch_init_packet(&batch
, packet
);
4194 odp_execute_actions(NULL
, &batch
, false, b
->data
, b
->size
, NULL
);
4201 execute_controller_action(struct xlate_ctx
*ctx
, int len
,
4202 enum ofp_packet_in_reason reason
,
4203 uint16_t controller_id
,
4204 const uint8_t *userdata
, size_t userdata_len
)
4206 struct dp_packet
*packet
;
4208 ctx
->xout
->slow
|= SLOW_CONTROLLER
;
4209 xlate_commit_actions(ctx
);
4210 if (!ctx
->xin
->packet
) {
4214 if (!ctx
->xin
->allow_side_effects
&& !ctx
->xin
->xcache
) {
4218 packet
= dp_packet_clone(ctx
->xin
->packet
);
4219 if (!xlate_execute_odp_actions(packet
, ctx
->odp_actions
->data
,
4220 ctx
->odp_actions
->size
)) {
4221 xlate_report_error(ctx
, "Failed to execute controller action");
4222 dp_packet_delete(packet
);
4225 /* A packet sent by an action in a table-miss rule is considered an
4226 * explicit table miss. OpenFlow before 1.3 doesn't have that concept so
4227 * it will get translated back to OFPR_ACTION for those versions. */
4228 if (reason
== OFPR_ACTION
4229 && ctx
->rule
&& rule_is_table_miss(&ctx
->rule
->up
)) {
4230 reason
= OFPR_EXPLICIT_MISS
;
4233 size_t packet_len
= dp_packet_size(packet
);
4235 struct ofproto_async_msg
*am
= xmalloc(sizeof *am
);
4236 *am
= (struct ofproto_async_msg
) {
4237 .controller_id
= controller_id
,
4238 .oam
= OAM_PACKET_IN
,
4242 .packet
= dp_packet_steal_data(packet
),
4243 .packet_len
= packet_len
,
4245 .table_id
= ctx
->table_id
,
4246 .cookie
= ctx
->rule_cookie
,
4247 .userdata
= (userdata_len
4248 ? xmemdup(userdata
, userdata_len
)
4250 .userdata_len
= userdata_len
,
4256 flow_get_metadata(&ctx
->xin
->flow
, &am
->pin
.up
.public.flow_metadata
);
4258 /* Async messages are only sent once, so if we send one now, no
4259 * xlate cache entry is created. */
4260 if (ctx
->xin
->allow_side_effects
) {
4261 ofproto_dpif_send_async_msg(ctx
->xbridge
->ofproto
, am
);
4262 } else /* xcache */ {
4263 struct xc_entry
*entry
;
4265 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_CONTROLLER
);
4266 entry
->controller
.ofproto
= ctx
->xbridge
->ofproto
;
4267 entry
->controller
.am
= am
;
4270 dp_packet_delete(packet
);
4274 emit_continuation(struct xlate_ctx
*ctx
, const struct frozen_state
*state
)
4276 if (!ctx
->xin
->allow_side_effects
&& !ctx
->xin
->xcache
) {
4280 struct ofproto_async_msg
*am
= xmalloc(sizeof *am
);
4281 *am
= (struct ofproto_async_msg
) {
4282 .controller_id
= ctx
->pause
->controller_id
,
4283 .oam
= OAM_PACKET_IN
,
4287 .userdata
= xmemdup(ctx
->pause
->userdata
,
4288 ctx
->pause
->userdata_len
),
4289 .userdata_len
= ctx
->pause
->userdata_len
,
4290 .packet
= xmemdup(dp_packet_data(ctx
->xin
->packet
),
4291 dp_packet_size(ctx
->xin
->packet
)),
4292 .packet_len
= dp_packet_size(ctx
->xin
->packet
),
4293 .reason
= ctx
->pause
->reason
,
4295 .bridge
= ctx
->xbridge
->ofproto
->uuid
,
4296 .stack
= xmemdup(state
->stack
, state
->stack_size
),
4297 .stack_size
= state
->stack_size
,
4298 .mirrors
= state
->mirrors
,
4299 .conntracked
= state
->conntracked
,
4300 .actions
= xmemdup(state
->ofpacts
, state
->ofpacts_len
),
4301 .actions_len
= state
->ofpacts_len
,
4302 .action_set
= xmemdup(state
->action_set
,
4303 state
->action_set_len
),
4304 .action_set_len
= state
->action_set_len
,
4306 .max_len
= UINT16_MAX
,
4309 flow_get_metadata(&ctx
->xin
->flow
, &am
->pin
.up
.public.flow_metadata
);
4311 /* Async messages are only sent once, so if we send one now, no
4312 * xlate cache entry is created. */
4313 if (ctx
->xin
->allow_side_effects
) {
4314 ofproto_dpif_send_async_msg(ctx
->xbridge
->ofproto
, am
);
4315 } else /* xcache */ {
4316 struct xc_entry
*entry
;
4318 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_CONTROLLER
);
4319 entry
->controller
.ofproto
= ctx
->xbridge
->ofproto
;
4320 entry
->controller
.am
= am
;
4325 finish_freezing__(struct xlate_ctx
*ctx
, uint8_t table
)
4327 ovs_assert(ctx
->freezing
);
4329 struct frozen_state state
= {
4331 .ofproto_uuid
= ctx
->xbridge
->ofproto
->uuid
,
4332 .stack
= ctx
->stack
.data
,
4333 .stack_size
= ctx
->stack
.size
,
4334 .mirrors
= ctx
->mirrors
,
4335 .conntracked
= ctx
->conntracked
,
4336 .ofpacts
= ctx
->frozen_actions
.data
,
4337 .ofpacts_len
= ctx
->frozen_actions
.size
,
4338 .action_set
= ctx
->action_set
.data
,
4339 .action_set_len
= ctx
->action_set
.size
,
4341 frozen_metadata_from_flow(&state
.metadata
, &ctx
->xin
->flow
);
4344 if (ctx
->xin
->packet
) {
4345 emit_continuation(ctx
, &state
);
4348 /* Allocate a unique recirc id for the given metadata state in the
4349 * flow. An existing id, with a new reference to the corresponding
4350 * recirculation context, will be returned if possible.
4351 * The life-cycle of this recirc id is managed by associating it
4352 * with the udpif key ('ukey') created for each new datapath flow. */
4353 uint32_t id
= recirc_alloc_id_ctx(&state
);
4355 xlate_report_error(ctx
, "Failed to allocate recirculation id");
4356 ctx
->error
= XLATE_NO_RECIRCULATION_CONTEXT
;
4359 recirc_refs_add(&ctx
->xout
->recircs
, id
);
4361 if (ctx
->recirc_update_dp_hash
) {
4362 struct ovs_action_hash
*act_hash
;
4365 act_hash
= nl_msg_put_unspec_uninit(ctx
->odp_actions
,
4366 OVS_ACTION_ATTR_HASH
,
4368 act_hash
->hash_alg
= OVS_HASH_ALG_L4
; /* Make configurable. */
4369 act_hash
->hash_basis
= 0; /* Make configurable. */
4371 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_RECIRC
, id
);
4374 /* Undo changes done by freezing. */
4375 ctx_cancel_freeze(ctx
);
4378 /* Called only when we're freezing. */
4380 finish_freezing(struct xlate_ctx
*ctx
)
4382 xlate_commit_actions(ctx
);
4383 finish_freezing__(ctx
, 0);
4386 /* Fork the pipeline here. The current packet will continue processing the
4387 * current action list. A clone of the current packet will recirculate, skip
4388 * the remainder of the current action list and asynchronously resume pipeline
4389 * processing in 'table' with the current metadata and action set. */
4391 compose_recirculate_and_fork(struct xlate_ctx
*ctx
, uint8_t table
)
4393 ctx
->freezing
= true;
4394 finish_freezing__(ctx
, table
);
4398 compose_mpls_push_action(struct xlate_ctx
*ctx
, struct ofpact_push_mpls
*mpls
)
4400 struct flow
*flow
= &ctx
->xin
->flow
;
4403 ovs_assert(eth_type_mpls(mpls
->ethertype
));
4405 n
= flow_count_mpls_labels(flow
, ctx
->wc
);
4407 xlate_commit_actions(ctx
);
4408 } else if (n
>= FLOW_MAX_MPLS_LABELS
) {
4409 if (ctx
->xin
->packet
!= NULL
) {
4410 xlate_report_error(ctx
, "dropping packet on which an MPLS push "
4411 "action can't be performed as it would have "
4412 "more MPLS LSEs than the %d supported.",
4413 FLOW_MAX_MPLS_LABELS
);
4415 ctx
->error
= XLATE_TOO_MANY_MPLS_LABELS
;
4419 /* Update flow's MPLS stack, and clear L3/4 fields to mark them invalid. */
4420 flow_push_mpls(flow
, n
, mpls
->ethertype
, ctx
->wc
, true);
4424 compose_mpls_pop_action(struct xlate_ctx
*ctx
, ovs_be16 eth_type
)
4426 struct flow
*flow
= &ctx
->xin
->flow
;
4427 int n
= flow_count_mpls_labels(flow
, ctx
->wc
);
4429 if (flow_pop_mpls(flow
, n
, eth_type
, ctx
->wc
)) {
4430 if (!eth_type_mpls(eth_type
) && ctx
->xbridge
->support
.odp
.recirc
) {
4431 ctx
->was_mpls
= true;
4433 } else if (n
>= FLOW_MAX_MPLS_LABELS
) {
4434 if (ctx
->xin
->packet
!= NULL
) {
4435 xlate_report_error(ctx
, "dropping packet on which an "
4436 "MPLS pop action can't be performed as it has "
4437 "more MPLS LSEs than the %d supported.",
4438 FLOW_MAX_MPLS_LABELS
);
4440 ctx
->error
= XLATE_TOO_MANY_MPLS_LABELS
;
4441 ofpbuf_clear(ctx
->odp_actions
);
4446 compose_dec_ttl(struct xlate_ctx
*ctx
, struct ofpact_cnt_ids
*ids
)
4448 struct flow
*flow
= &ctx
->xin
->flow
;
4450 if (!is_ip_any(flow
)) {
4454 ctx
->wc
->masks
.nw_ttl
= 0xff;
4455 if (flow
->nw_ttl
> 1) {
4461 for (i
= 0; i
< ids
->n_controllers
; i
++) {
4462 execute_controller_action(ctx
, UINT16_MAX
, OFPR_INVALID_TTL
,
4463 ids
->cnt_ids
[i
], NULL
, 0);
4466 /* Stop processing for current table. */
4467 xlate_report(ctx
, OFT_WARN
, "IPv%d decrement TTL exception",
4468 flow
->dl_type
== htons(ETH_TYPE_IP
) ? 4 : 6);
4474 compose_set_mpls_label_action(struct xlate_ctx
*ctx
, ovs_be32 label
)
4476 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
4477 ctx
->wc
->masks
.mpls_lse
[0] |= htonl(MPLS_LABEL_MASK
);
4478 set_mpls_lse_label(&ctx
->xin
->flow
.mpls_lse
[0], label
);
4483 compose_set_mpls_tc_action(struct xlate_ctx
*ctx
, uint8_t tc
)
4485 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
4486 ctx
->wc
->masks
.mpls_lse
[0] |= htonl(MPLS_TC_MASK
);
4487 set_mpls_lse_tc(&ctx
->xin
->flow
.mpls_lse
[0], tc
);
4492 compose_set_mpls_ttl_action(struct xlate_ctx
*ctx
, uint8_t ttl
)
4494 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
4495 ctx
->wc
->masks
.mpls_lse
[0] |= htonl(MPLS_TTL_MASK
);
4496 set_mpls_lse_ttl(&ctx
->xin
->flow
.mpls_lse
[0], ttl
);
4501 compose_dec_mpls_ttl_action(struct xlate_ctx
*ctx
)
4503 struct flow
*flow
= &ctx
->xin
->flow
;
4505 if (eth_type_mpls(flow
->dl_type
)) {
4506 uint8_t ttl
= mpls_lse_to_ttl(flow
->mpls_lse
[0]);
4508 ctx
->wc
->masks
.mpls_lse
[0] |= htonl(MPLS_TTL_MASK
);
4511 set_mpls_lse_ttl(&flow
->mpls_lse
[0], ttl
);
4514 execute_controller_action(ctx
, UINT16_MAX
, OFPR_INVALID_TTL
, 0,
4519 /* Stop processing for current table. */
4520 xlate_report(ctx
, OFT_WARN
, "MPLS decrement TTL exception");
4525 xlate_output_action(struct xlate_ctx
*ctx
,
4526 ofp_port_t port
, uint16_t max_len
, bool may_packet_in
)
4528 ofp_port_t prev_nf_output_iface
= ctx
->nf_output_iface
;
4530 ctx
->nf_output_iface
= NF_OUT_DROP
;
4534 compose_output_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
, NULL
);
4537 xlate_table_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
,
4538 0, may_packet_in
, true, false);
4544 flood_packets(ctx
, false);
4547 flood_packets(ctx
, true);
4549 case OFPP_CONTROLLER
:
4550 execute_controller_action(ctx
, max_len
,
4551 (ctx
->in_packet_out
? OFPR_PACKET_OUT
4552 : ctx
->in_group
? OFPR_GROUP
4553 : ctx
->in_action_set
? OFPR_ACTION_SET
4561 if (port
!= ctx
->xin
->flow
.in_port
.ofp_port
) {
4562 compose_output_action(ctx
, port
, NULL
);
4564 xlate_report(ctx
, OFT_WARN
, "skipping output to input port");
4569 if (prev_nf_output_iface
== NF_OUT_FLOOD
) {
4570 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
4571 } else if (ctx
->nf_output_iface
== NF_OUT_DROP
) {
4572 ctx
->nf_output_iface
= prev_nf_output_iface
;
4573 } else if (prev_nf_output_iface
!= NF_OUT_DROP
&&
4574 ctx
->nf_output_iface
!= NF_OUT_FLOOD
) {
4575 ctx
->nf_output_iface
= NF_OUT_MULTI
;
4580 xlate_output_reg_action(struct xlate_ctx
*ctx
,
4581 const struct ofpact_output_reg
*or)
4583 uint64_t port
= mf_get_subfield(&or->src
, &ctx
->xin
->flow
);
4584 if (port
<= UINT16_MAX
) {
4585 xlate_report(ctx
, OFT_DETAIL
, "output port is %"PRIu64
, port
);
4587 union mf_subvalue value
;
4589 memset(&value
, 0xff, sizeof value
);
4590 mf_write_subfield_flow(&or->src
, &value
, &ctx
->wc
->masks
);
4591 xlate_output_action(ctx
, u16_to_ofp(port
), or->max_len
, false);
4593 xlate_report(ctx
, OFT_WARN
, "output port %"PRIu64
" is out of range",
4599 xlate_output_trunc_action(struct xlate_ctx
*ctx
,
4600 ofp_port_t port
, uint32_t max_len
)
4602 bool support_trunc
= ctx
->xbridge
->support
.trunc
;
4603 struct ovs_action_trunc
*trunc
;
4604 char name
[OFP10_MAX_PORT_NAME_LEN
];
4611 case OFPP_CONTROLLER
:
4613 ofputil_port_to_string(port
, name
, sizeof name
);
4614 xlate_report(ctx
, OFT_WARN
,
4615 "output_trunc does not support port: %s", name
);
4620 if (port
!= ctx
->xin
->flow
.in_port
.ofp_port
) {
4621 const struct xport
*xport
= get_ofp_port(ctx
->xbridge
, port
);
4623 if (xport
== NULL
|| xport
->odp_port
== ODPP_NONE
) {
4624 /* Since truncate happens at its following output action, if
4625 * the output port is a patch port, the behavior is somehow
4626 * unpredicable. For simpilicity, disallow this case. */
4627 ofputil_port_to_string(port
, name
, sizeof name
);
4628 xlate_report_error(ctx
, "output_trunc does not support "
4629 "patch port %s", name
);
4633 trunc
= nl_msg_put_unspec_uninit(ctx
->odp_actions
,
4634 OVS_ACTION_ATTR_TRUNC
,
4636 trunc
->max_len
= max_len
;
4637 xlate_output_action(ctx
, port
, max_len
, false);
4638 if (!support_trunc
) {
4639 ctx
->xout
->slow
|= SLOW_ACTION
;
4642 xlate_report(ctx
, OFT_WARN
, "skipping output to input port");
4649 xlate_enqueue_action(struct xlate_ctx
*ctx
,
4650 const struct ofpact_enqueue
*enqueue
)
4652 ofp_port_t ofp_port
= enqueue
->port
;
4653 uint32_t queue_id
= enqueue
->queue
;
4654 uint32_t flow_priority
, priority
;
4657 /* Translate queue to priority. */
4658 error
= dpif_queue_to_priority(ctx
->xbridge
->dpif
, queue_id
, &priority
);
4660 /* Fall back to ordinary output action. */
4661 xlate_output_action(ctx
, enqueue
->port
, 0, false);
4665 /* Check output port. */
4666 if (ofp_port
== OFPP_IN_PORT
) {
4667 ofp_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
4668 } else if (ofp_port
== ctx
->xin
->flow
.in_port
.ofp_port
) {
4672 /* Add datapath actions. */
4673 flow_priority
= ctx
->xin
->flow
.skb_priority
;
4674 ctx
->xin
->flow
.skb_priority
= priority
;
4675 compose_output_action(ctx
, ofp_port
, NULL
);
4676 ctx
->xin
->flow
.skb_priority
= flow_priority
;
4678 /* Update NetFlow output port. */
4679 if (ctx
->nf_output_iface
== NF_OUT_DROP
) {
4680 ctx
->nf_output_iface
= ofp_port
;
4681 } else if (ctx
->nf_output_iface
!= NF_OUT_FLOOD
) {
4682 ctx
->nf_output_iface
= NF_OUT_MULTI
;
4687 xlate_set_queue_action(struct xlate_ctx
*ctx
, uint32_t queue_id
)
4689 uint32_t skb_priority
;
4691 if (!dpif_queue_to_priority(ctx
->xbridge
->dpif
, queue_id
, &skb_priority
)) {
4692 ctx
->xin
->flow
.skb_priority
= skb_priority
;
4694 /* Couldn't translate queue to a priority. Nothing to do. A warning
4695 * has already been logged. */
4700 slave_enabled_cb(ofp_port_t ofp_port
, void *xbridge_
)
4702 const struct xbridge
*xbridge
= xbridge_
;
4713 case OFPP_CONTROLLER
: /* Not supported by the bundle action. */
4716 port
= get_ofp_port(xbridge
, ofp_port
);
4717 return port
? port
->may_enable
: false;
4722 xlate_bundle_action(struct xlate_ctx
*ctx
,
4723 const struct ofpact_bundle
*bundle
)
4727 port
= bundle_execute(bundle
, &ctx
->xin
->flow
, ctx
->wc
, slave_enabled_cb
,
4728 CONST_CAST(struct xbridge
*, ctx
->xbridge
));
4729 if (bundle
->dst
.field
) {
4730 nxm_reg_load(&bundle
->dst
, ofp_to_u16(port
), &ctx
->xin
->flow
, ctx
->wc
);
4731 xlate_report_subfield(ctx
, &bundle
->dst
);
4733 xlate_output_action(ctx
, port
, 0, false);
4738 xlate_learn_action(struct xlate_ctx
*ctx
, const struct ofpact_learn
*learn
)
4740 learn_mask(learn
, ctx
->wc
);
4742 if (ctx
->xin
->xcache
|| ctx
->xin
->allow_side_effects
) {
4743 uint64_t ofpacts_stub
[1024 / 8];
4744 struct ofputil_flow_mod fm
;
4745 struct ofproto_flow_mod ofm__
, *ofm
;
4746 struct ofpbuf ofpacts
;
4749 if (ctx
->xin
->xcache
) {
4750 ofm
= xmalloc(sizeof *ofm
);
4755 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
4756 learn_execute(learn
, &ctx
->xin
->flow
, &fm
, &ofpacts
);
4757 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
4758 struct ds s
= DS_EMPTY_INITIALIZER
;
4759 ds_put_format(&s
, "table=%"PRIu8
" ", fm
.table_id
);
4760 match_format(&fm
.match
, &s
, OFP_DEFAULT_PRIORITY
);
4762 ds_put_format(&s
, " priority=%d", fm
.priority
);
4763 if (fm
.new_cookie
) {
4764 ds_put_format(&s
, " cookie=%#"PRIx64
, ntohll(fm
.new_cookie
));
4766 if (fm
.idle_timeout
!= OFP_FLOW_PERMANENT
) {
4767 ds_put_format(&s
, " idle=%"PRIu16
, fm
.idle_timeout
);
4769 if (fm
.hard_timeout
!= OFP_FLOW_PERMANENT
) {
4770 ds_put_format(&s
, " hard=%"PRIu16
, fm
.hard_timeout
);
4772 if (fm
.flags
& NX_LEARN_F_SEND_FLOW_REM
) {
4773 ds_put_cstr(&s
, " send_flow_rem");
4775 ds_put_cstr(&s
, " actions=");
4776 ofpacts_format(fm
.ofpacts
, fm
.ofpacts_len
, &s
);
4777 xlate_report(ctx
, OFT_DETAIL
, "%s", ds_cstr(&s
));
4780 error
= ofproto_dpif_flow_mod_init_for_learn(ctx
->xbridge
->ofproto
,
4782 ofpbuf_uninit(&ofpacts
);
4785 bool success
= true;
4786 if (ctx
->xin
->allow_side_effects
) {
4787 error
= ofproto_flow_mod_learn(ofm
, ctx
->xin
->xcache
!= NULL
,
4788 learn
->limit
, &success
);
4789 } else if (learn
->limit
) {
4791 || ofm
->temp_rule
->state
!= RULE_INSERTED
) {
4792 /* The learned rule expired and there are no packets, so
4793 * we cannot learn again. Since the translated actions
4794 * depend on the result of learning, we tell the caller
4795 * that there's no point in caching this result. */
4796 ctx
->xout
->avoid_caching
= true;
4800 if (learn
->flags
& NX_LEARN_F_WRITE_RESULT
) {
4801 nxm_reg_load(&learn
->result_dst
, success
? 1 : 0,
4802 &ctx
->xin
->flow
, ctx
->wc
);
4803 xlate_report_subfield(ctx
, &learn
->result_dst
);
4806 if (success
&& ctx
->xin
->xcache
) {
4807 struct xc_entry
*entry
;
4809 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_LEARN
);
4810 entry
->learn
.ofm
= ofm
;
4811 entry
->learn
.limit
= learn
->limit
;
4815 if (OVS_UNLIKELY(ctx
->xin
->trace
&& !success
)) {
4816 xlate_report(ctx
, OFT_DETAIL
, "Limit exceeded, learn failed");
4820 if (ctx
->xin
->xcache
) {
4825 xlate_report_error(ctx
, "LEARN action execution failed (%s).",
4826 ofperr_to_string(error
));
4829 xlate_report(ctx
, OFT_WARN
,
4830 "suppressing side effects, so learn action ignored");
4835 xlate_fin_timeout__(struct rule_dpif
*rule
, uint16_t tcp_flags
,
4836 uint16_t idle_timeout
, uint16_t hard_timeout
)
4838 if (tcp_flags
& (TCP_FIN
| TCP_RST
)) {
4839 ofproto_rule_reduce_timeouts(&rule
->up
, idle_timeout
, hard_timeout
);
4844 xlate_fin_timeout(struct xlate_ctx
*ctx
,
4845 const struct ofpact_fin_timeout
*oft
)
4848 if (ctx
->xin
->allow_side_effects
) {
4849 xlate_fin_timeout__(ctx
->rule
, ctx
->xin
->tcp_flags
,
4850 oft
->fin_idle_timeout
, oft
->fin_hard_timeout
);
4852 if (ctx
->xin
->xcache
) {
4853 struct xc_entry
*entry
;
4855 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_FIN_TIMEOUT
);
4856 /* XC_RULE already holds a reference on the rule, none is taken
4858 entry
->fin
.rule
= ctx
->rule
;
4859 entry
->fin
.idle
= oft
->fin_idle_timeout
;
4860 entry
->fin
.hard
= oft
->fin_hard_timeout
;
4866 xlate_sample_action(struct xlate_ctx
*ctx
,
4867 const struct ofpact_sample
*os
)
4869 odp_port_t output_odp_port
= ODPP_NONE
;
4870 odp_port_t tunnel_out_port
= ODPP_NONE
;
4871 struct dpif_ipfix
*ipfix
= ctx
->xbridge
->ipfix
;
4872 bool emit_set_tunnel
= false;
4874 if (!ipfix
|| ctx
->xin
->flow
.in_port
.ofp_port
== OFPP_NONE
) {
4878 /* Scale the probability from 16-bit to 32-bit while representing
4879 * the same percentage. */
4880 uint32_t probability
= (os
->probability
<< 16) | os
->probability
;
4882 if (!ctx
->xbridge
->support
.variable_length_userdata
) {
4883 xlate_report_error(ctx
, "ignoring NXAST_SAMPLE action because "
4884 "datapath lacks support (needs Linux 3.10+ or "
4885 "kernel module from OVS 1.11+)");
4889 /* If ofp_port in flow sample action is equel to ofp_port,
4890 * this sample action is a input port action. */
4891 if (os
->sampling_port
!= OFPP_NONE
&&
4892 os
->sampling_port
!= ctx
->xin
->flow
.in_port
.ofp_port
) {
4893 output_odp_port
= ofp_port_to_odp_port(ctx
->xbridge
,
4895 if (output_odp_port
== ODPP_NONE
) {
4896 xlate_report_error(ctx
, "can't use unknown port %d in flow sample "
4897 "action", os
->sampling_port
);
4901 if (dpif_ipfix_get_flow_exporter_tunnel_sampling(ipfix
,
4902 os
->collector_set_id
)
4903 && dpif_ipfix_get_tunnel_port(ipfix
, output_odp_port
)) {
4904 tunnel_out_port
= output_odp_port
;
4905 emit_set_tunnel
= true;
4909 xlate_commit_actions(ctx
);
4910 /* If 'emit_set_tunnel', sample(sampling_port=1) would translate
4911 * into datapath sample action set(tunnel(...)), sample(...) and
4912 * it is used for sampling egress tunnel information. */
4913 if (emit_set_tunnel
) {
4914 const struct xport
*xport
= get_ofp_port(ctx
->xbridge
,
4917 if (xport
&& xport
->is_tunnel
) {
4918 struct flow
*flow
= &ctx
->xin
->flow
;
4919 tnl_port_send(xport
->ofport
, flow
, ctx
->wc
);
4920 if (!ovs_native_tunneling_is_on(ctx
->xbridge
->ofproto
)) {
4921 struct flow_tnl flow_tnl
= flow
->tunnel
;
4923 commit_odp_tunnel_action(flow
, &ctx
->base_flow
,
4925 flow
->tunnel
= flow_tnl
;
4928 xlate_report_error(ctx
,
4929 "sampling_port:%d should be a tunnel port.",
4934 union user_action_cookie cookie
= {
4936 .type
= USER_ACTION_COOKIE_FLOW_SAMPLE
,
4937 .probability
= os
->probability
,
4938 .collector_set_id
= os
->collector_set_id
,
4939 .obs_domain_id
= os
->obs_domain_id
,
4940 .obs_point_id
= os
->obs_point_id
,
4941 .output_odp_port
= output_odp_port
,
4942 .direction
= os
->direction
,
4945 compose_sample_action(ctx
, probability
, &cookie
, sizeof cookie
.flow_sample
,
4946 tunnel_out_port
, false);
4949 /* Use datapath 'clone' or sample to enclose the translation of 'oc'. */
4951 compose_clone_action(struct xlate_ctx
*ctx
, const struct ofpact_nest
*oc
)
4953 size_t clone_offset
= nl_msg_start_nested(ctx
->odp_actions
,
4954 OVS_ACTION_ATTR_CLONE
);
4955 do_xlate_actions(oc
->actions
, ofpact_nest_get_action_len(oc
), ctx
);
4956 nl_msg_end_non_empty_nested(ctx
->odp_actions
, clone_offset
);
4959 /* Use datapath 'sample' action to translate clone. */
4961 compose_clone_action_using_sample(struct xlate_ctx
*ctx
,
4962 const struct ofpact_nest
*oc
)
4964 size_t offset
= nl_msg_start_nested(ctx
->odp_actions
,
4965 OVS_ACTION_ATTR_SAMPLE
);
4967 size_t ac_offset
= nl_msg_start_nested(ctx
->odp_actions
,
4968 OVS_SAMPLE_ATTR_ACTIONS
);
4970 do_xlate_actions(oc
->actions
, ofpact_nest_get_action_len(oc
), ctx
);
4972 if (nl_msg_end_non_empty_nested(ctx
->odp_actions
, ac_offset
)) {
4973 nl_msg_cancel_nested(ctx
->odp_actions
, offset
);
4975 nl_msg_put_u32(ctx
->odp_actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
4976 UINT32_MAX
); /* 100% probability. */
4977 nl_msg_end_nested(ctx
->odp_actions
, offset
);
4982 xlate_clone(struct xlate_ctx
*ctx
, const struct ofpact_nest
*oc
)
4984 bool old_was_mpls
= ctx
->was_mpls
;
4985 bool old_conntracked
= ctx
->conntracked
;
4986 struct flow old_flow
= ctx
->xin
->flow
;
4988 struct ofpbuf old_stack
= ctx
->stack
;
4989 union mf_subvalue new_stack
[1024 / sizeof(union mf_subvalue
)];
4990 ofpbuf_use_stub(&ctx
->stack
, new_stack
, sizeof new_stack
);
4991 ofpbuf_put(&ctx
->stack
, old_stack
.data
, old_stack
.size
);
4993 struct ofpbuf old_action_set
= ctx
->action_set
;
4994 uint64_t actset_stub
[1024 / 8];
4995 ofpbuf_use_stub(&ctx
->action_set
, actset_stub
, sizeof actset_stub
);
4996 ofpbuf_put(&ctx
->action_set
, old_action_set
.data
, old_action_set
.size
);
4998 /* Datapath clone action will make sure the pre clone packets
4999 * are used for actions after clone. Save and restore
5000 * ctx->base_flow to reflect this for the openflow pipeline. */
5001 if (ctx
->xbridge
->support
.clone
) {
5002 struct flow old_base_flow
= ctx
->base_flow
;
5003 compose_clone_action(ctx
, oc
);
5004 ctx
->base_flow
= old_base_flow
;
5005 } else if (ctx
->xbridge
->support
.sample_nesting
> 3) {
5006 /* Avoid generate sample action if datapath
5007 * only allow small number of nesting. Deeper nesting
5008 * can cause the datapath to reject the generated flow. */
5009 struct flow old_base_flow
= ctx
->base_flow
;
5010 compose_clone_action_using_sample(ctx
, oc
);
5011 ctx
->base_flow
= old_base_flow
;
5013 do_xlate_actions(oc
->actions
, ofpact_nest_get_action_len(oc
), ctx
);
5016 ofpbuf_uninit(&ctx
->action_set
);
5017 ctx
->action_set
= old_action_set
;
5019 ofpbuf_uninit(&ctx
->stack
);
5020 ctx
->stack
= old_stack
;
5022 ctx
->xin
->flow
= old_flow
;
5024 /* The clone's conntrack execution should have no effect on the original
5026 ctx
->conntracked
= old_conntracked
;
5028 /* Popping MPLS from the clone should have no effect on the original
5030 ctx
->was_mpls
= old_was_mpls
;
5034 xlate_meter_action(struct xlate_ctx
*ctx
, const struct ofpact_meter
*meter
)
5036 if (meter
->provider_meter_id
!= UINT32_MAX
) {
5037 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_METER
,
5038 meter
->provider_meter_id
);
5043 may_receive(const struct xport
*xport
, struct xlate_ctx
*ctx
)
5045 if (xport
->config
& (is_stp(&ctx
->xin
->flow
)
5046 ? OFPUTIL_PC_NO_RECV_STP
5047 : OFPUTIL_PC_NO_RECV
)) {
5051 /* Only drop packets here if both forwarding and learning are
5052 * disabled. If just learning is enabled, we need to have
5053 * OFPP_NORMAL and the learning action have a look at the packet
5054 * before we can drop it. */
5055 if ((!xport_stp_forward_state(xport
) && !xport_stp_learn_state(xport
)) ||
5056 (!xport_rstp_forward_state(xport
) && !xport_rstp_learn_state(xport
))) {
5064 xlate_write_actions__(struct xlate_ctx
*ctx
,
5065 const struct ofpact
*ofpacts
, size_t ofpacts_len
)
5067 /* Maintain actset_output depending on the contents of the action set:
5069 * - OFPP_UNSET, if there is no "output" action.
5071 * - The output port, if there is an "output" action and no "group"
5074 * - OFPP_UNSET, if there is a "group" action.
5076 if (!ctx
->action_set_has_group
) {
5077 const struct ofpact
*a
;
5078 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
5079 if (a
->type
== OFPACT_OUTPUT
) {
5080 ctx
->xin
->flow
.actset_output
= ofpact_get_OUTPUT(a
)->port
;
5081 } else if (a
->type
== OFPACT_GROUP
) {
5082 ctx
->xin
->flow
.actset_output
= OFPP_UNSET
;
5083 ctx
->action_set_has_group
= true;
5089 ofpbuf_put(&ctx
->action_set
, ofpacts
, ofpacts_len
);
5093 xlate_write_actions(struct xlate_ctx
*ctx
, const struct ofpact_nest
*a
)
5095 xlate_write_actions__(ctx
, a
->actions
, ofpact_nest_get_action_len(a
));
5099 xlate_action_set(struct xlate_ctx
*ctx
)
5101 uint64_t action_list_stub
[1024 / 8];
5102 struct ofpbuf action_list
= OFPBUF_STUB_INITIALIZER(action_list_stub
);
5103 ofpacts_execute_action_set(&action_list
, &ctx
->action_set
);
5104 /* Clear the action set, as it is not needed any more. */
5105 ofpbuf_clear(&ctx
->action_set
);
5106 if (action_list
.size
) {
5107 ctx
->in_action_set
= true;
5109 struct ovs_list
*old_trace
= ctx
->xin
->trace
;
5110 ctx
->xin
->trace
= xlate_report(ctx
, OFT_TABLE
,
5111 "--. Executing action set:");
5112 do_xlate_actions(action_list
.data
, action_list
.size
, ctx
);
5113 ctx
->xin
->trace
= old_trace
;
5115 ctx
->in_action_set
= false;
5117 ofpbuf_uninit(&action_list
);
5121 freeze_put_unroll_xlate(struct xlate_ctx
*ctx
)
5123 struct ofpact_unroll_xlate
*unroll
= ctx
->frozen_actions
.header
;
5125 /* Restore the table_id and rule cookie for a potential PACKET
5128 (ctx
->table_id
!= unroll
->rule_table_id
5129 || ctx
->rule_cookie
!= unroll
->rule_cookie
)) {
5130 unroll
= ofpact_put_UNROLL_XLATE(&ctx
->frozen_actions
);
5131 unroll
->rule_table_id
= ctx
->table_id
;
5132 unroll
->rule_cookie
= ctx
->rule_cookie
;
5133 ctx
->frozen_actions
.header
= unroll
;
5138 /* Copy actions 'a' through 'end' to ctx->frozen_actions, which will be
5139 * executed after thawing. Inserts an UNROLL_XLATE action, if none is already
5140 * present, before any action that may depend on the current table ID or flow
5143 freeze_unroll_actions(const struct ofpact
*a
, const struct ofpact
*end
,
5144 struct xlate_ctx
*ctx
)
5146 for (; a
< end
; a
= ofpact_next(a
)) {
5148 case OFPACT_OUTPUT_REG
:
5149 case OFPACT_OUTPUT_TRUNC
:
5152 case OFPACT_CONTROLLER
:
5153 case OFPACT_DEC_MPLS_TTL
:
5154 case OFPACT_DEC_TTL
:
5155 /* These actions may generate asynchronous messages, which include
5156 * table ID and flow cookie information. */
5157 freeze_put_unroll_xlate(ctx
);
5160 case OFPACT_RESUBMIT
:
5161 if (ofpact_get_RESUBMIT(a
)->table_id
== 0xff) {
5162 /* This resubmit action is relative to the current table, so we
5163 * need to track what table that is.*/
5164 freeze_put_unroll_xlate(ctx
);
5168 case OFPACT_SET_TUNNEL
:
5169 case OFPACT_REG_MOVE
:
5170 case OFPACT_SET_FIELD
:
5171 case OFPACT_STACK_PUSH
:
5172 case OFPACT_STACK_POP
:
5174 case OFPACT_WRITE_METADATA
:
5175 case OFPACT_GOTO_TABLE
:
5176 case OFPACT_ENQUEUE
:
5177 case OFPACT_SET_VLAN_VID
:
5178 case OFPACT_SET_VLAN_PCP
:
5179 case OFPACT_STRIP_VLAN
:
5180 case OFPACT_PUSH_VLAN
:
5181 case OFPACT_SET_ETH_SRC
:
5182 case OFPACT_SET_ETH_DST
:
5183 case OFPACT_SET_IPV4_SRC
:
5184 case OFPACT_SET_IPV4_DST
:
5185 case OFPACT_SET_IP_DSCP
:
5186 case OFPACT_SET_IP_ECN
:
5187 case OFPACT_SET_IP_TTL
:
5188 case OFPACT_SET_L4_SRC_PORT
:
5189 case OFPACT_SET_L4_DST_PORT
:
5190 case OFPACT_SET_QUEUE
:
5191 case OFPACT_POP_QUEUE
:
5192 case OFPACT_PUSH_MPLS
:
5193 case OFPACT_POP_MPLS
:
5194 case OFPACT_SET_MPLS_LABEL
:
5195 case OFPACT_SET_MPLS_TC
:
5196 case OFPACT_SET_MPLS_TTL
:
5197 case OFPACT_MULTIPATH
:
5200 case OFPACT_UNROLL_XLATE
:
5201 case OFPACT_FIN_TIMEOUT
:
5202 case OFPACT_CLEAR_ACTIONS
:
5203 case OFPACT_WRITE_ACTIONS
:
5207 case OFPACT_DEBUG_RECIRC
:
5209 case OFPACT_CT_CLEAR
:
5211 /* These may not generate PACKET INs. */
5215 case OFPACT_CONJUNCTION
:
5216 /* These need not be copied for restoration. */
5219 /* Copy the action over. */
5220 ofpbuf_put(&ctx
->frozen_actions
, a
, OFPACT_ALIGN(a
->len
));
5225 put_ct_mark(const struct flow
*flow
, struct ofpbuf
*odp_actions
,
5226 struct flow_wildcards
*wc
)
5228 if (wc
->masks
.ct_mark
) {
5234 odp_ct_mark
= nl_msg_put_unspec_uninit(odp_actions
, OVS_CT_ATTR_MARK
,
5235 sizeof(*odp_ct_mark
));
5236 odp_ct_mark
->key
= flow
->ct_mark
& wc
->masks
.ct_mark
;
5237 odp_ct_mark
->mask
= wc
->masks
.ct_mark
;
5242 put_ct_label(const struct flow
*flow
, struct ofpbuf
*odp_actions
,
5243 struct flow_wildcards
*wc
)
5245 if (!ovs_u128_is_zero(wc
->masks
.ct_label
)) {
5251 odp_ct_label
= nl_msg_put_unspec_uninit(odp_actions
,
5253 sizeof(*odp_ct_label
));
5254 odp_ct_label
->key
= ovs_u128_and(flow
->ct_label
, wc
->masks
.ct_label
);
5255 odp_ct_label
->mask
= wc
->masks
.ct_label
;
5260 put_ct_helper(struct xlate_ctx
*ctx
,
5261 struct ofpbuf
*odp_actions
, struct ofpact_conntrack
*ofc
)
5266 nl_msg_put_string(odp_actions
, OVS_CT_ATTR_HELPER
, "ftp");
5269 nl_msg_put_string(odp_actions
, OVS_CT_ATTR_HELPER
, "tftp");
5272 xlate_report_error(ctx
, "cannot serialize ct_helper %d", ofc
->alg
);
5279 put_ct_nat(struct xlate_ctx
*ctx
)
5281 struct ofpact_nat
*ofn
= ctx
->ct_nat_action
;
5288 nat_offset
= nl_msg_start_nested(ctx
->odp_actions
, OVS_CT_ATTR_NAT
);
5289 if (ofn
->flags
& NX_NAT_F_SRC
|| ofn
->flags
& NX_NAT_F_DST
) {
5290 nl_msg_put_flag(ctx
->odp_actions
, ofn
->flags
& NX_NAT_F_SRC
5291 ? OVS_NAT_ATTR_SRC
: OVS_NAT_ATTR_DST
);
5292 if (ofn
->flags
& NX_NAT_F_PERSISTENT
) {
5293 nl_msg_put_flag(ctx
->odp_actions
, OVS_NAT_ATTR_PERSISTENT
);
5295 if (ofn
->flags
& NX_NAT_F_PROTO_HASH
) {
5296 nl_msg_put_flag(ctx
->odp_actions
, OVS_NAT_ATTR_PROTO_HASH
);
5297 } else if (ofn
->flags
& NX_NAT_F_PROTO_RANDOM
) {
5298 nl_msg_put_flag(ctx
->odp_actions
, OVS_NAT_ATTR_PROTO_RANDOM
);
5300 if (ofn
->range_af
== AF_INET
) {
5301 nl_msg_put_be32(ctx
->odp_actions
, OVS_NAT_ATTR_IP_MIN
,
5302 ofn
->range
.addr
.ipv4
.min
);
5303 if (ofn
->range
.addr
.ipv4
.max
&&
5304 (ntohl(ofn
->range
.addr
.ipv4
.max
)
5305 > ntohl(ofn
->range
.addr
.ipv4
.min
))) {
5306 nl_msg_put_be32(ctx
->odp_actions
, OVS_NAT_ATTR_IP_MAX
,
5307 ofn
->range
.addr
.ipv4
.max
);
5309 } else if (ofn
->range_af
== AF_INET6
) {
5310 nl_msg_put_unspec(ctx
->odp_actions
, OVS_NAT_ATTR_IP_MIN
,
5311 &ofn
->range
.addr
.ipv6
.min
,
5312 sizeof ofn
->range
.addr
.ipv6
.min
);
5313 if (!ipv6_mask_is_any(&ofn
->range
.addr
.ipv6
.max
) &&
5314 memcmp(&ofn
->range
.addr
.ipv6
.max
, &ofn
->range
.addr
.ipv6
.min
,
5315 sizeof ofn
->range
.addr
.ipv6
.max
) > 0) {
5316 nl_msg_put_unspec(ctx
->odp_actions
, OVS_NAT_ATTR_IP_MAX
,
5317 &ofn
->range
.addr
.ipv6
.max
,
5318 sizeof ofn
->range
.addr
.ipv6
.max
);
5321 if (ofn
->range_af
!= AF_UNSPEC
&& ofn
->range
.proto
.min
) {
5322 nl_msg_put_u16(ctx
->odp_actions
, OVS_NAT_ATTR_PROTO_MIN
,
5323 ofn
->range
.proto
.min
);
5324 if (ofn
->range
.proto
.max
&&
5325 ofn
->range
.proto
.max
> ofn
->range
.proto
.min
) {
5326 nl_msg_put_u16(ctx
->odp_actions
, OVS_NAT_ATTR_PROTO_MAX
,
5327 ofn
->range
.proto
.max
);
5331 nl_msg_end_nested(ctx
->odp_actions
, nat_offset
);
5335 compose_conntrack_action(struct xlate_ctx
*ctx
, struct ofpact_conntrack
*ofc
)
5337 ovs_u128 old_ct_label
= ctx
->base_flow
.ct_label
;
5338 ovs_u128 old_ct_label_mask
= ctx
->wc
->masks
.ct_label
;
5339 uint32_t old_ct_mark
= ctx
->base_flow
.ct_mark
;
5340 uint32_t old_ct_mark_mask
= ctx
->wc
->masks
.ct_mark
;
5344 /* Ensure that any prior actions are applied before composing the new
5345 * conntrack action. */
5346 xlate_commit_actions(ctx
);
5348 /* Process nested actions first, to populate the key. */
5349 ctx
->ct_nat_action
= NULL
;
5350 ctx
->wc
->masks
.ct_mark
= 0;
5351 ctx
->wc
->masks
.ct_label
.u64
.hi
= ctx
->wc
->masks
.ct_label
.u64
.lo
= 0;
5352 do_xlate_actions(ofc
->actions
, ofpact_ct_get_action_len(ofc
), ctx
);
5354 if (ofc
->zone_src
.field
) {
5355 zone
= mf_get_subfield(&ofc
->zone_src
, &ctx
->xin
->flow
);
5357 zone
= ofc
->zone_imm
;
5360 ct_offset
= nl_msg_start_nested(ctx
->odp_actions
, OVS_ACTION_ATTR_CT
);
5361 if (ofc
->flags
& NX_CT_F_COMMIT
) {
5362 nl_msg_put_flag(ctx
->odp_actions
, ofc
->flags
& NX_CT_F_FORCE
?
5363 OVS_CT_ATTR_FORCE_COMMIT
: OVS_CT_ATTR_COMMIT
);
5364 if (ctx
->xbridge
->support
.ct_eventmask
) {
5365 nl_msg_put_u32(ctx
->odp_actions
, OVS_CT_ATTR_EVENTMASK
,
5366 OVS_CT_EVENTMASK_DEFAULT
);
5369 nl_msg_put_u16(ctx
->odp_actions
, OVS_CT_ATTR_ZONE
, zone
);
5370 put_ct_mark(&ctx
->xin
->flow
, ctx
->odp_actions
, ctx
->wc
);
5371 put_ct_label(&ctx
->xin
->flow
, ctx
->odp_actions
, ctx
->wc
);
5372 put_ct_helper(ctx
, ctx
->odp_actions
, ofc
);
5374 ctx
->ct_nat_action
= NULL
;
5375 nl_msg_end_nested(ctx
->odp_actions
, ct_offset
);
5377 /* Restore the original ct fields in the key. These should only be exposed
5378 * after recirculation to another table. */
5379 ctx
->base_flow
.ct_mark
= old_ct_mark
;
5380 ctx
->wc
->masks
.ct_mark
= old_ct_mark_mask
;
5381 ctx
->base_flow
.ct_label
= old_ct_label
;
5382 ctx
->wc
->masks
.ct_label
= old_ct_label_mask
;
5384 if (ofc
->recirc_table
== NX_CT_RECIRC_NONE
) {
5385 /* If we do not recirculate as part of this action, hide the results of
5386 * connection tracking from subsequent recirculations. */
5387 ctx
->conntracked
= false;
5389 /* Use ct_* fields from datapath during recirculation upcall. */
5390 ctx
->conntracked
= true;
5391 compose_recirculate_and_fork(ctx
, ofc
->recirc_table
);
5396 recirc_for_mpls(const struct ofpact
*a
, struct xlate_ctx
*ctx
)
5398 /* No need to recirculate if already exiting. */
5403 /* Do not consider recirculating unless the packet was previously MPLS. */
5404 if (!ctx
->was_mpls
) {
5408 /* Special case these actions, only recirculating if necessary.
5409 * This avoids the overhead of recirculation in common use-cases.
5413 /* Output actions do not require recirculation. */
5415 case OFPACT_OUTPUT_TRUNC
:
5416 case OFPACT_ENQUEUE
:
5417 case OFPACT_OUTPUT_REG
:
5418 /* Set actions that don't touch L3+ fields do not require recirculation. */
5419 case OFPACT_SET_VLAN_VID
:
5420 case OFPACT_SET_VLAN_PCP
:
5421 case OFPACT_SET_ETH_SRC
:
5422 case OFPACT_SET_ETH_DST
:
5423 case OFPACT_SET_TUNNEL
:
5424 case OFPACT_SET_QUEUE
:
5425 /* If actions of a group require recirculation that can be detected
5426 * when translating them. */
5430 /* Set field that don't touch L3+ fields don't require recirculation. */
5431 case OFPACT_SET_FIELD
:
5432 if (mf_is_l3_or_higher(ofpact_get_SET_FIELD(a
)->field
)) {
5437 /* For simplicity, recirculate in all other cases. */
5438 case OFPACT_CONTROLLER
:
5440 case OFPACT_STRIP_VLAN
:
5441 case OFPACT_PUSH_VLAN
:
5442 case OFPACT_SET_IPV4_SRC
:
5443 case OFPACT_SET_IPV4_DST
:
5444 case OFPACT_SET_IP_DSCP
:
5445 case OFPACT_SET_IP_ECN
:
5446 case OFPACT_SET_IP_TTL
:
5447 case OFPACT_SET_L4_SRC_PORT
:
5448 case OFPACT_SET_L4_DST_PORT
:
5449 case OFPACT_REG_MOVE
:
5450 case OFPACT_STACK_PUSH
:
5451 case OFPACT_STACK_POP
:
5452 case OFPACT_DEC_TTL
:
5453 case OFPACT_SET_MPLS_LABEL
:
5454 case OFPACT_SET_MPLS_TC
:
5455 case OFPACT_SET_MPLS_TTL
:
5456 case OFPACT_DEC_MPLS_TTL
:
5457 case OFPACT_PUSH_MPLS
:
5458 case OFPACT_POP_MPLS
:
5459 case OFPACT_POP_QUEUE
:
5460 case OFPACT_FIN_TIMEOUT
:
5461 case OFPACT_RESUBMIT
:
5463 case OFPACT_CONJUNCTION
:
5464 case OFPACT_MULTIPATH
:
5469 case OFPACT_UNROLL_XLATE
:
5471 case OFPACT_CT_CLEAR
:
5473 case OFPACT_DEBUG_RECIRC
:
5475 case OFPACT_CLEAR_ACTIONS
:
5476 case OFPACT_WRITE_ACTIONS
:
5477 case OFPACT_WRITE_METADATA
:
5478 case OFPACT_GOTO_TABLE
:
5484 ctx_trigger_freeze(ctx
);
5488 xlate_ofpact_reg_move(struct xlate_ctx
*ctx
, const struct ofpact_reg_move
*a
)
5490 mf_subfield_copy(&a
->src
, &a
->dst
, &ctx
->xin
->flow
, ctx
->wc
);
5491 xlate_report_subfield(ctx
, &a
->dst
);
5495 xlate_ofpact_stack_pop(struct xlate_ctx
*ctx
, const struct ofpact_stack
*a
)
5497 if (nxm_execute_stack_pop(a
, &ctx
->xin
->flow
, ctx
->wc
, &ctx
->stack
)) {
5498 xlate_report_subfield(ctx
, &a
->subfield
);
5500 xlate_report_error(ctx
, "stack underflow");
5504 /* Restore translation context data that was stored earlier. */
5506 xlate_ofpact_unroll_xlate(struct xlate_ctx
*ctx
,
5507 const struct ofpact_unroll_xlate
*a
)
5509 ctx
->table_id
= a
->rule_table_id
;
5510 ctx
->rule_cookie
= a
->rule_cookie
;
5511 xlate_report(ctx
, OFT_THAW
, "restored state: table=%"PRIu8
", "
5512 "cookie=%#"PRIx64
, a
->rule_table_id
, a
->rule_cookie
);
5516 do_xlate_actions(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
5517 struct xlate_ctx
*ctx
)
5519 struct flow_wildcards
*wc
= ctx
->wc
;
5520 struct flow
*flow
= &ctx
->xin
->flow
;
5521 const struct ofpact
*a
;
5523 if (ovs_native_tunneling_is_on(ctx
->xbridge
->ofproto
)) {
5524 tnl_neigh_snoop(flow
, wc
, ctx
->xbridge
->name
);
5526 /* dl_type already in the mask, not set below. */
5529 xlate_report(ctx
, OFT_ACTION
, "drop");
5533 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
5534 struct ofpact_controller
*controller
;
5535 const struct ofpact_metadata
*metadata
;
5536 const struct ofpact_set_field
*set_field
;
5537 const struct mf_field
*mf
;
5543 recirc_for_mpls(a
, ctx
);
5546 /* Check if need to store the remaining actions for later
5548 if (ctx
->freezing
) {
5549 freeze_unroll_actions(a
, ofpact_end(ofpacts
, ofpacts_len
),
5555 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
5556 struct ds s
= DS_EMPTY_INITIALIZER
;
5557 ofpacts_format(a
, OFPACT_ALIGN(a
->len
), &s
);
5558 xlate_report(ctx
, OFT_ACTION
, "%s", ds_cstr(&s
));
5564 xlate_output_action(ctx
, ofpact_get_OUTPUT(a
)->port
,
5565 ofpact_get_OUTPUT(a
)->max_len
, true);
5569 if (xlate_group_action(ctx
, ofpact_get_GROUP(a
)->group_id
)) {
5570 /* Group could not be found. */
5572 /* XXX: Terminates action list translation, but does not
5573 * terminate the pipeline. */
5578 case OFPACT_CONTROLLER
:
5579 controller
= ofpact_get_CONTROLLER(a
);
5580 if (controller
->pause
) {
5581 ctx
->pause
= controller
;
5582 ctx
->xout
->slow
|= SLOW_CONTROLLER
;
5583 ctx_trigger_freeze(ctx
);
5586 execute_controller_action(ctx
, controller
->max_len
,
5588 controller
->controller_id
,
5589 controller
->userdata
,
5590 controller
->userdata_len
);
5594 case OFPACT_ENQUEUE
:
5595 memset(&wc
->masks
.skb_priority
, 0xff,
5596 sizeof wc
->masks
.skb_priority
);
5597 xlate_enqueue_action(ctx
, ofpact_get_ENQUEUE(a
));
5600 case OFPACT_SET_VLAN_VID
:
5601 wc
->masks
.vlans
[0].tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
5602 if (flow
->vlans
[0].tci
& htons(VLAN_CFI
) ||
5603 ofpact_get_SET_VLAN_VID(a
)->push_vlan_if_needed
) {
5604 if (!flow
->vlans
[0].tpid
) {
5605 flow
->vlans
[0].tpid
= htons(ETH_TYPE_VLAN
);
5607 flow
->vlans
[0].tci
&= ~htons(VLAN_VID_MASK
);
5608 flow
->vlans
[0].tci
|=
5609 (htons(ofpact_get_SET_VLAN_VID(a
)->vlan_vid
) |
5614 case OFPACT_SET_VLAN_PCP
:
5615 wc
->masks
.vlans
[0].tci
|= htons(VLAN_PCP_MASK
| VLAN_CFI
);
5616 if (flow
->vlans
[0].tci
& htons(VLAN_CFI
) ||
5617 ofpact_get_SET_VLAN_PCP(a
)->push_vlan_if_needed
) {
5618 if (!flow
->vlans
[0].tpid
) {
5619 flow
->vlans
[0].tpid
= htons(ETH_TYPE_VLAN
);
5621 flow
->vlans
[0].tci
&= ~htons(VLAN_PCP_MASK
);
5622 flow
->vlans
[0].tci
|=
5623 htons((ofpact_get_SET_VLAN_PCP(a
)->vlan_pcp
5624 << VLAN_PCP_SHIFT
) | VLAN_CFI
);
5628 case OFPACT_STRIP_VLAN
:
5629 flow_pop_vlan(flow
, wc
);
5632 case OFPACT_PUSH_VLAN
:
5633 flow_push_vlan_uninit(flow
, wc
);
5634 flow
->vlans
[0].tpid
= ofpact_get_PUSH_VLAN(a
)->ethertype
;
5635 flow
->vlans
[0].tci
= htons(VLAN_CFI
);
5638 case OFPACT_SET_ETH_SRC
:
5639 WC_MASK_FIELD(wc
, dl_src
);
5640 flow
->dl_src
= ofpact_get_SET_ETH_SRC(a
)->mac
;
5643 case OFPACT_SET_ETH_DST
:
5644 WC_MASK_FIELD(wc
, dl_dst
);
5645 flow
->dl_dst
= ofpact_get_SET_ETH_DST(a
)->mac
;
5648 case OFPACT_SET_IPV4_SRC
:
5649 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
5650 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
5651 flow
->nw_src
= ofpact_get_SET_IPV4_SRC(a
)->ipv4
;
5655 case OFPACT_SET_IPV4_DST
:
5656 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
5657 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
5658 flow
->nw_dst
= ofpact_get_SET_IPV4_DST(a
)->ipv4
;
5662 case OFPACT_SET_IP_DSCP
:
5663 if (is_ip_any(flow
)) {
5664 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
5665 flow
->nw_tos
&= ~IP_DSCP_MASK
;
5666 flow
->nw_tos
|= ofpact_get_SET_IP_DSCP(a
)->dscp
;
5670 case OFPACT_SET_IP_ECN
:
5671 if (is_ip_any(flow
)) {
5672 wc
->masks
.nw_tos
|= IP_ECN_MASK
;
5673 flow
->nw_tos
&= ~IP_ECN_MASK
;
5674 flow
->nw_tos
|= ofpact_get_SET_IP_ECN(a
)->ecn
;
5678 case OFPACT_SET_IP_TTL
:
5679 if (is_ip_any(flow
)) {
5680 wc
->masks
.nw_ttl
= 0xff;
5681 flow
->nw_ttl
= ofpact_get_SET_IP_TTL(a
)->ttl
;
5685 case OFPACT_SET_L4_SRC_PORT
:
5686 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5687 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
5688 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
5689 flow
->tp_src
= htons(ofpact_get_SET_L4_SRC_PORT(a
)->port
);
5693 case OFPACT_SET_L4_DST_PORT
:
5694 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5695 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
5696 memset(&wc
->masks
.tp_dst
, 0xff, sizeof wc
->masks
.tp_dst
);
5697 flow
->tp_dst
= htons(ofpact_get_SET_L4_DST_PORT(a
)->port
);
5701 case OFPACT_RESUBMIT
:
5702 /* Freezing complicates resubmit. Some action in the flow
5703 * entry found by resubmit might trigger freezing. If that
5704 * happens, then we do not want to execute the resubmit again after
5705 * during thawing, so we want to skip back to the head of the loop
5706 * to avoid that, only adding any actions that follow the resubmit
5707 * to the frozen actions.
5709 xlate_ofpact_resubmit(ctx
, ofpact_get_RESUBMIT(a
));
5712 case OFPACT_SET_TUNNEL
:
5713 flow
->tunnel
.tun_id
= htonll(ofpact_get_SET_TUNNEL(a
)->tun_id
);
5716 case OFPACT_SET_QUEUE
:
5717 memset(&wc
->masks
.skb_priority
, 0xff,
5718 sizeof wc
->masks
.skb_priority
);
5719 xlate_set_queue_action(ctx
, ofpact_get_SET_QUEUE(a
)->queue_id
);
5722 case OFPACT_POP_QUEUE
:
5723 memset(&wc
->masks
.skb_priority
, 0xff,
5724 sizeof wc
->masks
.skb_priority
);
5725 if (flow
->skb_priority
!= ctx
->orig_skb_priority
) {
5726 flow
->skb_priority
= ctx
->orig_skb_priority
;
5727 xlate_report(ctx
, OFT_DETAIL
, "queue = %#"PRIx32
,
5728 flow
->skb_priority
);
5732 case OFPACT_REG_MOVE
:
5733 xlate_ofpact_reg_move(ctx
, ofpact_get_REG_MOVE(a
));
5736 case OFPACT_SET_FIELD
:
5737 set_field
= ofpact_get_SET_FIELD(a
);
5738 mf
= set_field
->field
;
5740 /* Set the field only if the packet actually has it. */
5741 if (mf_are_prereqs_ok(mf
, flow
, wc
)) {
5742 mf_mask_field_masked(mf
, ofpact_set_field_mask(set_field
), wc
);
5743 mf_set_flow_value_masked(mf
, set_field
->value
,
5744 ofpact_set_field_mask(set_field
),
5747 xlate_report(ctx
, OFT_WARN
,
5748 "unmet prerequisites for %s, set_field ignored",
5754 case OFPACT_STACK_PUSH
:
5755 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a
), flow
, wc
,
5759 case OFPACT_STACK_POP
:
5760 xlate_ofpact_stack_pop(ctx
, ofpact_get_STACK_POP(a
));
5763 case OFPACT_PUSH_MPLS
:
5764 compose_mpls_push_action(ctx
, ofpact_get_PUSH_MPLS(a
));
5767 case OFPACT_POP_MPLS
:
5768 compose_mpls_pop_action(ctx
, ofpact_get_POP_MPLS(a
)->ethertype
);
5771 case OFPACT_SET_MPLS_LABEL
:
5772 compose_set_mpls_label_action(
5773 ctx
, ofpact_get_SET_MPLS_LABEL(a
)->label
);
5776 case OFPACT_SET_MPLS_TC
:
5777 compose_set_mpls_tc_action(ctx
, ofpact_get_SET_MPLS_TC(a
)->tc
);
5780 case OFPACT_SET_MPLS_TTL
:
5781 compose_set_mpls_ttl_action(ctx
, ofpact_get_SET_MPLS_TTL(a
)->ttl
);
5784 case OFPACT_DEC_MPLS_TTL
:
5785 if (compose_dec_mpls_ttl_action(ctx
)) {
5790 case OFPACT_DEC_TTL
:
5791 wc
->masks
.nw_ttl
= 0xff;
5792 if (compose_dec_ttl(ctx
, ofpact_get_DEC_TTL(a
))) {
5798 /* Nothing to do. */
5801 case OFPACT_MULTIPATH
:
5802 multipath_execute(ofpact_get_MULTIPATH(a
), flow
, wc
);
5803 xlate_report_subfield(ctx
, &ofpact_get_MULTIPATH(a
)->dst
);
5807 xlate_bundle_action(ctx
, ofpact_get_BUNDLE(a
));
5810 case OFPACT_OUTPUT_REG
:
5811 xlate_output_reg_action(ctx
, ofpact_get_OUTPUT_REG(a
));
5814 case OFPACT_OUTPUT_TRUNC
:
5815 xlate_output_trunc_action(ctx
, ofpact_get_OUTPUT_TRUNC(a
)->port
,
5816 ofpact_get_OUTPUT_TRUNC(a
)->max_len
);
5820 xlate_learn_action(ctx
, ofpact_get_LEARN(a
));
5823 case OFPACT_CONJUNCTION
:
5824 /* A flow with a "conjunction" action represents part of a special
5825 * kind of "set membership match". Such a flow should not actually
5826 * get executed, but it could via, say, a "packet-out", even though
5827 * that wouldn't be useful. Log it to help debugging. */
5828 xlate_report_error(ctx
, "executing no-op conjunction action");
5835 case OFPACT_UNROLL_XLATE
:
5836 xlate_ofpact_unroll_xlate(ctx
, ofpact_get_UNROLL_XLATE(a
));
5839 case OFPACT_FIN_TIMEOUT
:
5840 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
5841 xlate_fin_timeout(ctx
, ofpact_get_FIN_TIMEOUT(a
));
5844 case OFPACT_CLEAR_ACTIONS
:
5845 xlate_report_action_set(ctx
, "was");
5846 ofpbuf_clear(&ctx
->action_set
);
5847 ctx
->xin
->flow
.actset_output
= OFPP_UNSET
;
5848 ctx
->action_set_has_group
= false;
5851 case OFPACT_WRITE_ACTIONS
:
5852 xlate_write_actions(ctx
, ofpact_get_WRITE_ACTIONS(a
));
5853 xlate_report_action_set(ctx
, "is");
5856 case OFPACT_WRITE_METADATA
:
5857 metadata
= ofpact_get_WRITE_METADATA(a
);
5858 flow
->metadata
&= ~metadata
->mask
;
5859 flow
->metadata
|= metadata
->metadata
& metadata
->mask
;
5863 xlate_meter_action(ctx
, ofpact_get_METER(a
));
5866 case OFPACT_GOTO_TABLE
: {
5867 struct ofpact_goto_table
*ogt
= ofpact_get_GOTO_TABLE(a
);
5869 ovs_assert(ctx
->table_id
< ogt
->table_id
);
5871 xlate_table_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
,
5872 ogt
->table_id
, true, true, false);
5877 xlate_sample_action(ctx
, ofpact_get_SAMPLE(a
));
5881 xlate_clone(ctx
, ofpact_get_CLONE(a
));
5885 compose_conntrack_action(ctx
, ofpact_get_CT(a
));
5888 case OFPACT_CT_CLEAR
:
5889 clear_conntrack(ctx
);
5893 /* This will be processed by compose_conntrack_action(). */
5894 ctx
->ct_nat_action
= ofpact_get_NAT(a
);
5897 case OFPACT_DEBUG_RECIRC
:
5898 ctx_trigger_freeze(ctx
);
5903 /* Check if need to store this and the remaining actions for later
5905 if (!ctx
->error
&& ctx
->exit
&& ctx_first_frozen_action(ctx
)) {
5906 freeze_unroll_actions(a
, ofpact_end(ofpacts
, ofpacts_len
), ctx
);
5913 xlate_in_init(struct xlate_in
*xin
, struct ofproto_dpif
*ofproto
,
5914 ovs_version_t version
, const struct flow
*flow
,
5915 ofp_port_t in_port
, struct rule_dpif
*rule
, uint16_t tcp_flags
,
5916 const struct dp_packet
*packet
, struct flow_wildcards
*wc
,
5917 struct ofpbuf
*odp_actions
)
5919 xin
->ofproto
= ofproto
;
5920 xin
->tables_version
= version
;
5922 xin
->upcall_flow
= flow
;
5923 xin
->flow
.in_port
.ofp_port
= in_port
;
5924 xin
->flow
.actset_output
= OFPP_UNSET
;
5925 xin
->packet
= packet
;
5926 xin
->allow_side_effects
= packet
!= NULL
;
5929 xin
->ofpacts
= NULL
;
5930 xin
->ofpacts_len
= 0;
5931 xin
->tcp_flags
= tcp_flags
;
5933 xin
->resubmit_stats
= NULL
;
5937 xin
->odp_actions
= odp_actions
;
5938 xin
->in_packet_out
= false;
5940 /* Do recirc lookup. */
5941 xin
->frozen_state
= NULL
;
5942 if (flow
->recirc_id
) {
5943 const struct recirc_id_node
*node
5944 = recirc_id_node_find(flow
->recirc_id
);
5946 xin
->frozen_state
= &node
->state
;
5952 xlate_out_uninit(struct xlate_out
*xout
)
5955 recirc_refs_unref(&xout
->recircs
);
5959 static struct skb_priority_to_dscp
*
5960 get_skb_priority(const struct xport
*xport
, uint32_t skb_priority
)
5962 struct skb_priority_to_dscp
*pdscp
;
5965 hash
= hash_int(skb_priority
, 0);
5966 HMAP_FOR_EACH_IN_BUCKET (pdscp
, hmap_node
, hash
, &xport
->skb_priorities
) {
5967 if (pdscp
->skb_priority
== skb_priority
) {
5975 dscp_from_skb_priority(const struct xport
*xport
, uint32_t skb_priority
,
5978 struct skb_priority_to_dscp
*pdscp
= get_skb_priority(xport
, skb_priority
);
5979 *dscp
= pdscp
? pdscp
->dscp
: 0;
5980 return pdscp
!= NULL
;
5984 count_skb_priorities(const struct xport
*xport
)
5986 return hmap_count(&xport
->skb_priorities
);
5990 clear_skb_priorities(struct xport
*xport
)
5992 struct skb_priority_to_dscp
*pdscp
;
5994 HMAP_FOR_EACH_POP (pdscp
, hmap_node
, &xport
->skb_priorities
) {
6000 actions_output_to_local_port(const struct xlate_ctx
*ctx
)
6002 odp_port_t local_odp_port
= ofp_port_to_odp_port(ctx
->xbridge
, OFPP_LOCAL
);
6003 const struct nlattr
*a
;
6006 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, ctx
->odp_actions
->data
,
6007 ctx
->odp_actions
->size
) {
6008 if (nl_attr_type(a
) == OVS_ACTION_ATTR_OUTPUT
6009 && nl_attr_get_odp_port(a
) == local_odp_port
) {
6016 #if defined(__linux__)
6017 /* Returns the maximum number of packets that the Linux kernel is willing to
6018 * queue up internally to certain kinds of software-implemented ports, or the
6019 * default (and rarely modified) value if it cannot be determined. */
6021 netdev_max_backlog(void)
6023 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
6024 static int max_backlog
= 1000; /* The normal default value. */
6026 if (ovsthread_once_start(&once
)) {
6027 static const char filename
[] = "/proc/sys/net/core/netdev_max_backlog";
6031 stream
= fopen(filename
, "r");
6033 VLOG_INFO("%s: open failed (%s)", filename
, ovs_strerror(errno
));
6035 if (fscanf(stream
, "%d", &n
) != 1) {
6036 VLOG_WARN("%s: read error", filename
);
6037 } else if (n
<= 100) {
6038 VLOG_WARN("%s: unexpectedly small value %d", filename
, n
);
6044 ovsthread_once_done(&once
);
6046 VLOG_DBG("%s: using %d max_backlog", filename
, max_backlog
);
6052 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
6055 count_output_actions(const struct ofpbuf
*odp_actions
)
6057 const struct nlattr
*a
;
6061 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, odp_actions
->data
, odp_actions
->size
) {
6062 if (a
->nla_type
== OVS_ACTION_ATTR_OUTPUT
) {
6068 #endif /* defined(__linux__) */
6070 /* Returns true if 'odp_actions' contains more output actions than the datapath
6071 * can reliably handle in one go. On Linux, this is the value of the
6072 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
6073 * packets that the kernel is willing to queue up for processing while the
6074 * datapath is processing a set of actions. */
6076 too_many_output_actions(const struct ofpbuf
*odp_actions OVS_UNUSED
)
6079 return (odp_actions
->size
/ NL_A_U32_SIZE
> netdev_max_backlog()
6080 && count_output_actions(odp_actions
) > netdev_max_backlog());
6082 /* OSes other than Linux might have similar limits, but we don't know how
6083 * to determine them.*/
6089 xlate_wc_init(struct xlate_ctx
*ctx
)
6091 flow_wildcards_init_catchall(ctx
->wc
);
6093 /* Some fields we consider to always be examined. */
6094 WC_MASK_FIELD(ctx
->wc
, in_port
);
6095 WC_MASK_FIELD(ctx
->wc
, dl_type
);
6096 if (is_ip_any(&ctx
->xin
->flow
)) {
6097 WC_MASK_FIELD_MASK(ctx
->wc
, nw_frag
, FLOW_NW_FRAG_MASK
);
6100 if (ctx
->xbridge
->support
.odp
.recirc
) {
6101 /* Always exactly match recirc_id when datapath supports
6103 WC_MASK_FIELD(ctx
->wc
, recirc_id
);
6106 if (ctx
->xbridge
->netflow
) {
6107 netflow_mask_wc(&ctx
->xin
->flow
, ctx
->wc
);
6110 tnl_wc_init(&ctx
->xin
->flow
, ctx
->wc
);
6114 xlate_wc_finish(struct xlate_ctx
*ctx
)
6118 /* Clear the metadata and register wildcard masks, because we won't
6119 * use non-header fields as part of the cache. */
6120 flow_wildcards_clear_non_packet_fields(ctx
->wc
);
6122 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
6123 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
6124 * represent these fields. The datapath interface, on the other hand,
6125 * represents them with just 8 bits each. This means that if the high
6126 * 8 bits of the masks for these fields somehow become set, then they
6127 * will get chopped off by a round trip through the datapath, and
6128 * revalidation will spot that as an inconsistency and delete the flow.
6129 * Avoid the problem here by making sure that only the low 8 bits of
6130 * either field can be unwildcarded for ICMP.
6132 if (is_icmpv4(&ctx
->xin
->flow
, NULL
) || is_icmpv6(&ctx
->xin
->flow
, NULL
)) {
6133 ctx
->wc
->masks
.tp_src
&= htons(UINT8_MAX
);
6134 ctx
->wc
->masks
.tp_dst
&= htons(UINT8_MAX
);
6136 /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
6137 for (i
= 0; i
< FLOW_MAX_VLAN_HEADERS
; i
++) {
6138 if (ctx
->wc
->masks
.vlans
[i
].tci
) {
6139 ctx
->wc
->masks
.vlans
[i
].tci
|= htons(VLAN_CFI
);
6143 /* The classifier might return masks that match on tp_src and tp_dst even
6144 * for later fragments. This happens because there might be flows that
6145 * match on tp_src or tp_dst without matching on the frag bits, because
6146 * it is not a prerequisite for OpenFlow. Since it is a prerequisite for
6147 * datapath flows and since tp_src and tp_dst are always going to be 0,
6148 * wildcard the fields here. */
6149 if (ctx
->xin
->flow
.nw_frag
& FLOW_NW_FRAG_LATER
) {
6150 ctx
->wc
->masks
.tp_src
= 0;
6151 ctx
->wc
->masks
.tp_dst
= 0;
6155 /* Translates the flow, actions, or rule in 'xin' into datapath actions in
6157 * The caller must take responsibility for eventually freeing 'xout', with
6158 * xlate_out_uninit().
6159 * Returns 'XLATE_OK' if translation was successful. In case of an error an
6160 * empty set of actions will be returned in 'xin->odp_actions' (if non-NULL),
6161 * so that most callers may ignore the return value and transparently install a
6162 * drop flow when the translation fails. */
6164 xlate_actions(struct xlate_in
*xin
, struct xlate_out
*xout
)
6166 *xout
= (struct xlate_out
) {
6168 .recircs
= RECIRC_REFS_EMPTY_INITIALIZER
,
6171 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
6172 struct xbridge
*xbridge
= xbridge_lookup(xcfg
, xin
->ofproto
);
6174 return XLATE_BRIDGE_NOT_FOUND
;
6177 struct flow
*flow
= &xin
->flow
;
6179 uint8_t stack_stub
[1024];
6180 uint64_t action_set_stub
[1024 / 8];
6181 uint64_t frozen_actions_stub
[1024 / 8];
6182 uint64_t actions_stub
[256 / 8];
6183 struct ofpbuf scratch_actions
= OFPBUF_STUB_INITIALIZER(actions_stub
);
6184 struct xlate_ctx ctx
= {
6188 .orig_tunnel_ipv6_dst
= flow_tnl_dst(&flow
->tunnel
),
6190 .stack
= OFPBUF_STUB_INITIALIZER(stack_stub
),
6194 : &(struct flow_wildcards
) { .masks
= { .dl_type
= 0 } }),
6195 .odp_actions
= xin
->odp_actions
? xin
->odp_actions
: &scratch_actions
,
6197 .depth
= xin
->depth
,
6198 .resubmits
= xin
->resubmits
,
6200 .in_action_set
= false,
6201 .in_packet_out
= xin
->in_packet_out
,
6204 .rule_cookie
= OVS_BE64_MAX
,
6205 .orig_skb_priority
= flow
->skb_priority
,
6206 .sflow_n_outputs
= 0,
6207 .sflow_odp_port
= 0,
6208 .nf_output_iface
= NF_OUT_DROP
,
6214 .recirc_update_dp_hash
= false,
6215 .frozen_actions
= OFPBUF_STUB_INITIALIZER(frozen_actions_stub
),
6219 .conntracked
= false,
6221 .ct_nat_action
= NULL
,
6223 .action_set_has_group
= false,
6224 .action_set
= OFPBUF_STUB_INITIALIZER(action_set_stub
),
6227 /* 'base_flow' reflects the packet as it came in, but we need it to reflect
6228 * the packet as the datapath will treat it for output actions. Our
6229 * datapath doesn't retain tunneling information without us re-setting
6230 * it, so clear the tunnel data.
6233 memset(&ctx
.base_flow
.tunnel
, 0, sizeof ctx
.base_flow
.tunnel
);
6235 ofpbuf_reserve(ctx
.odp_actions
, NL_A_U32_SIZE
);
6236 xlate_wc_init(&ctx
);
6238 COVERAGE_INC(xlate_actions
);
6240 xin
->trace
= xlate_report(&ctx
, OFT_BRIDGE
, "bridge(\"%s\")",
6242 if (xin
->frozen_state
) {
6243 const struct frozen_state
*state
= xin
->frozen_state
;
6245 struct ovs_list
*old_trace
= xin
->trace
;
6246 xin
->trace
= xlate_report(&ctx
, OFT_THAW
, "thaw");
6248 if (xin
->ofpacts_len
> 0 || ctx
.rule
) {
6249 xlate_report_error(&ctx
, "Recirculation conflict (%s)!",
6250 xin
->ofpacts_len
? "actions" : "rule");
6251 ctx
.error
= XLATE_RECIRCULATION_CONFLICT
;
6255 /* Set the bridge for post-recirculation processing if needed. */
6256 if (!uuid_equals(&ctx
.xbridge
->ofproto
->uuid
, &state
->ofproto_uuid
)) {
6257 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
6258 const struct xbridge
*new_bridge
6259 = xbridge_lookup_by_uuid(xcfg
, &state
->ofproto_uuid
);
6261 if (OVS_UNLIKELY(!new_bridge
)) {
6262 /* Drop the packet if the bridge cannot be found. */
6263 xlate_report_error(&ctx
, "Frozen bridge no longer exists.");
6264 ctx
.error
= XLATE_BRIDGE_NOT_FOUND
;
6265 xin
->trace
= old_trace
;
6268 ctx
.xbridge
= new_bridge
;
6269 /* The bridge is now known so obtain its table version. */
6270 ctx
.xin
->tables_version
6271 = ofproto_dpif_get_tables_version(ctx
.xbridge
->ofproto
);
6274 /* Set the thawed table id. Note: A table lookup is done only if there
6275 * are no frozen actions. */
6276 ctx
.table_id
= state
->table_id
;
6277 xlate_report(&ctx
, OFT_THAW
,
6278 "Resuming from table %"PRIu8
, ctx
.table_id
);
6280 if (!state
->conntracked
) {
6281 clear_conntrack(&ctx
);
6284 /* Restore pipeline metadata. May change flow's in_port and other
6285 * metadata to the values that existed when freezing was triggered. */
6286 frozen_metadata_to_flow(&state
->metadata
, flow
);
6288 /* Restore stack, if any. */
6290 ofpbuf_put(&ctx
.stack
, state
->stack
, state
->stack_size
);
6293 /* Restore mirror state. */
6294 ctx
.mirrors
= state
->mirrors
;
6296 /* Restore action set, if any. */
6297 if (state
->action_set_len
) {
6298 xlate_report_actions(&ctx
, OFT_THAW
, "Restoring action set",
6299 state
->action_set
, state
->action_set_len
);
6301 flow
->actset_output
= OFPP_UNSET
;
6302 xlate_write_actions__(&ctx
, state
->action_set
,
6303 state
->action_set_len
);
6306 /* Restore frozen actions. If there are no actions, processing will
6307 * start with a lookup in the table set above. */
6308 xin
->ofpacts
= state
->ofpacts
;
6309 xin
->ofpacts_len
= state
->ofpacts_len
;
6310 if (state
->ofpacts_len
) {
6311 xlate_report_actions(&ctx
, OFT_THAW
, "Restoring actions",
6312 xin
->ofpacts
, xin
->ofpacts_len
);
6315 xin
->trace
= old_trace
;
6316 } else if (OVS_UNLIKELY(flow
->recirc_id
)) {
6317 xlate_report_error(&ctx
,
6318 "Recirculation context not found for ID %"PRIx32
,
6320 ctx
.error
= XLATE_NO_RECIRCULATION_CONTEXT
;
6324 /* Tunnel metadata in udpif format must be normalized before translation. */
6325 if (flow
->tunnel
.flags
& FLOW_TNL_F_UDPIF
) {
6326 const struct tun_table
*tun_tab
= ofproto_get_tun_tab(
6327 &ctx
.xbridge
->ofproto
->up
);
6330 err
= tun_metadata_from_geneve_udpif(tun_tab
, &xin
->upcall_flow
->tunnel
,
6331 &xin
->upcall_flow
->tunnel
,
6334 xlate_report_error(&ctx
, "Invalid Geneve tunnel metadata");
6335 ctx
.error
= XLATE_INVALID_TUNNEL_METADATA
;
6338 } else if (!flow
->tunnel
.metadata
.tab
) {
6339 /* If the original flow did not come in on a tunnel, then it won't have
6340 * FLOW_TNL_F_UDPIF set. However, we still need to have a metadata
6341 * table in case we generate tunnel actions. */
6342 flow
->tunnel
.metadata
.tab
= ofproto_get_tun_tab(
6343 &ctx
.xbridge
->ofproto
->up
);
6345 ctx
.wc
->masks
.tunnel
.metadata
.tab
= flow
->tunnel
.metadata
.tab
;
6347 if (!xin
->ofpacts
&& !ctx
.rule
) {
6348 ctx
.rule
= rule_dpif_lookup_from_table(
6349 ctx
.xbridge
->ofproto
, ctx
.xin
->tables_version
, flow
, ctx
.wc
,
6350 ctx
.xin
->resubmit_stats
, &ctx
.table_id
,
6351 flow
->in_port
.ofp_port
, true, true, ctx
.xin
->xcache
);
6352 if (ctx
.xin
->resubmit_stats
) {
6353 rule_dpif_credit_stats(ctx
.rule
, ctx
.xin
->resubmit_stats
);
6355 if (ctx
.xin
->xcache
) {
6356 struct xc_entry
*entry
;
6358 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_RULE
);
6359 entry
->rule
= ctx
.rule
;
6360 ofproto_rule_ref(&ctx
.rule
->up
);
6363 xlate_report_table(&ctx
, ctx
.rule
, ctx
.table_id
);
6366 /* Get the proximate input port of the packet. (If xin->frozen_state,
6367 * flow->in_port is the ultimate input port of the packet.) */
6368 struct xport
*in_port
= get_ofp_port(xbridge
,
6369 ctx
.base_flow
.in_port
.ofp_port
);
6371 /* Tunnel stats only for not-thawed packets. */
6372 if (!xin
->frozen_state
&& in_port
&& in_port
->is_tunnel
) {
6373 if (ctx
.xin
->resubmit_stats
) {
6374 netdev_vport_inc_rx(in_port
->netdev
, ctx
.xin
->resubmit_stats
);
6376 bfd_account_rx(in_port
->bfd
, ctx
.xin
->resubmit_stats
);
6379 if (ctx
.xin
->xcache
) {
6380 struct xc_entry
*entry
;
6382 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_NETDEV
);
6383 entry
->dev
.rx
= netdev_ref(in_port
->netdev
);
6384 entry
->dev
.bfd
= bfd_ref(in_port
->bfd
);
6388 if (!xin
->frozen_state
&& process_special(&ctx
, in_port
)) {
6389 /* process_special() did all the processing for this packet.
6391 * We do not perform special processing on thawed packets, since that
6392 * was done before they were frozen and should not be redone. */
6393 } else if (in_port
&& in_port
->xbundle
6394 && xbundle_mirror_out(xbridge
, in_port
->xbundle
)) {
6395 xlate_report_error(&ctx
, "dropping packet received on port "
6396 "%s, which is reserved exclusively for mirroring",
6397 in_port
->xbundle
->name
);
6399 /* Sampling is done on initial reception; don't redo after thawing. */
6400 unsigned int user_cookie_offset
= 0;
6401 if (!xin
->frozen_state
) {
6402 user_cookie_offset
= compose_sflow_action(&ctx
);
6403 compose_ipfix_action(&ctx
, ODPP_NONE
);
6405 size_t sample_actions_len
= ctx
.odp_actions
->size
;
6407 if (tnl_process_ecn(flow
)
6408 && (!in_port
|| may_receive(in_port
, &ctx
))) {
6409 const struct ofpact
*ofpacts
;
6413 ofpacts
= xin
->ofpacts
;
6414 ofpacts_len
= xin
->ofpacts_len
;
6415 } else if (ctx
.rule
) {
6416 const struct rule_actions
*actions
6417 = rule_get_actions(&ctx
.rule
->up
);
6418 ofpacts
= actions
->ofpacts
;
6419 ofpacts_len
= actions
->ofpacts_len
;
6420 ctx
.rule_cookie
= ctx
.rule
->up
.flow_cookie
;
6425 mirror_ingress_packet(&ctx
);
6426 do_xlate_actions(ofpacts
, ofpacts_len
, &ctx
);
6431 /* We've let OFPP_NORMAL and the learning action look at the
6432 * packet, so cancel all actions and freezing if forwarding is
6434 if (in_port
&& (!xport_stp_forward_state(in_port
) ||
6435 !xport_rstp_forward_state(in_port
))) {
6436 ctx
.odp_actions
->size
= sample_actions_len
;
6437 ctx_cancel_freeze(&ctx
);
6438 ofpbuf_clear(&ctx
.action_set
);
6441 if (!ctx
.freezing
) {
6442 xlate_action_set(&ctx
);
6445 finish_freezing(&ctx
);
6449 /* Output only fully processed packets. */
6451 && xbridge
->has_in_band
6452 && in_band_must_output_to_local_port(flow
)
6453 && !actions_output_to_local_port(&ctx
)) {
6454 compose_output_action(&ctx
, OFPP_LOCAL
, NULL
);
6457 if (user_cookie_offset
) {
6458 fix_sflow_action(&ctx
, user_cookie_offset
);
6462 if (nl_attr_oversized(ctx
.odp_actions
->size
)) {
6463 /* These datapath actions are too big for a Netlink attribute, so we
6464 * can't hand them to the kernel directly. dpif_execute() can execute
6465 * them one by one with help, so just mark the result as SLOW_ACTION to
6466 * prevent the flow from being installed. */
6467 COVERAGE_INC(xlate_actions_oversize
);
6468 ctx
.xout
->slow
|= SLOW_ACTION
;
6469 } else if (too_many_output_actions(ctx
.odp_actions
)) {
6470 COVERAGE_INC(xlate_actions_too_many_output
);
6471 ctx
.xout
->slow
|= SLOW_ACTION
;
6474 /* Update NetFlow for non-frozen traffic. */
6475 if (xbridge
->netflow
&& !xin
->frozen_state
) {
6476 if (ctx
.xin
->resubmit_stats
) {
6477 netflow_flow_update(xbridge
->netflow
, flow
,
6478 ctx
.nf_output_iface
,
6479 ctx
.xin
->resubmit_stats
);
6481 if (ctx
.xin
->xcache
) {
6482 struct xc_entry
*entry
;
6484 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_NETFLOW
);
6485 entry
->nf
.netflow
= netflow_ref(xbridge
->netflow
);
6486 entry
->nf
.flow
= xmemdup(flow
, sizeof *flow
);
6487 entry
->nf
.iface
= ctx
.nf_output_iface
;
6491 /* Translate tunnel metadata masks to udpif format if necessary. */
6492 if (xin
->upcall_flow
->tunnel
.flags
& FLOW_TNL_F_UDPIF
) {
6493 if (ctx
.wc
->masks
.tunnel
.metadata
.present
.map
) {
6494 const struct flow_tnl
*upcall_tnl
= &xin
->upcall_flow
->tunnel
;
6495 struct geneve_opt opts
[TLV_TOT_OPT_SIZE
/
6496 sizeof(struct geneve_opt
)];
6498 tun_metadata_to_geneve_udpif_mask(&flow
->tunnel
,
6499 &ctx
.wc
->masks
.tunnel
,
6500 upcall_tnl
->metadata
.opts
.gnv
,
6501 upcall_tnl
->metadata
.present
.len
,
6503 memset(&ctx
.wc
->masks
.tunnel
.metadata
, 0,
6504 sizeof ctx
.wc
->masks
.tunnel
.metadata
);
6505 memcpy(&ctx
.wc
->masks
.tunnel
.metadata
.opts
.gnv
, opts
,
6506 upcall_tnl
->metadata
.present
.len
);
6508 ctx
.wc
->masks
.tunnel
.metadata
.present
.len
= 0xff;
6509 ctx
.wc
->masks
.tunnel
.metadata
.tab
= NULL
;
6510 ctx
.wc
->masks
.tunnel
.flags
|= FLOW_TNL_F_UDPIF
;
6511 } else if (!xin
->upcall_flow
->tunnel
.metadata
.tab
) {
6512 /* If we didn't have options in UDPIF format and didn't have an existing
6513 * metadata table, then it means that there were no options at all when
6514 * we started processing and any wildcards we picked up were from
6515 * action generation. Without options on the incoming packet, wildcards
6516 * aren't meaningful. To avoid them possibly getting misinterpreted,
6517 * just clear everything. */
6518 if (ctx
.wc
->masks
.tunnel
.metadata
.present
.map
) {
6519 memset(&ctx
.wc
->masks
.tunnel
.metadata
, 0,
6520 sizeof ctx
.wc
->masks
.tunnel
.metadata
);
6522 ctx
.wc
->masks
.tunnel
.metadata
.tab
= NULL
;
6526 xlate_wc_finish(&ctx
);
6529 /* Reset the table to what it was when we came in. If we only fetched
6530 * it locally, then it has no meaning outside of flow translation. */
6531 flow
->tunnel
.metadata
.tab
= xin
->upcall_flow
->tunnel
.metadata
.tab
;
6533 ofpbuf_uninit(&ctx
.stack
);
6534 ofpbuf_uninit(&ctx
.action_set
);
6535 ofpbuf_uninit(&ctx
.frozen_actions
);
6536 ofpbuf_uninit(&scratch_actions
);
6538 /* Make sure we return a "drop flow" in case of an error. */
6541 if (xin
->odp_actions
) {
6542 ofpbuf_clear(xin
->odp_actions
);
6549 xlate_resume(struct ofproto_dpif
*ofproto
,
6550 const struct ofputil_packet_in_private
*pin
,
6551 struct ofpbuf
*odp_actions
,
6552 enum slow_path_reason
*slow
)
6554 struct dp_packet packet
;
6555 dp_packet_use_const(&packet
, pin
->public.packet
,
6556 pin
->public.packet_len
);
6559 flow_extract(&packet
, &flow
);
6561 struct xlate_in xin
;
6562 xlate_in_init(&xin
, ofproto
, ofproto_dpif_get_tables_version(ofproto
),
6563 &flow
, 0, NULL
, ntohs(flow
.tcp_flags
),
6564 &packet
, NULL
, odp_actions
);
6566 struct ofpact_note noop
;
6567 ofpact_init_NOTE(&noop
);
6570 bool any_actions
= pin
->actions_len
> 0;
6571 struct frozen_state state
= {
6572 .table_id
= 0, /* Not the table where NXAST_PAUSE was executed. */
6573 .ofproto_uuid
= pin
->bridge
,
6574 .stack
= pin
->stack
,
6575 .stack_size
= pin
->stack_size
,
6576 .mirrors
= pin
->mirrors
,
6577 .conntracked
= pin
->conntracked
,
6579 /* When there are no actions, xlate_actions() will search the flow
6580 * table. We don't want it to do that (we want it to resume), so
6581 * supply a no-op action if there aren't any.
6583 * (We can't necessarily avoid translating actions entirely if there
6584 * aren't any actions, because there might be some finishing-up to do
6585 * at the end of the pipeline, and we don't check for those
6587 .ofpacts
= any_actions
? pin
->actions
: &noop
.ofpact
,
6588 .ofpacts_len
= any_actions
? pin
->actions_len
: sizeof noop
,
6590 .action_set
= pin
->action_set
,
6591 .action_set_len
= pin
->action_set_len
,
6593 frozen_metadata_from_flow(&state
.metadata
,
6594 &pin
->public.flow_metadata
.flow
);
6595 xin
.frozen_state
= &state
;
6597 struct xlate_out xout
;
6598 enum xlate_error error
= xlate_actions(&xin
, &xout
);
6600 xlate_out_uninit(&xout
);
6602 /* xlate_actions() can generate a number of errors, but only
6603 * XLATE_BRIDGE_NOT_FOUND really stands out to me as one that we should be
6604 * sure to report over OpenFlow. The others could come up in packet-outs
6605 * or regular flow translation and I don't think that it's going to be too
6606 * useful to report them to the controller. */
6607 return error
== XLATE_BRIDGE_NOT_FOUND
? OFPERR_NXR_STALE
: 0;
6610 /* Sends 'packet' out 'ofport'. If 'port' is a tunnel and that tunnel type
6611 * supports a notion of an OAM flag, sets it if 'oam' is true.
6612 * May modify 'packet'.
6613 * Returns 0 if successful, otherwise a positive errno value. */
6615 xlate_send_packet(const struct ofport_dpif
*ofport
, bool oam
,
6616 struct dp_packet
*packet
)
6618 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
6619 struct xport
*xport
;
6620 uint64_t ofpacts_stub
[1024 / 8];
6621 struct ofpbuf ofpacts
;
6624 ofpbuf_use_stack(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
6625 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
6626 flow_extract(packet
, &flow
);
6627 flow
.in_port
.ofp_port
= OFPP_NONE
;
6629 xport
= xport_lookup(xcfg
, ofport
);
6635 const ovs_be16 oam
= htons(NX_TUN_FLAG_OAM
);
6636 ofpact_put_set_field(&ofpacts
, mf_from_id(MFF_TUN_FLAGS
), &oam
, &oam
);
6639 ofpact_put_OUTPUT(&ofpacts
)->port
= xport
->ofp_port
;
6641 /* Actions here are not referring to anything versionable (flow tables or
6642 * groups) so we don't need to worry about the version here. */
6643 return ofproto_dpif_execute_actions(xport
->xbridge
->ofproto
,
6644 OVS_VERSION_MAX
, &flow
, NULL
,
6645 ofpacts
.data
, ofpacts
.size
, packet
);
6649 xlate_mac_learning_update(const struct ofproto_dpif
*ofproto
,
6650 ofp_port_t in_port
, struct eth_addr dl_src
,
6651 int vlan
, bool is_grat_arp
)
6653 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
6654 struct xbridge
*xbridge
;
6655 struct xbundle
*xbundle
;
6657 xbridge
= xbridge_lookup(xcfg
, ofproto
);
6662 xbundle
= lookup_input_bundle__(xbridge
, in_port
, NULL
);
6667 update_learning_table__(xbridge
, xbundle
, dl_src
, vlan
, is_grat_arp
);
6671 xlate_disable_dp_clone(const struct ofproto_dpif
*ofproto
)
6673 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
6674 struct xbridge
*xbridge
= xbridge_lookup(xcfg
, ofproto
);
6677 xbridge
->support
.clone
= false;