1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. */
17 #include "ofproto/ofproto-dpif-xlate.h"
20 #include <arpa/inet.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
25 #include "tnl-arp-cache.h"
30 #include "byte-order.h"
34 #include "dp-packet.h"
36 #include "dynamic-string.h"
42 #include "mac-learning.h"
43 #include "mcast-snooping.h"
44 #include "meta-flow.h"
45 #include "multipath.h"
46 #include "netdev-vport.h"
49 #include "odp-execute.h"
50 #include "ofp-actions.h"
51 #include "ofproto/ofproto-dpif-ipfix.h"
52 #include "ofproto/ofproto-dpif-mirror.h"
53 #include "ofproto/ofproto-dpif-monitor.h"
54 #include "ofproto/ofproto-dpif-sflow.h"
55 #include "ofproto/ofproto-dpif.h"
56 #include "ofproto/ofproto-provider.h"
57 #include "ovs-router.h"
58 #include "tnl-ports.h"
60 #include "openvswitch/vlog.h"
62 COVERAGE_DEFINE(xlate_actions);
63 COVERAGE_DEFINE(xlate_actions_oversize);
64 COVERAGE_DEFINE(xlate_actions_too_many_output);
66 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
68 /* Maximum depth of flow table recursion (due to resubmit actions) in a
69 * flow translation. */
70 #define MAX_RESUBMIT_RECURSION 64
71 #define MAX_INTERNAL_RESUBMITS 1 /* Max resbmits allowed using rules in
74 /* Maximum number of resubmit actions in a flow translation, whether they are
75 * recursive or not. */
76 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
79 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
80 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
82 struct ovs_list xbundles; /* Owned xbundles. */
83 struct hmap xports; /* Indexed by ofp_port. */
85 char *name; /* Name used in log messages. */
86 struct dpif *dpif; /* Datapath interface. */
87 struct mac_learning *ml; /* Mac learning handle. */
88 struct mcast_snooping *ms; /* Multicast Snooping handle. */
89 struct mbridge *mbridge; /* Mirroring. */
90 struct dpif_sflow *sflow; /* SFlow handle, or null. */
91 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
92 struct netflow *netflow; /* Netflow handle, or null. */
93 struct stp *stp; /* STP or null if disabled. */
94 struct rstp *rstp; /* RSTP or null if disabled. */
96 bool has_in_band; /* Bridge has in band control? */
97 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
99 /* Datapath feature support. */
100 struct dpif_backer_support support;
104 struct hmap_node hmap_node; /* In global 'xbundles' map. */
105 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
107 struct ovs_list list_node; /* In parent 'xbridges' list. */
108 struct xbridge *xbridge; /* Parent xbridge. */
110 struct ovs_list xports; /* Contains "struct xport"s. */
112 char *name; /* Name used in log messages. */
113 struct bond *bond; /* Nonnull iff more than one port. */
114 struct lacp *lacp; /* LACP handle or null. */
116 enum port_vlan_mode vlan_mode; /* VLAN mode. */
117 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
118 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
119 * NULL if all VLANs are trunked. */
120 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
121 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
125 struct hmap_node hmap_node; /* Node in global 'xports' map. */
126 struct ofport_dpif *ofport; /* Key in global 'xports map. */
128 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
129 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
131 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
133 struct ovs_list bundle_node; /* In parent xbundle (if it exists). */
134 struct xbundle *xbundle; /* Parent xbundle or null. */
136 struct netdev *netdev; /* 'ofport''s netdev. */
138 struct xbridge *xbridge; /* Parent bridge. */
139 struct xport *peer; /* Patch port peer or null. */
141 enum ofputil_port_config config; /* OpenFlow port configuration. */
142 enum ofputil_port_state state; /* OpenFlow port state. */
143 int stp_port_no; /* STP port number or -1 if not in use. */
144 struct rstp_port *rstp_port; /* RSTP port or null. */
146 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
148 bool may_enable; /* May be enabled in bonds. */
149 bool is_tunnel; /* Is a tunnel port. */
151 struct cfm *cfm; /* CFM handle or null. */
152 struct bfd *bfd; /* BFD handle or null. */
153 struct lldp *lldp; /* LLDP handle or null. */
157 struct xlate_in *xin;
158 struct xlate_out *xout;
160 const struct xbridge *xbridge;
162 /* Flow tables version at the beginning of the translation. */
163 cls_version_t tables_version;
165 /* Flow at the last commit. */
166 struct flow base_flow;
168 /* Tunnel IP destination address as received. This is stored separately
169 * as the base_flow.tunnel is cleared on init to reflect the datapath
170 * behavior. Used to make sure not to send tunneled output to ourselves,
171 * which might lead to an infinite loop. This could happen easily
172 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
173 * actually set the tun_dst field. */
174 ovs_be32 orig_tunnel_ip_dst;
176 /* Stack for the push and pop actions. Each stack element is of type
177 * "union mf_subvalue". */
180 /* The rule that we are currently translating, or NULL. */
181 struct rule_dpif *rule;
183 /* Flow translation populates this with wildcards relevant in translation.
184 * When 'xin->wc' is nonnull, this is the same pointer. When 'xin->wc' is
185 * null, this is a pointer to uninitialized scratch memory. This allows
186 * code to blindly write to 'ctx->wc' without worrying about whether the
187 * caller really wants wildcards. */
188 struct flow_wildcards *wc;
190 /* Output buffer for datapath actions. When 'xin->odp_actions' is nonnull,
191 * this is the same pointer. When 'xin->odp_actions' is null, this points
192 * to a scratch ofpbuf. This allows code to add actions to
193 * 'ctx->odp_actions' without worrying about whether the caller really
195 struct ofpbuf *odp_actions;
197 /* Resubmit statistics, via xlate_table_action(). */
198 int recurse; /* Current resubmit nesting depth. */
199 int resubmits; /* Total number of resubmits. */
200 bool in_group; /* Currently translating ofgroup, if true. */
201 bool in_action_set; /* Currently translating action_set, if true. */
203 uint8_t table_id; /* OpenFlow table ID where flow was found. */
204 ovs_be64 rule_cookie; /* Cookie of the rule being translated. */
205 uint32_t orig_skb_priority; /* Priority when packet arrived. */
206 uint32_t sflow_n_outputs; /* Number of output ports. */
207 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
208 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
209 bool exit; /* No further actions should be processed. */
211 /* These are used for non-bond recirculation. The recirculation IDs are
212 * stored in xout and must be associated with a datapath flow (ukey),
213 * otherwise they will be freed when the xout is uninitialized.
216 * Steps in Recirculation Translation
217 * ==================================
219 * At some point during translation, the code recognizes the need for
220 * recirculation. For example, recirculation is necessary when, after
221 * popping the last MPLS label, an action or a match tries to examine or
222 * modify a field that has been newly revealed following the MPLS label.
224 * The simplest part of the work to be done is to commit existing changes to
225 * the packet, which produces datapath actions corresponding to the changes,
226 * and after this, add an OVS_ACTION_ATTR_RECIRC datapath action.
228 * The main problem here is preserving state. When the datapath executes
229 * OVS_ACTION_ATTR_RECIRC, it will upcall to userspace to get a translation
230 * for the post-recirculation actions. At this point userspace has to
231 * resume the translation where it left off, which means that it has to
232 * execute the following:
234 * - The action that prompted recirculation, and any actions following
235 * it within the same flow.
237 * - If the action that prompted recirculation was invoked within a
238 * NXAST_RESUBMIT, then any actions following the resubmit. These
239 * "resubmit"s can be nested, so this has to go all the way up the
242 * - The OpenFlow 1.1+ action set.
244 * State that actions and flow table lookups can depend on, such as the
245 * following, must also be preserved:
247 * - Metadata fields (input port, registers, OF1.1+ metadata, ...).
249 * - Action set, stack
251 * - The table ID and cookie of the flow being translated at each level
252 * of the control stack (since OFPAT_CONTROLLER actions send these to
255 * Translation allows for the control of this state preservation via these
256 * members. When a need for recirculation is identified, the translation
259 * 1. Sets 'recirc_action_offset' to the current size of 'action_set'. The
260 * action set is part of what needs to be preserved, so this allows the
261 * action set and the additional state to share the 'action_set' buffer.
262 * Later steps can tell that setup for recirculation is in progress from
263 * the nonnegative value of 'recirc_action_offset'.
265 * 2. Sets 'exit' to true to tell later steps that we're exiting from the
266 * translation process.
268 * 3. Adds an OFPACT_UNROLL_XLATE action to 'action_set'. This action
269 * holds the current table ID and cookie so that they can be restored
270 * during a post-recirculation upcall translation.
272 * 4. Adds the action that prompted recirculation and any actions following
273 * it within the same flow to 'action_set', so that they can be executed
274 * during a post-recirculation upcall translation.
278 * 6. The action that prompted recirculation might be nested in a stack of
279 * nested "resubmit"s that have actions remaining. Each of these notices
280 * that we're exiting (from 'exit') and that recirculation setup is in
281 * progress (from 'recirc_action_offset') and responds by adding more
282 * OFPACT_UNROLL_XLATE actions to 'action_set', as necessary, and any
283 * actions that were yet unprocessed.
285 * The caller stores all the state produced by this process associated with
286 * the recirculation ID. For post-recirculation upcall translation, the
287 * caller passes it back in for the new translation to execute. The
288 * process yielded a set of ofpacts that can be translated directly, so it
289 * is not much of a special case at that point.
291 int recirc_action_offset; /* Offset in 'action_set' to actions to be
292 * executed after recirculation, or -1. */
293 int last_unroll_offset; /* Offset in 'action_set' to the latest unroll
296 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
297 * This is a trigger for recirculation in cases where translating an action
298 * or looking up a flow requires access to the fields of the packet after
299 * the MPLS label stack that was originally present. */
302 /* OpenFlow 1.1+ action set.
304 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
305 * When translation is otherwise complete, ofpacts_execute_action_set()
306 * converts it to a set of "struct ofpact"s that can be translated into
307 * datapath actions. */
308 bool action_set_has_group; /* Action set contains OFPACT_GROUP? */
309 struct ofpbuf action_set; /* Action set. */
312 static void xlate_action_set(struct xlate_ctx *ctx);
315 ctx_trigger_recirculation(struct xlate_ctx *ctx)
318 ctx->recirc_action_offset = ctx->action_set.size;
322 ctx_first_recirculation_action(const struct xlate_ctx *ctx)
324 return ctx->recirc_action_offset == ctx->action_set.size;
328 exit_recirculates(const struct xlate_ctx *ctx)
330 /* When recirculating the 'recirc_action_offset' has a non-negative value.
332 return ctx->recirc_action_offset >= 0;
335 static void compose_recirculate_action(struct xlate_ctx *ctx);
337 /* A controller may use OFPP_NONE as the ingress port to indicate that
338 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
339 * when an input bundle is needed for validation (e.g., mirroring or
340 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
341 * any 'port' structs, so care must be taken when dealing with it. */
342 static struct xbundle ofpp_none_bundle = {
344 .vlan_mode = PORT_VLAN_TRUNK
347 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
348 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
349 * traffic egressing the 'ofport' with that priority should be marked with. */
350 struct skb_priority_to_dscp {
351 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
352 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
354 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
370 /* xlate_cache entries hold enough information to perform the side effects of
371 * xlate_actions() for a rule, without needing to perform rule translation
372 * from scratch. The primary usage of these is to submit statistics to objects
373 * that a flow relates to, although they may be used for other effects as well
374 * (for instance, refreshing hard timeouts for learned flows). */
378 struct rule_dpif *rule;
385 struct netflow *netflow;
390 struct mbridge *mbridge;
391 mirror_mask_t mirrors;
399 struct ofproto_dpif *ofproto;
400 struct ofputil_flow_mod *fm;
401 struct ofpbuf *ofpacts;
404 struct ofproto_dpif *ofproto;
409 struct rule_dpif *rule;
414 struct group_dpif *group;
415 struct ofputil_bucket *bucket;
418 char br_name[IFNAMSIZ];
424 #define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
425 entries = xcache->entries; \
426 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
428 entry = ofpbuf_try_pull(&entries, sizeof *entry))
431 struct ofpbuf entries;
434 /* Xlate config contains hash maps of all bridges, bundles and ports.
435 * Xcfgp contains the pointer to the current xlate configuration.
436 * When the main thread needs to change the configuration, it copies xcfgp to
437 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
438 * does not block handler and revalidator threads. */
440 struct hmap xbridges;
441 struct hmap xbundles;
444 static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
445 static struct xlate_cfg *new_xcfg = NULL;
447 static bool may_receive(const struct xport *, struct xlate_ctx *);
448 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
450 static void xlate_normal(struct xlate_ctx *);
451 static inline void xlate_report(struct xlate_ctx *, const char *, ...)
452 OVS_PRINTF_FORMAT(2, 3);
453 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
454 uint8_t table_id, bool may_packet_in,
455 bool honor_table_miss);
456 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
457 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
458 static void output_normal(struct xlate_ctx *, const struct xbundle *,
461 /* Optional bond recirculation parameter to compose_output_action(). */
462 struct xlate_bond_recirc {
463 uint32_t recirc_id; /* !0 Use recirculation instead of output. */
464 uint8_t hash_alg; /* !0 Compute hash for recirc before. */
465 uint32_t hash_basis; /* Compute hash for recirc before. */
468 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port,
469 const struct xlate_bond_recirc *xr);
471 static struct xbridge *xbridge_lookup(struct xlate_cfg *,
472 const struct ofproto_dpif *);
473 static struct xbundle *xbundle_lookup(struct xlate_cfg *,
474 const struct ofbundle *);
475 static struct xport *xport_lookup(struct xlate_cfg *,
476 const struct ofport_dpif *);
477 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
478 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
479 uint32_t skb_priority);
480 static void clear_skb_priorities(struct xport *);
481 static size_t count_skb_priorities(const struct xport *);
482 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
485 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
487 static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
488 static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
489 static void xlate_xport_init(struct xlate_cfg *, struct xport *);
490 static void xlate_xbridge_set(struct xbridge *, struct dpif *,
491 const struct mac_learning *, struct stp *,
492 struct rstp *, const struct mcast_snooping *,
493 const struct mbridge *,
494 const struct dpif_sflow *,
495 const struct dpif_ipfix *,
496 const struct netflow *,
497 bool forward_bpdu, bool has_in_band,
498 const struct dpif_backer_support *);
499 static void xlate_xbundle_set(struct xbundle *xbundle,
500 enum port_vlan_mode vlan_mode, int vlan,
501 unsigned long *trunks, bool use_priority_tags,
502 const struct bond *bond, const struct lacp *lacp,
504 static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
505 const struct netdev *netdev, const struct cfm *cfm,
506 const struct bfd *bfd, const struct lldp *lldp,
507 int stp_port_no, const struct rstp_port *rstp_port,
508 enum ofputil_port_config config,
509 enum ofputil_port_state state, bool is_tunnel,
511 static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
512 static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
513 static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
514 static void xlate_xbridge_copy(struct xbridge *);
515 static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
516 static void xlate_xport_copy(struct xbridge *, struct xbundle *,
518 static void xlate_xcfg_free(struct xlate_cfg *);
521 xlate_report(struct xlate_ctx *ctx, const char *format, ...)
523 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
526 va_start(args, format);
527 ctx->xin->report_hook(ctx->xin, ctx->recurse, format, args);
533 xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
535 list_init(&xbridge->xbundles);
536 hmap_init(&xbridge->xports);
537 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
538 hash_pointer(xbridge->ofproto, 0));
542 xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
544 list_init(&xbundle->xports);
545 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
546 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
547 hash_pointer(xbundle->ofbundle, 0));
551 xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
553 hmap_init(&xport->skb_priorities);
554 hmap_insert(&xcfg->xports, &xport->hmap_node,
555 hash_pointer(xport->ofport, 0));
556 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
557 hash_ofp_port(xport->ofp_port));
561 xlate_xbridge_set(struct xbridge *xbridge,
563 const struct mac_learning *ml, struct stp *stp,
564 struct rstp *rstp, const struct mcast_snooping *ms,
565 const struct mbridge *mbridge,
566 const struct dpif_sflow *sflow,
567 const struct dpif_ipfix *ipfix,
568 const struct netflow *netflow,
569 bool forward_bpdu, bool has_in_band,
570 const struct dpif_backer_support *support)
572 if (xbridge->ml != ml) {
573 mac_learning_unref(xbridge->ml);
574 xbridge->ml = mac_learning_ref(ml);
577 if (xbridge->ms != ms) {
578 mcast_snooping_unref(xbridge->ms);
579 xbridge->ms = mcast_snooping_ref(ms);
582 if (xbridge->mbridge != mbridge) {
583 mbridge_unref(xbridge->mbridge);
584 xbridge->mbridge = mbridge_ref(mbridge);
587 if (xbridge->sflow != sflow) {
588 dpif_sflow_unref(xbridge->sflow);
589 xbridge->sflow = dpif_sflow_ref(sflow);
592 if (xbridge->ipfix != ipfix) {
593 dpif_ipfix_unref(xbridge->ipfix);
594 xbridge->ipfix = dpif_ipfix_ref(ipfix);
597 if (xbridge->stp != stp) {
598 stp_unref(xbridge->stp);
599 xbridge->stp = stp_ref(stp);
602 if (xbridge->rstp != rstp) {
603 rstp_unref(xbridge->rstp);
604 xbridge->rstp = rstp_ref(rstp);
607 if (xbridge->netflow != netflow) {
608 netflow_unref(xbridge->netflow);
609 xbridge->netflow = netflow_ref(netflow);
612 xbridge->dpif = dpif;
613 xbridge->forward_bpdu = forward_bpdu;
614 xbridge->has_in_band = has_in_band;
615 xbridge->support = *support;
619 xlate_xbundle_set(struct xbundle *xbundle,
620 enum port_vlan_mode vlan_mode, int vlan,
621 unsigned long *trunks, bool use_priority_tags,
622 const struct bond *bond, const struct lacp *lacp,
625 ovs_assert(xbundle->xbridge);
627 xbundle->vlan_mode = vlan_mode;
628 xbundle->vlan = vlan;
629 xbundle->trunks = trunks;
630 xbundle->use_priority_tags = use_priority_tags;
631 xbundle->floodable = floodable;
633 if (xbundle->bond != bond) {
634 bond_unref(xbundle->bond);
635 xbundle->bond = bond_ref(bond);
638 if (xbundle->lacp != lacp) {
639 lacp_unref(xbundle->lacp);
640 xbundle->lacp = lacp_ref(lacp);
645 xlate_xport_set(struct xport *xport, odp_port_t odp_port,
646 const struct netdev *netdev, const struct cfm *cfm,
647 const struct bfd *bfd, const struct lldp *lldp, int stp_port_no,
648 const struct rstp_port* rstp_port,
649 enum ofputil_port_config config, enum ofputil_port_state state,
650 bool is_tunnel, bool may_enable)
652 xport->config = config;
653 xport->state = state;
654 xport->stp_port_no = stp_port_no;
655 xport->is_tunnel = is_tunnel;
656 xport->may_enable = may_enable;
657 xport->odp_port = odp_port;
659 if (xport->rstp_port != rstp_port) {
660 rstp_port_unref(xport->rstp_port);
661 xport->rstp_port = rstp_port_ref(rstp_port);
664 if (xport->cfm != cfm) {
665 cfm_unref(xport->cfm);
666 xport->cfm = cfm_ref(cfm);
669 if (xport->bfd != bfd) {
670 bfd_unref(xport->bfd);
671 xport->bfd = bfd_ref(bfd);
674 if (xport->lldp != lldp) {
675 lldp_unref(xport->lldp);
676 xport->lldp = lldp_ref(lldp);
679 if (xport->netdev != netdev) {
680 netdev_close(xport->netdev);
681 xport->netdev = netdev_ref(netdev);
686 xlate_xbridge_copy(struct xbridge *xbridge)
688 struct xbundle *xbundle;
690 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
691 new_xbridge->ofproto = xbridge->ofproto;
692 new_xbridge->name = xstrdup(xbridge->name);
693 xlate_xbridge_init(new_xcfg, new_xbridge);
695 xlate_xbridge_set(new_xbridge,
696 xbridge->dpif, xbridge->ml, xbridge->stp,
697 xbridge->rstp, xbridge->ms, xbridge->mbridge,
698 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
699 xbridge->forward_bpdu, xbridge->has_in_band,
701 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
702 xlate_xbundle_copy(new_xbridge, xbundle);
705 /* Copy xports which are not part of a xbundle */
706 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
707 if (!xport->xbundle) {
708 xlate_xport_copy(new_xbridge, NULL, xport);
714 xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
717 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
718 new_xbundle->ofbundle = xbundle->ofbundle;
719 new_xbundle->xbridge = xbridge;
720 new_xbundle->name = xstrdup(xbundle->name);
721 xlate_xbundle_init(new_xcfg, new_xbundle);
723 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
724 xbundle->vlan, xbundle->trunks,
725 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
727 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
728 xlate_xport_copy(xbridge, new_xbundle, xport);
733 xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
736 struct skb_priority_to_dscp *pdscp, *new_pdscp;
737 struct xport *new_xport = xzalloc(sizeof *xport);
738 new_xport->ofport = xport->ofport;
739 new_xport->ofp_port = xport->ofp_port;
740 new_xport->xbridge = xbridge;
741 xlate_xport_init(new_xcfg, new_xport);
743 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
744 xport->bfd, xport->lldp, xport->stp_port_no,
745 xport->rstp_port, xport->config, xport->state,
746 xport->is_tunnel, xport->may_enable);
749 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
751 new_xport->peer = peer;
752 new_xport->peer->peer = new_xport;
757 new_xport->xbundle = xbundle;
758 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
761 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
762 new_pdscp = xmalloc(sizeof *pdscp);
763 new_pdscp->skb_priority = pdscp->skb_priority;
764 new_pdscp->dscp = pdscp->dscp;
765 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
766 hash_int(new_pdscp->skb_priority, 0));
770 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
771 * configuration in xcfgp.
773 * This needs to be called after editing the xlate configuration.
775 * Functions that edit the new xlate configuration are
776 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
782 * edit_xlate_configuration();
784 * xlate_txn_commit(); */
786 xlate_txn_commit(void)
788 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
790 ovsrcu_set(&xcfgp, new_xcfg);
791 ovsrcu_synchronize();
792 xlate_xcfg_free(xcfg);
796 /* Copies the current xlate configuration in xcfgp to new_xcfg.
798 * This needs to be called prior to editing the xlate configuration. */
800 xlate_txn_start(void)
802 struct xbridge *xbridge;
803 struct xlate_cfg *xcfg;
805 ovs_assert(!new_xcfg);
807 new_xcfg = xmalloc(sizeof *new_xcfg);
808 hmap_init(&new_xcfg->xbridges);
809 hmap_init(&new_xcfg->xbundles);
810 hmap_init(&new_xcfg->xports);
812 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
817 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
818 xlate_xbridge_copy(xbridge);
824 xlate_xcfg_free(struct xlate_cfg *xcfg)
826 struct xbridge *xbridge, *next_xbridge;
832 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
833 xlate_xbridge_remove(xcfg, xbridge);
836 hmap_destroy(&xcfg->xbridges);
837 hmap_destroy(&xcfg->xbundles);
838 hmap_destroy(&xcfg->xports);
843 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
845 const struct mac_learning *ml, struct stp *stp,
846 struct rstp *rstp, const struct mcast_snooping *ms,
847 const struct mbridge *mbridge,
848 const struct dpif_sflow *sflow,
849 const struct dpif_ipfix *ipfix,
850 const struct netflow *netflow,
851 bool forward_bpdu, bool has_in_band,
852 const struct dpif_backer_support *support)
854 struct xbridge *xbridge;
856 ovs_assert(new_xcfg);
858 xbridge = xbridge_lookup(new_xcfg, ofproto);
860 xbridge = xzalloc(sizeof *xbridge);
861 xbridge->ofproto = ofproto;
863 xlate_xbridge_init(new_xcfg, xbridge);
867 xbridge->name = xstrdup(name);
869 xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
870 netflow, forward_bpdu, has_in_band, support);
874 xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
876 struct xbundle *xbundle, *next_xbundle;
877 struct xport *xport, *next_xport;
883 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
884 xlate_xport_remove(xcfg, xport);
887 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
888 xlate_xbundle_remove(xcfg, xbundle);
891 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
892 mac_learning_unref(xbridge->ml);
893 mcast_snooping_unref(xbridge->ms);
894 mbridge_unref(xbridge->mbridge);
895 dpif_sflow_unref(xbridge->sflow);
896 dpif_ipfix_unref(xbridge->ipfix);
897 stp_unref(xbridge->stp);
898 rstp_unref(xbridge->rstp);
899 hmap_destroy(&xbridge->xports);
905 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
907 struct xbridge *xbridge;
909 ovs_assert(new_xcfg);
911 xbridge = xbridge_lookup(new_xcfg, ofproto);
912 xlate_xbridge_remove(new_xcfg, xbridge);
916 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
917 const char *name, enum port_vlan_mode vlan_mode, int vlan,
918 unsigned long *trunks, bool use_priority_tags,
919 const struct bond *bond, const struct lacp *lacp,
922 struct xbundle *xbundle;
924 ovs_assert(new_xcfg);
926 xbundle = xbundle_lookup(new_xcfg, ofbundle);
928 xbundle = xzalloc(sizeof *xbundle);
929 xbundle->ofbundle = ofbundle;
930 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
932 xlate_xbundle_init(new_xcfg, xbundle);
936 xbundle->name = xstrdup(name);
938 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
939 use_priority_tags, bond, lacp, floodable);
943 xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
951 LIST_FOR_EACH_POP (xport, bundle_node, &xbundle->xports) {
952 xport->xbundle = NULL;
955 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
956 list_remove(&xbundle->list_node);
957 bond_unref(xbundle->bond);
958 lacp_unref(xbundle->lacp);
964 xlate_bundle_remove(struct ofbundle *ofbundle)
966 struct xbundle *xbundle;
968 ovs_assert(new_xcfg);
970 xbundle = xbundle_lookup(new_xcfg, ofbundle);
971 xlate_xbundle_remove(new_xcfg, xbundle);
975 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
976 struct ofport_dpif *ofport, ofp_port_t ofp_port,
977 odp_port_t odp_port, const struct netdev *netdev,
978 const struct cfm *cfm, const struct bfd *bfd,
979 const struct lldp *lldp, struct ofport_dpif *peer,
980 int stp_port_no, const struct rstp_port *rstp_port,
981 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
982 enum ofputil_port_config config,
983 enum ofputil_port_state state, bool is_tunnel,
989 ovs_assert(new_xcfg);
991 xport = xport_lookup(new_xcfg, ofport);
993 xport = xzalloc(sizeof *xport);
994 xport->ofport = ofport;
995 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
996 xport->ofp_port = ofp_port;
998 xlate_xport_init(new_xcfg, xport);
1001 ovs_assert(xport->ofp_port == ofp_port);
1003 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, lldp,
1004 stp_port_no, rstp_port, config, state, is_tunnel,
1008 xport->peer->peer = NULL;
1010 xport->peer = xport_lookup(new_xcfg, peer);
1012 xport->peer->peer = xport;
1015 if (xport->xbundle) {
1016 list_remove(&xport->bundle_node);
1018 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
1019 if (xport->xbundle) {
1020 list_insert(&xport->xbundle->xports, &xport->bundle_node);
1023 clear_skb_priorities(xport);
1024 for (i = 0; i < n_qdscp; i++) {
1025 struct skb_priority_to_dscp *pdscp;
1026 uint32_t skb_priority;
1028 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
1033 pdscp = xmalloc(sizeof *pdscp);
1034 pdscp->skb_priority = skb_priority;
1035 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1036 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
1037 hash_int(pdscp->skb_priority, 0));
1042 xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
1049 xport->peer->peer = NULL;
1053 if (xport->xbundle) {
1054 list_remove(&xport->bundle_node);
1057 clear_skb_priorities(xport);
1058 hmap_destroy(&xport->skb_priorities);
1060 hmap_remove(&xcfg->xports, &xport->hmap_node);
1061 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
1063 netdev_close(xport->netdev);
1064 rstp_port_unref(xport->rstp_port);
1065 cfm_unref(xport->cfm);
1066 bfd_unref(xport->bfd);
1067 lldp_unref(xport->lldp);
1072 xlate_ofport_remove(struct ofport_dpif *ofport)
1074 struct xport *xport;
1076 ovs_assert(new_xcfg);
1078 xport = xport_lookup(new_xcfg, ofport);
1079 xlate_xport_remove(new_xcfg, xport);
1082 static struct ofproto_dpif *
1083 xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
1084 ofp_port_t *ofp_in_port, const struct xport **xportp)
1086 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1087 const struct xport *xport;
1089 xport = xport_lookup(xcfg, tnl_port_should_receive(flow)
1090 ? tnl_port_receive(flow)
1091 : odp_port_to_ofport(backer, flow->in_port.odp_port));
1092 if (OVS_UNLIKELY(!xport)) {
1097 *ofp_in_port = xport->ofp_port;
1099 return xport->xbridge->ofproto;
1102 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1103 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1104 struct ofproto_dpif *
1105 xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1106 ofp_port_t *ofp_in_port)
1108 const struct xport *xport;
1110 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1113 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1114 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
1115 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1116 * handles for those protocols if they're enabled. Caller may use the returned
1117 * pointers until quiescing, for longer term use additional references must
1120 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1123 xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1124 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1125 struct dpif_sflow **sflow, struct netflow **netflow,
1126 ofp_port_t *ofp_in_port)
1128 struct ofproto_dpif *ofproto;
1129 const struct xport *xport;
1131 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1138 *ofprotop = ofproto;
1142 *ipfix = xport ? xport->xbridge->ipfix : NULL;
1146 *sflow = xport ? xport->xbridge->sflow : NULL;
1150 *netflow = xport ? xport->xbridge->netflow : NULL;
1156 static struct xbridge *
1157 xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
1159 struct hmap *xbridges;
1160 struct xbridge *xbridge;
1162 if (!ofproto || !xcfg) {
1166 xbridges = &xcfg->xbridges;
1168 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
1170 if (xbridge->ofproto == ofproto) {
1177 static struct xbundle *
1178 xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
1180 struct hmap *xbundles;
1181 struct xbundle *xbundle;
1183 if (!ofbundle || !xcfg) {
1187 xbundles = &xcfg->xbundles;
1189 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
1191 if (xbundle->ofbundle == ofbundle) {
1198 static struct xport *
1199 xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
1201 struct hmap *xports;
1202 struct xport *xport;
1204 if (!ofport || !xcfg) {
1208 xports = &xcfg->xports;
1210 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
1212 if (xport->ofport == ofport) {
1219 static struct stp_port *
1220 xport_get_stp_port(const struct xport *xport)
1222 return xport->xbridge->stp && xport->stp_port_no != -1
1223 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1228 xport_stp_learn_state(const struct xport *xport)
1230 struct stp_port *sp = xport_get_stp_port(xport);
1232 ? stp_learn_in_state(stp_port_get_state(sp))
1237 xport_stp_forward_state(const struct xport *xport)
1239 struct stp_port *sp = xport_get_stp_port(xport);
1241 ? stp_forward_in_state(stp_port_get_state(sp))
1246 xport_stp_should_forward_bpdu(const struct xport *xport)
1248 struct stp_port *sp = xport_get_stp_port(xport);
1249 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
1252 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1253 * were used to make the determination.*/
1255 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1257 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1258 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1259 return is_stp(flow);
1263 stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1265 struct stp_port *sp = xport_get_stp_port(xport);
1266 struct dp_packet payload = *packet;
1267 struct eth_header *eth = dp_packet_data(&payload);
1269 /* Sink packets on ports that have STP disabled when the bridge has
1271 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1275 /* Trim off padding on payload. */
1276 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1277 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1280 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1281 stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
1285 static enum rstp_state
1286 xport_get_rstp_port_state(const struct xport *xport)
1288 return xport->rstp_port
1289 ? rstp_port_get_state(xport->rstp_port)
1294 xport_rstp_learn_state(const struct xport *xport)
1296 return xport->xbridge->rstp && xport->rstp_port
1297 ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1302 xport_rstp_forward_state(const struct xport *xport)
1304 return xport->xbridge->rstp && xport->rstp_port
1305 ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1310 xport_rstp_should_manage_bpdu(const struct xport *xport)
1312 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
1316 rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1318 struct dp_packet payload = *packet;
1319 struct eth_header *eth = dp_packet_data(&payload);
1321 /* Sink packets on ports that have no RSTP. */
1322 if (!xport->rstp_port) {
1326 /* Trim off padding on payload. */
1327 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1328 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1331 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1332 rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
1333 dp_packet_size(&payload));
1337 static struct xport *
1338 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1340 struct xport *xport;
1342 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1344 if (xport->ofp_port == ofp_port) {
1352 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1354 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1355 return xport ? xport->odp_port : ODPP_NONE;
1359 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1361 struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1362 return xport && xport->may_enable;
1365 static struct ofputil_bucket *
1366 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1370 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1372 struct group_dpif *group;
1374 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1375 struct ofputil_bucket *bucket;
1377 bucket = group_first_live_bucket(ctx, group, depth);
1378 group_dpif_unref(group);
1379 return bucket == NULL;
1385 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1388 bucket_is_alive(const struct xlate_ctx *ctx,
1389 struct ofputil_bucket *bucket, int depth)
1391 if (depth >= MAX_LIVENESS_RECURSION) {
1392 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1394 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1395 MAX_LIVENESS_RECURSION);
1399 return (!ofputil_bucket_has_liveness(bucket)
1400 || (bucket->watch_port != OFPP_ANY
1401 && odp_port_is_alive(ctx, bucket->watch_port))
1402 || (bucket->watch_group != OFPG_ANY
1403 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
1406 static struct ofputil_bucket *
1407 group_first_live_bucket(const struct xlate_ctx *ctx,
1408 const struct group_dpif *group, int depth)
1410 struct ofputil_bucket *bucket;
1411 const struct ovs_list *buckets;
1413 group_dpif_get_buckets(group, &buckets);
1414 LIST_FOR_EACH (bucket, list_node, buckets) {
1415 if (bucket_is_alive(ctx, bucket, depth)) {
1423 static struct ofputil_bucket *
1424 group_best_live_bucket(const struct xlate_ctx *ctx,
1425 const struct group_dpif *group,
1428 struct ofputil_bucket *best_bucket = NULL;
1429 uint32_t best_score = 0;
1432 struct ofputil_bucket *bucket;
1433 const struct ovs_list *buckets;
1435 group_dpif_get_buckets(group, &buckets);
1436 LIST_FOR_EACH (bucket, list_node, buckets) {
1437 if (bucket_is_alive(ctx, bucket, 0)) {
1438 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
1439 if (score >= best_score) {
1440 best_bucket = bucket;
1451 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
1453 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1454 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1458 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1460 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1463 static mirror_mask_t
1464 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1466 return xbundle != &ofpp_none_bundle
1467 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1471 static mirror_mask_t
1472 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
1474 return xbundle != &ofpp_none_bundle
1475 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1479 static mirror_mask_t
1480 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
1482 return xbundle != &ofpp_none_bundle
1483 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1487 static struct xbundle *
1488 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1489 bool warn, struct xport **in_xportp)
1491 struct xport *xport;
1493 /* Find the port and bundle for the received packet. */
1494 xport = get_ofp_port(xbridge, in_port);
1498 if (xport && xport->xbundle) {
1499 return xport->xbundle;
1502 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1503 * which a controller may use as the ingress port for traffic that
1504 * it is sourcing. */
1505 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
1506 return &ofpp_none_bundle;
1509 /* Odd. A few possible reasons here:
1511 * - We deleted a port but there are still a few packets queued up
1514 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1515 * we don't know about.
1517 * - The ofproto client didn't configure the port as part of a bundle.
1518 * This is particularly likely to happen if a packet was received on the
1519 * port after it was created, but before the client had a chance to
1520 * configure its bundle.
1523 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1525 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1526 "port %"PRIu16, xbridge->name, in_port);
1532 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
1534 const struct xbridge *xbridge = ctx->xbridge;
1535 mirror_mask_t mirrors;
1536 struct xbundle *in_xbundle;
1540 mirrors = ctx->xout->mirrors;
1541 ctx->xout->mirrors = 0;
1543 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
1544 ctx->xin->packet != NULL, NULL);
1548 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
1550 /* Drop frames on bundles reserved for mirroring. */
1551 if (xbundle_mirror_out(xbridge, in_xbundle)) {
1552 if (ctx->xin->packet != NULL) {
1553 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1554 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1555 "%s, which is reserved exclusively for mirroring",
1556 ctx->xbridge->name, in_xbundle->name);
1558 ofpbuf_clear(ctx->odp_actions);
1563 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
1564 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1567 vlan = input_vid_to_vlan(in_xbundle, vid);
1573 /* Restore the original packet before adding the mirror actions. */
1574 ctx->xin->flow = *orig_flow;
1577 mirror_mask_t dup_mirrors;
1578 struct ofbundle *out;
1579 const unsigned long *vlans;
1584 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1585 &vlans, &dup_mirrors, &out, &out_vlan);
1586 ovs_assert(has_mirror);
1589 ctx->wc->masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1591 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
1593 if (!vlan_mirrored) {
1594 mirrors = zero_rightmost_1bit(mirrors);
1598 mirrors &= ~dup_mirrors;
1599 ctx->xout->mirrors |= dup_mirrors;
1601 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1602 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1604 output_normal(ctx, out_xbundle, vlan);
1606 } else if (vlan != out_vlan
1607 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
1608 struct xbundle *xbundle;
1610 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1611 if (xbundle_includes_vlan(xbundle, out_vlan)
1612 && !xbundle_mirror_out(xbridge, xbundle)) {
1613 output_normal(ctx, xbundle, out_vlan);
1620 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1621 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1622 * the bundle on which the packet was received, returns the VLAN to which the
1625 * Both 'vid' and the return value are in the range 0...4095. */
1627 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1629 switch (in_xbundle->vlan_mode) {
1630 case PORT_VLAN_ACCESS:
1631 return in_xbundle->vlan;
1634 case PORT_VLAN_TRUNK:
1637 case PORT_VLAN_NATIVE_UNTAGGED:
1638 case PORT_VLAN_NATIVE_TAGGED:
1639 return vid ? vid : in_xbundle->vlan;
1646 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1647 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1650 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1651 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1654 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1656 /* Allow any VID on the OFPP_NONE port. */
1657 if (in_xbundle == &ofpp_none_bundle) {
1661 switch (in_xbundle->vlan_mode) {
1662 case PORT_VLAN_ACCESS:
1665 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1666 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1667 "packet received on port %s configured as VLAN "
1668 "%"PRIu16" access port", vid, in_xbundle->name,
1675 case PORT_VLAN_NATIVE_UNTAGGED:
1676 case PORT_VLAN_NATIVE_TAGGED:
1678 /* Port must always carry its native VLAN. */
1682 case PORT_VLAN_TRUNK:
1683 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1685 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1686 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1687 "received on port %s not configured for trunking "
1688 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1700 /* Given 'vlan', the VLAN that a packet belongs to, and
1701 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1702 * that should be included in the 802.1Q header. (If the return value is 0,
1703 * then the 802.1Q header should only be included in the packet if there is a
1706 * Both 'vlan' and the return value are in the range 0...4095. */
1708 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1710 switch (out_xbundle->vlan_mode) {
1711 case PORT_VLAN_ACCESS:
1714 case PORT_VLAN_TRUNK:
1715 case PORT_VLAN_NATIVE_TAGGED:
1718 case PORT_VLAN_NATIVE_UNTAGGED:
1719 return vlan == out_xbundle->vlan ? 0 : vlan;
1727 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1730 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1732 ovs_be16 tci, old_tci;
1733 struct xport *xport;
1734 struct xlate_bond_recirc xr;
1735 bool use_recirc = false;
1737 vid = output_vlan_to_vid(out_xbundle, vlan);
1738 if (list_is_empty(&out_xbundle->xports)) {
1739 /* Partially configured bundle with no slaves. Drop the packet. */
1741 } else if (!out_xbundle->bond) {
1742 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1745 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1746 struct flow_wildcards *wc = ctx->wc;
1747 struct ofport_dpif *ofport;
1749 if (ctx->xbridge->support.odp.recirc) {
1750 use_recirc = bond_may_recirc(
1751 out_xbundle->bond, &xr.recirc_id, &xr.hash_basis);
1754 /* Only TCP mode uses recirculation. */
1755 xr.hash_alg = OVS_HASH_ALG_L4;
1756 bond_update_post_recirc_rules(out_xbundle->bond, false);
1758 /* Recirculation does not require unmasking hash fields. */
1763 ofport = bond_choose_output_slave(out_xbundle->bond,
1764 &ctx->xin->flow, wc, vid);
1765 xport = xport_lookup(xcfg, ofport);
1768 /* No slaves enabled, so drop packet. */
1772 /* If use_recirc is set, the main thread will handle stats
1773 * accounting for this bond. */
1775 if (ctx->xin->resubmit_stats) {
1776 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1777 ctx->xin->resubmit_stats->n_bytes);
1779 if (ctx->xin->xcache) {
1780 struct xc_entry *entry;
1783 flow = &ctx->xin->flow;
1784 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1785 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1786 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1787 entry->u.bond.vid = vid;
1792 old_tci = *flow_tci;
1794 if (tci || out_xbundle->use_priority_tags) {
1795 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1797 tci |= htons(VLAN_CFI);
1802 compose_output_action(ctx, xport->ofp_port, use_recirc ? &xr : NULL);
1803 *flow_tci = old_tci;
1806 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1807 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1808 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1810 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1812 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1816 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1817 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1821 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1822 if (flow->nw_proto == ARP_OP_REPLY) {
1824 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1825 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1826 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1828 return flow->nw_src == flow->nw_dst;
1834 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1835 * dropped. Returns true if they may be forwarded, false if they should be
1838 * 'in_port' must be the xport that corresponds to flow->in_port.
1839 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1841 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1842 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1843 * checked by input_vid_is_valid().
1845 * May also add tags to '*tags', although the current implementation only does
1846 * so in one special case.
1849 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1852 struct xbundle *in_xbundle = in_port->xbundle;
1853 const struct xbridge *xbridge = ctx->xbridge;
1854 struct flow *flow = &ctx->xin->flow;
1856 /* Drop frames for reserved multicast addresses
1857 * only if forward_bpdu option is absent. */
1858 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1859 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1863 if (in_xbundle->bond) {
1864 struct mac_entry *mac;
1866 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1872 xlate_report(ctx, "bonding refused admissibility, dropping");
1875 case BV_DROP_IF_MOVED:
1876 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1877 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1879 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
1880 && (!is_gratuitous_arp(flow, ctx->wc)
1881 || mac_entry_is_grat_arp_locked(mac))) {
1882 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1883 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1887 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1895 /* Checks whether a MAC learning update is necessary for MAC learning table
1896 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1899 * Most packets processed through the MAC learning table do not actually
1900 * change it in any way. This function requires only a read lock on the MAC
1901 * learning table, so it is much cheaper in this common case.
1903 * Keep the code here synchronized with that in update_learning_table__()
1906 is_mac_learning_update_needed(const struct mac_learning *ml,
1907 const struct flow *flow,
1908 struct flow_wildcards *wc,
1909 int vlan, struct xbundle *in_xbundle)
1910 OVS_REQ_RDLOCK(ml->rwlock)
1912 struct mac_entry *mac;
1914 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1918 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1919 if (!mac || mac_entry_age(ml, mac)) {
1923 if (is_gratuitous_arp(flow, wc)) {
1924 /* We don't want to learn from gratuitous ARP packets that are
1925 * reflected back over bond slaves so we lock the learning table. */
1926 if (!in_xbundle->bond) {
1928 } else if (mac_entry_is_grat_arp_locked(mac)) {
1933 return mac_entry_get_port(ml, mac) != in_xbundle->ofbundle;
1937 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1938 * received on 'in_xbundle' in 'vlan'.
1940 * This code repeats all the checks in is_mac_learning_update_needed() because
1941 * the lock was released between there and here and thus the MAC learning state
1942 * could have changed.
1944 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1947 update_learning_table__(const struct xbridge *xbridge,
1948 const struct flow *flow, struct flow_wildcards *wc,
1949 int vlan, struct xbundle *in_xbundle)
1950 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1952 struct mac_entry *mac;
1954 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1958 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1959 if (is_gratuitous_arp(flow, wc)) {
1960 /* We don't want to learn from gratuitous ARP packets that are
1961 * reflected back over bond slaves so we lock the learning table. */
1962 if (!in_xbundle->bond) {
1963 mac_entry_set_grat_arp_lock(mac);
1964 } else if (mac_entry_is_grat_arp_locked(mac)) {
1969 if (mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle) {
1970 /* The log messages here could actually be useful in debugging,
1971 * so keep the rate limit relatively high. */
1972 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1974 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1975 "on port %s in VLAN %d",
1976 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1977 in_xbundle->name, vlan);
1979 mac_entry_set_port(xbridge->ml, mac, in_xbundle->ofbundle);
1984 update_learning_table(const struct xbridge *xbridge,
1985 const struct flow *flow, struct flow_wildcards *wc,
1986 int vlan, struct xbundle *in_xbundle)
1990 /* Don't learn the OFPP_NONE port. */
1991 if (in_xbundle == &ofpp_none_bundle) {
1995 /* First try the common case: no change to MAC learning table. */
1996 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1997 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1999 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2002 /* Slow path: MAC learning table might need an update. */
2003 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
2004 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
2005 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2009 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2010 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
2012 update_mcast_snooping_table4__(const struct xbridge *xbridge,
2013 const struct flow *flow,
2014 struct mcast_snooping *ms, int vlan,
2015 struct xbundle *in_xbundle,
2016 const struct dp_packet *packet)
2017 OVS_REQ_WRLOCK(ms->rwlock)
2019 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2021 ovs_be32 ip4 = flow->igmp_group_ip4;
2023 switch (ntohs(flow->tp_src)) {
2024 case IGMP_HOST_MEMBERSHIP_REPORT:
2025 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2026 if (mcast_snooping_add_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2027 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
2028 IP_FMT" is on port %s in VLAN %d",
2029 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2032 case IGMP_HOST_LEAVE_MESSAGE:
2033 if (mcast_snooping_leave_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2034 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
2035 IP_FMT" is on port %s in VLAN %d",
2036 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2039 case IGMP_HOST_MEMBERSHIP_QUERY:
2040 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
2041 in_xbundle->ofbundle)) {
2042 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
2043 IP_FMT" is on port %s in VLAN %d",
2044 xbridge->name, IP_ARGS(flow->nw_src),
2045 in_xbundle->name, vlan);
2048 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2049 if ((count = mcast_snooping_add_report(ms, packet, vlan,
2050 in_xbundle->ofbundle))) {
2051 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2052 "addresses on port %s in VLAN %d",
2053 xbridge->name, count, in_xbundle->name, vlan);
2060 update_mcast_snooping_table6__(const struct xbridge *xbridge,
2061 const struct flow *flow,
2062 struct mcast_snooping *ms, int vlan,
2063 struct xbundle *in_xbundle,
2064 const struct dp_packet *packet)
2065 OVS_REQ_WRLOCK(ms->rwlock)
2067 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2070 switch (ntohs(flow->tp_src)) {
2072 if (!ipv6_addr_equals(&flow->ipv6_src, &in6addr_any)
2073 && mcast_snooping_add_mrouter(ms, vlan, in_xbundle->ofbundle)) {
2074 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query on port %s"
2076 xbridge->name, in_xbundle->name, vlan);
2082 count = mcast_snooping_add_mld(ms, packet, vlan, in_xbundle->ofbundle);
2084 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2085 "addresses on port %s in VLAN %d",
2086 xbridge->name, count, in_xbundle->name, vlan);
2092 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2093 * was received on 'in_xbundle' in 'vlan'. */
2095 update_mcast_snooping_table(const struct xbridge *xbridge,
2096 const struct flow *flow, int vlan,
2097 struct xbundle *in_xbundle,
2098 const struct dp_packet *packet)
2100 struct mcast_snooping *ms = xbridge->ms;
2101 struct xlate_cfg *xcfg;
2102 struct xbundle *mcast_xbundle;
2103 struct mcast_port_bundle *fport;
2105 /* Don't learn the OFPP_NONE port. */
2106 if (in_xbundle == &ofpp_none_bundle) {
2110 /* Don't learn from flood ports */
2111 mcast_xbundle = NULL;
2112 ovs_rwlock_wrlock(&ms->rwlock);
2113 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2114 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2115 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2116 if (mcast_xbundle == in_xbundle) {
2121 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
2122 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2123 update_mcast_snooping_table4__(xbridge, flow, ms, vlan,
2124 in_xbundle, packet);
2126 update_mcast_snooping_table6__(xbridge, flow, ms, vlan,
2127 in_xbundle, packet);
2130 ovs_rwlock_unlock(&ms->rwlock);
2133 /* send the packet to ports having the multicast group learned */
2135 xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2136 struct mcast_snooping *ms OVS_UNUSED,
2137 struct mcast_group *grp,
2138 struct xbundle *in_xbundle, uint16_t vlan)
2139 OVS_REQ_RDLOCK(ms->rwlock)
2141 struct xlate_cfg *xcfg;
2142 struct mcast_group_bundle *b;
2143 struct xbundle *mcast_xbundle;
2145 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2146 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2147 mcast_xbundle = xbundle_lookup(xcfg, b->port);
2148 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2149 xlate_report(ctx, "forwarding to mcast group port");
2150 output_normal(ctx, mcast_xbundle, vlan);
2151 } else if (!mcast_xbundle) {
2152 xlate_report(ctx, "mcast group port is unknown, dropping");
2154 xlate_report(ctx, "mcast group port is input port, dropping");
2159 /* send the packet to ports connected to multicast routers */
2161 xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2162 struct mcast_snooping *ms,
2163 struct xbundle *in_xbundle, uint16_t vlan)
2164 OVS_REQ_RDLOCK(ms->rwlock)
2166 struct xlate_cfg *xcfg;
2167 struct mcast_mrouter_bundle *mrouter;
2168 struct xbundle *mcast_xbundle;
2170 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2171 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2172 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2173 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2174 xlate_report(ctx, "forwarding to mcast router port");
2175 output_normal(ctx, mcast_xbundle, vlan);
2176 } else if (!mcast_xbundle) {
2177 xlate_report(ctx, "mcast router port is unknown, dropping");
2179 xlate_report(ctx, "mcast router port is input port, dropping");
2184 /* send the packet to ports flagged to be flooded */
2186 xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2187 struct mcast_snooping *ms,
2188 struct xbundle *in_xbundle, uint16_t vlan)
2189 OVS_REQ_RDLOCK(ms->rwlock)
2191 struct xlate_cfg *xcfg;
2192 struct mcast_port_bundle *fport;
2193 struct xbundle *mcast_xbundle;
2195 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2196 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2197 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2198 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2199 xlate_report(ctx, "forwarding to mcast flood port");
2200 output_normal(ctx, mcast_xbundle, vlan);
2201 } else if (!mcast_xbundle) {
2202 xlate_report(ctx, "mcast flood port is unknown, dropping");
2204 xlate_report(ctx, "mcast flood port is input port, dropping");
2209 /* forward the Reports to configured ports */
2211 xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
2212 struct mcast_snooping *ms,
2213 struct xbundle *in_xbundle, uint16_t vlan)
2214 OVS_REQ_RDLOCK(ms->rwlock)
2216 struct xlate_cfg *xcfg;
2217 struct mcast_port_bundle *rport;
2218 struct xbundle *mcast_xbundle;
2220 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2221 LIST_FOR_EACH(rport, node, &ms->rport_list) {
2222 mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2223 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2224 xlate_report(ctx, "forwarding Report to mcast flagged port");
2225 output_normal(ctx, mcast_xbundle, vlan);
2226 } else if (!mcast_xbundle) {
2227 xlate_report(ctx, "mcast port is unknown, dropping the Report");
2229 xlate_report(ctx, "mcast port is input port, dropping the Report");
2235 xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2238 struct xbundle *xbundle;
2240 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2241 if (xbundle != in_xbundle
2242 && xbundle_includes_vlan(xbundle, vlan)
2243 && xbundle->floodable
2244 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2245 output_normal(ctx, xbundle, vlan);
2248 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2252 xlate_normal(struct xlate_ctx *ctx)
2254 struct flow_wildcards *wc = ctx->wc;
2255 struct flow *flow = &ctx->xin->flow;
2256 struct xbundle *in_xbundle;
2257 struct xport *in_port;
2258 struct mac_entry *mac;
2263 ctx->xout->has_normal = true;
2265 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2266 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2267 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2269 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
2270 ctx->xin->packet != NULL, &in_port);
2272 xlate_report(ctx, "no input bundle, dropping");
2276 /* Drop malformed frames. */
2277 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
2278 !(flow->vlan_tci & htons(VLAN_CFI))) {
2279 if (ctx->xin->packet != NULL) {
2280 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2281 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
2282 "VLAN tag received on port %s",
2283 ctx->xbridge->name, in_xbundle->name);
2285 xlate_report(ctx, "partial VLAN tag, dropping");
2289 /* Drop frames on bundles reserved for mirroring. */
2290 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
2291 if (ctx->xin->packet != NULL) {
2292 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2293 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2294 "%s, which is reserved exclusively for mirroring",
2295 ctx->xbridge->name, in_xbundle->name);
2297 xlate_report(ctx, "input port is mirror output port, dropping");
2302 vid = vlan_tci_to_vid(flow->vlan_tci);
2303 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
2304 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2307 vlan = input_vid_to_vlan(in_xbundle, vid);
2309 /* Check other admissibility requirements. */
2310 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2314 /* Learn source MAC. */
2315 if (ctx->xin->may_learn) {
2316 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
2318 if (ctx->xin->xcache) {
2319 struct xc_entry *entry;
2321 /* Save enough info to update mac learning table later. */
2322 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
2323 entry->u.normal.ofproto = ctx->xbridge->ofproto;
2324 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2325 entry->u.normal.vlan = vlan;
2328 /* Determine output bundle. */
2329 if (mcast_snooping_enabled(ctx->xbridge->ms)
2330 && !eth_addr_is_broadcast(flow->dl_dst)
2331 && eth_addr_is_multicast(flow->dl_dst)
2332 && is_ip_any(flow)) {
2333 struct mcast_snooping *ms = ctx->xbridge->ms;
2334 struct mcast_group *grp = NULL;
2336 if (is_igmp(flow)) {
2337 if (mcast_snooping_is_membership(flow->tp_src) ||
2338 mcast_snooping_is_query(flow->tp_src)) {
2339 if (ctx->xin->may_learn) {
2340 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2341 in_xbundle, ctx->xin->packet);
2344 * IGMP packets need to take the slow path, in order to be
2345 * processed for mdb updates. That will prevent expires
2346 * firing off even after hosts have sent reports.
2348 ctx->xout->slow |= SLOW_ACTION;
2351 if (mcast_snooping_is_membership(flow->tp_src)) {
2352 ovs_rwlock_rdlock(&ms->rwlock);
2353 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2354 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2355 * forward IGMP Membership Reports only to those ports where
2356 * multicast routers are attached. Alternatively stated: a
2357 * snooping switch should not forward IGMP Membership Reports
2358 * to ports on which only hosts are attached.
2359 * An administrative control may be provided to override this
2360 * restriction, allowing the report messages to be flooded to
2362 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2363 ovs_rwlock_unlock(&ms->rwlock);
2365 xlate_report(ctx, "multicast traffic, flooding");
2366 xlate_normal_flood(ctx, in_xbundle, vlan);
2369 } else if (is_mld(flow)) {
2370 ctx->xout->slow |= SLOW_ACTION;
2371 if (ctx->xin->may_learn) {
2372 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2373 in_xbundle, ctx->xin->packet);
2375 if (is_mld_report(flow)) {
2376 ovs_rwlock_rdlock(&ms->rwlock);
2377 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2378 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2379 ovs_rwlock_unlock(&ms->rwlock);
2381 xlate_report(ctx, "MLD query, flooding");
2382 xlate_normal_flood(ctx, in_xbundle, vlan);
2385 if ((flow->dl_type == htons(ETH_TYPE_IP)
2386 && ip_is_local_multicast(flow->nw_dst))
2387 || (flow->dl_type == htons(ETH_TYPE_IPV6)
2388 && ipv6_is_all_hosts(&flow->ipv6_dst))) {
2389 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2390 * address in the 224.0.0.x range which are not IGMP must
2391 * be forwarded on all ports */
2392 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2393 xlate_normal_flood(ctx, in_xbundle, vlan);
2398 /* forwarding to group base ports */
2399 ovs_rwlock_rdlock(&ms->rwlock);
2400 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2401 grp = mcast_snooping_lookup4(ms, flow->nw_dst, vlan);
2402 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2403 grp = mcast_snooping_lookup(ms, &flow->ipv6_dst, vlan);
2406 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2407 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2408 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2410 if (mcast_snooping_flood_unreg(ms)) {
2411 xlate_report(ctx, "unregistered multicast, flooding");
2412 xlate_normal_flood(ctx, in_xbundle, vlan);
2414 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2415 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2418 ovs_rwlock_unlock(&ms->rwlock);
2420 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2421 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
2422 mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
2423 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2426 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2427 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2428 if (mac_xbundle && mac_xbundle != in_xbundle) {
2429 xlate_report(ctx, "forwarding to learned port");
2430 output_normal(ctx, mac_xbundle, vlan);
2431 } else if (!mac_xbundle) {
2432 xlate_report(ctx, "learned port is unknown, dropping");
2434 xlate_report(ctx, "learned port is input port, dropping");
2437 xlate_report(ctx, "no learned MAC for destination, flooding");
2438 xlate_normal_flood(ctx, in_xbundle, vlan);
2443 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
2444 * the number of packets out of UINT32_MAX to sample. The given
2445 * cookie is passed back in the callback for each sampled packet.
2448 compose_sample_action(const struct xbridge *xbridge,
2449 struct ofpbuf *odp_actions,
2450 const struct flow *flow,
2451 const uint32_t probability,
2452 const union user_action_cookie *cookie,
2453 const size_t cookie_size,
2454 const odp_port_t tunnel_out_port,
2455 bool include_actions)
2457 size_t sample_offset, actions_offset;
2458 odp_port_t odp_port;
2462 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
2464 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
2466 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
2468 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
2469 pid = dpif_port_get_pid(xbridge->dpif, odp_port,
2470 flow_hash_5tuple(flow, 0));
2471 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2476 nl_msg_end_nested(odp_actions, actions_offset);
2477 nl_msg_end_nested(odp_actions, sample_offset);
2478 return cookie_offset;
2482 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
2483 odp_port_t odp_port, unsigned int n_outputs,
2484 union user_action_cookie *cookie)
2488 cookie->type = USER_ACTION_COOKIE_SFLOW;
2489 cookie->sflow.vlan_tci = vlan_tci;
2491 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2492 * port information") for the interpretation of cookie->output. */
2493 switch (n_outputs) {
2495 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2496 cookie->sflow.output = 0x40000000 | 256;
2500 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
2502 cookie->sflow.output = ifindex;
2507 /* 0x80000000 means "multiple output ports. */
2508 cookie->sflow.output = 0x80000000 | n_outputs;
2513 /* Compose SAMPLE action for sFlow bridge sampling. */
2515 compose_sflow_action(const struct xbridge *xbridge,
2516 struct ofpbuf *odp_actions,
2517 const struct flow *flow,
2518 odp_port_t odp_port)
2520 uint32_t probability;
2521 union user_action_cookie cookie;
2523 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
2527 probability = dpif_sflow_get_probability(xbridge->sflow);
2528 compose_sflow_cookie(xbridge, htons(0), odp_port,
2529 odp_port == ODPP_NONE ? 0 : 1, &cookie);
2531 return compose_sample_action(xbridge, odp_actions, flow, probability,
2532 &cookie, sizeof cookie.sflow, ODPP_NONE,
2537 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
2538 uint32_t obs_domain_id, uint32_t obs_point_id,
2539 union user_action_cookie *cookie)
2541 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
2542 cookie->flow_sample.probability = probability;
2543 cookie->flow_sample.collector_set_id = collector_set_id;
2544 cookie->flow_sample.obs_domain_id = obs_domain_id;
2545 cookie->flow_sample.obs_point_id = obs_point_id;
2549 compose_ipfix_cookie(union user_action_cookie *cookie,
2550 odp_port_t output_odp_port)
2552 cookie->type = USER_ACTION_COOKIE_IPFIX;
2553 cookie->ipfix.output_odp_port = output_odp_port;
2556 /* Compose SAMPLE action for IPFIX bridge sampling. */
2558 compose_ipfix_action(const struct xbridge *xbridge,
2559 struct ofpbuf *odp_actions,
2560 const struct flow *flow,
2561 odp_port_t output_odp_port)
2563 uint32_t probability;
2564 union user_action_cookie cookie;
2565 odp_port_t tunnel_out_port = ODPP_NONE;
2567 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
2571 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2573 if (output_odp_port == ODPP_NONE &&
2574 !dpif_ipfix_get_bridge_exporter_input_sampling(xbridge->ipfix)) {
2578 /* For output case, output_odp_port is valid*/
2579 if (output_odp_port != ODPP_NONE) {
2580 if (!dpif_ipfix_get_bridge_exporter_output_sampling(xbridge->ipfix)) {
2583 /* If tunnel sampling is enabled, put an additional option attribute:
2584 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2586 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(xbridge->ipfix) &&
2587 dpif_ipfix_get_tunnel_port(xbridge->ipfix, output_odp_port) ) {
2588 tunnel_out_port = output_odp_port;
2592 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
2593 compose_ipfix_cookie(&cookie, output_odp_port);
2595 compose_sample_action(xbridge, odp_actions, flow, probability,
2596 &cookie, sizeof cookie.ipfix, tunnel_out_port,
2600 /* SAMPLE action for sFlow must be first action in any given list of
2601 * actions. At this point we do not have all information required to
2602 * build it. So try to build sample action as complete as possible. */
2604 add_sflow_action(struct xlate_ctx *ctx)
2606 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
2608 &ctx->xin->flow, ODPP_NONE);
2609 ctx->sflow_odp_port = 0;
2610 ctx->sflow_n_outputs = 0;
2613 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
2614 * of actions, eventually after the SAMPLE action for sFlow. */
2616 add_ipfix_action(struct xlate_ctx *ctx)
2618 compose_ipfix_action(ctx->xbridge, ctx->odp_actions,
2619 &ctx->xin->flow, ODPP_NONE);
2623 add_ipfix_output_action(struct xlate_ctx *ctx, odp_port_t port)
2625 compose_ipfix_action(ctx->xbridge, ctx->odp_actions,
2626 &ctx->xin->flow, port);
2629 /* Fix SAMPLE action according to data collected while composing ODP actions.
2630 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
2631 * USERSPACE action's user-cookie which is required for sflow. */
2633 fix_sflow_action(struct xlate_ctx *ctx)
2635 const struct flow *base = &ctx->base_flow;
2636 union user_action_cookie *cookie;
2638 if (!ctx->user_cookie_offset) {
2642 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
2643 sizeof cookie->sflow);
2644 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2646 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
2647 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
2650 static enum slow_path_reason
2651 process_special(struct xlate_ctx *ctx, const struct flow *flow,
2652 const struct xport *xport, const struct dp_packet *packet)
2654 struct flow_wildcards *wc = ctx->wc;
2655 const struct xbridge *xbridge = ctx->xbridge;
2659 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
2661 cfm_process_heartbeat(xport->cfm, packet);
2664 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
2666 bfd_process_packet(xport->bfd, flow, packet);
2667 /* If POLL received, immediately sends FINAL back. */
2668 if (bfd_should_send_packet(xport->bfd)) {
2669 ofproto_dpif_monitor_port_send_soon(xport->ofport);
2673 } else if (xport->xbundle && xport->xbundle->lacp
2674 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2676 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
2679 } else if ((xbridge->stp || xbridge->rstp) &&
2680 stp_should_process_flow(flow, wc)) {
2683 ? stp_process_packet(xport, packet)
2684 : rstp_process_packet(xport, packet);
2687 } else if (xport->lldp && lldp_should_process_flow(xport->lldp, flow)) {
2689 lldp_process_packet(xport->lldp, packet);
2698 tnl_route_lookup_flow(const struct flow *oflow,
2699 ovs_be32 *ip, struct xport **out_port)
2701 char out_dev[IFNAMSIZ];
2702 struct xbridge *xbridge;
2703 struct xlate_cfg *xcfg;
2706 if (!ovs_router_lookup(oflow->tunnel.ip_dst, out_dev, &gw)) {
2713 *ip = oflow->tunnel.ip_dst;
2716 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2719 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
2720 if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
2723 HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
2724 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
2735 xlate_flood_packet(struct xbridge *xbridge, struct dp_packet *packet)
2737 struct ofpact_output output;
2740 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
2741 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
2742 flow_extract(packet, &flow);
2743 flow.in_port.ofp_port = OFPP_NONE;
2744 output.port = OFPP_FLOOD;
2747 return ofproto_dpif_execute_actions(xbridge->ofproto, &flow, NULL,
2748 &output.ofpact, sizeof output,
2753 tnl_send_arp_request(const struct xport *out_dev, const uint8_t eth_src[ETH_ADDR_LEN],
2754 ovs_be32 ip_src, ovs_be32 ip_dst)
2756 struct xbridge *xbridge = out_dev->xbridge;
2757 struct dp_packet packet;
2759 dp_packet_init(&packet, 0);
2760 compose_arp(&packet, ARP_OP_REQUEST,
2761 eth_src, eth_addr_zero, true, ip_src, ip_dst);
2763 xlate_flood_packet(xbridge, &packet);
2764 dp_packet_uninit(&packet);
2768 build_tunnel_send(struct xlate_ctx *ctx, const struct xport *xport,
2769 const struct flow *flow, odp_port_t tunnel_odp_port)
2771 struct ovs_action_push_tnl tnl_push_data;
2772 struct xport *out_dev = NULL;
2773 ovs_be32 s_ip, d_ip = 0;
2774 uint8_t smac[ETH_ADDR_LEN];
2775 uint8_t dmac[ETH_ADDR_LEN];
2778 err = tnl_route_lookup_flow(flow, &d_ip, &out_dev);
2780 xlate_report(ctx, "native tunnel routing failed");
2783 xlate_report(ctx, "tunneling to "IP_FMT" via %s",
2784 IP_ARGS(d_ip), netdev_get_name(out_dev->netdev));
2786 /* Use mac addr of bridge port of the peer. */
2787 err = netdev_get_etheraddr(out_dev->netdev, smac);
2789 xlate_report(ctx, "tunnel output device lacks Ethernet address");
2793 err = netdev_get_in4(out_dev->netdev, (struct in_addr *) &s_ip, NULL);
2795 xlate_report(ctx, "tunnel output device lacks IPv4 address");
2799 err = tnl_arp_lookup(out_dev->xbridge->name, d_ip, dmac);
2801 xlate_report(ctx, "ARP cache miss for "IP_FMT" on bridge %s, "
2802 "sending ARP request",
2803 IP_ARGS(d_ip), out_dev->xbridge->name);
2804 tnl_send_arp_request(out_dev, smac, s_ip, d_ip);
2807 if (ctx->xin->xcache) {
2808 struct xc_entry *entry;
2810 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_ARP);
2811 ovs_strlcpy(entry->u.tnl_arp_cache.br_name, out_dev->xbridge->name,
2812 sizeof entry->u.tnl_arp_cache.br_name);
2813 entry->u.tnl_arp_cache.d_ip = d_ip;
2816 xlate_report(ctx, "tunneling from "ETH_ADDR_FMT" "IP_FMT
2817 " to "ETH_ADDR_FMT" "IP_FMT,
2818 ETH_ADDR_ARGS(smac), IP_ARGS(s_ip),
2819 ETH_ADDR_ARGS(dmac), IP_ARGS(d_ip));
2820 err = tnl_port_build_header(xport->ofport, flow,
2821 dmac, smac, s_ip, &tnl_push_data);
2825 tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
2826 tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
2827 odp_put_tnl_push_action(ctx->odp_actions, &tnl_push_data);
2832 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
2833 const struct xlate_bond_recirc *xr, bool check_stp)
2835 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
2836 struct flow_wildcards *wc = ctx->wc;
2837 struct flow *flow = &ctx->xin->flow;
2838 struct flow_tnl flow_tnl;
2839 ovs_be16 flow_vlan_tci;
2840 uint32_t flow_pkt_mark;
2841 uint8_t flow_nw_tos;
2842 odp_port_t out_port, odp_port;
2843 bool tnl_push_pop_send = false;
2846 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2847 * before traversing a patch port. */
2848 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 33);
2849 memset(&flow_tnl, 0, sizeof flow_tnl);
2852 xlate_report(ctx, "Nonexistent output port");
2854 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
2855 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2857 } else if (check_stp) {
2858 if (is_stp(&ctx->base_flow)) {
2859 if (!xport_stp_should_forward_bpdu(xport) &&
2860 !xport_rstp_should_manage_bpdu(xport)) {
2861 if (ctx->xbridge->stp != NULL) {
2862 xlate_report(ctx, "STP not in listening state, "
2863 "skipping bpdu output");
2864 } else if (ctx->xbridge->rstp != NULL) {
2865 xlate_report(ctx, "RSTP not managing BPDU in this state, "
2866 "skipping bpdu output");
2870 } else if (!xport_stp_forward_state(xport) ||
2871 !xport_rstp_forward_state(xport)) {
2872 if (ctx->xbridge->stp != NULL) {
2873 xlate_report(ctx, "STP not in forwarding state, "
2875 } else if (ctx->xbridge->rstp != NULL) {
2876 xlate_report(ctx, "RSTP not in forwarding state, "
2883 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
2884 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
2889 const struct xport *peer = xport->peer;
2890 struct flow old_flow = ctx->xin->flow;
2891 bool old_was_mpls = ctx->was_mpls;
2892 cls_version_t old_version = ctx->tables_version;
2893 enum slow_path_reason special;
2894 struct ofpbuf old_stack = ctx->stack;
2895 union mf_subvalue new_stack[1024 / sizeof(union mf_subvalue)];
2896 struct ofpbuf old_action_set = ctx->action_set;
2897 uint64_t actset_stub[1024 / 8];
2899 ofpbuf_use_stub(&ctx->stack, new_stack, sizeof new_stack);
2900 ofpbuf_use_stub(&ctx->action_set, actset_stub, sizeof actset_stub);
2901 ctx->xbridge = peer->xbridge;
2902 flow->in_port.ofp_port = peer->ofp_port;
2903 flow->metadata = htonll(0);
2904 memset(&flow->tunnel, 0, sizeof flow->tunnel);
2905 memset(flow->regs, 0, sizeof flow->regs);
2906 flow->actset_output = OFPP_UNSET;
2908 /* The bridge is now known so obtain its table version. */
2910 = ofproto_dpif_get_tables_version(ctx->xbridge->ofproto);
2912 special = process_special(ctx, &ctx->xin->flow, peer,
2915 ctx->xout->slow |= special;
2916 } else if (may_receive(peer, ctx)) {
2917 if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
2918 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2919 if (ctx->action_set.size) {
2920 /* Translate action set only if not dropping the packet and
2921 * not recirculating. */
2922 if (!exit_recirculates(ctx)) {
2923 xlate_action_set(ctx);
2926 /* Check if need to recirculate. */
2927 if (exit_recirculates(ctx)) {
2928 compose_recirculate_action(ctx);
2931 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
2932 * the learning action look at the packet, then drop it. */
2933 struct flow old_base_flow = ctx->base_flow;
2934 size_t old_size = ctx->odp_actions->size;
2935 mirror_mask_t old_mirrors = ctx->xout->mirrors;
2937 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2938 ctx->xout->mirrors = old_mirrors;
2939 ctx->base_flow = old_base_flow;
2940 ctx->odp_actions->size = old_size;
2942 /* Undo changes that may have been done for recirculation. */
2943 if (exit_recirculates(ctx)) {
2944 ctx->action_set.size = ctx->recirc_action_offset;
2945 ctx->recirc_action_offset = -1;
2946 ctx->last_unroll_offset = -1;
2951 ctx->xin->flow = old_flow;
2952 ctx->xbridge = xport->xbridge;
2953 ofpbuf_uninit(&ctx->action_set);
2954 ctx->action_set = old_action_set;
2955 ofpbuf_uninit(&ctx->stack);
2956 ctx->stack = old_stack;
2958 /* Restore calling bridge's lookup version. */
2959 ctx->tables_version = old_version;
2961 /* The peer bridge popping MPLS should have no effect on the original
2963 ctx->was_mpls = old_was_mpls;
2965 /* The fact that the peer bridge exits (for any reason) does not mean
2966 * that the original bridge should exit. Specifically, if the peer
2967 * bridge recirculates (which typically modifies the packet), the
2968 * original bridge must continue processing with the original, not the
2969 * recirculated packet! */
2972 if (ctx->xin->resubmit_stats) {
2973 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2974 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
2976 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
2979 if (ctx->xin->xcache) {
2980 struct xc_entry *entry;
2982 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2983 entry->u.dev.tx = netdev_ref(xport->netdev);
2984 entry->u.dev.rx = netdev_ref(peer->netdev);
2985 entry->u.dev.bfd = bfd_ref(peer->bfd);
2990 flow_vlan_tci = flow->vlan_tci;
2991 flow_pkt_mark = flow->pkt_mark;
2992 flow_nw_tos = flow->nw_tos;
2994 if (count_skb_priorities(xport)) {
2995 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
2996 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
2997 wc->masks.nw_tos |= IP_DSCP_MASK;
2998 flow->nw_tos &= ~IP_DSCP_MASK;
2999 flow->nw_tos |= dscp;
3003 if (xport->is_tunnel) {
3004 /* Save tunnel metadata so that changes made due to
3005 * the Logical (tunnel) Port are not visible for any further
3006 * matches, while explicit set actions on tunnel metadata are.
3008 flow_tnl = flow->tunnel;
3009 odp_port = tnl_port_send(xport->ofport, flow, ctx->wc);
3010 if (odp_port == ODPP_NONE) {
3011 xlate_report(ctx, "Tunneling decided against output");
3012 goto out; /* restore flow_nw_tos */
3014 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
3015 xlate_report(ctx, "Not tunneling to our own address");
3016 goto out; /* restore flow_nw_tos */
3018 if (ctx->xin->resubmit_stats) {
3019 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
3021 if (ctx->xin->xcache) {
3022 struct xc_entry *entry;
3024 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
3025 entry->u.dev.tx = netdev_ref(xport->netdev);
3027 out_port = odp_port;
3028 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3029 xlate_report(ctx, "output to native tunnel");
3030 tnl_push_pop_send = true;
3032 xlate_report(ctx, "output to kernel tunnel");
3033 commit_odp_tunnel_action(flow, &ctx->base_flow, ctx->odp_actions);
3034 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3037 odp_port = xport->odp_port;
3038 out_port = odp_port;
3039 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
3040 ofp_port_t vlandev_port;
3042 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
3043 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
3044 ofp_port, flow->vlan_tci);
3045 if (vlandev_port != ofp_port) {
3046 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
3047 flow->vlan_tci = htons(0);
3052 if (out_port != ODPP_NONE) {
3053 bool use_masked = ctx->xbridge->support.masked_set_action;
3055 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
3060 struct ovs_action_hash *act_hash;
3063 act_hash = nl_msg_put_unspec_uninit(ctx->odp_actions,
3064 OVS_ACTION_ATTR_HASH,
3066 act_hash->hash_alg = xr->hash_alg;
3067 act_hash->hash_basis = xr->hash_basis;
3069 /* Recirc action. */
3070 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC,
3074 if (tnl_push_pop_send) {
3075 build_tunnel_send(ctx, xport, flow, odp_port);
3076 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3078 odp_port_t odp_tnl_port = ODPP_NONE;
3080 /* XXX: Write better Filter for tunnel port. We can use inport
3081 * int tunnel-port flow to avoid these checks completely. */
3082 if (ofp_port == OFPP_LOCAL &&
3083 ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3085 odp_tnl_port = tnl_port_map_lookup(flow, wc);
3088 if (odp_tnl_port != ODPP_NONE) {
3089 nl_msg_put_odp_port(ctx->odp_actions,
3090 OVS_ACTION_ATTR_TUNNEL_POP,
3093 /* Tunnel push-pop action is not compatible with
3095 add_ipfix_output_action(ctx, out_port);
3096 nl_msg_put_odp_port(ctx->odp_actions,
3097 OVS_ACTION_ATTR_OUTPUT,
3103 ctx->sflow_odp_port = odp_port;
3104 ctx->sflow_n_outputs++;
3105 ctx->xout->nf_output_iface = ofp_port;
3110 flow->vlan_tci = flow_vlan_tci;
3111 flow->pkt_mark = flow_pkt_mark;
3112 flow->nw_tos = flow_nw_tos;
3116 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port,
3117 const struct xlate_bond_recirc *xr)
3119 compose_output_action__(ctx, ofp_port, xr, true);
3123 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
3125 struct rule_dpif *old_rule = ctx->rule;
3126 ovs_be64 old_cookie = ctx->rule_cookie;
3127 const struct rule_actions *actions;
3129 if (ctx->xin->resubmit_stats) {
3130 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
3136 ctx->rule_cookie = rule_dpif_get_flow_cookie(rule);
3137 actions = rule_dpif_get_actions(rule);
3138 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
3139 ctx->rule_cookie = old_cookie;
3140 ctx->rule = old_rule;
3145 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
3147 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3149 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
3150 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
3151 MAX_RESUBMIT_RECURSION);
3152 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
3153 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
3154 } else if (ctx->odp_actions->size > UINT16_MAX) {
3155 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
3156 } else if (ctx->stack.size >= 65536) {
3157 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
3166 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
3167 bool may_packet_in, bool honor_table_miss)
3169 /* Check if we need to recirculate before matching in a table. */
3170 if (ctx->was_mpls) {
3171 ctx_trigger_recirculation(ctx);
3174 if (xlate_resubmit_resource_check(ctx)) {
3175 uint8_t old_table_id = ctx->table_id;
3176 struct rule_dpif *rule;
3178 ctx->table_id = table_id;
3180 rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
3181 ctx->tables_version,
3182 &ctx->xin->flow, ctx->xin->wc,
3183 ctx->xin->xcache != NULL,
3184 ctx->xin->resubmit_stats,
3185 &ctx->table_id, in_port,
3186 may_packet_in, honor_table_miss);
3188 if (OVS_UNLIKELY(ctx->xin->resubmit_hook)) {
3189 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse + 1);
3193 /* Fill in the cache entry here instead of xlate_recursively
3194 * to make the reference counting more explicit. We take a
3195 * reference in the lookups above if we are going to cache the
3197 if (ctx->xin->xcache) {
3198 struct xc_entry *entry;
3200 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
3201 entry->u.rule = rule;
3203 xlate_recursively(ctx, rule);
3206 ctx->table_id = old_table_id;
3214 xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
3215 struct ofputil_bucket *bucket)
3217 if (ctx->xin->resubmit_stats) {
3218 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
3220 if (ctx->xin->xcache) {
3221 struct xc_entry *entry;
3223 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
3224 entry->u.group.group = group_dpif_ref(group);
3225 entry->u.group.bucket = bucket;
3230 xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
3232 uint64_t action_list_stub[1024 / 8];
3233 struct ofpbuf action_list, action_set;
3234 struct flow old_flow = ctx->xin->flow;
3235 bool old_was_mpls = ctx->was_mpls;
3237 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
3238 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3240 ofpacts_execute_action_set(&action_list, &action_set);
3242 do_xlate_actions(action_list.data, action_list.size, ctx);
3245 ofpbuf_uninit(&action_set);
3246 ofpbuf_uninit(&action_list);
3248 /* Check if need to recirculate. */
3249 if (exit_recirculates(ctx)) {
3250 compose_recirculate_action(ctx);
3253 /* Roll back flow to previous state.
3254 * This is equivalent to cloning the packet for each bucket.
3256 * As a side effect any subsequently applied actions will
3257 * also effectively be applied to a clone of the packet taken
3258 * just before applying the all or indirect group.
3260 * Note that group buckets are action sets, hence they cannot modify the
3261 * main action set. Also any stack actions are ignored when executing an
3262 * action set, so group buckets cannot change the stack either.
3263 * However, we do allow resubmit actions in group buckets, which could
3264 * break the above assumptions. It is up to the controller to not mess up
3265 * with the action_set and stack in the tables resubmitted to from
3267 ctx->xin->flow = old_flow;
3269 /* The group bucket popping MPLS should have no effect after bucket
3271 ctx->was_mpls = old_was_mpls;
3273 /* The fact that the group bucket exits (for any reason) does not mean that
3274 * the translation after the group action should exit. Specifically, if
3275 * the group bucket recirculates (which typically modifies the packet), the
3276 * actions after the group action must continue processing with the
3277 * original, not the recirculated packet! */
3282 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3284 struct ofputil_bucket *bucket;
3285 const struct ovs_list *buckets;
3287 group_dpif_get_buckets(group, &buckets);
3289 LIST_FOR_EACH (bucket, list_node, buckets) {
3290 xlate_group_bucket(ctx, bucket);
3292 xlate_group_stats(ctx, group, NULL);
3296 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3298 struct ofputil_bucket *bucket;
3300 bucket = group_first_live_bucket(ctx, group, 0);
3302 xlate_group_bucket(ctx, bucket);
3303 xlate_group_stats(ctx, group, bucket);
3308 xlate_default_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3310 struct flow_wildcards *wc = ctx->wc;
3311 struct ofputil_bucket *bucket;
3314 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
3315 flow_mask_hash_fields(&ctx->xin->flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
3316 bucket = group_best_live_bucket(ctx, group, basis);
3318 xlate_group_bucket(ctx, bucket);
3319 xlate_group_stats(ctx, group, bucket);
3324 xlate_hash_fields_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3326 struct mf_bitmap hash_fields = MF_BITMAP_INITIALIZER;
3327 const struct field_array *fields;
3328 struct ofputil_bucket *bucket;
3332 fields = group_dpif_get_fields(group);
3333 basis = hash_uint64(group_dpif_get_selection_method_param(group));
3335 /* Determine which fields to hash */
3336 for (i = 0; i < MFF_N_IDS; i++) {
3337 if (bitmap_is_set(fields->used.bm, i)) {
3338 const struct mf_field *mf;
3340 /* If the field is already present in 'hash_fields' then
3341 * this loop has already checked that it and its pre-requisites
3342 * are present in the flow and its pre-requisites have
3343 * already been added to 'hash_fields'. There is nothing more
3344 * to do here and as an optimisation the loop can continue. */
3345 if (bitmap_is_set(hash_fields.bm, i)) {
3351 /* Only hash a field if it and its pre-requisites are present
3353 if (!mf_are_prereqs_ok(mf, &ctx->xin->flow)) {
3357 /* Hash both the field and its pre-requisites */
3358 mf_bitmap_set_field_and_prereqs(mf, &hash_fields);
3362 /* Hash the fields */
3363 for (i = 0; i < MFF_N_IDS; i++) {
3364 if (bitmap_is_set(hash_fields.bm, i)) {
3365 const struct mf_field *mf = mf_from_id(i);
3366 union mf_value value;
3369 mf_get_value(mf, &ctx->xin->flow, &value);
3370 /* This seems inefficient but so does apply_mask() */
3371 for (j = 0; j < mf->n_bytes; j++) {
3372 ((uint8_t *) &value)[j] &= ((uint8_t *) &fields->value[i])[j];
3374 basis = hash_bytes(&value, mf->n_bytes, basis);
3376 mf_mask_field(mf, &ctx->wc->masks);
3380 bucket = group_best_live_bucket(ctx, group, basis);
3382 xlate_group_bucket(ctx, bucket);
3383 xlate_group_stats(ctx, group, bucket);
3388 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3390 const char *selection_method = group_dpif_get_selection_method(group);
3392 if (selection_method[0] == '\0') {
3393 xlate_default_select_group(ctx, group);
3394 } else if (!strcasecmp("hash", selection_method)) {
3395 xlate_hash_fields_select_group(ctx, group);
3397 /* Parsing of groups should ensure this never happens */
3403 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3405 bool was_in_group = ctx->in_group;
3406 ctx->in_group = true;
3408 switch (group_dpif_get_type(group)) {
3410 case OFPGT11_INDIRECT:
3411 xlate_all_group(ctx, group);
3413 case OFPGT11_SELECT:
3414 xlate_select_group(ctx, group);
3417 xlate_ff_group(ctx, group);
3422 group_dpif_unref(group);
3424 ctx->in_group = was_in_group;
3428 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3430 if (xlate_resubmit_resource_check(ctx)) {
3431 struct group_dpif *group;
3434 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
3436 xlate_group_action__(ctx, group);
3446 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
3447 const struct ofpact_resubmit *resubmit)
3451 bool may_packet_in = false;
3452 bool honor_table_miss = false;
3454 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
3455 /* Still allow missed packets to be sent to the controller
3456 * if resubmitting from an internal table. */
3457 may_packet_in = true;
3458 honor_table_miss = true;
3461 in_port = resubmit->in_port;
3462 if (in_port == OFPP_IN_PORT) {
3463 in_port = ctx->xin->flow.in_port.ofp_port;
3466 table_id = resubmit->table_id;
3467 if (table_id == 255) {
3468 table_id = ctx->table_id;
3471 xlate_table_action(ctx, in_port, table_id, may_packet_in,
3476 flood_packets(struct xlate_ctx *ctx, bool all)
3478 const struct xport *xport;
3480 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
3481 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
3486 compose_output_action__(ctx, xport->ofp_port, NULL, false);
3487 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
3488 compose_output_action(ctx, xport->ofp_port, NULL);
3492 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3496 execute_controller_action(struct xlate_ctx *ctx, int len,
3497 enum ofp_packet_in_reason reason,
3498 uint16_t controller_id)
3500 struct ofproto_packet_in *pin;
3501 struct dp_packet *packet;
3504 ctx->xout->slow |= SLOW_CONTROLLER;
3505 if (!ctx->xin->packet) {
3509 packet = dp_packet_clone(ctx->xin->packet);
3511 use_masked = ctx->xbridge->support.masked_set_action;
3512 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3514 ctx->wc, use_masked);
3516 odp_execute_actions(NULL, &packet, 1, false,
3517 ctx->odp_actions->data, ctx->odp_actions->size, NULL);
3519 pin = xmalloc(sizeof *pin);
3520 pin->up.packet_len = dp_packet_size(packet);
3521 pin->up.packet = dp_packet_steal_data(packet);
3522 pin->up.reason = reason;
3523 pin->up.table_id = ctx->table_id;
3524 pin->up.cookie = ctx->rule_cookie;
3526 flow_get_metadata(&ctx->xin->flow, &pin->up.flow_metadata);
3528 pin->controller_id = controller_id;
3529 pin->send_len = len;
3530 /* If a rule is a table-miss rule then this is
3531 * a table-miss handled by a table-miss rule.
3533 * Else, if rule is internal and has a controller action,
3534 * the later being implied by the rule being processed here,
3535 * then this is a table-miss handled without a table-miss rule.
3537 * Otherwise this is not a table-miss. */
3538 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
3540 if (rule_dpif_is_table_miss(ctx->rule)) {
3541 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
3542 } else if (rule_dpif_is_internal(ctx->rule)) {
3543 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
3546 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
3547 dp_packet_delete(packet);
3550 /* Called only when ctx->recirc_action_offset is set. */
3552 compose_recirculate_action(struct xlate_ctx *ctx)
3554 struct recirc_metadata md;
3558 use_masked = ctx->xbridge->support.masked_set_action;
3559 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3561 ctx->wc, use_masked);
3563 recirc_metadata_from_flow(&md, &ctx->xin->flow);
3565 ovs_assert(ctx->recirc_action_offset >= 0);
3567 /* Only allocate recirculation ID if we have a packet. */
3568 if (ctx->xin->packet) {
3569 /* Allocate a unique recirc id for the given metadata state in the
3570 * flow. The life-cycle of this recirc id is managed by associating it
3571 * with the udpif key ('ukey') created for each new datapath flow. */
3572 id = recirc_alloc_id_ctx(ctx->xbridge->ofproto, 0, &md, &ctx->stack,
3573 ctx->recirc_action_offset,
3574 ctx->action_set.size, ctx->action_set.data);
3576 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3577 VLOG_ERR_RL(&rl, "Failed to allocate recirculation id");
3580 xlate_out_add_recirc(ctx->xout, id);
3582 /* Look up an existing recirc id for the given metadata state in the
3583 * flow. No new reference is taken, as the ID is RCU protected and is
3584 * only required temporarily for verification. */
3585 id = recirc_find_id(ctx->xbridge->ofproto, 0, &md, &ctx->stack,
3586 ctx->recirc_action_offset,
3587 ctx->action_set.size, ctx->action_set.data);
3588 /* We let zero 'id' to be used in the RECIRC action below, which will
3589 * fail all revalidations as zero is not a valid recirculation ID. */
3592 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
3594 /* Undo changes done by recirculation. */
3595 ctx->action_set.size = ctx->recirc_action_offset;
3596 ctx->recirc_action_offset = -1;
3597 ctx->last_unroll_offset = -1;
3601 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
3603 struct flow *flow = &ctx->xin->flow;
3606 ovs_assert(eth_type_mpls(mpls->ethertype));
3608 n = flow_count_mpls_labels(flow, ctx->wc);
3610 bool use_masked = ctx->xbridge->support.masked_set_action;
3612 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
3614 ctx->wc, use_masked);
3615 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3616 if (ctx->xin->packet != NULL) {
3617 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3618 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3619 "MPLS push action can't be performed as it would "
3620 "have more MPLS LSEs than the %d supported.",
3621 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3627 flow_push_mpls(flow, n, mpls->ethertype, ctx->wc);
3631 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
3633 struct flow *flow = &ctx->xin->flow;
3634 int n = flow_count_mpls_labels(flow, ctx->wc);
3636 if (flow_pop_mpls(flow, n, eth_type, ctx->wc)) {
3637 if (ctx->xbridge->support.odp.recirc) {
3638 ctx->was_mpls = true;
3640 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3641 if (ctx->xin->packet != NULL) {
3642 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3643 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3644 "MPLS pop action can't be performed as it has "
3645 "more MPLS LSEs than the %d supported.",
3646 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3649 ofpbuf_clear(ctx->odp_actions);
3654 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3656 struct flow *flow = &ctx->xin->flow;
3658 if (!is_ip_any(flow)) {
3662 ctx->wc->masks.nw_ttl = 0xff;
3663 if (flow->nw_ttl > 1) {
3669 for (i = 0; i < ids->n_controllers; i++) {
3670 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3674 /* Stop processing for current table. */
3680 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3682 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3683 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
3684 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
3689 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3691 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3692 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
3693 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
3698 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
3700 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3701 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3702 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
3707 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
3709 struct flow *flow = &ctx->xin->flow;
3711 if (eth_type_mpls(flow->dl_type)) {
3712 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
3714 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3717 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3720 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
3724 /* Stop processing for current table. */
3729 xlate_output_action(struct xlate_ctx *ctx,
3730 ofp_port_t port, uint16_t max_len, bool may_packet_in)
3732 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
3734 ctx->xout->nf_output_iface = NF_OUT_DROP;
3738 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port, NULL);
3741 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3742 0, may_packet_in, true);
3748 flood_packets(ctx, false);
3751 flood_packets(ctx, true);
3753 case OFPP_CONTROLLER:
3754 execute_controller_action(ctx, max_len,
3755 (ctx->in_group ? OFPR_GROUP
3756 : ctx->in_action_set ? OFPR_ACTION_SET
3764 if (port != ctx->xin->flow.in_port.ofp_port) {
3765 compose_output_action(ctx, port, NULL);
3767 xlate_report(ctx, "skipping output to input port");
3772 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3773 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3774 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3775 ctx->xout->nf_output_iface = prev_nf_output_iface;
3776 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3777 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3778 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3783 xlate_output_reg_action(struct xlate_ctx *ctx,
3784 const struct ofpact_output_reg *or)
3786 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3787 if (port <= UINT16_MAX) {
3788 union mf_subvalue value;
3790 memset(&value, 0xff, sizeof value);
3791 mf_write_subfield_flow(&or->src, &value, &ctx->wc->masks);
3792 xlate_output_action(ctx, u16_to_ofp(port),
3793 or->max_len, false);
3798 xlate_enqueue_action(struct xlate_ctx *ctx,
3799 const struct ofpact_enqueue *enqueue)
3801 ofp_port_t ofp_port = enqueue->port;
3802 uint32_t queue_id = enqueue->queue;
3803 uint32_t flow_priority, priority;
3806 /* Translate queue to priority. */
3807 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
3809 /* Fall back to ordinary output action. */
3810 xlate_output_action(ctx, enqueue->port, 0, false);
3814 /* Check output port. */
3815 if (ofp_port == OFPP_IN_PORT) {
3816 ofp_port = ctx->xin->flow.in_port.ofp_port;
3817 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
3821 /* Add datapath actions. */
3822 flow_priority = ctx->xin->flow.skb_priority;
3823 ctx->xin->flow.skb_priority = priority;
3824 compose_output_action(ctx, ofp_port, NULL);
3825 ctx->xin->flow.skb_priority = flow_priority;
3827 /* Update NetFlow output port. */
3828 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3829 ctx->xout->nf_output_iface = ofp_port;
3830 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3831 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3836 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3838 uint32_t skb_priority;
3840 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
3841 ctx->xin->flow.skb_priority = skb_priority;
3843 /* Couldn't translate queue to a priority. Nothing to do. A warning
3844 * has already been logged. */
3849 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
3851 const struct xbridge *xbridge = xbridge_;
3862 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3865 port = get_ofp_port(xbridge, ofp_port);
3866 return port ? port->may_enable : false;
3871 xlate_bundle_action(struct xlate_ctx *ctx,
3872 const struct ofpact_bundle *bundle)
3876 port = bundle_execute(bundle, &ctx->xin->flow, ctx->wc, slave_enabled_cb,
3877 CONST_CAST(struct xbridge *, ctx->xbridge));
3878 if (bundle->dst.field) {
3879 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow, ctx->wc);
3881 xlate_output_action(ctx, port, 0, false);
3886 xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
3887 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
3889 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
3890 if (ctx->xin->may_learn) {
3891 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
3896 xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
3898 ctx->xout->has_learn = true;
3899 learn_mask(learn, ctx->wc);
3901 if (ctx->xin->xcache) {
3902 struct xc_entry *entry;
3904 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
3905 entry->u.learn.ofproto = ctx->xbridge->ofproto;
3906 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
3907 entry->u.learn.ofpacts = ofpbuf_new(64);
3908 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
3909 entry->u.learn.ofpacts);
3910 } else if (ctx->xin->may_learn) {
3911 uint64_t ofpacts_stub[1024 / 8];
3912 struct ofputil_flow_mod fm;
3913 struct ofpbuf ofpacts;
3915 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3916 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
3917 ofpbuf_uninit(&ofpacts);
3922 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
3923 uint16_t idle_timeout, uint16_t hard_timeout)
3925 if (tcp_flags & (TCP_FIN | TCP_RST)) {
3926 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
3931 xlate_fin_timeout(struct xlate_ctx *ctx,
3932 const struct ofpact_fin_timeout *oft)
3935 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
3936 oft->fin_idle_timeout, oft->fin_hard_timeout);
3937 if (ctx->xin->xcache) {
3938 struct xc_entry *entry;
3940 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
3941 /* XC_RULE already holds a reference on the rule, none is taken
3943 entry->u.fin.rule = ctx->rule;
3944 entry->u.fin.idle = oft->fin_idle_timeout;
3945 entry->u.fin.hard = oft->fin_hard_timeout;
3951 xlate_sample_action(struct xlate_ctx *ctx,
3952 const struct ofpact_sample *os)
3954 union user_action_cookie cookie;
3955 /* Scale the probability from 16-bit to 32-bit while representing
3956 * the same percentage. */
3957 uint32_t probability = (os->probability << 16) | os->probability;
3960 if (!ctx->xbridge->support.variable_length_userdata) {
3961 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3963 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
3964 "lacks support (needs Linux 3.10+ or kernel module from "
3969 use_masked = ctx->xbridge->support.masked_set_action;
3970 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3972 ctx->wc, use_masked);
3974 compose_flow_sample_cookie(os->probability, os->collector_set_id,
3975 os->obs_domain_id, os->obs_point_id, &cookie);
3976 compose_sample_action(ctx->xbridge, ctx->odp_actions,
3977 &ctx->xin->flow, probability, &cookie,
3978 sizeof cookie.flow_sample, ODPP_NONE,
3983 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
3985 if (xport->config & (is_stp(&ctx->xin->flow)
3986 ? OFPUTIL_PC_NO_RECV_STP
3987 : OFPUTIL_PC_NO_RECV)) {
3991 /* Only drop packets here if both forwarding and learning are
3992 * disabled. If just learning is enabled, we need to have
3993 * OFPP_NORMAL and the learning action have a look at the packet
3994 * before we can drop it. */
3995 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
3996 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
4004 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
4006 const struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
4007 size_t on_len = ofpact_nest_get_action_len(on);
4008 const struct ofpact *inner;
4010 /* Maintain actset_output depending on the contents of the action set:
4012 * - OFPP_UNSET, if there is no "output" action.
4014 * - The output port, if there is an "output" action and no "group"
4017 * - OFPP_UNSET, if there is a "group" action.
4019 if (!ctx->action_set_has_group) {
4020 OFPACT_FOR_EACH (inner, on->actions, on_len) {
4021 if (inner->type == OFPACT_OUTPUT) {
4022 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(inner)->port;
4023 } else if (inner->type == OFPACT_GROUP) {
4024 ctx->xin->flow.actset_output = OFPP_UNSET;
4025 ctx->action_set_has_group = true;
4031 ofpbuf_put(&ctx->action_set, on->actions, on_len);
4032 ofpact_pad(&ctx->action_set);
4036 xlate_action_set(struct xlate_ctx *ctx)
4038 uint64_t action_list_stub[1024 / 64];
4039 struct ofpbuf action_list;
4041 ctx->in_action_set = true;
4042 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
4043 ofpacts_execute_action_set(&action_list, &ctx->action_set);
4044 /* Clear the action set, as it is not needed any more. */
4045 ofpbuf_clear(&ctx->action_set);
4046 do_xlate_actions(action_list.data, action_list.size, ctx);
4047 ctx->in_action_set = false;
4048 ofpbuf_uninit(&action_list);
4052 recirc_put_unroll_xlate(struct xlate_ctx *ctx)
4054 struct ofpact_unroll_xlate *unroll;
4056 unroll = ctx->last_unroll_offset < 0
4058 : ALIGNED_CAST(struct ofpact_unroll_xlate *,
4059 (char *)ctx->action_set.data + ctx->last_unroll_offset);
4061 /* Restore the table_id and rule cookie for a potential PACKET
4064 (ctx->table_id != unroll->rule_table_id
4065 || ctx->rule_cookie != unroll->rule_cookie)) {
4067 ctx->last_unroll_offset = ctx->action_set.size;
4068 unroll = ofpact_put_UNROLL_XLATE(&ctx->action_set);
4069 unroll->rule_table_id = ctx->table_id;
4070 unroll->rule_cookie = ctx->rule_cookie;
4075 /* Copy remaining actions to the action_set to be executed after recirculation.
4076 * UNROLL_XLATE action is inserted, if not already done so, before actions that
4077 * may generate PACKET_INs from the current table and without matching another
4080 recirc_unroll_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4081 struct xlate_ctx *ctx)
4083 const struct ofpact *a;
4085 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4087 /* May generate PACKET INs. */
4088 case OFPACT_OUTPUT_REG:
4091 case OFPACT_CONTROLLER:
4092 case OFPACT_DEC_MPLS_TTL:
4093 case OFPACT_DEC_TTL:
4094 recirc_put_unroll_xlate(ctx);
4097 /* These may not generate PACKET INs. */
4098 case OFPACT_SET_TUNNEL:
4099 case OFPACT_REG_MOVE:
4100 case OFPACT_SET_FIELD:
4101 case OFPACT_STACK_PUSH:
4102 case OFPACT_STACK_POP:
4104 case OFPACT_WRITE_METADATA:
4105 case OFPACT_RESUBMIT: /* May indirectly generate PACKET INs, */
4106 case OFPACT_GOTO_TABLE: /* but from a different table and rule. */
4107 case OFPACT_ENQUEUE:
4108 case OFPACT_SET_VLAN_VID:
4109 case OFPACT_SET_VLAN_PCP:
4110 case OFPACT_STRIP_VLAN:
4111 case OFPACT_PUSH_VLAN:
4112 case OFPACT_SET_ETH_SRC:
4113 case OFPACT_SET_ETH_DST:
4114 case OFPACT_SET_IPV4_SRC:
4115 case OFPACT_SET_IPV4_DST:
4116 case OFPACT_SET_IP_DSCP:
4117 case OFPACT_SET_IP_ECN:
4118 case OFPACT_SET_IP_TTL:
4119 case OFPACT_SET_L4_SRC_PORT:
4120 case OFPACT_SET_L4_DST_PORT:
4121 case OFPACT_SET_QUEUE:
4122 case OFPACT_POP_QUEUE:
4123 case OFPACT_PUSH_MPLS:
4124 case OFPACT_POP_MPLS:
4125 case OFPACT_SET_MPLS_LABEL:
4126 case OFPACT_SET_MPLS_TC:
4127 case OFPACT_SET_MPLS_TTL:
4128 case OFPACT_MULTIPATH:
4131 case OFPACT_UNROLL_XLATE:
4132 case OFPACT_FIN_TIMEOUT:
4133 case OFPACT_CLEAR_ACTIONS:
4134 case OFPACT_WRITE_ACTIONS:
4139 /* These need not be copied for restoration. */
4141 case OFPACT_CONJUNCTION:
4144 /* Copy the action over. */
4145 ofpbuf_put(&ctx->action_set, a, OFPACT_ALIGN(a->len));
4149 #define CHECK_MPLS_RECIRCULATION() \
4150 if (ctx->was_mpls) { \
4151 ctx_trigger_recirculation(ctx); \
4154 #define CHECK_MPLS_RECIRCULATION_IF(COND) \
4156 CHECK_MPLS_RECIRCULATION(); \
4160 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4161 struct xlate_ctx *ctx)
4163 struct flow_wildcards *wc = ctx->wc;
4164 struct flow *flow = &ctx->xin->flow;
4165 const struct ofpact *a;
4167 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
4168 tnl_arp_snoop(flow, wc, ctx->xbridge->name);
4170 /* dl_type already in the mask, not set below. */
4172 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4173 struct ofpact_controller *controller;
4174 const struct ofpact_metadata *metadata;
4175 const struct ofpact_set_field *set_field;
4176 const struct mf_field *mf;
4179 /* Check if need to store the remaining actions for later
4181 if (exit_recirculates(ctx)) {
4182 recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
4184 (uint8_t *)ofpacts)),
4192 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
4193 ofpact_get_OUTPUT(a)->max_len, true);
4197 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
4198 /* Group could not be found. */
4203 case OFPACT_CONTROLLER:
4204 controller = ofpact_get_CONTROLLER(a);
4205 execute_controller_action(ctx, controller->max_len,
4207 controller->controller_id);
4210 case OFPACT_ENQUEUE:
4211 memset(&wc->masks.skb_priority, 0xff,
4212 sizeof wc->masks.skb_priority);
4213 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
4216 case OFPACT_SET_VLAN_VID:
4217 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
4218 if (flow->vlan_tci & htons(VLAN_CFI) ||
4219 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
4220 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
4221 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
4226 case OFPACT_SET_VLAN_PCP:
4227 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
4228 if (flow->vlan_tci & htons(VLAN_CFI) ||
4229 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
4230 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
4231 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
4232 << VLAN_PCP_SHIFT) | VLAN_CFI);
4236 case OFPACT_STRIP_VLAN:
4237 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4238 flow->vlan_tci = htons(0);
4241 case OFPACT_PUSH_VLAN:
4242 /* XXX 802.1AD(QinQ) */
4243 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4244 flow->vlan_tci = htons(VLAN_CFI);
4247 case OFPACT_SET_ETH_SRC:
4248 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
4249 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
4252 case OFPACT_SET_ETH_DST:
4253 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
4254 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
4257 case OFPACT_SET_IPV4_SRC:
4258 CHECK_MPLS_RECIRCULATION();
4259 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4260 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
4261 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
4265 case OFPACT_SET_IPV4_DST:
4266 CHECK_MPLS_RECIRCULATION();
4267 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4268 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
4269 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
4273 case OFPACT_SET_IP_DSCP:
4274 CHECK_MPLS_RECIRCULATION();
4275 if (is_ip_any(flow)) {
4276 wc->masks.nw_tos |= IP_DSCP_MASK;
4277 flow->nw_tos &= ~IP_DSCP_MASK;
4278 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
4282 case OFPACT_SET_IP_ECN:
4283 CHECK_MPLS_RECIRCULATION();
4284 if (is_ip_any(flow)) {
4285 wc->masks.nw_tos |= IP_ECN_MASK;
4286 flow->nw_tos &= ~IP_ECN_MASK;
4287 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
4291 case OFPACT_SET_IP_TTL:
4292 CHECK_MPLS_RECIRCULATION();
4293 if (is_ip_any(flow)) {
4294 wc->masks.nw_ttl = 0xff;
4295 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
4299 case OFPACT_SET_L4_SRC_PORT:
4300 CHECK_MPLS_RECIRCULATION();
4301 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4302 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4303 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
4304 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
4308 case OFPACT_SET_L4_DST_PORT:
4309 CHECK_MPLS_RECIRCULATION();
4310 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4311 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4312 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
4313 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
4317 case OFPACT_RESUBMIT:
4318 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
4321 case OFPACT_SET_TUNNEL:
4322 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
4325 case OFPACT_SET_QUEUE:
4326 memset(&wc->masks.skb_priority, 0xff,
4327 sizeof wc->masks.skb_priority);
4328 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
4331 case OFPACT_POP_QUEUE:
4332 memset(&wc->masks.skb_priority, 0xff,
4333 sizeof wc->masks.skb_priority);
4334 flow->skb_priority = ctx->orig_skb_priority;
4337 case OFPACT_REG_MOVE:
4338 CHECK_MPLS_RECIRCULATION_IF(
4339 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
4340 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
4341 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
4344 case OFPACT_SET_FIELD:
4345 CHECK_MPLS_RECIRCULATION_IF(
4346 mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field));
4347 set_field = ofpact_get_SET_FIELD(a);
4348 mf = set_field->field;
4350 /* Set field action only ever overwrites packet's outermost
4351 * applicable header fields. Do nothing if no header exists. */
4352 if (mf->id == MFF_VLAN_VID) {
4353 wc->masks.vlan_tci |= htons(VLAN_CFI);
4354 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4357 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
4358 /* 'dl_type' is already unwildcarded. */
4359 && !eth_type_mpls(flow->dl_type)) {
4362 /* A flow may wildcard nw_frag. Do nothing if setting a trasport
4363 * header field on a packet that does not have them. */
4364 mf_mask_field_and_prereqs(mf, &wc->masks);
4365 if (mf_are_prereqs_ok(mf, flow)) {
4366 mf_set_flow_value_masked(mf, &set_field->value,
4367 &set_field->mask, flow);
4371 case OFPACT_STACK_PUSH:
4372 CHECK_MPLS_RECIRCULATION_IF(
4373 mf_is_l3_or_higher(ofpact_get_STACK_PUSH(a)->subfield.field));
4374 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
4378 case OFPACT_STACK_POP:
4379 CHECK_MPLS_RECIRCULATION_IF(
4380 mf_is_l3_or_higher(ofpact_get_STACK_POP(a)->subfield.field));
4381 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
4385 case OFPACT_PUSH_MPLS:
4386 /* Recirculate if it is an IP packet with a zero ttl. This may
4387 * indicate that the packet was previously MPLS and an MPLS pop
4388 * action converted it to IP. In this case recirculating should
4389 * reveal the IP TTL which is used as the basis for a new MPLS
4391 CHECK_MPLS_RECIRCULATION_IF(
4392 !flow_count_mpls_labels(flow, wc)
4393 && flow->nw_ttl == 0
4394 && is_ip_any(flow));
4395 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
4398 case OFPACT_POP_MPLS:
4399 CHECK_MPLS_RECIRCULATION();
4400 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
4403 case OFPACT_SET_MPLS_LABEL:
4404 CHECK_MPLS_RECIRCULATION();
4405 compose_set_mpls_label_action(
4406 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
4409 case OFPACT_SET_MPLS_TC:
4410 CHECK_MPLS_RECIRCULATION();
4411 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
4414 case OFPACT_SET_MPLS_TTL:
4415 CHECK_MPLS_RECIRCULATION();
4416 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
4419 case OFPACT_DEC_MPLS_TTL:
4420 CHECK_MPLS_RECIRCULATION();
4421 if (compose_dec_mpls_ttl_action(ctx)) {
4426 case OFPACT_DEC_TTL:
4427 CHECK_MPLS_RECIRCULATION();
4428 wc->masks.nw_ttl = 0xff;
4429 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
4435 /* Nothing to do. */
4438 case OFPACT_MULTIPATH:
4439 CHECK_MPLS_RECIRCULATION();
4440 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
4444 CHECK_MPLS_RECIRCULATION();
4445 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
4448 case OFPACT_OUTPUT_REG:
4449 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
4453 CHECK_MPLS_RECIRCULATION();
4454 xlate_learn_action(ctx, ofpact_get_LEARN(a));
4457 case OFPACT_CONJUNCTION: {
4458 /* A flow with a "conjunction" action represents part of a special
4459 * kind of "set membership match". Such a flow should not actually
4460 * get executed, but it could via, say, a "packet-out", even though
4461 * that wouldn't be useful. Log it to help debugging. */
4462 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4463 VLOG_INFO_RL(&rl, "executing no-op conjunction action");
4471 case OFPACT_UNROLL_XLATE: {
4472 struct ofpact_unroll_xlate *unroll = ofpact_get_UNROLL_XLATE(a);
4474 /* Restore translation context data that was stored earlier. */
4475 ctx->table_id = unroll->rule_table_id;
4476 ctx->rule_cookie = unroll->rule_cookie;
4479 case OFPACT_FIN_TIMEOUT:
4480 CHECK_MPLS_RECIRCULATION();
4481 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4482 ctx->xout->has_fin_timeout = true;
4483 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
4486 case OFPACT_CLEAR_ACTIONS:
4487 ofpbuf_clear(&ctx->action_set);
4488 ctx->xin->flow.actset_output = OFPP_UNSET;
4489 ctx->action_set_has_group = false;
4492 case OFPACT_WRITE_ACTIONS:
4493 xlate_write_actions(ctx, a);
4496 case OFPACT_WRITE_METADATA:
4497 metadata = ofpact_get_WRITE_METADATA(a);
4498 flow->metadata &= ~metadata->mask;
4499 flow->metadata |= metadata->metadata & metadata->mask;
4503 /* Not implemented yet. */
4506 case OFPACT_GOTO_TABLE: {
4507 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
4509 /* Allow ctx->table_id == TBL_INTERNAL, which will be greater
4510 * than ogt->table_id. This is to allow goto_table actions that
4511 * triggered recirculation: ctx->table_id will be TBL_INTERNAL
4512 * after recirculation. */
4513 ovs_assert(ctx->table_id == TBL_INTERNAL
4514 || ctx->table_id < ogt->table_id);
4515 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
4516 ogt->table_id, true, true);
4521 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
4525 /* Check if need to store this and the remaining actions for later
4527 if (ctx->exit && ctx_first_recirculation_action(ctx)) {
4528 recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
4530 (uint8_t *)ofpacts)),
4538 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
4539 const struct flow *flow, ofp_port_t in_port,
4540 struct rule_dpif *rule, uint16_t tcp_flags,
4541 const struct dp_packet *packet, struct flow_wildcards *wc,
4542 struct ofpbuf *odp_actions)
4544 xin->ofproto = ofproto;
4546 xin->flow.in_port.ofp_port = in_port;
4547 xin->flow.actset_output = OFPP_UNSET;
4548 xin->packet = packet;
4549 xin->may_learn = packet != NULL;
4552 xin->ofpacts = NULL;
4553 xin->ofpacts_len = 0;
4554 xin->tcp_flags = tcp_flags;
4555 xin->resubmit_hook = NULL;
4556 xin->report_hook = NULL;
4557 xin->resubmit_stats = NULL;
4559 xin->odp_actions = odp_actions;
4561 /* Do recirc lookup. */
4562 xin->recirc = flow->recirc_id
4563 ? recirc_id_node_find(flow->recirc_id)
4568 xlate_out_uninit(struct xlate_out *xout)
4571 xlate_out_free_recircs(xout);
4575 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
4576 * into datapath actions, using 'ctx', and discards the datapath actions. */
4578 xlate_actions_for_side_effects(struct xlate_in *xin)
4580 struct xlate_out xout;
4582 xlate_actions(xin, &xout);
4583 xlate_out_uninit(&xout);
4586 static struct skb_priority_to_dscp *
4587 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
4589 struct skb_priority_to_dscp *pdscp;
4592 hash = hash_int(skb_priority, 0);
4593 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
4594 if (pdscp->skb_priority == skb_priority) {
4602 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
4605 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
4606 *dscp = pdscp ? pdscp->dscp : 0;
4607 return pdscp != NULL;
4611 count_skb_priorities(const struct xport *xport)
4613 return hmap_count(&xport->skb_priorities);
4617 clear_skb_priorities(struct xport *xport)
4619 struct skb_priority_to_dscp *pdscp, *next;
4621 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
4622 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
4628 actions_output_to_local_port(const struct xlate_ctx *ctx)
4630 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
4631 const struct nlattr *a;
4634 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->odp_actions->data,
4635 ctx->odp_actions->size) {
4636 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
4637 && nl_attr_get_odp_port(a) == local_odp_port) {
4644 #if defined(__linux__)
4645 /* Returns the maximum number of packets that the Linux kernel is willing to
4646 * queue up internally to certain kinds of software-implemented ports, or the
4647 * default (and rarely modified) value if it cannot be determined. */
4649 netdev_max_backlog(void)
4651 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
4652 static int max_backlog = 1000; /* The normal default value. */
4654 if (ovsthread_once_start(&once)) {
4655 static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
4659 stream = fopen(filename, "r");
4661 VLOG_WARN("%s: open failed (%s)", filename, ovs_strerror(errno));
4663 if (fscanf(stream, "%d", &n) != 1) {
4664 VLOG_WARN("%s: read error", filename);
4665 } else if (n <= 100) {
4666 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
4672 ovsthread_once_done(&once);
4674 VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
4680 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
4683 count_output_actions(const struct ofpbuf *odp_actions)
4685 const struct nlattr *a;
4689 NL_ATTR_FOR_EACH_UNSAFE (a, left, odp_actions->data, odp_actions->size) {
4690 if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
4696 #endif /* defined(__linux__) */
4698 /* Returns true if 'odp_actions' contains more output actions than the datapath
4699 * can reliably handle in one go. On Linux, this is the value of the
4700 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
4701 * packets that the kernel is willing to queue up for processing while the
4702 * datapath is processing a set of actions. */
4704 too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
4707 return (odp_actions->size / NL_A_U32_SIZE > netdev_max_backlog()
4708 && count_output_actions(odp_actions) > netdev_max_backlog());
4710 /* OSes other than Linux might have similar limits, but we don't know how
4711 * to determine them.*/
4716 /* Translates the flow, actions, or rule in 'xin' into datapath actions in
4718 * The caller must take responsibility for eventually freeing 'xout', with
4719 * xlate_out_uninit(). */
4721 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
4723 *xout = (struct xlate_out) {
4727 .has_normal = false,
4728 .has_fin_timeout = false,
4729 .nf_output_iface = NF_OUT_DROP,
4734 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4735 struct xbridge *xbridge = xbridge_lookup(xcfg, xin->ofproto);
4740 struct flow *flow = &xin->flow;
4742 union mf_subvalue stack_stub[1024 / sizeof(union mf_subvalue)];
4743 uint64_t action_set_stub[1024 / 8];
4744 struct flow_wildcards scratch_wc;
4745 uint64_t actions_stub[256 / 8];
4746 struct ofpbuf scratch_actions = OFPBUF_STUB_INITIALIZER(actions_stub);
4747 struct xlate_ctx ctx = {
4751 .orig_tunnel_ip_dst = flow->tunnel.ip_dst,
4753 .stack = OFPBUF_STUB_INITIALIZER(stack_stub),
4755 .wc = xin->wc ? xin->wc : &scratch_wc,
4756 .odp_actions = xin->odp_actions ? xin->odp_actions : &scratch_actions,
4761 .in_action_set = false,
4764 .rule_cookie = OVS_BE64_MAX,
4765 .orig_skb_priority = flow->skb_priority,
4766 .sflow_n_outputs = 0,
4767 .sflow_odp_port = 0,
4768 .user_cookie_offset = 0,
4771 .recirc_action_offset = -1,
4772 .last_unroll_offset = -1,
4776 .action_set_has_group = false,
4777 .action_set = OFPBUF_STUB_INITIALIZER(action_set_stub),
4779 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
4780 ofpbuf_reserve(ctx.odp_actions, NL_A_U32_SIZE);
4782 enum slow_path_reason special;
4783 struct xport *in_port;
4784 struct flow orig_flow;
4788 COVERAGE_INC(xlate_actions);
4790 /* Flow initialization rules:
4791 * - 'base_flow' must match the kernel's view of the packet at the
4792 * time that action processing starts. 'flow' represents any
4793 * transformations we wish to make through actions.
4794 * - By default 'base_flow' and 'flow' are the same since the input
4795 * packet matches the output before any actions are applied.
4796 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
4797 * of the received packet as seen by the kernel. If we later output
4798 * to another device without any modifications this will cause us to
4799 * insert a new tag since the original one was stripped off by the
4801 * - Tunnel metadata as received is retained in 'flow'. This allows
4802 * tunnel metadata matching also in later tables.
4803 * Since a kernel action for setting the tunnel metadata will only be
4804 * generated with actual tunnel output, changing the tunnel metadata
4805 * values in 'flow' (such as tun_id) will only have effect with a later
4806 * tunnel output action.
4807 * - Tunnel 'base_flow' is completely cleared since that is what the
4808 * kernel does. If we wish to maintain the original values an action
4809 * needs to be generated. */
4812 flow_wildcards_init_catchall(ctx.wc);
4813 memset(&ctx.wc->masks.in_port, 0xff, sizeof ctx.wc->masks.in_port);
4814 memset(&ctx.wc->masks.dl_type, 0xff, sizeof ctx.wc->masks.dl_type);
4815 if (is_ip_any(flow)) {
4816 ctx.wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
4818 if (xbridge->support.odp.recirc) {
4819 /* Always exactly match recirc_id when datapath supports
4821 ctx.wc->masks.recirc_id = UINT32_MAX;
4823 if (xbridge->netflow) {
4824 netflow_mask_wc(flow, ctx.wc);
4827 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
4829 tnl_may_send = tnl_xlate_init(flow, xin->wc);
4831 /* The in_port of the original packet before recirculation. */
4832 in_port = get_ofp_port(xbridge, flow->in_port.ofp_port);
4835 const struct recirc_id_node *recirc = xin->recirc;
4837 if (xin->ofpacts_len > 0 || ctx.rule) {
4838 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4840 VLOG_WARN_RL(&rl, "Recirculation conflict (%s)!",
4841 xin->ofpacts_len > 0
4847 /* Set the bridge for post-recirculation processing if needed. */
4848 if (ctx.xbridge->ofproto != recirc->ofproto) {
4849 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4850 const struct xbridge *new_bridge = xbridge_lookup(xcfg,
4853 if (OVS_UNLIKELY(!new_bridge)) {
4854 /* Drop the packet if the bridge cannot be found. */
4855 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4856 VLOG_WARN_RL(&rl, "Recirculation bridge no longer exists.");
4859 ctx.xbridge = new_bridge;
4862 /* Set the post-recirculation table id. Note: A table lookup is done
4863 * only if there are no post-recirculation actions. */
4864 ctx.table_id = recirc->table_id;
4866 /* Restore pipeline metadata. May change flow's in_port and other
4867 * metadata to the values that existed when recirculation was
4869 recirc_metadata_to_flow(&recirc->metadata, flow);
4871 /* Restore stack, if any. */
4872 if (recirc->stack) {
4873 ofpbuf_put(&ctx.stack, recirc->stack->data, recirc->stack->size);
4876 /* Restore action set, if any. */
4877 if (recirc->action_set_len) {
4878 const struct ofpact *a;
4880 ofpbuf_put(&ctx.action_set, recirc->ofpacts,
4881 recirc->action_set_len);
4883 OFPACT_FOR_EACH(a, recirc->ofpacts, recirc->action_set_len) {
4884 if (a->type == OFPACT_GROUP) {
4885 ctx.action_set_has_group = true;
4891 /* Restore recirculation actions. If there are no actions, processing
4892 * will start with a lookup in the table set above. */
4893 if (recirc->ofpacts_len > recirc->action_set_len) {
4894 xin->ofpacts_len = recirc->ofpacts_len - recirc->action_set_len;
4895 xin->ofpacts = recirc->ofpacts +
4896 recirc->action_set_len / sizeof *recirc->ofpacts;
4898 } else if (OVS_UNLIKELY(flow->recirc_id)) {
4899 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4901 VLOG_WARN_RL(&rl, "Recirculation context not found for ID %"PRIx32,
4905 /* The bridge is now known so obtain its table version. */
4906 ctx.tables_version = ofproto_dpif_get_tables_version(ctx.xbridge->ofproto);
4908 if (!xin->ofpacts && !ctx.rule) {
4909 ctx.rule = rule_dpif_lookup_from_table(
4910 ctx.xbridge->ofproto, ctx.tables_version, flow, xin->wc,
4911 ctx.xin->xcache != NULL, ctx.xin->resubmit_stats, &ctx.table_id,
4912 flow->in_port.ofp_port, true, true);
4913 if (ctx.xin->resubmit_stats) {
4914 rule_dpif_credit_stats(ctx.rule, ctx.xin->resubmit_stats);
4916 if (ctx.xin->xcache) {
4917 struct xc_entry *entry;
4919 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
4920 entry->u.rule = ctx.rule;
4923 if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
4924 ctx.xin->resubmit_hook(ctx.xin, ctx.rule, 0);
4927 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
4929 if (mbridge_has_mirrors(xbridge->mbridge)) {
4930 /* Do this conditionally because the copy is expensive enough that it
4931 * shows up in profiles. */
4935 /* Tunnel stats only for non-recirculated packets. */
4936 if (!xin->recirc && in_port && in_port->is_tunnel) {
4937 if (ctx.xin->resubmit_stats) {
4938 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
4940 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
4943 if (ctx.xin->xcache) {
4944 struct xc_entry *entry;
4946 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
4947 entry->u.dev.rx = netdev_ref(in_port->netdev);
4948 entry->u.dev.bfd = bfd_ref(in_port->bfd);
4952 /* Do not perform special processing on recirculated packets,
4953 * as recirculated packets are not really received by the bridge. */
4955 (special = process_special(&ctx, flow, in_port, ctx.xin->packet))) {
4956 ctx.xout->slow |= special;
4958 size_t sample_actions_len;
4960 if (flow->in_port.ofp_port
4961 != vsp_realdev_to_vlandev(xbridge->ofproto,
4962 flow->in_port.ofp_port,
4964 ctx.base_flow.vlan_tci = 0;
4967 /* Sampling is done only for packets really received by the bridge. */
4969 add_sflow_action(&ctx);
4970 add_ipfix_action(&ctx);
4971 sample_actions_len = ctx.odp_actions->size;
4973 sample_actions_len = 0;
4976 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
4977 const struct ofpact *ofpacts;
4981 ofpacts = xin->ofpacts;
4982 ofpacts_len = xin->ofpacts_len;
4983 } else if (ctx.rule) {
4984 const struct rule_actions *actions
4985 = rule_dpif_get_actions(ctx.rule);
4986 ofpacts = actions->ofpacts;
4987 ofpacts_len = actions->ofpacts_len;
4988 ctx.rule_cookie = rule_dpif_get_flow_cookie(ctx.rule);
4993 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
4995 /* We've let OFPP_NORMAL and the learning action look at the
4996 * packet, so drop it now if forwarding is disabled. */
4997 if (in_port && (!xport_stp_forward_state(in_port) ||
4998 !xport_rstp_forward_state(in_port))) {
4999 /* Drop all actions added by do_xlate_actions() above. */
5000 ctx.odp_actions->size = sample_actions_len;
5002 /* Undo changes that may have been done for recirculation. */
5003 if (exit_recirculates(&ctx)) {
5004 ctx.action_set.size = ctx.recirc_action_offset;
5005 ctx.recirc_action_offset = -1;
5006 ctx.last_unroll_offset = -1;
5008 } else if (ctx.action_set.size) {
5009 /* Translate action set only if not dropping the packet and
5010 * not recirculating. */
5011 if (!exit_recirculates(&ctx)) {
5012 xlate_action_set(&ctx);
5015 /* Check if need to recirculate. */
5016 if (exit_recirculates(&ctx)) {
5017 compose_recirculate_action(&ctx);
5021 /* Output only fully processed packets. */
5022 if (!exit_recirculates(&ctx)
5023 && xbridge->has_in_band
5024 && in_band_must_output_to_local_port(flow)
5025 && !actions_output_to_local_port(&ctx)) {
5026 compose_output_action(&ctx, OFPP_LOCAL, NULL);
5030 fix_sflow_action(&ctx);
5032 /* Only mirror fully processed packets. */
5033 if (!exit_recirculates(&ctx)
5034 && mbridge_has_mirrors(xbridge->mbridge)) {
5035 add_mirror_actions(&ctx, &orig_flow);
5039 if (nl_attr_oversized(ctx.odp_actions->size)) {
5040 /* These datapath actions are too big for a Netlink attribute, so we
5041 * can't hand them to the kernel directly. dpif_execute() can execute
5042 * them one by one with help, so just mark the result as SLOW_ACTION to
5043 * prevent the flow from being installed. */
5044 COVERAGE_INC(xlate_actions_oversize);
5045 ctx.xout->slow |= SLOW_ACTION;
5046 } else if (too_many_output_actions(ctx.odp_actions)) {
5047 COVERAGE_INC(xlate_actions_too_many_output);
5048 ctx.xout->slow |= SLOW_ACTION;
5051 /* Update mirror stats only for packets really received by the bridge. */
5052 if (!xin->recirc && mbridge_has_mirrors(xbridge->mbridge)) {
5053 if (ctx.xin->resubmit_stats) {
5054 mirror_update_stats(xbridge->mbridge, xout->mirrors,
5055 ctx.xin->resubmit_stats->n_packets,
5056 ctx.xin->resubmit_stats->n_bytes);
5058 if (ctx.xin->xcache) {
5059 struct xc_entry *entry;
5061 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_MIRROR);
5062 entry->u.mirror.mbridge = mbridge_ref(xbridge->mbridge);
5063 entry->u.mirror.mirrors = xout->mirrors;
5067 /* Do netflow only for packets really received by the bridge and not sent
5068 * to the controller. We consider packets sent to the controller to be
5069 * part of the control plane rather than the data plane. */
5070 if (!xin->recirc && xbridge->netflow && !(xout->slow & SLOW_CONTROLLER)) {
5071 if (ctx.xin->resubmit_stats) {
5072 netflow_flow_update(xbridge->netflow, flow,
5073 xout->nf_output_iface,
5074 ctx.xin->resubmit_stats);
5076 if (ctx.xin->xcache) {
5077 struct xc_entry *entry;
5079 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
5080 entry->u.nf.netflow = netflow_ref(xbridge->netflow);
5081 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
5082 entry->u.nf.iface = xout->nf_output_iface;
5087 /* Clear the metadata and register wildcard masks, because we won't
5088 * use non-header fields as part of the cache. */
5089 flow_wildcards_clear_non_packet_fields(ctx.wc);
5091 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
5092 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
5093 * represent these fields. The datapath interface, on the other hand,
5094 * represents them with just 8 bits each. This means that if the high
5095 * 8 bits of the masks for these fields somehow become set, then they
5096 * will get chopped off by a round trip through the datapath, and
5097 * revalidation will spot that as an inconsistency and delete the flow.
5098 * Avoid the problem here by making sure that only the low 8 bits of
5099 * either field can be unwildcarded for ICMP.
5102 ctx.wc->masks.tp_src &= htons(UINT8_MAX);
5103 ctx.wc->masks.tp_dst &= htons(UINT8_MAX);
5105 /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
5106 if (ctx.wc->masks.vlan_tci) {
5107 ctx.wc->masks.vlan_tci |= htons(VLAN_CFI);
5112 ofpbuf_uninit(&ctx.stack);
5113 ofpbuf_uninit(&ctx.action_set);
5114 ofpbuf_uninit(&scratch_actions);
5117 /* Sends 'packet' out 'ofport'.
5118 * May modify 'packet'.
5119 * Returns 0 if successful, otherwise a positive errno value. */
5121 xlate_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
5123 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5124 struct xport *xport;
5125 struct ofpact_output output;
5128 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
5129 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
5130 flow_extract(packet, &flow);
5131 flow.in_port.ofp_port = OFPP_NONE;
5133 xport = xport_lookup(xcfg, ofport);
5137 output.port = xport->ofp_port;
5140 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
5141 &output.ofpact, sizeof output,
5145 struct xlate_cache *
5146 xlate_cache_new(void)
5148 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
5150 ofpbuf_init(&xcache->entries, 512);
5154 static struct xc_entry *
5155 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
5157 struct xc_entry *entry;
5159 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
5166 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
5168 if (entry->u.dev.tx) {
5169 netdev_vport_inc_tx(entry->u.dev.tx, stats);
5171 if (entry->u.dev.rx) {
5172 netdev_vport_inc_rx(entry->u.dev.rx, stats);
5174 if (entry->u.dev.bfd) {
5175 bfd_account_rx(entry->u.dev.bfd, stats);
5180 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
5182 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5183 struct xbridge *xbridge;
5184 struct xbundle *xbundle;
5185 struct flow_wildcards wc;
5187 xbridge = xbridge_lookup(xcfg, ofproto);
5192 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
5198 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
5201 /* Push stats and perform side effects of flow translation. */
5203 xlate_push_stats(struct xlate_cache *xcache,
5204 const struct dpif_flow_stats *stats)
5206 struct xc_entry *entry;
5207 struct ofpbuf entries = xcache->entries;
5208 uint8_t dmac[ETH_ADDR_LEN];
5210 if (!stats->n_packets) {
5214 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5215 switch (entry->type) {
5217 rule_dpif_credit_stats(entry->u.rule, stats);
5220 bond_account(entry->u.bond.bond, entry->u.bond.flow,
5221 entry->u.bond.vid, stats->n_bytes);
5224 xlate_cache_netdev(entry, stats);
5227 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
5228 entry->u.nf.iface, stats);
5231 mirror_update_stats(entry->u.mirror.mbridge,
5232 entry->u.mirror.mirrors,
5233 stats->n_packets, stats->n_bytes);
5236 ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
5239 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
5240 entry->u.normal.vlan);
5242 case XC_FIN_TIMEOUT:
5243 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
5244 entry->u.fin.idle, entry->u.fin.hard);
5247 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
5251 /* Lookup arp to avoid arp timeout. */
5252 tnl_arp_lookup(entry->u.tnl_arp_cache.br_name, entry->u.tnl_arp_cache.d_ip, dmac);
5261 xlate_dev_unref(struct xc_entry *entry)
5263 if (entry->u.dev.tx) {
5264 netdev_close(entry->u.dev.tx);
5266 if (entry->u.dev.rx) {
5267 netdev_close(entry->u.dev.rx);
5269 if (entry->u.dev.bfd) {
5270 bfd_unref(entry->u.dev.bfd);
5275 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
5277 netflow_flow_clear(netflow, flow);
5278 netflow_unref(netflow);
5283 xlate_cache_clear(struct xlate_cache *xcache)
5285 struct xc_entry *entry;
5286 struct ofpbuf entries;
5292 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5293 switch (entry->type) {
5295 rule_dpif_unref(entry->u.rule);
5298 free(entry->u.bond.flow);
5299 bond_unref(entry->u.bond.bond);
5302 xlate_dev_unref(entry);
5305 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
5308 mbridge_unref(entry->u.mirror.mbridge);
5311 free(entry->u.learn.fm);
5312 ofpbuf_delete(entry->u.learn.ofpacts);
5315 free(entry->u.normal.flow);
5317 case XC_FIN_TIMEOUT:
5318 /* 'u.fin.rule' is always already held as a XC_RULE, which
5319 * has already released it's reference above. */
5322 group_dpif_unref(entry->u.group.group);
5331 ofpbuf_clear(&xcache->entries);
5335 xlate_cache_delete(struct xlate_cache *xcache)
5337 xlate_cache_clear(xcache);
5338 ofpbuf_uninit(&xcache->entries);