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 a packet was but is no longer MPLS (due to an MPLS pop action).
324 * This is a trigger for recirculation in cases where translating an action
325 * or looking up a flow requires access to the fields of the packet after
326 * the MPLS label stack that was originally present. */
329 /* True if conntrack has been performed on this packet during processing
330 * on the current bridge. This is used to determine whether conntrack
331 * state from the datapath should be honored after thawing. */
334 /* Pointer to an embedded NAT action in a conntrack action, or NULL. */
335 struct ofpact_nat *ct_nat_action;
337 /* OpenFlow 1.1+ action set.
339 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
340 * When translation is otherwise complete, ofpacts_execute_action_set()
341 * converts it to a set of "struct ofpact"s that can be translated into
342 * datapath actions. */
343 bool action_set_has_group; /* Action set contains OFPACT_GROUP? */
344 struct ofpbuf action_set; /* Action set. */
346 enum xlate_error error; /* Translation failed. */
349 const char *xlate_strerror(enum xlate_error error)
354 case XLATE_BRIDGE_NOT_FOUND:
355 return "Bridge not found";
356 case XLATE_RECURSION_TOO_DEEP:
357 return "Recursion too deep";
358 case XLATE_TOO_MANY_RESUBMITS:
359 return "Too many resubmits";
360 case XLATE_STACK_TOO_DEEP:
361 return "Stack too deep";
362 case XLATE_NO_RECIRCULATION_CONTEXT:
363 return "No recirculation context";
364 case XLATE_RECIRCULATION_CONFLICT:
365 return "Recirculation conflict";
366 case XLATE_TOO_MANY_MPLS_LABELS:
367 return "Too many MPLS labels";
369 return "Unknown error";
372 static void xlate_action_set(struct xlate_ctx *ctx);
373 static void xlate_commit_actions(struct xlate_ctx *ctx);
376 ctx_trigger_freeze(struct xlate_ctx *ctx)
379 ctx->freezing = true;
383 ctx_first_frozen_action(const struct xlate_ctx *ctx)
385 return !ctx->frozen_actions.size;
389 ctx_cancel_freeze(struct xlate_ctx *ctx)
392 ctx->freezing = false;
393 ofpbuf_clear(&ctx->frozen_actions);
394 ctx->frozen_actions.header = NULL;
398 static void finish_freezing(struct xlate_ctx *ctx);
400 /* A controller may use OFPP_NONE as the ingress port to indicate that
401 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
402 * when an input bundle is needed for validation (e.g., mirroring or
403 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
404 * any 'port' structs, so care must be taken when dealing with it. */
405 static struct xbundle ofpp_none_bundle = {
407 .vlan_mode = PORT_VLAN_TRUNK
410 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
411 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
412 * traffic egressing the 'ofport' with that priority should be marked with. */
413 struct skb_priority_to_dscp {
414 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
415 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
417 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
433 /* xlate_cache entries hold enough information to perform the side effects of
434 * xlate_actions() for a rule, without needing to perform rule translation
435 * from scratch. The primary usage of these is to submit statistics to objects
436 * that a flow relates to, although they may be used for other effects as well
437 * (for instance, refreshing hard timeouts for learned flows). */
441 struct rule_dpif *rule;
448 struct netflow *netflow;
453 struct mbridge *mbridge;
454 mirror_mask_t mirrors;
462 struct ofproto_dpif *ofproto;
463 struct ofputil_flow_mod *fm;
464 struct ofpbuf *ofpacts;
467 struct ofproto_dpif *ofproto;
472 struct rule_dpif *rule;
477 struct group_dpif *group;
478 struct ofputil_bucket *bucket;
481 char br_name[IFNAMSIZ];
482 struct in6_addr d_ipv6;
487 #define XC_ENTRY_FOR_EACH(ENTRY, ENTRIES, XCACHE) \
488 ENTRIES = XCACHE->entries; \
489 for (ENTRY = ofpbuf_try_pull(&ENTRIES, sizeof *ENTRY); \
491 ENTRY = ofpbuf_try_pull(&ENTRIES, sizeof *ENTRY))
494 struct ofpbuf entries;
497 /* Xlate config contains hash maps of all bridges, bundles and ports.
498 * Xcfgp contains the pointer to the current xlate configuration.
499 * When the main thread needs to change the configuration, it copies xcfgp to
500 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
501 * does not block handler and revalidator threads. */
503 struct hmap xbridges;
504 struct hmap xbundles;
507 static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
508 static struct xlate_cfg *new_xcfg = NULL;
510 static bool may_receive(const struct xport *, struct xlate_ctx *);
511 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
513 static void xlate_normal(struct xlate_ctx *);
514 static inline void xlate_report(struct xlate_ctx *, const char *, ...)
515 OVS_PRINTF_FORMAT(2, 3);
516 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
517 uint8_t table_id, bool may_packet_in,
518 bool honor_table_miss);
519 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
520 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
521 static void output_normal(struct xlate_ctx *, const struct xbundle *,
524 /* Optional bond recirculation parameter to compose_output_action(). */
525 struct xlate_bond_recirc {
526 uint32_t recirc_id; /* !0 Use recirculation instead of output. */
527 uint8_t hash_alg; /* !0 Compute hash for recirc before. */
528 uint32_t hash_basis; /* Compute hash for recirc before. */
531 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port,
532 const struct xlate_bond_recirc *xr);
534 static struct xbridge *xbridge_lookup(struct xlate_cfg *,
535 const struct ofproto_dpif *);
536 static struct xbridge *xbridge_lookup_by_uuid(struct xlate_cfg *,
537 const struct uuid *);
538 static struct xbundle *xbundle_lookup(struct xlate_cfg *,
539 const struct ofbundle *);
540 static struct xport *xport_lookup(struct xlate_cfg *,
541 const struct ofport_dpif *);
542 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
543 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
544 uint32_t skb_priority);
545 static void clear_skb_priorities(struct xport *);
546 static size_t count_skb_priorities(const struct xport *);
547 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
550 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
552 static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
553 static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
554 static void xlate_xport_init(struct xlate_cfg *, struct xport *);
555 static void xlate_xbridge_set(struct xbridge *, struct dpif *,
556 const struct mac_learning *, struct stp *,
557 struct rstp *, const struct mcast_snooping *,
558 const struct mbridge *,
559 const struct dpif_sflow *,
560 const struct dpif_ipfix *,
561 const struct netflow *,
562 bool forward_bpdu, bool has_in_band,
563 const struct dpif_backer_support *);
564 static void xlate_xbundle_set(struct xbundle *xbundle,
565 enum port_vlan_mode vlan_mode, int vlan,
566 unsigned long *trunks, bool use_priority_tags,
567 const struct bond *bond, const struct lacp *lacp,
569 static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
570 const struct netdev *netdev, const struct cfm *cfm,
571 const struct bfd *bfd, const struct lldp *lldp,
572 int stp_port_no, const struct rstp_port *rstp_port,
573 enum ofputil_port_config config,
574 enum ofputil_port_state state, bool is_tunnel,
576 static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
577 static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
578 static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
579 static void xlate_xbridge_copy(struct xbridge *);
580 static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
581 static void xlate_xport_copy(struct xbridge *, struct xbundle *,
583 static void xlate_xcfg_free(struct xlate_cfg *);
586 xlate_report(struct xlate_ctx *ctx, const char *format, ...)
588 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
591 va_start(args, format);
592 ctx->xin->report_hook(ctx->xin, ctx->recurse, format, args);
597 static struct vlog_rate_limit error_report_rl = VLOG_RATE_LIMIT_INIT(1, 5);
599 #define XLATE_REPORT_ERROR(CTX, ...) \
601 if (OVS_UNLIKELY((CTX)->xin->report_hook)) { \
602 xlate_report(CTX, __VA_ARGS__); \
604 VLOG_ERR_RL(&error_report_rl, __VA_ARGS__); \
609 xlate_report_actions(struct xlate_ctx *ctx, const char *title,
610 const struct ofpact *ofpacts, size_t ofpacts_len)
612 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
613 struct ds s = DS_EMPTY_INITIALIZER;
614 ofpacts_format(ofpacts, ofpacts_len, &s);
615 xlate_report(ctx, "%s: %s", title, ds_cstr(&s));
621 xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
623 list_init(&xbridge->xbundles);
624 hmap_init(&xbridge->xports);
625 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
626 hash_pointer(xbridge->ofproto, 0));
630 xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
632 list_init(&xbundle->xports);
633 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
634 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
635 hash_pointer(xbundle->ofbundle, 0));
639 xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
641 hmap_init(&xport->skb_priorities);
642 hmap_insert(&xcfg->xports, &xport->hmap_node,
643 hash_pointer(xport->ofport, 0));
644 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
645 hash_ofp_port(xport->ofp_port));
649 xlate_xbridge_set(struct xbridge *xbridge,
651 const struct mac_learning *ml, struct stp *stp,
652 struct rstp *rstp, const struct mcast_snooping *ms,
653 const struct mbridge *mbridge,
654 const struct dpif_sflow *sflow,
655 const struct dpif_ipfix *ipfix,
656 const struct netflow *netflow,
657 bool forward_bpdu, bool has_in_band,
658 const struct dpif_backer_support *support)
660 if (xbridge->ml != ml) {
661 mac_learning_unref(xbridge->ml);
662 xbridge->ml = mac_learning_ref(ml);
665 if (xbridge->ms != ms) {
666 mcast_snooping_unref(xbridge->ms);
667 xbridge->ms = mcast_snooping_ref(ms);
670 if (xbridge->mbridge != mbridge) {
671 mbridge_unref(xbridge->mbridge);
672 xbridge->mbridge = mbridge_ref(mbridge);
675 if (xbridge->sflow != sflow) {
676 dpif_sflow_unref(xbridge->sflow);
677 xbridge->sflow = dpif_sflow_ref(sflow);
680 if (xbridge->ipfix != ipfix) {
681 dpif_ipfix_unref(xbridge->ipfix);
682 xbridge->ipfix = dpif_ipfix_ref(ipfix);
685 if (xbridge->stp != stp) {
686 stp_unref(xbridge->stp);
687 xbridge->stp = stp_ref(stp);
690 if (xbridge->rstp != rstp) {
691 rstp_unref(xbridge->rstp);
692 xbridge->rstp = rstp_ref(rstp);
695 if (xbridge->netflow != netflow) {
696 netflow_unref(xbridge->netflow);
697 xbridge->netflow = netflow_ref(netflow);
700 xbridge->dpif = dpif;
701 xbridge->forward_bpdu = forward_bpdu;
702 xbridge->has_in_band = has_in_band;
703 xbridge->support = *support;
707 xlate_xbundle_set(struct xbundle *xbundle,
708 enum port_vlan_mode vlan_mode, int vlan,
709 unsigned long *trunks, bool use_priority_tags,
710 const struct bond *bond, const struct lacp *lacp,
713 ovs_assert(xbundle->xbridge);
715 xbundle->vlan_mode = vlan_mode;
716 xbundle->vlan = vlan;
717 xbundle->trunks = trunks;
718 xbundle->use_priority_tags = use_priority_tags;
719 xbundle->floodable = floodable;
721 if (xbundle->bond != bond) {
722 bond_unref(xbundle->bond);
723 xbundle->bond = bond_ref(bond);
726 if (xbundle->lacp != lacp) {
727 lacp_unref(xbundle->lacp);
728 xbundle->lacp = lacp_ref(lacp);
733 xlate_xport_set(struct xport *xport, odp_port_t odp_port,
734 const struct netdev *netdev, const struct cfm *cfm,
735 const struct bfd *bfd, const struct lldp *lldp, int stp_port_no,
736 const struct rstp_port* rstp_port,
737 enum ofputil_port_config config, enum ofputil_port_state state,
738 bool is_tunnel, bool may_enable)
740 xport->config = config;
741 xport->state = state;
742 xport->stp_port_no = stp_port_no;
743 xport->is_tunnel = is_tunnel;
744 xport->may_enable = may_enable;
745 xport->odp_port = odp_port;
747 if (xport->rstp_port != rstp_port) {
748 rstp_port_unref(xport->rstp_port);
749 xport->rstp_port = rstp_port_ref(rstp_port);
752 if (xport->cfm != cfm) {
753 cfm_unref(xport->cfm);
754 xport->cfm = cfm_ref(cfm);
757 if (xport->bfd != bfd) {
758 bfd_unref(xport->bfd);
759 xport->bfd = bfd_ref(bfd);
762 if (xport->lldp != lldp) {
763 lldp_unref(xport->lldp);
764 xport->lldp = lldp_ref(lldp);
767 if (xport->netdev != netdev) {
768 netdev_close(xport->netdev);
769 xport->netdev = netdev_ref(netdev);
774 xlate_xbridge_copy(struct xbridge *xbridge)
776 struct xbundle *xbundle;
778 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
779 new_xbridge->ofproto = xbridge->ofproto;
780 new_xbridge->name = xstrdup(xbridge->name);
781 xlate_xbridge_init(new_xcfg, new_xbridge);
783 xlate_xbridge_set(new_xbridge,
784 xbridge->dpif, xbridge->ml, xbridge->stp,
785 xbridge->rstp, xbridge->ms, xbridge->mbridge,
786 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
787 xbridge->forward_bpdu, xbridge->has_in_band,
789 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
790 xlate_xbundle_copy(new_xbridge, xbundle);
793 /* Copy xports which are not part of a xbundle */
794 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
795 if (!xport->xbundle) {
796 xlate_xport_copy(new_xbridge, NULL, xport);
802 xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
805 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
806 new_xbundle->ofbundle = xbundle->ofbundle;
807 new_xbundle->xbridge = xbridge;
808 new_xbundle->name = xstrdup(xbundle->name);
809 xlate_xbundle_init(new_xcfg, new_xbundle);
811 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
812 xbundle->vlan, xbundle->trunks,
813 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
815 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
816 xlate_xport_copy(xbridge, new_xbundle, xport);
821 xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
824 struct skb_priority_to_dscp *pdscp, *new_pdscp;
825 struct xport *new_xport = xzalloc(sizeof *xport);
826 new_xport->ofport = xport->ofport;
827 new_xport->ofp_port = xport->ofp_port;
828 new_xport->xbridge = xbridge;
829 xlate_xport_init(new_xcfg, new_xport);
831 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
832 xport->bfd, xport->lldp, xport->stp_port_no,
833 xport->rstp_port, xport->config, xport->state,
834 xport->is_tunnel, xport->may_enable);
837 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
839 new_xport->peer = peer;
840 new_xport->peer->peer = new_xport;
845 new_xport->xbundle = xbundle;
846 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
849 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
850 new_pdscp = xmalloc(sizeof *pdscp);
851 new_pdscp->skb_priority = pdscp->skb_priority;
852 new_pdscp->dscp = pdscp->dscp;
853 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
854 hash_int(new_pdscp->skb_priority, 0));
858 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
859 * configuration in xcfgp.
861 * This needs to be called after editing the xlate configuration.
863 * Functions that edit the new xlate configuration are
864 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
870 * edit_xlate_configuration();
872 * xlate_txn_commit(); */
874 xlate_txn_commit(void)
876 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
878 ovsrcu_set(&xcfgp, new_xcfg);
879 ovsrcu_synchronize();
880 xlate_xcfg_free(xcfg);
884 /* Copies the current xlate configuration in xcfgp to new_xcfg.
886 * This needs to be called prior to editing the xlate configuration. */
888 xlate_txn_start(void)
890 struct xbridge *xbridge;
891 struct xlate_cfg *xcfg;
893 ovs_assert(!new_xcfg);
895 new_xcfg = xmalloc(sizeof *new_xcfg);
896 hmap_init(&new_xcfg->xbridges);
897 hmap_init(&new_xcfg->xbundles);
898 hmap_init(&new_xcfg->xports);
900 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
905 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
906 xlate_xbridge_copy(xbridge);
912 xlate_xcfg_free(struct xlate_cfg *xcfg)
914 struct xbridge *xbridge, *next_xbridge;
920 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
921 xlate_xbridge_remove(xcfg, xbridge);
924 hmap_destroy(&xcfg->xbridges);
925 hmap_destroy(&xcfg->xbundles);
926 hmap_destroy(&xcfg->xports);
931 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
933 const struct mac_learning *ml, struct stp *stp,
934 struct rstp *rstp, const struct mcast_snooping *ms,
935 const struct mbridge *mbridge,
936 const struct dpif_sflow *sflow,
937 const struct dpif_ipfix *ipfix,
938 const struct netflow *netflow,
939 bool forward_bpdu, bool has_in_band,
940 const struct dpif_backer_support *support)
942 struct xbridge *xbridge;
944 ovs_assert(new_xcfg);
946 xbridge = xbridge_lookup(new_xcfg, ofproto);
948 xbridge = xzalloc(sizeof *xbridge);
949 xbridge->ofproto = ofproto;
951 xlate_xbridge_init(new_xcfg, xbridge);
955 xbridge->name = xstrdup(name);
957 xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
958 netflow, forward_bpdu, has_in_band, support);
962 xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
964 struct xbundle *xbundle, *next_xbundle;
965 struct xport *xport, *next_xport;
971 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
972 xlate_xport_remove(xcfg, xport);
975 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
976 xlate_xbundle_remove(xcfg, xbundle);
979 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
980 mac_learning_unref(xbridge->ml);
981 mcast_snooping_unref(xbridge->ms);
982 mbridge_unref(xbridge->mbridge);
983 dpif_sflow_unref(xbridge->sflow);
984 dpif_ipfix_unref(xbridge->ipfix);
985 stp_unref(xbridge->stp);
986 rstp_unref(xbridge->rstp);
987 hmap_destroy(&xbridge->xports);
993 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
995 struct xbridge *xbridge;
997 ovs_assert(new_xcfg);
999 xbridge = xbridge_lookup(new_xcfg, ofproto);
1000 xlate_xbridge_remove(new_xcfg, xbridge);
1004 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
1005 const char *name, enum port_vlan_mode vlan_mode, int vlan,
1006 unsigned long *trunks, bool use_priority_tags,
1007 const struct bond *bond, const struct lacp *lacp,
1010 struct xbundle *xbundle;
1012 ovs_assert(new_xcfg);
1014 xbundle = xbundle_lookup(new_xcfg, ofbundle);
1016 xbundle = xzalloc(sizeof *xbundle);
1017 xbundle->ofbundle = ofbundle;
1018 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
1020 xlate_xbundle_init(new_xcfg, xbundle);
1023 free(xbundle->name);
1024 xbundle->name = xstrdup(name);
1026 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
1027 use_priority_tags, bond, lacp, floodable);
1031 xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
1033 struct xport *xport;
1039 LIST_FOR_EACH_POP (xport, bundle_node, &xbundle->xports) {
1040 xport->xbundle = NULL;
1043 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
1044 list_remove(&xbundle->list_node);
1045 bond_unref(xbundle->bond);
1046 lacp_unref(xbundle->lacp);
1047 free(xbundle->name);
1052 xlate_bundle_remove(struct ofbundle *ofbundle)
1054 struct xbundle *xbundle;
1056 ovs_assert(new_xcfg);
1058 xbundle = xbundle_lookup(new_xcfg, ofbundle);
1059 xlate_xbundle_remove(new_xcfg, xbundle);
1063 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
1064 struct ofport_dpif *ofport, ofp_port_t ofp_port,
1065 odp_port_t odp_port, const struct netdev *netdev,
1066 const struct cfm *cfm, const struct bfd *bfd,
1067 const struct lldp *lldp, struct ofport_dpif *peer,
1068 int stp_port_no, const struct rstp_port *rstp_port,
1069 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
1070 enum ofputil_port_config config,
1071 enum ofputil_port_state state, bool is_tunnel,
1075 struct xport *xport;
1077 ovs_assert(new_xcfg);
1079 xport = xport_lookup(new_xcfg, ofport);
1081 xport = xzalloc(sizeof *xport);
1082 xport->ofport = ofport;
1083 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
1084 xport->ofp_port = ofp_port;
1086 xlate_xport_init(new_xcfg, xport);
1089 ovs_assert(xport->ofp_port == ofp_port);
1091 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, lldp,
1092 stp_port_no, rstp_port, config, state, is_tunnel,
1096 xport->peer->peer = NULL;
1098 xport->peer = xport_lookup(new_xcfg, peer);
1100 xport->peer->peer = xport;
1103 if (xport->xbundle) {
1104 list_remove(&xport->bundle_node);
1106 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
1107 if (xport->xbundle) {
1108 list_insert(&xport->xbundle->xports, &xport->bundle_node);
1111 clear_skb_priorities(xport);
1112 for (i = 0; i < n_qdscp; i++) {
1113 struct skb_priority_to_dscp *pdscp;
1114 uint32_t skb_priority;
1116 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
1121 pdscp = xmalloc(sizeof *pdscp);
1122 pdscp->skb_priority = skb_priority;
1123 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1124 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
1125 hash_int(pdscp->skb_priority, 0));
1130 xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
1137 xport->peer->peer = NULL;
1141 if (xport->xbundle) {
1142 list_remove(&xport->bundle_node);
1145 clear_skb_priorities(xport);
1146 hmap_destroy(&xport->skb_priorities);
1148 hmap_remove(&xcfg->xports, &xport->hmap_node);
1149 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
1151 netdev_close(xport->netdev);
1152 rstp_port_unref(xport->rstp_port);
1153 cfm_unref(xport->cfm);
1154 bfd_unref(xport->bfd);
1155 lldp_unref(xport->lldp);
1160 xlate_ofport_remove(struct ofport_dpif *ofport)
1162 struct xport *xport;
1164 ovs_assert(new_xcfg);
1166 xport = xport_lookup(new_xcfg, ofport);
1167 xlate_xport_remove(new_xcfg, xport);
1170 static struct ofproto_dpif *
1171 xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
1172 ofp_port_t *ofp_in_port, const struct xport **xportp)
1174 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1175 const struct xport *xport;
1177 xport = xport_lookup(xcfg, tnl_port_should_receive(flow)
1178 ? tnl_port_receive(flow)
1179 : odp_port_to_ofport(backer, flow->in_port.odp_port));
1180 if (OVS_UNLIKELY(!xport)) {
1185 *ofp_in_port = xport->ofp_port;
1187 return xport->xbridge->ofproto;
1190 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1191 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1192 struct ofproto_dpif *
1193 xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1194 ofp_port_t *ofp_in_port)
1196 const struct xport *xport;
1198 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1201 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1202 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
1203 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1204 * handles for those protocols if they're enabled. Caller may use the returned
1205 * pointers until quiescing, for longer term use additional references must
1208 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1211 xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1212 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1213 struct dpif_sflow **sflow, struct netflow **netflow,
1214 ofp_port_t *ofp_in_port)
1216 struct ofproto_dpif *ofproto;
1217 const struct xport *xport;
1219 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1226 *ofprotop = ofproto;
1230 *ipfix = xport ? xport->xbridge->ipfix : NULL;
1234 *sflow = xport ? xport->xbridge->sflow : NULL;
1238 *netflow = xport ? xport->xbridge->netflow : NULL;
1244 static struct xbridge *
1245 xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
1247 struct hmap *xbridges;
1248 struct xbridge *xbridge;
1250 if (!ofproto || !xcfg) {
1254 xbridges = &xcfg->xbridges;
1256 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
1258 if (xbridge->ofproto == ofproto) {
1265 static struct xbridge *
1266 xbridge_lookup_by_uuid(struct xlate_cfg *xcfg, const struct uuid *uuid)
1268 struct xbridge *xbridge;
1270 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
1271 if (uuid_equals(ofproto_dpif_get_uuid(xbridge->ofproto), uuid)) {
1278 static struct xbundle *
1279 xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
1281 struct hmap *xbundles;
1282 struct xbundle *xbundle;
1284 if (!ofbundle || !xcfg) {
1288 xbundles = &xcfg->xbundles;
1290 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
1292 if (xbundle->ofbundle == ofbundle) {
1299 static struct xport *
1300 xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
1302 struct hmap *xports;
1303 struct xport *xport;
1305 if (!ofport || !xcfg) {
1309 xports = &xcfg->xports;
1311 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
1313 if (xport->ofport == ofport) {
1320 static struct stp_port *
1321 xport_get_stp_port(const struct xport *xport)
1323 return xport->xbridge->stp && xport->stp_port_no != -1
1324 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1329 xport_stp_learn_state(const struct xport *xport)
1331 struct stp_port *sp = xport_get_stp_port(xport);
1333 ? stp_learn_in_state(stp_port_get_state(sp))
1338 xport_stp_forward_state(const struct xport *xport)
1340 struct stp_port *sp = xport_get_stp_port(xport);
1342 ? stp_forward_in_state(stp_port_get_state(sp))
1347 xport_stp_should_forward_bpdu(const struct xport *xport)
1349 struct stp_port *sp = xport_get_stp_port(xport);
1350 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
1353 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1354 * were used to make the determination.*/
1356 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1358 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1359 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1360 return is_stp(flow);
1364 stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1366 struct stp_port *sp = xport_get_stp_port(xport);
1367 struct dp_packet payload = *packet;
1368 struct eth_header *eth = dp_packet_data(&payload);
1370 /* Sink packets on ports that have STP disabled when the bridge has
1372 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1376 /* Trim off padding on payload. */
1377 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1378 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1381 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1382 stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
1386 static enum rstp_state
1387 xport_get_rstp_port_state(const struct xport *xport)
1389 return xport->rstp_port
1390 ? rstp_port_get_state(xport->rstp_port)
1395 xport_rstp_learn_state(const struct xport *xport)
1397 return xport->xbridge->rstp && xport->rstp_port
1398 ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1403 xport_rstp_forward_state(const struct xport *xport)
1405 return xport->xbridge->rstp && xport->rstp_port
1406 ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1411 xport_rstp_should_manage_bpdu(const struct xport *xport)
1413 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
1417 rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1419 struct dp_packet payload = *packet;
1420 struct eth_header *eth = dp_packet_data(&payload);
1422 /* Sink packets on ports that have no RSTP. */
1423 if (!xport->rstp_port) {
1427 /* Trim off padding on payload. */
1428 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1429 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1432 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1433 rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
1434 dp_packet_size(&payload));
1438 static struct xport *
1439 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1441 struct xport *xport;
1443 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1445 if (xport->ofp_port == ofp_port) {
1453 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1455 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1456 return xport ? xport->odp_port : ODPP_NONE;
1460 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1462 struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1463 return xport && xport->may_enable;
1466 static struct ofputil_bucket *
1467 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1471 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1473 struct group_dpif *group;
1475 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1476 struct ofputil_bucket *bucket;
1478 bucket = group_first_live_bucket(ctx, group, depth);
1479 group_dpif_unref(group);
1480 return bucket == NULL;
1486 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1489 bucket_is_alive(const struct xlate_ctx *ctx,
1490 struct ofputil_bucket *bucket, int depth)
1492 if (depth >= MAX_LIVENESS_RECURSION) {
1493 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1495 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1496 MAX_LIVENESS_RECURSION);
1500 return (!ofputil_bucket_has_liveness(bucket)
1501 || (bucket->watch_port != OFPP_ANY
1502 && odp_port_is_alive(ctx, bucket->watch_port))
1503 || (bucket->watch_group != OFPG_ANY
1504 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
1507 static struct ofputil_bucket *
1508 group_first_live_bucket(const struct xlate_ctx *ctx,
1509 const struct group_dpif *group, int depth)
1511 struct ofputil_bucket *bucket;
1512 const struct ovs_list *buckets;
1514 group_dpif_get_buckets(group, &buckets);
1515 LIST_FOR_EACH (bucket, list_node, buckets) {
1516 if (bucket_is_alive(ctx, bucket, depth)) {
1524 static struct ofputil_bucket *
1525 group_best_live_bucket(const struct xlate_ctx *ctx,
1526 const struct group_dpif *group,
1529 struct ofputil_bucket *best_bucket = NULL;
1530 uint32_t best_score = 0;
1533 struct ofputil_bucket *bucket;
1534 const struct ovs_list *buckets;
1536 group_dpif_get_buckets(group, &buckets);
1537 LIST_FOR_EACH (bucket, list_node, buckets) {
1538 if (bucket_is_alive(ctx, bucket, 0)) {
1539 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
1540 if (score >= best_score) {
1541 best_bucket = bucket;
1552 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
1554 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1555 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1559 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1561 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1564 static mirror_mask_t
1565 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1567 return xbundle != &ofpp_none_bundle
1568 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1572 static mirror_mask_t
1573 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
1575 return xbundle != &ofpp_none_bundle
1576 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1580 static mirror_mask_t
1581 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
1583 return xbundle != &ofpp_none_bundle
1584 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1588 static struct xbundle *
1589 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1590 bool warn, struct xport **in_xportp)
1592 struct xport *xport;
1594 /* Find the port and bundle for the received packet. */
1595 xport = get_ofp_port(xbridge, in_port);
1599 if (xport && xport->xbundle) {
1600 return xport->xbundle;
1603 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1604 * which a controller may use as the ingress port for traffic that
1605 * it is sourcing. */
1606 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
1607 return &ofpp_none_bundle;
1610 /* Odd. A few possible reasons here:
1612 * - We deleted a port but there are still a few packets queued up
1615 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1616 * we don't know about.
1618 * - The ofproto client didn't configure the port as part of a bundle.
1619 * This is particularly likely to happen if a packet was received on the
1620 * port after it was created, but before the client had a chance to
1621 * configure its bundle.
1624 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1626 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1627 "port %"PRIu16, xbridge->name, in_port);
1632 /* Mirrors the packet represented by 'ctx' to appropriate mirror destinations,
1633 * given the packet is ingressing or egressing on 'xbundle', which has ingress
1634 * or egress (as appropriate) mirrors 'mirrors'. */
1636 mirror_packet(struct xlate_ctx *ctx, struct xbundle *xbundle,
1637 mirror_mask_t mirrors)
1639 /* Figure out what VLAN the packet is in (because mirrors can select
1640 * packets on basis of VLAN). */
1641 bool warn = ctx->xin->packet != NULL;
1642 uint16_t vid = vlan_tci_to_vid(ctx->xin->flow.vlan_tci);
1643 if (!input_vid_is_valid(vid, xbundle, warn)) {
1646 uint16_t vlan = input_vid_to_vlan(xbundle, vid);
1648 const struct xbridge *xbridge = ctx->xbridge;
1650 /* Don't mirror to destinations that we've already mirrored to. */
1651 mirrors &= ~ctx->mirrors;
1656 if (ctx->xin->resubmit_stats) {
1657 mirror_update_stats(xbridge->mbridge, mirrors,
1658 ctx->xin->resubmit_stats->n_packets,
1659 ctx->xin->resubmit_stats->n_bytes);
1661 if (ctx->xin->xcache) {
1662 struct xc_entry *entry;
1664 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_MIRROR);
1665 entry->u.mirror.mbridge = mbridge_ref(xbridge->mbridge);
1666 entry->u.mirror.mirrors = mirrors;
1669 /* 'mirrors' is a bit-mask of candidates for mirroring. Iterate as long as
1670 * some candidates remain. */
1672 const unsigned long *vlans;
1673 mirror_mask_t dup_mirrors;
1674 struct ofbundle *out;
1677 /* Get the details of the mirror represented by the rightmost 1-bit. */
1678 bool has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1679 &vlans, &dup_mirrors, &out, &out_vlan);
1680 ovs_assert(has_mirror);
1682 /* If this mirror selects on the basis of VLAN, and it does not select
1683 * 'vlan', then discard this mirror and go on to the next one. */
1685 ctx->wc->masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1687 if (vlans && !bitmap_is_set(vlans, vlan)) {
1688 mirrors = zero_rightmost_1bit(mirrors);
1692 /* Record the mirror, and the mirrors that output to the same
1693 * destination, so that we don't mirror to them again. This must be
1694 * done now to ensure that output_normal(), below, doesn't recursively
1695 * output to the same mirrors. */
1696 ctx->mirrors |= dup_mirrors;
1698 /* Send the packet to the mirror. */
1700 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1701 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1703 output_normal(ctx, out_xbundle, vlan);
1705 } else if (vlan != out_vlan
1706 && !eth_addr_is_reserved(ctx->xin->flow.dl_dst)) {
1707 struct xbundle *xbundle;
1709 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1710 if (xbundle_includes_vlan(xbundle, out_vlan)
1711 && !xbundle_mirror_out(xbridge, xbundle)) {
1712 output_normal(ctx, xbundle, out_vlan);
1717 /* output_normal() could have recursively output (to different
1718 * mirrors), so make sure that we don't send duplicates. */
1719 mirrors &= ~ctx->mirrors;
1724 mirror_ingress_packet(struct xlate_ctx *ctx)
1726 if (mbridge_has_mirrors(ctx->xbridge->mbridge)) {
1727 bool warn = ctx->xin->packet != NULL;
1728 struct xbundle *xbundle = lookup_input_bundle(
1729 ctx->xbridge, ctx->xin->flow.in_port.ofp_port, warn, NULL);
1731 mirror_packet(ctx, xbundle,
1732 xbundle_mirror_src(ctx->xbridge, xbundle));
1737 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1738 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1739 * the bundle on which the packet was received, returns the VLAN to which the
1742 * Both 'vid' and the return value are in the range 0...4095. */
1744 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1746 switch (in_xbundle->vlan_mode) {
1747 case PORT_VLAN_ACCESS:
1748 return in_xbundle->vlan;
1751 case PORT_VLAN_TRUNK:
1754 case PORT_VLAN_NATIVE_UNTAGGED:
1755 case PORT_VLAN_NATIVE_TAGGED:
1756 return vid ? vid : in_xbundle->vlan;
1763 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1764 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1767 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1768 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1771 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1773 /* Allow any VID on the OFPP_NONE port. */
1774 if (in_xbundle == &ofpp_none_bundle) {
1778 switch (in_xbundle->vlan_mode) {
1779 case PORT_VLAN_ACCESS:
1782 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1783 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1784 "packet received on port %s configured as VLAN "
1785 "%"PRIu16" access port", vid, in_xbundle->name,
1792 case PORT_VLAN_NATIVE_UNTAGGED:
1793 case PORT_VLAN_NATIVE_TAGGED:
1795 /* Port must always carry its native VLAN. */
1799 case PORT_VLAN_TRUNK:
1800 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1802 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1803 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1804 "received on port %s not configured for trunking "
1805 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1817 /* Given 'vlan', the VLAN that a packet belongs to, and
1818 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1819 * that should be included in the 802.1Q header. (If the return value is 0,
1820 * then the 802.1Q header should only be included in the packet if there is a
1823 * Both 'vlan' and the return value are in the range 0...4095. */
1825 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1827 switch (out_xbundle->vlan_mode) {
1828 case PORT_VLAN_ACCESS:
1831 case PORT_VLAN_TRUNK:
1832 case PORT_VLAN_NATIVE_TAGGED:
1835 case PORT_VLAN_NATIVE_UNTAGGED:
1836 return vlan == out_xbundle->vlan ? 0 : vlan;
1844 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1847 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1849 ovs_be16 tci, old_tci;
1850 struct xport *xport;
1851 struct xlate_bond_recirc xr;
1852 bool use_recirc = false;
1854 vid = output_vlan_to_vid(out_xbundle, vlan);
1855 if (list_is_empty(&out_xbundle->xports)) {
1856 /* Partially configured bundle with no slaves. Drop the packet. */
1858 } else if (!out_xbundle->bond) {
1859 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1862 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1863 struct flow_wildcards *wc = ctx->wc;
1864 struct ofport_dpif *ofport;
1866 if (ctx->xbridge->support.odp.recirc) {
1867 use_recirc = bond_may_recirc(
1868 out_xbundle->bond, &xr.recirc_id, &xr.hash_basis);
1871 /* Only TCP mode uses recirculation. */
1872 xr.hash_alg = OVS_HASH_ALG_L4;
1873 bond_update_post_recirc_rules(out_xbundle->bond, false);
1875 /* Recirculation does not require unmasking hash fields. */
1880 ofport = bond_choose_output_slave(out_xbundle->bond,
1881 &ctx->xin->flow, wc, vid);
1882 xport = xport_lookup(xcfg, ofport);
1885 /* No slaves enabled, so drop packet. */
1889 /* If use_recirc is set, the main thread will handle stats
1890 * accounting for this bond. */
1892 if (ctx->xin->resubmit_stats) {
1893 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1894 ctx->xin->resubmit_stats->n_bytes);
1896 if (ctx->xin->xcache) {
1897 struct xc_entry *entry;
1900 flow = &ctx->xin->flow;
1901 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1902 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1903 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1904 entry->u.bond.vid = vid;
1909 old_tci = *flow_tci;
1911 if (tci || out_xbundle->use_priority_tags) {
1912 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1914 tci |= htons(VLAN_CFI);
1919 compose_output_action(ctx, xport->ofp_port, use_recirc ? &xr : NULL);
1920 *flow_tci = old_tci;
1923 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1924 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1925 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1927 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1929 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1933 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1934 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1938 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1939 if (flow->nw_proto == ARP_OP_REPLY) {
1941 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1942 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1943 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1945 return flow->nw_src == flow->nw_dst;
1951 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1952 * dropped. Returns true if they may be forwarded, false if they should be
1955 * 'in_port' must be the xport that corresponds to flow->in_port.
1956 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1958 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1959 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1960 * checked by input_vid_is_valid().
1962 * May also add tags to '*tags', although the current implementation only does
1963 * so in one special case.
1966 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1969 struct xbundle *in_xbundle = in_port->xbundle;
1970 const struct xbridge *xbridge = ctx->xbridge;
1971 struct flow *flow = &ctx->xin->flow;
1973 /* Drop frames for reserved multicast addresses
1974 * only if forward_bpdu option is absent. */
1975 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1976 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1980 if (in_xbundle->bond) {
1981 struct mac_entry *mac;
1983 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1989 xlate_report(ctx, "bonding refused admissibility, dropping");
1992 case BV_DROP_IF_MOVED:
1993 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1994 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1996 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
1997 && (!is_gratuitous_arp(flow, ctx->wc)
1998 || mac_entry_is_grat_arp_locked(mac))) {
1999 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2000 xlate_report(ctx, "SLB bond thinks this packet looped back, "
2004 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2012 /* Checks whether a MAC learning update is necessary for MAC learning table
2013 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
2016 * Most packets processed through the MAC learning table do not actually
2017 * change it in any way. This function requires only a read lock on the MAC
2018 * learning table, so it is much cheaper in this common case.
2020 * Keep the code here synchronized with that in update_learning_table__()
2023 is_mac_learning_update_needed(const struct mac_learning *ml,
2024 const struct flow *flow,
2025 struct flow_wildcards *wc,
2026 int vlan, struct xbundle *in_xbundle)
2027 OVS_REQ_RDLOCK(ml->rwlock)
2029 struct mac_entry *mac;
2031 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
2035 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
2036 if (!mac || mac_entry_age(ml, mac)) {
2040 if (is_gratuitous_arp(flow, wc)) {
2041 /* We don't want to learn from gratuitous ARP packets that are
2042 * reflected back over bond slaves so we lock the learning table. */
2043 if (!in_xbundle->bond) {
2045 } else if (mac_entry_is_grat_arp_locked(mac)) {
2050 return mac_entry_get_port(ml, mac) != in_xbundle->ofbundle;
2054 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
2055 * received on 'in_xbundle' in 'vlan'.
2057 * This code repeats all the checks in is_mac_learning_update_needed() because
2058 * the lock was released between there and here and thus the MAC learning state
2059 * could have changed.
2061 * Keep the code here synchronized with that in is_mac_learning_update_needed()
2064 update_learning_table__(const struct xbridge *xbridge,
2065 const struct flow *flow, struct flow_wildcards *wc,
2066 int vlan, struct xbundle *in_xbundle)
2067 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
2069 struct mac_entry *mac;
2071 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
2075 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
2076 if (is_gratuitous_arp(flow, wc)) {
2077 /* We don't want to learn from gratuitous ARP packets that are
2078 * reflected back over bond slaves so we lock the learning table. */
2079 if (!in_xbundle->bond) {
2080 mac_entry_set_grat_arp_lock(mac);
2081 } else if (mac_entry_is_grat_arp_locked(mac)) {
2086 if (mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle) {
2087 /* The log messages here could actually be useful in debugging,
2088 * so keep the rate limit relatively high. */
2089 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
2091 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
2092 "on port %s in VLAN %d",
2093 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
2094 in_xbundle->name, vlan);
2096 mac_entry_set_port(xbridge->ml, mac, in_xbundle->ofbundle);
2101 update_learning_table(const struct xbridge *xbridge,
2102 const struct flow *flow, struct flow_wildcards *wc,
2103 int vlan, struct xbundle *in_xbundle)
2107 /* Don't learn the OFPP_NONE port. */
2108 if (in_xbundle == &ofpp_none_bundle) {
2112 /* First try the common case: no change to MAC learning table. */
2113 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
2114 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
2116 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2119 /* Slow path: MAC learning table might need an update. */
2120 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
2121 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
2122 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2126 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2127 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
2129 update_mcast_snooping_table4__(const struct xbridge *xbridge,
2130 const struct flow *flow,
2131 struct mcast_snooping *ms, int vlan,
2132 struct xbundle *in_xbundle,
2133 const struct dp_packet *packet)
2134 OVS_REQ_WRLOCK(ms->rwlock)
2136 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2138 ovs_be32 ip4 = flow->igmp_group_ip4;
2140 switch (ntohs(flow->tp_src)) {
2141 case IGMP_HOST_MEMBERSHIP_REPORT:
2142 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2143 if (mcast_snooping_add_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2144 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
2145 IP_FMT" is on port %s in VLAN %d",
2146 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2149 case IGMP_HOST_LEAVE_MESSAGE:
2150 if (mcast_snooping_leave_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2151 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
2152 IP_FMT" is on port %s in VLAN %d",
2153 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2156 case IGMP_HOST_MEMBERSHIP_QUERY:
2157 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
2158 in_xbundle->ofbundle)) {
2159 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
2160 IP_FMT" is on port %s in VLAN %d",
2161 xbridge->name, IP_ARGS(flow->nw_src),
2162 in_xbundle->name, vlan);
2165 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2166 if ((count = mcast_snooping_add_report(ms, packet, vlan,
2167 in_xbundle->ofbundle))) {
2168 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2169 "addresses on port %s in VLAN %d",
2170 xbridge->name, count, in_xbundle->name, vlan);
2177 update_mcast_snooping_table6__(const struct xbridge *xbridge,
2178 const struct flow *flow,
2179 struct mcast_snooping *ms, int vlan,
2180 struct xbundle *in_xbundle,
2181 const struct dp_packet *packet)
2182 OVS_REQ_WRLOCK(ms->rwlock)
2184 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2187 switch (ntohs(flow->tp_src)) {
2189 if (!ipv6_addr_equals(&flow->ipv6_src, &in6addr_any)
2190 && mcast_snooping_add_mrouter(ms, vlan, in_xbundle->ofbundle)) {
2191 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query on port %s"
2193 xbridge->name, in_xbundle->name, vlan);
2199 count = mcast_snooping_add_mld(ms, packet, vlan, in_xbundle->ofbundle);
2201 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2202 "addresses on port %s in VLAN %d",
2203 xbridge->name, count, in_xbundle->name, vlan);
2209 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2210 * was received on 'in_xbundle' in 'vlan'. */
2212 update_mcast_snooping_table(const struct xbridge *xbridge,
2213 const struct flow *flow, int vlan,
2214 struct xbundle *in_xbundle,
2215 const struct dp_packet *packet)
2217 struct mcast_snooping *ms = xbridge->ms;
2218 struct xlate_cfg *xcfg;
2219 struct xbundle *mcast_xbundle;
2220 struct mcast_port_bundle *fport;
2222 /* Don't learn the OFPP_NONE port. */
2223 if (in_xbundle == &ofpp_none_bundle) {
2227 /* Don't learn from flood ports */
2228 mcast_xbundle = NULL;
2229 ovs_rwlock_wrlock(&ms->rwlock);
2230 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2231 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2232 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2233 if (mcast_xbundle == in_xbundle) {
2238 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
2239 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2240 update_mcast_snooping_table4__(xbridge, flow, ms, vlan,
2241 in_xbundle, packet);
2243 update_mcast_snooping_table6__(xbridge, flow, ms, vlan,
2244 in_xbundle, packet);
2247 ovs_rwlock_unlock(&ms->rwlock);
2250 /* send the packet to ports having the multicast group learned */
2252 xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2253 struct mcast_snooping *ms OVS_UNUSED,
2254 struct mcast_group *grp,
2255 struct xbundle *in_xbundle, uint16_t vlan)
2256 OVS_REQ_RDLOCK(ms->rwlock)
2258 struct xlate_cfg *xcfg;
2259 struct mcast_group_bundle *b;
2260 struct xbundle *mcast_xbundle;
2262 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2263 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2264 mcast_xbundle = xbundle_lookup(xcfg, b->port);
2265 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2266 xlate_report(ctx, "forwarding to mcast group port");
2267 output_normal(ctx, mcast_xbundle, vlan);
2268 } else if (!mcast_xbundle) {
2269 xlate_report(ctx, "mcast group port is unknown, dropping");
2271 xlate_report(ctx, "mcast group port is input port, dropping");
2276 /* send the packet to ports connected to multicast routers */
2278 xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2279 struct mcast_snooping *ms,
2280 struct xbundle *in_xbundle, uint16_t vlan)
2281 OVS_REQ_RDLOCK(ms->rwlock)
2283 struct xlate_cfg *xcfg;
2284 struct mcast_mrouter_bundle *mrouter;
2285 struct xbundle *mcast_xbundle;
2287 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2288 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2289 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2290 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2291 xlate_report(ctx, "forwarding to mcast router port");
2292 output_normal(ctx, mcast_xbundle, vlan);
2293 } else if (!mcast_xbundle) {
2294 xlate_report(ctx, "mcast router port is unknown, dropping");
2296 xlate_report(ctx, "mcast router port is input port, dropping");
2301 /* send the packet to ports flagged to be flooded */
2303 xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2304 struct mcast_snooping *ms,
2305 struct xbundle *in_xbundle, uint16_t vlan)
2306 OVS_REQ_RDLOCK(ms->rwlock)
2308 struct xlate_cfg *xcfg;
2309 struct mcast_port_bundle *fport;
2310 struct xbundle *mcast_xbundle;
2312 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2313 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2314 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2315 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2316 xlate_report(ctx, "forwarding to mcast flood port");
2317 output_normal(ctx, mcast_xbundle, vlan);
2318 } else if (!mcast_xbundle) {
2319 xlate_report(ctx, "mcast flood port is unknown, dropping");
2321 xlate_report(ctx, "mcast flood port is input port, dropping");
2326 /* forward the Reports to configured ports */
2328 xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
2329 struct mcast_snooping *ms,
2330 struct xbundle *in_xbundle, uint16_t vlan)
2331 OVS_REQ_RDLOCK(ms->rwlock)
2333 struct xlate_cfg *xcfg;
2334 struct mcast_port_bundle *rport;
2335 struct xbundle *mcast_xbundle;
2337 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2338 LIST_FOR_EACH(rport, node, &ms->rport_list) {
2339 mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2340 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2341 xlate_report(ctx, "forwarding Report to mcast flagged port");
2342 output_normal(ctx, mcast_xbundle, vlan);
2343 } else if (!mcast_xbundle) {
2344 xlate_report(ctx, "mcast port is unknown, dropping the Report");
2346 xlate_report(ctx, "mcast port is input port, dropping the Report");
2352 xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2355 struct xbundle *xbundle;
2357 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2358 if (xbundle != in_xbundle
2359 && xbundle_includes_vlan(xbundle, vlan)
2360 && xbundle->floodable
2361 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2362 output_normal(ctx, xbundle, vlan);
2365 ctx->nf_output_iface = NF_OUT_FLOOD;
2369 xlate_normal(struct xlate_ctx *ctx)
2371 struct flow_wildcards *wc = ctx->wc;
2372 struct flow *flow = &ctx->xin->flow;
2373 struct xbundle *in_xbundle;
2374 struct xport *in_port;
2375 struct mac_entry *mac;
2380 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2381 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2382 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2384 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
2385 ctx->xin->packet != NULL, &in_port);
2387 xlate_report(ctx, "no input bundle, dropping");
2391 /* Drop malformed frames. */
2392 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
2393 !(flow->vlan_tci & htons(VLAN_CFI))) {
2394 if (ctx->xin->packet != NULL) {
2395 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2396 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
2397 "VLAN tag received on port %s",
2398 ctx->xbridge->name, in_xbundle->name);
2400 xlate_report(ctx, "partial VLAN tag, dropping");
2404 /* Drop frames on bundles reserved for mirroring. */
2405 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
2406 if (ctx->xin->packet != NULL) {
2407 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2408 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2409 "%s, which is reserved exclusively for mirroring",
2410 ctx->xbridge->name, in_xbundle->name);
2412 xlate_report(ctx, "input port is mirror output port, dropping");
2417 vid = vlan_tci_to_vid(flow->vlan_tci);
2418 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
2419 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2422 vlan = input_vid_to_vlan(in_xbundle, vid);
2424 /* Check other admissibility requirements. */
2425 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2429 /* Learn source MAC. */
2430 if (ctx->xin->may_learn) {
2431 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
2433 if (ctx->xin->xcache) {
2434 struct xc_entry *entry;
2436 /* Save enough info to update mac learning table later. */
2437 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
2438 entry->u.normal.ofproto = ctx->xbridge->ofproto;
2439 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2440 entry->u.normal.vlan = vlan;
2443 /* Determine output bundle. */
2444 if (mcast_snooping_enabled(ctx->xbridge->ms)
2445 && !eth_addr_is_broadcast(flow->dl_dst)
2446 && eth_addr_is_multicast(flow->dl_dst)
2447 && is_ip_any(flow)) {
2448 struct mcast_snooping *ms = ctx->xbridge->ms;
2449 struct mcast_group *grp = NULL;
2451 if (is_igmp(flow)) {
2452 if (mcast_snooping_is_membership(flow->tp_src) ||
2453 mcast_snooping_is_query(flow->tp_src)) {
2454 if (ctx->xin->may_learn && ctx->xin->packet) {
2455 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2456 in_xbundle, ctx->xin->packet);
2459 * IGMP packets need to take the slow path, in order to be
2460 * processed for mdb updates. That will prevent expires
2461 * firing off even after hosts have sent reports.
2463 ctx->xout->slow |= SLOW_ACTION;
2466 if (mcast_snooping_is_membership(flow->tp_src)) {
2467 ovs_rwlock_rdlock(&ms->rwlock);
2468 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2469 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2470 * forward IGMP Membership Reports only to those ports where
2471 * multicast routers are attached. Alternatively stated: a
2472 * snooping switch should not forward IGMP Membership Reports
2473 * to ports on which only hosts are attached.
2474 * An administrative control may be provided to override this
2475 * restriction, allowing the report messages to be flooded to
2477 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2478 ovs_rwlock_unlock(&ms->rwlock);
2480 xlate_report(ctx, "multicast traffic, flooding");
2481 xlate_normal_flood(ctx, in_xbundle, vlan);
2484 } else if (is_mld(flow)) {
2485 ctx->xout->slow |= SLOW_ACTION;
2486 if (ctx->xin->may_learn && ctx->xin->packet) {
2487 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2488 in_xbundle, ctx->xin->packet);
2490 if (is_mld_report(flow)) {
2491 ovs_rwlock_rdlock(&ms->rwlock);
2492 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2493 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2494 ovs_rwlock_unlock(&ms->rwlock);
2496 xlate_report(ctx, "MLD query, flooding");
2497 xlate_normal_flood(ctx, in_xbundle, vlan);
2500 if ((flow->dl_type == htons(ETH_TYPE_IP)
2501 && ip_is_local_multicast(flow->nw_dst))
2502 || (flow->dl_type == htons(ETH_TYPE_IPV6)
2503 && ipv6_is_all_hosts(&flow->ipv6_dst))) {
2504 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2505 * address in the 224.0.0.x range which are not IGMP must
2506 * be forwarded on all ports */
2507 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2508 xlate_normal_flood(ctx, in_xbundle, vlan);
2513 /* forwarding to group base ports */
2514 ovs_rwlock_rdlock(&ms->rwlock);
2515 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2516 grp = mcast_snooping_lookup4(ms, flow->nw_dst, vlan);
2517 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2518 grp = mcast_snooping_lookup(ms, &flow->ipv6_dst, vlan);
2521 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2522 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2523 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2525 if (mcast_snooping_flood_unreg(ms)) {
2526 xlate_report(ctx, "unregistered multicast, flooding");
2527 xlate_normal_flood(ctx, in_xbundle, vlan);
2529 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2530 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2533 ovs_rwlock_unlock(&ms->rwlock);
2535 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2536 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
2537 mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
2538 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2541 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2542 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2543 if (mac_xbundle && mac_xbundle != in_xbundle) {
2544 xlate_report(ctx, "forwarding to learned port");
2545 output_normal(ctx, mac_xbundle, vlan);
2546 } else if (!mac_xbundle) {
2547 xlate_report(ctx, "learned port is unknown, dropping");
2549 xlate_report(ctx, "learned port is input port, dropping");
2552 xlate_report(ctx, "no learned MAC for destination, flooding");
2553 xlate_normal_flood(ctx, in_xbundle, vlan);
2558 /* Appends a "sample" action for sFlow or IPFIX to 'ctx->odp_actions'. The
2559 * 'probability' is the number of packets out of UINT32_MAX to sample. The
2560 * 'cookie' (of length 'cookie_size' bytes) is passed back in the callback for
2561 * each sampled packet. 'tunnel_out_port', if not ODPP_NONE, is added as the
2562 * OVS_USERSPACE_ATTR_EGRESS_TUN_PORT attribute. If 'include_actions', an
2563 * OVS_USERSPACE_ATTR_ACTIONS attribute is added.
2566 compose_sample_action(struct xlate_ctx *ctx,
2567 const uint32_t probability,
2568 const union user_action_cookie *cookie,
2569 const size_t cookie_size,
2570 const odp_port_t tunnel_out_port,
2571 bool include_actions)
2573 size_t sample_offset = nl_msg_start_nested(ctx->odp_actions,
2574 OVS_ACTION_ATTR_SAMPLE);
2576 nl_msg_put_u32(ctx->odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
2578 size_t actions_offset = nl_msg_start_nested(ctx->odp_actions,
2579 OVS_SAMPLE_ATTR_ACTIONS);
2581 odp_port_t odp_port = ofp_port_to_odp_port(
2582 ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
2583 uint32_t pid = dpif_port_get_pid(ctx->xbridge->dpif, odp_port,
2584 flow_hash_5tuple(&ctx->xin->flow, 0));
2585 int cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2590 nl_msg_end_nested(ctx->odp_actions, actions_offset);
2591 nl_msg_end_nested(ctx->odp_actions, sample_offset);
2593 return cookie_offset;
2596 /* If sFLow is not enabled, returns 0 without doing anything.
2598 * If sFlow is enabled, appends a template "sample" action to the ODP actions
2599 * in 'ctx'. This action is a template because some of the information needed
2600 * to fill it out is not available until flow translation is complete. In this
2601 * case, this functions returns an offset, which is always nonzero, to pass
2602 * later to fix_sflow_action() to fill in the rest of the template. */
2604 compose_sflow_action(struct xlate_ctx *ctx)
2606 struct dpif_sflow *sflow = ctx->xbridge->sflow;
2607 if (!sflow || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
2611 union user_action_cookie cookie = { .type = USER_ACTION_COOKIE_SFLOW };
2612 return compose_sample_action(ctx, dpif_sflow_get_probability(sflow),
2613 &cookie, sizeof cookie.sflow, ODPP_NONE,
2617 /* If IPFIX is enabled, this appends a "sample" action to implement IPFIX to
2618 * 'ctx->odp_actions'. */
2620 compose_ipfix_action(struct xlate_ctx *ctx, odp_port_t output_odp_port)
2622 struct dpif_ipfix *ipfix = ctx->xbridge->ipfix;
2623 odp_port_t tunnel_out_port = ODPP_NONE;
2625 if (!ipfix || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
2629 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2631 if (output_odp_port == ODPP_NONE &&
2632 !dpif_ipfix_get_bridge_exporter_input_sampling(ipfix)) {
2636 /* For output case, output_odp_port is valid*/
2637 if (output_odp_port != ODPP_NONE) {
2638 if (!dpif_ipfix_get_bridge_exporter_output_sampling(ipfix)) {
2641 /* If tunnel sampling is enabled, put an additional option attribute:
2642 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2644 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(ipfix) &&
2645 dpif_ipfix_get_tunnel_port(ipfix, output_odp_port) ) {
2646 tunnel_out_port = output_odp_port;
2650 union user_action_cookie cookie = {
2652 .type = USER_ACTION_COOKIE_IPFIX,
2653 .output_odp_port = output_odp_port,
2656 compose_sample_action(ctx,
2657 dpif_ipfix_get_bridge_exporter_probability(ipfix),
2658 &cookie, sizeof cookie.ipfix, tunnel_out_port,
2662 /* Fix "sample" action according to data collected while composing ODP actions,
2663 * as described in compose_sflow_action().
2665 * 'user_cookie_offset' must be the offset returned by add_sflow_action(). */
2667 fix_sflow_action(struct xlate_ctx *ctx, unsigned int user_cookie_offset)
2669 const struct flow *base = &ctx->base_flow;
2670 union user_action_cookie *cookie;
2672 cookie = ofpbuf_at(ctx->odp_actions, user_cookie_offset,
2673 sizeof cookie->sflow);
2674 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2676 cookie->type = USER_ACTION_COOKIE_SFLOW;
2677 cookie->sflow.vlan_tci = base->vlan_tci;
2679 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2680 * port information") for the interpretation of cookie->output. */
2681 switch (ctx->sflow_n_outputs) {
2683 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2684 cookie->sflow.output = 0x40000000 | 256;
2688 cookie->sflow.output = dpif_sflow_odp_port_to_ifindex(
2689 ctx->xbridge->sflow, ctx->sflow_odp_port);
2690 if (cookie->sflow.output) {
2695 /* 0x80000000 means "multiple output ports. */
2696 cookie->sflow.output = 0x80000000 | ctx->sflow_n_outputs;
2702 process_special(struct xlate_ctx *ctx, const struct xport *xport)
2704 const struct flow *flow = &ctx->xin->flow;
2705 struct flow_wildcards *wc = ctx->wc;
2706 const struct xbridge *xbridge = ctx->xbridge;
2707 const struct dp_packet *packet = ctx->xin->packet;
2708 enum slow_path_reason slow;
2712 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
2714 cfm_process_heartbeat(xport->cfm, packet);
2717 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
2719 bfd_process_packet(xport->bfd, flow, packet);
2720 /* If POLL received, immediately sends FINAL back. */
2721 if (bfd_should_send_packet(xport->bfd)) {
2722 ofproto_dpif_monitor_port_send_soon(xport->ofport);
2726 } else if (xport->xbundle && xport->xbundle->lacp
2727 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2729 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
2732 } else if ((xbridge->stp || xbridge->rstp) &&
2733 stp_should_process_flow(flow, wc)) {
2736 ? stp_process_packet(xport, packet)
2737 : rstp_process_packet(xport, packet);
2740 } else if (xport->lldp && lldp_should_process_flow(xport->lldp, flow)) {
2742 lldp_process_packet(xport->lldp, packet);
2750 ctx->xout->slow |= slow;
2758 tnl_route_lookup_flow(const struct flow *oflow,
2759 struct in6_addr *ip, struct xport **out_port)
2761 char out_dev[IFNAMSIZ];
2762 struct xbridge *xbridge;
2763 struct xlate_cfg *xcfg;
2765 struct in6_addr dst;
2767 dst = flow_tnl_dst(&oflow->tunnel);
2768 if (!ovs_router_lookup(&dst, out_dev, &gw)) {
2772 if (ipv6_addr_is_set(&gw) &&
2773 (!IN6_IS_ADDR_V4MAPPED(&gw) || in6_addr_get_mapped_ipv4(&gw))) {
2779 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2782 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
2783 if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
2786 HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
2787 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
2798 compose_table_xlate(struct xlate_ctx *ctx, const struct xport *out_dev,
2799 struct dp_packet *packet)
2801 struct xbridge *xbridge = out_dev->xbridge;
2802 struct ofpact_output output;
2805 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
2806 flow_extract(packet, &flow);
2807 flow.in_port.ofp_port = out_dev->ofp_port;
2808 output.port = OFPP_TABLE;
2811 return ofproto_dpif_execute_actions__(xbridge->ofproto, &flow, NULL,
2812 &output.ofpact, sizeof output,
2813 ctx->recurse, ctx->resubmits, packet);
2817 tnl_send_nd_request(struct xlate_ctx *ctx, const struct xport *out_dev,
2818 const struct eth_addr eth_src,
2819 struct in6_addr * ipv6_src, struct in6_addr * ipv6_dst)
2821 struct dp_packet packet;
2823 dp_packet_init(&packet, 0);
2824 compose_nd(&packet, eth_src, ipv6_src, ipv6_dst);
2825 compose_table_xlate(ctx, out_dev, &packet);
2826 dp_packet_uninit(&packet);
2830 tnl_send_arp_request(struct xlate_ctx *ctx, const struct xport *out_dev,
2831 const struct eth_addr eth_src,
2832 ovs_be32 ip_src, ovs_be32 ip_dst)
2834 struct dp_packet packet;
2836 dp_packet_init(&packet, 0);
2837 compose_arp(&packet, ARP_OP_REQUEST,
2838 eth_src, eth_addr_zero, true, ip_src, ip_dst);
2840 compose_table_xlate(ctx, out_dev, &packet);
2841 dp_packet_uninit(&packet);
2845 build_tunnel_send(struct xlate_ctx *ctx, const struct xport *xport,
2846 const struct flow *flow, odp_port_t tunnel_odp_port)
2848 struct ovs_action_push_tnl tnl_push_data;
2849 struct xport *out_dev = NULL;
2850 ovs_be32 s_ip = 0, d_ip = 0;
2851 struct in6_addr s_ip6 = in6addr_any;
2852 struct in6_addr d_ip6 = in6addr_any;
2853 struct eth_addr smac;
2854 struct eth_addr dmac;
2856 char buf_sip6[INET6_ADDRSTRLEN];
2857 char buf_dip6[INET6_ADDRSTRLEN];
2859 err = tnl_route_lookup_flow(flow, &d_ip6, &out_dev);
2861 xlate_report(ctx, "native tunnel routing failed");
2865 xlate_report(ctx, "tunneling to %s via %s",
2866 ipv6_string_mapped(buf_dip6, &d_ip6),
2867 netdev_get_name(out_dev->netdev));
2869 /* Use mac addr of bridge port of the peer. */
2870 err = netdev_get_etheraddr(out_dev->netdev, &smac);
2872 xlate_report(ctx, "tunnel output device lacks Ethernet address");
2876 d_ip = in6_addr_get_mapped_ipv4(&d_ip6);
2878 err = netdev_get_in4(out_dev->netdev, (struct in_addr *) &s_ip, NULL);
2880 xlate_report(ctx, "tunnel output device lacks IPv4 address");
2883 in6_addr_set_mapped_ipv4(&s_ip6, s_ip);
2885 err = netdev_get_in6(out_dev->netdev, &s_ip6);
2887 xlate_report(ctx, "tunnel output device lacks IPv6 address");
2892 err = tnl_neigh_lookup(out_dev->xbridge->name, &d_ip6, &dmac);
2894 xlate_report(ctx, "neighbor cache miss for %s on bridge %s, "
2895 "sending %s request",
2896 buf_dip6, out_dev->xbridge->name, d_ip ? "ARP" : "ND");
2898 tnl_send_arp_request(ctx, out_dev, smac, s_ip, d_ip);
2900 tnl_send_nd_request(ctx, out_dev, smac, &s_ip6, &d_ip6);
2905 if (ctx->xin->xcache) {
2906 struct xc_entry *entry;
2908 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_NEIGH);
2909 ovs_strlcpy(entry->u.tnl_neigh_cache.br_name, out_dev->xbridge->name,
2910 sizeof entry->u.tnl_neigh_cache.br_name);
2911 entry->u.tnl_neigh_cache.d_ipv6 = d_ip6;
2914 xlate_report(ctx, "tunneling from "ETH_ADDR_FMT" %s"
2915 " to "ETH_ADDR_FMT" %s",
2916 ETH_ADDR_ARGS(smac), ipv6_string_mapped(buf_sip6, &s_ip6),
2917 ETH_ADDR_ARGS(dmac), buf_dip6);
2919 err = tnl_port_build_header(xport->ofport, flow,
2920 dmac, smac, &s_ip6, &tnl_push_data);
2924 tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
2925 tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
2926 odp_put_tnl_push_action(ctx->odp_actions, &tnl_push_data);
2931 xlate_commit_actions(struct xlate_ctx *ctx)
2933 bool use_masked = ctx->xbridge->support.masked_set_action;
2935 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2936 ctx->odp_actions, ctx->wc,
2941 clear_conntrack(struct flow *flow)
2946 memset(&flow->ct_label, 0, sizeof flow->ct_label);
2950 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
2951 const struct xlate_bond_recirc *xr, bool check_stp)
2953 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
2954 struct flow_wildcards *wc = ctx->wc;
2955 struct flow *flow = &ctx->xin->flow;
2956 struct flow_tnl flow_tnl;
2957 ovs_be16 flow_vlan_tci;
2958 uint32_t flow_pkt_mark;
2959 uint8_t flow_nw_tos;
2960 odp_port_t out_port, odp_port;
2961 bool tnl_push_pop_send = false;
2964 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2965 * before traversing a patch port. */
2966 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 35);
2967 memset(&flow_tnl, 0, sizeof flow_tnl);
2970 xlate_report(ctx, "Nonexistent output port");
2972 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
2973 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2975 } else if (check_stp) {
2976 if (is_stp(&ctx->base_flow)) {
2977 if (!xport_stp_should_forward_bpdu(xport) &&
2978 !xport_rstp_should_manage_bpdu(xport)) {
2979 if (ctx->xbridge->stp != NULL) {
2980 xlate_report(ctx, "STP not in listening state, "
2981 "skipping bpdu output");
2982 } else if (ctx->xbridge->rstp != NULL) {
2983 xlate_report(ctx, "RSTP not managing BPDU in this state, "
2984 "skipping bpdu output");
2988 } else if (!xport_stp_forward_state(xport) ||
2989 !xport_rstp_forward_state(xport)) {
2990 if (ctx->xbridge->stp != NULL) {
2991 xlate_report(ctx, "STP not in forwarding state, "
2993 } else if (ctx->xbridge->rstp != NULL) {
2994 xlate_report(ctx, "RSTP not in forwarding state, "
3002 const struct xport *peer = xport->peer;
3003 struct flow old_flow = ctx->xin->flow;
3004 bool old_conntrack = ctx->conntracked;
3005 bool old_was_mpls = ctx->was_mpls;
3006 cls_version_t old_version = ctx->tables_version;
3007 struct ofpbuf old_stack = ctx->stack;
3008 union mf_subvalue new_stack[1024 / sizeof(union mf_subvalue)];
3009 struct ofpbuf old_action_set = ctx->action_set;
3010 uint64_t actset_stub[1024 / 8];
3012 ofpbuf_use_stub(&ctx->stack, new_stack, sizeof new_stack);
3013 ofpbuf_use_stub(&ctx->action_set, actset_stub, sizeof actset_stub);
3014 ctx->xbridge = peer->xbridge;
3015 flow->in_port.ofp_port = peer->ofp_port;
3016 flow->metadata = htonll(0);
3017 memset(&flow->tunnel, 0, sizeof flow->tunnel);
3018 memset(flow->regs, 0, sizeof flow->regs);
3019 flow->actset_output = OFPP_UNSET;
3020 ctx->conntracked = false;
3021 clear_conntrack(flow);
3023 /* The bridge is now known so obtain its table version. */
3025 = ofproto_dpif_get_tables_version(ctx->xbridge->ofproto);
3027 if (!process_special(ctx, peer) && may_receive(peer, ctx)) {
3028 if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
3029 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
3030 if (!ctx->freezing) {
3031 xlate_action_set(ctx);
3033 if (ctx->freezing) {
3034 finish_freezing(ctx);
3037 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
3038 * the learning action look at the packet, then drop it. */
3039 struct flow old_base_flow = ctx->base_flow;
3040 size_t old_size = ctx->odp_actions->size;
3041 mirror_mask_t old_mirrors = ctx->mirrors;
3043 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
3044 ctx->mirrors = old_mirrors;
3045 ctx->base_flow = old_base_flow;
3046 ctx->odp_actions->size = old_size;
3048 /* Undo changes that may have been done for freezing. */
3049 ctx_cancel_freeze(ctx);
3053 ctx->xin->flow = old_flow;
3054 ctx->xbridge = xport->xbridge;
3055 ofpbuf_uninit(&ctx->action_set);
3056 ctx->action_set = old_action_set;
3057 ofpbuf_uninit(&ctx->stack);
3058 ctx->stack = old_stack;
3060 /* Restore calling bridge's lookup version. */
3061 ctx->tables_version = old_version;
3063 /* The peer bridge popping MPLS should have no effect on the original
3065 ctx->was_mpls = old_was_mpls;
3067 /* The peer bridge's conntrack execution should have no effect on the
3068 * original bridge. */
3069 ctx->conntracked = old_conntrack;
3071 /* The fact that the peer bridge exits (for any reason) does not mean
3072 * that the original bridge should exit. Specifically, if the peer
3073 * bridge freezes translation, the original bridge must continue
3074 * processing with the original, not the frozen packet! */
3077 /* Peer bridge errors do not propagate back. */
3078 ctx->error = XLATE_OK;
3080 if (ctx->xin->resubmit_stats) {
3081 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
3082 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
3084 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
3087 if (ctx->xin->xcache) {
3088 struct xc_entry *entry;
3090 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
3091 entry->u.dev.tx = netdev_ref(xport->netdev);
3092 entry->u.dev.rx = netdev_ref(peer->netdev);
3093 entry->u.dev.bfd = bfd_ref(peer->bfd);
3098 flow_vlan_tci = flow->vlan_tci;
3099 flow_pkt_mark = flow->pkt_mark;
3100 flow_nw_tos = flow->nw_tos;
3102 if (count_skb_priorities(xport)) {
3103 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3104 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
3105 wc->masks.nw_tos |= IP_DSCP_MASK;
3106 flow->nw_tos &= ~IP_DSCP_MASK;
3107 flow->nw_tos |= dscp;
3111 if (xport->is_tunnel) {
3112 struct in6_addr dst;
3113 /* Save tunnel metadata so that changes made due to
3114 * the Logical (tunnel) Port are not visible for any further
3115 * matches, while explicit set actions on tunnel metadata are.
3117 flow_tnl = flow->tunnel;
3118 odp_port = tnl_port_send(xport->ofport, flow, ctx->wc);
3119 if (odp_port == ODPP_NONE) {
3120 xlate_report(ctx, "Tunneling decided against output");
3121 goto out; /* restore flow_nw_tos */
3123 dst = flow_tnl_dst(&flow->tunnel);
3124 if (ipv6_addr_equals(&dst, &ctx->orig_tunnel_ipv6_dst)) {
3125 xlate_report(ctx, "Not tunneling to our own address");
3126 goto out; /* restore flow_nw_tos */
3128 if (ctx->xin->resubmit_stats) {
3129 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
3131 if (ctx->xin->xcache) {
3132 struct xc_entry *entry;
3134 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
3135 entry->u.dev.tx = netdev_ref(xport->netdev);
3137 out_port = odp_port;
3138 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3139 xlate_report(ctx, "output to native tunnel");
3140 tnl_push_pop_send = true;
3142 xlate_report(ctx, "output to kernel tunnel");
3143 commit_odp_tunnel_action(flow, &ctx->base_flow, ctx->odp_actions);
3144 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3147 odp_port = xport->odp_port;
3148 out_port = odp_port;
3149 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
3150 ofp_port_t vlandev_port;
3152 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
3153 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
3154 ofp_port, flow->vlan_tci);
3155 if (vlandev_port != ofp_port) {
3156 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
3157 flow->vlan_tci = htons(0);
3162 if (out_port != ODPP_NONE) {
3163 xlate_commit_actions(ctx);
3166 struct ovs_action_hash *act_hash;
3169 act_hash = nl_msg_put_unspec_uninit(ctx->odp_actions,
3170 OVS_ACTION_ATTR_HASH,
3172 act_hash->hash_alg = xr->hash_alg;
3173 act_hash->hash_basis = xr->hash_basis;
3175 /* Recirc action. */
3176 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC,
3180 if (tnl_push_pop_send) {
3181 build_tunnel_send(ctx, xport, flow, odp_port);
3182 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3184 odp_port_t odp_tnl_port = ODPP_NONE;
3186 /* XXX: Write better Filter for tunnel port. We can use inport
3187 * int tunnel-port flow to avoid these checks completely. */
3188 if (ofp_port == OFPP_LOCAL &&
3189 ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3191 odp_tnl_port = tnl_port_map_lookup(flow, wc);
3194 if (odp_tnl_port != ODPP_NONE) {
3195 nl_msg_put_odp_port(ctx->odp_actions,
3196 OVS_ACTION_ATTR_TUNNEL_POP,
3199 /* Tunnel push-pop action is not compatible with
3201 compose_ipfix_action(ctx, out_port);
3202 nl_msg_put_odp_port(ctx->odp_actions,
3203 OVS_ACTION_ATTR_OUTPUT,
3209 ctx->sflow_odp_port = odp_port;
3210 ctx->sflow_n_outputs++;
3211 ctx->nf_output_iface = ofp_port;
3214 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
3215 mirror_packet(ctx, xport->xbundle,
3216 xbundle_mirror_dst(xport->xbundle->xbridge,
3222 flow->vlan_tci = flow_vlan_tci;
3223 flow->pkt_mark = flow_pkt_mark;
3224 flow->nw_tos = flow_nw_tos;
3228 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port,
3229 const struct xlate_bond_recirc *xr)
3231 compose_output_action__(ctx, ofp_port, xr, true);
3235 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
3237 struct rule_dpif *old_rule = ctx->rule;
3238 ovs_be64 old_cookie = ctx->rule_cookie;
3239 const struct rule_actions *actions;
3241 if (ctx->xin->resubmit_stats) {
3242 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
3248 ctx->rule_cookie = rule_dpif_get_flow_cookie(rule);
3249 actions = rule_dpif_get_actions(rule);
3250 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
3251 ctx->rule_cookie = old_cookie;
3252 ctx->rule = old_rule;
3257 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
3259 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
3260 XLATE_REPORT_ERROR(ctx, "resubmit actions recursed over %d times",
3261 MAX_RESUBMIT_RECURSION);
3262 ctx->error = XLATE_RECURSION_TOO_DEEP;
3263 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
3264 XLATE_REPORT_ERROR(ctx, "over %d resubmit actions", MAX_RESUBMITS);
3265 ctx->error = XLATE_TOO_MANY_RESUBMITS;
3266 } else if (ctx->odp_actions->size > UINT16_MAX) {
3267 XLATE_REPORT_ERROR(ctx, "resubmits yielded over 64 kB of actions");
3268 /* NOT an error, as we'll be slow-pathing the flow in this case? */
3269 ctx->exit = true; /* XXX: translation still terminated! */
3270 } else if (ctx->stack.size >= 65536) {
3271 XLATE_REPORT_ERROR(ctx, "resubmits yielded over 64 kB of stack");
3272 ctx->error = XLATE_STACK_TOO_DEEP;
3281 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
3282 bool may_packet_in, bool honor_table_miss)
3284 /* Check if we need to recirculate before matching in a table. */
3285 if (ctx->was_mpls) {
3286 ctx_trigger_freeze(ctx);
3289 if (xlate_resubmit_resource_check(ctx)) {
3290 uint8_t old_table_id = ctx->table_id;
3291 struct rule_dpif *rule;
3293 ctx->table_id = table_id;
3295 rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
3296 ctx->tables_version,
3297 &ctx->xin->flow, ctx->xin->wc,
3298 ctx->xin->resubmit_stats,
3299 &ctx->table_id, in_port,
3300 may_packet_in, honor_table_miss);
3302 if (OVS_UNLIKELY(ctx->xin->resubmit_hook)) {
3303 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse + 1);
3307 /* Fill in the cache entry here instead of xlate_recursively
3308 * to make the reference counting more explicit. We take a
3309 * reference in the lookups above if we are going to cache the
3311 if (ctx->xin->xcache) {
3312 struct xc_entry *entry;
3314 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
3315 entry->u.rule = rule;
3316 rule_dpif_ref(rule);
3318 xlate_recursively(ctx, rule);
3321 ctx->table_id = old_table_id;
3327 xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
3328 struct ofputil_bucket *bucket)
3330 if (ctx->xin->resubmit_stats) {
3331 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
3333 if (ctx->xin->xcache) {
3334 struct xc_entry *entry;
3336 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
3337 entry->u.group.group = group_dpif_ref(group);
3338 entry->u.group.bucket = bucket;
3343 xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
3345 uint64_t action_list_stub[1024 / 8];
3346 struct ofpbuf action_list = OFPBUF_STUB_INITIALIZER(action_list_stub);
3347 struct ofpbuf action_set = ofpbuf_const_initializer(bucket->ofpacts,
3348 bucket->ofpacts_len);
3349 struct flow old_flow = ctx->xin->flow;
3350 bool old_was_mpls = ctx->was_mpls;
3352 ofpacts_execute_action_set(&action_list, &action_set);
3354 do_xlate_actions(action_list.data, action_list.size, ctx);
3357 ofpbuf_uninit(&action_list);
3359 /* Check if need to freeze. */
3360 if (ctx->freezing) {
3361 finish_freezing(ctx);
3364 /* Roll back flow to previous state.
3365 * This is equivalent to cloning the packet for each bucket.
3367 * As a side effect any subsequently applied actions will
3368 * also effectively be applied to a clone of the packet taken
3369 * just before applying the all or indirect group.
3371 * Note that group buckets are action sets, hence they cannot modify the
3372 * main action set. Also any stack actions are ignored when executing an
3373 * action set, so group buckets cannot change the stack either.
3374 * However, we do allow resubmit actions in group buckets, which could
3375 * break the above assumptions. It is up to the controller to not mess up
3376 * with the action_set and stack in the tables resubmitted to from
3378 ctx->xin->flow = old_flow;
3380 /* The group bucket popping MPLS should have no effect after bucket
3382 ctx->was_mpls = old_was_mpls;
3384 /* The fact that the group bucket exits (for any reason) does not mean that
3385 * the translation after the group action should exit. Specifically, if
3386 * the group bucket freezes translation, the actions after the group action
3387 * must continue processing with the original, not the frozen packet! */
3392 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3394 struct ofputil_bucket *bucket;
3395 const struct ovs_list *buckets;
3397 group_dpif_get_buckets(group, &buckets);
3399 LIST_FOR_EACH (bucket, list_node, buckets) {
3400 xlate_group_bucket(ctx, bucket);
3402 xlate_group_stats(ctx, group, NULL);
3406 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3408 struct ofputil_bucket *bucket;
3410 bucket = group_first_live_bucket(ctx, group, 0);
3412 xlate_group_bucket(ctx, bucket);
3413 xlate_group_stats(ctx, group, bucket);
3418 xlate_default_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3420 struct flow_wildcards *wc = ctx->wc;
3421 struct ofputil_bucket *bucket;
3424 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
3425 flow_mask_hash_fields(&ctx->xin->flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
3426 bucket = group_best_live_bucket(ctx, group, basis);
3428 xlate_group_bucket(ctx, bucket);
3429 xlate_group_stats(ctx, group, bucket);
3434 xlate_hash_fields_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3436 struct mf_bitmap hash_fields = MF_BITMAP_INITIALIZER;
3437 const struct field_array *fields;
3438 struct ofputil_bucket *bucket;
3442 fields = group_dpif_get_fields(group);
3443 basis = hash_uint64(group_dpif_get_selection_method_param(group));
3445 /* Determine which fields to hash */
3446 for (i = 0; i < MFF_N_IDS; i++) {
3447 if (bitmap_is_set(fields->used.bm, i)) {
3448 const struct mf_field *mf;
3450 /* If the field is already present in 'hash_fields' then
3451 * this loop has already checked that it and its pre-requisites
3452 * are present in the flow and its pre-requisites have
3453 * already been added to 'hash_fields'. There is nothing more
3454 * to do here and as an optimisation the loop can continue. */
3455 if (bitmap_is_set(hash_fields.bm, i)) {
3461 /* Only hash a field if it and its pre-requisites are present
3463 if (!mf_are_prereqs_ok(mf, &ctx->xin->flow)) {
3467 /* Hash both the field and its pre-requisites */
3468 mf_bitmap_set_field_and_prereqs(mf, &hash_fields);
3472 /* Hash the fields */
3473 for (i = 0; i < MFF_N_IDS; i++) {
3474 if (bitmap_is_set(hash_fields.bm, i)) {
3475 const struct mf_field *mf = mf_from_id(i);
3476 union mf_value value;
3479 mf_get_value(mf, &ctx->xin->flow, &value);
3480 /* This seems inefficient but so does apply_mask() */
3481 for (j = 0; j < mf->n_bytes; j++) {
3482 ((uint8_t *) &value)[j] &= ((uint8_t *) &fields->value[i])[j];
3484 basis = hash_bytes(&value, mf->n_bytes, basis);
3486 /* For tunnels, hash in whether the field is present. */
3487 if (mf_is_tun_metadata(mf)) {
3488 basis = hash_boolean(mf_is_set(mf, &ctx->xin->flow), basis);
3491 mf_mask_field(mf, &ctx->wc->masks);
3495 bucket = group_best_live_bucket(ctx, group, basis);
3497 xlate_group_bucket(ctx, bucket);
3498 xlate_group_stats(ctx, group, bucket);
3503 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3505 const char *selection_method = group_dpif_get_selection_method(group);
3507 if (selection_method[0] == '\0') {
3508 xlate_default_select_group(ctx, group);
3509 } else if (!strcasecmp("hash", selection_method)) {
3510 xlate_hash_fields_select_group(ctx, group);
3512 /* Parsing of groups should ensure this never happens */
3518 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3520 bool was_in_group = ctx->in_group;
3521 ctx->in_group = true;
3523 switch (group_dpif_get_type(group)) {
3525 case OFPGT11_INDIRECT:
3526 xlate_all_group(ctx, group);
3528 case OFPGT11_SELECT:
3529 xlate_select_group(ctx, group);
3532 xlate_ff_group(ctx, group);
3537 group_dpif_unref(group);
3539 ctx->in_group = was_in_group;
3543 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3545 if (xlate_resubmit_resource_check(ctx)) {
3546 struct group_dpif *group;
3549 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
3551 xlate_group_action__(ctx, group);
3561 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
3562 const struct ofpact_resubmit *resubmit)
3566 bool may_packet_in = false;
3567 bool honor_table_miss = false;
3569 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
3570 /* Still allow missed packets to be sent to the controller
3571 * if resubmitting from an internal table. */
3572 may_packet_in = true;
3573 honor_table_miss = true;
3576 in_port = resubmit->in_port;
3577 if (in_port == OFPP_IN_PORT) {
3578 in_port = ctx->xin->flow.in_port.ofp_port;
3581 table_id = resubmit->table_id;
3582 if (table_id == 255) {
3583 table_id = ctx->table_id;
3586 xlate_table_action(ctx, in_port, table_id, may_packet_in,
3591 flood_packets(struct xlate_ctx *ctx, bool all)
3593 const struct xport *xport;
3595 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
3596 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
3601 compose_output_action__(ctx, xport->ofp_port, NULL, false);
3602 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
3603 compose_output_action(ctx, xport->ofp_port, NULL);
3607 ctx->nf_output_iface = NF_OUT_FLOOD;
3611 execute_controller_action(struct xlate_ctx *ctx, int len,
3612 enum ofp_packet_in_reason reason,
3613 uint16_t controller_id,
3614 const uint8_t *userdata, size_t userdata_len)
3616 struct dp_packet *packet;
3618 ctx->xout->slow |= SLOW_CONTROLLER;
3619 xlate_commit_actions(ctx);
3620 if (!ctx->xin->packet) {
3624 packet = dp_packet_clone(ctx->xin->packet);
3626 odp_execute_actions(NULL, &packet, 1, false,
3627 ctx->odp_actions->data, ctx->odp_actions->size, NULL);
3629 /* A packet sent by an action in a table-miss rule is considered an
3630 * explicit table miss. OpenFlow before 1.3 doesn't have that concept so
3631 * it will get translated back to OFPR_ACTION for those versions. */
3632 if (reason == OFPR_ACTION
3633 && ctx->rule && rule_dpif_is_table_miss(ctx->rule)) {
3634 reason = OFPR_EXPLICIT_MISS;
3637 size_t packet_len = dp_packet_size(packet);
3639 struct ofproto_async_msg *am = xmalloc(sizeof *am);
3640 *am = (struct ofproto_async_msg) {
3641 .controller_id = controller_id,
3642 .oam = OAM_PACKET_IN,
3646 .packet = dp_packet_steal_data(packet),
3647 .packet_len = packet_len,
3649 .table_id = ctx->table_id,
3650 .cookie = ctx->rule_cookie,
3651 .userdata = (userdata_len
3652 ? xmemdup(userdata, userdata_len)
3654 .userdata_len = userdata_len,
3660 flow_get_metadata(&ctx->xin->flow, &am->pin.up.public.flow_metadata);
3662 ofproto_dpif_send_async_msg(ctx->xbridge->ofproto, am);
3663 dp_packet_delete(packet);
3667 emit_continuation(struct xlate_ctx *ctx, const struct frozen_state *state)
3669 struct ofproto_async_msg *am = xmalloc(sizeof *am);
3670 *am = (struct ofproto_async_msg) {
3671 .controller_id = ctx->pause->controller_id,
3672 .oam = OAM_PACKET_IN,
3676 .userdata = xmemdup(ctx->pause->userdata,
3677 ctx->pause->userdata_len),
3678 .userdata_len = ctx->pause->userdata_len,
3679 .packet = xmemdup(dp_packet_data(ctx->xin->packet),
3680 dp_packet_size(ctx->xin->packet)),
3681 .packet_len = dp_packet_size(ctx->xin->packet),
3683 .bridge = *ofproto_dpif_get_uuid(ctx->xbridge->ofproto),
3684 .stack = xmemdup(state->stack,
3685 state->n_stack * sizeof *state->stack),
3686 .n_stack = state->n_stack,
3687 .mirrors = state->mirrors,
3688 .conntracked = state->conntracked,
3689 .actions = xmemdup(state->ofpacts, state->ofpacts_len),
3690 .actions_len = state->ofpacts_len,
3691 .action_set = xmemdup(state->action_set,
3692 state->action_set_len),
3693 .action_set_len = state->action_set_len,
3695 .max_len = UINT16_MAX,
3698 flow_get_metadata(&ctx->xin->flow, &am->pin.up.public.flow_metadata);
3699 ofproto_dpif_send_async_msg(ctx->xbridge->ofproto, am);
3703 finish_freezing__(struct xlate_ctx *ctx, uint8_t table)
3705 ovs_assert(ctx->freezing);
3707 struct frozen_state state = {
3709 .ofproto_uuid = *ofproto_dpif_get_uuid(ctx->xbridge->ofproto),
3710 .stack = ctx->stack.data,
3711 .n_stack = ctx->stack.size / sizeof(union mf_subvalue),
3712 .mirrors = ctx->mirrors,
3713 .conntracked = ctx->conntracked,
3714 .ofpacts = ctx->frozen_actions.data,
3715 .ofpacts_len = ctx->frozen_actions.size,
3716 .action_set = ctx->action_set.data,
3717 .action_set_len = ctx->action_set.size,
3719 frozen_metadata_from_flow(&state.metadata, &ctx->xin->flow);
3722 if (ctx->xin->packet) {
3723 emit_continuation(ctx, &state);
3726 /* Allocate a unique recirc id for the given metadata state in the
3727 * flow. An existing id, with a new reference to the corresponding
3728 * recirculation context, will be returned if possible.
3729 * The life-cycle of this recirc id is managed by associating it
3730 * with the udpif key ('ukey') created for each new datapath flow. */
3731 uint32_t id = recirc_alloc_id_ctx(&state);
3733 XLATE_REPORT_ERROR(ctx, "Failed to allocate recirculation id");
3734 ctx->error = XLATE_NO_RECIRCULATION_CONTEXT;
3737 recirc_refs_add(&ctx->xout->recircs, id);
3739 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
3742 /* Undo changes done by freezing. */
3743 ctx_cancel_freeze(ctx);
3746 /* Called only when we're freezing. */
3748 finish_freezing(struct xlate_ctx *ctx)
3750 xlate_commit_actions(ctx);
3751 finish_freezing__(ctx, 0);
3754 /* Fork the pipeline here. The current packet will continue processing the
3755 * current action list. A clone of the current packet will recirculate, skip
3756 * the remainder of the current action list and asynchronously resume pipeline
3757 * processing in 'table' with the current metadata and action set. */
3759 compose_recirculate_and_fork(struct xlate_ctx *ctx, uint8_t table)
3761 ctx->freezing = true;
3762 finish_freezing__(ctx, table);
3766 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
3768 struct flow *flow = &ctx->xin->flow;
3771 ovs_assert(eth_type_mpls(mpls->ethertype));
3773 n = flow_count_mpls_labels(flow, ctx->wc);
3775 xlate_commit_actions(ctx);
3776 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3777 if (ctx->xin->packet != NULL) {
3778 XLATE_REPORT_ERROR(ctx, "bridge %s: dropping packet on which an "
3779 "MPLS push action can't be performed as it would "
3780 "have more MPLS LSEs than the %d supported.",
3781 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3783 ctx->error = XLATE_TOO_MANY_MPLS_LABELS;
3787 flow_push_mpls(flow, n, mpls->ethertype, ctx->wc);
3791 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
3793 struct flow *flow = &ctx->xin->flow;
3794 int n = flow_count_mpls_labels(flow, ctx->wc);
3796 if (flow_pop_mpls(flow, n, eth_type, ctx->wc)) {
3797 if (ctx->xbridge->support.odp.recirc) {
3798 ctx->was_mpls = true;
3800 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3801 if (ctx->xin->packet != NULL) {
3802 XLATE_REPORT_ERROR(ctx, "bridge %s: dropping packet on which an "
3803 "MPLS pop action can't be performed as it has "
3804 "more MPLS LSEs than the %d supported.",
3805 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3807 ctx->error = XLATE_TOO_MANY_MPLS_LABELS;
3808 ofpbuf_clear(ctx->odp_actions);
3813 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3815 struct flow *flow = &ctx->xin->flow;
3817 if (!is_ip_any(flow)) {
3821 ctx->wc->masks.nw_ttl = 0xff;
3822 if (flow->nw_ttl > 1) {
3828 for (i = 0; i < ids->n_controllers; i++) {
3829 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3830 ids->cnt_ids[i], NULL, 0);
3833 /* Stop processing for current table. */
3839 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3841 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3842 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
3843 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
3848 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3850 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3851 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
3852 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
3857 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
3859 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3860 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3861 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
3866 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
3868 struct flow *flow = &ctx->xin->flow;
3870 if (eth_type_mpls(flow->dl_type)) {
3871 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
3873 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3876 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3879 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0,
3884 /* Stop processing for current table. */
3889 xlate_output_action(struct xlate_ctx *ctx,
3890 ofp_port_t port, uint16_t max_len, bool may_packet_in)
3892 ofp_port_t prev_nf_output_iface = ctx->nf_output_iface;
3894 ctx->nf_output_iface = NF_OUT_DROP;
3898 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port, NULL);
3901 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3902 0, may_packet_in, true);
3908 flood_packets(ctx, false);
3911 flood_packets(ctx, true);
3913 case OFPP_CONTROLLER:
3914 execute_controller_action(ctx, max_len,
3915 (ctx->in_group ? OFPR_GROUP
3916 : ctx->in_action_set ? OFPR_ACTION_SET
3924 if (port != ctx->xin->flow.in_port.ofp_port) {
3925 compose_output_action(ctx, port, NULL);
3927 xlate_report(ctx, "skipping output to input port");
3932 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3933 ctx->nf_output_iface = NF_OUT_FLOOD;
3934 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3935 ctx->nf_output_iface = prev_nf_output_iface;
3936 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3937 ctx->nf_output_iface != NF_OUT_FLOOD) {
3938 ctx->nf_output_iface = NF_OUT_MULTI;
3943 xlate_output_reg_action(struct xlate_ctx *ctx,
3944 const struct ofpact_output_reg *or)
3946 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3947 if (port <= UINT16_MAX) {
3948 union mf_subvalue value;
3950 memset(&value, 0xff, sizeof value);
3951 mf_write_subfield_flow(&or->src, &value, &ctx->wc->masks);
3952 xlate_output_action(ctx, u16_to_ofp(port),
3953 or->max_len, false);
3958 xlate_enqueue_action(struct xlate_ctx *ctx,
3959 const struct ofpact_enqueue *enqueue)
3961 ofp_port_t ofp_port = enqueue->port;
3962 uint32_t queue_id = enqueue->queue;
3963 uint32_t flow_priority, priority;
3966 /* Translate queue to priority. */
3967 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
3969 /* Fall back to ordinary output action. */
3970 xlate_output_action(ctx, enqueue->port, 0, false);
3974 /* Check output port. */
3975 if (ofp_port == OFPP_IN_PORT) {
3976 ofp_port = ctx->xin->flow.in_port.ofp_port;
3977 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
3981 /* Add datapath actions. */
3982 flow_priority = ctx->xin->flow.skb_priority;
3983 ctx->xin->flow.skb_priority = priority;
3984 compose_output_action(ctx, ofp_port, NULL);
3985 ctx->xin->flow.skb_priority = flow_priority;
3987 /* Update NetFlow output port. */
3988 if (ctx->nf_output_iface == NF_OUT_DROP) {
3989 ctx->nf_output_iface = ofp_port;
3990 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3991 ctx->nf_output_iface = NF_OUT_MULTI;
3996 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3998 uint32_t skb_priority;
4000 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
4001 ctx->xin->flow.skb_priority = skb_priority;
4003 /* Couldn't translate queue to a priority. Nothing to do. A warning
4004 * has already been logged. */
4009 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
4011 const struct xbridge *xbridge = xbridge_;
4022 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
4025 port = get_ofp_port(xbridge, ofp_port);
4026 return port ? port->may_enable : false;
4031 xlate_bundle_action(struct xlate_ctx *ctx,
4032 const struct ofpact_bundle *bundle)
4036 port = bundle_execute(bundle, &ctx->xin->flow, ctx->wc, slave_enabled_cb,
4037 CONST_CAST(struct xbridge *, ctx->xbridge));
4038 if (bundle->dst.field) {
4039 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow, ctx->wc);
4041 xlate_output_action(ctx, port, 0, false);
4046 xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
4047 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
4049 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
4050 if (ctx->xin->may_learn) {
4051 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
4056 xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
4058 learn_mask(learn, ctx->wc);
4060 if (ctx->xin->xcache) {
4061 struct xc_entry *entry;
4063 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
4064 entry->u.learn.ofproto = ctx->xbridge->ofproto;
4065 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
4066 entry->u.learn.ofpacts = ofpbuf_new(64);
4067 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
4068 entry->u.learn.ofpacts);
4069 } else if (ctx->xin->may_learn) {
4070 uint64_t ofpacts_stub[1024 / 8];
4071 struct ofputil_flow_mod fm;
4072 struct ofpbuf ofpacts;
4074 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
4075 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
4076 ofpbuf_uninit(&ofpacts);
4081 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
4082 uint16_t idle_timeout, uint16_t hard_timeout)
4084 if (tcp_flags & (TCP_FIN | TCP_RST)) {
4085 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
4090 xlate_fin_timeout(struct xlate_ctx *ctx,
4091 const struct ofpact_fin_timeout *oft)
4094 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
4095 oft->fin_idle_timeout, oft->fin_hard_timeout);
4096 if (ctx->xin->xcache) {
4097 struct xc_entry *entry;
4099 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
4100 /* XC_RULE already holds a reference on the rule, none is taken
4102 entry->u.fin.rule = ctx->rule;
4103 entry->u.fin.idle = oft->fin_idle_timeout;
4104 entry->u.fin.hard = oft->fin_hard_timeout;
4110 xlate_sample_action(struct xlate_ctx *ctx,
4111 const struct ofpact_sample *os)
4113 /* Scale the probability from 16-bit to 32-bit while representing
4114 * the same percentage. */
4115 uint32_t probability = (os->probability << 16) | os->probability;
4117 if (!ctx->xbridge->support.variable_length_userdata) {
4118 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4120 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
4121 "lacks support (needs Linux 3.10+ or kernel module from "
4126 xlate_commit_actions(ctx);
4128 union user_action_cookie cookie = {
4130 .type = USER_ACTION_COOKIE_FLOW_SAMPLE,
4131 .probability = os->probability,
4132 .collector_set_id = os->collector_set_id,
4133 .obs_domain_id = os->obs_domain_id,
4134 .obs_point_id = os->obs_point_id,
4137 compose_sample_action(ctx, probability, &cookie, sizeof cookie.flow_sample,
4142 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
4144 if (xport->config & (is_stp(&ctx->xin->flow)
4145 ? OFPUTIL_PC_NO_RECV_STP
4146 : OFPUTIL_PC_NO_RECV)) {
4150 /* Only drop packets here if both forwarding and learning are
4151 * disabled. If just learning is enabled, we need to have
4152 * OFPP_NORMAL and the learning action have a look at the packet
4153 * before we can drop it. */
4154 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
4155 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
4163 xlate_write_actions__(struct xlate_ctx *ctx,
4164 const struct ofpact *ofpacts, size_t ofpacts_len)
4166 /* Maintain actset_output depending on the contents of the action set:
4168 * - OFPP_UNSET, if there is no "output" action.
4170 * - The output port, if there is an "output" action and no "group"
4173 * - OFPP_UNSET, if there is a "group" action.
4175 if (!ctx->action_set_has_group) {
4176 const struct ofpact *a;
4177 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4178 if (a->type == OFPACT_OUTPUT) {
4179 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(a)->port;
4180 } else if (a->type == OFPACT_GROUP) {
4181 ctx->xin->flow.actset_output = OFPP_UNSET;
4182 ctx->action_set_has_group = true;
4188 ofpbuf_put(&ctx->action_set, ofpacts, ofpacts_len);
4192 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact_nest *a)
4194 xlate_write_actions__(ctx, a->actions, ofpact_nest_get_action_len(a));
4198 xlate_action_set(struct xlate_ctx *ctx)
4200 uint64_t action_list_stub[1024 / 64];
4201 struct ofpbuf action_list;
4203 ctx->in_action_set = true;
4204 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
4205 ofpacts_execute_action_set(&action_list, &ctx->action_set);
4206 /* Clear the action set, as it is not needed any more. */
4207 ofpbuf_clear(&ctx->action_set);
4208 do_xlate_actions(action_list.data, action_list.size, ctx);
4209 ctx->in_action_set = false;
4210 ofpbuf_uninit(&action_list);
4214 freeze_put_unroll_xlate(struct xlate_ctx *ctx)
4216 struct ofpact_unroll_xlate *unroll = ctx->frozen_actions.header;
4218 /* Restore the table_id and rule cookie for a potential PACKET
4221 (ctx->table_id != unroll->rule_table_id
4222 || ctx->rule_cookie != unroll->rule_cookie)) {
4223 unroll = ofpact_put_UNROLL_XLATE(&ctx->frozen_actions);
4224 unroll->rule_table_id = ctx->table_id;
4225 unroll->rule_cookie = ctx->rule_cookie;
4226 ctx->frozen_actions.header = unroll;
4231 /* Copy actions 'a' through 'end' to ctx->frozen_actions, which will be
4232 * executed after thawing. Inserts an UNROLL_XLATE action, if none is already
4233 * present, before any action that may depend on the current table ID or flow
4236 freeze_unroll_actions(const struct ofpact *a, const struct ofpact *end,
4237 struct xlate_ctx *ctx)
4239 for (; a < end; a = ofpact_next(a)) {
4241 case OFPACT_OUTPUT_REG:
4244 case OFPACT_CONTROLLER:
4245 case OFPACT_DEC_MPLS_TTL:
4246 case OFPACT_DEC_TTL:
4247 /* These actions may generate asynchronous messages, which include
4248 * table ID and flow cookie information. */
4249 freeze_put_unroll_xlate(ctx);
4252 case OFPACT_RESUBMIT:
4253 if (ofpact_get_RESUBMIT(a)->table_id == 0xff) {
4254 /* This resubmit action is relative to the current table, so we
4255 * need to track what table that is.*/
4256 freeze_put_unroll_xlate(ctx);
4260 case OFPACT_SET_TUNNEL:
4261 case OFPACT_REG_MOVE:
4262 case OFPACT_SET_FIELD:
4263 case OFPACT_STACK_PUSH:
4264 case OFPACT_STACK_POP:
4266 case OFPACT_WRITE_METADATA:
4267 case OFPACT_GOTO_TABLE:
4268 case OFPACT_ENQUEUE:
4269 case OFPACT_SET_VLAN_VID:
4270 case OFPACT_SET_VLAN_PCP:
4271 case OFPACT_STRIP_VLAN:
4272 case OFPACT_PUSH_VLAN:
4273 case OFPACT_SET_ETH_SRC:
4274 case OFPACT_SET_ETH_DST:
4275 case OFPACT_SET_IPV4_SRC:
4276 case OFPACT_SET_IPV4_DST:
4277 case OFPACT_SET_IP_DSCP:
4278 case OFPACT_SET_IP_ECN:
4279 case OFPACT_SET_IP_TTL:
4280 case OFPACT_SET_L4_SRC_PORT:
4281 case OFPACT_SET_L4_DST_PORT:
4282 case OFPACT_SET_QUEUE:
4283 case OFPACT_POP_QUEUE:
4284 case OFPACT_PUSH_MPLS:
4285 case OFPACT_POP_MPLS:
4286 case OFPACT_SET_MPLS_LABEL:
4287 case OFPACT_SET_MPLS_TC:
4288 case OFPACT_SET_MPLS_TTL:
4289 case OFPACT_MULTIPATH:
4292 case OFPACT_UNROLL_XLATE:
4293 case OFPACT_FIN_TIMEOUT:
4294 case OFPACT_CLEAR_ACTIONS:
4295 case OFPACT_WRITE_ACTIONS:
4298 case OFPACT_DEBUG_RECIRC:
4301 /* These may not generate PACKET INs. */
4305 case OFPACT_CONJUNCTION:
4306 /* These need not be copied for restoration. */
4309 /* Copy the action over. */
4310 ofpbuf_put(&ctx->frozen_actions, a, OFPACT_ALIGN(a->len));
4314 #define CHECK_MPLS_RECIRCULATION() \
4315 if (ctx->was_mpls) { \
4316 ctx_trigger_freeze(ctx); \
4319 #define CHECK_MPLS_RECIRCULATION_IF(COND) \
4321 CHECK_MPLS_RECIRCULATION(); \
4325 put_ct_mark(const struct flow *flow, struct flow *base_flow,
4326 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4333 odp_attr.key = flow->ct_mark;
4334 odp_attr.mask = wc->masks.ct_mark;
4336 if (odp_attr.mask && odp_attr.key != base_flow->ct_mark) {
4337 nl_msg_put_unspec(odp_actions, OVS_CT_ATTR_MARK, &odp_attr,
4343 put_ct_label(const struct flow *flow, struct flow *base_flow,
4344 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4346 if (!ovs_u128_is_zero(&wc->masks.ct_label)
4347 && !ovs_u128_equals(&flow->ct_label, &base_flow->ct_label)) {
4353 odp_ct_label = nl_msg_put_unspec_uninit(odp_actions,
4355 sizeof(*odp_ct_label));
4356 odp_ct_label->key = flow->ct_label;
4357 odp_ct_label->mask = wc->masks.ct_label;
4362 put_ct_helper(struct ofpbuf *odp_actions, struct ofpact_conntrack *ofc)
4365 if (ofc->alg == IPPORT_FTP) {
4366 nl_msg_put_string(odp_actions, OVS_CT_ATTR_HELPER, "ftp");
4368 VLOG_WARN("Cannot serialize ct_helper %d\n", ofc->alg);
4374 put_ct_nat(struct xlate_ctx *ctx)
4376 struct ofpact_nat *ofn = ctx->ct_nat_action;
4383 nat_offset = nl_msg_start_nested(ctx->odp_actions, OVS_CT_ATTR_NAT);
4384 if (ofn->flags & NX_NAT_F_SRC || ofn->flags & NX_NAT_F_DST) {
4385 nl_msg_put_flag(ctx->odp_actions, ofn->flags & NX_NAT_F_SRC
4386 ? OVS_NAT_ATTR_SRC : OVS_NAT_ATTR_DST);
4387 if (ofn->flags & NX_NAT_F_PERSISTENT) {
4388 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PERSISTENT);
4390 if (ofn->flags & NX_NAT_F_PROTO_HASH) {
4391 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PROTO_HASH);
4392 } else if (ofn->flags & NX_NAT_F_PROTO_RANDOM) {
4393 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PROTO_RANDOM);
4395 if (ofn->range_af == AF_INET) {
4396 nl_msg_put_be32(ctx->odp_actions, OVS_NAT_ATTR_IP_MIN,
4397 ofn->range.addr.ipv4.min);
4398 if (ofn->range.addr.ipv4.max &&
4399 (ntohl(ofn->range.addr.ipv4.max)
4400 > ntohl(ofn->range.addr.ipv4.min))) {
4401 nl_msg_put_be32(ctx->odp_actions, OVS_NAT_ATTR_IP_MAX,
4402 ofn->range.addr.ipv4.max);
4404 } else if (ofn->range_af == AF_INET6) {
4405 nl_msg_put_unspec(ctx->odp_actions, OVS_NAT_ATTR_IP_MIN,
4406 &ofn->range.addr.ipv6.min,
4407 sizeof ofn->range.addr.ipv6.min);
4408 if (!ipv6_mask_is_any(&ofn->range.addr.ipv6.max) &&
4409 memcmp(&ofn->range.addr.ipv6.max, &ofn->range.addr.ipv6.min,
4410 sizeof ofn->range.addr.ipv6.max) > 0) {
4411 nl_msg_put_unspec(ctx->odp_actions, OVS_NAT_ATTR_IP_MAX,
4412 &ofn->range.addr.ipv6.max,
4413 sizeof ofn->range.addr.ipv6.max);
4416 if (ofn->range_af != AF_UNSPEC && ofn->range.proto.min) {
4417 nl_msg_put_u16(ctx->odp_actions, OVS_NAT_ATTR_PROTO_MIN,
4418 ofn->range.proto.min);
4419 if (ofn->range.proto.max &&
4420 ofn->range.proto.max > ofn->range.proto.min) {
4421 nl_msg_put_u16(ctx->odp_actions, OVS_NAT_ATTR_PROTO_MAX,
4422 ofn->range.proto.max);
4426 nl_msg_end_nested(ctx->odp_actions, nat_offset);
4430 compose_conntrack_action(struct xlate_ctx *ctx, struct ofpact_conntrack *ofc)
4432 ovs_u128 old_ct_label = ctx->base_flow.ct_label;
4433 uint32_t old_ct_mark = ctx->base_flow.ct_mark;
4437 /* Ensure that any prior actions are applied before composing the new
4438 * conntrack action. */
4439 xlate_commit_actions(ctx);
4441 /* Process nested actions first, to populate the key. */
4442 ctx->ct_nat_action = NULL;
4443 do_xlate_actions(ofc->actions, ofpact_ct_get_action_len(ofc), ctx);
4445 if (ofc->zone_src.field) {
4446 zone = mf_get_subfield(&ofc->zone_src, &ctx->xin->flow);
4448 zone = ofc->zone_imm;
4451 ct_offset = nl_msg_start_nested(ctx->odp_actions, OVS_ACTION_ATTR_CT);
4452 if (ofc->flags & NX_CT_F_COMMIT) {
4453 nl_msg_put_flag(ctx->odp_actions, OVS_CT_ATTR_COMMIT);
4455 nl_msg_put_u16(ctx->odp_actions, OVS_CT_ATTR_ZONE, zone);
4456 put_ct_mark(&ctx->xin->flow, &ctx->base_flow, ctx->odp_actions, ctx->wc);
4457 put_ct_label(&ctx->xin->flow, &ctx->base_flow, ctx->odp_actions, ctx->wc);
4458 put_ct_helper(ctx->odp_actions, ofc);
4460 ctx->ct_nat_action = NULL;
4461 nl_msg_end_nested(ctx->odp_actions, ct_offset);
4463 /* Restore the original ct fields in the key. These should only be exposed
4464 * after recirculation to another table. */
4465 ctx->base_flow.ct_mark = old_ct_mark;
4466 ctx->base_flow.ct_label = old_ct_label;
4468 if (ofc->recirc_table == NX_CT_RECIRC_NONE) {
4469 /* If we do not recirculate as part of this action, hide the results of
4470 * connection tracking from subsequent recirculations. */
4471 ctx->conntracked = false;
4473 /* Use ct_* fields from datapath during recirculation upcall. */
4474 ctx->conntracked = true;
4475 compose_recirculate_and_fork(ctx, ofc->recirc_table);
4480 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4481 struct xlate_ctx *ctx)
4483 struct flow_wildcards *wc = ctx->wc;
4484 struct flow *flow = &ctx->xin->flow;
4485 const struct ofpact *a;
4487 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
4488 tnl_neigh_snoop(flow, wc, ctx->xbridge->name);
4490 /* dl_type already in the mask, not set below. */
4492 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4493 struct ofpact_controller *controller;
4494 const struct ofpact_metadata *metadata;
4495 const struct ofpact_set_field *set_field;
4496 const struct mf_field *mf;
4503 /* Check if need to store the remaining actions for later
4505 if (ctx->freezing) {
4506 freeze_unroll_actions(a, ofpact_end(ofpacts, ofpacts_len),
4514 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
4515 ofpact_get_OUTPUT(a)->max_len, true);
4519 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
4520 /* Group could not be found. */
4525 case OFPACT_CONTROLLER:
4526 controller = ofpact_get_CONTROLLER(a);
4527 if (controller->pause) {
4528 ctx->pause = controller;
4529 ctx->xout->slow |= SLOW_CONTROLLER;
4530 ctx_trigger_freeze(ctx);
4533 execute_controller_action(ctx, controller->max_len,
4535 controller->controller_id,
4536 controller->userdata,
4537 controller->userdata_len);
4541 case OFPACT_ENQUEUE:
4542 memset(&wc->masks.skb_priority, 0xff,
4543 sizeof wc->masks.skb_priority);
4544 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
4547 case OFPACT_SET_VLAN_VID:
4548 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
4549 if (flow->vlan_tci & htons(VLAN_CFI) ||
4550 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
4551 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
4552 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
4557 case OFPACT_SET_VLAN_PCP:
4558 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
4559 if (flow->vlan_tci & htons(VLAN_CFI) ||
4560 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
4561 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
4562 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
4563 << VLAN_PCP_SHIFT) | VLAN_CFI);
4567 case OFPACT_STRIP_VLAN:
4568 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4569 flow->vlan_tci = htons(0);
4572 case OFPACT_PUSH_VLAN:
4573 /* XXX 802.1AD(QinQ) */
4574 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4575 flow->vlan_tci = htons(VLAN_CFI);
4578 case OFPACT_SET_ETH_SRC:
4579 WC_MASK_FIELD(wc, dl_src);
4580 flow->dl_src = ofpact_get_SET_ETH_SRC(a)->mac;
4583 case OFPACT_SET_ETH_DST:
4584 WC_MASK_FIELD(wc, dl_dst);
4585 flow->dl_dst = ofpact_get_SET_ETH_DST(a)->mac;
4588 case OFPACT_SET_IPV4_SRC:
4589 CHECK_MPLS_RECIRCULATION();
4590 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4591 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
4592 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
4596 case OFPACT_SET_IPV4_DST:
4597 CHECK_MPLS_RECIRCULATION();
4598 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4599 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
4600 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
4604 case OFPACT_SET_IP_DSCP:
4605 CHECK_MPLS_RECIRCULATION();
4606 if (is_ip_any(flow)) {
4607 wc->masks.nw_tos |= IP_DSCP_MASK;
4608 flow->nw_tos &= ~IP_DSCP_MASK;
4609 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
4613 case OFPACT_SET_IP_ECN:
4614 CHECK_MPLS_RECIRCULATION();
4615 if (is_ip_any(flow)) {
4616 wc->masks.nw_tos |= IP_ECN_MASK;
4617 flow->nw_tos &= ~IP_ECN_MASK;
4618 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
4622 case OFPACT_SET_IP_TTL:
4623 CHECK_MPLS_RECIRCULATION();
4624 if (is_ip_any(flow)) {
4625 wc->masks.nw_ttl = 0xff;
4626 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
4630 case OFPACT_SET_L4_SRC_PORT:
4631 CHECK_MPLS_RECIRCULATION();
4632 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4633 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4634 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
4635 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
4639 case OFPACT_SET_L4_DST_PORT:
4640 CHECK_MPLS_RECIRCULATION();
4641 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4642 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4643 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
4644 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
4648 case OFPACT_RESUBMIT:
4649 /* Freezing complicates resubmit. There are two cases:
4651 * - If mpls_pop has been executed, then the flow table lookup
4652 * as part of resubmit might depend on fields that can only
4653 * be obtained via recirculation, so the resubmit itself
4654 * triggers recirculation and we need to make sure that the
4655 * resubmit is executed again after recirculation.
4656 * Therefore, in this case we trigger recirculation and let
4657 * the code following this "switch" append the resubmit to
4658 * the post-recirculation actions.
4660 * - Otherwise, some action in the flow entry found by resubmit
4661 * might trigger freezing. If that happens, then we do not
4662 * want to execute the resubmit again during thawing, so we
4663 * want to skip back to the head of the loop to avoid that,
4664 * only adding any actions that follow the resubmit to the
4667 if (ctx->was_mpls) {
4668 ctx_trigger_freeze(ctx);
4671 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
4674 case OFPACT_SET_TUNNEL:
4675 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
4678 case OFPACT_SET_QUEUE:
4679 memset(&wc->masks.skb_priority, 0xff,
4680 sizeof wc->masks.skb_priority);
4681 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
4684 case OFPACT_POP_QUEUE:
4685 memset(&wc->masks.skb_priority, 0xff,
4686 sizeof wc->masks.skb_priority);
4687 flow->skb_priority = ctx->orig_skb_priority;
4690 case OFPACT_REG_MOVE:
4691 CHECK_MPLS_RECIRCULATION_IF(
4692 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
4693 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
4694 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
4697 case OFPACT_SET_FIELD:
4698 CHECK_MPLS_RECIRCULATION_IF(
4699 mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field));
4700 set_field = ofpact_get_SET_FIELD(a);
4701 mf = set_field->field;
4703 /* Set field action only ever overwrites packet's outermost
4704 * applicable header fields. Do nothing if no header exists. */
4705 if (mf->id == MFF_VLAN_VID) {
4706 wc->masks.vlan_tci |= htons(VLAN_CFI);
4707 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4710 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
4711 /* 'dl_type' is already unwildcarded. */
4712 && !eth_type_mpls(flow->dl_type)) {
4715 /* A flow may wildcard nw_frag. Do nothing if setting a transport
4716 * header field on a packet that does not have them. */
4717 mf_mask_field_and_prereqs(mf, wc);
4718 if (mf_are_prereqs_ok(mf, flow)) {
4719 mf_set_flow_value_masked(mf, &set_field->value,
4720 &set_field->mask, flow);
4724 case OFPACT_STACK_PUSH:
4725 CHECK_MPLS_RECIRCULATION_IF(
4726 mf_is_l3_or_higher(ofpact_get_STACK_PUSH(a)->subfield.field));
4727 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
4731 case OFPACT_STACK_POP:
4732 CHECK_MPLS_RECIRCULATION_IF(
4733 mf_is_l3_or_higher(ofpact_get_STACK_POP(a)->subfield.field));
4734 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
4738 case OFPACT_PUSH_MPLS:
4739 /* Recirculate if it is an IP packet with a zero ttl. This may
4740 * indicate that the packet was previously MPLS and an MPLS pop
4741 * action converted it to IP. In this case recirculating should
4742 * reveal the IP TTL which is used as the basis for a new MPLS
4744 CHECK_MPLS_RECIRCULATION_IF(
4745 !flow_count_mpls_labels(flow, wc)
4746 && flow->nw_ttl == 0
4747 && is_ip_any(flow));
4748 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
4751 case OFPACT_POP_MPLS:
4752 CHECK_MPLS_RECIRCULATION();
4753 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
4756 case OFPACT_SET_MPLS_LABEL:
4757 CHECK_MPLS_RECIRCULATION();
4758 compose_set_mpls_label_action(
4759 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
4762 case OFPACT_SET_MPLS_TC:
4763 CHECK_MPLS_RECIRCULATION();
4764 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
4767 case OFPACT_SET_MPLS_TTL:
4768 CHECK_MPLS_RECIRCULATION();
4769 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
4772 case OFPACT_DEC_MPLS_TTL:
4773 CHECK_MPLS_RECIRCULATION();
4774 if (compose_dec_mpls_ttl_action(ctx)) {
4779 case OFPACT_DEC_TTL:
4780 CHECK_MPLS_RECIRCULATION();
4781 wc->masks.nw_ttl = 0xff;
4782 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
4788 /* Nothing to do. */
4791 case OFPACT_MULTIPATH:
4792 CHECK_MPLS_RECIRCULATION();
4793 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
4797 CHECK_MPLS_RECIRCULATION();
4798 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
4801 case OFPACT_OUTPUT_REG:
4802 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
4806 CHECK_MPLS_RECIRCULATION();
4807 xlate_learn_action(ctx, ofpact_get_LEARN(a));
4810 case OFPACT_CONJUNCTION: {
4811 /* A flow with a "conjunction" action represents part of a special
4812 * kind of "set membership match". Such a flow should not actually
4813 * get executed, but it could via, say, a "packet-out", even though
4814 * that wouldn't be useful. Log it to help debugging. */
4815 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4816 VLOG_INFO_RL(&rl, "executing no-op conjunction action");
4824 case OFPACT_UNROLL_XLATE: {
4825 struct ofpact_unroll_xlate *unroll = ofpact_get_UNROLL_XLATE(a);
4827 /* Restore translation context data that was stored earlier. */
4828 ctx->table_id = unroll->rule_table_id;
4829 ctx->rule_cookie = unroll->rule_cookie;
4832 case OFPACT_FIN_TIMEOUT:
4833 CHECK_MPLS_RECIRCULATION();
4834 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4835 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
4838 case OFPACT_CLEAR_ACTIONS:
4839 ofpbuf_clear(&ctx->action_set);
4840 ctx->xin->flow.actset_output = OFPP_UNSET;
4841 ctx->action_set_has_group = false;
4844 case OFPACT_WRITE_ACTIONS:
4845 xlate_write_actions(ctx, ofpact_get_WRITE_ACTIONS(a));
4848 case OFPACT_WRITE_METADATA:
4849 metadata = ofpact_get_WRITE_METADATA(a);
4850 flow->metadata &= ~metadata->mask;
4851 flow->metadata |= metadata->metadata & metadata->mask;
4855 /* Not implemented yet. */
4858 case OFPACT_GOTO_TABLE: {
4859 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
4861 ovs_assert(ctx->table_id < ogt->table_id);
4863 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
4864 ogt->table_id, true, true);
4869 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
4873 CHECK_MPLS_RECIRCULATION();
4874 compose_conntrack_action(ctx, ofpact_get_CT(a));
4878 /* This will be processed by compose_conntrack_action(). */
4879 ctx->ct_nat_action = ofpact_get_NAT(a);
4882 case OFPACT_DEBUG_RECIRC:
4883 ctx_trigger_freeze(ctx);
4888 /* Check if need to store this and the remaining actions for later
4890 if (!ctx->error && ctx->exit && ctx_first_frozen_action(ctx)) {
4891 freeze_unroll_actions(a, ofpact_end(ofpacts, ofpacts_len), ctx);
4898 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
4899 const struct flow *flow, ofp_port_t in_port,
4900 struct rule_dpif *rule, uint16_t tcp_flags,
4901 const struct dp_packet *packet, struct flow_wildcards *wc,
4902 struct ofpbuf *odp_actions)
4904 xin->ofproto = ofproto;
4906 xin->flow.in_port.ofp_port = in_port;
4907 xin->flow.actset_output = OFPP_UNSET;
4908 xin->packet = packet;
4909 xin->may_learn = packet != NULL;
4912 xin->ofpacts = NULL;
4913 xin->ofpacts_len = 0;
4914 xin->tcp_flags = tcp_flags;
4915 xin->resubmit_hook = NULL;
4916 xin->report_hook = NULL;
4917 xin->resubmit_stats = NULL;
4921 xin->odp_actions = odp_actions;
4923 /* Do recirc lookup. */
4924 xin->frozen_state = NULL;
4925 if (flow->recirc_id) {
4926 const struct recirc_id_node *node
4927 = recirc_id_node_find(flow->recirc_id);
4929 xin->frozen_state = &node->state;
4935 xlate_out_uninit(struct xlate_out *xout)
4938 recirc_refs_unref(&xout->recircs);
4942 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
4943 * into datapath actions, using 'ctx', and discards the datapath actions. */
4945 xlate_actions_for_side_effects(struct xlate_in *xin)
4947 struct xlate_out xout;
4948 enum xlate_error error;
4950 error = xlate_actions(xin, &xout);
4952 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4954 VLOG_WARN_RL(&rl, "xlate_actions failed (%s)!", xlate_strerror(error));
4957 xlate_out_uninit(&xout);
4960 static struct skb_priority_to_dscp *
4961 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
4963 struct skb_priority_to_dscp *pdscp;
4966 hash = hash_int(skb_priority, 0);
4967 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
4968 if (pdscp->skb_priority == skb_priority) {
4976 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
4979 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
4980 *dscp = pdscp ? pdscp->dscp : 0;
4981 return pdscp != NULL;
4985 count_skb_priorities(const struct xport *xport)
4987 return hmap_count(&xport->skb_priorities);
4991 clear_skb_priorities(struct xport *xport)
4993 struct skb_priority_to_dscp *pdscp, *next;
4995 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
4996 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
5002 actions_output_to_local_port(const struct xlate_ctx *ctx)
5004 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
5005 const struct nlattr *a;
5008 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->odp_actions->data,
5009 ctx->odp_actions->size) {
5010 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
5011 && nl_attr_get_odp_port(a) == local_odp_port) {
5018 #if defined(__linux__)
5019 /* Returns the maximum number of packets that the Linux kernel is willing to
5020 * queue up internally to certain kinds of software-implemented ports, or the
5021 * default (and rarely modified) value if it cannot be determined. */
5023 netdev_max_backlog(void)
5025 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
5026 static int max_backlog = 1000; /* The normal default value. */
5028 if (ovsthread_once_start(&once)) {
5029 static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
5033 stream = fopen(filename, "r");
5035 VLOG_INFO("%s: open failed (%s)", filename, ovs_strerror(errno));
5037 if (fscanf(stream, "%d", &n) != 1) {
5038 VLOG_WARN("%s: read error", filename);
5039 } else if (n <= 100) {
5040 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
5046 ovsthread_once_done(&once);
5048 VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
5054 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
5057 count_output_actions(const struct ofpbuf *odp_actions)
5059 const struct nlattr *a;
5063 NL_ATTR_FOR_EACH_UNSAFE (a, left, odp_actions->data, odp_actions->size) {
5064 if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
5070 #endif /* defined(__linux__) */
5072 /* Returns true if 'odp_actions' contains more output actions than the datapath
5073 * can reliably handle in one go. On Linux, this is the value of the
5074 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
5075 * packets that the kernel is willing to queue up for processing while the
5076 * datapath is processing a set of actions. */
5078 too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
5081 return (odp_actions->size / NL_A_U32_SIZE > netdev_max_backlog()
5082 && count_output_actions(odp_actions) > netdev_max_backlog());
5084 /* OSes other than Linux might have similar limits, but we don't know how
5085 * to determine them.*/
5091 xlate_wc_init(struct xlate_ctx *ctx)
5093 flow_wildcards_init_catchall(ctx->wc);
5095 /* Some fields we consider to always be examined. */
5096 WC_MASK_FIELD(ctx->wc, in_port);
5097 WC_MASK_FIELD(ctx->wc, dl_type);
5098 if (is_ip_any(&ctx->xin->flow)) {
5099 WC_MASK_FIELD_MASK(ctx->wc, nw_frag, FLOW_NW_FRAG_MASK);
5102 if (ctx->xbridge->support.odp.recirc) {
5103 /* Always exactly match recirc_id when datapath supports
5105 WC_MASK_FIELD(ctx->wc, recirc_id);
5108 if (ctx->xbridge->netflow) {
5109 netflow_mask_wc(&ctx->xin->flow, ctx->wc);
5112 tnl_wc_init(&ctx->xin->flow, ctx->wc);
5116 xlate_wc_finish(struct xlate_ctx *ctx)
5118 /* Clear the metadata and register wildcard masks, because we won't
5119 * use non-header fields as part of the cache. */
5120 flow_wildcards_clear_non_packet_fields(ctx->wc);
5122 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
5123 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
5124 * represent these fields. The datapath interface, on the other hand,
5125 * represents them with just 8 bits each. This means that if the high
5126 * 8 bits of the masks for these fields somehow become set, then they
5127 * will get chopped off by a round trip through the datapath, and
5128 * revalidation will spot that as an inconsistency and delete the flow.
5129 * Avoid the problem here by making sure that only the low 8 bits of
5130 * either field can be unwildcarded for ICMP.
5132 if (is_icmpv4(&ctx->xin->flow) || is_icmpv6(&ctx->xin->flow)) {
5133 ctx->wc->masks.tp_src &= htons(UINT8_MAX);
5134 ctx->wc->masks.tp_dst &= htons(UINT8_MAX);
5136 /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
5137 if (ctx->wc->masks.vlan_tci) {
5138 ctx->wc->masks.vlan_tci |= htons(VLAN_CFI);
5142 /* Translates the flow, actions, or rule in 'xin' into datapath actions in
5144 * The caller must take responsibility for eventually freeing 'xout', with
5145 * xlate_out_uninit().
5146 * Returns 'XLATE_OK' if translation was successful. In case of an error an
5147 * empty set of actions will be returned in 'xin->odp_actions' (if non-NULL),
5148 * so that most callers may ignore the return value and transparently install a
5149 * drop flow when the translation fails. */
5151 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
5153 *xout = (struct xlate_out) {
5155 .recircs = RECIRC_REFS_EMPTY_INITIALIZER,
5158 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5159 struct xbridge *xbridge = xbridge_lookup(xcfg, xin->ofproto);
5161 return XLATE_BRIDGE_NOT_FOUND;
5164 struct flow *flow = &xin->flow;
5166 union mf_subvalue stack_stub[1024 / sizeof(union mf_subvalue)];
5167 uint64_t action_set_stub[1024 / 8];
5168 uint64_t frozen_actions_stub[1024 / 8];
5169 struct flow_wildcards scratch_wc;
5170 uint64_t actions_stub[256 / 8];
5171 struct ofpbuf scratch_actions = OFPBUF_STUB_INITIALIZER(actions_stub);
5172 struct xlate_ctx ctx = {
5176 .orig_tunnel_ipv6_dst = flow_tnl_dst(&flow->tunnel),
5178 .stack = OFPBUF_STUB_INITIALIZER(stack_stub),
5180 .wc = xin->wc ? xin->wc : &scratch_wc,
5181 .odp_actions = xin->odp_actions ? xin->odp_actions : &scratch_actions,
5183 .recurse = xin->recurse,
5184 .resubmits = xin->resubmits,
5186 .in_action_set = false,
5189 .rule_cookie = OVS_BE64_MAX,
5190 .orig_skb_priority = flow->skb_priority,
5191 .sflow_n_outputs = 0,
5192 .sflow_odp_port = 0,
5193 .nf_output_iface = NF_OUT_DROP,
5199 .frozen_actions = OFPBUF_STUB_INITIALIZER(frozen_actions_stub),
5203 .conntracked = false,
5205 .ct_nat_action = NULL,
5207 .action_set_has_group = false,
5208 .action_set = OFPBUF_STUB_INITIALIZER(action_set_stub),
5211 /* 'base_flow' reflects the packet as it came in, but we need it to reflect
5212 * the packet as the datapath will treat it for output actions:
5214 * - Our datapath doesn't retain tunneling information without us
5215 * re-setting it, so clear the tunnel data.
5217 * - For VLAN splinters, a higher layer may pretend that the packet
5218 * came in on 'flow->in_port.ofp_port' with 'flow->vlan_tci'
5219 * attached, because that's how we want to treat it from an OpenFlow
5220 * perspective. But from the datapath's perspective it actually came
5221 * in on a VLAN device without any VLAN attached. So here we put the
5222 * datapath's view of the VLAN information in 'base_flow' to ensure
5223 * correct treatment.
5225 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
5226 if (flow->in_port.ofp_port
5227 != vsp_realdev_to_vlandev(xbridge->ofproto,
5228 flow->in_port.ofp_port,
5230 ctx.base_flow.vlan_tci = 0;
5233 ofpbuf_reserve(ctx.odp_actions, NL_A_U32_SIZE);
5235 xlate_wc_init(&ctx);
5238 COVERAGE_INC(xlate_actions);
5240 if (xin->frozen_state) {
5241 const struct frozen_state *state = xin->frozen_state;
5243 xlate_report(&ctx, "Thawing frozen state:");
5245 if (xin->ofpacts_len > 0 || ctx.rule) {
5246 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
5247 const char *conflict = xin->ofpacts_len ? "actions" : "rule";
5249 VLOG_WARN_RL(&rl, "Recirculation conflict (%s)!", conflict);
5250 xlate_report(&ctx, "- Recirculation conflict (%s)!", conflict);
5251 ctx.error = XLATE_RECIRCULATION_CONFLICT;
5255 /* Set the bridge for post-recirculation processing if needed. */
5256 if (!uuid_equals(ofproto_dpif_get_uuid(ctx.xbridge->ofproto),
5257 &state->ofproto_uuid)) {
5258 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5259 const struct xbridge *new_bridge
5260 = xbridge_lookup_by_uuid(xcfg, &state->ofproto_uuid);
5262 if (OVS_UNLIKELY(!new_bridge)) {
5263 /* Drop the packet if the bridge cannot be found. */
5264 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
5265 VLOG_WARN_RL(&rl, "Frozen bridge no longer exists.");
5266 xlate_report(&ctx, "- Frozen bridge no longer exists.");
5267 ctx.error = XLATE_BRIDGE_NOT_FOUND;
5270 ctx.xbridge = new_bridge;
5273 /* Set the thawed table id. Note: A table lookup is done only if there
5274 * are no frozen actions. */
5275 ctx.table_id = state->table_id;
5276 xlate_report(&ctx, "- Resuming from table %"PRIu8, ctx.table_id);
5278 if (!state->conntracked) {
5279 clear_conntrack(flow);
5282 /* Restore pipeline metadata. May change flow's in_port and other
5283 * metadata to the values that existed when freezing was triggered. */
5284 frozen_metadata_to_flow(&state->metadata, flow);
5286 /* Restore stack, if any. */
5288 ofpbuf_put(&ctx.stack, state->stack,
5289 state->n_stack * sizeof *state->stack);
5292 /* Restore mirror state. */
5293 ctx.mirrors = state->mirrors;
5295 /* Restore action set, if any. */
5296 if (state->action_set_len) {
5297 xlate_report_actions(&ctx, "- Restoring action set",
5298 state->action_set, state->action_set_len);
5300 flow->actset_output = OFPP_UNSET;
5301 xlate_write_actions__(&ctx, state->action_set,
5302 state->action_set_len);
5305 /* Restore frozen actions. If there are no actions, processing will
5306 * start with a lookup in the table set above. */
5307 xin->ofpacts = state->ofpacts;
5308 xin->ofpacts_len = state->ofpacts_len;
5309 if (state->ofpacts_len) {
5310 xlate_report_actions(&ctx, "- Restoring actions",
5311 xin->ofpacts, xin->ofpacts_len);
5313 } else if (OVS_UNLIKELY(flow->recirc_id)) {
5314 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
5316 VLOG_WARN_RL(&rl, "Recirculation context not found for ID %"PRIx32,
5318 ctx.error = XLATE_NO_RECIRCULATION_CONTEXT;
5321 /* The bridge is now known so obtain its table version. */
5322 ctx.tables_version = ofproto_dpif_get_tables_version(ctx.xbridge->ofproto);
5324 if (!xin->ofpacts && !ctx.rule) {
5325 ctx.rule = rule_dpif_lookup_from_table(
5326 ctx.xbridge->ofproto, ctx.tables_version, flow, xin->wc,
5327 ctx.xin->resubmit_stats, &ctx.table_id,
5328 flow->in_port.ofp_port, true, true);
5329 if (ctx.xin->resubmit_stats) {
5330 rule_dpif_credit_stats(ctx.rule, ctx.xin->resubmit_stats);
5332 if (ctx.xin->xcache) {
5333 struct xc_entry *entry;
5335 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
5336 entry->u.rule = ctx.rule;
5337 rule_dpif_ref(ctx.rule);
5340 if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
5341 ctx.xin->resubmit_hook(ctx.xin, ctx.rule, 0);
5345 /* Get the proximate input port of the packet. (If xin->frozen_state,
5346 * flow->in_port is the ultimate input port of the packet.) */
5347 struct xport *in_port = get_ofp_port(xbridge,
5348 ctx.base_flow.in_port.ofp_port);
5350 /* Tunnel stats only for not-thawed packets. */
5351 if (!xin->frozen_state && in_port && in_port->is_tunnel) {
5352 if (ctx.xin->resubmit_stats) {
5353 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
5355 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
5358 if (ctx.xin->xcache) {
5359 struct xc_entry *entry;
5361 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
5362 entry->u.dev.rx = netdev_ref(in_port->netdev);
5363 entry->u.dev.bfd = bfd_ref(in_port->bfd);
5367 if (!xin->frozen_state && process_special(&ctx, in_port)) {
5368 /* process_special() did all the processing for this packet.
5370 * We do not perform special processing on thawed packets, since that
5371 * was done before they were frozen and should not be redone. */
5372 } else if (in_port && in_port->xbundle
5373 && xbundle_mirror_out(xbridge, in_port->xbundle)) {
5374 if (ctx.xin->packet != NULL) {
5375 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5376 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
5377 "%s, which is reserved exclusively for mirroring",
5378 ctx.xbridge->name, in_port->xbundle->name);
5381 /* Sampling is done on initial reception; don't redo after thawing. */
5382 unsigned int user_cookie_offset = 0;
5383 if (!xin->frozen_state) {
5384 user_cookie_offset = compose_sflow_action(&ctx);
5385 compose_ipfix_action(&ctx, ODPP_NONE);
5387 size_t sample_actions_len = ctx.odp_actions->size;
5389 if (tnl_process_ecn(flow)
5390 && (!in_port || may_receive(in_port, &ctx))) {
5391 const struct ofpact *ofpacts;
5395 ofpacts = xin->ofpacts;
5396 ofpacts_len = xin->ofpacts_len;
5397 } else if (ctx.rule) {
5398 const struct rule_actions *actions
5399 = rule_dpif_get_actions(ctx.rule);
5400 ofpacts = actions->ofpacts;
5401 ofpacts_len = actions->ofpacts_len;
5402 ctx.rule_cookie = rule_dpif_get_flow_cookie(ctx.rule);
5407 mirror_ingress_packet(&ctx);
5408 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
5413 /* We've let OFPP_NORMAL and the learning action look at the
5414 * packet, so cancel all actions and freezing if forwarding is
5416 if (in_port && (!xport_stp_forward_state(in_port) ||
5417 !xport_rstp_forward_state(in_port))) {
5418 ctx.odp_actions->size = sample_actions_len;
5419 ctx_cancel_freeze(&ctx);
5420 ofpbuf_clear(&ctx.action_set);
5423 if (!ctx.freezing) {
5424 xlate_action_set(&ctx);
5427 finish_freezing(&ctx);
5431 /* Output only fully processed packets. */
5433 && xbridge->has_in_band
5434 && in_band_must_output_to_local_port(flow)
5435 && !actions_output_to_local_port(&ctx)) {
5436 compose_output_action(&ctx, OFPP_LOCAL, NULL);
5439 if (user_cookie_offset) {
5440 fix_sflow_action(&ctx, user_cookie_offset);
5444 if (nl_attr_oversized(ctx.odp_actions->size)) {
5445 /* These datapath actions are too big for a Netlink attribute, so we
5446 * can't hand them to the kernel directly. dpif_execute() can execute
5447 * them one by one with help, so just mark the result as SLOW_ACTION to
5448 * prevent the flow from being installed. */
5449 COVERAGE_INC(xlate_actions_oversize);
5450 ctx.xout->slow |= SLOW_ACTION;
5451 } else if (too_many_output_actions(ctx.odp_actions)) {
5452 COVERAGE_INC(xlate_actions_too_many_output);
5453 ctx.xout->slow |= SLOW_ACTION;
5456 /* Do netflow only for packets on initial reception, that are not sent to
5457 * the controller. We consider packets sent to the controller to be part
5458 * of the control plane rather than the data plane. */
5459 if (!xin->frozen_state
5461 && !(xout->slow & SLOW_CONTROLLER)) {
5462 if (ctx.xin->resubmit_stats) {
5463 netflow_flow_update(xbridge->netflow, flow,
5464 ctx.nf_output_iface,
5465 ctx.xin->resubmit_stats);
5467 if (ctx.xin->xcache) {
5468 struct xc_entry *entry;
5470 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
5471 entry->u.nf.netflow = netflow_ref(xbridge->netflow);
5472 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
5473 entry->u.nf.iface = ctx.nf_output_iface;
5478 xlate_wc_finish(&ctx);
5482 ofpbuf_uninit(&ctx.stack);
5483 ofpbuf_uninit(&ctx.action_set);
5484 ofpbuf_uninit(&ctx.frozen_actions);
5485 ofpbuf_uninit(&scratch_actions);
5487 /* Make sure we return a "drop flow" in case of an error. */
5490 if (xin->odp_actions) {
5491 ofpbuf_clear(xin->odp_actions);
5498 xlate_resume(struct ofproto_dpif *ofproto,
5499 const struct ofputil_packet_in_private *pin,
5500 struct ofpbuf *odp_actions,
5501 enum slow_path_reason *slow)
5503 struct dp_packet packet;
5504 dp_packet_use_const(&packet, pin->public.packet,
5505 pin->public.packet_len);
5508 flow_extract(&packet, &flow);
5510 struct xlate_in xin;
5511 xlate_in_init(&xin, ofproto, &flow, 0, NULL, ntohs(flow.tcp_flags),
5512 &packet, NULL, odp_actions);
5514 struct ofpact_note noop;
5515 ofpact_init_NOTE(&noop);
5518 bool any_actions = pin->actions_len > 0;
5519 struct frozen_state state = {
5520 .table_id = 0, /* Not the table where NXAST_PAUSE was executed. */
5521 .ofproto_uuid = pin->bridge,
5522 .stack = pin->stack,
5523 .n_stack = pin->n_stack,
5524 .mirrors = pin->mirrors,
5525 .conntracked = pin->conntracked,
5527 /* When there are no actions, xlate_actions() will search the flow
5528 * table. We don't want it to do that (we want it to resume), so
5529 * supply a no-op action if there aren't any.
5531 * (We can't necessarily avoid translating actions entirely if there
5532 * aren't any actions, because there might be some finishing-up to do
5533 * at the end of the pipeline, and we don't check for those
5535 .ofpacts = any_actions ? pin->actions : &noop.ofpact,
5536 .ofpacts_len = any_actions ? pin->actions_len : sizeof noop,
5538 .action_set = pin->action_set,
5539 .action_set_len = pin->action_set_len,
5541 frozen_metadata_from_flow(&state.metadata,
5542 &pin->public.flow_metadata.flow);
5543 xin.frozen_state = &state;
5545 struct xlate_out xout;
5546 enum xlate_error error = xlate_actions(&xin, &xout);
5548 xlate_out_uninit(&xout);
5550 /* xlate_actions() can generate a number of errors, but only
5551 * XLATE_BRIDGE_NOT_FOUND really stands out to me as one that we should be
5552 * sure to report over OpenFlow. The others could come up in packet-outs
5553 * or regular flow translation and I don't think that it's going to be too
5554 * useful to report them to the controller. */
5555 return error == XLATE_BRIDGE_NOT_FOUND ? OFPERR_NXR_STALE : 0;
5558 /* Sends 'packet' out 'ofport'.
5559 * May modify 'packet'.
5560 * Returns 0 if successful, otherwise a positive errno value. */
5562 xlate_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
5564 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5565 struct xport *xport;
5566 struct ofpact_output output;
5569 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
5570 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
5571 flow_extract(packet, &flow);
5572 flow.in_port.ofp_port = OFPP_NONE;
5574 xport = xport_lookup(xcfg, ofport);
5578 output.port = xport->ofp_port;
5581 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
5582 &output.ofpact, sizeof output,
5586 struct xlate_cache *
5587 xlate_cache_new(void)
5589 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
5591 ofpbuf_init(&xcache->entries, 512);
5595 static struct xc_entry *
5596 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
5598 struct xc_entry *entry;
5600 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
5607 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
5609 if (entry->u.dev.tx) {
5610 netdev_vport_inc_tx(entry->u.dev.tx, stats);
5612 if (entry->u.dev.rx) {
5613 netdev_vport_inc_rx(entry->u.dev.rx, stats);
5615 if (entry->u.dev.bfd) {
5616 bfd_account_rx(entry->u.dev.bfd, stats);
5621 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
5623 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5624 struct xbridge *xbridge;
5625 struct xbundle *xbundle;
5626 struct flow_wildcards wc;
5628 xbridge = xbridge_lookup(xcfg, ofproto);
5633 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
5639 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
5642 /* Push stats and perform side effects of flow translation. */
5644 xlate_push_stats(struct xlate_cache *xcache,
5645 const struct dpif_flow_stats *stats)
5647 struct xc_entry *entry;
5648 struct ofpbuf entries = xcache->entries;
5649 struct eth_addr dmac;
5651 if (!stats->n_packets) {
5655 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5656 switch (entry->type) {
5658 rule_dpif_credit_stats(entry->u.rule, stats);
5661 bond_account(entry->u.bond.bond, entry->u.bond.flow,
5662 entry->u.bond.vid, stats->n_bytes);
5665 xlate_cache_netdev(entry, stats);
5668 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
5669 entry->u.nf.iface, stats);
5672 mirror_update_stats(entry->u.mirror.mbridge,
5673 entry->u.mirror.mirrors,
5674 stats->n_packets, stats->n_bytes);
5677 ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
5680 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
5681 entry->u.normal.vlan);
5683 case XC_FIN_TIMEOUT:
5684 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
5685 entry->u.fin.idle, entry->u.fin.hard);
5688 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
5692 /* Lookup neighbor to avoid timeout. */
5693 tnl_neigh_lookup(entry->u.tnl_neigh_cache.br_name,
5694 &entry->u.tnl_neigh_cache.d_ipv6, &dmac);
5703 xlate_dev_unref(struct xc_entry *entry)
5705 if (entry->u.dev.tx) {
5706 netdev_close(entry->u.dev.tx);
5708 if (entry->u.dev.rx) {
5709 netdev_close(entry->u.dev.rx);
5711 if (entry->u.dev.bfd) {
5712 bfd_unref(entry->u.dev.bfd);
5717 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
5719 netflow_flow_clear(netflow, flow);
5720 netflow_unref(netflow);
5725 xlate_cache_clear(struct xlate_cache *xcache)
5727 struct xc_entry *entry;
5728 struct ofpbuf entries;
5734 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5735 switch (entry->type) {
5737 rule_dpif_unref(entry->u.rule);
5740 free(entry->u.bond.flow);
5741 bond_unref(entry->u.bond.bond);
5744 xlate_dev_unref(entry);
5747 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
5750 mbridge_unref(entry->u.mirror.mbridge);
5753 free(entry->u.learn.fm);
5754 ofpbuf_delete(entry->u.learn.ofpacts);
5757 free(entry->u.normal.flow);
5759 case XC_FIN_TIMEOUT:
5760 /* 'u.fin.rule' is always already held as a XC_RULE, which
5761 * has already released it's reference above. */
5764 group_dpif_unref(entry->u.group.group);
5773 ofpbuf_clear(&xcache->entries);
5777 xlate_cache_delete(struct xlate_cache *xcache)
5779 xlate_cache_clear(xcache);
5780 ofpbuf_uninit(&xcache->entries);