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-arp-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"
57 #include "ovs-router.h"
58 #include "tnl-ports.h"
60 #include "openvswitch/vlog.h"
62 COVERAGE_DEFINE(xlate_actions);
63 COVERAGE_DEFINE(xlate_actions_oversize);
64 COVERAGE_DEFINE(xlate_actions_too_many_output);
66 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
68 /* Maximum depth of flow table recursion (due to resubmit actions) in a
69 * flow translation. */
70 #define MAX_RESUBMIT_RECURSION 64
71 #define MAX_INTERNAL_RESUBMITS 1 /* Max resbmits allowed using rules in
74 /* Maximum number of resubmit actions in a flow translation, whether they are
75 * recursive or not. */
76 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
79 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
80 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
82 struct ovs_list xbundles; /* Owned xbundles. */
83 struct hmap xports; /* Indexed by ofp_port. */
85 char *name; /* Name used in log messages. */
86 struct dpif *dpif; /* Datapath interface. */
87 struct mac_learning *ml; /* Mac learning handle. */
88 struct mcast_snooping *ms; /* Multicast Snooping handle. */
89 struct mbridge *mbridge; /* Mirroring. */
90 struct dpif_sflow *sflow; /* SFlow handle, or null. */
91 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
92 struct netflow *netflow; /* Netflow handle, or null. */
93 struct stp *stp; /* STP or null if disabled. */
94 struct rstp *rstp; /* RSTP or null if disabled. */
96 bool has_in_band; /* Bridge has in band control? */
97 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
99 /* Datapath feature support. */
100 struct dpif_backer_support support;
104 struct hmap_node hmap_node; /* In global 'xbundles' map. */
105 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
107 struct ovs_list list_node; /* In parent 'xbridges' list. */
108 struct xbridge *xbridge; /* Parent xbridge. */
110 struct ovs_list xports; /* Contains "struct xport"s. */
112 char *name; /* Name used in log messages. */
113 struct bond *bond; /* Nonnull iff more than one port. */
114 struct lacp *lacp; /* LACP handle or null. */
116 enum port_vlan_mode vlan_mode; /* VLAN mode. */
117 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
118 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
119 * NULL if all VLANs are trunked. */
120 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
121 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
125 struct hmap_node hmap_node; /* Node in global 'xports' map. */
126 struct ofport_dpif *ofport; /* Key in global 'xports map. */
128 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
129 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
131 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
133 struct ovs_list bundle_node; /* In parent xbundle (if it exists). */
134 struct xbundle *xbundle; /* Parent xbundle or null. */
136 struct netdev *netdev; /* 'ofport''s netdev. */
138 struct xbridge *xbridge; /* Parent bridge. */
139 struct xport *peer; /* Patch port peer or null. */
141 enum ofputil_port_config config; /* OpenFlow port configuration. */
142 enum ofputil_port_state state; /* OpenFlow port state. */
143 int stp_port_no; /* STP port number or -1 if not in use. */
144 struct rstp_port *rstp_port; /* RSTP port or null. */
146 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
148 bool may_enable; /* May be enabled in bonds. */
149 bool is_tunnel; /* Is a tunnel port. */
151 struct cfm *cfm; /* CFM handle or null. */
152 struct bfd *bfd; /* BFD handle or null. */
153 struct lldp *lldp; /* LLDP handle or null. */
157 struct xlate_in *xin;
158 struct xlate_out *xout;
160 const struct xbridge *xbridge;
162 /* Flow tables version at the beginning of the translation. */
163 cls_version_t tables_version;
165 /* Flow at the last commit. */
166 struct flow base_flow;
168 /* Tunnel IP destination address as received. This is stored separately
169 * as the base_flow.tunnel is cleared on init to reflect the datapath
170 * behavior. Used to make sure not to send tunneled output to ourselves,
171 * which might lead to an infinite loop. This could happen easily
172 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
173 * actually set the tun_dst field. */
174 ovs_be32 orig_tunnel_ip_dst;
176 /* Stack for the push and pop actions. Each stack element is of type
177 * "union mf_subvalue". */
180 /* The rule that we are currently translating, or NULL. */
181 struct rule_dpif *rule;
183 /* Flow translation populates this with wildcards relevant in translation.
184 * When 'xin->wc' is nonnull, this is the same pointer. When 'xin->wc' is
185 * null, this is a pointer to uninitialized scratch memory. This allows
186 * code to blindly write to 'ctx->wc' without worrying about whether the
187 * caller really wants wildcards. */
188 struct flow_wildcards *wc;
190 /* Output buffer for datapath actions. When 'xin->odp_actions' is nonnull,
191 * this is the same pointer. When 'xin->odp_actions' is null, this points
192 * to a scratch ofpbuf. This allows code to add actions to
193 * 'ctx->odp_actions' without worrying about whether the caller really
195 struct ofpbuf *odp_actions;
197 /* Resubmit statistics, via xlate_table_action(). */
198 int recurse; /* Current resubmit nesting depth. */
199 int resubmits; /* Total number of resubmits. */
200 bool in_group; /* Currently translating ofgroup, if true. */
201 bool in_action_set; /* Currently translating action_set, if true. */
203 uint8_t table_id; /* OpenFlow table ID where flow was found. */
204 ovs_be64 rule_cookie; /* Cookie of the rule being translated. */
205 uint32_t orig_skb_priority; /* Priority when packet arrived. */
206 uint32_t sflow_n_outputs; /* Number of output ports. */
207 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
208 ofp_port_t nf_output_iface; /* Output interface index for NetFlow. */
209 bool exit; /* No further actions should be processed. */
210 mirror_mask_t mirrors; /* Bitmap of associated mirrors. */
212 /* These are used for non-bond recirculation. The recirculation IDs are
213 * stored in xout and must be associated with a datapath flow (ukey),
214 * otherwise they will be freed when the xout is uninitialized.
217 * Steps in Recirculation Translation
218 * ==================================
220 * At some point during translation, the code recognizes the need for
221 * recirculation. For example, recirculation is necessary when, after
222 * popping the last MPLS label, an action or a match tries to examine or
223 * modify a field that has been newly revealed following the MPLS label.
225 * The simplest part of the work to be done is to commit existing changes to
226 * the packet, which produces datapath actions corresponding to the changes,
227 * and after this, add an OVS_ACTION_ATTR_RECIRC datapath action.
229 * The main problem here is preserving state. When the datapath executes
230 * OVS_ACTION_ATTR_RECIRC, it will upcall to userspace to get a translation
231 * for the post-recirculation actions. At this point userspace has to
232 * resume the translation where it left off, which means that it has to
233 * execute the following:
235 * - The action that prompted recirculation, and any actions following
236 * it within the same flow.
238 * - If the action that prompted recirculation was invoked within a
239 * NXAST_RESUBMIT, then any actions following the resubmit. These
240 * "resubmit"s can be nested, so this has to go all the way up the
243 * - The OpenFlow 1.1+ action set.
245 * State that actions and flow table lookups can depend on, such as the
246 * following, must also be preserved:
248 * - Metadata fields (input port, registers, OF1.1+ metadata, ...).
250 * - Action set, stack
252 * - The table ID and cookie of the flow being translated at each level
253 * of the control stack (since OFPAT_CONTROLLER actions send these to
256 * Translation allows for the control of this state preservation via these
257 * members. When a need for recirculation is identified, the translation
260 * 1. Sets 'recirc_action_offset' to the current size of 'action_set'. The
261 * action set is part of what needs to be preserved, so this allows the
262 * action set and the additional state to share the 'action_set' buffer.
263 * Later steps can tell that setup for recirculation is in progress from
264 * the nonnegative value of 'recirc_action_offset'.
266 * 2. Sets 'exit' to true to tell later steps that we're exiting from the
267 * translation process.
269 * 3. Adds an OFPACT_UNROLL_XLATE action to 'action_set'. This action
270 * holds the current table ID and cookie so that they can be restored
271 * during a post-recirculation upcall translation.
273 * 4. Adds the action that prompted recirculation and any actions following
274 * it within the same flow to 'action_set', so that they can be executed
275 * during a post-recirculation upcall translation.
279 * 6. The action that prompted recirculation might be nested in a stack of
280 * nested "resubmit"s that have actions remaining. Each of these notices
281 * that we're exiting (from 'exit') and that recirculation setup is in
282 * progress (from 'recirc_action_offset') and responds by adding more
283 * OFPACT_UNROLL_XLATE actions to 'action_set', as necessary, and any
284 * actions that were yet unprocessed.
286 * The caller stores all the state produced by this process associated with
287 * the recirculation ID. For post-recirculation upcall translation, the
288 * caller passes it back in for the new translation to execute. The
289 * process yielded a set of ofpacts that can be translated directly, so it
290 * is not much of a special case at that point.
292 int recirc_action_offset; /* Offset in 'action_set' to actions to be
293 * executed after recirculation, or -1. */
294 int last_unroll_offset; /* Offset in 'action_set' to the latest unroll
297 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
298 * This is a trigger for recirculation in cases where translating an action
299 * or looking up a flow requires access to the fields of the packet after
300 * the MPLS label stack that was originally present. */
303 /* OpenFlow 1.1+ action set.
305 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
306 * When translation is otherwise complete, ofpacts_execute_action_set()
307 * converts it to a set of "struct ofpact"s that can be translated into
308 * datapath actions. */
309 bool action_set_has_group; /* Action set contains OFPACT_GROUP? */
310 struct ofpbuf action_set; /* Action set. */
313 static void xlate_action_set(struct xlate_ctx *ctx);
316 ctx_trigger_recirculation(struct xlate_ctx *ctx)
319 ctx->recirc_action_offset = ctx->action_set.size;
323 ctx_first_recirculation_action(const struct xlate_ctx *ctx)
325 return ctx->recirc_action_offset == ctx->action_set.size;
329 exit_recirculates(const struct xlate_ctx *ctx)
331 /* When recirculating the 'recirc_action_offset' has a non-negative value.
333 return ctx->recirc_action_offset >= 0;
336 static void compose_recirculate_action(struct xlate_ctx *ctx);
338 /* A controller may use OFPP_NONE as the ingress port to indicate that
339 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
340 * when an input bundle is needed for validation (e.g., mirroring or
341 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
342 * any 'port' structs, so care must be taken when dealing with it. */
343 static struct xbundle ofpp_none_bundle = {
345 .vlan_mode = PORT_VLAN_TRUNK
348 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
349 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
350 * traffic egressing the 'ofport' with that priority should be marked with. */
351 struct skb_priority_to_dscp {
352 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
353 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
355 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
371 /* xlate_cache entries hold enough information to perform the side effects of
372 * xlate_actions() for a rule, without needing to perform rule translation
373 * from scratch. The primary usage of these is to submit statistics to objects
374 * that a flow relates to, although they may be used for other effects as well
375 * (for instance, refreshing hard timeouts for learned flows). */
379 struct rule_dpif *rule;
386 struct netflow *netflow;
391 struct mbridge *mbridge;
392 mirror_mask_t mirrors;
400 struct ofproto_dpif *ofproto;
401 struct ofputil_flow_mod *fm;
402 struct ofpbuf *ofpacts;
405 struct ofproto_dpif *ofproto;
410 struct rule_dpif *rule;
415 struct group_dpif *group;
416 struct ofputil_bucket *bucket;
419 char br_name[IFNAMSIZ];
425 #define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
426 entries = xcache->entries; \
427 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
429 entry = ofpbuf_try_pull(&entries, sizeof *entry))
432 struct ofpbuf entries;
435 /* Xlate config contains hash maps of all bridges, bundles and ports.
436 * Xcfgp contains the pointer to the current xlate configuration.
437 * When the main thread needs to change the configuration, it copies xcfgp to
438 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
439 * does not block handler and revalidator threads. */
441 struct hmap xbridges;
442 struct hmap xbundles;
445 static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
446 static struct xlate_cfg *new_xcfg = NULL;
448 static bool may_receive(const struct xport *, struct xlate_ctx *);
449 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
451 static void xlate_normal(struct xlate_ctx *);
452 static inline void xlate_report(struct xlate_ctx *, const char *, ...)
453 OVS_PRINTF_FORMAT(2, 3);
454 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
455 uint8_t table_id, bool may_packet_in,
456 bool honor_table_miss);
457 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
458 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
459 static void output_normal(struct xlate_ctx *, const struct xbundle *,
462 /* Optional bond recirculation parameter to compose_output_action(). */
463 struct xlate_bond_recirc {
464 uint32_t recirc_id; /* !0 Use recirculation instead of output. */
465 uint8_t hash_alg; /* !0 Compute hash for recirc before. */
466 uint32_t hash_basis; /* Compute hash for recirc before. */
469 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port,
470 const struct xlate_bond_recirc *xr);
472 static struct xbridge *xbridge_lookup(struct xlate_cfg *,
473 const struct ofproto_dpif *);
474 static struct xbundle *xbundle_lookup(struct xlate_cfg *,
475 const struct ofbundle *);
476 static struct xport *xport_lookup(struct xlate_cfg *,
477 const struct ofport_dpif *);
478 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
479 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
480 uint32_t skb_priority);
481 static void clear_skb_priorities(struct xport *);
482 static size_t count_skb_priorities(const struct xport *);
483 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
486 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
488 static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
489 static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
490 static void xlate_xport_init(struct xlate_cfg *, struct xport *);
491 static void xlate_xbridge_set(struct xbridge *, struct dpif *,
492 const struct mac_learning *, struct stp *,
493 struct rstp *, const struct mcast_snooping *,
494 const struct mbridge *,
495 const struct dpif_sflow *,
496 const struct dpif_ipfix *,
497 const struct netflow *,
498 bool forward_bpdu, bool has_in_band,
499 const struct dpif_backer_support *);
500 static void xlate_xbundle_set(struct xbundle *xbundle,
501 enum port_vlan_mode vlan_mode, int vlan,
502 unsigned long *trunks, bool use_priority_tags,
503 const struct bond *bond, const struct lacp *lacp,
505 static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
506 const struct netdev *netdev, const struct cfm *cfm,
507 const struct bfd *bfd, const struct lldp *lldp,
508 int stp_port_no, const struct rstp_port *rstp_port,
509 enum ofputil_port_config config,
510 enum ofputil_port_state state, bool is_tunnel,
512 static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
513 static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
514 static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
515 static void xlate_xbridge_copy(struct xbridge *);
516 static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
517 static void xlate_xport_copy(struct xbridge *, struct xbundle *,
519 static void xlate_xcfg_free(struct xlate_cfg *);
522 xlate_report(struct xlate_ctx *ctx, const char *format, ...)
524 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
527 va_start(args, format);
528 ctx->xin->report_hook(ctx->xin, ctx->recurse, format, args);
534 xlate_report_actions(struct xlate_ctx *ctx, const char *title,
535 const struct ofpact *ofpacts, size_t ofpacts_len)
537 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
538 struct ds s = DS_EMPTY_INITIALIZER;
539 ofpacts_format(ofpacts, ofpacts_len, &s);
540 xlate_report(ctx, "%s: %s", title, ds_cstr(&s));
546 xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
548 list_init(&xbridge->xbundles);
549 hmap_init(&xbridge->xports);
550 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
551 hash_pointer(xbridge->ofproto, 0));
555 xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
557 list_init(&xbundle->xports);
558 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
559 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
560 hash_pointer(xbundle->ofbundle, 0));
564 xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
566 hmap_init(&xport->skb_priorities);
567 hmap_insert(&xcfg->xports, &xport->hmap_node,
568 hash_pointer(xport->ofport, 0));
569 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
570 hash_ofp_port(xport->ofp_port));
574 xlate_xbridge_set(struct xbridge *xbridge,
576 const struct mac_learning *ml, struct stp *stp,
577 struct rstp *rstp, const struct mcast_snooping *ms,
578 const struct mbridge *mbridge,
579 const struct dpif_sflow *sflow,
580 const struct dpif_ipfix *ipfix,
581 const struct netflow *netflow,
582 bool forward_bpdu, bool has_in_band,
583 const struct dpif_backer_support *support)
585 if (xbridge->ml != ml) {
586 mac_learning_unref(xbridge->ml);
587 xbridge->ml = mac_learning_ref(ml);
590 if (xbridge->ms != ms) {
591 mcast_snooping_unref(xbridge->ms);
592 xbridge->ms = mcast_snooping_ref(ms);
595 if (xbridge->mbridge != mbridge) {
596 mbridge_unref(xbridge->mbridge);
597 xbridge->mbridge = mbridge_ref(mbridge);
600 if (xbridge->sflow != sflow) {
601 dpif_sflow_unref(xbridge->sflow);
602 xbridge->sflow = dpif_sflow_ref(sflow);
605 if (xbridge->ipfix != ipfix) {
606 dpif_ipfix_unref(xbridge->ipfix);
607 xbridge->ipfix = dpif_ipfix_ref(ipfix);
610 if (xbridge->stp != stp) {
611 stp_unref(xbridge->stp);
612 xbridge->stp = stp_ref(stp);
615 if (xbridge->rstp != rstp) {
616 rstp_unref(xbridge->rstp);
617 xbridge->rstp = rstp_ref(rstp);
620 if (xbridge->netflow != netflow) {
621 netflow_unref(xbridge->netflow);
622 xbridge->netflow = netflow_ref(netflow);
625 xbridge->dpif = dpif;
626 xbridge->forward_bpdu = forward_bpdu;
627 xbridge->has_in_band = has_in_band;
628 xbridge->support = *support;
632 xlate_xbundle_set(struct xbundle *xbundle,
633 enum port_vlan_mode vlan_mode, int vlan,
634 unsigned long *trunks, bool use_priority_tags,
635 const struct bond *bond, const struct lacp *lacp,
638 ovs_assert(xbundle->xbridge);
640 xbundle->vlan_mode = vlan_mode;
641 xbundle->vlan = vlan;
642 xbundle->trunks = trunks;
643 xbundle->use_priority_tags = use_priority_tags;
644 xbundle->floodable = floodable;
646 if (xbundle->bond != bond) {
647 bond_unref(xbundle->bond);
648 xbundle->bond = bond_ref(bond);
651 if (xbundle->lacp != lacp) {
652 lacp_unref(xbundle->lacp);
653 xbundle->lacp = lacp_ref(lacp);
658 xlate_xport_set(struct xport *xport, odp_port_t odp_port,
659 const struct netdev *netdev, const struct cfm *cfm,
660 const struct bfd *bfd, const struct lldp *lldp, int stp_port_no,
661 const struct rstp_port* rstp_port,
662 enum ofputil_port_config config, enum ofputil_port_state state,
663 bool is_tunnel, bool may_enable)
665 xport->config = config;
666 xport->state = state;
667 xport->stp_port_no = stp_port_no;
668 xport->is_tunnel = is_tunnel;
669 xport->may_enable = may_enable;
670 xport->odp_port = odp_port;
672 if (xport->rstp_port != rstp_port) {
673 rstp_port_unref(xport->rstp_port);
674 xport->rstp_port = rstp_port_ref(rstp_port);
677 if (xport->cfm != cfm) {
678 cfm_unref(xport->cfm);
679 xport->cfm = cfm_ref(cfm);
682 if (xport->bfd != bfd) {
683 bfd_unref(xport->bfd);
684 xport->bfd = bfd_ref(bfd);
687 if (xport->lldp != lldp) {
688 lldp_unref(xport->lldp);
689 xport->lldp = lldp_ref(lldp);
692 if (xport->netdev != netdev) {
693 netdev_close(xport->netdev);
694 xport->netdev = netdev_ref(netdev);
699 xlate_xbridge_copy(struct xbridge *xbridge)
701 struct xbundle *xbundle;
703 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
704 new_xbridge->ofproto = xbridge->ofproto;
705 new_xbridge->name = xstrdup(xbridge->name);
706 xlate_xbridge_init(new_xcfg, new_xbridge);
708 xlate_xbridge_set(new_xbridge,
709 xbridge->dpif, xbridge->ml, xbridge->stp,
710 xbridge->rstp, xbridge->ms, xbridge->mbridge,
711 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
712 xbridge->forward_bpdu, xbridge->has_in_band,
714 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
715 xlate_xbundle_copy(new_xbridge, xbundle);
718 /* Copy xports which are not part of a xbundle */
719 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
720 if (!xport->xbundle) {
721 xlate_xport_copy(new_xbridge, NULL, xport);
727 xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
730 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
731 new_xbundle->ofbundle = xbundle->ofbundle;
732 new_xbundle->xbridge = xbridge;
733 new_xbundle->name = xstrdup(xbundle->name);
734 xlate_xbundle_init(new_xcfg, new_xbundle);
736 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
737 xbundle->vlan, xbundle->trunks,
738 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
740 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
741 xlate_xport_copy(xbridge, new_xbundle, xport);
746 xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
749 struct skb_priority_to_dscp *pdscp, *new_pdscp;
750 struct xport *new_xport = xzalloc(sizeof *xport);
751 new_xport->ofport = xport->ofport;
752 new_xport->ofp_port = xport->ofp_port;
753 new_xport->xbridge = xbridge;
754 xlate_xport_init(new_xcfg, new_xport);
756 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
757 xport->bfd, xport->lldp, xport->stp_port_no,
758 xport->rstp_port, xport->config, xport->state,
759 xport->is_tunnel, xport->may_enable);
762 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
764 new_xport->peer = peer;
765 new_xport->peer->peer = new_xport;
770 new_xport->xbundle = xbundle;
771 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
774 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
775 new_pdscp = xmalloc(sizeof *pdscp);
776 new_pdscp->skb_priority = pdscp->skb_priority;
777 new_pdscp->dscp = pdscp->dscp;
778 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
779 hash_int(new_pdscp->skb_priority, 0));
783 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
784 * configuration in xcfgp.
786 * This needs to be called after editing the xlate configuration.
788 * Functions that edit the new xlate configuration are
789 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
795 * edit_xlate_configuration();
797 * xlate_txn_commit(); */
799 xlate_txn_commit(void)
801 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
803 ovsrcu_set(&xcfgp, new_xcfg);
804 ovsrcu_synchronize();
805 xlate_xcfg_free(xcfg);
809 /* Copies the current xlate configuration in xcfgp to new_xcfg.
811 * This needs to be called prior to editing the xlate configuration. */
813 xlate_txn_start(void)
815 struct xbridge *xbridge;
816 struct xlate_cfg *xcfg;
818 ovs_assert(!new_xcfg);
820 new_xcfg = xmalloc(sizeof *new_xcfg);
821 hmap_init(&new_xcfg->xbridges);
822 hmap_init(&new_xcfg->xbundles);
823 hmap_init(&new_xcfg->xports);
825 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
830 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
831 xlate_xbridge_copy(xbridge);
837 xlate_xcfg_free(struct xlate_cfg *xcfg)
839 struct xbridge *xbridge, *next_xbridge;
845 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
846 xlate_xbridge_remove(xcfg, xbridge);
849 hmap_destroy(&xcfg->xbridges);
850 hmap_destroy(&xcfg->xbundles);
851 hmap_destroy(&xcfg->xports);
856 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
858 const struct mac_learning *ml, struct stp *stp,
859 struct rstp *rstp, const struct mcast_snooping *ms,
860 const struct mbridge *mbridge,
861 const struct dpif_sflow *sflow,
862 const struct dpif_ipfix *ipfix,
863 const struct netflow *netflow,
864 bool forward_bpdu, bool has_in_band,
865 const struct dpif_backer_support *support)
867 struct xbridge *xbridge;
869 ovs_assert(new_xcfg);
871 xbridge = xbridge_lookup(new_xcfg, ofproto);
873 xbridge = xzalloc(sizeof *xbridge);
874 xbridge->ofproto = ofproto;
876 xlate_xbridge_init(new_xcfg, xbridge);
880 xbridge->name = xstrdup(name);
882 xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
883 netflow, forward_bpdu, has_in_band, support);
887 xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
889 struct xbundle *xbundle, *next_xbundle;
890 struct xport *xport, *next_xport;
896 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
897 xlate_xport_remove(xcfg, xport);
900 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
901 xlate_xbundle_remove(xcfg, xbundle);
904 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
905 mac_learning_unref(xbridge->ml);
906 mcast_snooping_unref(xbridge->ms);
907 mbridge_unref(xbridge->mbridge);
908 dpif_sflow_unref(xbridge->sflow);
909 dpif_ipfix_unref(xbridge->ipfix);
910 stp_unref(xbridge->stp);
911 rstp_unref(xbridge->rstp);
912 hmap_destroy(&xbridge->xports);
918 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
920 struct xbridge *xbridge;
922 ovs_assert(new_xcfg);
924 xbridge = xbridge_lookup(new_xcfg, ofproto);
925 xlate_xbridge_remove(new_xcfg, xbridge);
929 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
930 const char *name, enum port_vlan_mode vlan_mode, int vlan,
931 unsigned long *trunks, bool use_priority_tags,
932 const struct bond *bond, const struct lacp *lacp,
935 struct xbundle *xbundle;
937 ovs_assert(new_xcfg);
939 xbundle = xbundle_lookup(new_xcfg, ofbundle);
941 xbundle = xzalloc(sizeof *xbundle);
942 xbundle->ofbundle = ofbundle;
943 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
945 xlate_xbundle_init(new_xcfg, xbundle);
949 xbundle->name = xstrdup(name);
951 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
952 use_priority_tags, bond, lacp, floodable);
956 xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
964 LIST_FOR_EACH_POP (xport, bundle_node, &xbundle->xports) {
965 xport->xbundle = NULL;
968 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
969 list_remove(&xbundle->list_node);
970 bond_unref(xbundle->bond);
971 lacp_unref(xbundle->lacp);
977 xlate_bundle_remove(struct ofbundle *ofbundle)
979 struct xbundle *xbundle;
981 ovs_assert(new_xcfg);
983 xbundle = xbundle_lookup(new_xcfg, ofbundle);
984 xlate_xbundle_remove(new_xcfg, xbundle);
988 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
989 struct ofport_dpif *ofport, ofp_port_t ofp_port,
990 odp_port_t odp_port, const struct netdev *netdev,
991 const struct cfm *cfm, const struct bfd *bfd,
992 const struct lldp *lldp, struct ofport_dpif *peer,
993 int stp_port_no, const struct rstp_port *rstp_port,
994 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
995 enum ofputil_port_config config,
996 enum ofputil_port_state state, bool is_tunnel,
1000 struct xport *xport;
1002 ovs_assert(new_xcfg);
1004 xport = xport_lookup(new_xcfg, ofport);
1006 xport = xzalloc(sizeof *xport);
1007 xport->ofport = ofport;
1008 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
1009 xport->ofp_port = ofp_port;
1011 xlate_xport_init(new_xcfg, xport);
1014 ovs_assert(xport->ofp_port == ofp_port);
1016 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, lldp,
1017 stp_port_no, rstp_port, config, state, is_tunnel,
1021 xport->peer->peer = NULL;
1023 xport->peer = xport_lookup(new_xcfg, peer);
1025 xport->peer->peer = xport;
1028 if (xport->xbundle) {
1029 list_remove(&xport->bundle_node);
1031 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
1032 if (xport->xbundle) {
1033 list_insert(&xport->xbundle->xports, &xport->bundle_node);
1036 clear_skb_priorities(xport);
1037 for (i = 0; i < n_qdscp; i++) {
1038 struct skb_priority_to_dscp *pdscp;
1039 uint32_t skb_priority;
1041 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
1046 pdscp = xmalloc(sizeof *pdscp);
1047 pdscp->skb_priority = skb_priority;
1048 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1049 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
1050 hash_int(pdscp->skb_priority, 0));
1055 xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
1062 xport->peer->peer = NULL;
1066 if (xport->xbundle) {
1067 list_remove(&xport->bundle_node);
1070 clear_skb_priorities(xport);
1071 hmap_destroy(&xport->skb_priorities);
1073 hmap_remove(&xcfg->xports, &xport->hmap_node);
1074 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
1076 netdev_close(xport->netdev);
1077 rstp_port_unref(xport->rstp_port);
1078 cfm_unref(xport->cfm);
1079 bfd_unref(xport->bfd);
1080 lldp_unref(xport->lldp);
1085 xlate_ofport_remove(struct ofport_dpif *ofport)
1087 struct xport *xport;
1089 ovs_assert(new_xcfg);
1091 xport = xport_lookup(new_xcfg, ofport);
1092 xlate_xport_remove(new_xcfg, xport);
1095 static struct ofproto_dpif *
1096 xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
1097 ofp_port_t *ofp_in_port, const struct xport **xportp)
1099 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1100 const struct xport *xport;
1102 xport = xport_lookup(xcfg, tnl_port_should_receive(flow)
1103 ? tnl_port_receive(flow)
1104 : odp_port_to_ofport(backer, flow->in_port.odp_port));
1105 if (OVS_UNLIKELY(!xport)) {
1110 *ofp_in_port = xport->ofp_port;
1112 return xport->xbridge->ofproto;
1115 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1116 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1117 struct ofproto_dpif *
1118 xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1119 ofp_port_t *ofp_in_port)
1121 const struct xport *xport;
1123 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1126 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1127 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
1128 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1129 * handles for those protocols if they're enabled. Caller may use the returned
1130 * pointers until quiescing, for longer term use additional references must
1133 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1136 xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1137 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1138 struct dpif_sflow **sflow, struct netflow **netflow,
1139 ofp_port_t *ofp_in_port)
1141 struct ofproto_dpif *ofproto;
1142 const struct xport *xport;
1144 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1151 *ofprotop = ofproto;
1155 *ipfix = xport ? xport->xbridge->ipfix : NULL;
1159 *sflow = xport ? xport->xbridge->sflow : NULL;
1163 *netflow = xport ? xport->xbridge->netflow : NULL;
1169 static struct xbridge *
1170 xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
1172 struct hmap *xbridges;
1173 struct xbridge *xbridge;
1175 if (!ofproto || !xcfg) {
1179 xbridges = &xcfg->xbridges;
1181 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
1183 if (xbridge->ofproto == ofproto) {
1190 static struct xbundle *
1191 xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
1193 struct hmap *xbundles;
1194 struct xbundle *xbundle;
1196 if (!ofbundle || !xcfg) {
1200 xbundles = &xcfg->xbundles;
1202 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
1204 if (xbundle->ofbundle == ofbundle) {
1211 static struct xport *
1212 xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
1214 struct hmap *xports;
1215 struct xport *xport;
1217 if (!ofport || !xcfg) {
1221 xports = &xcfg->xports;
1223 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
1225 if (xport->ofport == ofport) {
1232 static struct stp_port *
1233 xport_get_stp_port(const struct xport *xport)
1235 return xport->xbridge->stp && xport->stp_port_no != -1
1236 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1241 xport_stp_learn_state(const struct xport *xport)
1243 struct stp_port *sp = xport_get_stp_port(xport);
1245 ? stp_learn_in_state(stp_port_get_state(sp))
1250 xport_stp_forward_state(const struct xport *xport)
1252 struct stp_port *sp = xport_get_stp_port(xport);
1254 ? stp_forward_in_state(stp_port_get_state(sp))
1259 xport_stp_should_forward_bpdu(const struct xport *xport)
1261 struct stp_port *sp = xport_get_stp_port(xport);
1262 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
1265 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1266 * were used to make the determination.*/
1268 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1270 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1271 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1272 return is_stp(flow);
1276 stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1278 struct stp_port *sp = xport_get_stp_port(xport);
1279 struct dp_packet payload = *packet;
1280 struct eth_header *eth = dp_packet_data(&payload);
1282 /* Sink packets on ports that have STP disabled when the bridge has
1284 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1288 /* Trim off padding on payload. */
1289 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1290 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1293 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1294 stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
1298 static enum rstp_state
1299 xport_get_rstp_port_state(const struct xport *xport)
1301 return xport->rstp_port
1302 ? rstp_port_get_state(xport->rstp_port)
1307 xport_rstp_learn_state(const struct xport *xport)
1309 return xport->xbridge->rstp && xport->rstp_port
1310 ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1315 xport_rstp_forward_state(const struct xport *xport)
1317 return xport->xbridge->rstp && xport->rstp_port
1318 ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1323 xport_rstp_should_manage_bpdu(const struct xport *xport)
1325 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
1329 rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1331 struct dp_packet payload = *packet;
1332 struct eth_header *eth = dp_packet_data(&payload);
1334 /* Sink packets on ports that have no RSTP. */
1335 if (!xport->rstp_port) {
1339 /* Trim off padding on payload. */
1340 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1341 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1344 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1345 rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
1346 dp_packet_size(&payload));
1350 static struct xport *
1351 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1353 struct xport *xport;
1355 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1357 if (xport->ofp_port == ofp_port) {
1365 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1367 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1368 return xport ? xport->odp_port : ODPP_NONE;
1372 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1374 struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1375 return xport && xport->may_enable;
1378 static struct ofputil_bucket *
1379 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1383 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1385 struct group_dpif *group;
1387 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1388 struct ofputil_bucket *bucket;
1390 bucket = group_first_live_bucket(ctx, group, depth);
1391 group_dpif_unref(group);
1392 return bucket == NULL;
1398 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1401 bucket_is_alive(const struct xlate_ctx *ctx,
1402 struct ofputil_bucket *bucket, int depth)
1404 if (depth >= MAX_LIVENESS_RECURSION) {
1405 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1407 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1408 MAX_LIVENESS_RECURSION);
1412 return (!ofputil_bucket_has_liveness(bucket)
1413 || (bucket->watch_port != OFPP_ANY
1414 && odp_port_is_alive(ctx, bucket->watch_port))
1415 || (bucket->watch_group != OFPG_ANY
1416 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
1419 static struct ofputil_bucket *
1420 group_first_live_bucket(const struct xlate_ctx *ctx,
1421 const struct group_dpif *group, int depth)
1423 struct ofputil_bucket *bucket;
1424 const struct ovs_list *buckets;
1426 group_dpif_get_buckets(group, &buckets);
1427 LIST_FOR_EACH (bucket, list_node, buckets) {
1428 if (bucket_is_alive(ctx, bucket, depth)) {
1436 static struct ofputil_bucket *
1437 group_best_live_bucket(const struct xlate_ctx *ctx,
1438 const struct group_dpif *group,
1441 struct ofputil_bucket *best_bucket = NULL;
1442 uint32_t best_score = 0;
1445 struct ofputil_bucket *bucket;
1446 const struct ovs_list *buckets;
1448 group_dpif_get_buckets(group, &buckets);
1449 LIST_FOR_EACH (bucket, list_node, buckets) {
1450 if (bucket_is_alive(ctx, bucket, 0)) {
1451 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
1452 if (score >= best_score) {
1453 best_bucket = bucket;
1464 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
1466 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1467 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1471 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1473 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1476 static mirror_mask_t
1477 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1479 return xbundle != &ofpp_none_bundle
1480 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1484 static mirror_mask_t
1485 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
1487 return xbundle != &ofpp_none_bundle
1488 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1492 static mirror_mask_t
1493 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
1495 return xbundle != &ofpp_none_bundle
1496 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1500 static struct xbundle *
1501 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1502 bool warn, struct xport **in_xportp)
1504 struct xport *xport;
1506 /* Find the port and bundle for the received packet. */
1507 xport = get_ofp_port(xbridge, in_port);
1511 if (xport && xport->xbundle) {
1512 return xport->xbundle;
1515 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1516 * which a controller may use as the ingress port for traffic that
1517 * it is sourcing. */
1518 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
1519 return &ofpp_none_bundle;
1522 /* Odd. A few possible reasons here:
1524 * - We deleted a port but there are still a few packets queued up
1527 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1528 * we don't know about.
1530 * - The ofproto client didn't configure the port as part of a bundle.
1531 * This is particularly likely to happen if a packet was received on the
1532 * port after it was created, but before the client had a chance to
1533 * configure its bundle.
1536 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1538 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1539 "port %"PRIu16, xbridge->name, in_port);
1545 mirror_packet(struct xlate_ctx *ctx, struct xbundle *xbundle,
1546 mirror_mask_t mirrors)
1548 bool warn = ctx->xin->packet != NULL;
1549 uint16_t vid = vlan_tci_to_vid(ctx->xin->flow.vlan_tci);
1550 if (!input_vid_is_valid(vid, xbundle, warn)) {
1553 uint16_t vlan = input_vid_to_vlan(xbundle, vid);
1555 const struct xbridge *xbridge = ctx->xbridge;
1557 /* Don't mirror to destinations that we've already mirrored to. */
1558 mirrors &= ~ctx->mirrors;
1563 /* Record these mirrors so that we don't mirror to them again. */
1564 ctx->mirrors |= mirrors;
1566 if (ctx->xin->resubmit_stats) {
1567 mirror_update_stats(xbridge->mbridge, mirrors,
1568 ctx->xin->resubmit_stats->n_packets,
1569 ctx->xin->resubmit_stats->n_bytes);
1571 if (ctx->xin->xcache) {
1572 struct xc_entry *entry;
1574 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_MIRROR);
1575 entry->u.mirror.mbridge = mbridge_ref(xbridge->mbridge);
1576 entry->u.mirror.mirrors = mirrors;
1580 const unsigned long *vlans;
1581 mirror_mask_t dup_mirrors;
1582 struct ofbundle *out;
1585 bool has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1586 &vlans, &dup_mirrors, &out, &out_vlan);
1587 ovs_assert(has_mirror);
1590 ctx->wc->masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1593 if (vlans && !bitmap_is_set(vlans, vlan)) {
1594 mirrors = zero_rightmost_1bit(mirrors);
1598 mirrors &= ~dup_mirrors;
1599 ctx->mirrors |= dup_mirrors;
1601 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1602 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1604 output_normal(ctx, out_xbundle, vlan);
1606 } else if (vlan != out_vlan
1607 && !eth_addr_is_reserved(ctx->xin->flow.dl_dst)) {
1608 struct xbundle *xbundle;
1610 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1611 if (xbundle_includes_vlan(xbundle, out_vlan)
1612 && !xbundle_mirror_out(xbridge, xbundle)) {
1613 output_normal(ctx, xbundle, out_vlan);
1621 mirror_ingress_packet(struct xlate_ctx *ctx)
1623 if (mbridge_has_mirrors(ctx->xbridge->mbridge)) {
1624 bool warn = ctx->xin->packet != NULL;
1625 struct xbundle *xbundle = lookup_input_bundle(
1626 ctx->xbridge, ctx->xin->flow.in_port.ofp_port, warn, NULL);
1628 mirror_packet(ctx, xbundle,
1629 xbundle_mirror_src(ctx->xbridge, xbundle));
1634 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1635 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1636 * the bundle on which the packet was received, returns the VLAN to which the
1639 * Both 'vid' and the return value are in the range 0...4095. */
1641 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1643 switch (in_xbundle->vlan_mode) {
1644 case PORT_VLAN_ACCESS:
1645 return in_xbundle->vlan;
1648 case PORT_VLAN_TRUNK:
1651 case PORT_VLAN_NATIVE_UNTAGGED:
1652 case PORT_VLAN_NATIVE_TAGGED:
1653 return vid ? vid : in_xbundle->vlan;
1660 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1661 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1664 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1665 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1668 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1670 /* Allow any VID on the OFPP_NONE port. */
1671 if (in_xbundle == &ofpp_none_bundle) {
1675 switch (in_xbundle->vlan_mode) {
1676 case PORT_VLAN_ACCESS:
1679 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1680 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1681 "packet received on port %s configured as VLAN "
1682 "%"PRIu16" access port", vid, in_xbundle->name,
1689 case PORT_VLAN_NATIVE_UNTAGGED:
1690 case PORT_VLAN_NATIVE_TAGGED:
1692 /* Port must always carry its native VLAN. */
1696 case PORT_VLAN_TRUNK:
1697 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1699 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1700 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1701 "received on port %s not configured for trunking "
1702 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1714 /* Given 'vlan', the VLAN that a packet belongs to, and
1715 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1716 * that should be included in the 802.1Q header. (If the return value is 0,
1717 * then the 802.1Q header should only be included in the packet if there is a
1720 * Both 'vlan' and the return value are in the range 0...4095. */
1722 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1724 switch (out_xbundle->vlan_mode) {
1725 case PORT_VLAN_ACCESS:
1728 case PORT_VLAN_TRUNK:
1729 case PORT_VLAN_NATIVE_TAGGED:
1732 case PORT_VLAN_NATIVE_UNTAGGED:
1733 return vlan == out_xbundle->vlan ? 0 : vlan;
1741 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1744 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1746 ovs_be16 tci, old_tci;
1747 struct xport *xport;
1748 struct xlate_bond_recirc xr;
1749 bool use_recirc = false;
1751 vid = output_vlan_to_vid(out_xbundle, vlan);
1752 if (list_is_empty(&out_xbundle->xports)) {
1753 /* Partially configured bundle with no slaves. Drop the packet. */
1755 } else if (!out_xbundle->bond) {
1756 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1759 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1760 struct flow_wildcards *wc = ctx->wc;
1761 struct ofport_dpif *ofport;
1763 if (ctx->xbridge->support.odp.recirc) {
1764 use_recirc = bond_may_recirc(
1765 out_xbundle->bond, &xr.recirc_id, &xr.hash_basis);
1768 /* Only TCP mode uses recirculation. */
1769 xr.hash_alg = OVS_HASH_ALG_L4;
1770 bond_update_post_recirc_rules(out_xbundle->bond, false);
1772 /* Recirculation does not require unmasking hash fields. */
1777 ofport = bond_choose_output_slave(out_xbundle->bond,
1778 &ctx->xin->flow, wc, vid);
1779 xport = xport_lookup(xcfg, ofport);
1782 /* No slaves enabled, so drop packet. */
1786 /* If use_recirc is set, the main thread will handle stats
1787 * accounting for this bond. */
1789 if (ctx->xin->resubmit_stats) {
1790 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1791 ctx->xin->resubmit_stats->n_bytes);
1793 if (ctx->xin->xcache) {
1794 struct xc_entry *entry;
1797 flow = &ctx->xin->flow;
1798 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1799 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1800 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1801 entry->u.bond.vid = vid;
1806 old_tci = *flow_tci;
1808 if (tci || out_xbundle->use_priority_tags) {
1809 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1811 tci |= htons(VLAN_CFI);
1816 compose_output_action(ctx, xport->ofp_port, use_recirc ? &xr : NULL);
1817 *flow_tci = old_tci;
1820 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1821 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1822 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1824 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1826 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1830 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1831 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1835 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1836 if (flow->nw_proto == ARP_OP_REPLY) {
1838 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1839 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1840 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1842 return flow->nw_src == flow->nw_dst;
1848 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1849 * dropped. Returns true if they may be forwarded, false if they should be
1852 * 'in_port' must be the xport that corresponds to flow->in_port.
1853 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1855 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1856 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1857 * checked by input_vid_is_valid().
1859 * May also add tags to '*tags', although the current implementation only does
1860 * so in one special case.
1863 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1866 struct xbundle *in_xbundle = in_port->xbundle;
1867 const struct xbridge *xbridge = ctx->xbridge;
1868 struct flow *flow = &ctx->xin->flow;
1870 /* Drop frames for reserved multicast addresses
1871 * only if forward_bpdu option is absent. */
1872 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1873 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1877 if (in_xbundle->bond) {
1878 struct mac_entry *mac;
1880 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1886 xlate_report(ctx, "bonding refused admissibility, dropping");
1889 case BV_DROP_IF_MOVED:
1890 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1891 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1893 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
1894 && (!is_gratuitous_arp(flow, ctx->wc)
1895 || mac_entry_is_grat_arp_locked(mac))) {
1896 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1897 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1901 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1909 /* Checks whether a MAC learning update is necessary for MAC learning table
1910 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1913 * Most packets processed through the MAC learning table do not actually
1914 * change it in any way. This function requires only a read lock on the MAC
1915 * learning table, so it is much cheaper in this common case.
1917 * Keep the code here synchronized with that in update_learning_table__()
1920 is_mac_learning_update_needed(const struct mac_learning *ml,
1921 const struct flow *flow,
1922 struct flow_wildcards *wc,
1923 int vlan, struct xbundle *in_xbundle)
1924 OVS_REQ_RDLOCK(ml->rwlock)
1926 struct mac_entry *mac;
1928 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1932 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1933 if (!mac || mac_entry_age(ml, mac)) {
1937 if (is_gratuitous_arp(flow, wc)) {
1938 /* We don't want to learn from gratuitous ARP packets that are
1939 * reflected back over bond slaves so we lock the learning table. */
1940 if (!in_xbundle->bond) {
1942 } else if (mac_entry_is_grat_arp_locked(mac)) {
1947 return mac_entry_get_port(ml, mac) != in_xbundle->ofbundle;
1951 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1952 * received on 'in_xbundle' in 'vlan'.
1954 * This code repeats all the checks in is_mac_learning_update_needed() because
1955 * the lock was released between there and here and thus the MAC learning state
1956 * could have changed.
1958 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1961 update_learning_table__(const struct xbridge *xbridge,
1962 const struct flow *flow, struct flow_wildcards *wc,
1963 int vlan, struct xbundle *in_xbundle)
1964 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1966 struct mac_entry *mac;
1968 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1972 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1973 if (is_gratuitous_arp(flow, wc)) {
1974 /* We don't want to learn from gratuitous ARP packets that are
1975 * reflected back over bond slaves so we lock the learning table. */
1976 if (!in_xbundle->bond) {
1977 mac_entry_set_grat_arp_lock(mac);
1978 } else if (mac_entry_is_grat_arp_locked(mac)) {
1983 if (mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle) {
1984 /* The log messages here could actually be useful in debugging,
1985 * so keep the rate limit relatively high. */
1986 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1988 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1989 "on port %s in VLAN %d",
1990 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1991 in_xbundle->name, vlan);
1993 mac_entry_set_port(xbridge->ml, mac, in_xbundle->ofbundle);
1998 update_learning_table(const struct xbridge *xbridge,
1999 const struct flow *flow, struct flow_wildcards *wc,
2000 int vlan, struct xbundle *in_xbundle)
2004 /* Don't learn the OFPP_NONE port. */
2005 if (in_xbundle == &ofpp_none_bundle) {
2009 /* First try the common case: no change to MAC learning table. */
2010 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
2011 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
2013 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2016 /* Slow path: MAC learning table might need an update. */
2017 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
2018 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
2019 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2023 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2024 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
2026 update_mcast_snooping_table4__(const struct xbridge *xbridge,
2027 const struct flow *flow,
2028 struct mcast_snooping *ms, int vlan,
2029 struct xbundle *in_xbundle,
2030 const struct dp_packet *packet)
2031 OVS_REQ_WRLOCK(ms->rwlock)
2033 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2035 ovs_be32 ip4 = flow->igmp_group_ip4;
2037 switch (ntohs(flow->tp_src)) {
2038 case IGMP_HOST_MEMBERSHIP_REPORT:
2039 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2040 if (mcast_snooping_add_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2041 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
2042 IP_FMT" is on port %s in VLAN %d",
2043 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2046 case IGMP_HOST_LEAVE_MESSAGE:
2047 if (mcast_snooping_leave_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2048 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
2049 IP_FMT" is on port %s in VLAN %d",
2050 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2053 case IGMP_HOST_MEMBERSHIP_QUERY:
2054 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
2055 in_xbundle->ofbundle)) {
2056 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
2057 IP_FMT" is on port %s in VLAN %d",
2058 xbridge->name, IP_ARGS(flow->nw_src),
2059 in_xbundle->name, vlan);
2062 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2063 if ((count = mcast_snooping_add_report(ms, packet, vlan,
2064 in_xbundle->ofbundle))) {
2065 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2066 "addresses on port %s in VLAN %d",
2067 xbridge->name, count, in_xbundle->name, vlan);
2074 update_mcast_snooping_table6__(const struct xbridge *xbridge,
2075 const struct flow *flow,
2076 struct mcast_snooping *ms, int vlan,
2077 struct xbundle *in_xbundle,
2078 const struct dp_packet *packet)
2079 OVS_REQ_WRLOCK(ms->rwlock)
2081 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2084 switch (ntohs(flow->tp_src)) {
2086 if (!ipv6_addr_equals(&flow->ipv6_src, &in6addr_any)
2087 && mcast_snooping_add_mrouter(ms, vlan, in_xbundle->ofbundle)) {
2088 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query on port %s"
2090 xbridge->name, in_xbundle->name, vlan);
2096 count = mcast_snooping_add_mld(ms, packet, vlan, in_xbundle->ofbundle);
2098 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2099 "addresses on port %s in VLAN %d",
2100 xbridge->name, count, in_xbundle->name, vlan);
2106 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2107 * was received on 'in_xbundle' in 'vlan'. */
2109 update_mcast_snooping_table(const struct xbridge *xbridge,
2110 const struct flow *flow, int vlan,
2111 struct xbundle *in_xbundle,
2112 const struct dp_packet *packet)
2114 struct mcast_snooping *ms = xbridge->ms;
2115 struct xlate_cfg *xcfg;
2116 struct xbundle *mcast_xbundle;
2117 struct mcast_port_bundle *fport;
2119 /* Don't learn the OFPP_NONE port. */
2120 if (in_xbundle == &ofpp_none_bundle) {
2124 /* Don't learn from flood ports */
2125 mcast_xbundle = NULL;
2126 ovs_rwlock_wrlock(&ms->rwlock);
2127 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2128 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2129 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2130 if (mcast_xbundle == in_xbundle) {
2135 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
2136 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2137 update_mcast_snooping_table4__(xbridge, flow, ms, vlan,
2138 in_xbundle, packet);
2140 update_mcast_snooping_table6__(xbridge, flow, ms, vlan,
2141 in_xbundle, packet);
2144 ovs_rwlock_unlock(&ms->rwlock);
2147 /* send the packet to ports having the multicast group learned */
2149 xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2150 struct mcast_snooping *ms OVS_UNUSED,
2151 struct mcast_group *grp,
2152 struct xbundle *in_xbundle, uint16_t vlan)
2153 OVS_REQ_RDLOCK(ms->rwlock)
2155 struct xlate_cfg *xcfg;
2156 struct mcast_group_bundle *b;
2157 struct xbundle *mcast_xbundle;
2159 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2160 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2161 mcast_xbundle = xbundle_lookup(xcfg, b->port);
2162 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2163 xlate_report(ctx, "forwarding to mcast group port");
2164 output_normal(ctx, mcast_xbundle, vlan);
2165 } else if (!mcast_xbundle) {
2166 xlate_report(ctx, "mcast group port is unknown, dropping");
2168 xlate_report(ctx, "mcast group port is input port, dropping");
2173 /* send the packet to ports connected to multicast routers */
2175 xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2176 struct mcast_snooping *ms,
2177 struct xbundle *in_xbundle, uint16_t vlan)
2178 OVS_REQ_RDLOCK(ms->rwlock)
2180 struct xlate_cfg *xcfg;
2181 struct mcast_mrouter_bundle *mrouter;
2182 struct xbundle *mcast_xbundle;
2184 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2185 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2186 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2187 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2188 xlate_report(ctx, "forwarding to mcast router port");
2189 output_normal(ctx, mcast_xbundle, vlan);
2190 } else if (!mcast_xbundle) {
2191 xlate_report(ctx, "mcast router port is unknown, dropping");
2193 xlate_report(ctx, "mcast router port is input port, dropping");
2198 /* send the packet to ports flagged to be flooded */
2200 xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2201 struct mcast_snooping *ms,
2202 struct xbundle *in_xbundle, uint16_t vlan)
2203 OVS_REQ_RDLOCK(ms->rwlock)
2205 struct xlate_cfg *xcfg;
2206 struct mcast_port_bundle *fport;
2207 struct xbundle *mcast_xbundle;
2209 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2210 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2211 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2212 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2213 xlate_report(ctx, "forwarding to mcast flood port");
2214 output_normal(ctx, mcast_xbundle, vlan);
2215 } else if (!mcast_xbundle) {
2216 xlate_report(ctx, "mcast flood port is unknown, dropping");
2218 xlate_report(ctx, "mcast flood port is input port, dropping");
2223 /* forward the Reports to configured ports */
2225 xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
2226 struct mcast_snooping *ms,
2227 struct xbundle *in_xbundle, uint16_t vlan)
2228 OVS_REQ_RDLOCK(ms->rwlock)
2230 struct xlate_cfg *xcfg;
2231 struct mcast_port_bundle *rport;
2232 struct xbundle *mcast_xbundle;
2234 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2235 LIST_FOR_EACH(rport, node, &ms->rport_list) {
2236 mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2237 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2238 xlate_report(ctx, "forwarding Report to mcast flagged port");
2239 output_normal(ctx, mcast_xbundle, vlan);
2240 } else if (!mcast_xbundle) {
2241 xlate_report(ctx, "mcast port is unknown, dropping the Report");
2243 xlate_report(ctx, "mcast port is input port, dropping the Report");
2249 xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2252 struct xbundle *xbundle;
2254 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2255 if (xbundle != in_xbundle
2256 && xbundle_includes_vlan(xbundle, vlan)
2257 && xbundle->floodable
2258 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2259 output_normal(ctx, xbundle, vlan);
2262 ctx->nf_output_iface = NF_OUT_FLOOD;
2266 xlate_normal(struct xlate_ctx *ctx)
2268 struct flow_wildcards *wc = ctx->wc;
2269 struct flow *flow = &ctx->xin->flow;
2270 struct xbundle *in_xbundle;
2271 struct xport *in_port;
2272 struct mac_entry *mac;
2277 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2278 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2279 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2281 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
2282 ctx->xin->packet != NULL, &in_port);
2284 xlate_report(ctx, "no input bundle, dropping");
2288 /* Drop malformed frames. */
2289 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
2290 !(flow->vlan_tci & htons(VLAN_CFI))) {
2291 if (ctx->xin->packet != NULL) {
2292 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2293 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
2294 "VLAN tag received on port %s",
2295 ctx->xbridge->name, in_xbundle->name);
2297 xlate_report(ctx, "partial VLAN tag, dropping");
2301 /* Drop frames on bundles reserved for mirroring. */
2302 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
2303 if (ctx->xin->packet != NULL) {
2304 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2305 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2306 "%s, which is reserved exclusively for mirroring",
2307 ctx->xbridge->name, in_xbundle->name);
2309 xlate_report(ctx, "input port is mirror output port, dropping");
2314 vid = vlan_tci_to_vid(flow->vlan_tci);
2315 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
2316 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2319 vlan = input_vid_to_vlan(in_xbundle, vid);
2321 /* Check other admissibility requirements. */
2322 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2326 /* Learn source MAC. */
2327 if (ctx->xin->may_learn) {
2328 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
2330 if (ctx->xin->xcache) {
2331 struct xc_entry *entry;
2333 /* Save enough info to update mac learning table later. */
2334 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
2335 entry->u.normal.ofproto = ctx->xbridge->ofproto;
2336 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2337 entry->u.normal.vlan = vlan;
2340 /* Determine output bundle. */
2341 if (mcast_snooping_enabled(ctx->xbridge->ms)
2342 && !eth_addr_is_broadcast(flow->dl_dst)
2343 && eth_addr_is_multicast(flow->dl_dst)
2344 && is_ip_any(flow)) {
2345 struct mcast_snooping *ms = ctx->xbridge->ms;
2346 struct mcast_group *grp = NULL;
2348 if (is_igmp(flow)) {
2349 if (mcast_snooping_is_membership(flow->tp_src) ||
2350 mcast_snooping_is_query(flow->tp_src)) {
2351 if (ctx->xin->may_learn) {
2352 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2353 in_xbundle, ctx->xin->packet);
2356 * IGMP packets need to take the slow path, in order to be
2357 * processed for mdb updates. That will prevent expires
2358 * firing off even after hosts have sent reports.
2360 ctx->xout->slow |= SLOW_ACTION;
2363 if (mcast_snooping_is_membership(flow->tp_src)) {
2364 ovs_rwlock_rdlock(&ms->rwlock);
2365 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2366 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2367 * forward IGMP Membership Reports only to those ports where
2368 * multicast routers are attached. Alternatively stated: a
2369 * snooping switch should not forward IGMP Membership Reports
2370 * to ports on which only hosts are attached.
2371 * An administrative control may be provided to override this
2372 * restriction, allowing the report messages to be flooded to
2374 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2375 ovs_rwlock_unlock(&ms->rwlock);
2377 xlate_report(ctx, "multicast traffic, flooding");
2378 xlate_normal_flood(ctx, in_xbundle, vlan);
2381 } else if (is_mld(flow)) {
2382 ctx->xout->slow |= SLOW_ACTION;
2383 if (ctx->xin->may_learn) {
2384 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2385 in_xbundle, ctx->xin->packet);
2387 if (is_mld_report(flow)) {
2388 ovs_rwlock_rdlock(&ms->rwlock);
2389 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2390 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2391 ovs_rwlock_unlock(&ms->rwlock);
2393 xlate_report(ctx, "MLD query, flooding");
2394 xlate_normal_flood(ctx, in_xbundle, vlan);
2397 if ((flow->dl_type == htons(ETH_TYPE_IP)
2398 && ip_is_local_multicast(flow->nw_dst))
2399 || (flow->dl_type == htons(ETH_TYPE_IPV6)
2400 && ipv6_is_all_hosts(&flow->ipv6_dst))) {
2401 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2402 * address in the 224.0.0.x range which are not IGMP must
2403 * be forwarded on all ports */
2404 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2405 xlate_normal_flood(ctx, in_xbundle, vlan);
2410 /* forwarding to group base ports */
2411 ovs_rwlock_rdlock(&ms->rwlock);
2412 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2413 grp = mcast_snooping_lookup4(ms, flow->nw_dst, vlan);
2414 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2415 grp = mcast_snooping_lookup(ms, &flow->ipv6_dst, vlan);
2418 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2419 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2420 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2422 if (mcast_snooping_flood_unreg(ms)) {
2423 xlate_report(ctx, "unregistered multicast, flooding");
2424 xlate_normal_flood(ctx, in_xbundle, vlan);
2426 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2427 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2430 ovs_rwlock_unlock(&ms->rwlock);
2432 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2433 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
2434 mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
2435 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2438 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2439 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2440 if (mac_xbundle && mac_xbundle != in_xbundle) {
2441 xlate_report(ctx, "forwarding to learned port");
2442 output_normal(ctx, mac_xbundle, vlan);
2443 } else if (!mac_xbundle) {
2444 xlate_report(ctx, "learned port is unknown, dropping");
2446 xlate_report(ctx, "learned port is input port, dropping");
2449 xlate_report(ctx, "no learned MAC for destination, flooding");
2450 xlate_normal_flood(ctx, in_xbundle, vlan);
2455 /* Appends a "sample" action for sFlow or IPFIX to 'ctx->odp_actions'. The
2456 * 'probability' is the number of packets out of UINT32_MAX to sample. The
2457 * 'cookie' (of length 'cookie_size' bytes) is passed back in the callback for
2458 * each sampled packet. 'tunnel_out_port', if not ODPP_NONE, is added as the
2459 * OVS_USERSPACE_ATTR_EGRESS_TUN_PORT attribute. If 'include_actions', an
2460 * OVS_USERSPACE_ATTR_ACTIONS attribute is added.
2463 compose_sample_action(struct xlate_ctx *ctx,
2464 const uint32_t probability,
2465 const union user_action_cookie *cookie,
2466 const size_t cookie_size,
2467 const odp_port_t tunnel_out_port,
2468 bool include_actions)
2470 size_t sample_offset = nl_msg_start_nested(ctx->odp_actions,
2471 OVS_ACTION_ATTR_SAMPLE);
2473 nl_msg_put_u32(ctx->odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
2475 size_t actions_offset = nl_msg_start_nested(ctx->odp_actions,
2476 OVS_SAMPLE_ATTR_ACTIONS);
2478 odp_port_t odp_port = ofp_port_to_odp_port(
2479 ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
2480 uint32_t pid = dpif_port_get_pid(ctx->xbridge->dpif, odp_port,
2481 flow_hash_5tuple(&ctx->xin->flow, 0));
2482 int cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2487 nl_msg_end_nested(ctx->odp_actions, actions_offset);
2488 nl_msg_end_nested(ctx->odp_actions, sample_offset);
2490 return cookie_offset;
2493 /* If sFLow is not enabled, returns 0 without doing anything.
2495 * If sFlow is enabled, appends a template "sample" action to the ODP actions
2496 * in 'ctx'. This action is a template because some of the information needed
2497 * to fill it out is not available until flow translation is complete. In this
2498 * case, this functions returns an offset, which is always nonzero, to pass
2499 * later to fix_sflow_action() to fill in the rest of the template. */
2501 compose_sflow_action(struct xlate_ctx *ctx)
2503 struct dpif_sflow *sflow = ctx->xbridge->sflow;
2504 if (!sflow || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
2508 union user_action_cookie cookie = { .type = USER_ACTION_COOKIE_SFLOW };
2509 return compose_sample_action(ctx, dpif_sflow_get_probability(sflow),
2510 &cookie, sizeof cookie.sflow, ODPP_NONE,
2514 /* If IPFIX is enabled, this appends a "sample" action to implement IPFIX to
2515 * 'ctx->odp_actions'. */
2517 compose_ipfix_action(struct xlate_ctx *ctx, odp_port_t output_odp_port)
2519 struct dpif_ipfix *ipfix = ctx->xbridge->ipfix;
2520 odp_port_t tunnel_out_port = ODPP_NONE;
2522 if (!ipfix || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
2526 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2528 if (output_odp_port == ODPP_NONE &&
2529 !dpif_ipfix_get_bridge_exporter_input_sampling(ipfix)) {
2533 /* For output case, output_odp_port is valid*/
2534 if (output_odp_port != ODPP_NONE) {
2535 if (!dpif_ipfix_get_bridge_exporter_output_sampling(ipfix)) {
2538 /* If tunnel sampling is enabled, put an additional option attribute:
2539 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2541 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(ipfix) &&
2542 dpif_ipfix_get_tunnel_port(ipfix, output_odp_port) ) {
2543 tunnel_out_port = output_odp_port;
2547 union user_action_cookie cookie = {
2549 .type = USER_ACTION_COOKIE_IPFIX,
2550 .output_odp_port = output_odp_port,
2553 compose_sample_action(ctx,
2554 dpif_ipfix_get_bridge_exporter_probability(ipfix),
2555 &cookie, sizeof cookie.ipfix, tunnel_out_port,
2559 /* Fix "sample" action according to data collected while composing ODP actions,
2560 * as described in compose_sflow_action().
2562 * 'user_cookie_offset' must be the offset returned by add_sflow_action(). */
2564 fix_sflow_action(struct xlate_ctx *ctx, unsigned int user_cookie_offset)
2566 const struct flow *base = &ctx->base_flow;
2567 union user_action_cookie *cookie;
2569 cookie = ofpbuf_at(ctx->odp_actions, user_cookie_offset,
2570 sizeof cookie->sflow);
2571 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2573 cookie->type = USER_ACTION_COOKIE_SFLOW;
2574 cookie->sflow.vlan_tci = base->vlan_tci;
2576 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2577 * port information") for the interpretation of cookie->output. */
2578 switch (ctx->sflow_n_outputs) {
2580 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2581 cookie->sflow.output = 0x40000000 | 256;
2585 cookie->sflow.output = dpif_sflow_odp_port_to_ifindex(
2586 ctx->xbridge->sflow, ctx->sflow_odp_port);
2587 if (cookie->sflow.output) {
2592 /* 0x80000000 means "multiple output ports. */
2593 cookie->sflow.output = 0x80000000 | ctx->sflow_n_outputs;
2599 process_special(struct xlate_ctx *ctx, const struct xport *xport)
2601 const struct flow *flow = &ctx->xin->flow;
2602 struct flow_wildcards *wc = ctx->wc;
2603 const struct xbridge *xbridge = ctx->xbridge;
2604 const struct dp_packet *packet = ctx->xin->packet;
2605 enum slow_path_reason slow;
2609 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
2611 cfm_process_heartbeat(xport->cfm, packet);
2614 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
2616 bfd_process_packet(xport->bfd, flow, packet);
2617 /* If POLL received, immediately sends FINAL back. */
2618 if (bfd_should_send_packet(xport->bfd)) {
2619 ofproto_dpif_monitor_port_send_soon(xport->ofport);
2623 } else if (xport->xbundle && xport->xbundle->lacp
2624 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2626 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
2629 } else if ((xbridge->stp || xbridge->rstp) &&
2630 stp_should_process_flow(flow, wc)) {
2633 ? stp_process_packet(xport, packet)
2634 : rstp_process_packet(xport, packet);
2637 } else if (xport->lldp && lldp_should_process_flow(xport->lldp, flow)) {
2639 lldp_process_packet(xport->lldp, packet);
2647 ctx->xout->slow |= slow;
2655 tnl_route_lookup_flow(const struct flow *oflow,
2656 ovs_be32 *ip, struct xport **out_port)
2658 char out_dev[IFNAMSIZ];
2659 struct xbridge *xbridge;
2660 struct xlate_cfg *xcfg;
2663 if (!ovs_router_lookup(oflow->tunnel.ip_dst, out_dev, &gw)) {
2670 *ip = oflow->tunnel.ip_dst;
2673 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2676 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
2677 if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
2680 HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
2681 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
2692 xlate_flood_packet(struct xbridge *xbridge, struct dp_packet *packet)
2694 struct ofpact_output output;
2697 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
2698 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
2699 flow_extract(packet, &flow);
2700 flow.in_port.ofp_port = OFPP_NONE;
2701 output.port = OFPP_FLOOD;
2704 return ofproto_dpif_execute_actions(xbridge->ofproto, &flow, NULL,
2705 &output.ofpact, sizeof output,
2710 tnl_send_arp_request(const struct xport *out_dev, const uint8_t eth_src[ETH_ADDR_LEN],
2711 ovs_be32 ip_src, ovs_be32 ip_dst)
2713 struct xbridge *xbridge = out_dev->xbridge;
2714 struct dp_packet packet;
2716 dp_packet_init(&packet, 0);
2717 compose_arp(&packet, ARP_OP_REQUEST,
2718 eth_src, eth_addr_zero, true, ip_src, ip_dst);
2720 xlate_flood_packet(xbridge, &packet);
2721 dp_packet_uninit(&packet);
2725 build_tunnel_send(struct xlate_ctx *ctx, const struct xport *xport,
2726 const struct flow *flow, odp_port_t tunnel_odp_port)
2728 struct ovs_action_push_tnl tnl_push_data;
2729 struct xport *out_dev = NULL;
2730 ovs_be32 s_ip, d_ip = 0;
2731 uint8_t smac[ETH_ADDR_LEN];
2732 uint8_t dmac[ETH_ADDR_LEN];
2735 err = tnl_route_lookup_flow(flow, &d_ip, &out_dev);
2737 xlate_report(ctx, "native tunnel routing failed");
2740 xlate_report(ctx, "tunneling to "IP_FMT" via %s",
2741 IP_ARGS(d_ip), netdev_get_name(out_dev->netdev));
2743 /* Use mac addr of bridge port of the peer. */
2744 err = netdev_get_etheraddr(out_dev->netdev, smac);
2746 xlate_report(ctx, "tunnel output device lacks Ethernet address");
2750 err = netdev_get_in4(out_dev->netdev, (struct in_addr *) &s_ip, NULL);
2752 xlate_report(ctx, "tunnel output device lacks IPv4 address");
2756 err = tnl_arp_lookup(out_dev->xbridge->name, d_ip, dmac);
2758 xlate_report(ctx, "ARP cache miss for "IP_FMT" on bridge %s, "
2759 "sending ARP request",
2760 IP_ARGS(d_ip), out_dev->xbridge->name);
2761 tnl_send_arp_request(out_dev, smac, s_ip, d_ip);
2764 if (ctx->xin->xcache) {
2765 struct xc_entry *entry;
2767 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_ARP);
2768 ovs_strlcpy(entry->u.tnl_arp_cache.br_name, out_dev->xbridge->name,
2769 sizeof entry->u.tnl_arp_cache.br_name);
2770 entry->u.tnl_arp_cache.d_ip = d_ip;
2773 xlate_report(ctx, "tunneling from "ETH_ADDR_FMT" "IP_FMT
2774 " to "ETH_ADDR_FMT" "IP_FMT,
2775 ETH_ADDR_ARGS(smac), IP_ARGS(s_ip),
2776 ETH_ADDR_ARGS(dmac), IP_ARGS(d_ip));
2777 err = tnl_port_build_header(xport->ofport, flow,
2778 dmac, smac, s_ip, &tnl_push_data);
2782 tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
2783 tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
2784 odp_put_tnl_push_action(ctx->odp_actions, &tnl_push_data);
2789 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
2790 const struct xlate_bond_recirc *xr, bool check_stp)
2792 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
2793 struct flow_wildcards *wc = ctx->wc;
2794 struct flow *flow = &ctx->xin->flow;
2795 struct flow_tnl flow_tnl;
2796 ovs_be16 flow_vlan_tci;
2797 uint32_t flow_pkt_mark;
2798 uint8_t flow_nw_tos;
2799 odp_port_t out_port, odp_port;
2800 bool tnl_push_pop_send = false;
2803 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2804 * before traversing a patch port. */
2805 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 33);
2806 memset(&flow_tnl, 0, sizeof flow_tnl);
2809 xlate_report(ctx, "Nonexistent output port");
2811 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
2812 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2814 } else if (check_stp) {
2815 if (is_stp(&ctx->base_flow)) {
2816 if (!xport_stp_should_forward_bpdu(xport) &&
2817 !xport_rstp_should_manage_bpdu(xport)) {
2818 if (ctx->xbridge->stp != NULL) {
2819 xlate_report(ctx, "STP not in listening state, "
2820 "skipping bpdu output");
2821 } else if (ctx->xbridge->rstp != NULL) {
2822 xlate_report(ctx, "RSTP not managing BPDU in this state, "
2823 "skipping bpdu output");
2827 } else if (!xport_stp_forward_state(xport) ||
2828 !xport_rstp_forward_state(xport)) {
2829 if (ctx->xbridge->stp != NULL) {
2830 xlate_report(ctx, "STP not in forwarding state, "
2832 } else if (ctx->xbridge->rstp != NULL) {
2833 xlate_report(ctx, "RSTP not in forwarding state, "
2841 const struct xport *peer = xport->peer;
2842 struct flow old_flow = ctx->xin->flow;
2843 bool old_was_mpls = ctx->was_mpls;
2844 cls_version_t old_version = ctx->tables_version;
2845 struct ofpbuf old_stack = ctx->stack;
2846 union mf_subvalue new_stack[1024 / sizeof(union mf_subvalue)];
2847 struct ofpbuf old_action_set = ctx->action_set;
2848 uint64_t actset_stub[1024 / 8];
2850 ofpbuf_use_stub(&ctx->stack, new_stack, sizeof new_stack);
2851 ofpbuf_use_stub(&ctx->action_set, actset_stub, sizeof actset_stub);
2852 ctx->xbridge = peer->xbridge;
2853 flow->in_port.ofp_port = peer->ofp_port;
2854 flow->metadata = htonll(0);
2855 memset(&flow->tunnel, 0, sizeof flow->tunnel);
2856 memset(flow->regs, 0, sizeof flow->regs);
2857 flow->actset_output = OFPP_UNSET;
2859 /* The bridge is now known so obtain its table version. */
2861 = ofproto_dpif_get_tables_version(ctx->xbridge->ofproto);
2863 if (!process_special(ctx, peer) && may_receive(peer, ctx)) {
2864 if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
2865 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2866 if (ctx->action_set.size) {
2867 /* Translate action set only if not dropping the packet and
2868 * not recirculating. */
2869 if (!exit_recirculates(ctx)) {
2870 xlate_action_set(ctx);
2873 /* Check if need to recirculate. */
2874 if (exit_recirculates(ctx)) {
2875 compose_recirculate_action(ctx);
2878 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
2879 * the learning action look at the packet, then drop it. */
2880 struct flow old_base_flow = ctx->base_flow;
2881 size_t old_size = ctx->odp_actions->size;
2882 mirror_mask_t old_mirrors = ctx->mirrors;
2884 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2885 ctx->mirrors = old_mirrors;
2886 ctx->base_flow = old_base_flow;
2887 ctx->odp_actions->size = old_size;
2889 /* Undo changes that may have been done for recirculation. */
2890 if (exit_recirculates(ctx)) {
2891 ctx->action_set.size = ctx->recirc_action_offset;
2892 ctx->recirc_action_offset = -1;
2893 ctx->last_unroll_offset = -1;
2898 ctx->xin->flow = old_flow;
2899 ctx->xbridge = xport->xbridge;
2900 ofpbuf_uninit(&ctx->action_set);
2901 ctx->action_set = old_action_set;
2902 ofpbuf_uninit(&ctx->stack);
2903 ctx->stack = old_stack;
2905 /* Restore calling bridge's lookup version. */
2906 ctx->tables_version = old_version;
2908 /* The peer bridge popping MPLS should have no effect on the original
2910 ctx->was_mpls = old_was_mpls;
2912 /* The fact that the peer bridge exits (for any reason) does not mean
2913 * that the original bridge should exit. Specifically, if the peer
2914 * bridge recirculates (which typically modifies the packet), the
2915 * original bridge must continue processing with the original, not the
2916 * recirculated packet! */
2919 if (ctx->xin->resubmit_stats) {
2920 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2921 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
2923 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
2926 if (ctx->xin->xcache) {
2927 struct xc_entry *entry;
2929 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2930 entry->u.dev.tx = netdev_ref(xport->netdev);
2931 entry->u.dev.rx = netdev_ref(peer->netdev);
2932 entry->u.dev.bfd = bfd_ref(peer->bfd);
2937 flow_vlan_tci = flow->vlan_tci;
2938 flow_pkt_mark = flow->pkt_mark;
2939 flow_nw_tos = flow->nw_tos;
2941 if (count_skb_priorities(xport)) {
2942 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
2943 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
2944 wc->masks.nw_tos |= IP_DSCP_MASK;
2945 flow->nw_tos &= ~IP_DSCP_MASK;
2946 flow->nw_tos |= dscp;
2950 if (xport->is_tunnel) {
2951 /* Save tunnel metadata so that changes made due to
2952 * the Logical (tunnel) Port are not visible for any further
2953 * matches, while explicit set actions on tunnel metadata are.
2955 flow_tnl = flow->tunnel;
2956 odp_port = tnl_port_send(xport->ofport, flow, ctx->wc);
2957 if (odp_port == ODPP_NONE) {
2958 xlate_report(ctx, "Tunneling decided against output");
2959 goto out; /* restore flow_nw_tos */
2961 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
2962 xlate_report(ctx, "Not tunneling to our own address");
2963 goto out; /* restore flow_nw_tos */
2965 if (ctx->xin->resubmit_stats) {
2966 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2968 if (ctx->xin->xcache) {
2969 struct xc_entry *entry;
2971 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2972 entry->u.dev.tx = netdev_ref(xport->netdev);
2974 out_port = odp_port;
2975 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
2976 xlate_report(ctx, "output to native tunnel");
2977 tnl_push_pop_send = true;
2979 xlate_report(ctx, "output to kernel tunnel");
2980 commit_odp_tunnel_action(flow, &ctx->base_flow, ctx->odp_actions);
2981 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2984 odp_port = xport->odp_port;
2985 out_port = odp_port;
2986 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
2987 ofp_port_t vlandev_port;
2989 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2990 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
2991 ofp_port, flow->vlan_tci);
2992 if (vlandev_port != ofp_port) {
2993 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
2994 flow->vlan_tci = htons(0);
2999 if (out_port != ODPP_NONE) {
3000 bool use_masked = ctx->xbridge->support.masked_set_action;
3002 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
3007 struct ovs_action_hash *act_hash;
3010 act_hash = nl_msg_put_unspec_uninit(ctx->odp_actions,
3011 OVS_ACTION_ATTR_HASH,
3013 act_hash->hash_alg = xr->hash_alg;
3014 act_hash->hash_basis = xr->hash_basis;
3016 /* Recirc action. */
3017 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC,
3021 if (tnl_push_pop_send) {
3022 build_tunnel_send(ctx, xport, flow, odp_port);
3023 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3025 odp_port_t odp_tnl_port = ODPP_NONE;
3027 /* XXX: Write better Filter for tunnel port. We can use inport
3028 * int tunnel-port flow to avoid these checks completely. */
3029 if (ofp_port == OFPP_LOCAL &&
3030 ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3032 odp_tnl_port = tnl_port_map_lookup(flow, wc);
3035 if (odp_tnl_port != ODPP_NONE) {
3036 nl_msg_put_odp_port(ctx->odp_actions,
3037 OVS_ACTION_ATTR_TUNNEL_POP,
3040 /* Tunnel push-pop action is not compatible with
3042 compose_ipfix_action(ctx, out_port);
3043 nl_msg_put_odp_port(ctx->odp_actions,
3044 OVS_ACTION_ATTR_OUTPUT,
3050 ctx->sflow_odp_port = odp_port;
3051 ctx->sflow_n_outputs++;
3052 ctx->nf_output_iface = ofp_port;
3055 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
3056 mirror_packet(ctx, xport->xbundle,
3057 xbundle_mirror_dst(xport->xbundle->xbridge,
3063 flow->vlan_tci = flow_vlan_tci;
3064 flow->pkt_mark = flow_pkt_mark;
3065 flow->nw_tos = flow_nw_tos;
3069 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port,
3070 const struct xlate_bond_recirc *xr)
3072 compose_output_action__(ctx, ofp_port, xr, true);
3076 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
3078 struct rule_dpif *old_rule = ctx->rule;
3079 ovs_be64 old_cookie = ctx->rule_cookie;
3080 const struct rule_actions *actions;
3082 if (ctx->xin->resubmit_stats) {
3083 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
3089 ctx->rule_cookie = rule_dpif_get_flow_cookie(rule);
3090 actions = rule_dpif_get_actions(rule);
3091 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
3092 ctx->rule_cookie = old_cookie;
3093 ctx->rule = old_rule;
3098 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
3100 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3102 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
3103 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
3104 MAX_RESUBMIT_RECURSION);
3105 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
3106 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
3107 } else if (ctx->odp_actions->size > UINT16_MAX) {
3108 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
3109 } else if (ctx->stack.size >= 65536) {
3110 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
3119 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
3120 bool may_packet_in, bool honor_table_miss)
3122 /* Check if we need to recirculate before matching in a table. */
3123 if (ctx->was_mpls) {
3124 ctx_trigger_recirculation(ctx);
3127 if (xlate_resubmit_resource_check(ctx)) {
3128 uint8_t old_table_id = ctx->table_id;
3129 struct rule_dpif *rule;
3131 ctx->table_id = table_id;
3133 rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
3134 ctx->tables_version,
3135 &ctx->xin->flow, ctx->xin->wc,
3136 ctx->xin->xcache != NULL,
3137 ctx->xin->resubmit_stats,
3138 &ctx->table_id, in_port,
3139 may_packet_in, honor_table_miss);
3141 if (OVS_UNLIKELY(ctx->xin->resubmit_hook)) {
3142 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse + 1);
3146 /* Fill in the cache entry here instead of xlate_recursively
3147 * to make the reference counting more explicit. We take a
3148 * reference in the lookups above if we are going to cache the
3150 if (ctx->xin->xcache) {
3151 struct xc_entry *entry;
3153 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
3154 entry->u.rule = rule;
3156 xlate_recursively(ctx, rule);
3159 ctx->table_id = old_table_id;
3167 xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
3168 struct ofputil_bucket *bucket)
3170 if (ctx->xin->resubmit_stats) {
3171 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
3173 if (ctx->xin->xcache) {
3174 struct xc_entry *entry;
3176 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
3177 entry->u.group.group = group_dpif_ref(group);
3178 entry->u.group.bucket = bucket;
3183 xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
3185 uint64_t action_list_stub[1024 / 8];
3186 struct ofpbuf action_list, action_set;
3187 struct flow old_flow = ctx->xin->flow;
3188 bool old_was_mpls = ctx->was_mpls;
3190 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
3191 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3193 ofpacts_execute_action_set(&action_list, &action_set);
3195 do_xlate_actions(action_list.data, action_list.size, ctx);
3198 ofpbuf_uninit(&action_set);
3199 ofpbuf_uninit(&action_list);
3201 /* Check if need to recirculate. */
3202 if (exit_recirculates(ctx)) {
3203 compose_recirculate_action(ctx);
3206 /* Roll back flow to previous state.
3207 * This is equivalent to cloning the packet for each bucket.
3209 * As a side effect any subsequently applied actions will
3210 * also effectively be applied to a clone of the packet taken
3211 * just before applying the all or indirect group.
3213 * Note that group buckets are action sets, hence they cannot modify the
3214 * main action set. Also any stack actions are ignored when executing an
3215 * action set, so group buckets cannot change the stack either.
3216 * However, we do allow resubmit actions in group buckets, which could
3217 * break the above assumptions. It is up to the controller to not mess up
3218 * with the action_set and stack in the tables resubmitted to from
3220 ctx->xin->flow = old_flow;
3222 /* The group bucket popping MPLS should have no effect after bucket
3224 ctx->was_mpls = old_was_mpls;
3226 /* The fact that the group bucket exits (for any reason) does not mean that
3227 * the translation after the group action should exit. Specifically, if
3228 * the group bucket recirculates (which typically modifies the packet), the
3229 * actions after the group action must continue processing with the
3230 * original, not the recirculated packet! */
3235 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3237 struct ofputil_bucket *bucket;
3238 const struct ovs_list *buckets;
3240 group_dpif_get_buckets(group, &buckets);
3242 LIST_FOR_EACH (bucket, list_node, buckets) {
3243 xlate_group_bucket(ctx, bucket);
3245 xlate_group_stats(ctx, group, NULL);
3249 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3251 struct ofputil_bucket *bucket;
3253 bucket = group_first_live_bucket(ctx, group, 0);
3255 xlate_group_bucket(ctx, bucket);
3256 xlate_group_stats(ctx, group, bucket);
3261 xlate_default_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3263 struct flow_wildcards *wc = ctx->wc;
3264 struct ofputil_bucket *bucket;
3267 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
3268 flow_mask_hash_fields(&ctx->xin->flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
3269 bucket = group_best_live_bucket(ctx, group, basis);
3271 xlate_group_bucket(ctx, bucket);
3272 xlate_group_stats(ctx, group, bucket);
3277 xlate_hash_fields_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3279 struct mf_bitmap hash_fields = MF_BITMAP_INITIALIZER;
3280 const struct field_array *fields;
3281 struct ofputil_bucket *bucket;
3285 fields = group_dpif_get_fields(group);
3286 basis = hash_uint64(group_dpif_get_selection_method_param(group));
3288 /* Determine which fields to hash */
3289 for (i = 0; i < MFF_N_IDS; i++) {
3290 if (bitmap_is_set(fields->used.bm, i)) {
3291 const struct mf_field *mf;
3293 /* If the field is already present in 'hash_fields' then
3294 * this loop has already checked that it and its pre-requisites
3295 * are present in the flow and its pre-requisites have
3296 * already been added to 'hash_fields'. There is nothing more
3297 * to do here and as an optimisation the loop can continue. */
3298 if (bitmap_is_set(hash_fields.bm, i)) {
3304 /* Only hash a field if it and its pre-requisites are present
3306 if (!mf_are_prereqs_ok(mf, &ctx->xin->flow)) {
3310 /* Hash both the field and its pre-requisites */
3311 mf_bitmap_set_field_and_prereqs(mf, &hash_fields);
3315 /* Hash the fields */
3316 for (i = 0; i < MFF_N_IDS; i++) {
3317 if (bitmap_is_set(hash_fields.bm, i)) {
3318 const struct mf_field *mf = mf_from_id(i);
3319 union mf_value value;
3322 mf_get_value(mf, &ctx->xin->flow, &value);
3323 /* This seems inefficient but so does apply_mask() */
3324 for (j = 0; j < mf->n_bytes; j++) {
3325 ((uint8_t *) &value)[j] &= ((uint8_t *) &fields->value[i])[j];
3327 basis = hash_bytes(&value, mf->n_bytes, basis);
3329 mf_mask_field(mf, &ctx->wc->masks);
3333 bucket = group_best_live_bucket(ctx, group, basis);
3335 xlate_group_bucket(ctx, bucket);
3336 xlate_group_stats(ctx, group, bucket);
3341 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3343 const char *selection_method = group_dpif_get_selection_method(group);
3345 if (selection_method[0] == '\0') {
3346 xlate_default_select_group(ctx, group);
3347 } else if (!strcasecmp("hash", selection_method)) {
3348 xlate_hash_fields_select_group(ctx, group);
3350 /* Parsing of groups should ensure this never happens */
3356 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3358 bool was_in_group = ctx->in_group;
3359 ctx->in_group = true;
3361 switch (group_dpif_get_type(group)) {
3363 case OFPGT11_INDIRECT:
3364 xlate_all_group(ctx, group);
3366 case OFPGT11_SELECT:
3367 xlate_select_group(ctx, group);
3370 xlate_ff_group(ctx, group);
3375 group_dpif_unref(group);
3377 ctx->in_group = was_in_group;
3381 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3383 if (xlate_resubmit_resource_check(ctx)) {
3384 struct group_dpif *group;
3387 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
3389 xlate_group_action__(ctx, group);
3399 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
3400 const struct ofpact_resubmit *resubmit)
3404 bool may_packet_in = false;
3405 bool honor_table_miss = false;
3407 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
3408 /* Still allow missed packets to be sent to the controller
3409 * if resubmitting from an internal table. */
3410 may_packet_in = true;
3411 honor_table_miss = true;
3414 in_port = resubmit->in_port;
3415 if (in_port == OFPP_IN_PORT) {
3416 in_port = ctx->xin->flow.in_port.ofp_port;
3419 table_id = resubmit->table_id;
3420 if (table_id == 255) {
3421 table_id = ctx->table_id;
3424 xlate_table_action(ctx, in_port, table_id, may_packet_in,
3429 flood_packets(struct xlate_ctx *ctx, bool all)
3431 const struct xport *xport;
3433 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
3434 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
3439 compose_output_action__(ctx, xport->ofp_port, NULL, false);
3440 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
3441 compose_output_action(ctx, xport->ofp_port, NULL);
3445 ctx->nf_output_iface = NF_OUT_FLOOD;
3449 execute_controller_action(struct xlate_ctx *ctx, int len,
3450 enum ofp_packet_in_reason reason,
3451 uint16_t controller_id)
3453 struct ofproto_packet_in *pin;
3454 struct dp_packet *packet;
3457 ctx->xout->slow |= SLOW_CONTROLLER;
3458 if (!ctx->xin->packet) {
3462 packet = dp_packet_clone(ctx->xin->packet);
3464 use_masked = ctx->xbridge->support.masked_set_action;
3465 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3467 ctx->wc, use_masked);
3469 odp_execute_actions(NULL, &packet, 1, false,
3470 ctx->odp_actions->data, ctx->odp_actions->size, NULL);
3472 pin = xmalloc(sizeof *pin);
3473 pin->up.packet_len = dp_packet_size(packet);
3474 pin->up.packet = dp_packet_steal_data(packet);
3475 pin->up.reason = reason;
3476 pin->up.table_id = ctx->table_id;
3477 pin->up.cookie = ctx->rule_cookie;
3479 flow_get_metadata(&ctx->xin->flow, &pin->up.flow_metadata);
3481 pin->controller_id = controller_id;
3482 pin->send_len = len;
3483 /* If a rule is a table-miss rule then this is
3484 * a table-miss handled by a table-miss rule.
3486 * Else, if rule is internal and has a controller action,
3487 * the later being implied by the rule being processed here,
3488 * then this is a table-miss handled without a table-miss rule.
3490 * Otherwise this is not a table-miss. */
3491 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
3493 if (rule_dpif_is_table_miss(ctx->rule)) {
3494 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
3495 } else if (rule_dpif_is_internal(ctx->rule)) {
3496 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
3499 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
3500 dp_packet_delete(packet);
3503 /* Called only when ctx->recirc_action_offset is set. */
3505 compose_recirculate_action(struct xlate_ctx *ctx)
3507 struct recirc_metadata md;
3511 use_masked = ctx->xbridge->support.masked_set_action;
3512 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3514 ctx->wc, use_masked);
3516 recirc_metadata_from_flow(&md, &ctx->xin->flow);
3518 ovs_assert(ctx->recirc_action_offset >= 0);
3520 struct recirc_state state = {
3522 .ofproto = ctx->xbridge->ofproto,
3524 .stack = &ctx->stack,
3525 .mirrors = ctx->mirrors,
3526 .action_set_len = ctx->recirc_action_offset,
3527 .ofpacts_len = ctx->action_set.size,
3528 .ofpacts = ctx->action_set.data,
3531 /* Only allocate recirculation ID if we have a packet. */
3532 if (ctx->xin->packet) {
3533 /* Allocate a unique recirc id for the given metadata state in the
3534 * flow. The life-cycle of this recirc id is managed by associating it
3535 * with the udpif key ('ukey') created for each new datapath flow. */
3536 id = recirc_alloc_id_ctx(&state);
3538 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3539 VLOG_ERR_RL(&rl, "Failed to allocate recirculation id");
3542 xlate_out_add_recirc(ctx->xout, id);
3544 /* Look up an existing recirc id for the given metadata state in the
3545 * flow. No new reference is taken, as the ID is RCU protected and is
3546 * only required temporarily for verification.
3548 * This might fail and return 0. We let zero 'id' to be used in the
3549 * RECIRC action below, which will fail all revalidations as zero is
3550 * not a valid recirculation ID. */
3551 id = recirc_find_id(&state);
3554 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
3556 /* Undo changes done by recirculation. */
3557 ctx->action_set.size = ctx->recirc_action_offset;
3558 ctx->recirc_action_offset = -1;
3559 ctx->last_unroll_offset = -1;
3563 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
3565 struct flow *flow = &ctx->xin->flow;
3568 ovs_assert(eth_type_mpls(mpls->ethertype));
3570 n = flow_count_mpls_labels(flow, ctx->wc);
3572 bool use_masked = ctx->xbridge->support.masked_set_action;
3574 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
3576 ctx->wc, use_masked);
3577 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3578 if (ctx->xin->packet != NULL) {
3579 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3580 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3581 "MPLS push action can't be performed as it would "
3582 "have more MPLS LSEs than the %d supported.",
3583 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3589 flow_push_mpls(flow, n, mpls->ethertype, ctx->wc);
3593 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
3595 struct flow *flow = &ctx->xin->flow;
3596 int n = flow_count_mpls_labels(flow, ctx->wc);
3598 if (flow_pop_mpls(flow, n, eth_type, ctx->wc)) {
3599 if (ctx->xbridge->support.odp.recirc) {
3600 ctx->was_mpls = true;
3602 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3603 if (ctx->xin->packet != NULL) {
3604 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3605 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3606 "MPLS pop action can't be performed as it has "
3607 "more MPLS LSEs than the %d supported.",
3608 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3611 ofpbuf_clear(ctx->odp_actions);
3616 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3618 struct flow *flow = &ctx->xin->flow;
3620 if (!is_ip_any(flow)) {
3624 ctx->wc->masks.nw_ttl = 0xff;
3625 if (flow->nw_ttl > 1) {
3631 for (i = 0; i < ids->n_controllers; i++) {
3632 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3636 /* Stop processing for current table. */
3642 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3644 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3645 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
3646 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
3651 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3653 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3654 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
3655 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
3660 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
3662 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3663 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3664 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
3669 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
3671 struct flow *flow = &ctx->xin->flow;
3673 if (eth_type_mpls(flow->dl_type)) {
3674 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
3676 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3679 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3682 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
3686 /* Stop processing for current table. */
3691 xlate_output_action(struct xlate_ctx *ctx,
3692 ofp_port_t port, uint16_t max_len, bool may_packet_in)
3694 ofp_port_t prev_nf_output_iface = ctx->nf_output_iface;
3696 ctx->nf_output_iface = NF_OUT_DROP;
3700 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port, NULL);
3703 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3704 0, may_packet_in, true);
3710 flood_packets(ctx, false);
3713 flood_packets(ctx, true);
3715 case OFPP_CONTROLLER:
3716 execute_controller_action(ctx, max_len,
3717 (ctx->in_group ? OFPR_GROUP
3718 : ctx->in_action_set ? OFPR_ACTION_SET
3726 if (port != ctx->xin->flow.in_port.ofp_port) {
3727 compose_output_action(ctx, port, NULL);
3729 xlate_report(ctx, "skipping output to input port");
3734 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3735 ctx->nf_output_iface = NF_OUT_FLOOD;
3736 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3737 ctx->nf_output_iface = prev_nf_output_iface;
3738 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3739 ctx->nf_output_iface != NF_OUT_FLOOD) {
3740 ctx->nf_output_iface = NF_OUT_MULTI;
3745 xlate_output_reg_action(struct xlate_ctx *ctx,
3746 const struct ofpact_output_reg *or)
3748 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3749 if (port <= UINT16_MAX) {
3750 union mf_subvalue value;
3752 memset(&value, 0xff, sizeof value);
3753 mf_write_subfield_flow(&or->src, &value, &ctx->wc->masks);
3754 xlate_output_action(ctx, u16_to_ofp(port),
3755 or->max_len, false);
3760 xlate_enqueue_action(struct xlate_ctx *ctx,
3761 const struct ofpact_enqueue *enqueue)
3763 ofp_port_t ofp_port = enqueue->port;
3764 uint32_t queue_id = enqueue->queue;
3765 uint32_t flow_priority, priority;
3768 /* Translate queue to priority. */
3769 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
3771 /* Fall back to ordinary output action. */
3772 xlate_output_action(ctx, enqueue->port, 0, false);
3776 /* Check output port. */
3777 if (ofp_port == OFPP_IN_PORT) {
3778 ofp_port = ctx->xin->flow.in_port.ofp_port;
3779 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
3783 /* Add datapath actions. */
3784 flow_priority = ctx->xin->flow.skb_priority;
3785 ctx->xin->flow.skb_priority = priority;
3786 compose_output_action(ctx, ofp_port, NULL);
3787 ctx->xin->flow.skb_priority = flow_priority;
3789 /* Update NetFlow output port. */
3790 if (ctx->nf_output_iface == NF_OUT_DROP) {
3791 ctx->nf_output_iface = ofp_port;
3792 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3793 ctx->nf_output_iface = NF_OUT_MULTI;
3798 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3800 uint32_t skb_priority;
3802 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
3803 ctx->xin->flow.skb_priority = skb_priority;
3805 /* Couldn't translate queue to a priority. Nothing to do. A warning
3806 * has already been logged. */
3811 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
3813 const struct xbridge *xbridge = xbridge_;
3824 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3827 port = get_ofp_port(xbridge, ofp_port);
3828 return port ? port->may_enable : false;
3833 xlate_bundle_action(struct xlate_ctx *ctx,
3834 const struct ofpact_bundle *bundle)
3838 port = bundle_execute(bundle, &ctx->xin->flow, ctx->wc, slave_enabled_cb,
3839 CONST_CAST(struct xbridge *, ctx->xbridge));
3840 if (bundle->dst.field) {
3841 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow, ctx->wc);
3843 xlate_output_action(ctx, port, 0, false);
3848 xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
3849 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
3851 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
3852 if (ctx->xin->may_learn) {
3853 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
3858 xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
3860 learn_mask(learn, ctx->wc);
3862 if (ctx->xin->xcache) {
3863 struct xc_entry *entry;
3865 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
3866 entry->u.learn.ofproto = ctx->xbridge->ofproto;
3867 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
3868 entry->u.learn.ofpacts = ofpbuf_new(64);
3869 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
3870 entry->u.learn.ofpacts);
3871 } else if (ctx->xin->may_learn) {
3872 uint64_t ofpacts_stub[1024 / 8];
3873 struct ofputil_flow_mod fm;
3874 struct ofpbuf ofpacts;
3876 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3877 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
3878 ofpbuf_uninit(&ofpacts);
3883 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
3884 uint16_t idle_timeout, uint16_t hard_timeout)
3886 if (tcp_flags & (TCP_FIN | TCP_RST)) {
3887 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
3892 xlate_fin_timeout(struct xlate_ctx *ctx,
3893 const struct ofpact_fin_timeout *oft)
3896 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
3897 oft->fin_idle_timeout, oft->fin_hard_timeout);
3898 if (ctx->xin->xcache) {
3899 struct xc_entry *entry;
3901 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
3902 /* XC_RULE already holds a reference on the rule, none is taken
3904 entry->u.fin.rule = ctx->rule;
3905 entry->u.fin.idle = oft->fin_idle_timeout;
3906 entry->u.fin.hard = oft->fin_hard_timeout;
3912 xlate_sample_action(struct xlate_ctx *ctx,
3913 const struct ofpact_sample *os)
3915 /* Scale the probability from 16-bit to 32-bit while representing
3916 * the same percentage. */
3917 uint32_t probability = (os->probability << 16) | os->probability;
3920 if (!ctx->xbridge->support.variable_length_userdata) {
3921 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3923 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
3924 "lacks support (needs Linux 3.10+ or kernel module from "
3929 use_masked = ctx->xbridge->support.masked_set_action;
3930 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3932 ctx->wc, use_masked);
3934 union user_action_cookie cookie = {
3936 .type = USER_ACTION_COOKIE_FLOW_SAMPLE,
3937 .probability = os->probability,
3938 .collector_set_id = os->collector_set_id,
3939 .obs_domain_id = os->obs_domain_id,
3940 .obs_point_id = os->obs_point_id,
3943 compose_sample_action(ctx, probability, &cookie, sizeof cookie.flow_sample,
3948 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
3950 if (xport->config & (is_stp(&ctx->xin->flow)
3951 ? OFPUTIL_PC_NO_RECV_STP
3952 : OFPUTIL_PC_NO_RECV)) {
3956 /* Only drop packets here if both forwarding and learning are
3957 * disabled. If just learning is enabled, we need to have
3958 * OFPP_NORMAL and the learning action have a look at the packet
3959 * before we can drop it. */
3960 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
3961 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
3969 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
3971 const struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3972 size_t on_len = ofpact_nest_get_action_len(on);
3973 const struct ofpact *inner;
3975 /* Maintain actset_output depending on the contents of the action set:
3977 * - OFPP_UNSET, if there is no "output" action.
3979 * - The output port, if there is an "output" action and no "group"
3982 * - OFPP_UNSET, if there is a "group" action.
3984 if (!ctx->action_set_has_group) {
3985 OFPACT_FOR_EACH (inner, on->actions, on_len) {
3986 if (inner->type == OFPACT_OUTPUT) {
3987 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(inner)->port;
3988 } else if (inner->type == OFPACT_GROUP) {
3989 ctx->xin->flow.actset_output = OFPP_UNSET;
3990 ctx->action_set_has_group = true;
3996 ofpbuf_put(&ctx->action_set, on->actions, on_len);
3997 ofpact_pad(&ctx->action_set);
4001 xlate_action_set(struct xlate_ctx *ctx)
4003 uint64_t action_list_stub[1024 / 64];
4004 struct ofpbuf action_list;
4006 ctx->in_action_set = true;
4007 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
4008 ofpacts_execute_action_set(&action_list, &ctx->action_set);
4009 /* Clear the action set, as it is not needed any more. */
4010 ofpbuf_clear(&ctx->action_set);
4011 do_xlate_actions(action_list.data, action_list.size, ctx);
4012 ctx->in_action_set = false;
4013 ofpbuf_uninit(&action_list);
4017 recirc_put_unroll_xlate(struct xlate_ctx *ctx)
4019 struct ofpact_unroll_xlate *unroll;
4021 unroll = ctx->last_unroll_offset < 0
4023 : ALIGNED_CAST(struct ofpact_unroll_xlate *,
4024 (char *)ctx->action_set.data + ctx->last_unroll_offset);
4026 /* Restore the table_id and rule cookie for a potential PACKET
4029 (ctx->table_id != unroll->rule_table_id
4030 || ctx->rule_cookie != unroll->rule_cookie)) {
4032 ctx->last_unroll_offset = ctx->action_set.size;
4033 unroll = ofpact_put_UNROLL_XLATE(&ctx->action_set);
4034 unroll->rule_table_id = ctx->table_id;
4035 unroll->rule_cookie = ctx->rule_cookie;
4040 /* Copy remaining actions to the action_set to be executed after recirculation.
4041 * UNROLL_XLATE action is inserted, if not already done so, before actions that
4042 * may generate PACKET_INs from the current table and without matching another
4045 recirc_unroll_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4046 struct xlate_ctx *ctx)
4048 const struct ofpact *a;
4050 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4052 /* May generate PACKET INs. */
4053 case OFPACT_OUTPUT_REG:
4056 case OFPACT_CONTROLLER:
4057 case OFPACT_DEC_MPLS_TTL:
4058 case OFPACT_DEC_TTL:
4059 recirc_put_unroll_xlate(ctx);
4062 /* These may not generate PACKET INs. */
4063 case OFPACT_SET_TUNNEL:
4064 case OFPACT_REG_MOVE:
4065 case OFPACT_SET_FIELD:
4066 case OFPACT_STACK_PUSH:
4067 case OFPACT_STACK_POP:
4069 case OFPACT_WRITE_METADATA:
4070 case OFPACT_RESUBMIT: /* May indirectly generate PACKET INs, */
4071 case OFPACT_GOTO_TABLE: /* but from a different table and rule. */
4072 case OFPACT_ENQUEUE:
4073 case OFPACT_SET_VLAN_VID:
4074 case OFPACT_SET_VLAN_PCP:
4075 case OFPACT_STRIP_VLAN:
4076 case OFPACT_PUSH_VLAN:
4077 case OFPACT_SET_ETH_SRC:
4078 case OFPACT_SET_ETH_DST:
4079 case OFPACT_SET_IPV4_SRC:
4080 case OFPACT_SET_IPV4_DST:
4081 case OFPACT_SET_IP_DSCP:
4082 case OFPACT_SET_IP_ECN:
4083 case OFPACT_SET_IP_TTL:
4084 case OFPACT_SET_L4_SRC_PORT:
4085 case OFPACT_SET_L4_DST_PORT:
4086 case OFPACT_SET_QUEUE:
4087 case OFPACT_POP_QUEUE:
4088 case OFPACT_PUSH_MPLS:
4089 case OFPACT_POP_MPLS:
4090 case OFPACT_SET_MPLS_LABEL:
4091 case OFPACT_SET_MPLS_TC:
4092 case OFPACT_SET_MPLS_TTL:
4093 case OFPACT_MULTIPATH:
4096 case OFPACT_UNROLL_XLATE:
4097 case OFPACT_FIN_TIMEOUT:
4098 case OFPACT_CLEAR_ACTIONS:
4099 case OFPACT_WRITE_ACTIONS:
4102 case OFPACT_DEBUG_RECIRC:
4105 /* These need not be copied for restoration. */
4107 case OFPACT_CONJUNCTION:
4110 /* Copy the action over. */
4111 ofpbuf_put(&ctx->action_set, a, OFPACT_ALIGN(a->len));
4115 #define CHECK_MPLS_RECIRCULATION() \
4116 if (ctx->was_mpls) { \
4117 ctx_trigger_recirculation(ctx); \
4120 #define CHECK_MPLS_RECIRCULATION_IF(COND) \
4122 CHECK_MPLS_RECIRCULATION(); \
4126 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4127 struct xlate_ctx *ctx)
4129 struct flow_wildcards *wc = ctx->wc;
4130 struct flow *flow = &ctx->xin->flow;
4131 const struct ofpact *a;
4133 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
4134 tnl_arp_snoop(flow, wc, ctx->xbridge->name);
4136 /* dl_type already in the mask, not set below. */
4138 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4139 struct ofpact_controller *controller;
4140 const struct ofpact_metadata *metadata;
4141 const struct ofpact_set_field *set_field;
4142 const struct mf_field *mf;
4145 /* Check if need to store the remaining actions for later
4147 if (exit_recirculates(ctx)) {
4148 recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
4150 (uint8_t *)ofpacts)),
4158 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
4159 ofpact_get_OUTPUT(a)->max_len, true);
4163 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
4164 /* Group could not be found. */
4169 case OFPACT_CONTROLLER:
4170 controller = ofpact_get_CONTROLLER(a);
4171 execute_controller_action(ctx, controller->max_len,
4173 controller->controller_id);
4176 case OFPACT_ENQUEUE:
4177 memset(&wc->masks.skb_priority, 0xff,
4178 sizeof wc->masks.skb_priority);
4179 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
4182 case OFPACT_SET_VLAN_VID:
4183 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
4184 if (flow->vlan_tci & htons(VLAN_CFI) ||
4185 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
4186 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
4187 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
4192 case OFPACT_SET_VLAN_PCP:
4193 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
4194 if (flow->vlan_tci & htons(VLAN_CFI) ||
4195 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
4196 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
4197 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
4198 << VLAN_PCP_SHIFT) | VLAN_CFI);
4202 case OFPACT_STRIP_VLAN:
4203 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4204 flow->vlan_tci = htons(0);
4207 case OFPACT_PUSH_VLAN:
4208 /* XXX 802.1AD(QinQ) */
4209 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4210 flow->vlan_tci = htons(VLAN_CFI);
4213 case OFPACT_SET_ETH_SRC:
4214 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
4215 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
4218 case OFPACT_SET_ETH_DST:
4219 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
4220 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
4223 case OFPACT_SET_IPV4_SRC:
4224 CHECK_MPLS_RECIRCULATION();
4225 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4226 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
4227 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
4231 case OFPACT_SET_IPV4_DST:
4232 CHECK_MPLS_RECIRCULATION();
4233 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4234 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
4235 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
4239 case OFPACT_SET_IP_DSCP:
4240 CHECK_MPLS_RECIRCULATION();
4241 if (is_ip_any(flow)) {
4242 wc->masks.nw_tos |= IP_DSCP_MASK;
4243 flow->nw_tos &= ~IP_DSCP_MASK;
4244 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
4248 case OFPACT_SET_IP_ECN:
4249 CHECK_MPLS_RECIRCULATION();
4250 if (is_ip_any(flow)) {
4251 wc->masks.nw_tos |= IP_ECN_MASK;
4252 flow->nw_tos &= ~IP_ECN_MASK;
4253 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
4257 case OFPACT_SET_IP_TTL:
4258 CHECK_MPLS_RECIRCULATION();
4259 if (is_ip_any(flow)) {
4260 wc->masks.nw_ttl = 0xff;
4261 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
4265 case OFPACT_SET_L4_SRC_PORT:
4266 CHECK_MPLS_RECIRCULATION();
4267 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4268 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4269 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
4270 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
4274 case OFPACT_SET_L4_DST_PORT:
4275 CHECK_MPLS_RECIRCULATION();
4276 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4277 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4278 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
4279 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
4283 case OFPACT_RESUBMIT:
4284 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
4287 case OFPACT_SET_TUNNEL:
4288 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
4291 case OFPACT_SET_QUEUE:
4292 memset(&wc->masks.skb_priority, 0xff,
4293 sizeof wc->masks.skb_priority);
4294 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
4297 case OFPACT_POP_QUEUE:
4298 memset(&wc->masks.skb_priority, 0xff,
4299 sizeof wc->masks.skb_priority);
4300 flow->skb_priority = ctx->orig_skb_priority;
4303 case OFPACT_REG_MOVE:
4304 CHECK_MPLS_RECIRCULATION_IF(
4305 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
4306 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
4307 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
4310 case OFPACT_SET_FIELD:
4311 CHECK_MPLS_RECIRCULATION_IF(
4312 mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field));
4313 set_field = ofpact_get_SET_FIELD(a);
4314 mf = set_field->field;
4316 /* Set field action only ever overwrites packet's outermost
4317 * applicable header fields. Do nothing if no header exists. */
4318 if (mf->id == MFF_VLAN_VID) {
4319 wc->masks.vlan_tci |= htons(VLAN_CFI);
4320 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4323 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
4324 /* 'dl_type' is already unwildcarded. */
4325 && !eth_type_mpls(flow->dl_type)) {
4328 /* A flow may wildcard nw_frag. Do nothing if setting a trasport
4329 * header field on a packet that does not have them. */
4330 mf_mask_field_and_prereqs(mf, &wc->masks);
4331 if (mf_are_prereqs_ok(mf, flow)) {
4332 mf_set_flow_value_masked(mf, &set_field->value,
4333 &set_field->mask, flow);
4337 case OFPACT_STACK_PUSH:
4338 CHECK_MPLS_RECIRCULATION_IF(
4339 mf_is_l3_or_higher(ofpact_get_STACK_PUSH(a)->subfield.field));
4340 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
4344 case OFPACT_STACK_POP:
4345 CHECK_MPLS_RECIRCULATION_IF(
4346 mf_is_l3_or_higher(ofpact_get_STACK_POP(a)->subfield.field));
4347 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
4351 case OFPACT_PUSH_MPLS:
4352 /* Recirculate if it is an IP packet with a zero ttl. This may
4353 * indicate that the packet was previously MPLS and an MPLS pop
4354 * action converted it to IP. In this case recirculating should
4355 * reveal the IP TTL which is used as the basis for a new MPLS
4357 CHECK_MPLS_RECIRCULATION_IF(
4358 !flow_count_mpls_labels(flow, wc)
4359 && flow->nw_ttl == 0
4360 && is_ip_any(flow));
4361 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
4364 case OFPACT_POP_MPLS:
4365 CHECK_MPLS_RECIRCULATION();
4366 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
4369 case OFPACT_SET_MPLS_LABEL:
4370 CHECK_MPLS_RECIRCULATION();
4371 compose_set_mpls_label_action(
4372 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
4375 case OFPACT_SET_MPLS_TC:
4376 CHECK_MPLS_RECIRCULATION();
4377 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
4380 case OFPACT_SET_MPLS_TTL:
4381 CHECK_MPLS_RECIRCULATION();
4382 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
4385 case OFPACT_DEC_MPLS_TTL:
4386 CHECK_MPLS_RECIRCULATION();
4387 if (compose_dec_mpls_ttl_action(ctx)) {
4392 case OFPACT_DEC_TTL:
4393 CHECK_MPLS_RECIRCULATION();
4394 wc->masks.nw_ttl = 0xff;
4395 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
4401 /* Nothing to do. */
4404 case OFPACT_MULTIPATH:
4405 CHECK_MPLS_RECIRCULATION();
4406 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
4410 CHECK_MPLS_RECIRCULATION();
4411 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
4414 case OFPACT_OUTPUT_REG:
4415 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
4419 CHECK_MPLS_RECIRCULATION();
4420 xlate_learn_action(ctx, ofpact_get_LEARN(a));
4423 case OFPACT_CONJUNCTION: {
4424 /* A flow with a "conjunction" action represents part of a special
4425 * kind of "set membership match". Such a flow should not actually
4426 * get executed, but it could via, say, a "packet-out", even though
4427 * that wouldn't be useful. Log it to help debugging. */
4428 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4429 VLOG_INFO_RL(&rl, "executing no-op conjunction action");
4437 case OFPACT_UNROLL_XLATE: {
4438 struct ofpact_unroll_xlate *unroll = ofpact_get_UNROLL_XLATE(a);
4440 /* Restore translation context data that was stored earlier. */
4441 ctx->table_id = unroll->rule_table_id;
4442 ctx->rule_cookie = unroll->rule_cookie;
4445 case OFPACT_FIN_TIMEOUT:
4446 CHECK_MPLS_RECIRCULATION();
4447 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4448 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
4451 case OFPACT_CLEAR_ACTIONS:
4452 ofpbuf_clear(&ctx->action_set);
4453 ctx->xin->flow.actset_output = OFPP_UNSET;
4454 ctx->action_set_has_group = false;
4457 case OFPACT_WRITE_ACTIONS:
4458 xlate_write_actions(ctx, a);
4461 case OFPACT_WRITE_METADATA:
4462 metadata = ofpact_get_WRITE_METADATA(a);
4463 flow->metadata &= ~metadata->mask;
4464 flow->metadata |= metadata->metadata & metadata->mask;
4468 /* Not implemented yet. */
4471 case OFPACT_GOTO_TABLE: {
4472 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
4474 /* Allow ctx->table_id == TBL_INTERNAL, which will be greater
4475 * than ogt->table_id. This is to allow goto_table actions that
4476 * triggered recirculation: ctx->table_id will be TBL_INTERNAL
4477 * after recirculation. */
4478 ovs_assert(ctx->table_id == TBL_INTERNAL
4479 || ctx->table_id < ogt->table_id);
4480 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
4481 ogt->table_id, true, true);
4486 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
4489 case OFPACT_DEBUG_RECIRC:
4490 ctx_trigger_recirculation(ctx);
4495 /* Check if need to store this and the remaining actions for later
4497 if (ctx->exit && ctx_first_recirculation_action(ctx)) {
4498 recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
4500 (uint8_t *)ofpacts)),
4508 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
4509 const struct flow *flow, ofp_port_t in_port,
4510 struct rule_dpif *rule, uint16_t tcp_flags,
4511 const struct dp_packet *packet, struct flow_wildcards *wc,
4512 struct ofpbuf *odp_actions)
4514 xin->ofproto = ofproto;
4516 xin->flow.in_port.ofp_port = in_port;
4517 xin->flow.actset_output = OFPP_UNSET;
4518 xin->packet = packet;
4519 xin->may_learn = packet != NULL;
4522 xin->ofpacts = NULL;
4523 xin->ofpacts_len = 0;
4524 xin->tcp_flags = tcp_flags;
4525 xin->resubmit_hook = NULL;
4526 xin->report_hook = NULL;
4527 xin->resubmit_stats = NULL;
4529 xin->odp_actions = odp_actions;
4531 /* Do recirc lookup. */
4532 xin->recirc = flow->recirc_id
4533 ? recirc_id_node_find(flow->recirc_id)
4538 xlate_out_uninit(struct xlate_out *xout)
4541 xlate_out_free_recircs(xout);
4545 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
4546 * into datapath actions, using 'ctx', and discards the datapath actions. */
4548 xlate_actions_for_side_effects(struct xlate_in *xin)
4550 struct xlate_out xout;
4552 xlate_actions(xin, &xout);
4553 xlate_out_uninit(&xout);
4556 static struct skb_priority_to_dscp *
4557 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
4559 struct skb_priority_to_dscp *pdscp;
4562 hash = hash_int(skb_priority, 0);
4563 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
4564 if (pdscp->skb_priority == skb_priority) {
4572 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
4575 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
4576 *dscp = pdscp ? pdscp->dscp : 0;
4577 return pdscp != NULL;
4581 count_skb_priorities(const struct xport *xport)
4583 return hmap_count(&xport->skb_priorities);
4587 clear_skb_priorities(struct xport *xport)
4589 struct skb_priority_to_dscp *pdscp, *next;
4591 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
4592 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
4598 actions_output_to_local_port(const struct xlate_ctx *ctx)
4600 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
4601 const struct nlattr *a;
4604 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->odp_actions->data,
4605 ctx->odp_actions->size) {
4606 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
4607 && nl_attr_get_odp_port(a) == local_odp_port) {
4614 #if defined(__linux__)
4615 /* Returns the maximum number of packets that the Linux kernel is willing to
4616 * queue up internally to certain kinds of software-implemented ports, or the
4617 * default (and rarely modified) value if it cannot be determined. */
4619 netdev_max_backlog(void)
4621 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
4622 static int max_backlog = 1000; /* The normal default value. */
4624 if (ovsthread_once_start(&once)) {
4625 static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
4629 stream = fopen(filename, "r");
4631 VLOG_WARN("%s: open failed (%s)", filename, ovs_strerror(errno));
4633 if (fscanf(stream, "%d", &n) != 1) {
4634 VLOG_WARN("%s: read error", filename);
4635 } else if (n <= 100) {
4636 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
4642 ovsthread_once_done(&once);
4644 VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
4650 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
4653 count_output_actions(const struct ofpbuf *odp_actions)
4655 const struct nlattr *a;
4659 NL_ATTR_FOR_EACH_UNSAFE (a, left, odp_actions->data, odp_actions->size) {
4660 if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
4666 #endif /* defined(__linux__) */
4668 /* Returns true if 'odp_actions' contains more output actions than the datapath
4669 * can reliably handle in one go. On Linux, this is the value of the
4670 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
4671 * packets that the kernel is willing to queue up for processing while the
4672 * datapath is processing a set of actions. */
4674 too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
4677 return (odp_actions->size / NL_A_U32_SIZE > netdev_max_backlog()
4678 && count_output_actions(odp_actions) > netdev_max_backlog());
4680 /* OSes other than Linux might have similar limits, but we don't know how
4681 * to determine them.*/
4687 xlate_wc_init(struct xlate_ctx *ctx)
4689 flow_wildcards_init_catchall(ctx->wc);
4691 /* Some fields we consider to always be examined. */
4692 memset(&ctx->wc->masks.in_port, 0xff, sizeof ctx->wc->masks.in_port);
4693 memset(&ctx->wc->masks.dl_type, 0xff, sizeof ctx->wc->masks.dl_type);
4694 if (is_ip_any(&ctx->xin->flow)) {
4695 ctx->wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
4698 if (ctx->xbridge->support.odp.recirc) {
4699 /* Always exactly match recirc_id when datapath supports
4701 ctx->wc->masks.recirc_id = UINT32_MAX;
4704 if (ctx->xbridge->netflow) {
4705 netflow_mask_wc(&ctx->xin->flow, ctx->wc);
4708 tnl_wc_init(&ctx->xin->flow, ctx->wc);
4712 xlate_wc_finish(struct xlate_ctx *ctx)
4714 /* Clear the metadata and register wildcard masks, because we won't
4715 * use non-header fields as part of the cache. */
4716 flow_wildcards_clear_non_packet_fields(ctx->wc);
4718 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
4719 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
4720 * represent these fields. The datapath interface, on the other hand,
4721 * represents them with just 8 bits each. This means that if the high
4722 * 8 bits of the masks for these fields somehow become set, then they
4723 * will get chopped off by a round trip through the datapath, and
4724 * revalidation will spot that as an inconsistency and delete the flow.
4725 * Avoid the problem here by making sure that only the low 8 bits of
4726 * either field can be unwildcarded for ICMP.
4728 if (is_icmpv4(&ctx->xin->flow) || is_icmpv6(&ctx->xin->flow)) {
4729 ctx->wc->masks.tp_src &= htons(UINT8_MAX);
4730 ctx->wc->masks.tp_dst &= htons(UINT8_MAX);
4732 /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
4733 if (ctx->wc->masks.vlan_tci) {
4734 ctx->wc->masks.vlan_tci |= htons(VLAN_CFI);
4738 /* Translates the flow, actions, or rule in 'xin' into datapath actions in
4740 * The caller must take responsibility for eventually freeing 'xout', with
4741 * xlate_out_uninit(). */
4743 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
4745 *xout = (struct xlate_out) {
4751 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4752 struct xbridge *xbridge = xbridge_lookup(xcfg, xin->ofproto);
4757 struct flow *flow = &xin->flow;
4759 union mf_subvalue stack_stub[1024 / sizeof(union mf_subvalue)];
4760 uint64_t action_set_stub[1024 / 8];
4761 struct flow_wildcards scratch_wc;
4762 uint64_t actions_stub[256 / 8];
4763 struct ofpbuf scratch_actions = OFPBUF_STUB_INITIALIZER(actions_stub);
4764 struct xlate_ctx ctx = {
4768 .orig_tunnel_ip_dst = flow->tunnel.ip_dst,
4770 .stack = OFPBUF_STUB_INITIALIZER(stack_stub),
4772 .wc = xin->wc ? xin->wc : &scratch_wc,
4773 .odp_actions = xin->odp_actions ? xin->odp_actions : &scratch_actions,
4778 .in_action_set = false,
4781 .rule_cookie = OVS_BE64_MAX,
4782 .orig_skb_priority = flow->skb_priority,
4783 .sflow_n_outputs = 0,
4784 .sflow_odp_port = 0,
4785 .nf_output_iface = NF_OUT_DROP,
4789 .recirc_action_offset = -1,
4790 .last_unroll_offset = -1,
4794 .action_set_has_group = false,
4795 .action_set = OFPBUF_STUB_INITIALIZER(action_set_stub),
4798 /* 'base_flow' reflects the packet as it came in, but we need it to reflect
4799 * the packet as the datapath will treat it for output actions:
4801 * - Our datapath doesn't retain tunneling information without us
4802 * re-setting it, so clear the tunnel data.
4804 * - For VLAN splinters, a higher layer may pretend that the packet
4805 * came in on 'flow->in_port.ofp_port' with 'flow->vlan_tci'
4806 * attached, because that's how we want to treat it from an OpenFlow
4807 * perspective. But from the datapath's perspective it actually came
4808 * in on a VLAN device without any VLAN attached. So here we put the
4809 * datapath's view of the VLAN information in 'base_flow' to ensure
4810 * correct treatment.
4812 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
4813 if (flow->in_port.ofp_port
4814 != vsp_realdev_to_vlandev(xbridge->ofproto,
4815 flow->in_port.ofp_port,
4817 ctx.base_flow.vlan_tci = 0;
4820 ofpbuf_reserve(ctx.odp_actions, NL_A_U32_SIZE);
4822 xlate_wc_init(&ctx);
4825 COVERAGE_INC(xlate_actions);
4828 const struct recirc_state *state = &xin->recirc->state;
4830 xlate_report(&ctx, "Restoring state post-recirculation:");
4832 if (xin->ofpacts_len > 0 || ctx.rule) {
4833 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4834 const char *conflict = xin->ofpacts_len ? "actions" : "rule";
4836 VLOG_WARN_RL(&rl, "Recirculation conflict (%s)!", conflict);
4837 xlate_report(&ctx, "- Recirculation conflict (%s)!", conflict);
4841 /* Set the bridge for post-recirculation processing if needed. */
4842 if (ctx.xbridge->ofproto != state->ofproto) {
4843 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4844 const struct xbridge *new_bridge
4845 = xbridge_lookup(xcfg, state->ofproto);
4847 if (OVS_UNLIKELY(!new_bridge)) {
4848 /* Drop the packet if the bridge cannot be found. */
4849 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4850 VLOG_WARN_RL(&rl, "Recirculation bridge no longer exists.");
4851 xlate_report(&ctx, "- Recirculation bridge no longer exists.");
4854 ctx.xbridge = new_bridge;
4857 /* Set the post-recirculation table id. Note: A table lookup is done
4858 * only if there are no post-recirculation actions. */
4859 ctx.table_id = state->table_id;
4860 xlate_report(&ctx, "- Resuming from table %"PRIu8, ctx.table_id);
4862 /* Restore pipeline metadata. May change flow's in_port and other
4863 * metadata to the values that existed when recirculation was
4865 recirc_metadata_to_flow(&state->metadata, flow);
4867 /* Restore stack, if any. */
4869 ofpbuf_put(&ctx.stack, state->stack->data, state->stack->size);
4872 /* Restore mirror state. */
4873 ctx.mirrors = state->mirrors;
4875 /* Restore action set, if any. */
4876 if (state->action_set_len) {
4877 const struct ofpact *a;
4879 xlate_report_actions(&ctx, "- Restoring action set",
4880 state->ofpacts, state->action_set_len);
4882 ofpbuf_put(&ctx.action_set, state->ofpacts, state->action_set_len);
4884 OFPACT_FOR_EACH(a, state->ofpacts, state->action_set_len) {
4885 if (a->type == OFPACT_GROUP) {
4886 ctx.action_set_has_group = true;
4892 /* Restore recirculation actions. If there are no actions, processing
4893 * will start with a lookup in the table set above. */
4894 if (state->ofpacts_len > state->action_set_len) {
4895 xin->ofpacts_len = state->ofpacts_len - state->action_set_len;
4896 xin->ofpacts = state->ofpacts +
4897 state->action_set_len / sizeof *state->ofpacts;
4899 xlate_report_actions(&ctx, "- Restoring actions",
4900 xin->ofpacts, xin->ofpacts_len);
4902 } else if (OVS_UNLIKELY(flow->recirc_id)) {
4903 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4905 VLOG_WARN_RL(&rl, "Recirculation context not found for ID %"PRIx32,
4909 /* The bridge is now known so obtain its table version. */
4910 ctx.tables_version = ofproto_dpif_get_tables_version(ctx.xbridge->ofproto);
4912 if (!xin->ofpacts && !ctx.rule) {
4913 ctx.rule = rule_dpif_lookup_from_table(
4914 ctx.xbridge->ofproto, ctx.tables_version, flow, xin->wc,
4915 ctx.xin->xcache != NULL, ctx.xin->resubmit_stats, &ctx.table_id,
4916 flow->in_port.ofp_port, true, true);
4917 if (ctx.xin->resubmit_stats) {
4918 rule_dpif_credit_stats(ctx.rule, ctx.xin->resubmit_stats);
4920 if (ctx.xin->xcache) {
4921 struct xc_entry *entry;
4923 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
4924 entry->u.rule = ctx.rule;
4927 if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
4928 ctx.xin->resubmit_hook(ctx.xin, ctx.rule, 0);
4931 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
4933 /* Get the proximate input port of the packet. (If xin->recirc,
4934 * flow->in_port is the ultimate input port of the packet.) */
4935 struct xport *in_port = get_ofp_port(xbridge,
4936 ctx.base_flow.in_port.ofp_port);
4938 /* Tunnel stats only for non-recirculated packets. */
4939 if (!xin->recirc && in_port && in_port->is_tunnel) {
4940 if (ctx.xin->resubmit_stats) {
4941 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
4943 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
4946 if (ctx.xin->xcache) {
4947 struct xc_entry *entry;
4949 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
4950 entry->u.dev.rx = netdev_ref(in_port->netdev);
4951 entry->u.dev.bfd = bfd_ref(in_port->bfd);
4955 if (!xin->recirc && process_special(&ctx, in_port)) {
4956 /* process_special() did all the processing for this packet.
4958 * We do not perform special processing on recirculated packets, as
4959 * recirculated packets are not really received by the bridge.*/
4960 } else if (in_port && in_port->xbundle
4961 && xbundle_mirror_out(xbridge, in_port->xbundle)) {
4962 if (ctx.xin->packet != NULL) {
4963 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4964 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
4965 "%s, which is reserved exclusively for mirroring",
4966 ctx.xbridge->name, in_port->xbundle->name);
4969 /* Sampling is done only for packets really received by the bridge. */
4970 unsigned int user_cookie_offset = 0;
4972 user_cookie_offset = compose_sflow_action(&ctx);
4973 compose_ipfix_action(&ctx, ODPP_NONE);
4975 size_t sample_actions_len = ctx.odp_actions->size;
4977 if (tnl_process_ecn(flow)
4978 && (!in_port || may_receive(in_port, &ctx))) {
4979 const struct ofpact *ofpacts;
4983 ofpacts = xin->ofpacts;
4984 ofpacts_len = xin->ofpacts_len;
4985 } else if (ctx.rule) {
4986 const struct rule_actions *actions
4987 = rule_dpif_get_actions(ctx.rule);
4988 ofpacts = actions->ofpacts;
4989 ofpacts_len = actions->ofpacts_len;
4990 ctx.rule_cookie = rule_dpif_get_flow_cookie(ctx.rule);
4995 mirror_ingress_packet(&ctx);
4996 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
4998 /* We've let OFPP_NORMAL and the learning action look at the
4999 * packet, so drop it now if forwarding is disabled. */
5000 if (in_port && (!xport_stp_forward_state(in_port) ||
5001 !xport_rstp_forward_state(in_port))) {
5002 /* Drop all actions added by do_xlate_actions() above. */
5003 ctx.odp_actions->size = sample_actions_len;
5005 /* Undo changes that may have been done for recirculation. */
5006 if (exit_recirculates(&ctx)) {
5007 ctx.action_set.size = ctx.recirc_action_offset;
5008 ctx.recirc_action_offset = -1;
5009 ctx.last_unroll_offset = -1;
5011 } else if (ctx.action_set.size) {
5012 /* Translate action set only if not dropping the packet and
5013 * not recirculating. */
5014 if (!exit_recirculates(&ctx)) {
5015 xlate_action_set(&ctx);
5018 /* Check if need to recirculate. */
5019 if (exit_recirculates(&ctx)) {
5020 compose_recirculate_action(&ctx);
5024 /* Output only fully processed packets. */
5025 if (!exit_recirculates(&ctx)
5026 && xbridge->has_in_band
5027 && in_band_must_output_to_local_port(flow)
5028 && !actions_output_to_local_port(&ctx)) {
5029 compose_output_action(&ctx, OFPP_LOCAL, NULL);
5032 if (user_cookie_offset) {
5033 fix_sflow_action(&ctx, user_cookie_offset);
5037 if (nl_attr_oversized(ctx.odp_actions->size)) {
5038 /* These datapath actions are too big for a Netlink attribute, so we
5039 * can't hand them to the kernel directly. dpif_execute() can execute
5040 * them one by one with help, so just mark the result as SLOW_ACTION to
5041 * prevent the flow from being installed. */
5042 COVERAGE_INC(xlate_actions_oversize);
5043 ctx.xout->slow |= SLOW_ACTION;
5044 } else if (too_many_output_actions(ctx.odp_actions)) {
5045 COVERAGE_INC(xlate_actions_too_many_output);
5046 ctx.xout->slow |= SLOW_ACTION;
5049 /* Do netflow only for packets really received by the bridge and not sent
5050 * to the controller. We consider packets sent to the controller to be
5051 * part of the control plane rather than the data plane. */
5052 if (!xin->recirc && xbridge->netflow && !(xout->slow & SLOW_CONTROLLER)) {
5053 if (ctx.xin->resubmit_stats) {
5054 netflow_flow_update(xbridge->netflow, flow,
5055 ctx.nf_output_iface,
5056 ctx.xin->resubmit_stats);
5058 if (ctx.xin->xcache) {
5059 struct xc_entry *entry;
5061 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
5062 entry->u.nf.netflow = netflow_ref(xbridge->netflow);
5063 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
5064 entry->u.nf.iface = ctx.nf_output_iface;
5069 xlate_wc_finish(&ctx);
5073 ofpbuf_uninit(&ctx.stack);
5074 ofpbuf_uninit(&ctx.action_set);
5075 ofpbuf_uninit(&scratch_actions);
5078 /* Sends 'packet' out 'ofport'.
5079 * May modify 'packet'.
5080 * Returns 0 if successful, otherwise a positive errno value. */
5082 xlate_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
5084 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5085 struct xport *xport;
5086 struct ofpact_output output;
5089 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
5090 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
5091 flow_extract(packet, &flow);
5092 flow.in_port.ofp_port = OFPP_NONE;
5094 xport = xport_lookup(xcfg, ofport);
5098 output.port = xport->ofp_port;
5101 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
5102 &output.ofpact, sizeof output,
5106 struct xlate_cache *
5107 xlate_cache_new(void)
5109 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
5111 ofpbuf_init(&xcache->entries, 512);
5115 static struct xc_entry *
5116 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
5118 struct xc_entry *entry;
5120 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
5127 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
5129 if (entry->u.dev.tx) {
5130 netdev_vport_inc_tx(entry->u.dev.tx, stats);
5132 if (entry->u.dev.rx) {
5133 netdev_vport_inc_rx(entry->u.dev.rx, stats);
5135 if (entry->u.dev.bfd) {
5136 bfd_account_rx(entry->u.dev.bfd, stats);
5141 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
5143 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5144 struct xbridge *xbridge;
5145 struct xbundle *xbundle;
5146 struct flow_wildcards wc;
5148 xbridge = xbridge_lookup(xcfg, ofproto);
5153 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
5159 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
5162 /* Push stats and perform side effects of flow translation. */
5164 xlate_push_stats(struct xlate_cache *xcache,
5165 const struct dpif_flow_stats *stats)
5167 struct xc_entry *entry;
5168 struct ofpbuf entries = xcache->entries;
5169 uint8_t dmac[ETH_ADDR_LEN];
5171 if (!stats->n_packets) {
5175 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5176 switch (entry->type) {
5178 rule_dpif_credit_stats(entry->u.rule, stats);
5181 bond_account(entry->u.bond.bond, entry->u.bond.flow,
5182 entry->u.bond.vid, stats->n_bytes);
5185 xlate_cache_netdev(entry, stats);
5188 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
5189 entry->u.nf.iface, stats);
5192 mirror_update_stats(entry->u.mirror.mbridge,
5193 entry->u.mirror.mirrors,
5194 stats->n_packets, stats->n_bytes);
5197 ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
5200 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
5201 entry->u.normal.vlan);
5203 case XC_FIN_TIMEOUT:
5204 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
5205 entry->u.fin.idle, entry->u.fin.hard);
5208 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
5212 /* Lookup arp to avoid arp timeout. */
5213 tnl_arp_lookup(entry->u.tnl_arp_cache.br_name, entry->u.tnl_arp_cache.d_ip, dmac);
5222 xlate_dev_unref(struct xc_entry *entry)
5224 if (entry->u.dev.tx) {
5225 netdev_close(entry->u.dev.tx);
5227 if (entry->u.dev.rx) {
5228 netdev_close(entry->u.dev.rx);
5230 if (entry->u.dev.bfd) {
5231 bfd_unref(entry->u.dev.bfd);
5236 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
5238 netflow_flow_clear(netflow, flow);
5239 netflow_unref(netflow);
5244 xlate_cache_clear(struct xlate_cache *xcache)
5246 struct xc_entry *entry;
5247 struct ofpbuf entries;
5253 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5254 switch (entry->type) {
5256 rule_dpif_unref(entry->u.rule);
5259 free(entry->u.bond.flow);
5260 bond_unref(entry->u.bond.bond);
5263 xlate_dev_unref(entry);
5266 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
5269 mbridge_unref(entry->u.mirror.mbridge);
5272 free(entry->u.learn.fm);
5273 ofpbuf_delete(entry->u.learn.ofpacts);
5276 free(entry->u.normal.flow);
5278 case XC_FIN_TIMEOUT:
5279 /* 'u.fin.rule' is always already held as a XC_RULE, which
5280 * has already released it's reference above. */
5283 group_dpif_unref(entry->u.group.group);
5292 ofpbuf_clear(&xcache->entries);
5296 xlate_cache_delete(struct xlate_cache *xcache)
5298 xlate_cache_clear(xcache);
5299 ofpbuf_uninit(&xcache->entries);