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 /* These are used for non-bond recirculation. The recirculation IDs are
214 * stored in xout and must be associated with a datapath flow (ukey),
215 * otherwise they will be freed when the xout is uninitialized.
218 * Steps in Recirculation Translation
219 * ==================================
221 * At some point during translation, the code recognizes the need for
222 * recirculation. For example, recirculation is necessary when, after
223 * popping the last MPLS label, an action or a match tries to examine or
224 * modify a field that has been newly revealed following the MPLS label.
226 * The simplest part of the work to be done is to commit existing changes to
227 * the packet, which produces datapath actions corresponding to the changes,
228 * and after this, add an OVS_ACTION_ATTR_RECIRC datapath action.
230 * The main problem here is preserving state. When the datapath executes
231 * OVS_ACTION_ATTR_RECIRC, it will upcall to userspace to get a translation
232 * for the post-recirculation actions. At this point userspace has to
233 * resume the translation where it left off, which means that it has to
234 * execute the following:
236 * - The action that prompted recirculation, and any actions following
237 * it within the same flow.
239 * - If the action that prompted recirculation was invoked within a
240 * NXAST_RESUBMIT, then any actions following the resubmit. These
241 * "resubmit"s can be nested, so this has to go all the way up the
244 * - The OpenFlow 1.1+ action set.
246 * State that actions and flow table lookups can depend on, such as the
247 * following, must also be preserved:
249 * - Metadata fields (input port, registers, OF1.1+ metadata, ...).
251 * - Action set, stack
253 * - The table ID and cookie of the flow being translated at each level
254 * of the control stack (since OFPAT_CONTROLLER actions send these to
257 * Translation allows for the control of this state preservation via these
258 * members. When a need for recirculation is identified, the translation
261 * 1. Sets 'recirc_action_offset' to the current size of 'action_set'. The
262 * action set is part of what needs to be preserved, so this allows the
263 * action set and the additional state to share the 'action_set' buffer.
264 * Later steps can tell that setup for recirculation is in progress from
265 * the nonnegative value of 'recirc_action_offset'.
267 * 2. Sets 'exit' to true to tell later steps that we're exiting from the
268 * translation process.
270 * 3. Adds an OFPACT_UNROLL_XLATE action to 'action_set'. This action
271 * holds the current table ID and cookie so that they can be restored
272 * during a post-recirculation upcall translation.
274 * 4. Adds the action that prompted recirculation and any actions following
275 * it within the same flow to 'action_set', so that they can be executed
276 * during a post-recirculation upcall translation.
280 * 6. The action that prompted recirculation might be nested in a stack of
281 * nested "resubmit"s that have actions remaining. Each of these notices
282 * that we're exiting (from 'exit') and that recirculation setup is in
283 * progress (from 'recirc_action_offset') and responds by adding more
284 * OFPACT_UNROLL_XLATE actions to 'action_set', as necessary, and any
285 * actions that were yet unprocessed.
287 * The caller stores all the state produced by this process associated with
288 * the recirculation ID. For post-recirculation upcall translation, the
289 * caller passes it back in for the new translation to execute. The
290 * process yielded a set of ofpacts that can be translated directly, so it
291 * is not much of a special case at that point.
293 int recirc_action_offset; /* Offset in 'action_set' to actions to be
294 * executed after recirculation, or -1. */
295 int last_unroll_offset; /* Offset in 'action_set' to the latest unroll
298 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
299 * This is a trigger for recirculation in cases where translating an action
300 * or looking up a flow requires access to the fields of the packet after
301 * the MPLS label stack that was originally present. */
304 /* True if conntrack has been performed on this packet during processing
305 * on the current bridge. This is used to determine whether conntrack
306 * state from the datapath should be honored after recirculation. */
309 /* Pointer to an embedded NAT action in a conntrack action, or NULL. */
310 struct ofpact_nat *ct_nat_action;
312 /* OpenFlow 1.1+ action set.
314 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
315 * When translation is otherwise complete, ofpacts_execute_action_set()
316 * converts it to a set of "struct ofpact"s that can be translated into
317 * datapath actions. */
318 bool action_set_has_group; /* Action set contains OFPACT_GROUP? */
319 struct ofpbuf action_set; /* Action set. */
321 enum xlate_error error; /* Translation failed. */
324 const char *xlate_strerror(enum xlate_error error)
329 case XLATE_BRIDGE_NOT_FOUND:
330 return "Bridge not found";
331 case XLATE_RECURSION_TOO_DEEP:
332 return "Recursion too deep";
333 case XLATE_TOO_MANY_RESUBMITS:
334 return "Too many resubmits";
335 case XLATE_STACK_TOO_DEEP:
336 return "Stack too deep";
337 case XLATE_NO_RECIRCULATION_CONTEXT:
338 return "No recirculation context";
339 case XLATE_RECIRCULATION_CONFLICT:
340 return "Recirculation conflict";
341 case XLATE_TOO_MANY_MPLS_LABELS:
342 return "Too many MPLS labels";
344 return "Unknown error";
347 static void xlate_action_set(struct xlate_ctx *ctx);
348 static void xlate_commit_actions(struct xlate_ctx *ctx);
351 ctx_trigger_recirculation(struct xlate_ctx *ctx)
354 ctx->recirc_action_offset = ctx->action_set.size;
358 ctx_first_recirculation_action(const struct xlate_ctx *ctx)
360 return ctx->recirc_action_offset == ctx->action_set.size;
364 exit_recirculates(const struct xlate_ctx *ctx)
366 /* When recirculating the 'recirc_action_offset' has a non-negative value.
368 return ctx->recirc_action_offset >= 0;
372 ctx_cancel_recirculation(struct xlate_ctx *ctx)
374 if (exit_recirculates(ctx)) {
375 ctx->action_set.size = ctx->recirc_action_offset;
376 ctx->recirc_action_offset = -1;
377 ctx->last_unroll_offset = -1;
381 static void compose_recirculate_action(struct xlate_ctx *ctx);
383 /* A controller may use OFPP_NONE as the ingress port to indicate that
384 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
385 * when an input bundle is needed for validation (e.g., mirroring or
386 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
387 * any 'port' structs, so care must be taken when dealing with it. */
388 static struct xbundle ofpp_none_bundle = {
390 .vlan_mode = PORT_VLAN_TRUNK
393 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
394 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
395 * traffic egressing the 'ofport' with that priority should be marked with. */
396 struct skb_priority_to_dscp {
397 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
398 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
400 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
416 /* xlate_cache entries hold enough information to perform the side effects of
417 * xlate_actions() for a rule, without needing to perform rule translation
418 * from scratch. The primary usage of these is to submit statistics to objects
419 * that a flow relates to, although they may be used for other effects as well
420 * (for instance, refreshing hard timeouts for learned flows). */
424 struct rule_dpif *rule;
431 struct netflow *netflow;
436 struct mbridge *mbridge;
437 mirror_mask_t mirrors;
445 struct ofproto_dpif *ofproto;
446 struct ofputil_flow_mod *fm;
447 struct ofpbuf *ofpacts;
450 struct ofproto_dpif *ofproto;
455 struct rule_dpif *rule;
460 struct group_dpif *group;
461 struct ofputil_bucket *bucket;
464 char br_name[IFNAMSIZ];
465 struct in6_addr d_ipv6;
470 #define XC_ENTRY_FOR_EACH(ENTRY, ENTRIES, XCACHE) \
471 ENTRIES = XCACHE->entries; \
472 for (ENTRY = ofpbuf_try_pull(&ENTRIES, sizeof *ENTRY); \
474 ENTRY = ofpbuf_try_pull(&ENTRIES, sizeof *ENTRY))
477 struct ofpbuf entries;
480 /* Xlate config contains hash maps of all bridges, bundles and ports.
481 * Xcfgp contains the pointer to the current xlate configuration.
482 * When the main thread needs to change the configuration, it copies xcfgp to
483 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
484 * does not block handler and revalidator threads. */
486 struct hmap xbridges;
487 struct hmap xbundles;
490 static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
491 static struct xlate_cfg *new_xcfg = NULL;
493 static bool may_receive(const struct xport *, struct xlate_ctx *);
494 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
496 static void xlate_normal(struct xlate_ctx *);
497 static inline void xlate_report(struct xlate_ctx *, const char *, ...)
498 OVS_PRINTF_FORMAT(2, 3);
499 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
500 uint8_t table_id, bool may_packet_in,
501 bool honor_table_miss);
502 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
503 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
504 static void output_normal(struct xlate_ctx *, const struct xbundle *,
507 /* Optional bond recirculation parameter to compose_output_action(). */
508 struct xlate_bond_recirc {
509 uint32_t recirc_id; /* !0 Use recirculation instead of output. */
510 uint8_t hash_alg; /* !0 Compute hash for recirc before. */
511 uint32_t hash_basis; /* Compute hash for recirc before. */
514 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port,
515 const struct xlate_bond_recirc *xr);
517 static struct xbridge *xbridge_lookup(struct xlate_cfg *,
518 const struct ofproto_dpif *);
519 static struct xbridge *xbridge_lookup_by_uuid(struct xlate_cfg *,
520 const struct uuid *);
521 static struct xbundle *xbundle_lookup(struct xlate_cfg *,
522 const struct ofbundle *);
523 static struct xport *xport_lookup(struct xlate_cfg *,
524 const struct ofport_dpif *);
525 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
526 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
527 uint32_t skb_priority);
528 static void clear_skb_priorities(struct xport *);
529 static size_t count_skb_priorities(const struct xport *);
530 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
533 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
535 static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
536 static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
537 static void xlate_xport_init(struct xlate_cfg *, struct xport *);
538 static void xlate_xbridge_set(struct xbridge *, struct dpif *,
539 const struct mac_learning *, struct stp *,
540 struct rstp *, const struct mcast_snooping *,
541 const struct mbridge *,
542 const struct dpif_sflow *,
543 const struct dpif_ipfix *,
544 const struct netflow *,
545 bool forward_bpdu, bool has_in_band,
546 const struct dpif_backer_support *);
547 static void xlate_xbundle_set(struct xbundle *xbundle,
548 enum port_vlan_mode vlan_mode, int vlan,
549 unsigned long *trunks, bool use_priority_tags,
550 const struct bond *bond, const struct lacp *lacp,
552 static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
553 const struct netdev *netdev, const struct cfm *cfm,
554 const struct bfd *bfd, const struct lldp *lldp,
555 int stp_port_no, const struct rstp_port *rstp_port,
556 enum ofputil_port_config config,
557 enum ofputil_port_state state, bool is_tunnel,
559 static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
560 static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
561 static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
562 static void xlate_xbridge_copy(struct xbridge *);
563 static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
564 static void xlate_xport_copy(struct xbridge *, struct xbundle *,
566 static void xlate_xcfg_free(struct xlate_cfg *);
569 xlate_report(struct xlate_ctx *ctx, const char *format, ...)
571 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
574 va_start(args, format);
575 ctx->xin->report_hook(ctx->xin, ctx->recurse, format, args);
580 static struct vlog_rate_limit error_report_rl = VLOG_RATE_LIMIT_INIT(1, 5);
582 #define XLATE_REPORT_ERROR(CTX, ...) \
584 if (OVS_UNLIKELY((CTX)->xin->report_hook)) { \
585 xlate_report(CTX, __VA_ARGS__); \
587 VLOG_ERR_RL(&error_report_rl, __VA_ARGS__); \
592 xlate_report_actions(struct xlate_ctx *ctx, const char *title,
593 const struct ofpact *ofpacts, size_t ofpacts_len)
595 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
596 struct ds s = DS_EMPTY_INITIALIZER;
597 ofpacts_format(ofpacts, ofpacts_len, &s);
598 xlate_report(ctx, "%s: %s", title, ds_cstr(&s));
604 xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
606 list_init(&xbridge->xbundles);
607 hmap_init(&xbridge->xports);
608 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
609 hash_pointer(xbridge->ofproto, 0));
613 xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
615 list_init(&xbundle->xports);
616 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
617 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
618 hash_pointer(xbundle->ofbundle, 0));
622 xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
624 hmap_init(&xport->skb_priorities);
625 hmap_insert(&xcfg->xports, &xport->hmap_node,
626 hash_pointer(xport->ofport, 0));
627 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
628 hash_ofp_port(xport->ofp_port));
632 xlate_xbridge_set(struct xbridge *xbridge,
634 const struct mac_learning *ml, struct stp *stp,
635 struct rstp *rstp, const struct mcast_snooping *ms,
636 const struct mbridge *mbridge,
637 const struct dpif_sflow *sflow,
638 const struct dpif_ipfix *ipfix,
639 const struct netflow *netflow,
640 bool forward_bpdu, bool has_in_band,
641 const struct dpif_backer_support *support)
643 if (xbridge->ml != ml) {
644 mac_learning_unref(xbridge->ml);
645 xbridge->ml = mac_learning_ref(ml);
648 if (xbridge->ms != ms) {
649 mcast_snooping_unref(xbridge->ms);
650 xbridge->ms = mcast_snooping_ref(ms);
653 if (xbridge->mbridge != mbridge) {
654 mbridge_unref(xbridge->mbridge);
655 xbridge->mbridge = mbridge_ref(mbridge);
658 if (xbridge->sflow != sflow) {
659 dpif_sflow_unref(xbridge->sflow);
660 xbridge->sflow = dpif_sflow_ref(sflow);
663 if (xbridge->ipfix != ipfix) {
664 dpif_ipfix_unref(xbridge->ipfix);
665 xbridge->ipfix = dpif_ipfix_ref(ipfix);
668 if (xbridge->stp != stp) {
669 stp_unref(xbridge->stp);
670 xbridge->stp = stp_ref(stp);
673 if (xbridge->rstp != rstp) {
674 rstp_unref(xbridge->rstp);
675 xbridge->rstp = rstp_ref(rstp);
678 if (xbridge->netflow != netflow) {
679 netflow_unref(xbridge->netflow);
680 xbridge->netflow = netflow_ref(netflow);
683 xbridge->dpif = dpif;
684 xbridge->forward_bpdu = forward_bpdu;
685 xbridge->has_in_band = has_in_band;
686 xbridge->support = *support;
690 xlate_xbundle_set(struct xbundle *xbundle,
691 enum port_vlan_mode vlan_mode, int vlan,
692 unsigned long *trunks, bool use_priority_tags,
693 const struct bond *bond, const struct lacp *lacp,
696 ovs_assert(xbundle->xbridge);
698 xbundle->vlan_mode = vlan_mode;
699 xbundle->vlan = vlan;
700 xbundle->trunks = trunks;
701 xbundle->use_priority_tags = use_priority_tags;
702 xbundle->floodable = floodable;
704 if (xbundle->bond != bond) {
705 bond_unref(xbundle->bond);
706 xbundle->bond = bond_ref(bond);
709 if (xbundle->lacp != lacp) {
710 lacp_unref(xbundle->lacp);
711 xbundle->lacp = lacp_ref(lacp);
716 xlate_xport_set(struct xport *xport, odp_port_t odp_port,
717 const struct netdev *netdev, const struct cfm *cfm,
718 const struct bfd *bfd, const struct lldp *lldp, int stp_port_no,
719 const struct rstp_port* rstp_port,
720 enum ofputil_port_config config, enum ofputil_port_state state,
721 bool is_tunnel, bool may_enable)
723 xport->config = config;
724 xport->state = state;
725 xport->stp_port_no = stp_port_no;
726 xport->is_tunnel = is_tunnel;
727 xport->may_enable = may_enable;
728 xport->odp_port = odp_port;
730 if (xport->rstp_port != rstp_port) {
731 rstp_port_unref(xport->rstp_port);
732 xport->rstp_port = rstp_port_ref(rstp_port);
735 if (xport->cfm != cfm) {
736 cfm_unref(xport->cfm);
737 xport->cfm = cfm_ref(cfm);
740 if (xport->bfd != bfd) {
741 bfd_unref(xport->bfd);
742 xport->bfd = bfd_ref(bfd);
745 if (xport->lldp != lldp) {
746 lldp_unref(xport->lldp);
747 xport->lldp = lldp_ref(lldp);
750 if (xport->netdev != netdev) {
751 netdev_close(xport->netdev);
752 xport->netdev = netdev_ref(netdev);
757 xlate_xbridge_copy(struct xbridge *xbridge)
759 struct xbundle *xbundle;
761 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
762 new_xbridge->ofproto = xbridge->ofproto;
763 new_xbridge->name = xstrdup(xbridge->name);
764 xlate_xbridge_init(new_xcfg, new_xbridge);
766 xlate_xbridge_set(new_xbridge,
767 xbridge->dpif, xbridge->ml, xbridge->stp,
768 xbridge->rstp, xbridge->ms, xbridge->mbridge,
769 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
770 xbridge->forward_bpdu, xbridge->has_in_band,
772 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
773 xlate_xbundle_copy(new_xbridge, xbundle);
776 /* Copy xports which are not part of a xbundle */
777 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
778 if (!xport->xbundle) {
779 xlate_xport_copy(new_xbridge, NULL, xport);
785 xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
788 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
789 new_xbundle->ofbundle = xbundle->ofbundle;
790 new_xbundle->xbridge = xbridge;
791 new_xbundle->name = xstrdup(xbundle->name);
792 xlate_xbundle_init(new_xcfg, new_xbundle);
794 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
795 xbundle->vlan, xbundle->trunks,
796 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
798 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
799 xlate_xport_copy(xbridge, new_xbundle, xport);
804 xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
807 struct skb_priority_to_dscp *pdscp, *new_pdscp;
808 struct xport *new_xport = xzalloc(sizeof *xport);
809 new_xport->ofport = xport->ofport;
810 new_xport->ofp_port = xport->ofp_port;
811 new_xport->xbridge = xbridge;
812 xlate_xport_init(new_xcfg, new_xport);
814 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
815 xport->bfd, xport->lldp, xport->stp_port_no,
816 xport->rstp_port, xport->config, xport->state,
817 xport->is_tunnel, xport->may_enable);
820 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
822 new_xport->peer = peer;
823 new_xport->peer->peer = new_xport;
828 new_xport->xbundle = xbundle;
829 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
832 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
833 new_pdscp = xmalloc(sizeof *pdscp);
834 new_pdscp->skb_priority = pdscp->skb_priority;
835 new_pdscp->dscp = pdscp->dscp;
836 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
837 hash_int(new_pdscp->skb_priority, 0));
841 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
842 * configuration in xcfgp.
844 * This needs to be called after editing the xlate configuration.
846 * Functions that edit the new xlate configuration are
847 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
853 * edit_xlate_configuration();
855 * xlate_txn_commit(); */
857 xlate_txn_commit(void)
859 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
861 ovsrcu_set(&xcfgp, new_xcfg);
862 ovsrcu_synchronize();
863 xlate_xcfg_free(xcfg);
867 /* Copies the current xlate configuration in xcfgp to new_xcfg.
869 * This needs to be called prior to editing the xlate configuration. */
871 xlate_txn_start(void)
873 struct xbridge *xbridge;
874 struct xlate_cfg *xcfg;
876 ovs_assert(!new_xcfg);
878 new_xcfg = xmalloc(sizeof *new_xcfg);
879 hmap_init(&new_xcfg->xbridges);
880 hmap_init(&new_xcfg->xbundles);
881 hmap_init(&new_xcfg->xports);
883 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
888 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
889 xlate_xbridge_copy(xbridge);
895 xlate_xcfg_free(struct xlate_cfg *xcfg)
897 struct xbridge *xbridge, *next_xbridge;
903 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
904 xlate_xbridge_remove(xcfg, xbridge);
907 hmap_destroy(&xcfg->xbridges);
908 hmap_destroy(&xcfg->xbundles);
909 hmap_destroy(&xcfg->xports);
914 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
916 const struct mac_learning *ml, struct stp *stp,
917 struct rstp *rstp, const struct mcast_snooping *ms,
918 const struct mbridge *mbridge,
919 const struct dpif_sflow *sflow,
920 const struct dpif_ipfix *ipfix,
921 const struct netflow *netflow,
922 bool forward_bpdu, bool has_in_band,
923 const struct dpif_backer_support *support)
925 struct xbridge *xbridge;
927 ovs_assert(new_xcfg);
929 xbridge = xbridge_lookup(new_xcfg, ofproto);
931 xbridge = xzalloc(sizeof *xbridge);
932 xbridge->ofproto = ofproto;
934 xlate_xbridge_init(new_xcfg, xbridge);
938 xbridge->name = xstrdup(name);
940 xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
941 netflow, forward_bpdu, has_in_band, support);
945 xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
947 struct xbundle *xbundle, *next_xbundle;
948 struct xport *xport, *next_xport;
954 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
955 xlate_xport_remove(xcfg, xport);
958 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
959 xlate_xbundle_remove(xcfg, xbundle);
962 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
963 mac_learning_unref(xbridge->ml);
964 mcast_snooping_unref(xbridge->ms);
965 mbridge_unref(xbridge->mbridge);
966 dpif_sflow_unref(xbridge->sflow);
967 dpif_ipfix_unref(xbridge->ipfix);
968 stp_unref(xbridge->stp);
969 rstp_unref(xbridge->rstp);
970 hmap_destroy(&xbridge->xports);
976 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
978 struct xbridge *xbridge;
980 ovs_assert(new_xcfg);
982 xbridge = xbridge_lookup(new_xcfg, ofproto);
983 xlate_xbridge_remove(new_xcfg, xbridge);
987 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
988 const char *name, enum port_vlan_mode vlan_mode, int vlan,
989 unsigned long *trunks, bool use_priority_tags,
990 const struct bond *bond, const struct lacp *lacp,
993 struct xbundle *xbundle;
995 ovs_assert(new_xcfg);
997 xbundle = xbundle_lookup(new_xcfg, ofbundle);
999 xbundle = xzalloc(sizeof *xbundle);
1000 xbundle->ofbundle = ofbundle;
1001 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
1003 xlate_xbundle_init(new_xcfg, xbundle);
1006 free(xbundle->name);
1007 xbundle->name = xstrdup(name);
1009 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
1010 use_priority_tags, bond, lacp, floodable);
1014 xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
1016 struct xport *xport;
1022 LIST_FOR_EACH_POP (xport, bundle_node, &xbundle->xports) {
1023 xport->xbundle = NULL;
1026 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
1027 list_remove(&xbundle->list_node);
1028 bond_unref(xbundle->bond);
1029 lacp_unref(xbundle->lacp);
1030 free(xbundle->name);
1035 xlate_bundle_remove(struct ofbundle *ofbundle)
1037 struct xbundle *xbundle;
1039 ovs_assert(new_xcfg);
1041 xbundle = xbundle_lookup(new_xcfg, ofbundle);
1042 xlate_xbundle_remove(new_xcfg, xbundle);
1046 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
1047 struct ofport_dpif *ofport, ofp_port_t ofp_port,
1048 odp_port_t odp_port, const struct netdev *netdev,
1049 const struct cfm *cfm, const struct bfd *bfd,
1050 const struct lldp *lldp, struct ofport_dpif *peer,
1051 int stp_port_no, const struct rstp_port *rstp_port,
1052 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
1053 enum ofputil_port_config config,
1054 enum ofputil_port_state state, bool is_tunnel,
1058 struct xport *xport;
1060 ovs_assert(new_xcfg);
1062 xport = xport_lookup(new_xcfg, ofport);
1064 xport = xzalloc(sizeof *xport);
1065 xport->ofport = ofport;
1066 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
1067 xport->ofp_port = ofp_port;
1069 xlate_xport_init(new_xcfg, xport);
1072 ovs_assert(xport->ofp_port == ofp_port);
1074 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, lldp,
1075 stp_port_no, rstp_port, config, state, is_tunnel,
1079 xport->peer->peer = NULL;
1081 xport->peer = xport_lookup(new_xcfg, peer);
1083 xport->peer->peer = xport;
1086 if (xport->xbundle) {
1087 list_remove(&xport->bundle_node);
1089 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
1090 if (xport->xbundle) {
1091 list_insert(&xport->xbundle->xports, &xport->bundle_node);
1094 clear_skb_priorities(xport);
1095 for (i = 0; i < n_qdscp; i++) {
1096 struct skb_priority_to_dscp *pdscp;
1097 uint32_t skb_priority;
1099 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
1104 pdscp = xmalloc(sizeof *pdscp);
1105 pdscp->skb_priority = skb_priority;
1106 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1107 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
1108 hash_int(pdscp->skb_priority, 0));
1113 xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
1120 xport->peer->peer = NULL;
1124 if (xport->xbundle) {
1125 list_remove(&xport->bundle_node);
1128 clear_skb_priorities(xport);
1129 hmap_destroy(&xport->skb_priorities);
1131 hmap_remove(&xcfg->xports, &xport->hmap_node);
1132 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
1134 netdev_close(xport->netdev);
1135 rstp_port_unref(xport->rstp_port);
1136 cfm_unref(xport->cfm);
1137 bfd_unref(xport->bfd);
1138 lldp_unref(xport->lldp);
1143 xlate_ofport_remove(struct ofport_dpif *ofport)
1145 struct xport *xport;
1147 ovs_assert(new_xcfg);
1149 xport = xport_lookup(new_xcfg, ofport);
1150 xlate_xport_remove(new_xcfg, xport);
1153 static struct ofproto_dpif *
1154 xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
1155 ofp_port_t *ofp_in_port, const struct xport **xportp)
1157 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1158 const struct xport *xport;
1160 xport = xport_lookup(xcfg, tnl_port_should_receive(flow)
1161 ? tnl_port_receive(flow)
1162 : odp_port_to_ofport(backer, flow->in_port.odp_port));
1163 if (OVS_UNLIKELY(!xport)) {
1168 *ofp_in_port = xport->ofp_port;
1170 return xport->xbridge->ofproto;
1173 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1174 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1175 struct ofproto_dpif *
1176 xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1177 ofp_port_t *ofp_in_port)
1179 const struct xport *xport;
1181 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1184 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1185 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
1186 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1187 * handles for those protocols if they're enabled. Caller may use the returned
1188 * pointers until quiescing, for longer term use additional references must
1191 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1194 xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1195 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1196 struct dpif_sflow **sflow, struct netflow **netflow,
1197 ofp_port_t *ofp_in_port)
1199 struct ofproto_dpif *ofproto;
1200 const struct xport *xport;
1202 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1209 *ofprotop = ofproto;
1213 *ipfix = xport ? xport->xbridge->ipfix : NULL;
1217 *sflow = xport ? xport->xbridge->sflow : NULL;
1221 *netflow = xport ? xport->xbridge->netflow : NULL;
1227 static struct xbridge *
1228 xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
1230 struct hmap *xbridges;
1231 struct xbridge *xbridge;
1233 if (!ofproto || !xcfg) {
1237 xbridges = &xcfg->xbridges;
1239 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
1241 if (xbridge->ofproto == ofproto) {
1248 static struct xbridge *
1249 xbridge_lookup_by_uuid(struct xlate_cfg *xcfg, const struct uuid *uuid)
1251 struct xbridge *xbridge;
1253 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
1254 if (uuid_equals(ofproto_dpif_get_uuid(xbridge->ofproto), uuid)) {
1261 static struct xbundle *
1262 xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
1264 struct hmap *xbundles;
1265 struct xbundle *xbundle;
1267 if (!ofbundle || !xcfg) {
1271 xbundles = &xcfg->xbundles;
1273 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
1275 if (xbundle->ofbundle == ofbundle) {
1282 static struct xport *
1283 xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
1285 struct hmap *xports;
1286 struct xport *xport;
1288 if (!ofport || !xcfg) {
1292 xports = &xcfg->xports;
1294 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
1296 if (xport->ofport == ofport) {
1303 static struct stp_port *
1304 xport_get_stp_port(const struct xport *xport)
1306 return xport->xbridge->stp && xport->stp_port_no != -1
1307 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1312 xport_stp_learn_state(const struct xport *xport)
1314 struct stp_port *sp = xport_get_stp_port(xport);
1316 ? stp_learn_in_state(stp_port_get_state(sp))
1321 xport_stp_forward_state(const struct xport *xport)
1323 struct stp_port *sp = xport_get_stp_port(xport);
1325 ? stp_forward_in_state(stp_port_get_state(sp))
1330 xport_stp_should_forward_bpdu(const struct xport *xport)
1332 struct stp_port *sp = xport_get_stp_port(xport);
1333 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
1336 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1337 * were used to make the determination.*/
1339 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1341 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1342 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1343 return is_stp(flow);
1347 stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1349 struct stp_port *sp = xport_get_stp_port(xport);
1350 struct dp_packet payload = *packet;
1351 struct eth_header *eth = dp_packet_data(&payload);
1353 /* Sink packets on ports that have STP disabled when the bridge has
1355 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1359 /* Trim off padding on payload. */
1360 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1361 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1364 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1365 stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
1369 static enum rstp_state
1370 xport_get_rstp_port_state(const struct xport *xport)
1372 return xport->rstp_port
1373 ? rstp_port_get_state(xport->rstp_port)
1378 xport_rstp_learn_state(const struct xport *xport)
1380 return xport->xbridge->rstp && xport->rstp_port
1381 ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1386 xport_rstp_forward_state(const struct xport *xport)
1388 return xport->xbridge->rstp && xport->rstp_port
1389 ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1394 xport_rstp_should_manage_bpdu(const struct xport *xport)
1396 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
1400 rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1402 struct dp_packet payload = *packet;
1403 struct eth_header *eth = dp_packet_data(&payload);
1405 /* Sink packets on ports that have no RSTP. */
1406 if (!xport->rstp_port) {
1410 /* Trim off padding on payload. */
1411 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1412 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1415 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1416 rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
1417 dp_packet_size(&payload));
1421 static struct xport *
1422 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1424 struct xport *xport;
1426 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1428 if (xport->ofp_port == ofp_port) {
1436 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1438 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1439 return xport ? xport->odp_port : ODPP_NONE;
1443 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1445 struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1446 return xport && xport->may_enable;
1449 static struct ofputil_bucket *
1450 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1454 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1456 struct group_dpif *group;
1458 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1459 struct ofputil_bucket *bucket;
1461 bucket = group_first_live_bucket(ctx, group, depth);
1462 group_dpif_unref(group);
1463 return bucket == NULL;
1469 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1472 bucket_is_alive(const struct xlate_ctx *ctx,
1473 struct ofputil_bucket *bucket, int depth)
1475 if (depth >= MAX_LIVENESS_RECURSION) {
1476 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1478 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1479 MAX_LIVENESS_RECURSION);
1483 return (!ofputil_bucket_has_liveness(bucket)
1484 || (bucket->watch_port != OFPP_ANY
1485 && odp_port_is_alive(ctx, bucket->watch_port))
1486 || (bucket->watch_group != OFPG_ANY
1487 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
1490 static struct ofputil_bucket *
1491 group_first_live_bucket(const struct xlate_ctx *ctx,
1492 const struct group_dpif *group, int depth)
1494 struct ofputil_bucket *bucket;
1495 const struct ovs_list *buckets;
1497 group_dpif_get_buckets(group, &buckets);
1498 LIST_FOR_EACH (bucket, list_node, buckets) {
1499 if (bucket_is_alive(ctx, bucket, depth)) {
1507 static struct ofputil_bucket *
1508 group_best_live_bucket(const struct xlate_ctx *ctx,
1509 const struct group_dpif *group,
1512 struct ofputil_bucket *best_bucket = NULL;
1513 uint32_t best_score = 0;
1516 struct ofputil_bucket *bucket;
1517 const struct ovs_list *buckets;
1519 group_dpif_get_buckets(group, &buckets);
1520 LIST_FOR_EACH (bucket, list_node, buckets) {
1521 if (bucket_is_alive(ctx, bucket, 0)) {
1522 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
1523 if (score >= best_score) {
1524 best_bucket = bucket;
1535 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
1537 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1538 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1542 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1544 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1547 static mirror_mask_t
1548 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1550 return xbundle != &ofpp_none_bundle
1551 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1555 static mirror_mask_t
1556 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
1558 return xbundle != &ofpp_none_bundle
1559 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1563 static mirror_mask_t
1564 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
1566 return xbundle != &ofpp_none_bundle
1567 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1571 static struct xbundle *
1572 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1573 bool warn, struct xport **in_xportp)
1575 struct xport *xport;
1577 /* Find the port and bundle for the received packet. */
1578 xport = get_ofp_port(xbridge, in_port);
1582 if (xport && xport->xbundle) {
1583 return xport->xbundle;
1586 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1587 * which a controller may use as the ingress port for traffic that
1588 * it is sourcing. */
1589 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
1590 return &ofpp_none_bundle;
1593 /* Odd. A few possible reasons here:
1595 * - We deleted a port but there are still a few packets queued up
1598 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1599 * we don't know about.
1601 * - The ofproto client didn't configure the port as part of a bundle.
1602 * This is particularly likely to happen if a packet was received on the
1603 * port after it was created, but before the client had a chance to
1604 * configure its bundle.
1607 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1609 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1610 "port %"PRIu16, xbridge->name, in_port);
1616 mirror_packet(struct xlate_ctx *ctx, struct xbundle *xbundle,
1617 mirror_mask_t mirrors)
1619 bool warn = ctx->xin->packet != NULL;
1620 uint16_t vid = vlan_tci_to_vid(ctx->xin->flow.vlan_tci);
1621 if (!input_vid_is_valid(vid, xbundle, warn)) {
1624 uint16_t vlan = input_vid_to_vlan(xbundle, vid);
1626 const struct xbridge *xbridge = ctx->xbridge;
1628 /* Don't mirror to destinations that we've already mirrored to. */
1629 mirrors &= ~ctx->mirrors;
1634 /* Record these mirrors so that we don't mirror to them again. */
1635 ctx->mirrors |= mirrors;
1637 if (ctx->xin->resubmit_stats) {
1638 mirror_update_stats(xbridge->mbridge, mirrors,
1639 ctx->xin->resubmit_stats->n_packets,
1640 ctx->xin->resubmit_stats->n_bytes);
1642 if (ctx->xin->xcache) {
1643 struct xc_entry *entry;
1645 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_MIRROR);
1646 entry->u.mirror.mbridge = mbridge_ref(xbridge->mbridge);
1647 entry->u.mirror.mirrors = mirrors;
1651 const unsigned long *vlans;
1652 mirror_mask_t dup_mirrors;
1653 struct ofbundle *out;
1656 bool has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1657 &vlans, &dup_mirrors, &out, &out_vlan);
1658 ovs_assert(has_mirror);
1661 ctx->wc->masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1664 if (vlans && !bitmap_is_set(vlans, vlan)) {
1665 mirrors = zero_rightmost_1bit(mirrors);
1669 mirrors &= ~dup_mirrors;
1670 ctx->mirrors |= dup_mirrors;
1672 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1673 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1675 output_normal(ctx, out_xbundle, vlan);
1677 } else if (vlan != out_vlan
1678 && !eth_addr_is_reserved(ctx->xin->flow.dl_dst)) {
1679 struct xbundle *xbundle;
1681 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1682 if (xbundle_includes_vlan(xbundle, out_vlan)
1683 && !xbundle_mirror_out(xbridge, xbundle)) {
1684 output_normal(ctx, xbundle, out_vlan);
1692 mirror_ingress_packet(struct xlate_ctx *ctx)
1694 if (mbridge_has_mirrors(ctx->xbridge->mbridge)) {
1695 bool warn = ctx->xin->packet != NULL;
1696 struct xbundle *xbundle = lookup_input_bundle(
1697 ctx->xbridge, ctx->xin->flow.in_port.ofp_port, warn, NULL);
1699 mirror_packet(ctx, xbundle,
1700 xbundle_mirror_src(ctx->xbridge, xbundle));
1705 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1706 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1707 * the bundle on which the packet was received, returns the VLAN to which the
1710 * Both 'vid' and the return value are in the range 0...4095. */
1712 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1714 switch (in_xbundle->vlan_mode) {
1715 case PORT_VLAN_ACCESS:
1716 return in_xbundle->vlan;
1719 case PORT_VLAN_TRUNK:
1722 case PORT_VLAN_NATIVE_UNTAGGED:
1723 case PORT_VLAN_NATIVE_TAGGED:
1724 return vid ? vid : in_xbundle->vlan;
1731 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1732 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1735 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1736 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1739 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1741 /* Allow any VID on the OFPP_NONE port. */
1742 if (in_xbundle == &ofpp_none_bundle) {
1746 switch (in_xbundle->vlan_mode) {
1747 case PORT_VLAN_ACCESS:
1750 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1751 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1752 "packet received on port %s configured as VLAN "
1753 "%"PRIu16" access port", vid, in_xbundle->name,
1760 case PORT_VLAN_NATIVE_UNTAGGED:
1761 case PORT_VLAN_NATIVE_TAGGED:
1763 /* Port must always carry its native VLAN. */
1767 case PORT_VLAN_TRUNK:
1768 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1770 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1771 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1772 "received on port %s not configured for trunking "
1773 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1785 /* Given 'vlan', the VLAN that a packet belongs to, and
1786 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1787 * that should be included in the 802.1Q header. (If the return value is 0,
1788 * then the 802.1Q header should only be included in the packet if there is a
1791 * Both 'vlan' and the return value are in the range 0...4095. */
1793 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1795 switch (out_xbundle->vlan_mode) {
1796 case PORT_VLAN_ACCESS:
1799 case PORT_VLAN_TRUNK:
1800 case PORT_VLAN_NATIVE_TAGGED:
1803 case PORT_VLAN_NATIVE_UNTAGGED:
1804 return vlan == out_xbundle->vlan ? 0 : vlan;
1812 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1815 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1817 ovs_be16 tci, old_tci;
1818 struct xport *xport;
1819 struct xlate_bond_recirc xr;
1820 bool use_recirc = false;
1822 vid = output_vlan_to_vid(out_xbundle, vlan);
1823 if (list_is_empty(&out_xbundle->xports)) {
1824 /* Partially configured bundle with no slaves. Drop the packet. */
1826 } else if (!out_xbundle->bond) {
1827 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1830 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1831 struct flow_wildcards *wc = ctx->wc;
1832 struct ofport_dpif *ofport;
1834 if (ctx->xbridge->support.odp.recirc) {
1835 use_recirc = bond_may_recirc(
1836 out_xbundle->bond, &xr.recirc_id, &xr.hash_basis);
1839 /* Only TCP mode uses recirculation. */
1840 xr.hash_alg = OVS_HASH_ALG_L4;
1841 bond_update_post_recirc_rules(out_xbundle->bond, false);
1843 /* Recirculation does not require unmasking hash fields. */
1848 ofport = bond_choose_output_slave(out_xbundle->bond,
1849 &ctx->xin->flow, wc, vid);
1850 xport = xport_lookup(xcfg, ofport);
1853 /* No slaves enabled, so drop packet. */
1857 /* If use_recirc is set, the main thread will handle stats
1858 * accounting for this bond. */
1860 if (ctx->xin->resubmit_stats) {
1861 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1862 ctx->xin->resubmit_stats->n_bytes);
1864 if (ctx->xin->xcache) {
1865 struct xc_entry *entry;
1868 flow = &ctx->xin->flow;
1869 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1870 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1871 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1872 entry->u.bond.vid = vid;
1877 old_tci = *flow_tci;
1879 if (tci || out_xbundle->use_priority_tags) {
1880 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1882 tci |= htons(VLAN_CFI);
1887 compose_output_action(ctx, xport->ofp_port, use_recirc ? &xr : NULL);
1888 *flow_tci = old_tci;
1891 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1892 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1893 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1895 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1897 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1901 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1902 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1906 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1907 if (flow->nw_proto == ARP_OP_REPLY) {
1909 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1910 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1911 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1913 return flow->nw_src == flow->nw_dst;
1919 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1920 * dropped. Returns true if they may be forwarded, false if they should be
1923 * 'in_port' must be the xport that corresponds to flow->in_port.
1924 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1926 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1927 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1928 * checked by input_vid_is_valid().
1930 * May also add tags to '*tags', although the current implementation only does
1931 * so in one special case.
1934 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1937 struct xbundle *in_xbundle = in_port->xbundle;
1938 const struct xbridge *xbridge = ctx->xbridge;
1939 struct flow *flow = &ctx->xin->flow;
1941 /* Drop frames for reserved multicast addresses
1942 * only if forward_bpdu option is absent. */
1943 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1944 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1948 if (in_xbundle->bond) {
1949 struct mac_entry *mac;
1951 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1957 xlate_report(ctx, "bonding refused admissibility, dropping");
1960 case BV_DROP_IF_MOVED:
1961 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1962 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1964 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
1965 && (!is_gratuitous_arp(flow, ctx->wc)
1966 || mac_entry_is_grat_arp_locked(mac))) {
1967 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1968 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1972 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1980 /* Checks whether a MAC learning update is necessary for MAC learning table
1981 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1984 * Most packets processed through the MAC learning table do not actually
1985 * change it in any way. This function requires only a read lock on the MAC
1986 * learning table, so it is much cheaper in this common case.
1988 * Keep the code here synchronized with that in update_learning_table__()
1991 is_mac_learning_update_needed(const struct mac_learning *ml,
1992 const struct flow *flow,
1993 struct flow_wildcards *wc,
1994 int vlan, struct xbundle *in_xbundle)
1995 OVS_REQ_RDLOCK(ml->rwlock)
1997 struct mac_entry *mac;
1999 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
2003 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
2004 if (!mac || mac_entry_age(ml, mac)) {
2008 if (is_gratuitous_arp(flow, wc)) {
2009 /* We don't want to learn from gratuitous ARP packets that are
2010 * reflected back over bond slaves so we lock the learning table. */
2011 if (!in_xbundle->bond) {
2013 } else if (mac_entry_is_grat_arp_locked(mac)) {
2018 return mac_entry_get_port(ml, mac) != in_xbundle->ofbundle;
2022 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
2023 * received on 'in_xbundle' in 'vlan'.
2025 * This code repeats all the checks in is_mac_learning_update_needed() because
2026 * the lock was released between there and here and thus the MAC learning state
2027 * could have changed.
2029 * Keep the code here synchronized with that in is_mac_learning_update_needed()
2032 update_learning_table__(const struct xbridge *xbridge,
2033 const struct flow *flow, struct flow_wildcards *wc,
2034 int vlan, struct xbundle *in_xbundle)
2035 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
2037 struct mac_entry *mac;
2039 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
2043 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
2044 if (is_gratuitous_arp(flow, wc)) {
2045 /* We don't want to learn from gratuitous ARP packets that are
2046 * reflected back over bond slaves so we lock the learning table. */
2047 if (!in_xbundle->bond) {
2048 mac_entry_set_grat_arp_lock(mac);
2049 } else if (mac_entry_is_grat_arp_locked(mac)) {
2054 if (mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle) {
2055 /* The log messages here could actually be useful in debugging,
2056 * so keep the rate limit relatively high. */
2057 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
2059 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
2060 "on port %s in VLAN %d",
2061 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
2062 in_xbundle->name, vlan);
2064 mac_entry_set_port(xbridge->ml, mac, in_xbundle->ofbundle);
2069 update_learning_table(const struct xbridge *xbridge,
2070 const struct flow *flow, struct flow_wildcards *wc,
2071 int vlan, struct xbundle *in_xbundle)
2075 /* Don't learn the OFPP_NONE port. */
2076 if (in_xbundle == &ofpp_none_bundle) {
2080 /* First try the common case: no change to MAC learning table. */
2081 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
2082 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
2084 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2087 /* Slow path: MAC learning table might need an update. */
2088 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
2089 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
2090 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2094 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2095 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
2097 update_mcast_snooping_table4__(const struct xbridge *xbridge,
2098 const struct flow *flow,
2099 struct mcast_snooping *ms, int vlan,
2100 struct xbundle *in_xbundle,
2101 const struct dp_packet *packet)
2102 OVS_REQ_WRLOCK(ms->rwlock)
2104 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2106 ovs_be32 ip4 = flow->igmp_group_ip4;
2108 switch (ntohs(flow->tp_src)) {
2109 case IGMP_HOST_MEMBERSHIP_REPORT:
2110 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2111 if (mcast_snooping_add_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2112 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
2113 IP_FMT" is on port %s in VLAN %d",
2114 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2117 case IGMP_HOST_LEAVE_MESSAGE:
2118 if (mcast_snooping_leave_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2119 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
2120 IP_FMT" is on port %s in VLAN %d",
2121 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2124 case IGMP_HOST_MEMBERSHIP_QUERY:
2125 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
2126 in_xbundle->ofbundle)) {
2127 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
2128 IP_FMT" is on port %s in VLAN %d",
2129 xbridge->name, IP_ARGS(flow->nw_src),
2130 in_xbundle->name, vlan);
2133 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2134 if ((count = mcast_snooping_add_report(ms, packet, vlan,
2135 in_xbundle->ofbundle))) {
2136 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2137 "addresses on port %s in VLAN %d",
2138 xbridge->name, count, in_xbundle->name, vlan);
2145 update_mcast_snooping_table6__(const struct xbridge *xbridge,
2146 const struct flow *flow,
2147 struct mcast_snooping *ms, int vlan,
2148 struct xbundle *in_xbundle,
2149 const struct dp_packet *packet)
2150 OVS_REQ_WRLOCK(ms->rwlock)
2152 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2155 switch (ntohs(flow->tp_src)) {
2157 if (!ipv6_addr_equals(&flow->ipv6_src, &in6addr_any)
2158 && mcast_snooping_add_mrouter(ms, vlan, in_xbundle->ofbundle)) {
2159 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query on port %s"
2161 xbridge->name, in_xbundle->name, vlan);
2167 count = mcast_snooping_add_mld(ms, packet, vlan, in_xbundle->ofbundle);
2169 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2170 "addresses on port %s in VLAN %d",
2171 xbridge->name, count, in_xbundle->name, vlan);
2177 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2178 * was received on 'in_xbundle' in 'vlan'. */
2180 update_mcast_snooping_table(const struct xbridge *xbridge,
2181 const struct flow *flow, int vlan,
2182 struct xbundle *in_xbundle,
2183 const struct dp_packet *packet)
2185 struct mcast_snooping *ms = xbridge->ms;
2186 struct xlate_cfg *xcfg;
2187 struct xbundle *mcast_xbundle;
2188 struct mcast_port_bundle *fport;
2190 /* Don't learn the OFPP_NONE port. */
2191 if (in_xbundle == &ofpp_none_bundle) {
2195 /* Don't learn from flood ports */
2196 mcast_xbundle = NULL;
2197 ovs_rwlock_wrlock(&ms->rwlock);
2198 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2199 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2200 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2201 if (mcast_xbundle == in_xbundle) {
2206 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
2207 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2208 update_mcast_snooping_table4__(xbridge, flow, ms, vlan,
2209 in_xbundle, packet);
2211 update_mcast_snooping_table6__(xbridge, flow, ms, vlan,
2212 in_xbundle, packet);
2215 ovs_rwlock_unlock(&ms->rwlock);
2218 /* send the packet to ports having the multicast group learned */
2220 xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2221 struct mcast_snooping *ms OVS_UNUSED,
2222 struct mcast_group *grp,
2223 struct xbundle *in_xbundle, uint16_t vlan)
2224 OVS_REQ_RDLOCK(ms->rwlock)
2226 struct xlate_cfg *xcfg;
2227 struct mcast_group_bundle *b;
2228 struct xbundle *mcast_xbundle;
2230 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2231 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2232 mcast_xbundle = xbundle_lookup(xcfg, b->port);
2233 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2234 xlate_report(ctx, "forwarding to mcast group port");
2235 output_normal(ctx, mcast_xbundle, vlan);
2236 } else if (!mcast_xbundle) {
2237 xlate_report(ctx, "mcast group port is unknown, dropping");
2239 xlate_report(ctx, "mcast group port is input port, dropping");
2244 /* send the packet to ports connected to multicast routers */
2246 xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2247 struct mcast_snooping *ms,
2248 struct xbundle *in_xbundle, uint16_t vlan)
2249 OVS_REQ_RDLOCK(ms->rwlock)
2251 struct xlate_cfg *xcfg;
2252 struct mcast_mrouter_bundle *mrouter;
2253 struct xbundle *mcast_xbundle;
2255 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2256 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2257 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2258 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2259 xlate_report(ctx, "forwarding to mcast router port");
2260 output_normal(ctx, mcast_xbundle, vlan);
2261 } else if (!mcast_xbundle) {
2262 xlate_report(ctx, "mcast router port is unknown, dropping");
2264 xlate_report(ctx, "mcast router port is input port, dropping");
2269 /* send the packet to ports flagged to be flooded */
2271 xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2272 struct mcast_snooping *ms,
2273 struct xbundle *in_xbundle, uint16_t vlan)
2274 OVS_REQ_RDLOCK(ms->rwlock)
2276 struct xlate_cfg *xcfg;
2277 struct mcast_port_bundle *fport;
2278 struct xbundle *mcast_xbundle;
2280 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2281 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2282 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2283 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2284 xlate_report(ctx, "forwarding to mcast flood port");
2285 output_normal(ctx, mcast_xbundle, vlan);
2286 } else if (!mcast_xbundle) {
2287 xlate_report(ctx, "mcast flood port is unknown, dropping");
2289 xlate_report(ctx, "mcast flood port is input port, dropping");
2294 /* forward the Reports to configured ports */
2296 xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
2297 struct mcast_snooping *ms,
2298 struct xbundle *in_xbundle, uint16_t vlan)
2299 OVS_REQ_RDLOCK(ms->rwlock)
2301 struct xlate_cfg *xcfg;
2302 struct mcast_port_bundle *rport;
2303 struct xbundle *mcast_xbundle;
2305 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2306 LIST_FOR_EACH(rport, node, &ms->rport_list) {
2307 mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2308 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2309 xlate_report(ctx, "forwarding Report to mcast flagged port");
2310 output_normal(ctx, mcast_xbundle, vlan);
2311 } else if (!mcast_xbundle) {
2312 xlate_report(ctx, "mcast port is unknown, dropping the Report");
2314 xlate_report(ctx, "mcast port is input port, dropping the Report");
2320 xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2323 struct xbundle *xbundle;
2325 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2326 if (xbundle != in_xbundle
2327 && xbundle_includes_vlan(xbundle, vlan)
2328 && xbundle->floodable
2329 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2330 output_normal(ctx, xbundle, vlan);
2333 ctx->nf_output_iface = NF_OUT_FLOOD;
2337 xlate_normal(struct xlate_ctx *ctx)
2339 struct flow_wildcards *wc = ctx->wc;
2340 struct flow *flow = &ctx->xin->flow;
2341 struct xbundle *in_xbundle;
2342 struct xport *in_port;
2343 struct mac_entry *mac;
2348 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2349 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2350 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2352 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
2353 ctx->xin->packet != NULL, &in_port);
2355 xlate_report(ctx, "no input bundle, dropping");
2359 /* Drop malformed frames. */
2360 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
2361 !(flow->vlan_tci & htons(VLAN_CFI))) {
2362 if (ctx->xin->packet != NULL) {
2363 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2364 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
2365 "VLAN tag received on port %s",
2366 ctx->xbridge->name, in_xbundle->name);
2368 xlate_report(ctx, "partial VLAN tag, dropping");
2372 /* Drop frames on bundles reserved for mirroring. */
2373 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
2374 if (ctx->xin->packet != NULL) {
2375 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2376 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2377 "%s, which is reserved exclusively for mirroring",
2378 ctx->xbridge->name, in_xbundle->name);
2380 xlate_report(ctx, "input port is mirror output port, dropping");
2385 vid = vlan_tci_to_vid(flow->vlan_tci);
2386 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
2387 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2390 vlan = input_vid_to_vlan(in_xbundle, vid);
2392 /* Check other admissibility requirements. */
2393 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2397 /* Learn source MAC. */
2398 if (ctx->xin->may_learn) {
2399 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
2401 if (ctx->xin->xcache) {
2402 struct xc_entry *entry;
2404 /* Save enough info to update mac learning table later. */
2405 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
2406 entry->u.normal.ofproto = ctx->xbridge->ofproto;
2407 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2408 entry->u.normal.vlan = vlan;
2411 /* Determine output bundle. */
2412 if (mcast_snooping_enabled(ctx->xbridge->ms)
2413 && !eth_addr_is_broadcast(flow->dl_dst)
2414 && eth_addr_is_multicast(flow->dl_dst)
2415 && is_ip_any(flow)) {
2416 struct mcast_snooping *ms = ctx->xbridge->ms;
2417 struct mcast_group *grp = NULL;
2419 if (is_igmp(flow)) {
2420 if (mcast_snooping_is_membership(flow->tp_src) ||
2421 mcast_snooping_is_query(flow->tp_src)) {
2422 if (ctx->xin->may_learn) {
2423 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2424 in_xbundle, ctx->xin->packet);
2427 * IGMP packets need to take the slow path, in order to be
2428 * processed for mdb updates. That will prevent expires
2429 * firing off even after hosts have sent reports.
2431 ctx->xout->slow |= SLOW_ACTION;
2434 if (mcast_snooping_is_membership(flow->tp_src)) {
2435 ovs_rwlock_rdlock(&ms->rwlock);
2436 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2437 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2438 * forward IGMP Membership Reports only to those ports where
2439 * multicast routers are attached. Alternatively stated: a
2440 * snooping switch should not forward IGMP Membership Reports
2441 * to ports on which only hosts are attached.
2442 * An administrative control may be provided to override this
2443 * restriction, allowing the report messages to be flooded to
2445 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2446 ovs_rwlock_unlock(&ms->rwlock);
2448 xlate_report(ctx, "multicast traffic, flooding");
2449 xlate_normal_flood(ctx, in_xbundle, vlan);
2452 } else if (is_mld(flow)) {
2453 ctx->xout->slow |= SLOW_ACTION;
2454 if (ctx->xin->may_learn) {
2455 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2456 in_xbundle, ctx->xin->packet);
2458 if (is_mld_report(flow)) {
2459 ovs_rwlock_rdlock(&ms->rwlock);
2460 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2461 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2462 ovs_rwlock_unlock(&ms->rwlock);
2464 xlate_report(ctx, "MLD query, flooding");
2465 xlate_normal_flood(ctx, in_xbundle, vlan);
2468 if ((flow->dl_type == htons(ETH_TYPE_IP)
2469 && ip_is_local_multicast(flow->nw_dst))
2470 || (flow->dl_type == htons(ETH_TYPE_IPV6)
2471 && ipv6_is_all_hosts(&flow->ipv6_dst))) {
2472 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2473 * address in the 224.0.0.x range which are not IGMP must
2474 * be forwarded on all ports */
2475 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2476 xlate_normal_flood(ctx, in_xbundle, vlan);
2481 /* forwarding to group base ports */
2482 ovs_rwlock_rdlock(&ms->rwlock);
2483 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2484 grp = mcast_snooping_lookup4(ms, flow->nw_dst, vlan);
2485 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2486 grp = mcast_snooping_lookup(ms, &flow->ipv6_dst, vlan);
2489 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2490 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2491 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2493 if (mcast_snooping_flood_unreg(ms)) {
2494 xlate_report(ctx, "unregistered multicast, flooding");
2495 xlate_normal_flood(ctx, in_xbundle, vlan);
2497 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2498 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2501 ovs_rwlock_unlock(&ms->rwlock);
2503 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2504 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
2505 mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
2506 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2509 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2510 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2511 if (mac_xbundle && mac_xbundle != in_xbundle) {
2512 xlate_report(ctx, "forwarding to learned port");
2513 output_normal(ctx, mac_xbundle, vlan);
2514 } else if (!mac_xbundle) {
2515 xlate_report(ctx, "learned port is unknown, dropping");
2517 xlate_report(ctx, "learned port is input port, dropping");
2520 xlate_report(ctx, "no learned MAC for destination, flooding");
2521 xlate_normal_flood(ctx, in_xbundle, vlan);
2526 /* Appends a "sample" action for sFlow or IPFIX to 'ctx->odp_actions'. The
2527 * 'probability' is the number of packets out of UINT32_MAX to sample. The
2528 * 'cookie' (of length 'cookie_size' bytes) is passed back in the callback for
2529 * each sampled packet. 'tunnel_out_port', if not ODPP_NONE, is added as the
2530 * OVS_USERSPACE_ATTR_EGRESS_TUN_PORT attribute. If 'include_actions', an
2531 * OVS_USERSPACE_ATTR_ACTIONS attribute is added.
2534 compose_sample_action(struct xlate_ctx *ctx,
2535 const uint32_t probability,
2536 const union user_action_cookie *cookie,
2537 const size_t cookie_size,
2538 const odp_port_t tunnel_out_port,
2539 bool include_actions)
2541 size_t sample_offset = nl_msg_start_nested(ctx->odp_actions,
2542 OVS_ACTION_ATTR_SAMPLE);
2544 nl_msg_put_u32(ctx->odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
2546 size_t actions_offset = nl_msg_start_nested(ctx->odp_actions,
2547 OVS_SAMPLE_ATTR_ACTIONS);
2549 odp_port_t odp_port = ofp_port_to_odp_port(
2550 ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
2551 uint32_t pid = dpif_port_get_pid(ctx->xbridge->dpif, odp_port,
2552 flow_hash_5tuple(&ctx->xin->flow, 0));
2553 int cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2558 nl_msg_end_nested(ctx->odp_actions, actions_offset);
2559 nl_msg_end_nested(ctx->odp_actions, sample_offset);
2561 return cookie_offset;
2564 /* If sFLow is not enabled, returns 0 without doing anything.
2566 * If sFlow is enabled, appends a template "sample" action to the ODP actions
2567 * in 'ctx'. This action is a template because some of the information needed
2568 * to fill it out is not available until flow translation is complete. In this
2569 * case, this functions returns an offset, which is always nonzero, to pass
2570 * later to fix_sflow_action() to fill in the rest of the template. */
2572 compose_sflow_action(struct xlate_ctx *ctx)
2574 struct dpif_sflow *sflow = ctx->xbridge->sflow;
2575 if (!sflow || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
2579 union user_action_cookie cookie = { .type = USER_ACTION_COOKIE_SFLOW };
2580 return compose_sample_action(ctx, dpif_sflow_get_probability(sflow),
2581 &cookie, sizeof cookie.sflow, ODPP_NONE,
2585 /* If IPFIX is enabled, this appends a "sample" action to implement IPFIX to
2586 * 'ctx->odp_actions'. */
2588 compose_ipfix_action(struct xlate_ctx *ctx, odp_port_t output_odp_port)
2590 struct dpif_ipfix *ipfix = ctx->xbridge->ipfix;
2591 odp_port_t tunnel_out_port = ODPP_NONE;
2593 if (!ipfix || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
2597 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2599 if (output_odp_port == ODPP_NONE &&
2600 !dpif_ipfix_get_bridge_exporter_input_sampling(ipfix)) {
2604 /* For output case, output_odp_port is valid*/
2605 if (output_odp_port != ODPP_NONE) {
2606 if (!dpif_ipfix_get_bridge_exporter_output_sampling(ipfix)) {
2609 /* If tunnel sampling is enabled, put an additional option attribute:
2610 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2612 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(ipfix) &&
2613 dpif_ipfix_get_tunnel_port(ipfix, output_odp_port) ) {
2614 tunnel_out_port = output_odp_port;
2618 union user_action_cookie cookie = {
2620 .type = USER_ACTION_COOKIE_IPFIX,
2621 .output_odp_port = output_odp_port,
2624 compose_sample_action(ctx,
2625 dpif_ipfix_get_bridge_exporter_probability(ipfix),
2626 &cookie, sizeof cookie.ipfix, tunnel_out_port,
2630 /* Fix "sample" action according to data collected while composing ODP actions,
2631 * as described in compose_sflow_action().
2633 * 'user_cookie_offset' must be the offset returned by add_sflow_action(). */
2635 fix_sflow_action(struct xlate_ctx *ctx, unsigned int user_cookie_offset)
2637 const struct flow *base = &ctx->base_flow;
2638 union user_action_cookie *cookie;
2640 cookie = ofpbuf_at(ctx->odp_actions, user_cookie_offset,
2641 sizeof cookie->sflow);
2642 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2644 cookie->type = USER_ACTION_COOKIE_SFLOW;
2645 cookie->sflow.vlan_tci = base->vlan_tci;
2647 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2648 * port information") for the interpretation of cookie->output. */
2649 switch (ctx->sflow_n_outputs) {
2651 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2652 cookie->sflow.output = 0x40000000 | 256;
2656 cookie->sflow.output = dpif_sflow_odp_port_to_ifindex(
2657 ctx->xbridge->sflow, ctx->sflow_odp_port);
2658 if (cookie->sflow.output) {
2663 /* 0x80000000 means "multiple output ports. */
2664 cookie->sflow.output = 0x80000000 | ctx->sflow_n_outputs;
2670 process_special(struct xlate_ctx *ctx, const struct xport *xport)
2672 const struct flow *flow = &ctx->xin->flow;
2673 struct flow_wildcards *wc = ctx->wc;
2674 const struct xbridge *xbridge = ctx->xbridge;
2675 const struct dp_packet *packet = ctx->xin->packet;
2676 enum slow_path_reason slow;
2680 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
2682 cfm_process_heartbeat(xport->cfm, packet);
2685 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
2687 bfd_process_packet(xport->bfd, flow, packet);
2688 /* If POLL received, immediately sends FINAL back. */
2689 if (bfd_should_send_packet(xport->bfd)) {
2690 ofproto_dpif_monitor_port_send_soon(xport->ofport);
2694 } else if (xport->xbundle && xport->xbundle->lacp
2695 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2697 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
2700 } else if ((xbridge->stp || xbridge->rstp) &&
2701 stp_should_process_flow(flow, wc)) {
2704 ? stp_process_packet(xport, packet)
2705 : rstp_process_packet(xport, packet);
2708 } else if (xport->lldp && lldp_should_process_flow(xport->lldp, flow)) {
2710 lldp_process_packet(xport->lldp, packet);
2718 ctx->xout->slow |= slow;
2726 tnl_route_lookup_flow(const struct flow *oflow,
2727 struct in6_addr *ip, struct xport **out_port)
2729 char out_dev[IFNAMSIZ];
2730 struct xbridge *xbridge;
2731 struct xlate_cfg *xcfg;
2733 struct in6_addr dst;
2735 dst = flow_tnl_dst(&oflow->tunnel);
2736 if (!ovs_router_lookup(&dst, out_dev, &gw)) {
2740 if (ipv6_addr_is_set(&gw) &&
2741 (!IN6_IS_ADDR_V4MAPPED(&gw) || in6_addr_get_mapped_ipv4(&gw))) {
2747 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2750 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
2751 if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
2754 HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
2755 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
2766 compose_table_xlate(struct xlate_ctx *ctx, const struct xport *out_dev,
2767 struct dp_packet *packet)
2769 struct xbridge *xbridge = out_dev->xbridge;
2770 struct ofpact_output output;
2773 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
2774 flow_extract(packet, &flow);
2775 flow.in_port.ofp_port = out_dev->ofp_port;
2776 output.port = OFPP_TABLE;
2779 return ofproto_dpif_execute_actions__(xbridge->ofproto, &flow, NULL,
2780 &output.ofpact, sizeof output,
2781 ctx->recurse, ctx->resubmits, packet);
2785 tnl_send_nd_request(struct xlate_ctx *ctx, const struct xport *out_dev,
2786 const struct eth_addr eth_src,
2787 struct in6_addr * ipv6_src, struct in6_addr * ipv6_dst)
2789 struct dp_packet packet;
2791 dp_packet_init(&packet, 0);
2792 compose_nd(&packet, eth_src, ipv6_src, ipv6_dst);
2793 compose_table_xlate(ctx, out_dev, &packet);
2794 dp_packet_uninit(&packet);
2798 tnl_send_arp_request(struct xlate_ctx *ctx, const struct xport *out_dev,
2799 const struct eth_addr eth_src,
2800 ovs_be32 ip_src, ovs_be32 ip_dst)
2802 struct dp_packet packet;
2804 dp_packet_init(&packet, 0);
2805 compose_arp(&packet, ARP_OP_REQUEST,
2806 eth_src, eth_addr_zero, true, ip_src, ip_dst);
2808 compose_table_xlate(ctx, out_dev, &packet);
2809 dp_packet_uninit(&packet);
2813 build_tunnel_send(struct xlate_ctx *ctx, const struct xport *xport,
2814 const struct flow *flow, odp_port_t tunnel_odp_port)
2816 struct ovs_action_push_tnl tnl_push_data;
2817 struct xport *out_dev = NULL;
2818 ovs_be32 s_ip = 0, d_ip = 0;
2819 struct in6_addr s_ip6 = in6addr_any;
2820 struct in6_addr d_ip6 = in6addr_any;
2821 struct eth_addr smac;
2822 struct eth_addr dmac;
2824 char buf_sip6[INET6_ADDRSTRLEN];
2825 char buf_dip6[INET6_ADDRSTRLEN];
2827 err = tnl_route_lookup_flow(flow, &d_ip6, &out_dev);
2829 xlate_report(ctx, "native tunnel routing failed");
2833 xlate_report(ctx, "tunneling to %s via %s",
2834 ipv6_string_mapped(buf_dip6, &d_ip6),
2835 netdev_get_name(out_dev->netdev));
2837 /* Use mac addr of bridge port of the peer. */
2838 err = netdev_get_etheraddr(out_dev->netdev, &smac);
2840 xlate_report(ctx, "tunnel output device lacks Ethernet address");
2844 d_ip = in6_addr_get_mapped_ipv4(&d_ip6);
2846 err = netdev_get_in4(out_dev->netdev, (struct in_addr *) &s_ip, NULL);
2848 xlate_report(ctx, "tunnel output device lacks IPv4 address");
2851 in6_addr_set_mapped_ipv4(&s_ip6, s_ip);
2853 err = netdev_get_in6(out_dev->netdev, &s_ip6);
2855 xlate_report(ctx, "tunnel output device lacks IPv6 address");
2860 err = tnl_neigh_lookup(out_dev->xbridge->name, &d_ip6, &dmac);
2862 xlate_report(ctx, "neighbor cache miss for %s on bridge %s, "
2863 "sending %s request",
2864 buf_dip6, out_dev->xbridge->name, d_ip ? "ARP" : "ND");
2866 tnl_send_arp_request(ctx, out_dev, smac, s_ip, d_ip);
2868 tnl_send_nd_request(ctx, out_dev, smac, &s_ip6, &d_ip6);
2873 if (ctx->xin->xcache) {
2874 struct xc_entry *entry;
2876 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_NEIGH);
2877 ovs_strlcpy(entry->u.tnl_neigh_cache.br_name, out_dev->xbridge->name,
2878 sizeof entry->u.tnl_neigh_cache.br_name);
2879 entry->u.tnl_neigh_cache.d_ipv6 = d_ip6;
2882 xlate_report(ctx, "tunneling from "ETH_ADDR_FMT" %s"
2883 " to "ETH_ADDR_FMT" %s",
2884 ETH_ADDR_ARGS(smac), ipv6_string_mapped(buf_sip6, &s_ip6),
2885 ETH_ADDR_ARGS(dmac), buf_dip6);
2887 err = tnl_port_build_header(xport->ofport, flow,
2888 dmac, smac, &s_ip6, &tnl_push_data);
2892 tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
2893 tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
2894 odp_put_tnl_push_action(ctx->odp_actions, &tnl_push_data);
2899 xlate_commit_actions(struct xlate_ctx *ctx)
2901 bool use_masked = ctx->xbridge->support.masked_set_action;
2903 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2904 ctx->odp_actions, ctx->wc,
2909 clear_conntrack(struct flow *flow)
2914 memset(&flow->ct_label, 0, sizeof flow->ct_label);
2918 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
2919 const struct xlate_bond_recirc *xr, bool check_stp)
2921 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
2922 struct flow_wildcards *wc = ctx->wc;
2923 struct flow *flow = &ctx->xin->flow;
2924 struct flow_tnl flow_tnl;
2925 ovs_be16 flow_vlan_tci;
2926 uint32_t flow_pkt_mark;
2927 uint8_t flow_nw_tos;
2928 odp_port_t out_port, odp_port;
2929 bool tnl_push_pop_send = false;
2932 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2933 * before traversing a patch port. */
2934 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 35);
2935 memset(&flow_tnl, 0, sizeof flow_tnl);
2938 xlate_report(ctx, "Nonexistent output port");
2940 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
2941 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2943 } else if (check_stp) {
2944 if (is_stp(&ctx->base_flow)) {
2945 if (!xport_stp_should_forward_bpdu(xport) &&
2946 !xport_rstp_should_manage_bpdu(xport)) {
2947 if (ctx->xbridge->stp != NULL) {
2948 xlate_report(ctx, "STP not in listening state, "
2949 "skipping bpdu output");
2950 } else if (ctx->xbridge->rstp != NULL) {
2951 xlate_report(ctx, "RSTP not managing BPDU in this state, "
2952 "skipping bpdu output");
2956 } else if (!xport_stp_forward_state(xport) ||
2957 !xport_rstp_forward_state(xport)) {
2958 if (ctx->xbridge->stp != NULL) {
2959 xlate_report(ctx, "STP not in forwarding state, "
2961 } else if (ctx->xbridge->rstp != NULL) {
2962 xlate_report(ctx, "RSTP not in forwarding state, "
2970 const struct xport *peer = xport->peer;
2971 struct flow old_flow = ctx->xin->flow;
2972 bool old_conntrack = ctx->conntracked;
2973 bool old_was_mpls = ctx->was_mpls;
2974 cls_version_t old_version = ctx->tables_version;
2975 struct ofpbuf old_stack = ctx->stack;
2976 union mf_subvalue new_stack[1024 / sizeof(union mf_subvalue)];
2977 struct ofpbuf old_action_set = ctx->action_set;
2978 uint64_t actset_stub[1024 / 8];
2980 ofpbuf_use_stub(&ctx->stack, new_stack, sizeof new_stack);
2981 ofpbuf_use_stub(&ctx->action_set, actset_stub, sizeof actset_stub);
2982 ctx->xbridge = peer->xbridge;
2983 flow->in_port.ofp_port = peer->ofp_port;
2984 flow->metadata = htonll(0);
2985 memset(&flow->tunnel, 0, sizeof flow->tunnel);
2986 memset(flow->regs, 0, sizeof flow->regs);
2987 flow->actset_output = OFPP_UNSET;
2988 ctx->conntracked = false;
2989 clear_conntrack(flow);
2991 /* The bridge is now known so obtain its table version. */
2993 = ofproto_dpif_get_tables_version(ctx->xbridge->ofproto);
2995 if (!process_special(ctx, peer) && may_receive(peer, ctx)) {
2996 if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
2997 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2998 if (ctx->action_set.size) {
2999 /* Translate action set only if not dropping the packet and
3000 * not recirculating. */
3001 if (!exit_recirculates(ctx)) {
3002 xlate_action_set(ctx);
3005 /* Check if need to recirculate. */
3006 if (exit_recirculates(ctx)) {
3007 compose_recirculate_action(ctx);
3010 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
3011 * the learning action look at the packet, then drop it. */
3012 struct flow old_base_flow = ctx->base_flow;
3013 size_t old_size = ctx->odp_actions->size;
3014 mirror_mask_t old_mirrors = ctx->mirrors;
3016 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
3017 ctx->mirrors = old_mirrors;
3018 ctx->base_flow = old_base_flow;
3019 ctx->odp_actions->size = old_size;
3021 /* Undo changes that may have been done for recirculation. */
3022 ctx_cancel_recirculation(ctx);
3026 ctx->xin->flow = old_flow;
3027 ctx->xbridge = xport->xbridge;
3028 ofpbuf_uninit(&ctx->action_set);
3029 ctx->action_set = old_action_set;
3030 ofpbuf_uninit(&ctx->stack);
3031 ctx->stack = old_stack;
3033 /* Restore calling bridge's lookup version. */
3034 ctx->tables_version = old_version;
3036 /* The peer bridge popping MPLS should have no effect on the original
3038 ctx->was_mpls = old_was_mpls;
3040 /* The peer bridge's conntrack execution should have no effect on the
3041 * original bridge. */
3042 ctx->conntracked = old_conntrack;
3044 /* The fact that the peer bridge exits (for any reason) does not mean
3045 * that the original bridge should exit. Specifically, if the peer
3046 * bridge recirculates (which typically modifies the packet), the
3047 * original bridge must continue processing with the original, not the
3048 * recirculated packet! */
3051 /* Peer bridge errors do not propagate back. */
3052 ctx->error = XLATE_OK;
3054 if (ctx->xin->resubmit_stats) {
3055 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
3056 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
3058 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
3061 if (ctx->xin->xcache) {
3062 struct xc_entry *entry;
3064 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
3065 entry->u.dev.tx = netdev_ref(xport->netdev);
3066 entry->u.dev.rx = netdev_ref(peer->netdev);
3067 entry->u.dev.bfd = bfd_ref(peer->bfd);
3072 flow_vlan_tci = flow->vlan_tci;
3073 flow_pkt_mark = flow->pkt_mark;
3074 flow_nw_tos = flow->nw_tos;
3076 if (count_skb_priorities(xport)) {
3077 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3078 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
3079 wc->masks.nw_tos |= IP_DSCP_MASK;
3080 flow->nw_tos &= ~IP_DSCP_MASK;
3081 flow->nw_tos |= dscp;
3085 if (xport->is_tunnel) {
3086 struct in6_addr dst;
3087 /* Save tunnel metadata so that changes made due to
3088 * the Logical (tunnel) Port are not visible for any further
3089 * matches, while explicit set actions on tunnel metadata are.
3091 flow_tnl = flow->tunnel;
3092 odp_port = tnl_port_send(xport->ofport, flow, ctx->wc);
3093 if (odp_port == ODPP_NONE) {
3094 xlate_report(ctx, "Tunneling decided against output");
3095 goto out; /* restore flow_nw_tos */
3097 dst = flow_tnl_dst(&flow->tunnel);
3098 if (ipv6_addr_equals(&dst, &ctx->orig_tunnel_ipv6_dst)) {
3099 xlate_report(ctx, "Not tunneling to our own address");
3100 goto out; /* restore flow_nw_tos */
3102 if (ctx->xin->resubmit_stats) {
3103 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
3105 if (ctx->xin->xcache) {
3106 struct xc_entry *entry;
3108 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
3109 entry->u.dev.tx = netdev_ref(xport->netdev);
3111 out_port = odp_port;
3112 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3113 xlate_report(ctx, "output to native tunnel");
3114 tnl_push_pop_send = true;
3116 xlate_report(ctx, "output to kernel tunnel");
3117 commit_odp_tunnel_action(flow, &ctx->base_flow, ctx->odp_actions);
3118 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3121 odp_port = xport->odp_port;
3122 out_port = odp_port;
3123 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
3124 ofp_port_t vlandev_port;
3126 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
3127 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
3128 ofp_port, flow->vlan_tci);
3129 if (vlandev_port != ofp_port) {
3130 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
3131 flow->vlan_tci = htons(0);
3136 if (out_port != ODPP_NONE) {
3137 xlate_commit_actions(ctx);
3140 struct ovs_action_hash *act_hash;
3143 act_hash = nl_msg_put_unspec_uninit(ctx->odp_actions,
3144 OVS_ACTION_ATTR_HASH,
3146 act_hash->hash_alg = xr->hash_alg;
3147 act_hash->hash_basis = xr->hash_basis;
3149 /* Recirc action. */
3150 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC,
3154 if (tnl_push_pop_send) {
3155 build_tunnel_send(ctx, xport, flow, odp_port);
3156 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3158 odp_port_t odp_tnl_port = ODPP_NONE;
3160 /* XXX: Write better Filter for tunnel port. We can use inport
3161 * int tunnel-port flow to avoid these checks completely. */
3162 if (ofp_port == OFPP_LOCAL &&
3163 ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3165 odp_tnl_port = tnl_port_map_lookup(flow, wc);
3168 if (odp_tnl_port != ODPP_NONE) {
3169 nl_msg_put_odp_port(ctx->odp_actions,
3170 OVS_ACTION_ATTR_TUNNEL_POP,
3173 /* Tunnel push-pop action is not compatible with
3175 compose_ipfix_action(ctx, out_port);
3176 nl_msg_put_odp_port(ctx->odp_actions,
3177 OVS_ACTION_ATTR_OUTPUT,
3183 ctx->sflow_odp_port = odp_port;
3184 ctx->sflow_n_outputs++;
3185 ctx->nf_output_iface = ofp_port;
3188 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
3189 mirror_packet(ctx, xport->xbundle,
3190 xbundle_mirror_dst(xport->xbundle->xbridge,
3196 flow->vlan_tci = flow_vlan_tci;
3197 flow->pkt_mark = flow_pkt_mark;
3198 flow->nw_tos = flow_nw_tos;
3202 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port,
3203 const struct xlate_bond_recirc *xr)
3205 compose_output_action__(ctx, ofp_port, xr, true);
3209 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
3211 struct rule_dpif *old_rule = ctx->rule;
3212 ovs_be64 old_cookie = ctx->rule_cookie;
3213 const struct rule_actions *actions;
3215 if (ctx->xin->resubmit_stats) {
3216 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
3222 ctx->rule_cookie = rule_dpif_get_flow_cookie(rule);
3223 actions = rule_dpif_get_actions(rule);
3224 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
3225 ctx->rule_cookie = old_cookie;
3226 ctx->rule = old_rule;
3231 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
3233 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
3234 XLATE_REPORT_ERROR(ctx, "resubmit actions recursed over %d times",
3235 MAX_RESUBMIT_RECURSION);
3236 ctx->error = XLATE_RECURSION_TOO_DEEP;
3237 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
3238 XLATE_REPORT_ERROR(ctx, "over %d resubmit actions", MAX_RESUBMITS);
3239 ctx->error = XLATE_TOO_MANY_RESUBMITS;
3240 } else if (ctx->odp_actions->size > UINT16_MAX) {
3241 XLATE_REPORT_ERROR(ctx, "resubmits yielded over 64 kB of actions");
3242 /* NOT an error, as we'll be slow-pathing the flow in this case? */
3243 ctx->exit = true; /* XXX: translation still terminated! */
3244 } else if (ctx->stack.size >= 65536) {
3245 XLATE_REPORT_ERROR(ctx, "resubmits yielded over 64 kB of stack");
3246 ctx->error = XLATE_STACK_TOO_DEEP;
3255 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
3256 bool may_packet_in, bool honor_table_miss)
3258 /* Check if we need to recirculate before matching in a table. */
3259 if (ctx->was_mpls) {
3260 ctx_trigger_recirculation(ctx);
3263 if (xlate_resubmit_resource_check(ctx)) {
3264 uint8_t old_table_id = ctx->table_id;
3265 struct rule_dpif *rule;
3267 ctx->table_id = table_id;
3269 rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
3270 ctx->tables_version,
3271 &ctx->xin->flow, ctx->xin->wc,
3272 ctx->xin->resubmit_stats,
3273 &ctx->table_id, in_port,
3274 may_packet_in, honor_table_miss);
3276 if (OVS_UNLIKELY(ctx->xin->resubmit_hook)) {
3277 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse + 1);
3281 /* Fill in the cache entry here instead of xlate_recursively
3282 * to make the reference counting more explicit. We take a
3283 * reference in the lookups above if we are going to cache the
3285 if (ctx->xin->xcache) {
3286 struct xc_entry *entry;
3288 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
3289 entry->u.rule = rule;
3290 rule_dpif_ref(rule);
3292 xlate_recursively(ctx, rule);
3295 ctx->table_id = old_table_id;
3301 xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
3302 struct ofputil_bucket *bucket)
3304 if (ctx->xin->resubmit_stats) {
3305 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
3307 if (ctx->xin->xcache) {
3308 struct xc_entry *entry;
3310 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
3311 entry->u.group.group = group_dpif_ref(group);
3312 entry->u.group.bucket = bucket;
3317 xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
3319 uint64_t action_list_stub[1024 / 8];
3320 struct ofpbuf action_list, action_set;
3321 struct flow old_flow = ctx->xin->flow;
3322 bool old_was_mpls = ctx->was_mpls;
3324 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
3325 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3327 ofpacts_execute_action_set(&action_list, &action_set);
3329 do_xlate_actions(action_list.data, action_list.size, ctx);
3332 ofpbuf_uninit(&action_set);
3333 ofpbuf_uninit(&action_list);
3335 /* Check if need to recirculate. */
3336 if (exit_recirculates(ctx)) {
3337 compose_recirculate_action(ctx);
3340 /* Roll back flow to previous state.
3341 * This is equivalent to cloning the packet for each bucket.
3343 * As a side effect any subsequently applied actions will
3344 * also effectively be applied to a clone of the packet taken
3345 * just before applying the all or indirect group.
3347 * Note that group buckets are action sets, hence they cannot modify the
3348 * main action set. Also any stack actions are ignored when executing an
3349 * action set, so group buckets cannot change the stack either.
3350 * However, we do allow resubmit actions in group buckets, which could
3351 * break the above assumptions. It is up to the controller to not mess up
3352 * with the action_set and stack in the tables resubmitted to from
3354 ctx->xin->flow = old_flow;
3356 /* The group bucket popping MPLS should have no effect after bucket
3358 ctx->was_mpls = old_was_mpls;
3360 /* The fact that the group bucket exits (for any reason) does not mean that
3361 * the translation after the group action should exit. Specifically, if
3362 * the group bucket recirculates (which typically modifies the packet), the
3363 * actions after the group action must continue processing with the
3364 * original, not the recirculated packet! */
3369 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3371 struct ofputil_bucket *bucket;
3372 const struct ovs_list *buckets;
3374 group_dpif_get_buckets(group, &buckets);
3376 LIST_FOR_EACH (bucket, list_node, buckets) {
3377 xlate_group_bucket(ctx, bucket);
3379 xlate_group_stats(ctx, group, NULL);
3383 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3385 struct ofputil_bucket *bucket;
3387 bucket = group_first_live_bucket(ctx, group, 0);
3389 xlate_group_bucket(ctx, bucket);
3390 xlate_group_stats(ctx, group, bucket);
3395 xlate_default_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3397 struct flow_wildcards *wc = ctx->wc;
3398 struct ofputil_bucket *bucket;
3401 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
3402 flow_mask_hash_fields(&ctx->xin->flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
3403 bucket = group_best_live_bucket(ctx, group, basis);
3405 xlate_group_bucket(ctx, bucket);
3406 xlate_group_stats(ctx, group, bucket);
3411 xlate_hash_fields_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3413 struct mf_bitmap hash_fields = MF_BITMAP_INITIALIZER;
3414 const struct field_array *fields;
3415 struct ofputil_bucket *bucket;
3419 fields = group_dpif_get_fields(group);
3420 basis = hash_uint64(group_dpif_get_selection_method_param(group));
3422 /* Determine which fields to hash */
3423 for (i = 0; i < MFF_N_IDS; i++) {
3424 if (bitmap_is_set(fields->used.bm, i)) {
3425 const struct mf_field *mf;
3427 /* If the field is already present in 'hash_fields' then
3428 * this loop has already checked that it and its pre-requisites
3429 * are present in the flow and its pre-requisites have
3430 * already been added to 'hash_fields'. There is nothing more
3431 * to do here and as an optimisation the loop can continue. */
3432 if (bitmap_is_set(hash_fields.bm, i)) {
3438 /* Only hash a field if it and its pre-requisites are present
3440 if (!mf_are_prereqs_ok(mf, &ctx->xin->flow)) {
3444 /* Hash both the field and its pre-requisites */
3445 mf_bitmap_set_field_and_prereqs(mf, &hash_fields);
3449 /* Hash the fields */
3450 for (i = 0; i < MFF_N_IDS; i++) {
3451 if (bitmap_is_set(hash_fields.bm, i)) {
3452 const struct mf_field *mf = mf_from_id(i);
3453 union mf_value value;
3456 mf_get_value(mf, &ctx->xin->flow, &value);
3457 /* This seems inefficient but so does apply_mask() */
3458 for (j = 0; j < mf->n_bytes; j++) {
3459 ((uint8_t *) &value)[j] &= ((uint8_t *) &fields->value[i])[j];
3461 basis = hash_bytes(&value, mf->n_bytes, basis);
3463 /* For tunnels, hash in whether the field is present. */
3464 if (mf_is_tun_metadata(mf)) {
3465 basis = hash_boolean(mf_is_set(mf, &ctx->xin->flow), basis);
3468 mf_mask_field(mf, &ctx->wc->masks);
3472 bucket = group_best_live_bucket(ctx, group, basis);
3474 xlate_group_bucket(ctx, bucket);
3475 xlate_group_stats(ctx, group, bucket);
3480 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3482 const char *selection_method = group_dpif_get_selection_method(group);
3484 if (selection_method[0] == '\0') {
3485 xlate_default_select_group(ctx, group);
3486 } else if (!strcasecmp("hash", selection_method)) {
3487 xlate_hash_fields_select_group(ctx, group);
3489 /* Parsing of groups should ensure this never happens */
3495 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3497 bool was_in_group = ctx->in_group;
3498 ctx->in_group = true;
3500 switch (group_dpif_get_type(group)) {
3502 case OFPGT11_INDIRECT:
3503 xlate_all_group(ctx, group);
3505 case OFPGT11_SELECT:
3506 xlate_select_group(ctx, group);
3509 xlate_ff_group(ctx, group);
3514 group_dpif_unref(group);
3516 ctx->in_group = was_in_group;
3520 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3522 if (xlate_resubmit_resource_check(ctx)) {
3523 struct group_dpif *group;
3526 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
3528 xlate_group_action__(ctx, group);
3538 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
3539 const struct ofpact_resubmit *resubmit)
3543 bool may_packet_in = false;
3544 bool honor_table_miss = false;
3546 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
3547 /* Still allow missed packets to be sent to the controller
3548 * if resubmitting from an internal table. */
3549 may_packet_in = true;
3550 honor_table_miss = true;
3553 in_port = resubmit->in_port;
3554 if (in_port == OFPP_IN_PORT) {
3555 in_port = ctx->xin->flow.in_port.ofp_port;
3558 table_id = resubmit->table_id;
3559 if (table_id == 255) {
3560 table_id = ctx->table_id;
3563 xlate_table_action(ctx, in_port, table_id, may_packet_in,
3568 flood_packets(struct xlate_ctx *ctx, bool all)
3570 const struct xport *xport;
3572 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
3573 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
3578 compose_output_action__(ctx, xport->ofp_port, NULL, false);
3579 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
3580 compose_output_action(ctx, xport->ofp_port, NULL);
3584 ctx->nf_output_iface = NF_OUT_FLOOD;
3588 execute_controller_action(struct xlate_ctx *ctx, int len,
3589 enum ofp_packet_in_reason reason,
3590 uint16_t controller_id)
3592 struct dp_packet *packet;
3594 ctx->xout->slow |= SLOW_CONTROLLER;
3595 xlate_commit_actions(ctx);
3596 if (!ctx->xin->packet) {
3600 packet = dp_packet_clone(ctx->xin->packet);
3602 odp_execute_actions(NULL, &packet, 1, false,
3603 ctx->odp_actions->data, ctx->odp_actions->size, NULL);
3605 /* A packet sent by an action in a table-miss rule is considered an
3606 * explicit table miss. OpenFlow before 1.3 doesn't have that concept so
3607 * it will get translated back to OFPR_ACTION for those versions. */
3608 if (reason == OFPR_ACTION
3609 && ctx->rule && rule_dpif_is_table_miss(ctx->rule)) {
3610 reason = OFPR_EXPLICIT_MISS;
3613 size_t packet_len = dp_packet_size(packet);
3615 struct ofproto_async_msg *am = xmalloc(sizeof *am);
3616 *am = (struct ofproto_async_msg) {
3617 .controller_id = controller_id,
3618 .oam = OAM_PACKET_IN,
3621 .packet = dp_packet_steal_data(packet),
3624 .table_id = ctx->table_id,
3625 .cookie = ctx->rule_cookie,
3630 flow_get_metadata(&ctx->xin->flow, &am->pin.up.flow_metadata);
3632 ofproto_dpif_send_async_msg(ctx->xbridge->ofproto, am);
3633 dp_packet_delete(packet);
3637 compose_recirculate_action__(struct xlate_ctx *ctx, uint8_t table)
3639 struct recirc_metadata md;
3642 recirc_metadata_from_flow(&md, &ctx->xin->flow);
3644 ovs_assert(ctx->recirc_action_offset >= 0);
3646 struct recirc_state state = {
3648 .ofproto_uuid = *ofproto_dpif_get_uuid(ctx->xbridge->ofproto),
3650 .stack = ctx->stack.data,
3651 .n_stack = ctx->stack.size / sizeof(union mf_subvalue),
3652 .mirrors = ctx->mirrors,
3653 .conntracked = ctx->conntracked,
3654 .ofpacts = ((struct ofpact *) ctx->action_set.data
3655 + ctx->recirc_action_offset / sizeof(struct ofpact)),
3656 .ofpacts_len = ctx->action_set.size - ctx->recirc_action_offset,
3657 .action_set = ctx->action_set.data,
3658 .action_set_len = ctx->recirc_action_offset,
3661 /* Allocate a unique recirc id for the given metadata state in the
3662 * flow. An existing id, with a new reference to the corresponding
3663 * recirculation context, will be returned if possible.
3664 * The life-cycle of this recirc id is managed by associating it
3665 * with the udpif key ('ukey') created for each new datapath flow. */
3666 id = recirc_alloc_id_ctx(&state);
3668 XLATE_REPORT_ERROR(ctx, "Failed to allocate recirculation id");
3669 ctx->error = XLATE_NO_RECIRCULATION_CONTEXT;
3672 recirc_refs_add(&ctx->xout->recircs, id);
3674 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
3676 /* Undo changes done by recirculation. */
3677 ctx_cancel_recirculation(ctx);
3680 /* Called only when ctx->recirc_action_offset is set. */
3682 compose_recirculate_action(struct xlate_ctx *ctx)
3684 xlate_commit_actions(ctx);
3685 compose_recirculate_action__(ctx, 0);
3688 /* Fork the pipeline here. The current packet will continue processing the
3689 * current action list. A clone of the current packet will recirculate, skip
3690 * the remainder of the current action list and asynchronously resume pipeline
3691 * processing in 'table' with the current metadata and action set. */
3693 compose_recirculate_and_fork(struct xlate_ctx *ctx, uint8_t table)
3695 ctx->recirc_action_offset = ctx->action_set.size;
3696 compose_recirculate_action__(ctx, table);
3700 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
3702 struct flow *flow = &ctx->xin->flow;
3705 ovs_assert(eth_type_mpls(mpls->ethertype));
3707 n = flow_count_mpls_labels(flow, ctx->wc);
3709 xlate_commit_actions(ctx);
3710 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3711 if (ctx->xin->packet != NULL) {
3712 XLATE_REPORT_ERROR(ctx, "bridge %s: dropping packet on which an "
3713 "MPLS push action can't be performed as it would "
3714 "have more MPLS LSEs than the %d supported.",
3715 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3717 ctx->error = XLATE_TOO_MANY_MPLS_LABELS;
3721 flow_push_mpls(flow, n, mpls->ethertype, ctx->wc);
3725 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
3727 struct flow *flow = &ctx->xin->flow;
3728 int n = flow_count_mpls_labels(flow, ctx->wc);
3730 if (flow_pop_mpls(flow, n, eth_type, ctx->wc)) {
3731 if (ctx->xbridge->support.odp.recirc) {
3732 ctx->was_mpls = true;
3734 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3735 if (ctx->xin->packet != NULL) {
3736 XLATE_REPORT_ERROR(ctx, "bridge %s: dropping packet on which an "
3737 "MPLS pop action can't be performed as it has "
3738 "more MPLS LSEs than the %d supported.",
3739 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3741 ctx->error = XLATE_TOO_MANY_MPLS_LABELS;
3742 ofpbuf_clear(ctx->odp_actions);
3747 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3749 struct flow *flow = &ctx->xin->flow;
3751 if (!is_ip_any(flow)) {
3755 ctx->wc->masks.nw_ttl = 0xff;
3756 if (flow->nw_ttl > 1) {
3762 for (i = 0; i < ids->n_controllers; i++) {
3763 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3767 /* Stop processing for current table. */
3773 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3775 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3776 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
3777 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
3782 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3784 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3785 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
3786 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
3791 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
3793 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3794 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3795 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
3800 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
3802 struct flow *flow = &ctx->xin->flow;
3804 if (eth_type_mpls(flow->dl_type)) {
3805 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
3807 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3810 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3813 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
3817 /* Stop processing for current table. */
3822 xlate_output_action(struct xlate_ctx *ctx,
3823 ofp_port_t port, uint16_t max_len, bool may_packet_in)
3825 ofp_port_t prev_nf_output_iface = ctx->nf_output_iface;
3827 ctx->nf_output_iface = NF_OUT_DROP;
3831 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port, NULL);
3834 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3835 0, may_packet_in, true);
3841 flood_packets(ctx, false);
3844 flood_packets(ctx, true);
3846 case OFPP_CONTROLLER:
3847 execute_controller_action(ctx, max_len,
3848 (ctx->in_group ? OFPR_GROUP
3849 : ctx->in_action_set ? OFPR_ACTION_SET
3857 if (port != ctx->xin->flow.in_port.ofp_port) {
3858 compose_output_action(ctx, port, NULL);
3860 xlate_report(ctx, "skipping output to input port");
3865 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3866 ctx->nf_output_iface = NF_OUT_FLOOD;
3867 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3868 ctx->nf_output_iface = prev_nf_output_iface;
3869 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3870 ctx->nf_output_iface != NF_OUT_FLOOD) {
3871 ctx->nf_output_iface = NF_OUT_MULTI;
3876 xlate_output_reg_action(struct xlate_ctx *ctx,
3877 const struct ofpact_output_reg *or)
3879 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3880 if (port <= UINT16_MAX) {
3881 union mf_subvalue value;
3883 memset(&value, 0xff, sizeof value);
3884 mf_write_subfield_flow(&or->src, &value, &ctx->wc->masks);
3885 xlate_output_action(ctx, u16_to_ofp(port),
3886 or->max_len, false);
3891 xlate_enqueue_action(struct xlate_ctx *ctx,
3892 const struct ofpact_enqueue *enqueue)
3894 ofp_port_t ofp_port = enqueue->port;
3895 uint32_t queue_id = enqueue->queue;
3896 uint32_t flow_priority, priority;
3899 /* Translate queue to priority. */
3900 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
3902 /* Fall back to ordinary output action. */
3903 xlate_output_action(ctx, enqueue->port, 0, false);
3907 /* Check output port. */
3908 if (ofp_port == OFPP_IN_PORT) {
3909 ofp_port = ctx->xin->flow.in_port.ofp_port;
3910 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
3914 /* Add datapath actions. */
3915 flow_priority = ctx->xin->flow.skb_priority;
3916 ctx->xin->flow.skb_priority = priority;
3917 compose_output_action(ctx, ofp_port, NULL);
3918 ctx->xin->flow.skb_priority = flow_priority;
3920 /* Update NetFlow output port. */
3921 if (ctx->nf_output_iface == NF_OUT_DROP) {
3922 ctx->nf_output_iface = ofp_port;
3923 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3924 ctx->nf_output_iface = NF_OUT_MULTI;
3929 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3931 uint32_t skb_priority;
3933 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
3934 ctx->xin->flow.skb_priority = skb_priority;
3936 /* Couldn't translate queue to a priority. Nothing to do. A warning
3937 * has already been logged. */
3942 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
3944 const struct xbridge *xbridge = xbridge_;
3955 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3958 port = get_ofp_port(xbridge, ofp_port);
3959 return port ? port->may_enable : false;
3964 xlate_bundle_action(struct xlate_ctx *ctx,
3965 const struct ofpact_bundle *bundle)
3969 port = bundle_execute(bundle, &ctx->xin->flow, ctx->wc, slave_enabled_cb,
3970 CONST_CAST(struct xbridge *, ctx->xbridge));
3971 if (bundle->dst.field) {
3972 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow, ctx->wc);
3974 xlate_output_action(ctx, port, 0, false);
3979 xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
3980 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
3982 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
3983 if (ctx->xin->may_learn) {
3984 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
3989 xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
3991 learn_mask(learn, ctx->wc);
3993 if (ctx->xin->xcache) {
3994 struct xc_entry *entry;
3996 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
3997 entry->u.learn.ofproto = ctx->xbridge->ofproto;
3998 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
3999 entry->u.learn.ofpacts = ofpbuf_new(64);
4000 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
4001 entry->u.learn.ofpacts);
4002 } else if (ctx->xin->may_learn) {
4003 uint64_t ofpacts_stub[1024 / 8];
4004 struct ofputil_flow_mod fm;
4005 struct ofpbuf ofpacts;
4007 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
4008 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
4009 ofpbuf_uninit(&ofpacts);
4014 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
4015 uint16_t idle_timeout, uint16_t hard_timeout)
4017 if (tcp_flags & (TCP_FIN | TCP_RST)) {
4018 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
4023 xlate_fin_timeout(struct xlate_ctx *ctx,
4024 const struct ofpact_fin_timeout *oft)
4027 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
4028 oft->fin_idle_timeout, oft->fin_hard_timeout);
4029 if (ctx->xin->xcache) {
4030 struct xc_entry *entry;
4032 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
4033 /* XC_RULE already holds a reference on the rule, none is taken
4035 entry->u.fin.rule = ctx->rule;
4036 entry->u.fin.idle = oft->fin_idle_timeout;
4037 entry->u.fin.hard = oft->fin_hard_timeout;
4043 xlate_sample_action(struct xlate_ctx *ctx,
4044 const struct ofpact_sample *os)
4046 /* Scale the probability from 16-bit to 32-bit while representing
4047 * the same percentage. */
4048 uint32_t probability = (os->probability << 16) | os->probability;
4050 if (!ctx->xbridge->support.variable_length_userdata) {
4051 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4053 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
4054 "lacks support (needs Linux 3.10+ or kernel module from "
4059 xlate_commit_actions(ctx);
4061 union user_action_cookie cookie = {
4063 .type = USER_ACTION_COOKIE_FLOW_SAMPLE,
4064 .probability = os->probability,
4065 .collector_set_id = os->collector_set_id,
4066 .obs_domain_id = os->obs_domain_id,
4067 .obs_point_id = os->obs_point_id,
4070 compose_sample_action(ctx, probability, &cookie, sizeof cookie.flow_sample,
4075 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
4077 if (xport->config & (is_stp(&ctx->xin->flow)
4078 ? OFPUTIL_PC_NO_RECV_STP
4079 : OFPUTIL_PC_NO_RECV)) {
4083 /* Only drop packets here if both forwarding and learning are
4084 * disabled. If just learning is enabled, we need to have
4085 * OFPP_NORMAL and the learning action have a look at the packet
4086 * before we can drop it. */
4087 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
4088 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
4096 xlate_write_actions__(struct xlate_ctx *ctx,
4097 const struct ofpact *ofpacts, size_t ofpacts_len)
4099 /* Maintain actset_output depending on the contents of the action set:
4101 * - OFPP_UNSET, if there is no "output" action.
4103 * - The output port, if there is an "output" action and no "group"
4106 * - OFPP_UNSET, if there is a "group" action.
4108 if (!ctx->action_set_has_group) {
4109 const struct ofpact *a;
4110 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4111 if (a->type == OFPACT_OUTPUT) {
4112 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(a)->port;
4113 } else if (a->type == OFPACT_GROUP) {
4114 ctx->xin->flow.actset_output = OFPP_UNSET;
4115 ctx->action_set_has_group = true;
4121 ofpbuf_put(&ctx->action_set, ofpacts, ofpacts_len);
4125 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact_nest *a)
4127 xlate_write_actions__(ctx, a->actions, ofpact_nest_get_action_len(a));
4131 xlate_action_set(struct xlate_ctx *ctx)
4133 uint64_t action_list_stub[1024 / 64];
4134 struct ofpbuf action_list;
4136 ctx->in_action_set = true;
4137 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
4138 ofpacts_execute_action_set(&action_list, &ctx->action_set);
4139 /* Clear the action set, as it is not needed any more. */
4140 ofpbuf_clear(&ctx->action_set);
4141 do_xlate_actions(action_list.data, action_list.size, ctx);
4142 ctx->in_action_set = false;
4143 ofpbuf_uninit(&action_list);
4147 recirc_put_unroll_xlate(struct xlate_ctx *ctx)
4149 struct ofpact_unroll_xlate *unroll;
4151 unroll = ctx->last_unroll_offset < 0
4153 : ALIGNED_CAST(struct ofpact_unroll_xlate *,
4154 (char *)ctx->action_set.data + ctx->last_unroll_offset);
4156 /* Restore the table_id and rule cookie for a potential PACKET
4159 (ctx->table_id != unroll->rule_table_id
4160 || ctx->rule_cookie != unroll->rule_cookie)) {
4162 ctx->last_unroll_offset = ctx->action_set.size;
4163 unroll = ofpact_put_UNROLL_XLATE(&ctx->action_set);
4164 unroll->rule_table_id = ctx->table_id;
4165 unroll->rule_cookie = ctx->rule_cookie;
4170 /* Copy actions 'a' through 'end' to the action_set to be executed after
4171 * recirculation. UNROLL_XLATE action is inserted, if not already done so,
4172 * before actions that may depend on the current table ID or flow cookie. */
4174 recirc_unroll_actions(const struct ofpact *a, const struct ofpact *end,
4175 struct xlate_ctx *ctx)
4177 for (; a < end; a = ofpact_next(a)) {
4179 case OFPACT_OUTPUT_REG:
4182 case OFPACT_CONTROLLER:
4183 case OFPACT_DEC_MPLS_TTL:
4184 case OFPACT_DEC_TTL:
4185 /* These actions may generate asynchronous messages, which include
4186 * table ID and flow cookie information. */
4187 recirc_put_unroll_xlate(ctx);
4190 case OFPACT_RESUBMIT:
4191 if (ofpact_get_RESUBMIT(a)->table_id == 0xff) {
4192 /* This resubmit action is relative to the current table, so we
4193 * need to track what table that is.*/
4194 recirc_put_unroll_xlate(ctx);
4198 case OFPACT_SET_TUNNEL:
4199 case OFPACT_REG_MOVE:
4200 case OFPACT_SET_FIELD:
4201 case OFPACT_STACK_PUSH:
4202 case OFPACT_STACK_POP:
4204 case OFPACT_WRITE_METADATA:
4205 case OFPACT_GOTO_TABLE:
4206 case OFPACT_ENQUEUE:
4207 case OFPACT_SET_VLAN_VID:
4208 case OFPACT_SET_VLAN_PCP:
4209 case OFPACT_STRIP_VLAN:
4210 case OFPACT_PUSH_VLAN:
4211 case OFPACT_SET_ETH_SRC:
4212 case OFPACT_SET_ETH_DST:
4213 case OFPACT_SET_IPV4_SRC:
4214 case OFPACT_SET_IPV4_DST:
4215 case OFPACT_SET_IP_DSCP:
4216 case OFPACT_SET_IP_ECN:
4217 case OFPACT_SET_IP_TTL:
4218 case OFPACT_SET_L4_SRC_PORT:
4219 case OFPACT_SET_L4_DST_PORT:
4220 case OFPACT_SET_QUEUE:
4221 case OFPACT_POP_QUEUE:
4222 case OFPACT_PUSH_MPLS:
4223 case OFPACT_POP_MPLS:
4224 case OFPACT_SET_MPLS_LABEL:
4225 case OFPACT_SET_MPLS_TC:
4226 case OFPACT_SET_MPLS_TTL:
4227 case OFPACT_MULTIPATH:
4230 case OFPACT_UNROLL_XLATE:
4231 case OFPACT_FIN_TIMEOUT:
4232 case OFPACT_CLEAR_ACTIONS:
4233 case OFPACT_WRITE_ACTIONS:
4236 case OFPACT_DEBUG_RECIRC:
4239 /* These may not generate PACKET INs. */
4243 case OFPACT_CONJUNCTION:
4244 /* These need not be copied for restoration. */
4247 /* Copy the action over. */
4248 ofpbuf_put(&ctx->action_set, a, OFPACT_ALIGN(a->len));
4252 #define CHECK_MPLS_RECIRCULATION() \
4253 if (ctx->was_mpls) { \
4254 ctx_trigger_recirculation(ctx); \
4257 #define CHECK_MPLS_RECIRCULATION_IF(COND) \
4259 CHECK_MPLS_RECIRCULATION(); \
4263 put_ct_mark(const struct flow *flow, struct flow *base_flow,
4264 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4271 odp_attr.key = flow->ct_mark;
4272 odp_attr.mask = wc->masks.ct_mark;
4274 if (odp_attr.mask && odp_attr.key != base_flow->ct_mark) {
4275 nl_msg_put_unspec(odp_actions, OVS_CT_ATTR_MARK, &odp_attr,
4281 put_ct_label(const struct flow *flow, struct flow *base_flow,
4282 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4284 if (!ovs_u128_is_zero(&wc->masks.ct_label)
4285 && !ovs_u128_equals(&flow->ct_label, &base_flow->ct_label)) {
4291 odp_ct_label = nl_msg_put_unspec_uninit(odp_actions,
4293 sizeof(*odp_ct_label));
4294 odp_ct_label->key = flow->ct_label;
4295 odp_ct_label->mask = wc->masks.ct_label;
4300 put_ct_helper(struct ofpbuf *odp_actions, struct ofpact_conntrack *ofc)
4303 if (ofc->alg == IPPORT_FTP) {
4304 nl_msg_put_string(odp_actions, OVS_CT_ATTR_HELPER, "ftp");
4306 VLOG_WARN("Cannot serialize ct_helper %d\n", ofc->alg);
4312 put_ct_nat(struct xlate_ctx *ctx)
4314 struct ofpact_nat *ofn = ctx->ct_nat_action;
4321 nat_offset = nl_msg_start_nested(ctx->odp_actions, OVS_CT_ATTR_NAT);
4322 if (ofn->flags & NX_NAT_F_SRC || ofn->flags & NX_NAT_F_DST) {
4323 nl_msg_put_flag(ctx->odp_actions, ofn->flags & NX_NAT_F_SRC
4324 ? OVS_NAT_ATTR_SRC : OVS_NAT_ATTR_DST);
4325 if (ofn->flags & NX_NAT_F_PERSISTENT) {
4326 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PERSISTENT);
4328 if (ofn->flags & NX_NAT_F_PROTO_HASH) {
4329 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PROTO_HASH);
4330 } else if (ofn->flags & NX_NAT_F_PROTO_RANDOM) {
4331 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PROTO_RANDOM);
4333 if (ofn->range_af == AF_INET) {
4334 nl_msg_put_be32(ctx->odp_actions, OVS_NAT_ATTR_IP_MIN,
4335 ofn->range.addr.ipv4.min);
4336 if (ofn->range.addr.ipv4.max &&
4337 (ntohl(ofn->range.addr.ipv4.max)
4338 > ntohl(ofn->range.addr.ipv4.min))) {
4339 nl_msg_put_be32(ctx->odp_actions, OVS_NAT_ATTR_IP_MAX,
4340 ofn->range.addr.ipv4.max);
4342 } else if (ofn->range_af == AF_INET6) {
4343 nl_msg_put_unspec(ctx->odp_actions, OVS_NAT_ATTR_IP_MIN,
4344 &ofn->range.addr.ipv6.min,
4345 sizeof ofn->range.addr.ipv6.min);
4346 if (!ipv6_mask_is_any(&ofn->range.addr.ipv6.max) &&
4347 memcmp(&ofn->range.addr.ipv6.max, &ofn->range.addr.ipv6.min,
4348 sizeof ofn->range.addr.ipv6.max) > 0) {
4349 nl_msg_put_unspec(ctx->odp_actions, OVS_NAT_ATTR_IP_MAX,
4350 &ofn->range.addr.ipv6.max,
4351 sizeof ofn->range.addr.ipv6.max);
4354 if (ofn->range_af != AF_UNSPEC && ofn->range.proto.min) {
4355 nl_msg_put_u16(ctx->odp_actions, OVS_NAT_ATTR_PROTO_MIN,
4356 ofn->range.proto.min);
4357 if (ofn->range.proto.max &&
4358 ofn->range.proto.max > ofn->range.proto.min) {
4359 nl_msg_put_u16(ctx->odp_actions, OVS_NAT_ATTR_PROTO_MAX,
4360 ofn->range.proto.max);
4364 nl_msg_end_nested(ctx->odp_actions, nat_offset);
4368 compose_conntrack_action(struct xlate_ctx *ctx, struct ofpact_conntrack *ofc)
4370 ovs_u128 old_ct_label = ctx->base_flow.ct_label;
4371 uint32_t old_ct_mark = ctx->base_flow.ct_mark;
4375 /* Ensure that any prior actions are applied before composing the new
4376 * conntrack action. */
4377 xlate_commit_actions(ctx);
4379 /* Process nested actions first, to populate the key. */
4380 ctx->ct_nat_action = NULL;
4381 do_xlate_actions(ofc->actions, ofpact_ct_get_action_len(ofc), ctx);
4383 if (ofc->zone_src.field) {
4384 zone = mf_get_subfield(&ofc->zone_src, &ctx->xin->flow);
4386 zone = ofc->zone_imm;
4389 ct_offset = nl_msg_start_nested(ctx->odp_actions, OVS_ACTION_ATTR_CT);
4390 if (ofc->flags & NX_CT_F_COMMIT) {
4391 nl_msg_put_flag(ctx->odp_actions, OVS_CT_ATTR_COMMIT);
4393 nl_msg_put_u16(ctx->odp_actions, OVS_CT_ATTR_ZONE, zone);
4394 put_ct_mark(&ctx->xin->flow, &ctx->base_flow, ctx->odp_actions, ctx->wc);
4395 put_ct_label(&ctx->xin->flow, &ctx->base_flow, ctx->odp_actions, ctx->wc);
4396 put_ct_helper(ctx->odp_actions, ofc);
4398 ctx->ct_nat_action = NULL;
4399 nl_msg_end_nested(ctx->odp_actions, ct_offset);
4401 /* Restore the original ct fields in the key. These should only be exposed
4402 * after recirculation to another table. */
4403 ctx->base_flow.ct_mark = old_ct_mark;
4404 ctx->base_flow.ct_label = old_ct_label;
4406 if (ofc->recirc_table == NX_CT_RECIRC_NONE) {
4407 /* If we do not recirculate as part of this action, hide the results of
4408 * connection tracking from subsequent recirculations. */
4409 ctx->conntracked = false;
4411 /* Use ct_* fields from datapath during recirculation upcall. */
4412 ctx->conntracked = true;
4413 compose_recirculate_and_fork(ctx, ofc->recirc_table);
4418 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4419 struct xlate_ctx *ctx)
4421 struct flow_wildcards *wc = ctx->wc;
4422 struct flow *flow = &ctx->xin->flow;
4423 const struct ofpact *a;
4425 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
4426 tnl_neigh_snoop(flow, wc, ctx->xbridge->name);
4428 /* dl_type already in the mask, not set below. */
4430 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4431 struct ofpact_controller *controller;
4432 const struct ofpact_metadata *metadata;
4433 const struct ofpact_set_field *set_field;
4434 const struct mf_field *mf;
4441 /* Check if need to store the remaining actions for later
4443 if (exit_recirculates(ctx)) {
4444 recirc_unroll_actions(a, ofpact_end(ofpacts, ofpacts_len),
4452 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
4453 ofpact_get_OUTPUT(a)->max_len, true);
4457 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
4458 /* Group could not be found. */
4463 case OFPACT_CONTROLLER:
4464 controller = ofpact_get_CONTROLLER(a);
4465 execute_controller_action(ctx, controller->max_len,
4467 controller->controller_id);
4470 case OFPACT_ENQUEUE:
4471 memset(&wc->masks.skb_priority, 0xff,
4472 sizeof wc->masks.skb_priority);
4473 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
4476 case OFPACT_SET_VLAN_VID:
4477 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
4478 if (flow->vlan_tci & htons(VLAN_CFI) ||
4479 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
4480 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
4481 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
4486 case OFPACT_SET_VLAN_PCP:
4487 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
4488 if (flow->vlan_tci & htons(VLAN_CFI) ||
4489 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
4490 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
4491 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
4492 << VLAN_PCP_SHIFT) | VLAN_CFI);
4496 case OFPACT_STRIP_VLAN:
4497 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4498 flow->vlan_tci = htons(0);
4501 case OFPACT_PUSH_VLAN:
4502 /* XXX 802.1AD(QinQ) */
4503 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4504 flow->vlan_tci = htons(VLAN_CFI);
4507 case OFPACT_SET_ETH_SRC:
4508 WC_MASK_FIELD(wc, dl_src);
4509 flow->dl_src = ofpact_get_SET_ETH_SRC(a)->mac;
4512 case OFPACT_SET_ETH_DST:
4513 WC_MASK_FIELD(wc, dl_dst);
4514 flow->dl_dst = ofpact_get_SET_ETH_DST(a)->mac;
4517 case OFPACT_SET_IPV4_SRC:
4518 CHECK_MPLS_RECIRCULATION();
4519 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4520 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
4521 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
4525 case OFPACT_SET_IPV4_DST:
4526 CHECK_MPLS_RECIRCULATION();
4527 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4528 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
4529 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
4533 case OFPACT_SET_IP_DSCP:
4534 CHECK_MPLS_RECIRCULATION();
4535 if (is_ip_any(flow)) {
4536 wc->masks.nw_tos |= IP_DSCP_MASK;
4537 flow->nw_tos &= ~IP_DSCP_MASK;
4538 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
4542 case OFPACT_SET_IP_ECN:
4543 CHECK_MPLS_RECIRCULATION();
4544 if (is_ip_any(flow)) {
4545 wc->masks.nw_tos |= IP_ECN_MASK;
4546 flow->nw_tos &= ~IP_ECN_MASK;
4547 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
4551 case OFPACT_SET_IP_TTL:
4552 CHECK_MPLS_RECIRCULATION();
4553 if (is_ip_any(flow)) {
4554 wc->masks.nw_ttl = 0xff;
4555 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
4559 case OFPACT_SET_L4_SRC_PORT:
4560 CHECK_MPLS_RECIRCULATION();
4561 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4562 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4563 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
4564 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
4568 case OFPACT_SET_L4_DST_PORT:
4569 CHECK_MPLS_RECIRCULATION();
4570 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4571 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4572 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
4573 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
4577 case OFPACT_RESUBMIT:
4578 /* Recirculation complicates resubmit. There are two cases:
4580 * - If mpls_pop has been executed, then the flow table lookup
4581 * as part of resubmit might depend on fields that can only
4582 * be obtained via recirculation, so the resubmit itself
4583 * triggers recirculation and we need to make sure that the
4584 * resubmit is executed again after recirculation.
4585 * Therefore, in this case we trigger recirculation and let
4586 * the code following this "switch" append the resubmit to
4587 * the post-recirculation actions.
4589 * - Otherwise, some action in the flow entry found by resubmit
4590 * might trigger recirculation. If that happens, then we do
4591 * not want to execute the resubmit again after
4592 * recirculation, so we want to skip back to the head of the
4593 * loop to avoid that, only adding any actions that follow
4594 * the resubmit to the post-recirculation actions.
4596 if (ctx->was_mpls) {
4597 ctx_trigger_recirculation(ctx);
4600 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
4603 case OFPACT_SET_TUNNEL:
4604 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
4607 case OFPACT_SET_QUEUE:
4608 memset(&wc->masks.skb_priority, 0xff,
4609 sizeof wc->masks.skb_priority);
4610 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
4613 case OFPACT_POP_QUEUE:
4614 memset(&wc->masks.skb_priority, 0xff,
4615 sizeof wc->masks.skb_priority);
4616 flow->skb_priority = ctx->orig_skb_priority;
4619 case OFPACT_REG_MOVE:
4620 CHECK_MPLS_RECIRCULATION_IF(
4621 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
4622 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
4623 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
4626 case OFPACT_SET_FIELD:
4627 CHECK_MPLS_RECIRCULATION_IF(
4628 mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field));
4629 set_field = ofpact_get_SET_FIELD(a);
4630 mf = set_field->field;
4632 /* Set field action only ever overwrites packet's outermost
4633 * applicable header fields. Do nothing if no header exists. */
4634 if (mf->id == MFF_VLAN_VID) {
4635 wc->masks.vlan_tci |= htons(VLAN_CFI);
4636 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4639 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
4640 /* 'dl_type' is already unwildcarded. */
4641 && !eth_type_mpls(flow->dl_type)) {
4644 /* A flow may wildcard nw_frag. Do nothing if setting a transport
4645 * header field on a packet that does not have them. */
4646 mf_mask_field_and_prereqs(mf, wc);
4647 if (mf_are_prereqs_ok(mf, flow)) {
4648 mf_set_flow_value_masked(mf, &set_field->value,
4649 &set_field->mask, flow);
4653 case OFPACT_STACK_PUSH:
4654 CHECK_MPLS_RECIRCULATION_IF(
4655 mf_is_l3_or_higher(ofpact_get_STACK_PUSH(a)->subfield.field));
4656 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
4660 case OFPACT_STACK_POP:
4661 CHECK_MPLS_RECIRCULATION_IF(
4662 mf_is_l3_or_higher(ofpact_get_STACK_POP(a)->subfield.field));
4663 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
4667 case OFPACT_PUSH_MPLS:
4668 /* Recirculate if it is an IP packet with a zero ttl. This may
4669 * indicate that the packet was previously MPLS and an MPLS pop
4670 * action converted it to IP. In this case recirculating should
4671 * reveal the IP TTL which is used as the basis for a new MPLS
4673 CHECK_MPLS_RECIRCULATION_IF(
4674 !flow_count_mpls_labels(flow, wc)
4675 && flow->nw_ttl == 0
4676 && is_ip_any(flow));
4677 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
4680 case OFPACT_POP_MPLS:
4681 CHECK_MPLS_RECIRCULATION();
4682 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
4685 case OFPACT_SET_MPLS_LABEL:
4686 CHECK_MPLS_RECIRCULATION();
4687 compose_set_mpls_label_action(
4688 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
4691 case OFPACT_SET_MPLS_TC:
4692 CHECK_MPLS_RECIRCULATION();
4693 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
4696 case OFPACT_SET_MPLS_TTL:
4697 CHECK_MPLS_RECIRCULATION();
4698 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
4701 case OFPACT_DEC_MPLS_TTL:
4702 CHECK_MPLS_RECIRCULATION();
4703 if (compose_dec_mpls_ttl_action(ctx)) {
4708 case OFPACT_DEC_TTL:
4709 CHECK_MPLS_RECIRCULATION();
4710 wc->masks.nw_ttl = 0xff;
4711 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
4717 /* Nothing to do. */
4720 case OFPACT_MULTIPATH:
4721 CHECK_MPLS_RECIRCULATION();
4722 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
4726 CHECK_MPLS_RECIRCULATION();
4727 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
4730 case OFPACT_OUTPUT_REG:
4731 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
4735 CHECK_MPLS_RECIRCULATION();
4736 xlate_learn_action(ctx, ofpact_get_LEARN(a));
4739 case OFPACT_CONJUNCTION: {
4740 /* A flow with a "conjunction" action represents part of a special
4741 * kind of "set membership match". Such a flow should not actually
4742 * get executed, but it could via, say, a "packet-out", even though
4743 * that wouldn't be useful. Log it to help debugging. */
4744 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4745 VLOG_INFO_RL(&rl, "executing no-op conjunction action");
4753 case OFPACT_UNROLL_XLATE: {
4754 struct ofpact_unroll_xlate *unroll = ofpact_get_UNROLL_XLATE(a);
4756 /* Restore translation context data that was stored earlier. */
4757 ctx->table_id = unroll->rule_table_id;
4758 ctx->rule_cookie = unroll->rule_cookie;
4761 case OFPACT_FIN_TIMEOUT:
4762 CHECK_MPLS_RECIRCULATION();
4763 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4764 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
4767 case OFPACT_CLEAR_ACTIONS:
4768 ofpbuf_clear(&ctx->action_set);
4769 ctx->xin->flow.actset_output = OFPP_UNSET;
4770 ctx->action_set_has_group = false;
4773 case OFPACT_WRITE_ACTIONS:
4774 xlate_write_actions(ctx, ofpact_get_WRITE_ACTIONS(a));
4777 case OFPACT_WRITE_METADATA:
4778 metadata = ofpact_get_WRITE_METADATA(a);
4779 flow->metadata &= ~metadata->mask;
4780 flow->metadata |= metadata->metadata & metadata->mask;
4784 /* Not implemented yet. */
4787 case OFPACT_GOTO_TABLE: {
4788 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
4790 ovs_assert(ctx->table_id < ogt->table_id);
4792 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
4793 ogt->table_id, true, true);
4798 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
4802 CHECK_MPLS_RECIRCULATION();
4803 compose_conntrack_action(ctx, ofpact_get_CT(a));
4807 /* This will be processed by compose_conntrack_action(). */
4808 ctx->ct_nat_action = ofpact_get_NAT(a);
4811 case OFPACT_DEBUG_RECIRC:
4812 ctx_trigger_recirculation(ctx);
4817 /* Check if need to store this and the remaining actions for later
4819 if (!ctx->error && ctx->exit && ctx_first_recirculation_action(ctx)) {
4820 recirc_unroll_actions(a, ofpact_end(ofpacts, ofpacts_len), ctx);
4827 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
4828 const struct flow *flow, ofp_port_t in_port,
4829 struct rule_dpif *rule, uint16_t tcp_flags,
4830 const struct dp_packet *packet, struct flow_wildcards *wc,
4831 struct ofpbuf *odp_actions)
4833 xin->ofproto = ofproto;
4835 xin->flow.in_port.ofp_port = in_port;
4836 xin->flow.actset_output = OFPP_UNSET;
4837 xin->packet = packet;
4838 xin->may_learn = packet != NULL;
4841 xin->ofpacts = NULL;
4842 xin->ofpacts_len = 0;
4843 xin->tcp_flags = tcp_flags;
4844 xin->resubmit_hook = NULL;
4845 xin->report_hook = NULL;
4846 xin->resubmit_stats = NULL;
4850 xin->odp_actions = odp_actions;
4852 /* Do recirc lookup. */
4854 if (flow->recirc_id) {
4855 const struct recirc_id_node *node
4856 = recirc_id_node_find(flow->recirc_id);
4858 xin->recirc = &node->state;
4864 xlate_out_uninit(struct xlate_out *xout)
4867 recirc_refs_unref(&xout->recircs);
4871 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
4872 * into datapath actions, using 'ctx', and discards the datapath actions. */
4874 xlate_actions_for_side_effects(struct xlate_in *xin)
4876 struct xlate_out xout;
4877 enum xlate_error error;
4879 error = xlate_actions(xin, &xout);
4881 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4883 VLOG_WARN_RL(&rl, "xlate_actions failed (%s)!", xlate_strerror(error));
4886 xlate_out_uninit(&xout);
4889 static struct skb_priority_to_dscp *
4890 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
4892 struct skb_priority_to_dscp *pdscp;
4895 hash = hash_int(skb_priority, 0);
4896 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
4897 if (pdscp->skb_priority == skb_priority) {
4905 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
4908 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
4909 *dscp = pdscp ? pdscp->dscp : 0;
4910 return pdscp != NULL;
4914 count_skb_priorities(const struct xport *xport)
4916 return hmap_count(&xport->skb_priorities);
4920 clear_skb_priorities(struct xport *xport)
4922 struct skb_priority_to_dscp *pdscp, *next;
4924 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
4925 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
4931 actions_output_to_local_port(const struct xlate_ctx *ctx)
4933 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
4934 const struct nlattr *a;
4937 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->odp_actions->data,
4938 ctx->odp_actions->size) {
4939 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
4940 && nl_attr_get_odp_port(a) == local_odp_port) {
4947 #if defined(__linux__)
4948 /* Returns the maximum number of packets that the Linux kernel is willing to
4949 * queue up internally to certain kinds of software-implemented ports, or the
4950 * default (and rarely modified) value if it cannot be determined. */
4952 netdev_max_backlog(void)
4954 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
4955 static int max_backlog = 1000; /* The normal default value. */
4957 if (ovsthread_once_start(&once)) {
4958 static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
4962 stream = fopen(filename, "r");
4964 VLOG_INFO("%s: open failed (%s)", filename, ovs_strerror(errno));
4966 if (fscanf(stream, "%d", &n) != 1) {
4967 VLOG_WARN("%s: read error", filename);
4968 } else if (n <= 100) {
4969 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
4975 ovsthread_once_done(&once);
4977 VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
4983 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
4986 count_output_actions(const struct ofpbuf *odp_actions)
4988 const struct nlattr *a;
4992 NL_ATTR_FOR_EACH_UNSAFE (a, left, odp_actions->data, odp_actions->size) {
4993 if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
4999 #endif /* defined(__linux__) */
5001 /* Returns true if 'odp_actions' contains more output actions than the datapath
5002 * can reliably handle in one go. On Linux, this is the value of the
5003 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
5004 * packets that the kernel is willing to queue up for processing while the
5005 * datapath is processing a set of actions. */
5007 too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
5010 return (odp_actions->size / NL_A_U32_SIZE > netdev_max_backlog()
5011 && count_output_actions(odp_actions) > netdev_max_backlog());
5013 /* OSes other than Linux might have similar limits, but we don't know how
5014 * to determine them.*/
5020 xlate_wc_init(struct xlate_ctx *ctx)
5022 flow_wildcards_init_catchall(ctx->wc);
5024 /* Some fields we consider to always be examined. */
5025 WC_MASK_FIELD(ctx->wc, in_port);
5026 WC_MASK_FIELD(ctx->wc, dl_type);
5027 if (is_ip_any(&ctx->xin->flow)) {
5028 WC_MASK_FIELD_MASK(ctx->wc, nw_frag, FLOW_NW_FRAG_MASK);
5031 if (ctx->xbridge->support.odp.recirc) {
5032 /* Always exactly match recirc_id when datapath supports
5034 WC_MASK_FIELD(ctx->wc, recirc_id);
5037 if (ctx->xbridge->netflow) {
5038 netflow_mask_wc(&ctx->xin->flow, ctx->wc);
5041 tnl_wc_init(&ctx->xin->flow, ctx->wc);
5045 xlate_wc_finish(struct xlate_ctx *ctx)
5047 /* Clear the metadata and register wildcard masks, because we won't
5048 * use non-header fields as part of the cache. */
5049 flow_wildcards_clear_non_packet_fields(ctx->wc);
5051 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
5052 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
5053 * represent these fields. The datapath interface, on the other hand,
5054 * represents them with just 8 bits each. This means that if the high
5055 * 8 bits of the masks for these fields somehow become set, then they
5056 * will get chopped off by a round trip through the datapath, and
5057 * revalidation will spot that as an inconsistency and delete the flow.
5058 * Avoid the problem here by making sure that only the low 8 bits of
5059 * either field can be unwildcarded for ICMP.
5061 if (is_icmpv4(&ctx->xin->flow) || is_icmpv6(&ctx->xin->flow)) {
5062 ctx->wc->masks.tp_src &= htons(UINT8_MAX);
5063 ctx->wc->masks.tp_dst &= htons(UINT8_MAX);
5065 /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
5066 if (ctx->wc->masks.vlan_tci) {
5067 ctx->wc->masks.vlan_tci |= htons(VLAN_CFI);
5071 /* Translates the flow, actions, or rule in 'xin' into datapath actions in
5073 * The caller must take responsibility for eventually freeing 'xout', with
5074 * xlate_out_uninit().
5075 * Returns 'XLATE_OK' if translation was successful. In case of an error an
5076 * empty set of actions will be returned in 'xin->odp_actions' (if non-NULL),
5077 * so that most callers may ignore the return value and transparently install a
5078 * drop flow when the translation fails. */
5080 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
5082 *xout = (struct xlate_out) {
5084 .recircs = RECIRC_REFS_EMPTY_INITIALIZER,
5087 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5088 struct xbridge *xbridge = xbridge_lookup(xcfg, xin->ofproto);
5090 return XLATE_BRIDGE_NOT_FOUND;
5093 struct flow *flow = &xin->flow;
5095 union mf_subvalue stack_stub[1024 / sizeof(union mf_subvalue)];
5096 uint64_t action_set_stub[1024 / 8];
5097 struct flow_wildcards scratch_wc;
5098 uint64_t actions_stub[256 / 8];
5099 struct ofpbuf scratch_actions = OFPBUF_STUB_INITIALIZER(actions_stub);
5100 struct xlate_ctx ctx = {
5104 .orig_tunnel_ipv6_dst = flow_tnl_dst(&flow->tunnel),
5106 .stack = OFPBUF_STUB_INITIALIZER(stack_stub),
5108 .wc = xin->wc ? xin->wc : &scratch_wc,
5109 .odp_actions = xin->odp_actions ? xin->odp_actions : &scratch_actions,
5111 .recurse = xin->recurse,
5112 .resubmits = xin->resubmits,
5114 .in_action_set = false,
5117 .rule_cookie = OVS_BE64_MAX,
5118 .orig_skb_priority = flow->skb_priority,
5119 .sflow_n_outputs = 0,
5120 .sflow_odp_port = 0,
5121 .nf_output_iface = NF_OUT_DROP,
5126 .recirc_action_offset = -1,
5127 .last_unroll_offset = -1,
5130 .conntracked = false,
5132 .ct_nat_action = NULL,
5134 .action_set_has_group = false,
5135 .action_set = OFPBUF_STUB_INITIALIZER(action_set_stub),
5138 /* 'base_flow' reflects the packet as it came in, but we need it to reflect
5139 * the packet as the datapath will treat it for output actions:
5141 * - Our datapath doesn't retain tunneling information without us
5142 * re-setting it, so clear the tunnel data.
5144 * - For VLAN splinters, a higher layer may pretend that the packet
5145 * came in on 'flow->in_port.ofp_port' with 'flow->vlan_tci'
5146 * attached, because that's how we want to treat it from an OpenFlow
5147 * perspective. But from the datapath's perspective it actually came
5148 * in on a VLAN device without any VLAN attached. So here we put the
5149 * datapath's view of the VLAN information in 'base_flow' to ensure
5150 * correct treatment.
5152 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
5153 if (flow->in_port.ofp_port
5154 != vsp_realdev_to_vlandev(xbridge->ofproto,
5155 flow->in_port.ofp_port,
5157 ctx.base_flow.vlan_tci = 0;
5160 ofpbuf_reserve(ctx.odp_actions, NL_A_U32_SIZE);
5162 xlate_wc_init(&ctx);
5165 COVERAGE_INC(xlate_actions);
5168 const struct recirc_state *state = xin->recirc;
5170 xlate_report(&ctx, "Restoring state post-recirculation:");
5172 if (xin->ofpacts_len > 0 || ctx.rule) {
5173 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
5174 const char *conflict = xin->ofpacts_len ? "actions" : "rule";
5176 VLOG_WARN_RL(&rl, "Recirculation conflict (%s)!", conflict);
5177 xlate_report(&ctx, "- Recirculation conflict (%s)!", conflict);
5178 ctx.error = XLATE_RECIRCULATION_CONFLICT;
5182 /* Set the bridge for post-recirculation processing if needed. */
5183 if (!uuid_equals(ofproto_dpif_get_uuid(ctx.xbridge->ofproto),
5184 &state->ofproto_uuid)) {
5185 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5186 const struct xbridge *new_bridge
5187 = xbridge_lookup_by_uuid(xcfg, &state->ofproto_uuid);
5189 if (OVS_UNLIKELY(!new_bridge)) {
5190 /* Drop the packet if the bridge cannot be found. */
5191 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
5192 VLOG_WARN_RL(&rl, "Recirculation bridge no longer exists.");
5193 xlate_report(&ctx, "- Recirculation bridge no longer exists.");
5194 ctx.error = XLATE_BRIDGE_NOT_FOUND;
5197 ctx.xbridge = new_bridge;
5200 /* Set the post-recirculation table id. Note: A table lookup is done
5201 * only if there are no post-recirculation actions. */
5202 ctx.table_id = state->table_id;
5203 xlate_report(&ctx, "- Resuming from table %"PRIu8, ctx.table_id);
5205 if (!state->conntracked) {
5206 clear_conntrack(flow);
5209 /* Restore pipeline metadata. May change flow's in_port and other
5210 * metadata to the values that existed when recirculation was
5212 recirc_metadata_to_flow(&state->metadata, flow);
5214 /* Restore stack, if any. */
5216 ofpbuf_put(&ctx.stack, state->stack,
5217 state->n_stack * sizeof *state->stack);
5220 /* Restore mirror state. */
5221 ctx.mirrors = state->mirrors;
5223 /* Restore action set, if any. */
5224 if (state->action_set_len) {
5225 xlate_report_actions(&ctx, "- Restoring action set",
5226 state->action_set, state->action_set_len);
5228 flow->actset_output = OFPP_UNSET;
5229 xlate_write_actions__(&ctx, state->action_set,
5230 state->action_set_len);
5233 /* Restore recirculation actions. If there are no actions, processing
5234 * will start with a lookup in the table set above. */
5235 xin->ofpacts = state->ofpacts;
5236 xin->ofpacts_len = state->ofpacts_len;
5237 if (state->ofpacts_len) {
5238 xlate_report_actions(&ctx, "- Restoring actions",
5239 xin->ofpacts, xin->ofpacts_len);
5241 } else if (OVS_UNLIKELY(flow->recirc_id)) {
5242 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
5244 VLOG_WARN_RL(&rl, "Recirculation context not found for ID %"PRIx32,
5246 ctx.error = XLATE_NO_RECIRCULATION_CONTEXT;
5249 /* The bridge is now known so obtain its table version. */
5250 ctx.tables_version = ofproto_dpif_get_tables_version(ctx.xbridge->ofproto);
5252 if (!xin->ofpacts && !ctx.rule) {
5253 ctx.rule = rule_dpif_lookup_from_table(
5254 ctx.xbridge->ofproto, ctx.tables_version, flow, xin->wc,
5255 ctx.xin->resubmit_stats, &ctx.table_id,
5256 flow->in_port.ofp_port, true, true);
5257 if (ctx.xin->resubmit_stats) {
5258 rule_dpif_credit_stats(ctx.rule, ctx.xin->resubmit_stats);
5260 if (ctx.xin->xcache) {
5261 struct xc_entry *entry;
5263 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
5264 entry->u.rule = ctx.rule;
5265 rule_dpif_ref(ctx.rule);
5268 if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
5269 ctx.xin->resubmit_hook(ctx.xin, ctx.rule, 0);
5273 /* Get the proximate input port of the packet. (If xin->recirc,
5274 * flow->in_port is the ultimate input port of the packet.) */
5275 struct xport *in_port = get_ofp_port(xbridge,
5276 ctx.base_flow.in_port.ofp_port);
5278 /* Tunnel stats only for non-recirculated packets. */
5279 if (!xin->recirc && in_port && in_port->is_tunnel) {
5280 if (ctx.xin->resubmit_stats) {
5281 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
5283 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
5286 if (ctx.xin->xcache) {
5287 struct xc_entry *entry;
5289 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
5290 entry->u.dev.rx = netdev_ref(in_port->netdev);
5291 entry->u.dev.bfd = bfd_ref(in_port->bfd);
5295 if (!xin->recirc && process_special(&ctx, in_port)) {
5296 /* process_special() did all the processing for this packet.
5298 * We do not perform special processing on recirculated packets, as
5299 * recirculated packets are not really received by the bridge.*/
5300 } else if (in_port && in_port->xbundle
5301 && xbundle_mirror_out(xbridge, in_port->xbundle)) {
5302 if (ctx.xin->packet != NULL) {
5303 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5304 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
5305 "%s, which is reserved exclusively for mirroring",
5306 ctx.xbridge->name, in_port->xbundle->name);
5309 /* Sampling is done only for packets really received by the bridge. */
5310 unsigned int user_cookie_offset = 0;
5312 user_cookie_offset = compose_sflow_action(&ctx);
5313 compose_ipfix_action(&ctx, ODPP_NONE);
5315 size_t sample_actions_len = ctx.odp_actions->size;
5317 if (tnl_process_ecn(flow)
5318 && (!in_port || may_receive(in_port, &ctx))) {
5319 const struct ofpact *ofpacts;
5323 ofpacts = xin->ofpacts;
5324 ofpacts_len = xin->ofpacts_len;
5325 } else if (ctx.rule) {
5326 const struct rule_actions *actions
5327 = rule_dpif_get_actions(ctx.rule);
5328 ofpacts = actions->ofpacts;
5329 ofpacts_len = actions->ofpacts_len;
5330 ctx.rule_cookie = rule_dpif_get_flow_cookie(ctx.rule);
5335 mirror_ingress_packet(&ctx);
5336 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
5341 /* We've let OFPP_NORMAL and the learning action look at the
5342 * packet, so drop it now if forwarding is disabled. */
5343 if (in_port && (!xport_stp_forward_state(in_port) ||
5344 !xport_rstp_forward_state(in_port))) {
5345 /* Drop all actions added by do_xlate_actions() above. */
5346 ctx.odp_actions->size = sample_actions_len;
5348 /* Undo changes that may have been done for recirculation. */
5349 ctx_cancel_recirculation(&ctx);
5350 } else if (ctx.action_set.size) {
5351 /* Translate action set only if not dropping the packet and
5352 * not recirculating. */
5353 if (!exit_recirculates(&ctx)) {
5354 xlate_action_set(&ctx);
5357 /* Check if need to recirculate. */
5358 if (exit_recirculates(&ctx)) {
5359 compose_recirculate_action(&ctx);
5363 /* Output only fully processed packets. */
5364 if (!exit_recirculates(&ctx)
5365 && xbridge->has_in_band
5366 && in_band_must_output_to_local_port(flow)
5367 && !actions_output_to_local_port(&ctx)) {
5368 compose_output_action(&ctx, OFPP_LOCAL, NULL);
5371 if (user_cookie_offset) {
5372 fix_sflow_action(&ctx, user_cookie_offset);
5376 if (nl_attr_oversized(ctx.odp_actions->size)) {
5377 /* These datapath actions are too big for a Netlink attribute, so we
5378 * can't hand them to the kernel directly. dpif_execute() can execute
5379 * them one by one with help, so just mark the result as SLOW_ACTION to
5380 * prevent the flow from being installed. */
5381 COVERAGE_INC(xlate_actions_oversize);
5382 ctx.xout->slow |= SLOW_ACTION;
5383 } else if (too_many_output_actions(ctx.odp_actions)) {
5384 COVERAGE_INC(xlate_actions_too_many_output);
5385 ctx.xout->slow |= SLOW_ACTION;
5388 /* Do netflow only for packets really received by the bridge and not sent
5389 * to the controller. We consider packets sent to the controller to be
5390 * part of the control plane rather than the data plane. */
5391 if (!xin->recirc && xbridge->netflow && !(xout->slow & SLOW_CONTROLLER)) {
5392 if (ctx.xin->resubmit_stats) {
5393 netflow_flow_update(xbridge->netflow, flow,
5394 ctx.nf_output_iface,
5395 ctx.xin->resubmit_stats);
5397 if (ctx.xin->xcache) {
5398 struct xc_entry *entry;
5400 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
5401 entry->u.nf.netflow = netflow_ref(xbridge->netflow);
5402 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
5403 entry->u.nf.iface = ctx.nf_output_iface;
5408 xlate_wc_finish(&ctx);
5412 ofpbuf_uninit(&ctx.stack);
5413 ofpbuf_uninit(&ctx.action_set);
5414 ofpbuf_uninit(&scratch_actions);
5416 /* Make sure we return a "drop flow" in case of an error. */
5419 if (xin->odp_actions) {
5420 ofpbuf_clear(xin->odp_actions);
5426 /* Sends 'packet' out 'ofport'.
5427 * May modify 'packet'.
5428 * Returns 0 if successful, otherwise a positive errno value. */
5430 xlate_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
5432 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5433 struct xport *xport;
5434 struct ofpact_output output;
5437 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
5438 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
5439 flow_extract(packet, &flow);
5440 flow.in_port.ofp_port = OFPP_NONE;
5442 xport = xport_lookup(xcfg, ofport);
5446 output.port = xport->ofp_port;
5449 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
5450 &output.ofpact, sizeof output,
5454 struct xlate_cache *
5455 xlate_cache_new(void)
5457 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
5459 ofpbuf_init(&xcache->entries, 512);
5463 static struct xc_entry *
5464 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
5466 struct xc_entry *entry;
5468 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
5475 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
5477 if (entry->u.dev.tx) {
5478 netdev_vport_inc_tx(entry->u.dev.tx, stats);
5480 if (entry->u.dev.rx) {
5481 netdev_vport_inc_rx(entry->u.dev.rx, stats);
5483 if (entry->u.dev.bfd) {
5484 bfd_account_rx(entry->u.dev.bfd, stats);
5489 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
5491 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5492 struct xbridge *xbridge;
5493 struct xbundle *xbundle;
5494 struct flow_wildcards wc;
5496 xbridge = xbridge_lookup(xcfg, ofproto);
5501 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
5507 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
5510 /* Push stats and perform side effects of flow translation. */
5512 xlate_push_stats(struct xlate_cache *xcache,
5513 const struct dpif_flow_stats *stats)
5515 struct xc_entry *entry;
5516 struct ofpbuf entries = xcache->entries;
5517 struct eth_addr dmac;
5519 if (!stats->n_packets) {
5523 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5524 switch (entry->type) {
5526 rule_dpif_credit_stats(entry->u.rule, stats);
5529 bond_account(entry->u.bond.bond, entry->u.bond.flow,
5530 entry->u.bond.vid, stats->n_bytes);
5533 xlate_cache_netdev(entry, stats);
5536 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
5537 entry->u.nf.iface, stats);
5540 mirror_update_stats(entry->u.mirror.mbridge,
5541 entry->u.mirror.mirrors,
5542 stats->n_packets, stats->n_bytes);
5545 ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
5548 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
5549 entry->u.normal.vlan);
5551 case XC_FIN_TIMEOUT:
5552 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
5553 entry->u.fin.idle, entry->u.fin.hard);
5556 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
5560 /* Lookup neighbor to avoid timeout. */
5561 tnl_neigh_lookup(entry->u.tnl_neigh_cache.br_name,
5562 &entry->u.tnl_neigh_cache.d_ipv6, &dmac);
5571 xlate_dev_unref(struct xc_entry *entry)
5573 if (entry->u.dev.tx) {
5574 netdev_close(entry->u.dev.tx);
5576 if (entry->u.dev.rx) {
5577 netdev_close(entry->u.dev.rx);
5579 if (entry->u.dev.bfd) {
5580 bfd_unref(entry->u.dev.bfd);
5585 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
5587 netflow_flow_clear(netflow, flow);
5588 netflow_unref(netflow);
5593 xlate_cache_clear(struct xlate_cache *xcache)
5595 struct xc_entry *entry;
5596 struct ofpbuf entries;
5602 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5603 switch (entry->type) {
5605 rule_dpif_unref(entry->u.rule);
5608 free(entry->u.bond.flow);
5609 bond_unref(entry->u.bond.bond);
5612 xlate_dev_unref(entry);
5615 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
5618 mbridge_unref(entry->u.mirror.mbridge);
5621 free(entry->u.learn.fm);
5622 ofpbuf_delete(entry->u.learn.ofpacts);
5625 free(entry->u.normal.flow);
5627 case XC_FIN_TIMEOUT:
5628 /* 'u.fin.rule' is always already held as a XC_RULE, which
5629 * has already released it's reference above. */
5632 group_dpif_unref(entry->u.group.group);
5641 ofpbuf_clear(&xcache->entries);
5645 xlate_cache_delete(struct xlate_cache *xcache)
5647 xlate_cache_clear(xcache);
5648 ofpbuf_uninit(&xcache->entries);