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 int vlan
; /* -1=trunk port, else a 12-bit VLAN ID. */
131 unsigned long *trunks
; /* Bitmap of trunked VLANs, if 'vlan' == -1.
132 * NULL if all VLANs are trunked. */
133 bool use_priority_tags
; /* Use 802.1p tag for frames in VLAN 0? */
134 bool floodable
; /* No port has OFPUTIL_PC_NO_FLOOD set? */
135 bool protected; /* Protected port mode */
139 struct hmap_node hmap_node
; /* Node in global 'xports' map. */
140 struct ofport_dpif
*ofport
; /* Key in global 'xports map. */
142 struct hmap_node ofp_node
; /* Node in parent xbridge 'xports' map. */
143 ofp_port_t ofp_port
; /* Key in parent xbridge 'xports' map. */
145 odp_port_t odp_port
; /* Datapath port number or ODPP_NONE. */
147 struct ovs_list bundle_node
; /* In parent xbundle (if it exists). */
148 struct xbundle
*xbundle
; /* Parent xbundle or null. */
150 struct netdev
*netdev
; /* 'ofport''s netdev. */
152 struct xbridge
*xbridge
; /* Parent bridge. */
153 struct xport
*peer
; /* Patch port peer or null. */
155 enum ofputil_port_config config
; /* OpenFlow port configuration. */
156 enum ofputil_port_state state
; /* OpenFlow port state. */
157 int stp_port_no
; /* STP port number or -1 if not in use. */
158 struct rstp_port
*rstp_port
; /* RSTP port or null. */
160 struct hmap skb_priorities
; /* Map of 'skb_priority_to_dscp's. */
162 bool may_enable
; /* May be enabled in bonds. */
163 bool is_tunnel
; /* Is a tunnel port. */
165 struct cfm
*cfm
; /* CFM handle or null. */
166 struct bfd
*bfd
; /* BFD handle or null. */
167 struct lldp
*lldp
; /* LLDP handle or null. */
171 struct xlate_in
*xin
;
172 struct xlate_out
*xout
;
174 const struct xbridge
*xbridge
;
176 /* Flow at the last commit. */
177 struct flow base_flow
;
179 /* Tunnel IP destination address as received. This is stored separately
180 * as the base_flow.tunnel is cleared on init to reflect the datapath
181 * behavior. Used to make sure not to send tunneled output to ourselves,
182 * which might lead to an infinite loop. This could happen easily
183 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
184 * actually set the tun_dst field. */
185 struct in6_addr orig_tunnel_ipv6_dst
;
187 /* Stack for the push and pop actions. See comment above nx_stack_push()
188 * in nx-match.c for info on how the stack is stored. */
191 /* The rule that we are currently translating, or NULL. */
192 struct rule_dpif
*rule
;
194 /* Flow translation populates this with wildcards relevant in translation.
195 * When 'xin->wc' is nonnull, this is the same pointer. When 'xin->wc' is
196 * null, this is a pointer to a temporary buffer. */
197 struct flow_wildcards
*wc
;
199 /* Output buffer for datapath actions. When 'xin->odp_actions' is nonnull,
200 * this is the same pointer. When 'xin->odp_actions' is null, this points
201 * to a scratch ofpbuf. This allows code to add actions to
202 * 'ctx->odp_actions' without worrying about whether the caller really
204 struct ofpbuf
*odp_actions
;
206 /* Statistics maintained by xlate_table_action().
208 * These statistics limit the amount of work that a single flow
209 * translation can perform. The goal of the first of these, 'depth', is
210 * primarily to prevent translation from performing an infinite amount of
211 * work. It counts the current depth of nested "resubmit"s (and a few
212 * other activities); when a resubmit returns, it decreases. Resubmits to
213 * tables in strictly monotonically increasing order don't contribute to
214 * 'depth' because they cannot cause a flow translation to take an infinite
215 * amount of time (because the number of tables is finite). Translation
216 * aborts when 'depth' exceeds MAX_DEPTH.
218 * 'resubmits', on the other hand, prevents flow translation from
219 * performing an extraordinarily large while still finite amount of work.
220 * It counts the total number of resubmits (and a few other activities)
221 * that have been executed. Returning from a resubmit does not affect this
222 * counter. Thus, this limits the amount of work that a particular
223 * translation can perform. Translation aborts when 'resubmits' exceeds
224 * MAX_RESUBMITS (which is much larger than MAX_DEPTH).
226 int depth
; /* Current resubmit nesting depth. */
227 int resubmits
; /* Total number of resubmits. */
228 bool in_group
; /* Currently translating ofgroup, if true. */
229 bool in_action_set
; /* Currently translating action_set, if true. */
231 uint8_t table_id
; /* OpenFlow table ID where flow was found. */
232 ovs_be64 rule_cookie
; /* Cookie of the rule being translated. */
233 uint32_t orig_skb_priority
; /* Priority when packet arrived. */
234 uint32_t sflow_n_outputs
; /* Number of output ports. */
235 odp_port_t sflow_odp_port
; /* Output port for composing sFlow action. */
236 ofp_port_t nf_output_iface
; /* Output interface index for NetFlow. */
237 bool exit
; /* No further actions should be processed. */
238 mirror_mask_t mirrors
; /* Bitmap of associated mirrors. */
239 int mirror_snaplen
; /* Max size of a mirror packet in byte. */
241 /* Freezing Translation
242 * ====================
244 * At some point during translation, the code may recognize the need to halt
245 * and checkpoint the translation in a way that it can be restarted again
246 * later. We call the checkpointing process "freezing" and the restarting
249 * The use cases for freezing are:
251 * - "Recirculation", where the translation process discovers that it
252 * doesn't have enough information to complete translation without
253 * actually executing the actions that have already been translated,
254 * which provides the additionally needed information. In these
255 * situations, translation freezes translation and assigns the frozen
256 * data a unique "recirculation ID", which it associates with the data
257 * in a table in userspace (see ofproto-dpif-rid.h). It also adds a
258 * OVS_ACTION_ATTR_RECIRC action specifying that ID to the datapath
259 * actions. When a packet hits that action, the datapath looks its
260 * flow up again using the ID. If there's a miss, it comes back to
261 * userspace, which find the recirculation table entry for the ID,
262 * thaws the associated frozen data, and continues translation from
263 * that point given the additional information that is now known.
265 * The archetypal example is MPLS. As MPLS is implemented in
266 * OpenFlow, the protocol that follows the last MPLS label becomes
267 * known only when that label is popped by an OpenFlow action. That
268 * means that Open vSwitch can't extract the headers beyond the MPLS
269 * labels until the pop action is executed. Thus, at that point
270 * translation uses the recirculation process to extract the headers
271 * beyond the MPLS labels.
273 * (OVS also uses OVS_ACTION_ATTR_RECIRC to implement hashing for
274 * output to bonds. OVS pre-populates all the datapath flows for bond
275 * output in the datapath, though, which means that the elaborate
276 * process of coming back to userspace for a second round of
277 * translation isn't needed, and so bonds don't follow the above
280 * - "Continuation". A continuation is a way for an OpenFlow controller
281 * to interpose on a packet's traversal of the OpenFlow tables. When
282 * the translation process encounters a "controller" action with the
283 * "pause" flag, it freezes translation, serializes the frozen data,
284 * and sends it to an OpenFlow controller. The controller then
285 * examines and possibly modifies the frozen data and eventually sends
286 * it back to the switch, which thaws it and continues translation.
288 * The main problem of freezing translation is preserving state, so that
289 * when the translation is thawed later it resumes from where it left off,
290 * without disruption. In particular, actions must be preserved as follows:
292 * - If we're freezing because an action needed more information, the
293 * action that prompted it.
295 * - Any actions remaining to be translated within the current flow.
297 * - If translation was frozen within a NXAST_RESUBMIT, then any actions
298 * following the resubmit action. Resubmit actions can be nested, so
299 * this has to go all the way up the control stack.
301 * - The OpenFlow 1.1+ action set.
303 * State that actions and flow table lookups can depend on, such as the
304 * following, must also be preserved:
306 * - Metadata fields (input port, registers, OF1.1+ metadata, ...).
308 * - The stack used by NXAST_STACK_PUSH and NXAST_STACK_POP actions.
310 * - The table ID and cookie of the flow being translated at each level
311 * of the control stack, because these can become visible through
312 * OFPAT_CONTROLLER actions (and other ways).
314 * Translation allows for the control of this state preservation via these
315 * members. When a need to freeze translation is identified, the
316 * translation process:
318 * 1. Sets 'freezing' to true.
320 * 2. Sets 'exit' to true to tell later steps that we're exiting from the
321 * translation process.
323 * 3. Adds an OFPACT_UNROLL_XLATE action to 'frozen_actions', and points
324 * frozen_actions.header to the action to make it easy to find it later.
325 * This action holds the current table ID and cookie so that they can be
326 * restored during a post-recirculation upcall translation.
328 * 4. Adds the action that prompted recirculation and any actions following
329 * it within the same flow to 'frozen_actions', so that they can be
330 * executed during a post-recirculation upcall translation.
334 * 6. The action that prompted recirculation might be nested in a stack of
335 * nested "resubmit"s that have actions remaining. Each of these notices
336 * that we're exiting and freezing and responds by adding more
337 * OFPACT_UNROLL_XLATE actions to 'frozen_actions', as necessary,
338 * followed by any actions that were yet unprocessed.
340 * If we're freezing because of recirculation, the caller generates a
341 * recirculation ID and associates all the state produced by this process
342 * with it. For post-recirculation upcall translation, the caller passes it
343 * back in for the new translation to execute. The process yielded a set of
344 * ofpacts that can be translated directly, so it is not much of a special
345 * case at that point.
348 bool recirc_update_dp_hash
; /* Generated recirculation will be preceded
349 * by datapath HASH action to get an updated
350 * dp_hash after recirculation. */
351 uint32_t dp_hash_alg
;
352 uint32_t dp_hash_basis
;
353 struct ofpbuf frozen_actions
;
354 const struct ofpact_controller
*pause
;
356 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
357 * This is a trigger for recirculation in cases where translating an action
358 * or looking up a flow requires access to the fields of the packet after
359 * the MPLS label stack that was originally present. */
362 /* True if conntrack has been performed on this packet during processing
363 * on the current bridge. This is used to determine whether conntrack
364 * state from the datapath should be honored after thawing. */
367 /* Pointer to an embedded NAT action in a conntrack action, or NULL. */
368 struct ofpact_nat
*ct_nat_action
;
370 /* OpenFlow 1.1+ action set.
372 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
373 * When translation is otherwise complete, ofpacts_execute_action_set()
374 * converts it to a set of "struct ofpact"s that can be translated into
375 * datapath actions. */
376 bool action_set_has_group
; /* Action set contains OFPACT_GROUP? */
377 struct ofpbuf action_set
; /* Action set. */
379 enum xlate_error error
; /* Translation failed. */
382 const char *xlate_strerror(enum xlate_error error
)
387 case XLATE_BRIDGE_NOT_FOUND
:
388 return "Bridge not found";
389 case XLATE_RECURSION_TOO_DEEP
:
390 return "Recursion too deep";
391 case XLATE_TOO_MANY_RESUBMITS
:
392 return "Too many resubmits";
393 case XLATE_STACK_TOO_DEEP
:
394 return "Stack too deep";
395 case XLATE_NO_RECIRCULATION_CONTEXT
:
396 return "No recirculation context";
397 case XLATE_RECIRCULATION_CONFLICT
:
398 return "Recirculation conflict";
399 case XLATE_TOO_MANY_MPLS_LABELS
:
400 return "Too many MPLS labels";
401 case XLATE_INVALID_TUNNEL_METADATA
:
402 return "Invalid tunnel metadata";
404 return "Unknown error";
407 static void xlate_action_set(struct xlate_ctx
*ctx
);
408 static void xlate_commit_actions(struct xlate_ctx
*ctx
);
411 ctx_trigger_freeze(struct xlate_ctx
*ctx
)
414 ctx
->freezing
= true;
418 ctx_trigger_recirculate_with_hash(struct xlate_ctx
*ctx
, uint32_t type
,
422 ctx
->freezing
= true;
423 ctx
->recirc_update_dp_hash
= true;
424 ctx
->dp_hash_alg
= type
;
425 ctx
->dp_hash_basis
= basis
;
429 ctx_first_frozen_action(const struct xlate_ctx
*ctx
)
431 return !ctx
->frozen_actions
.size
;
435 ctx_cancel_freeze(struct xlate_ctx
*ctx
)
438 ctx
->freezing
= false;
439 ctx
->recirc_update_dp_hash
= false;
440 ofpbuf_clear(&ctx
->frozen_actions
);
441 ctx
->frozen_actions
.header
= NULL
;
445 static void finish_freezing(struct xlate_ctx
*ctx
);
447 /* A controller may use OFPP_NONE as the ingress port to indicate that
448 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
449 * when an input bundle is needed for validation (e.g., mirroring or
450 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
451 * any 'port' structs, so care must be taken when dealing with it. */
452 static struct xbundle ofpp_none_bundle
= {
454 .vlan_mode
= PORT_VLAN_TRUNK
457 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
458 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
459 * traffic egressing the 'ofport' with that priority should be marked with. */
460 struct skb_priority_to_dscp
{
461 struct hmap_node hmap_node
; /* Node in 'ofport_dpif''s 'skb_priorities'. */
462 uint32_t skb_priority
; /* Priority of this queue (see struct flow). */
464 uint8_t dscp
; /* DSCP bits to mark outgoing traffic with. */
467 /* Xlate config contains hash maps of all bridges, bundles and ports.
468 * Xcfgp contains the pointer to the current xlate configuration.
469 * When the main thread needs to change the configuration, it copies xcfgp to
470 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
471 * does not block handler and revalidator threads. */
473 struct hmap xbridges
;
474 struct hmap xbundles
;
477 static OVSRCU_TYPE(struct xlate_cfg
*) xcfgp
= OVSRCU_INITIALIZER(NULL
);
478 static struct xlate_cfg
*new_xcfg
= NULL
;
480 static bool may_receive(const struct xport
*, struct xlate_ctx
*);
481 static void do_xlate_actions(const struct ofpact
*, size_t ofpacts_len
,
483 static void xlate_normal(struct xlate_ctx
*);
484 static void xlate_table_action(struct xlate_ctx
*, ofp_port_t in_port
,
485 uint8_t table_id
, bool may_packet_in
,
486 bool honor_table_miss
);
487 static bool input_vid_is_valid(const struct xlate_ctx
*,
488 uint16_t vid
, struct xbundle
*);
489 static uint16_t input_vid_to_vlan(const struct xbundle
*, uint16_t vid
);
490 static void output_normal(struct xlate_ctx
*, const struct xbundle
*,
493 /* Optional bond recirculation parameter to compose_output_action(). */
494 struct xlate_bond_recirc
{
495 uint32_t recirc_id
; /* !0 Use recirculation instead of output. */
496 uint8_t hash_alg
; /* !0 Compute hash for recirc before. */
497 uint32_t hash_basis
; /* Compute hash for recirc before. */
500 static void compose_output_action(struct xlate_ctx
*, ofp_port_t ofp_port
,
501 const struct xlate_bond_recirc
*xr
);
503 static struct xbridge
*xbridge_lookup(struct xlate_cfg
*,
504 const struct ofproto_dpif
*);
505 static struct xbridge
*xbridge_lookup_by_uuid(struct xlate_cfg
*,
506 const struct uuid
*);
507 static struct xbundle
*xbundle_lookup(struct xlate_cfg
*,
508 const struct ofbundle
*);
509 static struct xport
*xport_lookup(struct xlate_cfg
*,
510 const struct ofport_dpif
*);
511 static struct xport
*get_ofp_port(const struct xbridge
*, ofp_port_t ofp_port
);
512 static struct skb_priority_to_dscp
*get_skb_priority(const struct xport
*,
513 uint32_t skb_priority
);
514 static void clear_skb_priorities(struct xport
*);
515 static size_t count_skb_priorities(const struct xport
*);
516 static bool dscp_from_skb_priority(const struct xport
*, uint32_t skb_priority
,
519 static void xlate_xbridge_init(struct xlate_cfg
*, struct xbridge
*);
520 static void xlate_xbundle_init(struct xlate_cfg
*, struct xbundle
*);
521 static void xlate_xport_init(struct xlate_cfg
*, struct xport
*);
522 static void xlate_xbridge_set(struct xbridge
*, struct dpif
*,
523 const struct mac_learning
*, struct stp
*,
524 struct rstp
*, const struct mcast_snooping
*,
525 const struct mbridge
*,
526 const struct dpif_sflow
*,
527 const struct dpif_ipfix
*,
528 const struct netflow
*,
529 bool forward_bpdu
, bool has_in_band
,
530 const struct dpif_backer_support
*);
531 static void xlate_xbundle_set(struct xbundle
*xbundle
,
532 enum port_vlan_mode vlan_mode
, int vlan
,
533 unsigned long *trunks
, bool use_priority_tags
,
534 const struct bond
*bond
, const struct lacp
*lacp
,
535 bool floodable
, bool protected);
536 static void xlate_xport_set(struct xport
*xport
, odp_port_t odp_port
,
537 const struct netdev
*netdev
, const struct cfm
*cfm
,
538 const struct bfd
*bfd
, const struct lldp
*lldp
,
539 int stp_port_no
, const struct rstp_port
*rstp_port
,
540 enum ofputil_port_config config
,
541 enum ofputil_port_state state
, bool is_tunnel
,
543 static void xlate_xbridge_remove(struct xlate_cfg
*, struct xbridge
*);
544 static void xlate_xbundle_remove(struct xlate_cfg
*, struct xbundle
*);
545 static void xlate_xport_remove(struct xlate_cfg
*, struct xport
*);
546 static void xlate_xbridge_copy(struct xbridge
*);
547 static void xlate_xbundle_copy(struct xbridge
*, struct xbundle
*);
548 static void xlate_xport_copy(struct xbridge
*, struct xbundle
*,
550 static void xlate_xcfg_free(struct xlate_cfg
*);
552 /* Tracing helpers. */
554 /* If tracing is enabled in 'ctx', creates a new trace node and appends it to
555 * the list of nodes maintained in ctx->xin. The new node has type 'type' and
556 * its text is created from 'format' by treating it as a printf format string.
557 * Returns the list of nodes embedded within the new trace node; ordinarily,
558 * the calleer can ignore this, but it is useful if the caller needs to nest
559 * more trace nodes within the new node.
561 * If tracing is not enabled, does nothing and returns NULL. */
562 static struct ovs_list
* OVS_PRINTF_FORMAT(3, 4)
563 xlate_report(const struct xlate_ctx
*ctx
, enum oftrace_node_type type
,
564 const char *format
, ...)
566 struct ovs_list
*subtrace
= NULL
;
567 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
569 va_start(args
, format
);
570 char *text
= xvasprintf(format
, args
);
571 subtrace
= &oftrace_report(ctx
->xin
->trace
, type
, text
)->subs
;
578 /* This is like xlate_report() for errors that are serious enough that we
579 * should log them even if we are not tracing. */
580 static void OVS_PRINTF_FORMAT(2, 3)
581 xlate_report_error(const struct xlate_ctx
*ctx
, const char *format
, ...)
583 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
584 if (!OVS_UNLIKELY(ctx
->xin
->trace
)
585 && (!ctx
->xin
->packet
|| VLOG_DROP_WARN(&rl
))) {
589 struct ds s
= DS_EMPTY_INITIALIZER
;
591 va_start(args
, format
);
592 ds_put_format_valist(&s
, format
, args
);
595 if (ctx
->xin
->trace
) {
596 oftrace_report(ctx
->xin
->trace
, OFT_ERROR
, ds_cstr(&s
));
598 ds_put_cstr(&s
, " while processing ");
599 flow_format(&s
, &ctx
->base_flow
);
600 ds_put_format(&s
, " on bridge %s", ctx
->xbridge
->name
);
601 VLOG_WARN("%s", ds_cstr(&s
));
606 /* This is like xlate_report() for messages that should be logged at debug
607 * level (even if we are not tracing) because they can be valuable for
609 static void OVS_PRINTF_FORMAT(3, 4)
610 xlate_report_debug(const struct xlate_ctx
*ctx
, enum oftrace_node_type type
,
611 const char *format
, ...)
613 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(30, 300);
614 if (!OVS_UNLIKELY(ctx
->xin
->trace
)
615 && (!ctx
->xin
->packet
|| VLOG_DROP_DBG(&rl
))) {
619 struct ds s
= DS_EMPTY_INITIALIZER
;
621 va_start(args
, format
);
622 ds_put_format_valist(&s
, format
, args
);
625 if (ctx
->xin
->trace
) {
626 oftrace_report(ctx
->xin
->trace
, type
, ds_cstr(&s
));
628 VLOG_DBG("bridge %s: %s", ctx
->xbridge
->name
, ds_cstr(&s
));
633 /* If tracing is enabled in 'ctx', appends a node of the given 'type' to the
634 * trace, whose text is 'title' followed by a formatted version of the
635 * 'ofpacts_len' OpenFlow actions in 'ofpacts'.
637 * If tracing is not enabled, does nothing. */
639 xlate_report_actions(const struct xlate_ctx
*ctx
, enum oftrace_node_type type
,
641 const struct ofpact
*ofpacts
, size_t ofpacts_len
)
643 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
644 struct ds s
= DS_EMPTY_INITIALIZER
;
645 ds_put_format(&s
, "%s: ", title
);
646 ofpacts_format(ofpacts
, ofpacts_len
, &s
);
647 oftrace_report(ctx
->xin
->trace
, type
, ds_cstr(&s
));
652 /* If tracing is enabled in 'ctx', appends a node of type OFT_DETAIL to the
653 * trace, whose the message is a formatted version of the OpenFlow action set.
654 * 'verb' should be "was" or "is", depending on whether the action set reported
655 * is the new action set or the old one.
657 * If tracing is not enabled, does nothing. */
659 xlate_report_action_set(const struct xlate_ctx
*ctx
, const char *verb
)
661 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
662 struct ofpbuf action_list
;
663 ofpbuf_init(&action_list
, 0);
664 ofpacts_execute_action_set(&action_list
, &ctx
->action_set
);
665 if (action_list
.size
) {
666 struct ds s
= DS_EMPTY_INITIALIZER
;
667 ofpacts_format(action_list
.data
, action_list
.size
, &s
);
668 xlate_report(ctx
, OFT_DETAIL
, "action set %s: %s",
672 xlate_report(ctx
, OFT_DETAIL
, "action set %s empty", verb
);
674 ofpbuf_uninit(&action_list
);
679 /* If tracing is enabled in 'ctx', appends a node representing 'rule' (in
680 * OpenFlow table 'table_id') to the trace and makes this node the parent for
681 * future trace nodes. The caller should save ctx->xin->trace before calling
682 * this function, then after tracing all of the activities under the table,
683 * restore its previous value.
685 * If tracing is not enabled, does nothing. */
687 xlate_report_table(const struct xlate_ctx
*ctx
, struct rule_dpif
*rule
,
690 if (OVS_LIKELY(!ctx
->xin
->trace
)) {
694 struct ds s
= DS_EMPTY_INITIALIZER
;
695 ds_put_format(&s
, "%2d. ", table_id
);
696 if (rule
== ctx
->xin
->ofproto
->miss_rule
) {
697 ds_put_cstr(&s
, "No match, and a \"packet-in\" is called for.");
698 } else if (rule
== ctx
->xin
->ofproto
->no_packet_in_rule
) {
699 ds_put_cstr(&s
, "No match.");
700 } else if (rule
== ctx
->xin
->ofproto
->drop_frags_rule
) {
701 ds_put_cstr(&s
, "Packets are IP fragments and "
702 "the fragment handling mode is \"drop\".");
704 minimatch_format(&rule
->up
.cr
.match
,
705 ofproto_get_tun_tab(&ctx
->xin
->ofproto
->up
),
706 &s
, OFP_DEFAULT_PRIORITY
);
707 if (ds_last(&s
) != ' ') {
708 ds_put_cstr(&s
, ", ");
710 ds_put_format(&s
, "priority %d", rule
->up
.cr
.priority
);
711 if (rule
->up
.flow_cookie
) {
712 ds_put_format(&s
, ", cookie %#"PRIx64
,
713 ntohll(rule
->up
.flow_cookie
));
716 ctx
->xin
->trace
= &oftrace_report(ctx
->xin
->trace
, OFT_TABLE
,
721 /* If tracing is enabled in 'ctx', adds an OFT_DETAIL trace node to 'ctx'
722 * reporting the value of subfield 'sf'.
724 * If tracing is not enabled, does nothing. */
726 xlate_report_subfield(const struct xlate_ctx
*ctx
,
727 const struct mf_subfield
*sf
)
729 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
730 struct ds s
= DS_EMPTY_INITIALIZER
;
731 mf_format_subfield(sf
, &s
);
732 ds_put_cstr(&s
, " is now ");
734 if (sf
->ofs
== 0 && sf
->n_bits
>= sf
->field
->n_bits
) {
735 union mf_value value
;
736 mf_get_value(sf
->field
, &ctx
->xin
->flow
, &value
);
737 mf_format(sf
->field
, &value
, NULL
, &s
);
739 union mf_subvalue cst
;
740 mf_read_subfield(sf
, &ctx
->xin
->flow
, &cst
);
741 ds_put_hex(&s
, &cst
, sizeof cst
);
744 xlate_report(ctx
, OFT_DETAIL
, "%s", ds_cstr(&s
));
751 xlate_xbridge_init(struct xlate_cfg
*xcfg
, struct xbridge
*xbridge
)
753 ovs_list_init(&xbridge
->xbundles
);
754 hmap_init(&xbridge
->xports
);
755 hmap_insert(&xcfg
->xbridges
, &xbridge
->hmap_node
,
756 hash_pointer(xbridge
->ofproto
, 0));
760 xlate_xbundle_init(struct xlate_cfg
*xcfg
, struct xbundle
*xbundle
)
762 ovs_list_init(&xbundle
->xports
);
763 ovs_list_insert(&xbundle
->xbridge
->xbundles
, &xbundle
->list_node
);
764 hmap_insert(&xcfg
->xbundles
, &xbundle
->hmap_node
,
765 hash_pointer(xbundle
->ofbundle
, 0));
769 xlate_xport_init(struct xlate_cfg
*xcfg
, struct xport
*xport
)
771 hmap_init(&xport
->skb_priorities
);
772 hmap_insert(&xcfg
->xports
, &xport
->hmap_node
,
773 hash_pointer(xport
->ofport
, 0));
774 hmap_insert(&xport
->xbridge
->xports
, &xport
->ofp_node
,
775 hash_ofp_port(xport
->ofp_port
));
779 xlate_xbridge_set(struct xbridge
*xbridge
,
781 const struct mac_learning
*ml
, struct stp
*stp
,
782 struct rstp
*rstp
, const struct mcast_snooping
*ms
,
783 const struct mbridge
*mbridge
,
784 const struct dpif_sflow
*sflow
,
785 const struct dpif_ipfix
*ipfix
,
786 const struct netflow
*netflow
,
787 bool forward_bpdu
, bool has_in_band
,
788 const struct dpif_backer_support
*support
)
790 if (xbridge
->ml
!= ml
) {
791 mac_learning_unref(xbridge
->ml
);
792 xbridge
->ml
= mac_learning_ref(ml
);
795 if (xbridge
->ms
!= ms
) {
796 mcast_snooping_unref(xbridge
->ms
);
797 xbridge
->ms
= mcast_snooping_ref(ms
);
800 if (xbridge
->mbridge
!= mbridge
) {
801 mbridge_unref(xbridge
->mbridge
);
802 xbridge
->mbridge
= mbridge_ref(mbridge
);
805 if (xbridge
->sflow
!= sflow
) {
806 dpif_sflow_unref(xbridge
->sflow
);
807 xbridge
->sflow
= dpif_sflow_ref(sflow
);
810 if (xbridge
->ipfix
!= ipfix
) {
811 dpif_ipfix_unref(xbridge
->ipfix
);
812 xbridge
->ipfix
= dpif_ipfix_ref(ipfix
);
815 if (xbridge
->stp
!= stp
) {
816 stp_unref(xbridge
->stp
);
817 xbridge
->stp
= stp_ref(stp
);
820 if (xbridge
->rstp
!= rstp
) {
821 rstp_unref(xbridge
->rstp
);
822 xbridge
->rstp
= rstp_ref(rstp
);
825 if (xbridge
->netflow
!= netflow
) {
826 netflow_unref(xbridge
->netflow
);
827 xbridge
->netflow
= netflow_ref(netflow
);
830 xbridge
->dpif
= dpif
;
831 xbridge
->forward_bpdu
= forward_bpdu
;
832 xbridge
->has_in_band
= has_in_band
;
833 xbridge
->support
= *support
;
837 xlate_xbundle_set(struct xbundle
*xbundle
,
838 enum port_vlan_mode vlan_mode
, int vlan
,
839 unsigned long *trunks
, bool use_priority_tags
,
840 const struct bond
*bond
, const struct lacp
*lacp
,
841 bool floodable
, bool protected)
843 ovs_assert(xbundle
->xbridge
);
845 xbundle
->vlan_mode
= vlan_mode
;
846 xbundle
->vlan
= vlan
;
847 xbundle
->trunks
= trunks
;
848 xbundle
->use_priority_tags
= use_priority_tags
;
849 xbundle
->floodable
= floodable
;
850 xbundle
->protected = protected;
852 if (xbundle
->bond
!= bond
) {
853 bond_unref(xbundle
->bond
);
854 xbundle
->bond
= bond_ref(bond
);
857 if (xbundle
->lacp
!= lacp
) {
858 lacp_unref(xbundle
->lacp
);
859 xbundle
->lacp
= lacp_ref(lacp
);
864 xlate_xport_set(struct xport
*xport
, odp_port_t odp_port
,
865 const struct netdev
*netdev
, const struct cfm
*cfm
,
866 const struct bfd
*bfd
, const struct lldp
*lldp
, int stp_port_no
,
867 const struct rstp_port
* rstp_port
,
868 enum ofputil_port_config config
, enum ofputil_port_state state
,
869 bool is_tunnel
, bool may_enable
)
871 xport
->config
= config
;
872 xport
->state
= state
;
873 xport
->stp_port_no
= stp_port_no
;
874 xport
->is_tunnel
= is_tunnel
;
875 xport
->may_enable
= may_enable
;
876 xport
->odp_port
= odp_port
;
878 if (xport
->rstp_port
!= rstp_port
) {
879 rstp_port_unref(xport
->rstp_port
);
880 xport
->rstp_port
= rstp_port_ref(rstp_port
);
883 if (xport
->cfm
!= cfm
) {
884 cfm_unref(xport
->cfm
);
885 xport
->cfm
= cfm_ref(cfm
);
888 if (xport
->bfd
!= bfd
) {
889 bfd_unref(xport
->bfd
);
890 xport
->bfd
= bfd_ref(bfd
);
893 if (xport
->lldp
!= lldp
) {
894 lldp_unref(xport
->lldp
);
895 xport
->lldp
= lldp_ref(lldp
);
898 if (xport
->netdev
!= netdev
) {
899 netdev_close(xport
->netdev
);
900 xport
->netdev
= netdev_ref(netdev
);
905 xlate_xbridge_copy(struct xbridge
*xbridge
)
907 struct xbundle
*xbundle
;
909 struct xbridge
*new_xbridge
= xzalloc(sizeof *xbridge
);
910 new_xbridge
->ofproto
= xbridge
->ofproto
;
911 new_xbridge
->name
= xstrdup(xbridge
->name
);
912 xlate_xbridge_init(new_xcfg
, new_xbridge
);
914 xlate_xbridge_set(new_xbridge
,
915 xbridge
->dpif
, xbridge
->ml
, xbridge
->stp
,
916 xbridge
->rstp
, xbridge
->ms
, xbridge
->mbridge
,
917 xbridge
->sflow
, xbridge
->ipfix
, xbridge
->netflow
,
918 xbridge
->forward_bpdu
, xbridge
->has_in_band
,
920 LIST_FOR_EACH (xbundle
, list_node
, &xbridge
->xbundles
) {
921 xlate_xbundle_copy(new_xbridge
, xbundle
);
924 /* Copy xports which are not part of a xbundle */
925 HMAP_FOR_EACH (xport
, ofp_node
, &xbridge
->xports
) {
926 if (!xport
->xbundle
) {
927 xlate_xport_copy(new_xbridge
, NULL
, xport
);
933 xlate_xbundle_copy(struct xbridge
*xbridge
, struct xbundle
*xbundle
)
936 struct xbundle
*new_xbundle
= xzalloc(sizeof *xbundle
);
937 new_xbundle
->ofbundle
= xbundle
->ofbundle
;
938 new_xbundle
->xbridge
= xbridge
;
939 new_xbundle
->name
= xstrdup(xbundle
->name
);
940 xlate_xbundle_init(new_xcfg
, new_xbundle
);
942 xlate_xbundle_set(new_xbundle
, xbundle
->vlan_mode
,
943 xbundle
->vlan
, xbundle
->trunks
,
944 xbundle
->use_priority_tags
, xbundle
->bond
, xbundle
->lacp
,
945 xbundle
->floodable
, xbundle
->protected);
946 LIST_FOR_EACH (xport
, bundle_node
, &xbundle
->xports
) {
947 xlate_xport_copy(xbridge
, new_xbundle
, xport
);
952 xlate_xport_copy(struct xbridge
*xbridge
, struct xbundle
*xbundle
,
955 struct skb_priority_to_dscp
*pdscp
, *new_pdscp
;
956 struct xport
*new_xport
= xzalloc(sizeof *xport
);
957 new_xport
->ofport
= xport
->ofport
;
958 new_xport
->ofp_port
= xport
->ofp_port
;
959 new_xport
->xbridge
= xbridge
;
960 xlate_xport_init(new_xcfg
, new_xport
);
962 xlate_xport_set(new_xport
, xport
->odp_port
, xport
->netdev
, xport
->cfm
,
963 xport
->bfd
, xport
->lldp
, xport
->stp_port_no
,
964 xport
->rstp_port
, xport
->config
, xport
->state
,
965 xport
->is_tunnel
, xport
->may_enable
);
968 struct xport
*peer
= xport_lookup(new_xcfg
, xport
->peer
->ofport
);
970 new_xport
->peer
= peer
;
971 new_xport
->peer
->peer
= new_xport
;
976 new_xport
->xbundle
= xbundle
;
977 ovs_list_insert(&new_xport
->xbundle
->xports
, &new_xport
->bundle_node
);
980 HMAP_FOR_EACH (pdscp
, hmap_node
, &xport
->skb_priorities
) {
981 new_pdscp
= xmalloc(sizeof *pdscp
);
982 new_pdscp
->skb_priority
= pdscp
->skb_priority
;
983 new_pdscp
->dscp
= pdscp
->dscp
;
984 hmap_insert(&new_xport
->skb_priorities
, &new_pdscp
->hmap_node
,
985 hash_int(new_pdscp
->skb_priority
, 0));
989 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
990 * configuration in xcfgp.
992 * This needs to be called after editing the xlate configuration.
994 * Functions that edit the new xlate configuration are
995 * xlate_<ofproto/bundle/ofport>_set and xlate_<ofproto/bundle/ofport>_remove.
1001 * edit_xlate_configuration();
1003 * xlate_txn_commit(); */
1005 xlate_txn_commit(void)
1007 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1009 ovsrcu_set(&xcfgp
, new_xcfg
);
1010 ovsrcu_synchronize();
1011 xlate_xcfg_free(xcfg
);
1015 /* Copies the current xlate configuration in xcfgp to new_xcfg.
1017 * This needs to be called prior to editing the xlate configuration. */
1019 xlate_txn_start(void)
1021 struct xbridge
*xbridge
;
1022 struct xlate_cfg
*xcfg
;
1024 ovs_assert(!new_xcfg
);
1026 new_xcfg
= xmalloc(sizeof *new_xcfg
);
1027 hmap_init(&new_xcfg
->xbridges
);
1028 hmap_init(&new_xcfg
->xbundles
);
1029 hmap_init(&new_xcfg
->xports
);
1031 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1036 HMAP_FOR_EACH (xbridge
, hmap_node
, &xcfg
->xbridges
) {
1037 xlate_xbridge_copy(xbridge
);
1043 xlate_xcfg_free(struct xlate_cfg
*xcfg
)
1045 struct xbridge
*xbridge
, *next_xbridge
;
1051 HMAP_FOR_EACH_SAFE (xbridge
, next_xbridge
, hmap_node
, &xcfg
->xbridges
) {
1052 xlate_xbridge_remove(xcfg
, xbridge
);
1055 hmap_destroy(&xcfg
->xbridges
);
1056 hmap_destroy(&xcfg
->xbundles
);
1057 hmap_destroy(&xcfg
->xports
);
1062 xlate_ofproto_set(struct ofproto_dpif
*ofproto
, const char *name
,
1064 const struct mac_learning
*ml
, struct stp
*stp
,
1065 struct rstp
*rstp
, const struct mcast_snooping
*ms
,
1066 const struct mbridge
*mbridge
,
1067 const struct dpif_sflow
*sflow
,
1068 const struct dpif_ipfix
*ipfix
,
1069 const struct netflow
*netflow
,
1070 bool forward_bpdu
, bool has_in_band
,
1071 const struct dpif_backer_support
*support
)
1073 struct xbridge
*xbridge
;
1075 ovs_assert(new_xcfg
);
1077 xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
1079 xbridge
= xzalloc(sizeof *xbridge
);
1080 xbridge
->ofproto
= ofproto
;
1082 xlate_xbridge_init(new_xcfg
, xbridge
);
1085 free(xbridge
->name
);
1086 xbridge
->name
= xstrdup(name
);
1088 xlate_xbridge_set(xbridge
, dpif
, ml
, stp
, rstp
, ms
, mbridge
, sflow
, ipfix
,
1089 netflow
, forward_bpdu
, has_in_band
, support
);
1093 xlate_xbridge_remove(struct xlate_cfg
*xcfg
, struct xbridge
*xbridge
)
1095 struct xbundle
*xbundle
, *next_xbundle
;
1096 struct xport
*xport
, *next_xport
;
1102 HMAP_FOR_EACH_SAFE (xport
, next_xport
, ofp_node
, &xbridge
->xports
) {
1103 xlate_xport_remove(xcfg
, xport
);
1106 LIST_FOR_EACH_SAFE (xbundle
, next_xbundle
, list_node
, &xbridge
->xbundles
) {
1107 xlate_xbundle_remove(xcfg
, xbundle
);
1110 hmap_remove(&xcfg
->xbridges
, &xbridge
->hmap_node
);
1111 mac_learning_unref(xbridge
->ml
);
1112 mcast_snooping_unref(xbridge
->ms
);
1113 mbridge_unref(xbridge
->mbridge
);
1114 dpif_sflow_unref(xbridge
->sflow
);
1115 dpif_ipfix_unref(xbridge
->ipfix
);
1116 stp_unref(xbridge
->stp
);
1117 rstp_unref(xbridge
->rstp
);
1118 hmap_destroy(&xbridge
->xports
);
1119 free(xbridge
->name
);
1124 xlate_remove_ofproto(struct ofproto_dpif
*ofproto
)
1126 struct xbridge
*xbridge
;
1128 ovs_assert(new_xcfg
);
1130 xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
1131 xlate_xbridge_remove(new_xcfg
, xbridge
);
1135 xlate_bundle_set(struct ofproto_dpif
*ofproto
, struct ofbundle
*ofbundle
,
1136 const char *name
, enum port_vlan_mode vlan_mode
, int vlan
,
1137 unsigned long *trunks
, bool use_priority_tags
,
1138 const struct bond
*bond
, const struct lacp
*lacp
,
1139 bool floodable
, bool protected)
1141 struct xbundle
*xbundle
;
1143 ovs_assert(new_xcfg
);
1145 xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
1147 xbundle
= xzalloc(sizeof *xbundle
);
1148 xbundle
->ofbundle
= ofbundle
;
1149 xbundle
->xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
1151 xlate_xbundle_init(new_xcfg
, xbundle
);
1154 free(xbundle
->name
);
1155 xbundle
->name
= xstrdup(name
);
1157 xlate_xbundle_set(xbundle
, vlan_mode
, vlan
, trunks
,
1158 use_priority_tags
, bond
, lacp
, floodable
, protected);
1162 xlate_xbundle_remove(struct xlate_cfg
*xcfg
, struct xbundle
*xbundle
)
1164 struct xport
*xport
;
1170 LIST_FOR_EACH_POP (xport
, bundle_node
, &xbundle
->xports
) {
1171 xport
->xbundle
= NULL
;
1174 hmap_remove(&xcfg
->xbundles
, &xbundle
->hmap_node
);
1175 ovs_list_remove(&xbundle
->list_node
);
1176 bond_unref(xbundle
->bond
);
1177 lacp_unref(xbundle
->lacp
);
1178 free(xbundle
->name
);
1183 xlate_bundle_remove(struct ofbundle
*ofbundle
)
1185 struct xbundle
*xbundle
;
1187 ovs_assert(new_xcfg
);
1189 xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
1190 xlate_xbundle_remove(new_xcfg
, xbundle
);
1194 xlate_ofport_set(struct ofproto_dpif
*ofproto
, struct ofbundle
*ofbundle
,
1195 struct ofport_dpif
*ofport
, ofp_port_t ofp_port
,
1196 odp_port_t odp_port
, const struct netdev
*netdev
,
1197 const struct cfm
*cfm
, const struct bfd
*bfd
,
1198 const struct lldp
*lldp
, struct ofport_dpif
*peer
,
1199 int stp_port_no
, const struct rstp_port
*rstp_port
,
1200 const struct ofproto_port_queue
*qdscp_list
, size_t n_qdscp
,
1201 enum ofputil_port_config config
,
1202 enum ofputil_port_state state
, bool is_tunnel
,
1206 struct xport
*xport
;
1208 ovs_assert(new_xcfg
);
1210 xport
= xport_lookup(new_xcfg
, ofport
);
1212 xport
= xzalloc(sizeof *xport
);
1213 xport
->ofport
= ofport
;
1214 xport
->xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
1215 xport
->ofp_port
= ofp_port
;
1217 xlate_xport_init(new_xcfg
, xport
);
1220 ovs_assert(xport
->ofp_port
== ofp_port
);
1222 xlate_xport_set(xport
, odp_port
, netdev
, cfm
, bfd
, lldp
,
1223 stp_port_no
, rstp_port
, config
, state
, is_tunnel
,
1227 xport
->peer
->peer
= NULL
;
1229 xport
->peer
= xport_lookup(new_xcfg
, peer
);
1231 xport
->peer
->peer
= xport
;
1234 if (xport
->xbundle
) {
1235 ovs_list_remove(&xport
->bundle_node
);
1237 xport
->xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
1238 if (xport
->xbundle
) {
1239 ovs_list_insert(&xport
->xbundle
->xports
, &xport
->bundle_node
);
1242 clear_skb_priorities(xport
);
1243 for (i
= 0; i
< n_qdscp
; i
++) {
1244 struct skb_priority_to_dscp
*pdscp
;
1245 uint32_t skb_priority
;
1247 if (dpif_queue_to_priority(xport
->xbridge
->dpif
, qdscp_list
[i
].queue
,
1252 pdscp
= xmalloc(sizeof *pdscp
);
1253 pdscp
->skb_priority
= skb_priority
;
1254 pdscp
->dscp
= (qdscp_list
[i
].dscp
<< 2) & IP_DSCP_MASK
;
1255 hmap_insert(&xport
->skb_priorities
, &pdscp
->hmap_node
,
1256 hash_int(pdscp
->skb_priority
, 0));
1261 xlate_xport_remove(struct xlate_cfg
*xcfg
, struct xport
*xport
)
1268 xport
->peer
->peer
= NULL
;
1272 if (xport
->xbundle
) {
1273 ovs_list_remove(&xport
->bundle_node
);
1276 clear_skb_priorities(xport
);
1277 hmap_destroy(&xport
->skb_priorities
);
1279 hmap_remove(&xcfg
->xports
, &xport
->hmap_node
);
1280 hmap_remove(&xport
->xbridge
->xports
, &xport
->ofp_node
);
1282 netdev_close(xport
->netdev
);
1283 rstp_port_unref(xport
->rstp_port
);
1284 cfm_unref(xport
->cfm
);
1285 bfd_unref(xport
->bfd
);
1286 lldp_unref(xport
->lldp
);
1291 xlate_ofport_remove(struct ofport_dpif
*ofport
)
1293 struct xport
*xport
;
1295 ovs_assert(new_xcfg
);
1297 xport
= xport_lookup(new_xcfg
, ofport
);
1298 xlate_xport_remove(new_xcfg
, xport
);
1301 static struct ofproto_dpif
*
1302 xlate_lookup_ofproto_(const struct dpif_backer
*backer
, const struct flow
*flow
,
1303 ofp_port_t
*ofp_in_port
, const struct xport
**xportp
)
1305 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1306 const struct xport
*xport
;
1308 xport
= xport_lookup(xcfg
, tnl_port_should_receive(flow
)
1309 ? tnl_port_receive(flow
)
1310 : odp_port_to_ofport(backer
, flow
->in_port
.odp_port
));
1311 if (OVS_UNLIKELY(!xport
)) {
1316 *ofp_in_port
= xport
->ofp_port
;
1318 return xport
->xbridge
->ofproto
;
1321 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1322 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1323 struct ofproto_dpif
*
1324 xlate_lookup_ofproto(const struct dpif_backer
*backer
, const struct flow
*flow
,
1325 ofp_port_t
*ofp_in_port
)
1327 const struct xport
*xport
;
1329 return xlate_lookup_ofproto_(backer
, flow
, ofp_in_port
, &xport
);
1332 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1333 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
1334 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1335 * handles for those protocols if they're enabled. Caller may use the returned
1336 * pointers until quiescing, for longer term use additional references must
1339 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1342 xlate_lookup(const struct dpif_backer
*backer
, const struct flow
*flow
,
1343 struct ofproto_dpif
**ofprotop
, struct dpif_ipfix
**ipfix
,
1344 struct dpif_sflow
**sflow
, struct netflow
**netflow
,
1345 ofp_port_t
*ofp_in_port
)
1347 struct ofproto_dpif
*ofproto
;
1348 const struct xport
*xport
;
1350 ofproto
= xlate_lookup_ofproto_(backer
, flow
, ofp_in_port
, &xport
);
1357 *ofprotop
= ofproto
;
1361 *ipfix
= xport
? xport
->xbridge
->ipfix
: NULL
;
1365 *sflow
= xport
? xport
->xbridge
->sflow
: NULL
;
1369 *netflow
= xport
? xport
->xbridge
->netflow
: NULL
;
1375 static struct xbridge
*
1376 xbridge_lookup(struct xlate_cfg
*xcfg
, const struct ofproto_dpif
*ofproto
)
1378 struct hmap
*xbridges
;
1379 struct xbridge
*xbridge
;
1381 if (!ofproto
|| !xcfg
) {
1385 xbridges
= &xcfg
->xbridges
;
1387 HMAP_FOR_EACH_IN_BUCKET (xbridge
, hmap_node
, hash_pointer(ofproto
, 0),
1389 if (xbridge
->ofproto
== ofproto
) {
1396 static struct xbridge
*
1397 xbridge_lookup_by_uuid(struct xlate_cfg
*xcfg
, const struct uuid
*uuid
)
1399 struct xbridge
*xbridge
;
1401 HMAP_FOR_EACH (xbridge
, hmap_node
, &xcfg
->xbridges
) {
1402 if (uuid_equals(&xbridge
->ofproto
->uuid
, uuid
)) {
1409 static struct xbundle
*
1410 xbundle_lookup(struct xlate_cfg
*xcfg
, const struct ofbundle
*ofbundle
)
1412 struct hmap
*xbundles
;
1413 struct xbundle
*xbundle
;
1415 if (!ofbundle
|| !xcfg
) {
1419 xbundles
= &xcfg
->xbundles
;
1421 HMAP_FOR_EACH_IN_BUCKET (xbundle
, hmap_node
, hash_pointer(ofbundle
, 0),
1423 if (xbundle
->ofbundle
== ofbundle
) {
1430 static struct xport
*
1431 xport_lookup(struct xlate_cfg
*xcfg
, const struct ofport_dpif
*ofport
)
1433 struct hmap
*xports
;
1434 struct xport
*xport
;
1436 if (!ofport
|| !xcfg
) {
1440 xports
= &xcfg
->xports
;
1442 HMAP_FOR_EACH_IN_BUCKET (xport
, hmap_node
, hash_pointer(ofport
, 0),
1444 if (xport
->ofport
== ofport
) {
1451 static struct stp_port
*
1452 xport_get_stp_port(const struct xport
*xport
)
1454 return xport
->xbridge
->stp
&& xport
->stp_port_no
!= -1
1455 ? stp_get_port(xport
->xbridge
->stp
, xport
->stp_port_no
)
1460 xport_stp_learn_state(const struct xport
*xport
)
1462 struct stp_port
*sp
= xport_get_stp_port(xport
);
1464 ? stp_learn_in_state(stp_port_get_state(sp
))
1469 xport_stp_forward_state(const struct xport
*xport
)
1471 struct stp_port
*sp
= xport_get_stp_port(xport
);
1473 ? stp_forward_in_state(stp_port_get_state(sp
))
1478 xport_stp_should_forward_bpdu(const struct xport
*xport
)
1480 struct stp_port
*sp
= xport_get_stp_port(xport
);
1481 return stp_should_forward_bpdu(sp
? stp_port_get_state(sp
) : STP_DISABLED
);
1484 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1485 * were used to make the determination.*/
1487 stp_should_process_flow(const struct flow
*flow
, struct flow_wildcards
*wc
)
1489 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1490 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
1491 return is_stp(flow
);
1495 stp_process_packet(const struct xport
*xport
, const struct dp_packet
*packet
)
1497 struct stp_port
*sp
= xport_get_stp_port(xport
);
1498 struct dp_packet payload
= *packet
;
1499 struct eth_header
*eth
= dp_packet_data(&payload
);
1501 /* Sink packets on ports that have STP disabled when the bridge has
1503 if (!sp
|| stp_port_get_state(sp
) == STP_DISABLED
) {
1507 /* Trim off padding on payload. */
1508 if (dp_packet_size(&payload
) > ntohs(eth
->eth_type
) + ETH_HEADER_LEN
) {
1509 dp_packet_set_size(&payload
, ntohs(eth
->eth_type
) + ETH_HEADER_LEN
);
1512 if (dp_packet_try_pull(&payload
, ETH_HEADER_LEN
+ LLC_HEADER_LEN
)) {
1513 stp_received_bpdu(sp
, dp_packet_data(&payload
), dp_packet_size(&payload
));
1517 static enum rstp_state
1518 xport_get_rstp_port_state(const struct xport
*xport
)
1520 return xport
->rstp_port
1521 ? rstp_port_get_state(xport
->rstp_port
)
1526 xport_rstp_learn_state(const struct xport
*xport
)
1528 return xport
->xbridge
->rstp
&& xport
->rstp_port
1529 ? rstp_learn_in_state(xport_get_rstp_port_state(xport
))
1534 xport_rstp_forward_state(const struct xport
*xport
)
1536 return xport
->xbridge
->rstp
&& xport
->rstp_port
1537 ? rstp_forward_in_state(xport_get_rstp_port_state(xport
))
1542 xport_rstp_should_manage_bpdu(const struct xport
*xport
)
1544 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport
));
1548 rstp_process_packet(const struct xport
*xport
, const struct dp_packet
*packet
)
1550 struct dp_packet payload
= *packet
;
1551 struct eth_header
*eth
= dp_packet_data(&payload
);
1553 /* Sink packets on ports that have no RSTP. */
1554 if (!xport
->rstp_port
) {
1558 /* Trim off padding on payload. */
1559 if (dp_packet_size(&payload
) > ntohs(eth
->eth_type
) + ETH_HEADER_LEN
) {
1560 dp_packet_set_size(&payload
, ntohs(eth
->eth_type
) + ETH_HEADER_LEN
);
1563 if (dp_packet_try_pull(&payload
, ETH_HEADER_LEN
+ LLC_HEADER_LEN
)) {
1564 rstp_port_received_bpdu(xport
->rstp_port
, dp_packet_data(&payload
),
1565 dp_packet_size(&payload
));
1569 static struct xport
*
1570 get_ofp_port(const struct xbridge
*xbridge
, ofp_port_t ofp_port
)
1572 struct xport
*xport
;
1574 HMAP_FOR_EACH_IN_BUCKET (xport
, ofp_node
, hash_ofp_port(ofp_port
),
1576 if (xport
->ofp_port
== ofp_port
) {
1584 ofp_port_to_odp_port(const struct xbridge
*xbridge
, ofp_port_t ofp_port
)
1586 const struct xport
*xport
= get_ofp_port(xbridge
, ofp_port
);
1587 return xport
? xport
->odp_port
: ODPP_NONE
;
1591 odp_port_is_alive(const struct xlate_ctx
*ctx
, ofp_port_t ofp_port
)
1593 struct xport
*xport
= get_ofp_port(ctx
->xbridge
, ofp_port
);
1594 return xport
&& xport
->may_enable
;
1597 static struct ofputil_bucket
*
1598 group_first_live_bucket(const struct xlate_ctx
*, const struct group_dpif
*,
1602 group_is_alive(const struct xlate_ctx
*ctx
, uint32_t group_id
, int depth
)
1604 struct group_dpif
*group
;
1606 group
= group_dpif_lookup(ctx
->xbridge
->ofproto
, group_id
,
1607 ctx
->xin
->tables_version
, false);
1609 return group_first_live_bucket(ctx
, group
, depth
) != NULL
;
1615 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1618 bucket_is_alive(const struct xlate_ctx
*ctx
,
1619 struct ofputil_bucket
*bucket
, int depth
)
1621 if (depth
>= MAX_LIVENESS_RECURSION
) {
1622 xlate_report_error(ctx
, "bucket chaining exceeded %d links",
1623 MAX_LIVENESS_RECURSION
);
1627 return (!ofputil_bucket_has_liveness(bucket
)
1628 || (bucket
->watch_port
!= OFPP_ANY
1629 && odp_port_is_alive(ctx
, bucket
->watch_port
))
1630 || (bucket
->watch_group
!= OFPG_ANY
1631 && group_is_alive(ctx
, bucket
->watch_group
, depth
+ 1)));
1634 static struct ofputil_bucket
*
1635 group_first_live_bucket(const struct xlate_ctx
*ctx
,
1636 const struct group_dpif
*group
, int depth
)
1638 struct ofputil_bucket
*bucket
;
1639 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
1640 if (bucket_is_alive(ctx
, bucket
, depth
)) {
1648 static struct ofputil_bucket
*
1649 group_best_live_bucket(const struct xlate_ctx
*ctx
,
1650 const struct group_dpif
*group
,
1653 struct ofputil_bucket
*best_bucket
= NULL
;
1654 uint32_t best_score
= 0;
1656 struct ofputil_bucket
*bucket
;
1657 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
1658 if (bucket_is_alive(ctx
, bucket
, 0)) {
1660 (hash_int(bucket
->bucket_id
, basis
) & 0xffff) * bucket
->weight
;
1661 if (score
>= best_score
) {
1662 best_bucket
= bucket
;
1672 xbundle_trunks_vlan(const struct xbundle
*bundle
, uint16_t vlan
)
1674 return (bundle
->vlan_mode
!= PORT_VLAN_ACCESS
1675 && (!bundle
->trunks
|| bitmap_is_set(bundle
->trunks
, vlan
)));
1679 xbundle_includes_vlan(const struct xbundle
*xbundle
, uint16_t vlan
)
1681 return vlan
== xbundle
->vlan
|| xbundle_trunks_vlan(xbundle
, vlan
);
1684 static mirror_mask_t
1685 xbundle_mirror_out(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1687 return xbundle
!= &ofpp_none_bundle
1688 ? mirror_bundle_out(xbridge
->mbridge
, xbundle
->ofbundle
)
1692 static mirror_mask_t
1693 xbundle_mirror_src(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1695 return xbundle
!= &ofpp_none_bundle
1696 ? mirror_bundle_src(xbridge
->mbridge
, xbundle
->ofbundle
)
1700 static mirror_mask_t
1701 xbundle_mirror_dst(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1703 return xbundle
!= &ofpp_none_bundle
1704 ? mirror_bundle_dst(xbridge
->mbridge
, xbundle
->ofbundle
)
1708 static struct xbundle
*
1709 lookup_input_bundle__(const struct xbridge
*xbridge
,
1710 ofp_port_t in_port
, struct xport
**in_xportp
)
1712 struct xport
*xport
;
1714 /* Find the port and bundle for the received packet. */
1715 xport
= get_ofp_port(xbridge
, in_port
);
1719 if (xport
&& xport
->xbundle
) {
1720 return xport
->xbundle
;
1723 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1724 * which a controller may use as the ingress port for traffic that
1725 * it is sourcing. */
1726 if (in_port
== OFPP_CONTROLLER
|| in_port
== OFPP_NONE
) {
1727 return &ofpp_none_bundle
;
1732 static struct xbundle
*
1733 lookup_input_bundle(const struct xlate_ctx
*ctx
,
1734 ofp_port_t in_port
, struct xport
**in_xportp
)
1736 struct xbundle
*xbundle
= lookup_input_bundle__(ctx
->xbridge
,
1737 in_port
, in_xportp
);
1739 /* Odd. A few possible reasons here:
1741 * - We deleted a port but there are still a few packets queued up
1744 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1745 * we don't know about.
1747 * - The ofproto client didn't configure the port as part of a bundle.
1748 * This is particularly likely to happen if a packet was received on
1749 * the port after it was created, but before the client had a chance
1750 * to configure its bundle.
1752 xlate_report_error(ctx
, "received packet on unknown port %"PRIu32
,
1758 /* Mirrors the packet represented by 'ctx' to appropriate mirror destinations,
1759 * given the packet is ingressing or egressing on 'xbundle', which has ingress
1760 * or egress (as appropriate) mirrors 'mirrors'. */
1762 mirror_packet(struct xlate_ctx
*ctx
, struct xbundle
*xbundle
,
1763 mirror_mask_t mirrors
)
1765 /* Figure out what VLAN the packet is in (because mirrors can select
1766 * packets on basis of VLAN). */
1767 uint16_t vid
= vlan_tci_to_vid(ctx
->xin
->flow
.vlan_tci
);
1768 if (!input_vid_is_valid(ctx
, vid
, xbundle
)) {
1771 uint16_t vlan
= input_vid_to_vlan(xbundle
, vid
);
1773 const struct xbridge
*xbridge
= ctx
->xbridge
;
1775 /* Don't mirror to destinations that we've already mirrored to. */
1776 mirrors
&= ~ctx
->mirrors
;
1781 if (ctx
->xin
->resubmit_stats
) {
1782 mirror_update_stats(xbridge
->mbridge
, mirrors
,
1783 ctx
->xin
->resubmit_stats
->n_packets
,
1784 ctx
->xin
->resubmit_stats
->n_bytes
);
1786 if (ctx
->xin
->xcache
) {
1787 struct xc_entry
*entry
;
1789 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_MIRROR
);
1790 entry
->mirror
.mbridge
= mbridge_ref(xbridge
->mbridge
);
1791 entry
->mirror
.mirrors
= mirrors
;
1794 /* 'mirrors' is a bit-mask of candidates for mirroring. Iterate as long as
1795 * some candidates remain. */
1797 const unsigned long *vlans
;
1798 mirror_mask_t dup_mirrors
;
1799 struct ofbundle
*out
;
1803 /* Get the details of the mirror represented by the rightmost 1-bit. */
1804 bool has_mirror
= mirror_get(xbridge
->mbridge
, raw_ctz(mirrors
),
1805 &vlans
, &dup_mirrors
,
1806 &out
, &snaplen
, &out_vlan
);
1807 ovs_assert(has_mirror
);
1810 /* If this mirror selects on the basis of VLAN, and it does not select
1811 * 'vlan', then discard this mirror and go on to the next one. */
1813 ctx
->wc
->masks
.vlan_tci
|= htons(VLAN_CFI
| VLAN_VID_MASK
);
1815 if (vlans
&& !bitmap_is_set(vlans
, vlan
)) {
1816 mirrors
= zero_rightmost_1bit(mirrors
);
1820 /* Record the mirror, and the mirrors that output to the same
1821 * destination, so that we don't mirror to them again. This must be
1822 * done now to ensure that output_normal(), below, doesn't recursively
1823 * output to the same mirrors. */
1824 ctx
->mirrors
|= dup_mirrors
;
1825 ctx
->mirror_snaplen
= snaplen
;
1827 /* Send the packet to the mirror. */
1829 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1830 struct xbundle
*out_xbundle
= xbundle_lookup(xcfg
, out
);
1832 output_normal(ctx
, out_xbundle
, vlan
);
1834 } else if (vlan
!= out_vlan
1835 && !eth_addr_is_reserved(ctx
->xin
->flow
.dl_dst
)) {
1836 struct xbundle
*xbundle
;
1838 LIST_FOR_EACH (xbundle
, list_node
, &xbridge
->xbundles
) {
1839 if (xbundle_includes_vlan(xbundle
, out_vlan
)
1840 && !xbundle_mirror_out(xbridge
, xbundle
)) {
1841 output_normal(ctx
, xbundle
, out_vlan
);
1846 /* output_normal() could have recursively output (to different
1847 * mirrors), so make sure that we don't send duplicates. */
1848 mirrors
&= ~ctx
->mirrors
;
1849 ctx
->mirror_snaplen
= 0;
1854 mirror_ingress_packet(struct xlate_ctx
*ctx
)
1856 if (mbridge_has_mirrors(ctx
->xbridge
->mbridge
)) {
1857 struct xbundle
*xbundle
= lookup_input_bundle(
1858 ctx
, ctx
->xin
->flow
.in_port
.ofp_port
, NULL
);
1860 mirror_packet(ctx
, xbundle
,
1861 xbundle_mirror_src(ctx
->xbridge
, xbundle
));
1866 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1867 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1868 * the bundle on which the packet was received, returns the VLAN to which the
1871 * Both 'vid' and the return value are in the range 0...4095. */
1873 input_vid_to_vlan(const struct xbundle
*in_xbundle
, uint16_t vid
)
1875 switch (in_xbundle
->vlan_mode
) {
1876 case PORT_VLAN_ACCESS
:
1877 return in_xbundle
->vlan
;
1880 case PORT_VLAN_TRUNK
:
1883 case PORT_VLAN_NATIVE_UNTAGGED
:
1884 case PORT_VLAN_NATIVE_TAGGED
:
1885 return vid
? vid
: in_xbundle
->vlan
;
1892 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1893 * If so, returns true. Otherwise, returns false.
1895 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1896 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1899 input_vid_is_valid(const struct xlate_ctx
*ctx
,
1900 uint16_t vid
, struct xbundle
*in_xbundle
)
1902 /* Allow any VID on the OFPP_NONE port. */
1903 if (in_xbundle
== &ofpp_none_bundle
) {
1907 switch (in_xbundle
->vlan_mode
) {
1908 case PORT_VLAN_ACCESS
:
1910 xlate_report_error(ctx
, "dropping VLAN %"PRIu16
" tagged "
1911 "packet received on port %s configured as VLAN "
1912 "%"PRIu16
" access port", vid
, in_xbundle
->name
,
1918 case PORT_VLAN_NATIVE_UNTAGGED
:
1919 case PORT_VLAN_NATIVE_TAGGED
:
1921 /* Port must always carry its native VLAN. */
1925 case PORT_VLAN_TRUNK
:
1926 if (!xbundle_includes_vlan(in_xbundle
, vid
)) {
1927 xlate_report_error(ctx
, "dropping VLAN %"PRIu16
" packet "
1928 "received on port %s not configured for "
1929 "trunking VLAN %"PRIu16
,
1930 vid
, in_xbundle
->name
, vid
);
1941 /* Given 'vlan', the VLAN that a packet belongs to, and
1942 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1943 * that should be included in the 802.1Q header. (If the return value is 0,
1944 * then the 802.1Q header should only be included in the packet if there is a
1947 * Both 'vlan' and the return value are in the range 0...4095. */
1949 output_vlan_to_vid(const struct xbundle
*out_xbundle
, uint16_t vlan
)
1951 switch (out_xbundle
->vlan_mode
) {
1952 case PORT_VLAN_ACCESS
:
1955 case PORT_VLAN_TRUNK
:
1956 case PORT_VLAN_NATIVE_TAGGED
:
1959 case PORT_VLAN_NATIVE_UNTAGGED
:
1960 return vlan
== out_xbundle
->vlan
? 0 : vlan
;
1968 output_normal(struct xlate_ctx
*ctx
, const struct xbundle
*out_xbundle
,
1971 ovs_be16
*flow_tci
= &ctx
->xin
->flow
.vlan_tci
;
1973 ovs_be16 tci
, old_tci
;
1974 struct xport
*xport
;
1975 struct xlate_bond_recirc xr
;
1976 bool use_recirc
= false;
1978 vid
= output_vlan_to_vid(out_xbundle
, vlan
);
1979 if (ovs_list_is_empty(&out_xbundle
->xports
)) {
1980 /* Partially configured bundle with no slaves. Drop the packet. */
1982 } else if (!out_xbundle
->bond
) {
1983 xport
= CONTAINER_OF(ovs_list_front(&out_xbundle
->xports
), struct xport
,
1986 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1987 struct flow_wildcards
*wc
= ctx
->wc
;
1988 struct ofport_dpif
*ofport
;
1990 if (ctx
->xbridge
->support
.odp
.recirc
) {
1991 use_recirc
= bond_may_recirc(
1992 out_xbundle
->bond
, &xr
.recirc_id
, &xr
.hash_basis
);
1995 /* Only TCP mode uses recirculation. */
1996 xr
.hash_alg
= OVS_HASH_ALG_L4
;
1997 bond_update_post_recirc_rules(out_xbundle
->bond
, false);
1999 /* Recirculation does not require unmasking hash fields. */
2004 ofport
= bond_choose_output_slave(out_xbundle
->bond
,
2005 &ctx
->xin
->flow
, wc
, vid
);
2006 xport
= xport_lookup(xcfg
, ofport
);
2009 /* No slaves enabled, so drop packet. */
2013 /* If use_recirc is set, the main thread will handle stats
2014 * accounting for this bond. */
2016 if (ctx
->xin
->resubmit_stats
) {
2017 bond_account(out_xbundle
->bond
, &ctx
->xin
->flow
, vid
,
2018 ctx
->xin
->resubmit_stats
->n_bytes
);
2020 if (ctx
->xin
->xcache
) {
2021 struct xc_entry
*entry
;
2024 flow
= &ctx
->xin
->flow
;
2025 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_BOND
);
2026 entry
->bond
.bond
= bond_ref(out_xbundle
->bond
);
2027 entry
->bond
.flow
= xmemdup(flow
, sizeof *flow
);
2028 entry
->bond
.vid
= vid
;
2033 old_tci
= *flow_tci
;
2035 if (tci
|| out_xbundle
->use_priority_tags
) {
2036 tci
|= *flow_tci
& htons(VLAN_PCP_MASK
);
2038 tci
|= htons(VLAN_CFI
);
2043 compose_output_action(ctx
, xport
->ofp_port
, use_recirc
? &xr
: NULL
);
2044 *flow_tci
= old_tci
;
2047 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
2048 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
2049 * indicate this; newer upstream kernels use gratuitous ARP requests. */
2051 is_gratuitous_arp(const struct flow
*flow
, struct flow_wildcards
*wc
)
2053 if (flow
->dl_type
!= htons(ETH_TYPE_ARP
)) {
2057 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
2058 if (!eth_addr_is_broadcast(flow
->dl_dst
)) {
2062 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
2063 if (flow
->nw_proto
== ARP_OP_REPLY
) {
2065 } else if (flow
->nw_proto
== ARP_OP_REQUEST
) {
2066 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
2067 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
2069 return flow
->nw_src
== flow
->nw_dst
;
2075 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
2076 * dropped. Returns true if they may be forwarded, false if they should be
2079 * 'in_port' must be the xport that corresponds to flow->in_port.
2080 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
2082 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
2083 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
2084 * checked by input_vid_is_valid().
2086 * May also add tags to '*tags', although the current implementation only does
2087 * so in one special case.
2090 is_admissible(struct xlate_ctx
*ctx
, struct xport
*in_port
,
2093 struct xbundle
*in_xbundle
= in_port
->xbundle
;
2094 const struct xbridge
*xbridge
= ctx
->xbridge
;
2095 struct flow
*flow
= &ctx
->xin
->flow
;
2097 /* Drop frames for reserved multicast addresses
2098 * only if forward_bpdu option is absent. */
2099 if (!xbridge
->forward_bpdu
&& eth_addr_is_reserved(flow
->dl_dst
)) {
2100 xlate_report(ctx
, OFT_DETAIL
,
2101 "packet has reserved destination MAC, dropping");
2105 if (in_xbundle
->bond
) {
2106 struct mac_entry
*mac
;
2108 switch (bond_check_admissibility(in_xbundle
->bond
, in_port
->ofport
,
2114 xlate_report(ctx
, OFT_DETAIL
,
2115 "bonding refused admissibility, dropping");
2118 case BV_DROP_IF_MOVED
:
2119 ovs_rwlock_rdlock(&xbridge
->ml
->rwlock
);
2120 mac
= mac_learning_lookup(xbridge
->ml
, flow
->dl_src
, vlan
);
2122 && mac_entry_get_port(xbridge
->ml
, mac
) != in_xbundle
->ofbundle
2123 && (!is_gratuitous_arp(flow
, ctx
->wc
)
2124 || mac_entry_is_grat_arp_locked(mac
))) {
2125 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
2126 xlate_report(ctx
, OFT_DETAIL
,
2127 "SLB bond thinks this packet looped back, "
2131 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
2140 update_learning_table__(const struct xbridge
*xbridge
,
2141 struct xbundle
*in_xbundle
, struct eth_addr dl_src
,
2142 int vlan
, bool is_grat_arp
)
2144 return (in_xbundle
== &ofpp_none_bundle
2145 || !mac_learning_update(xbridge
->ml
, dl_src
, vlan
,
2147 in_xbundle
->bond
!= NULL
,
2148 in_xbundle
->ofbundle
));
2152 update_learning_table(const struct xlate_ctx
*ctx
,
2153 struct xbundle
*in_xbundle
, struct eth_addr dl_src
,
2154 int vlan
, bool is_grat_arp
)
2156 if (!update_learning_table__(ctx
->xbridge
, in_xbundle
, dl_src
, vlan
,
2158 xlate_report_debug(ctx
, OFT_DETAIL
, "learned that "ETH_ADDR_FMT
" is "
2159 "on port %s in VLAN %d",
2160 ETH_ADDR_ARGS(dl_src
), in_xbundle
->name
, vlan
);
2164 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2165 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
2167 update_mcast_snooping_table4__(const struct xlate_ctx
*ctx
,
2168 const struct flow
*flow
,
2169 struct mcast_snooping
*ms
, int vlan
,
2170 struct xbundle
*in_xbundle
,
2171 const struct dp_packet
*packet
)
2172 OVS_REQ_WRLOCK(ms
->rwlock
)
2174 const struct igmp_header
*igmp
;
2177 ovs_be32 ip4
= flow
->igmp_group_ip4
;
2179 offset
= (char *) dp_packet_l4(packet
) - (char *) dp_packet_data(packet
);
2180 igmp
= dp_packet_at(packet
, offset
, IGMP_HEADER_LEN
);
2181 if (!igmp
|| csum(igmp
, dp_packet_l4_size(packet
)) != 0) {
2182 xlate_report_debug(ctx
, OFT_DETAIL
,
2183 "multicast snooping received bad IGMP "
2184 "checksum on port %s in VLAN %d",
2185 in_xbundle
->name
, vlan
);
2189 switch (ntohs(flow
->tp_src
)) {
2190 case IGMP_HOST_MEMBERSHIP_REPORT
:
2191 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
2192 if (mcast_snooping_add_group4(ms
, ip4
, vlan
, in_xbundle
->ofbundle
)) {
2193 xlate_report_debug(ctx
, OFT_DETAIL
,
2194 "multicast snooping learned that "
2195 IP_FMT
" is on port %s in VLAN %d",
2196 IP_ARGS(ip4
), in_xbundle
->name
, vlan
);
2199 case IGMP_HOST_LEAVE_MESSAGE
:
2200 if (mcast_snooping_leave_group4(ms
, ip4
, vlan
, in_xbundle
->ofbundle
)) {
2201 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping leaving "
2202 IP_FMT
" is on port %s in VLAN %d",
2203 IP_ARGS(ip4
), in_xbundle
->name
, vlan
);
2206 case IGMP_HOST_MEMBERSHIP_QUERY
:
2207 if (flow
->nw_src
&& mcast_snooping_add_mrouter(ms
, vlan
,
2208 in_xbundle
->ofbundle
)) {
2209 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping query "
2210 "from "IP_FMT
" is on port %s in VLAN %d",
2211 IP_ARGS(flow
->nw_src
), in_xbundle
->name
, vlan
);
2214 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
2215 count
= mcast_snooping_add_report(ms
, packet
, vlan
,
2216 in_xbundle
->ofbundle
);
2218 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping processed "
2219 "%d addresses on port %s in VLAN %d",
2220 count
, in_xbundle
->name
, vlan
);
2227 update_mcast_snooping_table6__(const struct xlate_ctx
*ctx
,
2228 const struct flow
*flow
,
2229 struct mcast_snooping
*ms
, int vlan
,
2230 struct xbundle
*in_xbundle
,
2231 const struct dp_packet
*packet
)
2232 OVS_REQ_WRLOCK(ms
->rwlock
)
2234 const struct mld_header
*mld
;
2238 offset
= (char *) dp_packet_l4(packet
) - (char *) dp_packet_data(packet
);
2239 mld
= dp_packet_at(packet
, offset
, MLD_HEADER_LEN
);
2242 packet_csum_upperlayer6(dp_packet_l3(packet
),
2243 mld
, IPPROTO_ICMPV6
,
2244 dp_packet_l4_size(packet
)) != 0) {
2245 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping received "
2246 "bad MLD checksum on port %s in VLAN %d",
2247 in_xbundle
->name
, vlan
);
2251 switch (ntohs(flow
->tp_src
)) {
2253 if (!ipv6_addr_equals(&flow
->ipv6_src
, &in6addr_any
)
2254 && mcast_snooping_add_mrouter(ms
, vlan
, in_xbundle
->ofbundle
)) {
2255 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping query on "
2256 "port %s in VLAN %d", in_xbundle
->name
, vlan
);
2262 count
= mcast_snooping_add_mld(ms
, packet
, vlan
, in_xbundle
->ofbundle
);
2264 xlate_report_debug(ctx
, OFT_DETAIL
, "multicast snooping processed "
2265 "%d addresses on port %s in VLAN %d",
2266 count
, in_xbundle
->name
, vlan
);
2272 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2273 * was received on 'in_xbundle' in 'vlan'. */
2275 update_mcast_snooping_table(const struct xlate_ctx
*ctx
,
2276 const struct flow
*flow
, int vlan
,
2277 struct xbundle
*in_xbundle
,
2278 const struct dp_packet
*packet
)
2280 struct mcast_snooping
*ms
= ctx
->xbridge
->ms
;
2281 struct xlate_cfg
*xcfg
;
2282 struct xbundle
*mcast_xbundle
;
2283 struct mcast_port_bundle
*fport
;
2285 /* Don't learn the OFPP_NONE port. */
2286 if (in_xbundle
== &ofpp_none_bundle
) {
2290 /* Don't learn from flood ports */
2291 mcast_xbundle
= NULL
;
2292 ovs_rwlock_wrlock(&ms
->rwlock
);
2293 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2294 LIST_FOR_EACH(fport
, node
, &ms
->fport_list
) {
2295 mcast_xbundle
= xbundle_lookup(xcfg
, fport
->port
);
2296 if (mcast_xbundle
== in_xbundle
) {
2301 if (!mcast_xbundle
|| mcast_xbundle
!= in_xbundle
) {
2302 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2303 update_mcast_snooping_table4__(ctx
, flow
, ms
, vlan
,
2304 in_xbundle
, packet
);
2306 update_mcast_snooping_table6__(ctx
, flow
, ms
, vlan
,
2307 in_xbundle
, packet
);
2310 ovs_rwlock_unlock(&ms
->rwlock
);
2313 /* send the packet to ports having the multicast group learned */
2315 xlate_normal_mcast_send_group(struct xlate_ctx
*ctx
,
2316 struct mcast_snooping
*ms OVS_UNUSED
,
2317 struct mcast_group
*grp
,
2318 struct xbundle
*in_xbundle
, uint16_t vlan
)
2319 OVS_REQ_RDLOCK(ms
->rwlock
)
2321 struct xlate_cfg
*xcfg
;
2322 struct mcast_group_bundle
*b
;
2323 struct xbundle
*mcast_xbundle
;
2325 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2326 LIST_FOR_EACH(b
, bundle_node
, &grp
->bundle_lru
) {
2327 mcast_xbundle
= xbundle_lookup(xcfg
, b
->port
);
2328 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
) {
2329 xlate_report(ctx
, OFT_DETAIL
, "forwarding to mcast group port");
2330 output_normal(ctx
, mcast_xbundle
, vlan
);
2331 } else if (!mcast_xbundle
) {
2332 xlate_report(ctx
, OFT_WARN
,
2333 "mcast group port is unknown, dropping");
2335 xlate_report(ctx
, OFT_DETAIL
,
2336 "mcast group port is input port, dropping");
2341 /* send the packet to ports connected to multicast routers */
2343 xlate_normal_mcast_send_mrouters(struct xlate_ctx
*ctx
,
2344 struct mcast_snooping
*ms
,
2345 struct xbundle
*in_xbundle
, uint16_t vlan
)
2346 OVS_REQ_RDLOCK(ms
->rwlock
)
2348 struct xlate_cfg
*xcfg
;
2349 struct mcast_mrouter_bundle
*mrouter
;
2350 struct xbundle
*mcast_xbundle
;
2352 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2353 LIST_FOR_EACH(mrouter
, mrouter_node
, &ms
->mrouter_lru
) {
2354 mcast_xbundle
= xbundle_lookup(xcfg
, mrouter
->port
);
2355 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
2356 && mrouter
->vlan
== vlan
) {
2357 xlate_report(ctx
, OFT_DETAIL
, "forwarding to mcast router port");
2358 output_normal(ctx
, mcast_xbundle
, vlan
);
2359 } else if (!mcast_xbundle
) {
2360 xlate_report(ctx
, OFT_WARN
,
2361 "mcast router port is unknown, dropping");
2362 } else if (mrouter
->vlan
!= vlan
) {
2363 xlate_report(ctx
, OFT_DETAIL
,
2364 "mcast router is on another vlan, dropping");
2366 xlate_report(ctx
, OFT_DETAIL
,
2367 "mcast router port is input port, dropping");
2372 /* send the packet to ports flagged to be flooded */
2374 xlate_normal_mcast_send_fports(struct xlate_ctx
*ctx
,
2375 struct mcast_snooping
*ms
,
2376 struct xbundle
*in_xbundle
, uint16_t vlan
)
2377 OVS_REQ_RDLOCK(ms
->rwlock
)
2379 struct xlate_cfg
*xcfg
;
2380 struct mcast_port_bundle
*fport
;
2381 struct xbundle
*mcast_xbundle
;
2383 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2384 LIST_FOR_EACH(fport
, node
, &ms
->fport_list
) {
2385 mcast_xbundle
= xbundle_lookup(xcfg
, fport
->port
);
2386 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
) {
2387 xlate_report(ctx
, OFT_DETAIL
, "forwarding to mcast flood port");
2388 output_normal(ctx
, mcast_xbundle
, vlan
);
2389 } else if (!mcast_xbundle
) {
2390 xlate_report(ctx
, OFT_WARN
,
2391 "mcast flood port is unknown, dropping");
2393 xlate_report(ctx
, OFT_DETAIL
,
2394 "mcast flood port is input port, dropping");
2399 /* forward the Reports to configured ports */
2401 xlate_normal_mcast_send_rports(struct xlate_ctx
*ctx
,
2402 struct mcast_snooping
*ms
,
2403 struct xbundle
*in_xbundle
, uint16_t vlan
)
2404 OVS_REQ_RDLOCK(ms
->rwlock
)
2406 struct xlate_cfg
*xcfg
;
2407 struct mcast_port_bundle
*rport
;
2408 struct xbundle
*mcast_xbundle
;
2410 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2411 LIST_FOR_EACH(rport
, node
, &ms
->rport_list
) {
2412 mcast_xbundle
= xbundle_lookup(xcfg
, rport
->port
);
2413 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
) {
2414 xlate_report(ctx
, OFT_DETAIL
,
2415 "forwarding report to mcast flagged port");
2416 output_normal(ctx
, mcast_xbundle
, vlan
);
2417 } else if (!mcast_xbundle
) {
2418 xlate_report(ctx
, OFT_WARN
,
2419 "mcast port is unknown, dropping the report");
2421 xlate_report(ctx
, OFT_DETAIL
,
2422 "mcast port is input port, dropping the Report");
2428 xlate_normal_flood(struct xlate_ctx
*ctx
, struct xbundle
*in_xbundle
,
2431 struct xbundle
*xbundle
;
2433 LIST_FOR_EACH (xbundle
, list_node
, &ctx
->xbridge
->xbundles
) {
2434 if (xbundle
!= in_xbundle
2435 && xbundle_includes_vlan(xbundle
, vlan
)
2436 && xbundle
->floodable
2437 && !xbundle_mirror_out(ctx
->xbridge
, xbundle
)) {
2438 output_normal(ctx
, xbundle
, vlan
);
2441 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
2445 is_ip_local_multicast(const struct flow
*flow
, struct flow_wildcards
*wc
)
2447 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2448 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
2449 return ip_is_local_multicast(flow
->nw_dst
);
2450 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
2451 memset(&wc
->masks
.ipv6_dst
, 0xff, sizeof wc
->masks
.ipv6_dst
);
2452 return ipv6_is_all_hosts(&flow
->ipv6_dst
);
2459 xlate_normal(struct xlate_ctx
*ctx
)
2461 struct flow_wildcards
*wc
= ctx
->wc
;
2462 struct flow
*flow
= &ctx
->xin
->flow
;
2463 struct xbundle
*in_xbundle
;
2464 struct xport
*in_port
;
2465 struct mac_entry
*mac
;
2470 memset(&wc
->masks
.dl_src
, 0xff, sizeof wc
->masks
.dl_src
);
2471 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
2472 wc
->masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
2474 in_xbundle
= lookup_input_bundle(ctx
, flow
->in_port
.ofp_port
, &in_port
);
2476 xlate_report(ctx
, OFT_WARN
, "no input bundle, dropping");
2480 /* Drop malformed frames. */
2481 if (flow
->dl_type
== htons(ETH_TYPE_VLAN
) &&
2482 !(flow
->vlan_tci
& htons(VLAN_CFI
))) {
2483 if (ctx
->xin
->packet
!= NULL
) {
2484 xlate_report_error(ctx
, "dropping packet with partial "
2485 "VLAN tag received on port %s",
2488 xlate_report(ctx
, OFT_WARN
, "partial VLAN tag, dropping");
2492 /* Drop frames on bundles reserved for mirroring. */
2493 if (xbundle_mirror_out(ctx
->xbridge
, in_xbundle
)) {
2494 if (ctx
->xin
->packet
!= NULL
) {
2495 xlate_report_error(ctx
, "dropping packet received on port %s, "
2496 "which is reserved exclusively for mirroring",
2499 xlate_report(ctx
, OFT_WARN
,
2500 "input port is mirror output port, dropping");
2505 vid
= vlan_tci_to_vid(flow
->vlan_tci
);
2506 if (!input_vid_is_valid(ctx
, vid
, in_xbundle
)) {
2507 xlate_report(ctx
, OFT_WARN
,
2508 "disallowed VLAN VID for this input port, dropping");
2511 vlan
= input_vid_to_vlan(in_xbundle
, vid
);
2513 /* Check other admissibility requirements. */
2514 if (in_port
&& !is_admissible(ctx
, in_port
, vlan
)) {
2518 /* Learn source MAC. */
2519 bool is_grat_arp
= is_gratuitous_arp(flow
, wc
);
2520 if (ctx
->xin
->allow_side_effects
) {
2521 update_learning_table(ctx
, in_xbundle
, flow
->dl_src
, vlan
,
2524 if (ctx
->xin
->xcache
&& in_xbundle
!= &ofpp_none_bundle
) {
2525 struct xc_entry
*entry
;
2527 /* Save just enough info to update mac learning table later. */
2528 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NORMAL
);
2529 entry
->normal
.ofproto
= ctx
->xbridge
->ofproto
;
2530 entry
->normal
.in_port
= flow
->in_port
.ofp_port
;
2531 entry
->normal
.dl_src
= flow
->dl_src
;
2532 entry
->normal
.vlan
= vlan
;
2533 entry
->normal
.is_gratuitous_arp
= is_grat_arp
;
2536 /* Determine output bundle. */
2537 if (mcast_snooping_enabled(ctx
->xbridge
->ms
)
2538 && !eth_addr_is_broadcast(flow
->dl_dst
)
2539 && eth_addr_is_multicast(flow
->dl_dst
)
2540 && is_ip_any(flow
)) {
2541 struct mcast_snooping
*ms
= ctx
->xbridge
->ms
;
2542 struct mcast_group
*grp
= NULL
;
2544 if (is_igmp(flow
, wc
)) {
2545 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
2546 if (mcast_snooping_is_membership(flow
->tp_src
) ||
2547 mcast_snooping_is_query(flow
->tp_src
)) {
2548 if (ctx
->xin
->allow_side_effects
&& ctx
->xin
->packet
) {
2549 update_mcast_snooping_table(ctx
, flow
, vlan
,
2550 in_xbundle
, ctx
->xin
->packet
);
2553 * IGMP packets need to take the slow path, in order to be
2554 * processed for mdb updates. That will prevent expires
2555 * firing off even after hosts have sent reports.
2557 ctx
->xout
->slow
|= SLOW_ACTION
;
2560 if (mcast_snooping_is_membership(flow
->tp_src
)) {
2561 ovs_rwlock_rdlock(&ms
->rwlock
);
2562 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, vlan
);
2563 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2564 * forward IGMP Membership Reports only to those ports where
2565 * multicast routers are attached. Alternatively stated: a
2566 * snooping switch should not forward IGMP Membership Reports
2567 * to ports on which only hosts are attached.
2568 * An administrative control may be provided to override this
2569 * restriction, allowing the report messages to be flooded to
2571 xlate_normal_mcast_send_rports(ctx
, ms
, in_xbundle
, vlan
);
2572 ovs_rwlock_unlock(&ms
->rwlock
);
2574 xlate_report(ctx
, OFT_DETAIL
, "multicast traffic, flooding");
2575 xlate_normal_flood(ctx
, in_xbundle
, vlan
);
2578 } else if (is_mld(flow
, wc
)) {
2579 ctx
->xout
->slow
|= SLOW_ACTION
;
2580 if (ctx
->xin
->allow_side_effects
&& ctx
->xin
->packet
) {
2581 update_mcast_snooping_table(ctx
, flow
, vlan
,
2582 in_xbundle
, ctx
->xin
->packet
);
2584 if (is_mld_report(flow
, wc
)) {
2585 ovs_rwlock_rdlock(&ms
->rwlock
);
2586 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, vlan
);
2587 xlate_normal_mcast_send_rports(ctx
, ms
, in_xbundle
, vlan
);
2588 ovs_rwlock_unlock(&ms
->rwlock
);
2590 xlate_report(ctx
, OFT_DETAIL
, "MLD query, flooding");
2591 xlate_normal_flood(ctx
, in_xbundle
, vlan
);
2594 if (is_ip_local_multicast(flow
, wc
)) {
2595 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2596 * address in the 224.0.0.x range which are not IGMP must
2597 * be forwarded on all ports */
2598 xlate_report(ctx
, OFT_DETAIL
,
2599 "RFC4541: section 2.1.2, item 2, flooding");
2600 xlate_normal_flood(ctx
, in_xbundle
, vlan
);
2605 /* forwarding to group base ports */
2606 ovs_rwlock_rdlock(&ms
->rwlock
);
2607 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2608 grp
= mcast_snooping_lookup4(ms
, flow
->nw_dst
, vlan
);
2609 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
2610 grp
= mcast_snooping_lookup(ms
, &flow
->ipv6_dst
, vlan
);
2613 xlate_normal_mcast_send_group(ctx
, ms
, grp
, in_xbundle
, vlan
);
2614 xlate_normal_mcast_send_fports(ctx
, ms
, in_xbundle
, vlan
);
2615 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, vlan
);
2617 if (mcast_snooping_flood_unreg(ms
)) {
2618 xlate_report(ctx
, OFT_DETAIL
,
2619 "unregistered multicast, flooding");
2620 xlate_normal_flood(ctx
, in_xbundle
, vlan
);
2622 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, vlan
);
2623 xlate_normal_mcast_send_fports(ctx
, ms
, in_xbundle
, vlan
);
2626 ovs_rwlock_unlock(&ms
->rwlock
);
2628 ovs_rwlock_rdlock(&ctx
->xbridge
->ml
->rwlock
);
2629 mac
= mac_learning_lookup(ctx
->xbridge
->ml
, flow
->dl_dst
, vlan
);
2630 mac_port
= mac
? mac_entry_get_port(ctx
->xbridge
->ml
, mac
) : NULL
;
2631 ovs_rwlock_unlock(&ctx
->xbridge
->ml
->rwlock
);
2634 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2635 struct xbundle
*mac_xbundle
= xbundle_lookup(xcfg
, mac_port
);
2636 if (mac_xbundle
&& mac_xbundle
!= in_xbundle
) {
2637 xlate_report(ctx
, OFT_DETAIL
, "forwarding to learned port");
2638 output_normal(ctx
, mac_xbundle
, vlan
);
2639 } else if (!mac_xbundle
) {
2640 xlate_report(ctx
, OFT_WARN
,
2641 "learned port is unknown, dropping");
2643 xlate_report(ctx
, OFT_DETAIL
,
2644 "learned port is input port, dropping");
2647 xlate_report(ctx
, OFT_DETAIL
,
2648 "no learned MAC for destination, flooding");
2649 xlate_normal_flood(ctx
, in_xbundle
, vlan
);
2654 /* Appends a "sample" action for sFlow or IPFIX to 'ctx->odp_actions'. The
2655 * 'probability' is the number of packets out of UINT32_MAX to sample. The
2656 * 'cookie' (of length 'cookie_size' bytes) is passed back in the callback for
2657 * each sampled packet. 'tunnel_out_port', if not ODPP_NONE, is added as the
2658 * OVS_USERSPACE_ATTR_EGRESS_TUN_PORT attribute. If 'include_actions', an
2659 * OVS_USERSPACE_ATTR_ACTIONS attribute is added. If 'emit_set_tunnel',
2660 * sample(sampling_port=1) would translate into datapath sample action
2661 * set(tunnel(...)), sample(...) and it is used for sampling egress tunnel
2665 compose_sample_action(struct xlate_ctx
*ctx
,
2666 const uint32_t probability
,
2667 const union user_action_cookie
*cookie
,
2668 const size_t cookie_size
,
2669 const odp_port_t tunnel_out_port
,
2670 bool include_actions
)
2672 if (probability
== 0) {
2673 /* No need to generate sampling or the inner action. */
2677 /* No need to generate sample action for 100% sampling rate. */
2678 bool is_sample
= probability
< UINT32_MAX
;
2679 size_t sample_offset
, actions_offset
;
2681 sample_offset
= nl_msg_start_nested(ctx
->odp_actions
,
2682 OVS_ACTION_ATTR_SAMPLE
);
2683 nl_msg_put_u32(ctx
->odp_actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
2685 actions_offset
= nl_msg_start_nested(ctx
->odp_actions
,
2686 OVS_SAMPLE_ATTR_ACTIONS
);
2689 odp_port_t odp_port
= ofp_port_to_odp_port(
2690 ctx
->xbridge
, ctx
->xin
->flow
.in_port
.ofp_port
);
2691 uint32_t pid
= dpif_port_get_pid(ctx
->xbridge
->dpif
, odp_port
,
2692 flow_hash_5tuple(&ctx
->xin
->flow
, 0));
2693 int cookie_offset
= odp_put_userspace_action(pid
, cookie
, cookie_size
,
2699 nl_msg_end_nested(ctx
->odp_actions
, actions_offset
);
2700 nl_msg_end_nested(ctx
->odp_actions
, sample_offset
);
2703 return cookie_offset
;
2706 /* If sFLow is not enabled, returns 0 without doing anything.
2708 * If sFlow is enabled, appends a template "sample" action to the ODP actions
2709 * in 'ctx'. This action is a template because some of the information needed
2710 * to fill it out is not available until flow translation is complete. In this
2711 * case, this functions returns an offset, which is always nonzero, to pass
2712 * later to fix_sflow_action() to fill in the rest of the template. */
2714 compose_sflow_action(struct xlate_ctx
*ctx
)
2716 struct dpif_sflow
*sflow
= ctx
->xbridge
->sflow
;
2717 if (!sflow
|| ctx
->xin
->flow
.in_port
.ofp_port
== OFPP_NONE
) {
2721 union user_action_cookie cookie
= { .type
= USER_ACTION_COOKIE_SFLOW
};
2722 return compose_sample_action(ctx
, dpif_sflow_get_probability(sflow
),
2723 &cookie
, sizeof cookie
.sflow
, ODPP_NONE
,
2727 /* If flow IPFIX is enabled, make sure IPFIX flow sample action
2728 * at egress point of tunnel port is just in front of corresponding
2729 * output action. If bridge IPFIX is enabled, this appends an IPFIX
2730 * sample action to 'ctx->odp_actions'. */
2732 compose_ipfix_action(struct xlate_ctx
*ctx
, odp_port_t output_odp_port
)
2734 struct dpif_ipfix
*ipfix
= ctx
->xbridge
->ipfix
;
2735 odp_port_t tunnel_out_port
= ODPP_NONE
;
2737 if (!ipfix
|| ctx
->xin
->flow
.in_port
.ofp_port
== OFPP_NONE
) {
2741 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2743 if (output_odp_port
== ODPP_NONE
&&
2744 !dpif_ipfix_get_bridge_exporter_input_sampling(ipfix
)) {
2748 /* For output case, output_odp_port is valid. */
2749 if (output_odp_port
!= ODPP_NONE
) {
2750 if (!dpif_ipfix_get_bridge_exporter_output_sampling(ipfix
)) {
2753 /* If tunnel sampling is enabled, put an additional option attribute:
2754 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2756 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(ipfix
) &&
2757 dpif_ipfix_get_tunnel_port(ipfix
, output_odp_port
) ) {
2758 tunnel_out_port
= output_odp_port
;
2762 union user_action_cookie cookie
= {
2764 .type
= USER_ACTION_COOKIE_IPFIX
,
2765 .output_odp_port
= output_odp_port
,
2768 compose_sample_action(ctx
,
2769 dpif_ipfix_get_bridge_exporter_probability(ipfix
),
2770 &cookie
, sizeof cookie
.ipfix
, tunnel_out_port
,
2774 /* Fix "sample" action according to data collected while composing ODP actions,
2775 * as described in compose_sflow_action().
2777 * 'user_cookie_offset' must be the offset returned by add_sflow_action(). */
2779 fix_sflow_action(struct xlate_ctx
*ctx
, unsigned int user_cookie_offset
)
2781 const struct flow
*base
= &ctx
->base_flow
;
2782 union user_action_cookie
*cookie
;
2784 cookie
= ofpbuf_at(ctx
->odp_actions
, user_cookie_offset
,
2785 sizeof cookie
->sflow
);
2786 ovs_assert(cookie
->type
== USER_ACTION_COOKIE_SFLOW
);
2788 cookie
->type
= USER_ACTION_COOKIE_SFLOW
;
2789 cookie
->sflow
.vlan_tci
= base
->vlan_tci
;
2791 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2792 * port information") for the interpretation of cookie->output. */
2793 switch (ctx
->sflow_n_outputs
) {
2795 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2796 cookie
->sflow
.output
= 0x40000000 | 256;
2800 cookie
->sflow
.output
= dpif_sflow_odp_port_to_ifindex(
2801 ctx
->xbridge
->sflow
, ctx
->sflow_odp_port
);
2802 if (cookie
->sflow
.output
) {
2807 /* 0x80000000 means "multiple output ports. */
2808 cookie
->sflow
.output
= 0x80000000 | ctx
->sflow_n_outputs
;
2814 process_special(struct xlate_ctx
*ctx
, const struct xport
*xport
)
2816 const struct flow
*flow
= &ctx
->xin
->flow
;
2817 struct flow_wildcards
*wc
= ctx
->wc
;
2818 const struct xbridge
*xbridge
= ctx
->xbridge
;
2819 const struct dp_packet
*packet
= ctx
->xin
->packet
;
2820 enum slow_path_reason slow
;
2824 } else if (xport
->cfm
&& cfm_should_process_flow(xport
->cfm
, flow
, wc
)) {
2826 cfm_process_heartbeat(xport
->cfm
, packet
);
2829 } else if (xport
->bfd
&& bfd_should_process_flow(xport
->bfd
, flow
, wc
)) {
2831 bfd_process_packet(xport
->bfd
, flow
, packet
);
2832 /* If POLL received, immediately sends FINAL back. */
2833 if (bfd_should_send_packet(xport
->bfd
)) {
2834 ofproto_dpif_monitor_port_send_soon(xport
->ofport
);
2838 } else if (xport
->xbundle
&& xport
->xbundle
->lacp
2839 && flow
->dl_type
== htons(ETH_TYPE_LACP
)) {
2841 lacp_process_packet(xport
->xbundle
->lacp
, xport
->ofport
, packet
);
2844 } else if ((xbridge
->stp
|| xbridge
->rstp
) &&
2845 stp_should_process_flow(flow
, wc
)) {
2848 ? stp_process_packet(xport
, packet
)
2849 : rstp_process_packet(xport
, packet
);
2852 } else if (xport
->lldp
&& lldp_should_process_flow(xport
->lldp
, flow
)) {
2854 lldp_process_packet(xport
->lldp
, packet
);
2862 ctx
->xout
->slow
|= slow
;
2870 tnl_route_lookup_flow(const struct flow
*oflow
,
2871 struct in6_addr
*ip
, struct in6_addr
*src
,
2872 struct xport
**out_port
)
2874 char out_dev
[IFNAMSIZ
];
2875 struct xbridge
*xbridge
;
2876 struct xlate_cfg
*xcfg
;
2878 struct in6_addr dst
;
2880 dst
= flow_tnl_dst(&oflow
->tunnel
);
2881 if (!ovs_router_lookup(&dst
, out_dev
, src
, &gw
)) {
2885 if (ipv6_addr_is_set(&gw
) &&
2886 (!IN6_IS_ADDR_V4MAPPED(&gw
) || in6_addr_get_mapped_ipv4(&gw
))) {
2892 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2895 HMAP_FOR_EACH (xbridge
, hmap_node
, &xcfg
->xbridges
) {
2896 if (!strncmp(xbridge
->name
, out_dev
, IFNAMSIZ
)) {
2899 HMAP_FOR_EACH (port
, ofp_node
, &xbridge
->xports
) {
2900 if (!strncmp(netdev_get_name(port
->netdev
), out_dev
, IFNAMSIZ
)) {
2911 compose_table_xlate(struct xlate_ctx
*ctx
, const struct xport
*out_dev
,
2912 struct dp_packet
*packet
)
2914 struct xbridge
*xbridge
= out_dev
->xbridge
;
2915 struct ofpact_output output
;
2918 ofpact_init(&output
.ofpact
, OFPACT_OUTPUT
, sizeof output
);
2919 flow_extract(packet
, &flow
);
2920 flow
.in_port
.ofp_port
= out_dev
->ofp_port
;
2921 output
.port
= OFPP_TABLE
;
2924 return ofproto_dpif_execute_actions__(xbridge
->ofproto
,
2925 ctx
->xin
->tables_version
, &flow
,
2926 NULL
, &output
.ofpact
, sizeof output
,
2927 ctx
->depth
, ctx
->resubmits
, packet
);
2931 tnl_send_nd_request(struct xlate_ctx
*ctx
, const struct xport
*out_dev
,
2932 const struct eth_addr eth_src
,
2933 struct in6_addr
* ipv6_src
, struct in6_addr
* ipv6_dst
)
2935 struct dp_packet packet
;
2937 dp_packet_init(&packet
, 0);
2938 compose_nd_ns(&packet
, eth_src
, ipv6_src
, ipv6_dst
);
2939 compose_table_xlate(ctx
, out_dev
, &packet
);
2940 dp_packet_uninit(&packet
);
2944 tnl_send_arp_request(struct xlate_ctx
*ctx
, const struct xport
*out_dev
,
2945 const struct eth_addr eth_src
,
2946 ovs_be32 ip_src
, ovs_be32 ip_dst
)
2948 struct dp_packet packet
;
2950 dp_packet_init(&packet
, 0);
2951 compose_arp(&packet
, ARP_OP_REQUEST
,
2952 eth_src
, eth_addr_zero
, true, ip_src
, ip_dst
);
2954 compose_table_xlate(ctx
, out_dev
, &packet
);
2955 dp_packet_uninit(&packet
);
2959 build_tunnel_send(struct xlate_ctx
*ctx
, const struct xport
*xport
,
2960 const struct flow
*flow
, odp_port_t tunnel_odp_port
)
2962 struct netdev_tnl_build_header_params tnl_params
;
2963 struct ovs_action_push_tnl tnl_push_data
;
2964 struct xport
*out_dev
= NULL
;
2965 ovs_be32 s_ip
= 0, d_ip
= 0;
2966 struct in6_addr s_ip6
= in6addr_any
;
2967 struct in6_addr d_ip6
= in6addr_any
;
2968 struct eth_addr smac
;
2969 struct eth_addr dmac
;
2971 char buf_sip6
[INET6_ADDRSTRLEN
];
2972 char buf_dip6
[INET6_ADDRSTRLEN
];
2974 err
= tnl_route_lookup_flow(flow
, &d_ip6
, &s_ip6
, &out_dev
);
2976 xlate_report(ctx
, OFT_WARN
, "native tunnel routing failed");
2980 xlate_report(ctx
, OFT_DETAIL
, "tunneling to %s via %s",
2981 ipv6_string_mapped(buf_dip6
, &d_ip6
),
2982 netdev_get_name(out_dev
->netdev
));
2984 /* Use mac addr of bridge port of the peer. */
2985 err
= netdev_get_etheraddr(out_dev
->netdev
, &smac
);
2987 xlate_report(ctx
, OFT_WARN
,
2988 "tunnel output device lacks Ethernet address");
2992 d_ip
= in6_addr_get_mapped_ipv4(&d_ip6
);
2994 s_ip
= in6_addr_get_mapped_ipv4(&s_ip6
);
2997 err
= tnl_neigh_lookup(out_dev
->xbridge
->name
, &d_ip6
, &dmac
);
2999 xlate_report(ctx
, OFT_DETAIL
,
3000 "neighbor cache miss for %s on bridge %s, "
3001 "sending %s request",
3002 buf_dip6
, out_dev
->xbridge
->name
, d_ip
? "ARP" : "ND");
3004 tnl_send_arp_request(ctx
, out_dev
, smac
, s_ip
, d_ip
);
3006 tnl_send_nd_request(ctx
, out_dev
, smac
, &s_ip6
, &d_ip6
);
3011 if (ctx
->xin
->xcache
) {
3012 struct xc_entry
*entry
;
3014 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_TNL_NEIGH
);
3015 ovs_strlcpy(entry
->tnl_neigh_cache
.br_name
, out_dev
->xbridge
->name
,
3016 sizeof entry
->tnl_neigh_cache
.br_name
);
3017 entry
->tnl_neigh_cache
.d_ipv6
= d_ip6
;
3020 xlate_report(ctx
, OFT_DETAIL
, "tunneling from "ETH_ADDR_FMT
" %s"
3021 " to "ETH_ADDR_FMT
" %s",
3022 ETH_ADDR_ARGS(smac
), ipv6_string_mapped(buf_sip6
, &s_ip6
),
3023 ETH_ADDR_ARGS(dmac
), buf_dip6
);
3025 netdev_init_tnl_build_header_params(&tnl_params
, flow
, &s_ip6
, dmac
, smac
);
3026 err
= tnl_port_build_header(xport
->ofport
, &tnl_push_data
, &tnl_params
);
3030 tnl_push_data
.tnl_port
= odp_to_u32(tunnel_odp_port
);
3031 tnl_push_data
.out_port
= odp_to_u32(out_dev
->odp_port
);
3032 odp_put_tnl_push_action(ctx
->odp_actions
, &tnl_push_data
);
3037 xlate_commit_actions(struct xlate_ctx
*ctx
)
3039 bool use_masked
= ctx
->xbridge
->support
.masked_set_action
;
3041 ctx
->xout
->slow
|= commit_odp_actions(&ctx
->xin
->flow
, &ctx
->base_flow
,
3042 ctx
->odp_actions
, ctx
->wc
,
3047 clear_conntrack(struct xlate_ctx
*ctx
)
3049 ctx
->conntracked
= false;
3051 struct flow
*flow
= &ctx
->xin
->flow
;
3055 flow
->ct_label
= OVS_U128_ZERO
;
3059 xlate_flow_is_protected(const struct xlate_ctx
*ctx
, const struct flow
*flow
, const struct xport
*xport_out
)
3061 const struct xport
*xport_in
;
3067 xport_in
= get_ofp_port(ctx
->xbridge
, flow
->in_port
.ofp_port
);
3069 return (xport_in
&& xport_in
->xbundle
&& xport_out
->xbundle
&&
3070 xport_in
->xbundle
->protected && xport_out
->xbundle
->protected);
3074 compose_output_action__(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
,
3075 const struct xlate_bond_recirc
*xr
, bool check_stp
)
3077 const struct xport
*xport
= get_ofp_port(ctx
->xbridge
, ofp_port
);
3078 struct flow_wildcards
*wc
= ctx
->wc
;
3079 struct flow
*flow
= &ctx
->xin
->flow
;
3080 struct flow_tnl flow_tnl
;
3081 ovs_be16 flow_vlan_tci
;
3082 uint32_t flow_pkt_mark
;
3083 uint8_t flow_nw_tos
;
3084 odp_port_t out_port
, odp_port
;
3085 bool tnl_push_pop_send
= false;
3088 /* If 'struct flow' gets additional metadata, we'll need to zero it out
3089 * before traversing a patch port. */
3090 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 36);
3091 memset(&flow_tnl
, 0, sizeof flow_tnl
);
3094 xlate_report(ctx
, OFT_WARN
, "Nonexistent output port");
3096 } else if (xport
->config
& OFPUTIL_PC_NO_FWD
) {
3097 xlate_report(ctx
, OFT_DETAIL
, "OFPPC_NO_FWD set, skipping output");
3099 } else if (ctx
->mirror_snaplen
!= 0 && xport
->odp_port
== ODPP_NONE
) {
3100 xlate_report(ctx
, OFT_WARN
,
3101 "Mirror truncate to ODPP_NONE, skipping output");
3103 } else if (xlate_flow_is_protected(ctx
, flow
, xport
)) {
3104 xlate_report(ctx
, OFT_WARN
,
3105 "Flow is between protected ports, skipping output.");
3107 } else if (check_stp
) {
3108 if (is_stp(&ctx
->base_flow
)) {
3109 if (!xport_stp_should_forward_bpdu(xport
) &&
3110 !xport_rstp_should_manage_bpdu(xport
)) {
3111 if (ctx
->xbridge
->stp
!= NULL
) {
3112 xlate_report(ctx
, OFT_WARN
,
3113 "STP not in listening state, "
3114 "skipping bpdu output");
3115 } else if (ctx
->xbridge
->rstp
!= NULL
) {
3116 xlate_report(ctx
, OFT_WARN
,
3117 "RSTP not managing BPDU in this state, "
3118 "skipping bpdu output");
3122 } else if (!xport_stp_forward_state(xport
) ||
3123 !xport_rstp_forward_state(xport
)) {
3124 if (ctx
->xbridge
->stp
!= NULL
) {
3125 xlate_report(ctx
, OFT_WARN
,
3126 "STP not in forwarding state, skipping output");
3127 } else if (ctx
->xbridge
->rstp
!= NULL
) {
3128 xlate_report(ctx
, OFT_WARN
,
3129 "RSTP not in forwarding state, skipping output");
3136 const struct xport
*peer
= xport
->peer
;
3137 struct flow old_flow
= ctx
->xin
->flow
;
3138 struct flow_tnl old_flow_tnl_wc
= ctx
->wc
->masks
.tunnel
;
3139 bool old_conntrack
= ctx
->conntracked
;
3140 bool old_was_mpls
= ctx
->was_mpls
;
3141 ovs_version_t old_version
= ctx
->xin
->tables_version
;
3142 struct ofpbuf old_stack
= ctx
->stack
;
3143 uint8_t new_stack
[1024];
3144 struct ofpbuf old_action_set
= ctx
->action_set
;
3145 struct ovs_list
*old_trace
= ctx
->xin
->trace
;
3146 uint64_t actset_stub
[1024 / 8];
3148 ofpbuf_use_stub(&ctx
->stack
, new_stack
, sizeof new_stack
);
3149 ofpbuf_use_stub(&ctx
->action_set
, actset_stub
, sizeof actset_stub
);
3150 flow
->in_port
.ofp_port
= peer
->ofp_port
;
3151 flow
->metadata
= htonll(0);
3152 memset(&flow
->tunnel
, 0, sizeof flow
->tunnel
);
3153 flow
->tunnel
.metadata
.tab
= ofproto_get_tun_tab(
3154 &peer
->xbridge
->ofproto
->up
);
3155 ctx
->wc
->masks
.tunnel
.metadata
.tab
= flow
->tunnel
.metadata
.tab
;
3156 memset(flow
->regs
, 0, sizeof flow
->regs
);
3157 flow
->actset_output
= OFPP_UNSET
;
3158 clear_conntrack(ctx
);
3159 ctx
->xin
->trace
= xlate_report(ctx
, OFT_BRIDGE
,
3160 "bridge(\"%s\")", peer
->xbridge
->name
);
3162 /* When the patch port points to a different bridge, then the mirrors
3163 * for that bridge clearly apply independently to the packet, so we
3164 * reset the mirror bitmap to zero and then restore it after the packet
3167 * When the patch port points to the same bridge, this is more of a
3168 * design decision: can mirrors be re-applied to the packet after it
3169 * re-enters the bridge, or should we treat that as doubly mirroring a
3170 * single packet? The former may be cleaner, since it respects the
3171 * model in which a patch port is like a physical cable plugged from
3172 * one switch port to another, but the latter may be less surprising to
3173 * users. We take the latter choice, for now at least. (To use the
3174 * former choice, hard-code 'independent_mirrors' to "true".) */
3175 mirror_mask_t old_mirrors
= ctx
->mirrors
;
3176 bool independent_mirrors
= peer
->xbridge
!= ctx
->xbridge
;
3177 if (independent_mirrors
) {
3180 ctx
->xbridge
= peer
->xbridge
;
3182 /* The bridge is now known so obtain its table version. */
3183 ctx
->xin
->tables_version
3184 = ofproto_dpif_get_tables_version(ctx
->xbridge
->ofproto
);
3186 if (!process_special(ctx
, peer
) && may_receive(peer
, ctx
)) {
3187 if (xport_stp_forward_state(peer
) && xport_rstp_forward_state(peer
)) {
3188 xlate_table_action(ctx
, flow
->in_port
.ofp_port
, 0, true, true);
3189 if (!ctx
->freezing
) {
3190 xlate_action_set(ctx
);
3192 if (ctx
->freezing
) {
3193 finish_freezing(ctx
);
3196 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
3197 * the learning action look at the packet, then drop it. */
3198 struct flow old_base_flow
= ctx
->base_flow
;
3199 size_t old_size
= ctx
->odp_actions
->size
;
3200 mirror_mask_t old_mirrors2
= ctx
->mirrors
;
3202 xlate_table_action(ctx
, flow
->in_port
.ofp_port
, 0, true, true);
3203 ctx
->mirrors
= old_mirrors2
;
3204 ctx
->base_flow
= old_base_flow
;
3205 ctx
->odp_actions
->size
= old_size
;
3207 /* Undo changes that may have been done for freezing. */
3208 ctx_cancel_freeze(ctx
);
3212 ctx
->xin
->trace
= old_trace
;
3213 if (independent_mirrors
) {
3214 ctx
->mirrors
= old_mirrors
;
3216 ctx
->xin
->flow
= old_flow
;
3217 ctx
->xbridge
= xport
->xbridge
;
3218 ofpbuf_uninit(&ctx
->action_set
);
3219 ctx
->action_set
= old_action_set
;
3220 ofpbuf_uninit(&ctx
->stack
);
3221 ctx
->stack
= old_stack
;
3223 /* Restore calling bridge's lookup version. */
3224 ctx
->xin
->tables_version
= old_version
;
3226 /* Since this packet came in on a patch port (from the perspective of
3227 * the peer bridge), it cannot have useful tunnel information. As a
3228 * result, any wildcards generated on that tunnel also cannot be valid.
3229 * The tunnel wildcards must be restored to their original version since
3230 * the peer bridge uses a separate tunnel metadata table and therefore
3231 * any generated wildcards will be garbage in the context of our
3232 * metadata table. */
3233 ctx
->wc
->masks
.tunnel
= old_flow_tnl_wc
;
3235 /* The peer bridge popping MPLS should have no effect on the original
3237 ctx
->was_mpls
= old_was_mpls
;
3239 /* The peer bridge's conntrack execution should have no effect on the
3240 * original bridge. */
3241 ctx
->conntracked
= old_conntrack
;
3243 /* The fact that the peer bridge exits (for any reason) does not mean
3244 * that the original bridge should exit. Specifically, if the peer
3245 * bridge freezes translation, the original bridge must continue
3246 * processing with the original, not the frozen packet! */
3249 /* Peer bridge errors do not propagate back. */
3250 ctx
->error
= XLATE_OK
;
3252 if (ctx
->xin
->resubmit_stats
) {
3253 netdev_vport_inc_tx(xport
->netdev
, ctx
->xin
->resubmit_stats
);
3254 netdev_vport_inc_rx(peer
->netdev
, ctx
->xin
->resubmit_stats
);
3256 bfd_account_rx(peer
->bfd
, ctx
->xin
->resubmit_stats
);
3259 if (ctx
->xin
->xcache
) {
3260 struct xc_entry
*entry
;
3262 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NETDEV
);
3263 entry
->dev
.tx
= netdev_ref(xport
->netdev
);
3264 entry
->dev
.rx
= netdev_ref(peer
->netdev
);
3265 entry
->dev
.bfd
= bfd_ref(peer
->bfd
);
3270 flow_vlan_tci
= flow
->vlan_tci
;
3271 flow_pkt_mark
= flow
->pkt_mark
;
3272 flow_nw_tos
= flow
->nw_tos
;
3274 if (count_skb_priorities(xport
)) {
3275 memset(&wc
->masks
.skb_priority
, 0xff, sizeof wc
->masks
.skb_priority
);
3276 if (dscp_from_skb_priority(xport
, flow
->skb_priority
, &dscp
)) {
3277 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
3278 flow
->nw_tos
&= ~IP_DSCP_MASK
;
3279 flow
->nw_tos
|= dscp
;
3283 if (xport
->is_tunnel
) {
3284 struct in6_addr dst
;
3285 /* Save tunnel metadata so that changes made due to
3286 * the Logical (tunnel) Port are not visible for any further
3287 * matches, while explicit set actions on tunnel metadata are.
3289 flow_tnl
= flow
->tunnel
;
3290 odp_port
= tnl_port_send(xport
->ofport
, flow
, ctx
->wc
);
3291 if (odp_port
== ODPP_NONE
) {
3292 xlate_report(ctx
, OFT_WARN
, "Tunneling decided against output");
3293 goto out
; /* restore flow_nw_tos */
3295 dst
= flow_tnl_dst(&flow
->tunnel
);
3296 if (ipv6_addr_equals(&dst
, &ctx
->orig_tunnel_ipv6_dst
)) {
3297 xlate_report(ctx
, OFT_WARN
, "Not tunneling to our own address");
3298 goto out
; /* restore flow_nw_tos */
3300 if (ctx
->xin
->resubmit_stats
) {
3301 netdev_vport_inc_tx(xport
->netdev
, ctx
->xin
->resubmit_stats
);
3303 if (ctx
->xin
->xcache
) {
3304 struct xc_entry
*entry
;
3306 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NETDEV
);
3307 entry
->dev
.tx
= netdev_ref(xport
->netdev
);
3309 out_port
= odp_port
;
3310 if (ovs_native_tunneling_is_on(ctx
->xbridge
->ofproto
)) {
3311 xlate_report(ctx
, OFT_DETAIL
, "output to native tunnel");
3312 tnl_push_pop_send
= true;
3314 xlate_report(ctx
, OFT_DETAIL
, "output to kernel tunnel");
3315 commit_odp_tunnel_action(flow
, &ctx
->base_flow
, ctx
->odp_actions
);
3316 flow
->tunnel
= flow_tnl
; /* Restore tunnel metadata */
3319 odp_port
= xport
->odp_port
;
3320 out_port
= odp_port
;
3323 if (out_port
!= ODPP_NONE
) {
3324 xlate_commit_actions(ctx
);
3327 struct ovs_action_hash
*act_hash
;
3330 act_hash
= nl_msg_put_unspec_uninit(ctx
->odp_actions
,
3331 OVS_ACTION_ATTR_HASH
,
3333 act_hash
->hash_alg
= xr
->hash_alg
;
3334 act_hash
->hash_basis
= xr
->hash_basis
;
3336 /* Recirc action. */
3337 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_RECIRC
,
3341 if (tnl_push_pop_send
) {
3342 build_tunnel_send(ctx
, xport
, flow
, odp_port
);
3343 flow
->tunnel
= flow_tnl
; /* Restore tunnel metadata */
3345 odp_port_t odp_tnl_port
= ODPP_NONE
;
3347 /* XXX: Write better Filter for tunnel port. We can use inport
3348 * int tunnel-port flow to avoid these checks completely. */
3349 if (ofp_port
== OFPP_LOCAL
&&
3350 ovs_native_tunneling_is_on(ctx
->xbridge
->ofproto
)) {
3352 odp_tnl_port
= tnl_port_map_lookup(flow
, wc
);
3355 if (odp_tnl_port
!= ODPP_NONE
) {
3356 nl_msg_put_odp_port(ctx
->odp_actions
,
3357 OVS_ACTION_ATTR_TUNNEL_POP
,
3360 /* Tunnel push-pop action is not compatible with
3362 compose_ipfix_action(ctx
, out_port
);
3364 /* Handle truncation of the mirrored packet. */
3365 if (ctx
->mirror_snaplen
> 0 &&
3366 ctx
->mirror_snaplen
< UINT16_MAX
) {
3367 struct ovs_action_trunc
*trunc
;
3369 trunc
= nl_msg_put_unspec_uninit(ctx
->odp_actions
,
3370 OVS_ACTION_ATTR_TRUNC
,
3372 trunc
->max_len
= ctx
->mirror_snaplen
;
3373 if (!ctx
->xbridge
->support
.trunc
) {
3374 ctx
->xout
->slow
|= SLOW_ACTION
;
3378 nl_msg_put_odp_port(ctx
->odp_actions
,
3379 OVS_ACTION_ATTR_OUTPUT
,
3385 ctx
->sflow_odp_port
= odp_port
;
3386 ctx
->sflow_n_outputs
++;
3387 ctx
->nf_output_iface
= ofp_port
;
3390 if (mbridge_has_mirrors(ctx
->xbridge
->mbridge
) && xport
->xbundle
) {
3391 mirror_packet(ctx
, xport
->xbundle
,
3392 xbundle_mirror_dst(xport
->xbundle
->xbridge
,
3398 flow
->vlan_tci
= flow_vlan_tci
;
3399 flow
->pkt_mark
= flow_pkt_mark
;
3400 flow
->nw_tos
= flow_nw_tos
;
3404 compose_output_action(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
,
3405 const struct xlate_bond_recirc
*xr
)
3407 compose_output_action__(ctx
, ofp_port
, xr
, true);
3411 xlate_recursively(struct xlate_ctx
*ctx
, struct rule_dpif
*rule
, bool deepens
)
3413 struct rule_dpif
*old_rule
= ctx
->rule
;
3414 ovs_be64 old_cookie
= ctx
->rule_cookie
;
3415 const struct rule_actions
*actions
;
3417 if (ctx
->xin
->resubmit_stats
) {
3418 rule_dpif_credit_stats(rule
, ctx
->xin
->resubmit_stats
);
3423 ctx
->depth
+= deepens
;
3425 ctx
->rule_cookie
= rule
->up
.flow_cookie
;
3426 actions
= rule_get_actions(&rule
->up
);
3427 do_xlate_actions(actions
->ofpacts
, actions
->ofpacts_len
, ctx
);
3428 ctx
->rule_cookie
= old_cookie
;
3429 ctx
->rule
= old_rule
;
3430 ctx
->depth
-= deepens
;
3434 xlate_resubmit_resource_check(struct xlate_ctx
*ctx
)
3436 if (ctx
->depth
>= MAX_DEPTH
) {
3437 xlate_report_error(ctx
, "over max translation depth %d", MAX_DEPTH
);
3438 ctx
->error
= XLATE_RECURSION_TOO_DEEP
;
3439 } else if (ctx
->resubmits
>= MAX_RESUBMITS
) {
3440 xlate_report_error(ctx
, "over %d resubmit actions", MAX_RESUBMITS
);
3441 ctx
->error
= XLATE_TOO_MANY_RESUBMITS
;
3442 } else if (ctx
->odp_actions
->size
> UINT16_MAX
) {
3443 xlate_report_error(ctx
, "resubmits yielded over 64 kB of actions");
3444 /* NOT an error, as we'll be slow-pathing the flow in this case? */
3445 ctx
->exit
= true; /* XXX: translation still terminated! */
3446 } else if (ctx
->stack
.size
>= 65536) {
3447 xlate_report_error(ctx
, "resubmits yielded over 64 kB of stack");
3448 ctx
->error
= XLATE_STACK_TOO_DEEP
;
3457 xlate_table_action(struct xlate_ctx
*ctx
, ofp_port_t in_port
, uint8_t table_id
,
3458 bool may_packet_in
, bool honor_table_miss
)
3460 /* Check if we need to recirculate before matching in a table. */
3461 if (ctx
->was_mpls
) {
3462 ctx_trigger_freeze(ctx
);
3465 if (xlate_resubmit_resource_check(ctx
)) {
3466 uint8_t old_table_id
= ctx
->table_id
;
3467 struct rule_dpif
*rule
;
3469 ctx
->table_id
= table_id
;
3471 rule
= rule_dpif_lookup_from_table(ctx
->xbridge
->ofproto
,
3472 ctx
->xin
->tables_version
,
3473 &ctx
->xin
->flow
, ctx
->wc
,
3474 ctx
->xin
->resubmit_stats
,
3475 &ctx
->table_id
, in_port
,
3476 may_packet_in
, honor_table_miss
,
3480 /* Fill in the cache entry here instead of xlate_recursively
3481 * to make the reference counting more explicit. We take a
3482 * reference in the lookups above if we are going to cache the
3484 if (ctx
->xin
->xcache
) {
3485 struct xc_entry
*entry
;
3487 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_RULE
);
3489 ofproto_rule_ref(&rule
->up
);
3492 struct ovs_list
*old_trace
= ctx
->xin
->trace
;
3493 xlate_report_table(ctx
, rule
, table_id
);
3494 xlate_recursively(ctx
, rule
, table_id
<= old_table_id
);
3495 ctx
->xin
->trace
= old_trace
;
3498 ctx
->table_id
= old_table_id
;
3503 /* Consumes the group reference, which is only taken if xcache exists. */
3505 xlate_group_stats(struct xlate_ctx
*ctx
, struct group_dpif
*group
,
3506 struct ofputil_bucket
*bucket
)
3508 if (ctx
->xin
->resubmit_stats
) {
3509 group_dpif_credit_stats(group
, bucket
, ctx
->xin
->resubmit_stats
);
3511 if (ctx
->xin
->xcache
) {
3512 struct xc_entry
*entry
;
3514 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_GROUP
);
3515 entry
->group
.group
= group
;
3516 entry
->group
.bucket
= bucket
;
3521 xlate_group_bucket(struct xlate_ctx
*ctx
, struct ofputil_bucket
*bucket
)
3523 uint64_t action_list_stub
[1024 / 8];
3524 struct ofpbuf action_list
= OFPBUF_STUB_INITIALIZER(action_list_stub
);
3525 struct ofpbuf action_set
= ofpbuf_const_initializer(bucket
->ofpacts
,
3526 bucket
->ofpacts_len
);
3527 struct flow old_flow
= ctx
->xin
->flow
;
3528 bool old_was_mpls
= ctx
->was_mpls
;
3530 ofpacts_execute_action_set(&action_list
, &action_set
);
3532 do_xlate_actions(action_list
.data
, action_list
.size
, ctx
);
3535 ofpbuf_uninit(&action_list
);
3537 /* Check if need to freeze. */
3538 if (ctx
->freezing
) {
3539 finish_freezing(ctx
);
3542 /* Roll back flow to previous state.
3543 * This is equivalent to cloning the packet for each bucket.
3545 * As a side effect any subsequently applied actions will
3546 * also effectively be applied to a clone of the packet taken
3547 * just before applying the all or indirect group.
3549 * Note that group buckets are action sets, hence they cannot modify the
3550 * main action set. Also any stack actions are ignored when executing an
3551 * action set, so group buckets cannot change the stack either.
3552 * However, we do allow resubmit actions in group buckets, which could
3553 * break the above assumptions. It is up to the controller to not mess up
3554 * with the action_set and stack in the tables resubmitted to from
3556 ctx
->xin
->flow
= old_flow
;
3558 /* The group bucket popping MPLS should have no effect after bucket
3560 ctx
->was_mpls
= old_was_mpls
;
3562 /* The fact that the group bucket exits (for any reason) does not mean that
3563 * the translation after the group action should exit. Specifically, if
3564 * the group bucket freezes translation, the actions after the group action
3565 * must continue processing with the original, not the frozen packet! */
3570 xlate_all_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
3572 struct ofputil_bucket
*bucket
;
3573 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
3574 xlate_group_bucket(ctx
, bucket
);
3576 xlate_group_stats(ctx
, group
, NULL
);
3580 xlate_ff_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
3582 struct ofputil_bucket
*bucket
;
3584 bucket
= group_first_live_bucket(ctx
, group
, 0);
3586 xlate_group_bucket(ctx
, bucket
);
3587 xlate_group_stats(ctx
, group
, bucket
);
3588 } else if (ctx
->xin
->xcache
) {
3589 ofproto_group_unref(&group
->up
);
3594 xlate_default_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
3596 struct flow_wildcards
*wc
= ctx
->wc
;
3597 struct ofputil_bucket
*bucket
;
3600 basis
= flow_hash_symmetric_l4(&ctx
->xin
->flow
, 0);
3601 flow_mask_hash_fields(&ctx
->xin
->flow
, wc
, NX_HASH_FIELDS_SYMMETRIC_L4
);
3602 bucket
= group_best_live_bucket(ctx
, group
, basis
);
3604 xlate_group_bucket(ctx
, bucket
);
3605 xlate_group_stats(ctx
, group
, bucket
);
3606 } else if (ctx
->xin
->xcache
) {
3607 ofproto_group_unref(&group
->up
);
3612 xlate_hash_fields_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
3614 const struct field_array
*fields
= &group
->up
.props
.fields
;
3615 const uint8_t *mask_values
= fields
->values
;
3616 uint32_t basis
= hash_uint64(group
->up
.props
.selection_method_param
);
3619 BITMAP_FOR_EACH_1 (i
, MFF_N_IDS
, fields
->used
.bm
) {
3620 const struct mf_field
*mf
= mf_from_id(i
);
3622 /* Skip fields for which prerequisities are not met. */
3623 if (!mf_are_prereqs_ok(mf
, &ctx
->xin
->flow
, ctx
->wc
)) {
3624 /* Skip the mask bytes for this field. */
3625 mask_values
+= mf
->n_bytes
;
3629 union mf_value value
;
3630 union mf_value mask
;
3632 mf_get_value(mf
, &ctx
->xin
->flow
, &value
);
3633 /* Mask the value. */
3634 for (int j
= 0; j
< mf
->n_bytes
; j
++) {
3635 mask
.b
[j
] = *mask_values
++;
3636 value
.b
[j
] &= mask
.b
[j
];
3638 basis
= hash_bytes(&value
, mf
->n_bytes
, basis
);
3640 /* For tunnels, hash in whether the field is present. */
3641 if (mf_is_tun_metadata(mf
)) {
3642 basis
= hash_boolean(mf_is_set(mf
, &ctx
->xin
->flow
), basis
);
3645 mf_mask_field_masked(mf
, &mask
, ctx
->wc
);
3648 struct ofputil_bucket
*bucket
= group_best_live_bucket(ctx
, group
, basis
);
3650 xlate_group_bucket(ctx
, bucket
);
3651 xlate_group_stats(ctx
, group
, bucket
);
3652 } else if (ctx
->xin
->xcache
) {
3653 ofproto_group_unref(&group
->up
);
3658 xlate_dp_hash_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
3660 struct ofputil_bucket
*bucket
;
3662 /* dp_hash value 0 is special since it means that the dp_hash has not been
3663 * computed, as all computed dp_hash values are non-zero. Therefore
3664 * compare to zero can be used to decide if the dp_hash value is valid
3665 * without masking the dp_hash field. */
3666 if (!ctx
->xin
->flow
.dp_hash
) {
3667 uint64_t param
= group
->up
.props
.selection_method_param
;
3669 ctx_trigger_recirculate_with_hash(ctx
, param
>> 32, (uint32_t)param
);
3671 uint32_t n_buckets
= group
->up
.n_buckets
;
3673 /* Minimal mask to cover the number of buckets. */
3674 uint32_t mask
= (1 << log_2_ceil(n_buckets
)) - 1;
3675 /* Multiplier chosen to make the trivial 1 bit case to
3676 * actually distribute amongst two equal weight buckets. */
3677 uint32_t basis
= 0xc2b73583 * (ctx
->xin
->flow
.dp_hash
& mask
);
3679 ctx
->wc
->masks
.dp_hash
|= mask
;
3680 bucket
= group_best_live_bucket(ctx
, group
, basis
);
3682 xlate_group_bucket(ctx
, bucket
);
3683 xlate_group_stats(ctx
, group
, bucket
);
3690 xlate_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
3692 const char *selection_method
= group
->up
.props
.selection_method
;
3694 /* Select groups may access flow keys beyond L2 in order to
3695 * select a bucket. Recirculate as appropriate to make this possible.
3697 if (ctx
->was_mpls
) {
3698 ctx_trigger_freeze(ctx
);
3701 if (selection_method
[0] == '\0') {
3702 xlate_default_select_group(ctx
, group
);
3703 } else if (!strcasecmp("hash", selection_method
)) {
3704 xlate_hash_fields_select_group(ctx
, group
);
3705 } else if (!strcasecmp("dp_hash", selection_method
)) {
3706 xlate_dp_hash_select_group(ctx
, group
);
3708 /* Parsing of groups should ensure this never happens */
3714 xlate_group_action__(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
3716 bool was_in_group
= ctx
->in_group
;
3717 ctx
->in_group
= true;
3719 switch (group
->up
.type
) {
3721 case OFPGT11_INDIRECT
:
3722 xlate_all_group(ctx
, group
);
3724 case OFPGT11_SELECT
:
3725 xlate_select_group(ctx
, group
);
3728 xlate_ff_group(ctx
, group
);
3734 ctx
->in_group
= was_in_group
;
3738 xlate_group_action(struct xlate_ctx
*ctx
, uint32_t group_id
)
3740 if (xlate_resubmit_resource_check(ctx
)) {
3741 struct group_dpif
*group
;
3743 /* Take ref only if xcache exists. */
3744 group
= group_dpif_lookup(ctx
->xbridge
->ofproto
, group_id
,
3745 ctx
->xin
->tables_version
, ctx
->xin
->xcache
);
3747 /* XXX: Should set ctx->error ? */
3748 xlate_report(ctx
, OFT_WARN
, "output to nonexistent group %"PRIu32
,
3752 xlate_group_action__(ctx
, group
);
3759 xlate_ofpact_resubmit(struct xlate_ctx
*ctx
,
3760 const struct ofpact_resubmit
*resubmit
)
3764 bool may_packet_in
= false;
3765 bool honor_table_miss
= false;
3767 if (ctx
->rule
&& rule_dpif_is_internal(ctx
->rule
)) {
3768 /* Still allow missed packets to be sent to the controller
3769 * if resubmitting from an internal table. */
3770 may_packet_in
= true;
3771 honor_table_miss
= true;
3774 in_port
= resubmit
->in_port
;
3775 if (in_port
== OFPP_IN_PORT
) {
3776 in_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
3779 table_id
= resubmit
->table_id
;
3780 if (table_id
== 255) {
3781 table_id
= ctx
->table_id
;
3784 xlate_table_action(ctx
, in_port
, table_id
, may_packet_in
,
3789 flood_packets(struct xlate_ctx
*ctx
, bool all
)
3791 const struct xport
*xport
;
3793 HMAP_FOR_EACH (xport
, ofp_node
, &ctx
->xbridge
->xports
) {
3794 if (xport
->ofp_port
== ctx
->xin
->flow
.in_port
.ofp_port
) {
3799 compose_output_action__(ctx
, xport
->ofp_port
, NULL
, false);
3800 } else if (!(xport
->config
& OFPUTIL_PC_NO_FLOOD
)) {
3801 compose_output_action(ctx
, xport
->ofp_port
, NULL
);
3805 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
3809 execute_controller_action(struct xlate_ctx
*ctx
, int len
,
3810 enum ofp_packet_in_reason reason
,
3811 uint16_t controller_id
,
3812 const uint8_t *userdata
, size_t userdata_len
)
3814 struct dp_packet_batch batch
;
3815 struct dp_packet
*packet
;
3817 ctx
->xout
->slow
|= SLOW_CONTROLLER
;
3818 xlate_commit_actions(ctx
);
3819 if (!ctx
->xin
->packet
) {
3823 if (!ctx
->xin
->allow_side_effects
&& !ctx
->xin
->xcache
) {
3827 packet
= dp_packet_clone(ctx
->xin
->packet
);
3828 dp_packet_batch_init_packet(&batch
, packet
);
3829 odp_execute_actions(NULL
, &batch
, false,
3830 ctx
->odp_actions
->data
, ctx
->odp_actions
->size
, NULL
);
3832 /* A packet sent by an action in a table-miss rule is considered an
3833 * explicit table miss. OpenFlow before 1.3 doesn't have that concept so
3834 * it will get translated back to OFPR_ACTION for those versions. */
3835 if (reason
== OFPR_ACTION
3836 && ctx
->rule
&& rule_is_table_miss(&ctx
->rule
->up
)) {
3837 reason
= OFPR_EXPLICIT_MISS
;
3840 size_t packet_len
= dp_packet_size(packet
);
3842 struct ofproto_async_msg
*am
= xmalloc(sizeof *am
);
3843 *am
= (struct ofproto_async_msg
) {
3844 .controller_id
= controller_id
,
3845 .oam
= OAM_PACKET_IN
,
3849 .packet
= dp_packet_steal_data(packet
),
3850 .packet_len
= packet_len
,
3852 .table_id
= ctx
->table_id
,
3853 .cookie
= ctx
->rule_cookie
,
3854 .userdata
= (userdata_len
3855 ? xmemdup(userdata
, userdata_len
)
3857 .userdata_len
= userdata_len
,
3863 flow_get_metadata(&ctx
->xin
->flow
, &am
->pin
.up
.public.flow_metadata
);
3865 /* Async messages are only sent once, so if we send one now, no
3866 * xlate cache entry is created. */
3867 if (ctx
->xin
->allow_side_effects
) {
3868 ofproto_dpif_send_async_msg(ctx
->xbridge
->ofproto
, am
);
3869 } else /* xcache */ {
3870 struct xc_entry
*entry
;
3872 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_CONTROLLER
);
3873 entry
->controller
.ofproto
= ctx
->xbridge
->ofproto
;
3874 entry
->controller
.am
= am
;
3877 dp_packet_delete(packet
);
3881 emit_continuation(struct xlate_ctx
*ctx
, const struct frozen_state
*state
)
3883 if (!ctx
->xin
->allow_side_effects
&& !ctx
->xin
->xcache
) {
3887 struct ofproto_async_msg
*am
= xmalloc(sizeof *am
);
3888 *am
= (struct ofproto_async_msg
) {
3889 .controller_id
= ctx
->pause
->controller_id
,
3890 .oam
= OAM_PACKET_IN
,
3894 .userdata
= xmemdup(ctx
->pause
->userdata
,
3895 ctx
->pause
->userdata_len
),
3896 .userdata_len
= ctx
->pause
->userdata_len
,
3897 .packet
= xmemdup(dp_packet_data(ctx
->xin
->packet
),
3898 dp_packet_size(ctx
->xin
->packet
)),
3899 .packet_len
= dp_packet_size(ctx
->xin
->packet
),
3900 .reason
= ctx
->pause
->reason
,
3902 .bridge
= ctx
->xbridge
->ofproto
->uuid
,
3903 .stack
= xmemdup(state
->stack
, state
->stack_size
),
3904 .stack_size
= state
->stack_size
,
3905 .mirrors
= state
->mirrors
,
3906 .conntracked
= state
->conntracked
,
3907 .actions
= xmemdup(state
->ofpacts
, state
->ofpacts_len
),
3908 .actions_len
= state
->ofpacts_len
,
3909 .action_set
= xmemdup(state
->action_set
,
3910 state
->action_set_len
),
3911 .action_set_len
= state
->action_set_len
,
3913 .max_len
= UINT16_MAX
,
3916 flow_get_metadata(&ctx
->xin
->flow
, &am
->pin
.up
.public.flow_metadata
);
3918 /* Async messages are only sent once, so if we send one now, no
3919 * xlate cache entry is created. */
3920 if (ctx
->xin
->allow_side_effects
) {
3921 ofproto_dpif_send_async_msg(ctx
->xbridge
->ofproto
, am
);
3922 } else /* xcache */ {
3923 struct xc_entry
*entry
;
3925 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_CONTROLLER
);
3926 entry
->controller
.ofproto
= ctx
->xbridge
->ofproto
;
3927 entry
->controller
.am
= am
;
3932 finish_freezing__(struct xlate_ctx
*ctx
, uint8_t table
)
3934 ovs_assert(ctx
->freezing
);
3936 struct frozen_state state
= {
3938 .ofproto_uuid
= ctx
->xbridge
->ofproto
->uuid
,
3939 .stack
= ctx
->stack
.data
,
3940 .stack_size
= ctx
->stack
.size
,
3941 .mirrors
= ctx
->mirrors
,
3942 .conntracked
= ctx
->conntracked
,
3943 .ofpacts
= ctx
->frozen_actions
.data
,
3944 .ofpacts_len
= ctx
->frozen_actions
.size
,
3945 .action_set
= ctx
->action_set
.data
,
3946 .action_set_len
= ctx
->action_set
.size
,
3948 frozen_metadata_from_flow(&state
.metadata
, &ctx
->xin
->flow
);
3951 if (ctx
->xin
->packet
) {
3952 emit_continuation(ctx
, &state
);
3955 /* Allocate a unique recirc id for the given metadata state in the
3956 * flow. An existing id, with a new reference to the corresponding
3957 * recirculation context, will be returned if possible.
3958 * The life-cycle of this recirc id is managed by associating it
3959 * with the udpif key ('ukey') created for each new datapath flow. */
3960 uint32_t id
= recirc_alloc_id_ctx(&state
);
3962 xlate_report_error(ctx
, "Failed to allocate recirculation id");
3963 ctx
->error
= XLATE_NO_RECIRCULATION_CONTEXT
;
3966 recirc_refs_add(&ctx
->xout
->recircs
, id
);
3968 if (ctx
->recirc_update_dp_hash
) {
3969 struct ovs_action_hash
*act_hash
;
3972 act_hash
= nl_msg_put_unspec_uninit(ctx
->odp_actions
,
3973 OVS_ACTION_ATTR_HASH
,
3975 act_hash
->hash_alg
= OVS_HASH_ALG_L4
; /* Make configurable. */
3976 act_hash
->hash_basis
= 0; /* Make configurable. */
3978 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_RECIRC
, id
);
3981 /* Undo changes done by freezing. */
3982 ctx_cancel_freeze(ctx
);
3985 /* Called only when we're freezing. */
3987 finish_freezing(struct xlate_ctx
*ctx
)
3989 xlate_commit_actions(ctx
);
3990 finish_freezing__(ctx
, 0);
3993 /* Fork the pipeline here. The current packet will continue processing the
3994 * current action list. A clone of the current packet will recirculate, skip
3995 * the remainder of the current action list and asynchronously resume pipeline
3996 * processing in 'table' with the current metadata and action set. */
3998 compose_recirculate_and_fork(struct xlate_ctx
*ctx
, uint8_t table
)
4000 ctx
->freezing
= true;
4001 finish_freezing__(ctx
, table
);
4005 compose_mpls_push_action(struct xlate_ctx
*ctx
, struct ofpact_push_mpls
*mpls
)
4007 struct flow
*flow
= &ctx
->xin
->flow
;
4010 ovs_assert(eth_type_mpls(mpls
->ethertype
));
4012 n
= flow_count_mpls_labels(flow
, ctx
->wc
);
4014 xlate_commit_actions(ctx
);
4015 } else if (n
>= FLOW_MAX_MPLS_LABELS
) {
4016 if (ctx
->xin
->packet
!= NULL
) {
4017 xlate_report_error(ctx
, "dropping packet on which an MPLS push "
4018 "action can't be performed as it would have "
4019 "more MPLS LSEs than the %d supported.",
4020 FLOW_MAX_MPLS_LABELS
);
4022 ctx
->error
= XLATE_TOO_MANY_MPLS_LABELS
;
4026 /* Update flow's MPLS stack, and clear L3/4 fields to mark them invalid. */
4027 flow_push_mpls(flow
, n
, mpls
->ethertype
, ctx
->wc
, true);
4031 compose_mpls_pop_action(struct xlate_ctx
*ctx
, ovs_be16 eth_type
)
4033 struct flow
*flow
= &ctx
->xin
->flow
;
4034 int n
= flow_count_mpls_labels(flow
, ctx
->wc
);
4036 if (flow_pop_mpls(flow
, n
, eth_type
, ctx
->wc
)) {
4037 if (!eth_type_mpls(eth_type
) && ctx
->xbridge
->support
.odp
.recirc
) {
4038 ctx
->was_mpls
= true;
4040 } else if (n
>= FLOW_MAX_MPLS_LABELS
) {
4041 if (ctx
->xin
->packet
!= NULL
) {
4042 xlate_report_error(ctx
, "dropping packet on which an "
4043 "MPLS pop action can't be performed as it has "
4044 "more MPLS LSEs than the %d supported.",
4045 FLOW_MAX_MPLS_LABELS
);
4047 ctx
->error
= XLATE_TOO_MANY_MPLS_LABELS
;
4048 ofpbuf_clear(ctx
->odp_actions
);
4053 compose_dec_ttl(struct xlate_ctx
*ctx
, struct ofpact_cnt_ids
*ids
)
4055 struct flow
*flow
= &ctx
->xin
->flow
;
4057 if (!is_ip_any(flow
)) {
4061 ctx
->wc
->masks
.nw_ttl
= 0xff;
4062 if (flow
->nw_ttl
> 1) {
4068 for (i
= 0; i
< ids
->n_controllers
; i
++) {
4069 execute_controller_action(ctx
, UINT16_MAX
, OFPR_INVALID_TTL
,
4070 ids
->cnt_ids
[i
], NULL
, 0);
4073 /* Stop processing for current table. */
4074 xlate_report(ctx
, OFT_WARN
, "IPv%d decrement TTL exception",
4075 flow
->dl_type
== htons(ETH_TYPE_IP
) ? 4 : 6);
4081 compose_set_mpls_label_action(struct xlate_ctx
*ctx
, ovs_be32 label
)
4083 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
4084 ctx
->wc
->masks
.mpls_lse
[0] |= htonl(MPLS_LABEL_MASK
);
4085 set_mpls_lse_label(&ctx
->xin
->flow
.mpls_lse
[0], label
);
4090 compose_set_mpls_tc_action(struct xlate_ctx
*ctx
, uint8_t tc
)
4092 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
4093 ctx
->wc
->masks
.mpls_lse
[0] |= htonl(MPLS_TC_MASK
);
4094 set_mpls_lse_tc(&ctx
->xin
->flow
.mpls_lse
[0], tc
);
4099 compose_set_mpls_ttl_action(struct xlate_ctx
*ctx
, uint8_t ttl
)
4101 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
4102 ctx
->wc
->masks
.mpls_lse
[0] |= htonl(MPLS_TTL_MASK
);
4103 set_mpls_lse_ttl(&ctx
->xin
->flow
.mpls_lse
[0], ttl
);
4108 compose_dec_mpls_ttl_action(struct xlate_ctx
*ctx
)
4110 struct flow
*flow
= &ctx
->xin
->flow
;
4112 if (eth_type_mpls(flow
->dl_type
)) {
4113 uint8_t ttl
= mpls_lse_to_ttl(flow
->mpls_lse
[0]);
4115 ctx
->wc
->masks
.mpls_lse
[0] |= htonl(MPLS_TTL_MASK
);
4118 set_mpls_lse_ttl(&flow
->mpls_lse
[0], ttl
);
4121 execute_controller_action(ctx
, UINT16_MAX
, OFPR_INVALID_TTL
, 0,
4126 /* Stop processing for current table. */
4127 xlate_report(ctx
, OFT_WARN
, "MPLS decrement TTL exception");
4132 xlate_output_action(struct xlate_ctx
*ctx
,
4133 ofp_port_t port
, uint16_t max_len
, bool may_packet_in
)
4135 ofp_port_t prev_nf_output_iface
= ctx
->nf_output_iface
;
4137 ctx
->nf_output_iface
= NF_OUT_DROP
;
4141 compose_output_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
, NULL
);
4144 xlate_table_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
,
4145 0, may_packet_in
, true);
4151 flood_packets(ctx
, false);
4154 flood_packets(ctx
, true);
4156 case OFPP_CONTROLLER
:
4157 execute_controller_action(ctx
, max_len
,
4158 (ctx
->in_group
? OFPR_GROUP
4159 : ctx
->in_action_set
? OFPR_ACTION_SET
4167 if (port
!= ctx
->xin
->flow
.in_port
.ofp_port
) {
4168 compose_output_action(ctx
, port
, NULL
);
4170 xlate_report(ctx
, OFT_WARN
, "skipping output to input port");
4175 if (prev_nf_output_iface
== NF_OUT_FLOOD
) {
4176 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
4177 } else if (ctx
->nf_output_iface
== NF_OUT_DROP
) {
4178 ctx
->nf_output_iface
= prev_nf_output_iface
;
4179 } else if (prev_nf_output_iface
!= NF_OUT_DROP
&&
4180 ctx
->nf_output_iface
!= NF_OUT_FLOOD
) {
4181 ctx
->nf_output_iface
= NF_OUT_MULTI
;
4186 xlate_output_reg_action(struct xlate_ctx
*ctx
,
4187 const struct ofpact_output_reg
*or)
4189 uint64_t port
= mf_get_subfield(&or->src
, &ctx
->xin
->flow
);
4190 if (port
<= UINT16_MAX
) {
4191 xlate_report(ctx
, OFT_DETAIL
, "output port is %"PRIu64
, port
);
4193 union mf_subvalue value
;
4195 memset(&value
, 0xff, sizeof value
);
4196 mf_write_subfield_flow(&or->src
, &value
, &ctx
->wc
->masks
);
4197 xlate_output_action(ctx
, u16_to_ofp(port
), or->max_len
, false);
4199 xlate_report(ctx
, OFT_WARN
, "output port %"PRIu64
" is out of range",
4205 xlate_output_trunc_action(struct xlate_ctx
*ctx
,
4206 ofp_port_t port
, uint32_t max_len
)
4208 bool support_trunc
= ctx
->xbridge
->support
.trunc
;
4209 struct ovs_action_trunc
*trunc
;
4210 char name
[OFP_MAX_PORT_NAME_LEN
];
4217 case OFPP_CONTROLLER
:
4219 ofputil_port_to_string(port
, name
, sizeof name
);
4220 xlate_report(ctx
, OFT_WARN
,
4221 "output_trunc does not support port: %s", name
);
4226 if (port
!= ctx
->xin
->flow
.in_port
.ofp_port
) {
4227 const struct xport
*xport
= get_ofp_port(ctx
->xbridge
, port
);
4229 if (xport
== NULL
|| xport
->odp_port
== ODPP_NONE
) {
4230 /* Since truncate happens at its following output action, if
4231 * the output port is a patch port, the behavior is somehow
4232 * unpredicable. For simpilicity, disallow this case. */
4233 ofputil_port_to_string(port
, name
, sizeof name
);
4234 xlate_report_error(ctx
, "output_trunc does not support "
4235 "patch port %s", name
);
4239 trunc
= nl_msg_put_unspec_uninit(ctx
->odp_actions
,
4240 OVS_ACTION_ATTR_TRUNC
,
4242 trunc
->max_len
= max_len
;
4243 xlate_output_action(ctx
, port
, max_len
, false);
4244 if (!support_trunc
) {
4245 ctx
->xout
->slow
|= SLOW_ACTION
;
4248 xlate_report(ctx
, OFT_WARN
, "skipping output to input port");
4255 xlate_enqueue_action(struct xlate_ctx
*ctx
,
4256 const struct ofpact_enqueue
*enqueue
)
4258 ofp_port_t ofp_port
= enqueue
->port
;
4259 uint32_t queue_id
= enqueue
->queue
;
4260 uint32_t flow_priority
, priority
;
4263 /* Translate queue to priority. */
4264 error
= dpif_queue_to_priority(ctx
->xbridge
->dpif
, queue_id
, &priority
);
4266 /* Fall back to ordinary output action. */
4267 xlate_output_action(ctx
, enqueue
->port
, 0, false);
4271 /* Check output port. */
4272 if (ofp_port
== OFPP_IN_PORT
) {
4273 ofp_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
4274 } else if (ofp_port
== ctx
->xin
->flow
.in_port
.ofp_port
) {
4278 /* Add datapath actions. */
4279 flow_priority
= ctx
->xin
->flow
.skb_priority
;
4280 ctx
->xin
->flow
.skb_priority
= priority
;
4281 compose_output_action(ctx
, ofp_port
, NULL
);
4282 ctx
->xin
->flow
.skb_priority
= flow_priority
;
4284 /* Update NetFlow output port. */
4285 if (ctx
->nf_output_iface
== NF_OUT_DROP
) {
4286 ctx
->nf_output_iface
= ofp_port
;
4287 } else if (ctx
->nf_output_iface
!= NF_OUT_FLOOD
) {
4288 ctx
->nf_output_iface
= NF_OUT_MULTI
;
4293 xlate_set_queue_action(struct xlate_ctx
*ctx
, uint32_t queue_id
)
4295 uint32_t skb_priority
;
4297 if (!dpif_queue_to_priority(ctx
->xbridge
->dpif
, queue_id
, &skb_priority
)) {
4298 ctx
->xin
->flow
.skb_priority
= skb_priority
;
4300 /* Couldn't translate queue to a priority. Nothing to do. A warning
4301 * has already been logged. */
4306 slave_enabled_cb(ofp_port_t ofp_port
, void *xbridge_
)
4308 const struct xbridge
*xbridge
= xbridge_
;
4319 case OFPP_CONTROLLER
: /* Not supported by the bundle action. */
4322 port
= get_ofp_port(xbridge
, ofp_port
);
4323 return port
? port
->may_enable
: false;
4328 xlate_bundle_action(struct xlate_ctx
*ctx
,
4329 const struct ofpact_bundle
*bundle
)
4333 port
= bundle_execute(bundle
, &ctx
->xin
->flow
, ctx
->wc
, slave_enabled_cb
,
4334 CONST_CAST(struct xbridge
*, ctx
->xbridge
));
4335 if (bundle
->dst
.field
) {
4336 nxm_reg_load(&bundle
->dst
, ofp_to_u16(port
), &ctx
->xin
->flow
, ctx
->wc
);
4337 xlate_report_subfield(ctx
, &bundle
->dst
);
4339 xlate_output_action(ctx
, port
, 0, false);
4344 xlate_learn_action(struct xlate_ctx
*ctx
, const struct ofpact_learn
*learn
)
4346 learn_mask(learn
, ctx
->wc
);
4348 if (ctx
->xin
->xcache
|| ctx
->xin
->allow_side_effects
) {
4349 uint64_t ofpacts_stub
[1024 / 8];
4350 struct ofputil_flow_mod fm
;
4351 struct ofproto_flow_mod ofm__
, *ofm
;
4352 struct ofpbuf ofpacts
;
4355 if (ctx
->xin
->xcache
) {
4356 struct xc_entry
*entry
;
4358 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_LEARN
);
4359 entry
->learn
.ofm
= xmalloc(sizeof *entry
->learn
.ofm
);
4360 ofm
= entry
->learn
.ofm
;
4365 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
4366 learn_execute(learn
, &ctx
->xin
->flow
, &fm
, &ofpacts
);
4367 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
4368 struct ds s
= DS_EMPTY_INITIALIZER
;
4369 ds_put_format(&s
, "table=%"PRIu8
" ", fm
.table_id
);
4370 match_format(&fm
.match
, &s
, OFP_DEFAULT_PRIORITY
);
4372 ds_put_format(&s
, " priority=%d", fm
.priority
);
4373 if (fm
.new_cookie
) {
4374 ds_put_format(&s
, " cookie=%#"PRIx64
, ntohll(fm
.new_cookie
));
4376 if (fm
.idle_timeout
!= OFP_FLOW_PERMANENT
) {
4377 ds_put_format(&s
, " idle=%"PRIu16
, fm
.idle_timeout
);
4379 if (fm
.hard_timeout
!= OFP_FLOW_PERMANENT
) {
4380 ds_put_format(&s
, " hard=%"PRIu16
, fm
.hard_timeout
);
4382 if (fm
.flags
& NX_LEARN_F_SEND_FLOW_REM
) {
4383 ds_put_cstr(&s
, " send_flow_rem");
4385 ds_put_cstr(&s
, " actions=");
4386 ofpacts_format(fm
.ofpacts
, fm
.ofpacts_len
, &s
);
4387 xlate_report(ctx
, OFT_DETAIL
, "%s", ds_cstr(&s
));
4390 error
= ofproto_dpif_flow_mod_init_for_learn(ctx
->xbridge
->ofproto
,
4392 ofpbuf_uninit(&ofpacts
);
4394 if (!error
&& ctx
->xin
->allow_side_effects
) {
4395 error
= ofproto_flow_mod_learn(ofm
, ctx
->xin
->xcache
!= NULL
);
4399 xlate_report_error(ctx
, "LEARN action execution failed (%s).",
4400 ofperr_to_string(error
));
4403 xlate_report(ctx
, OFT_WARN
,
4404 "suppressing side effects, so learn action ignored");
4409 xlate_fin_timeout__(struct rule_dpif
*rule
, uint16_t tcp_flags
,
4410 uint16_t idle_timeout
, uint16_t hard_timeout
)
4412 if (tcp_flags
& (TCP_FIN
| TCP_RST
)) {
4413 ofproto_rule_reduce_timeouts(&rule
->up
, idle_timeout
, hard_timeout
);
4418 xlate_fin_timeout(struct xlate_ctx
*ctx
,
4419 const struct ofpact_fin_timeout
*oft
)
4422 if (ctx
->xin
->allow_side_effects
) {
4423 xlate_fin_timeout__(ctx
->rule
, ctx
->xin
->tcp_flags
,
4424 oft
->fin_idle_timeout
, oft
->fin_hard_timeout
);
4426 if (ctx
->xin
->xcache
) {
4427 struct xc_entry
*entry
;
4429 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_FIN_TIMEOUT
);
4430 /* XC_RULE already holds a reference on the rule, none is taken
4432 entry
->fin
.rule
= ctx
->rule
;
4433 entry
->fin
.idle
= oft
->fin_idle_timeout
;
4434 entry
->fin
.hard
= oft
->fin_hard_timeout
;
4440 xlate_sample_action(struct xlate_ctx
*ctx
,
4441 const struct ofpact_sample
*os
)
4443 odp_port_t output_odp_port
= ODPP_NONE
;
4444 odp_port_t tunnel_out_port
= ODPP_NONE
;
4445 struct dpif_ipfix
*ipfix
= ctx
->xbridge
->ipfix
;
4446 bool emit_set_tunnel
= false;
4448 if (!ipfix
|| ctx
->xin
->flow
.in_port
.ofp_port
== OFPP_NONE
) {
4452 /* Scale the probability from 16-bit to 32-bit while representing
4453 * the same percentage. */
4454 uint32_t probability
= (os
->probability
<< 16) | os
->probability
;
4456 if (!ctx
->xbridge
->support
.variable_length_userdata
) {
4457 xlate_report_error(ctx
, "ignoring NXAST_SAMPLE action because "
4458 "datapath lacks support (needs Linux 3.10+ or "
4459 "kernel module from OVS 1.11+)");
4463 /* If ofp_port in flow sample action is equel to ofp_port,
4464 * this sample action is a input port action. */
4465 if (os
->sampling_port
!= OFPP_NONE
&&
4466 os
->sampling_port
!= ctx
->xin
->flow
.in_port
.ofp_port
) {
4467 output_odp_port
= ofp_port_to_odp_port(ctx
->xbridge
,
4469 if (output_odp_port
== ODPP_NONE
) {
4470 xlate_report_error(ctx
, "can't use unknown port %d in flow sample "
4471 "action", os
->sampling_port
);
4475 if (dpif_ipfix_get_flow_exporter_tunnel_sampling(ipfix
,
4476 os
->collector_set_id
)
4477 && dpif_ipfix_get_tunnel_port(ipfix
, output_odp_port
)) {
4478 tunnel_out_port
= output_odp_port
;
4479 emit_set_tunnel
= true;
4483 xlate_commit_actions(ctx
);
4484 /* If 'emit_set_tunnel', sample(sampling_port=1) would translate
4485 * into datapath sample action set(tunnel(...)), sample(...) and
4486 * it is used for sampling egress tunnel information. */
4487 if (emit_set_tunnel
) {
4488 const struct xport
*xport
= get_ofp_port(ctx
->xbridge
,
4491 if (xport
&& xport
->is_tunnel
) {
4492 struct flow
*flow
= &ctx
->xin
->flow
;
4493 tnl_port_send(xport
->ofport
, flow
, ctx
->wc
);
4494 if (!ovs_native_tunneling_is_on(ctx
->xbridge
->ofproto
)) {
4495 struct flow_tnl flow_tnl
= flow
->tunnel
;
4497 commit_odp_tunnel_action(flow
, &ctx
->base_flow
,
4499 flow
->tunnel
= flow_tnl
;
4502 xlate_report_error(ctx
,
4503 "sampling_port:%d should be a tunnel port.",
4508 union user_action_cookie cookie
= {
4510 .type
= USER_ACTION_COOKIE_FLOW_SAMPLE
,
4511 .probability
= os
->probability
,
4512 .collector_set_id
= os
->collector_set_id
,
4513 .obs_domain_id
= os
->obs_domain_id
,
4514 .obs_point_id
= os
->obs_point_id
,
4515 .output_odp_port
= output_odp_port
,
4516 .direction
= os
->direction
,
4519 compose_sample_action(ctx
, probability
, &cookie
, sizeof cookie
.flow_sample
,
4520 tunnel_out_port
, false);
4523 /* Only called if the datapath supports 'OVS_ACTION_ATTR_CLONE'.
4525 * Translates 'oc' within OVS_ACTION_ATTR_CLONE. */
4527 compose_clone_action(struct xlate_ctx
*ctx
, const struct ofpact_nest
*oc
)
4529 size_t clone_offset
= nl_msg_start_nested(ctx
->odp_actions
,
4530 OVS_ACTION_ATTR_CLONE
);
4532 do_xlate_actions(oc
->actions
, ofpact_nest_get_action_len(oc
), ctx
);
4534 nl_msg_end_non_empty_nested(ctx
->odp_actions
, clone_offset
);
4538 xlate_clone(struct xlate_ctx
*ctx
, const struct ofpact_nest
*oc
)
4540 bool old_was_mpls
= ctx
->was_mpls
;
4541 bool old_conntracked
= ctx
->conntracked
;
4542 struct flow old_flow
= ctx
->xin
->flow
;
4544 struct ofpbuf old_stack
= ctx
->stack
;
4545 union mf_subvalue new_stack
[1024 / sizeof(union mf_subvalue
)];
4546 ofpbuf_use_stub(&ctx
->stack
, new_stack
, sizeof new_stack
);
4547 ofpbuf_put(&ctx
->stack
, old_stack
.data
, old_stack
.size
);
4549 struct ofpbuf old_action_set
= ctx
->action_set
;
4550 uint64_t actset_stub
[1024 / 8];
4551 ofpbuf_use_stub(&ctx
->action_set
, actset_stub
, sizeof actset_stub
);
4552 ofpbuf_put(&ctx
->action_set
, old_action_set
.data
, old_action_set
.size
);
4554 if (ctx
->xbridge
->support
.clone
) {
4555 /* Datapath clone action will make sure the pre clone packets
4556 * are used for actions after clone. Save and restore
4557 * ctx->base_flow to reflect this for the openflow pipeline. */
4558 struct flow old_base_flow
= ctx
->base_flow
;
4559 compose_clone_action(ctx
, oc
);
4560 ctx
->base_flow
= old_base_flow
;
4562 do_xlate_actions(oc
->actions
, ofpact_nest_get_action_len(oc
), ctx
);
4565 ofpbuf_uninit(&ctx
->action_set
);
4566 ctx
->action_set
= old_action_set
;
4568 ofpbuf_uninit(&ctx
->stack
);
4569 ctx
->stack
= old_stack
;
4571 ctx
->xin
->flow
= old_flow
;
4573 /* The clone's conntrack execution should have no effect on the original
4575 ctx
->conntracked
= old_conntracked
;
4577 /* Popping MPLS from the clone should have no effect on the original
4579 ctx
->was_mpls
= old_was_mpls
;
4583 may_receive(const struct xport
*xport
, struct xlate_ctx
*ctx
)
4585 if (xport
->config
& (is_stp(&ctx
->xin
->flow
)
4586 ? OFPUTIL_PC_NO_RECV_STP
4587 : OFPUTIL_PC_NO_RECV
)) {
4591 /* Only drop packets here if both forwarding and learning are
4592 * disabled. If just learning is enabled, we need to have
4593 * OFPP_NORMAL and the learning action have a look at the packet
4594 * before we can drop it. */
4595 if ((!xport_stp_forward_state(xport
) && !xport_stp_learn_state(xport
)) ||
4596 (!xport_rstp_forward_state(xport
) && !xport_rstp_learn_state(xport
))) {
4604 xlate_write_actions__(struct xlate_ctx
*ctx
,
4605 const struct ofpact
*ofpacts
, size_t ofpacts_len
)
4607 /* Maintain actset_output depending on the contents of the action set:
4609 * - OFPP_UNSET, if there is no "output" action.
4611 * - The output port, if there is an "output" action and no "group"
4614 * - OFPP_UNSET, if there is a "group" action.
4616 if (!ctx
->action_set_has_group
) {
4617 const struct ofpact
*a
;
4618 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
4619 if (a
->type
== OFPACT_OUTPUT
) {
4620 ctx
->xin
->flow
.actset_output
= ofpact_get_OUTPUT(a
)->port
;
4621 } else if (a
->type
== OFPACT_GROUP
) {
4622 ctx
->xin
->flow
.actset_output
= OFPP_UNSET
;
4623 ctx
->action_set_has_group
= true;
4629 ofpbuf_put(&ctx
->action_set
, ofpacts
, ofpacts_len
);
4633 xlate_write_actions(struct xlate_ctx
*ctx
, const struct ofpact_nest
*a
)
4635 xlate_write_actions__(ctx
, a
->actions
, ofpact_nest_get_action_len(a
));
4639 xlate_action_set(struct xlate_ctx
*ctx
)
4641 uint64_t action_list_stub
[1024 / 8];
4642 struct ofpbuf action_list
= OFPBUF_STUB_INITIALIZER(action_list_stub
);
4643 ofpacts_execute_action_set(&action_list
, &ctx
->action_set
);
4644 /* Clear the action set, as it is not needed any more. */
4645 ofpbuf_clear(&ctx
->action_set
);
4646 if (action_list
.size
) {
4647 ctx
->in_action_set
= true;
4649 struct ovs_list
*old_trace
= ctx
->xin
->trace
;
4650 ctx
->xin
->trace
= xlate_report(ctx
, OFT_TABLE
,
4651 "--. Executing action set:");
4652 do_xlate_actions(action_list
.data
, action_list
.size
, ctx
);
4653 ctx
->xin
->trace
= old_trace
;
4655 ctx
->in_action_set
= false;
4657 ofpbuf_uninit(&action_list
);
4661 freeze_put_unroll_xlate(struct xlate_ctx
*ctx
)
4663 struct ofpact_unroll_xlate
*unroll
= ctx
->frozen_actions
.header
;
4665 /* Restore the table_id and rule cookie for a potential PACKET
4668 (ctx
->table_id
!= unroll
->rule_table_id
4669 || ctx
->rule_cookie
!= unroll
->rule_cookie
)) {
4670 unroll
= ofpact_put_UNROLL_XLATE(&ctx
->frozen_actions
);
4671 unroll
->rule_table_id
= ctx
->table_id
;
4672 unroll
->rule_cookie
= ctx
->rule_cookie
;
4673 ctx
->frozen_actions
.header
= unroll
;
4678 /* Copy actions 'a' through 'end' to ctx->frozen_actions, which will be
4679 * executed after thawing. Inserts an UNROLL_XLATE action, if none is already
4680 * present, before any action that may depend on the current table ID or flow
4683 freeze_unroll_actions(const struct ofpact
*a
, const struct ofpact
*end
,
4684 struct xlate_ctx
*ctx
)
4686 for (; a
< end
; a
= ofpact_next(a
)) {
4688 case OFPACT_OUTPUT_REG
:
4689 case OFPACT_OUTPUT_TRUNC
:
4692 case OFPACT_CONTROLLER
:
4693 case OFPACT_DEC_MPLS_TTL
:
4694 case OFPACT_DEC_TTL
:
4695 /* These actions may generate asynchronous messages, which include
4696 * table ID and flow cookie information. */
4697 freeze_put_unroll_xlate(ctx
);
4700 case OFPACT_RESUBMIT
:
4701 if (ofpact_get_RESUBMIT(a
)->table_id
== 0xff) {
4702 /* This resubmit action is relative to the current table, so we
4703 * need to track what table that is.*/
4704 freeze_put_unroll_xlate(ctx
);
4708 case OFPACT_SET_TUNNEL
:
4709 case OFPACT_REG_MOVE
:
4710 case OFPACT_SET_FIELD
:
4711 case OFPACT_STACK_PUSH
:
4712 case OFPACT_STACK_POP
:
4714 case OFPACT_WRITE_METADATA
:
4715 case OFPACT_GOTO_TABLE
:
4716 case OFPACT_ENQUEUE
:
4717 case OFPACT_SET_VLAN_VID
:
4718 case OFPACT_SET_VLAN_PCP
:
4719 case OFPACT_STRIP_VLAN
:
4720 case OFPACT_PUSH_VLAN
:
4721 case OFPACT_SET_ETH_SRC
:
4722 case OFPACT_SET_ETH_DST
:
4723 case OFPACT_SET_IPV4_SRC
:
4724 case OFPACT_SET_IPV4_DST
:
4725 case OFPACT_SET_IP_DSCP
:
4726 case OFPACT_SET_IP_ECN
:
4727 case OFPACT_SET_IP_TTL
:
4728 case OFPACT_SET_L4_SRC_PORT
:
4729 case OFPACT_SET_L4_DST_PORT
:
4730 case OFPACT_SET_QUEUE
:
4731 case OFPACT_POP_QUEUE
:
4732 case OFPACT_PUSH_MPLS
:
4733 case OFPACT_POP_MPLS
:
4734 case OFPACT_SET_MPLS_LABEL
:
4735 case OFPACT_SET_MPLS_TC
:
4736 case OFPACT_SET_MPLS_TTL
:
4737 case OFPACT_MULTIPATH
:
4740 case OFPACT_UNROLL_XLATE
:
4741 case OFPACT_FIN_TIMEOUT
:
4742 case OFPACT_CLEAR_ACTIONS
:
4743 case OFPACT_WRITE_ACTIONS
:
4747 case OFPACT_DEBUG_RECIRC
:
4749 case OFPACT_CT_CLEAR
:
4751 /* These may not generate PACKET INs. */
4755 case OFPACT_CONJUNCTION
:
4756 /* These need not be copied for restoration. */
4759 /* Copy the action over. */
4760 ofpbuf_put(&ctx
->frozen_actions
, a
, OFPACT_ALIGN(a
->len
));
4765 put_ct_mark(const struct flow
*flow
, struct ofpbuf
*odp_actions
,
4766 struct flow_wildcards
*wc
)
4768 if (wc
->masks
.ct_mark
) {
4774 odp_ct_mark
= nl_msg_put_unspec_uninit(odp_actions
, OVS_CT_ATTR_MARK
,
4775 sizeof(*odp_ct_mark
));
4776 odp_ct_mark
->key
= flow
->ct_mark
& wc
->masks
.ct_mark
;
4777 odp_ct_mark
->mask
= wc
->masks
.ct_mark
;
4782 put_ct_label(const struct flow
*flow
, struct ofpbuf
*odp_actions
,
4783 struct flow_wildcards
*wc
)
4785 if (!ovs_u128_is_zero(wc
->masks
.ct_label
)) {
4791 odp_ct_label
= nl_msg_put_unspec_uninit(odp_actions
,
4793 sizeof(*odp_ct_label
));
4794 odp_ct_label
->key
= ovs_u128_and(flow
->ct_label
, wc
->masks
.ct_label
);
4795 odp_ct_label
->mask
= wc
->masks
.ct_label
;
4800 put_ct_helper(struct xlate_ctx
*ctx
,
4801 struct ofpbuf
*odp_actions
, struct ofpact_conntrack
*ofc
)
4806 nl_msg_put_string(odp_actions
, OVS_CT_ATTR_HELPER
, "ftp");
4809 nl_msg_put_string(odp_actions
, OVS_CT_ATTR_HELPER
, "tftp");
4812 xlate_report_error(ctx
, "cannot serialize ct_helper %d", ofc
->alg
);
4819 put_ct_nat(struct xlate_ctx
*ctx
)
4821 struct ofpact_nat
*ofn
= ctx
->ct_nat_action
;
4828 nat_offset
= nl_msg_start_nested(ctx
->odp_actions
, OVS_CT_ATTR_NAT
);
4829 if (ofn
->flags
& NX_NAT_F_SRC
|| ofn
->flags
& NX_NAT_F_DST
) {
4830 nl_msg_put_flag(ctx
->odp_actions
, ofn
->flags
& NX_NAT_F_SRC
4831 ? OVS_NAT_ATTR_SRC
: OVS_NAT_ATTR_DST
);
4832 if (ofn
->flags
& NX_NAT_F_PERSISTENT
) {
4833 nl_msg_put_flag(ctx
->odp_actions
, OVS_NAT_ATTR_PERSISTENT
);
4835 if (ofn
->flags
& NX_NAT_F_PROTO_HASH
) {
4836 nl_msg_put_flag(ctx
->odp_actions
, OVS_NAT_ATTR_PROTO_HASH
);
4837 } else if (ofn
->flags
& NX_NAT_F_PROTO_RANDOM
) {
4838 nl_msg_put_flag(ctx
->odp_actions
, OVS_NAT_ATTR_PROTO_RANDOM
);
4840 if (ofn
->range_af
== AF_INET
) {
4841 nl_msg_put_be32(ctx
->odp_actions
, OVS_NAT_ATTR_IP_MIN
,
4842 ofn
->range
.addr
.ipv4
.min
);
4843 if (ofn
->range
.addr
.ipv4
.max
&&
4844 (ntohl(ofn
->range
.addr
.ipv4
.max
)
4845 > ntohl(ofn
->range
.addr
.ipv4
.min
))) {
4846 nl_msg_put_be32(ctx
->odp_actions
, OVS_NAT_ATTR_IP_MAX
,
4847 ofn
->range
.addr
.ipv4
.max
);
4849 } else if (ofn
->range_af
== AF_INET6
) {
4850 nl_msg_put_unspec(ctx
->odp_actions
, OVS_NAT_ATTR_IP_MIN
,
4851 &ofn
->range
.addr
.ipv6
.min
,
4852 sizeof ofn
->range
.addr
.ipv6
.min
);
4853 if (!ipv6_mask_is_any(&ofn
->range
.addr
.ipv6
.max
) &&
4854 memcmp(&ofn
->range
.addr
.ipv6
.max
, &ofn
->range
.addr
.ipv6
.min
,
4855 sizeof ofn
->range
.addr
.ipv6
.max
) > 0) {
4856 nl_msg_put_unspec(ctx
->odp_actions
, OVS_NAT_ATTR_IP_MAX
,
4857 &ofn
->range
.addr
.ipv6
.max
,
4858 sizeof ofn
->range
.addr
.ipv6
.max
);
4861 if (ofn
->range_af
!= AF_UNSPEC
&& ofn
->range
.proto
.min
) {
4862 nl_msg_put_u16(ctx
->odp_actions
, OVS_NAT_ATTR_PROTO_MIN
,
4863 ofn
->range
.proto
.min
);
4864 if (ofn
->range
.proto
.max
&&
4865 ofn
->range
.proto
.max
> ofn
->range
.proto
.min
) {
4866 nl_msg_put_u16(ctx
->odp_actions
, OVS_NAT_ATTR_PROTO_MAX
,
4867 ofn
->range
.proto
.max
);
4871 nl_msg_end_nested(ctx
->odp_actions
, nat_offset
);
4875 compose_conntrack_action(struct xlate_ctx
*ctx
, struct ofpact_conntrack
*ofc
)
4877 ovs_u128 old_ct_label
= ctx
->base_flow
.ct_label
;
4878 ovs_u128 old_ct_label_mask
= ctx
->wc
->masks
.ct_label
;
4879 uint32_t old_ct_mark
= ctx
->base_flow
.ct_mark
;
4880 uint32_t old_ct_mark_mask
= ctx
->wc
->masks
.ct_mark
;
4884 /* Ensure that any prior actions are applied before composing the new
4885 * conntrack action. */
4886 xlate_commit_actions(ctx
);
4888 /* Process nested actions first, to populate the key. */
4889 ctx
->ct_nat_action
= NULL
;
4890 ctx
->wc
->masks
.ct_mark
= 0;
4891 ctx
->wc
->masks
.ct_label
.u64
.hi
= ctx
->wc
->masks
.ct_label
.u64
.lo
= 0;
4892 do_xlate_actions(ofc
->actions
, ofpact_ct_get_action_len(ofc
), ctx
);
4894 if (ofc
->zone_src
.field
) {
4895 zone
= mf_get_subfield(&ofc
->zone_src
, &ctx
->xin
->flow
);
4897 zone
= ofc
->zone_imm
;
4900 ct_offset
= nl_msg_start_nested(ctx
->odp_actions
, OVS_ACTION_ATTR_CT
);
4901 if (ofc
->flags
& NX_CT_F_COMMIT
) {
4902 nl_msg_put_flag(ctx
->odp_actions
, OVS_CT_ATTR_COMMIT
);
4904 nl_msg_put_u16(ctx
->odp_actions
, OVS_CT_ATTR_ZONE
, zone
);
4905 put_ct_mark(&ctx
->xin
->flow
, ctx
->odp_actions
, ctx
->wc
);
4906 put_ct_label(&ctx
->xin
->flow
, ctx
->odp_actions
, ctx
->wc
);
4907 put_ct_helper(ctx
, ctx
->odp_actions
, ofc
);
4909 ctx
->ct_nat_action
= NULL
;
4910 nl_msg_end_nested(ctx
->odp_actions
, ct_offset
);
4912 /* Restore the original ct fields in the key. These should only be exposed
4913 * after recirculation to another table. */
4914 ctx
->base_flow
.ct_mark
= old_ct_mark
;
4915 ctx
->wc
->masks
.ct_mark
= old_ct_mark_mask
;
4916 ctx
->base_flow
.ct_label
= old_ct_label
;
4917 ctx
->wc
->masks
.ct_label
= old_ct_label_mask
;
4919 if (ofc
->recirc_table
== NX_CT_RECIRC_NONE
) {
4920 /* If we do not recirculate as part of this action, hide the results of
4921 * connection tracking from subsequent recirculations. */
4922 ctx
->conntracked
= false;
4924 /* Use ct_* fields from datapath during recirculation upcall. */
4925 ctx
->conntracked
= true;
4926 compose_recirculate_and_fork(ctx
, ofc
->recirc_table
);
4931 recirc_for_mpls(const struct ofpact
*a
, struct xlate_ctx
*ctx
)
4933 /* No need to recirculate if already exiting. */
4938 /* Do not consider recirculating unless the packet was previously MPLS. */
4939 if (!ctx
->was_mpls
) {
4943 /* Special case these actions, only recirculating if necessary.
4944 * This avoids the overhead of recirculation in common use-cases.
4948 /* Output actions do not require recirculation. */
4950 case OFPACT_OUTPUT_TRUNC
:
4951 case OFPACT_ENQUEUE
:
4952 case OFPACT_OUTPUT_REG
:
4953 /* Set actions that don't touch L3+ fields do not require recirculation. */
4954 case OFPACT_SET_VLAN_VID
:
4955 case OFPACT_SET_VLAN_PCP
:
4956 case OFPACT_SET_ETH_SRC
:
4957 case OFPACT_SET_ETH_DST
:
4958 case OFPACT_SET_TUNNEL
:
4959 case OFPACT_SET_QUEUE
:
4960 /* If actions of a group require recirculation that can be detected
4961 * when translating them. */
4965 /* Set field that don't touch L3+ fields don't require recirculation. */
4966 case OFPACT_SET_FIELD
:
4967 if (mf_is_l3_or_higher(ofpact_get_SET_FIELD(a
)->field
)) {
4972 /* For simplicity, recirculate in all other cases. */
4973 case OFPACT_CONTROLLER
:
4975 case OFPACT_STRIP_VLAN
:
4976 case OFPACT_PUSH_VLAN
:
4977 case OFPACT_SET_IPV4_SRC
:
4978 case OFPACT_SET_IPV4_DST
:
4979 case OFPACT_SET_IP_DSCP
:
4980 case OFPACT_SET_IP_ECN
:
4981 case OFPACT_SET_IP_TTL
:
4982 case OFPACT_SET_L4_SRC_PORT
:
4983 case OFPACT_SET_L4_DST_PORT
:
4984 case OFPACT_REG_MOVE
:
4985 case OFPACT_STACK_PUSH
:
4986 case OFPACT_STACK_POP
:
4987 case OFPACT_DEC_TTL
:
4988 case OFPACT_SET_MPLS_LABEL
:
4989 case OFPACT_SET_MPLS_TC
:
4990 case OFPACT_SET_MPLS_TTL
:
4991 case OFPACT_DEC_MPLS_TTL
:
4992 case OFPACT_PUSH_MPLS
:
4993 case OFPACT_POP_MPLS
:
4994 case OFPACT_POP_QUEUE
:
4995 case OFPACT_FIN_TIMEOUT
:
4996 case OFPACT_RESUBMIT
:
4998 case OFPACT_CONJUNCTION
:
4999 case OFPACT_MULTIPATH
:
5004 case OFPACT_UNROLL_XLATE
:
5006 case OFPACT_CT_CLEAR
:
5008 case OFPACT_DEBUG_RECIRC
:
5010 case OFPACT_CLEAR_ACTIONS
:
5011 case OFPACT_WRITE_ACTIONS
:
5012 case OFPACT_WRITE_METADATA
:
5013 case OFPACT_GOTO_TABLE
:
5019 ctx_trigger_freeze(ctx
);
5023 xlate_ofpact_reg_move(struct xlate_ctx
*ctx
, const struct ofpact_reg_move
*a
)
5025 mf_subfield_copy(&a
->src
, &a
->dst
, &ctx
->xin
->flow
, ctx
->wc
);
5026 xlate_report_subfield(ctx
, &a
->dst
);
5030 xlate_ofpact_stack_pop(struct xlate_ctx
*ctx
, const struct ofpact_stack
*a
)
5032 if (nxm_execute_stack_pop(a
, &ctx
->xin
->flow
, ctx
->wc
, &ctx
->stack
)) {
5033 xlate_report_subfield(ctx
, &a
->subfield
);
5035 xlate_report_error(ctx
, "stack underflow");
5039 /* Restore translation context data that was stored earlier. */
5041 xlate_ofpact_unroll_xlate(struct xlate_ctx
*ctx
,
5042 const struct ofpact_unroll_xlate
*a
)
5044 ctx
->table_id
= a
->rule_table_id
;
5045 ctx
->rule_cookie
= a
->rule_cookie
;
5046 xlate_report(ctx
, OFT_THAW
, "restored state: table=%"PRIu8
", "
5047 "cookie=%#"PRIx64
, a
->rule_table_id
, a
->rule_cookie
);
5051 do_xlate_actions(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
5052 struct xlate_ctx
*ctx
)
5054 struct flow_wildcards
*wc
= ctx
->wc
;
5055 struct flow
*flow
= &ctx
->xin
->flow
;
5056 const struct ofpact
*a
;
5058 if (ovs_native_tunneling_is_on(ctx
->xbridge
->ofproto
)) {
5059 tnl_neigh_snoop(flow
, wc
, ctx
->xbridge
->name
);
5061 /* dl_type already in the mask, not set below. */
5064 xlate_report(ctx
, OFT_ACTION
, "drop");
5068 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
5069 struct ofpact_controller
*controller
;
5070 const struct ofpact_metadata
*metadata
;
5071 const struct ofpact_set_field
*set_field
;
5072 const struct mf_field
*mf
;
5078 recirc_for_mpls(a
, ctx
);
5081 /* Check if need to store the remaining actions for later
5083 if (ctx
->freezing
) {
5084 freeze_unroll_actions(a
, ofpact_end(ofpacts
, ofpacts_len
),
5090 if (OVS_UNLIKELY(ctx
->xin
->trace
)) {
5091 struct ds s
= DS_EMPTY_INITIALIZER
;
5092 ofpacts_format(a
, OFPACT_ALIGN(a
->len
), &s
);
5093 xlate_report(ctx
, OFT_ACTION
, "%s", ds_cstr(&s
));
5099 xlate_output_action(ctx
, ofpact_get_OUTPUT(a
)->port
,
5100 ofpact_get_OUTPUT(a
)->max_len
, true);
5104 if (xlate_group_action(ctx
, ofpact_get_GROUP(a
)->group_id
)) {
5105 /* Group could not be found. */
5107 /* XXX: Terminates action list translation, but does not
5108 * terminate the pipeline. */
5113 case OFPACT_CONTROLLER
:
5114 controller
= ofpact_get_CONTROLLER(a
);
5115 if (controller
->pause
) {
5116 ctx
->pause
= controller
;
5117 ctx
->xout
->slow
|= SLOW_CONTROLLER
;
5118 ctx_trigger_freeze(ctx
);
5121 execute_controller_action(ctx
, controller
->max_len
,
5123 controller
->controller_id
,
5124 controller
->userdata
,
5125 controller
->userdata_len
);
5129 case OFPACT_ENQUEUE
:
5130 memset(&wc
->masks
.skb_priority
, 0xff,
5131 sizeof wc
->masks
.skb_priority
);
5132 xlate_enqueue_action(ctx
, ofpact_get_ENQUEUE(a
));
5135 case OFPACT_SET_VLAN_VID
:
5136 wc
->masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
5137 if (flow
->vlan_tci
& htons(VLAN_CFI
) ||
5138 ofpact_get_SET_VLAN_VID(a
)->push_vlan_if_needed
) {
5139 flow
->vlan_tci
&= ~htons(VLAN_VID_MASK
);
5140 flow
->vlan_tci
|= (htons(ofpact_get_SET_VLAN_VID(a
)->vlan_vid
)
5145 case OFPACT_SET_VLAN_PCP
:
5146 wc
->masks
.vlan_tci
|= htons(VLAN_PCP_MASK
| VLAN_CFI
);
5147 if (flow
->vlan_tci
& htons(VLAN_CFI
) ||
5148 ofpact_get_SET_VLAN_PCP(a
)->push_vlan_if_needed
) {
5149 flow
->vlan_tci
&= ~htons(VLAN_PCP_MASK
);
5150 flow
->vlan_tci
|= htons((ofpact_get_SET_VLAN_PCP(a
)->vlan_pcp
5151 << VLAN_PCP_SHIFT
) | VLAN_CFI
);
5155 case OFPACT_STRIP_VLAN
:
5156 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
5157 flow
->vlan_tci
= htons(0);
5160 case OFPACT_PUSH_VLAN
:
5161 /* XXX 802.1AD(QinQ) */
5162 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
5163 flow
->vlan_tci
= htons(VLAN_CFI
);
5166 case OFPACT_SET_ETH_SRC
:
5167 WC_MASK_FIELD(wc
, dl_src
);
5168 flow
->dl_src
= ofpact_get_SET_ETH_SRC(a
)->mac
;
5171 case OFPACT_SET_ETH_DST
:
5172 WC_MASK_FIELD(wc
, dl_dst
);
5173 flow
->dl_dst
= ofpact_get_SET_ETH_DST(a
)->mac
;
5176 case OFPACT_SET_IPV4_SRC
:
5177 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
5178 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
5179 flow
->nw_src
= ofpact_get_SET_IPV4_SRC(a
)->ipv4
;
5183 case OFPACT_SET_IPV4_DST
:
5184 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
5185 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
5186 flow
->nw_dst
= ofpact_get_SET_IPV4_DST(a
)->ipv4
;
5190 case OFPACT_SET_IP_DSCP
:
5191 if (is_ip_any(flow
)) {
5192 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
5193 flow
->nw_tos
&= ~IP_DSCP_MASK
;
5194 flow
->nw_tos
|= ofpact_get_SET_IP_DSCP(a
)->dscp
;
5198 case OFPACT_SET_IP_ECN
:
5199 if (is_ip_any(flow
)) {
5200 wc
->masks
.nw_tos
|= IP_ECN_MASK
;
5201 flow
->nw_tos
&= ~IP_ECN_MASK
;
5202 flow
->nw_tos
|= ofpact_get_SET_IP_ECN(a
)->ecn
;
5206 case OFPACT_SET_IP_TTL
:
5207 if (is_ip_any(flow
)) {
5208 wc
->masks
.nw_ttl
= 0xff;
5209 flow
->nw_ttl
= ofpact_get_SET_IP_TTL(a
)->ttl
;
5213 case OFPACT_SET_L4_SRC_PORT
:
5214 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5215 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
5216 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
5217 flow
->tp_src
= htons(ofpact_get_SET_L4_SRC_PORT(a
)->port
);
5221 case OFPACT_SET_L4_DST_PORT
:
5222 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5223 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
5224 memset(&wc
->masks
.tp_dst
, 0xff, sizeof wc
->masks
.tp_dst
);
5225 flow
->tp_dst
= htons(ofpact_get_SET_L4_DST_PORT(a
)->port
);
5229 case OFPACT_RESUBMIT
:
5230 /* Freezing complicates resubmit. Some action in the flow
5231 * entry found by resubmit might trigger freezing. If that
5232 * happens, then we do not want to execute the resubmit again after
5233 * during thawing, so we want to skip back to the head of the loop
5234 * to avoid that, only adding any actions that follow the resubmit
5235 * to the frozen actions.
5237 xlate_ofpact_resubmit(ctx
, ofpact_get_RESUBMIT(a
));
5240 case OFPACT_SET_TUNNEL
:
5241 flow
->tunnel
.tun_id
= htonll(ofpact_get_SET_TUNNEL(a
)->tun_id
);
5244 case OFPACT_SET_QUEUE
:
5245 memset(&wc
->masks
.skb_priority
, 0xff,
5246 sizeof wc
->masks
.skb_priority
);
5247 xlate_set_queue_action(ctx
, ofpact_get_SET_QUEUE(a
)->queue_id
);
5250 case OFPACT_POP_QUEUE
:
5251 memset(&wc
->masks
.skb_priority
, 0xff,
5252 sizeof wc
->masks
.skb_priority
);
5253 if (flow
->skb_priority
!= ctx
->orig_skb_priority
) {
5254 flow
->skb_priority
= ctx
->orig_skb_priority
;
5255 xlate_report(ctx
, OFT_DETAIL
, "queue = %#"PRIx32
,
5256 flow
->skb_priority
);
5260 case OFPACT_REG_MOVE
:
5261 xlate_ofpact_reg_move(ctx
, ofpact_get_REG_MOVE(a
));
5264 case OFPACT_SET_FIELD
:
5265 set_field
= ofpact_get_SET_FIELD(a
);
5266 mf
= set_field
->field
;
5268 /* Set the field only if the packet actually has it. */
5269 if (mf_are_prereqs_ok(mf
, flow
, wc
)) {
5270 mf_mask_field_masked(mf
, ofpact_set_field_mask(set_field
), wc
);
5271 mf_set_flow_value_masked(mf
, set_field
->value
,
5272 ofpact_set_field_mask(set_field
),
5275 xlate_report(ctx
, OFT_WARN
,
5276 "unmet prerequisites for %s, set_field ignored",
5282 case OFPACT_STACK_PUSH
:
5283 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a
), flow
, wc
,
5287 case OFPACT_STACK_POP
:
5288 xlate_ofpact_stack_pop(ctx
, ofpact_get_STACK_POP(a
));
5291 case OFPACT_PUSH_MPLS
:
5292 compose_mpls_push_action(ctx
, ofpact_get_PUSH_MPLS(a
));
5295 case OFPACT_POP_MPLS
:
5296 compose_mpls_pop_action(ctx
, ofpact_get_POP_MPLS(a
)->ethertype
);
5299 case OFPACT_SET_MPLS_LABEL
:
5300 compose_set_mpls_label_action(
5301 ctx
, ofpact_get_SET_MPLS_LABEL(a
)->label
);
5304 case OFPACT_SET_MPLS_TC
:
5305 compose_set_mpls_tc_action(ctx
, ofpact_get_SET_MPLS_TC(a
)->tc
);
5308 case OFPACT_SET_MPLS_TTL
:
5309 compose_set_mpls_ttl_action(ctx
, ofpact_get_SET_MPLS_TTL(a
)->ttl
);
5312 case OFPACT_DEC_MPLS_TTL
:
5313 if (compose_dec_mpls_ttl_action(ctx
)) {
5318 case OFPACT_DEC_TTL
:
5319 wc
->masks
.nw_ttl
= 0xff;
5320 if (compose_dec_ttl(ctx
, ofpact_get_DEC_TTL(a
))) {
5326 /* Nothing to do. */
5329 case OFPACT_MULTIPATH
:
5330 multipath_execute(ofpact_get_MULTIPATH(a
), flow
, wc
);
5331 xlate_report_subfield(ctx
, &ofpact_get_MULTIPATH(a
)->dst
);
5335 xlate_bundle_action(ctx
, ofpact_get_BUNDLE(a
));
5338 case OFPACT_OUTPUT_REG
:
5339 xlate_output_reg_action(ctx
, ofpact_get_OUTPUT_REG(a
));
5342 case OFPACT_OUTPUT_TRUNC
:
5343 xlate_output_trunc_action(ctx
, ofpact_get_OUTPUT_TRUNC(a
)->port
,
5344 ofpact_get_OUTPUT_TRUNC(a
)->max_len
);
5348 xlate_learn_action(ctx
, ofpact_get_LEARN(a
));
5351 case OFPACT_CONJUNCTION
:
5352 /* A flow with a "conjunction" action represents part of a special
5353 * kind of "set membership match". Such a flow should not actually
5354 * get executed, but it could via, say, a "packet-out", even though
5355 * that wouldn't be useful. Log it to help debugging. */
5356 xlate_report_error(ctx
, "executing no-op conjunction action");
5363 case OFPACT_UNROLL_XLATE
:
5364 xlate_ofpact_unroll_xlate(ctx
, ofpact_get_UNROLL_XLATE(a
));
5367 case OFPACT_FIN_TIMEOUT
:
5368 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
5369 xlate_fin_timeout(ctx
, ofpact_get_FIN_TIMEOUT(a
));
5372 case OFPACT_CLEAR_ACTIONS
:
5373 xlate_report_action_set(ctx
, "was");
5374 ofpbuf_clear(&ctx
->action_set
);
5375 ctx
->xin
->flow
.actset_output
= OFPP_UNSET
;
5376 ctx
->action_set_has_group
= false;
5379 case OFPACT_WRITE_ACTIONS
:
5380 xlate_write_actions(ctx
, ofpact_get_WRITE_ACTIONS(a
));
5381 xlate_report_action_set(ctx
, "is");
5384 case OFPACT_WRITE_METADATA
:
5385 metadata
= ofpact_get_WRITE_METADATA(a
);
5386 flow
->metadata
&= ~metadata
->mask
;
5387 flow
->metadata
|= metadata
->metadata
& metadata
->mask
;
5391 /* Not implemented yet. */
5394 case OFPACT_GOTO_TABLE
: {
5395 struct ofpact_goto_table
*ogt
= ofpact_get_GOTO_TABLE(a
);
5397 ovs_assert(ctx
->table_id
< ogt
->table_id
);
5399 xlate_table_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
,
5400 ogt
->table_id
, true, true);
5405 xlate_sample_action(ctx
, ofpact_get_SAMPLE(a
));
5409 xlate_clone(ctx
, ofpact_get_CLONE(a
));
5413 compose_conntrack_action(ctx
, ofpact_get_CT(a
));
5416 case OFPACT_CT_CLEAR
:
5417 clear_conntrack(ctx
);
5421 /* This will be processed by compose_conntrack_action(). */
5422 ctx
->ct_nat_action
= ofpact_get_NAT(a
);
5425 case OFPACT_DEBUG_RECIRC
:
5426 ctx_trigger_freeze(ctx
);
5431 /* Check if need to store this and the remaining actions for later
5433 if (!ctx
->error
&& ctx
->exit
&& ctx_first_frozen_action(ctx
)) {
5434 freeze_unroll_actions(a
, ofpact_end(ofpacts
, ofpacts_len
), ctx
);
5441 xlate_in_init(struct xlate_in
*xin
, struct ofproto_dpif
*ofproto
,
5442 ovs_version_t version
, const struct flow
*flow
,
5443 ofp_port_t in_port
, struct rule_dpif
*rule
, uint16_t tcp_flags
,
5444 const struct dp_packet
*packet
, struct flow_wildcards
*wc
,
5445 struct ofpbuf
*odp_actions
)
5447 xin
->ofproto
= ofproto
;
5448 xin
->tables_version
= version
;
5450 xin
->upcall_flow
= flow
;
5451 xin
->flow
.in_port
.ofp_port
= in_port
;
5452 xin
->flow
.actset_output
= OFPP_UNSET
;
5453 xin
->packet
= packet
;
5454 xin
->allow_side_effects
= packet
!= NULL
;
5457 xin
->ofpacts
= NULL
;
5458 xin
->ofpacts_len
= 0;
5459 xin
->tcp_flags
= tcp_flags
;
5461 xin
->resubmit_stats
= NULL
;
5465 xin
->odp_actions
= odp_actions
;
5467 /* Do recirc lookup. */
5468 xin
->frozen_state
= NULL
;
5469 if (flow
->recirc_id
) {
5470 const struct recirc_id_node
*node
5471 = recirc_id_node_find(flow
->recirc_id
);
5473 xin
->frozen_state
= &node
->state
;
5479 xlate_out_uninit(struct xlate_out
*xout
)
5482 recirc_refs_unref(&xout
->recircs
);
5486 static struct skb_priority_to_dscp
*
5487 get_skb_priority(const struct xport
*xport
, uint32_t skb_priority
)
5489 struct skb_priority_to_dscp
*pdscp
;
5492 hash
= hash_int(skb_priority
, 0);
5493 HMAP_FOR_EACH_IN_BUCKET (pdscp
, hmap_node
, hash
, &xport
->skb_priorities
) {
5494 if (pdscp
->skb_priority
== skb_priority
) {
5502 dscp_from_skb_priority(const struct xport
*xport
, uint32_t skb_priority
,
5505 struct skb_priority_to_dscp
*pdscp
= get_skb_priority(xport
, skb_priority
);
5506 *dscp
= pdscp
? pdscp
->dscp
: 0;
5507 return pdscp
!= NULL
;
5511 count_skb_priorities(const struct xport
*xport
)
5513 return hmap_count(&xport
->skb_priorities
);
5517 clear_skb_priorities(struct xport
*xport
)
5519 struct skb_priority_to_dscp
*pdscp
;
5521 HMAP_FOR_EACH_POP (pdscp
, hmap_node
, &xport
->skb_priorities
) {
5527 actions_output_to_local_port(const struct xlate_ctx
*ctx
)
5529 odp_port_t local_odp_port
= ofp_port_to_odp_port(ctx
->xbridge
, OFPP_LOCAL
);
5530 const struct nlattr
*a
;
5533 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, ctx
->odp_actions
->data
,
5534 ctx
->odp_actions
->size
) {
5535 if (nl_attr_type(a
) == OVS_ACTION_ATTR_OUTPUT
5536 && nl_attr_get_odp_port(a
) == local_odp_port
) {
5543 #if defined(__linux__)
5544 /* Returns the maximum number of packets that the Linux kernel is willing to
5545 * queue up internally to certain kinds of software-implemented ports, or the
5546 * default (and rarely modified) value if it cannot be determined. */
5548 netdev_max_backlog(void)
5550 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
5551 static int max_backlog
= 1000; /* The normal default value. */
5553 if (ovsthread_once_start(&once
)) {
5554 static const char filename
[] = "/proc/sys/net/core/netdev_max_backlog";
5558 stream
= fopen(filename
, "r");
5560 VLOG_INFO("%s: open failed (%s)", filename
, ovs_strerror(errno
));
5562 if (fscanf(stream
, "%d", &n
) != 1) {
5563 VLOG_WARN("%s: read error", filename
);
5564 } else if (n
<= 100) {
5565 VLOG_WARN("%s: unexpectedly small value %d", filename
, n
);
5571 ovsthread_once_done(&once
);
5573 VLOG_DBG("%s: using %d max_backlog", filename
, max_backlog
);
5579 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
5582 count_output_actions(const struct ofpbuf
*odp_actions
)
5584 const struct nlattr
*a
;
5588 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, odp_actions
->data
, odp_actions
->size
) {
5589 if (a
->nla_type
== OVS_ACTION_ATTR_OUTPUT
) {
5595 #endif /* defined(__linux__) */
5597 /* Returns true if 'odp_actions' contains more output actions than the datapath
5598 * can reliably handle in one go. On Linux, this is the value of the
5599 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
5600 * packets that the kernel is willing to queue up for processing while the
5601 * datapath is processing a set of actions. */
5603 too_many_output_actions(const struct ofpbuf
*odp_actions OVS_UNUSED
)
5606 return (odp_actions
->size
/ NL_A_U32_SIZE
> netdev_max_backlog()
5607 && count_output_actions(odp_actions
) > netdev_max_backlog());
5609 /* OSes other than Linux might have similar limits, but we don't know how
5610 * to determine them.*/
5616 xlate_wc_init(struct xlate_ctx
*ctx
)
5618 flow_wildcards_init_catchall(ctx
->wc
);
5620 /* Some fields we consider to always be examined. */
5621 WC_MASK_FIELD(ctx
->wc
, in_port
);
5622 WC_MASK_FIELD(ctx
->wc
, dl_type
);
5623 if (is_ip_any(&ctx
->xin
->flow
)) {
5624 WC_MASK_FIELD_MASK(ctx
->wc
, nw_frag
, FLOW_NW_FRAG_MASK
);
5627 if (ctx
->xbridge
->support
.odp
.recirc
) {
5628 /* Always exactly match recirc_id when datapath supports
5630 WC_MASK_FIELD(ctx
->wc
, recirc_id
);
5633 if (ctx
->xbridge
->netflow
) {
5634 netflow_mask_wc(&ctx
->xin
->flow
, ctx
->wc
);
5637 tnl_wc_init(&ctx
->xin
->flow
, ctx
->wc
);
5641 xlate_wc_finish(struct xlate_ctx
*ctx
)
5643 /* Clear the metadata and register wildcard masks, because we won't
5644 * use non-header fields as part of the cache. */
5645 flow_wildcards_clear_non_packet_fields(ctx
->wc
);
5647 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
5648 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
5649 * represent these fields. The datapath interface, on the other hand,
5650 * represents them with just 8 bits each. This means that if the high
5651 * 8 bits of the masks for these fields somehow become set, then they
5652 * will get chopped off by a round trip through the datapath, and
5653 * revalidation will spot that as an inconsistency and delete the flow.
5654 * Avoid the problem here by making sure that only the low 8 bits of
5655 * either field can be unwildcarded for ICMP.
5657 if (is_icmpv4(&ctx
->xin
->flow
, NULL
) || is_icmpv6(&ctx
->xin
->flow
, NULL
)) {
5658 ctx
->wc
->masks
.tp_src
&= htons(UINT8_MAX
);
5659 ctx
->wc
->masks
.tp_dst
&= htons(UINT8_MAX
);
5661 /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
5662 if (ctx
->wc
->masks
.vlan_tci
) {
5663 ctx
->wc
->masks
.vlan_tci
|= htons(VLAN_CFI
);
5666 /* The classifier might return masks that match on tp_src and tp_dst even
5667 * for later fragments. This happens because there might be flows that
5668 * match on tp_src or tp_dst without matching on the frag bits, because
5669 * it is not a prerequisite for OpenFlow. Since it is a prerequisite for
5670 * datapath flows and since tp_src and tp_dst are always going to be 0,
5671 * wildcard the fields here. */
5672 if (ctx
->xin
->flow
.nw_frag
& FLOW_NW_FRAG_LATER
) {
5673 ctx
->wc
->masks
.tp_src
= 0;
5674 ctx
->wc
->masks
.tp_dst
= 0;
5678 /* Translates the flow, actions, or rule in 'xin' into datapath actions in
5680 * The caller must take responsibility for eventually freeing 'xout', with
5681 * xlate_out_uninit().
5682 * Returns 'XLATE_OK' if translation was successful. In case of an error an
5683 * empty set of actions will be returned in 'xin->odp_actions' (if non-NULL),
5684 * so that most callers may ignore the return value and transparently install a
5685 * drop flow when the translation fails. */
5687 xlate_actions(struct xlate_in
*xin
, struct xlate_out
*xout
)
5689 *xout
= (struct xlate_out
) {
5691 .recircs
= RECIRC_REFS_EMPTY_INITIALIZER
,
5694 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
5695 struct xbridge
*xbridge
= xbridge_lookup(xcfg
, xin
->ofproto
);
5697 return XLATE_BRIDGE_NOT_FOUND
;
5700 struct flow
*flow
= &xin
->flow
;
5702 uint8_t stack_stub
[1024];
5703 uint64_t action_set_stub
[1024 / 8];
5704 uint64_t frozen_actions_stub
[1024 / 8];
5705 uint64_t actions_stub
[256 / 8];
5706 struct ofpbuf scratch_actions
= OFPBUF_STUB_INITIALIZER(actions_stub
);
5707 struct xlate_ctx ctx
= {
5711 .orig_tunnel_ipv6_dst
= flow_tnl_dst(&flow
->tunnel
),
5713 .stack
= OFPBUF_STUB_INITIALIZER(stack_stub
),
5717 : &(struct flow_wildcards
) { .masks
= { .dl_type
= 0 } }),
5718 .odp_actions
= xin
->odp_actions
? xin
->odp_actions
: &scratch_actions
,
5720 .depth
= xin
->depth
,
5721 .resubmits
= xin
->resubmits
,
5723 .in_action_set
= false,
5726 .rule_cookie
= OVS_BE64_MAX
,
5727 .orig_skb_priority
= flow
->skb_priority
,
5728 .sflow_n_outputs
= 0,
5729 .sflow_odp_port
= 0,
5730 .nf_output_iface
= NF_OUT_DROP
,
5736 .recirc_update_dp_hash
= false,
5737 .frozen_actions
= OFPBUF_STUB_INITIALIZER(frozen_actions_stub
),
5741 .conntracked
= false,
5743 .ct_nat_action
= NULL
,
5745 .action_set_has_group
= false,
5746 .action_set
= OFPBUF_STUB_INITIALIZER(action_set_stub
),
5749 /* 'base_flow' reflects the packet as it came in, but we need it to reflect
5750 * the packet as the datapath will treat it for output actions. Our
5751 * datapath doesn't retain tunneling information without us re-setting
5752 * it, so clear the tunnel data.
5755 memset(&ctx
.base_flow
.tunnel
, 0, sizeof ctx
.base_flow
.tunnel
);
5757 ofpbuf_reserve(ctx
.odp_actions
, NL_A_U32_SIZE
);
5758 xlate_wc_init(&ctx
);
5760 COVERAGE_INC(xlate_actions
);
5762 xin
->trace
= xlate_report(&ctx
, OFT_BRIDGE
, "bridge(\"%s\")",
5764 if (xin
->frozen_state
) {
5765 const struct frozen_state
*state
= xin
->frozen_state
;
5767 struct ovs_list
*old_trace
= xin
->trace
;
5768 xin
->trace
= xlate_report(&ctx
, OFT_THAW
, "thaw");
5770 if (xin
->ofpacts_len
> 0 || ctx
.rule
) {
5771 xlate_report_error(&ctx
, "Recirculation conflict (%s)!",
5772 xin
->ofpacts_len
? "actions" : "rule");
5773 ctx
.error
= XLATE_RECIRCULATION_CONFLICT
;
5777 /* Set the bridge for post-recirculation processing if needed. */
5778 if (!uuid_equals(&ctx
.xbridge
->ofproto
->uuid
, &state
->ofproto_uuid
)) {
5779 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
5780 const struct xbridge
*new_bridge
5781 = xbridge_lookup_by_uuid(xcfg
, &state
->ofproto_uuid
);
5783 if (OVS_UNLIKELY(!new_bridge
)) {
5784 /* Drop the packet if the bridge cannot be found. */
5785 xlate_report_error(&ctx
, "Frozen bridge no longer exists.");
5786 ctx
.error
= XLATE_BRIDGE_NOT_FOUND
;
5787 xin
->trace
= old_trace
;
5790 ctx
.xbridge
= new_bridge
;
5791 /* The bridge is now known so obtain its table version. */
5792 ctx
.xin
->tables_version
5793 = ofproto_dpif_get_tables_version(ctx
.xbridge
->ofproto
);
5796 /* Set the thawed table id. Note: A table lookup is done only if there
5797 * are no frozen actions. */
5798 ctx
.table_id
= state
->table_id
;
5799 xlate_report(&ctx
, OFT_THAW
,
5800 "Resuming from table %"PRIu8
, ctx
.table_id
);
5802 if (!state
->conntracked
) {
5803 clear_conntrack(&ctx
);
5806 /* Restore pipeline metadata. May change flow's in_port and other
5807 * metadata to the values that existed when freezing was triggered. */
5808 frozen_metadata_to_flow(&state
->metadata
, flow
);
5810 /* Restore stack, if any. */
5812 ofpbuf_put(&ctx
.stack
, state
->stack
, state
->stack_size
);
5815 /* Restore mirror state. */
5816 ctx
.mirrors
= state
->mirrors
;
5818 /* Restore action set, if any. */
5819 if (state
->action_set_len
) {
5820 xlate_report_actions(&ctx
, OFT_THAW
, "Restoring action set",
5821 state
->action_set
, state
->action_set_len
);
5823 flow
->actset_output
= OFPP_UNSET
;
5824 xlate_write_actions__(&ctx
, state
->action_set
,
5825 state
->action_set_len
);
5828 /* Restore frozen actions. If there are no actions, processing will
5829 * start with a lookup in the table set above. */
5830 xin
->ofpacts
= state
->ofpacts
;
5831 xin
->ofpacts_len
= state
->ofpacts_len
;
5832 if (state
->ofpacts_len
) {
5833 xlate_report_actions(&ctx
, OFT_THAW
, "Restoring actions",
5834 xin
->ofpacts
, xin
->ofpacts_len
);
5837 xin
->trace
= old_trace
;
5838 } else if (OVS_UNLIKELY(flow
->recirc_id
)) {
5839 xlate_report_error(&ctx
,
5840 "Recirculation context not found for ID %"PRIx32
,
5842 ctx
.error
= XLATE_NO_RECIRCULATION_CONTEXT
;
5846 /* Tunnel metadata in udpif format must be normalized before translation. */
5847 if (flow
->tunnel
.flags
& FLOW_TNL_F_UDPIF
) {
5848 const struct tun_table
*tun_tab
= ofproto_get_tun_tab(
5849 &ctx
.xbridge
->ofproto
->up
);
5852 err
= tun_metadata_from_geneve_udpif(tun_tab
, &xin
->upcall_flow
->tunnel
,
5853 &xin
->upcall_flow
->tunnel
,
5856 xlate_report_error(&ctx
, "Invalid Geneve tunnel metadata");
5857 ctx
.error
= XLATE_INVALID_TUNNEL_METADATA
;
5860 } else if (!flow
->tunnel
.metadata
.tab
) {
5861 /* If the original flow did not come in on a tunnel, then it won't have
5862 * FLOW_TNL_F_UDPIF set. However, we still need to have a metadata
5863 * table in case we generate tunnel actions. */
5864 flow
->tunnel
.metadata
.tab
= ofproto_get_tun_tab(
5865 &ctx
.xbridge
->ofproto
->up
);
5867 ctx
.wc
->masks
.tunnel
.metadata
.tab
= flow
->tunnel
.metadata
.tab
;
5869 if (!xin
->ofpacts
&& !ctx
.rule
) {
5870 ctx
.rule
= rule_dpif_lookup_from_table(
5871 ctx
.xbridge
->ofproto
, ctx
.xin
->tables_version
, flow
, ctx
.wc
,
5872 ctx
.xin
->resubmit_stats
, &ctx
.table_id
,
5873 flow
->in_port
.ofp_port
, true, true, ctx
.xin
->xcache
);
5874 if (ctx
.xin
->resubmit_stats
) {
5875 rule_dpif_credit_stats(ctx
.rule
, ctx
.xin
->resubmit_stats
);
5877 if (ctx
.xin
->xcache
) {
5878 struct xc_entry
*entry
;
5880 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_RULE
);
5881 entry
->rule
= ctx
.rule
;
5882 ofproto_rule_ref(&ctx
.rule
->up
);
5885 xlate_report_table(&ctx
, ctx
.rule
, ctx
.table_id
);
5888 /* Get the proximate input port of the packet. (If xin->frozen_state,
5889 * flow->in_port is the ultimate input port of the packet.) */
5890 struct xport
*in_port
= get_ofp_port(xbridge
,
5891 ctx
.base_flow
.in_port
.ofp_port
);
5893 /* Tunnel stats only for not-thawed packets. */
5894 if (!xin
->frozen_state
&& in_port
&& in_port
->is_tunnel
) {
5895 if (ctx
.xin
->resubmit_stats
) {
5896 netdev_vport_inc_rx(in_port
->netdev
, ctx
.xin
->resubmit_stats
);
5898 bfd_account_rx(in_port
->bfd
, ctx
.xin
->resubmit_stats
);
5901 if (ctx
.xin
->xcache
) {
5902 struct xc_entry
*entry
;
5904 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_NETDEV
);
5905 entry
->dev
.rx
= netdev_ref(in_port
->netdev
);
5906 entry
->dev
.bfd
= bfd_ref(in_port
->bfd
);
5910 if (!xin
->frozen_state
&& process_special(&ctx
, in_port
)) {
5911 /* process_special() did all the processing for this packet.
5913 * We do not perform special processing on thawed packets, since that
5914 * was done before they were frozen and should not be redone. */
5915 } else if (in_port
&& in_port
->xbundle
5916 && xbundle_mirror_out(xbridge
, in_port
->xbundle
)) {
5917 xlate_report_error(&ctx
, "dropping packet received on port "
5918 "%s, which is reserved exclusively for mirroring",
5919 in_port
->xbundle
->name
);
5921 /* Sampling is done on initial reception; don't redo after thawing. */
5922 unsigned int user_cookie_offset
= 0;
5923 if (!xin
->frozen_state
) {
5924 user_cookie_offset
= compose_sflow_action(&ctx
);
5925 compose_ipfix_action(&ctx
, ODPP_NONE
);
5927 size_t sample_actions_len
= ctx
.odp_actions
->size
;
5929 if (tnl_process_ecn(flow
)
5930 && (!in_port
|| may_receive(in_port
, &ctx
))) {
5931 const struct ofpact
*ofpacts
;
5935 ofpacts
= xin
->ofpacts
;
5936 ofpacts_len
= xin
->ofpacts_len
;
5937 } else if (ctx
.rule
) {
5938 const struct rule_actions
*actions
5939 = rule_get_actions(&ctx
.rule
->up
);
5940 ofpacts
= actions
->ofpacts
;
5941 ofpacts_len
= actions
->ofpacts_len
;
5942 ctx
.rule_cookie
= ctx
.rule
->up
.flow_cookie
;
5947 mirror_ingress_packet(&ctx
);
5948 do_xlate_actions(ofpacts
, ofpacts_len
, &ctx
);
5953 /* We've let OFPP_NORMAL and the learning action look at the
5954 * packet, so cancel all actions and freezing if forwarding is
5956 if (in_port
&& (!xport_stp_forward_state(in_port
) ||
5957 !xport_rstp_forward_state(in_port
))) {
5958 ctx
.odp_actions
->size
= sample_actions_len
;
5959 ctx_cancel_freeze(&ctx
);
5960 ofpbuf_clear(&ctx
.action_set
);
5963 if (!ctx
.freezing
) {
5964 xlate_action_set(&ctx
);
5967 finish_freezing(&ctx
);
5971 /* Output only fully processed packets. */
5973 && xbridge
->has_in_band
5974 && in_band_must_output_to_local_port(flow
)
5975 && !actions_output_to_local_port(&ctx
)) {
5976 compose_output_action(&ctx
, OFPP_LOCAL
, NULL
);
5979 if (user_cookie_offset
) {
5980 fix_sflow_action(&ctx
, user_cookie_offset
);
5984 if (nl_attr_oversized(ctx
.odp_actions
->size
)) {
5985 /* These datapath actions are too big for a Netlink attribute, so we
5986 * can't hand them to the kernel directly. dpif_execute() can execute
5987 * them one by one with help, so just mark the result as SLOW_ACTION to
5988 * prevent the flow from being installed. */
5989 COVERAGE_INC(xlate_actions_oversize
);
5990 ctx
.xout
->slow
|= SLOW_ACTION
;
5991 } else if (too_many_output_actions(ctx
.odp_actions
)) {
5992 COVERAGE_INC(xlate_actions_too_many_output
);
5993 ctx
.xout
->slow
|= SLOW_ACTION
;
5996 /* Do netflow only for packets on initial reception, that are not sent to
5997 * the controller. We consider packets sent to the controller to be part
5998 * of the control plane rather than the data plane. */
5999 if (!xin
->frozen_state
6001 && !(xout
->slow
& SLOW_CONTROLLER
)) {
6002 if (ctx
.xin
->resubmit_stats
) {
6003 netflow_flow_update(xbridge
->netflow
, flow
,
6004 ctx
.nf_output_iface
,
6005 ctx
.xin
->resubmit_stats
);
6007 if (ctx
.xin
->xcache
) {
6008 struct xc_entry
*entry
;
6010 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_NETFLOW
);
6011 entry
->nf
.netflow
= netflow_ref(xbridge
->netflow
);
6012 entry
->nf
.flow
= xmemdup(flow
, sizeof *flow
);
6013 entry
->nf
.iface
= ctx
.nf_output_iface
;
6017 /* Translate tunnel metadata masks to udpif format if necessary. */
6018 if (xin
->upcall_flow
->tunnel
.flags
& FLOW_TNL_F_UDPIF
) {
6019 if (ctx
.wc
->masks
.tunnel
.metadata
.present
.map
) {
6020 const struct flow_tnl
*upcall_tnl
= &xin
->upcall_flow
->tunnel
;
6021 struct geneve_opt opts
[TLV_TOT_OPT_SIZE
/
6022 sizeof(struct geneve_opt
)];
6024 tun_metadata_to_geneve_udpif_mask(&flow
->tunnel
,
6025 &ctx
.wc
->masks
.tunnel
,
6026 upcall_tnl
->metadata
.opts
.gnv
,
6027 upcall_tnl
->metadata
.present
.len
,
6029 memset(&ctx
.wc
->masks
.tunnel
.metadata
, 0,
6030 sizeof ctx
.wc
->masks
.tunnel
.metadata
);
6031 memcpy(&ctx
.wc
->masks
.tunnel
.metadata
.opts
.gnv
, opts
,
6032 upcall_tnl
->metadata
.present
.len
);
6034 ctx
.wc
->masks
.tunnel
.metadata
.present
.len
= 0xff;
6035 ctx
.wc
->masks
.tunnel
.metadata
.tab
= NULL
;
6036 ctx
.wc
->masks
.tunnel
.flags
|= FLOW_TNL_F_UDPIF
;
6037 } else if (!xin
->upcall_flow
->tunnel
.metadata
.tab
) {
6038 /* If we didn't have options in UDPIF format and didn't have an existing
6039 * metadata table, then it means that there were no options at all when
6040 * we started processing and any wildcards we picked up were from
6041 * action generation. Without options on the incoming packet, wildcards
6042 * aren't meaningful. To avoid them possibly getting misinterpreted,
6043 * just clear everything. */
6044 if (ctx
.wc
->masks
.tunnel
.metadata
.present
.map
) {
6045 memset(&ctx
.wc
->masks
.tunnel
.metadata
, 0,
6046 sizeof ctx
.wc
->masks
.tunnel
.metadata
);
6048 ctx
.wc
->masks
.tunnel
.metadata
.tab
= NULL
;
6052 xlate_wc_finish(&ctx
);
6055 /* Reset the table to what it was when we came in. If we only fetched
6056 * it locally, then it has no meaning outside of flow translation. */
6057 flow
->tunnel
.metadata
.tab
= xin
->upcall_flow
->tunnel
.metadata
.tab
;
6059 ofpbuf_uninit(&ctx
.stack
);
6060 ofpbuf_uninit(&ctx
.action_set
);
6061 ofpbuf_uninit(&ctx
.frozen_actions
);
6062 ofpbuf_uninit(&scratch_actions
);
6064 /* Make sure we return a "drop flow" in case of an error. */
6067 if (xin
->odp_actions
) {
6068 ofpbuf_clear(xin
->odp_actions
);
6075 xlate_resume(struct ofproto_dpif
*ofproto
,
6076 const struct ofputil_packet_in_private
*pin
,
6077 struct ofpbuf
*odp_actions
,
6078 enum slow_path_reason
*slow
)
6080 struct dp_packet packet
;
6081 dp_packet_use_const(&packet
, pin
->public.packet
,
6082 pin
->public.packet_len
);
6085 flow_extract(&packet
, &flow
);
6087 struct xlate_in xin
;
6088 xlate_in_init(&xin
, ofproto
, ofproto_dpif_get_tables_version(ofproto
),
6089 &flow
, 0, NULL
, ntohs(flow
.tcp_flags
),
6090 &packet
, NULL
, odp_actions
);
6092 struct ofpact_note noop
;
6093 ofpact_init_NOTE(&noop
);
6096 bool any_actions
= pin
->actions_len
> 0;
6097 struct frozen_state state
= {
6098 .table_id
= 0, /* Not the table where NXAST_PAUSE was executed. */
6099 .ofproto_uuid
= pin
->bridge
,
6100 .stack
= pin
->stack
,
6101 .stack_size
= pin
->stack_size
,
6102 .mirrors
= pin
->mirrors
,
6103 .conntracked
= pin
->conntracked
,
6105 /* When there are no actions, xlate_actions() will search the flow
6106 * table. We don't want it to do that (we want it to resume), so
6107 * supply a no-op action if there aren't any.
6109 * (We can't necessarily avoid translating actions entirely if there
6110 * aren't any actions, because there might be some finishing-up to do
6111 * at the end of the pipeline, and we don't check for those
6113 .ofpacts
= any_actions
? pin
->actions
: &noop
.ofpact
,
6114 .ofpacts_len
= any_actions
? pin
->actions_len
: sizeof noop
,
6116 .action_set
= pin
->action_set
,
6117 .action_set_len
= pin
->action_set_len
,
6119 frozen_metadata_from_flow(&state
.metadata
,
6120 &pin
->public.flow_metadata
.flow
);
6121 xin
.frozen_state
= &state
;
6123 struct xlate_out xout
;
6124 enum xlate_error error
= xlate_actions(&xin
, &xout
);
6126 xlate_out_uninit(&xout
);
6128 /* xlate_actions() can generate a number of errors, but only
6129 * XLATE_BRIDGE_NOT_FOUND really stands out to me as one that we should be
6130 * sure to report over OpenFlow. The others could come up in packet-outs
6131 * or regular flow translation and I don't think that it's going to be too
6132 * useful to report them to the controller. */
6133 return error
== XLATE_BRIDGE_NOT_FOUND
? OFPERR_NXR_STALE
: 0;
6136 /* Sends 'packet' out 'ofport'. If 'port' is a tunnel and that tunnel type
6137 * supports a notion of an OAM flag, sets it if 'oam' is true.
6138 * May modify 'packet'.
6139 * Returns 0 if successful, otherwise a positive errno value. */
6141 xlate_send_packet(const struct ofport_dpif
*ofport
, bool oam
,
6142 struct dp_packet
*packet
)
6144 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
6145 struct xport
*xport
;
6146 uint64_t ofpacts_stub
[1024 / 8];
6147 struct ofpbuf ofpacts
;
6150 ofpbuf_use_stack(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
6151 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
6152 flow_extract(packet
, &flow
);
6153 flow
.in_port
.ofp_port
= OFPP_NONE
;
6155 xport
= xport_lookup(xcfg
, ofport
);
6161 const ovs_be16 oam
= htons(NX_TUN_FLAG_OAM
);
6162 ofpact_put_set_field(&ofpacts
, mf_from_id(MFF_TUN_FLAGS
), &oam
, &oam
);
6165 ofpact_put_OUTPUT(&ofpacts
)->port
= xport
->ofp_port
;
6167 /* Actions here are not referring to anything versionable (flow tables or
6168 * groups) so we don't need to worry about the version here. */
6169 return ofproto_dpif_execute_actions(xport
->xbridge
->ofproto
,
6170 OVS_VERSION_MAX
, &flow
, NULL
,
6171 ofpacts
.data
, ofpacts
.size
, packet
);
6175 xlate_mac_learning_update(const struct ofproto_dpif
*ofproto
,
6176 ofp_port_t in_port
, struct eth_addr dl_src
,
6177 int vlan
, bool is_grat_arp
)
6179 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
6180 struct xbridge
*xbridge
;
6181 struct xbundle
*xbundle
;
6183 xbridge
= xbridge_lookup(xcfg
, ofproto
);
6188 xbundle
= lookup_input_bundle__(xbridge
, in_port
, NULL
);
6193 update_learning_table__(xbridge
, xbundle
, dl_src
, vlan
, is_grat_arp
);
6197 xlate_disable_dp_clone(const struct ofproto_dpif
*ofproto
)
6199 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
6200 struct xbridge
*xbridge
= xbridge_lookup(xcfg
, ofproto
);
6203 xbridge
->support
.clone
= false;