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". */
178 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
181 /* The rule that we are currently translating, or NULL. */
182 struct rule_dpif *rule;
184 /* Resubmit statistics, via xlate_table_action(). */
185 int recurse; /* Current resubmit nesting depth. */
186 int resubmits; /* Total number of resubmits. */
187 bool in_group; /* Currently translating ofgroup, if true. */
188 bool in_action_set; /* Currently translating action_set, if true. */
190 uint8_t table_id; /* OpenFlow table ID where flow was found. */
191 ovs_be64 rule_cookie; /* Cookie of the rule being translated. */
192 uint32_t orig_skb_priority; /* Priority when packet arrived. */
193 uint32_t sflow_n_outputs; /* Number of output ports. */
194 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
195 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
196 bool exit; /* No further actions should be processed. */
198 /* These are used for non-bond recirculation. The recirculation IDs are
199 * stored in xout and must be associated with a datapath flow (ukey),
200 * otherwise they will be freed when the xout is uninitialized.
203 * Steps in Recirculation Translation
204 * ==================================
206 * At some point during translation, the code recognizes the need for
207 * recirculation. For example, recirculation is necessary when, after
208 * popping the last MPLS label, an action or a match tries to examine or
209 * modify a field that has been newly revealed following the MPLS label.
211 * The simplest part of the work to be done is to commit existing changes to
212 * the packet, which produces datapath actions corresponding to the changes,
213 * and after this, add an OVS_ACTION_ATTR_RECIRC datapath action.
215 * The main problem here is preserving state. When the datapath executes
216 * OVS_ACTION_ATTR_RECIRC, it will upcall to userspace to get a translation
217 * for the post-recirculation actions. At this point userspace has to
218 * resume the translation where it left off, which means that it has to
219 * execute the following:
221 * - The action that prompted recirculation, and any actions following
222 * it within the same flow.
224 * - If the action that prompted recirculation was invoked within a
225 * NXAST_RESUBMIT, then any actions following the resubmit. These
226 * "resubmit"s can be nested, so this has to go all the way up the
229 * - The OpenFlow 1.1+ action set.
231 * State that actions and flow table lookups can depend on, such as the
232 * following, must also be preserved:
234 * - Metadata fields (input port, registers, OF1.1+ metadata, ...).
236 * - Action set, stack
238 * - The table ID and cookie of the flow being translated at each level
239 * of the control stack (since OFPAT_CONTROLLER actions send these to
242 * Translation allows for the control of this state preservation via these
243 * members. When a need for recirculation is identified, the translation
246 * 1. Sets 'recirc_action_offset' to the current size of 'action_set'. The
247 * action set is part of what needs to be preserved, so this allows the
248 * action set and the additional state to share the 'action_set' buffer.
249 * Later steps can tell that setup for recirculation is in progress from
250 * the nonnegative value of 'recirc_action_offset'.
252 * 2. Sets 'exit' to true to tell later steps that we're exiting from the
253 * translation process.
255 * 3. Adds an OFPACT_UNROLL_XLATE action to 'action_set'. This action
256 * holds the current table ID and cookie so that they can be restored
257 * during a post-recirculation upcall translation.
259 * 4. Adds the action that prompted recirculation and any actions following
260 * it within the same flow to 'action_set', so that they can be executed
261 * during a post-recirculation upcall translation.
265 * 6. The action that prompted recirculation might be nested in a stack of
266 * nested "resubmit"s that have actions remaining. Each of these notices
267 * that we're exiting (from 'exit') and that recirculation setup is in
268 * progress (from 'recirc_action_offset') and responds by adding more
269 * OFPACT_UNROLL_XLATE actions to 'action_set', as necessary, and any
270 * actions that were yet unprocessed.
272 * The caller stores all the state produced by this process associated with
273 * the recirculation ID. For post-recirculation upcall translation, the
274 * caller passes it back in for the new translation to execute. The
275 * process yielded a set of ofpacts that can be translated directly, so it
276 * is not much of a special case at that point.
278 int recirc_action_offset; /* Offset in 'action_set' to actions to be
279 * executed after recirculation, or -1. */
280 int last_unroll_offset; /* Offset in 'action_set' to the latest unroll
283 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
284 * This is a trigger for recirculation in cases where translating an action
285 * or looking up a flow requires access to the fields of the packet after
286 * the MPLS label stack that was originally present. */
289 /* OpenFlow 1.1+ action set.
291 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
292 * When translation is otherwise complete, ofpacts_execute_action_set()
293 * converts it to a set of "struct ofpact"s that can be translated into
294 * datapath actions. */
295 bool action_set_has_group; /* Action set contains OFPACT_GROUP? */
296 struct ofpbuf action_set; /* Action set. */
297 uint64_t action_set_stub[1024 / 8];
300 static void xlate_action_set(struct xlate_ctx *ctx);
303 ctx_trigger_recirculation(struct xlate_ctx *ctx)
306 ctx->recirc_action_offset = ctx->action_set.size;
310 ctx_first_recirculation_action(const struct xlate_ctx *ctx)
312 return ctx->recirc_action_offset == ctx->action_set.size;
316 exit_recirculates(const struct xlate_ctx *ctx)
318 /* When recirculating the 'recirc_action_offset' has a non-negative value.
320 return ctx->recirc_action_offset >= 0;
323 static void compose_recirculate_action(struct xlate_ctx *ctx);
325 /* A controller may use OFPP_NONE as the ingress port to indicate that
326 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
327 * when an input bundle is needed for validation (e.g., mirroring or
328 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
329 * any 'port' structs, so care must be taken when dealing with it. */
330 static struct xbundle ofpp_none_bundle = {
332 .vlan_mode = PORT_VLAN_TRUNK
335 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
336 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
337 * traffic egressing the 'ofport' with that priority should be marked with. */
338 struct skb_priority_to_dscp {
339 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
340 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
342 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
358 /* xlate_cache entries hold enough information to perform the side effects of
359 * xlate_actions() for a rule, without needing to perform rule translation
360 * from scratch. The primary usage of these is to submit statistics to objects
361 * that a flow relates to, although they may be used for other effects as well
362 * (for instance, refreshing hard timeouts for learned flows). */
366 struct rule_dpif *rule;
373 struct netflow *netflow;
378 struct mbridge *mbridge;
379 mirror_mask_t mirrors;
387 struct ofproto_dpif *ofproto;
388 struct ofputil_flow_mod *fm;
389 struct ofpbuf *ofpacts;
392 struct ofproto_dpif *ofproto;
397 struct rule_dpif *rule;
402 struct group_dpif *group;
403 struct ofputil_bucket *bucket;
406 char br_name[IFNAMSIZ];
412 #define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
413 entries = xcache->entries; \
414 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
416 entry = ofpbuf_try_pull(&entries, sizeof *entry))
419 struct ofpbuf entries;
422 /* Xlate config contains hash maps of all bridges, bundles and ports.
423 * Xcfgp contains the pointer to the current xlate configuration.
424 * When the main thread needs to change the configuration, it copies xcfgp to
425 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
426 * does not block handler and revalidator threads. */
428 struct hmap xbridges;
429 struct hmap xbundles;
432 static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
433 static struct xlate_cfg *new_xcfg = NULL;
435 static bool may_receive(const struct xport *, struct xlate_ctx *);
436 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
438 static void xlate_normal(struct xlate_ctx *);
439 static inline void xlate_report(struct xlate_ctx *, const char *);
440 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
441 uint8_t table_id, bool may_packet_in,
442 bool honor_table_miss);
443 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
444 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
445 static void output_normal(struct xlate_ctx *, const struct xbundle *,
448 /* Optional bond recirculation parameter to compose_output_action(). */
449 struct xlate_bond_recirc {
450 uint32_t recirc_id; /* !0 Use recirculation instead of output. */
451 uint8_t hash_alg; /* !0 Compute hash for recirc before. */
452 uint32_t hash_basis; /* Compute hash for recirc before. */
455 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port,
456 const struct xlate_bond_recirc *xr);
458 static struct xbridge *xbridge_lookup(struct xlate_cfg *,
459 const struct ofproto_dpif *);
460 static struct xbundle *xbundle_lookup(struct xlate_cfg *,
461 const struct ofbundle *);
462 static struct xport *xport_lookup(struct xlate_cfg *,
463 const struct ofport_dpif *);
464 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
465 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
466 uint32_t skb_priority);
467 static void clear_skb_priorities(struct xport *);
468 static size_t count_skb_priorities(const struct xport *);
469 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
472 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
474 static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
475 static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
476 static void xlate_xport_init(struct xlate_cfg *, struct xport *);
477 static void xlate_xbridge_set(struct xbridge *, struct dpif *,
478 const struct mac_learning *, struct stp *,
479 struct rstp *, const struct mcast_snooping *,
480 const struct mbridge *,
481 const struct dpif_sflow *,
482 const struct dpif_ipfix *,
483 const struct netflow *,
484 bool forward_bpdu, bool has_in_band,
485 const struct dpif_backer_support *);
486 static void xlate_xbundle_set(struct xbundle *xbundle,
487 enum port_vlan_mode vlan_mode, int vlan,
488 unsigned long *trunks, bool use_priority_tags,
489 const struct bond *bond, const struct lacp *lacp,
491 static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
492 const struct netdev *netdev, const struct cfm *cfm,
493 const struct bfd *bfd, const struct lldp *lldp,
494 int stp_port_no, const struct rstp_port *rstp_port,
495 enum ofputil_port_config config,
496 enum ofputil_port_state state, bool is_tunnel,
498 static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
499 static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
500 static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
501 static void xlate_xbridge_copy(struct xbridge *);
502 static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
503 static void xlate_xport_copy(struct xbridge *, struct xbundle *,
505 static void xlate_xcfg_free(struct xlate_cfg *);
508 xlate_report(struct xlate_ctx *ctx, const char *s)
510 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
511 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
516 xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
518 list_init(&xbridge->xbundles);
519 hmap_init(&xbridge->xports);
520 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
521 hash_pointer(xbridge->ofproto, 0));
525 xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
527 list_init(&xbundle->xports);
528 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
529 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
530 hash_pointer(xbundle->ofbundle, 0));
534 xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
536 hmap_init(&xport->skb_priorities);
537 hmap_insert(&xcfg->xports, &xport->hmap_node,
538 hash_pointer(xport->ofport, 0));
539 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
540 hash_ofp_port(xport->ofp_port));
544 xlate_xbridge_set(struct xbridge *xbridge,
546 const struct mac_learning *ml, struct stp *stp,
547 struct rstp *rstp, const struct mcast_snooping *ms,
548 const struct mbridge *mbridge,
549 const struct dpif_sflow *sflow,
550 const struct dpif_ipfix *ipfix,
551 const struct netflow *netflow,
552 bool forward_bpdu, bool has_in_band,
553 const struct dpif_backer_support *support)
555 if (xbridge->ml != ml) {
556 mac_learning_unref(xbridge->ml);
557 xbridge->ml = mac_learning_ref(ml);
560 if (xbridge->ms != ms) {
561 mcast_snooping_unref(xbridge->ms);
562 xbridge->ms = mcast_snooping_ref(ms);
565 if (xbridge->mbridge != mbridge) {
566 mbridge_unref(xbridge->mbridge);
567 xbridge->mbridge = mbridge_ref(mbridge);
570 if (xbridge->sflow != sflow) {
571 dpif_sflow_unref(xbridge->sflow);
572 xbridge->sflow = dpif_sflow_ref(sflow);
575 if (xbridge->ipfix != ipfix) {
576 dpif_ipfix_unref(xbridge->ipfix);
577 xbridge->ipfix = dpif_ipfix_ref(ipfix);
580 if (xbridge->stp != stp) {
581 stp_unref(xbridge->stp);
582 xbridge->stp = stp_ref(stp);
585 if (xbridge->rstp != rstp) {
586 rstp_unref(xbridge->rstp);
587 xbridge->rstp = rstp_ref(rstp);
590 if (xbridge->netflow != netflow) {
591 netflow_unref(xbridge->netflow);
592 xbridge->netflow = netflow_ref(netflow);
595 xbridge->dpif = dpif;
596 xbridge->forward_bpdu = forward_bpdu;
597 xbridge->has_in_band = has_in_band;
598 xbridge->support = *support;
602 xlate_xbundle_set(struct xbundle *xbundle,
603 enum port_vlan_mode vlan_mode, int vlan,
604 unsigned long *trunks, bool use_priority_tags,
605 const struct bond *bond, const struct lacp *lacp,
608 ovs_assert(xbundle->xbridge);
610 xbundle->vlan_mode = vlan_mode;
611 xbundle->vlan = vlan;
612 xbundle->trunks = trunks;
613 xbundle->use_priority_tags = use_priority_tags;
614 xbundle->floodable = floodable;
616 if (xbundle->bond != bond) {
617 bond_unref(xbundle->bond);
618 xbundle->bond = bond_ref(bond);
621 if (xbundle->lacp != lacp) {
622 lacp_unref(xbundle->lacp);
623 xbundle->lacp = lacp_ref(lacp);
628 xlate_xport_set(struct xport *xport, odp_port_t odp_port,
629 const struct netdev *netdev, const struct cfm *cfm,
630 const struct bfd *bfd, const struct lldp *lldp, int stp_port_no,
631 const struct rstp_port* rstp_port,
632 enum ofputil_port_config config, enum ofputil_port_state state,
633 bool is_tunnel, bool may_enable)
635 xport->config = config;
636 xport->state = state;
637 xport->stp_port_no = stp_port_no;
638 xport->is_tunnel = is_tunnel;
639 xport->may_enable = may_enable;
640 xport->odp_port = odp_port;
642 if (xport->rstp_port != rstp_port) {
643 rstp_port_unref(xport->rstp_port);
644 xport->rstp_port = rstp_port_ref(rstp_port);
647 if (xport->cfm != cfm) {
648 cfm_unref(xport->cfm);
649 xport->cfm = cfm_ref(cfm);
652 if (xport->bfd != bfd) {
653 bfd_unref(xport->bfd);
654 xport->bfd = bfd_ref(bfd);
657 if (xport->lldp != lldp) {
658 lldp_unref(xport->lldp);
659 xport->lldp = lldp_ref(lldp);
662 if (xport->netdev != netdev) {
663 netdev_close(xport->netdev);
664 xport->netdev = netdev_ref(netdev);
669 xlate_xbridge_copy(struct xbridge *xbridge)
671 struct xbundle *xbundle;
673 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
674 new_xbridge->ofproto = xbridge->ofproto;
675 new_xbridge->name = xstrdup(xbridge->name);
676 xlate_xbridge_init(new_xcfg, new_xbridge);
678 xlate_xbridge_set(new_xbridge,
679 xbridge->dpif, xbridge->ml, xbridge->stp,
680 xbridge->rstp, xbridge->ms, xbridge->mbridge,
681 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
682 xbridge->forward_bpdu, xbridge->has_in_band,
684 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
685 xlate_xbundle_copy(new_xbridge, xbundle);
688 /* Copy xports which are not part of a xbundle */
689 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
690 if (!xport->xbundle) {
691 xlate_xport_copy(new_xbridge, NULL, xport);
697 xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
700 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
701 new_xbundle->ofbundle = xbundle->ofbundle;
702 new_xbundle->xbridge = xbridge;
703 new_xbundle->name = xstrdup(xbundle->name);
704 xlate_xbundle_init(new_xcfg, new_xbundle);
706 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
707 xbundle->vlan, xbundle->trunks,
708 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
710 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
711 xlate_xport_copy(xbridge, new_xbundle, xport);
716 xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
719 struct skb_priority_to_dscp *pdscp, *new_pdscp;
720 struct xport *new_xport = xzalloc(sizeof *xport);
721 new_xport->ofport = xport->ofport;
722 new_xport->ofp_port = xport->ofp_port;
723 new_xport->xbridge = xbridge;
724 xlate_xport_init(new_xcfg, new_xport);
726 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
727 xport->bfd, xport->lldp, xport->stp_port_no,
728 xport->rstp_port, xport->config, xport->state,
729 xport->is_tunnel, xport->may_enable);
732 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
734 new_xport->peer = peer;
735 new_xport->peer->peer = new_xport;
740 new_xport->xbundle = xbundle;
741 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
744 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
745 new_pdscp = xmalloc(sizeof *pdscp);
746 new_pdscp->skb_priority = pdscp->skb_priority;
747 new_pdscp->dscp = pdscp->dscp;
748 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
749 hash_int(new_pdscp->skb_priority, 0));
753 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
754 * configuration in xcfgp.
756 * This needs to be called after editing the xlate configuration.
758 * Functions that edit the new xlate configuration are
759 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
765 * edit_xlate_configuration();
767 * xlate_txn_commit(); */
769 xlate_txn_commit(void)
771 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
773 ovsrcu_set(&xcfgp, new_xcfg);
774 ovsrcu_synchronize();
775 xlate_xcfg_free(xcfg);
779 /* Copies the current xlate configuration in xcfgp to new_xcfg.
781 * This needs to be called prior to editing the xlate configuration. */
783 xlate_txn_start(void)
785 struct xbridge *xbridge;
786 struct xlate_cfg *xcfg;
788 ovs_assert(!new_xcfg);
790 new_xcfg = xmalloc(sizeof *new_xcfg);
791 hmap_init(&new_xcfg->xbridges);
792 hmap_init(&new_xcfg->xbundles);
793 hmap_init(&new_xcfg->xports);
795 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
800 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
801 xlate_xbridge_copy(xbridge);
807 xlate_xcfg_free(struct xlate_cfg *xcfg)
809 struct xbridge *xbridge, *next_xbridge;
815 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
816 xlate_xbridge_remove(xcfg, xbridge);
819 hmap_destroy(&xcfg->xbridges);
820 hmap_destroy(&xcfg->xbundles);
821 hmap_destroy(&xcfg->xports);
826 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
828 const struct mac_learning *ml, struct stp *stp,
829 struct rstp *rstp, const struct mcast_snooping *ms,
830 const struct mbridge *mbridge,
831 const struct dpif_sflow *sflow,
832 const struct dpif_ipfix *ipfix,
833 const struct netflow *netflow,
834 bool forward_bpdu, bool has_in_band,
835 const struct dpif_backer_support *support)
837 struct xbridge *xbridge;
839 ovs_assert(new_xcfg);
841 xbridge = xbridge_lookup(new_xcfg, ofproto);
843 xbridge = xzalloc(sizeof *xbridge);
844 xbridge->ofproto = ofproto;
846 xlate_xbridge_init(new_xcfg, xbridge);
850 xbridge->name = xstrdup(name);
852 xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
853 netflow, forward_bpdu, has_in_band, support);
857 xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
859 struct xbundle *xbundle, *next_xbundle;
860 struct xport *xport, *next_xport;
866 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
867 xlate_xport_remove(xcfg, xport);
870 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
871 xlate_xbundle_remove(xcfg, xbundle);
874 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
875 mac_learning_unref(xbridge->ml);
876 mcast_snooping_unref(xbridge->ms);
877 mbridge_unref(xbridge->mbridge);
878 dpif_sflow_unref(xbridge->sflow);
879 dpif_ipfix_unref(xbridge->ipfix);
880 stp_unref(xbridge->stp);
881 rstp_unref(xbridge->rstp);
882 hmap_destroy(&xbridge->xports);
888 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
890 struct xbridge *xbridge;
892 ovs_assert(new_xcfg);
894 xbridge = xbridge_lookup(new_xcfg, ofproto);
895 xlate_xbridge_remove(new_xcfg, xbridge);
899 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
900 const char *name, enum port_vlan_mode vlan_mode, int vlan,
901 unsigned long *trunks, bool use_priority_tags,
902 const struct bond *bond, const struct lacp *lacp,
905 struct xbundle *xbundle;
907 ovs_assert(new_xcfg);
909 xbundle = xbundle_lookup(new_xcfg, ofbundle);
911 xbundle = xzalloc(sizeof *xbundle);
912 xbundle->ofbundle = ofbundle;
913 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
915 xlate_xbundle_init(new_xcfg, xbundle);
919 xbundle->name = xstrdup(name);
921 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
922 use_priority_tags, bond, lacp, floodable);
926 xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
934 LIST_FOR_EACH_POP (xport, bundle_node, &xbundle->xports) {
935 xport->xbundle = NULL;
938 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
939 list_remove(&xbundle->list_node);
940 bond_unref(xbundle->bond);
941 lacp_unref(xbundle->lacp);
947 xlate_bundle_remove(struct ofbundle *ofbundle)
949 struct xbundle *xbundle;
951 ovs_assert(new_xcfg);
953 xbundle = xbundle_lookup(new_xcfg, ofbundle);
954 xlate_xbundle_remove(new_xcfg, xbundle);
958 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
959 struct ofport_dpif *ofport, ofp_port_t ofp_port,
960 odp_port_t odp_port, const struct netdev *netdev,
961 const struct cfm *cfm, const struct bfd *bfd,
962 const struct lldp *lldp, struct ofport_dpif *peer,
963 int stp_port_no, const struct rstp_port *rstp_port,
964 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
965 enum ofputil_port_config config,
966 enum ofputil_port_state state, bool is_tunnel,
972 ovs_assert(new_xcfg);
974 xport = xport_lookup(new_xcfg, ofport);
976 xport = xzalloc(sizeof *xport);
977 xport->ofport = ofport;
978 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
979 xport->ofp_port = ofp_port;
981 xlate_xport_init(new_xcfg, xport);
984 ovs_assert(xport->ofp_port == ofp_port);
986 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, lldp,
987 stp_port_no, rstp_port, config, state, is_tunnel,
991 xport->peer->peer = NULL;
993 xport->peer = xport_lookup(new_xcfg, peer);
995 xport->peer->peer = xport;
998 if (xport->xbundle) {
999 list_remove(&xport->bundle_node);
1001 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
1002 if (xport->xbundle) {
1003 list_insert(&xport->xbundle->xports, &xport->bundle_node);
1006 clear_skb_priorities(xport);
1007 for (i = 0; i < n_qdscp; i++) {
1008 struct skb_priority_to_dscp *pdscp;
1009 uint32_t skb_priority;
1011 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
1016 pdscp = xmalloc(sizeof *pdscp);
1017 pdscp->skb_priority = skb_priority;
1018 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1019 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
1020 hash_int(pdscp->skb_priority, 0));
1025 xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
1032 xport->peer->peer = NULL;
1036 if (xport->xbundle) {
1037 list_remove(&xport->bundle_node);
1040 clear_skb_priorities(xport);
1041 hmap_destroy(&xport->skb_priorities);
1043 hmap_remove(&xcfg->xports, &xport->hmap_node);
1044 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
1046 netdev_close(xport->netdev);
1047 rstp_port_unref(xport->rstp_port);
1048 cfm_unref(xport->cfm);
1049 bfd_unref(xport->bfd);
1050 lldp_unref(xport->lldp);
1055 xlate_ofport_remove(struct ofport_dpif *ofport)
1057 struct xport *xport;
1059 ovs_assert(new_xcfg);
1061 xport = xport_lookup(new_xcfg, ofport);
1062 xlate_xport_remove(new_xcfg, xport);
1065 static struct ofproto_dpif *
1066 xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
1067 ofp_port_t *ofp_in_port, const struct xport **xportp)
1069 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1070 const struct xport *xport;
1072 xport = xport_lookup(xcfg, tnl_port_should_receive(flow)
1073 ? tnl_port_receive(flow)
1074 : odp_port_to_ofport(backer, flow->in_port.odp_port));
1075 if (OVS_UNLIKELY(!xport)) {
1080 *ofp_in_port = xport->ofp_port;
1082 return xport->xbridge->ofproto;
1085 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1086 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1087 struct ofproto_dpif *
1088 xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1089 ofp_port_t *ofp_in_port)
1091 const struct xport *xport;
1093 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1096 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1097 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
1098 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1099 * handles for those protocols if they're enabled. Caller may use the returned
1100 * pointers until quiescing, for longer term use additional references must
1103 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1106 xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1107 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1108 struct dpif_sflow **sflow, struct netflow **netflow,
1109 ofp_port_t *ofp_in_port)
1111 struct ofproto_dpif *ofproto;
1112 const struct xport *xport;
1114 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1121 *ofprotop = ofproto;
1125 *ipfix = xport ? xport->xbridge->ipfix : NULL;
1129 *sflow = xport ? xport->xbridge->sflow : NULL;
1133 *netflow = xport ? xport->xbridge->netflow : NULL;
1139 static struct xbridge *
1140 xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
1142 struct hmap *xbridges;
1143 struct xbridge *xbridge;
1145 if (!ofproto || !xcfg) {
1149 xbridges = &xcfg->xbridges;
1151 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
1153 if (xbridge->ofproto == ofproto) {
1160 static struct xbundle *
1161 xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
1163 struct hmap *xbundles;
1164 struct xbundle *xbundle;
1166 if (!ofbundle || !xcfg) {
1170 xbundles = &xcfg->xbundles;
1172 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
1174 if (xbundle->ofbundle == ofbundle) {
1181 static struct xport *
1182 xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
1184 struct hmap *xports;
1185 struct xport *xport;
1187 if (!ofport || !xcfg) {
1191 xports = &xcfg->xports;
1193 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
1195 if (xport->ofport == ofport) {
1202 static struct stp_port *
1203 xport_get_stp_port(const struct xport *xport)
1205 return xport->xbridge->stp && xport->stp_port_no != -1
1206 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1211 xport_stp_learn_state(const struct xport *xport)
1213 struct stp_port *sp = xport_get_stp_port(xport);
1215 ? stp_learn_in_state(stp_port_get_state(sp))
1220 xport_stp_forward_state(const struct xport *xport)
1222 struct stp_port *sp = xport_get_stp_port(xport);
1224 ? stp_forward_in_state(stp_port_get_state(sp))
1229 xport_stp_should_forward_bpdu(const struct xport *xport)
1231 struct stp_port *sp = xport_get_stp_port(xport);
1232 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
1235 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1236 * were used to make the determination.*/
1238 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1240 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1241 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1242 return is_stp(flow);
1246 stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1248 struct stp_port *sp = xport_get_stp_port(xport);
1249 struct dp_packet payload = *packet;
1250 struct eth_header *eth = dp_packet_data(&payload);
1252 /* Sink packets on ports that have STP disabled when the bridge has
1254 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1258 /* Trim off padding on payload. */
1259 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1260 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1263 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1264 stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
1268 static enum rstp_state
1269 xport_get_rstp_port_state(const struct xport *xport)
1271 return xport->rstp_port
1272 ? rstp_port_get_state(xport->rstp_port)
1277 xport_rstp_learn_state(const struct xport *xport)
1279 return xport->xbridge->rstp && xport->rstp_port
1280 ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1285 xport_rstp_forward_state(const struct xport *xport)
1287 return xport->xbridge->rstp && xport->rstp_port
1288 ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1293 xport_rstp_should_manage_bpdu(const struct xport *xport)
1295 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
1299 rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1301 struct dp_packet payload = *packet;
1302 struct eth_header *eth = dp_packet_data(&payload);
1304 /* Sink packets on ports that have no RSTP. */
1305 if (!xport->rstp_port) {
1309 /* Trim off padding on payload. */
1310 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1311 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1314 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1315 rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
1316 dp_packet_size(&payload));
1320 static struct xport *
1321 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1323 struct xport *xport;
1325 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1327 if (xport->ofp_port == ofp_port) {
1335 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1337 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1338 return xport ? xport->odp_port : ODPP_NONE;
1342 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1344 struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1345 return xport && xport->may_enable;
1348 static struct ofputil_bucket *
1349 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1353 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1355 struct group_dpif *group;
1357 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1358 struct ofputil_bucket *bucket;
1360 bucket = group_first_live_bucket(ctx, group, depth);
1361 group_dpif_unref(group);
1362 return bucket == NULL;
1368 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1371 bucket_is_alive(const struct xlate_ctx *ctx,
1372 struct ofputil_bucket *bucket, int depth)
1374 if (depth >= MAX_LIVENESS_RECURSION) {
1375 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1377 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1378 MAX_LIVENESS_RECURSION);
1382 return (!ofputil_bucket_has_liveness(bucket)
1383 || (bucket->watch_port != OFPP_ANY
1384 && odp_port_is_alive(ctx, bucket->watch_port))
1385 || (bucket->watch_group != OFPG_ANY
1386 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
1389 static struct ofputil_bucket *
1390 group_first_live_bucket(const struct xlate_ctx *ctx,
1391 const struct group_dpif *group, int depth)
1393 struct ofputil_bucket *bucket;
1394 const struct ovs_list *buckets;
1396 group_dpif_get_buckets(group, &buckets);
1397 LIST_FOR_EACH (bucket, list_node, buckets) {
1398 if (bucket_is_alive(ctx, bucket, depth)) {
1406 static struct ofputil_bucket *
1407 group_best_live_bucket(const struct xlate_ctx *ctx,
1408 const struct group_dpif *group,
1411 struct ofputil_bucket *best_bucket = NULL;
1412 uint32_t best_score = 0;
1415 struct ofputil_bucket *bucket;
1416 const struct ovs_list *buckets;
1418 group_dpif_get_buckets(group, &buckets);
1419 LIST_FOR_EACH (bucket, list_node, buckets) {
1420 if (bucket_is_alive(ctx, bucket, 0)) {
1421 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
1422 if (score >= best_score) {
1423 best_bucket = bucket;
1434 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
1436 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1437 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1441 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1443 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1446 static mirror_mask_t
1447 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1449 return xbundle != &ofpp_none_bundle
1450 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1454 static mirror_mask_t
1455 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
1457 return xbundle != &ofpp_none_bundle
1458 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1462 static mirror_mask_t
1463 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
1465 return xbundle != &ofpp_none_bundle
1466 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1470 static struct xbundle *
1471 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1472 bool warn, struct xport **in_xportp)
1474 struct xport *xport;
1476 /* Find the port and bundle for the received packet. */
1477 xport = get_ofp_port(xbridge, in_port);
1481 if (xport && xport->xbundle) {
1482 return xport->xbundle;
1485 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1486 * which a controller may use as the ingress port for traffic that
1487 * it is sourcing. */
1488 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
1489 return &ofpp_none_bundle;
1492 /* Odd. A few possible reasons here:
1494 * - We deleted a port but there are still a few packets queued up
1497 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1498 * we don't know about.
1500 * - The ofproto client didn't configure the port as part of a bundle.
1501 * This is particularly likely to happen if a packet was received on the
1502 * port after it was created, but before the client had a chance to
1503 * configure its bundle.
1506 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1508 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1509 "port %"PRIu16, xbridge->name, in_port);
1515 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
1517 const struct xbridge *xbridge = ctx->xbridge;
1518 mirror_mask_t mirrors;
1519 struct xbundle *in_xbundle;
1523 mirrors = ctx->xout->mirrors;
1524 ctx->xout->mirrors = 0;
1526 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
1527 ctx->xin->packet != NULL, NULL);
1531 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
1533 /* Drop frames on bundles reserved for mirroring. */
1534 if (xbundle_mirror_out(xbridge, in_xbundle)) {
1535 if (ctx->xin->packet != NULL) {
1536 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1537 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1538 "%s, which is reserved exclusively for mirroring",
1539 ctx->xbridge->name, in_xbundle->name);
1541 ofpbuf_clear(ctx->xout->odp_actions);
1546 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
1547 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1550 vlan = input_vid_to_vlan(in_xbundle, vid);
1556 /* Restore the original packet before adding the mirror actions. */
1557 ctx->xin->flow = *orig_flow;
1560 mirror_mask_t dup_mirrors;
1561 struct ofbundle *out;
1562 unsigned long *vlans;
1567 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1568 &vlans, &dup_mirrors, &out, &out_vlan);
1569 ovs_assert(has_mirror);
1572 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1574 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
1577 if (!vlan_mirrored) {
1578 mirrors = zero_rightmost_1bit(mirrors);
1582 mirrors &= ~dup_mirrors;
1583 ctx->xout->mirrors |= dup_mirrors;
1585 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1586 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1588 output_normal(ctx, out_xbundle, vlan);
1590 } else if (vlan != out_vlan
1591 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
1592 struct xbundle *xbundle;
1594 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1595 if (xbundle_includes_vlan(xbundle, out_vlan)
1596 && !xbundle_mirror_out(xbridge, xbundle)) {
1597 output_normal(ctx, xbundle, out_vlan);
1604 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1605 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1606 * the bundle on which the packet was received, returns the VLAN to which the
1609 * Both 'vid' and the return value are in the range 0...4095. */
1611 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1613 switch (in_xbundle->vlan_mode) {
1614 case PORT_VLAN_ACCESS:
1615 return in_xbundle->vlan;
1618 case PORT_VLAN_TRUNK:
1621 case PORT_VLAN_NATIVE_UNTAGGED:
1622 case PORT_VLAN_NATIVE_TAGGED:
1623 return vid ? vid : in_xbundle->vlan;
1630 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1631 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1634 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1635 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1638 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1640 /* Allow any VID on the OFPP_NONE port. */
1641 if (in_xbundle == &ofpp_none_bundle) {
1645 switch (in_xbundle->vlan_mode) {
1646 case PORT_VLAN_ACCESS:
1649 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1650 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1651 "packet received on port %s configured as VLAN "
1652 "%"PRIu16" access port", vid, in_xbundle->name,
1659 case PORT_VLAN_NATIVE_UNTAGGED:
1660 case PORT_VLAN_NATIVE_TAGGED:
1662 /* Port must always carry its native VLAN. */
1666 case PORT_VLAN_TRUNK:
1667 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1669 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1670 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1671 "received on port %s not configured for trunking "
1672 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1684 /* Given 'vlan', the VLAN that a packet belongs to, and
1685 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1686 * that should be included in the 802.1Q header. (If the return value is 0,
1687 * then the 802.1Q header should only be included in the packet if there is a
1690 * Both 'vlan' and the return value are in the range 0...4095. */
1692 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1694 switch (out_xbundle->vlan_mode) {
1695 case PORT_VLAN_ACCESS:
1698 case PORT_VLAN_TRUNK:
1699 case PORT_VLAN_NATIVE_TAGGED:
1702 case PORT_VLAN_NATIVE_UNTAGGED:
1703 return vlan == out_xbundle->vlan ? 0 : vlan;
1711 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1714 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1716 ovs_be16 tci, old_tci;
1717 struct xport *xport;
1718 struct xlate_bond_recirc xr;
1719 bool use_recirc = false;
1721 vid = output_vlan_to_vid(out_xbundle, vlan);
1722 if (list_is_empty(&out_xbundle->xports)) {
1723 /* Partially configured bundle with no slaves. Drop the packet. */
1725 } else if (!out_xbundle->bond) {
1726 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1729 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1730 struct flow_wildcards *wc = &ctx->xout->wc;
1731 struct ofport_dpif *ofport;
1733 if (ctx->xbridge->support.recirc) {
1734 use_recirc = bond_may_recirc(
1735 out_xbundle->bond, &xr.recirc_id, &xr.hash_basis);
1738 /* Only TCP mode uses recirculation. */
1739 xr.hash_alg = OVS_HASH_ALG_L4;
1740 bond_update_post_recirc_rules(out_xbundle->bond, false);
1742 /* Recirculation does not require unmasking hash fields. */
1747 ofport = bond_choose_output_slave(out_xbundle->bond,
1748 &ctx->xin->flow, wc, vid);
1749 xport = xport_lookup(xcfg, ofport);
1752 /* No slaves enabled, so drop packet. */
1756 /* If use_recirc is set, the main thread will handle stats
1757 * accounting for this bond. */
1759 if (ctx->xin->resubmit_stats) {
1760 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1761 ctx->xin->resubmit_stats->n_bytes);
1763 if (ctx->xin->xcache) {
1764 struct xc_entry *entry;
1767 flow = &ctx->xin->flow;
1768 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1769 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1770 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1771 entry->u.bond.vid = vid;
1776 old_tci = *flow_tci;
1778 if (tci || out_xbundle->use_priority_tags) {
1779 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1781 tci |= htons(VLAN_CFI);
1786 compose_output_action(ctx, xport->ofp_port, use_recirc ? &xr : NULL);
1787 *flow_tci = old_tci;
1790 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1791 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1792 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1794 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1796 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1800 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1801 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1805 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1806 if (flow->nw_proto == ARP_OP_REPLY) {
1808 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1809 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1810 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1812 return flow->nw_src == flow->nw_dst;
1818 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1819 * dropped. Returns true if they may be forwarded, false if they should be
1822 * 'in_port' must be the xport that corresponds to flow->in_port.
1823 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1825 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1826 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1827 * checked by input_vid_is_valid().
1829 * May also add tags to '*tags', although the current implementation only does
1830 * so in one special case.
1833 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1836 struct xbundle *in_xbundle = in_port->xbundle;
1837 const struct xbridge *xbridge = ctx->xbridge;
1838 struct flow *flow = &ctx->xin->flow;
1840 /* Drop frames for reserved multicast addresses
1841 * only if forward_bpdu option is absent. */
1842 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1843 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1847 if (in_xbundle->bond) {
1848 struct mac_entry *mac;
1850 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1856 xlate_report(ctx, "bonding refused admissibility, dropping");
1859 case BV_DROP_IF_MOVED:
1860 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1861 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1863 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
1864 && (!is_gratuitous_arp(flow, &ctx->xout->wc)
1865 || mac_entry_is_grat_arp_locked(mac))) {
1866 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1867 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1871 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1879 /* Checks whether a MAC learning update is necessary for MAC learning table
1880 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1883 * Most packets processed through the MAC learning table do not actually
1884 * change it in any way. This function requires only a read lock on the MAC
1885 * learning table, so it is much cheaper in this common case.
1887 * Keep the code here synchronized with that in update_learning_table__()
1890 is_mac_learning_update_needed(const struct mac_learning *ml,
1891 const struct flow *flow,
1892 struct flow_wildcards *wc,
1893 int vlan, struct xbundle *in_xbundle)
1894 OVS_REQ_RDLOCK(ml->rwlock)
1896 struct mac_entry *mac;
1898 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1902 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1903 if (!mac || mac_entry_age(ml, mac)) {
1907 if (is_gratuitous_arp(flow, wc)) {
1908 /* We don't want to learn from gratuitous ARP packets that are
1909 * reflected back over bond slaves so we lock the learning table. */
1910 if (!in_xbundle->bond) {
1912 } else if (mac_entry_is_grat_arp_locked(mac)) {
1917 return mac_entry_get_port(ml, mac) != in_xbundle->ofbundle;
1921 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1922 * received on 'in_xbundle' in 'vlan'.
1924 * This code repeats all the checks in is_mac_learning_update_needed() because
1925 * the lock was released between there and here and thus the MAC learning state
1926 * could have changed.
1928 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1931 update_learning_table__(const struct xbridge *xbridge,
1932 const struct flow *flow, struct flow_wildcards *wc,
1933 int vlan, struct xbundle *in_xbundle)
1934 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1936 struct mac_entry *mac;
1938 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1942 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1943 if (is_gratuitous_arp(flow, wc)) {
1944 /* We don't want to learn from gratuitous ARP packets that are
1945 * reflected back over bond slaves so we lock the learning table. */
1946 if (!in_xbundle->bond) {
1947 mac_entry_set_grat_arp_lock(mac);
1948 } else if (mac_entry_is_grat_arp_locked(mac)) {
1953 if (mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle) {
1954 /* The log messages here could actually be useful in debugging,
1955 * so keep the rate limit relatively high. */
1956 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1958 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1959 "on port %s in VLAN %d",
1960 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1961 in_xbundle->name, vlan);
1963 mac_entry_set_port(xbridge->ml, mac, in_xbundle->ofbundle);
1968 update_learning_table(const struct xbridge *xbridge,
1969 const struct flow *flow, struct flow_wildcards *wc,
1970 int vlan, struct xbundle *in_xbundle)
1974 /* Don't learn the OFPP_NONE port. */
1975 if (in_xbundle == &ofpp_none_bundle) {
1979 /* First try the common case: no change to MAC learning table. */
1980 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1981 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1983 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1986 /* Slow path: MAC learning table might need an update. */
1987 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1988 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1989 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1993 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1994 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
1996 update_mcast_snooping_table__(const struct xbridge *xbridge,
1997 const struct flow *flow,
1998 struct mcast_snooping *ms,
1999 ovs_be32 ip4, int vlan,
2000 struct xbundle *in_xbundle,
2001 const struct dp_packet *packet)
2002 OVS_REQ_WRLOCK(ms->rwlock)
2004 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2007 switch (ntohs(flow->tp_src)) {
2008 case IGMP_HOST_MEMBERSHIP_REPORT:
2009 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2010 if (mcast_snooping_add_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
2011 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
2012 IP_FMT" is on port %s in VLAN %d",
2013 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2016 case IGMP_HOST_LEAVE_MESSAGE:
2017 if (mcast_snooping_leave_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
2018 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
2019 IP_FMT" is on port %s in VLAN %d",
2020 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2023 case IGMP_HOST_MEMBERSHIP_QUERY:
2024 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
2025 in_xbundle->ofbundle)) {
2026 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
2027 IP_FMT" is on port %s in VLAN %d",
2028 xbridge->name, IP_ARGS(flow->nw_src),
2029 in_xbundle->name, vlan);
2032 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2033 if ((count = mcast_snooping_add_report(ms, packet, vlan,
2034 in_xbundle->ofbundle))) {
2035 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2036 "addresses on port %s in VLAN %d",
2037 xbridge->name, count, in_xbundle->name, vlan);
2043 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2044 * was received on 'in_xbundle' in 'vlan'. */
2046 update_mcast_snooping_table(const struct xbridge *xbridge,
2047 const struct flow *flow, int vlan,
2048 struct xbundle *in_xbundle,
2049 const struct dp_packet *packet)
2051 struct mcast_snooping *ms = xbridge->ms;
2052 struct xlate_cfg *xcfg;
2053 struct xbundle *mcast_xbundle;
2054 struct mcast_port_bundle *fport;
2056 /* Don't learn the OFPP_NONE port. */
2057 if (in_xbundle == &ofpp_none_bundle) {
2061 /* Don't learn from flood ports */
2062 mcast_xbundle = NULL;
2063 ovs_rwlock_wrlock(&ms->rwlock);
2064 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2065 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2066 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2067 if (mcast_xbundle == in_xbundle) {
2072 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
2073 update_mcast_snooping_table__(xbridge, flow, ms, flow->igmp_group_ip4,
2074 vlan, in_xbundle, packet);
2076 ovs_rwlock_unlock(&ms->rwlock);
2079 /* send the packet to ports having the multicast group learned */
2081 xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2082 struct mcast_snooping *ms OVS_UNUSED,
2083 struct mcast_group *grp,
2084 struct xbundle *in_xbundle, uint16_t vlan)
2085 OVS_REQ_RDLOCK(ms->rwlock)
2087 struct xlate_cfg *xcfg;
2088 struct mcast_group_bundle *b;
2089 struct xbundle *mcast_xbundle;
2091 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2092 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2093 mcast_xbundle = xbundle_lookup(xcfg, b->port);
2094 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2095 xlate_report(ctx, "forwarding to mcast group port");
2096 output_normal(ctx, mcast_xbundle, vlan);
2097 } else if (!mcast_xbundle) {
2098 xlate_report(ctx, "mcast group port is unknown, dropping");
2100 xlate_report(ctx, "mcast group port is input port, dropping");
2105 /* send the packet to ports connected to multicast routers */
2107 xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2108 struct mcast_snooping *ms,
2109 struct xbundle *in_xbundle, uint16_t vlan)
2110 OVS_REQ_RDLOCK(ms->rwlock)
2112 struct xlate_cfg *xcfg;
2113 struct mcast_mrouter_bundle *mrouter;
2114 struct xbundle *mcast_xbundle;
2116 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2117 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2118 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2119 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2120 xlate_report(ctx, "forwarding to mcast router port");
2121 output_normal(ctx, mcast_xbundle, vlan);
2122 } else if (!mcast_xbundle) {
2123 xlate_report(ctx, "mcast router port is unknown, dropping");
2125 xlate_report(ctx, "mcast router port is input port, dropping");
2130 /* send the packet to ports flagged to be flooded */
2132 xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2133 struct mcast_snooping *ms,
2134 struct xbundle *in_xbundle, uint16_t vlan)
2135 OVS_REQ_RDLOCK(ms->rwlock)
2137 struct xlate_cfg *xcfg;
2138 struct mcast_port_bundle *fport;
2139 struct xbundle *mcast_xbundle;
2141 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2142 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2143 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2144 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2145 xlate_report(ctx, "forwarding to mcast flood port");
2146 output_normal(ctx, mcast_xbundle, vlan);
2147 } else if (!mcast_xbundle) {
2148 xlate_report(ctx, "mcast flood port is unknown, dropping");
2150 xlate_report(ctx, "mcast flood port is input port, dropping");
2155 /* forward the Reports to configured ports */
2157 xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
2158 struct mcast_snooping *ms,
2159 struct xbundle *in_xbundle, uint16_t vlan)
2160 OVS_REQ_RDLOCK(ms->rwlock)
2162 struct xlate_cfg *xcfg;
2163 struct mcast_port_bundle *rport;
2164 struct xbundle *mcast_xbundle;
2166 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2167 LIST_FOR_EACH(rport, node, &ms->rport_list) {
2168 mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2169 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2170 xlate_report(ctx, "forwarding Report to mcast flagged port");
2171 output_normal(ctx, mcast_xbundle, vlan);
2172 } else if (!mcast_xbundle) {
2173 xlate_report(ctx, "mcast port is unknown, dropping the Report");
2175 xlate_report(ctx, "mcast port is input port, dropping the Report");
2181 xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2184 struct xbundle *xbundle;
2186 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2187 if (xbundle != in_xbundle
2188 && xbundle_includes_vlan(xbundle, vlan)
2189 && xbundle->floodable
2190 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2191 output_normal(ctx, xbundle, vlan);
2194 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2198 xlate_normal(struct xlate_ctx *ctx)
2200 struct flow_wildcards *wc = &ctx->xout->wc;
2201 struct flow *flow = &ctx->xin->flow;
2202 struct xbundle *in_xbundle;
2203 struct xport *in_port;
2204 struct mac_entry *mac;
2209 ctx->xout->has_normal = true;
2211 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2212 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2213 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2215 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
2216 ctx->xin->packet != NULL, &in_port);
2218 xlate_report(ctx, "no input bundle, dropping");
2222 /* Drop malformed frames. */
2223 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
2224 !(flow->vlan_tci & htons(VLAN_CFI))) {
2225 if (ctx->xin->packet != NULL) {
2226 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2227 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
2228 "VLAN tag received on port %s",
2229 ctx->xbridge->name, in_xbundle->name);
2231 xlate_report(ctx, "partial VLAN tag, dropping");
2235 /* Drop frames on bundles reserved for mirroring. */
2236 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
2237 if (ctx->xin->packet != NULL) {
2238 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2239 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2240 "%s, which is reserved exclusively for mirroring",
2241 ctx->xbridge->name, in_xbundle->name);
2243 xlate_report(ctx, "input port is mirror output port, dropping");
2248 vid = vlan_tci_to_vid(flow->vlan_tci);
2249 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
2250 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2253 vlan = input_vid_to_vlan(in_xbundle, vid);
2255 /* Check other admissibility requirements. */
2256 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2260 /* Learn source MAC. */
2261 if (ctx->xin->may_learn) {
2262 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
2264 if (ctx->xin->xcache) {
2265 struct xc_entry *entry;
2267 /* Save enough info to update mac learning table later. */
2268 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
2269 entry->u.normal.ofproto = ctx->xbridge->ofproto;
2270 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2271 entry->u.normal.vlan = vlan;
2274 /* Determine output bundle. */
2275 if (mcast_snooping_enabled(ctx->xbridge->ms)
2276 && !eth_addr_is_broadcast(flow->dl_dst)
2277 && eth_addr_is_multicast(flow->dl_dst)
2278 && flow->dl_type == htons(ETH_TYPE_IP)) {
2279 struct mcast_snooping *ms = ctx->xbridge->ms;
2280 struct mcast_group *grp;
2282 if (flow->nw_proto == IPPROTO_IGMP) {
2283 if (mcast_snooping_is_membership(flow->tp_src) ||
2284 mcast_snooping_is_query(flow->tp_src)) {
2285 if (ctx->xin->may_learn) {
2286 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2287 in_xbundle, ctx->xin->packet);
2290 * IGMP packets need to take the slow path, in order to be
2291 * processed for mdb updates. That will prevent expires
2292 * firing off even after hosts have sent reports.
2294 ctx->xout->slow |= SLOW_ACTION;
2297 if (mcast_snooping_is_membership(flow->tp_src)) {
2298 ovs_rwlock_rdlock(&ms->rwlock);
2299 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2300 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2301 * forward IGMP Membership Reports only to those ports where
2302 * multicast routers are attached. Alternatively stated: a
2303 * snooping switch should not forward IGMP Membership Reports
2304 * to ports on which only hosts are attached.
2305 * An administrative control may be provided to override this
2306 * restriction, allowing the report messages to be flooded to
2308 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2309 ovs_rwlock_unlock(&ms->rwlock);
2311 xlate_report(ctx, "multicast traffic, flooding");
2312 xlate_normal_flood(ctx, in_xbundle, vlan);
2316 if (ip_is_local_multicast(flow->nw_dst)) {
2317 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2318 * address in the 224.0.0.x range which are not IGMP must
2319 * be forwarded on all ports */
2320 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2321 xlate_normal_flood(ctx, in_xbundle, vlan);
2326 /* forwarding to group base ports */
2327 ovs_rwlock_rdlock(&ms->rwlock);
2328 grp = mcast_snooping_lookup(ms, flow->nw_dst, vlan);
2330 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2331 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2332 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2334 if (mcast_snooping_flood_unreg(ms)) {
2335 xlate_report(ctx, "unregistered multicast, flooding");
2336 xlate_normal_flood(ctx, in_xbundle, vlan);
2338 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2339 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2342 ovs_rwlock_unlock(&ms->rwlock);
2344 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2345 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
2346 mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
2347 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2350 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2351 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2352 if (mac_xbundle && mac_xbundle != in_xbundle) {
2353 xlate_report(ctx, "forwarding to learned port");
2354 output_normal(ctx, mac_xbundle, vlan);
2355 } else if (!mac_xbundle) {
2356 xlate_report(ctx, "learned port is unknown, dropping");
2358 xlate_report(ctx, "learned port is input port, dropping");
2361 xlate_report(ctx, "no learned MAC for destination, flooding");
2362 xlate_normal_flood(ctx, in_xbundle, vlan);
2367 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
2368 * the number of packets out of UINT32_MAX to sample. The given
2369 * cookie is passed back in the callback for each sampled packet.
2372 compose_sample_action(const struct xbridge *xbridge,
2373 struct ofpbuf *odp_actions,
2374 const struct flow *flow,
2375 const uint32_t probability,
2376 const union user_action_cookie *cookie,
2377 const size_t cookie_size,
2378 const odp_port_t tunnel_out_port)
2380 size_t sample_offset, actions_offset;
2381 odp_port_t odp_port;
2385 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
2387 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
2389 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
2391 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
2392 pid = dpif_port_get_pid(xbridge->dpif, odp_port,
2393 flow_hash_5tuple(flow, 0));
2394 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2395 tunnel_out_port, odp_actions);
2397 nl_msg_end_nested(odp_actions, actions_offset);
2398 nl_msg_end_nested(odp_actions, sample_offset);
2399 return cookie_offset;
2403 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
2404 odp_port_t odp_port, unsigned int n_outputs,
2405 union user_action_cookie *cookie)
2409 cookie->type = USER_ACTION_COOKIE_SFLOW;
2410 cookie->sflow.vlan_tci = vlan_tci;
2412 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2413 * port information") for the interpretation of cookie->output. */
2414 switch (n_outputs) {
2416 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2417 cookie->sflow.output = 0x40000000 | 256;
2421 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
2423 cookie->sflow.output = ifindex;
2428 /* 0x80000000 means "multiple output ports. */
2429 cookie->sflow.output = 0x80000000 | n_outputs;
2434 /* Compose SAMPLE action for sFlow bridge sampling. */
2436 compose_sflow_action(const struct xbridge *xbridge,
2437 struct ofpbuf *odp_actions,
2438 const struct flow *flow,
2439 odp_port_t odp_port)
2441 uint32_t probability;
2442 union user_action_cookie cookie;
2444 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
2448 probability = dpif_sflow_get_probability(xbridge->sflow);
2449 compose_sflow_cookie(xbridge, htons(0), odp_port,
2450 odp_port == ODPP_NONE ? 0 : 1, &cookie);
2452 return compose_sample_action(xbridge, odp_actions, flow, probability,
2453 &cookie, sizeof cookie.sflow, ODPP_NONE);
2457 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
2458 uint32_t obs_domain_id, uint32_t obs_point_id,
2459 union user_action_cookie *cookie)
2461 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
2462 cookie->flow_sample.probability = probability;
2463 cookie->flow_sample.collector_set_id = collector_set_id;
2464 cookie->flow_sample.obs_domain_id = obs_domain_id;
2465 cookie->flow_sample.obs_point_id = obs_point_id;
2469 compose_ipfix_cookie(union user_action_cookie *cookie,
2470 odp_port_t output_odp_port)
2472 cookie->type = USER_ACTION_COOKIE_IPFIX;
2473 cookie->ipfix.output_odp_port = output_odp_port;
2476 /* Compose SAMPLE action for IPFIX bridge sampling. */
2478 compose_ipfix_action(const struct xbridge *xbridge,
2479 struct ofpbuf *odp_actions,
2480 const struct flow *flow,
2481 odp_port_t output_odp_port)
2483 uint32_t probability;
2484 union user_action_cookie cookie;
2485 odp_port_t tunnel_out_port = ODPP_NONE;
2487 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
2491 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2493 if (output_odp_port == ODPP_NONE &&
2494 !dpif_ipfix_get_bridge_exporter_input_sampling(xbridge->ipfix)) {
2498 /* For output case, output_odp_port is valid*/
2499 if (output_odp_port != ODPP_NONE) {
2500 if (!dpif_ipfix_get_bridge_exporter_output_sampling(xbridge->ipfix)) {
2503 /* If tunnel sampling is enabled, put an additional option attribute:
2504 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2506 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(xbridge->ipfix) &&
2507 dpif_ipfix_get_tunnel_port(xbridge->ipfix, output_odp_port) ) {
2508 tunnel_out_port = output_odp_port;
2512 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
2513 compose_ipfix_cookie(&cookie, output_odp_port);
2515 compose_sample_action(xbridge, odp_actions, flow, probability,
2516 &cookie, sizeof cookie.ipfix, tunnel_out_port);
2519 /* SAMPLE action for sFlow must be first action in any given list of
2520 * actions. At this point we do not have all information required to
2521 * build it. So try to build sample action as complete as possible. */
2523 add_sflow_action(struct xlate_ctx *ctx)
2525 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
2526 ctx->xout->odp_actions,
2527 &ctx->xin->flow, ODPP_NONE);
2528 ctx->sflow_odp_port = 0;
2529 ctx->sflow_n_outputs = 0;
2532 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
2533 * of actions, eventually after the SAMPLE action for sFlow. */
2535 add_ipfix_action(struct xlate_ctx *ctx)
2537 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2538 &ctx->xin->flow, ODPP_NONE);
2542 add_ipfix_output_action(struct xlate_ctx *ctx, odp_port_t port)
2544 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2545 &ctx->xin->flow, port);
2548 /* Fix SAMPLE action according to data collected while composing ODP actions.
2549 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
2550 * USERSPACE action's user-cookie which is required for sflow. */
2552 fix_sflow_action(struct xlate_ctx *ctx)
2554 const struct flow *base = &ctx->base_flow;
2555 union user_action_cookie *cookie;
2557 if (!ctx->user_cookie_offset) {
2561 cookie = ofpbuf_at(ctx->xout->odp_actions, ctx->user_cookie_offset,
2562 sizeof cookie->sflow);
2563 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2565 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
2566 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
2569 static enum slow_path_reason
2570 process_special(struct xlate_ctx *ctx, const struct flow *flow,
2571 const struct xport *xport, const struct dp_packet *packet)
2573 struct flow_wildcards *wc = &ctx->xout->wc;
2574 const struct xbridge *xbridge = ctx->xbridge;
2578 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
2580 cfm_process_heartbeat(xport->cfm, packet);
2583 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
2585 bfd_process_packet(xport->bfd, flow, packet);
2586 /* If POLL received, immediately sends FINAL back. */
2587 if (bfd_should_send_packet(xport->bfd)) {
2588 ofproto_dpif_monitor_port_send_soon(xport->ofport);
2592 } else if (xport->xbundle && xport->xbundle->lacp
2593 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2595 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
2598 } else if ((xbridge->stp || xbridge->rstp) &&
2599 stp_should_process_flow(flow, wc)) {
2602 ? stp_process_packet(xport, packet)
2603 : rstp_process_packet(xport, packet);
2606 } else if (xport->lldp && lldp_should_process_flow(xport->lldp, flow)) {
2608 lldp_process_packet(xport->lldp, packet);
2617 tnl_route_lookup_flow(const struct flow *oflow,
2618 ovs_be32 *ip, struct xport **out_port)
2620 char out_dev[IFNAMSIZ];
2621 struct xbridge *xbridge;
2622 struct xlate_cfg *xcfg;
2625 if (!ovs_router_lookup(oflow->tunnel.ip_dst, out_dev, &gw)) {
2632 *ip = oflow->tunnel.ip_dst;
2635 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2638 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
2639 if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
2642 HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
2643 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
2654 xlate_flood_packet(struct xbridge *xbridge, struct dp_packet *packet)
2656 struct ofpact_output output;
2659 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
2660 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
2661 flow_extract(packet, &flow);
2662 flow.in_port.ofp_port = OFPP_NONE;
2663 output.port = OFPP_FLOOD;
2666 return ofproto_dpif_execute_actions(xbridge->ofproto, &flow, NULL,
2667 &output.ofpact, sizeof output,
2672 tnl_send_arp_request(const struct xport *out_dev, const uint8_t eth_src[ETH_ADDR_LEN],
2673 ovs_be32 ip_src, ovs_be32 ip_dst)
2675 struct xbridge *xbridge = out_dev->xbridge;
2676 struct dp_packet packet;
2678 dp_packet_init(&packet, 0);
2679 compose_arp(&packet, eth_src, ip_src, ip_dst);
2681 xlate_flood_packet(xbridge, &packet);
2682 dp_packet_uninit(&packet);
2686 build_tunnel_send(const struct xlate_ctx *ctx, const struct xport *xport,
2687 const struct flow *flow, odp_port_t tunnel_odp_port)
2689 struct ovs_action_push_tnl tnl_push_data;
2690 struct xport *out_dev = NULL;
2691 ovs_be32 s_ip, d_ip = 0;
2692 uint8_t smac[ETH_ADDR_LEN];
2693 uint8_t dmac[ETH_ADDR_LEN];
2696 err = tnl_route_lookup_flow(flow, &d_ip, &out_dev);
2701 /* Use mac addr of bridge port of the peer. */
2702 err = netdev_get_etheraddr(out_dev->netdev, smac);
2707 err = netdev_get_in4(out_dev->netdev, (struct in_addr *) &s_ip, NULL);
2712 err = tnl_arp_lookup(out_dev->xbridge->name, d_ip, dmac);
2714 tnl_send_arp_request(out_dev, smac, s_ip, d_ip);
2717 if (ctx->xin->xcache) {
2718 struct xc_entry *entry;
2720 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_ARP);
2721 ovs_strlcpy(entry->u.tnl_arp_cache.br_name, out_dev->xbridge->name,
2722 sizeof entry->u.tnl_arp_cache.br_name);
2723 entry->u.tnl_arp_cache.d_ip = d_ip;
2725 err = tnl_port_build_header(xport->ofport, flow,
2726 dmac, smac, s_ip, &tnl_push_data);
2730 tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
2731 tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
2732 odp_put_tnl_push_action(ctx->xout->odp_actions, &tnl_push_data);
2737 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
2738 const struct xlate_bond_recirc *xr, bool check_stp)
2740 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
2741 struct flow_wildcards *wc = &ctx->xout->wc;
2742 struct flow *flow = &ctx->xin->flow;
2743 struct flow_tnl flow_tnl;
2744 ovs_be16 flow_vlan_tci;
2745 uint32_t flow_pkt_mark;
2746 uint8_t flow_nw_tos;
2747 odp_port_t out_port, odp_port;
2748 bool tnl_push_pop_send = false;
2751 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2752 * before traversing a patch port. */
2753 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 31);
2754 memset(&flow_tnl, 0, sizeof flow_tnl);
2757 xlate_report(ctx, "Nonexistent output port");
2759 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
2760 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2762 } else if (check_stp) {
2763 if (is_stp(&ctx->base_flow)) {
2764 if (!xport_stp_should_forward_bpdu(xport) &&
2765 !xport_rstp_should_manage_bpdu(xport)) {
2766 if (ctx->xbridge->stp != NULL) {
2767 xlate_report(ctx, "STP not in listening state, "
2768 "skipping bpdu output");
2769 } else if (ctx->xbridge->rstp != NULL) {
2770 xlate_report(ctx, "RSTP not managing BPDU in this state, "
2771 "skipping bpdu output");
2775 } else if (!xport_stp_forward_state(xport) ||
2776 !xport_rstp_forward_state(xport)) {
2777 if (ctx->xbridge->stp != NULL) {
2778 xlate_report(ctx, "STP not in forwarding state, "
2780 } else if (ctx->xbridge->rstp != NULL) {
2781 xlate_report(ctx, "RSTP not in forwarding state, "
2788 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
2789 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
2794 const struct xport *peer = xport->peer;
2795 struct flow old_flow = ctx->xin->flow;
2796 bool old_was_mpls = ctx->was_mpls;
2797 cls_version_t old_version = ctx->tables_version;
2798 enum slow_path_reason special;
2799 struct ofpbuf old_stack = ctx->stack;
2800 union mf_subvalue new_stack[1024 / sizeof(union mf_subvalue)];
2801 struct ofpbuf old_action_set = ctx->action_set;
2802 uint64_t actset_stub[1024 / 8];
2804 ofpbuf_use_stub(&ctx->stack, new_stack, sizeof new_stack);
2805 ofpbuf_use_stub(&ctx->action_set, actset_stub, sizeof actset_stub);
2806 ctx->xbridge = peer->xbridge;
2807 flow->in_port.ofp_port = peer->ofp_port;
2808 flow->metadata = htonll(0);
2809 memset(&flow->tunnel, 0, sizeof flow->tunnel);
2810 memset(flow->regs, 0, sizeof flow->regs);
2811 flow->actset_output = OFPP_UNSET;
2813 /* The bridge is now known so obtain its table version. */
2815 = ofproto_dpif_get_tables_version(ctx->xbridge->ofproto);
2817 special = process_special(ctx, &ctx->xin->flow, peer,
2820 ctx->xout->slow |= special;
2821 } else if (may_receive(peer, ctx)) {
2822 if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
2823 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2824 if (ctx->action_set.size) {
2825 /* Translate action set only if not dropping the packet and
2826 * not recirculating. */
2827 if (!exit_recirculates(ctx)) {
2828 xlate_action_set(ctx);
2831 /* Check if need to recirculate. */
2832 if (exit_recirculates(ctx)) {
2833 compose_recirculate_action(ctx);
2836 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
2837 * the learning action look at the packet, then drop it. */
2838 struct flow old_base_flow = ctx->base_flow;
2839 size_t old_size = ctx->xout->odp_actions->size;
2840 mirror_mask_t old_mirrors = ctx->xout->mirrors;
2842 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2843 ctx->xout->mirrors = old_mirrors;
2844 ctx->base_flow = old_base_flow;
2845 ctx->xout->odp_actions->size = old_size;
2847 /* Undo changes that may have been done for recirculation. */
2848 if (exit_recirculates(ctx)) {
2849 ctx->action_set.size = ctx->recirc_action_offset;
2850 ctx->recirc_action_offset = -1;
2851 ctx->last_unroll_offset = -1;
2856 ctx->xin->flow = old_flow;
2857 ctx->xbridge = xport->xbridge;
2858 ofpbuf_uninit(&ctx->action_set);
2859 ctx->action_set = old_action_set;
2860 ofpbuf_uninit(&ctx->stack);
2861 ctx->stack = old_stack;
2863 /* Restore calling bridge's lookup version. */
2864 ctx->tables_version = old_version;
2866 /* The peer bridge popping MPLS should have no effect on the original
2868 ctx->was_mpls = old_was_mpls;
2870 /* The fact that the peer bridge exits (for any reason) does not mean
2871 * that the original bridge should exit. Specifically, if the peer
2872 * bridge recirculates (which typically modifies the packet), the
2873 * original bridge must continue processing with the original, not the
2874 * recirculated packet! */
2877 if (ctx->xin->resubmit_stats) {
2878 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2879 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
2881 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
2884 if (ctx->xin->xcache) {
2885 struct xc_entry *entry;
2887 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2888 entry->u.dev.tx = netdev_ref(xport->netdev);
2889 entry->u.dev.rx = netdev_ref(peer->netdev);
2890 entry->u.dev.bfd = bfd_ref(peer->bfd);
2895 flow_vlan_tci = flow->vlan_tci;
2896 flow_pkt_mark = flow->pkt_mark;
2897 flow_nw_tos = flow->nw_tos;
2899 if (count_skb_priorities(xport)) {
2900 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
2901 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
2902 wc->masks.nw_tos |= IP_DSCP_MASK;
2903 flow->nw_tos &= ~IP_DSCP_MASK;
2904 flow->nw_tos |= dscp;
2908 if (xport->is_tunnel) {
2909 /* Save tunnel metadata so that changes made due to
2910 * the Logical (tunnel) Port are not visible for any further
2911 * matches, while explicit set actions on tunnel metadata are.
2913 flow_tnl = flow->tunnel;
2914 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
2915 if (odp_port == ODPP_NONE) {
2916 xlate_report(ctx, "Tunneling decided against output");
2917 goto out; /* restore flow_nw_tos */
2919 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
2920 xlate_report(ctx, "Not tunneling to our own address");
2921 goto out; /* restore flow_nw_tos */
2923 if (ctx->xin->resubmit_stats) {
2924 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2926 if (ctx->xin->xcache) {
2927 struct xc_entry *entry;
2929 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2930 entry->u.dev.tx = netdev_ref(xport->netdev);
2932 out_port = odp_port;
2933 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
2934 tnl_push_pop_send = true;
2936 commit_odp_tunnel_action(flow, &ctx->base_flow,
2937 ctx->xout->odp_actions);
2938 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2941 odp_port = xport->odp_port;
2942 out_port = odp_port;
2943 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
2944 ofp_port_t vlandev_port;
2946 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2947 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
2948 ofp_port, flow->vlan_tci);
2949 if (vlandev_port != ofp_port) {
2950 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
2951 flow->vlan_tci = htons(0);
2956 if (out_port != ODPP_NONE) {
2957 bool use_masked = ctx->xbridge->support.masked_set_action;
2959 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
2960 ctx->xout->odp_actions,
2964 struct ovs_action_hash *act_hash;
2967 act_hash = nl_msg_put_unspec_uninit(ctx->xout->odp_actions,
2968 OVS_ACTION_ATTR_HASH,
2970 act_hash->hash_alg = xr->hash_alg;
2971 act_hash->hash_basis = xr->hash_basis;
2973 /* Recirc action. */
2974 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC,
2978 if (tnl_push_pop_send) {
2979 build_tunnel_send(ctx, xport, flow, odp_port);
2980 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2982 odp_port_t odp_tnl_port = ODPP_NONE;
2984 /* XXX: Write better Filter for tunnel port. We can use inport
2985 * int tunnel-port flow to avoid these checks completely. */
2986 if (ofp_port == OFPP_LOCAL &&
2987 ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
2989 odp_tnl_port = tnl_port_map_lookup(flow, wc);
2992 if (odp_tnl_port != ODPP_NONE) {
2993 nl_msg_put_odp_port(ctx->xout->odp_actions,
2994 OVS_ACTION_ATTR_TUNNEL_POP,
2997 /* Tunnel push-pop action is not compatible with
2999 add_ipfix_output_action(ctx, out_port);
3000 nl_msg_put_odp_port(ctx->xout->odp_actions,
3001 OVS_ACTION_ATTR_OUTPUT,
3007 ctx->sflow_odp_port = odp_port;
3008 ctx->sflow_n_outputs++;
3009 ctx->xout->nf_output_iface = ofp_port;
3014 flow->vlan_tci = flow_vlan_tci;
3015 flow->pkt_mark = flow_pkt_mark;
3016 flow->nw_tos = flow_nw_tos;
3020 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port,
3021 const struct xlate_bond_recirc *xr)
3023 compose_output_action__(ctx, ofp_port, xr, true);
3027 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
3029 struct rule_dpif *old_rule = ctx->rule;
3030 ovs_be64 old_cookie = ctx->rule_cookie;
3031 const struct rule_actions *actions;
3033 if (ctx->xin->resubmit_stats) {
3034 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
3040 ctx->rule_cookie = rule_dpif_get_flow_cookie(rule);
3041 actions = rule_dpif_get_actions(rule);
3042 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
3043 ctx->rule_cookie = old_cookie;
3044 ctx->rule = old_rule;
3049 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
3051 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3053 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
3054 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
3055 MAX_RESUBMIT_RECURSION);
3056 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
3057 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
3058 } else if (ctx->xout->odp_actions->size > UINT16_MAX) {
3059 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
3060 } else if (ctx->stack.size >= 65536) {
3061 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
3070 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
3071 bool may_packet_in, bool honor_table_miss)
3073 /* Check if we need to recirculate before matching in a table. */
3074 if (ctx->was_mpls) {
3075 ctx_trigger_recirculation(ctx);
3078 if (xlate_resubmit_resource_check(ctx)) {
3079 struct flow_wildcards *wc;
3080 uint8_t old_table_id = ctx->table_id;
3081 struct rule_dpif *rule;
3083 ctx->table_id = table_id;
3084 wc = (ctx->xin->skip_wildcards) ? NULL : &ctx->xout->wc;
3086 rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
3087 ctx->tables_version,
3088 &ctx->xin->flow, wc,
3089 ctx->xin->xcache != NULL,
3090 ctx->xin->resubmit_stats,
3091 &ctx->table_id, in_port,
3092 may_packet_in, honor_table_miss);
3094 if (OVS_UNLIKELY(ctx->xin->resubmit_hook)) {
3095 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse + 1);
3099 /* Fill in the cache entry here instead of xlate_recursively
3100 * to make the reference counting more explicit. We take a
3101 * reference in the lookups above if we are going to cache the
3103 if (ctx->xin->xcache) {
3104 struct xc_entry *entry;
3106 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
3107 entry->u.rule = rule;
3109 xlate_recursively(ctx, rule);
3112 ctx->table_id = old_table_id;
3120 xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
3121 struct ofputil_bucket *bucket)
3123 if (ctx->xin->resubmit_stats) {
3124 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
3126 if (ctx->xin->xcache) {
3127 struct xc_entry *entry;
3129 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
3130 entry->u.group.group = group_dpif_ref(group);
3131 entry->u.group.bucket = bucket;
3136 xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
3138 uint64_t action_list_stub[1024 / 8];
3139 struct ofpbuf action_list, action_set;
3140 struct flow old_flow = ctx->xin->flow;
3141 bool old_was_mpls = ctx->was_mpls;
3143 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
3144 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3146 ofpacts_execute_action_set(&action_list, &action_set);
3148 do_xlate_actions(action_list.data, action_list.size, ctx);
3151 ofpbuf_uninit(&action_set);
3152 ofpbuf_uninit(&action_list);
3154 /* Check if need to recirculate. */
3155 if (exit_recirculates(ctx)) {
3156 compose_recirculate_action(ctx);
3159 /* Roll back flow to previous state.
3160 * This is equivalent to cloning the packet for each bucket.
3162 * As a side effect any subsequently applied actions will
3163 * also effectively be applied to a clone of the packet taken
3164 * just before applying the all or indirect group.
3166 * Note that group buckets are action sets, hence they cannot modify the
3167 * main action set. Also any stack actions are ignored when executing an
3168 * action set, so group buckets cannot change the stack either.
3169 * However, we do allow resubmit actions in group buckets, which could
3170 * break the above assumptions. It is up to the controller to not mess up
3171 * with the action_set and stack in the tables resubmitted to from
3173 ctx->xin->flow = old_flow;
3175 /* The group bucket popping MPLS should have no effect after bucket
3177 ctx->was_mpls = old_was_mpls;
3179 /* The fact that the group bucket exits (for any reason) does not mean that
3180 * the translation after the group action should exit. Specifically, if
3181 * the group bucket recirculates (which typically modifies the packet), the
3182 * actions after the group action must continue processing with the
3183 * original, not the recirculated packet! */
3188 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3190 struct ofputil_bucket *bucket;
3191 const struct ovs_list *buckets;
3193 group_dpif_get_buckets(group, &buckets);
3195 LIST_FOR_EACH (bucket, list_node, buckets) {
3196 xlate_group_bucket(ctx, bucket);
3198 xlate_group_stats(ctx, group, NULL);
3202 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3204 struct ofputil_bucket *bucket;
3206 bucket = group_first_live_bucket(ctx, group, 0);
3208 xlate_group_bucket(ctx, bucket);
3209 xlate_group_stats(ctx, group, bucket);
3214 xlate_default_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3216 struct flow_wildcards *wc = &ctx->xout->wc;
3217 struct ofputil_bucket *bucket;
3220 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
3221 flow_mask_hash_fields(&ctx->xin->flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
3222 bucket = group_best_live_bucket(ctx, group, basis);
3224 xlate_group_bucket(ctx, bucket);
3225 xlate_group_stats(ctx, group, bucket);
3230 xlate_hash_fields_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3232 struct mf_bitmap hash_fields = MF_BITMAP_INITIALIZER;
3233 struct flow_wildcards *wc = &ctx->xout->wc;
3234 const struct field_array *fields;
3235 struct ofputil_bucket *bucket;
3239 fields = group_dpif_get_fields(group);
3240 basis = hash_uint64(group_dpif_get_selection_method_param(group));
3242 /* Determine which fields to hash */
3243 for (i = 0; i < MFF_N_IDS; i++) {
3244 if (bitmap_is_set(fields->used.bm, i)) {
3245 const struct mf_field *mf;
3247 /* If the field is already present in 'hash_fields' then
3248 * this loop has already checked that it and its pre-requisites
3249 * are present in the flow and its pre-requisites have
3250 * already been added to 'hash_fields'. There is nothing more
3251 * to do here and as an optimisation the loop can continue. */
3252 if (bitmap_is_set(hash_fields.bm, i)) {
3258 /* Only hash a field if it and its pre-requisites are present
3260 if (!mf_are_prereqs_ok(mf, &ctx->xin->flow)) {
3264 /* Hash both the field and its pre-requisites */
3265 mf_bitmap_set_field_and_prereqs(mf, &hash_fields);
3269 /* Hash the fields */
3270 for (i = 0; i < MFF_N_IDS; i++) {
3271 if (bitmap_is_set(hash_fields.bm, i)) {
3272 const struct mf_field *mf = mf_from_id(i);
3273 union mf_value value;
3276 mf_get_value(mf, &ctx->xin->flow, &value);
3277 /* This seems inefficient but so does apply_mask() */
3278 for (j = 0; j < mf->n_bytes; j++) {
3279 ((uint8_t *) &value)[j] &= ((uint8_t *) &fields->value[i])[j];
3281 basis = hash_bytes(&value, mf->n_bytes, basis);
3283 mf_mask_field(mf, &wc->masks);
3287 bucket = group_best_live_bucket(ctx, group, basis);
3289 xlate_group_bucket(ctx, bucket);
3290 xlate_group_stats(ctx, group, bucket);
3295 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3297 const char *selection_method = group_dpif_get_selection_method(group);
3299 if (selection_method[0] == '\0') {
3300 xlate_default_select_group(ctx, group);
3301 } else if (!strcasecmp("hash", selection_method)) {
3302 xlate_hash_fields_select_group(ctx, group);
3304 /* Parsing of groups should ensure this never happens */
3310 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3312 ctx->in_group = true;
3314 switch (group_dpif_get_type(group)) {
3316 case OFPGT11_INDIRECT:
3317 xlate_all_group(ctx, group);
3319 case OFPGT11_SELECT:
3320 xlate_select_group(ctx, group);
3323 xlate_ff_group(ctx, group);
3328 group_dpif_unref(group);
3330 ctx->in_group = false;
3334 xlate_group_resource_check(struct xlate_ctx *ctx)
3336 if (!xlate_resubmit_resource_check(ctx)) {
3338 } else if (ctx->in_group) {
3339 /* Prevent nested translation of OpenFlow groups.
3341 * OpenFlow allows this restriction. We enforce this restriction only
3342 * because, with the current architecture, we would otherwise have to
3343 * take a possibly recursive read lock on the ofgroup rwlock, which is
3344 * unsafe given that POSIX allows taking a read lock to block if there
3345 * is a thread blocked on taking the write lock. Other solutions
3346 * without this restriction are also possible, but seem unwarranted
3347 * given the current limited use of groups. */
3348 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3350 VLOG_ERR_RL(&rl, "cannot recursively translate OpenFlow group");
3358 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3360 if (xlate_group_resource_check(ctx)) {
3361 struct group_dpif *group;
3364 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
3366 xlate_group_action__(ctx, group);
3376 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
3377 const struct ofpact_resubmit *resubmit)
3381 bool may_packet_in = false;
3382 bool honor_table_miss = false;
3384 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
3385 /* Still allow missed packets to be sent to the controller
3386 * if resubmitting from an internal table. */
3387 may_packet_in = true;
3388 honor_table_miss = true;
3391 in_port = resubmit->in_port;
3392 if (in_port == OFPP_IN_PORT) {
3393 in_port = ctx->xin->flow.in_port.ofp_port;
3396 table_id = resubmit->table_id;
3397 if (table_id == 255) {
3398 table_id = ctx->table_id;
3401 xlate_table_action(ctx, in_port, table_id, may_packet_in,
3406 flood_packets(struct xlate_ctx *ctx, bool all)
3408 const struct xport *xport;
3410 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
3411 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
3416 compose_output_action__(ctx, xport->ofp_port, NULL, false);
3417 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
3418 compose_output_action(ctx, xport->ofp_port, NULL);
3422 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3426 execute_controller_action(struct xlate_ctx *ctx, int len,
3427 enum ofp_packet_in_reason reason,
3428 uint16_t controller_id)
3430 struct ofproto_packet_in *pin;
3431 struct dp_packet *packet;
3434 ctx->xout->slow |= SLOW_CONTROLLER;
3435 if (!ctx->xin->packet) {
3439 packet = dp_packet_clone(ctx->xin->packet);
3441 use_masked = ctx->xbridge->support.masked_set_action;
3442 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3443 ctx->xout->odp_actions,
3444 &ctx->xout->wc, use_masked);
3446 odp_execute_actions(NULL, &packet, 1, false,
3447 ctx->xout->odp_actions->data,
3448 ctx->xout->odp_actions->size, NULL);
3450 pin = xmalloc(sizeof *pin);
3451 pin->up.packet_len = dp_packet_size(packet);
3452 pin->up.packet = dp_packet_steal_data(packet);
3453 pin->up.reason = reason;
3454 pin->up.table_id = ctx->table_id;
3455 pin->up.cookie = ctx->rule_cookie;
3457 flow_get_metadata(&ctx->xin->flow, &pin->up.flow_metadata);
3459 pin->controller_id = controller_id;
3460 pin->send_len = len;
3461 /* If a rule is a table-miss rule then this is
3462 * a table-miss handled by a table-miss rule.
3464 * Else, if rule is internal and has a controller action,
3465 * the later being implied by the rule being processed here,
3466 * then this is a table-miss handled without a table-miss rule.
3468 * Otherwise this is not a table-miss. */
3469 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
3471 if (rule_dpif_is_table_miss(ctx->rule)) {
3472 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
3473 } else if (rule_dpif_is_internal(ctx->rule)) {
3474 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
3477 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
3478 dp_packet_delete(packet);
3481 /* Called only when ctx->recirc_action_offset is set. */
3483 compose_recirculate_action(struct xlate_ctx *ctx)
3485 struct recirc_metadata md;
3489 use_masked = ctx->xbridge->support.masked_set_action;
3490 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3491 ctx->xout->odp_actions,
3492 &ctx->xout->wc, use_masked);
3494 recirc_metadata_from_flow(&md, &ctx->xin->flow);
3496 ovs_assert(ctx->recirc_action_offset >= 0);
3498 /* Only allocate recirculation ID if we have a packet. */
3499 if (ctx->xin->packet) {
3500 /* Allocate a unique recirc id for the given metadata state in the
3501 * flow. The life-cycle of this recirc id is managed by associating it
3502 * with the udpif key ('ukey') created for each new datapath flow. */
3503 id = recirc_alloc_id_ctx(ctx->xbridge->ofproto, 0, &md, &ctx->stack,
3504 ctx->recirc_action_offset,
3505 ctx->action_set.size, ctx->action_set.data);
3507 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3508 VLOG_ERR_RL(&rl, "Failed to allocate recirculation id");
3511 xlate_out_add_recirc(ctx->xout, id);
3513 /* Look up an existing recirc id for the given metadata state in the
3514 * flow. No new reference is taken, as the ID is RCU protected and is
3515 * only required temporarily for verification. */
3516 id = recirc_find_id(ctx->xbridge->ofproto, 0, &md, &ctx->stack,
3517 ctx->recirc_action_offset,
3518 ctx->action_set.size, ctx->action_set.data);
3519 /* We let zero 'id' to be used in the RECIRC action below, which will
3520 * fail all revalidations as zero is not a valid recirculation ID. */
3523 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
3525 /* Undo changes done by recirculation. */
3526 ctx->action_set.size = ctx->recirc_action_offset;
3527 ctx->recirc_action_offset = -1;
3528 ctx->last_unroll_offset = -1;
3532 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
3534 struct flow_wildcards *wc = &ctx->xout->wc;
3535 struct flow *flow = &ctx->xin->flow;
3538 ovs_assert(eth_type_mpls(mpls->ethertype));
3540 n = flow_count_mpls_labels(flow, wc);
3542 bool use_masked = ctx->xbridge->support.masked_set_action;
3544 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
3545 ctx->xout->odp_actions,
3546 &ctx->xout->wc, use_masked);
3547 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3548 if (ctx->xin->packet != NULL) {
3549 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3550 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3551 "MPLS push action can't be performed as it would "
3552 "have more MPLS LSEs than the %d supported.",
3553 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3559 flow_push_mpls(flow, n, mpls->ethertype, wc);
3563 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
3565 struct flow_wildcards *wc = &ctx->xout->wc;
3566 struct flow *flow = &ctx->xin->flow;
3567 int n = flow_count_mpls_labels(flow, wc);
3569 if (flow_pop_mpls(flow, n, eth_type, wc)) {
3570 if (ctx->xbridge->support.recirc) {
3571 ctx->was_mpls = true;
3573 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3574 if (ctx->xin->packet != NULL) {
3575 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3576 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3577 "MPLS pop action can't be performed as it has "
3578 "more MPLS LSEs than the %d supported.",
3579 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3582 ofpbuf_clear(ctx->xout->odp_actions);
3587 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3589 struct flow *flow = &ctx->xin->flow;
3591 if (!is_ip_any(flow)) {
3595 ctx->xout->wc.masks.nw_ttl = 0xff;
3596 if (flow->nw_ttl > 1) {
3602 for (i = 0; i < ids->n_controllers; i++) {
3603 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3607 /* Stop processing for current table. */
3613 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3615 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3616 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
3617 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
3622 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3624 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3625 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
3626 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
3631 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
3633 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3634 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3635 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
3640 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
3642 struct flow *flow = &ctx->xin->flow;
3643 struct flow_wildcards *wc = &ctx->xout->wc;
3645 if (eth_type_mpls(flow->dl_type)) {
3646 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
3648 wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3651 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3654 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
3658 /* Stop processing for current table. */
3663 xlate_output_action(struct xlate_ctx *ctx,
3664 ofp_port_t port, uint16_t max_len, bool may_packet_in)
3666 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
3668 ctx->xout->nf_output_iface = NF_OUT_DROP;
3672 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port, NULL);
3675 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3676 0, may_packet_in, true);
3682 flood_packets(ctx, false);
3685 flood_packets(ctx, true);
3687 case OFPP_CONTROLLER:
3688 execute_controller_action(ctx, max_len,
3689 (ctx->in_group ? OFPR_GROUP
3690 : ctx->in_action_set ? OFPR_ACTION_SET
3698 if (port != ctx->xin->flow.in_port.ofp_port) {
3699 compose_output_action(ctx, port, NULL);
3701 xlate_report(ctx, "skipping output to input port");
3706 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3707 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3708 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3709 ctx->xout->nf_output_iface = prev_nf_output_iface;
3710 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3711 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3712 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3717 xlate_output_reg_action(struct xlate_ctx *ctx,
3718 const struct ofpact_output_reg *or)
3720 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3721 if (port <= UINT16_MAX) {
3722 union mf_subvalue value;
3724 memset(&value, 0xff, sizeof value);
3725 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
3726 xlate_output_action(ctx, u16_to_ofp(port),
3727 or->max_len, false);
3732 xlate_enqueue_action(struct xlate_ctx *ctx,
3733 const struct ofpact_enqueue *enqueue)
3735 ofp_port_t ofp_port = enqueue->port;
3736 uint32_t queue_id = enqueue->queue;
3737 uint32_t flow_priority, priority;
3740 /* Translate queue to priority. */
3741 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
3743 /* Fall back to ordinary output action. */
3744 xlate_output_action(ctx, enqueue->port, 0, false);
3748 /* Check output port. */
3749 if (ofp_port == OFPP_IN_PORT) {
3750 ofp_port = ctx->xin->flow.in_port.ofp_port;
3751 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
3755 /* Add datapath actions. */
3756 flow_priority = ctx->xin->flow.skb_priority;
3757 ctx->xin->flow.skb_priority = priority;
3758 compose_output_action(ctx, ofp_port, NULL);
3759 ctx->xin->flow.skb_priority = flow_priority;
3761 /* Update NetFlow output port. */
3762 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3763 ctx->xout->nf_output_iface = ofp_port;
3764 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3765 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3770 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3772 uint32_t skb_priority;
3774 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
3775 ctx->xin->flow.skb_priority = skb_priority;
3777 /* Couldn't translate queue to a priority. Nothing to do. A warning
3778 * has already been logged. */
3783 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
3785 const struct xbridge *xbridge = xbridge_;
3796 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3799 port = get_ofp_port(xbridge, ofp_port);
3800 return port ? port->may_enable : false;
3805 xlate_bundle_action(struct xlate_ctx *ctx,
3806 const struct ofpact_bundle *bundle)
3810 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
3812 CONST_CAST(struct xbridge *, ctx->xbridge));
3813 if (bundle->dst.field) {
3814 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
3817 xlate_output_action(ctx, port, 0, false);
3822 xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
3823 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
3825 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
3826 if (ctx->xin->may_learn) {
3827 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
3832 xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
3834 ctx->xout->has_learn = true;
3835 learn_mask(learn, &ctx->xout->wc);
3837 if (ctx->xin->xcache) {
3838 struct xc_entry *entry;
3840 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
3841 entry->u.learn.ofproto = ctx->xbridge->ofproto;
3842 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
3843 entry->u.learn.ofpacts = ofpbuf_new(64);
3844 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
3845 entry->u.learn.ofpacts);
3846 } else if (ctx->xin->may_learn) {
3847 uint64_t ofpacts_stub[1024 / 8];
3848 struct ofputil_flow_mod fm;
3849 struct ofpbuf ofpacts;
3851 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3852 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
3853 ofpbuf_uninit(&ofpacts);
3858 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
3859 uint16_t idle_timeout, uint16_t hard_timeout)
3861 if (tcp_flags & (TCP_FIN | TCP_RST)) {
3862 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
3867 xlate_fin_timeout(struct xlate_ctx *ctx,
3868 const struct ofpact_fin_timeout *oft)
3871 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
3872 oft->fin_idle_timeout, oft->fin_hard_timeout);
3873 if (ctx->xin->xcache) {
3874 struct xc_entry *entry;
3876 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
3877 /* XC_RULE already holds a reference on the rule, none is taken
3879 entry->u.fin.rule = ctx->rule;
3880 entry->u.fin.idle = oft->fin_idle_timeout;
3881 entry->u.fin.hard = oft->fin_hard_timeout;
3887 xlate_sample_action(struct xlate_ctx *ctx,
3888 const struct ofpact_sample *os)
3890 union user_action_cookie cookie;
3891 /* Scale the probability from 16-bit to 32-bit while representing
3892 * the same percentage. */
3893 uint32_t probability = (os->probability << 16) | os->probability;
3896 if (!ctx->xbridge->support.variable_length_userdata) {
3897 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3899 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
3900 "lacks support (needs Linux 3.10+ or kernel module from "
3905 use_masked = ctx->xbridge->support.masked_set_action;
3906 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3907 ctx->xout->odp_actions,
3908 &ctx->xout->wc, use_masked);
3910 compose_flow_sample_cookie(os->probability, os->collector_set_id,
3911 os->obs_domain_id, os->obs_point_id, &cookie);
3912 compose_sample_action(ctx->xbridge, ctx->xout->odp_actions,
3913 &ctx->xin->flow, probability, &cookie,
3914 sizeof cookie.flow_sample, ODPP_NONE);
3918 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
3920 if (xport->config & (is_stp(&ctx->xin->flow)
3921 ? OFPUTIL_PC_NO_RECV_STP
3922 : OFPUTIL_PC_NO_RECV)) {
3926 /* Only drop packets here if both forwarding and learning are
3927 * disabled. If just learning is enabled, we need to have
3928 * OFPP_NORMAL and the learning action have a look at the packet
3929 * before we can drop it. */
3930 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
3931 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
3939 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
3941 const struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3942 size_t on_len = ofpact_nest_get_action_len(on);
3943 const struct ofpact *inner;
3945 /* Maintain actset_output depending on the contents of the action set:
3947 * - OFPP_UNSET, if there is no "output" action.
3949 * - The output port, if there is an "output" action and no "group"
3952 * - OFPP_UNSET, if there is a "group" action.
3954 if (!ctx->action_set_has_group) {
3955 OFPACT_FOR_EACH (inner, on->actions, on_len) {
3956 if (inner->type == OFPACT_OUTPUT) {
3957 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(inner)->port;
3958 } else if (inner->type == OFPACT_GROUP) {
3959 ctx->xin->flow.actset_output = OFPP_UNSET;
3960 ctx->action_set_has_group = true;
3965 ofpbuf_put(&ctx->action_set, on->actions, on_len);
3966 ofpact_pad(&ctx->action_set);
3970 xlate_action_set(struct xlate_ctx *ctx)
3972 uint64_t action_list_stub[1024 / 64];
3973 struct ofpbuf action_list;
3975 ctx->in_action_set = true;
3976 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3977 ofpacts_execute_action_set(&action_list, &ctx->action_set);
3978 /* Clear the action set, as it is not needed any more. */
3979 ofpbuf_clear(&ctx->action_set);
3980 do_xlate_actions(action_list.data, action_list.size, ctx);
3981 ctx->in_action_set = false;
3982 ofpbuf_uninit(&action_list);
3986 recirc_put_unroll_xlate(struct xlate_ctx *ctx)
3988 struct ofpact_unroll_xlate *unroll;
3990 unroll = ctx->last_unroll_offset < 0
3992 : ALIGNED_CAST(struct ofpact_unroll_xlate *,
3993 (char *)ctx->action_set.data + ctx->last_unroll_offset);
3995 /* Restore the table_id and rule cookie for a potential PACKET
3998 (ctx->table_id != unroll->rule_table_id
3999 || ctx->rule_cookie != unroll->rule_cookie)) {
4001 ctx->last_unroll_offset = ctx->action_set.size;
4002 unroll = ofpact_put_UNROLL_XLATE(&ctx->action_set);
4003 unroll->rule_table_id = ctx->table_id;
4004 unroll->rule_cookie = ctx->rule_cookie;
4009 /* Copy remaining actions to the action_set to be executed after recirculation.
4010 * UNROLL_XLATE action is inserted, if not already done so, before actions that
4011 * may generate PACKET_INs from the current table and without matching another
4014 recirc_unroll_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4015 struct xlate_ctx *ctx)
4017 const struct ofpact *a;
4019 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4021 /* May generate PACKET INs. */
4022 case OFPACT_OUTPUT_REG:
4025 case OFPACT_CONTROLLER:
4026 case OFPACT_DEC_MPLS_TTL:
4027 case OFPACT_DEC_TTL:
4028 recirc_put_unroll_xlate(ctx);
4031 /* These may not generate PACKET INs. */
4032 case OFPACT_SET_TUNNEL:
4033 case OFPACT_REG_MOVE:
4034 case OFPACT_SET_FIELD:
4035 case OFPACT_STACK_PUSH:
4036 case OFPACT_STACK_POP:
4038 case OFPACT_WRITE_METADATA:
4039 case OFPACT_RESUBMIT: /* May indirectly generate PACKET INs, */
4040 case OFPACT_GOTO_TABLE: /* but from a different table and rule. */
4041 case OFPACT_ENQUEUE:
4042 case OFPACT_SET_VLAN_VID:
4043 case OFPACT_SET_VLAN_PCP:
4044 case OFPACT_STRIP_VLAN:
4045 case OFPACT_PUSH_VLAN:
4046 case OFPACT_SET_ETH_SRC:
4047 case OFPACT_SET_ETH_DST:
4048 case OFPACT_SET_IPV4_SRC:
4049 case OFPACT_SET_IPV4_DST:
4050 case OFPACT_SET_IP_DSCP:
4051 case OFPACT_SET_IP_ECN:
4052 case OFPACT_SET_IP_TTL:
4053 case OFPACT_SET_L4_SRC_PORT:
4054 case OFPACT_SET_L4_DST_PORT:
4055 case OFPACT_SET_QUEUE:
4056 case OFPACT_POP_QUEUE:
4057 case OFPACT_PUSH_MPLS:
4058 case OFPACT_POP_MPLS:
4059 case OFPACT_SET_MPLS_LABEL:
4060 case OFPACT_SET_MPLS_TC:
4061 case OFPACT_SET_MPLS_TTL:
4062 case OFPACT_MULTIPATH:
4065 case OFPACT_UNROLL_XLATE:
4066 case OFPACT_FIN_TIMEOUT:
4067 case OFPACT_CLEAR_ACTIONS:
4068 case OFPACT_WRITE_ACTIONS:
4073 /* These need not be copied for restoration. */
4075 case OFPACT_CONJUNCTION:
4078 /* Copy the action over. */
4079 ofpbuf_put(&ctx->action_set, a, OFPACT_ALIGN(a->len));
4083 #define CHECK_MPLS_RECIRCULATION() \
4084 if (ctx->was_mpls) { \
4085 ctx_trigger_recirculation(ctx); \
4088 #define CHECK_MPLS_RECIRCULATION_IF(COND) \
4090 CHECK_MPLS_RECIRCULATION(); \
4094 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4095 struct xlate_ctx *ctx)
4097 struct flow_wildcards *wc = &ctx->xout->wc;
4098 struct flow *flow = &ctx->xin->flow;
4099 const struct ofpact *a;
4101 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
4102 tnl_arp_snoop(flow, wc, ctx->xbridge->name);
4104 /* dl_type already in the mask, not set below. */
4106 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4107 struct ofpact_controller *controller;
4108 const struct ofpact_metadata *metadata;
4109 const struct ofpact_set_field *set_field;
4110 const struct mf_field *mf;
4113 /* Check if need to store the remaining actions for later
4115 if (exit_recirculates(ctx)) {
4116 recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
4118 (uint8_t *)ofpacts)),
4126 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
4127 ofpact_get_OUTPUT(a)->max_len, true);
4131 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
4132 /* Group could not be found. */
4137 case OFPACT_CONTROLLER:
4138 controller = ofpact_get_CONTROLLER(a);
4139 execute_controller_action(ctx, controller->max_len,
4141 controller->controller_id);
4144 case OFPACT_ENQUEUE:
4145 memset(&wc->masks.skb_priority, 0xff,
4146 sizeof wc->masks.skb_priority);
4147 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
4150 case OFPACT_SET_VLAN_VID:
4151 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
4152 if (flow->vlan_tci & htons(VLAN_CFI) ||
4153 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
4154 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
4155 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
4160 case OFPACT_SET_VLAN_PCP:
4161 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
4162 if (flow->vlan_tci & htons(VLAN_CFI) ||
4163 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
4164 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
4165 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
4166 << VLAN_PCP_SHIFT) | VLAN_CFI);
4170 case OFPACT_STRIP_VLAN:
4171 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4172 flow->vlan_tci = htons(0);
4175 case OFPACT_PUSH_VLAN:
4176 /* XXX 802.1AD(QinQ) */
4177 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4178 flow->vlan_tci = htons(VLAN_CFI);
4181 case OFPACT_SET_ETH_SRC:
4182 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
4183 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
4186 case OFPACT_SET_ETH_DST:
4187 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
4188 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
4191 case OFPACT_SET_IPV4_SRC:
4192 CHECK_MPLS_RECIRCULATION();
4193 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4194 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
4195 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
4199 case OFPACT_SET_IPV4_DST:
4200 CHECK_MPLS_RECIRCULATION();
4201 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4202 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
4203 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
4207 case OFPACT_SET_IP_DSCP:
4208 CHECK_MPLS_RECIRCULATION();
4209 if (is_ip_any(flow)) {
4210 wc->masks.nw_tos |= IP_DSCP_MASK;
4211 flow->nw_tos &= ~IP_DSCP_MASK;
4212 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
4216 case OFPACT_SET_IP_ECN:
4217 CHECK_MPLS_RECIRCULATION();
4218 if (is_ip_any(flow)) {
4219 wc->masks.nw_tos |= IP_ECN_MASK;
4220 flow->nw_tos &= ~IP_ECN_MASK;
4221 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
4225 case OFPACT_SET_IP_TTL:
4226 CHECK_MPLS_RECIRCULATION();
4227 if (is_ip_any(flow)) {
4228 wc->masks.nw_ttl = 0xff;
4229 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
4233 case OFPACT_SET_L4_SRC_PORT:
4234 CHECK_MPLS_RECIRCULATION();
4235 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4236 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4237 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
4238 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
4242 case OFPACT_SET_L4_DST_PORT:
4243 CHECK_MPLS_RECIRCULATION();
4244 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4245 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4246 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
4247 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
4251 case OFPACT_RESUBMIT:
4252 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
4255 case OFPACT_SET_TUNNEL:
4256 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
4259 case OFPACT_SET_QUEUE:
4260 memset(&wc->masks.skb_priority, 0xff,
4261 sizeof wc->masks.skb_priority);
4262 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
4265 case OFPACT_POP_QUEUE:
4266 memset(&wc->masks.skb_priority, 0xff,
4267 sizeof wc->masks.skb_priority);
4268 flow->skb_priority = ctx->orig_skb_priority;
4271 case OFPACT_REG_MOVE:
4272 CHECK_MPLS_RECIRCULATION_IF(
4273 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
4274 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
4275 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
4278 case OFPACT_SET_FIELD:
4279 CHECK_MPLS_RECIRCULATION_IF(
4280 mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field));
4281 set_field = ofpact_get_SET_FIELD(a);
4282 mf = set_field->field;
4284 /* Set field action only ever overwrites packet's outermost
4285 * applicable header fields. Do nothing if no header exists. */
4286 if (mf->id == MFF_VLAN_VID) {
4287 wc->masks.vlan_tci |= htons(VLAN_CFI);
4288 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4291 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
4292 /* 'dl_type' is already unwildcarded. */
4293 && !eth_type_mpls(flow->dl_type)) {
4296 /* A flow may wildcard nw_frag. Do nothing if setting a trasport
4297 * header field on a packet that does not have them. */
4298 mf_mask_field_and_prereqs(mf, &wc->masks);
4299 if (mf_are_prereqs_ok(mf, flow)) {
4300 mf_set_flow_value_masked(mf, &set_field->value,
4301 &set_field->mask, flow);
4305 case OFPACT_STACK_PUSH:
4306 CHECK_MPLS_RECIRCULATION_IF(
4307 mf_is_l3_or_higher(ofpact_get_STACK_PUSH(a)->subfield.field));
4308 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
4312 case OFPACT_STACK_POP:
4313 CHECK_MPLS_RECIRCULATION_IF(
4314 mf_is_l3_or_higher(ofpact_get_STACK_POP(a)->subfield.field));
4315 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
4319 case OFPACT_PUSH_MPLS:
4320 /* Recirculate if it is an IP packet with a zero ttl. This may
4321 * indicate that the packet was previously MPLS and an MPLS pop
4322 * action converted it to IP. In this case recirculating should
4323 * reveal the IP TTL which is used as the basis for a new MPLS
4325 CHECK_MPLS_RECIRCULATION_IF(
4326 !flow_count_mpls_labels(flow, wc)
4327 && flow->nw_ttl == 0
4328 && is_ip_any(flow));
4329 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
4332 case OFPACT_POP_MPLS:
4333 CHECK_MPLS_RECIRCULATION();
4334 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
4337 case OFPACT_SET_MPLS_LABEL:
4338 CHECK_MPLS_RECIRCULATION();
4339 compose_set_mpls_label_action(
4340 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
4343 case OFPACT_SET_MPLS_TC:
4344 CHECK_MPLS_RECIRCULATION();
4345 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
4348 case OFPACT_SET_MPLS_TTL:
4349 CHECK_MPLS_RECIRCULATION();
4350 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
4353 case OFPACT_DEC_MPLS_TTL:
4354 CHECK_MPLS_RECIRCULATION();
4355 if (compose_dec_mpls_ttl_action(ctx)) {
4360 case OFPACT_DEC_TTL:
4361 CHECK_MPLS_RECIRCULATION();
4362 wc->masks.nw_ttl = 0xff;
4363 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
4369 /* Nothing to do. */
4372 case OFPACT_MULTIPATH:
4373 CHECK_MPLS_RECIRCULATION();
4374 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
4378 CHECK_MPLS_RECIRCULATION();
4379 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
4382 case OFPACT_OUTPUT_REG:
4383 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
4387 CHECK_MPLS_RECIRCULATION();
4388 xlate_learn_action(ctx, ofpact_get_LEARN(a));
4391 case OFPACT_CONJUNCTION: {
4392 /* A flow with a "conjunction" action represents part of a special
4393 * kind of "set membership match". Such a flow should not actually
4394 * get executed, but it could via, say, a "packet-out", even though
4395 * that wouldn't be useful. Log it to help debugging. */
4396 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4397 VLOG_INFO_RL(&rl, "executing no-op conjunction action");
4405 case OFPACT_UNROLL_XLATE: {
4406 struct ofpact_unroll_xlate *unroll = ofpact_get_UNROLL_XLATE(a);
4408 /* Restore translation context data that was stored earlier. */
4409 ctx->table_id = unroll->rule_table_id;
4410 ctx->rule_cookie = unroll->rule_cookie;
4413 case OFPACT_FIN_TIMEOUT:
4414 CHECK_MPLS_RECIRCULATION();
4415 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4416 ctx->xout->has_fin_timeout = true;
4417 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
4420 case OFPACT_CLEAR_ACTIONS:
4421 ofpbuf_clear(&ctx->action_set);
4422 ctx->xin->flow.actset_output = OFPP_UNSET;
4423 ctx->action_set_has_group = false;
4426 case OFPACT_WRITE_ACTIONS:
4427 xlate_write_actions(ctx, a);
4430 case OFPACT_WRITE_METADATA:
4431 metadata = ofpact_get_WRITE_METADATA(a);
4432 flow->metadata &= ~metadata->mask;
4433 flow->metadata |= metadata->metadata & metadata->mask;
4437 /* Not implemented yet. */
4440 case OFPACT_GOTO_TABLE: {
4441 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
4443 /* Allow ctx->table_id == TBL_INTERNAL, which will be greater
4444 * than ogt->table_id. This is to allow goto_table actions that
4445 * triggered recirculation: ctx->table_id will be TBL_INTERNAL
4446 * after recirculation. */
4447 ovs_assert(ctx->table_id == TBL_INTERNAL
4448 || ctx->table_id < ogt->table_id);
4449 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
4450 ogt->table_id, true, true);
4455 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
4459 /* Check if need to store this and the remaining actions for later
4461 if (ctx->exit && ctx_first_recirculation_action(ctx)) {
4462 recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
4464 (uint8_t *)ofpacts)),
4472 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
4473 const struct flow *flow, ofp_port_t in_port,
4474 struct rule_dpif *rule, uint16_t tcp_flags,
4475 const struct dp_packet *packet)
4477 xin->ofproto = ofproto;
4479 xin->flow.in_port.ofp_port = in_port;
4480 xin->flow.actset_output = OFPP_UNSET;
4481 xin->packet = packet;
4482 xin->may_learn = packet != NULL;
4485 xin->ofpacts = NULL;
4486 xin->ofpacts_len = 0;
4487 xin->tcp_flags = tcp_flags;
4488 xin->resubmit_hook = NULL;
4489 xin->report_hook = NULL;
4490 xin->resubmit_stats = NULL;
4491 xin->skip_wildcards = false;
4492 xin->odp_actions = NULL;
4494 /* Do recirc lookup. */
4495 xin->recirc = flow->recirc_id
4496 ? recirc_id_node_find(flow->recirc_id)
4501 xlate_out_uninit(struct xlate_out *xout)
4504 if (xout->odp_actions == &xout->odp_actions_buf) {
4505 ofpbuf_uninit(xout->odp_actions);
4507 xlate_out_free_recircs(xout);
4511 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
4512 * into datapath actions, using 'ctx', and discards the datapath actions. */
4514 xlate_actions_for_side_effects(struct xlate_in *xin)
4516 struct xlate_out xout;
4518 xlate_actions(xin, &xout);
4519 xlate_out_uninit(&xout);
4523 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
4526 dst->slow = src->slow;
4527 dst->has_learn = src->has_learn;
4528 dst->has_normal = src->has_normal;
4529 dst->has_fin_timeout = src->has_fin_timeout;
4530 dst->nf_output_iface = src->nf_output_iface;
4531 dst->mirrors = src->mirrors;
4533 dst->odp_actions = &dst->odp_actions_buf;
4534 ofpbuf_use_stub(dst->odp_actions, dst->odp_actions_stub,
4535 sizeof dst->odp_actions_stub);
4536 ofpbuf_put(dst->odp_actions, src->odp_actions->data, src->odp_actions->size);
4539 static struct skb_priority_to_dscp *
4540 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
4542 struct skb_priority_to_dscp *pdscp;
4545 hash = hash_int(skb_priority, 0);
4546 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
4547 if (pdscp->skb_priority == skb_priority) {
4555 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
4558 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
4559 *dscp = pdscp ? pdscp->dscp : 0;
4560 return pdscp != NULL;
4564 count_skb_priorities(const struct xport *xport)
4566 return hmap_count(&xport->skb_priorities);
4570 clear_skb_priorities(struct xport *xport)
4572 struct skb_priority_to_dscp *pdscp, *next;
4574 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
4575 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
4581 actions_output_to_local_port(const struct xlate_ctx *ctx)
4583 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
4584 const struct nlattr *a;
4587 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions->data,
4588 ctx->xout->odp_actions->size) {
4589 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
4590 && nl_attr_get_odp_port(a) == local_odp_port) {
4597 #if defined(__linux__)
4598 /* Returns the maximum number of packets that the Linux kernel is willing to
4599 * queue up internally to certain kinds of software-implemented ports, or the
4600 * default (and rarely modified) value if it cannot be determined. */
4602 netdev_max_backlog(void)
4604 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
4605 static int max_backlog = 1000; /* The normal default value. */
4607 if (ovsthread_once_start(&once)) {
4608 static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
4612 stream = fopen(filename, "r");
4614 VLOG_WARN("%s: open failed (%s)", filename, ovs_strerror(errno));
4616 if (fscanf(stream, "%d", &n) != 1) {
4617 VLOG_WARN("%s: read error", filename);
4618 } else if (n <= 100) {
4619 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
4625 ovsthread_once_done(&once);
4627 VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
4633 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
4636 count_output_actions(const struct ofpbuf *odp_actions)
4638 const struct nlattr *a;
4642 NL_ATTR_FOR_EACH_UNSAFE (a, left, odp_actions->data, odp_actions->size) {
4643 if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
4649 #endif /* defined(__linux__) */
4651 /* Returns true if 'odp_actions' contains more output actions than the datapath
4652 * can reliably handle in one go. On Linux, this is the value of the
4653 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
4654 * packets that the kernel is willing to queue up for processing while the
4655 * datapath is processing a set of actions. */
4657 too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
4660 return (odp_actions->size / NL_A_U32_SIZE > netdev_max_backlog()
4661 && count_output_actions(odp_actions) > netdev_max_backlog());
4663 /* OSes other than Linux might have similar limits, but we don't know how
4664 * to determine them.*/
4669 /* Translates the flow, actions, or rule in 'xin' into datapath actions in
4671 * The caller must take responsibility for eventually freeing 'xout', with
4672 * xlate_out_uninit(). */
4674 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
4676 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4677 struct flow_wildcards *wc = NULL;
4678 struct flow *flow = &xin->flow;
4679 struct rule_dpif *rule = NULL;
4681 enum slow_path_reason special;
4682 const struct ofpact *ofpacts;
4683 struct xbridge *xbridge;
4684 struct xport *in_port;
4685 struct flow orig_flow;
4686 struct xlate_ctx ctx;
4691 COVERAGE_INC(xlate_actions);
4693 /* Flow initialization rules:
4694 * - 'base_flow' must match the kernel's view of the packet at the
4695 * time that action processing starts. 'flow' represents any
4696 * transformations we wish to make through actions.
4697 * - By default 'base_flow' and 'flow' are the same since the input
4698 * packet matches the output before any actions are applied.
4699 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
4700 * of the received packet as seen by the kernel. If we later output
4701 * to another device without any modifications this will cause us to
4702 * insert a new tag since the original one was stripped off by the
4704 * - Tunnel metadata as received is retained in 'flow'. This allows
4705 * tunnel metadata matching also in later tables.
4706 * Since a kernel action for setting the tunnel metadata will only be
4707 * generated with actual tunnel output, changing the tunnel metadata
4708 * values in 'flow' (such as tun_id) will only have effect with a later
4709 * tunnel output action.
4710 * - Tunnel 'base_flow' is completely cleared since that is what the
4711 * kernel does. If we wish to maintain the original values an action
4712 * needs to be generated. */
4717 ctx.xout->has_learn = false;
4718 ctx.xout->has_normal = false;
4719 ctx.xout->has_fin_timeout = false;
4720 ctx.xout->nf_output_iface = NF_OUT_DROP;
4721 ctx.xout->mirrors = 0;
4722 ctx.xout->n_recircs = 0;
4724 xout->odp_actions = xin->odp_actions;
4725 if (!xout->odp_actions) {
4726 xout->odp_actions = &xout->odp_actions_buf;
4727 ofpbuf_use_stub(xout->odp_actions, xout->odp_actions_stub,
4728 sizeof xout->odp_actions_stub);
4730 ofpbuf_reserve(xout->odp_actions, NL_A_U32_SIZE);
4732 xbridge = xbridge_lookup(xcfg, xin->ofproto);
4736 /* 'ctx.xbridge' may be changed by action processing, whereas 'xbridge'
4737 * will remain set on the original input bridge. */
4738 ctx.xbridge = xbridge;
4739 ctx.rule = xin->rule;
4741 ctx.base_flow = *flow;
4742 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
4743 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
4745 if (!xin->skip_wildcards) {
4747 flow_wildcards_init_catchall(wc);
4748 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
4749 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
4750 if (is_ip_any(flow)) {
4751 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
4753 if (xbridge->support.recirc) {
4754 /* Always exactly match recirc_id when datapath supports
4756 wc->masks.recirc_id = UINT32_MAX;
4758 if (xbridge->netflow) {
4759 netflow_mask_wc(flow, wc);
4762 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
4764 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
4768 ctx.in_group = false;
4769 ctx.in_action_set = false;
4770 ctx.orig_skb_priority = flow->skb_priority;
4772 ctx.rule_cookie = OVS_BE64_MAX;
4774 ctx.was_mpls = false;
4775 ctx.recirc_action_offset = -1;
4776 ctx.last_unroll_offset = -1;
4778 ctx.action_set_has_group = false;
4779 ofpbuf_use_stub(&ctx.action_set,
4780 ctx.action_set_stub, sizeof ctx.action_set_stub);
4782 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
4784 /* The in_port of the original packet before recirculation. */
4785 in_port = get_ofp_port(xbridge, flow->in_port.ofp_port);
4788 const struct recirc_id_node *recirc = xin->recirc;
4790 if (xin->ofpacts_len > 0 || ctx.rule) {
4791 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4793 VLOG_WARN_RL(&rl, "Recirculation conflict (%s)!",
4794 xin->ofpacts_len > 0
4800 /* Set the bridge for post-recirculation processing if needed. */
4801 if (ctx.xbridge->ofproto != recirc->ofproto) {
4802 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4803 const struct xbridge *new_bridge = xbridge_lookup(xcfg,
4806 if (OVS_UNLIKELY(!new_bridge)) {
4807 /* Drop the packet if the bridge cannot be found. */
4808 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4809 VLOG_WARN_RL(&rl, "Recirculation bridge no longer exists.");
4812 ctx.xbridge = new_bridge;
4815 /* Set the post-recirculation table id. Note: A table lookup is done
4816 * only if there are no post-recirculation actions. */
4817 ctx.table_id = recirc->table_id;
4819 /* Restore pipeline metadata. May change flow's in_port and other
4820 * metadata to the values that existed when recirculation was
4822 recirc_metadata_to_flow(&recirc->metadata, flow);
4824 /* Restore stack, if any. */
4825 if (recirc->stack) {
4826 ofpbuf_put(&ctx.stack, recirc->stack->data, recirc->stack->size);
4829 /* Restore action set, if any. */
4830 if (recirc->action_set_len) {
4831 const struct ofpact *a;
4833 ofpbuf_put(&ctx.action_set, recirc->ofpacts,
4834 recirc->action_set_len);
4836 OFPACT_FOR_EACH(a, recirc->ofpacts, recirc->action_set_len) {
4837 if (a->type == OFPACT_GROUP) {
4838 ctx.action_set_has_group = true;
4844 /* Restore recirculation actions. If there are no actions, processing
4845 * will start with a lookup in the table set above. */
4846 if (recirc->ofpacts_len > recirc->action_set_len) {
4847 xin->ofpacts_len = recirc->ofpacts_len - recirc->action_set_len;
4848 xin->ofpacts = recirc->ofpacts +
4849 recirc->action_set_len / sizeof *recirc->ofpacts;
4851 } else if (OVS_UNLIKELY(flow->recirc_id)) {
4852 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4854 VLOG_WARN_RL(&rl, "Recirculation context not found for ID %"PRIx32,
4858 /* The bridge is now known so obtain its table version. */
4859 ctx.tables_version = ofproto_dpif_get_tables_version(ctx.xbridge->ofproto);
4861 if (!xin->ofpacts && !ctx.rule) {
4862 rule = rule_dpif_lookup_from_table(ctx.xbridge->ofproto,
4863 ctx.tables_version, flow, wc,
4864 ctx.xin->xcache != NULL,
4865 ctx.xin->resubmit_stats,
4867 flow->in_port.ofp_port, true, true);
4868 if (ctx.xin->resubmit_stats) {
4869 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
4871 if (ctx.xin->xcache) {
4872 struct xc_entry *entry;
4874 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
4875 entry->u.rule = rule;
4879 if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
4880 ctx.xin->resubmit_hook(ctx.xin, rule, 0);
4883 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
4886 ofpacts = xin->ofpacts;
4887 ofpacts_len = xin->ofpacts_len;
4888 } else if (ctx.rule) {
4889 const struct rule_actions *actions = rule_dpif_get_actions(ctx.rule);
4891 ofpacts = actions->ofpacts;
4892 ofpacts_len = actions->ofpacts_len;
4894 ctx.rule_cookie = rule_dpif_get_flow_cookie(ctx.rule);
4899 if (mbridge_has_mirrors(xbridge->mbridge)) {
4900 /* Do this conditionally because the copy is expensive enough that it
4901 * shows up in profiles. */
4905 /* Tunnel stats only for non-recirculated packets. */
4906 if (!xin->recirc && in_port && in_port->is_tunnel) {
4907 if (ctx.xin->resubmit_stats) {
4908 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
4910 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
4913 if (ctx.xin->xcache) {
4914 struct xc_entry *entry;
4916 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
4917 entry->u.dev.rx = netdev_ref(in_port->netdev);
4918 entry->u.dev.bfd = bfd_ref(in_port->bfd);
4922 /* Do not perform special processing on recirculated packets,
4923 * as recirculated packets are not really received by the bridge. */
4925 (special = process_special(&ctx, flow, in_port, ctx.xin->packet))) {
4926 ctx.xout->slow |= special;
4928 size_t sample_actions_len;
4930 if (flow->in_port.ofp_port
4931 != vsp_realdev_to_vlandev(xbridge->ofproto,
4932 flow->in_port.ofp_port,
4934 ctx.base_flow.vlan_tci = 0;
4937 /* Sampling is done only for packets really received by the bridge. */
4939 add_sflow_action(&ctx);
4940 add_ipfix_action(&ctx);
4941 sample_actions_len = ctx.xout->odp_actions->size;
4943 sample_actions_len = 0;
4946 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
4947 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
4949 /* We've let OFPP_NORMAL and the learning action look at the
4950 * packet, so drop it now if forwarding is disabled. */
4951 if (in_port && (!xport_stp_forward_state(in_port) ||
4952 !xport_rstp_forward_state(in_port))) {
4953 /* Drop all actions added by do_xlate_actions() above. */
4954 ctx.xout->odp_actions->size = sample_actions_len;
4956 /* Undo changes that may have been done for recirculation. */
4957 if (exit_recirculates(&ctx)) {
4958 ctx.action_set.size = ctx.recirc_action_offset;
4959 ctx.recirc_action_offset = -1;
4960 ctx.last_unroll_offset = -1;
4962 } else if (ctx.action_set.size) {
4963 /* Translate action set only if not dropping the packet and
4964 * not recirculating. */
4965 if (!exit_recirculates(&ctx)) {
4966 xlate_action_set(&ctx);
4969 /* Check if need to recirculate. */
4970 if (exit_recirculates(&ctx)) {
4971 compose_recirculate_action(&ctx);
4975 /* Output only fully processed packets. */
4976 if (!exit_recirculates(&ctx)
4977 && xbridge->has_in_band
4978 && in_band_must_output_to_local_port(flow)
4979 && !actions_output_to_local_port(&ctx)) {
4980 compose_output_action(&ctx, OFPP_LOCAL, NULL);
4984 fix_sflow_action(&ctx);
4986 /* Only mirror fully processed packets. */
4987 if (!exit_recirculates(&ctx)
4988 && mbridge_has_mirrors(xbridge->mbridge)) {
4989 add_mirror_actions(&ctx, &orig_flow);
4993 if (nl_attr_oversized(ctx.xout->odp_actions->size)) {
4994 /* These datapath actions are too big for a Netlink attribute, so we
4995 * can't hand them to the kernel directly. dpif_execute() can execute
4996 * them one by one with help, so just mark the result as SLOW_ACTION to
4997 * prevent the flow from being installed. */
4998 COVERAGE_INC(xlate_actions_oversize);
4999 ctx.xout->slow |= SLOW_ACTION;
5000 } else if (too_many_output_actions(ctx.xout->odp_actions)) {
5001 COVERAGE_INC(xlate_actions_too_many_output);
5002 ctx.xout->slow |= SLOW_ACTION;
5005 /* Update mirror stats only for packets really received by the bridge. */
5006 if (!xin->recirc && mbridge_has_mirrors(xbridge->mbridge)) {
5007 if (ctx.xin->resubmit_stats) {
5008 mirror_update_stats(xbridge->mbridge, xout->mirrors,
5009 ctx.xin->resubmit_stats->n_packets,
5010 ctx.xin->resubmit_stats->n_bytes);
5012 if (ctx.xin->xcache) {
5013 struct xc_entry *entry;
5015 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_MIRROR);
5016 entry->u.mirror.mbridge = mbridge_ref(xbridge->mbridge);
5017 entry->u.mirror.mirrors = xout->mirrors;
5021 /* Do netflow only for packets really received by the bridge. */
5022 if (!xin->recirc && xbridge->netflow) {
5023 /* Only update netflow if we don't have controller flow. We don't
5024 * report NetFlow expiration messages for such facets because they
5025 * are just part of the control logic for the network, not real
5027 if (ofpacts_len == 0
5028 || ofpacts->type != OFPACT_CONTROLLER
5029 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
5030 if (ctx.xin->resubmit_stats) {
5031 netflow_flow_update(xbridge->netflow, flow,
5032 xout->nf_output_iface,
5033 ctx.xin->resubmit_stats);
5035 if (ctx.xin->xcache) {
5036 struct xc_entry *entry;
5038 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
5039 entry->u.nf.netflow = netflow_ref(xbridge->netflow);
5040 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
5041 entry->u.nf.iface = xout->nf_output_iface;
5046 ofpbuf_uninit(&ctx.stack);
5047 ofpbuf_uninit(&ctx.action_set);
5050 /* Clear the metadata and register wildcard masks, because we won't
5051 * use non-header fields as part of the cache. */
5052 flow_wildcards_clear_non_packet_fields(wc);
5054 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
5055 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
5056 * represent these fields. The datapath interface, on the other hand,
5057 * represents them with just 8 bits each. This means that if the high
5058 * 8 bits of the masks for these fields somehow become set, then they
5059 * will get chopped off by a round trip through the datapath, and
5060 * revalidation will spot that as an inconsistency and delete the flow.
5061 * Avoid the problem here by making sure that only the low 8 bits of
5062 * either field can be unwildcarded for ICMP.
5065 wc->masks.tp_src &= htons(UINT8_MAX);
5066 wc->masks.tp_dst &= htons(UINT8_MAX);
5068 /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
5069 if (wc->masks.vlan_tci) {
5070 wc->masks.vlan_tci |= htons(VLAN_CFI);
5075 /* Sends 'packet' out 'ofport'.
5076 * May modify 'packet'.
5077 * Returns 0 if successful, otherwise a positive errno value. */
5079 xlate_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
5081 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5082 struct xport *xport;
5083 struct ofpact_output output;
5086 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
5087 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
5088 flow_extract(packet, &flow);
5089 flow.in_port.ofp_port = OFPP_NONE;
5091 xport = xport_lookup(xcfg, ofport);
5095 output.port = xport->ofp_port;
5098 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
5099 &output.ofpact, sizeof output,
5103 struct xlate_cache *
5104 xlate_cache_new(void)
5106 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
5108 ofpbuf_init(&xcache->entries, 512);
5112 static struct xc_entry *
5113 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
5115 struct xc_entry *entry;
5117 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
5124 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
5126 if (entry->u.dev.tx) {
5127 netdev_vport_inc_tx(entry->u.dev.tx, stats);
5129 if (entry->u.dev.rx) {
5130 netdev_vport_inc_rx(entry->u.dev.rx, stats);
5132 if (entry->u.dev.bfd) {
5133 bfd_account_rx(entry->u.dev.bfd, stats);
5138 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
5140 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5141 struct xbridge *xbridge;
5142 struct xbundle *xbundle;
5143 struct flow_wildcards wc;
5145 xbridge = xbridge_lookup(xcfg, ofproto);
5150 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
5156 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
5159 /* Push stats and perform side effects of flow translation. */
5161 xlate_push_stats(struct xlate_cache *xcache,
5162 const struct dpif_flow_stats *stats)
5164 struct xc_entry *entry;
5165 struct ofpbuf entries = xcache->entries;
5166 uint8_t dmac[ETH_ADDR_LEN];
5168 if (!stats->n_packets) {
5172 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5173 switch (entry->type) {
5175 rule_dpif_credit_stats(entry->u.rule, stats);
5178 bond_account(entry->u.bond.bond, entry->u.bond.flow,
5179 entry->u.bond.vid, stats->n_bytes);
5182 xlate_cache_netdev(entry, stats);
5185 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
5186 entry->u.nf.iface, stats);
5189 mirror_update_stats(entry->u.mirror.mbridge,
5190 entry->u.mirror.mirrors,
5191 stats->n_packets, stats->n_bytes);
5194 ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
5197 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
5198 entry->u.normal.vlan);
5200 case XC_FIN_TIMEOUT:
5201 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
5202 entry->u.fin.idle, entry->u.fin.hard);
5205 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
5209 /* Lookup arp to avoid arp timeout. */
5210 tnl_arp_lookup(entry->u.tnl_arp_cache.br_name, entry->u.tnl_arp_cache.d_ip, dmac);
5219 xlate_dev_unref(struct xc_entry *entry)
5221 if (entry->u.dev.tx) {
5222 netdev_close(entry->u.dev.tx);
5224 if (entry->u.dev.rx) {
5225 netdev_close(entry->u.dev.rx);
5227 if (entry->u.dev.bfd) {
5228 bfd_unref(entry->u.dev.bfd);
5233 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
5235 netflow_flow_clear(netflow, flow);
5236 netflow_unref(netflow);
5241 xlate_cache_clear(struct xlate_cache *xcache)
5243 struct xc_entry *entry;
5244 struct ofpbuf entries;
5250 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5251 switch (entry->type) {
5253 rule_dpif_unref(entry->u.rule);
5256 free(entry->u.bond.flow);
5257 bond_unref(entry->u.bond.bond);
5260 xlate_dev_unref(entry);
5263 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
5266 mbridge_unref(entry->u.mirror.mbridge);
5269 free(entry->u.learn.fm);
5270 ofpbuf_delete(entry->u.learn.ofpacts);
5273 free(entry->u.normal.flow);
5275 case XC_FIN_TIMEOUT:
5276 /* 'u.fin.rule' is always already held as a XC_RULE, which
5277 * has already released it's reference above. */
5280 group_dpif_unref(entry->u.group.group);
5289 ofpbuf_clear(&xcache->entries);
5293 xlate_cache_delete(struct xlate_cache *xcache)
5295 xlate_cache_clear(xcache);
5296 ofpbuf_uninit(&xcache->entries);