1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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;
371 static void compose_recirculate_action(struct xlate_ctx *ctx);
373 /* A controller may use OFPP_NONE as the ingress port to indicate that
374 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
375 * when an input bundle is needed for validation (e.g., mirroring or
376 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
377 * any 'port' structs, so care must be taken when dealing with it. */
378 static struct xbundle ofpp_none_bundle = {
380 .vlan_mode = PORT_VLAN_TRUNK
383 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
384 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
385 * traffic egressing the 'ofport' with that priority should be marked with. */
386 struct skb_priority_to_dscp {
387 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
388 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
390 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
406 /* xlate_cache entries hold enough information to perform the side effects of
407 * xlate_actions() for a rule, without needing to perform rule translation
408 * from scratch. The primary usage of these is to submit statistics to objects
409 * that a flow relates to, although they may be used for other effects as well
410 * (for instance, refreshing hard timeouts for learned flows). */
414 struct rule_dpif *rule;
421 struct netflow *netflow;
426 struct mbridge *mbridge;
427 mirror_mask_t mirrors;
435 struct ofproto_dpif *ofproto;
436 struct ofputil_flow_mod *fm;
437 struct ofpbuf *ofpacts;
440 struct ofproto_dpif *ofproto;
445 struct rule_dpif *rule;
450 struct group_dpif *group;
451 struct ofputil_bucket *bucket;
454 char br_name[IFNAMSIZ];
455 struct in6_addr d_ipv6;
460 #define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
461 entries = xcache->entries; \
462 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
464 entry = ofpbuf_try_pull(&entries, sizeof *entry))
467 struct ofpbuf entries;
470 /* Xlate config contains hash maps of all bridges, bundles and ports.
471 * Xcfgp contains the pointer to the current xlate configuration.
472 * When the main thread needs to change the configuration, it copies xcfgp to
473 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
474 * does not block handler and revalidator threads. */
476 struct hmap xbridges;
477 struct hmap xbundles;
480 static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
481 static struct xlate_cfg *new_xcfg = NULL;
483 static bool may_receive(const struct xport *, struct xlate_ctx *);
484 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
486 static void xlate_normal(struct xlate_ctx *);
487 static inline void xlate_report(struct xlate_ctx *, const char *, ...)
488 OVS_PRINTF_FORMAT(2, 3);
489 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
490 uint8_t table_id, bool may_packet_in,
491 bool honor_table_miss);
492 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
493 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
494 static void output_normal(struct xlate_ctx *, const struct xbundle *,
497 /* Optional bond recirculation parameter to compose_output_action(). */
498 struct xlate_bond_recirc {
499 uint32_t recirc_id; /* !0 Use recirculation instead of output. */
500 uint8_t hash_alg; /* !0 Compute hash for recirc before. */
501 uint32_t hash_basis; /* Compute hash for recirc before. */
504 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port,
505 const struct xlate_bond_recirc *xr);
507 static struct xbridge *xbridge_lookup(struct xlate_cfg *,
508 const struct ofproto_dpif *);
509 static struct xbundle *xbundle_lookup(struct xlate_cfg *,
510 const struct ofbundle *);
511 static struct xport *xport_lookup(struct xlate_cfg *,
512 const struct ofport_dpif *);
513 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
514 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
515 uint32_t skb_priority);
516 static void clear_skb_priorities(struct xport *);
517 static size_t count_skb_priorities(const struct xport *);
518 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
521 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
523 static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
524 static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
525 static void xlate_xport_init(struct xlate_cfg *, struct xport *);
526 static void xlate_xbridge_set(struct xbridge *, struct dpif *,
527 const struct mac_learning *, struct stp *,
528 struct rstp *, const struct mcast_snooping *,
529 const struct mbridge *,
530 const struct dpif_sflow *,
531 const struct dpif_ipfix *,
532 const struct netflow *,
533 bool forward_bpdu, bool has_in_band,
534 const struct dpif_backer_support *);
535 static void xlate_xbundle_set(struct xbundle *xbundle,
536 enum port_vlan_mode vlan_mode, int vlan,
537 unsigned long *trunks, bool use_priority_tags,
538 const struct bond *bond, const struct lacp *lacp,
540 static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
541 const struct netdev *netdev, const struct cfm *cfm,
542 const struct bfd *bfd, const struct lldp *lldp,
543 int stp_port_no, const struct rstp_port *rstp_port,
544 enum ofputil_port_config config,
545 enum ofputil_port_state state, bool is_tunnel,
547 static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
548 static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
549 static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
550 static void xlate_xbridge_copy(struct xbridge *);
551 static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
552 static void xlate_xport_copy(struct xbridge *, struct xbundle *,
554 static void xlate_xcfg_free(struct xlate_cfg *);
557 xlate_report(struct xlate_ctx *ctx, const char *format, ...)
559 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
562 va_start(args, format);
563 ctx->xin->report_hook(ctx->xin, ctx->recurse, format, args);
568 static struct vlog_rate_limit error_report_rl = VLOG_RATE_LIMIT_INIT(1, 5);
570 #define XLATE_REPORT_ERROR(CTX, ...) \
572 if (OVS_UNLIKELY((CTX)->xin->report_hook)) { \
573 xlate_report(CTX, __VA_ARGS__); \
575 VLOG_ERR_RL(&error_report_rl, __VA_ARGS__); \
580 xlate_report_actions(struct xlate_ctx *ctx, const char *title,
581 const struct ofpact *ofpacts, size_t ofpacts_len)
583 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
584 struct ds s = DS_EMPTY_INITIALIZER;
585 ofpacts_format(ofpacts, ofpacts_len, &s);
586 xlate_report(ctx, "%s: %s", title, ds_cstr(&s));
592 xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
594 list_init(&xbridge->xbundles);
595 hmap_init(&xbridge->xports);
596 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
597 hash_pointer(xbridge->ofproto, 0));
601 xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
603 list_init(&xbundle->xports);
604 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
605 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
606 hash_pointer(xbundle->ofbundle, 0));
610 xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
612 hmap_init(&xport->skb_priorities);
613 hmap_insert(&xcfg->xports, &xport->hmap_node,
614 hash_pointer(xport->ofport, 0));
615 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
616 hash_ofp_port(xport->ofp_port));
620 xlate_xbridge_set(struct xbridge *xbridge,
622 const struct mac_learning *ml, struct stp *stp,
623 struct rstp *rstp, const struct mcast_snooping *ms,
624 const struct mbridge *mbridge,
625 const struct dpif_sflow *sflow,
626 const struct dpif_ipfix *ipfix,
627 const struct netflow *netflow,
628 bool forward_bpdu, bool has_in_band,
629 const struct dpif_backer_support *support)
631 if (xbridge->ml != ml) {
632 mac_learning_unref(xbridge->ml);
633 xbridge->ml = mac_learning_ref(ml);
636 if (xbridge->ms != ms) {
637 mcast_snooping_unref(xbridge->ms);
638 xbridge->ms = mcast_snooping_ref(ms);
641 if (xbridge->mbridge != mbridge) {
642 mbridge_unref(xbridge->mbridge);
643 xbridge->mbridge = mbridge_ref(mbridge);
646 if (xbridge->sflow != sflow) {
647 dpif_sflow_unref(xbridge->sflow);
648 xbridge->sflow = dpif_sflow_ref(sflow);
651 if (xbridge->ipfix != ipfix) {
652 dpif_ipfix_unref(xbridge->ipfix);
653 xbridge->ipfix = dpif_ipfix_ref(ipfix);
656 if (xbridge->stp != stp) {
657 stp_unref(xbridge->stp);
658 xbridge->stp = stp_ref(stp);
661 if (xbridge->rstp != rstp) {
662 rstp_unref(xbridge->rstp);
663 xbridge->rstp = rstp_ref(rstp);
666 if (xbridge->netflow != netflow) {
667 netflow_unref(xbridge->netflow);
668 xbridge->netflow = netflow_ref(netflow);
671 xbridge->dpif = dpif;
672 xbridge->forward_bpdu = forward_bpdu;
673 xbridge->has_in_band = has_in_band;
674 xbridge->support = *support;
678 xlate_xbundle_set(struct xbundle *xbundle,
679 enum port_vlan_mode vlan_mode, int vlan,
680 unsigned long *trunks, bool use_priority_tags,
681 const struct bond *bond, const struct lacp *lacp,
684 ovs_assert(xbundle->xbridge);
686 xbundle->vlan_mode = vlan_mode;
687 xbundle->vlan = vlan;
688 xbundle->trunks = trunks;
689 xbundle->use_priority_tags = use_priority_tags;
690 xbundle->floodable = floodable;
692 if (xbundle->bond != bond) {
693 bond_unref(xbundle->bond);
694 xbundle->bond = bond_ref(bond);
697 if (xbundle->lacp != lacp) {
698 lacp_unref(xbundle->lacp);
699 xbundle->lacp = lacp_ref(lacp);
704 xlate_xport_set(struct xport *xport, odp_port_t odp_port,
705 const struct netdev *netdev, const struct cfm *cfm,
706 const struct bfd *bfd, const struct lldp *lldp, int stp_port_no,
707 const struct rstp_port* rstp_port,
708 enum ofputil_port_config config, enum ofputil_port_state state,
709 bool is_tunnel, bool may_enable)
711 xport->config = config;
712 xport->state = state;
713 xport->stp_port_no = stp_port_no;
714 xport->is_tunnel = is_tunnel;
715 xport->may_enable = may_enable;
716 xport->odp_port = odp_port;
718 if (xport->rstp_port != rstp_port) {
719 rstp_port_unref(xport->rstp_port);
720 xport->rstp_port = rstp_port_ref(rstp_port);
723 if (xport->cfm != cfm) {
724 cfm_unref(xport->cfm);
725 xport->cfm = cfm_ref(cfm);
728 if (xport->bfd != bfd) {
729 bfd_unref(xport->bfd);
730 xport->bfd = bfd_ref(bfd);
733 if (xport->lldp != lldp) {
734 lldp_unref(xport->lldp);
735 xport->lldp = lldp_ref(lldp);
738 if (xport->netdev != netdev) {
739 netdev_close(xport->netdev);
740 xport->netdev = netdev_ref(netdev);
745 xlate_xbridge_copy(struct xbridge *xbridge)
747 struct xbundle *xbundle;
749 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
750 new_xbridge->ofproto = xbridge->ofproto;
751 new_xbridge->name = xstrdup(xbridge->name);
752 xlate_xbridge_init(new_xcfg, new_xbridge);
754 xlate_xbridge_set(new_xbridge,
755 xbridge->dpif, xbridge->ml, xbridge->stp,
756 xbridge->rstp, xbridge->ms, xbridge->mbridge,
757 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
758 xbridge->forward_bpdu, xbridge->has_in_band,
760 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
761 xlate_xbundle_copy(new_xbridge, xbundle);
764 /* Copy xports which are not part of a xbundle */
765 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
766 if (!xport->xbundle) {
767 xlate_xport_copy(new_xbridge, NULL, xport);
773 xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
776 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
777 new_xbundle->ofbundle = xbundle->ofbundle;
778 new_xbundle->xbridge = xbridge;
779 new_xbundle->name = xstrdup(xbundle->name);
780 xlate_xbundle_init(new_xcfg, new_xbundle);
782 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
783 xbundle->vlan, xbundle->trunks,
784 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
786 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
787 xlate_xport_copy(xbridge, new_xbundle, xport);
792 xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
795 struct skb_priority_to_dscp *pdscp, *new_pdscp;
796 struct xport *new_xport = xzalloc(sizeof *xport);
797 new_xport->ofport = xport->ofport;
798 new_xport->ofp_port = xport->ofp_port;
799 new_xport->xbridge = xbridge;
800 xlate_xport_init(new_xcfg, new_xport);
802 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
803 xport->bfd, xport->lldp, xport->stp_port_no,
804 xport->rstp_port, xport->config, xport->state,
805 xport->is_tunnel, xport->may_enable);
808 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
810 new_xport->peer = peer;
811 new_xport->peer->peer = new_xport;
816 new_xport->xbundle = xbundle;
817 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
820 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
821 new_pdscp = xmalloc(sizeof *pdscp);
822 new_pdscp->skb_priority = pdscp->skb_priority;
823 new_pdscp->dscp = pdscp->dscp;
824 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
825 hash_int(new_pdscp->skb_priority, 0));
829 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
830 * configuration in xcfgp.
832 * This needs to be called after editing the xlate configuration.
834 * Functions that edit the new xlate configuration are
835 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
841 * edit_xlate_configuration();
843 * xlate_txn_commit(); */
845 xlate_txn_commit(void)
847 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
849 ovsrcu_set(&xcfgp, new_xcfg);
850 ovsrcu_synchronize();
851 xlate_xcfg_free(xcfg);
855 /* Copies the current xlate configuration in xcfgp to new_xcfg.
857 * This needs to be called prior to editing the xlate configuration. */
859 xlate_txn_start(void)
861 struct xbridge *xbridge;
862 struct xlate_cfg *xcfg;
864 ovs_assert(!new_xcfg);
866 new_xcfg = xmalloc(sizeof *new_xcfg);
867 hmap_init(&new_xcfg->xbridges);
868 hmap_init(&new_xcfg->xbundles);
869 hmap_init(&new_xcfg->xports);
871 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
876 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
877 xlate_xbridge_copy(xbridge);
883 xlate_xcfg_free(struct xlate_cfg *xcfg)
885 struct xbridge *xbridge, *next_xbridge;
891 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
892 xlate_xbridge_remove(xcfg, xbridge);
895 hmap_destroy(&xcfg->xbridges);
896 hmap_destroy(&xcfg->xbundles);
897 hmap_destroy(&xcfg->xports);
902 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
904 const struct mac_learning *ml, struct stp *stp,
905 struct rstp *rstp, const struct mcast_snooping *ms,
906 const struct mbridge *mbridge,
907 const struct dpif_sflow *sflow,
908 const struct dpif_ipfix *ipfix,
909 const struct netflow *netflow,
910 bool forward_bpdu, bool has_in_band,
911 const struct dpif_backer_support *support)
913 struct xbridge *xbridge;
915 ovs_assert(new_xcfg);
917 xbridge = xbridge_lookup(new_xcfg, ofproto);
919 xbridge = xzalloc(sizeof *xbridge);
920 xbridge->ofproto = ofproto;
922 xlate_xbridge_init(new_xcfg, xbridge);
926 xbridge->name = xstrdup(name);
928 xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
929 netflow, forward_bpdu, has_in_band, support);
933 xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
935 struct xbundle *xbundle, *next_xbundle;
936 struct xport *xport, *next_xport;
942 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
943 xlate_xport_remove(xcfg, xport);
946 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
947 xlate_xbundle_remove(xcfg, xbundle);
950 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
951 mac_learning_unref(xbridge->ml);
952 mcast_snooping_unref(xbridge->ms);
953 mbridge_unref(xbridge->mbridge);
954 dpif_sflow_unref(xbridge->sflow);
955 dpif_ipfix_unref(xbridge->ipfix);
956 stp_unref(xbridge->stp);
957 rstp_unref(xbridge->rstp);
958 hmap_destroy(&xbridge->xports);
964 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
966 struct xbridge *xbridge;
968 ovs_assert(new_xcfg);
970 xbridge = xbridge_lookup(new_xcfg, ofproto);
971 xlate_xbridge_remove(new_xcfg, xbridge);
975 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
976 const char *name, enum port_vlan_mode vlan_mode, int vlan,
977 unsigned long *trunks, bool use_priority_tags,
978 const struct bond *bond, const struct lacp *lacp,
981 struct xbundle *xbundle;
983 ovs_assert(new_xcfg);
985 xbundle = xbundle_lookup(new_xcfg, ofbundle);
987 xbundle = xzalloc(sizeof *xbundle);
988 xbundle->ofbundle = ofbundle;
989 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
991 xlate_xbundle_init(new_xcfg, xbundle);
995 xbundle->name = xstrdup(name);
997 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
998 use_priority_tags, bond, lacp, floodable);
1002 xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
1004 struct xport *xport;
1010 LIST_FOR_EACH_POP (xport, bundle_node, &xbundle->xports) {
1011 xport->xbundle = NULL;
1014 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
1015 list_remove(&xbundle->list_node);
1016 bond_unref(xbundle->bond);
1017 lacp_unref(xbundle->lacp);
1018 free(xbundle->name);
1023 xlate_bundle_remove(struct ofbundle *ofbundle)
1025 struct xbundle *xbundle;
1027 ovs_assert(new_xcfg);
1029 xbundle = xbundle_lookup(new_xcfg, ofbundle);
1030 xlate_xbundle_remove(new_xcfg, xbundle);
1034 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
1035 struct ofport_dpif *ofport, ofp_port_t ofp_port,
1036 odp_port_t odp_port, const struct netdev *netdev,
1037 const struct cfm *cfm, const struct bfd *bfd,
1038 const struct lldp *lldp, struct ofport_dpif *peer,
1039 int stp_port_no, const struct rstp_port *rstp_port,
1040 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
1041 enum ofputil_port_config config,
1042 enum ofputil_port_state state, bool is_tunnel,
1046 struct xport *xport;
1048 ovs_assert(new_xcfg);
1050 xport = xport_lookup(new_xcfg, ofport);
1052 xport = xzalloc(sizeof *xport);
1053 xport->ofport = ofport;
1054 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
1055 xport->ofp_port = ofp_port;
1057 xlate_xport_init(new_xcfg, xport);
1060 ovs_assert(xport->ofp_port == ofp_port);
1062 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, lldp,
1063 stp_port_no, rstp_port, config, state, is_tunnel,
1067 xport->peer->peer = NULL;
1069 xport->peer = xport_lookup(new_xcfg, peer);
1071 xport->peer->peer = xport;
1074 if (xport->xbundle) {
1075 list_remove(&xport->bundle_node);
1077 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
1078 if (xport->xbundle) {
1079 list_insert(&xport->xbundle->xports, &xport->bundle_node);
1082 clear_skb_priorities(xport);
1083 for (i = 0; i < n_qdscp; i++) {
1084 struct skb_priority_to_dscp *pdscp;
1085 uint32_t skb_priority;
1087 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
1092 pdscp = xmalloc(sizeof *pdscp);
1093 pdscp->skb_priority = skb_priority;
1094 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1095 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
1096 hash_int(pdscp->skb_priority, 0));
1101 xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
1108 xport->peer->peer = NULL;
1112 if (xport->xbundle) {
1113 list_remove(&xport->bundle_node);
1116 clear_skb_priorities(xport);
1117 hmap_destroy(&xport->skb_priorities);
1119 hmap_remove(&xcfg->xports, &xport->hmap_node);
1120 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
1122 netdev_close(xport->netdev);
1123 rstp_port_unref(xport->rstp_port);
1124 cfm_unref(xport->cfm);
1125 bfd_unref(xport->bfd);
1126 lldp_unref(xport->lldp);
1131 xlate_ofport_remove(struct ofport_dpif *ofport)
1133 struct xport *xport;
1135 ovs_assert(new_xcfg);
1137 xport = xport_lookup(new_xcfg, ofport);
1138 xlate_xport_remove(new_xcfg, xport);
1141 static struct ofproto_dpif *
1142 xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
1143 ofp_port_t *ofp_in_port, const struct xport **xportp)
1145 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1146 const struct xport *xport;
1148 xport = xport_lookup(xcfg, tnl_port_should_receive(flow)
1149 ? tnl_port_receive(flow)
1150 : odp_port_to_ofport(backer, flow->in_port.odp_port));
1151 if (OVS_UNLIKELY(!xport)) {
1156 *ofp_in_port = xport->ofp_port;
1158 return xport->xbridge->ofproto;
1161 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1162 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1163 struct ofproto_dpif *
1164 xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1165 ofp_port_t *ofp_in_port)
1167 const struct xport *xport;
1169 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1172 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1173 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
1174 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1175 * handles for those protocols if they're enabled. Caller may use the returned
1176 * pointers until quiescing, for longer term use additional references must
1179 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1182 xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1183 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1184 struct dpif_sflow **sflow, struct netflow **netflow,
1185 ofp_port_t *ofp_in_port)
1187 struct ofproto_dpif *ofproto;
1188 const struct xport *xport;
1190 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1197 *ofprotop = ofproto;
1201 *ipfix = xport ? xport->xbridge->ipfix : NULL;
1205 *sflow = xport ? xport->xbridge->sflow : NULL;
1209 *netflow = xport ? xport->xbridge->netflow : NULL;
1215 static struct xbridge *
1216 xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
1218 struct hmap *xbridges;
1219 struct xbridge *xbridge;
1221 if (!ofproto || !xcfg) {
1225 xbridges = &xcfg->xbridges;
1227 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
1229 if (xbridge->ofproto == ofproto) {
1236 static struct xbundle *
1237 xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
1239 struct hmap *xbundles;
1240 struct xbundle *xbundle;
1242 if (!ofbundle || !xcfg) {
1246 xbundles = &xcfg->xbundles;
1248 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
1250 if (xbundle->ofbundle == ofbundle) {
1257 static struct xport *
1258 xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
1260 struct hmap *xports;
1261 struct xport *xport;
1263 if (!ofport || !xcfg) {
1267 xports = &xcfg->xports;
1269 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
1271 if (xport->ofport == ofport) {
1278 static struct stp_port *
1279 xport_get_stp_port(const struct xport *xport)
1281 return xport->xbridge->stp && xport->stp_port_no != -1
1282 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1287 xport_stp_learn_state(const struct xport *xport)
1289 struct stp_port *sp = xport_get_stp_port(xport);
1291 ? stp_learn_in_state(stp_port_get_state(sp))
1296 xport_stp_forward_state(const struct xport *xport)
1298 struct stp_port *sp = xport_get_stp_port(xport);
1300 ? stp_forward_in_state(stp_port_get_state(sp))
1305 xport_stp_should_forward_bpdu(const struct xport *xport)
1307 struct stp_port *sp = xport_get_stp_port(xport);
1308 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
1311 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1312 * were used to make the determination.*/
1314 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1316 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1317 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1318 return is_stp(flow);
1322 stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1324 struct stp_port *sp = xport_get_stp_port(xport);
1325 struct dp_packet payload = *packet;
1326 struct eth_header *eth = dp_packet_data(&payload);
1328 /* Sink packets on ports that have STP disabled when the bridge has
1330 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1334 /* Trim off padding on payload. */
1335 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1336 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1339 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1340 stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
1344 static enum rstp_state
1345 xport_get_rstp_port_state(const struct xport *xport)
1347 return xport->rstp_port
1348 ? rstp_port_get_state(xport->rstp_port)
1353 xport_rstp_learn_state(const struct xport *xport)
1355 return xport->xbridge->rstp && xport->rstp_port
1356 ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1361 xport_rstp_forward_state(const struct xport *xport)
1363 return xport->xbridge->rstp && xport->rstp_port
1364 ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1369 xport_rstp_should_manage_bpdu(const struct xport *xport)
1371 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
1375 rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1377 struct dp_packet payload = *packet;
1378 struct eth_header *eth = dp_packet_data(&payload);
1380 /* Sink packets on ports that have no RSTP. */
1381 if (!xport->rstp_port) {
1385 /* Trim off padding on payload. */
1386 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1387 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1390 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1391 rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
1392 dp_packet_size(&payload));
1396 static struct xport *
1397 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1399 struct xport *xport;
1401 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1403 if (xport->ofp_port == ofp_port) {
1411 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1413 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1414 return xport ? xport->odp_port : ODPP_NONE;
1418 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1420 struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1421 return xport && xport->may_enable;
1424 static struct ofputil_bucket *
1425 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1429 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1431 struct group_dpif *group;
1433 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1434 struct ofputil_bucket *bucket;
1436 bucket = group_first_live_bucket(ctx, group, depth);
1437 group_dpif_unref(group);
1438 return bucket == NULL;
1444 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1447 bucket_is_alive(const struct xlate_ctx *ctx,
1448 struct ofputil_bucket *bucket, int depth)
1450 if (depth >= MAX_LIVENESS_RECURSION) {
1451 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1453 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1454 MAX_LIVENESS_RECURSION);
1458 return (!ofputil_bucket_has_liveness(bucket)
1459 || (bucket->watch_port != OFPP_ANY
1460 && odp_port_is_alive(ctx, bucket->watch_port))
1461 || (bucket->watch_group != OFPG_ANY
1462 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
1465 static struct ofputil_bucket *
1466 group_first_live_bucket(const struct xlate_ctx *ctx,
1467 const struct group_dpif *group, int depth)
1469 struct ofputil_bucket *bucket;
1470 const struct ovs_list *buckets;
1472 group_dpif_get_buckets(group, &buckets);
1473 LIST_FOR_EACH (bucket, list_node, buckets) {
1474 if (bucket_is_alive(ctx, bucket, depth)) {
1482 static struct ofputil_bucket *
1483 group_best_live_bucket(const struct xlate_ctx *ctx,
1484 const struct group_dpif *group,
1487 struct ofputil_bucket *best_bucket = NULL;
1488 uint32_t best_score = 0;
1491 struct ofputil_bucket *bucket;
1492 const struct ovs_list *buckets;
1494 group_dpif_get_buckets(group, &buckets);
1495 LIST_FOR_EACH (bucket, list_node, buckets) {
1496 if (bucket_is_alive(ctx, bucket, 0)) {
1497 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
1498 if (score >= best_score) {
1499 best_bucket = bucket;
1510 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
1512 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1513 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1517 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1519 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1522 static mirror_mask_t
1523 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1525 return xbundle != &ofpp_none_bundle
1526 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1530 static mirror_mask_t
1531 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
1533 return xbundle != &ofpp_none_bundle
1534 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1538 static mirror_mask_t
1539 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
1541 return xbundle != &ofpp_none_bundle
1542 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1546 static struct xbundle *
1547 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1548 bool warn, struct xport **in_xportp)
1550 struct xport *xport;
1552 /* Find the port and bundle for the received packet. */
1553 xport = get_ofp_port(xbridge, in_port);
1557 if (xport && xport->xbundle) {
1558 return xport->xbundle;
1561 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1562 * which a controller may use as the ingress port for traffic that
1563 * it is sourcing. */
1564 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
1565 return &ofpp_none_bundle;
1568 /* Odd. A few possible reasons here:
1570 * - We deleted a port but there are still a few packets queued up
1573 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1574 * we don't know about.
1576 * - The ofproto client didn't configure the port as part of a bundle.
1577 * This is particularly likely to happen if a packet was received on the
1578 * port after it was created, but before the client had a chance to
1579 * configure its bundle.
1582 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1584 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1585 "port %"PRIu16, xbridge->name, in_port);
1591 mirror_packet(struct xlate_ctx *ctx, struct xbundle *xbundle,
1592 mirror_mask_t mirrors)
1594 bool warn = ctx->xin->packet != NULL;
1595 uint16_t vid = vlan_tci_to_vid(ctx->xin->flow.vlan_tci);
1596 if (!input_vid_is_valid(vid, xbundle, warn)) {
1599 uint16_t vlan = input_vid_to_vlan(xbundle, vid);
1601 const struct xbridge *xbridge = ctx->xbridge;
1603 /* Don't mirror to destinations that we've already mirrored to. */
1604 mirrors &= ~ctx->mirrors;
1609 /* Record these mirrors so that we don't mirror to them again. */
1610 ctx->mirrors |= mirrors;
1612 if (ctx->xin->resubmit_stats) {
1613 mirror_update_stats(xbridge->mbridge, mirrors,
1614 ctx->xin->resubmit_stats->n_packets,
1615 ctx->xin->resubmit_stats->n_bytes);
1617 if (ctx->xin->xcache) {
1618 struct xc_entry *entry;
1620 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_MIRROR);
1621 entry->u.mirror.mbridge = mbridge_ref(xbridge->mbridge);
1622 entry->u.mirror.mirrors = mirrors;
1626 const unsigned long *vlans;
1627 mirror_mask_t dup_mirrors;
1628 struct ofbundle *out;
1631 bool has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1632 &vlans, &dup_mirrors, &out, &out_vlan);
1633 ovs_assert(has_mirror);
1636 ctx->wc->masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1639 if (vlans && !bitmap_is_set(vlans, vlan)) {
1640 mirrors = zero_rightmost_1bit(mirrors);
1644 mirrors &= ~dup_mirrors;
1645 ctx->mirrors |= dup_mirrors;
1647 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1648 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1650 output_normal(ctx, out_xbundle, vlan);
1652 } else if (vlan != out_vlan
1653 && !eth_addr_is_reserved(ctx->xin->flow.dl_dst)) {
1654 struct xbundle *xbundle;
1656 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1657 if (xbundle_includes_vlan(xbundle, out_vlan)
1658 && !xbundle_mirror_out(xbridge, xbundle)) {
1659 output_normal(ctx, xbundle, out_vlan);
1667 mirror_ingress_packet(struct xlate_ctx *ctx)
1669 if (mbridge_has_mirrors(ctx->xbridge->mbridge)) {
1670 bool warn = ctx->xin->packet != NULL;
1671 struct xbundle *xbundle = lookup_input_bundle(
1672 ctx->xbridge, ctx->xin->flow.in_port.ofp_port, warn, NULL);
1674 mirror_packet(ctx, xbundle,
1675 xbundle_mirror_src(ctx->xbridge, xbundle));
1680 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1681 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1682 * the bundle on which the packet was received, returns the VLAN to which the
1685 * Both 'vid' and the return value are in the range 0...4095. */
1687 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1689 switch (in_xbundle->vlan_mode) {
1690 case PORT_VLAN_ACCESS:
1691 return in_xbundle->vlan;
1694 case PORT_VLAN_TRUNK:
1697 case PORT_VLAN_NATIVE_UNTAGGED:
1698 case PORT_VLAN_NATIVE_TAGGED:
1699 return vid ? vid : in_xbundle->vlan;
1706 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1707 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1710 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1711 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1714 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1716 /* Allow any VID on the OFPP_NONE port. */
1717 if (in_xbundle == &ofpp_none_bundle) {
1721 switch (in_xbundle->vlan_mode) {
1722 case PORT_VLAN_ACCESS:
1725 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1726 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1727 "packet received on port %s configured as VLAN "
1728 "%"PRIu16" access port", vid, in_xbundle->name,
1735 case PORT_VLAN_NATIVE_UNTAGGED:
1736 case PORT_VLAN_NATIVE_TAGGED:
1738 /* Port must always carry its native VLAN. */
1742 case PORT_VLAN_TRUNK:
1743 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1745 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1746 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1747 "received on port %s not configured for trunking "
1748 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1760 /* Given 'vlan', the VLAN that a packet belongs to, and
1761 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1762 * that should be included in the 802.1Q header. (If the return value is 0,
1763 * then the 802.1Q header should only be included in the packet if there is a
1766 * Both 'vlan' and the return value are in the range 0...4095. */
1768 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1770 switch (out_xbundle->vlan_mode) {
1771 case PORT_VLAN_ACCESS:
1774 case PORT_VLAN_TRUNK:
1775 case PORT_VLAN_NATIVE_TAGGED:
1778 case PORT_VLAN_NATIVE_UNTAGGED:
1779 return vlan == out_xbundle->vlan ? 0 : vlan;
1787 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1790 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1792 ovs_be16 tci, old_tci;
1793 struct xport *xport;
1794 struct xlate_bond_recirc xr;
1795 bool use_recirc = false;
1797 vid = output_vlan_to_vid(out_xbundle, vlan);
1798 if (list_is_empty(&out_xbundle->xports)) {
1799 /* Partially configured bundle with no slaves. Drop the packet. */
1801 } else if (!out_xbundle->bond) {
1802 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1805 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1806 struct flow_wildcards *wc = ctx->wc;
1807 struct ofport_dpif *ofport;
1809 if (ctx->xbridge->support.odp.recirc) {
1810 use_recirc = bond_may_recirc(
1811 out_xbundle->bond, &xr.recirc_id, &xr.hash_basis);
1814 /* Only TCP mode uses recirculation. */
1815 xr.hash_alg = OVS_HASH_ALG_L4;
1816 bond_update_post_recirc_rules(out_xbundle->bond, false);
1818 /* Recirculation does not require unmasking hash fields. */
1823 ofport = bond_choose_output_slave(out_xbundle->bond,
1824 &ctx->xin->flow, wc, vid);
1825 xport = xport_lookup(xcfg, ofport);
1828 /* No slaves enabled, so drop packet. */
1832 /* If use_recirc is set, the main thread will handle stats
1833 * accounting for this bond. */
1835 if (ctx->xin->resubmit_stats) {
1836 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1837 ctx->xin->resubmit_stats->n_bytes);
1839 if (ctx->xin->xcache) {
1840 struct xc_entry *entry;
1843 flow = &ctx->xin->flow;
1844 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1845 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1846 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1847 entry->u.bond.vid = vid;
1852 old_tci = *flow_tci;
1854 if (tci || out_xbundle->use_priority_tags) {
1855 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1857 tci |= htons(VLAN_CFI);
1862 compose_output_action(ctx, xport->ofp_port, use_recirc ? &xr : NULL);
1863 *flow_tci = old_tci;
1866 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1867 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1868 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1870 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1872 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1876 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1877 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1881 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1882 if (flow->nw_proto == ARP_OP_REPLY) {
1884 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1885 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1886 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1888 return flow->nw_src == flow->nw_dst;
1894 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1895 * dropped. Returns true if they may be forwarded, false if they should be
1898 * 'in_port' must be the xport that corresponds to flow->in_port.
1899 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1901 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1902 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1903 * checked by input_vid_is_valid().
1905 * May also add tags to '*tags', although the current implementation only does
1906 * so in one special case.
1909 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1912 struct xbundle *in_xbundle = in_port->xbundle;
1913 const struct xbridge *xbridge = ctx->xbridge;
1914 struct flow *flow = &ctx->xin->flow;
1916 /* Drop frames for reserved multicast addresses
1917 * only if forward_bpdu option is absent. */
1918 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1919 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1923 if (in_xbundle->bond) {
1924 struct mac_entry *mac;
1926 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1932 xlate_report(ctx, "bonding refused admissibility, dropping");
1935 case BV_DROP_IF_MOVED:
1936 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1937 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1939 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
1940 && (!is_gratuitous_arp(flow, ctx->wc)
1941 || mac_entry_is_grat_arp_locked(mac))) {
1942 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1943 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1947 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1955 /* Checks whether a MAC learning update is necessary for MAC learning table
1956 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1959 * Most packets processed through the MAC learning table do not actually
1960 * change it in any way. This function requires only a read lock on the MAC
1961 * learning table, so it is much cheaper in this common case.
1963 * Keep the code here synchronized with that in update_learning_table__()
1966 is_mac_learning_update_needed(const struct mac_learning *ml,
1967 const struct flow *flow,
1968 struct flow_wildcards *wc,
1969 int vlan, struct xbundle *in_xbundle)
1970 OVS_REQ_RDLOCK(ml->rwlock)
1972 struct mac_entry *mac;
1974 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1978 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1979 if (!mac || mac_entry_age(ml, mac)) {
1983 if (is_gratuitous_arp(flow, wc)) {
1984 /* We don't want to learn from gratuitous ARP packets that are
1985 * reflected back over bond slaves so we lock the learning table. */
1986 if (!in_xbundle->bond) {
1988 } else if (mac_entry_is_grat_arp_locked(mac)) {
1993 return mac_entry_get_port(ml, mac) != in_xbundle->ofbundle;
1997 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1998 * received on 'in_xbundle' in 'vlan'.
2000 * This code repeats all the checks in is_mac_learning_update_needed() because
2001 * the lock was released between there and here and thus the MAC learning state
2002 * could have changed.
2004 * Keep the code here synchronized with that in is_mac_learning_update_needed()
2007 update_learning_table__(const struct xbridge *xbridge,
2008 const struct flow *flow, struct flow_wildcards *wc,
2009 int vlan, struct xbundle *in_xbundle)
2010 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
2012 struct mac_entry *mac;
2014 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
2018 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
2019 if (is_gratuitous_arp(flow, wc)) {
2020 /* We don't want to learn from gratuitous ARP packets that are
2021 * reflected back over bond slaves so we lock the learning table. */
2022 if (!in_xbundle->bond) {
2023 mac_entry_set_grat_arp_lock(mac);
2024 } else if (mac_entry_is_grat_arp_locked(mac)) {
2029 if (mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle) {
2030 /* The log messages here could actually be useful in debugging,
2031 * so keep the rate limit relatively high. */
2032 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
2034 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
2035 "on port %s in VLAN %d",
2036 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
2037 in_xbundle->name, vlan);
2039 mac_entry_set_port(xbridge->ml, mac, in_xbundle->ofbundle);
2044 update_learning_table(const struct xbridge *xbridge,
2045 const struct flow *flow, struct flow_wildcards *wc,
2046 int vlan, struct xbundle *in_xbundle)
2050 /* Don't learn the OFPP_NONE port. */
2051 if (in_xbundle == &ofpp_none_bundle) {
2055 /* First try the common case: no change to MAC learning table. */
2056 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
2057 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
2059 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2062 /* Slow path: MAC learning table might need an update. */
2063 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
2064 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
2065 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2069 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2070 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
2072 update_mcast_snooping_table4__(const struct xbridge *xbridge,
2073 const struct flow *flow,
2074 struct mcast_snooping *ms, int vlan,
2075 struct xbundle *in_xbundle,
2076 const struct dp_packet *packet)
2077 OVS_REQ_WRLOCK(ms->rwlock)
2079 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2081 ovs_be32 ip4 = flow->igmp_group_ip4;
2083 switch (ntohs(flow->tp_src)) {
2084 case IGMP_HOST_MEMBERSHIP_REPORT:
2085 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2086 if (mcast_snooping_add_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2087 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
2088 IP_FMT" is on port %s in VLAN %d",
2089 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2092 case IGMP_HOST_LEAVE_MESSAGE:
2093 if (mcast_snooping_leave_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2094 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
2095 IP_FMT" is on port %s in VLAN %d",
2096 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2099 case IGMP_HOST_MEMBERSHIP_QUERY:
2100 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
2101 in_xbundle->ofbundle)) {
2102 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
2103 IP_FMT" is on port %s in VLAN %d",
2104 xbridge->name, IP_ARGS(flow->nw_src),
2105 in_xbundle->name, vlan);
2108 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2109 if ((count = mcast_snooping_add_report(ms, packet, vlan,
2110 in_xbundle->ofbundle))) {
2111 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2112 "addresses on port %s in VLAN %d",
2113 xbridge->name, count, in_xbundle->name, vlan);
2120 update_mcast_snooping_table6__(const struct xbridge *xbridge,
2121 const struct flow *flow,
2122 struct mcast_snooping *ms, int vlan,
2123 struct xbundle *in_xbundle,
2124 const struct dp_packet *packet)
2125 OVS_REQ_WRLOCK(ms->rwlock)
2127 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2130 switch (ntohs(flow->tp_src)) {
2132 if (!ipv6_addr_equals(&flow->ipv6_src, &in6addr_any)
2133 && mcast_snooping_add_mrouter(ms, vlan, in_xbundle->ofbundle)) {
2134 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query on port %s"
2136 xbridge->name, in_xbundle->name, vlan);
2142 count = mcast_snooping_add_mld(ms, packet, vlan, in_xbundle->ofbundle);
2144 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2145 "addresses on port %s in VLAN %d",
2146 xbridge->name, count, in_xbundle->name, vlan);
2152 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2153 * was received on 'in_xbundle' in 'vlan'. */
2155 update_mcast_snooping_table(const struct xbridge *xbridge,
2156 const struct flow *flow, int vlan,
2157 struct xbundle *in_xbundle,
2158 const struct dp_packet *packet)
2160 struct mcast_snooping *ms = xbridge->ms;
2161 struct xlate_cfg *xcfg;
2162 struct xbundle *mcast_xbundle;
2163 struct mcast_port_bundle *fport;
2165 /* Don't learn the OFPP_NONE port. */
2166 if (in_xbundle == &ofpp_none_bundle) {
2170 /* Don't learn from flood ports */
2171 mcast_xbundle = NULL;
2172 ovs_rwlock_wrlock(&ms->rwlock);
2173 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2174 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2175 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2176 if (mcast_xbundle == in_xbundle) {
2181 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
2182 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2183 update_mcast_snooping_table4__(xbridge, flow, ms, vlan,
2184 in_xbundle, packet);
2186 update_mcast_snooping_table6__(xbridge, flow, ms, vlan,
2187 in_xbundle, packet);
2190 ovs_rwlock_unlock(&ms->rwlock);
2193 /* send the packet to ports having the multicast group learned */
2195 xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2196 struct mcast_snooping *ms OVS_UNUSED,
2197 struct mcast_group *grp,
2198 struct xbundle *in_xbundle, uint16_t vlan)
2199 OVS_REQ_RDLOCK(ms->rwlock)
2201 struct xlate_cfg *xcfg;
2202 struct mcast_group_bundle *b;
2203 struct xbundle *mcast_xbundle;
2205 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2206 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2207 mcast_xbundle = xbundle_lookup(xcfg, b->port);
2208 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2209 xlate_report(ctx, "forwarding to mcast group port");
2210 output_normal(ctx, mcast_xbundle, vlan);
2211 } else if (!mcast_xbundle) {
2212 xlate_report(ctx, "mcast group port is unknown, dropping");
2214 xlate_report(ctx, "mcast group port is input port, dropping");
2219 /* send the packet to ports connected to multicast routers */
2221 xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2222 struct mcast_snooping *ms,
2223 struct xbundle *in_xbundle, uint16_t vlan)
2224 OVS_REQ_RDLOCK(ms->rwlock)
2226 struct xlate_cfg *xcfg;
2227 struct mcast_mrouter_bundle *mrouter;
2228 struct xbundle *mcast_xbundle;
2230 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2231 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2232 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2233 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2234 xlate_report(ctx, "forwarding to mcast router port");
2235 output_normal(ctx, mcast_xbundle, vlan);
2236 } else if (!mcast_xbundle) {
2237 xlate_report(ctx, "mcast router port is unknown, dropping");
2239 xlate_report(ctx, "mcast router port is input port, dropping");
2244 /* send the packet to ports flagged to be flooded */
2246 xlate_normal_mcast_send_fports(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_port_bundle *fport;
2253 struct xbundle *mcast_xbundle;
2255 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2256 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2257 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2258 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2259 xlate_report(ctx, "forwarding to mcast flood port");
2260 output_normal(ctx, mcast_xbundle, vlan);
2261 } else if (!mcast_xbundle) {
2262 xlate_report(ctx, "mcast flood port is unknown, dropping");
2264 xlate_report(ctx, "mcast flood port is input port, dropping");
2269 /* forward the Reports to configured ports */
2271 xlate_normal_mcast_send_rports(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 *rport;
2278 struct xbundle *mcast_xbundle;
2280 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2281 LIST_FOR_EACH(rport, node, &ms->rport_list) {
2282 mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2283 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2284 xlate_report(ctx, "forwarding Report to mcast flagged port");
2285 output_normal(ctx, mcast_xbundle, vlan);
2286 } else if (!mcast_xbundle) {
2287 xlate_report(ctx, "mcast port is unknown, dropping the Report");
2289 xlate_report(ctx, "mcast port is input port, dropping the Report");
2295 xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2298 struct xbundle *xbundle;
2300 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2301 if (xbundle != in_xbundle
2302 && xbundle_includes_vlan(xbundle, vlan)
2303 && xbundle->floodable
2304 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2305 output_normal(ctx, xbundle, vlan);
2308 ctx->nf_output_iface = NF_OUT_FLOOD;
2312 xlate_normal(struct xlate_ctx *ctx)
2314 struct flow_wildcards *wc = ctx->wc;
2315 struct flow *flow = &ctx->xin->flow;
2316 struct xbundle *in_xbundle;
2317 struct xport *in_port;
2318 struct mac_entry *mac;
2323 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2324 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2325 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2327 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
2328 ctx->xin->packet != NULL, &in_port);
2330 xlate_report(ctx, "no input bundle, dropping");
2334 /* Drop malformed frames. */
2335 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
2336 !(flow->vlan_tci & htons(VLAN_CFI))) {
2337 if (ctx->xin->packet != NULL) {
2338 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2339 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
2340 "VLAN tag received on port %s",
2341 ctx->xbridge->name, in_xbundle->name);
2343 xlate_report(ctx, "partial VLAN tag, dropping");
2347 /* Drop frames on bundles reserved for mirroring. */
2348 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
2349 if (ctx->xin->packet != NULL) {
2350 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2351 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2352 "%s, which is reserved exclusively for mirroring",
2353 ctx->xbridge->name, in_xbundle->name);
2355 xlate_report(ctx, "input port is mirror output port, dropping");
2360 vid = vlan_tci_to_vid(flow->vlan_tci);
2361 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
2362 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2365 vlan = input_vid_to_vlan(in_xbundle, vid);
2367 /* Check other admissibility requirements. */
2368 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2372 /* Learn source MAC. */
2373 if (ctx->xin->may_learn) {
2374 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
2376 if (ctx->xin->xcache) {
2377 struct xc_entry *entry;
2379 /* Save enough info to update mac learning table later. */
2380 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
2381 entry->u.normal.ofproto = ctx->xbridge->ofproto;
2382 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2383 entry->u.normal.vlan = vlan;
2386 /* Determine output bundle. */
2387 if (mcast_snooping_enabled(ctx->xbridge->ms)
2388 && !eth_addr_is_broadcast(flow->dl_dst)
2389 && eth_addr_is_multicast(flow->dl_dst)
2390 && is_ip_any(flow)) {
2391 struct mcast_snooping *ms = ctx->xbridge->ms;
2392 struct mcast_group *grp = NULL;
2394 if (is_igmp(flow)) {
2395 if (mcast_snooping_is_membership(flow->tp_src) ||
2396 mcast_snooping_is_query(flow->tp_src)) {
2397 if (ctx->xin->may_learn) {
2398 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2399 in_xbundle, ctx->xin->packet);
2402 * IGMP packets need to take the slow path, in order to be
2403 * processed for mdb updates. That will prevent expires
2404 * firing off even after hosts have sent reports.
2406 ctx->xout->slow |= SLOW_ACTION;
2409 if (mcast_snooping_is_membership(flow->tp_src)) {
2410 ovs_rwlock_rdlock(&ms->rwlock);
2411 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2412 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2413 * forward IGMP Membership Reports only to those ports where
2414 * multicast routers are attached. Alternatively stated: a
2415 * snooping switch should not forward IGMP Membership Reports
2416 * to ports on which only hosts are attached.
2417 * An administrative control may be provided to override this
2418 * restriction, allowing the report messages to be flooded to
2420 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2421 ovs_rwlock_unlock(&ms->rwlock);
2423 xlate_report(ctx, "multicast traffic, flooding");
2424 xlate_normal_flood(ctx, in_xbundle, vlan);
2427 } else if (is_mld(flow)) {
2428 ctx->xout->slow |= SLOW_ACTION;
2429 if (ctx->xin->may_learn) {
2430 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2431 in_xbundle, ctx->xin->packet);
2433 if (is_mld_report(flow)) {
2434 ovs_rwlock_rdlock(&ms->rwlock);
2435 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2436 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2437 ovs_rwlock_unlock(&ms->rwlock);
2439 xlate_report(ctx, "MLD query, flooding");
2440 xlate_normal_flood(ctx, in_xbundle, vlan);
2443 if ((flow->dl_type == htons(ETH_TYPE_IP)
2444 && ip_is_local_multicast(flow->nw_dst))
2445 || (flow->dl_type == htons(ETH_TYPE_IPV6)
2446 && ipv6_is_all_hosts(&flow->ipv6_dst))) {
2447 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2448 * address in the 224.0.0.x range which are not IGMP must
2449 * be forwarded on all ports */
2450 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2451 xlate_normal_flood(ctx, in_xbundle, vlan);
2456 /* forwarding to group base ports */
2457 ovs_rwlock_rdlock(&ms->rwlock);
2458 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2459 grp = mcast_snooping_lookup4(ms, flow->nw_dst, vlan);
2460 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2461 grp = mcast_snooping_lookup(ms, &flow->ipv6_dst, vlan);
2464 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2465 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2466 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2468 if (mcast_snooping_flood_unreg(ms)) {
2469 xlate_report(ctx, "unregistered multicast, flooding");
2470 xlate_normal_flood(ctx, in_xbundle, vlan);
2472 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2473 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2476 ovs_rwlock_unlock(&ms->rwlock);
2478 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2479 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
2480 mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
2481 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2484 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2485 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2486 if (mac_xbundle && mac_xbundle != in_xbundle) {
2487 xlate_report(ctx, "forwarding to learned port");
2488 output_normal(ctx, mac_xbundle, vlan);
2489 } else if (!mac_xbundle) {
2490 xlate_report(ctx, "learned port is unknown, dropping");
2492 xlate_report(ctx, "learned port is input port, dropping");
2495 xlate_report(ctx, "no learned MAC for destination, flooding");
2496 xlate_normal_flood(ctx, in_xbundle, vlan);
2501 /* Appends a "sample" action for sFlow or IPFIX to 'ctx->odp_actions'. The
2502 * 'probability' is the number of packets out of UINT32_MAX to sample. The
2503 * 'cookie' (of length 'cookie_size' bytes) is passed back in the callback for
2504 * each sampled packet. 'tunnel_out_port', if not ODPP_NONE, is added as the
2505 * OVS_USERSPACE_ATTR_EGRESS_TUN_PORT attribute. If 'include_actions', an
2506 * OVS_USERSPACE_ATTR_ACTIONS attribute is added.
2509 compose_sample_action(struct xlate_ctx *ctx,
2510 const uint32_t probability,
2511 const union user_action_cookie *cookie,
2512 const size_t cookie_size,
2513 const odp_port_t tunnel_out_port,
2514 bool include_actions)
2516 size_t sample_offset = nl_msg_start_nested(ctx->odp_actions,
2517 OVS_ACTION_ATTR_SAMPLE);
2519 nl_msg_put_u32(ctx->odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
2521 size_t actions_offset = nl_msg_start_nested(ctx->odp_actions,
2522 OVS_SAMPLE_ATTR_ACTIONS);
2524 odp_port_t odp_port = ofp_port_to_odp_port(
2525 ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
2526 uint32_t pid = dpif_port_get_pid(ctx->xbridge->dpif, odp_port,
2527 flow_hash_5tuple(&ctx->xin->flow, 0));
2528 int cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2533 nl_msg_end_nested(ctx->odp_actions, actions_offset);
2534 nl_msg_end_nested(ctx->odp_actions, sample_offset);
2536 return cookie_offset;
2539 /* If sFLow is not enabled, returns 0 without doing anything.
2541 * If sFlow is enabled, appends a template "sample" action to the ODP actions
2542 * in 'ctx'. This action is a template because some of the information needed
2543 * to fill it out is not available until flow translation is complete. In this
2544 * case, this functions returns an offset, which is always nonzero, to pass
2545 * later to fix_sflow_action() to fill in the rest of the template. */
2547 compose_sflow_action(struct xlate_ctx *ctx)
2549 struct dpif_sflow *sflow = ctx->xbridge->sflow;
2550 if (!sflow || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
2554 union user_action_cookie cookie = { .type = USER_ACTION_COOKIE_SFLOW };
2555 return compose_sample_action(ctx, dpif_sflow_get_probability(sflow),
2556 &cookie, sizeof cookie.sflow, ODPP_NONE,
2560 /* If IPFIX is enabled, this appends a "sample" action to implement IPFIX to
2561 * 'ctx->odp_actions'. */
2563 compose_ipfix_action(struct xlate_ctx *ctx, odp_port_t output_odp_port)
2565 struct dpif_ipfix *ipfix = ctx->xbridge->ipfix;
2566 odp_port_t tunnel_out_port = ODPP_NONE;
2568 if (!ipfix || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
2572 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2574 if (output_odp_port == ODPP_NONE &&
2575 !dpif_ipfix_get_bridge_exporter_input_sampling(ipfix)) {
2579 /* For output case, output_odp_port is valid*/
2580 if (output_odp_port != ODPP_NONE) {
2581 if (!dpif_ipfix_get_bridge_exporter_output_sampling(ipfix)) {
2584 /* If tunnel sampling is enabled, put an additional option attribute:
2585 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2587 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(ipfix) &&
2588 dpif_ipfix_get_tunnel_port(ipfix, output_odp_port) ) {
2589 tunnel_out_port = output_odp_port;
2593 union user_action_cookie cookie = {
2595 .type = USER_ACTION_COOKIE_IPFIX,
2596 .output_odp_port = output_odp_port,
2599 compose_sample_action(ctx,
2600 dpif_ipfix_get_bridge_exporter_probability(ipfix),
2601 &cookie, sizeof cookie.ipfix, tunnel_out_port,
2605 /* Fix "sample" action according to data collected while composing ODP actions,
2606 * as described in compose_sflow_action().
2608 * 'user_cookie_offset' must be the offset returned by add_sflow_action(). */
2610 fix_sflow_action(struct xlate_ctx *ctx, unsigned int user_cookie_offset)
2612 const struct flow *base = &ctx->base_flow;
2613 union user_action_cookie *cookie;
2615 cookie = ofpbuf_at(ctx->odp_actions, user_cookie_offset,
2616 sizeof cookie->sflow);
2617 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2619 cookie->type = USER_ACTION_COOKIE_SFLOW;
2620 cookie->sflow.vlan_tci = base->vlan_tci;
2622 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2623 * port information") for the interpretation of cookie->output. */
2624 switch (ctx->sflow_n_outputs) {
2626 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2627 cookie->sflow.output = 0x40000000 | 256;
2631 cookie->sflow.output = dpif_sflow_odp_port_to_ifindex(
2632 ctx->xbridge->sflow, ctx->sflow_odp_port);
2633 if (cookie->sflow.output) {
2638 /* 0x80000000 means "multiple output ports. */
2639 cookie->sflow.output = 0x80000000 | ctx->sflow_n_outputs;
2645 process_special(struct xlate_ctx *ctx, const struct xport *xport)
2647 const struct flow *flow = &ctx->xin->flow;
2648 struct flow_wildcards *wc = ctx->wc;
2649 const struct xbridge *xbridge = ctx->xbridge;
2650 const struct dp_packet *packet = ctx->xin->packet;
2651 enum slow_path_reason slow;
2655 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
2657 cfm_process_heartbeat(xport->cfm, packet);
2660 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
2662 bfd_process_packet(xport->bfd, flow, packet);
2663 /* If POLL received, immediately sends FINAL back. */
2664 if (bfd_should_send_packet(xport->bfd)) {
2665 ofproto_dpif_monitor_port_send_soon(xport->ofport);
2669 } else if (xport->xbundle && xport->xbundle->lacp
2670 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2672 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
2675 } else if ((xbridge->stp || xbridge->rstp) &&
2676 stp_should_process_flow(flow, wc)) {
2679 ? stp_process_packet(xport, packet)
2680 : rstp_process_packet(xport, packet);
2683 } else if (xport->lldp && lldp_should_process_flow(xport->lldp, flow)) {
2685 lldp_process_packet(xport->lldp, packet);
2693 ctx->xout->slow |= slow;
2701 tnl_route_lookup_flow(const struct flow *oflow,
2702 struct in6_addr *ip, struct xport **out_port)
2704 char out_dev[IFNAMSIZ];
2705 struct xbridge *xbridge;
2706 struct xlate_cfg *xcfg;
2708 struct in6_addr dst;
2710 dst = flow_tnl_dst(&oflow->tunnel);
2711 if (!ovs_router_lookup(&dst, out_dev, &gw)) {
2715 if (ipv6_addr_is_set(&gw) &&
2716 (!IN6_IS_ADDR_V4MAPPED(&gw) || in6_addr_get_mapped_ipv4(&gw))) {
2722 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2725 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
2726 if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
2729 HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
2730 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
2741 compose_table_xlate(struct xlate_ctx *ctx, const struct xport *out_dev,
2742 struct dp_packet *packet)
2744 struct xbridge *xbridge = out_dev->xbridge;
2745 struct ofpact_output output;
2748 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
2749 flow_extract(packet, &flow);
2750 flow.in_port.ofp_port = out_dev->ofp_port;
2751 output.port = OFPP_TABLE;
2754 return ofproto_dpif_execute_actions__(xbridge->ofproto, &flow, NULL,
2755 &output.ofpact, sizeof output,
2756 ctx->recurse, ctx->resubmits, packet);
2760 tnl_send_nd_request(struct xlate_ctx *ctx, const struct xport *out_dev,
2761 const struct eth_addr eth_src,
2762 struct in6_addr * ipv6_src, struct in6_addr * ipv6_dst)
2764 struct dp_packet packet;
2766 dp_packet_init(&packet, 0);
2767 compose_nd(&packet, eth_src, ipv6_src, ipv6_dst);
2768 compose_table_xlate(ctx, out_dev, &packet);
2769 dp_packet_uninit(&packet);
2773 tnl_send_arp_request(struct xlate_ctx *ctx, const struct xport *out_dev,
2774 const struct eth_addr eth_src,
2775 ovs_be32 ip_src, ovs_be32 ip_dst)
2777 struct dp_packet packet;
2779 dp_packet_init(&packet, 0);
2780 compose_arp(&packet, ARP_OP_REQUEST,
2781 eth_src, eth_addr_zero, true, ip_src, ip_dst);
2783 compose_table_xlate(ctx, out_dev, &packet);
2784 dp_packet_uninit(&packet);
2788 build_tunnel_send(struct xlate_ctx *ctx, const struct xport *xport,
2789 const struct flow *flow, odp_port_t tunnel_odp_port)
2791 struct ovs_action_push_tnl tnl_push_data;
2792 struct xport *out_dev = NULL;
2793 ovs_be32 s_ip = 0, d_ip = 0;
2794 struct in6_addr s_ip6 = in6addr_any;
2795 struct in6_addr d_ip6 = in6addr_any;
2796 struct eth_addr smac;
2797 struct eth_addr dmac;
2799 char buf_sip6[INET6_ADDRSTRLEN];
2800 char buf_dip6[INET6_ADDRSTRLEN];
2802 err = tnl_route_lookup_flow(flow, &d_ip6, &out_dev);
2804 xlate_report(ctx, "native tunnel routing failed");
2808 xlate_report(ctx, "tunneling to %s via %s",
2809 ipv6_string_mapped(buf_dip6, &d_ip6),
2810 netdev_get_name(out_dev->netdev));
2812 /* Use mac addr of bridge port of the peer. */
2813 err = netdev_get_etheraddr(out_dev->netdev, &smac);
2815 xlate_report(ctx, "tunnel output device lacks Ethernet address");
2819 d_ip = in6_addr_get_mapped_ipv4(&d_ip6);
2821 err = netdev_get_in4(out_dev->netdev, (struct in_addr *) &s_ip, NULL);
2823 xlate_report(ctx, "tunnel output device lacks IPv4 address");
2826 in6_addr_set_mapped_ipv4(&s_ip6, s_ip);
2828 err = netdev_get_in6(out_dev->netdev, &s_ip6);
2830 xlate_report(ctx, "tunnel output device lacks IPv6 address");
2835 err = tnl_neigh_lookup(out_dev->xbridge->name, &d_ip6, &dmac);
2837 xlate_report(ctx, "neighbor cache miss for %s on bridge %s, "
2838 "sending %s request",
2839 buf_dip6, out_dev->xbridge->name, d_ip ? "ARP" : "ND");
2841 tnl_send_arp_request(ctx, out_dev, smac, s_ip, d_ip);
2843 tnl_send_nd_request(ctx, out_dev, smac, &s_ip6, &d_ip6);
2848 if (ctx->xin->xcache) {
2849 struct xc_entry *entry;
2851 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_NEIGH);
2852 ovs_strlcpy(entry->u.tnl_neigh_cache.br_name, out_dev->xbridge->name,
2853 sizeof entry->u.tnl_neigh_cache.br_name);
2854 entry->u.tnl_neigh_cache.d_ipv6 = d_ip6;
2857 xlate_report(ctx, "tunneling from "ETH_ADDR_FMT" %s"
2858 " to "ETH_ADDR_FMT" %s",
2859 ETH_ADDR_ARGS(smac), ipv6_string_mapped(buf_sip6, &s_ip6),
2860 ETH_ADDR_ARGS(dmac), buf_dip6);
2862 err = tnl_port_build_header(xport->ofport, flow,
2863 dmac, smac, &s_ip6, &tnl_push_data);
2867 tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
2868 tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
2869 odp_put_tnl_push_action(ctx->odp_actions, &tnl_push_data);
2874 xlate_commit_actions(struct xlate_ctx *ctx)
2876 bool use_masked = ctx->xbridge->support.masked_set_action;
2878 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2879 ctx->odp_actions, ctx->wc,
2884 clear_conntrack(struct flow *flow)
2889 memset(&flow->ct_label, 0, sizeof flow->ct_label);
2893 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
2894 const struct xlate_bond_recirc *xr, bool check_stp)
2896 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
2897 struct flow_wildcards *wc = ctx->wc;
2898 struct flow *flow = &ctx->xin->flow;
2899 struct flow_tnl flow_tnl;
2900 ovs_be16 flow_vlan_tci;
2901 uint32_t flow_pkt_mark;
2902 uint8_t flow_nw_tos;
2903 odp_port_t out_port, odp_port;
2904 bool tnl_push_pop_send = false;
2907 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2908 * before traversing a patch port. */
2909 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 35);
2910 memset(&flow_tnl, 0, sizeof flow_tnl);
2913 xlate_report(ctx, "Nonexistent output port");
2915 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
2916 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2918 } else if (check_stp) {
2919 if (is_stp(&ctx->base_flow)) {
2920 if (!xport_stp_should_forward_bpdu(xport) &&
2921 !xport_rstp_should_manage_bpdu(xport)) {
2922 if (ctx->xbridge->stp != NULL) {
2923 xlate_report(ctx, "STP not in listening state, "
2924 "skipping bpdu output");
2925 } else if (ctx->xbridge->rstp != NULL) {
2926 xlate_report(ctx, "RSTP not managing BPDU in this state, "
2927 "skipping bpdu output");
2931 } else if (!xport_stp_forward_state(xport) ||
2932 !xport_rstp_forward_state(xport)) {
2933 if (ctx->xbridge->stp != NULL) {
2934 xlate_report(ctx, "STP not in forwarding state, "
2936 } else if (ctx->xbridge->rstp != NULL) {
2937 xlate_report(ctx, "RSTP not in forwarding state, "
2945 const struct xport *peer = xport->peer;
2946 struct flow old_flow = ctx->xin->flow;
2947 bool old_conntrack = ctx->conntracked;
2948 bool old_was_mpls = ctx->was_mpls;
2949 cls_version_t old_version = ctx->tables_version;
2950 struct ofpbuf old_stack = ctx->stack;
2951 union mf_subvalue new_stack[1024 / sizeof(union mf_subvalue)];
2952 struct ofpbuf old_action_set = ctx->action_set;
2953 uint64_t actset_stub[1024 / 8];
2955 ofpbuf_use_stub(&ctx->stack, new_stack, sizeof new_stack);
2956 ofpbuf_use_stub(&ctx->action_set, actset_stub, sizeof actset_stub);
2957 ctx->xbridge = peer->xbridge;
2958 flow->in_port.ofp_port = peer->ofp_port;
2959 flow->metadata = htonll(0);
2960 memset(&flow->tunnel, 0, sizeof flow->tunnel);
2961 memset(flow->regs, 0, sizeof flow->regs);
2962 flow->actset_output = OFPP_UNSET;
2963 ctx->conntracked = false;
2964 clear_conntrack(flow);
2966 /* The bridge is now known so obtain its table version. */
2968 = ofproto_dpif_get_tables_version(ctx->xbridge->ofproto);
2970 if (!process_special(ctx, peer) && may_receive(peer, ctx)) {
2971 if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
2972 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2973 if (ctx->action_set.size) {
2974 /* Translate action set only if not dropping the packet and
2975 * not recirculating. */
2976 if (!exit_recirculates(ctx)) {
2977 xlate_action_set(ctx);
2980 /* Check if need to recirculate. */
2981 if (exit_recirculates(ctx)) {
2982 compose_recirculate_action(ctx);
2985 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
2986 * the learning action look at the packet, then drop it. */
2987 struct flow old_base_flow = ctx->base_flow;
2988 size_t old_size = ctx->odp_actions->size;
2989 mirror_mask_t old_mirrors = ctx->mirrors;
2991 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2992 ctx->mirrors = old_mirrors;
2993 ctx->base_flow = old_base_flow;
2994 ctx->odp_actions->size = old_size;
2996 /* Undo changes that may have been done for recirculation. */
2997 if (exit_recirculates(ctx)) {
2998 ctx->action_set.size = ctx->recirc_action_offset;
2999 ctx->recirc_action_offset = -1;
3000 ctx->last_unroll_offset = -1;
3005 ctx->xin->flow = old_flow;
3006 ctx->xbridge = xport->xbridge;
3007 ofpbuf_uninit(&ctx->action_set);
3008 ctx->action_set = old_action_set;
3009 ofpbuf_uninit(&ctx->stack);
3010 ctx->stack = old_stack;
3012 /* Restore calling bridge's lookup version. */
3013 ctx->tables_version = old_version;
3015 /* The peer bridge popping MPLS should have no effect on the original
3017 ctx->was_mpls = old_was_mpls;
3019 /* The peer bridge's conntrack execution should have no effect on the
3020 * original bridge. */
3021 ctx->conntracked = old_conntrack;
3023 /* The fact that the peer bridge exits (for any reason) does not mean
3024 * that the original bridge should exit. Specifically, if the peer
3025 * bridge recirculates (which typically modifies the packet), the
3026 * original bridge must continue processing with the original, not the
3027 * recirculated packet! */
3030 /* Peer bridge errors do not propagate back. */
3031 ctx->error = XLATE_OK;
3033 if (ctx->xin->resubmit_stats) {
3034 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
3035 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
3037 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
3040 if (ctx->xin->xcache) {
3041 struct xc_entry *entry;
3043 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
3044 entry->u.dev.tx = netdev_ref(xport->netdev);
3045 entry->u.dev.rx = netdev_ref(peer->netdev);
3046 entry->u.dev.bfd = bfd_ref(peer->bfd);
3051 flow_vlan_tci = flow->vlan_tci;
3052 flow_pkt_mark = flow->pkt_mark;
3053 flow_nw_tos = flow->nw_tos;
3055 if (count_skb_priorities(xport)) {
3056 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3057 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
3058 wc->masks.nw_tos |= IP_DSCP_MASK;
3059 flow->nw_tos &= ~IP_DSCP_MASK;
3060 flow->nw_tos |= dscp;
3064 if (xport->is_tunnel) {
3065 struct in6_addr dst;
3066 /* Save tunnel metadata so that changes made due to
3067 * the Logical (tunnel) Port are not visible for any further
3068 * matches, while explicit set actions on tunnel metadata are.
3070 flow_tnl = flow->tunnel;
3071 odp_port = tnl_port_send(xport->ofport, flow, ctx->wc);
3072 if (odp_port == ODPP_NONE) {
3073 xlate_report(ctx, "Tunneling decided against output");
3074 goto out; /* restore flow_nw_tos */
3076 dst = flow_tnl_dst(&flow->tunnel);
3077 if (ipv6_addr_equals(&dst, &ctx->orig_tunnel_ipv6_dst)) {
3078 xlate_report(ctx, "Not tunneling to our own address");
3079 goto out; /* restore flow_nw_tos */
3081 if (ctx->xin->resubmit_stats) {
3082 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
3084 if (ctx->xin->xcache) {
3085 struct xc_entry *entry;
3087 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
3088 entry->u.dev.tx = netdev_ref(xport->netdev);
3090 out_port = odp_port;
3091 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3092 xlate_report(ctx, "output to native tunnel");
3093 tnl_push_pop_send = true;
3095 xlate_report(ctx, "output to kernel tunnel");
3096 commit_odp_tunnel_action(flow, &ctx->base_flow, ctx->odp_actions);
3097 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3100 odp_port = xport->odp_port;
3101 out_port = odp_port;
3102 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
3103 ofp_port_t vlandev_port;
3105 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
3106 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
3107 ofp_port, flow->vlan_tci);
3108 if (vlandev_port != ofp_port) {
3109 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
3110 flow->vlan_tci = htons(0);
3115 if (out_port != ODPP_NONE) {
3116 xlate_commit_actions(ctx);
3119 struct ovs_action_hash *act_hash;
3122 act_hash = nl_msg_put_unspec_uninit(ctx->odp_actions,
3123 OVS_ACTION_ATTR_HASH,
3125 act_hash->hash_alg = xr->hash_alg;
3126 act_hash->hash_basis = xr->hash_basis;
3128 /* Recirc action. */
3129 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC,
3133 if (tnl_push_pop_send) {
3134 build_tunnel_send(ctx, xport, flow, odp_port);
3135 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3137 odp_port_t odp_tnl_port = ODPP_NONE;
3139 /* XXX: Write better Filter for tunnel port. We can use inport
3140 * int tunnel-port flow to avoid these checks completely. */
3141 if (ofp_port == OFPP_LOCAL &&
3142 ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3144 odp_tnl_port = tnl_port_map_lookup(flow, wc);
3147 if (odp_tnl_port != ODPP_NONE) {
3148 nl_msg_put_odp_port(ctx->odp_actions,
3149 OVS_ACTION_ATTR_TUNNEL_POP,
3152 /* Tunnel push-pop action is not compatible with
3154 compose_ipfix_action(ctx, out_port);
3155 nl_msg_put_odp_port(ctx->odp_actions,
3156 OVS_ACTION_ATTR_OUTPUT,
3162 ctx->sflow_odp_port = odp_port;
3163 ctx->sflow_n_outputs++;
3164 ctx->nf_output_iface = ofp_port;
3167 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
3168 mirror_packet(ctx, xport->xbundle,
3169 xbundle_mirror_dst(xport->xbundle->xbridge,
3175 flow->vlan_tci = flow_vlan_tci;
3176 flow->pkt_mark = flow_pkt_mark;
3177 flow->nw_tos = flow_nw_tos;
3181 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port,
3182 const struct xlate_bond_recirc *xr)
3184 compose_output_action__(ctx, ofp_port, xr, true);
3188 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
3190 struct rule_dpif *old_rule = ctx->rule;
3191 ovs_be64 old_cookie = ctx->rule_cookie;
3192 const struct rule_actions *actions;
3194 if (ctx->xin->resubmit_stats) {
3195 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
3201 ctx->rule_cookie = rule_dpif_get_flow_cookie(rule);
3202 actions = rule_dpif_get_actions(rule);
3203 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
3204 ctx->rule_cookie = old_cookie;
3205 ctx->rule = old_rule;
3210 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
3212 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
3213 XLATE_REPORT_ERROR(ctx, "resubmit actions recursed over %d times",
3214 MAX_RESUBMIT_RECURSION);
3215 ctx->error = XLATE_RECURSION_TOO_DEEP;
3216 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
3217 XLATE_REPORT_ERROR(ctx, "over %d resubmit actions", MAX_RESUBMITS);
3218 ctx->error = XLATE_TOO_MANY_RESUBMITS;
3219 } else if (ctx->odp_actions->size > UINT16_MAX) {
3220 XLATE_REPORT_ERROR(ctx, "resubmits yielded over 64 kB of actions");
3221 /* NOT an error, as we'll be slow-pathing the flow in this case? */
3222 ctx->exit = true; /* XXX: translation still terminated! */
3223 } else if (ctx->stack.size >= 65536) {
3224 XLATE_REPORT_ERROR(ctx, "resubmits yielded over 64 kB of stack");
3225 ctx->error = XLATE_STACK_TOO_DEEP;
3234 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
3235 bool may_packet_in, bool honor_table_miss)
3237 /* Check if we need to recirculate before matching in a table. */
3238 if (ctx->was_mpls) {
3239 ctx_trigger_recirculation(ctx);
3242 if (xlate_resubmit_resource_check(ctx)) {
3243 uint8_t old_table_id = ctx->table_id;
3244 struct rule_dpif *rule;
3246 ctx->table_id = table_id;
3248 rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
3249 ctx->tables_version,
3250 &ctx->xin->flow, ctx->xin->wc,
3251 ctx->xin->resubmit_stats,
3252 &ctx->table_id, in_port,
3253 may_packet_in, honor_table_miss);
3255 if (OVS_UNLIKELY(ctx->xin->resubmit_hook)) {
3256 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse + 1);
3260 /* Fill in the cache entry here instead of xlate_recursively
3261 * to make the reference counting more explicit. We take a
3262 * reference in the lookups above if we are going to cache the
3264 if (ctx->xin->xcache) {
3265 struct xc_entry *entry;
3267 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
3268 entry->u.rule = rule;
3269 rule_dpif_ref(rule);
3271 xlate_recursively(ctx, rule);
3274 ctx->table_id = old_table_id;
3280 xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
3281 struct ofputil_bucket *bucket)
3283 if (ctx->xin->resubmit_stats) {
3284 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
3286 if (ctx->xin->xcache) {
3287 struct xc_entry *entry;
3289 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
3290 entry->u.group.group = group_dpif_ref(group);
3291 entry->u.group.bucket = bucket;
3296 xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
3298 uint64_t action_list_stub[1024 / 8];
3299 struct ofpbuf action_list, action_set;
3300 struct flow old_flow = ctx->xin->flow;
3301 bool old_was_mpls = ctx->was_mpls;
3303 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
3304 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3306 ofpacts_execute_action_set(&action_list, &action_set);
3308 do_xlate_actions(action_list.data, action_list.size, ctx);
3311 ofpbuf_uninit(&action_set);
3312 ofpbuf_uninit(&action_list);
3314 /* Check if need to recirculate. */
3315 if (exit_recirculates(ctx)) {
3316 compose_recirculate_action(ctx);
3319 /* Roll back flow to previous state.
3320 * This is equivalent to cloning the packet for each bucket.
3322 * As a side effect any subsequently applied actions will
3323 * also effectively be applied to a clone of the packet taken
3324 * just before applying the all or indirect group.
3326 * Note that group buckets are action sets, hence they cannot modify the
3327 * main action set. Also any stack actions are ignored when executing an
3328 * action set, so group buckets cannot change the stack either.
3329 * However, we do allow resubmit actions in group buckets, which could
3330 * break the above assumptions. It is up to the controller to not mess up
3331 * with the action_set and stack in the tables resubmitted to from
3333 ctx->xin->flow = old_flow;
3335 /* The group bucket popping MPLS should have no effect after bucket
3337 ctx->was_mpls = old_was_mpls;
3339 /* The fact that the group bucket exits (for any reason) does not mean that
3340 * the translation after the group action should exit. Specifically, if
3341 * the group bucket recirculates (which typically modifies the packet), the
3342 * actions after the group action must continue processing with the
3343 * original, not the recirculated packet! */
3348 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3350 struct ofputil_bucket *bucket;
3351 const struct ovs_list *buckets;
3353 group_dpif_get_buckets(group, &buckets);
3355 LIST_FOR_EACH (bucket, list_node, buckets) {
3356 xlate_group_bucket(ctx, bucket);
3358 xlate_group_stats(ctx, group, NULL);
3362 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3364 struct ofputil_bucket *bucket;
3366 bucket = group_first_live_bucket(ctx, group, 0);
3368 xlate_group_bucket(ctx, bucket);
3369 xlate_group_stats(ctx, group, bucket);
3374 xlate_default_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3376 struct flow_wildcards *wc = ctx->wc;
3377 struct ofputil_bucket *bucket;
3380 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
3381 flow_mask_hash_fields(&ctx->xin->flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
3382 bucket = group_best_live_bucket(ctx, group, basis);
3384 xlate_group_bucket(ctx, bucket);
3385 xlate_group_stats(ctx, group, bucket);
3390 xlate_hash_fields_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3392 struct mf_bitmap hash_fields = MF_BITMAP_INITIALIZER;
3393 const struct field_array *fields;
3394 struct ofputil_bucket *bucket;
3398 fields = group_dpif_get_fields(group);
3399 basis = hash_uint64(group_dpif_get_selection_method_param(group));
3401 /* Determine which fields to hash */
3402 for (i = 0; i < MFF_N_IDS; i++) {
3403 if (bitmap_is_set(fields->used.bm, i)) {
3404 const struct mf_field *mf;
3406 /* If the field is already present in 'hash_fields' then
3407 * this loop has already checked that it and its pre-requisites
3408 * are present in the flow and its pre-requisites have
3409 * already been added to 'hash_fields'. There is nothing more
3410 * to do here and as an optimisation the loop can continue. */
3411 if (bitmap_is_set(hash_fields.bm, i)) {
3417 /* Only hash a field if it and its pre-requisites are present
3419 if (!mf_are_prereqs_ok(mf, &ctx->xin->flow)) {
3423 /* Hash both the field and its pre-requisites */
3424 mf_bitmap_set_field_and_prereqs(mf, &hash_fields);
3428 /* Hash the fields */
3429 for (i = 0; i < MFF_N_IDS; i++) {
3430 if (bitmap_is_set(hash_fields.bm, i)) {
3431 const struct mf_field *mf = mf_from_id(i);
3432 union mf_value value;
3435 mf_get_value(mf, &ctx->xin->flow, &value);
3436 /* This seems inefficient but so does apply_mask() */
3437 for (j = 0; j < mf->n_bytes; j++) {
3438 ((uint8_t *) &value)[j] &= ((uint8_t *) &fields->value[i])[j];
3440 basis = hash_bytes(&value, mf->n_bytes, basis);
3442 /* For tunnels, hash in whether the field is present. */
3443 if (mf_is_tun_metadata(mf)) {
3444 basis = hash_boolean(mf_is_set(mf, &ctx->xin->flow), basis);
3447 mf_mask_field(mf, &ctx->wc->masks);
3451 bucket = group_best_live_bucket(ctx, group, basis);
3453 xlate_group_bucket(ctx, bucket);
3454 xlate_group_stats(ctx, group, bucket);
3459 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3461 const char *selection_method = group_dpif_get_selection_method(group);
3463 if (selection_method[0] == '\0') {
3464 xlate_default_select_group(ctx, group);
3465 } else if (!strcasecmp("hash", selection_method)) {
3466 xlate_hash_fields_select_group(ctx, group);
3468 /* Parsing of groups should ensure this never happens */
3474 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3476 bool was_in_group = ctx->in_group;
3477 ctx->in_group = true;
3479 switch (group_dpif_get_type(group)) {
3481 case OFPGT11_INDIRECT:
3482 xlate_all_group(ctx, group);
3484 case OFPGT11_SELECT:
3485 xlate_select_group(ctx, group);
3488 xlate_ff_group(ctx, group);
3493 group_dpif_unref(group);
3495 ctx->in_group = was_in_group;
3499 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3501 if (xlate_resubmit_resource_check(ctx)) {
3502 struct group_dpif *group;
3505 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
3507 xlate_group_action__(ctx, group);
3517 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
3518 const struct ofpact_resubmit *resubmit)
3522 bool may_packet_in = false;
3523 bool honor_table_miss = false;
3525 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
3526 /* Still allow missed packets to be sent to the controller
3527 * if resubmitting from an internal table. */
3528 may_packet_in = true;
3529 honor_table_miss = true;
3532 in_port = resubmit->in_port;
3533 if (in_port == OFPP_IN_PORT) {
3534 in_port = ctx->xin->flow.in_port.ofp_port;
3537 table_id = resubmit->table_id;
3538 if (table_id == 255) {
3539 table_id = ctx->table_id;
3542 xlate_table_action(ctx, in_port, table_id, may_packet_in,
3547 flood_packets(struct xlate_ctx *ctx, bool all)
3549 const struct xport *xport;
3551 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
3552 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
3557 compose_output_action__(ctx, xport->ofp_port, NULL, false);
3558 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
3559 compose_output_action(ctx, xport->ofp_port, NULL);
3563 ctx->nf_output_iface = NF_OUT_FLOOD;
3567 execute_controller_action(struct xlate_ctx *ctx, int len,
3568 enum ofp_packet_in_reason reason,
3569 uint16_t controller_id)
3571 struct ofproto_packet_in *pin;
3572 struct dp_packet *packet;
3574 ctx->xout->slow |= SLOW_CONTROLLER;
3575 xlate_commit_actions(ctx);
3576 if (!ctx->xin->packet) {
3580 packet = dp_packet_clone(ctx->xin->packet);
3582 odp_execute_actions(NULL, &packet, 1, false,
3583 ctx->odp_actions->data, ctx->odp_actions->size, NULL);
3585 pin = xmalloc(sizeof *pin);
3586 pin->up.packet_len = dp_packet_size(packet);
3587 pin->up.packet = dp_packet_steal_data(packet);
3588 pin->up.reason = reason;
3589 pin->up.table_id = ctx->table_id;
3590 pin->up.cookie = ctx->rule_cookie;
3592 flow_get_metadata(&ctx->xin->flow, &pin->up.flow_metadata);
3594 pin->controller_id = controller_id;
3595 pin->send_len = len;
3596 /* If a rule is a table-miss rule then this is
3597 * a table-miss handled by a table-miss rule.
3599 * Else, if rule is internal and has a controller action,
3600 * the later being implied by the rule being processed here,
3601 * then this is a table-miss handled without a table-miss rule.
3603 * Otherwise this is not a table-miss. */
3604 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
3606 if (rule_dpif_is_table_miss(ctx->rule)) {
3607 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
3608 } else if (rule_dpif_is_internal(ctx->rule)) {
3609 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
3612 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
3613 dp_packet_delete(packet);
3617 compose_recirculate_action__(struct xlate_ctx *ctx, uint8_t table)
3619 struct recirc_metadata md;
3622 recirc_metadata_from_flow(&md, &ctx->xin->flow);
3624 ovs_assert(ctx->recirc_action_offset >= 0);
3626 struct recirc_state state = {
3628 .ofproto = ctx->xbridge->ofproto,
3630 .stack = &ctx->stack,
3631 .mirrors = ctx->mirrors,
3632 .conntracked = ctx->conntracked,
3633 .action_set_len = ctx->recirc_action_offset,
3634 .ofpacts_len = ctx->action_set.size,
3635 .ofpacts = ctx->action_set.data,
3638 /* Allocate a unique recirc id for the given metadata state in the
3639 * flow. An existing id, with a new reference to the corresponding
3640 * recirculation context, will be returned if possible.
3641 * The life-cycle of this recirc id is managed by associating it
3642 * with the udpif key ('ukey') created for each new datapath flow. */
3643 id = recirc_alloc_id_ctx(&state);
3645 XLATE_REPORT_ERROR(ctx, "Failed to allocate recirculation id");
3646 ctx->error = XLATE_NO_RECIRCULATION_CONTEXT;
3649 recirc_refs_add(&ctx->xout->recircs, id);
3651 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
3653 /* Undo changes done by recirculation. */
3654 ctx->action_set.size = ctx->recirc_action_offset;
3655 ctx->recirc_action_offset = -1;
3656 ctx->last_unroll_offset = -1;
3659 /* Called only when ctx->recirc_action_offset is set. */
3661 compose_recirculate_action(struct xlate_ctx *ctx)
3663 xlate_commit_actions(ctx);
3664 compose_recirculate_action__(ctx, 0);
3667 /* Fork the pipeline here. The current packet will continue processing the
3668 * current action list. A clone of the current packet will recirculate, skip
3669 * the remainder of the current action list and asynchronously resume pipeline
3670 * processing in 'table' with the current metadata and action set. */
3672 compose_recirculate_and_fork(struct xlate_ctx *ctx, uint8_t table)
3674 ctx->recirc_action_offset = ctx->action_set.size;
3675 compose_recirculate_action__(ctx, table);
3679 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
3681 struct flow *flow = &ctx->xin->flow;
3684 ovs_assert(eth_type_mpls(mpls->ethertype));
3686 n = flow_count_mpls_labels(flow, ctx->wc);
3688 xlate_commit_actions(ctx);
3689 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3690 if (ctx->xin->packet != NULL) {
3691 XLATE_REPORT_ERROR(ctx, "bridge %s: dropping packet on which an "
3692 "MPLS push action can't be performed as it would "
3693 "have more MPLS LSEs than the %d supported.",
3694 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3696 ctx->error = XLATE_TOO_MANY_MPLS_LABELS;
3700 flow_push_mpls(flow, n, mpls->ethertype, ctx->wc);
3704 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
3706 struct flow *flow = &ctx->xin->flow;
3707 int n = flow_count_mpls_labels(flow, ctx->wc);
3709 if (flow_pop_mpls(flow, n, eth_type, ctx->wc)) {
3710 if (ctx->xbridge->support.odp.recirc) {
3711 ctx->was_mpls = true;
3713 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3714 if (ctx->xin->packet != NULL) {
3715 XLATE_REPORT_ERROR(ctx, "bridge %s: dropping packet on which an "
3716 "MPLS pop action can't be performed as it has "
3717 "more MPLS LSEs than the %d supported.",
3718 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3720 ctx->error = XLATE_TOO_MANY_MPLS_LABELS;
3721 ofpbuf_clear(ctx->odp_actions);
3726 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3728 struct flow *flow = &ctx->xin->flow;
3730 if (!is_ip_any(flow)) {
3734 ctx->wc->masks.nw_ttl = 0xff;
3735 if (flow->nw_ttl > 1) {
3741 for (i = 0; i < ids->n_controllers; i++) {
3742 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3746 /* Stop processing for current table. */
3752 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3754 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3755 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
3756 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
3761 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3763 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3764 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
3765 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
3770 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
3772 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3773 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3774 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
3779 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
3781 struct flow *flow = &ctx->xin->flow;
3783 if (eth_type_mpls(flow->dl_type)) {
3784 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
3786 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3789 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3792 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
3796 /* Stop processing for current table. */
3801 xlate_output_action(struct xlate_ctx *ctx,
3802 ofp_port_t port, uint16_t max_len, bool may_packet_in)
3804 ofp_port_t prev_nf_output_iface = ctx->nf_output_iface;
3806 ctx->nf_output_iface = NF_OUT_DROP;
3810 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port, NULL);
3813 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3814 0, may_packet_in, true);
3820 flood_packets(ctx, false);
3823 flood_packets(ctx, true);
3825 case OFPP_CONTROLLER:
3826 execute_controller_action(ctx, max_len,
3827 (ctx->in_group ? OFPR_GROUP
3828 : ctx->in_action_set ? OFPR_ACTION_SET
3836 if (port != ctx->xin->flow.in_port.ofp_port) {
3837 compose_output_action(ctx, port, NULL);
3839 xlate_report(ctx, "skipping output to input port");
3844 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3845 ctx->nf_output_iface = NF_OUT_FLOOD;
3846 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3847 ctx->nf_output_iface = prev_nf_output_iface;
3848 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3849 ctx->nf_output_iface != NF_OUT_FLOOD) {
3850 ctx->nf_output_iface = NF_OUT_MULTI;
3855 xlate_output_reg_action(struct xlate_ctx *ctx,
3856 const struct ofpact_output_reg *or)
3858 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3859 if (port <= UINT16_MAX) {
3860 union mf_subvalue value;
3862 memset(&value, 0xff, sizeof value);
3863 mf_write_subfield_flow(&or->src, &value, &ctx->wc->masks);
3864 xlate_output_action(ctx, u16_to_ofp(port),
3865 or->max_len, false);
3870 xlate_enqueue_action(struct xlate_ctx *ctx,
3871 const struct ofpact_enqueue *enqueue)
3873 ofp_port_t ofp_port = enqueue->port;
3874 uint32_t queue_id = enqueue->queue;
3875 uint32_t flow_priority, priority;
3878 /* Translate queue to priority. */
3879 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
3881 /* Fall back to ordinary output action. */
3882 xlate_output_action(ctx, enqueue->port, 0, false);
3886 /* Check output port. */
3887 if (ofp_port == OFPP_IN_PORT) {
3888 ofp_port = ctx->xin->flow.in_port.ofp_port;
3889 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
3893 /* Add datapath actions. */
3894 flow_priority = ctx->xin->flow.skb_priority;
3895 ctx->xin->flow.skb_priority = priority;
3896 compose_output_action(ctx, ofp_port, NULL);
3897 ctx->xin->flow.skb_priority = flow_priority;
3899 /* Update NetFlow output port. */
3900 if (ctx->nf_output_iface == NF_OUT_DROP) {
3901 ctx->nf_output_iface = ofp_port;
3902 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3903 ctx->nf_output_iface = NF_OUT_MULTI;
3908 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3910 uint32_t skb_priority;
3912 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
3913 ctx->xin->flow.skb_priority = skb_priority;
3915 /* Couldn't translate queue to a priority. Nothing to do. A warning
3916 * has already been logged. */
3921 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
3923 const struct xbridge *xbridge = xbridge_;
3934 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3937 port = get_ofp_port(xbridge, ofp_port);
3938 return port ? port->may_enable : false;
3943 xlate_bundle_action(struct xlate_ctx *ctx,
3944 const struct ofpact_bundle *bundle)
3948 port = bundle_execute(bundle, &ctx->xin->flow, ctx->wc, slave_enabled_cb,
3949 CONST_CAST(struct xbridge *, ctx->xbridge));
3950 if (bundle->dst.field) {
3951 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow, ctx->wc);
3953 xlate_output_action(ctx, port, 0, false);
3958 xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
3959 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
3961 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
3962 if (ctx->xin->may_learn) {
3963 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
3968 xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
3970 learn_mask(learn, ctx->wc);
3972 if (ctx->xin->xcache) {
3973 struct xc_entry *entry;
3975 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
3976 entry->u.learn.ofproto = ctx->xbridge->ofproto;
3977 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
3978 entry->u.learn.ofpacts = ofpbuf_new(64);
3979 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
3980 entry->u.learn.ofpacts);
3981 } else if (ctx->xin->may_learn) {
3982 uint64_t ofpacts_stub[1024 / 8];
3983 struct ofputil_flow_mod fm;
3984 struct ofpbuf ofpacts;
3986 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3987 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
3988 ofpbuf_uninit(&ofpacts);
3993 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
3994 uint16_t idle_timeout, uint16_t hard_timeout)
3996 if (tcp_flags & (TCP_FIN | TCP_RST)) {
3997 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
4002 xlate_fin_timeout(struct xlate_ctx *ctx,
4003 const struct ofpact_fin_timeout *oft)
4006 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
4007 oft->fin_idle_timeout, oft->fin_hard_timeout);
4008 if (ctx->xin->xcache) {
4009 struct xc_entry *entry;
4011 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
4012 /* XC_RULE already holds a reference on the rule, none is taken
4014 entry->u.fin.rule = ctx->rule;
4015 entry->u.fin.idle = oft->fin_idle_timeout;
4016 entry->u.fin.hard = oft->fin_hard_timeout;
4022 xlate_sample_action(struct xlate_ctx *ctx,
4023 const struct ofpact_sample *os)
4025 /* Scale the probability from 16-bit to 32-bit while representing
4026 * the same percentage. */
4027 uint32_t probability = (os->probability << 16) | os->probability;
4029 if (!ctx->xbridge->support.variable_length_userdata) {
4030 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4032 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
4033 "lacks support (needs Linux 3.10+ or kernel module from "
4038 xlate_commit_actions(ctx);
4040 union user_action_cookie cookie = {
4042 .type = USER_ACTION_COOKIE_FLOW_SAMPLE,
4043 .probability = os->probability,
4044 .collector_set_id = os->collector_set_id,
4045 .obs_domain_id = os->obs_domain_id,
4046 .obs_point_id = os->obs_point_id,
4049 compose_sample_action(ctx, probability, &cookie, sizeof cookie.flow_sample,
4054 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
4056 if (xport->config & (is_stp(&ctx->xin->flow)
4057 ? OFPUTIL_PC_NO_RECV_STP
4058 : OFPUTIL_PC_NO_RECV)) {
4062 /* Only drop packets here if both forwarding and learning are
4063 * disabled. If just learning is enabled, we need to have
4064 * OFPP_NORMAL and the learning action have a look at the packet
4065 * before we can drop it. */
4066 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
4067 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
4075 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
4077 const struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
4078 size_t on_len = ofpact_nest_get_action_len(on);
4079 const struct ofpact *inner;
4081 /* Maintain actset_output depending on the contents of the action set:
4083 * - OFPP_UNSET, if there is no "output" action.
4085 * - The output port, if there is an "output" action and no "group"
4088 * - OFPP_UNSET, if there is a "group" action.
4090 if (!ctx->action_set_has_group) {
4091 OFPACT_FOR_EACH (inner, on->actions, on_len) {
4092 if (inner->type == OFPACT_OUTPUT) {
4093 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(inner)->port;
4094 } else if (inner->type == OFPACT_GROUP) {
4095 ctx->xin->flow.actset_output = OFPP_UNSET;
4096 ctx->action_set_has_group = true;
4102 ofpbuf_put(&ctx->action_set, on->actions, on_len);
4103 ofpact_pad(&ctx->action_set);
4107 xlate_action_set(struct xlate_ctx *ctx)
4109 uint64_t action_list_stub[1024 / 64];
4110 struct ofpbuf action_list;
4112 ctx->in_action_set = true;
4113 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
4114 ofpacts_execute_action_set(&action_list, &ctx->action_set);
4115 /* Clear the action set, as it is not needed any more. */
4116 ofpbuf_clear(&ctx->action_set);
4117 do_xlate_actions(action_list.data, action_list.size, ctx);
4118 ctx->in_action_set = false;
4119 ofpbuf_uninit(&action_list);
4123 recirc_put_unroll_xlate(struct xlate_ctx *ctx)
4125 struct ofpact_unroll_xlate *unroll;
4127 unroll = ctx->last_unroll_offset < 0
4129 : ALIGNED_CAST(struct ofpact_unroll_xlate *,
4130 (char *)ctx->action_set.data + ctx->last_unroll_offset);
4132 /* Restore the table_id and rule cookie for a potential PACKET
4135 (ctx->table_id != unroll->rule_table_id
4136 || ctx->rule_cookie != unroll->rule_cookie)) {
4138 ctx->last_unroll_offset = ctx->action_set.size;
4139 unroll = ofpact_put_UNROLL_XLATE(&ctx->action_set);
4140 unroll->rule_table_id = ctx->table_id;
4141 unroll->rule_cookie = ctx->rule_cookie;
4146 /* Copy remaining actions to the action_set to be executed after recirculation.
4147 * UNROLL_XLATE action is inserted, if not already done so, before actions that
4148 * may generate PACKET_INs from the current table and without matching another
4151 recirc_unroll_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4152 struct xlate_ctx *ctx)
4154 const struct ofpact *a;
4156 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4158 /* May generate PACKET INs. */
4159 case OFPACT_OUTPUT_REG:
4162 case OFPACT_CONTROLLER:
4163 case OFPACT_DEC_MPLS_TTL:
4164 case OFPACT_DEC_TTL:
4165 recirc_put_unroll_xlate(ctx);
4168 /* These may not generate PACKET INs. */
4169 case OFPACT_SET_TUNNEL:
4170 case OFPACT_REG_MOVE:
4171 case OFPACT_SET_FIELD:
4172 case OFPACT_STACK_PUSH:
4173 case OFPACT_STACK_POP:
4175 case OFPACT_WRITE_METADATA:
4176 case OFPACT_RESUBMIT: /* May indirectly generate PACKET INs, */
4177 case OFPACT_GOTO_TABLE: /* but from a different table and rule. */
4178 case OFPACT_ENQUEUE:
4179 case OFPACT_SET_VLAN_VID:
4180 case OFPACT_SET_VLAN_PCP:
4181 case OFPACT_STRIP_VLAN:
4182 case OFPACT_PUSH_VLAN:
4183 case OFPACT_SET_ETH_SRC:
4184 case OFPACT_SET_ETH_DST:
4185 case OFPACT_SET_IPV4_SRC:
4186 case OFPACT_SET_IPV4_DST:
4187 case OFPACT_SET_IP_DSCP:
4188 case OFPACT_SET_IP_ECN:
4189 case OFPACT_SET_IP_TTL:
4190 case OFPACT_SET_L4_SRC_PORT:
4191 case OFPACT_SET_L4_DST_PORT:
4192 case OFPACT_SET_QUEUE:
4193 case OFPACT_POP_QUEUE:
4194 case OFPACT_PUSH_MPLS:
4195 case OFPACT_POP_MPLS:
4196 case OFPACT_SET_MPLS_LABEL:
4197 case OFPACT_SET_MPLS_TC:
4198 case OFPACT_SET_MPLS_TTL:
4199 case OFPACT_MULTIPATH:
4202 case OFPACT_UNROLL_XLATE:
4203 case OFPACT_FIN_TIMEOUT:
4204 case OFPACT_CLEAR_ACTIONS:
4205 case OFPACT_WRITE_ACTIONS:
4208 case OFPACT_DEBUG_RECIRC:
4213 /* These need not be copied for restoration. */
4215 case OFPACT_CONJUNCTION:
4218 /* Copy the action over. */
4219 ofpbuf_put(&ctx->action_set, a, OFPACT_ALIGN(a->len));
4223 #define CHECK_MPLS_RECIRCULATION() \
4224 if (ctx->was_mpls) { \
4225 ctx_trigger_recirculation(ctx); \
4228 #define CHECK_MPLS_RECIRCULATION_IF(COND) \
4230 CHECK_MPLS_RECIRCULATION(); \
4234 put_ct_mark(const struct flow *flow, struct flow *base_flow,
4235 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4242 odp_attr.key = flow->ct_mark;
4243 odp_attr.mask = wc->masks.ct_mark;
4245 if (odp_attr.mask && odp_attr.key != base_flow->ct_mark) {
4246 nl_msg_put_unspec(odp_actions, OVS_CT_ATTR_MARK, &odp_attr,
4252 put_ct_label(const struct flow *flow, struct flow *base_flow,
4253 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4255 if (!ovs_u128_is_zero(&wc->masks.ct_label)
4256 && !ovs_u128_equals(&flow->ct_label, &base_flow->ct_label)) {
4262 odp_ct_label = nl_msg_put_unspec_uninit(odp_actions,
4264 sizeof(*odp_ct_label));
4265 odp_ct_label->key = flow->ct_label;
4266 odp_ct_label->mask = wc->masks.ct_label;
4271 put_ct_helper(struct ofpbuf *odp_actions, struct ofpact_conntrack *ofc)
4274 if (ofc->alg == IPPORT_FTP) {
4275 nl_msg_put_string(odp_actions, OVS_CT_ATTR_HELPER, "ftp");
4277 VLOG_WARN("Cannot serialize ct_helper %d\n", ofc->alg);
4283 put_ct_nat(struct xlate_ctx *ctx)
4285 struct ofpact_nat *ofn = ctx->ct_nat_action;
4292 nat_offset = nl_msg_start_nested(ctx->odp_actions, OVS_CT_ATTR_NAT);
4293 if (ofn->flags & NX_NAT_F_SRC || ofn->flags & NX_NAT_F_DST) {
4294 nl_msg_put_flag(ctx->odp_actions, ofn->flags & NX_NAT_F_SRC
4295 ? OVS_NAT_ATTR_SRC : OVS_NAT_ATTR_DST);
4296 if (ofn->flags & NX_NAT_F_PERSISTENT) {
4297 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PERSISTENT);
4299 if (ofn->flags & NX_NAT_F_PROTO_HASH) {
4300 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PROTO_HASH);
4301 } else if (ofn->flags & NX_NAT_F_PROTO_RANDOM) {
4302 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PROTO_RANDOM);
4304 if (ofn->range_af == AF_INET) {
4305 nl_msg_put_be32(ctx->odp_actions, OVS_NAT_ATTR_IP_MIN,
4306 ofn->range.addr.ipv4.min);
4307 if (ofn->range.addr.ipv4.max &&
4308 (ntohl(ofn->range.addr.ipv4.max)
4309 > ntohl(ofn->range.addr.ipv4.min))) {
4310 nl_msg_put_be32(ctx->odp_actions, OVS_NAT_ATTR_IP_MAX,
4311 ofn->range.addr.ipv4.max);
4313 } else if (ofn->range_af == AF_INET6) {
4314 nl_msg_put_unspec(ctx->odp_actions, OVS_NAT_ATTR_IP_MIN,
4315 &ofn->range.addr.ipv6.min,
4316 sizeof ofn->range.addr.ipv6.min);
4317 if (!ipv6_mask_is_any(&ofn->range.addr.ipv6.max) &&
4318 memcmp(&ofn->range.addr.ipv6.max, &ofn->range.addr.ipv6.min,
4319 sizeof ofn->range.addr.ipv6.max) > 0) {
4320 nl_msg_put_unspec(ctx->odp_actions, OVS_NAT_ATTR_IP_MAX,
4321 &ofn->range.addr.ipv6.max,
4322 sizeof ofn->range.addr.ipv6.max);
4325 if (ofn->range_af != AF_UNSPEC && ofn->range.proto.min) {
4326 nl_msg_put_u16(ctx->odp_actions, OVS_NAT_ATTR_PROTO_MIN,
4327 ofn->range.proto.min);
4328 if (ofn->range.proto.max &&
4329 ofn->range.proto.max > ofn->range.proto.min) {
4330 nl_msg_put_u16(ctx->odp_actions, OVS_NAT_ATTR_PROTO_MAX,
4331 ofn->range.proto.max);
4335 nl_msg_end_nested(ctx->odp_actions, nat_offset);
4339 compose_conntrack_action(struct xlate_ctx *ctx, struct ofpact_conntrack *ofc)
4341 ovs_u128 old_ct_label = ctx->base_flow.ct_label;
4342 uint32_t old_ct_mark = ctx->base_flow.ct_mark;
4346 /* Ensure that any prior actions are applied before composing the new
4347 * conntrack action. */
4348 xlate_commit_actions(ctx);
4350 /* Process nested actions first, to populate the key. */
4351 ctx->ct_nat_action = NULL;
4352 do_xlate_actions(ofc->actions, ofpact_ct_get_action_len(ofc), ctx);
4354 if (ofc->zone_src.field) {
4355 zone = mf_get_subfield(&ofc->zone_src, &ctx->xin->flow);
4357 zone = ofc->zone_imm;
4360 ct_offset = nl_msg_start_nested(ctx->odp_actions, OVS_ACTION_ATTR_CT);
4361 if (ofc->flags & NX_CT_F_COMMIT) {
4362 nl_msg_put_flag(ctx->odp_actions, OVS_CT_ATTR_COMMIT);
4364 nl_msg_put_u16(ctx->odp_actions, OVS_CT_ATTR_ZONE, zone);
4365 put_ct_mark(&ctx->xin->flow, &ctx->base_flow, ctx->odp_actions, ctx->wc);
4366 put_ct_label(&ctx->xin->flow, &ctx->base_flow, ctx->odp_actions, ctx->wc);
4367 put_ct_helper(ctx->odp_actions, ofc);
4369 ctx->ct_nat_action = NULL;
4370 nl_msg_end_nested(ctx->odp_actions, ct_offset);
4372 /* Restore the original ct fields in the key. These should only be exposed
4373 * after recirculation to another table. */
4374 ctx->base_flow.ct_mark = old_ct_mark;
4375 ctx->base_flow.ct_label = old_ct_label;
4377 if (ofc->recirc_table == NX_CT_RECIRC_NONE) {
4378 /* If we do not recirculate as part of this action, hide the results of
4379 * connection tracking from subsequent recirculations. */
4380 ctx->conntracked = false;
4382 /* Use ct_* fields from datapath during recirculation upcall. */
4383 ctx->conntracked = true;
4384 compose_recirculate_and_fork(ctx, ofc->recirc_table);
4389 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4390 struct xlate_ctx *ctx)
4392 struct flow_wildcards *wc = ctx->wc;
4393 struct flow *flow = &ctx->xin->flow;
4394 const struct ofpact *a;
4396 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
4397 tnl_neigh_snoop(flow, wc, ctx->xbridge->name);
4399 /* dl_type already in the mask, not set below. */
4401 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4402 struct ofpact_controller *controller;
4403 const struct ofpact_metadata *metadata;
4404 const struct ofpact_set_field *set_field;
4405 const struct mf_field *mf;
4412 /* Check if need to store the remaining actions for later
4414 if (exit_recirculates(ctx)) {
4415 recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
4417 (uint8_t *)ofpacts)),
4425 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
4426 ofpact_get_OUTPUT(a)->max_len, true);
4430 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
4431 /* Group could not be found. */
4436 case OFPACT_CONTROLLER:
4437 controller = ofpact_get_CONTROLLER(a);
4438 execute_controller_action(ctx, controller->max_len,
4440 controller->controller_id);
4443 case OFPACT_ENQUEUE:
4444 memset(&wc->masks.skb_priority, 0xff,
4445 sizeof wc->masks.skb_priority);
4446 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
4449 case OFPACT_SET_VLAN_VID:
4450 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
4451 if (flow->vlan_tci & htons(VLAN_CFI) ||
4452 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
4453 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
4454 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
4459 case OFPACT_SET_VLAN_PCP:
4460 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
4461 if (flow->vlan_tci & htons(VLAN_CFI) ||
4462 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
4463 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
4464 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
4465 << VLAN_PCP_SHIFT) | VLAN_CFI);
4469 case OFPACT_STRIP_VLAN:
4470 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4471 flow->vlan_tci = htons(0);
4474 case OFPACT_PUSH_VLAN:
4475 /* XXX 802.1AD(QinQ) */
4476 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4477 flow->vlan_tci = htons(VLAN_CFI);
4480 case OFPACT_SET_ETH_SRC:
4481 WC_MASK_FIELD(wc, dl_src);
4482 flow->dl_src = ofpact_get_SET_ETH_SRC(a)->mac;
4485 case OFPACT_SET_ETH_DST:
4486 WC_MASK_FIELD(wc, dl_dst);
4487 flow->dl_dst = ofpact_get_SET_ETH_DST(a)->mac;
4490 case OFPACT_SET_IPV4_SRC:
4491 CHECK_MPLS_RECIRCULATION();
4492 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4493 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
4494 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
4498 case OFPACT_SET_IPV4_DST:
4499 CHECK_MPLS_RECIRCULATION();
4500 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4501 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
4502 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
4506 case OFPACT_SET_IP_DSCP:
4507 CHECK_MPLS_RECIRCULATION();
4508 if (is_ip_any(flow)) {
4509 wc->masks.nw_tos |= IP_DSCP_MASK;
4510 flow->nw_tos &= ~IP_DSCP_MASK;
4511 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
4515 case OFPACT_SET_IP_ECN:
4516 CHECK_MPLS_RECIRCULATION();
4517 if (is_ip_any(flow)) {
4518 wc->masks.nw_tos |= IP_ECN_MASK;
4519 flow->nw_tos &= ~IP_ECN_MASK;
4520 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
4524 case OFPACT_SET_IP_TTL:
4525 CHECK_MPLS_RECIRCULATION();
4526 if (is_ip_any(flow)) {
4527 wc->masks.nw_ttl = 0xff;
4528 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
4532 case OFPACT_SET_L4_SRC_PORT:
4533 CHECK_MPLS_RECIRCULATION();
4534 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4535 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4536 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
4537 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
4541 case OFPACT_SET_L4_DST_PORT:
4542 CHECK_MPLS_RECIRCULATION();
4543 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4544 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4545 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
4546 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
4550 case OFPACT_RESUBMIT:
4551 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
4554 case OFPACT_SET_TUNNEL:
4555 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
4558 case OFPACT_SET_QUEUE:
4559 memset(&wc->masks.skb_priority, 0xff,
4560 sizeof wc->masks.skb_priority);
4561 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
4564 case OFPACT_POP_QUEUE:
4565 memset(&wc->masks.skb_priority, 0xff,
4566 sizeof wc->masks.skb_priority);
4567 flow->skb_priority = ctx->orig_skb_priority;
4570 case OFPACT_REG_MOVE:
4571 CHECK_MPLS_RECIRCULATION_IF(
4572 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
4573 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
4574 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
4577 case OFPACT_SET_FIELD:
4578 CHECK_MPLS_RECIRCULATION_IF(
4579 mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field));
4580 set_field = ofpact_get_SET_FIELD(a);
4581 mf = set_field->field;
4583 /* Set field action only ever overwrites packet's outermost
4584 * applicable header fields. Do nothing if no header exists. */
4585 if (mf->id == MFF_VLAN_VID) {
4586 wc->masks.vlan_tci |= htons(VLAN_CFI);
4587 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4590 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
4591 /* 'dl_type' is already unwildcarded. */
4592 && !eth_type_mpls(flow->dl_type)) {
4595 /* A flow may wildcard nw_frag. Do nothing if setting a transport
4596 * header field on a packet that does not have them. */
4597 mf_mask_field_and_prereqs(mf, wc);
4598 if (mf_are_prereqs_ok(mf, flow)) {
4599 mf_set_flow_value_masked(mf, &set_field->value,
4600 &set_field->mask, flow);
4604 case OFPACT_STACK_PUSH:
4605 CHECK_MPLS_RECIRCULATION_IF(
4606 mf_is_l3_or_higher(ofpact_get_STACK_PUSH(a)->subfield.field));
4607 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
4611 case OFPACT_STACK_POP:
4612 CHECK_MPLS_RECIRCULATION_IF(
4613 mf_is_l3_or_higher(ofpact_get_STACK_POP(a)->subfield.field));
4614 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
4618 case OFPACT_PUSH_MPLS:
4619 /* Recirculate if it is an IP packet with a zero ttl. This may
4620 * indicate that the packet was previously MPLS and an MPLS pop
4621 * action converted it to IP. In this case recirculating should
4622 * reveal the IP TTL which is used as the basis for a new MPLS
4624 CHECK_MPLS_RECIRCULATION_IF(
4625 !flow_count_mpls_labels(flow, wc)
4626 && flow->nw_ttl == 0
4627 && is_ip_any(flow));
4628 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
4631 case OFPACT_POP_MPLS:
4632 CHECK_MPLS_RECIRCULATION();
4633 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
4636 case OFPACT_SET_MPLS_LABEL:
4637 CHECK_MPLS_RECIRCULATION();
4638 compose_set_mpls_label_action(
4639 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
4642 case OFPACT_SET_MPLS_TC:
4643 CHECK_MPLS_RECIRCULATION();
4644 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
4647 case OFPACT_SET_MPLS_TTL:
4648 CHECK_MPLS_RECIRCULATION();
4649 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
4652 case OFPACT_DEC_MPLS_TTL:
4653 CHECK_MPLS_RECIRCULATION();
4654 if (compose_dec_mpls_ttl_action(ctx)) {
4659 case OFPACT_DEC_TTL:
4660 CHECK_MPLS_RECIRCULATION();
4661 wc->masks.nw_ttl = 0xff;
4662 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
4668 /* Nothing to do. */
4671 case OFPACT_MULTIPATH:
4672 CHECK_MPLS_RECIRCULATION();
4673 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
4677 CHECK_MPLS_RECIRCULATION();
4678 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
4681 case OFPACT_OUTPUT_REG:
4682 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
4686 CHECK_MPLS_RECIRCULATION();
4687 xlate_learn_action(ctx, ofpact_get_LEARN(a));
4690 case OFPACT_CONJUNCTION: {
4691 /* A flow with a "conjunction" action represents part of a special
4692 * kind of "set membership match". Such a flow should not actually
4693 * get executed, but it could via, say, a "packet-out", even though
4694 * that wouldn't be useful. Log it to help debugging. */
4695 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4696 VLOG_INFO_RL(&rl, "executing no-op conjunction action");
4704 case OFPACT_UNROLL_XLATE: {
4705 struct ofpact_unroll_xlate *unroll = ofpact_get_UNROLL_XLATE(a);
4707 /* Restore translation context data that was stored earlier. */
4708 ctx->table_id = unroll->rule_table_id;
4709 ctx->rule_cookie = unroll->rule_cookie;
4712 case OFPACT_FIN_TIMEOUT:
4713 CHECK_MPLS_RECIRCULATION();
4714 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4715 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
4718 case OFPACT_CLEAR_ACTIONS:
4719 ofpbuf_clear(&ctx->action_set);
4720 ctx->xin->flow.actset_output = OFPP_UNSET;
4721 ctx->action_set_has_group = false;
4724 case OFPACT_WRITE_ACTIONS:
4725 xlate_write_actions(ctx, a);
4728 case OFPACT_WRITE_METADATA:
4729 metadata = ofpact_get_WRITE_METADATA(a);
4730 flow->metadata &= ~metadata->mask;
4731 flow->metadata |= metadata->metadata & metadata->mask;
4735 /* Not implemented yet. */
4738 case OFPACT_GOTO_TABLE: {
4739 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
4741 /* Allow ctx->table_id == TBL_INTERNAL, which will be greater
4742 * than ogt->table_id. This is to allow goto_table actions that
4743 * triggered recirculation: ctx->table_id will be TBL_INTERNAL
4744 * after recirculation. */
4745 ovs_assert(ctx->table_id == TBL_INTERNAL
4746 || ctx->table_id < ogt->table_id);
4747 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
4748 ogt->table_id, true, true);
4753 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
4757 CHECK_MPLS_RECIRCULATION();
4758 compose_conntrack_action(ctx, ofpact_get_CT(a));
4762 /* This will be processed by compose_conntrack_action(). */
4763 ctx->ct_nat_action = ofpact_get_NAT(a);
4766 case OFPACT_DEBUG_RECIRC:
4767 ctx_trigger_recirculation(ctx);
4772 /* Check if need to store this and the remaining actions for later
4774 if (!ctx->error && ctx->exit && ctx_first_recirculation_action(ctx)) {
4775 recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
4777 (uint8_t *)ofpacts)),
4785 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
4786 const struct flow *flow, ofp_port_t in_port,
4787 struct rule_dpif *rule, uint16_t tcp_flags,
4788 const struct dp_packet *packet, struct flow_wildcards *wc,
4789 struct ofpbuf *odp_actions)
4791 xin->ofproto = ofproto;
4793 xin->flow.in_port.ofp_port = in_port;
4794 xin->flow.actset_output = OFPP_UNSET;
4795 xin->packet = packet;
4796 xin->may_learn = packet != NULL;
4799 xin->ofpacts = NULL;
4800 xin->ofpacts_len = 0;
4801 xin->tcp_flags = tcp_flags;
4802 xin->resubmit_hook = NULL;
4803 xin->report_hook = NULL;
4804 xin->resubmit_stats = NULL;
4808 xin->odp_actions = odp_actions;
4810 /* Do recirc lookup. */
4811 xin->recirc = flow->recirc_id
4812 ? recirc_id_node_find(flow->recirc_id)
4817 xlate_out_uninit(struct xlate_out *xout)
4820 recirc_refs_unref(&xout->recircs);
4824 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
4825 * into datapath actions, using 'ctx', and discards the datapath actions. */
4827 xlate_actions_for_side_effects(struct xlate_in *xin)
4829 struct xlate_out xout;
4830 enum xlate_error error;
4832 error = xlate_actions(xin, &xout);
4834 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4836 VLOG_WARN_RL(&rl, "xlate_actions failed (%s)!", xlate_strerror(error));
4839 xlate_out_uninit(&xout);
4842 static struct skb_priority_to_dscp *
4843 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
4845 struct skb_priority_to_dscp *pdscp;
4848 hash = hash_int(skb_priority, 0);
4849 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
4850 if (pdscp->skb_priority == skb_priority) {
4858 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
4861 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
4862 *dscp = pdscp ? pdscp->dscp : 0;
4863 return pdscp != NULL;
4867 count_skb_priorities(const struct xport *xport)
4869 return hmap_count(&xport->skb_priorities);
4873 clear_skb_priorities(struct xport *xport)
4875 struct skb_priority_to_dscp *pdscp, *next;
4877 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
4878 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
4884 actions_output_to_local_port(const struct xlate_ctx *ctx)
4886 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
4887 const struct nlattr *a;
4890 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->odp_actions->data,
4891 ctx->odp_actions->size) {
4892 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
4893 && nl_attr_get_odp_port(a) == local_odp_port) {
4900 #if defined(__linux__)
4901 /* Returns the maximum number of packets that the Linux kernel is willing to
4902 * queue up internally to certain kinds of software-implemented ports, or the
4903 * default (and rarely modified) value if it cannot be determined. */
4905 netdev_max_backlog(void)
4907 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
4908 static int max_backlog = 1000; /* The normal default value. */
4910 if (ovsthread_once_start(&once)) {
4911 static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
4915 stream = fopen(filename, "r");
4917 VLOG_INFO("%s: open failed (%s)", filename, ovs_strerror(errno));
4919 if (fscanf(stream, "%d", &n) != 1) {
4920 VLOG_WARN("%s: read error", filename);
4921 } else if (n <= 100) {
4922 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
4928 ovsthread_once_done(&once);
4930 VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
4936 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
4939 count_output_actions(const struct ofpbuf *odp_actions)
4941 const struct nlattr *a;
4945 NL_ATTR_FOR_EACH_UNSAFE (a, left, odp_actions->data, odp_actions->size) {
4946 if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
4952 #endif /* defined(__linux__) */
4954 /* Returns true if 'odp_actions' contains more output actions than the datapath
4955 * can reliably handle in one go. On Linux, this is the value of the
4956 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
4957 * packets that the kernel is willing to queue up for processing while the
4958 * datapath is processing a set of actions. */
4960 too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
4963 return (odp_actions->size / NL_A_U32_SIZE > netdev_max_backlog()
4964 && count_output_actions(odp_actions) > netdev_max_backlog());
4966 /* OSes other than Linux might have similar limits, but we don't know how
4967 * to determine them.*/
4973 xlate_wc_init(struct xlate_ctx *ctx)
4975 flow_wildcards_init_catchall(ctx->wc);
4977 /* Some fields we consider to always be examined. */
4978 WC_MASK_FIELD(ctx->wc, in_port);
4979 WC_MASK_FIELD(ctx->wc, dl_type);
4980 if (is_ip_any(&ctx->xin->flow)) {
4981 WC_MASK_FIELD_MASK(ctx->wc, nw_frag, FLOW_NW_FRAG_MASK);
4984 if (ctx->xbridge->support.odp.recirc) {
4985 /* Always exactly match recirc_id when datapath supports
4987 WC_MASK_FIELD(ctx->wc, recirc_id);
4990 if (ctx->xbridge->netflow) {
4991 netflow_mask_wc(&ctx->xin->flow, ctx->wc);
4994 tnl_wc_init(&ctx->xin->flow, ctx->wc);
4998 xlate_wc_finish(struct xlate_ctx *ctx)
5000 /* Clear the metadata and register wildcard masks, because we won't
5001 * use non-header fields as part of the cache. */
5002 flow_wildcards_clear_non_packet_fields(ctx->wc);
5004 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
5005 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
5006 * represent these fields. The datapath interface, on the other hand,
5007 * represents them with just 8 bits each. This means that if the high
5008 * 8 bits of the masks for these fields somehow become set, then they
5009 * will get chopped off by a round trip through the datapath, and
5010 * revalidation will spot that as an inconsistency and delete the flow.
5011 * Avoid the problem here by making sure that only the low 8 bits of
5012 * either field can be unwildcarded for ICMP.
5014 if (is_icmpv4(&ctx->xin->flow) || is_icmpv6(&ctx->xin->flow)) {
5015 ctx->wc->masks.tp_src &= htons(UINT8_MAX);
5016 ctx->wc->masks.tp_dst &= htons(UINT8_MAX);
5018 /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
5019 if (ctx->wc->masks.vlan_tci) {
5020 ctx->wc->masks.vlan_tci |= htons(VLAN_CFI);
5024 /* Translates the flow, actions, or rule in 'xin' into datapath actions in
5026 * The caller must take responsibility for eventually freeing 'xout', with
5027 * xlate_out_uninit().
5028 * Returns 'XLATE_OK' if translation was successful. In case of an error an
5029 * empty set of actions will be returned in 'xin->odp_actions' (if non-NULL),
5030 * so that most callers may ignore the return value and transparently install a
5031 * drop flow when the translation fails. */
5033 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
5035 *xout = (struct xlate_out) {
5038 .recircs = RECIRC_REFS_EMPTY_INITIALIZER,
5041 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5042 struct xbridge *xbridge = xbridge_lookup(xcfg, xin->ofproto);
5044 return XLATE_BRIDGE_NOT_FOUND;
5047 struct flow *flow = &xin->flow;
5049 union mf_subvalue stack_stub[1024 / sizeof(union mf_subvalue)];
5050 uint64_t action_set_stub[1024 / 8];
5051 struct flow_wildcards scratch_wc;
5052 uint64_t actions_stub[256 / 8];
5053 struct ofpbuf scratch_actions = OFPBUF_STUB_INITIALIZER(actions_stub);
5054 struct xlate_ctx ctx = {
5058 .orig_tunnel_ipv6_dst = flow_tnl_dst(&flow->tunnel),
5060 .stack = OFPBUF_STUB_INITIALIZER(stack_stub),
5062 .wc = xin->wc ? xin->wc : &scratch_wc,
5063 .odp_actions = xin->odp_actions ? xin->odp_actions : &scratch_actions,
5065 .recurse = xin->recurse,
5066 .resubmits = xin->resubmits,
5068 .in_action_set = false,
5071 .rule_cookie = OVS_BE64_MAX,
5072 .orig_skb_priority = flow->skb_priority,
5073 .sflow_n_outputs = 0,
5074 .sflow_odp_port = 0,
5075 .nf_output_iface = NF_OUT_DROP,
5080 .recirc_action_offset = -1,
5081 .last_unroll_offset = -1,
5084 .conntracked = false,
5086 .ct_nat_action = NULL,
5088 .action_set_has_group = false,
5089 .action_set = OFPBUF_STUB_INITIALIZER(action_set_stub),
5092 /* 'base_flow' reflects the packet as it came in, but we need it to reflect
5093 * the packet as the datapath will treat it for output actions:
5095 * - Our datapath doesn't retain tunneling information without us
5096 * re-setting it, so clear the tunnel data.
5098 * - For VLAN splinters, a higher layer may pretend that the packet
5099 * came in on 'flow->in_port.ofp_port' with 'flow->vlan_tci'
5100 * attached, because that's how we want to treat it from an OpenFlow
5101 * perspective. But from the datapath's perspective it actually came
5102 * in on a VLAN device without any VLAN attached. So here we put the
5103 * datapath's view of the VLAN information in 'base_flow' to ensure
5104 * correct treatment.
5106 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
5107 if (flow->in_port.ofp_port
5108 != vsp_realdev_to_vlandev(xbridge->ofproto,
5109 flow->in_port.ofp_port,
5111 ctx.base_flow.vlan_tci = 0;
5114 ofpbuf_reserve(ctx.odp_actions, NL_A_U32_SIZE);
5116 xlate_wc_init(&ctx);
5119 COVERAGE_INC(xlate_actions);
5122 const struct recirc_state *state = &xin->recirc->state;
5124 xlate_report(&ctx, "Restoring state post-recirculation:");
5126 if (xin->ofpacts_len > 0 || ctx.rule) {
5127 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
5128 const char *conflict = xin->ofpacts_len ? "actions" : "rule";
5130 VLOG_WARN_RL(&rl, "Recirculation conflict (%s)!", conflict);
5131 xlate_report(&ctx, "- Recirculation conflict (%s)!", conflict);
5132 ctx.error = XLATE_RECIRCULATION_CONFLICT;
5136 /* Set the bridge for post-recirculation processing if needed. */
5137 if (ctx.xbridge->ofproto != state->ofproto) {
5138 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5139 const struct xbridge *new_bridge
5140 = xbridge_lookup(xcfg, state->ofproto);
5142 if (OVS_UNLIKELY(!new_bridge)) {
5143 /* Drop the packet if the bridge cannot be found. */
5144 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
5145 VLOG_WARN_RL(&rl, "Recirculation bridge no longer exists.");
5146 xlate_report(&ctx, "- Recirculation bridge no longer exists.");
5147 ctx.error = XLATE_BRIDGE_NOT_FOUND;
5150 ctx.xbridge = new_bridge;
5153 /* Set the post-recirculation table id. Note: A table lookup is done
5154 * only if there are no post-recirculation actions. */
5155 ctx.table_id = state->table_id;
5156 xlate_report(&ctx, "- Resuming from table %"PRIu8, ctx.table_id);
5158 if (!state->conntracked) {
5159 clear_conntrack(flow);
5162 /* Restore pipeline metadata. May change flow's in_port and other
5163 * metadata to the values that existed when recirculation was
5165 recirc_metadata_to_flow(&state->metadata, flow);
5167 /* Restore stack, if any. */
5169 ofpbuf_put(&ctx.stack, state->stack->data, state->stack->size);
5172 /* Restore mirror state. */
5173 ctx.mirrors = state->mirrors;
5175 /* Restore action set, if any. */
5176 if (state->action_set_len) {
5177 const struct ofpact *a;
5179 xlate_report_actions(&ctx, "- Restoring action set",
5180 state->ofpacts, state->action_set_len);
5182 ofpbuf_put(&ctx.action_set, state->ofpacts, state->action_set_len);
5184 OFPACT_FOR_EACH(a, state->ofpacts, state->action_set_len) {
5185 if (a->type == OFPACT_GROUP) {
5186 ctx.action_set_has_group = true;
5192 /* Restore recirculation actions. If there are no actions, processing
5193 * will start with a lookup in the table set above. */
5194 if (state->ofpacts_len > state->action_set_len) {
5195 xin->ofpacts_len = state->ofpacts_len - state->action_set_len;
5196 xin->ofpacts = state->ofpacts +
5197 state->action_set_len / sizeof *state->ofpacts;
5199 xlate_report_actions(&ctx, "- Restoring actions",
5200 xin->ofpacts, xin->ofpacts_len);
5202 } else if (OVS_UNLIKELY(flow->recirc_id)) {
5203 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
5205 VLOG_WARN_RL(&rl, "Recirculation context not found for ID %"PRIx32,
5207 ctx.error = XLATE_NO_RECIRCULATION_CONTEXT;
5210 /* The bridge is now known so obtain its table version. */
5211 ctx.tables_version = ofproto_dpif_get_tables_version(ctx.xbridge->ofproto);
5213 if (!xin->ofpacts && !ctx.rule) {
5214 ctx.rule = rule_dpif_lookup_from_table(
5215 ctx.xbridge->ofproto, ctx.tables_version, flow, xin->wc,
5216 ctx.xin->resubmit_stats, &ctx.table_id,
5217 flow->in_port.ofp_port, true, true);
5218 if (ctx.xin->resubmit_stats) {
5219 rule_dpif_credit_stats(ctx.rule, ctx.xin->resubmit_stats);
5221 if (ctx.xin->xcache) {
5222 struct xc_entry *entry;
5224 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
5225 entry->u.rule = ctx.rule;
5226 rule_dpif_ref(ctx.rule);
5229 if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
5230 ctx.xin->resubmit_hook(ctx.xin, ctx.rule, 0);
5233 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
5235 /* Get the proximate input port of the packet. (If xin->recirc,
5236 * flow->in_port is the ultimate input port of the packet.) */
5237 struct xport *in_port = get_ofp_port(xbridge,
5238 ctx.base_flow.in_port.ofp_port);
5240 /* Tunnel stats only for non-recirculated packets. */
5241 if (!xin->recirc && in_port && in_port->is_tunnel) {
5242 if (ctx.xin->resubmit_stats) {
5243 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
5245 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
5248 if (ctx.xin->xcache) {
5249 struct xc_entry *entry;
5251 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
5252 entry->u.dev.rx = netdev_ref(in_port->netdev);
5253 entry->u.dev.bfd = bfd_ref(in_port->bfd);
5257 if (!xin->recirc && process_special(&ctx, in_port)) {
5258 /* process_special() did all the processing for this packet.
5260 * We do not perform special processing on recirculated packets, as
5261 * recirculated packets are not really received by the bridge.*/
5262 } else if (in_port && in_port->xbundle
5263 && xbundle_mirror_out(xbridge, in_port->xbundle)) {
5264 if (ctx.xin->packet != NULL) {
5265 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5266 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
5267 "%s, which is reserved exclusively for mirroring",
5268 ctx.xbridge->name, in_port->xbundle->name);
5271 /* Sampling is done only for packets really received by the bridge. */
5272 unsigned int user_cookie_offset = 0;
5274 user_cookie_offset = compose_sflow_action(&ctx);
5275 compose_ipfix_action(&ctx, ODPP_NONE);
5277 size_t sample_actions_len = ctx.odp_actions->size;
5279 if (tnl_process_ecn(flow)
5280 && (!in_port || may_receive(in_port, &ctx))) {
5281 const struct ofpact *ofpacts;
5285 ofpacts = xin->ofpacts;
5286 ofpacts_len = xin->ofpacts_len;
5287 } else if (ctx.rule) {
5288 const struct rule_actions *actions
5289 = rule_dpif_get_actions(ctx.rule);
5290 ofpacts = actions->ofpacts;
5291 ofpacts_len = actions->ofpacts_len;
5292 ctx.rule_cookie = rule_dpif_get_flow_cookie(ctx.rule);
5297 mirror_ingress_packet(&ctx);
5298 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
5303 /* We've let OFPP_NORMAL and the learning action look at the
5304 * packet, so drop it now if forwarding is disabled. */
5305 if (in_port && (!xport_stp_forward_state(in_port) ||
5306 !xport_rstp_forward_state(in_port))) {
5307 /* Drop all actions added by do_xlate_actions() above. */
5308 ctx.odp_actions->size = sample_actions_len;
5310 /* Undo changes that may have been done for recirculation. */
5311 if (exit_recirculates(&ctx)) {
5312 ctx.action_set.size = ctx.recirc_action_offset;
5313 ctx.recirc_action_offset = -1;
5314 ctx.last_unroll_offset = -1;
5316 } else if (ctx.action_set.size) {
5317 /* Translate action set only if not dropping the packet and
5318 * not recirculating. */
5319 if (!exit_recirculates(&ctx)) {
5320 xlate_action_set(&ctx);
5323 /* Check if need to recirculate. */
5324 if (exit_recirculates(&ctx)) {
5325 compose_recirculate_action(&ctx);
5329 /* Output only fully processed packets. */
5330 if (!exit_recirculates(&ctx)
5331 && xbridge->has_in_band
5332 && in_band_must_output_to_local_port(flow)
5333 && !actions_output_to_local_port(&ctx)) {
5334 compose_output_action(&ctx, OFPP_LOCAL, NULL);
5337 if (user_cookie_offset) {
5338 fix_sflow_action(&ctx, user_cookie_offset);
5342 if (nl_attr_oversized(ctx.odp_actions->size)) {
5343 /* These datapath actions are too big for a Netlink attribute, so we
5344 * can't hand them to the kernel directly. dpif_execute() can execute
5345 * them one by one with help, so just mark the result as SLOW_ACTION to
5346 * prevent the flow from being installed. */
5347 COVERAGE_INC(xlate_actions_oversize);
5348 ctx.xout->slow |= SLOW_ACTION;
5349 } else if (too_many_output_actions(ctx.odp_actions)) {
5350 COVERAGE_INC(xlate_actions_too_many_output);
5351 ctx.xout->slow |= SLOW_ACTION;
5354 /* Do netflow only for packets really received by the bridge and not sent
5355 * to the controller. We consider packets sent to the controller to be
5356 * part of the control plane rather than the data plane. */
5357 if (!xin->recirc && xbridge->netflow && !(xout->slow & SLOW_CONTROLLER)) {
5358 if (ctx.xin->resubmit_stats) {
5359 netflow_flow_update(xbridge->netflow, flow,
5360 ctx.nf_output_iface,
5361 ctx.xin->resubmit_stats);
5363 if (ctx.xin->xcache) {
5364 struct xc_entry *entry;
5366 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
5367 entry->u.nf.netflow = netflow_ref(xbridge->netflow);
5368 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
5369 entry->u.nf.iface = ctx.nf_output_iface;
5374 xlate_wc_finish(&ctx);
5378 ofpbuf_uninit(&ctx.stack);
5379 ofpbuf_uninit(&ctx.action_set);
5380 ofpbuf_uninit(&scratch_actions);
5382 /* Make sure we return a "drop flow" in case of an error. */
5385 if (xin->odp_actions) {
5386 ofpbuf_clear(xin->odp_actions);
5392 /* Sends 'packet' out 'ofport'.
5393 * May modify 'packet'.
5394 * Returns 0 if successful, otherwise a positive errno value. */
5396 xlate_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
5398 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5399 struct xport *xport;
5400 struct ofpact_output output;
5403 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
5404 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
5405 flow_extract(packet, &flow);
5406 flow.in_port.ofp_port = OFPP_NONE;
5408 xport = xport_lookup(xcfg, ofport);
5412 output.port = xport->ofp_port;
5415 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
5416 &output.ofpact, sizeof output,
5420 struct xlate_cache *
5421 xlate_cache_new(void)
5423 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
5425 ofpbuf_init(&xcache->entries, 512);
5429 static struct xc_entry *
5430 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
5432 struct xc_entry *entry;
5434 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
5441 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
5443 if (entry->u.dev.tx) {
5444 netdev_vport_inc_tx(entry->u.dev.tx, stats);
5446 if (entry->u.dev.rx) {
5447 netdev_vport_inc_rx(entry->u.dev.rx, stats);
5449 if (entry->u.dev.bfd) {
5450 bfd_account_rx(entry->u.dev.bfd, stats);
5455 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
5457 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5458 struct xbridge *xbridge;
5459 struct xbundle *xbundle;
5460 struct flow_wildcards wc;
5462 xbridge = xbridge_lookup(xcfg, ofproto);
5467 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
5473 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
5476 /* Push stats and perform side effects of flow translation. */
5478 xlate_push_stats(struct xlate_cache *xcache,
5479 const struct dpif_flow_stats *stats)
5481 struct xc_entry *entry;
5482 struct ofpbuf entries = xcache->entries;
5483 struct eth_addr dmac;
5485 if (!stats->n_packets) {
5489 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5490 switch (entry->type) {
5492 rule_dpif_credit_stats(entry->u.rule, stats);
5495 bond_account(entry->u.bond.bond, entry->u.bond.flow,
5496 entry->u.bond.vid, stats->n_bytes);
5499 xlate_cache_netdev(entry, stats);
5502 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
5503 entry->u.nf.iface, stats);
5506 mirror_update_stats(entry->u.mirror.mbridge,
5507 entry->u.mirror.mirrors,
5508 stats->n_packets, stats->n_bytes);
5511 ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
5514 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
5515 entry->u.normal.vlan);
5517 case XC_FIN_TIMEOUT:
5518 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
5519 entry->u.fin.idle, entry->u.fin.hard);
5522 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
5526 /* Lookup neighbor to avoid timeout. */
5527 tnl_neigh_lookup(entry->u.tnl_neigh_cache.br_name,
5528 &entry->u.tnl_neigh_cache.d_ipv6, &dmac);
5537 xlate_dev_unref(struct xc_entry *entry)
5539 if (entry->u.dev.tx) {
5540 netdev_close(entry->u.dev.tx);
5542 if (entry->u.dev.rx) {
5543 netdev_close(entry->u.dev.rx);
5545 if (entry->u.dev.bfd) {
5546 bfd_unref(entry->u.dev.bfd);
5551 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
5553 netflow_flow_clear(netflow, flow);
5554 netflow_unref(netflow);
5559 xlate_cache_clear(struct xlate_cache *xcache)
5561 struct xc_entry *entry;
5562 struct ofpbuf entries;
5568 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5569 switch (entry->type) {
5571 rule_dpif_unref(entry->u.rule);
5574 free(entry->u.bond.flow);
5575 bond_unref(entry->u.bond.bond);
5578 xlate_dev_unref(entry);
5581 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
5584 mbridge_unref(entry->u.mirror.mbridge);
5587 free(entry->u.learn.fm);
5588 ofpbuf_delete(entry->u.learn.ofpacts);
5591 free(entry->u.normal.flow);
5593 case XC_FIN_TIMEOUT:
5594 /* 'u.fin.rule' is always already held as a XC_RULE, which
5595 * has already released it's reference above. */
5598 group_dpif_unref(entry->u.group.group);
5607 ofpbuf_clear(&xcache->entries);
5611 xlate_cache_delete(struct xlate_cache *xcache)
5613 xlate_cache_clear(xcache);
5614 ofpbuf_uninit(&xcache->entries);