1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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>
25 #include "tnl-neigh-cache.h"
30 #include "byte-order.h"
34 #include "dp-packet.h"
36 #include "dynamic-string.h"
42 #include "mac-learning.h"
43 #include "mcast-snooping.h"
44 #include "meta-flow.h"
45 #include "multipath.h"
46 #include "netdev-vport.h"
49 #include "odp-execute.h"
50 #include "ofp-actions.h"
51 #include "ofproto/ofproto-dpif-ipfix.h"
52 #include "ofproto/ofproto-dpif-mirror.h"
53 #include "ofproto/ofproto-dpif-monitor.h"
54 #include "ofproto/ofproto-dpif-sflow.h"
55 #include "ofproto/ofproto-dpif.h"
56 #include "ofproto/ofproto-provider.h"
58 #include "ovs-router.h"
59 #include "tnl-ports.h"
61 #include "openvswitch/vlog.h"
63 COVERAGE_DEFINE(xlate_actions);
64 COVERAGE_DEFINE(xlate_actions_oversize);
65 COVERAGE_DEFINE(xlate_actions_too_many_output);
67 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
69 /* Maximum depth of flow table recursion (due to resubmit actions) in a
70 * flow translation. */
71 #define MAX_RESUBMIT_RECURSION 64
72 #define MAX_INTERNAL_RESUBMITS 1 /* Max resbmits allowed using rules in
75 /* Maximum number of resubmit actions in a flow translation, whether they are
76 * recursive or not. */
77 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
80 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
81 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
83 struct ovs_list xbundles; /* Owned xbundles. */
84 struct hmap xports; /* Indexed by ofp_port. */
86 char *name; /* Name used in log messages. */
87 struct dpif *dpif; /* Datapath interface. */
88 struct mac_learning *ml; /* Mac learning handle. */
89 struct mcast_snooping *ms; /* Multicast Snooping handle. */
90 struct mbridge *mbridge; /* Mirroring. */
91 struct dpif_sflow *sflow; /* SFlow handle, or null. */
92 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
93 struct netflow *netflow; /* Netflow handle, or null. */
94 struct stp *stp; /* STP or null if disabled. */
95 struct rstp *rstp; /* RSTP or null if disabled. */
97 bool has_in_band; /* Bridge has in band control? */
98 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
100 /* Datapath feature support. */
101 struct dpif_backer_support support;
105 struct hmap_node hmap_node; /* In global 'xbundles' map. */
106 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
108 struct ovs_list list_node; /* In parent 'xbridges' list. */
109 struct xbridge *xbridge; /* Parent xbridge. */
111 struct ovs_list xports; /* Contains "struct xport"s. */
113 char *name; /* Name used in log messages. */
114 struct bond *bond; /* Nonnull iff more than one port. */
115 struct lacp *lacp; /* LACP handle or null. */
117 enum port_vlan_mode vlan_mode; /* VLAN mode. */
118 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
119 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
120 * NULL if all VLANs are trunked. */
121 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
122 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
126 struct hmap_node hmap_node; /* Node in global 'xports' map. */
127 struct ofport_dpif *ofport; /* Key in global 'xports map. */
129 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
130 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
132 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
134 struct ovs_list bundle_node; /* In parent xbundle (if it exists). */
135 struct xbundle *xbundle; /* Parent xbundle or null. */
137 struct netdev *netdev; /* 'ofport''s netdev. */
139 struct xbridge *xbridge; /* Parent bridge. */
140 struct xport *peer; /* Patch port peer or null. */
142 enum ofputil_port_config config; /* OpenFlow port configuration. */
143 enum ofputil_port_state state; /* OpenFlow port state. */
144 int stp_port_no; /* STP port number or -1 if not in use. */
145 struct rstp_port *rstp_port; /* RSTP port or null. */
147 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
149 bool may_enable; /* May be enabled in bonds. */
150 bool is_tunnel; /* Is a tunnel port. */
152 struct cfm *cfm; /* CFM handle or null. */
153 struct bfd *bfd; /* BFD handle or null. */
154 struct lldp *lldp; /* LLDP handle or null. */
158 struct xlate_in *xin;
159 struct xlate_out *xout;
161 const struct xbridge *xbridge;
163 /* Flow tables version at the beginning of the translation. */
164 cls_version_t tables_version;
166 /* Flow at the last commit. */
167 struct flow base_flow;
169 /* Tunnel IP destination address as received. This is stored separately
170 * as the base_flow.tunnel is cleared on init to reflect the datapath
171 * behavior. Used to make sure not to send tunneled output to ourselves,
172 * which might lead to an infinite loop. This could happen easily
173 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
174 * actually set the tun_dst field. */
175 struct in6_addr orig_tunnel_ipv6_dst;
177 /* Stack for the push and pop actions. Each stack element is of type
178 * "union mf_subvalue". */
181 /* The rule that we are currently translating, or NULL. */
182 struct rule_dpif *rule;
184 /* Flow translation populates this with wildcards relevant in translation.
185 * When 'xin->wc' is nonnull, this is the same pointer. When 'xin->wc' is
186 * null, this is a pointer to uninitialized scratch memory. This allows
187 * code to blindly write to 'ctx->wc' without worrying about whether the
188 * caller really wants wildcards. */
189 struct flow_wildcards *wc;
191 /* Output buffer for datapath actions. When 'xin->odp_actions' is nonnull,
192 * this is the same pointer. When 'xin->odp_actions' is null, this points
193 * to a scratch ofpbuf. This allows code to add actions to
194 * 'ctx->odp_actions' without worrying about whether the caller really
196 struct ofpbuf *odp_actions;
198 /* Resubmit statistics, via xlate_table_action(). */
199 int recurse; /* Current resubmit nesting depth. */
200 int resubmits; /* Total number of resubmits. */
201 bool in_group; /* Currently translating ofgroup, if true. */
202 bool in_action_set; /* Currently translating action_set, if true. */
204 uint8_t table_id; /* OpenFlow table ID where flow was found. */
205 ovs_be64 rule_cookie; /* Cookie of the rule being translated. */
206 uint32_t orig_skb_priority; /* Priority when packet arrived. */
207 uint32_t sflow_n_outputs; /* Number of output ports. */
208 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
209 ofp_port_t nf_output_iface; /* Output interface index for NetFlow. */
210 bool exit; /* No further actions should be processed. */
211 mirror_mask_t mirrors; /* Bitmap of associated mirrors. */
213 /* Freezing Translation
214 * ====================
216 * At some point during translation, the code may recognize the need to halt
217 * and checkpoint the translation in a way that it can be restarted again
218 * later. We call the checkpointing process "freezing" and the restarting
221 * The use cases for freezing are:
223 * - "Recirculation", where the translation process discovers that it
224 * doesn't have enough information to complete translation without
225 * actually executing the actions that have already been translated,
226 * which provides the additionally needed information. In these
227 * situations, translation freezes translation and assigns the frozen
228 * data a unique "recirculation ID", which it associates with the data
229 * in a table in userspace (see ofproto-dpif-rid.h). It also adds a
230 * OVS_ACTION_ATTR_RECIRC action specifying that ID to the datapath
231 * actions. When a packet hits that action, the datapath looks its
232 * flow up again using the ID. If there's a miss, it comes back to
233 * userspace, which find the recirculation table entry for the ID,
234 * thaws the associated frozen data, and continues translation from
235 * that point given the additional information that is now known.
237 * The archetypal example is MPLS. As MPLS is implemented in
238 * OpenFlow, the protocol that follows the last MPLS label becomes
239 * known only when that label is popped by an OpenFlow action. That
240 * means that Open vSwitch can't extract the headers beyond the MPLS
241 * labels until the pop action is executed. Thus, at that point
242 * translation uses the recirculation process to extract the headers
243 * beyond the MPLS labels.
245 * (OVS also uses OVS_ACTION_ATTR_RECIRC to implement hashing for
246 * output to bonds. OVS pre-populates all the datapath flows for bond
247 * output in the datapath, though, which means that the elaborate
248 * process of coming back to userspace for a second round of
249 * translation isn't needed, and so bonds don't follow the above
252 * - "Continuation". A continuation is a way for an OpenFlow controller
253 * to interpose on a packet's traversal of the OpenFlow tables. When
254 * the translation process encounters a "controller" action with the
255 * "pause" flag, it freezes translation, serializes the frozen data,
256 * and sends it to an OpenFlow controller. The controller then
257 * examines and possibly modifies the frozen data and eventually sends
258 * it back to the switch, which thaws it and continues translation.
260 * The main problem of freezing translation is preserving state, so that
261 * when the translation is thawed later it resumes from where it left off,
262 * without disruption. In particular, actions must be preserved as follows:
264 * - If we're freezing because an action needed more information, the
265 * action that prompted it.
267 * - Any actions remaining to be translated within the current flow.
269 * - If translation was frozen within a NXAST_RESUBMIT, then any actions
270 * following the resubmit action. Resubmit actions can be nested, so
271 * this has to go all the way up the control stack.
273 * - The OpenFlow 1.1+ action set.
275 * State that actions and flow table lookups can depend on, such as the
276 * following, must also be preserved:
278 * - Metadata fields (input port, registers, OF1.1+ metadata, ...).
280 * - The stack used by NXAST_STACK_PUSH and NXAST_STACK_POP actions.
282 * - The table ID and cookie of the flow being translated at each level
283 * of the control stack, because these can become visible through
284 * OFPAT_CONTROLLER actions (and other ways).
286 * Translation allows for the control of this state preservation via these
287 * members. When a need to freeze translation is identified, the
288 * translation process:
290 * 1. Sets 'freezing' to true.
292 * 2. Sets 'exit' to true to tell later steps that we're exiting from the
293 * translation process.
295 * 3. Adds an OFPACT_UNROLL_XLATE action to 'frozen_actions', and points
296 * frozen_actions.header to the action to make it easy to find it later.
297 * This action holds the current table ID and cookie so that they can be
298 * restored during a post-recirculation upcall translation.
300 * 4. Adds the action that prompted recirculation and any actions following
301 * it within the same flow to 'frozen_actions', so that they can be
302 * executed during a post-recirculation upcall translation.
306 * 6. The action that prompted recirculation might be nested in a stack of
307 * nested "resubmit"s that have actions remaining. Each of these notices
308 * that we're exiting and freezing and responds by adding more
309 * OFPACT_UNROLL_XLATE actions to 'frozen_actions', as necessary,
310 * followed by any actions that were yet unprocessed.
312 * If we're freezing because of recirculation, the caller generates a
313 * recirculation ID and associates all the state produced by this process
314 * with it. For post-recirculation upcall translation, the caller passes it
315 * back in for the new translation to execute. The process yielded a set of
316 * ofpacts that can be translated directly, so it is not much of a special
317 * case at that point.
320 struct ofpbuf frozen_actions;
321 const struct ofpact_controller *pause;
323 /* True if conntrack has been performed on this packet during processing
324 * on the current bridge. This is used to determine whether conntrack
325 * state from the datapath should be honored after thawing. */
328 /* Pointer to an embedded NAT action in a conntrack action, or NULL. */
329 struct ofpact_nat *ct_nat_action;
331 /* OpenFlow 1.1+ action set.
333 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
334 * When translation is otherwise complete, ofpacts_execute_action_set()
335 * converts it to a set of "struct ofpact"s that can be translated into
336 * datapath actions. */
337 bool action_set_has_group; /* Action set contains OFPACT_GROUP? */
338 struct ofpbuf action_set; /* Action set. */
340 enum xlate_error error; /* Translation failed. */
343 const char *xlate_strerror(enum xlate_error error)
348 case XLATE_BRIDGE_NOT_FOUND:
349 return "Bridge not found";
350 case XLATE_RECURSION_TOO_DEEP:
351 return "Recursion too deep";
352 case XLATE_TOO_MANY_RESUBMITS:
353 return "Too many resubmits";
354 case XLATE_STACK_TOO_DEEP:
355 return "Stack too deep";
356 case XLATE_NO_RECIRCULATION_CONTEXT:
357 return "No recirculation context";
358 case XLATE_RECIRCULATION_CONFLICT:
359 return "Recirculation conflict";
360 case XLATE_TOO_MANY_MPLS_LABELS:
361 return "Too many MPLS labels";
363 return "Unknown error";
366 static void xlate_action_set(struct xlate_ctx *ctx);
367 static void xlate_commit_actions(struct xlate_ctx *ctx);
370 ctx_trigger_freeze(struct xlate_ctx *ctx)
373 ctx->freezing = true;
377 ctx_first_frozen_action(const struct xlate_ctx *ctx)
379 return !ctx->frozen_actions.size;
383 ctx_cancel_freeze(struct xlate_ctx *ctx)
386 ctx->freezing = false;
387 ofpbuf_clear(&ctx->frozen_actions);
388 ctx->frozen_actions.header = NULL;
392 static void finish_freezing(struct xlate_ctx *ctx);
394 /* A controller may use OFPP_NONE as the ingress port to indicate that
395 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
396 * when an input bundle is needed for validation (e.g., mirroring or
397 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
398 * any 'port' structs, so care must be taken when dealing with it. */
399 static struct xbundle ofpp_none_bundle = {
401 .vlan_mode = PORT_VLAN_TRUNK
404 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
405 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
406 * traffic egressing the 'ofport' with that priority should be marked with. */
407 struct skb_priority_to_dscp {
408 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
409 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
411 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
427 /* xlate_cache entries hold enough information to perform the side effects of
428 * xlate_actions() for a rule, without needing to perform rule translation
429 * from scratch. The primary usage of these is to submit statistics to objects
430 * that a flow relates to, although they may be used for other effects as well
431 * (for instance, refreshing hard timeouts for learned flows). */
435 struct rule_dpif *rule;
442 struct netflow *netflow;
447 struct mbridge *mbridge;
448 mirror_mask_t mirrors;
456 struct ofproto_dpif *ofproto;
457 struct ofputil_flow_mod *fm;
458 struct ofpbuf *ofpacts;
461 struct ofproto_dpif *ofproto;
466 struct rule_dpif *rule;
471 struct group_dpif *group;
472 struct ofputil_bucket *bucket;
475 char br_name[IFNAMSIZ];
476 struct in6_addr d_ipv6;
481 #define XC_ENTRY_FOR_EACH(ENTRY, ENTRIES, XCACHE) \
482 ENTRIES = XCACHE->entries; \
483 for (ENTRY = ofpbuf_try_pull(&ENTRIES, sizeof *ENTRY); \
485 ENTRY = ofpbuf_try_pull(&ENTRIES, sizeof *ENTRY))
488 struct ofpbuf entries;
491 /* Xlate config contains hash maps of all bridges, bundles and ports.
492 * Xcfgp contains the pointer to the current xlate configuration.
493 * When the main thread needs to change the configuration, it copies xcfgp to
494 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
495 * does not block handler and revalidator threads. */
497 struct hmap xbridges;
498 struct hmap xbundles;
501 static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
502 static struct xlate_cfg *new_xcfg = NULL;
504 static bool may_receive(const struct xport *, struct xlate_ctx *);
505 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
507 static void xlate_normal(struct xlate_ctx *);
508 static inline void xlate_report(struct xlate_ctx *, const char *, ...)
509 OVS_PRINTF_FORMAT(2, 3);
510 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
511 uint8_t table_id, bool may_packet_in,
512 bool honor_table_miss);
513 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
514 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
515 static void output_normal(struct xlate_ctx *, const struct xbundle *,
518 /* Optional bond recirculation parameter to compose_output_action(). */
519 struct xlate_bond_recirc {
520 uint32_t recirc_id; /* !0 Use recirculation instead of output. */
521 uint8_t hash_alg; /* !0 Compute hash for recirc before. */
522 uint32_t hash_basis; /* Compute hash for recirc before. */
525 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port,
526 const struct xlate_bond_recirc *xr);
528 static struct xbridge *xbridge_lookup(struct xlate_cfg *,
529 const struct ofproto_dpif *);
530 static struct xbridge *xbridge_lookup_by_uuid(struct xlate_cfg *,
531 const struct uuid *);
532 static struct xbundle *xbundle_lookup(struct xlate_cfg *,
533 const struct ofbundle *);
534 static struct xport *xport_lookup(struct xlate_cfg *,
535 const struct ofport_dpif *);
536 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
537 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
538 uint32_t skb_priority);
539 static void clear_skb_priorities(struct xport *);
540 static size_t count_skb_priorities(const struct xport *);
541 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
544 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
546 static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
547 static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
548 static void xlate_xport_init(struct xlate_cfg *, struct xport *);
549 static void xlate_xbridge_set(struct xbridge *, struct dpif *,
550 const struct mac_learning *, struct stp *,
551 struct rstp *, const struct mcast_snooping *,
552 const struct mbridge *,
553 const struct dpif_sflow *,
554 const struct dpif_ipfix *,
555 const struct netflow *,
556 bool forward_bpdu, bool has_in_band,
557 const struct dpif_backer_support *);
558 static void xlate_xbundle_set(struct xbundle *xbundle,
559 enum port_vlan_mode vlan_mode, int vlan,
560 unsigned long *trunks, bool use_priority_tags,
561 const struct bond *bond, const struct lacp *lacp,
563 static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
564 const struct netdev *netdev, const struct cfm *cfm,
565 const struct bfd *bfd, const struct lldp *lldp,
566 int stp_port_no, const struct rstp_port *rstp_port,
567 enum ofputil_port_config config,
568 enum ofputil_port_state state, bool is_tunnel,
570 static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
571 static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
572 static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
573 static void xlate_xbridge_copy(struct xbridge *);
574 static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
575 static void xlate_xport_copy(struct xbridge *, struct xbundle *,
577 static void xlate_xcfg_free(struct xlate_cfg *);
580 xlate_report(struct xlate_ctx *ctx, const char *format, ...)
582 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
585 va_start(args, format);
586 ctx->xin->report_hook(ctx->xin, ctx->recurse, format, args);
591 static struct vlog_rate_limit error_report_rl = VLOG_RATE_LIMIT_INIT(1, 5);
593 #define XLATE_REPORT_ERROR(CTX, ...) \
595 if (OVS_UNLIKELY((CTX)->xin->report_hook)) { \
596 xlate_report(CTX, __VA_ARGS__); \
598 VLOG_ERR_RL(&error_report_rl, __VA_ARGS__); \
603 xlate_report_actions(struct xlate_ctx *ctx, const char *title,
604 const struct ofpact *ofpacts, size_t ofpacts_len)
606 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
607 struct ds s = DS_EMPTY_INITIALIZER;
608 ofpacts_format(ofpacts, ofpacts_len, &s);
609 xlate_report(ctx, "%s: %s", title, ds_cstr(&s));
615 xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
617 list_init(&xbridge->xbundles);
618 hmap_init(&xbridge->xports);
619 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
620 hash_pointer(xbridge->ofproto, 0));
624 xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
626 list_init(&xbundle->xports);
627 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
628 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
629 hash_pointer(xbundle->ofbundle, 0));
633 xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
635 hmap_init(&xport->skb_priorities);
636 hmap_insert(&xcfg->xports, &xport->hmap_node,
637 hash_pointer(xport->ofport, 0));
638 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
639 hash_ofp_port(xport->ofp_port));
643 xlate_xbridge_set(struct xbridge *xbridge,
645 const struct mac_learning *ml, struct stp *stp,
646 struct rstp *rstp, const struct mcast_snooping *ms,
647 const struct mbridge *mbridge,
648 const struct dpif_sflow *sflow,
649 const struct dpif_ipfix *ipfix,
650 const struct netflow *netflow,
651 bool forward_bpdu, bool has_in_band,
652 const struct dpif_backer_support *support)
654 if (xbridge->ml != ml) {
655 mac_learning_unref(xbridge->ml);
656 xbridge->ml = mac_learning_ref(ml);
659 if (xbridge->ms != ms) {
660 mcast_snooping_unref(xbridge->ms);
661 xbridge->ms = mcast_snooping_ref(ms);
664 if (xbridge->mbridge != mbridge) {
665 mbridge_unref(xbridge->mbridge);
666 xbridge->mbridge = mbridge_ref(mbridge);
669 if (xbridge->sflow != sflow) {
670 dpif_sflow_unref(xbridge->sflow);
671 xbridge->sflow = dpif_sflow_ref(sflow);
674 if (xbridge->ipfix != ipfix) {
675 dpif_ipfix_unref(xbridge->ipfix);
676 xbridge->ipfix = dpif_ipfix_ref(ipfix);
679 if (xbridge->stp != stp) {
680 stp_unref(xbridge->stp);
681 xbridge->stp = stp_ref(stp);
684 if (xbridge->rstp != rstp) {
685 rstp_unref(xbridge->rstp);
686 xbridge->rstp = rstp_ref(rstp);
689 if (xbridge->netflow != netflow) {
690 netflow_unref(xbridge->netflow);
691 xbridge->netflow = netflow_ref(netflow);
694 xbridge->dpif = dpif;
695 xbridge->forward_bpdu = forward_bpdu;
696 xbridge->has_in_band = has_in_band;
697 xbridge->support = *support;
701 xlate_xbundle_set(struct xbundle *xbundle,
702 enum port_vlan_mode vlan_mode, int vlan,
703 unsigned long *trunks, bool use_priority_tags,
704 const struct bond *bond, const struct lacp *lacp,
707 ovs_assert(xbundle->xbridge);
709 xbundle->vlan_mode = vlan_mode;
710 xbundle->vlan = vlan;
711 xbundle->trunks = trunks;
712 xbundle->use_priority_tags = use_priority_tags;
713 xbundle->floodable = floodable;
715 if (xbundle->bond != bond) {
716 bond_unref(xbundle->bond);
717 xbundle->bond = bond_ref(bond);
720 if (xbundle->lacp != lacp) {
721 lacp_unref(xbundle->lacp);
722 xbundle->lacp = lacp_ref(lacp);
727 xlate_xport_set(struct xport *xport, odp_port_t odp_port,
728 const struct netdev *netdev, const struct cfm *cfm,
729 const struct bfd *bfd, const struct lldp *lldp, int stp_port_no,
730 const struct rstp_port* rstp_port,
731 enum ofputil_port_config config, enum ofputil_port_state state,
732 bool is_tunnel, bool may_enable)
734 xport->config = config;
735 xport->state = state;
736 xport->stp_port_no = stp_port_no;
737 xport->is_tunnel = is_tunnel;
738 xport->may_enable = may_enable;
739 xport->odp_port = odp_port;
741 if (xport->rstp_port != rstp_port) {
742 rstp_port_unref(xport->rstp_port);
743 xport->rstp_port = rstp_port_ref(rstp_port);
746 if (xport->cfm != cfm) {
747 cfm_unref(xport->cfm);
748 xport->cfm = cfm_ref(cfm);
751 if (xport->bfd != bfd) {
752 bfd_unref(xport->bfd);
753 xport->bfd = bfd_ref(bfd);
756 if (xport->lldp != lldp) {
757 lldp_unref(xport->lldp);
758 xport->lldp = lldp_ref(lldp);
761 if (xport->netdev != netdev) {
762 netdev_close(xport->netdev);
763 xport->netdev = netdev_ref(netdev);
768 xlate_xbridge_copy(struct xbridge *xbridge)
770 struct xbundle *xbundle;
772 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
773 new_xbridge->ofproto = xbridge->ofproto;
774 new_xbridge->name = xstrdup(xbridge->name);
775 xlate_xbridge_init(new_xcfg, new_xbridge);
777 xlate_xbridge_set(new_xbridge,
778 xbridge->dpif, xbridge->ml, xbridge->stp,
779 xbridge->rstp, xbridge->ms, xbridge->mbridge,
780 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
781 xbridge->forward_bpdu, xbridge->has_in_band,
783 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
784 xlate_xbundle_copy(new_xbridge, xbundle);
787 /* Copy xports which are not part of a xbundle */
788 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
789 if (!xport->xbundle) {
790 xlate_xport_copy(new_xbridge, NULL, xport);
796 xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
799 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
800 new_xbundle->ofbundle = xbundle->ofbundle;
801 new_xbundle->xbridge = xbridge;
802 new_xbundle->name = xstrdup(xbundle->name);
803 xlate_xbundle_init(new_xcfg, new_xbundle);
805 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
806 xbundle->vlan, xbundle->trunks,
807 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
809 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
810 xlate_xport_copy(xbridge, new_xbundle, xport);
815 xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
818 struct skb_priority_to_dscp *pdscp, *new_pdscp;
819 struct xport *new_xport = xzalloc(sizeof *xport);
820 new_xport->ofport = xport->ofport;
821 new_xport->ofp_port = xport->ofp_port;
822 new_xport->xbridge = xbridge;
823 xlate_xport_init(new_xcfg, new_xport);
825 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
826 xport->bfd, xport->lldp, xport->stp_port_no,
827 xport->rstp_port, xport->config, xport->state,
828 xport->is_tunnel, xport->may_enable);
831 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
833 new_xport->peer = peer;
834 new_xport->peer->peer = new_xport;
839 new_xport->xbundle = xbundle;
840 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
843 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
844 new_pdscp = xmalloc(sizeof *pdscp);
845 new_pdscp->skb_priority = pdscp->skb_priority;
846 new_pdscp->dscp = pdscp->dscp;
847 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
848 hash_int(new_pdscp->skb_priority, 0));
852 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
853 * configuration in xcfgp.
855 * This needs to be called after editing the xlate configuration.
857 * Functions that edit the new xlate configuration are
858 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
864 * edit_xlate_configuration();
866 * xlate_txn_commit(); */
868 xlate_txn_commit(void)
870 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
872 ovsrcu_set(&xcfgp, new_xcfg);
873 ovsrcu_synchronize();
874 xlate_xcfg_free(xcfg);
878 /* Copies the current xlate configuration in xcfgp to new_xcfg.
880 * This needs to be called prior to editing the xlate configuration. */
882 xlate_txn_start(void)
884 struct xbridge *xbridge;
885 struct xlate_cfg *xcfg;
887 ovs_assert(!new_xcfg);
889 new_xcfg = xmalloc(sizeof *new_xcfg);
890 hmap_init(&new_xcfg->xbridges);
891 hmap_init(&new_xcfg->xbundles);
892 hmap_init(&new_xcfg->xports);
894 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
899 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
900 xlate_xbridge_copy(xbridge);
906 xlate_xcfg_free(struct xlate_cfg *xcfg)
908 struct xbridge *xbridge, *next_xbridge;
914 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
915 xlate_xbridge_remove(xcfg, xbridge);
918 hmap_destroy(&xcfg->xbridges);
919 hmap_destroy(&xcfg->xbundles);
920 hmap_destroy(&xcfg->xports);
925 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
927 const struct mac_learning *ml, struct stp *stp,
928 struct rstp *rstp, const struct mcast_snooping *ms,
929 const struct mbridge *mbridge,
930 const struct dpif_sflow *sflow,
931 const struct dpif_ipfix *ipfix,
932 const struct netflow *netflow,
933 bool forward_bpdu, bool has_in_band,
934 const struct dpif_backer_support *support)
936 struct xbridge *xbridge;
938 ovs_assert(new_xcfg);
940 xbridge = xbridge_lookup(new_xcfg, ofproto);
942 xbridge = xzalloc(sizeof *xbridge);
943 xbridge->ofproto = ofproto;
945 xlate_xbridge_init(new_xcfg, xbridge);
949 xbridge->name = xstrdup(name);
951 xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
952 netflow, forward_bpdu, has_in_band, support);
956 xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
958 struct xbundle *xbundle, *next_xbundle;
959 struct xport *xport, *next_xport;
965 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
966 xlate_xport_remove(xcfg, xport);
969 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
970 xlate_xbundle_remove(xcfg, xbundle);
973 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
974 mac_learning_unref(xbridge->ml);
975 mcast_snooping_unref(xbridge->ms);
976 mbridge_unref(xbridge->mbridge);
977 dpif_sflow_unref(xbridge->sflow);
978 dpif_ipfix_unref(xbridge->ipfix);
979 stp_unref(xbridge->stp);
980 rstp_unref(xbridge->rstp);
981 hmap_destroy(&xbridge->xports);
987 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
989 struct xbridge *xbridge;
991 ovs_assert(new_xcfg);
993 xbridge = xbridge_lookup(new_xcfg, ofproto);
994 xlate_xbridge_remove(new_xcfg, xbridge);
998 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
999 const char *name, enum port_vlan_mode vlan_mode, int vlan,
1000 unsigned long *trunks, bool use_priority_tags,
1001 const struct bond *bond, const struct lacp *lacp,
1004 struct xbundle *xbundle;
1006 ovs_assert(new_xcfg);
1008 xbundle = xbundle_lookup(new_xcfg, ofbundle);
1010 xbundle = xzalloc(sizeof *xbundle);
1011 xbundle->ofbundle = ofbundle;
1012 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
1014 xlate_xbundle_init(new_xcfg, xbundle);
1017 free(xbundle->name);
1018 xbundle->name = xstrdup(name);
1020 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
1021 use_priority_tags, bond, lacp, floodable);
1025 xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
1027 struct xport *xport;
1033 LIST_FOR_EACH_POP (xport, bundle_node, &xbundle->xports) {
1034 xport->xbundle = NULL;
1037 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
1038 list_remove(&xbundle->list_node);
1039 bond_unref(xbundle->bond);
1040 lacp_unref(xbundle->lacp);
1041 free(xbundle->name);
1046 xlate_bundle_remove(struct ofbundle *ofbundle)
1048 struct xbundle *xbundle;
1050 ovs_assert(new_xcfg);
1052 xbundle = xbundle_lookup(new_xcfg, ofbundle);
1053 xlate_xbundle_remove(new_xcfg, xbundle);
1057 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
1058 struct ofport_dpif *ofport, ofp_port_t ofp_port,
1059 odp_port_t odp_port, const struct netdev *netdev,
1060 const struct cfm *cfm, const struct bfd *bfd,
1061 const struct lldp *lldp, struct ofport_dpif *peer,
1062 int stp_port_no, const struct rstp_port *rstp_port,
1063 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
1064 enum ofputil_port_config config,
1065 enum ofputil_port_state state, bool is_tunnel,
1069 struct xport *xport;
1071 ovs_assert(new_xcfg);
1073 xport = xport_lookup(new_xcfg, ofport);
1075 xport = xzalloc(sizeof *xport);
1076 xport->ofport = ofport;
1077 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
1078 xport->ofp_port = ofp_port;
1080 xlate_xport_init(new_xcfg, xport);
1083 ovs_assert(xport->ofp_port == ofp_port);
1085 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, lldp,
1086 stp_port_no, rstp_port, config, state, is_tunnel,
1090 xport->peer->peer = NULL;
1092 xport->peer = xport_lookup(new_xcfg, peer);
1094 xport->peer->peer = xport;
1097 if (xport->xbundle) {
1098 list_remove(&xport->bundle_node);
1100 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
1101 if (xport->xbundle) {
1102 list_insert(&xport->xbundle->xports, &xport->bundle_node);
1105 clear_skb_priorities(xport);
1106 for (i = 0; i < n_qdscp; i++) {
1107 struct skb_priority_to_dscp *pdscp;
1108 uint32_t skb_priority;
1110 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
1115 pdscp = xmalloc(sizeof *pdscp);
1116 pdscp->skb_priority = skb_priority;
1117 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1118 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
1119 hash_int(pdscp->skb_priority, 0));
1124 xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
1131 xport->peer->peer = NULL;
1135 if (xport->xbundle) {
1136 list_remove(&xport->bundle_node);
1139 clear_skb_priorities(xport);
1140 hmap_destroy(&xport->skb_priorities);
1142 hmap_remove(&xcfg->xports, &xport->hmap_node);
1143 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
1145 netdev_close(xport->netdev);
1146 rstp_port_unref(xport->rstp_port);
1147 cfm_unref(xport->cfm);
1148 bfd_unref(xport->bfd);
1149 lldp_unref(xport->lldp);
1154 xlate_ofport_remove(struct ofport_dpif *ofport)
1156 struct xport *xport;
1158 ovs_assert(new_xcfg);
1160 xport = xport_lookup(new_xcfg, ofport);
1161 xlate_xport_remove(new_xcfg, xport);
1164 static struct ofproto_dpif *
1165 xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
1166 ofp_port_t *ofp_in_port, const struct xport **xportp)
1168 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1169 const struct xport *xport;
1171 xport = xport_lookup(xcfg, tnl_port_should_receive(flow)
1172 ? tnl_port_receive(flow)
1173 : odp_port_to_ofport(backer, flow->in_port.odp_port));
1174 if (OVS_UNLIKELY(!xport)) {
1179 *ofp_in_port = xport->ofp_port;
1181 return xport->xbridge->ofproto;
1184 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1185 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1186 struct ofproto_dpif *
1187 xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1188 ofp_port_t *ofp_in_port)
1190 const struct xport *xport;
1192 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1195 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1196 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
1197 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1198 * handles for those protocols if they're enabled. Caller may use the returned
1199 * pointers until quiescing, for longer term use additional references must
1202 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1205 xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1206 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1207 struct dpif_sflow **sflow, struct netflow **netflow,
1208 ofp_port_t *ofp_in_port)
1210 struct ofproto_dpif *ofproto;
1211 const struct xport *xport;
1213 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1220 *ofprotop = ofproto;
1224 *ipfix = xport ? xport->xbridge->ipfix : NULL;
1228 *sflow = xport ? xport->xbridge->sflow : NULL;
1232 *netflow = xport ? xport->xbridge->netflow : NULL;
1238 static struct xbridge *
1239 xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
1241 struct hmap *xbridges;
1242 struct xbridge *xbridge;
1244 if (!ofproto || !xcfg) {
1248 xbridges = &xcfg->xbridges;
1250 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
1252 if (xbridge->ofproto == ofproto) {
1259 static struct xbridge *
1260 xbridge_lookup_by_uuid(struct xlate_cfg *xcfg, const struct uuid *uuid)
1262 struct xbridge *xbridge;
1264 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
1265 if (uuid_equals(ofproto_dpif_get_uuid(xbridge->ofproto), uuid)) {
1272 static struct xbundle *
1273 xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
1275 struct hmap *xbundles;
1276 struct xbundle *xbundle;
1278 if (!ofbundle || !xcfg) {
1282 xbundles = &xcfg->xbundles;
1284 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
1286 if (xbundle->ofbundle == ofbundle) {
1293 static struct xport *
1294 xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
1296 struct hmap *xports;
1297 struct xport *xport;
1299 if (!ofport || !xcfg) {
1303 xports = &xcfg->xports;
1305 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
1307 if (xport->ofport == ofport) {
1314 static struct stp_port *
1315 xport_get_stp_port(const struct xport *xport)
1317 return xport->xbridge->stp && xport->stp_port_no != -1
1318 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1323 xport_stp_learn_state(const struct xport *xport)
1325 struct stp_port *sp = xport_get_stp_port(xport);
1327 ? stp_learn_in_state(stp_port_get_state(sp))
1332 xport_stp_forward_state(const struct xport *xport)
1334 struct stp_port *sp = xport_get_stp_port(xport);
1336 ? stp_forward_in_state(stp_port_get_state(sp))
1341 xport_stp_should_forward_bpdu(const struct xport *xport)
1343 struct stp_port *sp = xport_get_stp_port(xport);
1344 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
1347 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1348 * were used to make the determination.*/
1350 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1352 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1353 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1354 return is_stp(flow);
1358 stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1360 struct stp_port *sp = xport_get_stp_port(xport);
1361 struct dp_packet payload = *packet;
1362 struct eth_header *eth = dp_packet_data(&payload);
1364 /* Sink packets on ports that have STP disabled when the bridge has
1366 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1370 /* Trim off padding on payload. */
1371 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1372 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1375 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1376 stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
1380 static enum rstp_state
1381 xport_get_rstp_port_state(const struct xport *xport)
1383 return xport->rstp_port
1384 ? rstp_port_get_state(xport->rstp_port)
1389 xport_rstp_learn_state(const struct xport *xport)
1391 return xport->xbridge->rstp && xport->rstp_port
1392 ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1397 xport_rstp_forward_state(const struct xport *xport)
1399 return xport->xbridge->rstp && xport->rstp_port
1400 ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1405 xport_rstp_should_manage_bpdu(const struct xport *xport)
1407 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
1411 rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1413 struct dp_packet payload = *packet;
1414 struct eth_header *eth = dp_packet_data(&payload);
1416 /* Sink packets on ports that have no RSTP. */
1417 if (!xport->rstp_port) {
1421 /* Trim off padding on payload. */
1422 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1423 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1426 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1427 rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
1428 dp_packet_size(&payload));
1432 static struct xport *
1433 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1435 struct xport *xport;
1437 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1439 if (xport->ofp_port == ofp_port) {
1447 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1449 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1450 return xport ? xport->odp_port : ODPP_NONE;
1454 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1456 struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1457 return xport && xport->may_enable;
1460 static struct ofputil_bucket *
1461 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1465 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1467 struct group_dpif *group;
1469 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1470 struct ofputil_bucket *bucket;
1472 bucket = group_first_live_bucket(ctx, group, depth);
1473 group_dpif_unref(group);
1474 return bucket == NULL;
1480 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1483 bucket_is_alive(const struct xlate_ctx *ctx,
1484 struct ofputil_bucket *bucket, int depth)
1486 if (depth >= MAX_LIVENESS_RECURSION) {
1487 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1489 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1490 MAX_LIVENESS_RECURSION);
1494 return (!ofputil_bucket_has_liveness(bucket)
1495 || (bucket->watch_port != OFPP_ANY
1496 && odp_port_is_alive(ctx, bucket->watch_port))
1497 || (bucket->watch_group != OFPG_ANY
1498 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
1501 static struct ofputil_bucket *
1502 group_first_live_bucket(const struct xlate_ctx *ctx,
1503 const struct group_dpif *group, int depth)
1505 struct ofputil_bucket *bucket;
1506 const struct ovs_list *buckets;
1508 group_dpif_get_buckets(group, &buckets);
1509 LIST_FOR_EACH (bucket, list_node, buckets) {
1510 if (bucket_is_alive(ctx, bucket, depth)) {
1518 static struct ofputil_bucket *
1519 group_best_live_bucket(const struct xlate_ctx *ctx,
1520 const struct group_dpif *group,
1523 struct ofputil_bucket *best_bucket = NULL;
1524 uint32_t best_score = 0;
1527 struct ofputil_bucket *bucket;
1528 const struct ovs_list *buckets;
1530 group_dpif_get_buckets(group, &buckets);
1531 LIST_FOR_EACH (bucket, list_node, buckets) {
1532 if (bucket_is_alive(ctx, bucket, 0)) {
1533 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
1534 if (score >= best_score) {
1535 best_bucket = bucket;
1546 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
1548 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1549 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1553 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1555 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1558 static mirror_mask_t
1559 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1561 return xbundle != &ofpp_none_bundle
1562 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1566 static mirror_mask_t
1567 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
1569 return xbundle != &ofpp_none_bundle
1570 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1574 static mirror_mask_t
1575 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
1577 return xbundle != &ofpp_none_bundle
1578 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1582 static struct xbundle *
1583 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1584 bool warn, struct xport **in_xportp)
1586 struct xport *xport;
1588 /* Find the port and bundle for the received packet. */
1589 xport = get_ofp_port(xbridge, in_port);
1593 if (xport && xport->xbundle) {
1594 return xport->xbundle;
1597 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1598 * which a controller may use as the ingress port for traffic that
1599 * it is sourcing. */
1600 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
1601 return &ofpp_none_bundle;
1604 /* Odd. A few possible reasons here:
1606 * - We deleted a port but there are still a few packets queued up
1609 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1610 * we don't know about.
1612 * - The ofproto client didn't configure the port as part of a bundle.
1613 * This is particularly likely to happen if a packet was received on the
1614 * port after it was created, but before the client had a chance to
1615 * configure its bundle.
1618 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1620 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1621 "port %"PRIu16, xbridge->name, in_port);
1626 /* Mirrors the packet represented by 'ctx' to appropriate mirror destinations,
1627 * given the packet is ingressing or egressing on 'xbundle', which has ingress
1628 * or egress (as appropriate) mirrors 'mirrors'. */
1630 mirror_packet(struct xlate_ctx *ctx, struct xbundle *xbundle,
1631 mirror_mask_t mirrors)
1633 /* Figure out what VLAN the packet is in (because mirrors can select
1634 * packets on basis of VLAN). */
1635 bool warn = ctx->xin->packet != NULL;
1636 uint16_t vid = vlan_tci_to_vid(ctx->xin->flow.vlan_tci);
1637 if (!input_vid_is_valid(vid, xbundle, warn)) {
1640 uint16_t vlan = input_vid_to_vlan(xbundle, vid);
1642 const struct xbridge *xbridge = ctx->xbridge;
1644 /* Don't mirror to destinations that we've already mirrored to. */
1645 mirrors &= ~ctx->mirrors;
1650 if (ctx->xin->resubmit_stats) {
1651 mirror_update_stats(xbridge->mbridge, mirrors,
1652 ctx->xin->resubmit_stats->n_packets,
1653 ctx->xin->resubmit_stats->n_bytes);
1655 if (ctx->xin->xcache) {
1656 struct xc_entry *entry;
1658 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_MIRROR);
1659 entry->u.mirror.mbridge = mbridge_ref(xbridge->mbridge);
1660 entry->u.mirror.mirrors = mirrors;
1663 /* 'mirrors' is a bit-mask of candidates for mirroring. Iterate as long as
1664 * some candidates remain. */
1666 const unsigned long *vlans;
1667 mirror_mask_t dup_mirrors;
1668 struct ofbundle *out;
1671 /* Get the details of the mirror represented by the rightmost 1-bit. */
1672 bool has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1673 &vlans, &dup_mirrors, &out, &out_vlan);
1674 ovs_assert(has_mirror);
1676 /* If this mirror selects on the basis of VLAN, and it does not select
1677 * 'vlan', then discard this mirror and go on to the next one. */
1679 ctx->wc->masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1681 if (vlans && !bitmap_is_set(vlans, vlan)) {
1682 mirrors = zero_rightmost_1bit(mirrors);
1686 /* Record the mirror, and the mirrors that output to the same
1687 * destination, so that we don't mirror to them again. This must be
1688 * done now to ensure that output_normal(), below, doesn't recursively
1689 * output to the same mirrors. */
1690 ctx->mirrors |= dup_mirrors;
1692 /* Send the packet to the mirror. */
1694 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1695 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1697 output_normal(ctx, out_xbundle, vlan);
1699 } else if (vlan != out_vlan
1700 && !eth_addr_is_reserved(ctx->xin->flow.dl_dst)) {
1701 struct xbundle *xbundle;
1703 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1704 if (xbundle_includes_vlan(xbundle, out_vlan)
1705 && !xbundle_mirror_out(xbridge, xbundle)) {
1706 output_normal(ctx, xbundle, out_vlan);
1711 /* output_normal() could have recursively output (to different
1712 * mirrors), so make sure that we don't send duplicates. */
1713 mirrors &= ~ctx->mirrors;
1718 mirror_ingress_packet(struct xlate_ctx *ctx)
1720 if (mbridge_has_mirrors(ctx->xbridge->mbridge)) {
1721 bool warn = ctx->xin->packet != NULL;
1722 struct xbundle *xbundle = lookup_input_bundle(
1723 ctx->xbridge, ctx->xin->flow.in_port.ofp_port, warn, NULL);
1725 mirror_packet(ctx, xbundle,
1726 xbundle_mirror_src(ctx->xbridge, xbundle));
1731 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1732 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1733 * the bundle on which the packet was received, returns the VLAN to which the
1736 * Both 'vid' and the return value are in the range 0...4095. */
1738 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1740 switch (in_xbundle->vlan_mode) {
1741 case PORT_VLAN_ACCESS:
1742 return in_xbundle->vlan;
1745 case PORT_VLAN_TRUNK:
1748 case PORT_VLAN_NATIVE_UNTAGGED:
1749 case PORT_VLAN_NATIVE_TAGGED:
1750 return vid ? vid : in_xbundle->vlan;
1757 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1758 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1761 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1762 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1765 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1767 /* Allow any VID on the OFPP_NONE port. */
1768 if (in_xbundle == &ofpp_none_bundle) {
1772 switch (in_xbundle->vlan_mode) {
1773 case PORT_VLAN_ACCESS:
1776 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1777 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1778 "packet received on port %s configured as VLAN "
1779 "%"PRIu16" access port", vid, in_xbundle->name,
1786 case PORT_VLAN_NATIVE_UNTAGGED:
1787 case PORT_VLAN_NATIVE_TAGGED:
1789 /* Port must always carry its native VLAN. */
1793 case PORT_VLAN_TRUNK:
1794 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1796 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1797 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1798 "received on port %s not configured for trunking "
1799 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1811 /* Given 'vlan', the VLAN that a packet belongs to, and
1812 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1813 * that should be included in the 802.1Q header. (If the return value is 0,
1814 * then the 802.1Q header should only be included in the packet if there is a
1817 * Both 'vlan' and the return value are in the range 0...4095. */
1819 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1821 switch (out_xbundle->vlan_mode) {
1822 case PORT_VLAN_ACCESS:
1825 case PORT_VLAN_TRUNK:
1826 case PORT_VLAN_NATIVE_TAGGED:
1829 case PORT_VLAN_NATIVE_UNTAGGED:
1830 return vlan == out_xbundle->vlan ? 0 : vlan;
1838 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1841 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1843 ovs_be16 tci, old_tci;
1844 struct xport *xport;
1845 struct xlate_bond_recirc xr;
1846 bool use_recirc = false;
1848 vid = output_vlan_to_vid(out_xbundle, vlan);
1849 if (list_is_empty(&out_xbundle->xports)) {
1850 /* Partially configured bundle with no slaves. Drop the packet. */
1852 } else if (!out_xbundle->bond) {
1853 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1856 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1857 struct flow_wildcards *wc = ctx->wc;
1858 struct ofport_dpif *ofport;
1860 if (ctx->xbridge->support.odp.recirc) {
1861 use_recirc = bond_may_recirc(
1862 out_xbundle->bond, &xr.recirc_id, &xr.hash_basis);
1865 /* Only TCP mode uses recirculation. */
1866 xr.hash_alg = OVS_HASH_ALG_L4;
1867 bond_update_post_recirc_rules(out_xbundle->bond, false);
1869 /* Recirculation does not require unmasking hash fields. */
1874 ofport = bond_choose_output_slave(out_xbundle->bond,
1875 &ctx->xin->flow, wc, vid);
1876 xport = xport_lookup(xcfg, ofport);
1879 /* No slaves enabled, so drop packet. */
1883 /* If use_recirc is set, the main thread will handle stats
1884 * accounting for this bond. */
1886 if (ctx->xin->resubmit_stats) {
1887 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1888 ctx->xin->resubmit_stats->n_bytes);
1890 if (ctx->xin->xcache) {
1891 struct xc_entry *entry;
1894 flow = &ctx->xin->flow;
1895 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1896 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1897 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1898 entry->u.bond.vid = vid;
1903 old_tci = *flow_tci;
1905 if (tci || out_xbundle->use_priority_tags) {
1906 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1908 tci |= htons(VLAN_CFI);
1913 compose_output_action(ctx, xport->ofp_port, use_recirc ? &xr : NULL);
1914 *flow_tci = old_tci;
1917 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1918 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1919 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1921 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1923 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1927 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1928 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1932 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1933 if (flow->nw_proto == ARP_OP_REPLY) {
1935 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1936 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1937 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1939 return flow->nw_src == flow->nw_dst;
1945 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1946 * dropped. Returns true if they may be forwarded, false if they should be
1949 * 'in_port' must be the xport that corresponds to flow->in_port.
1950 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1952 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1953 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1954 * checked by input_vid_is_valid().
1956 * May also add tags to '*tags', although the current implementation only does
1957 * so in one special case.
1960 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1963 struct xbundle *in_xbundle = in_port->xbundle;
1964 const struct xbridge *xbridge = ctx->xbridge;
1965 struct flow *flow = &ctx->xin->flow;
1967 /* Drop frames for reserved multicast addresses
1968 * only if forward_bpdu option is absent. */
1969 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1970 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1974 if (in_xbundle->bond) {
1975 struct mac_entry *mac;
1977 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1983 xlate_report(ctx, "bonding refused admissibility, dropping");
1986 case BV_DROP_IF_MOVED:
1987 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1988 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1990 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
1991 && (!is_gratuitous_arp(flow, ctx->wc)
1992 || mac_entry_is_grat_arp_locked(mac))) {
1993 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1994 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1998 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2006 /* Checks whether a MAC learning update is necessary for MAC learning table
2007 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
2010 * Most packets processed through the MAC learning table do not actually
2011 * change it in any way. This function requires only a read lock on the MAC
2012 * learning table, so it is much cheaper in this common case.
2014 * Keep the code here synchronized with that in update_learning_table__()
2017 is_mac_learning_update_needed(const struct mac_learning *ml,
2018 const struct flow *flow,
2019 struct flow_wildcards *wc,
2020 int vlan, struct xbundle *in_xbundle)
2021 OVS_REQ_RDLOCK(ml->rwlock)
2023 struct mac_entry *mac;
2025 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
2029 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
2030 if (!mac || mac_entry_age(ml, mac)) {
2034 if (is_gratuitous_arp(flow, wc)) {
2035 /* We don't want to learn from gratuitous ARP packets that are
2036 * reflected back over bond slaves so we lock the learning table. */
2037 if (!in_xbundle->bond) {
2039 } else if (mac_entry_is_grat_arp_locked(mac)) {
2044 return mac_entry_get_port(ml, mac) != in_xbundle->ofbundle;
2048 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
2049 * received on 'in_xbundle' in 'vlan'.
2051 * This code repeats all the checks in is_mac_learning_update_needed() because
2052 * the lock was released between there and here and thus the MAC learning state
2053 * could have changed.
2055 * Keep the code here synchronized with that in is_mac_learning_update_needed()
2058 update_learning_table__(const struct xbridge *xbridge,
2059 const struct flow *flow, struct flow_wildcards *wc,
2060 int vlan, struct xbundle *in_xbundle)
2061 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
2063 struct mac_entry *mac;
2065 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
2069 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
2070 if (is_gratuitous_arp(flow, wc)) {
2071 /* We don't want to learn from gratuitous ARP packets that are
2072 * reflected back over bond slaves so we lock the learning table. */
2073 if (!in_xbundle->bond) {
2074 mac_entry_set_grat_arp_lock(mac);
2075 } else if (mac_entry_is_grat_arp_locked(mac)) {
2080 if (mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle) {
2081 /* The log messages here could actually be useful in debugging,
2082 * so keep the rate limit relatively high. */
2083 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
2085 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
2086 "on port %s in VLAN %d",
2087 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
2088 in_xbundle->name, vlan);
2090 mac_entry_set_port(xbridge->ml, mac, in_xbundle->ofbundle);
2095 update_learning_table(const struct xbridge *xbridge,
2096 const struct flow *flow, struct flow_wildcards *wc,
2097 int vlan, struct xbundle *in_xbundle)
2101 /* Don't learn the OFPP_NONE port. */
2102 if (in_xbundle == &ofpp_none_bundle) {
2106 /* First try the common case: no change to MAC learning table. */
2107 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
2108 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
2110 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2113 /* Slow path: MAC learning table might need an update. */
2114 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
2115 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
2116 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2120 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2121 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
2123 update_mcast_snooping_table4__(const struct xbridge *xbridge,
2124 const struct flow *flow,
2125 struct mcast_snooping *ms, int vlan,
2126 struct xbundle *in_xbundle,
2127 const struct dp_packet *packet)
2128 OVS_REQ_WRLOCK(ms->rwlock)
2130 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2132 ovs_be32 ip4 = flow->igmp_group_ip4;
2134 switch (ntohs(flow->tp_src)) {
2135 case IGMP_HOST_MEMBERSHIP_REPORT:
2136 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2137 if (mcast_snooping_add_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2138 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
2139 IP_FMT" is on port %s in VLAN %d",
2140 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2143 case IGMP_HOST_LEAVE_MESSAGE:
2144 if (mcast_snooping_leave_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2145 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
2146 IP_FMT" is on port %s in VLAN %d",
2147 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2150 case IGMP_HOST_MEMBERSHIP_QUERY:
2151 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
2152 in_xbundle->ofbundle)) {
2153 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
2154 IP_FMT" is on port %s in VLAN %d",
2155 xbridge->name, IP_ARGS(flow->nw_src),
2156 in_xbundle->name, vlan);
2159 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2160 if ((count = mcast_snooping_add_report(ms, packet, vlan,
2161 in_xbundle->ofbundle))) {
2162 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2163 "addresses on port %s in VLAN %d",
2164 xbridge->name, count, in_xbundle->name, vlan);
2171 update_mcast_snooping_table6__(const struct xbridge *xbridge,
2172 const struct flow *flow,
2173 struct mcast_snooping *ms, int vlan,
2174 struct xbundle *in_xbundle,
2175 const struct dp_packet *packet)
2176 OVS_REQ_WRLOCK(ms->rwlock)
2178 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2181 switch (ntohs(flow->tp_src)) {
2183 if (!ipv6_addr_equals(&flow->ipv6_src, &in6addr_any)
2184 && mcast_snooping_add_mrouter(ms, vlan, in_xbundle->ofbundle)) {
2185 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query on port %s"
2187 xbridge->name, in_xbundle->name, vlan);
2193 count = mcast_snooping_add_mld(ms, packet, vlan, in_xbundle->ofbundle);
2195 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2196 "addresses on port %s in VLAN %d",
2197 xbridge->name, count, in_xbundle->name, vlan);
2203 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2204 * was received on 'in_xbundle' in 'vlan'. */
2206 update_mcast_snooping_table(const struct xbridge *xbridge,
2207 const struct flow *flow, int vlan,
2208 struct xbundle *in_xbundle,
2209 const struct dp_packet *packet)
2211 struct mcast_snooping *ms = xbridge->ms;
2212 struct xlate_cfg *xcfg;
2213 struct xbundle *mcast_xbundle;
2214 struct mcast_port_bundle *fport;
2216 /* Don't learn the OFPP_NONE port. */
2217 if (in_xbundle == &ofpp_none_bundle) {
2221 /* Don't learn from flood ports */
2222 mcast_xbundle = NULL;
2223 ovs_rwlock_wrlock(&ms->rwlock);
2224 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2225 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2226 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2227 if (mcast_xbundle == in_xbundle) {
2232 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
2233 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2234 update_mcast_snooping_table4__(xbridge, flow, ms, vlan,
2235 in_xbundle, packet);
2237 update_mcast_snooping_table6__(xbridge, flow, ms, vlan,
2238 in_xbundle, packet);
2241 ovs_rwlock_unlock(&ms->rwlock);
2244 /* send the packet to ports having the multicast group learned */
2246 xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2247 struct mcast_snooping *ms OVS_UNUSED,
2248 struct mcast_group *grp,
2249 struct xbundle *in_xbundle, uint16_t vlan)
2250 OVS_REQ_RDLOCK(ms->rwlock)
2252 struct xlate_cfg *xcfg;
2253 struct mcast_group_bundle *b;
2254 struct xbundle *mcast_xbundle;
2256 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2257 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2258 mcast_xbundle = xbundle_lookup(xcfg, b->port);
2259 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2260 xlate_report(ctx, "forwarding to mcast group port");
2261 output_normal(ctx, mcast_xbundle, vlan);
2262 } else if (!mcast_xbundle) {
2263 xlate_report(ctx, "mcast group port is unknown, dropping");
2265 xlate_report(ctx, "mcast group port is input port, dropping");
2270 /* send the packet to ports connected to multicast routers */
2272 xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2273 struct mcast_snooping *ms,
2274 struct xbundle *in_xbundle, uint16_t vlan)
2275 OVS_REQ_RDLOCK(ms->rwlock)
2277 struct xlate_cfg *xcfg;
2278 struct mcast_mrouter_bundle *mrouter;
2279 struct xbundle *mcast_xbundle;
2281 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2282 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2283 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2284 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2285 xlate_report(ctx, "forwarding to mcast router port");
2286 output_normal(ctx, mcast_xbundle, vlan);
2287 } else if (!mcast_xbundle) {
2288 xlate_report(ctx, "mcast router port is unknown, dropping");
2290 xlate_report(ctx, "mcast router port is input port, dropping");
2295 /* send the packet to ports flagged to be flooded */
2297 xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2298 struct mcast_snooping *ms,
2299 struct xbundle *in_xbundle, uint16_t vlan)
2300 OVS_REQ_RDLOCK(ms->rwlock)
2302 struct xlate_cfg *xcfg;
2303 struct mcast_port_bundle *fport;
2304 struct xbundle *mcast_xbundle;
2306 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2307 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2308 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2309 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2310 xlate_report(ctx, "forwarding to mcast flood port");
2311 output_normal(ctx, mcast_xbundle, vlan);
2312 } else if (!mcast_xbundle) {
2313 xlate_report(ctx, "mcast flood port is unknown, dropping");
2315 xlate_report(ctx, "mcast flood port is input port, dropping");
2320 /* forward the Reports to configured ports */
2322 xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
2323 struct mcast_snooping *ms,
2324 struct xbundle *in_xbundle, uint16_t vlan)
2325 OVS_REQ_RDLOCK(ms->rwlock)
2327 struct xlate_cfg *xcfg;
2328 struct mcast_port_bundle *rport;
2329 struct xbundle *mcast_xbundle;
2331 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2332 LIST_FOR_EACH(rport, node, &ms->rport_list) {
2333 mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2334 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2335 xlate_report(ctx, "forwarding Report to mcast flagged port");
2336 output_normal(ctx, mcast_xbundle, vlan);
2337 } else if (!mcast_xbundle) {
2338 xlate_report(ctx, "mcast port is unknown, dropping the Report");
2340 xlate_report(ctx, "mcast port is input port, dropping the Report");
2346 xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2349 struct xbundle *xbundle;
2351 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2352 if (xbundle != in_xbundle
2353 && xbundle_includes_vlan(xbundle, vlan)
2354 && xbundle->floodable
2355 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2356 output_normal(ctx, xbundle, vlan);
2359 ctx->nf_output_iface = NF_OUT_FLOOD;
2363 xlate_normal(struct xlate_ctx *ctx)
2365 struct flow_wildcards *wc = ctx->wc;
2366 struct flow *flow = &ctx->xin->flow;
2367 struct xbundle *in_xbundle;
2368 struct xport *in_port;
2369 struct mac_entry *mac;
2374 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2375 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2376 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2378 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
2379 ctx->xin->packet != NULL, &in_port);
2381 xlate_report(ctx, "no input bundle, dropping");
2385 /* Drop malformed frames. */
2386 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
2387 !(flow->vlan_tci & htons(VLAN_CFI))) {
2388 if (ctx->xin->packet != NULL) {
2389 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2390 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
2391 "VLAN tag received on port %s",
2392 ctx->xbridge->name, in_xbundle->name);
2394 xlate_report(ctx, "partial VLAN tag, dropping");
2398 /* Drop frames on bundles reserved for mirroring. */
2399 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
2400 if (ctx->xin->packet != NULL) {
2401 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2402 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2403 "%s, which is reserved exclusively for mirroring",
2404 ctx->xbridge->name, in_xbundle->name);
2406 xlate_report(ctx, "input port is mirror output port, dropping");
2411 vid = vlan_tci_to_vid(flow->vlan_tci);
2412 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
2413 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2416 vlan = input_vid_to_vlan(in_xbundle, vid);
2418 /* Check other admissibility requirements. */
2419 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2423 /* Learn source MAC. */
2424 if (ctx->xin->may_learn) {
2425 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
2427 if (ctx->xin->xcache) {
2428 struct xc_entry *entry;
2430 /* Save enough info to update mac learning table later. */
2431 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
2432 entry->u.normal.ofproto = ctx->xbridge->ofproto;
2433 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2434 entry->u.normal.vlan = vlan;
2437 /* Determine output bundle. */
2438 if (mcast_snooping_enabled(ctx->xbridge->ms)
2439 && !eth_addr_is_broadcast(flow->dl_dst)
2440 && eth_addr_is_multicast(flow->dl_dst)
2441 && is_ip_any(flow)) {
2442 struct mcast_snooping *ms = ctx->xbridge->ms;
2443 struct mcast_group *grp = NULL;
2445 if (is_igmp(flow)) {
2446 if (mcast_snooping_is_membership(flow->tp_src) ||
2447 mcast_snooping_is_query(flow->tp_src)) {
2448 if (ctx->xin->may_learn && ctx->xin->packet) {
2449 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2450 in_xbundle, ctx->xin->packet);
2453 * IGMP packets need to take the slow path, in order to be
2454 * processed for mdb updates. That will prevent expires
2455 * firing off even after hosts have sent reports.
2457 ctx->xout->slow |= SLOW_ACTION;
2460 if (mcast_snooping_is_membership(flow->tp_src)) {
2461 ovs_rwlock_rdlock(&ms->rwlock);
2462 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2463 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2464 * forward IGMP Membership Reports only to those ports where
2465 * multicast routers are attached. Alternatively stated: a
2466 * snooping switch should not forward IGMP Membership Reports
2467 * to ports on which only hosts are attached.
2468 * An administrative control may be provided to override this
2469 * restriction, allowing the report messages to be flooded to
2471 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2472 ovs_rwlock_unlock(&ms->rwlock);
2474 xlate_report(ctx, "multicast traffic, flooding");
2475 xlate_normal_flood(ctx, in_xbundle, vlan);
2478 } else if (is_mld(flow)) {
2479 ctx->xout->slow |= SLOW_ACTION;
2480 if (ctx->xin->may_learn && ctx->xin->packet) {
2481 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2482 in_xbundle, ctx->xin->packet);
2484 if (is_mld_report(flow)) {
2485 ovs_rwlock_rdlock(&ms->rwlock);
2486 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2487 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2488 ovs_rwlock_unlock(&ms->rwlock);
2490 xlate_report(ctx, "MLD query, flooding");
2491 xlate_normal_flood(ctx, in_xbundle, vlan);
2494 if ((flow->dl_type == htons(ETH_TYPE_IP)
2495 && ip_is_local_multicast(flow->nw_dst))
2496 || (flow->dl_type == htons(ETH_TYPE_IPV6)
2497 && ipv6_is_all_hosts(&flow->ipv6_dst))) {
2498 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2499 * address in the 224.0.0.x range which are not IGMP must
2500 * be forwarded on all ports */
2501 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2502 xlate_normal_flood(ctx, in_xbundle, vlan);
2507 /* forwarding to group base ports */
2508 ovs_rwlock_rdlock(&ms->rwlock);
2509 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2510 grp = mcast_snooping_lookup4(ms, flow->nw_dst, vlan);
2511 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2512 grp = mcast_snooping_lookup(ms, &flow->ipv6_dst, vlan);
2515 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2516 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2517 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2519 if (mcast_snooping_flood_unreg(ms)) {
2520 xlate_report(ctx, "unregistered multicast, flooding");
2521 xlate_normal_flood(ctx, in_xbundle, vlan);
2523 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2524 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2527 ovs_rwlock_unlock(&ms->rwlock);
2529 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2530 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
2531 mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
2532 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2535 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2536 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2537 if (mac_xbundle && mac_xbundle != in_xbundle) {
2538 xlate_report(ctx, "forwarding to learned port");
2539 output_normal(ctx, mac_xbundle, vlan);
2540 } else if (!mac_xbundle) {
2541 xlate_report(ctx, "learned port is unknown, dropping");
2543 xlate_report(ctx, "learned port is input port, dropping");
2546 xlate_report(ctx, "no learned MAC for destination, flooding");
2547 xlate_normal_flood(ctx, in_xbundle, vlan);
2552 /* Appends a "sample" action for sFlow or IPFIX to 'ctx->odp_actions'. The
2553 * 'probability' is the number of packets out of UINT32_MAX to sample. The
2554 * 'cookie' (of length 'cookie_size' bytes) is passed back in the callback for
2555 * each sampled packet. 'tunnel_out_port', if not ODPP_NONE, is added as the
2556 * OVS_USERSPACE_ATTR_EGRESS_TUN_PORT attribute. If 'include_actions', an
2557 * OVS_USERSPACE_ATTR_ACTIONS attribute is added.
2560 compose_sample_action(struct xlate_ctx *ctx,
2561 const uint32_t probability,
2562 const union user_action_cookie *cookie,
2563 const size_t cookie_size,
2564 const odp_port_t tunnel_out_port,
2565 bool include_actions)
2567 size_t sample_offset = nl_msg_start_nested(ctx->odp_actions,
2568 OVS_ACTION_ATTR_SAMPLE);
2570 nl_msg_put_u32(ctx->odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
2572 size_t actions_offset = nl_msg_start_nested(ctx->odp_actions,
2573 OVS_SAMPLE_ATTR_ACTIONS);
2575 odp_port_t odp_port = ofp_port_to_odp_port(
2576 ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
2577 uint32_t pid = dpif_port_get_pid(ctx->xbridge->dpif, odp_port,
2578 flow_hash_5tuple(&ctx->xin->flow, 0));
2579 int cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2584 nl_msg_end_nested(ctx->odp_actions, actions_offset);
2585 nl_msg_end_nested(ctx->odp_actions, sample_offset);
2587 return cookie_offset;
2590 /* If sFLow is not enabled, returns 0 without doing anything.
2592 * If sFlow is enabled, appends a template "sample" action to the ODP actions
2593 * in 'ctx'. This action is a template because some of the information needed
2594 * to fill it out is not available until flow translation is complete. In this
2595 * case, this functions returns an offset, which is always nonzero, to pass
2596 * later to fix_sflow_action() to fill in the rest of the template. */
2598 compose_sflow_action(struct xlate_ctx *ctx)
2600 struct dpif_sflow *sflow = ctx->xbridge->sflow;
2601 if (!sflow || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
2605 union user_action_cookie cookie = { .type = USER_ACTION_COOKIE_SFLOW };
2606 return compose_sample_action(ctx, dpif_sflow_get_probability(sflow),
2607 &cookie, sizeof cookie.sflow, ODPP_NONE,
2611 /* If IPFIX is enabled, this appends a "sample" action to implement IPFIX to
2612 * 'ctx->odp_actions'. */
2614 compose_ipfix_action(struct xlate_ctx *ctx, odp_port_t output_odp_port)
2616 struct dpif_ipfix *ipfix = ctx->xbridge->ipfix;
2617 odp_port_t tunnel_out_port = ODPP_NONE;
2619 if (!ipfix || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
2623 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2625 if (output_odp_port == ODPP_NONE &&
2626 !dpif_ipfix_get_bridge_exporter_input_sampling(ipfix)) {
2630 /* For output case, output_odp_port is valid*/
2631 if (output_odp_port != ODPP_NONE) {
2632 if (!dpif_ipfix_get_bridge_exporter_output_sampling(ipfix)) {
2635 /* If tunnel sampling is enabled, put an additional option attribute:
2636 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2638 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(ipfix) &&
2639 dpif_ipfix_get_tunnel_port(ipfix, output_odp_port) ) {
2640 tunnel_out_port = output_odp_port;
2644 union user_action_cookie cookie = {
2646 .type = USER_ACTION_COOKIE_IPFIX,
2647 .output_odp_port = output_odp_port,
2650 compose_sample_action(ctx,
2651 dpif_ipfix_get_bridge_exporter_probability(ipfix),
2652 &cookie, sizeof cookie.ipfix, tunnel_out_port,
2656 /* Fix "sample" action according to data collected while composing ODP actions,
2657 * as described in compose_sflow_action().
2659 * 'user_cookie_offset' must be the offset returned by add_sflow_action(). */
2661 fix_sflow_action(struct xlate_ctx *ctx, unsigned int user_cookie_offset)
2663 const struct flow *base = &ctx->base_flow;
2664 union user_action_cookie *cookie;
2666 cookie = ofpbuf_at(ctx->odp_actions, user_cookie_offset,
2667 sizeof cookie->sflow);
2668 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2670 cookie->type = USER_ACTION_COOKIE_SFLOW;
2671 cookie->sflow.vlan_tci = base->vlan_tci;
2673 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2674 * port information") for the interpretation of cookie->output. */
2675 switch (ctx->sflow_n_outputs) {
2677 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2678 cookie->sflow.output = 0x40000000 | 256;
2682 cookie->sflow.output = dpif_sflow_odp_port_to_ifindex(
2683 ctx->xbridge->sflow, ctx->sflow_odp_port);
2684 if (cookie->sflow.output) {
2689 /* 0x80000000 means "multiple output ports. */
2690 cookie->sflow.output = 0x80000000 | ctx->sflow_n_outputs;
2696 process_special(struct xlate_ctx *ctx, const struct xport *xport)
2698 const struct flow *flow = &ctx->xin->flow;
2699 struct flow_wildcards *wc = ctx->wc;
2700 const struct xbridge *xbridge = ctx->xbridge;
2701 const struct dp_packet *packet = ctx->xin->packet;
2702 enum slow_path_reason slow;
2706 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
2708 cfm_process_heartbeat(xport->cfm, packet);
2711 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
2713 bfd_process_packet(xport->bfd, flow, packet);
2714 /* If POLL received, immediately sends FINAL back. */
2715 if (bfd_should_send_packet(xport->bfd)) {
2716 ofproto_dpif_monitor_port_send_soon(xport->ofport);
2720 } else if (xport->xbundle && xport->xbundle->lacp
2721 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2723 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
2726 } else if ((xbridge->stp || xbridge->rstp) &&
2727 stp_should_process_flow(flow, wc)) {
2730 ? stp_process_packet(xport, packet)
2731 : rstp_process_packet(xport, packet);
2734 } else if (xport->lldp && lldp_should_process_flow(xport->lldp, flow)) {
2736 lldp_process_packet(xport->lldp, packet);
2744 ctx->xout->slow |= slow;
2752 tnl_route_lookup_flow(const struct flow *oflow,
2753 struct in6_addr *ip, struct xport **out_port)
2755 char out_dev[IFNAMSIZ];
2756 struct xbridge *xbridge;
2757 struct xlate_cfg *xcfg;
2759 struct in6_addr dst;
2761 dst = flow_tnl_dst(&oflow->tunnel);
2762 if (!ovs_router_lookup(&dst, out_dev, &gw)) {
2766 if (ipv6_addr_is_set(&gw) &&
2767 (!IN6_IS_ADDR_V4MAPPED(&gw) || in6_addr_get_mapped_ipv4(&gw))) {
2773 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2776 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
2777 if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
2780 HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
2781 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
2792 compose_table_xlate(struct xlate_ctx *ctx, const struct xport *out_dev,
2793 struct dp_packet *packet)
2795 struct xbridge *xbridge = out_dev->xbridge;
2796 struct ofpact_output output;
2799 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
2800 flow_extract(packet, &flow);
2801 flow.in_port.ofp_port = out_dev->ofp_port;
2802 output.port = OFPP_TABLE;
2805 return ofproto_dpif_execute_actions__(xbridge->ofproto, &flow, NULL,
2806 &output.ofpact, sizeof output,
2807 ctx->recurse, ctx->resubmits, packet);
2811 tnl_send_nd_request(struct xlate_ctx *ctx, const struct xport *out_dev,
2812 const struct eth_addr eth_src,
2813 struct in6_addr * ipv6_src, struct in6_addr * ipv6_dst)
2815 struct dp_packet packet;
2817 dp_packet_init(&packet, 0);
2818 compose_nd(&packet, eth_src, ipv6_src, ipv6_dst);
2819 compose_table_xlate(ctx, out_dev, &packet);
2820 dp_packet_uninit(&packet);
2824 tnl_send_arp_request(struct xlate_ctx *ctx, const struct xport *out_dev,
2825 const struct eth_addr eth_src,
2826 ovs_be32 ip_src, ovs_be32 ip_dst)
2828 struct dp_packet packet;
2830 dp_packet_init(&packet, 0);
2831 compose_arp(&packet, ARP_OP_REQUEST,
2832 eth_src, eth_addr_zero, true, ip_src, ip_dst);
2834 compose_table_xlate(ctx, out_dev, &packet);
2835 dp_packet_uninit(&packet);
2839 build_tunnel_send(struct xlate_ctx *ctx, const struct xport *xport,
2840 const struct flow *flow, odp_port_t tunnel_odp_port)
2842 struct ovs_action_push_tnl tnl_push_data;
2843 struct xport *out_dev = NULL;
2844 ovs_be32 s_ip = 0, d_ip = 0;
2845 struct in6_addr s_ip6 = in6addr_any;
2846 struct in6_addr d_ip6 = in6addr_any;
2847 struct eth_addr smac;
2848 struct eth_addr dmac;
2850 char buf_sip6[INET6_ADDRSTRLEN];
2851 char buf_dip6[INET6_ADDRSTRLEN];
2853 err = tnl_route_lookup_flow(flow, &d_ip6, &out_dev);
2855 xlate_report(ctx, "native tunnel routing failed");
2859 xlate_report(ctx, "tunneling to %s via %s",
2860 ipv6_string_mapped(buf_dip6, &d_ip6),
2861 netdev_get_name(out_dev->netdev));
2863 /* Use mac addr of bridge port of the peer. */
2864 err = netdev_get_etheraddr(out_dev->netdev, &smac);
2866 xlate_report(ctx, "tunnel output device lacks Ethernet address");
2870 d_ip = in6_addr_get_mapped_ipv4(&d_ip6);
2872 err = netdev_get_in4(out_dev->netdev, (struct in_addr *) &s_ip, NULL);
2874 xlate_report(ctx, "tunnel output device lacks IPv4 address");
2877 in6_addr_set_mapped_ipv4(&s_ip6, s_ip);
2879 err = netdev_get_in6(out_dev->netdev, &s_ip6);
2881 xlate_report(ctx, "tunnel output device lacks IPv6 address");
2886 err = tnl_neigh_lookup(out_dev->xbridge->name, &d_ip6, &dmac);
2888 xlate_report(ctx, "neighbor cache miss for %s on bridge %s, "
2889 "sending %s request",
2890 buf_dip6, out_dev->xbridge->name, d_ip ? "ARP" : "ND");
2892 tnl_send_arp_request(ctx, out_dev, smac, s_ip, d_ip);
2894 tnl_send_nd_request(ctx, out_dev, smac, &s_ip6, &d_ip6);
2899 if (ctx->xin->xcache) {
2900 struct xc_entry *entry;
2902 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_NEIGH);
2903 ovs_strlcpy(entry->u.tnl_neigh_cache.br_name, out_dev->xbridge->name,
2904 sizeof entry->u.tnl_neigh_cache.br_name);
2905 entry->u.tnl_neigh_cache.d_ipv6 = d_ip6;
2908 xlate_report(ctx, "tunneling from "ETH_ADDR_FMT" %s"
2909 " to "ETH_ADDR_FMT" %s",
2910 ETH_ADDR_ARGS(smac), ipv6_string_mapped(buf_sip6, &s_ip6),
2911 ETH_ADDR_ARGS(dmac), buf_dip6);
2913 err = tnl_port_build_header(xport->ofport, flow,
2914 dmac, smac, &s_ip6, &tnl_push_data);
2918 tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
2919 tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
2920 odp_put_tnl_push_action(ctx->odp_actions, &tnl_push_data);
2925 xlate_commit_actions(struct xlate_ctx *ctx)
2927 bool use_masked = ctx->xbridge->support.masked_set_action;
2929 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2930 ctx->odp_actions, ctx->wc,
2935 clear_conntrack(struct flow *flow)
2940 memset(&flow->ct_label, 0, sizeof flow->ct_label);
2944 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
2945 const struct xlate_bond_recirc *xr, bool check_stp)
2947 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
2948 struct flow_wildcards *wc = ctx->wc;
2949 struct flow *flow = &ctx->xin->flow;
2950 struct flow_tnl flow_tnl;
2951 ovs_be16 flow_vlan_tci;
2952 uint32_t flow_pkt_mark;
2953 uint8_t flow_nw_tos;
2954 odp_port_t out_port, odp_port;
2955 bool tnl_push_pop_send = false;
2958 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2959 * before traversing a patch port. */
2960 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 35);
2961 memset(&flow_tnl, 0, sizeof flow_tnl);
2964 xlate_report(ctx, "Nonexistent output port");
2966 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
2967 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2969 } else if (check_stp) {
2970 if (is_stp(&ctx->base_flow)) {
2971 if (!xport_stp_should_forward_bpdu(xport) &&
2972 !xport_rstp_should_manage_bpdu(xport)) {
2973 if (ctx->xbridge->stp != NULL) {
2974 xlate_report(ctx, "STP not in listening state, "
2975 "skipping bpdu output");
2976 } else if (ctx->xbridge->rstp != NULL) {
2977 xlate_report(ctx, "RSTP not managing BPDU in this state, "
2978 "skipping bpdu output");
2982 } else if (!xport_stp_forward_state(xport) ||
2983 !xport_rstp_forward_state(xport)) {
2984 if (ctx->xbridge->stp != NULL) {
2985 xlate_report(ctx, "STP not in forwarding state, "
2987 } else if (ctx->xbridge->rstp != NULL) {
2988 xlate_report(ctx, "RSTP not in forwarding state, "
2996 const struct xport *peer = xport->peer;
2997 struct flow old_flow = ctx->xin->flow;
2998 bool old_conntrack = ctx->conntracked;
2999 cls_version_t old_version = ctx->tables_version;
3000 struct ofpbuf old_stack = ctx->stack;
3001 union mf_subvalue new_stack[1024 / sizeof(union mf_subvalue)];
3002 struct ofpbuf old_action_set = ctx->action_set;
3003 uint64_t actset_stub[1024 / 8];
3005 ofpbuf_use_stub(&ctx->stack, new_stack, sizeof new_stack);
3006 ofpbuf_use_stub(&ctx->action_set, actset_stub, sizeof actset_stub);
3007 ctx->xbridge = peer->xbridge;
3008 flow->in_port.ofp_port = peer->ofp_port;
3009 flow->metadata = htonll(0);
3010 memset(&flow->tunnel, 0, sizeof flow->tunnel);
3011 memset(flow->regs, 0, sizeof flow->regs);
3012 flow->actset_output = OFPP_UNSET;
3013 ctx->conntracked = false;
3014 clear_conntrack(flow);
3016 /* The bridge is now known so obtain its table version. */
3018 = ofproto_dpif_get_tables_version(ctx->xbridge->ofproto);
3020 if (!process_special(ctx, peer) && may_receive(peer, ctx)) {
3021 if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
3022 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
3023 if (!ctx->freezing) {
3024 xlate_action_set(ctx);
3026 if (ctx->freezing) {
3027 finish_freezing(ctx);
3030 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
3031 * the learning action look at the packet, then drop it. */
3032 struct flow old_base_flow = ctx->base_flow;
3033 size_t old_size = ctx->odp_actions->size;
3034 mirror_mask_t old_mirrors = ctx->mirrors;
3036 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
3037 ctx->mirrors = old_mirrors;
3038 ctx->base_flow = old_base_flow;
3039 ctx->odp_actions->size = old_size;
3041 /* Undo changes that may have been done for freezing. */
3042 ctx_cancel_freeze(ctx);
3046 ctx->xin->flow = old_flow;
3047 ctx->xbridge = xport->xbridge;
3048 ofpbuf_uninit(&ctx->action_set);
3049 ctx->action_set = old_action_set;
3050 ofpbuf_uninit(&ctx->stack);
3051 ctx->stack = old_stack;
3053 /* Restore calling bridge's lookup version. */
3054 ctx->tables_version = old_version;
3056 /* The peer bridge's conntrack execution should have no effect on the
3057 * original bridge. */
3058 ctx->conntracked = old_conntrack;
3060 /* The fact that the peer bridge exits (for any reason) does not mean
3061 * that the original bridge should exit. Specifically, if the peer
3062 * bridge freezes translation, the original bridge must continue
3063 * processing with the original, not the frozen packet! */
3066 /* Peer bridge errors do not propagate back. */
3067 ctx->error = XLATE_OK;
3069 if (ctx->xin->resubmit_stats) {
3070 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
3071 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
3073 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
3076 if (ctx->xin->xcache) {
3077 struct xc_entry *entry;
3079 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
3080 entry->u.dev.tx = netdev_ref(xport->netdev);
3081 entry->u.dev.rx = netdev_ref(peer->netdev);
3082 entry->u.dev.bfd = bfd_ref(peer->bfd);
3087 flow_vlan_tci = flow->vlan_tci;
3088 flow_pkt_mark = flow->pkt_mark;
3089 flow_nw_tos = flow->nw_tos;
3091 if (count_skb_priorities(xport)) {
3092 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3093 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
3094 wc->masks.nw_tos |= IP_DSCP_MASK;
3095 flow->nw_tos &= ~IP_DSCP_MASK;
3096 flow->nw_tos |= dscp;
3100 if (xport->is_tunnel) {
3101 struct in6_addr dst;
3102 /* Save tunnel metadata so that changes made due to
3103 * the Logical (tunnel) Port are not visible for any further
3104 * matches, while explicit set actions on tunnel metadata are.
3106 flow_tnl = flow->tunnel;
3107 odp_port = tnl_port_send(xport->ofport, flow, ctx->wc);
3108 if (odp_port == ODPP_NONE) {
3109 xlate_report(ctx, "Tunneling decided against output");
3110 goto out; /* restore flow_nw_tos */
3112 dst = flow_tnl_dst(&flow->tunnel);
3113 if (ipv6_addr_equals(&dst, &ctx->orig_tunnel_ipv6_dst)) {
3114 xlate_report(ctx, "Not tunneling to our own address");
3115 goto out; /* restore flow_nw_tos */
3117 if (ctx->xin->resubmit_stats) {
3118 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
3120 if (ctx->xin->xcache) {
3121 struct xc_entry *entry;
3123 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
3124 entry->u.dev.tx = netdev_ref(xport->netdev);
3126 out_port = odp_port;
3127 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3128 xlate_report(ctx, "output to native tunnel");
3129 tnl_push_pop_send = true;
3131 xlate_report(ctx, "output to kernel tunnel");
3132 commit_odp_tunnel_action(flow, &ctx->base_flow, ctx->odp_actions);
3133 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3136 odp_port = xport->odp_port;
3137 out_port = odp_port;
3138 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
3139 ofp_port_t vlandev_port;
3141 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
3142 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
3143 ofp_port, flow->vlan_tci);
3144 if (vlandev_port != ofp_port) {
3145 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
3146 flow->vlan_tci = htons(0);
3151 if (out_port != ODPP_NONE) {
3152 xlate_commit_actions(ctx);
3155 struct ovs_action_hash *act_hash;
3158 act_hash = nl_msg_put_unspec_uninit(ctx->odp_actions,
3159 OVS_ACTION_ATTR_HASH,
3161 act_hash->hash_alg = xr->hash_alg;
3162 act_hash->hash_basis = xr->hash_basis;
3164 /* Recirc action. */
3165 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC,
3169 if (tnl_push_pop_send) {
3170 build_tunnel_send(ctx, xport, flow, odp_port);
3171 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3173 odp_port_t odp_tnl_port = ODPP_NONE;
3175 /* XXX: Write better Filter for tunnel port. We can use inport
3176 * int tunnel-port flow to avoid these checks completely. */
3177 if (ofp_port == OFPP_LOCAL &&
3178 ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3180 odp_tnl_port = tnl_port_map_lookup(flow, wc);
3183 if (odp_tnl_port != ODPP_NONE) {
3184 nl_msg_put_odp_port(ctx->odp_actions,
3185 OVS_ACTION_ATTR_TUNNEL_POP,
3188 /* Tunnel push-pop action is not compatible with
3190 compose_ipfix_action(ctx, out_port);
3191 nl_msg_put_odp_port(ctx->odp_actions,
3192 OVS_ACTION_ATTR_OUTPUT,
3198 ctx->sflow_odp_port = odp_port;
3199 ctx->sflow_n_outputs++;
3200 ctx->nf_output_iface = ofp_port;
3203 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
3204 mirror_packet(ctx, xport->xbundle,
3205 xbundle_mirror_dst(xport->xbundle->xbridge,
3211 flow->vlan_tci = flow_vlan_tci;
3212 flow->pkt_mark = flow_pkt_mark;
3213 flow->nw_tos = flow_nw_tos;
3217 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port,
3218 const struct xlate_bond_recirc *xr)
3220 compose_output_action__(ctx, ofp_port, xr, true);
3224 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
3226 struct rule_dpif *old_rule = ctx->rule;
3227 ovs_be64 old_cookie = ctx->rule_cookie;
3228 const struct rule_actions *actions;
3230 if (ctx->xin->resubmit_stats) {
3231 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
3237 ctx->rule_cookie = rule_dpif_get_flow_cookie(rule);
3238 actions = rule_dpif_get_actions(rule);
3239 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
3240 ctx->rule_cookie = old_cookie;
3241 ctx->rule = old_rule;
3246 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
3248 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
3249 XLATE_REPORT_ERROR(ctx, "resubmit actions recursed over %d times",
3250 MAX_RESUBMIT_RECURSION);
3251 ctx->error = XLATE_RECURSION_TOO_DEEP;
3252 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
3253 XLATE_REPORT_ERROR(ctx, "over %d resubmit actions", MAX_RESUBMITS);
3254 ctx->error = XLATE_TOO_MANY_RESUBMITS;
3255 } else if (ctx->odp_actions->size > UINT16_MAX) {
3256 XLATE_REPORT_ERROR(ctx, "resubmits yielded over 64 kB of actions");
3257 /* NOT an error, as we'll be slow-pathing the flow in this case? */
3258 ctx->exit = true; /* XXX: translation still terminated! */
3259 } else if (ctx->stack.size >= 65536) {
3260 XLATE_REPORT_ERROR(ctx, "resubmits yielded over 64 kB of stack");
3261 ctx->error = XLATE_STACK_TOO_DEEP;
3270 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
3271 bool may_packet_in, bool honor_table_miss)
3273 if (xlate_resubmit_resource_check(ctx)) {
3274 uint8_t old_table_id = ctx->table_id;
3275 struct rule_dpif *rule;
3277 ctx->table_id = table_id;
3279 rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
3280 ctx->tables_version,
3281 &ctx->xin->flow, ctx->xin->wc,
3282 ctx->xin->resubmit_stats,
3283 &ctx->table_id, in_port,
3284 may_packet_in, honor_table_miss);
3286 if (OVS_UNLIKELY(ctx->xin->resubmit_hook)) {
3287 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse + 1);
3291 /* Fill in the cache entry here instead of xlate_recursively
3292 * to make the reference counting more explicit. We take a
3293 * reference in the lookups above if we are going to cache the
3295 if (ctx->xin->xcache) {
3296 struct xc_entry *entry;
3298 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
3299 entry->u.rule = rule;
3300 rule_dpif_ref(rule);
3302 xlate_recursively(ctx, rule);
3305 ctx->table_id = old_table_id;
3311 xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
3312 struct ofputil_bucket *bucket)
3314 if (ctx->xin->resubmit_stats) {
3315 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
3317 if (ctx->xin->xcache) {
3318 struct xc_entry *entry;
3320 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
3321 entry->u.group.group = group_dpif_ref(group);
3322 entry->u.group.bucket = bucket;
3327 xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
3329 uint64_t action_list_stub[1024 / 8];
3330 struct ofpbuf action_list = OFPBUF_STUB_INITIALIZER(action_list_stub);
3331 struct ofpbuf action_set = ofpbuf_const_initializer(bucket->ofpacts,
3332 bucket->ofpacts_len);
3333 struct flow old_flow = ctx->xin->flow;
3335 ofpacts_execute_action_set(&action_list, &action_set);
3337 do_xlate_actions(action_list.data, action_list.size, ctx);
3340 ofpbuf_uninit(&action_list);
3342 /* Check if need to freeze. */
3343 if (ctx->freezing) {
3344 finish_freezing(ctx);
3347 /* Roll back flow to previous state.
3348 * This is equivalent to cloning the packet for each bucket.
3350 * As a side effect any subsequently applied actions will
3351 * also effectively be applied to a clone of the packet taken
3352 * just before applying the all or indirect group.
3354 * Note that group buckets are action sets, hence they cannot modify the
3355 * main action set. Also any stack actions are ignored when executing an
3356 * action set, so group buckets cannot change the stack either.
3357 * However, we do allow resubmit actions in group buckets, which could
3358 * break the above assumptions. It is up to the controller to not mess up
3359 * with the action_set and stack in the tables resubmitted to from
3361 ctx->xin->flow = old_flow;
3363 /* The fact that the group bucket exits (for any reason) does not mean that
3364 * the translation after the group action should exit. Specifically, if
3365 * the group bucket freezes translation, the actions after the group action
3366 * must continue processing with the original, not the frozen packet! */
3371 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3373 struct ofputil_bucket *bucket;
3374 const struct ovs_list *buckets;
3376 group_dpif_get_buckets(group, &buckets);
3378 LIST_FOR_EACH (bucket, list_node, buckets) {
3379 xlate_group_bucket(ctx, bucket);
3381 xlate_group_stats(ctx, group, NULL);
3385 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3387 struct ofputil_bucket *bucket;
3389 bucket = group_first_live_bucket(ctx, group, 0);
3391 xlate_group_bucket(ctx, bucket);
3392 xlate_group_stats(ctx, group, bucket);
3397 xlate_default_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3399 struct flow_wildcards *wc = ctx->wc;
3400 struct ofputil_bucket *bucket;
3403 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
3404 flow_mask_hash_fields(&ctx->xin->flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
3405 bucket = group_best_live_bucket(ctx, group, basis);
3407 xlate_group_bucket(ctx, bucket);
3408 xlate_group_stats(ctx, group, bucket);
3413 xlate_hash_fields_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3415 struct mf_bitmap hash_fields = MF_BITMAP_INITIALIZER;
3416 const struct field_array *fields;
3417 struct ofputil_bucket *bucket;
3421 fields = group_dpif_get_fields(group);
3422 basis = hash_uint64(group_dpif_get_selection_method_param(group));
3424 /* Determine which fields to hash */
3425 for (i = 0; i < MFF_N_IDS; i++) {
3426 if (bitmap_is_set(fields->used.bm, i)) {
3427 const struct mf_field *mf;
3429 /* If the field is already present in 'hash_fields' then
3430 * this loop has already checked that it and its pre-requisites
3431 * are present in the flow and its pre-requisites have
3432 * already been added to 'hash_fields'. There is nothing more
3433 * to do here and as an optimisation the loop can continue. */
3434 if (bitmap_is_set(hash_fields.bm, i)) {
3440 /* Only hash a field if it and its pre-requisites are present
3442 if (!mf_are_prereqs_ok(mf, &ctx->xin->flow)) {
3446 /* Hash both the field and its pre-requisites */
3447 mf_bitmap_set_field_and_prereqs(mf, &hash_fields);
3451 /* Hash the fields */
3452 for (i = 0; i < MFF_N_IDS; i++) {
3453 if (bitmap_is_set(hash_fields.bm, i)) {
3454 const struct mf_field *mf = mf_from_id(i);
3455 union mf_value value;
3458 mf_get_value(mf, &ctx->xin->flow, &value);
3459 /* This seems inefficient but so does apply_mask() */
3460 for (j = 0; j < mf->n_bytes; j++) {
3461 ((uint8_t *) &value)[j] &= ((uint8_t *) &fields->value[i])[j];
3463 basis = hash_bytes(&value, mf->n_bytes, basis);
3465 /* For tunnels, hash in whether the field is present. */
3466 if (mf_is_tun_metadata(mf)) {
3467 basis = hash_boolean(mf_is_set(mf, &ctx->xin->flow), basis);
3470 mf_mask_field(mf, &ctx->wc->masks);
3474 bucket = group_best_live_bucket(ctx, group, basis);
3476 xlate_group_bucket(ctx, bucket);
3477 xlate_group_stats(ctx, group, bucket);
3482 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3484 const char *selection_method = group_dpif_get_selection_method(group);
3486 if (selection_method[0] == '\0') {
3487 xlate_default_select_group(ctx, group);
3488 } else if (!strcasecmp("hash", selection_method)) {
3489 xlate_hash_fields_select_group(ctx, group);
3491 /* Parsing of groups should ensure this never happens */
3497 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3499 bool was_in_group = ctx->in_group;
3500 ctx->in_group = true;
3502 switch (group_dpif_get_type(group)) {
3504 case OFPGT11_INDIRECT:
3505 xlate_all_group(ctx, group);
3507 case OFPGT11_SELECT:
3508 xlate_select_group(ctx, group);
3511 xlate_ff_group(ctx, group);
3516 group_dpif_unref(group);
3518 ctx->in_group = was_in_group;
3522 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3524 if (xlate_resubmit_resource_check(ctx)) {
3525 struct group_dpif *group;
3528 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
3530 xlate_group_action__(ctx, group);
3540 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
3541 const struct ofpact_resubmit *resubmit)
3545 bool may_packet_in = false;
3546 bool honor_table_miss = false;
3548 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
3549 /* Still allow missed packets to be sent to the controller
3550 * if resubmitting from an internal table. */
3551 may_packet_in = true;
3552 honor_table_miss = true;
3555 in_port = resubmit->in_port;
3556 if (in_port == OFPP_IN_PORT) {
3557 in_port = ctx->xin->flow.in_port.ofp_port;
3560 table_id = resubmit->table_id;
3561 if (table_id == 255) {
3562 table_id = ctx->table_id;
3565 xlate_table_action(ctx, in_port, table_id, may_packet_in,
3570 flood_packets(struct xlate_ctx *ctx, bool all)
3572 const struct xport *xport;
3574 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
3575 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
3580 compose_output_action__(ctx, xport->ofp_port, NULL, false);
3581 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
3582 compose_output_action(ctx, xport->ofp_port, NULL);
3586 ctx->nf_output_iface = NF_OUT_FLOOD;
3590 execute_controller_action(struct xlate_ctx *ctx, int len,
3591 enum ofp_packet_in_reason reason,
3592 uint16_t controller_id,
3593 const uint8_t *userdata, size_t userdata_len)
3595 struct dp_packet *packet;
3597 ctx->xout->slow |= SLOW_CONTROLLER;
3598 xlate_commit_actions(ctx);
3599 if (!ctx->xin->packet) {
3603 packet = dp_packet_clone(ctx->xin->packet);
3605 odp_execute_actions(NULL, &packet, 1, false,
3606 ctx->odp_actions->data, ctx->odp_actions->size, NULL);
3608 /* A packet sent by an action in a table-miss rule is considered an
3609 * explicit table miss. OpenFlow before 1.3 doesn't have that concept so
3610 * it will get translated back to OFPR_ACTION for those versions. */
3611 if (reason == OFPR_ACTION
3612 && ctx->rule && rule_dpif_is_table_miss(ctx->rule)) {
3613 reason = OFPR_EXPLICIT_MISS;
3616 size_t packet_len = dp_packet_size(packet);
3618 struct ofproto_async_msg *am = xmalloc(sizeof *am);
3619 *am = (struct ofproto_async_msg) {
3620 .controller_id = controller_id,
3621 .oam = OAM_PACKET_IN,
3625 .packet = dp_packet_steal_data(packet),
3626 .packet_len = packet_len,
3628 .table_id = ctx->table_id,
3629 .cookie = ctx->rule_cookie,
3630 .userdata = (userdata_len
3631 ? xmemdup(userdata, userdata_len)
3633 .userdata_len = userdata_len,
3639 flow_get_metadata(&ctx->xin->flow, &am->pin.up.public.flow_metadata);
3641 ofproto_dpif_send_async_msg(ctx->xbridge->ofproto, am);
3642 dp_packet_delete(packet);
3646 emit_continuation(struct xlate_ctx *ctx, const struct frozen_state *state)
3648 struct ofproto_async_msg *am = xmalloc(sizeof *am);
3649 *am = (struct ofproto_async_msg) {
3650 .controller_id = ctx->pause->controller_id,
3651 .oam = OAM_PACKET_IN,
3655 .userdata = xmemdup(ctx->pause->userdata,
3656 ctx->pause->userdata_len),
3657 .userdata_len = ctx->pause->userdata_len,
3658 .packet = xmemdup(dp_packet_data(ctx->xin->packet),
3659 dp_packet_size(ctx->xin->packet)),
3660 .packet_len = dp_packet_size(ctx->xin->packet),
3662 .bridge = *ofproto_dpif_get_uuid(ctx->xbridge->ofproto),
3663 .stack = xmemdup(state->stack,
3664 state->n_stack * sizeof *state->stack),
3665 .n_stack = state->n_stack,
3666 .mirrors = state->mirrors,
3667 .conntracked = state->conntracked,
3668 .actions = xmemdup(state->ofpacts, state->ofpacts_len),
3669 .actions_len = state->ofpacts_len,
3670 .action_set = xmemdup(state->action_set,
3671 state->action_set_len),
3672 .action_set_len = state->action_set_len,
3674 .max_len = UINT16_MAX,
3677 flow_get_metadata(&ctx->xin->flow, &am->pin.up.public.flow_metadata);
3678 ofproto_dpif_send_async_msg(ctx->xbridge->ofproto, am);
3682 finish_freezing__(struct xlate_ctx *ctx, uint8_t table)
3684 ovs_assert(ctx->freezing);
3686 struct frozen_state state = {
3688 .ofproto_uuid = *ofproto_dpif_get_uuid(ctx->xbridge->ofproto),
3689 .stack = ctx->stack.data,
3690 .n_stack = ctx->stack.size / sizeof(union mf_subvalue),
3691 .mirrors = ctx->mirrors,
3692 .conntracked = ctx->conntracked,
3693 .ofpacts = ctx->frozen_actions.data,
3694 .ofpacts_len = ctx->frozen_actions.size,
3695 .action_set = ctx->action_set.data,
3696 .action_set_len = ctx->action_set.size,
3698 frozen_metadata_from_flow(&state.metadata, &ctx->xin->flow);
3701 if (ctx->xin->packet) {
3702 emit_continuation(ctx, &state);
3705 /* Allocate a unique recirc id for the given metadata state in the
3706 * flow. An existing id, with a new reference to the corresponding
3707 * recirculation context, will be returned if possible.
3708 * The life-cycle of this recirc id is managed by associating it
3709 * with the udpif key ('ukey') created for each new datapath flow. */
3710 uint32_t id = recirc_alloc_id_ctx(&state);
3712 XLATE_REPORT_ERROR(ctx, "Failed to allocate recirculation id");
3713 ctx->error = XLATE_NO_RECIRCULATION_CONTEXT;
3716 recirc_refs_add(&ctx->xout->recircs, id);
3718 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
3721 /* Undo changes done by freezing. */
3722 ctx_cancel_freeze(ctx);
3725 /* Called only when we're freezing. */
3727 finish_freezing(struct xlate_ctx *ctx)
3729 xlate_commit_actions(ctx);
3730 finish_freezing__(ctx, 0);
3733 /* Fork the pipeline here. The current packet will continue processing the
3734 * current action list. A clone of the current packet will recirculate, skip
3735 * the remainder of the current action list and asynchronously resume pipeline
3736 * processing in 'table' with the current metadata and action set. */
3738 compose_recirculate_and_fork(struct xlate_ctx *ctx, uint8_t table)
3740 ctx->freezing = true;
3741 finish_freezing__(ctx, table);
3745 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
3747 struct flow *flow = &ctx->xin->flow;
3750 ovs_assert(eth_type_mpls(mpls->ethertype));
3752 n = flow_count_mpls_labels(flow, ctx->wc);
3754 xlate_commit_actions(ctx);
3755 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3756 if (ctx->xin->packet != NULL) {
3757 XLATE_REPORT_ERROR(ctx, "bridge %s: dropping packet on which an "
3758 "MPLS push action can't be performed as it would "
3759 "have more MPLS LSEs than the %d supported.",
3760 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3762 ctx->error = XLATE_TOO_MANY_MPLS_LABELS;
3766 flow_push_mpls(flow, n, mpls->ethertype, ctx->wc);
3770 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
3772 struct flow *flow = &ctx->xin->flow;
3773 int n = flow_count_mpls_labels(flow, ctx->wc);
3775 if (flow_pop_mpls(flow, n, eth_type, ctx->wc)) {
3776 if (!eth_type_mpls(eth_type) && ctx->xbridge->support.odp.recirc) {
3777 ctx_trigger_freeze(ctx);
3779 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3780 if (ctx->xin->packet != NULL) {
3781 XLATE_REPORT_ERROR(ctx, "bridge %s: dropping packet on which an "
3782 "MPLS pop action can't be performed as it has "
3783 "more MPLS LSEs than the %d supported.",
3784 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3786 ctx->error = XLATE_TOO_MANY_MPLS_LABELS;
3787 ofpbuf_clear(ctx->odp_actions);
3792 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3794 struct flow *flow = &ctx->xin->flow;
3796 if (!is_ip_any(flow)) {
3800 ctx->wc->masks.nw_ttl = 0xff;
3801 if (flow->nw_ttl > 1) {
3807 for (i = 0; i < ids->n_controllers; i++) {
3808 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3809 ids->cnt_ids[i], NULL, 0);
3812 /* Stop processing for current table. */
3818 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3820 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3821 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
3822 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
3827 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3829 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3830 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
3831 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
3836 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
3838 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3839 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3840 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
3845 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
3847 struct flow *flow = &ctx->xin->flow;
3849 if (eth_type_mpls(flow->dl_type)) {
3850 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
3852 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3855 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3858 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0,
3863 /* Stop processing for current table. */
3868 xlate_output_action(struct xlate_ctx *ctx,
3869 ofp_port_t port, uint16_t max_len, bool may_packet_in)
3871 ofp_port_t prev_nf_output_iface = ctx->nf_output_iface;
3873 ctx->nf_output_iface = NF_OUT_DROP;
3877 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port, NULL);
3880 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3881 0, may_packet_in, true);
3887 flood_packets(ctx, false);
3890 flood_packets(ctx, true);
3892 case OFPP_CONTROLLER:
3893 execute_controller_action(ctx, max_len,
3894 (ctx->in_group ? OFPR_GROUP
3895 : ctx->in_action_set ? OFPR_ACTION_SET
3903 if (port != ctx->xin->flow.in_port.ofp_port) {
3904 compose_output_action(ctx, port, NULL);
3906 xlate_report(ctx, "skipping output to input port");
3911 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3912 ctx->nf_output_iface = NF_OUT_FLOOD;
3913 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3914 ctx->nf_output_iface = prev_nf_output_iface;
3915 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3916 ctx->nf_output_iface != NF_OUT_FLOOD) {
3917 ctx->nf_output_iface = NF_OUT_MULTI;
3922 xlate_output_reg_action(struct xlate_ctx *ctx,
3923 const struct ofpact_output_reg *or)
3925 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3926 if (port <= UINT16_MAX) {
3927 union mf_subvalue value;
3929 memset(&value, 0xff, sizeof value);
3930 mf_write_subfield_flow(&or->src, &value, &ctx->wc->masks);
3931 xlate_output_action(ctx, u16_to_ofp(port),
3932 or->max_len, false);
3937 xlate_enqueue_action(struct xlate_ctx *ctx,
3938 const struct ofpact_enqueue *enqueue)
3940 ofp_port_t ofp_port = enqueue->port;
3941 uint32_t queue_id = enqueue->queue;
3942 uint32_t flow_priority, priority;
3945 /* Translate queue to priority. */
3946 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
3948 /* Fall back to ordinary output action. */
3949 xlate_output_action(ctx, enqueue->port, 0, false);
3953 /* Check output port. */
3954 if (ofp_port == OFPP_IN_PORT) {
3955 ofp_port = ctx->xin->flow.in_port.ofp_port;
3956 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
3960 /* Add datapath actions. */
3961 flow_priority = ctx->xin->flow.skb_priority;
3962 ctx->xin->flow.skb_priority = priority;
3963 compose_output_action(ctx, ofp_port, NULL);
3964 ctx->xin->flow.skb_priority = flow_priority;
3966 /* Update NetFlow output port. */
3967 if (ctx->nf_output_iface == NF_OUT_DROP) {
3968 ctx->nf_output_iface = ofp_port;
3969 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3970 ctx->nf_output_iface = NF_OUT_MULTI;
3975 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3977 uint32_t skb_priority;
3979 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
3980 ctx->xin->flow.skb_priority = skb_priority;
3982 /* Couldn't translate queue to a priority. Nothing to do. A warning
3983 * has already been logged. */
3988 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
3990 const struct xbridge *xbridge = xbridge_;
4001 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
4004 port = get_ofp_port(xbridge, ofp_port);
4005 return port ? port->may_enable : false;
4010 xlate_bundle_action(struct xlate_ctx *ctx,
4011 const struct ofpact_bundle *bundle)
4015 port = bundle_execute(bundle, &ctx->xin->flow, ctx->wc, slave_enabled_cb,
4016 CONST_CAST(struct xbridge *, ctx->xbridge));
4017 if (bundle->dst.field) {
4018 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow, ctx->wc);
4020 xlate_output_action(ctx, port, 0, false);
4025 xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
4026 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
4028 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
4029 if (ctx->xin->may_learn) {
4030 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
4035 xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
4037 learn_mask(learn, ctx->wc);
4039 if (ctx->xin->xcache) {
4040 struct xc_entry *entry;
4042 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
4043 entry->u.learn.ofproto = ctx->xbridge->ofproto;
4044 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
4045 entry->u.learn.ofpacts = ofpbuf_new(64);
4046 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
4047 entry->u.learn.ofpacts);
4048 } else if (ctx->xin->may_learn) {
4049 uint64_t ofpacts_stub[1024 / 8];
4050 struct ofputil_flow_mod fm;
4051 struct ofpbuf ofpacts;
4053 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
4054 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
4055 ofpbuf_uninit(&ofpacts);
4060 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
4061 uint16_t idle_timeout, uint16_t hard_timeout)
4063 if (tcp_flags & (TCP_FIN | TCP_RST)) {
4064 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
4069 xlate_fin_timeout(struct xlate_ctx *ctx,
4070 const struct ofpact_fin_timeout *oft)
4073 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
4074 oft->fin_idle_timeout, oft->fin_hard_timeout);
4075 if (ctx->xin->xcache) {
4076 struct xc_entry *entry;
4078 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
4079 /* XC_RULE already holds a reference on the rule, none is taken
4081 entry->u.fin.rule = ctx->rule;
4082 entry->u.fin.idle = oft->fin_idle_timeout;
4083 entry->u.fin.hard = oft->fin_hard_timeout;
4089 xlate_sample_action(struct xlate_ctx *ctx,
4090 const struct ofpact_sample *os)
4092 /* Scale the probability from 16-bit to 32-bit while representing
4093 * the same percentage. */
4094 uint32_t probability = (os->probability << 16) | os->probability;
4096 if (!ctx->xbridge->support.variable_length_userdata) {
4097 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4099 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
4100 "lacks support (needs Linux 3.10+ or kernel module from "
4105 xlate_commit_actions(ctx);
4107 union user_action_cookie cookie = {
4109 .type = USER_ACTION_COOKIE_FLOW_SAMPLE,
4110 .probability = os->probability,
4111 .collector_set_id = os->collector_set_id,
4112 .obs_domain_id = os->obs_domain_id,
4113 .obs_point_id = os->obs_point_id,
4116 compose_sample_action(ctx, probability, &cookie, sizeof cookie.flow_sample,
4121 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
4123 if (xport->config & (is_stp(&ctx->xin->flow)
4124 ? OFPUTIL_PC_NO_RECV_STP
4125 : OFPUTIL_PC_NO_RECV)) {
4129 /* Only drop packets here if both forwarding and learning are
4130 * disabled. If just learning is enabled, we need to have
4131 * OFPP_NORMAL and the learning action have a look at the packet
4132 * before we can drop it. */
4133 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
4134 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
4142 xlate_write_actions__(struct xlate_ctx *ctx,
4143 const struct ofpact *ofpacts, size_t ofpacts_len)
4145 /* Maintain actset_output depending on the contents of the action set:
4147 * - OFPP_UNSET, if there is no "output" action.
4149 * - The output port, if there is an "output" action and no "group"
4152 * - OFPP_UNSET, if there is a "group" action.
4154 if (!ctx->action_set_has_group) {
4155 const struct ofpact *a;
4156 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4157 if (a->type == OFPACT_OUTPUT) {
4158 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(a)->port;
4159 } else if (a->type == OFPACT_GROUP) {
4160 ctx->xin->flow.actset_output = OFPP_UNSET;
4161 ctx->action_set_has_group = true;
4167 ofpbuf_put(&ctx->action_set, ofpacts, ofpacts_len);
4171 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact_nest *a)
4173 xlate_write_actions__(ctx, a->actions, ofpact_nest_get_action_len(a));
4177 xlate_action_set(struct xlate_ctx *ctx)
4179 uint64_t action_list_stub[1024 / 64];
4180 struct ofpbuf action_list;
4182 ctx->in_action_set = true;
4183 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
4184 ofpacts_execute_action_set(&action_list, &ctx->action_set);
4185 /* Clear the action set, as it is not needed any more. */
4186 ofpbuf_clear(&ctx->action_set);
4187 do_xlate_actions(action_list.data, action_list.size, ctx);
4188 ctx->in_action_set = false;
4189 ofpbuf_uninit(&action_list);
4193 freeze_put_unroll_xlate(struct xlate_ctx *ctx)
4195 struct ofpact_unroll_xlate *unroll = ctx->frozen_actions.header;
4197 /* Restore the table_id and rule cookie for a potential PACKET
4200 (ctx->table_id != unroll->rule_table_id
4201 || ctx->rule_cookie != unroll->rule_cookie)) {
4202 unroll = ofpact_put_UNROLL_XLATE(&ctx->frozen_actions);
4203 unroll->rule_table_id = ctx->table_id;
4204 unroll->rule_cookie = ctx->rule_cookie;
4205 ctx->frozen_actions.header = unroll;
4210 /* Copy actions 'a' through 'end' to ctx->frozen_actions, which will be
4211 * executed after thawing. Inserts an UNROLL_XLATE action, if none is already
4212 * present, before any action that may depend on the current table ID or flow
4215 freeze_unroll_actions(const struct ofpact *a, const struct ofpact *end,
4216 struct xlate_ctx *ctx)
4218 for (; a < end; a = ofpact_next(a)) {
4220 case OFPACT_OUTPUT_REG:
4223 case OFPACT_CONTROLLER:
4224 case OFPACT_DEC_MPLS_TTL:
4225 case OFPACT_DEC_TTL:
4226 /* These actions may generate asynchronous messages, which include
4227 * table ID and flow cookie information. */
4228 freeze_put_unroll_xlate(ctx);
4231 case OFPACT_RESUBMIT:
4232 if (ofpact_get_RESUBMIT(a)->table_id == 0xff) {
4233 /* This resubmit action is relative to the current table, so we
4234 * need to track what table that is.*/
4235 freeze_put_unroll_xlate(ctx);
4239 case OFPACT_SET_TUNNEL:
4240 case OFPACT_REG_MOVE:
4241 case OFPACT_SET_FIELD:
4242 case OFPACT_STACK_PUSH:
4243 case OFPACT_STACK_POP:
4245 case OFPACT_WRITE_METADATA:
4246 case OFPACT_GOTO_TABLE:
4247 case OFPACT_ENQUEUE:
4248 case OFPACT_SET_VLAN_VID:
4249 case OFPACT_SET_VLAN_PCP:
4250 case OFPACT_STRIP_VLAN:
4251 case OFPACT_PUSH_VLAN:
4252 case OFPACT_SET_ETH_SRC:
4253 case OFPACT_SET_ETH_DST:
4254 case OFPACT_SET_IPV4_SRC:
4255 case OFPACT_SET_IPV4_DST:
4256 case OFPACT_SET_IP_DSCP:
4257 case OFPACT_SET_IP_ECN:
4258 case OFPACT_SET_IP_TTL:
4259 case OFPACT_SET_L4_SRC_PORT:
4260 case OFPACT_SET_L4_DST_PORT:
4261 case OFPACT_SET_QUEUE:
4262 case OFPACT_POP_QUEUE:
4263 case OFPACT_PUSH_MPLS:
4264 case OFPACT_POP_MPLS:
4265 case OFPACT_SET_MPLS_LABEL:
4266 case OFPACT_SET_MPLS_TC:
4267 case OFPACT_SET_MPLS_TTL:
4268 case OFPACT_MULTIPATH:
4271 case OFPACT_UNROLL_XLATE:
4272 case OFPACT_FIN_TIMEOUT:
4273 case OFPACT_CLEAR_ACTIONS:
4274 case OFPACT_WRITE_ACTIONS:
4277 case OFPACT_DEBUG_RECIRC:
4280 /* These may not generate PACKET INs. */
4284 case OFPACT_CONJUNCTION:
4285 /* These need not be copied for restoration. */
4288 /* Copy the action over. */
4289 ofpbuf_put(&ctx->frozen_actions, a, OFPACT_ALIGN(a->len));
4294 put_ct_mark(const struct flow *flow, struct flow *base_flow,
4295 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4302 odp_attr.key = flow->ct_mark;
4303 odp_attr.mask = wc->masks.ct_mark;
4305 if (odp_attr.mask && odp_attr.key != base_flow->ct_mark) {
4306 nl_msg_put_unspec(odp_actions, OVS_CT_ATTR_MARK, &odp_attr,
4312 put_ct_label(const struct flow *flow, struct flow *base_flow,
4313 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4315 if (!ovs_u128_is_zero(&wc->masks.ct_label)
4316 && !ovs_u128_equals(&flow->ct_label, &base_flow->ct_label)) {
4322 odp_ct_label = nl_msg_put_unspec_uninit(odp_actions,
4324 sizeof(*odp_ct_label));
4325 odp_ct_label->key = flow->ct_label;
4326 odp_ct_label->mask = wc->masks.ct_label;
4331 put_ct_helper(struct ofpbuf *odp_actions, struct ofpact_conntrack *ofc)
4334 if (ofc->alg == IPPORT_FTP) {
4335 nl_msg_put_string(odp_actions, OVS_CT_ATTR_HELPER, "ftp");
4337 VLOG_WARN("Cannot serialize ct_helper %d\n", ofc->alg);
4343 put_ct_nat(struct xlate_ctx *ctx)
4345 struct ofpact_nat *ofn = ctx->ct_nat_action;
4352 nat_offset = nl_msg_start_nested(ctx->odp_actions, OVS_CT_ATTR_NAT);
4353 if (ofn->flags & NX_NAT_F_SRC || ofn->flags & NX_NAT_F_DST) {
4354 nl_msg_put_flag(ctx->odp_actions, ofn->flags & NX_NAT_F_SRC
4355 ? OVS_NAT_ATTR_SRC : OVS_NAT_ATTR_DST);
4356 if (ofn->flags & NX_NAT_F_PERSISTENT) {
4357 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PERSISTENT);
4359 if (ofn->flags & NX_NAT_F_PROTO_HASH) {
4360 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PROTO_HASH);
4361 } else if (ofn->flags & NX_NAT_F_PROTO_RANDOM) {
4362 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PROTO_RANDOM);
4364 if (ofn->range_af == AF_INET) {
4365 nl_msg_put_be32(ctx->odp_actions, OVS_NAT_ATTR_IP_MIN,
4366 ofn->range.addr.ipv4.min);
4367 if (ofn->range.addr.ipv4.max &&
4368 (ntohl(ofn->range.addr.ipv4.max)
4369 > ntohl(ofn->range.addr.ipv4.min))) {
4370 nl_msg_put_be32(ctx->odp_actions, OVS_NAT_ATTR_IP_MAX,
4371 ofn->range.addr.ipv4.max);
4373 } else if (ofn->range_af == AF_INET6) {
4374 nl_msg_put_unspec(ctx->odp_actions, OVS_NAT_ATTR_IP_MIN,
4375 &ofn->range.addr.ipv6.min,
4376 sizeof ofn->range.addr.ipv6.min);
4377 if (!ipv6_mask_is_any(&ofn->range.addr.ipv6.max) &&
4378 memcmp(&ofn->range.addr.ipv6.max, &ofn->range.addr.ipv6.min,
4379 sizeof ofn->range.addr.ipv6.max) > 0) {
4380 nl_msg_put_unspec(ctx->odp_actions, OVS_NAT_ATTR_IP_MAX,
4381 &ofn->range.addr.ipv6.max,
4382 sizeof ofn->range.addr.ipv6.max);
4385 if (ofn->range_af != AF_UNSPEC && ofn->range.proto.min) {
4386 nl_msg_put_u16(ctx->odp_actions, OVS_NAT_ATTR_PROTO_MIN,
4387 ofn->range.proto.min);
4388 if (ofn->range.proto.max &&
4389 ofn->range.proto.max > ofn->range.proto.min) {
4390 nl_msg_put_u16(ctx->odp_actions, OVS_NAT_ATTR_PROTO_MAX,
4391 ofn->range.proto.max);
4395 nl_msg_end_nested(ctx->odp_actions, nat_offset);
4399 compose_conntrack_action(struct xlate_ctx *ctx, struct ofpact_conntrack *ofc)
4401 ovs_u128 old_ct_label = ctx->base_flow.ct_label;
4402 uint32_t old_ct_mark = ctx->base_flow.ct_mark;
4406 /* Ensure that any prior actions are applied before composing the new
4407 * conntrack action. */
4408 xlate_commit_actions(ctx);
4410 /* Process nested actions first, to populate the key. */
4411 ctx->ct_nat_action = NULL;
4412 do_xlate_actions(ofc->actions, ofpact_ct_get_action_len(ofc), ctx);
4414 if (ofc->zone_src.field) {
4415 zone = mf_get_subfield(&ofc->zone_src, &ctx->xin->flow);
4417 zone = ofc->zone_imm;
4420 ct_offset = nl_msg_start_nested(ctx->odp_actions, OVS_ACTION_ATTR_CT);
4421 if (ofc->flags & NX_CT_F_COMMIT) {
4422 nl_msg_put_flag(ctx->odp_actions, OVS_CT_ATTR_COMMIT);
4424 nl_msg_put_u16(ctx->odp_actions, OVS_CT_ATTR_ZONE, zone);
4425 put_ct_mark(&ctx->xin->flow, &ctx->base_flow, ctx->odp_actions, ctx->wc);
4426 put_ct_label(&ctx->xin->flow, &ctx->base_flow, ctx->odp_actions, ctx->wc);
4427 put_ct_helper(ctx->odp_actions, ofc);
4429 ctx->ct_nat_action = NULL;
4430 nl_msg_end_nested(ctx->odp_actions, ct_offset);
4432 /* Restore the original ct fields in the key. These should only be exposed
4433 * after recirculation to another table. */
4434 ctx->base_flow.ct_mark = old_ct_mark;
4435 ctx->base_flow.ct_label = old_ct_label;
4437 if (ofc->recirc_table == NX_CT_RECIRC_NONE) {
4438 /* If we do not recirculate as part of this action, hide the results of
4439 * connection tracking from subsequent recirculations. */
4440 ctx->conntracked = false;
4442 /* Use ct_* fields from datapath during recirculation upcall. */
4443 ctx->conntracked = true;
4444 compose_recirculate_and_fork(ctx, ofc->recirc_table);
4449 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4450 struct xlate_ctx *ctx)
4452 struct flow_wildcards *wc = ctx->wc;
4453 struct flow *flow = &ctx->xin->flow;
4454 const struct ofpact *a;
4456 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
4457 tnl_neigh_snoop(flow, wc, ctx->xbridge->name);
4459 /* dl_type already in the mask, not set below. */
4461 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4462 struct ofpact_controller *controller;
4463 const struct ofpact_metadata *metadata;
4464 const struct ofpact_set_field *set_field;
4465 const struct mf_field *mf;
4472 /* Check if need to store the remaining actions for later
4474 if (ctx->freezing) {
4475 freeze_unroll_actions(a, ofpact_end(ofpacts, ofpacts_len),
4483 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
4484 ofpact_get_OUTPUT(a)->max_len, true);
4488 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
4489 /* Group could not be found. */
4494 case OFPACT_CONTROLLER:
4495 controller = ofpact_get_CONTROLLER(a);
4496 if (controller->pause) {
4497 ctx->pause = controller;
4498 ctx->xout->slow |= SLOW_CONTROLLER;
4499 ctx_trigger_freeze(ctx);
4502 execute_controller_action(ctx, controller->max_len,
4504 controller->controller_id,
4505 controller->userdata,
4506 controller->userdata_len);
4510 case OFPACT_ENQUEUE:
4511 memset(&wc->masks.skb_priority, 0xff,
4512 sizeof wc->masks.skb_priority);
4513 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
4516 case OFPACT_SET_VLAN_VID:
4517 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
4518 if (flow->vlan_tci & htons(VLAN_CFI) ||
4519 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
4520 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
4521 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
4526 case OFPACT_SET_VLAN_PCP:
4527 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
4528 if (flow->vlan_tci & htons(VLAN_CFI) ||
4529 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
4530 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
4531 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
4532 << VLAN_PCP_SHIFT) | VLAN_CFI);
4536 case OFPACT_STRIP_VLAN:
4537 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4538 flow->vlan_tci = htons(0);
4541 case OFPACT_PUSH_VLAN:
4542 /* XXX 802.1AD(QinQ) */
4543 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4544 flow->vlan_tci = htons(VLAN_CFI);
4547 case OFPACT_SET_ETH_SRC:
4548 WC_MASK_FIELD(wc, dl_src);
4549 flow->dl_src = ofpact_get_SET_ETH_SRC(a)->mac;
4552 case OFPACT_SET_ETH_DST:
4553 WC_MASK_FIELD(wc, dl_dst);
4554 flow->dl_dst = ofpact_get_SET_ETH_DST(a)->mac;
4557 case OFPACT_SET_IPV4_SRC:
4558 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4559 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
4560 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
4564 case OFPACT_SET_IPV4_DST:
4565 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4566 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
4567 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
4571 case OFPACT_SET_IP_DSCP:
4572 if (is_ip_any(flow)) {
4573 wc->masks.nw_tos |= IP_DSCP_MASK;
4574 flow->nw_tos &= ~IP_DSCP_MASK;
4575 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
4579 case OFPACT_SET_IP_ECN:
4580 if (is_ip_any(flow)) {
4581 wc->masks.nw_tos |= IP_ECN_MASK;
4582 flow->nw_tos &= ~IP_ECN_MASK;
4583 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
4587 case OFPACT_SET_IP_TTL:
4588 if (is_ip_any(flow)) {
4589 wc->masks.nw_ttl = 0xff;
4590 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
4594 case OFPACT_SET_L4_SRC_PORT:
4595 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4596 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4597 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
4598 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
4602 case OFPACT_SET_L4_DST_PORT:
4603 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4604 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4605 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
4606 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
4610 case OFPACT_RESUBMIT:
4611 /* Freezing complicates resubmit. Some action in the flow
4612 * entry found by resubmit might trigger freezing. If that
4613 * happens, then we do not want to execute the resubmit again after
4614 * during thawing, so we want to skip back to the head of the loop
4615 * to avoid that, only adding any actions that follow the resubmit
4616 * to the frozen actions.
4618 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
4621 case OFPACT_SET_TUNNEL:
4622 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
4625 case OFPACT_SET_QUEUE:
4626 memset(&wc->masks.skb_priority, 0xff,
4627 sizeof wc->masks.skb_priority);
4628 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
4631 case OFPACT_POP_QUEUE:
4632 memset(&wc->masks.skb_priority, 0xff,
4633 sizeof wc->masks.skb_priority);
4634 flow->skb_priority = ctx->orig_skb_priority;
4637 case OFPACT_REG_MOVE:
4638 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
4641 case OFPACT_SET_FIELD:
4642 set_field = ofpact_get_SET_FIELD(a);
4643 mf = set_field->field;
4645 /* Set field action only ever overwrites packet's outermost
4646 * applicable header fields. Do nothing if no header exists. */
4647 if (mf->id == MFF_VLAN_VID) {
4648 wc->masks.vlan_tci |= htons(VLAN_CFI);
4649 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4652 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
4653 /* 'dl_type' is already unwildcarded. */
4654 && !eth_type_mpls(flow->dl_type)) {
4657 /* A flow may wildcard nw_frag. Do nothing if setting a transport
4658 * header field on a packet that does not have them. */
4659 mf_mask_field_and_prereqs(mf, wc);
4660 if (mf_are_prereqs_ok(mf, flow)) {
4661 mf_set_flow_value_masked(mf, &set_field->value,
4662 &set_field->mask, flow);
4666 case OFPACT_STACK_PUSH:
4667 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
4671 case OFPACT_STACK_POP:
4672 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
4676 case OFPACT_PUSH_MPLS:
4677 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
4680 case OFPACT_POP_MPLS:
4681 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
4684 case OFPACT_SET_MPLS_LABEL:
4685 compose_set_mpls_label_action(
4686 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
4689 case OFPACT_SET_MPLS_TC:
4690 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
4693 case OFPACT_SET_MPLS_TTL:
4694 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
4697 case OFPACT_DEC_MPLS_TTL:
4698 if (compose_dec_mpls_ttl_action(ctx)) {
4703 case OFPACT_DEC_TTL:
4704 wc->masks.nw_ttl = 0xff;
4705 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
4711 /* Nothing to do. */
4714 case OFPACT_MULTIPATH:
4715 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
4719 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
4722 case OFPACT_OUTPUT_REG:
4723 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
4727 xlate_learn_action(ctx, ofpact_get_LEARN(a));
4730 case OFPACT_CONJUNCTION: {
4731 /* A flow with a "conjunction" action represents part of a special
4732 * kind of "set membership match". Such a flow should not actually
4733 * get executed, but it could via, say, a "packet-out", even though
4734 * that wouldn't be useful. Log it to help debugging. */
4735 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4736 VLOG_INFO_RL(&rl, "executing no-op conjunction action");
4744 case OFPACT_UNROLL_XLATE: {
4745 struct ofpact_unroll_xlate *unroll = ofpact_get_UNROLL_XLATE(a);
4747 /* Restore translation context data that was stored earlier. */
4748 ctx->table_id = unroll->rule_table_id;
4749 ctx->rule_cookie = unroll->rule_cookie;
4752 case OFPACT_FIN_TIMEOUT:
4753 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4754 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
4757 case OFPACT_CLEAR_ACTIONS:
4758 ofpbuf_clear(&ctx->action_set);
4759 ctx->xin->flow.actset_output = OFPP_UNSET;
4760 ctx->action_set_has_group = false;
4763 case OFPACT_WRITE_ACTIONS:
4764 xlate_write_actions(ctx, ofpact_get_WRITE_ACTIONS(a));
4767 case OFPACT_WRITE_METADATA:
4768 metadata = ofpact_get_WRITE_METADATA(a);
4769 flow->metadata &= ~metadata->mask;
4770 flow->metadata |= metadata->metadata & metadata->mask;
4774 /* Not implemented yet. */
4777 case OFPACT_GOTO_TABLE: {
4778 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
4780 ovs_assert(ctx->table_id < ogt->table_id);
4782 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
4783 ogt->table_id, true, true);
4788 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
4792 compose_conntrack_action(ctx, ofpact_get_CT(a));
4796 /* This will be processed by compose_conntrack_action(). */
4797 ctx->ct_nat_action = ofpact_get_NAT(a);
4800 case OFPACT_DEBUG_RECIRC:
4801 ctx_trigger_freeze(ctx);
4806 /* Check if need to store this and the remaining actions for later
4808 if (!ctx->error && ctx->exit && ctx_first_frozen_action(ctx)) {
4809 freeze_unroll_actions(a, ofpact_end(ofpacts, ofpacts_len), ctx);
4816 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
4817 const struct flow *flow, ofp_port_t in_port,
4818 struct rule_dpif *rule, uint16_t tcp_flags,
4819 const struct dp_packet *packet, struct flow_wildcards *wc,
4820 struct ofpbuf *odp_actions)
4822 xin->ofproto = ofproto;
4824 xin->flow.in_port.ofp_port = in_port;
4825 xin->flow.actset_output = OFPP_UNSET;
4826 xin->packet = packet;
4827 xin->may_learn = packet != NULL;
4830 xin->ofpacts = NULL;
4831 xin->ofpacts_len = 0;
4832 xin->tcp_flags = tcp_flags;
4833 xin->resubmit_hook = NULL;
4834 xin->report_hook = NULL;
4835 xin->resubmit_stats = NULL;
4839 xin->odp_actions = odp_actions;
4841 /* Do recirc lookup. */
4842 xin->frozen_state = NULL;
4843 if (flow->recirc_id) {
4844 const struct recirc_id_node *node
4845 = recirc_id_node_find(flow->recirc_id);
4847 xin->frozen_state = &node->state;
4853 xlate_out_uninit(struct xlate_out *xout)
4856 recirc_refs_unref(&xout->recircs);
4860 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
4861 * into datapath actions, using 'ctx', and discards the datapath actions. */
4863 xlate_actions_for_side_effects(struct xlate_in *xin)
4865 struct xlate_out xout;
4866 enum xlate_error error;
4868 error = xlate_actions(xin, &xout);
4870 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4872 VLOG_WARN_RL(&rl, "xlate_actions failed (%s)!", xlate_strerror(error));
4875 xlate_out_uninit(&xout);
4878 static struct skb_priority_to_dscp *
4879 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
4881 struct skb_priority_to_dscp *pdscp;
4884 hash = hash_int(skb_priority, 0);
4885 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
4886 if (pdscp->skb_priority == skb_priority) {
4894 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
4897 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
4898 *dscp = pdscp ? pdscp->dscp : 0;
4899 return pdscp != NULL;
4903 count_skb_priorities(const struct xport *xport)
4905 return hmap_count(&xport->skb_priorities);
4909 clear_skb_priorities(struct xport *xport)
4911 struct skb_priority_to_dscp *pdscp, *next;
4913 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
4914 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
4920 actions_output_to_local_port(const struct xlate_ctx *ctx)
4922 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
4923 const struct nlattr *a;
4926 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->odp_actions->data,
4927 ctx->odp_actions->size) {
4928 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
4929 && nl_attr_get_odp_port(a) == local_odp_port) {
4936 #if defined(__linux__)
4937 /* Returns the maximum number of packets that the Linux kernel is willing to
4938 * queue up internally to certain kinds of software-implemented ports, or the
4939 * default (and rarely modified) value if it cannot be determined. */
4941 netdev_max_backlog(void)
4943 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
4944 static int max_backlog = 1000; /* The normal default value. */
4946 if (ovsthread_once_start(&once)) {
4947 static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
4951 stream = fopen(filename, "r");
4953 VLOG_INFO("%s: open failed (%s)", filename, ovs_strerror(errno));
4955 if (fscanf(stream, "%d", &n) != 1) {
4956 VLOG_WARN("%s: read error", filename);
4957 } else if (n <= 100) {
4958 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
4964 ovsthread_once_done(&once);
4966 VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
4972 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
4975 count_output_actions(const struct ofpbuf *odp_actions)
4977 const struct nlattr *a;
4981 NL_ATTR_FOR_EACH_UNSAFE (a, left, odp_actions->data, odp_actions->size) {
4982 if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
4988 #endif /* defined(__linux__) */
4990 /* Returns true if 'odp_actions' contains more output actions than the datapath
4991 * can reliably handle in one go. On Linux, this is the value of the
4992 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
4993 * packets that the kernel is willing to queue up for processing while the
4994 * datapath is processing a set of actions. */
4996 too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
4999 return (odp_actions->size / NL_A_U32_SIZE > netdev_max_backlog()
5000 && count_output_actions(odp_actions) > netdev_max_backlog());
5002 /* OSes other than Linux might have similar limits, but we don't know how
5003 * to determine them.*/
5009 xlate_wc_init(struct xlate_ctx *ctx)
5011 flow_wildcards_init_catchall(ctx->wc);
5013 /* Some fields we consider to always be examined. */
5014 WC_MASK_FIELD(ctx->wc, in_port);
5015 WC_MASK_FIELD(ctx->wc, dl_type);
5016 if (is_ip_any(&ctx->xin->flow)) {
5017 WC_MASK_FIELD_MASK(ctx->wc, nw_frag, FLOW_NW_FRAG_MASK);
5020 if (ctx->xbridge->support.odp.recirc) {
5021 /* Always exactly match recirc_id when datapath supports
5023 WC_MASK_FIELD(ctx->wc, recirc_id);
5026 if (ctx->xbridge->netflow) {
5027 netflow_mask_wc(&ctx->xin->flow, ctx->wc);
5030 tnl_wc_init(&ctx->xin->flow, ctx->wc);
5034 xlate_wc_finish(struct xlate_ctx *ctx)
5036 /* Clear the metadata and register wildcard masks, because we won't
5037 * use non-header fields as part of the cache. */
5038 flow_wildcards_clear_non_packet_fields(ctx->wc);
5040 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
5041 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
5042 * represent these fields. The datapath interface, on the other hand,
5043 * represents them with just 8 bits each. This means that if the high
5044 * 8 bits of the masks for these fields somehow become set, then they
5045 * will get chopped off by a round trip through the datapath, and
5046 * revalidation will spot that as an inconsistency and delete the flow.
5047 * Avoid the problem here by making sure that only the low 8 bits of
5048 * either field can be unwildcarded for ICMP.
5050 if (is_icmpv4(&ctx->xin->flow) || is_icmpv6(&ctx->xin->flow)) {
5051 ctx->wc->masks.tp_src &= htons(UINT8_MAX);
5052 ctx->wc->masks.tp_dst &= htons(UINT8_MAX);
5054 /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
5055 if (ctx->wc->masks.vlan_tci) {
5056 ctx->wc->masks.vlan_tci |= htons(VLAN_CFI);
5060 /* Translates the flow, actions, or rule in 'xin' into datapath actions in
5062 * The caller must take responsibility for eventually freeing 'xout', with
5063 * xlate_out_uninit().
5064 * Returns 'XLATE_OK' if translation was successful. In case of an error an
5065 * empty set of actions will be returned in 'xin->odp_actions' (if non-NULL),
5066 * so that most callers may ignore the return value and transparently install a
5067 * drop flow when the translation fails. */
5069 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
5071 *xout = (struct xlate_out) {
5073 .recircs = RECIRC_REFS_EMPTY_INITIALIZER,
5076 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5077 struct xbridge *xbridge = xbridge_lookup(xcfg, xin->ofproto);
5079 return XLATE_BRIDGE_NOT_FOUND;
5082 struct flow *flow = &xin->flow;
5084 union mf_subvalue stack_stub[1024 / sizeof(union mf_subvalue)];
5085 uint64_t action_set_stub[1024 / 8];
5086 uint64_t frozen_actions_stub[1024 / 8];
5087 struct flow_wildcards scratch_wc;
5088 uint64_t actions_stub[256 / 8];
5089 struct ofpbuf scratch_actions = OFPBUF_STUB_INITIALIZER(actions_stub);
5090 struct xlate_ctx ctx = {
5094 .orig_tunnel_ipv6_dst = flow_tnl_dst(&flow->tunnel),
5096 .stack = OFPBUF_STUB_INITIALIZER(stack_stub),
5098 .wc = xin->wc ? xin->wc : &scratch_wc,
5099 .odp_actions = xin->odp_actions ? xin->odp_actions : &scratch_actions,
5101 .recurse = xin->recurse,
5102 .resubmits = xin->resubmits,
5104 .in_action_set = false,
5107 .rule_cookie = OVS_BE64_MAX,
5108 .orig_skb_priority = flow->skb_priority,
5109 .sflow_n_outputs = 0,
5110 .sflow_odp_port = 0,
5111 .nf_output_iface = NF_OUT_DROP,
5117 .frozen_actions = OFPBUF_STUB_INITIALIZER(frozen_actions_stub),
5120 .conntracked = false,
5122 .ct_nat_action = NULL,
5124 .action_set_has_group = false,
5125 .action_set = OFPBUF_STUB_INITIALIZER(action_set_stub),
5128 /* 'base_flow' reflects the packet as it came in, but we need it to reflect
5129 * the packet as the datapath will treat it for output actions:
5131 * - Our datapath doesn't retain tunneling information without us
5132 * re-setting it, so clear the tunnel data.
5134 * - For VLAN splinters, a higher layer may pretend that the packet
5135 * came in on 'flow->in_port.ofp_port' with 'flow->vlan_tci'
5136 * attached, because that's how we want to treat it from an OpenFlow
5137 * perspective. But from the datapath's perspective it actually came
5138 * in on a VLAN device without any VLAN attached. So here we put the
5139 * datapath's view of the VLAN information in 'base_flow' to ensure
5140 * correct treatment.
5142 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
5143 if (flow->in_port.ofp_port
5144 != vsp_realdev_to_vlandev(xbridge->ofproto,
5145 flow->in_port.ofp_port,
5147 ctx.base_flow.vlan_tci = 0;
5150 ofpbuf_reserve(ctx.odp_actions, NL_A_U32_SIZE);
5152 xlate_wc_init(&ctx);
5155 COVERAGE_INC(xlate_actions);
5157 if (xin->frozen_state) {
5158 const struct frozen_state *state = xin->frozen_state;
5160 xlate_report(&ctx, "Thawing frozen state:");
5162 if (xin->ofpacts_len > 0 || ctx.rule) {
5163 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
5164 const char *conflict = xin->ofpacts_len ? "actions" : "rule";
5166 VLOG_WARN_RL(&rl, "Recirculation conflict (%s)!", conflict);
5167 xlate_report(&ctx, "- Recirculation conflict (%s)!", conflict);
5168 ctx.error = XLATE_RECIRCULATION_CONFLICT;
5172 /* Set the bridge for post-recirculation processing if needed. */
5173 if (!uuid_equals(ofproto_dpif_get_uuid(ctx.xbridge->ofproto),
5174 &state->ofproto_uuid)) {
5175 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5176 const struct xbridge *new_bridge
5177 = xbridge_lookup_by_uuid(xcfg, &state->ofproto_uuid);
5179 if (OVS_UNLIKELY(!new_bridge)) {
5180 /* Drop the packet if the bridge cannot be found. */
5181 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
5182 VLOG_WARN_RL(&rl, "Frozen bridge no longer exists.");
5183 xlate_report(&ctx, "- Frozen bridge no longer exists.");
5184 ctx.error = XLATE_BRIDGE_NOT_FOUND;
5187 ctx.xbridge = new_bridge;
5190 /* Set the thawed table id. Note: A table lookup is done only if there
5191 * are no frozen actions. */
5192 ctx.table_id = state->table_id;
5193 xlate_report(&ctx, "- Resuming from table %"PRIu8, ctx.table_id);
5195 if (!state->conntracked) {
5196 clear_conntrack(flow);
5199 /* Restore pipeline metadata. May change flow's in_port and other
5200 * metadata to the values that existed when freezing was triggered. */
5201 frozen_metadata_to_flow(&state->metadata, flow);
5203 /* Restore stack, if any. */
5205 ofpbuf_put(&ctx.stack, state->stack,
5206 state->n_stack * sizeof *state->stack);
5209 /* Restore mirror state. */
5210 ctx.mirrors = state->mirrors;
5212 /* Restore action set, if any. */
5213 if (state->action_set_len) {
5214 xlate_report_actions(&ctx, "- Restoring action set",
5215 state->action_set, state->action_set_len);
5217 flow->actset_output = OFPP_UNSET;
5218 xlate_write_actions__(&ctx, state->action_set,
5219 state->action_set_len);
5222 /* Restore frozen actions. If there are no actions, processing will
5223 * start with a lookup in the table set above. */
5224 xin->ofpacts = state->ofpacts;
5225 xin->ofpacts_len = state->ofpacts_len;
5226 if (state->ofpacts_len) {
5227 xlate_report_actions(&ctx, "- Restoring actions",
5228 xin->ofpacts, xin->ofpacts_len);
5230 } else if (OVS_UNLIKELY(flow->recirc_id)) {
5231 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
5233 VLOG_WARN_RL(&rl, "Recirculation context not found for ID %"PRIx32,
5235 ctx.error = XLATE_NO_RECIRCULATION_CONTEXT;
5238 /* The bridge is now known so obtain its table version. */
5239 ctx.tables_version = ofproto_dpif_get_tables_version(ctx.xbridge->ofproto);
5241 if (!xin->ofpacts && !ctx.rule) {
5242 ctx.rule = rule_dpif_lookup_from_table(
5243 ctx.xbridge->ofproto, ctx.tables_version, flow, xin->wc,
5244 ctx.xin->resubmit_stats, &ctx.table_id,
5245 flow->in_port.ofp_port, true, true);
5246 if (ctx.xin->resubmit_stats) {
5247 rule_dpif_credit_stats(ctx.rule, ctx.xin->resubmit_stats);
5249 if (ctx.xin->xcache) {
5250 struct xc_entry *entry;
5252 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
5253 entry->u.rule = ctx.rule;
5254 rule_dpif_ref(ctx.rule);
5257 if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
5258 ctx.xin->resubmit_hook(ctx.xin, ctx.rule, 0);
5262 /* Get the proximate input port of the packet. (If xin->frozen_state,
5263 * flow->in_port is the ultimate input port of the packet.) */
5264 struct xport *in_port = get_ofp_port(xbridge,
5265 ctx.base_flow.in_port.ofp_port);
5267 /* Tunnel stats only for not-thawed packets. */
5268 if (!xin->frozen_state && in_port && in_port->is_tunnel) {
5269 if (ctx.xin->resubmit_stats) {
5270 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
5272 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
5275 if (ctx.xin->xcache) {
5276 struct xc_entry *entry;
5278 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
5279 entry->u.dev.rx = netdev_ref(in_port->netdev);
5280 entry->u.dev.bfd = bfd_ref(in_port->bfd);
5284 if (!xin->frozen_state && process_special(&ctx, in_port)) {
5285 /* process_special() did all the processing for this packet.
5287 * We do not perform special processing on thawed packets, since that
5288 * was done before they were frozen and should not be redone. */
5289 } else if (in_port && in_port->xbundle
5290 && xbundle_mirror_out(xbridge, in_port->xbundle)) {
5291 if (ctx.xin->packet != NULL) {
5292 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5293 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
5294 "%s, which is reserved exclusively for mirroring",
5295 ctx.xbridge->name, in_port->xbundle->name);
5298 /* Sampling is done on initial reception; don't redo after thawing. */
5299 unsigned int user_cookie_offset = 0;
5300 if (!xin->frozen_state) {
5301 user_cookie_offset = compose_sflow_action(&ctx);
5302 compose_ipfix_action(&ctx, ODPP_NONE);
5304 size_t sample_actions_len = ctx.odp_actions->size;
5306 if (tnl_process_ecn(flow)
5307 && (!in_port || may_receive(in_port, &ctx))) {
5308 const struct ofpact *ofpacts;
5312 ofpacts = xin->ofpacts;
5313 ofpacts_len = xin->ofpacts_len;
5314 } else if (ctx.rule) {
5315 const struct rule_actions *actions
5316 = rule_dpif_get_actions(ctx.rule);
5317 ofpacts = actions->ofpacts;
5318 ofpacts_len = actions->ofpacts_len;
5319 ctx.rule_cookie = rule_dpif_get_flow_cookie(ctx.rule);
5324 mirror_ingress_packet(&ctx);
5325 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
5330 /* We've let OFPP_NORMAL and the learning action look at the
5331 * packet, so cancel all actions and freezing if forwarding is
5333 if (in_port && (!xport_stp_forward_state(in_port) ||
5334 !xport_rstp_forward_state(in_port))) {
5335 ctx.odp_actions->size = sample_actions_len;
5336 ctx_cancel_freeze(&ctx);
5337 ofpbuf_clear(&ctx.action_set);
5340 if (!ctx.freezing) {
5341 xlate_action_set(&ctx);
5344 finish_freezing(&ctx);
5348 /* Output only fully processed packets. */
5350 && xbridge->has_in_band
5351 && in_band_must_output_to_local_port(flow)
5352 && !actions_output_to_local_port(&ctx)) {
5353 compose_output_action(&ctx, OFPP_LOCAL, NULL);
5356 if (user_cookie_offset) {
5357 fix_sflow_action(&ctx, user_cookie_offset);
5361 if (nl_attr_oversized(ctx.odp_actions->size)) {
5362 /* These datapath actions are too big for a Netlink attribute, so we
5363 * can't hand them to the kernel directly. dpif_execute() can execute
5364 * them one by one with help, so just mark the result as SLOW_ACTION to
5365 * prevent the flow from being installed. */
5366 COVERAGE_INC(xlate_actions_oversize);
5367 ctx.xout->slow |= SLOW_ACTION;
5368 } else if (too_many_output_actions(ctx.odp_actions)) {
5369 COVERAGE_INC(xlate_actions_too_many_output);
5370 ctx.xout->slow |= SLOW_ACTION;
5373 /* Do netflow only for packets on initial reception, that are not sent to
5374 * the controller. We consider packets sent to the controller to be part
5375 * of the control plane rather than the data plane. */
5376 if (!xin->frozen_state
5378 && !(xout->slow & SLOW_CONTROLLER)) {
5379 if (ctx.xin->resubmit_stats) {
5380 netflow_flow_update(xbridge->netflow, flow,
5381 ctx.nf_output_iface,
5382 ctx.xin->resubmit_stats);
5384 if (ctx.xin->xcache) {
5385 struct xc_entry *entry;
5387 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
5388 entry->u.nf.netflow = netflow_ref(xbridge->netflow);
5389 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
5390 entry->u.nf.iface = ctx.nf_output_iface;
5395 xlate_wc_finish(&ctx);
5399 ofpbuf_uninit(&ctx.stack);
5400 ofpbuf_uninit(&ctx.action_set);
5401 ofpbuf_uninit(&ctx.frozen_actions);
5402 ofpbuf_uninit(&scratch_actions);
5404 /* Make sure we return a "drop flow" in case of an error. */
5407 if (xin->odp_actions) {
5408 ofpbuf_clear(xin->odp_actions);
5415 xlate_resume(struct ofproto_dpif *ofproto,
5416 const struct ofputil_packet_in_private *pin,
5417 struct ofpbuf *odp_actions,
5418 enum slow_path_reason *slow)
5420 struct dp_packet packet;
5421 dp_packet_use_const(&packet, pin->public.packet,
5422 pin->public.packet_len);
5425 flow_extract(&packet, &flow);
5427 struct xlate_in xin;
5428 xlate_in_init(&xin, ofproto, &flow, 0, NULL, ntohs(flow.tcp_flags),
5429 &packet, NULL, odp_actions);
5431 struct ofpact_note noop;
5432 ofpact_init_NOTE(&noop);
5435 bool any_actions = pin->actions_len > 0;
5436 struct frozen_state state = {
5437 .table_id = 0, /* Not the table where NXAST_PAUSE was executed. */
5438 .ofproto_uuid = pin->bridge,
5439 .stack = pin->stack,
5440 .n_stack = pin->n_stack,
5441 .mirrors = pin->mirrors,
5442 .conntracked = pin->conntracked,
5444 /* When there are no actions, xlate_actions() will search the flow
5445 * table. We don't want it to do that (we want it to resume), so
5446 * supply a no-op action if there aren't any.
5448 * (We can't necessarily avoid translating actions entirely if there
5449 * aren't any actions, because there might be some finishing-up to do
5450 * at the end of the pipeline, and we don't check for those
5452 .ofpacts = any_actions ? pin->actions : &noop.ofpact,
5453 .ofpacts_len = any_actions ? pin->actions_len : sizeof noop,
5455 .action_set = pin->action_set,
5456 .action_set_len = pin->action_set_len,
5458 frozen_metadata_from_flow(&state.metadata,
5459 &pin->public.flow_metadata.flow);
5460 xin.frozen_state = &state;
5462 struct xlate_out xout;
5463 enum xlate_error error = xlate_actions(&xin, &xout);
5465 xlate_out_uninit(&xout);
5467 /* xlate_actions() can generate a number of errors, but only
5468 * XLATE_BRIDGE_NOT_FOUND really stands out to me as one that we should be
5469 * sure to report over OpenFlow. The others could come up in packet-outs
5470 * or regular flow translation and I don't think that it's going to be too
5471 * useful to report them to the controller. */
5472 return error == XLATE_BRIDGE_NOT_FOUND ? OFPERR_NXR_STALE : 0;
5475 /* Sends 'packet' out 'ofport'.
5476 * May modify 'packet'.
5477 * Returns 0 if successful, otherwise a positive errno value. */
5479 xlate_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
5481 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5482 struct xport *xport;
5483 struct ofpact_output output;
5486 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
5487 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
5488 flow_extract(packet, &flow);
5489 flow.in_port.ofp_port = OFPP_NONE;
5491 xport = xport_lookup(xcfg, ofport);
5495 output.port = xport->ofp_port;
5498 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
5499 &output.ofpact, sizeof output,
5503 struct xlate_cache *
5504 xlate_cache_new(void)
5506 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
5508 ofpbuf_init(&xcache->entries, 512);
5512 static struct xc_entry *
5513 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
5515 struct xc_entry *entry;
5517 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
5524 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
5526 if (entry->u.dev.tx) {
5527 netdev_vport_inc_tx(entry->u.dev.tx, stats);
5529 if (entry->u.dev.rx) {
5530 netdev_vport_inc_rx(entry->u.dev.rx, stats);
5532 if (entry->u.dev.bfd) {
5533 bfd_account_rx(entry->u.dev.bfd, stats);
5538 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
5540 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5541 struct xbridge *xbridge;
5542 struct xbundle *xbundle;
5543 struct flow_wildcards wc;
5545 xbridge = xbridge_lookup(xcfg, ofproto);
5550 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
5556 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
5559 /* Push stats and perform side effects of flow translation. */
5561 xlate_push_stats(struct xlate_cache *xcache,
5562 const struct dpif_flow_stats *stats)
5564 struct xc_entry *entry;
5565 struct ofpbuf entries = xcache->entries;
5566 struct eth_addr dmac;
5568 if (!stats->n_packets) {
5572 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5573 switch (entry->type) {
5575 rule_dpif_credit_stats(entry->u.rule, stats);
5578 bond_account(entry->u.bond.bond, entry->u.bond.flow,
5579 entry->u.bond.vid, stats->n_bytes);
5582 xlate_cache_netdev(entry, stats);
5585 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
5586 entry->u.nf.iface, stats);
5589 mirror_update_stats(entry->u.mirror.mbridge,
5590 entry->u.mirror.mirrors,
5591 stats->n_packets, stats->n_bytes);
5594 ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
5597 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
5598 entry->u.normal.vlan);
5600 case XC_FIN_TIMEOUT:
5601 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
5602 entry->u.fin.idle, entry->u.fin.hard);
5605 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
5609 /* Lookup neighbor to avoid timeout. */
5610 tnl_neigh_lookup(entry->u.tnl_neigh_cache.br_name,
5611 &entry->u.tnl_neigh_cache.d_ipv6, &dmac);
5620 xlate_dev_unref(struct xc_entry *entry)
5622 if (entry->u.dev.tx) {
5623 netdev_close(entry->u.dev.tx);
5625 if (entry->u.dev.rx) {
5626 netdev_close(entry->u.dev.rx);
5628 if (entry->u.dev.bfd) {
5629 bfd_unref(entry->u.dev.bfd);
5634 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
5636 netflow_flow_clear(netflow, flow);
5637 netflow_unref(netflow);
5642 xlate_cache_clear(struct xlate_cache *xcache)
5644 struct xc_entry *entry;
5645 struct ofpbuf entries;
5651 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5652 switch (entry->type) {
5654 rule_dpif_unref(entry->u.rule);
5657 free(entry->u.bond.flow);
5658 bond_unref(entry->u.bond.bond);
5661 xlate_dev_unref(entry);
5664 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
5667 mbridge_unref(entry->u.mirror.mbridge);
5670 free(entry->u.learn.fm);
5671 ofpbuf_delete(entry->u.learn.ofpacts);
5674 free(entry->u.normal.flow);
5676 case XC_FIN_TIMEOUT:
5677 /* 'u.fin.rule' is always already held as a XC_RULE, which
5678 * has already released it's reference above. */
5681 group_dpif_unref(entry->u.group.group);
5690 ofpbuf_clear(&xcache->entries);
5694 xlate_cache_delete(struct xlate_cache *xcache)
5696 xlate_cache_clear(xcache);
5697 ofpbuf_uninit(&xcache->entries);