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 /* Resubmit statistics, via xlate_table_action(). */
191 int recurse; /* Current resubmit nesting depth. */
192 int resubmits; /* Total number of resubmits. */
193 bool in_group; /* Currently translating ofgroup, if true. */
194 bool in_action_set; /* Currently translating action_set, if true. */
196 uint8_t table_id; /* OpenFlow table ID where flow was found. */
197 ovs_be64 rule_cookie; /* Cookie of the rule being translated. */
198 uint32_t orig_skb_priority; /* Priority when packet arrived. */
199 uint32_t sflow_n_outputs; /* Number of output ports. */
200 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
201 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
202 bool exit; /* No further actions should be processed. */
204 /* These are used for non-bond recirculation. The recirculation IDs are
205 * stored in xout and must be associated with a datapath flow (ukey),
206 * otherwise they will be freed when the xout is uninitialized.
209 * Steps in Recirculation Translation
210 * ==================================
212 * At some point during translation, the code recognizes the need for
213 * recirculation. For example, recirculation is necessary when, after
214 * popping the last MPLS label, an action or a match tries to examine or
215 * modify a field that has been newly revealed following the MPLS label.
217 * The simplest part of the work to be done is to commit existing changes to
218 * the packet, which produces datapath actions corresponding to the changes,
219 * and after this, add an OVS_ACTION_ATTR_RECIRC datapath action.
221 * The main problem here is preserving state. When the datapath executes
222 * OVS_ACTION_ATTR_RECIRC, it will upcall to userspace to get a translation
223 * for the post-recirculation actions. At this point userspace has to
224 * resume the translation where it left off, which means that it has to
225 * execute the following:
227 * - The action that prompted recirculation, and any actions following
228 * it within the same flow.
230 * - If the action that prompted recirculation was invoked within a
231 * NXAST_RESUBMIT, then any actions following the resubmit. These
232 * "resubmit"s can be nested, so this has to go all the way up the
235 * - The OpenFlow 1.1+ action set.
237 * State that actions and flow table lookups can depend on, such as the
238 * following, must also be preserved:
240 * - Metadata fields (input port, registers, OF1.1+ metadata, ...).
242 * - Action set, stack
244 * - The table ID and cookie of the flow being translated at each level
245 * of the control stack (since OFPAT_CONTROLLER actions send these to
248 * Translation allows for the control of this state preservation via these
249 * members. When a need for recirculation is identified, the translation
252 * 1. Sets 'recirc_action_offset' to the current size of 'action_set'. The
253 * action set is part of what needs to be preserved, so this allows the
254 * action set and the additional state to share the 'action_set' buffer.
255 * Later steps can tell that setup for recirculation is in progress from
256 * the nonnegative value of 'recirc_action_offset'.
258 * 2. Sets 'exit' to true to tell later steps that we're exiting from the
259 * translation process.
261 * 3. Adds an OFPACT_UNROLL_XLATE action to 'action_set'. This action
262 * holds the current table ID and cookie so that they can be restored
263 * during a post-recirculation upcall translation.
265 * 4. Adds the action that prompted recirculation and any actions following
266 * it within the same flow to 'action_set', so that they can be executed
267 * during a post-recirculation upcall translation.
271 * 6. The action that prompted recirculation might be nested in a stack of
272 * nested "resubmit"s that have actions remaining. Each of these notices
273 * that we're exiting (from 'exit') and that recirculation setup is in
274 * progress (from 'recirc_action_offset') and responds by adding more
275 * OFPACT_UNROLL_XLATE actions to 'action_set', as necessary, and any
276 * actions that were yet unprocessed.
278 * The caller stores all the state produced by this process associated with
279 * the recirculation ID. For post-recirculation upcall translation, the
280 * caller passes it back in for the new translation to execute. The
281 * process yielded a set of ofpacts that can be translated directly, so it
282 * is not much of a special case at that point.
284 int recirc_action_offset; /* Offset in 'action_set' to actions to be
285 * executed after recirculation, or -1. */
286 int last_unroll_offset; /* Offset in 'action_set' to the latest unroll
289 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
290 * This is a trigger for recirculation in cases where translating an action
291 * or looking up a flow requires access to the fields of the packet after
292 * the MPLS label stack that was originally present. */
295 /* OpenFlow 1.1+ action set.
297 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
298 * When translation is otherwise complete, ofpacts_execute_action_set()
299 * converts it to a set of "struct ofpact"s that can be translated into
300 * datapath actions. */
301 bool action_set_has_group; /* Action set contains OFPACT_GROUP? */
302 struct ofpbuf action_set; /* Action set. */
305 static void xlate_action_set(struct xlate_ctx *ctx);
308 ctx_trigger_recirculation(struct xlate_ctx *ctx)
311 ctx->recirc_action_offset = ctx->action_set.size;
315 ctx_first_recirculation_action(const struct xlate_ctx *ctx)
317 return ctx->recirc_action_offset == ctx->action_set.size;
321 exit_recirculates(const struct xlate_ctx *ctx)
323 /* When recirculating the 'recirc_action_offset' has a non-negative value.
325 return ctx->recirc_action_offset >= 0;
328 static void compose_recirculate_action(struct xlate_ctx *ctx);
330 /* A controller may use OFPP_NONE as the ingress port to indicate that
331 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
332 * when an input bundle is needed for validation (e.g., mirroring or
333 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
334 * any 'port' structs, so care must be taken when dealing with it. */
335 static struct xbundle ofpp_none_bundle = {
337 .vlan_mode = PORT_VLAN_TRUNK
340 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
341 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
342 * traffic egressing the 'ofport' with that priority should be marked with. */
343 struct skb_priority_to_dscp {
344 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
345 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
347 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
363 /* xlate_cache entries hold enough information to perform the side effects of
364 * xlate_actions() for a rule, without needing to perform rule translation
365 * from scratch. The primary usage of these is to submit statistics to objects
366 * that a flow relates to, although they may be used for other effects as well
367 * (for instance, refreshing hard timeouts for learned flows). */
371 struct rule_dpif *rule;
378 struct netflow *netflow;
383 struct mbridge *mbridge;
384 mirror_mask_t mirrors;
392 struct ofproto_dpif *ofproto;
393 struct ofputil_flow_mod *fm;
394 struct ofpbuf *ofpacts;
397 struct ofproto_dpif *ofproto;
402 struct rule_dpif *rule;
407 struct group_dpif *group;
408 struct ofputil_bucket *bucket;
411 char br_name[IFNAMSIZ];
417 #define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
418 entries = xcache->entries; \
419 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
421 entry = ofpbuf_try_pull(&entries, sizeof *entry))
424 struct ofpbuf entries;
427 /* Xlate config contains hash maps of all bridges, bundles and ports.
428 * Xcfgp contains the pointer to the current xlate configuration.
429 * When the main thread needs to change the configuration, it copies xcfgp to
430 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
431 * does not block handler and revalidator threads. */
433 struct hmap xbridges;
434 struct hmap xbundles;
437 static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
438 static struct xlate_cfg *new_xcfg = NULL;
440 static bool may_receive(const struct xport *, struct xlate_ctx *);
441 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
443 static void xlate_normal(struct xlate_ctx *);
444 static inline void xlate_report(struct xlate_ctx *, const char *, ...)
445 OVS_PRINTF_FORMAT(2, 3);
446 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
447 uint8_t table_id, bool may_packet_in,
448 bool honor_table_miss);
449 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
450 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
451 static void output_normal(struct xlate_ctx *, const struct xbundle *,
454 /* Optional bond recirculation parameter to compose_output_action(). */
455 struct xlate_bond_recirc {
456 uint32_t recirc_id; /* !0 Use recirculation instead of output. */
457 uint8_t hash_alg; /* !0 Compute hash for recirc before. */
458 uint32_t hash_basis; /* Compute hash for recirc before. */
461 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port,
462 const struct xlate_bond_recirc *xr);
464 static struct xbridge *xbridge_lookup(struct xlate_cfg *,
465 const struct ofproto_dpif *);
466 static struct xbundle *xbundle_lookup(struct xlate_cfg *,
467 const struct ofbundle *);
468 static struct xport *xport_lookup(struct xlate_cfg *,
469 const struct ofport_dpif *);
470 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
471 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
472 uint32_t skb_priority);
473 static void clear_skb_priorities(struct xport *);
474 static size_t count_skb_priorities(const struct xport *);
475 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
478 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
480 static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
481 static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
482 static void xlate_xport_init(struct xlate_cfg *, struct xport *);
483 static void xlate_xbridge_set(struct xbridge *, struct dpif *,
484 const struct mac_learning *, struct stp *,
485 struct rstp *, const struct mcast_snooping *,
486 const struct mbridge *,
487 const struct dpif_sflow *,
488 const struct dpif_ipfix *,
489 const struct netflow *,
490 bool forward_bpdu, bool has_in_band,
491 const struct dpif_backer_support *);
492 static void xlate_xbundle_set(struct xbundle *xbundle,
493 enum port_vlan_mode vlan_mode, int vlan,
494 unsigned long *trunks, bool use_priority_tags,
495 const struct bond *bond, const struct lacp *lacp,
497 static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
498 const struct netdev *netdev, const struct cfm *cfm,
499 const struct bfd *bfd, const struct lldp *lldp,
500 int stp_port_no, const struct rstp_port *rstp_port,
501 enum ofputil_port_config config,
502 enum ofputil_port_state state, bool is_tunnel,
504 static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
505 static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
506 static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
507 static void xlate_xbridge_copy(struct xbridge *);
508 static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
509 static void xlate_xport_copy(struct xbridge *, struct xbundle *,
511 static void xlate_xcfg_free(struct xlate_cfg *);
514 xlate_report(struct xlate_ctx *ctx, const char *format, ...)
516 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
519 va_start(args, format);
520 ctx->xin->report_hook(ctx->xin, ctx->recurse, format, args);
526 xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
528 list_init(&xbridge->xbundles);
529 hmap_init(&xbridge->xports);
530 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
531 hash_pointer(xbridge->ofproto, 0));
535 xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
537 list_init(&xbundle->xports);
538 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
539 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
540 hash_pointer(xbundle->ofbundle, 0));
544 xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
546 hmap_init(&xport->skb_priorities);
547 hmap_insert(&xcfg->xports, &xport->hmap_node,
548 hash_pointer(xport->ofport, 0));
549 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
550 hash_ofp_port(xport->ofp_port));
554 xlate_xbridge_set(struct xbridge *xbridge,
556 const struct mac_learning *ml, struct stp *stp,
557 struct rstp *rstp, const struct mcast_snooping *ms,
558 const struct mbridge *mbridge,
559 const struct dpif_sflow *sflow,
560 const struct dpif_ipfix *ipfix,
561 const struct netflow *netflow,
562 bool forward_bpdu, bool has_in_band,
563 const struct dpif_backer_support *support)
565 if (xbridge->ml != ml) {
566 mac_learning_unref(xbridge->ml);
567 xbridge->ml = mac_learning_ref(ml);
570 if (xbridge->ms != ms) {
571 mcast_snooping_unref(xbridge->ms);
572 xbridge->ms = mcast_snooping_ref(ms);
575 if (xbridge->mbridge != mbridge) {
576 mbridge_unref(xbridge->mbridge);
577 xbridge->mbridge = mbridge_ref(mbridge);
580 if (xbridge->sflow != sflow) {
581 dpif_sflow_unref(xbridge->sflow);
582 xbridge->sflow = dpif_sflow_ref(sflow);
585 if (xbridge->ipfix != ipfix) {
586 dpif_ipfix_unref(xbridge->ipfix);
587 xbridge->ipfix = dpif_ipfix_ref(ipfix);
590 if (xbridge->stp != stp) {
591 stp_unref(xbridge->stp);
592 xbridge->stp = stp_ref(stp);
595 if (xbridge->rstp != rstp) {
596 rstp_unref(xbridge->rstp);
597 xbridge->rstp = rstp_ref(rstp);
600 if (xbridge->netflow != netflow) {
601 netflow_unref(xbridge->netflow);
602 xbridge->netflow = netflow_ref(netflow);
605 xbridge->dpif = dpif;
606 xbridge->forward_bpdu = forward_bpdu;
607 xbridge->has_in_band = has_in_band;
608 xbridge->support = *support;
612 xlate_xbundle_set(struct xbundle *xbundle,
613 enum port_vlan_mode vlan_mode, int vlan,
614 unsigned long *trunks, bool use_priority_tags,
615 const struct bond *bond, const struct lacp *lacp,
618 ovs_assert(xbundle->xbridge);
620 xbundle->vlan_mode = vlan_mode;
621 xbundle->vlan = vlan;
622 xbundle->trunks = trunks;
623 xbundle->use_priority_tags = use_priority_tags;
624 xbundle->floodable = floodable;
626 if (xbundle->bond != bond) {
627 bond_unref(xbundle->bond);
628 xbundle->bond = bond_ref(bond);
631 if (xbundle->lacp != lacp) {
632 lacp_unref(xbundle->lacp);
633 xbundle->lacp = lacp_ref(lacp);
638 xlate_xport_set(struct xport *xport, odp_port_t odp_port,
639 const struct netdev *netdev, const struct cfm *cfm,
640 const struct bfd *bfd, const struct lldp *lldp, int stp_port_no,
641 const struct rstp_port* rstp_port,
642 enum ofputil_port_config config, enum ofputil_port_state state,
643 bool is_tunnel, bool may_enable)
645 xport->config = config;
646 xport->state = state;
647 xport->stp_port_no = stp_port_no;
648 xport->is_tunnel = is_tunnel;
649 xport->may_enable = may_enable;
650 xport->odp_port = odp_port;
652 if (xport->rstp_port != rstp_port) {
653 rstp_port_unref(xport->rstp_port);
654 xport->rstp_port = rstp_port_ref(rstp_port);
657 if (xport->cfm != cfm) {
658 cfm_unref(xport->cfm);
659 xport->cfm = cfm_ref(cfm);
662 if (xport->bfd != bfd) {
663 bfd_unref(xport->bfd);
664 xport->bfd = bfd_ref(bfd);
667 if (xport->lldp != lldp) {
668 lldp_unref(xport->lldp);
669 xport->lldp = lldp_ref(lldp);
672 if (xport->netdev != netdev) {
673 netdev_close(xport->netdev);
674 xport->netdev = netdev_ref(netdev);
679 xlate_xbridge_copy(struct xbridge *xbridge)
681 struct xbundle *xbundle;
683 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
684 new_xbridge->ofproto = xbridge->ofproto;
685 new_xbridge->name = xstrdup(xbridge->name);
686 xlate_xbridge_init(new_xcfg, new_xbridge);
688 xlate_xbridge_set(new_xbridge,
689 xbridge->dpif, xbridge->ml, xbridge->stp,
690 xbridge->rstp, xbridge->ms, xbridge->mbridge,
691 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
692 xbridge->forward_bpdu, xbridge->has_in_band,
694 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
695 xlate_xbundle_copy(new_xbridge, xbundle);
698 /* Copy xports which are not part of a xbundle */
699 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
700 if (!xport->xbundle) {
701 xlate_xport_copy(new_xbridge, NULL, xport);
707 xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
710 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
711 new_xbundle->ofbundle = xbundle->ofbundle;
712 new_xbundle->xbridge = xbridge;
713 new_xbundle->name = xstrdup(xbundle->name);
714 xlate_xbundle_init(new_xcfg, new_xbundle);
716 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
717 xbundle->vlan, xbundle->trunks,
718 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
720 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
721 xlate_xport_copy(xbridge, new_xbundle, xport);
726 xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
729 struct skb_priority_to_dscp *pdscp, *new_pdscp;
730 struct xport *new_xport = xzalloc(sizeof *xport);
731 new_xport->ofport = xport->ofport;
732 new_xport->ofp_port = xport->ofp_port;
733 new_xport->xbridge = xbridge;
734 xlate_xport_init(new_xcfg, new_xport);
736 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
737 xport->bfd, xport->lldp, xport->stp_port_no,
738 xport->rstp_port, xport->config, xport->state,
739 xport->is_tunnel, xport->may_enable);
742 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
744 new_xport->peer = peer;
745 new_xport->peer->peer = new_xport;
750 new_xport->xbundle = xbundle;
751 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
754 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
755 new_pdscp = xmalloc(sizeof *pdscp);
756 new_pdscp->skb_priority = pdscp->skb_priority;
757 new_pdscp->dscp = pdscp->dscp;
758 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
759 hash_int(new_pdscp->skb_priority, 0));
763 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
764 * configuration in xcfgp.
766 * This needs to be called after editing the xlate configuration.
768 * Functions that edit the new xlate configuration are
769 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
775 * edit_xlate_configuration();
777 * xlate_txn_commit(); */
779 xlate_txn_commit(void)
781 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
783 ovsrcu_set(&xcfgp, new_xcfg);
784 ovsrcu_synchronize();
785 xlate_xcfg_free(xcfg);
789 /* Copies the current xlate configuration in xcfgp to new_xcfg.
791 * This needs to be called prior to editing the xlate configuration. */
793 xlate_txn_start(void)
795 struct xbridge *xbridge;
796 struct xlate_cfg *xcfg;
798 ovs_assert(!new_xcfg);
800 new_xcfg = xmalloc(sizeof *new_xcfg);
801 hmap_init(&new_xcfg->xbridges);
802 hmap_init(&new_xcfg->xbundles);
803 hmap_init(&new_xcfg->xports);
805 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
810 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
811 xlate_xbridge_copy(xbridge);
817 xlate_xcfg_free(struct xlate_cfg *xcfg)
819 struct xbridge *xbridge, *next_xbridge;
825 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
826 xlate_xbridge_remove(xcfg, xbridge);
829 hmap_destroy(&xcfg->xbridges);
830 hmap_destroy(&xcfg->xbundles);
831 hmap_destroy(&xcfg->xports);
836 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
838 const struct mac_learning *ml, struct stp *stp,
839 struct rstp *rstp, const struct mcast_snooping *ms,
840 const struct mbridge *mbridge,
841 const struct dpif_sflow *sflow,
842 const struct dpif_ipfix *ipfix,
843 const struct netflow *netflow,
844 bool forward_bpdu, bool has_in_band,
845 const struct dpif_backer_support *support)
847 struct xbridge *xbridge;
849 ovs_assert(new_xcfg);
851 xbridge = xbridge_lookup(new_xcfg, ofproto);
853 xbridge = xzalloc(sizeof *xbridge);
854 xbridge->ofproto = ofproto;
856 xlate_xbridge_init(new_xcfg, xbridge);
860 xbridge->name = xstrdup(name);
862 xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
863 netflow, forward_bpdu, has_in_band, support);
867 xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
869 struct xbundle *xbundle, *next_xbundle;
870 struct xport *xport, *next_xport;
876 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
877 xlate_xport_remove(xcfg, xport);
880 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
881 xlate_xbundle_remove(xcfg, xbundle);
884 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
885 mac_learning_unref(xbridge->ml);
886 mcast_snooping_unref(xbridge->ms);
887 mbridge_unref(xbridge->mbridge);
888 dpif_sflow_unref(xbridge->sflow);
889 dpif_ipfix_unref(xbridge->ipfix);
890 stp_unref(xbridge->stp);
891 rstp_unref(xbridge->rstp);
892 hmap_destroy(&xbridge->xports);
898 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
900 struct xbridge *xbridge;
902 ovs_assert(new_xcfg);
904 xbridge = xbridge_lookup(new_xcfg, ofproto);
905 xlate_xbridge_remove(new_xcfg, xbridge);
909 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
910 const char *name, enum port_vlan_mode vlan_mode, int vlan,
911 unsigned long *trunks, bool use_priority_tags,
912 const struct bond *bond, const struct lacp *lacp,
915 struct xbundle *xbundle;
917 ovs_assert(new_xcfg);
919 xbundle = xbundle_lookup(new_xcfg, ofbundle);
921 xbundle = xzalloc(sizeof *xbundle);
922 xbundle->ofbundle = ofbundle;
923 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
925 xlate_xbundle_init(new_xcfg, xbundle);
929 xbundle->name = xstrdup(name);
931 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
932 use_priority_tags, bond, lacp, floodable);
936 xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
944 LIST_FOR_EACH_POP (xport, bundle_node, &xbundle->xports) {
945 xport->xbundle = NULL;
948 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
949 list_remove(&xbundle->list_node);
950 bond_unref(xbundle->bond);
951 lacp_unref(xbundle->lacp);
957 xlate_bundle_remove(struct ofbundle *ofbundle)
959 struct xbundle *xbundle;
961 ovs_assert(new_xcfg);
963 xbundle = xbundle_lookup(new_xcfg, ofbundle);
964 xlate_xbundle_remove(new_xcfg, xbundle);
968 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
969 struct ofport_dpif *ofport, ofp_port_t ofp_port,
970 odp_port_t odp_port, const struct netdev *netdev,
971 const struct cfm *cfm, const struct bfd *bfd,
972 const struct lldp *lldp, struct ofport_dpif *peer,
973 int stp_port_no, const struct rstp_port *rstp_port,
974 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
975 enum ofputil_port_config config,
976 enum ofputil_port_state state, bool is_tunnel,
982 ovs_assert(new_xcfg);
984 xport = xport_lookup(new_xcfg, ofport);
986 xport = xzalloc(sizeof *xport);
987 xport->ofport = ofport;
988 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
989 xport->ofp_port = ofp_port;
991 xlate_xport_init(new_xcfg, xport);
994 ovs_assert(xport->ofp_port == ofp_port);
996 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, lldp,
997 stp_port_no, rstp_port, config, state, is_tunnel,
1001 xport->peer->peer = NULL;
1003 xport->peer = xport_lookup(new_xcfg, peer);
1005 xport->peer->peer = xport;
1008 if (xport->xbundle) {
1009 list_remove(&xport->bundle_node);
1011 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
1012 if (xport->xbundle) {
1013 list_insert(&xport->xbundle->xports, &xport->bundle_node);
1016 clear_skb_priorities(xport);
1017 for (i = 0; i < n_qdscp; i++) {
1018 struct skb_priority_to_dscp *pdscp;
1019 uint32_t skb_priority;
1021 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
1026 pdscp = xmalloc(sizeof *pdscp);
1027 pdscp->skb_priority = skb_priority;
1028 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1029 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
1030 hash_int(pdscp->skb_priority, 0));
1035 xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
1042 xport->peer->peer = NULL;
1046 if (xport->xbundle) {
1047 list_remove(&xport->bundle_node);
1050 clear_skb_priorities(xport);
1051 hmap_destroy(&xport->skb_priorities);
1053 hmap_remove(&xcfg->xports, &xport->hmap_node);
1054 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
1056 netdev_close(xport->netdev);
1057 rstp_port_unref(xport->rstp_port);
1058 cfm_unref(xport->cfm);
1059 bfd_unref(xport->bfd);
1060 lldp_unref(xport->lldp);
1065 xlate_ofport_remove(struct ofport_dpif *ofport)
1067 struct xport *xport;
1069 ovs_assert(new_xcfg);
1071 xport = xport_lookup(new_xcfg, ofport);
1072 xlate_xport_remove(new_xcfg, xport);
1075 static struct ofproto_dpif *
1076 xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
1077 ofp_port_t *ofp_in_port, const struct xport **xportp)
1079 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1080 const struct xport *xport;
1082 xport = xport_lookup(xcfg, tnl_port_should_receive(flow)
1083 ? tnl_port_receive(flow)
1084 : odp_port_to_ofport(backer, flow->in_port.odp_port));
1085 if (OVS_UNLIKELY(!xport)) {
1090 *ofp_in_port = xport->ofp_port;
1092 return xport->xbridge->ofproto;
1095 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1096 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1097 struct ofproto_dpif *
1098 xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1099 ofp_port_t *ofp_in_port)
1101 const struct xport *xport;
1103 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1106 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1107 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
1108 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1109 * handles for those protocols if they're enabled. Caller may use the returned
1110 * pointers until quiescing, for longer term use additional references must
1113 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1116 xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1117 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1118 struct dpif_sflow **sflow, struct netflow **netflow,
1119 ofp_port_t *ofp_in_port)
1121 struct ofproto_dpif *ofproto;
1122 const struct xport *xport;
1124 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1131 *ofprotop = ofproto;
1135 *ipfix = xport ? xport->xbridge->ipfix : NULL;
1139 *sflow = xport ? xport->xbridge->sflow : NULL;
1143 *netflow = xport ? xport->xbridge->netflow : NULL;
1149 static struct xbridge *
1150 xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
1152 struct hmap *xbridges;
1153 struct xbridge *xbridge;
1155 if (!ofproto || !xcfg) {
1159 xbridges = &xcfg->xbridges;
1161 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
1163 if (xbridge->ofproto == ofproto) {
1170 static struct xbundle *
1171 xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
1173 struct hmap *xbundles;
1174 struct xbundle *xbundle;
1176 if (!ofbundle || !xcfg) {
1180 xbundles = &xcfg->xbundles;
1182 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
1184 if (xbundle->ofbundle == ofbundle) {
1191 static struct xport *
1192 xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
1194 struct hmap *xports;
1195 struct xport *xport;
1197 if (!ofport || !xcfg) {
1201 xports = &xcfg->xports;
1203 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
1205 if (xport->ofport == ofport) {
1212 static struct stp_port *
1213 xport_get_stp_port(const struct xport *xport)
1215 return xport->xbridge->stp && xport->stp_port_no != -1
1216 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1221 xport_stp_learn_state(const struct xport *xport)
1223 struct stp_port *sp = xport_get_stp_port(xport);
1225 ? stp_learn_in_state(stp_port_get_state(sp))
1230 xport_stp_forward_state(const struct xport *xport)
1232 struct stp_port *sp = xport_get_stp_port(xport);
1234 ? stp_forward_in_state(stp_port_get_state(sp))
1239 xport_stp_should_forward_bpdu(const struct xport *xport)
1241 struct stp_port *sp = xport_get_stp_port(xport);
1242 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
1245 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1246 * were used to make the determination.*/
1248 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1250 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1251 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1252 return is_stp(flow);
1256 stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1258 struct stp_port *sp = xport_get_stp_port(xport);
1259 struct dp_packet payload = *packet;
1260 struct eth_header *eth = dp_packet_data(&payload);
1262 /* Sink packets on ports that have STP disabled when the bridge has
1264 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1268 /* Trim off padding on payload. */
1269 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1270 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1273 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1274 stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
1278 static enum rstp_state
1279 xport_get_rstp_port_state(const struct xport *xport)
1281 return xport->rstp_port
1282 ? rstp_port_get_state(xport->rstp_port)
1287 xport_rstp_learn_state(const struct xport *xport)
1289 return xport->xbridge->rstp && xport->rstp_port
1290 ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1295 xport_rstp_forward_state(const struct xport *xport)
1297 return xport->xbridge->rstp && xport->rstp_port
1298 ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1303 xport_rstp_should_manage_bpdu(const struct xport *xport)
1305 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
1309 rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1311 struct dp_packet payload = *packet;
1312 struct eth_header *eth = dp_packet_data(&payload);
1314 /* Sink packets on ports that have no RSTP. */
1315 if (!xport->rstp_port) {
1319 /* Trim off padding on payload. */
1320 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1321 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1324 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1325 rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
1326 dp_packet_size(&payload));
1330 static struct xport *
1331 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1333 struct xport *xport;
1335 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1337 if (xport->ofp_port == ofp_port) {
1345 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1347 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1348 return xport ? xport->odp_port : ODPP_NONE;
1352 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1354 struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1355 return xport && xport->may_enable;
1358 static struct ofputil_bucket *
1359 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1363 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1365 struct group_dpif *group;
1367 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1368 struct ofputil_bucket *bucket;
1370 bucket = group_first_live_bucket(ctx, group, depth);
1371 group_dpif_unref(group);
1372 return bucket == NULL;
1378 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1381 bucket_is_alive(const struct xlate_ctx *ctx,
1382 struct ofputil_bucket *bucket, int depth)
1384 if (depth >= MAX_LIVENESS_RECURSION) {
1385 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1387 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1388 MAX_LIVENESS_RECURSION);
1392 return (!ofputil_bucket_has_liveness(bucket)
1393 || (bucket->watch_port != OFPP_ANY
1394 && odp_port_is_alive(ctx, bucket->watch_port))
1395 || (bucket->watch_group != OFPG_ANY
1396 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
1399 static struct ofputil_bucket *
1400 group_first_live_bucket(const struct xlate_ctx *ctx,
1401 const struct group_dpif *group, int depth)
1403 struct ofputil_bucket *bucket;
1404 const struct ovs_list *buckets;
1406 group_dpif_get_buckets(group, &buckets);
1407 LIST_FOR_EACH (bucket, list_node, buckets) {
1408 if (bucket_is_alive(ctx, bucket, depth)) {
1416 static struct ofputil_bucket *
1417 group_best_live_bucket(const struct xlate_ctx *ctx,
1418 const struct group_dpif *group,
1421 struct ofputil_bucket *best_bucket = NULL;
1422 uint32_t best_score = 0;
1425 struct ofputil_bucket *bucket;
1426 const struct ovs_list *buckets;
1428 group_dpif_get_buckets(group, &buckets);
1429 LIST_FOR_EACH (bucket, list_node, buckets) {
1430 if (bucket_is_alive(ctx, bucket, 0)) {
1431 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
1432 if (score >= best_score) {
1433 best_bucket = bucket;
1444 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
1446 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1447 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1451 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1453 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1456 static mirror_mask_t
1457 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1459 return xbundle != &ofpp_none_bundle
1460 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1464 static mirror_mask_t
1465 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
1467 return xbundle != &ofpp_none_bundle
1468 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1472 static mirror_mask_t
1473 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
1475 return xbundle != &ofpp_none_bundle
1476 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1480 static struct xbundle *
1481 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1482 bool warn, struct xport **in_xportp)
1484 struct xport *xport;
1486 /* Find the port and bundle for the received packet. */
1487 xport = get_ofp_port(xbridge, in_port);
1491 if (xport && xport->xbundle) {
1492 return xport->xbundle;
1495 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1496 * which a controller may use as the ingress port for traffic that
1497 * it is sourcing. */
1498 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
1499 return &ofpp_none_bundle;
1502 /* Odd. A few possible reasons here:
1504 * - We deleted a port but there are still a few packets queued up
1507 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1508 * we don't know about.
1510 * - The ofproto client didn't configure the port as part of a bundle.
1511 * This is particularly likely to happen if a packet was received on the
1512 * port after it was created, but before the client had a chance to
1513 * configure its bundle.
1516 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1518 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1519 "port %"PRIu16, xbridge->name, in_port);
1525 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
1527 const struct xbridge *xbridge = ctx->xbridge;
1528 mirror_mask_t mirrors;
1529 struct xbundle *in_xbundle;
1533 mirrors = ctx->xout->mirrors;
1534 ctx->xout->mirrors = 0;
1536 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
1537 ctx->xin->packet != NULL, NULL);
1541 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
1543 /* Drop frames on bundles reserved for mirroring. */
1544 if (xbundle_mirror_out(xbridge, in_xbundle)) {
1545 if (ctx->xin->packet != NULL) {
1546 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1547 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1548 "%s, which is reserved exclusively for mirroring",
1549 ctx->xbridge->name, in_xbundle->name);
1551 ofpbuf_clear(ctx->xout->odp_actions);
1556 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
1557 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1560 vlan = input_vid_to_vlan(in_xbundle, vid);
1566 /* Restore the original packet before adding the mirror actions. */
1567 ctx->xin->flow = *orig_flow;
1570 mirror_mask_t dup_mirrors;
1571 struct ofbundle *out;
1572 const unsigned long *vlans;
1577 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1578 &vlans, &dup_mirrors, &out, &out_vlan);
1579 ovs_assert(has_mirror);
1582 ctx->wc->masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1584 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
1586 if (!vlan_mirrored) {
1587 mirrors = zero_rightmost_1bit(mirrors);
1591 mirrors &= ~dup_mirrors;
1592 ctx->xout->mirrors |= dup_mirrors;
1594 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1595 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1597 output_normal(ctx, out_xbundle, vlan);
1599 } else if (vlan != out_vlan
1600 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
1601 struct xbundle *xbundle;
1603 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1604 if (xbundle_includes_vlan(xbundle, out_vlan)
1605 && !xbundle_mirror_out(xbridge, xbundle)) {
1606 output_normal(ctx, xbundle, out_vlan);
1613 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1614 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1615 * the bundle on which the packet was received, returns the VLAN to which the
1618 * Both 'vid' and the return value are in the range 0...4095. */
1620 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1622 switch (in_xbundle->vlan_mode) {
1623 case PORT_VLAN_ACCESS:
1624 return in_xbundle->vlan;
1627 case PORT_VLAN_TRUNK:
1630 case PORT_VLAN_NATIVE_UNTAGGED:
1631 case PORT_VLAN_NATIVE_TAGGED:
1632 return vid ? vid : in_xbundle->vlan;
1639 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1640 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1643 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1644 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1647 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1649 /* Allow any VID on the OFPP_NONE port. */
1650 if (in_xbundle == &ofpp_none_bundle) {
1654 switch (in_xbundle->vlan_mode) {
1655 case PORT_VLAN_ACCESS:
1658 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1659 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1660 "packet received on port %s configured as VLAN "
1661 "%"PRIu16" access port", vid, in_xbundle->name,
1668 case PORT_VLAN_NATIVE_UNTAGGED:
1669 case PORT_VLAN_NATIVE_TAGGED:
1671 /* Port must always carry its native VLAN. */
1675 case PORT_VLAN_TRUNK:
1676 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1678 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1679 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1680 "received on port %s not configured for trunking "
1681 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1693 /* Given 'vlan', the VLAN that a packet belongs to, and
1694 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1695 * that should be included in the 802.1Q header. (If the return value is 0,
1696 * then the 802.1Q header should only be included in the packet if there is a
1699 * Both 'vlan' and the return value are in the range 0...4095. */
1701 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1703 switch (out_xbundle->vlan_mode) {
1704 case PORT_VLAN_ACCESS:
1707 case PORT_VLAN_TRUNK:
1708 case PORT_VLAN_NATIVE_TAGGED:
1711 case PORT_VLAN_NATIVE_UNTAGGED:
1712 return vlan == out_xbundle->vlan ? 0 : vlan;
1720 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1723 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1725 ovs_be16 tci, old_tci;
1726 struct xport *xport;
1727 struct xlate_bond_recirc xr;
1728 bool use_recirc = false;
1730 vid = output_vlan_to_vid(out_xbundle, vlan);
1731 if (list_is_empty(&out_xbundle->xports)) {
1732 /* Partially configured bundle with no slaves. Drop the packet. */
1734 } else if (!out_xbundle->bond) {
1735 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1738 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1739 struct flow_wildcards *wc = ctx->wc;
1740 struct ofport_dpif *ofport;
1742 if (ctx->xbridge->support.odp.recirc) {
1743 use_recirc = bond_may_recirc(
1744 out_xbundle->bond, &xr.recirc_id, &xr.hash_basis);
1747 /* Only TCP mode uses recirculation. */
1748 xr.hash_alg = OVS_HASH_ALG_L4;
1749 bond_update_post_recirc_rules(out_xbundle->bond, false);
1751 /* Recirculation does not require unmasking hash fields. */
1756 ofport = bond_choose_output_slave(out_xbundle->bond,
1757 &ctx->xin->flow, wc, vid);
1758 xport = xport_lookup(xcfg, ofport);
1761 /* No slaves enabled, so drop packet. */
1765 /* If use_recirc is set, the main thread will handle stats
1766 * accounting for this bond. */
1768 if (ctx->xin->resubmit_stats) {
1769 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1770 ctx->xin->resubmit_stats->n_bytes);
1772 if (ctx->xin->xcache) {
1773 struct xc_entry *entry;
1776 flow = &ctx->xin->flow;
1777 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1778 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1779 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1780 entry->u.bond.vid = vid;
1785 old_tci = *flow_tci;
1787 if (tci || out_xbundle->use_priority_tags) {
1788 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1790 tci |= htons(VLAN_CFI);
1795 compose_output_action(ctx, xport->ofp_port, use_recirc ? &xr : NULL);
1796 *flow_tci = old_tci;
1799 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1800 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1801 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1803 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1805 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1809 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1810 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1814 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1815 if (flow->nw_proto == ARP_OP_REPLY) {
1817 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1818 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1819 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1821 return flow->nw_src == flow->nw_dst;
1827 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1828 * dropped. Returns true if they may be forwarded, false if they should be
1831 * 'in_port' must be the xport that corresponds to flow->in_port.
1832 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1834 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1835 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1836 * checked by input_vid_is_valid().
1838 * May also add tags to '*tags', although the current implementation only does
1839 * so in one special case.
1842 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1845 struct xbundle *in_xbundle = in_port->xbundle;
1846 const struct xbridge *xbridge = ctx->xbridge;
1847 struct flow *flow = &ctx->xin->flow;
1849 /* Drop frames for reserved multicast addresses
1850 * only if forward_bpdu option is absent. */
1851 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1852 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1856 if (in_xbundle->bond) {
1857 struct mac_entry *mac;
1859 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1865 xlate_report(ctx, "bonding refused admissibility, dropping");
1868 case BV_DROP_IF_MOVED:
1869 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1870 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1872 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
1873 && (!is_gratuitous_arp(flow, ctx->wc)
1874 || mac_entry_is_grat_arp_locked(mac))) {
1875 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1876 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1880 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1888 /* Checks whether a MAC learning update is necessary for MAC learning table
1889 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1892 * Most packets processed through the MAC learning table do not actually
1893 * change it in any way. This function requires only a read lock on the MAC
1894 * learning table, so it is much cheaper in this common case.
1896 * Keep the code here synchronized with that in update_learning_table__()
1899 is_mac_learning_update_needed(const struct mac_learning *ml,
1900 const struct flow *flow,
1901 struct flow_wildcards *wc,
1902 int vlan, struct xbundle *in_xbundle)
1903 OVS_REQ_RDLOCK(ml->rwlock)
1905 struct mac_entry *mac;
1907 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1911 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1912 if (!mac || mac_entry_age(ml, mac)) {
1916 if (is_gratuitous_arp(flow, wc)) {
1917 /* We don't want to learn from gratuitous ARP packets that are
1918 * reflected back over bond slaves so we lock the learning table. */
1919 if (!in_xbundle->bond) {
1921 } else if (mac_entry_is_grat_arp_locked(mac)) {
1926 return mac_entry_get_port(ml, mac) != in_xbundle->ofbundle;
1930 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1931 * received on 'in_xbundle' in 'vlan'.
1933 * This code repeats all the checks in is_mac_learning_update_needed() because
1934 * the lock was released between there and here and thus the MAC learning state
1935 * could have changed.
1937 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1940 update_learning_table__(const struct xbridge *xbridge,
1941 const struct flow *flow, struct flow_wildcards *wc,
1942 int vlan, struct xbundle *in_xbundle)
1943 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1945 struct mac_entry *mac;
1947 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1951 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1952 if (is_gratuitous_arp(flow, wc)) {
1953 /* We don't want to learn from gratuitous ARP packets that are
1954 * reflected back over bond slaves so we lock the learning table. */
1955 if (!in_xbundle->bond) {
1956 mac_entry_set_grat_arp_lock(mac);
1957 } else if (mac_entry_is_grat_arp_locked(mac)) {
1962 if (mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle) {
1963 /* The log messages here could actually be useful in debugging,
1964 * so keep the rate limit relatively high. */
1965 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1967 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1968 "on port %s in VLAN %d",
1969 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1970 in_xbundle->name, vlan);
1972 mac_entry_set_port(xbridge->ml, mac, in_xbundle->ofbundle);
1977 update_learning_table(const struct xbridge *xbridge,
1978 const struct flow *flow, struct flow_wildcards *wc,
1979 int vlan, struct xbundle *in_xbundle)
1983 /* Don't learn the OFPP_NONE port. */
1984 if (in_xbundle == &ofpp_none_bundle) {
1988 /* First try the common case: no change to MAC learning table. */
1989 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1990 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1992 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1995 /* Slow path: MAC learning table might need an update. */
1996 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1997 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1998 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2002 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2003 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
2005 update_mcast_snooping_table4__(const struct xbridge *xbridge,
2006 const struct flow *flow,
2007 struct mcast_snooping *ms, int vlan,
2008 struct xbundle *in_xbundle,
2009 const struct dp_packet *packet)
2010 OVS_REQ_WRLOCK(ms->rwlock)
2012 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2014 ovs_be32 ip4 = flow->igmp_group_ip4;
2016 switch (ntohs(flow->tp_src)) {
2017 case IGMP_HOST_MEMBERSHIP_REPORT:
2018 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2019 if (mcast_snooping_add_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2020 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
2021 IP_FMT" is on port %s in VLAN %d",
2022 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2025 case IGMP_HOST_LEAVE_MESSAGE:
2026 if (mcast_snooping_leave_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2027 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
2028 IP_FMT" is on port %s in VLAN %d",
2029 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2032 case IGMP_HOST_MEMBERSHIP_QUERY:
2033 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
2034 in_xbundle->ofbundle)) {
2035 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
2036 IP_FMT" is on port %s in VLAN %d",
2037 xbridge->name, IP_ARGS(flow->nw_src),
2038 in_xbundle->name, vlan);
2041 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2042 if ((count = mcast_snooping_add_report(ms, packet, vlan,
2043 in_xbundle->ofbundle))) {
2044 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2045 "addresses on port %s in VLAN %d",
2046 xbridge->name, count, in_xbundle->name, vlan);
2053 update_mcast_snooping_table6__(const struct xbridge *xbridge,
2054 const struct flow *flow,
2055 struct mcast_snooping *ms, int vlan,
2056 struct xbundle *in_xbundle,
2057 const struct dp_packet *packet)
2058 OVS_REQ_WRLOCK(ms->rwlock)
2060 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2063 switch (ntohs(flow->tp_src)) {
2065 if (!ipv6_addr_equals(&flow->ipv6_src, &in6addr_any)
2066 && mcast_snooping_add_mrouter(ms, vlan, in_xbundle->ofbundle)) {
2067 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query on port %s"
2069 xbridge->name, in_xbundle->name, vlan);
2075 count = mcast_snooping_add_mld(ms, packet, vlan, in_xbundle->ofbundle);
2077 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2078 "addresses on port %s in VLAN %d",
2079 xbridge->name, count, in_xbundle->name, vlan);
2085 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2086 * was received on 'in_xbundle' in 'vlan'. */
2088 update_mcast_snooping_table(const struct xbridge *xbridge,
2089 const struct flow *flow, int vlan,
2090 struct xbundle *in_xbundle,
2091 const struct dp_packet *packet)
2093 struct mcast_snooping *ms = xbridge->ms;
2094 struct xlate_cfg *xcfg;
2095 struct xbundle *mcast_xbundle;
2096 struct mcast_port_bundle *fport;
2098 /* Don't learn the OFPP_NONE port. */
2099 if (in_xbundle == &ofpp_none_bundle) {
2103 /* Don't learn from flood ports */
2104 mcast_xbundle = NULL;
2105 ovs_rwlock_wrlock(&ms->rwlock);
2106 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2107 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2108 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2109 if (mcast_xbundle == in_xbundle) {
2114 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
2115 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2116 update_mcast_snooping_table4__(xbridge, flow, ms, vlan,
2117 in_xbundle, packet);
2119 update_mcast_snooping_table6__(xbridge, flow, ms, vlan,
2120 in_xbundle, packet);
2123 ovs_rwlock_unlock(&ms->rwlock);
2126 /* send the packet to ports having the multicast group learned */
2128 xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2129 struct mcast_snooping *ms OVS_UNUSED,
2130 struct mcast_group *grp,
2131 struct xbundle *in_xbundle, uint16_t vlan)
2132 OVS_REQ_RDLOCK(ms->rwlock)
2134 struct xlate_cfg *xcfg;
2135 struct mcast_group_bundle *b;
2136 struct xbundle *mcast_xbundle;
2138 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2139 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2140 mcast_xbundle = xbundle_lookup(xcfg, b->port);
2141 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2142 xlate_report(ctx, "forwarding to mcast group port");
2143 output_normal(ctx, mcast_xbundle, vlan);
2144 } else if (!mcast_xbundle) {
2145 xlate_report(ctx, "mcast group port is unknown, dropping");
2147 xlate_report(ctx, "mcast group port is input port, dropping");
2152 /* send the packet to ports connected to multicast routers */
2154 xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2155 struct mcast_snooping *ms,
2156 struct xbundle *in_xbundle, uint16_t vlan)
2157 OVS_REQ_RDLOCK(ms->rwlock)
2159 struct xlate_cfg *xcfg;
2160 struct mcast_mrouter_bundle *mrouter;
2161 struct xbundle *mcast_xbundle;
2163 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2164 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2165 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2166 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2167 xlate_report(ctx, "forwarding to mcast router port");
2168 output_normal(ctx, mcast_xbundle, vlan);
2169 } else if (!mcast_xbundle) {
2170 xlate_report(ctx, "mcast router port is unknown, dropping");
2172 xlate_report(ctx, "mcast router port is input port, dropping");
2177 /* send the packet to ports flagged to be flooded */
2179 xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2180 struct mcast_snooping *ms,
2181 struct xbundle *in_xbundle, uint16_t vlan)
2182 OVS_REQ_RDLOCK(ms->rwlock)
2184 struct xlate_cfg *xcfg;
2185 struct mcast_port_bundle *fport;
2186 struct xbundle *mcast_xbundle;
2188 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2189 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2190 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2191 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2192 xlate_report(ctx, "forwarding to mcast flood port");
2193 output_normal(ctx, mcast_xbundle, vlan);
2194 } else if (!mcast_xbundle) {
2195 xlate_report(ctx, "mcast flood port is unknown, dropping");
2197 xlate_report(ctx, "mcast flood port is input port, dropping");
2202 /* forward the Reports to configured ports */
2204 xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
2205 struct mcast_snooping *ms,
2206 struct xbundle *in_xbundle, uint16_t vlan)
2207 OVS_REQ_RDLOCK(ms->rwlock)
2209 struct xlate_cfg *xcfg;
2210 struct mcast_port_bundle *rport;
2211 struct xbundle *mcast_xbundle;
2213 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2214 LIST_FOR_EACH(rport, node, &ms->rport_list) {
2215 mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2216 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2217 xlate_report(ctx, "forwarding Report to mcast flagged port");
2218 output_normal(ctx, mcast_xbundle, vlan);
2219 } else if (!mcast_xbundle) {
2220 xlate_report(ctx, "mcast port is unknown, dropping the Report");
2222 xlate_report(ctx, "mcast port is input port, dropping the Report");
2228 xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2231 struct xbundle *xbundle;
2233 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2234 if (xbundle != in_xbundle
2235 && xbundle_includes_vlan(xbundle, vlan)
2236 && xbundle->floodable
2237 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2238 output_normal(ctx, xbundle, vlan);
2241 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2245 xlate_normal(struct xlate_ctx *ctx)
2247 struct flow_wildcards *wc = ctx->wc;
2248 struct flow *flow = &ctx->xin->flow;
2249 struct xbundle *in_xbundle;
2250 struct xport *in_port;
2251 struct mac_entry *mac;
2256 ctx->xout->has_normal = true;
2258 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2259 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2260 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2262 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
2263 ctx->xin->packet != NULL, &in_port);
2265 xlate_report(ctx, "no input bundle, dropping");
2269 /* Drop malformed frames. */
2270 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
2271 !(flow->vlan_tci & htons(VLAN_CFI))) {
2272 if (ctx->xin->packet != NULL) {
2273 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2274 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
2275 "VLAN tag received on port %s",
2276 ctx->xbridge->name, in_xbundle->name);
2278 xlate_report(ctx, "partial VLAN tag, dropping");
2282 /* Drop frames on bundles reserved for mirroring. */
2283 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
2284 if (ctx->xin->packet != NULL) {
2285 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2286 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2287 "%s, which is reserved exclusively for mirroring",
2288 ctx->xbridge->name, in_xbundle->name);
2290 xlate_report(ctx, "input port is mirror output port, dropping");
2295 vid = vlan_tci_to_vid(flow->vlan_tci);
2296 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
2297 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2300 vlan = input_vid_to_vlan(in_xbundle, vid);
2302 /* Check other admissibility requirements. */
2303 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2307 /* Learn source MAC. */
2308 if (ctx->xin->may_learn) {
2309 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
2311 if (ctx->xin->xcache) {
2312 struct xc_entry *entry;
2314 /* Save enough info to update mac learning table later. */
2315 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
2316 entry->u.normal.ofproto = ctx->xbridge->ofproto;
2317 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2318 entry->u.normal.vlan = vlan;
2321 /* Determine output bundle. */
2322 if (mcast_snooping_enabled(ctx->xbridge->ms)
2323 && !eth_addr_is_broadcast(flow->dl_dst)
2324 && eth_addr_is_multicast(flow->dl_dst)
2325 && is_ip_any(flow)) {
2326 struct mcast_snooping *ms = ctx->xbridge->ms;
2327 struct mcast_group *grp = NULL;
2329 if (is_igmp(flow)) {
2330 if (mcast_snooping_is_membership(flow->tp_src) ||
2331 mcast_snooping_is_query(flow->tp_src)) {
2332 if (ctx->xin->may_learn) {
2333 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2334 in_xbundle, ctx->xin->packet);
2337 * IGMP packets need to take the slow path, in order to be
2338 * processed for mdb updates. That will prevent expires
2339 * firing off even after hosts have sent reports.
2341 ctx->xout->slow |= SLOW_ACTION;
2344 if (mcast_snooping_is_membership(flow->tp_src)) {
2345 ovs_rwlock_rdlock(&ms->rwlock);
2346 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2347 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2348 * forward IGMP Membership Reports only to those ports where
2349 * multicast routers are attached. Alternatively stated: a
2350 * snooping switch should not forward IGMP Membership Reports
2351 * to ports on which only hosts are attached.
2352 * An administrative control may be provided to override this
2353 * restriction, allowing the report messages to be flooded to
2355 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2356 ovs_rwlock_unlock(&ms->rwlock);
2358 xlate_report(ctx, "multicast traffic, flooding");
2359 xlate_normal_flood(ctx, in_xbundle, vlan);
2362 } else if (is_mld(flow)) {
2363 ctx->xout->slow |= SLOW_ACTION;
2364 if (ctx->xin->may_learn) {
2365 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2366 in_xbundle, ctx->xin->packet);
2368 if (is_mld_report(flow)) {
2369 ovs_rwlock_rdlock(&ms->rwlock);
2370 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2371 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2372 ovs_rwlock_unlock(&ms->rwlock);
2374 xlate_report(ctx, "MLD query, flooding");
2375 xlate_normal_flood(ctx, in_xbundle, vlan);
2378 if ((flow->dl_type == htons(ETH_TYPE_IP)
2379 && ip_is_local_multicast(flow->nw_dst))
2380 || (flow->dl_type == htons(ETH_TYPE_IPV6)
2381 && ipv6_is_all_hosts(&flow->ipv6_dst))) {
2382 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2383 * address in the 224.0.0.x range which are not IGMP must
2384 * be forwarded on all ports */
2385 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2386 xlate_normal_flood(ctx, in_xbundle, vlan);
2391 /* forwarding to group base ports */
2392 ovs_rwlock_rdlock(&ms->rwlock);
2393 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2394 grp = mcast_snooping_lookup4(ms, flow->nw_dst, vlan);
2395 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2396 grp = mcast_snooping_lookup(ms, &flow->ipv6_dst, vlan);
2399 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2400 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2401 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2403 if (mcast_snooping_flood_unreg(ms)) {
2404 xlate_report(ctx, "unregistered multicast, flooding");
2405 xlate_normal_flood(ctx, in_xbundle, vlan);
2407 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2408 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2411 ovs_rwlock_unlock(&ms->rwlock);
2413 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2414 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
2415 mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
2416 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2419 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2420 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2421 if (mac_xbundle && mac_xbundle != in_xbundle) {
2422 xlate_report(ctx, "forwarding to learned port");
2423 output_normal(ctx, mac_xbundle, vlan);
2424 } else if (!mac_xbundle) {
2425 xlate_report(ctx, "learned port is unknown, dropping");
2427 xlate_report(ctx, "learned port is input port, dropping");
2430 xlate_report(ctx, "no learned MAC for destination, flooding");
2431 xlate_normal_flood(ctx, in_xbundle, vlan);
2436 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
2437 * the number of packets out of UINT32_MAX to sample. The given
2438 * cookie is passed back in the callback for each sampled packet.
2441 compose_sample_action(const struct xbridge *xbridge,
2442 struct ofpbuf *odp_actions,
2443 const struct flow *flow,
2444 const uint32_t probability,
2445 const union user_action_cookie *cookie,
2446 const size_t cookie_size,
2447 const odp_port_t tunnel_out_port,
2448 bool include_actions)
2450 size_t sample_offset, actions_offset;
2451 odp_port_t odp_port;
2455 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
2457 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
2459 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
2461 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
2462 pid = dpif_port_get_pid(xbridge->dpif, odp_port,
2463 flow_hash_5tuple(flow, 0));
2464 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2469 nl_msg_end_nested(odp_actions, actions_offset);
2470 nl_msg_end_nested(odp_actions, sample_offset);
2471 return cookie_offset;
2475 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
2476 odp_port_t odp_port, unsigned int n_outputs,
2477 union user_action_cookie *cookie)
2481 cookie->type = USER_ACTION_COOKIE_SFLOW;
2482 cookie->sflow.vlan_tci = vlan_tci;
2484 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2485 * port information") for the interpretation of cookie->output. */
2486 switch (n_outputs) {
2488 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2489 cookie->sflow.output = 0x40000000 | 256;
2493 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
2495 cookie->sflow.output = ifindex;
2500 /* 0x80000000 means "multiple output ports. */
2501 cookie->sflow.output = 0x80000000 | n_outputs;
2506 /* Compose SAMPLE action for sFlow bridge sampling. */
2508 compose_sflow_action(const struct xbridge *xbridge,
2509 struct ofpbuf *odp_actions,
2510 const struct flow *flow,
2511 odp_port_t odp_port)
2513 uint32_t probability;
2514 union user_action_cookie cookie;
2516 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
2520 probability = dpif_sflow_get_probability(xbridge->sflow);
2521 compose_sflow_cookie(xbridge, htons(0), odp_port,
2522 odp_port == ODPP_NONE ? 0 : 1, &cookie);
2524 return compose_sample_action(xbridge, odp_actions, flow, probability,
2525 &cookie, sizeof cookie.sflow, ODPP_NONE,
2530 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
2531 uint32_t obs_domain_id, uint32_t obs_point_id,
2532 union user_action_cookie *cookie)
2534 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
2535 cookie->flow_sample.probability = probability;
2536 cookie->flow_sample.collector_set_id = collector_set_id;
2537 cookie->flow_sample.obs_domain_id = obs_domain_id;
2538 cookie->flow_sample.obs_point_id = obs_point_id;
2542 compose_ipfix_cookie(union user_action_cookie *cookie,
2543 odp_port_t output_odp_port)
2545 cookie->type = USER_ACTION_COOKIE_IPFIX;
2546 cookie->ipfix.output_odp_port = output_odp_port;
2549 /* Compose SAMPLE action for IPFIX bridge sampling. */
2551 compose_ipfix_action(const struct xbridge *xbridge,
2552 struct ofpbuf *odp_actions,
2553 const struct flow *flow,
2554 odp_port_t output_odp_port)
2556 uint32_t probability;
2557 union user_action_cookie cookie;
2558 odp_port_t tunnel_out_port = ODPP_NONE;
2560 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
2564 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2566 if (output_odp_port == ODPP_NONE &&
2567 !dpif_ipfix_get_bridge_exporter_input_sampling(xbridge->ipfix)) {
2571 /* For output case, output_odp_port is valid*/
2572 if (output_odp_port != ODPP_NONE) {
2573 if (!dpif_ipfix_get_bridge_exporter_output_sampling(xbridge->ipfix)) {
2576 /* If tunnel sampling is enabled, put an additional option attribute:
2577 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2579 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(xbridge->ipfix) &&
2580 dpif_ipfix_get_tunnel_port(xbridge->ipfix, output_odp_port) ) {
2581 tunnel_out_port = output_odp_port;
2585 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
2586 compose_ipfix_cookie(&cookie, output_odp_port);
2588 compose_sample_action(xbridge, odp_actions, flow, probability,
2589 &cookie, sizeof cookie.ipfix, tunnel_out_port,
2593 /* SAMPLE action for sFlow must be first action in any given list of
2594 * actions. At this point we do not have all information required to
2595 * build it. So try to build sample action as complete as possible. */
2597 add_sflow_action(struct xlate_ctx *ctx)
2599 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
2600 ctx->xout->odp_actions,
2601 &ctx->xin->flow, ODPP_NONE);
2602 ctx->sflow_odp_port = 0;
2603 ctx->sflow_n_outputs = 0;
2606 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
2607 * of actions, eventually after the SAMPLE action for sFlow. */
2609 add_ipfix_action(struct xlate_ctx *ctx)
2611 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2612 &ctx->xin->flow, ODPP_NONE);
2616 add_ipfix_output_action(struct xlate_ctx *ctx, odp_port_t port)
2618 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2619 &ctx->xin->flow, port);
2622 /* Fix SAMPLE action according to data collected while composing ODP actions.
2623 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
2624 * USERSPACE action's user-cookie which is required for sflow. */
2626 fix_sflow_action(struct xlate_ctx *ctx)
2628 const struct flow *base = &ctx->base_flow;
2629 union user_action_cookie *cookie;
2631 if (!ctx->user_cookie_offset) {
2635 cookie = ofpbuf_at(ctx->xout->odp_actions, ctx->user_cookie_offset,
2636 sizeof cookie->sflow);
2637 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2639 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
2640 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
2643 static enum slow_path_reason
2644 process_special(struct xlate_ctx *ctx, const struct flow *flow,
2645 const struct xport *xport, const struct dp_packet *packet)
2647 struct flow_wildcards *wc = ctx->wc;
2648 const struct xbridge *xbridge = ctx->xbridge;
2652 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
2654 cfm_process_heartbeat(xport->cfm, packet);
2657 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
2659 bfd_process_packet(xport->bfd, flow, packet);
2660 /* If POLL received, immediately sends FINAL back. */
2661 if (bfd_should_send_packet(xport->bfd)) {
2662 ofproto_dpif_monitor_port_send_soon(xport->ofport);
2666 } else if (xport->xbundle && xport->xbundle->lacp
2667 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2669 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
2672 } else if ((xbridge->stp || xbridge->rstp) &&
2673 stp_should_process_flow(flow, wc)) {
2676 ? stp_process_packet(xport, packet)
2677 : rstp_process_packet(xport, packet);
2680 } else if (xport->lldp && lldp_should_process_flow(xport->lldp, flow)) {
2682 lldp_process_packet(xport->lldp, packet);
2691 tnl_route_lookup_flow(const struct flow *oflow,
2692 ovs_be32 *ip, struct xport **out_port)
2694 char out_dev[IFNAMSIZ];
2695 struct xbridge *xbridge;
2696 struct xlate_cfg *xcfg;
2699 if (!ovs_router_lookup(oflow->tunnel.ip_dst, out_dev, &gw)) {
2706 *ip = oflow->tunnel.ip_dst;
2709 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2712 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
2713 if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
2716 HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
2717 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
2728 xlate_flood_packet(struct xbridge *xbridge, struct dp_packet *packet)
2730 struct ofpact_output output;
2733 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
2734 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
2735 flow_extract(packet, &flow);
2736 flow.in_port.ofp_port = OFPP_NONE;
2737 output.port = OFPP_FLOOD;
2740 return ofproto_dpif_execute_actions(xbridge->ofproto, &flow, NULL,
2741 &output.ofpact, sizeof output,
2746 tnl_send_arp_request(const struct xport *out_dev, const uint8_t eth_src[ETH_ADDR_LEN],
2747 ovs_be32 ip_src, ovs_be32 ip_dst)
2749 struct xbridge *xbridge = out_dev->xbridge;
2750 struct dp_packet packet;
2752 dp_packet_init(&packet, 0);
2753 compose_arp(&packet, ARP_OP_REQUEST,
2754 eth_src, eth_addr_zero, true, ip_src, ip_dst);
2756 xlate_flood_packet(xbridge, &packet);
2757 dp_packet_uninit(&packet);
2761 build_tunnel_send(struct xlate_ctx *ctx, const struct xport *xport,
2762 const struct flow *flow, odp_port_t tunnel_odp_port)
2764 struct ovs_action_push_tnl tnl_push_data;
2765 struct xport *out_dev = NULL;
2766 ovs_be32 s_ip, d_ip = 0;
2767 uint8_t smac[ETH_ADDR_LEN];
2768 uint8_t dmac[ETH_ADDR_LEN];
2771 err = tnl_route_lookup_flow(flow, &d_ip, &out_dev);
2773 xlate_report(ctx, "native tunnel routing failed");
2776 xlate_report(ctx, "tunneling to "IP_FMT" via %s",
2777 IP_ARGS(d_ip), netdev_get_name(out_dev->netdev));
2779 /* Use mac addr of bridge port of the peer. */
2780 err = netdev_get_etheraddr(out_dev->netdev, smac);
2782 xlate_report(ctx, "tunnel output device lacks Ethernet address");
2786 err = netdev_get_in4(out_dev->netdev, (struct in_addr *) &s_ip, NULL);
2788 xlate_report(ctx, "tunnel output device lacks IPv4 address");
2792 err = tnl_arp_lookup(out_dev->xbridge->name, d_ip, dmac);
2794 xlate_report(ctx, "ARP cache miss for "IP_FMT" on bridge %s, "
2795 "sending ARP request",
2796 IP_ARGS(d_ip), out_dev->xbridge->name);
2797 tnl_send_arp_request(out_dev, smac, s_ip, d_ip);
2800 if (ctx->xin->xcache) {
2801 struct xc_entry *entry;
2803 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_ARP);
2804 ovs_strlcpy(entry->u.tnl_arp_cache.br_name, out_dev->xbridge->name,
2805 sizeof entry->u.tnl_arp_cache.br_name);
2806 entry->u.tnl_arp_cache.d_ip = d_ip;
2809 xlate_report(ctx, "tunneling from "ETH_ADDR_FMT" "IP_FMT
2810 " to "ETH_ADDR_FMT" "IP_FMT,
2811 ETH_ADDR_ARGS(smac), IP_ARGS(s_ip),
2812 ETH_ADDR_ARGS(dmac), IP_ARGS(d_ip));
2813 err = tnl_port_build_header(xport->ofport, flow,
2814 dmac, smac, s_ip, &tnl_push_data);
2818 tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
2819 tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
2820 odp_put_tnl_push_action(ctx->xout->odp_actions, &tnl_push_data);
2825 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
2826 const struct xlate_bond_recirc *xr, bool check_stp)
2828 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
2829 struct flow_wildcards *wc = ctx->wc;
2830 struct flow *flow = &ctx->xin->flow;
2831 struct flow_tnl flow_tnl;
2832 ovs_be16 flow_vlan_tci;
2833 uint32_t flow_pkt_mark;
2834 uint8_t flow_nw_tos;
2835 odp_port_t out_port, odp_port;
2836 bool tnl_push_pop_send = false;
2839 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2840 * before traversing a patch port. */
2841 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 33);
2842 memset(&flow_tnl, 0, sizeof flow_tnl);
2845 xlate_report(ctx, "Nonexistent output port");
2847 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
2848 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2850 } else if (check_stp) {
2851 if (is_stp(&ctx->base_flow)) {
2852 if (!xport_stp_should_forward_bpdu(xport) &&
2853 !xport_rstp_should_manage_bpdu(xport)) {
2854 if (ctx->xbridge->stp != NULL) {
2855 xlate_report(ctx, "STP not in listening state, "
2856 "skipping bpdu output");
2857 } else if (ctx->xbridge->rstp != NULL) {
2858 xlate_report(ctx, "RSTP not managing BPDU in this state, "
2859 "skipping bpdu output");
2863 } else if (!xport_stp_forward_state(xport) ||
2864 !xport_rstp_forward_state(xport)) {
2865 if (ctx->xbridge->stp != NULL) {
2866 xlate_report(ctx, "STP not in forwarding state, "
2868 } else if (ctx->xbridge->rstp != NULL) {
2869 xlate_report(ctx, "RSTP not in forwarding state, "
2876 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
2877 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
2882 const struct xport *peer = xport->peer;
2883 struct flow old_flow = ctx->xin->flow;
2884 bool old_was_mpls = ctx->was_mpls;
2885 cls_version_t old_version = ctx->tables_version;
2886 enum slow_path_reason special;
2887 struct ofpbuf old_stack = ctx->stack;
2888 union mf_subvalue new_stack[1024 / sizeof(union mf_subvalue)];
2889 struct ofpbuf old_action_set = ctx->action_set;
2890 uint64_t actset_stub[1024 / 8];
2892 ofpbuf_use_stub(&ctx->stack, new_stack, sizeof new_stack);
2893 ofpbuf_use_stub(&ctx->action_set, actset_stub, sizeof actset_stub);
2894 ctx->xbridge = peer->xbridge;
2895 flow->in_port.ofp_port = peer->ofp_port;
2896 flow->metadata = htonll(0);
2897 memset(&flow->tunnel, 0, sizeof flow->tunnel);
2898 memset(flow->regs, 0, sizeof flow->regs);
2899 flow->actset_output = OFPP_UNSET;
2901 /* The bridge is now known so obtain its table version. */
2903 = ofproto_dpif_get_tables_version(ctx->xbridge->ofproto);
2905 special = process_special(ctx, &ctx->xin->flow, peer,
2908 ctx->xout->slow |= special;
2909 } else if (may_receive(peer, ctx)) {
2910 if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
2911 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2912 if (ctx->action_set.size) {
2913 /* Translate action set only if not dropping the packet and
2914 * not recirculating. */
2915 if (!exit_recirculates(ctx)) {
2916 xlate_action_set(ctx);
2919 /* Check if need to recirculate. */
2920 if (exit_recirculates(ctx)) {
2921 compose_recirculate_action(ctx);
2924 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
2925 * the learning action look at the packet, then drop it. */
2926 struct flow old_base_flow = ctx->base_flow;
2927 size_t old_size = ctx->xout->odp_actions->size;
2928 mirror_mask_t old_mirrors = ctx->xout->mirrors;
2930 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2931 ctx->xout->mirrors = old_mirrors;
2932 ctx->base_flow = old_base_flow;
2933 ctx->xout->odp_actions->size = old_size;
2935 /* Undo changes that may have been done for recirculation. */
2936 if (exit_recirculates(ctx)) {
2937 ctx->action_set.size = ctx->recirc_action_offset;
2938 ctx->recirc_action_offset = -1;
2939 ctx->last_unroll_offset = -1;
2944 ctx->xin->flow = old_flow;
2945 ctx->xbridge = xport->xbridge;
2946 ofpbuf_uninit(&ctx->action_set);
2947 ctx->action_set = old_action_set;
2948 ofpbuf_uninit(&ctx->stack);
2949 ctx->stack = old_stack;
2951 /* Restore calling bridge's lookup version. */
2952 ctx->tables_version = old_version;
2954 /* The peer bridge popping MPLS should have no effect on the original
2956 ctx->was_mpls = old_was_mpls;
2958 /* The fact that the peer bridge exits (for any reason) does not mean
2959 * that the original bridge should exit. Specifically, if the peer
2960 * bridge recirculates (which typically modifies the packet), the
2961 * original bridge must continue processing with the original, not the
2962 * recirculated packet! */
2965 if (ctx->xin->resubmit_stats) {
2966 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2967 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
2969 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
2972 if (ctx->xin->xcache) {
2973 struct xc_entry *entry;
2975 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2976 entry->u.dev.tx = netdev_ref(xport->netdev);
2977 entry->u.dev.rx = netdev_ref(peer->netdev);
2978 entry->u.dev.bfd = bfd_ref(peer->bfd);
2983 flow_vlan_tci = flow->vlan_tci;
2984 flow_pkt_mark = flow->pkt_mark;
2985 flow_nw_tos = flow->nw_tos;
2987 if (count_skb_priorities(xport)) {
2988 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
2989 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
2990 wc->masks.nw_tos |= IP_DSCP_MASK;
2991 flow->nw_tos &= ~IP_DSCP_MASK;
2992 flow->nw_tos |= dscp;
2996 if (xport->is_tunnel) {
2997 /* Save tunnel metadata so that changes made due to
2998 * the Logical (tunnel) Port are not visible for any further
2999 * matches, while explicit set actions on tunnel metadata are.
3001 flow_tnl = flow->tunnel;
3002 odp_port = tnl_port_send(xport->ofport, flow, ctx->wc);
3003 if (odp_port == ODPP_NONE) {
3004 xlate_report(ctx, "Tunneling decided against output");
3005 goto out; /* restore flow_nw_tos */
3007 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
3008 xlate_report(ctx, "Not tunneling to our own address");
3009 goto out; /* restore flow_nw_tos */
3011 if (ctx->xin->resubmit_stats) {
3012 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
3014 if (ctx->xin->xcache) {
3015 struct xc_entry *entry;
3017 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
3018 entry->u.dev.tx = netdev_ref(xport->netdev);
3020 out_port = odp_port;
3021 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3022 xlate_report(ctx, "output to native tunnel");
3023 tnl_push_pop_send = true;
3025 xlate_report(ctx, "output to kernel tunnel");
3026 commit_odp_tunnel_action(flow, &ctx->base_flow,
3027 ctx->xout->odp_actions);
3028 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3031 odp_port = xport->odp_port;
3032 out_port = odp_port;
3033 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
3034 ofp_port_t vlandev_port;
3036 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
3037 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
3038 ofp_port, flow->vlan_tci);
3039 if (vlandev_port != ofp_port) {
3040 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
3041 flow->vlan_tci = htons(0);
3046 if (out_port != ODPP_NONE) {
3047 bool use_masked = ctx->xbridge->support.masked_set_action;
3049 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
3050 ctx->xout->odp_actions,
3054 struct ovs_action_hash *act_hash;
3057 act_hash = nl_msg_put_unspec_uninit(ctx->xout->odp_actions,
3058 OVS_ACTION_ATTR_HASH,
3060 act_hash->hash_alg = xr->hash_alg;
3061 act_hash->hash_basis = xr->hash_basis;
3063 /* Recirc action. */
3064 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC,
3068 if (tnl_push_pop_send) {
3069 build_tunnel_send(ctx, xport, flow, odp_port);
3070 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3072 odp_port_t odp_tnl_port = ODPP_NONE;
3074 /* XXX: Write better Filter for tunnel port. We can use inport
3075 * int tunnel-port flow to avoid these checks completely. */
3076 if (ofp_port == OFPP_LOCAL &&
3077 ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3079 odp_tnl_port = tnl_port_map_lookup(flow, wc);
3082 if (odp_tnl_port != ODPP_NONE) {
3083 nl_msg_put_odp_port(ctx->xout->odp_actions,
3084 OVS_ACTION_ATTR_TUNNEL_POP,
3087 /* Tunnel push-pop action is not compatible with
3089 add_ipfix_output_action(ctx, out_port);
3090 nl_msg_put_odp_port(ctx->xout->odp_actions,
3091 OVS_ACTION_ATTR_OUTPUT,
3097 ctx->sflow_odp_port = odp_port;
3098 ctx->sflow_n_outputs++;
3099 ctx->xout->nf_output_iface = ofp_port;
3104 flow->vlan_tci = flow_vlan_tci;
3105 flow->pkt_mark = flow_pkt_mark;
3106 flow->nw_tos = flow_nw_tos;
3110 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port,
3111 const struct xlate_bond_recirc *xr)
3113 compose_output_action__(ctx, ofp_port, xr, true);
3117 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
3119 struct rule_dpif *old_rule = ctx->rule;
3120 ovs_be64 old_cookie = ctx->rule_cookie;
3121 const struct rule_actions *actions;
3123 if (ctx->xin->resubmit_stats) {
3124 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
3130 ctx->rule_cookie = rule_dpif_get_flow_cookie(rule);
3131 actions = rule_dpif_get_actions(rule);
3132 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
3133 ctx->rule_cookie = old_cookie;
3134 ctx->rule = old_rule;
3139 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
3141 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3143 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
3144 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
3145 MAX_RESUBMIT_RECURSION);
3146 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
3147 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
3148 } else if (ctx->xout->odp_actions->size > UINT16_MAX) {
3149 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
3150 } else if (ctx->stack.size >= 65536) {
3151 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
3160 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
3161 bool may_packet_in, bool honor_table_miss)
3163 /* Check if we need to recirculate before matching in a table. */
3164 if (ctx->was_mpls) {
3165 ctx_trigger_recirculation(ctx);
3168 if (xlate_resubmit_resource_check(ctx)) {
3169 uint8_t old_table_id = ctx->table_id;
3170 struct rule_dpif *rule;
3172 ctx->table_id = table_id;
3174 rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
3175 ctx->tables_version,
3176 &ctx->xin->flow, ctx->xin->wc,
3177 ctx->xin->xcache != NULL,
3178 ctx->xin->resubmit_stats,
3179 &ctx->table_id, in_port,
3180 may_packet_in, honor_table_miss);
3182 if (OVS_UNLIKELY(ctx->xin->resubmit_hook)) {
3183 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse + 1);
3187 /* Fill in the cache entry here instead of xlate_recursively
3188 * to make the reference counting more explicit. We take a
3189 * reference in the lookups above if we are going to cache the
3191 if (ctx->xin->xcache) {
3192 struct xc_entry *entry;
3194 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
3195 entry->u.rule = rule;
3197 xlate_recursively(ctx, rule);
3200 ctx->table_id = old_table_id;
3208 xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
3209 struct ofputil_bucket *bucket)
3211 if (ctx->xin->resubmit_stats) {
3212 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
3214 if (ctx->xin->xcache) {
3215 struct xc_entry *entry;
3217 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
3218 entry->u.group.group = group_dpif_ref(group);
3219 entry->u.group.bucket = bucket;
3224 xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
3226 uint64_t action_list_stub[1024 / 8];
3227 struct ofpbuf action_list, action_set;
3228 struct flow old_flow = ctx->xin->flow;
3229 bool old_was_mpls = ctx->was_mpls;
3231 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
3232 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3234 ofpacts_execute_action_set(&action_list, &action_set);
3236 do_xlate_actions(action_list.data, action_list.size, ctx);
3239 ofpbuf_uninit(&action_set);
3240 ofpbuf_uninit(&action_list);
3242 /* Check if need to recirculate. */
3243 if (exit_recirculates(ctx)) {
3244 compose_recirculate_action(ctx);
3247 /* Roll back flow to previous state.
3248 * This is equivalent to cloning the packet for each bucket.
3250 * As a side effect any subsequently applied actions will
3251 * also effectively be applied to a clone of the packet taken
3252 * just before applying the all or indirect group.
3254 * Note that group buckets are action sets, hence they cannot modify the
3255 * main action set. Also any stack actions are ignored when executing an
3256 * action set, so group buckets cannot change the stack either.
3257 * However, we do allow resubmit actions in group buckets, which could
3258 * break the above assumptions. It is up to the controller to not mess up
3259 * with the action_set and stack in the tables resubmitted to from
3261 ctx->xin->flow = old_flow;
3263 /* The group bucket popping MPLS should have no effect after bucket
3265 ctx->was_mpls = old_was_mpls;
3267 /* The fact that the group bucket exits (for any reason) does not mean that
3268 * the translation after the group action should exit. Specifically, if
3269 * the group bucket recirculates (which typically modifies the packet), the
3270 * actions after the group action must continue processing with the
3271 * original, not the recirculated packet! */
3276 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3278 struct ofputil_bucket *bucket;
3279 const struct ovs_list *buckets;
3281 group_dpif_get_buckets(group, &buckets);
3283 LIST_FOR_EACH (bucket, list_node, buckets) {
3284 xlate_group_bucket(ctx, bucket);
3286 xlate_group_stats(ctx, group, NULL);
3290 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3292 struct ofputil_bucket *bucket;
3294 bucket = group_first_live_bucket(ctx, group, 0);
3296 xlate_group_bucket(ctx, bucket);
3297 xlate_group_stats(ctx, group, bucket);
3302 xlate_default_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3304 struct flow_wildcards *wc = ctx->wc;
3305 struct ofputil_bucket *bucket;
3308 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
3309 flow_mask_hash_fields(&ctx->xin->flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
3310 bucket = group_best_live_bucket(ctx, group, basis);
3312 xlate_group_bucket(ctx, bucket);
3313 xlate_group_stats(ctx, group, bucket);
3318 xlate_hash_fields_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3320 struct mf_bitmap hash_fields = MF_BITMAP_INITIALIZER;
3321 const struct field_array *fields;
3322 struct ofputil_bucket *bucket;
3326 fields = group_dpif_get_fields(group);
3327 basis = hash_uint64(group_dpif_get_selection_method_param(group));
3329 /* Determine which fields to hash */
3330 for (i = 0; i < MFF_N_IDS; i++) {
3331 if (bitmap_is_set(fields->used.bm, i)) {
3332 const struct mf_field *mf;
3334 /* If the field is already present in 'hash_fields' then
3335 * this loop has already checked that it and its pre-requisites
3336 * are present in the flow and its pre-requisites have
3337 * already been added to 'hash_fields'. There is nothing more
3338 * to do here and as an optimisation the loop can continue. */
3339 if (bitmap_is_set(hash_fields.bm, i)) {
3345 /* Only hash a field if it and its pre-requisites are present
3347 if (!mf_are_prereqs_ok(mf, &ctx->xin->flow)) {
3351 /* Hash both the field and its pre-requisites */
3352 mf_bitmap_set_field_and_prereqs(mf, &hash_fields);
3356 /* Hash the fields */
3357 for (i = 0; i < MFF_N_IDS; i++) {
3358 if (bitmap_is_set(hash_fields.bm, i)) {
3359 const struct mf_field *mf = mf_from_id(i);
3360 union mf_value value;
3363 mf_get_value(mf, &ctx->xin->flow, &value);
3364 /* This seems inefficient but so does apply_mask() */
3365 for (j = 0; j < mf->n_bytes; j++) {
3366 ((uint8_t *) &value)[j] &= ((uint8_t *) &fields->value[i])[j];
3368 basis = hash_bytes(&value, mf->n_bytes, basis);
3370 mf_mask_field(mf, &ctx->wc->masks);
3374 bucket = group_best_live_bucket(ctx, group, basis);
3376 xlate_group_bucket(ctx, bucket);
3377 xlate_group_stats(ctx, group, bucket);
3382 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3384 const char *selection_method = group_dpif_get_selection_method(group);
3386 if (selection_method[0] == '\0') {
3387 xlate_default_select_group(ctx, group);
3388 } else if (!strcasecmp("hash", selection_method)) {
3389 xlate_hash_fields_select_group(ctx, group);
3391 /* Parsing of groups should ensure this never happens */
3397 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3399 bool was_in_group = ctx->in_group;
3400 ctx->in_group = true;
3402 switch (group_dpif_get_type(group)) {
3404 case OFPGT11_INDIRECT:
3405 xlate_all_group(ctx, group);
3407 case OFPGT11_SELECT:
3408 xlate_select_group(ctx, group);
3411 xlate_ff_group(ctx, group);
3416 group_dpif_unref(group);
3418 ctx->in_group = was_in_group;
3422 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3424 if (xlate_resubmit_resource_check(ctx)) {
3425 struct group_dpif *group;
3428 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
3430 xlate_group_action__(ctx, group);
3440 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
3441 const struct ofpact_resubmit *resubmit)
3445 bool may_packet_in = false;
3446 bool honor_table_miss = false;
3448 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
3449 /* Still allow missed packets to be sent to the controller
3450 * if resubmitting from an internal table. */
3451 may_packet_in = true;
3452 honor_table_miss = true;
3455 in_port = resubmit->in_port;
3456 if (in_port == OFPP_IN_PORT) {
3457 in_port = ctx->xin->flow.in_port.ofp_port;
3460 table_id = resubmit->table_id;
3461 if (table_id == 255) {
3462 table_id = ctx->table_id;
3465 xlate_table_action(ctx, in_port, table_id, may_packet_in,
3470 flood_packets(struct xlate_ctx *ctx, bool all)
3472 const struct xport *xport;
3474 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
3475 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
3480 compose_output_action__(ctx, xport->ofp_port, NULL, false);
3481 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
3482 compose_output_action(ctx, xport->ofp_port, NULL);
3486 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3490 execute_controller_action(struct xlate_ctx *ctx, int len,
3491 enum ofp_packet_in_reason reason,
3492 uint16_t controller_id)
3494 struct ofproto_packet_in *pin;
3495 struct dp_packet *packet;
3498 ctx->xout->slow |= SLOW_CONTROLLER;
3499 if (!ctx->xin->packet) {
3503 packet = dp_packet_clone(ctx->xin->packet);
3505 use_masked = ctx->xbridge->support.masked_set_action;
3506 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3507 ctx->xout->odp_actions,
3508 ctx->wc, use_masked);
3510 odp_execute_actions(NULL, &packet, 1, false,
3511 ctx->xout->odp_actions->data,
3512 ctx->xout->odp_actions->size, NULL);
3514 pin = xmalloc(sizeof *pin);
3515 pin->up.packet_len = dp_packet_size(packet);
3516 pin->up.packet = dp_packet_steal_data(packet);
3517 pin->up.reason = reason;
3518 pin->up.table_id = ctx->table_id;
3519 pin->up.cookie = ctx->rule_cookie;
3521 flow_get_metadata(&ctx->xin->flow, &pin->up.flow_metadata);
3523 pin->controller_id = controller_id;
3524 pin->send_len = len;
3525 /* If a rule is a table-miss rule then this is
3526 * a table-miss handled by a table-miss rule.
3528 * Else, if rule is internal and has a controller action,
3529 * the later being implied by the rule being processed here,
3530 * then this is a table-miss handled without a table-miss rule.
3532 * Otherwise this is not a table-miss. */
3533 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
3535 if (rule_dpif_is_table_miss(ctx->rule)) {
3536 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
3537 } else if (rule_dpif_is_internal(ctx->rule)) {
3538 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
3541 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
3542 dp_packet_delete(packet);
3545 /* Called only when ctx->recirc_action_offset is set. */
3547 compose_recirculate_action(struct xlate_ctx *ctx)
3549 struct recirc_metadata md;
3553 use_masked = ctx->xbridge->support.masked_set_action;
3554 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3555 ctx->xout->odp_actions,
3556 ctx->wc, use_masked);
3558 recirc_metadata_from_flow(&md, &ctx->xin->flow);
3560 ovs_assert(ctx->recirc_action_offset >= 0);
3562 /* Only allocate recirculation ID if we have a packet. */
3563 if (ctx->xin->packet) {
3564 /* Allocate a unique recirc id for the given metadata state in the
3565 * flow. The life-cycle of this recirc id is managed by associating it
3566 * with the udpif key ('ukey') created for each new datapath flow. */
3567 id = recirc_alloc_id_ctx(ctx->xbridge->ofproto, 0, &md, &ctx->stack,
3568 ctx->recirc_action_offset,
3569 ctx->action_set.size, ctx->action_set.data);
3571 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3572 VLOG_ERR_RL(&rl, "Failed to allocate recirculation id");
3575 xlate_out_add_recirc(ctx->xout, id);
3577 /* Look up an existing recirc id for the given metadata state in the
3578 * flow. No new reference is taken, as the ID is RCU protected and is
3579 * only required temporarily for verification. */
3580 id = recirc_find_id(ctx->xbridge->ofproto, 0, &md, &ctx->stack,
3581 ctx->recirc_action_offset,
3582 ctx->action_set.size, ctx->action_set.data);
3583 /* We let zero 'id' to be used in the RECIRC action below, which will
3584 * fail all revalidations as zero is not a valid recirculation ID. */
3587 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
3589 /* Undo changes done by recirculation. */
3590 ctx->action_set.size = ctx->recirc_action_offset;
3591 ctx->recirc_action_offset = -1;
3592 ctx->last_unroll_offset = -1;
3596 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
3598 struct flow *flow = &ctx->xin->flow;
3601 ovs_assert(eth_type_mpls(mpls->ethertype));
3603 n = flow_count_mpls_labels(flow, ctx->wc);
3605 bool use_masked = ctx->xbridge->support.masked_set_action;
3607 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
3608 ctx->xout->odp_actions,
3609 ctx->wc, use_masked);
3610 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3611 if (ctx->xin->packet != NULL) {
3612 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3613 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3614 "MPLS push action can't be performed as it would "
3615 "have more MPLS LSEs than the %d supported.",
3616 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3622 flow_push_mpls(flow, n, mpls->ethertype, ctx->wc);
3626 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
3628 struct flow *flow = &ctx->xin->flow;
3629 int n = flow_count_mpls_labels(flow, ctx->wc);
3631 if (flow_pop_mpls(flow, n, eth_type, ctx->wc)) {
3632 if (ctx->xbridge->support.odp.recirc) {
3633 ctx->was_mpls = true;
3635 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3636 if (ctx->xin->packet != NULL) {
3637 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3638 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3639 "MPLS pop action can't be performed as it has "
3640 "more MPLS LSEs than the %d supported.",
3641 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3644 ofpbuf_clear(ctx->xout->odp_actions);
3649 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3651 struct flow *flow = &ctx->xin->flow;
3653 if (!is_ip_any(flow)) {
3657 ctx->wc->masks.nw_ttl = 0xff;
3658 if (flow->nw_ttl > 1) {
3664 for (i = 0; i < ids->n_controllers; i++) {
3665 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3669 /* Stop processing for current table. */
3675 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3677 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3678 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
3679 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
3684 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3686 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3687 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
3688 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
3693 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
3695 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3696 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3697 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
3702 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
3704 struct flow *flow = &ctx->xin->flow;
3706 if (eth_type_mpls(flow->dl_type)) {
3707 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
3709 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3712 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3715 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
3719 /* Stop processing for current table. */
3724 xlate_output_action(struct xlate_ctx *ctx,
3725 ofp_port_t port, uint16_t max_len, bool may_packet_in)
3727 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
3729 ctx->xout->nf_output_iface = NF_OUT_DROP;
3733 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port, NULL);
3736 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3737 0, may_packet_in, true);
3743 flood_packets(ctx, false);
3746 flood_packets(ctx, true);
3748 case OFPP_CONTROLLER:
3749 execute_controller_action(ctx, max_len,
3750 (ctx->in_group ? OFPR_GROUP
3751 : ctx->in_action_set ? OFPR_ACTION_SET
3759 if (port != ctx->xin->flow.in_port.ofp_port) {
3760 compose_output_action(ctx, port, NULL);
3762 xlate_report(ctx, "skipping output to input port");
3767 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3768 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3769 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3770 ctx->xout->nf_output_iface = prev_nf_output_iface;
3771 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3772 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3773 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3778 xlate_output_reg_action(struct xlate_ctx *ctx,
3779 const struct ofpact_output_reg *or)
3781 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3782 if (port <= UINT16_MAX) {
3783 union mf_subvalue value;
3785 memset(&value, 0xff, sizeof value);
3786 mf_write_subfield_flow(&or->src, &value, &ctx->wc->masks);
3787 xlate_output_action(ctx, u16_to_ofp(port),
3788 or->max_len, false);
3793 xlate_enqueue_action(struct xlate_ctx *ctx,
3794 const struct ofpact_enqueue *enqueue)
3796 ofp_port_t ofp_port = enqueue->port;
3797 uint32_t queue_id = enqueue->queue;
3798 uint32_t flow_priority, priority;
3801 /* Translate queue to priority. */
3802 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
3804 /* Fall back to ordinary output action. */
3805 xlate_output_action(ctx, enqueue->port, 0, false);
3809 /* Check output port. */
3810 if (ofp_port == OFPP_IN_PORT) {
3811 ofp_port = ctx->xin->flow.in_port.ofp_port;
3812 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
3816 /* Add datapath actions. */
3817 flow_priority = ctx->xin->flow.skb_priority;
3818 ctx->xin->flow.skb_priority = priority;
3819 compose_output_action(ctx, ofp_port, NULL);
3820 ctx->xin->flow.skb_priority = flow_priority;
3822 /* Update NetFlow output port. */
3823 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3824 ctx->xout->nf_output_iface = ofp_port;
3825 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3826 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3831 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3833 uint32_t skb_priority;
3835 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
3836 ctx->xin->flow.skb_priority = skb_priority;
3838 /* Couldn't translate queue to a priority. Nothing to do. A warning
3839 * has already been logged. */
3844 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
3846 const struct xbridge *xbridge = xbridge_;
3857 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3860 port = get_ofp_port(xbridge, ofp_port);
3861 return port ? port->may_enable : false;
3866 xlate_bundle_action(struct xlate_ctx *ctx,
3867 const struct ofpact_bundle *bundle)
3871 port = bundle_execute(bundle, &ctx->xin->flow, ctx->wc, slave_enabled_cb,
3872 CONST_CAST(struct xbridge *, ctx->xbridge));
3873 if (bundle->dst.field) {
3874 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow, ctx->wc);
3876 xlate_output_action(ctx, port, 0, false);
3881 xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
3882 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
3884 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
3885 if (ctx->xin->may_learn) {
3886 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
3891 xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
3893 ctx->xout->has_learn = true;
3894 learn_mask(learn, ctx->wc);
3896 if (ctx->xin->xcache) {
3897 struct xc_entry *entry;
3899 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
3900 entry->u.learn.ofproto = ctx->xbridge->ofproto;
3901 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
3902 entry->u.learn.ofpacts = ofpbuf_new(64);
3903 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
3904 entry->u.learn.ofpacts);
3905 } else if (ctx->xin->may_learn) {
3906 uint64_t ofpacts_stub[1024 / 8];
3907 struct ofputil_flow_mod fm;
3908 struct ofpbuf ofpacts;
3910 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3911 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
3912 ofpbuf_uninit(&ofpacts);
3917 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
3918 uint16_t idle_timeout, uint16_t hard_timeout)
3920 if (tcp_flags & (TCP_FIN | TCP_RST)) {
3921 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
3926 xlate_fin_timeout(struct xlate_ctx *ctx,
3927 const struct ofpact_fin_timeout *oft)
3930 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
3931 oft->fin_idle_timeout, oft->fin_hard_timeout);
3932 if (ctx->xin->xcache) {
3933 struct xc_entry *entry;
3935 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
3936 /* XC_RULE already holds a reference on the rule, none is taken
3938 entry->u.fin.rule = ctx->rule;
3939 entry->u.fin.idle = oft->fin_idle_timeout;
3940 entry->u.fin.hard = oft->fin_hard_timeout;
3946 xlate_sample_action(struct xlate_ctx *ctx,
3947 const struct ofpact_sample *os)
3949 union user_action_cookie cookie;
3950 /* Scale the probability from 16-bit to 32-bit while representing
3951 * the same percentage. */
3952 uint32_t probability = (os->probability << 16) | os->probability;
3955 if (!ctx->xbridge->support.variable_length_userdata) {
3956 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3958 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
3959 "lacks support (needs Linux 3.10+ or kernel module from "
3964 use_masked = ctx->xbridge->support.masked_set_action;
3965 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3966 ctx->xout->odp_actions,
3967 ctx->wc, use_masked);
3969 compose_flow_sample_cookie(os->probability, os->collector_set_id,
3970 os->obs_domain_id, os->obs_point_id, &cookie);
3971 compose_sample_action(ctx->xbridge, ctx->xout->odp_actions,
3972 &ctx->xin->flow, probability, &cookie,
3973 sizeof cookie.flow_sample, ODPP_NONE,
3978 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
3980 if (xport->config & (is_stp(&ctx->xin->flow)
3981 ? OFPUTIL_PC_NO_RECV_STP
3982 : OFPUTIL_PC_NO_RECV)) {
3986 /* Only drop packets here if both forwarding and learning are
3987 * disabled. If just learning is enabled, we need to have
3988 * OFPP_NORMAL and the learning action have a look at the packet
3989 * before we can drop it. */
3990 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
3991 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
3999 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
4001 const struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
4002 size_t on_len = ofpact_nest_get_action_len(on);
4003 const struct ofpact *inner;
4005 /* Maintain actset_output depending on the contents of the action set:
4007 * - OFPP_UNSET, if there is no "output" action.
4009 * - The output port, if there is an "output" action and no "group"
4012 * - OFPP_UNSET, if there is a "group" action.
4014 if (!ctx->action_set_has_group) {
4015 OFPACT_FOR_EACH (inner, on->actions, on_len) {
4016 if (inner->type == OFPACT_OUTPUT) {
4017 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(inner)->port;
4018 } else if (inner->type == OFPACT_GROUP) {
4019 ctx->xin->flow.actset_output = OFPP_UNSET;
4020 ctx->action_set_has_group = true;
4026 ofpbuf_put(&ctx->action_set, on->actions, on_len);
4027 ofpact_pad(&ctx->action_set);
4031 xlate_action_set(struct xlate_ctx *ctx)
4033 uint64_t action_list_stub[1024 / 64];
4034 struct ofpbuf action_list;
4036 ctx->in_action_set = true;
4037 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
4038 ofpacts_execute_action_set(&action_list, &ctx->action_set);
4039 /* Clear the action set, as it is not needed any more. */
4040 ofpbuf_clear(&ctx->action_set);
4041 do_xlate_actions(action_list.data, action_list.size, ctx);
4042 ctx->in_action_set = false;
4043 ofpbuf_uninit(&action_list);
4047 recirc_put_unroll_xlate(struct xlate_ctx *ctx)
4049 struct ofpact_unroll_xlate *unroll;
4051 unroll = ctx->last_unroll_offset < 0
4053 : ALIGNED_CAST(struct ofpact_unroll_xlate *,
4054 (char *)ctx->action_set.data + ctx->last_unroll_offset);
4056 /* Restore the table_id and rule cookie for a potential PACKET
4059 (ctx->table_id != unroll->rule_table_id
4060 || ctx->rule_cookie != unroll->rule_cookie)) {
4062 ctx->last_unroll_offset = ctx->action_set.size;
4063 unroll = ofpact_put_UNROLL_XLATE(&ctx->action_set);
4064 unroll->rule_table_id = ctx->table_id;
4065 unroll->rule_cookie = ctx->rule_cookie;
4070 /* Copy remaining actions to the action_set to be executed after recirculation.
4071 * UNROLL_XLATE action is inserted, if not already done so, before actions that
4072 * may generate PACKET_INs from the current table and without matching another
4075 recirc_unroll_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4076 struct xlate_ctx *ctx)
4078 const struct ofpact *a;
4080 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4082 /* May generate PACKET INs. */
4083 case OFPACT_OUTPUT_REG:
4086 case OFPACT_CONTROLLER:
4087 case OFPACT_DEC_MPLS_TTL:
4088 case OFPACT_DEC_TTL:
4089 recirc_put_unroll_xlate(ctx);
4092 /* These may not generate PACKET INs. */
4093 case OFPACT_SET_TUNNEL:
4094 case OFPACT_REG_MOVE:
4095 case OFPACT_SET_FIELD:
4096 case OFPACT_STACK_PUSH:
4097 case OFPACT_STACK_POP:
4099 case OFPACT_WRITE_METADATA:
4100 case OFPACT_RESUBMIT: /* May indirectly generate PACKET INs, */
4101 case OFPACT_GOTO_TABLE: /* but from a different table and rule. */
4102 case OFPACT_ENQUEUE:
4103 case OFPACT_SET_VLAN_VID:
4104 case OFPACT_SET_VLAN_PCP:
4105 case OFPACT_STRIP_VLAN:
4106 case OFPACT_PUSH_VLAN:
4107 case OFPACT_SET_ETH_SRC:
4108 case OFPACT_SET_ETH_DST:
4109 case OFPACT_SET_IPV4_SRC:
4110 case OFPACT_SET_IPV4_DST:
4111 case OFPACT_SET_IP_DSCP:
4112 case OFPACT_SET_IP_ECN:
4113 case OFPACT_SET_IP_TTL:
4114 case OFPACT_SET_L4_SRC_PORT:
4115 case OFPACT_SET_L4_DST_PORT:
4116 case OFPACT_SET_QUEUE:
4117 case OFPACT_POP_QUEUE:
4118 case OFPACT_PUSH_MPLS:
4119 case OFPACT_POP_MPLS:
4120 case OFPACT_SET_MPLS_LABEL:
4121 case OFPACT_SET_MPLS_TC:
4122 case OFPACT_SET_MPLS_TTL:
4123 case OFPACT_MULTIPATH:
4126 case OFPACT_UNROLL_XLATE:
4127 case OFPACT_FIN_TIMEOUT:
4128 case OFPACT_CLEAR_ACTIONS:
4129 case OFPACT_WRITE_ACTIONS:
4134 /* These need not be copied for restoration. */
4136 case OFPACT_CONJUNCTION:
4139 /* Copy the action over. */
4140 ofpbuf_put(&ctx->action_set, a, OFPACT_ALIGN(a->len));
4144 #define CHECK_MPLS_RECIRCULATION() \
4145 if (ctx->was_mpls) { \
4146 ctx_trigger_recirculation(ctx); \
4149 #define CHECK_MPLS_RECIRCULATION_IF(COND) \
4151 CHECK_MPLS_RECIRCULATION(); \
4155 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4156 struct xlate_ctx *ctx)
4158 struct flow_wildcards *wc = ctx->wc;
4159 struct flow *flow = &ctx->xin->flow;
4160 const struct ofpact *a;
4162 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
4163 tnl_arp_snoop(flow, wc, ctx->xbridge->name);
4165 /* dl_type already in the mask, not set below. */
4167 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4168 struct ofpact_controller *controller;
4169 const struct ofpact_metadata *metadata;
4170 const struct ofpact_set_field *set_field;
4171 const struct mf_field *mf;
4174 /* Check if need to store the remaining actions for later
4176 if (exit_recirculates(ctx)) {
4177 recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
4179 (uint8_t *)ofpacts)),
4187 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
4188 ofpact_get_OUTPUT(a)->max_len, true);
4192 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
4193 /* Group could not be found. */
4198 case OFPACT_CONTROLLER:
4199 controller = ofpact_get_CONTROLLER(a);
4200 execute_controller_action(ctx, controller->max_len,
4202 controller->controller_id);
4205 case OFPACT_ENQUEUE:
4206 memset(&wc->masks.skb_priority, 0xff,
4207 sizeof wc->masks.skb_priority);
4208 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
4211 case OFPACT_SET_VLAN_VID:
4212 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
4213 if (flow->vlan_tci & htons(VLAN_CFI) ||
4214 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
4215 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
4216 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
4221 case OFPACT_SET_VLAN_PCP:
4222 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
4223 if (flow->vlan_tci & htons(VLAN_CFI) ||
4224 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
4225 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
4226 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
4227 << VLAN_PCP_SHIFT) | VLAN_CFI);
4231 case OFPACT_STRIP_VLAN:
4232 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4233 flow->vlan_tci = htons(0);
4236 case OFPACT_PUSH_VLAN:
4237 /* XXX 802.1AD(QinQ) */
4238 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4239 flow->vlan_tci = htons(VLAN_CFI);
4242 case OFPACT_SET_ETH_SRC:
4243 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
4244 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
4247 case OFPACT_SET_ETH_DST:
4248 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
4249 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
4252 case OFPACT_SET_IPV4_SRC:
4253 CHECK_MPLS_RECIRCULATION();
4254 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4255 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
4256 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
4260 case OFPACT_SET_IPV4_DST:
4261 CHECK_MPLS_RECIRCULATION();
4262 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4263 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
4264 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
4268 case OFPACT_SET_IP_DSCP:
4269 CHECK_MPLS_RECIRCULATION();
4270 if (is_ip_any(flow)) {
4271 wc->masks.nw_tos |= IP_DSCP_MASK;
4272 flow->nw_tos &= ~IP_DSCP_MASK;
4273 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
4277 case OFPACT_SET_IP_ECN:
4278 CHECK_MPLS_RECIRCULATION();
4279 if (is_ip_any(flow)) {
4280 wc->masks.nw_tos |= IP_ECN_MASK;
4281 flow->nw_tos &= ~IP_ECN_MASK;
4282 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
4286 case OFPACT_SET_IP_TTL:
4287 CHECK_MPLS_RECIRCULATION();
4288 if (is_ip_any(flow)) {
4289 wc->masks.nw_ttl = 0xff;
4290 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
4294 case OFPACT_SET_L4_SRC_PORT:
4295 CHECK_MPLS_RECIRCULATION();
4296 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4297 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4298 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
4299 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
4303 case OFPACT_SET_L4_DST_PORT:
4304 CHECK_MPLS_RECIRCULATION();
4305 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4306 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4307 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
4308 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
4312 case OFPACT_RESUBMIT:
4313 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
4316 case OFPACT_SET_TUNNEL:
4317 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
4320 case OFPACT_SET_QUEUE:
4321 memset(&wc->masks.skb_priority, 0xff,
4322 sizeof wc->masks.skb_priority);
4323 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
4326 case OFPACT_POP_QUEUE:
4327 memset(&wc->masks.skb_priority, 0xff,
4328 sizeof wc->masks.skb_priority);
4329 flow->skb_priority = ctx->orig_skb_priority;
4332 case OFPACT_REG_MOVE:
4333 CHECK_MPLS_RECIRCULATION_IF(
4334 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
4335 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
4336 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
4339 case OFPACT_SET_FIELD:
4340 CHECK_MPLS_RECIRCULATION_IF(
4341 mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field));
4342 set_field = ofpact_get_SET_FIELD(a);
4343 mf = set_field->field;
4345 /* Set field action only ever overwrites packet's outermost
4346 * applicable header fields. Do nothing if no header exists. */
4347 if (mf->id == MFF_VLAN_VID) {
4348 wc->masks.vlan_tci |= htons(VLAN_CFI);
4349 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4352 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
4353 /* 'dl_type' is already unwildcarded. */
4354 && !eth_type_mpls(flow->dl_type)) {
4357 /* A flow may wildcard nw_frag. Do nothing if setting a trasport
4358 * header field on a packet that does not have them. */
4359 mf_mask_field_and_prereqs(mf, &wc->masks);
4360 if (mf_are_prereqs_ok(mf, flow)) {
4361 mf_set_flow_value_masked(mf, &set_field->value,
4362 &set_field->mask, flow);
4366 case OFPACT_STACK_PUSH:
4367 CHECK_MPLS_RECIRCULATION_IF(
4368 mf_is_l3_or_higher(ofpact_get_STACK_PUSH(a)->subfield.field));
4369 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
4373 case OFPACT_STACK_POP:
4374 CHECK_MPLS_RECIRCULATION_IF(
4375 mf_is_l3_or_higher(ofpact_get_STACK_POP(a)->subfield.field));
4376 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
4380 case OFPACT_PUSH_MPLS:
4381 /* Recirculate if it is an IP packet with a zero ttl. This may
4382 * indicate that the packet was previously MPLS and an MPLS pop
4383 * action converted it to IP. In this case recirculating should
4384 * reveal the IP TTL which is used as the basis for a new MPLS
4386 CHECK_MPLS_RECIRCULATION_IF(
4387 !flow_count_mpls_labels(flow, wc)
4388 && flow->nw_ttl == 0
4389 && is_ip_any(flow));
4390 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
4393 case OFPACT_POP_MPLS:
4394 CHECK_MPLS_RECIRCULATION();
4395 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
4398 case OFPACT_SET_MPLS_LABEL:
4399 CHECK_MPLS_RECIRCULATION();
4400 compose_set_mpls_label_action(
4401 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
4404 case OFPACT_SET_MPLS_TC:
4405 CHECK_MPLS_RECIRCULATION();
4406 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
4409 case OFPACT_SET_MPLS_TTL:
4410 CHECK_MPLS_RECIRCULATION();
4411 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
4414 case OFPACT_DEC_MPLS_TTL:
4415 CHECK_MPLS_RECIRCULATION();
4416 if (compose_dec_mpls_ttl_action(ctx)) {
4421 case OFPACT_DEC_TTL:
4422 CHECK_MPLS_RECIRCULATION();
4423 wc->masks.nw_ttl = 0xff;
4424 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
4430 /* Nothing to do. */
4433 case OFPACT_MULTIPATH:
4434 CHECK_MPLS_RECIRCULATION();
4435 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
4439 CHECK_MPLS_RECIRCULATION();
4440 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
4443 case OFPACT_OUTPUT_REG:
4444 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
4448 CHECK_MPLS_RECIRCULATION();
4449 xlate_learn_action(ctx, ofpact_get_LEARN(a));
4452 case OFPACT_CONJUNCTION: {
4453 /* A flow with a "conjunction" action represents part of a special
4454 * kind of "set membership match". Such a flow should not actually
4455 * get executed, but it could via, say, a "packet-out", even though
4456 * that wouldn't be useful. Log it to help debugging. */
4457 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4458 VLOG_INFO_RL(&rl, "executing no-op conjunction action");
4466 case OFPACT_UNROLL_XLATE: {
4467 struct ofpact_unroll_xlate *unroll = ofpact_get_UNROLL_XLATE(a);
4469 /* Restore translation context data that was stored earlier. */
4470 ctx->table_id = unroll->rule_table_id;
4471 ctx->rule_cookie = unroll->rule_cookie;
4474 case OFPACT_FIN_TIMEOUT:
4475 CHECK_MPLS_RECIRCULATION();
4476 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4477 ctx->xout->has_fin_timeout = true;
4478 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
4481 case OFPACT_CLEAR_ACTIONS:
4482 ofpbuf_clear(&ctx->action_set);
4483 ctx->xin->flow.actset_output = OFPP_UNSET;
4484 ctx->action_set_has_group = false;
4487 case OFPACT_WRITE_ACTIONS:
4488 xlate_write_actions(ctx, a);
4491 case OFPACT_WRITE_METADATA:
4492 metadata = ofpact_get_WRITE_METADATA(a);
4493 flow->metadata &= ~metadata->mask;
4494 flow->metadata |= metadata->metadata & metadata->mask;
4498 /* Not implemented yet. */
4501 case OFPACT_GOTO_TABLE: {
4502 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
4504 /* Allow ctx->table_id == TBL_INTERNAL, which will be greater
4505 * than ogt->table_id. This is to allow goto_table actions that
4506 * triggered recirculation: ctx->table_id will be TBL_INTERNAL
4507 * after recirculation. */
4508 ovs_assert(ctx->table_id == TBL_INTERNAL
4509 || ctx->table_id < ogt->table_id);
4510 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
4511 ogt->table_id, true, true);
4516 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
4520 /* Check if need to store this and the remaining actions for later
4522 if (ctx->exit && ctx_first_recirculation_action(ctx)) {
4523 recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
4525 (uint8_t *)ofpacts)),
4533 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
4534 const struct flow *flow, ofp_port_t in_port,
4535 struct rule_dpif *rule, uint16_t tcp_flags,
4536 const struct dp_packet *packet, struct flow_wildcards *wc)
4538 xin->ofproto = ofproto;
4540 xin->flow.in_port.ofp_port = in_port;
4541 xin->flow.actset_output = OFPP_UNSET;
4542 xin->packet = packet;
4543 xin->may_learn = packet != NULL;
4546 xin->ofpacts = NULL;
4547 xin->ofpacts_len = 0;
4548 xin->tcp_flags = tcp_flags;
4549 xin->resubmit_hook = NULL;
4550 xin->report_hook = NULL;
4551 xin->resubmit_stats = NULL;
4553 xin->odp_actions = NULL;
4555 /* Do recirc lookup. */
4556 xin->recirc = flow->recirc_id
4557 ? recirc_id_node_find(flow->recirc_id)
4562 xlate_out_uninit(struct xlate_out *xout)
4565 if (xout->odp_actions == &xout->odp_actions_buf) {
4566 ofpbuf_uninit(xout->odp_actions);
4568 xlate_out_free_recircs(xout);
4572 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
4573 * into datapath actions, using 'ctx', and discards the datapath actions. */
4575 xlate_actions_for_side_effects(struct xlate_in *xin)
4577 struct xlate_out xout;
4579 xlate_actions(xin, &xout);
4580 xlate_out_uninit(&xout);
4583 static struct skb_priority_to_dscp *
4584 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
4586 struct skb_priority_to_dscp *pdscp;
4589 hash = hash_int(skb_priority, 0);
4590 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
4591 if (pdscp->skb_priority == skb_priority) {
4599 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
4602 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
4603 *dscp = pdscp ? pdscp->dscp : 0;
4604 return pdscp != NULL;
4608 count_skb_priorities(const struct xport *xport)
4610 return hmap_count(&xport->skb_priorities);
4614 clear_skb_priorities(struct xport *xport)
4616 struct skb_priority_to_dscp *pdscp, *next;
4618 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
4619 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
4625 actions_output_to_local_port(const struct xlate_ctx *ctx)
4627 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
4628 const struct nlattr *a;
4631 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions->data,
4632 ctx->xout->odp_actions->size) {
4633 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
4634 && nl_attr_get_odp_port(a) == local_odp_port) {
4641 #if defined(__linux__)
4642 /* Returns the maximum number of packets that the Linux kernel is willing to
4643 * queue up internally to certain kinds of software-implemented ports, or the
4644 * default (and rarely modified) value if it cannot be determined. */
4646 netdev_max_backlog(void)
4648 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
4649 static int max_backlog = 1000; /* The normal default value. */
4651 if (ovsthread_once_start(&once)) {
4652 static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
4656 stream = fopen(filename, "r");
4658 VLOG_WARN("%s: open failed (%s)", filename, ovs_strerror(errno));
4660 if (fscanf(stream, "%d", &n) != 1) {
4661 VLOG_WARN("%s: read error", filename);
4662 } else if (n <= 100) {
4663 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
4669 ovsthread_once_done(&once);
4671 VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
4677 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
4680 count_output_actions(const struct ofpbuf *odp_actions)
4682 const struct nlattr *a;
4686 NL_ATTR_FOR_EACH_UNSAFE (a, left, odp_actions->data, odp_actions->size) {
4687 if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
4693 #endif /* defined(__linux__) */
4695 /* Returns true if 'odp_actions' contains more output actions than the datapath
4696 * can reliably handle in one go. On Linux, this is the value of the
4697 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
4698 * packets that the kernel is willing to queue up for processing while the
4699 * datapath is processing a set of actions. */
4701 too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
4704 return (odp_actions->size / NL_A_U32_SIZE > netdev_max_backlog()
4705 && count_output_actions(odp_actions) > netdev_max_backlog());
4707 /* OSes other than Linux might have similar limits, but we don't know how
4708 * to determine them.*/
4713 /* Translates the flow, actions, or rule in 'xin' into datapath actions in
4715 * The caller must take responsibility for eventually freeing 'xout', with
4716 * xlate_out_uninit(). */
4718 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
4720 *xout = (struct xlate_out) {
4724 .has_normal = false,
4725 .has_fin_timeout = false,
4726 .nf_output_iface = NF_OUT_DROP,
4731 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4732 struct xbridge *xbridge = xbridge_lookup(xcfg, xin->ofproto);
4737 struct flow *flow = &xin->flow;
4738 struct rule_dpif *rule = NULL;
4740 union mf_subvalue stack_stub[1024 / sizeof(union mf_subvalue)];
4741 uint64_t action_set_stub[1024 / 8];
4742 struct flow_wildcards scratch_wc;
4743 struct xlate_ctx ctx = {
4747 .orig_tunnel_ip_dst = flow->tunnel.ip_dst,
4749 .stack = OFPBUF_STUB_INITIALIZER(stack_stub),
4751 .wc = xin->wc ? xin->wc : &scratch_wc,
4756 .in_action_set = false,
4759 .rule_cookie = OVS_BE64_MAX,
4760 .orig_skb_priority = flow->skb_priority,
4761 .sflow_n_outputs = 0,
4762 .sflow_odp_port = 0,
4763 .user_cookie_offset = 0,
4766 .recirc_action_offset = -1,
4767 .last_unroll_offset = -1,
4771 .action_set_has_group = false,
4772 .action_set = OFPBUF_STUB_INITIALIZER(action_set_stub),
4774 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
4776 enum slow_path_reason special;
4777 const struct ofpact *ofpacts;
4778 struct xport *in_port;
4779 struct flow orig_flow;
4784 COVERAGE_INC(xlate_actions);
4786 /* Flow initialization rules:
4787 * - 'base_flow' must match the kernel's view of the packet at the
4788 * time that action processing starts. 'flow' represents any
4789 * transformations we wish to make through actions.
4790 * - By default 'base_flow' and 'flow' are the same since the input
4791 * packet matches the output before any actions are applied.
4792 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
4793 * of the received packet as seen by the kernel. If we later output
4794 * to another device without any modifications this will cause us to
4795 * insert a new tag since the original one was stripped off by the
4797 * - Tunnel metadata as received is retained in 'flow'. This allows
4798 * tunnel metadata matching also in later tables.
4799 * Since a kernel action for setting the tunnel metadata will only be
4800 * generated with actual tunnel output, changing the tunnel metadata
4801 * values in 'flow' (such as tun_id) will only have effect with a later
4802 * tunnel output action.
4803 * - Tunnel 'base_flow' is completely cleared since that is what the
4804 * kernel does. If we wish to maintain the original values an action
4805 * needs to be generated. */
4807 xout->odp_actions = xin->odp_actions;
4808 if (!xout->odp_actions) {
4809 xout->odp_actions = &xout->odp_actions_buf;
4810 ofpbuf_use_stub(xout->odp_actions, xout->odp_actions_stub,
4811 sizeof xout->odp_actions_stub);
4813 ofpbuf_reserve(xout->odp_actions, NL_A_U32_SIZE);
4816 flow_wildcards_init_catchall(ctx.wc);
4817 memset(&ctx.wc->masks.in_port, 0xff, sizeof ctx.wc->masks.in_port);
4818 memset(&ctx.wc->masks.dl_type, 0xff, sizeof ctx.wc->masks.dl_type);
4819 if (is_ip_any(flow)) {
4820 ctx.wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
4822 if (xbridge->support.odp.recirc) {
4823 /* Always exactly match recirc_id when datapath supports
4825 ctx.wc->masks.recirc_id = UINT32_MAX;
4827 if (xbridge->netflow) {
4828 netflow_mask_wc(flow, ctx.wc);
4831 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
4833 tnl_may_send = tnl_xlate_init(flow, xin->wc);
4835 /* The in_port of the original packet before recirculation. */
4836 in_port = get_ofp_port(xbridge, flow->in_port.ofp_port);
4839 const struct recirc_id_node *recirc = xin->recirc;
4841 if (xin->ofpacts_len > 0 || ctx.rule) {
4842 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4844 VLOG_WARN_RL(&rl, "Recirculation conflict (%s)!",
4845 xin->ofpacts_len > 0
4851 /* Set the bridge for post-recirculation processing if needed. */
4852 if (ctx.xbridge->ofproto != recirc->ofproto) {
4853 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4854 const struct xbridge *new_bridge = xbridge_lookup(xcfg,
4857 if (OVS_UNLIKELY(!new_bridge)) {
4858 /* Drop the packet if the bridge cannot be found. */
4859 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4860 VLOG_WARN_RL(&rl, "Recirculation bridge no longer exists.");
4863 ctx.xbridge = new_bridge;
4866 /* Set the post-recirculation table id. Note: A table lookup is done
4867 * only if there are no post-recirculation actions. */
4868 ctx.table_id = recirc->table_id;
4870 /* Restore pipeline metadata. May change flow's in_port and other
4871 * metadata to the values that existed when recirculation was
4873 recirc_metadata_to_flow(&recirc->metadata, flow);
4875 /* Restore stack, if any. */
4876 if (recirc->stack) {
4877 ofpbuf_put(&ctx.stack, recirc->stack->data, recirc->stack->size);
4880 /* Restore action set, if any. */
4881 if (recirc->action_set_len) {
4882 const struct ofpact *a;
4884 ofpbuf_put(&ctx.action_set, recirc->ofpacts,
4885 recirc->action_set_len);
4887 OFPACT_FOR_EACH(a, recirc->ofpacts, recirc->action_set_len) {
4888 if (a->type == OFPACT_GROUP) {
4889 ctx.action_set_has_group = true;
4895 /* Restore recirculation actions. If there are no actions, processing
4896 * will start with a lookup in the table set above. */
4897 if (recirc->ofpacts_len > recirc->action_set_len) {
4898 xin->ofpacts_len = recirc->ofpacts_len - recirc->action_set_len;
4899 xin->ofpacts = recirc->ofpacts +
4900 recirc->action_set_len / sizeof *recirc->ofpacts;
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 rule = rule_dpif_lookup_from_table(ctx.xbridge->ofproto,
4914 ctx.tables_version, flow, xin->wc,
4915 ctx.xin->xcache != NULL,
4916 ctx.xin->resubmit_stats,
4918 flow->in_port.ofp_port, true, true);
4919 if (ctx.xin->resubmit_stats) {
4920 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
4922 if (ctx.xin->xcache) {
4923 struct xc_entry *entry;
4925 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
4926 entry->u.rule = rule;
4930 if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
4931 ctx.xin->resubmit_hook(ctx.xin, rule, 0);
4934 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
4937 ofpacts = xin->ofpacts;
4938 ofpacts_len = xin->ofpacts_len;
4939 } else if (ctx.rule) {
4940 const struct rule_actions *actions = rule_dpif_get_actions(ctx.rule);
4942 ofpacts = actions->ofpacts;
4943 ofpacts_len = actions->ofpacts_len;
4945 ctx.rule_cookie = rule_dpif_get_flow_cookie(ctx.rule);
4950 if (mbridge_has_mirrors(xbridge->mbridge)) {
4951 /* Do this conditionally because the copy is expensive enough that it
4952 * shows up in profiles. */
4956 /* Tunnel stats only for non-recirculated packets. */
4957 if (!xin->recirc && in_port && in_port->is_tunnel) {
4958 if (ctx.xin->resubmit_stats) {
4959 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
4961 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
4964 if (ctx.xin->xcache) {
4965 struct xc_entry *entry;
4967 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
4968 entry->u.dev.rx = netdev_ref(in_port->netdev);
4969 entry->u.dev.bfd = bfd_ref(in_port->bfd);
4973 /* Do not perform special processing on recirculated packets,
4974 * as recirculated packets are not really received by the bridge. */
4976 (special = process_special(&ctx, flow, in_port, ctx.xin->packet))) {
4977 ctx.xout->slow |= special;
4979 size_t sample_actions_len;
4981 if (flow->in_port.ofp_port
4982 != vsp_realdev_to_vlandev(xbridge->ofproto,
4983 flow->in_port.ofp_port,
4985 ctx.base_flow.vlan_tci = 0;
4988 /* Sampling is done only for packets really received by the bridge. */
4990 add_sflow_action(&ctx);
4991 add_ipfix_action(&ctx);
4992 sample_actions_len = ctx.xout->odp_actions->size;
4994 sample_actions_len = 0;
4997 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
4998 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
5000 /* We've let OFPP_NORMAL and the learning action look at the
5001 * packet, so drop it now if forwarding is disabled. */
5002 if (in_port && (!xport_stp_forward_state(in_port) ||
5003 !xport_rstp_forward_state(in_port))) {
5004 /* Drop all actions added by do_xlate_actions() above. */
5005 ctx.xout->odp_actions->size = sample_actions_len;
5007 /* Undo changes that may have been done for recirculation. */
5008 if (exit_recirculates(&ctx)) {
5009 ctx.action_set.size = ctx.recirc_action_offset;
5010 ctx.recirc_action_offset = -1;
5011 ctx.last_unroll_offset = -1;
5013 } else if (ctx.action_set.size) {
5014 /* Translate action set only if not dropping the packet and
5015 * not recirculating. */
5016 if (!exit_recirculates(&ctx)) {
5017 xlate_action_set(&ctx);
5020 /* Check if need to recirculate. */
5021 if (exit_recirculates(&ctx)) {
5022 compose_recirculate_action(&ctx);
5026 /* Output only fully processed packets. */
5027 if (!exit_recirculates(&ctx)
5028 && xbridge->has_in_band
5029 && in_band_must_output_to_local_port(flow)
5030 && !actions_output_to_local_port(&ctx)) {
5031 compose_output_action(&ctx, OFPP_LOCAL, NULL);
5035 fix_sflow_action(&ctx);
5037 /* Only mirror fully processed packets. */
5038 if (!exit_recirculates(&ctx)
5039 && mbridge_has_mirrors(xbridge->mbridge)) {
5040 add_mirror_actions(&ctx, &orig_flow);
5044 if (nl_attr_oversized(ctx.xout->odp_actions->size)) {
5045 /* These datapath actions are too big for a Netlink attribute, so we
5046 * can't hand them to the kernel directly. dpif_execute() can execute
5047 * them one by one with help, so just mark the result as SLOW_ACTION to
5048 * prevent the flow from being installed. */
5049 COVERAGE_INC(xlate_actions_oversize);
5050 ctx.xout->slow |= SLOW_ACTION;
5051 } else if (too_many_output_actions(ctx.xout->odp_actions)) {
5052 COVERAGE_INC(xlate_actions_too_many_output);
5053 ctx.xout->slow |= SLOW_ACTION;
5056 /* Update mirror stats only for packets really received by the bridge. */
5057 if (!xin->recirc && mbridge_has_mirrors(xbridge->mbridge)) {
5058 if (ctx.xin->resubmit_stats) {
5059 mirror_update_stats(xbridge->mbridge, xout->mirrors,
5060 ctx.xin->resubmit_stats->n_packets,
5061 ctx.xin->resubmit_stats->n_bytes);
5063 if (ctx.xin->xcache) {
5064 struct xc_entry *entry;
5066 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_MIRROR);
5067 entry->u.mirror.mbridge = mbridge_ref(xbridge->mbridge);
5068 entry->u.mirror.mirrors = xout->mirrors;
5072 /* Do netflow only for packets really received by the bridge. */
5073 if (!xin->recirc && xbridge->netflow) {
5074 /* Only update netflow if we don't have controller flow. We don't
5075 * report NetFlow expiration messages for such facets because they
5076 * are just part of the control logic for the network, not real
5078 if (ofpacts_len == 0
5079 || ofpacts->type != OFPACT_CONTROLLER
5080 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
5081 if (ctx.xin->resubmit_stats) {
5082 netflow_flow_update(xbridge->netflow, flow,
5083 xout->nf_output_iface,
5084 ctx.xin->resubmit_stats);
5086 if (ctx.xin->xcache) {
5087 struct xc_entry *entry;
5089 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
5090 entry->u.nf.netflow = netflow_ref(xbridge->netflow);
5091 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
5092 entry->u.nf.iface = xout->nf_output_iface;
5097 ofpbuf_uninit(&ctx.stack);
5098 ofpbuf_uninit(&ctx.action_set);
5101 /* Clear the metadata and register wildcard masks, because we won't
5102 * use non-header fields as part of the cache. */
5103 flow_wildcards_clear_non_packet_fields(ctx.wc);
5105 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
5106 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
5107 * represent these fields. The datapath interface, on the other hand,
5108 * represents them with just 8 bits each. This means that if the high
5109 * 8 bits of the masks for these fields somehow become set, then they
5110 * will get chopped off by a round trip through the datapath, and
5111 * revalidation will spot that as an inconsistency and delete the flow.
5112 * Avoid the problem here by making sure that only the low 8 bits of
5113 * either field can be unwildcarded for ICMP.
5116 ctx.wc->masks.tp_src &= htons(UINT8_MAX);
5117 ctx.wc->masks.tp_dst &= htons(UINT8_MAX);
5119 /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
5120 if (ctx.wc->masks.vlan_tci) {
5121 ctx.wc->masks.vlan_tci |= htons(VLAN_CFI);
5126 /* Sends 'packet' out 'ofport'.
5127 * May modify 'packet'.
5128 * Returns 0 if successful, otherwise a positive errno value. */
5130 xlate_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
5132 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5133 struct xport *xport;
5134 struct ofpact_output output;
5137 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
5138 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
5139 flow_extract(packet, &flow);
5140 flow.in_port.ofp_port = OFPP_NONE;
5142 xport = xport_lookup(xcfg, ofport);
5146 output.port = xport->ofp_port;
5149 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
5150 &output.ofpact, sizeof output,
5154 struct xlate_cache *
5155 xlate_cache_new(void)
5157 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
5159 ofpbuf_init(&xcache->entries, 512);
5163 static struct xc_entry *
5164 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
5166 struct xc_entry *entry;
5168 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
5175 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
5177 if (entry->u.dev.tx) {
5178 netdev_vport_inc_tx(entry->u.dev.tx, stats);
5180 if (entry->u.dev.rx) {
5181 netdev_vport_inc_rx(entry->u.dev.rx, stats);
5183 if (entry->u.dev.bfd) {
5184 bfd_account_rx(entry->u.dev.bfd, stats);
5189 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
5191 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5192 struct xbridge *xbridge;
5193 struct xbundle *xbundle;
5194 struct flow_wildcards wc;
5196 xbridge = xbridge_lookup(xcfg, ofproto);
5201 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
5207 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
5210 /* Push stats and perform side effects of flow translation. */
5212 xlate_push_stats(struct xlate_cache *xcache,
5213 const struct dpif_flow_stats *stats)
5215 struct xc_entry *entry;
5216 struct ofpbuf entries = xcache->entries;
5217 uint8_t dmac[ETH_ADDR_LEN];
5219 if (!stats->n_packets) {
5223 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5224 switch (entry->type) {
5226 rule_dpif_credit_stats(entry->u.rule, stats);
5229 bond_account(entry->u.bond.bond, entry->u.bond.flow,
5230 entry->u.bond.vid, stats->n_bytes);
5233 xlate_cache_netdev(entry, stats);
5236 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
5237 entry->u.nf.iface, stats);
5240 mirror_update_stats(entry->u.mirror.mbridge,
5241 entry->u.mirror.mirrors,
5242 stats->n_packets, stats->n_bytes);
5245 ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
5248 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
5249 entry->u.normal.vlan);
5251 case XC_FIN_TIMEOUT:
5252 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
5253 entry->u.fin.idle, entry->u.fin.hard);
5256 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
5260 /* Lookup arp to avoid arp timeout. */
5261 tnl_arp_lookup(entry->u.tnl_arp_cache.br_name, entry->u.tnl_arp_cache.d_ip, dmac);
5270 xlate_dev_unref(struct xc_entry *entry)
5272 if (entry->u.dev.tx) {
5273 netdev_close(entry->u.dev.tx);
5275 if (entry->u.dev.rx) {
5276 netdev_close(entry->u.dev.rx);
5278 if (entry->u.dev.bfd) {
5279 bfd_unref(entry->u.dev.bfd);
5284 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
5286 netflow_flow_clear(netflow, flow);
5287 netflow_unref(netflow);
5292 xlate_cache_clear(struct xlate_cache *xcache)
5294 struct xc_entry *entry;
5295 struct ofpbuf entries;
5301 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5302 switch (entry->type) {
5304 rule_dpif_unref(entry->u.rule);
5307 free(entry->u.bond.flow);
5308 bond_unref(entry->u.bond.bond);
5311 xlate_dev_unref(entry);
5314 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
5317 mbridge_unref(entry->u.mirror.mbridge);
5320 free(entry->u.learn.fm);
5321 ofpbuf_delete(entry->u.learn.ofpacts);
5324 free(entry->u.normal.flow);
5326 case XC_FIN_TIMEOUT:
5327 /* 'u.fin.rule' is always already held as a XC_RULE, which
5328 * has already released it's reference above. */
5331 group_dpif_unref(entry->u.group.group);
5340 ofpbuf_clear(&xcache->entries);
5344 xlate_cache_delete(struct xlate_cache *xcache)
5346 xlate_cache_clear(xcache);
5347 ofpbuf_uninit(&xcache->entries);