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 OVS_PRINTF_FORMAT(2, 3);
441 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
442 uint8_t table_id, bool may_packet_in,
443 bool honor_table_miss);
444 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
445 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
446 static void output_normal(struct xlate_ctx *, const struct xbundle *,
449 /* Optional bond recirculation parameter to compose_output_action(). */
450 struct xlate_bond_recirc {
451 uint32_t recirc_id; /* !0 Use recirculation instead of output. */
452 uint8_t hash_alg; /* !0 Compute hash for recirc before. */
453 uint32_t hash_basis; /* Compute hash for recirc before. */
456 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port,
457 const struct xlate_bond_recirc *xr);
459 static struct xbridge *xbridge_lookup(struct xlate_cfg *,
460 const struct ofproto_dpif *);
461 static struct xbundle *xbundle_lookup(struct xlate_cfg *,
462 const struct ofbundle *);
463 static struct xport *xport_lookup(struct xlate_cfg *,
464 const struct ofport_dpif *);
465 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
466 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
467 uint32_t skb_priority);
468 static void clear_skb_priorities(struct xport *);
469 static size_t count_skb_priorities(const struct xport *);
470 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
473 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
475 static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
476 static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
477 static void xlate_xport_init(struct xlate_cfg *, struct xport *);
478 static void xlate_xbridge_set(struct xbridge *, struct dpif *,
479 const struct mac_learning *, struct stp *,
480 struct rstp *, const struct mcast_snooping *,
481 const struct mbridge *,
482 const struct dpif_sflow *,
483 const struct dpif_ipfix *,
484 const struct netflow *,
485 bool forward_bpdu, bool has_in_band,
486 const struct dpif_backer_support *);
487 static void xlate_xbundle_set(struct xbundle *xbundle,
488 enum port_vlan_mode vlan_mode, int vlan,
489 unsigned long *trunks, bool use_priority_tags,
490 const struct bond *bond, const struct lacp *lacp,
492 static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
493 const struct netdev *netdev, const struct cfm *cfm,
494 const struct bfd *bfd, const struct lldp *lldp,
495 int stp_port_no, const struct rstp_port *rstp_port,
496 enum ofputil_port_config config,
497 enum ofputil_port_state state, bool is_tunnel,
499 static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
500 static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
501 static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
502 static void xlate_xbridge_copy(struct xbridge *);
503 static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
504 static void xlate_xport_copy(struct xbridge *, struct xbundle *,
506 static void xlate_xcfg_free(struct xlate_cfg *);
509 xlate_report(struct xlate_ctx *ctx, const char *format, ...)
511 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
514 va_start(args, format);
515 ctx->xin->report_hook(ctx->xin, ctx->recurse, format, args);
521 xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
523 list_init(&xbridge->xbundles);
524 hmap_init(&xbridge->xports);
525 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
526 hash_pointer(xbridge->ofproto, 0));
530 xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
532 list_init(&xbundle->xports);
533 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
534 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
535 hash_pointer(xbundle->ofbundle, 0));
539 xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
541 hmap_init(&xport->skb_priorities);
542 hmap_insert(&xcfg->xports, &xport->hmap_node,
543 hash_pointer(xport->ofport, 0));
544 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
545 hash_ofp_port(xport->ofp_port));
549 xlate_xbridge_set(struct xbridge *xbridge,
551 const struct mac_learning *ml, struct stp *stp,
552 struct rstp *rstp, const struct mcast_snooping *ms,
553 const struct mbridge *mbridge,
554 const struct dpif_sflow *sflow,
555 const struct dpif_ipfix *ipfix,
556 const struct netflow *netflow,
557 bool forward_bpdu, bool has_in_band,
558 const struct dpif_backer_support *support)
560 if (xbridge->ml != ml) {
561 mac_learning_unref(xbridge->ml);
562 xbridge->ml = mac_learning_ref(ml);
565 if (xbridge->ms != ms) {
566 mcast_snooping_unref(xbridge->ms);
567 xbridge->ms = mcast_snooping_ref(ms);
570 if (xbridge->mbridge != mbridge) {
571 mbridge_unref(xbridge->mbridge);
572 xbridge->mbridge = mbridge_ref(mbridge);
575 if (xbridge->sflow != sflow) {
576 dpif_sflow_unref(xbridge->sflow);
577 xbridge->sflow = dpif_sflow_ref(sflow);
580 if (xbridge->ipfix != ipfix) {
581 dpif_ipfix_unref(xbridge->ipfix);
582 xbridge->ipfix = dpif_ipfix_ref(ipfix);
585 if (xbridge->stp != stp) {
586 stp_unref(xbridge->stp);
587 xbridge->stp = stp_ref(stp);
590 if (xbridge->rstp != rstp) {
591 rstp_unref(xbridge->rstp);
592 xbridge->rstp = rstp_ref(rstp);
595 if (xbridge->netflow != netflow) {
596 netflow_unref(xbridge->netflow);
597 xbridge->netflow = netflow_ref(netflow);
600 xbridge->dpif = dpif;
601 xbridge->forward_bpdu = forward_bpdu;
602 xbridge->has_in_band = has_in_band;
603 xbridge->support = *support;
607 xlate_xbundle_set(struct xbundle *xbundle,
608 enum port_vlan_mode vlan_mode, int vlan,
609 unsigned long *trunks, bool use_priority_tags,
610 const struct bond *bond, const struct lacp *lacp,
613 ovs_assert(xbundle->xbridge);
615 xbundle->vlan_mode = vlan_mode;
616 xbundle->vlan = vlan;
617 xbundle->trunks = trunks;
618 xbundle->use_priority_tags = use_priority_tags;
619 xbundle->floodable = floodable;
621 if (xbundle->bond != bond) {
622 bond_unref(xbundle->bond);
623 xbundle->bond = bond_ref(bond);
626 if (xbundle->lacp != lacp) {
627 lacp_unref(xbundle->lacp);
628 xbundle->lacp = lacp_ref(lacp);
633 xlate_xport_set(struct xport *xport, odp_port_t odp_port,
634 const struct netdev *netdev, const struct cfm *cfm,
635 const struct bfd *bfd, const struct lldp *lldp, int stp_port_no,
636 const struct rstp_port* rstp_port,
637 enum ofputil_port_config config, enum ofputil_port_state state,
638 bool is_tunnel, bool may_enable)
640 xport->config = config;
641 xport->state = state;
642 xport->stp_port_no = stp_port_no;
643 xport->is_tunnel = is_tunnel;
644 xport->may_enable = may_enable;
645 xport->odp_port = odp_port;
647 if (xport->rstp_port != rstp_port) {
648 rstp_port_unref(xport->rstp_port);
649 xport->rstp_port = rstp_port_ref(rstp_port);
652 if (xport->cfm != cfm) {
653 cfm_unref(xport->cfm);
654 xport->cfm = cfm_ref(cfm);
657 if (xport->bfd != bfd) {
658 bfd_unref(xport->bfd);
659 xport->bfd = bfd_ref(bfd);
662 if (xport->lldp != lldp) {
663 lldp_unref(xport->lldp);
664 xport->lldp = lldp_ref(lldp);
667 if (xport->netdev != netdev) {
668 netdev_close(xport->netdev);
669 xport->netdev = netdev_ref(netdev);
674 xlate_xbridge_copy(struct xbridge *xbridge)
676 struct xbundle *xbundle;
678 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
679 new_xbridge->ofproto = xbridge->ofproto;
680 new_xbridge->name = xstrdup(xbridge->name);
681 xlate_xbridge_init(new_xcfg, new_xbridge);
683 xlate_xbridge_set(new_xbridge,
684 xbridge->dpif, xbridge->ml, xbridge->stp,
685 xbridge->rstp, xbridge->ms, xbridge->mbridge,
686 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
687 xbridge->forward_bpdu, xbridge->has_in_band,
689 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
690 xlate_xbundle_copy(new_xbridge, xbundle);
693 /* Copy xports which are not part of a xbundle */
694 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
695 if (!xport->xbundle) {
696 xlate_xport_copy(new_xbridge, NULL, xport);
702 xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
705 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
706 new_xbundle->ofbundle = xbundle->ofbundle;
707 new_xbundle->xbridge = xbridge;
708 new_xbundle->name = xstrdup(xbundle->name);
709 xlate_xbundle_init(new_xcfg, new_xbundle);
711 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
712 xbundle->vlan, xbundle->trunks,
713 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
715 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
716 xlate_xport_copy(xbridge, new_xbundle, xport);
721 xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
724 struct skb_priority_to_dscp *pdscp, *new_pdscp;
725 struct xport *new_xport = xzalloc(sizeof *xport);
726 new_xport->ofport = xport->ofport;
727 new_xport->ofp_port = xport->ofp_port;
728 new_xport->xbridge = xbridge;
729 xlate_xport_init(new_xcfg, new_xport);
731 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
732 xport->bfd, xport->lldp, xport->stp_port_no,
733 xport->rstp_port, xport->config, xport->state,
734 xport->is_tunnel, xport->may_enable);
737 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
739 new_xport->peer = peer;
740 new_xport->peer->peer = new_xport;
745 new_xport->xbundle = xbundle;
746 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
749 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
750 new_pdscp = xmalloc(sizeof *pdscp);
751 new_pdscp->skb_priority = pdscp->skb_priority;
752 new_pdscp->dscp = pdscp->dscp;
753 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
754 hash_int(new_pdscp->skb_priority, 0));
758 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
759 * configuration in xcfgp.
761 * This needs to be called after editing the xlate configuration.
763 * Functions that edit the new xlate configuration are
764 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
770 * edit_xlate_configuration();
772 * xlate_txn_commit(); */
774 xlate_txn_commit(void)
776 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
778 ovsrcu_set(&xcfgp, new_xcfg);
779 ovsrcu_synchronize();
780 xlate_xcfg_free(xcfg);
784 /* Copies the current xlate configuration in xcfgp to new_xcfg.
786 * This needs to be called prior to editing the xlate configuration. */
788 xlate_txn_start(void)
790 struct xbridge *xbridge;
791 struct xlate_cfg *xcfg;
793 ovs_assert(!new_xcfg);
795 new_xcfg = xmalloc(sizeof *new_xcfg);
796 hmap_init(&new_xcfg->xbridges);
797 hmap_init(&new_xcfg->xbundles);
798 hmap_init(&new_xcfg->xports);
800 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
805 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
806 xlate_xbridge_copy(xbridge);
812 xlate_xcfg_free(struct xlate_cfg *xcfg)
814 struct xbridge *xbridge, *next_xbridge;
820 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
821 xlate_xbridge_remove(xcfg, xbridge);
824 hmap_destroy(&xcfg->xbridges);
825 hmap_destroy(&xcfg->xbundles);
826 hmap_destroy(&xcfg->xports);
831 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
833 const struct mac_learning *ml, struct stp *stp,
834 struct rstp *rstp, const struct mcast_snooping *ms,
835 const struct mbridge *mbridge,
836 const struct dpif_sflow *sflow,
837 const struct dpif_ipfix *ipfix,
838 const struct netflow *netflow,
839 bool forward_bpdu, bool has_in_band,
840 const struct dpif_backer_support *support)
842 struct xbridge *xbridge;
844 ovs_assert(new_xcfg);
846 xbridge = xbridge_lookup(new_xcfg, ofproto);
848 xbridge = xzalloc(sizeof *xbridge);
849 xbridge->ofproto = ofproto;
851 xlate_xbridge_init(new_xcfg, xbridge);
855 xbridge->name = xstrdup(name);
857 xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
858 netflow, forward_bpdu, has_in_band, support);
862 xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
864 struct xbundle *xbundle, *next_xbundle;
865 struct xport *xport, *next_xport;
871 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
872 xlate_xport_remove(xcfg, xport);
875 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
876 xlate_xbundle_remove(xcfg, xbundle);
879 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
880 mac_learning_unref(xbridge->ml);
881 mcast_snooping_unref(xbridge->ms);
882 mbridge_unref(xbridge->mbridge);
883 dpif_sflow_unref(xbridge->sflow);
884 dpif_ipfix_unref(xbridge->ipfix);
885 stp_unref(xbridge->stp);
886 rstp_unref(xbridge->rstp);
887 hmap_destroy(&xbridge->xports);
893 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
895 struct xbridge *xbridge;
897 ovs_assert(new_xcfg);
899 xbridge = xbridge_lookup(new_xcfg, ofproto);
900 xlate_xbridge_remove(new_xcfg, xbridge);
904 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
905 const char *name, enum port_vlan_mode vlan_mode, int vlan,
906 unsigned long *trunks, bool use_priority_tags,
907 const struct bond *bond, const struct lacp *lacp,
910 struct xbundle *xbundle;
912 ovs_assert(new_xcfg);
914 xbundle = xbundle_lookup(new_xcfg, ofbundle);
916 xbundle = xzalloc(sizeof *xbundle);
917 xbundle->ofbundle = ofbundle;
918 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
920 xlate_xbundle_init(new_xcfg, xbundle);
924 xbundle->name = xstrdup(name);
926 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
927 use_priority_tags, bond, lacp, floodable);
931 xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
939 LIST_FOR_EACH_POP (xport, bundle_node, &xbundle->xports) {
940 xport->xbundle = NULL;
943 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
944 list_remove(&xbundle->list_node);
945 bond_unref(xbundle->bond);
946 lacp_unref(xbundle->lacp);
952 xlate_bundle_remove(struct ofbundle *ofbundle)
954 struct xbundle *xbundle;
956 ovs_assert(new_xcfg);
958 xbundle = xbundle_lookup(new_xcfg, ofbundle);
959 xlate_xbundle_remove(new_xcfg, xbundle);
963 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
964 struct ofport_dpif *ofport, ofp_port_t ofp_port,
965 odp_port_t odp_port, const struct netdev *netdev,
966 const struct cfm *cfm, const struct bfd *bfd,
967 const struct lldp *lldp, struct ofport_dpif *peer,
968 int stp_port_no, const struct rstp_port *rstp_port,
969 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
970 enum ofputil_port_config config,
971 enum ofputil_port_state state, bool is_tunnel,
977 ovs_assert(new_xcfg);
979 xport = xport_lookup(new_xcfg, ofport);
981 xport = xzalloc(sizeof *xport);
982 xport->ofport = ofport;
983 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
984 xport->ofp_port = ofp_port;
986 xlate_xport_init(new_xcfg, xport);
989 ovs_assert(xport->ofp_port == ofp_port);
991 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, lldp,
992 stp_port_no, rstp_port, config, state, is_tunnel,
996 xport->peer->peer = NULL;
998 xport->peer = xport_lookup(new_xcfg, peer);
1000 xport->peer->peer = xport;
1003 if (xport->xbundle) {
1004 list_remove(&xport->bundle_node);
1006 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
1007 if (xport->xbundle) {
1008 list_insert(&xport->xbundle->xports, &xport->bundle_node);
1011 clear_skb_priorities(xport);
1012 for (i = 0; i < n_qdscp; i++) {
1013 struct skb_priority_to_dscp *pdscp;
1014 uint32_t skb_priority;
1016 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
1021 pdscp = xmalloc(sizeof *pdscp);
1022 pdscp->skb_priority = skb_priority;
1023 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1024 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
1025 hash_int(pdscp->skb_priority, 0));
1030 xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
1037 xport->peer->peer = NULL;
1041 if (xport->xbundle) {
1042 list_remove(&xport->bundle_node);
1045 clear_skb_priorities(xport);
1046 hmap_destroy(&xport->skb_priorities);
1048 hmap_remove(&xcfg->xports, &xport->hmap_node);
1049 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
1051 netdev_close(xport->netdev);
1052 rstp_port_unref(xport->rstp_port);
1053 cfm_unref(xport->cfm);
1054 bfd_unref(xport->bfd);
1055 lldp_unref(xport->lldp);
1060 xlate_ofport_remove(struct ofport_dpif *ofport)
1062 struct xport *xport;
1064 ovs_assert(new_xcfg);
1066 xport = xport_lookup(new_xcfg, ofport);
1067 xlate_xport_remove(new_xcfg, xport);
1070 static struct ofproto_dpif *
1071 xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
1072 ofp_port_t *ofp_in_port, const struct xport **xportp)
1074 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1075 const struct xport *xport;
1077 xport = xport_lookup(xcfg, tnl_port_should_receive(flow)
1078 ? tnl_port_receive(flow)
1079 : odp_port_to_ofport(backer, flow->in_port.odp_port));
1080 if (OVS_UNLIKELY(!xport)) {
1085 *ofp_in_port = xport->ofp_port;
1087 return xport->xbridge->ofproto;
1090 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1091 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1092 struct ofproto_dpif *
1093 xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1094 ofp_port_t *ofp_in_port)
1096 const struct xport *xport;
1098 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1101 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1102 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
1103 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1104 * handles for those protocols if they're enabled. Caller may use the returned
1105 * pointers until quiescing, for longer term use additional references must
1108 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1111 xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1112 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1113 struct dpif_sflow **sflow, struct netflow **netflow,
1114 ofp_port_t *ofp_in_port)
1116 struct ofproto_dpif *ofproto;
1117 const struct xport *xport;
1119 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1126 *ofprotop = ofproto;
1130 *ipfix = xport ? xport->xbridge->ipfix : NULL;
1134 *sflow = xport ? xport->xbridge->sflow : NULL;
1138 *netflow = xport ? xport->xbridge->netflow : NULL;
1144 static struct xbridge *
1145 xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
1147 struct hmap *xbridges;
1148 struct xbridge *xbridge;
1150 if (!ofproto || !xcfg) {
1154 xbridges = &xcfg->xbridges;
1156 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
1158 if (xbridge->ofproto == ofproto) {
1165 static struct xbundle *
1166 xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
1168 struct hmap *xbundles;
1169 struct xbundle *xbundle;
1171 if (!ofbundle || !xcfg) {
1175 xbundles = &xcfg->xbundles;
1177 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
1179 if (xbundle->ofbundle == ofbundle) {
1186 static struct xport *
1187 xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
1189 struct hmap *xports;
1190 struct xport *xport;
1192 if (!ofport || !xcfg) {
1196 xports = &xcfg->xports;
1198 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
1200 if (xport->ofport == ofport) {
1207 static struct stp_port *
1208 xport_get_stp_port(const struct xport *xport)
1210 return xport->xbridge->stp && xport->stp_port_no != -1
1211 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1216 xport_stp_learn_state(const struct xport *xport)
1218 struct stp_port *sp = xport_get_stp_port(xport);
1220 ? stp_learn_in_state(stp_port_get_state(sp))
1225 xport_stp_forward_state(const struct xport *xport)
1227 struct stp_port *sp = xport_get_stp_port(xport);
1229 ? stp_forward_in_state(stp_port_get_state(sp))
1234 xport_stp_should_forward_bpdu(const struct xport *xport)
1236 struct stp_port *sp = xport_get_stp_port(xport);
1237 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
1240 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1241 * were used to make the determination.*/
1243 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1245 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1246 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1247 return is_stp(flow);
1251 stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1253 struct stp_port *sp = xport_get_stp_port(xport);
1254 struct dp_packet payload = *packet;
1255 struct eth_header *eth = dp_packet_data(&payload);
1257 /* Sink packets on ports that have STP disabled when the bridge has
1259 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1263 /* Trim off padding on payload. */
1264 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1265 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1268 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1269 stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
1273 static enum rstp_state
1274 xport_get_rstp_port_state(const struct xport *xport)
1276 return xport->rstp_port
1277 ? rstp_port_get_state(xport->rstp_port)
1282 xport_rstp_learn_state(const struct xport *xport)
1284 return xport->xbridge->rstp && xport->rstp_port
1285 ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1290 xport_rstp_forward_state(const struct xport *xport)
1292 return xport->xbridge->rstp && xport->rstp_port
1293 ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1298 xport_rstp_should_manage_bpdu(const struct xport *xport)
1300 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
1304 rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1306 struct dp_packet payload = *packet;
1307 struct eth_header *eth = dp_packet_data(&payload);
1309 /* Sink packets on ports that have no RSTP. */
1310 if (!xport->rstp_port) {
1314 /* Trim off padding on payload. */
1315 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1316 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1319 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1320 rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
1321 dp_packet_size(&payload));
1325 static struct xport *
1326 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1328 struct xport *xport;
1330 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1332 if (xport->ofp_port == ofp_port) {
1340 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1342 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1343 return xport ? xport->odp_port : ODPP_NONE;
1347 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1349 struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1350 return xport && xport->may_enable;
1353 static struct ofputil_bucket *
1354 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1358 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1360 struct group_dpif *group;
1362 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1363 struct ofputil_bucket *bucket;
1365 bucket = group_first_live_bucket(ctx, group, depth);
1366 group_dpif_unref(group);
1367 return bucket == NULL;
1373 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1376 bucket_is_alive(const struct xlate_ctx *ctx,
1377 struct ofputil_bucket *bucket, int depth)
1379 if (depth >= MAX_LIVENESS_RECURSION) {
1380 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1382 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1383 MAX_LIVENESS_RECURSION);
1387 return (!ofputil_bucket_has_liveness(bucket)
1388 || (bucket->watch_port != OFPP_ANY
1389 && odp_port_is_alive(ctx, bucket->watch_port))
1390 || (bucket->watch_group != OFPG_ANY
1391 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
1394 static struct ofputil_bucket *
1395 group_first_live_bucket(const struct xlate_ctx *ctx,
1396 const struct group_dpif *group, int depth)
1398 struct ofputil_bucket *bucket;
1399 const struct ovs_list *buckets;
1401 group_dpif_get_buckets(group, &buckets);
1402 LIST_FOR_EACH (bucket, list_node, buckets) {
1403 if (bucket_is_alive(ctx, bucket, depth)) {
1411 static struct ofputil_bucket *
1412 group_best_live_bucket(const struct xlate_ctx *ctx,
1413 const struct group_dpif *group,
1416 struct ofputil_bucket *best_bucket = NULL;
1417 uint32_t best_score = 0;
1420 struct ofputil_bucket *bucket;
1421 const struct ovs_list *buckets;
1423 group_dpif_get_buckets(group, &buckets);
1424 LIST_FOR_EACH (bucket, list_node, buckets) {
1425 if (bucket_is_alive(ctx, bucket, 0)) {
1426 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
1427 if (score >= best_score) {
1428 best_bucket = bucket;
1439 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
1441 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1442 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1446 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1448 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1451 static mirror_mask_t
1452 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1454 return xbundle != &ofpp_none_bundle
1455 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1459 static mirror_mask_t
1460 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
1462 return xbundle != &ofpp_none_bundle
1463 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1467 static mirror_mask_t
1468 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
1470 return xbundle != &ofpp_none_bundle
1471 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1475 static struct xbundle *
1476 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1477 bool warn, struct xport **in_xportp)
1479 struct xport *xport;
1481 /* Find the port and bundle for the received packet. */
1482 xport = get_ofp_port(xbridge, in_port);
1486 if (xport && xport->xbundle) {
1487 return xport->xbundle;
1490 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1491 * which a controller may use as the ingress port for traffic that
1492 * it is sourcing. */
1493 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
1494 return &ofpp_none_bundle;
1497 /* Odd. A few possible reasons here:
1499 * - We deleted a port but there are still a few packets queued up
1502 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1503 * we don't know about.
1505 * - The ofproto client didn't configure the port as part of a bundle.
1506 * This is particularly likely to happen if a packet was received on the
1507 * port after it was created, but before the client had a chance to
1508 * configure its bundle.
1511 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1513 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1514 "port %"PRIu16, xbridge->name, in_port);
1520 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
1522 const struct xbridge *xbridge = ctx->xbridge;
1523 mirror_mask_t mirrors;
1524 struct xbundle *in_xbundle;
1528 mirrors = ctx->xout->mirrors;
1529 ctx->xout->mirrors = 0;
1531 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
1532 ctx->xin->packet != NULL, NULL);
1536 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
1538 /* Drop frames on bundles reserved for mirroring. */
1539 if (xbundle_mirror_out(xbridge, in_xbundle)) {
1540 if (ctx->xin->packet != NULL) {
1541 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1542 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1543 "%s, which is reserved exclusively for mirroring",
1544 ctx->xbridge->name, in_xbundle->name);
1546 ofpbuf_clear(ctx->xout->odp_actions);
1551 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
1552 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1555 vlan = input_vid_to_vlan(in_xbundle, vid);
1561 /* Restore the original packet before adding the mirror actions. */
1562 ctx->xin->flow = *orig_flow;
1565 mirror_mask_t dup_mirrors;
1566 struct ofbundle *out;
1567 unsigned long *vlans;
1572 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1573 &vlans, &dup_mirrors, &out, &out_vlan);
1574 ovs_assert(has_mirror);
1577 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1579 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
1582 if (!vlan_mirrored) {
1583 mirrors = zero_rightmost_1bit(mirrors);
1587 mirrors &= ~dup_mirrors;
1588 ctx->xout->mirrors |= dup_mirrors;
1590 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1591 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1593 output_normal(ctx, out_xbundle, vlan);
1595 } else if (vlan != out_vlan
1596 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
1597 struct xbundle *xbundle;
1599 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1600 if (xbundle_includes_vlan(xbundle, out_vlan)
1601 && !xbundle_mirror_out(xbridge, xbundle)) {
1602 output_normal(ctx, xbundle, out_vlan);
1609 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1610 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1611 * the bundle on which the packet was received, returns the VLAN to which the
1614 * Both 'vid' and the return value are in the range 0...4095. */
1616 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1618 switch (in_xbundle->vlan_mode) {
1619 case PORT_VLAN_ACCESS:
1620 return in_xbundle->vlan;
1623 case PORT_VLAN_TRUNK:
1626 case PORT_VLAN_NATIVE_UNTAGGED:
1627 case PORT_VLAN_NATIVE_TAGGED:
1628 return vid ? vid : in_xbundle->vlan;
1635 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1636 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1639 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1640 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1643 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1645 /* Allow any VID on the OFPP_NONE port. */
1646 if (in_xbundle == &ofpp_none_bundle) {
1650 switch (in_xbundle->vlan_mode) {
1651 case PORT_VLAN_ACCESS:
1654 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1655 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1656 "packet received on port %s configured as VLAN "
1657 "%"PRIu16" access port", vid, in_xbundle->name,
1664 case PORT_VLAN_NATIVE_UNTAGGED:
1665 case PORT_VLAN_NATIVE_TAGGED:
1667 /* Port must always carry its native VLAN. */
1671 case PORT_VLAN_TRUNK:
1672 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1674 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1675 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1676 "received on port %s not configured for trunking "
1677 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1689 /* Given 'vlan', the VLAN that a packet belongs to, and
1690 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1691 * that should be included in the 802.1Q header. (If the return value is 0,
1692 * then the 802.1Q header should only be included in the packet if there is a
1695 * Both 'vlan' and the return value are in the range 0...4095. */
1697 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1699 switch (out_xbundle->vlan_mode) {
1700 case PORT_VLAN_ACCESS:
1703 case PORT_VLAN_TRUNK:
1704 case PORT_VLAN_NATIVE_TAGGED:
1707 case PORT_VLAN_NATIVE_UNTAGGED:
1708 return vlan == out_xbundle->vlan ? 0 : vlan;
1716 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1719 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1721 ovs_be16 tci, old_tci;
1722 struct xport *xport;
1723 struct xlate_bond_recirc xr;
1724 bool use_recirc = false;
1726 vid = output_vlan_to_vid(out_xbundle, vlan);
1727 if (list_is_empty(&out_xbundle->xports)) {
1728 /* Partially configured bundle with no slaves. Drop the packet. */
1730 } else if (!out_xbundle->bond) {
1731 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1734 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1735 struct flow_wildcards *wc = &ctx->xout->wc;
1736 struct ofport_dpif *ofport;
1738 if (ctx->xbridge->support.recirc) {
1739 use_recirc = bond_may_recirc(
1740 out_xbundle->bond, &xr.recirc_id, &xr.hash_basis);
1743 /* Only TCP mode uses recirculation. */
1744 xr.hash_alg = OVS_HASH_ALG_L4;
1745 bond_update_post_recirc_rules(out_xbundle->bond, false);
1747 /* Recirculation does not require unmasking hash fields. */
1752 ofport = bond_choose_output_slave(out_xbundle->bond,
1753 &ctx->xin->flow, wc, vid);
1754 xport = xport_lookup(xcfg, ofport);
1757 /* No slaves enabled, so drop packet. */
1761 /* If use_recirc is set, the main thread will handle stats
1762 * accounting for this bond. */
1764 if (ctx->xin->resubmit_stats) {
1765 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1766 ctx->xin->resubmit_stats->n_bytes);
1768 if (ctx->xin->xcache) {
1769 struct xc_entry *entry;
1772 flow = &ctx->xin->flow;
1773 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1774 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1775 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1776 entry->u.bond.vid = vid;
1781 old_tci = *flow_tci;
1783 if (tci || out_xbundle->use_priority_tags) {
1784 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1786 tci |= htons(VLAN_CFI);
1791 compose_output_action(ctx, xport->ofp_port, use_recirc ? &xr : NULL);
1792 *flow_tci = old_tci;
1795 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1796 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1797 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1799 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1801 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1805 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1806 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1810 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1811 if (flow->nw_proto == ARP_OP_REPLY) {
1813 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1814 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1815 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1817 return flow->nw_src == flow->nw_dst;
1823 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1824 * dropped. Returns true if they may be forwarded, false if they should be
1827 * 'in_port' must be the xport that corresponds to flow->in_port.
1828 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1830 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1831 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1832 * checked by input_vid_is_valid().
1834 * May also add tags to '*tags', although the current implementation only does
1835 * so in one special case.
1838 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1841 struct xbundle *in_xbundle = in_port->xbundle;
1842 const struct xbridge *xbridge = ctx->xbridge;
1843 struct flow *flow = &ctx->xin->flow;
1845 /* Drop frames for reserved multicast addresses
1846 * only if forward_bpdu option is absent. */
1847 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1848 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1852 if (in_xbundle->bond) {
1853 struct mac_entry *mac;
1855 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1861 xlate_report(ctx, "bonding refused admissibility, dropping");
1864 case BV_DROP_IF_MOVED:
1865 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1866 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1868 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
1869 && (!is_gratuitous_arp(flow, &ctx->xout->wc)
1870 || mac_entry_is_grat_arp_locked(mac))) {
1871 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1872 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1876 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1884 /* Checks whether a MAC learning update is necessary for MAC learning table
1885 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1888 * Most packets processed through the MAC learning table do not actually
1889 * change it in any way. This function requires only a read lock on the MAC
1890 * learning table, so it is much cheaper in this common case.
1892 * Keep the code here synchronized with that in update_learning_table__()
1895 is_mac_learning_update_needed(const struct mac_learning *ml,
1896 const struct flow *flow,
1897 struct flow_wildcards *wc,
1898 int vlan, struct xbundle *in_xbundle)
1899 OVS_REQ_RDLOCK(ml->rwlock)
1901 struct mac_entry *mac;
1903 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1907 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1908 if (!mac || mac_entry_age(ml, mac)) {
1912 if (is_gratuitous_arp(flow, wc)) {
1913 /* We don't want to learn from gratuitous ARP packets that are
1914 * reflected back over bond slaves so we lock the learning table. */
1915 if (!in_xbundle->bond) {
1917 } else if (mac_entry_is_grat_arp_locked(mac)) {
1922 return mac_entry_get_port(ml, mac) != in_xbundle->ofbundle;
1926 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1927 * received on 'in_xbundle' in 'vlan'.
1929 * This code repeats all the checks in is_mac_learning_update_needed() because
1930 * the lock was released between there and here and thus the MAC learning state
1931 * could have changed.
1933 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1936 update_learning_table__(const struct xbridge *xbridge,
1937 const struct flow *flow, struct flow_wildcards *wc,
1938 int vlan, struct xbundle *in_xbundle)
1939 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1941 struct mac_entry *mac;
1943 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1947 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1948 if (is_gratuitous_arp(flow, wc)) {
1949 /* We don't want to learn from gratuitous ARP packets that are
1950 * reflected back over bond slaves so we lock the learning table. */
1951 if (!in_xbundle->bond) {
1952 mac_entry_set_grat_arp_lock(mac);
1953 } else if (mac_entry_is_grat_arp_locked(mac)) {
1958 if (mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle) {
1959 /* The log messages here could actually be useful in debugging,
1960 * so keep the rate limit relatively high. */
1961 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1963 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1964 "on port %s in VLAN %d",
1965 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1966 in_xbundle->name, vlan);
1968 mac_entry_set_port(xbridge->ml, mac, in_xbundle->ofbundle);
1973 update_learning_table(const struct xbridge *xbridge,
1974 const struct flow *flow, struct flow_wildcards *wc,
1975 int vlan, struct xbundle *in_xbundle)
1979 /* Don't learn the OFPP_NONE port. */
1980 if (in_xbundle == &ofpp_none_bundle) {
1984 /* First try the common case: no change to MAC learning table. */
1985 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1986 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1988 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1991 /* Slow path: MAC learning table might need an update. */
1992 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1993 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1994 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1998 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1999 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
2001 update_mcast_snooping_table__(const struct xbridge *xbridge,
2002 const struct flow *flow,
2003 struct mcast_snooping *ms,
2004 ovs_be32 ip4, int vlan,
2005 struct xbundle *in_xbundle,
2006 const struct dp_packet *packet)
2007 OVS_REQ_WRLOCK(ms->rwlock)
2009 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2012 switch (ntohs(flow->tp_src)) {
2013 case IGMP_HOST_MEMBERSHIP_REPORT:
2014 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2015 if (mcast_snooping_add_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
2016 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
2017 IP_FMT" is on port %s in VLAN %d",
2018 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2021 case IGMP_HOST_LEAVE_MESSAGE:
2022 if (mcast_snooping_leave_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
2023 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
2024 IP_FMT" is on port %s in VLAN %d",
2025 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2028 case IGMP_HOST_MEMBERSHIP_QUERY:
2029 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
2030 in_xbundle->ofbundle)) {
2031 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
2032 IP_FMT" is on port %s in VLAN %d",
2033 xbridge->name, IP_ARGS(flow->nw_src),
2034 in_xbundle->name, vlan);
2037 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2038 if ((count = mcast_snooping_add_report(ms, packet, vlan,
2039 in_xbundle->ofbundle))) {
2040 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2041 "addresses on port %s in VLAN %d",
2042 xbridge->name, count, in_xbundle->name, vlan);
2048 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2049 * was received on 'in_xbundle' in 'vlan'. */
2051 update_mcast_snooping_table(const struct xbridge *xbridge,
2052 const struct flow *flow, int vlan,
2053 struct xbundle *in_xbundle,
2054 const struct dp_packet *packet)
2056 struct mcast_snooping *ms = xbridge->ms;
2057 struct xlate_cfg *xcfg;
2058 struct xbundle *mcast_xbundle;
2059 struct mcast_port_bundle *fport;
2061 /* Don't learn the OFPP_NONE port. */
2062 if (in_xbundle == &ofpp_none_bundle) {
2066 /* Don't learn from flood ports */
2067 mcast_xbundle = NULL;
2068 ovs_rwlock_wrlock(&ms->rwlock);
2069 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2070 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2071 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2072 if (mcast_xbundle == in_xbundle) {
2077 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
2078 update_mcast_snooping_table__(xbridge, flow, ms, flow->igmp_group_ip4,
2079 vlan, in_xbundle, packet);
2081 ovs_rwlock_unlock(&ms->rwlock);
2084 /* send the packet to ports having the multicast group learned */
2086 xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2087 struct mcast_snooping *ms OVS_UNUSED,
2088 struct mcast_group *grp,
2089 struct xbundle *in_xbundle, uint16_t vlan)
2090 OVS_REQ_RDLOCK(ms->rwlock)
2092 struct xlate_cfg *xcfg;
2093 struct mcast_group_bundle *b;
2094 struct xbundle *mcast_xbundle;
2096 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2097 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2098 mcast_xbundle = xbundle_lookup(xcfg, b->port);
2099 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2100 xlate_report(ctx, "forwarding to mcast group port");
2101 output_normal(ctx, mcast_xbundle, vlan);
2102 } else if (!mcast_xbundle) {
2103 xlate_report(ctx, "mcast group port is unknown, dropping");
2105 xlate_report(ctx, "mcast group port is input port, dropping");
2110 /* send the packet to ports connected to multicast routers */
2112 xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2113 struct mcast_snooping *ms,
2114 struct xbundle *in_xbundle, uint16_t vlan)
2115 OVS_REQ_RDLOCK(ms->rwlock)
2117 struct xlate_cfg *xcfg;
2118 struct mcast_mrouter_bundle *mrouter;
2119 struct xbundle *mcast_xbundle;
2121 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2122 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2123 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2124 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2125 xlate_report(ctx, "forwarding to mcast router port");
2126 output_normal(ctx, mcast_xbundle, vlan);
2127 } else if (!mcast_xbundle) {
2128 xlate_report(ctx, "mcast router port is unknown, dropping");
2130 xlate_report(ctx, "mcast router port is input port, dropping");
2135 /* send the packet to ports flagged to be flooded */
2137 xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2138 struct mcast_snooping *ms,
2139 struct xbundle *in_xbundle, uint16_t vlan)
2140 OVS_REQ_RDLOCK(ms->rwlock)
2142 struct xlate_cfg *xcfg;
2143 struct mcast_port_bundle *fport;
2144 struct xbundle *mcast_xbundle;
2146 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2147 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2148 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2149 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2150 xlate_report(ctx, "forwarding to mcast flood port");
2151 output_normal(ctx, mcast_xbundle, vlan);
2152 } else if (!mcast_xbundle) {
2153 xlate_report(ctx, "mcast flood port is unknown, dropping");
2155 xlate_report(ctx, "mcast flood port is input port, dropping");
2160 /* forward the Reports to configured ports */
2162 xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
2163 struct mcast_snooping *ms,
2164 struct xbundle *in_xbundle, uint16_t vlan)
2165 OVS_REQ_RDLOCK(ms->rwlock)
2167 struct xlate_cfg *xcfg;
2168 struct mcast_port_bundle *rport;
2169 struct xbundle *mcast_xbundle;
2171 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2172 LIST_FOR_EACH(rport, node, &ms->rport_list) {
2173 mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2174 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2175 xlate_report(ctx, "forwarding Report to mcast flagged port");
2176 output_normal(ctx, mcast_xbundle, vlan);
2177 } else if (!mcast_xbundle) {
2178 xlate_report(ctx, "mcast port is unknown, dropping the Report");
2180 xlate_report(ctx, "mcast port is input port, dropping the Report");
2186 xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2189 struct xbundle *xbundle;
2191 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2192 if (xbundle != in_xbundle
2193 && xbundle_includes_vlan(xbundle, vlan)
2194 && xbundle->floodable
2195 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2196 output_normal(ctx, xbundle, vlan);
2199 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2203 xlate_normal(struct xlate_ctx *ctx)
2205 struct flow_wildcards *wc = &ctx->xout->wc;
2206 struct flow *flow = &ctx->xin->flow;
2207 struct xbundle *in_xbundle;
2208 struct xport *in_port;
2209 struct mac_entry *mac;
2214 ctx->xout->has_normal = true;
2216 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2217 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2218 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2220 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
2221 ctx->xin->packet != NULL, &in_port);
2223 xlate_report(ctx, "no input bundle, dropping");
2227 /* Drop malformed frames. */
2228 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
2229 !(flow->vlan_tci & htons(VLAN_CFI))) {
2230 if (ctx->xin->packet != NULL) {
2231 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2232 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
2233 "VLAN tag received on port %s",
2234 ctx->xbridge->name, in_xbundle->name);
2236 xlate_report(ctx, "partial VLAN tag, dropping");
2240 /* Drop frames on bundles reserved for mirroring. */
2241 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
2242 if (ctx->xin->packet != NULL) {
2243 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2244 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2245 "%s, which is reserved exclusively for mirroring",
2246 ctx->xbridge->name, in_xbundle->name);
2248 xlate_report(ctx, "input port is mirror output port, dropping");
2253 vid = vlan_tci_to_vid(flow->vlan_tci);
2254 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
2255 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2258 vlan = input_vid_to_vlan(in_xbundle, vid);
2260 /* Check other admissibility requirements. */
2261 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2265 /* Learn source MAC. */
2266 if (ctx->xin->may_learn) {
2267 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
2269 if (ctx->xin->xcache) {
2270 struct xc_entry *entry;
2272 /* Save enough info to update mac learning table later. */
2273 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
2274 entry->u.normal.ofproto = ctx->xbridge->ofproto;
2275 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2276 entry->u.normal.vlan = vlan;
2279 /* Determine output bundle. */
2280 if (mcast_snooping_enabled(ctx->xbridge->ms)
2281 && !eth_addr_is_broadcast(flow->dl_dst)
2282 && eth_addr_is_multicast(flow->dl_dst)
2283 && flow->dl_type == htons(ETH_TYPE_IP)) {
2284 struct mcast_snooping *ms = ctx->xbridge->ms;
2285 struct mcast_group *grp;
2287 if (flow->nw_proto == IPPROTO_IGMP) {
2288 if (mcast_snooping_is_membership(flow->tp_src) ||
2289 mcast_snooping_is_query(flow->tp_src)) {
2290 if (ctx->xin->may_learn) {
2291 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2292 in_xbundle, ctx->xin->packet);
2295 * IGMP packets need to take the slow path, in order to be
2296 * processed for mdb updates. That will prevent expires
2297 * firing off even after hosts have sent reports.
2299 ctx->xout->slow |= SLOW_ACTION;
2302 if (mcast_snooping_is_membership(flow->tp_src)) {
2303 ovs_rwlock_rdlock(&ms->rwlock);
2304 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2305 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2306 * forward IGMP Membership Reports only to those ports where
2307 * multicast routers are attached. Alternatively stated: a
2308 * snooping switch should not forward IGMP Membership Reports
2309 * to ports on which only hosts are attached.
2310 * An administrative control may be provided to override this
2311 * restriction, allowing the report messages to be flooded to
2313 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2314 ovs_rwlock_unlock(&ms->rwlock);
2316 xlate_report(ctx, "multicast traffic, flooding");
2317 xlate_normal_flood(ctx, in_xbundle, vlan);
2321 if (ip_is_local_multicast(flow->nw_dst)) {
2322 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2323 * address in the 224.0.0.x range which are not IGMP must
2324 * be forwarded on all ports */
2325 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2326 xlate_normal_flood(ctx, in_xbundle, vlan);
2331 /* forwarding to group base ports */
2332 ovs_rwlock_rdlock(&ms->rwlock);
2333 grp = mcast_snooping_lookup(ms, flow->nw_dst, vlan);
2335 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2336 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2337 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2339 if (mcast_snooping_flood_unreg(ms)) {
2340 xlate_report(ctx, "unregistered multicast, flooding");
2341 xlate_normal_flood(ctx, in_xbundle, vlan);
2343 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2344 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2347 ovs_rwlock_unlock(&ms->rwlock);
2349 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2350 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
2351 mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
2352 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2355 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2356 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2357 if (mac_xbundle && mac_xbundle != in_xbundle) {
2358 xlate_report(ctx, "forwarding to learned port");
2359 output_normal(ctx, mac_xbundle, vlan);
2360 } else if (!mac_xbundle) {
2361 xlate_report(ctx, "learned port is unknown, dropping");
2363 xlate_report(ctx, "learned port is input port, dropping");
2366 xlate_report(ctx, "no learned MAC for destination, flooding");
2367 xlate_normal_flood(ctx, in_xbundle, vlan);
2372 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
2373 * the number of packets out of UINT32_MAX to sample. The given
2374 * cookie is passed back in the callback for each sampled packet.
2377 compose_sample_action(const struct xbridge *xbridge,
2378 struct ofpbuf *odp_actions,
2379 const struct flow *flow,
2380 const uint32_t probability,
2381 const union user_action_cookie *cookie,
2382 const size_t cookie_size,
2383 const odp_port_t tunnel_out_port)
2385 size_t sample_offset, actions_offset;
2386 odp_port_t odp_port;
2390 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
2392 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
2394 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
2396 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
2397 pid = dpif_port_get_pid(xbridge->dpif, odp_port,
2398 flow_hash_5tuple(flow, 0));
2399 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2400 tunnel_out_port, odp_actions);
2402 nl_msg_end_nested(odp_actions, actions_offset);
2403 nl_msg_end_nested(odp_actions, sample_offset);
2404 return cookie_offset;
2408 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
2409 odp_port_t odp_port, unsigned int n_outputs,
2410 union user_action_cookie *cookie)
2414 cookie->type = USER_ACTION_COOKIE_SFLOW;
2415 cookie->sflow.vlan_tci = vlan_tci;
2417 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2418 * port information") for the interpretation of cookie->output. */
2419 switch (n_outputs) {
2421 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2422 cookie->sflow.output = 0x40000000 | 256;
2426 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
2428 cookie->sflow.output = ifindex;
2433 /* 0x80000000 means "multiple output ports. */
2434 cookie->sflow.output = 0x80000000 | n_outputs;
2439 /* Compose SAMPLE action for sFlow bridge sampling. */
2441 compose_sflow_action(const struct xbridge *xbridge,
2442 struct ofpbuf *odp_actions,
2443 const struct flow *flow,
2444 odp_port_t odp_port)
2446 uint32_t probability;
2447 union user_action_cookie cookie;
2449 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
2453 probability = dpif_sflow_get_probability(xbridge->sflow);
2454 compose_sflow_cookie(xbridge, htons(0), odp_port,
2455 odp_port == ODPP_NONE ? 0 : 1, &cookie);
2457 return compose_sample_action(xbridge, odp_actions, flow, probability,
2458 &cookie, sizeof cookie.sflow, ODPP_NONE);
2462 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
2463 uint32_t obs_domain_id, uint32_t obs_point_id,
2464 union user_action_cookie *cookie)
2466 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
2467 cookie->flow_sample.probability = probability;
2468 cookie->flow_sample.collector_set_id = collector_set_id;
2469 cookie->flow_sample.obs_domain_id = obs_domain_id;
2470 cookie->flow_sample.obs_point_id = obs_point_id;
2474 compose_ipfix_cookie(union user_action_cookie *cookie,
2475 odp_port_t output_odp_port)
2477 cookie->type = USER_ACTION_COOKIE_IPFIX;
2478 cookie->ipfix.output_odp_port = output_odp_port;
2481 /* Compose SAMPLE action for IPFIX bridge sampling. */
2483 compose_ipfix_action(const struct xbridge *xbridge,
2484 struct ofpbuf *odp_actions,
2485 const struct flow *flow,
2486 odp_port_t output_odp_port)
2488 uint32_t probability;
2489 union user_action_cookie cookie;
2490 odp_port_t tunnel_out_port = ODPP_NONE;
2492 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
2496 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2498 if (output_odp_port == ODPP_NONE &&
2499 !dpif_ipfix_get_bridge_exporter_input_sampling(xbridge->ipfix)) {
2503 /* For output case, output_odp_port is valid*/
2504 if (output_odp_port != ODPP_NONE) {
2505 if (!dpif_ipfix_get_bridge_exporter_output_sampling(xbridge->ipfix)) {
2508 /* If tunnel sampling is enabled, put an additional option attribute:
2509 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2511 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(xbridge->ipfix) &&
2512 dpif_ipfix_get_tunnel_port(xbridge->ipfix, output_odp_port) ) {
2513 tunnel_out_port = output_odp_port;
2517 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
2518 compose_ipfix_cookie(&cookie, output_odp_port);
2520 compose_sample_action(xbridge, odp_actions, flow, probability,
2521 &cookie, sizeof cookie.ipfix, tunnel_out_port);
2524 /* SAMPLE action for sFlow must be first action in any given list of
2525 * actions. At this point we do not have all information required to
2526 * build it. So try to build sample action as complete as possible. */
2528 add_sflow_action(struct xlate_ctx *ctx)
2530 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
2531 ctx->xout->odp_actions,
2532 &ctx->xin->flow, ODPP_NONE);
2533 ctx->sflow_odp_port = 0;
2534 ctx->sflow_n_outputs = 0;
2537 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
2538 * of actions, eventually after the SAMPLE action for sFlow. */
2540 add_ipfix_action(struct xlate_ctx *ctx)
2542 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2543 &ctx->xin->flow, ODPP_NONE);
2547 add_ipfix_output_action(struct xlate_ctx *ctx, odp_port_t port)
2549 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2550 &ctx->xin->flow, port);
2553 /* Fix SAMPLE action according to data collected while composing ODP actions.
2554 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
2555 * USERSPACE action's user-cookie which is required for sflow. */
2557 fix_sflow_action(struct xlate_ctx *ctx)
2559 const struct flow *base = &ctx->base_flow;
2560 union user_action_cookie *cookie;
2562 if (!ctx->user_cookie_offset) {
2566 cookie = ofpbuf_at(ctx->xout->odp_actions, ctx->user_cookie_offset,
2567 sizeof cookie->sflow);
2568 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2570 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
2571 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
2574 static enum slow_path_reason
2575 process_special(struct xlate_ctx *ctx, const struct flow *flow,
2576 const struct xport *xport, const struct dp_packet *packet)
2578 struct flow_wildcards *wc = &ctx->xout->wc;
2579 const struct xbridge *xbridge = ctx->xbridge;
2583 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
2585 cfm_process_heartbeat(xport->cfm, packet);
2588 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
2590 bfd_process_packet(xport->bfd, flow, packet);
2591 /* If POLL received, immediately sends FINAL back. */
2592 if (bfd_should_send_packet(xport->bfd)) {
2593 ofproto_dpif_monitor_port_send_soon(xport->ofport);
2597 } else if (xport->xbundle && xport->xbundle->lacp
2598 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2600 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
2603 } else if ((xbridge->stp || xbridge->rstp) &&
2604 stp_should_process_flow(flow, wc)) {
2607 ? stp_process_packet(xport, packet)
2608 : rstp_process_packet(xport, packet);
2611 } else if (xport->lldp && lldp_should_process_flow(xport->lldp, flow)) {
2613 lldp_process_packet(xport->lldp, packet);
2622 tnl_route_lookup_flow(const struct flow *oflow,
2623 ovs_be32 *ip, struct xport **out_port)
2625 char out_dev[IFNAMSIZ];
2626 struct xbridge *xbridge;
2627 struct xlate_cfg *xcfg;
2630 if (!ovs_router_lookup(oflow->tunnel.ip_dst, out_dev, &gw)) {
2637 *ip = oflow->tunnel.ip_dst;
2640 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2643 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
2644 if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
2647 HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
2648 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
2659 xlate_flood_packet(struct xbridge *xbridge, struct dp_packet *packet)
2661 struct ofpact_output output;
2664 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
2665 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
2666 flow_extract(packet, &flow);
2667 flow.in_port.ofp_port = OFPP_NONE;
2668 output.port = OFPP_FLOOD;
2671 return ofproto_dpif_execute_actions(xbridge->ofproto, &flow, NULL,
2672 &output.ofpact, sizeof output,
2677 tnl_send_arp_request(const struct xport *out_dev, const uint8_t eth_src[ETH_ADDR_LEN],
2678 ovs_be32 ip_src, ovs_be32 ip_dst)
2680 struct xbridge *xbridge = out_dev->xbridge;
2681 struct dp_packet packet;
2683 dp_packet_init(&packet, 0);
2684 compose_arp(&packet, ARP_OP_REQUEST,
2685 eth_src, eth_addr_zero, true, ip_src, ip_dst);
2687 xlate_flood_packet(xbridge, &packet);
2688 dp_packet_uninit(&packet);
2692 build_tunnel_send(struct xlate_ctx *ctx, const struct xport *xport,
2693 const struct flow *flow, odp_port_t tunnel_odp_port)
2695 struct ovs_action_push_tnl tnl_push_data;
2696 struct xport *out_dev = NULL;
2697 ovs_be32 s_ip, d_ip = 0;
2698 uint8_t smac[ETH_ADDR_LEN];
2699 uint8_t dmac[ETH_ADDR_LEN];
2702 err = tnl_route_lookup_flow(flow, &d_ip, &out_dev);
2704 xlate_report(ctx, "native tunnel routing failed");
2707 xlate_report(ctx, "tunneling to "IP_FMT" via %s",
2708 IP_ARGS(d_ip), netdev_get_name(out_dev->netdev));
2710 /* Use mac addr of bridge port of the peer. */
2711 err = netdev_get_etheraddr(out_dev->netdev, smac);
2713 xlate_report(ctx, "tunnel output device lacks Ethernet address");
2717 err = netdev_get_in4(out_dev->netdev, (struct in_addr *) &s_ip, NULL);
2719 xlate_report(ctx, "tunnel output device lacks IPv4 address");
2723 err = tnl_arp_lookup(out_dev->xbridge->name, d_ip, dmac);
2725 xlate_report(ctx, "ARP cache miss for "IP_FMT" on bridge %s, "
2726 "sending ARP request",
2727 IP_ARGS(d_ip), out_dev->xbridge->name);
2728 tnl_send_arp_request(out_dev, smac, s_ip, d_ip);
2731 if (ctx->xin->xcache) {
2732 struct xc_entry *entry;
2734 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_ARP);
2735 ovs_strlcpy(entry->u.tnl_arp_cache.br_name, out_dev->xbridge->name,
2736 sizeof entry->u.tnl_arp_cache.br_name);
2737 entry->u.tnl_arp_cache.d_ip = d_ip;
2740 xlate_report(ctx, "tunneling from "ETH_ADDR_FMT" "IP_FMT
2741 " to "ETH_ADDR_FMT" "IP_FMT,
2742 ETH_ADDR_ARGS(smac), IP_ARGS(s_ip),
2743 ETH_ADDR_ARGS(dmac), IP_ARGS(d_ip));
2744 err = tnl_port_build_header(xport->ofport, flow,
2745 dmac, smac, s_ip, &tnl_push_data);
2749 tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
2750 tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
2751 odp_put_tnl_push_action(ctx->xout->odp_actions, &tnl_push_data);
2756 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
2757 const struct xlate_bond_recirc *xr, bool check_stp)
2759 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
2760 struct flow_wildcards *wc = &ctx->xout->wc;
2761 struct flow *flow = &ctx->xin->flow;
2762 struct flow_tnl flow_tnl;
2763 ovs_be16 flow_vlan_tci;
2764 uint32_t flow_pkt_mark;
2765 uint8_t flow_nw_tos;
2766 odp_port_t out_port, odp_port;
2767 bool tnl_push_pop_send = false;
2770 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2771 * before traversing a patch port. */
2772 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 32);
2773 memset(&flow_tnl, 0, sizeof flow_tnl);
2776 xlate_report(ctx, "Nonexistent output port");
2778 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
2779 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2781 } else if (check_stp) {
2782 if (is_stp(&ctx->base_flow)) {
2783 if (!xport_stp_should_forward_bpdu(xport) &&
2784 !xport_rstp_should_manage_bpdu(xport)) {
2785 if (ctx->xbridge->stp != NULL) {
2786 xlate_report(ctx, "STP not in listening state, "
2787 "skipping bpdu output");
2788 } else if (ctx->xbridge->rstp != NULL) {
2789 xlate_report(ctx, "RSTP not managing BPDU in this state, "
2790 "skipping bpdu output");
2794 } else if (!xport_stp_forward_state(xport) ||
2795 !xport_rstp_forward_state(xport)) {
2796 if (ctx->xbridge->stp != NULL) {
2797 xlate_report(ctx, "STP not in forwarding state, "
2799 } else if (ctx->xbridge->rstp != NULL) {
2800 xlate_report(ctx, "RSTP not in forwarding state, "
2807 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
2808 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
2813 const struct xport *peer = xport->peer;
2814 struct flow old_flow = ctx->xin->flow;
2815 bool old_was_mpls = ctx->was_mpls;
2816 cls_version_t old_version = ctx->tables_version;
2817 enum slow_path_reason special;
2818 struct ofpbuf old_stack = ctx->stack;
2819 union mf_subvalue new_stack[1024 / sizeof(union mf_subvalue)];
2820 struct ofpbuf old_action_set = ctx->action_set;
2821 uint64_t actset_stub[1024 / 8];
2823 ofpbuf_use_stub(&ctx->stack, new_stack, sizeof new_stack);
2824 ofpbuf_use_stub(&ctx->action_set, actset_stub, sizeof actset_stub);
2825 ctx->xbridge = peer->xbridge;
2826 flow->in_port.ofp_port = peer->ofp_port;
2827 flow->metadata = htonll(0);
2828 memset(&flow->tunnel, 0, sizeof flow->tunnel);
2829 memset(flow->regs, 0, sizeof flow->regs);
2830 flow->actset_output = OFPP_UNSET;
2832 /* The bridge is now known so obtain its table version. */
2834 = ofproto_dpif_get_tables_version(ctx->xbridge->ofproto);
2836 special = process_special(ctx, &ctx->xin->flow, peer,
2839 ctx->xout->slow |= special;
2840 } else if (may_receive(peer, ctx)) {
2841 if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
2842 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2843 if (ctx->action_set.size) {
2844 /* Translate action set only if not dropping the packet and
2845 * not recirculating. */
2846 if (!exit_recirculates(ctx)) {
2847 xlate_action_set(ctx);
2850 /* Check if need to recirculate. */
2851 if (exit_recirculates(ctx)) {
2852 compose_recirculate_action(ctx);
2855 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
2856 * the learning action look at the packet, then drop it. */
2857 struct flow old_base_flow = ctx->base_flow;
2858 size_t old_size = ctx->xout->odp_actions->size;
2859 mirror_mask_t old_mirrors = ctx->xout->mirrors;
2861 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2862 ctx->xout->mirrors = old_mirrors;
2863 ctx->base_flow = old_base_flow;
2864 ctx->xout->odp_actions->size = old_size;
2866 /* Undo changes that may have been done for recirculation. */
2867 if (exit_recirculates(ctx)) {
2868 ctx->action_set.size = ctx->recirc_action_offset;
2869 ctx->recirc_action_offset = -1;
2870 ctx->last_unroll_offset = -1;
2875 ctx->xin->flow = old_flow;
2876 ctx->xbridge = xport->xbridge;
2877 ofpbuf_uninit(&ctx->action_set);
2878 ctx->action_set = old_action_set;
2879 ofpbuf_uninit(&ctx->stack);
2880 ctx->stack = old_stack;
2882 /* Restore calling bridge's lookup version. */
2883 ctx->tables_version = old_version;
2885 /* The peer bridge popping MPLS should have no effect on the original
2887 ctx->was_mpls = old_was_mpls;
2889 /* The fact that the peer bridge exits (for any reason) does not mean
2890 * that the original bridge should exit. Specifically, if the peer
2891 * bridge recirculates (which typically modifies the packet), the
2892 * original bridge must continue processing with the original, not the
2893 * recirculated packet! */
2896 if (ctx->xin->resubmit_stats) {
2897 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2898 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
2900 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
2903 if (ctx->xin->xcache) {
2904 struct xc_entry *entry;
2906 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2907 entry->u.dev.tx = netdev_ref(xport->netdev);
2908 entry->u.dev.rx = netdev_ref(peer->netdev);
2909 entry->u.dev.bfd = bfd_ref(peer->bfd);
2914 flow_vlan_tci = flow->vlan_tci;
2915 flow_pkt_mark = flow->pkt_mark;
2916 flow_nw_tos = flow->nw_tos;
2918 if (count_skb_priorities(xport)) {
2919 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
2920 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
2921 wc->masks.nw_tos |= IP_DSCP_MASK;
2922 flow->nw_tos &= ~IP_DSCP_MASK;
2923 flow->nw_tos |= dscp;
2927 if (xport->is_tunnel) {
2928 /* Save tunnel metadata so that changes made due to
2929 * the Logical (tunnel) Port are not visible for any further
2930 * matches, while explicit set actions on tunnel metadata are.
2932 flow_tnl = flow->tunnel;
2933 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
2934 if (odp_port == ODPP_NONE) {
2935 xlate_report(ctx, "Tunneling decided against output");
2936 goto out; /* restore flow_nw_tos */
2938 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
2939 xlate_report(ctx, "Not tunneling to our own address");
2940 goto out; /* restore flow_nw_tos */
2942 if (ctx->xin->resubmit_stats) {
2943 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2945 if (ctx->xin->xcache) {
2946 struct xc_entry *entry;
2948 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2949 entry->u.dev.tx = netdev_ref(xport->netdev);
2951 out_port = odp_port;
2952 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
2953 xlate_report(ctx, "output to native tunnel");
2954 tnl_push_pop_send = true;
2956 xlate_report(ctx, "output to kernel tunnel");
2957 commit_odp_tunnel_action(flow, &ctx->base_flow,
2958 ctx->xout->odp_actions);
2959 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2962 odp_port = xport->odp_port;
2963 out_port = odp_port;
2964 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
2965 ofp_port_t vlandev_port;
2967 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2968 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
2969 ofp_port, flow->vlan_tci);
2970 if (vlandev_port != ofp_port) {
2971 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
2972 flow->vlan_tci = htons(0);
2977 if (out_port != ODPP_NONE) {
2978 bool use_masked = ctx->xbridge->support.masked_set_action;
2980 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
2981 ctx->xout->odp_actions,
2985 struct ovs_action_hash *act_hash;
2988 act_hash = nl_msg_put_unspec_uninit(ctx->xout->odp_actions,
2989 OVS_ACTION_ATTR_HASH,
2991 act_hash->hash_alg = xr->hash_alg;
2992 act_hash->hash_basis = xr->hash_basis;
2994 /* Recirc action. */
2995 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC,
2999 if (tnl_push_pop_send) {
3000 build_tunnel_send(ctx, xport, flow, odp_port);
3001 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3003 odp_port_t odp_tnl_port = ODPP_NONE;
3005 /* XXX: Write better Filter for tunnel port. We can use inport
3006 * int tunnel-port flow to avoid these checks completely. */
3007 if (ofp_port == OFPP_LOCAL &&
3008 ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3010 odp_tnl_port = tnl_port_map_lookup(flow, wc);
3013 if (odp_tnl_port != ODPP_NONE) {
3014 nl_msg_put_odp_port(ctx->xout->odp_actions,
3015 OVS_ACTION_ATTR_TUNNEL_POP,
3018 /* Tunnel push-pop action is not compatible with
3020 add_ipfix_output_action(ctx, out_port);
3021 nl_msg_put_odp_port(ctx->xout->odp_actions,
3022 OVS_ACTION_ATTR_OUTPUT,
3028 ctx->sflow_odp_port = odp_port;
3029 ctx->sflow_n_outputs++;
3030 ctx->xout->nf_output_iface = ofp_port;
3035 flow->vlan_tci = flow_vlan_tci;
3036 flow->pkt_mark = flow_pkt_mark;
3037 flow->nw_tos = flow_nw_tos;
3041 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port,
3042 const struct xlate_bond_recirc *xr)
3044 compose_output_action__(ctx, ofp_port, xr, true);
3048 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
3050 struct rule_dpif *old_rule = ctx->rule;
3051 ovs_be64 old_cookie = ctx->rule_cookie;
3052 const struct rule_actions *actions;
3054 if (ctx->xin->resubmit_stats) {
3055 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
3061 ctx->rule_cookie = rule_dpif_get_flow_cookie(rule);
3062 actions = rule_dpif_get_actions(rule);
3063 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
3064 ctx->rule_cookie = old_cookie;
3065 ctx->rule = old_rule;
3070 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
3072 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3074 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
3075 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
3076 MAX_RESUBMIT_RECURSION);
3077 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
3078 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
3079 } else if (ctx->xout->odp_actions->size > UINT16_MAX) {
3080 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
3081 } else if (ctx->stack.size >= 65536) {
3082 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
3091 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
3092 bool may_packet_in, bool honor_table_miss)
3094 /* Check if we need to recirculate before matching in a table. */
3095 if (ctx->was_mpls) {
3096 ctx_trigger_recirculation(ctx);
3099 if (xlate_resubmit_resource_check(ctx)) {
3100 struct flow_wildcards *wc;
3101 uint8_t old_table_id = ctx->table_id;
3102 struct rule_dpif *rule;
3104 ctx->table_id = table_id;
3105 wc = (ctx->xin->skip_wildcards) ? NULL : &ctx->xout->wc;
3107 rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
3108 ctx->tables_version,
3109 &ctx->xin->flow, wc,
3110 ctx->xin->xcache != NULL,
3111 ctx->xin->resubmit_stats,
3112 &ctx->table_id, in_port,
3113 may_packet_in, honor_table_miss);
3115 if (OVS_UNLIKELY(ctx->xin->resubmit_hook)) {
3116 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse + 1);
3120 /* Fill in the cache entry here instead of xlate_recursively
3121 * to make the reference counting more explicit. We take a
3122 * reference in the lookups above if we are going to cache the
3124 if (ctx->xin->xcache) {
3125 struct xc_entry *entry;
3127 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
3128 entry->u.rule = rule;
3130 xlate_recursively(ctx, rule);
3133 ctx->table_id = old_table_id;
3141 xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
3142 struct ofputil_bucket *bucket)
3144 if (ctx->xin->resubmit_stats) {
3145 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
3147 if (ctx->xin->xcache) {
3148 struct xc_entry *entry;
3150 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
3151 entry->u.group.group = group_dpif_ref(group);
3152 entry->u.group.bucket = bucket;
3157 xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
3159 uint64_t action_list_stub[1024 / 8];
3160 struct ofpbuf action_list, action_set;
3161 struct flow old_flow = ctx->xin->flow;
3162 bool old_was_mpls = ctx->was_mpls;
3164 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
3165 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3167 ofpacts_execute_action_set(&action_list, &action_set);
3169 do_xlate_actions(action_list.data, action_list.size, ctx);
3172 ofpbuf_uninit(&action_set);
3173 ofpbuf_uninit(&action_list);
3175 /* Check if need to recirculate. */
3176 if (exit_recirculates(ctx)) {
3177 compose_recirculate_action(ctx);
3180 /* Roll back flow to previous state.
3181 * This is equivalent to cloning the packet for each bucket.
3183 * As a side effect any subsequently applied actions will
3184 * also effectively be applied to a clone of the packet taken
3185 * just before applying the all or indirect group.
3187 * Note that group buckets are action sets, hence they cannot modify the
3188 * main action set. Also any stack actions are ignored when executing an
3189 * action set, so group buckets cannot change the stack either.
3190 * However, we do allow resubmit actions in group buckets, which could
3191 * break the above assumptions. It is up to the controller to not mess up
3192 * with the action_set and stack in the tables resubmitted to from
3194 ctx->xin->flow = old_flow;
3196 /* The group bucket popping MPLS should have no effect after bucket
3198 ctx->was_mpls = old_was_mpls;
3200 /* The fact that the group bucket exits (for any reason) does not mean that
3201 * the translation after the group action should exit. Specifically, if
3202 * the group bucket recirculates (which typically modifies the packet), the
3203 * actions after the group action must continue processing with the
3204 * original, not the recirculated packet! */
3209 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3211 struct ofputil_bucket *bucket;
3212 const struct ovs_list *buckets;
3214 group_dpif_get_buckets(group, &buckets);
3216 LIST_FOR_EACH (bucket, list_node, buckets) {
3217 xlate_group_bucket(ctx, bucket);
3219 xlate_group_stats(ctx, group, NULL);
3223 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3225 struct ofputil_bucket *bucket;
3227 bucket = group_first_live_bucket(ctx, group, 0);
3229 xlate_group_bucket(ctx, bucket);
3230 xlate_group_stats(ctx, group, bucket);
3235 xlate_default_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3237 struct flow_wildcards *wc = &ctx->xout->wc;
3238 struct ofputil_bucket *bucket;
3241 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
3242 flow_mask_hash_fields(&ctx->xin->flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
3243 bucket = group_best_live_bucket(ctx, group, basis);
3245 xlate_group_bucket(ctx, bucket);
3246 xlate_group_stats(ctx, group, bucket);
3251 xlate_hash_fields_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3253 struct mf_bitmap hash_fields = MF_BITMAP_INITIALIZER;
3254 struct flow_wildcards *wc = &ctx->xout->wc;
3255 const struct field_array *fields;
3256 struct ofputil_bucket *bucket;
3260 fields = group_dpif_get_fields(group);
3261 basis = hash_uint64(group_dpif_get_selection_method_param(group));
3263 /* Determine which fields to hash */
3264 for (i = 0; i < MFF_N_IDS; i++) {
3265 if (bitmap_is_set(fields->used.bm, i)) {
3266 const struct mf_field *mf;
3268 /* If the field is already present in 'hash_fields' then
3269 * this loop has already checked that it and its pre-requisites
3270 * are present in the flow and its pre-requisites have
3271 * already been added to 'hash_fields'. There is nothing more
3272 * to do here and as an optimisation the loop can continue. */
3273 if (bitmap_is_set(hash_fields.bm, i)) {
3279 /* Only hash a field if it and its pre-requisites are present
3281 if (!mf_are_prereqs_ok(mf, &ctx->xin->flow)) {
3285 /* Hash both the field and its pre-requisites */
3286 mf_bitmap_set_field_and_prereqs(mf, &hash_fields);
3290 /* Hash the fields */
3291 for (i = 0; i < MFF_N_IDS; i++) {
3292 if (bitmap_is_set(hash_fields.bm, i)) {
3293 const struct mf_field *mf = mf_from_id(i);
3294 union mf_value value;
3297 mf_get_value(mf, &ctx->xin->flow, &value);
3298 /* This seems inefficient but so does apply_mask() */
3299 for (j = 0; j < mf->n_bytes; j++) {
3300 ((uint8_t *) &value)[j] &= ((uint8_t *) &fields->value[i])[j];
3302 basis = hash_bytes(&value, mf->n_bytes, basis);
3304 mf_mask_field(mf, &wc->masks);
3308 bucket = group_best_live_bucket(ctx, group, basis);
3310 xlate_group_bucket(ctx, bucket);
3311 xlate_group_stats(ctx, group, bucket);
3316 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3318 const char *selection_method = group_dpif_get_selection_method(group);
3320 if (selection_method[0] == '\0') {
3321 xlate_default_select_group(ctx, group);
3322 } else if (!strcasecmp("hash", selection_method)) {
3323 xlate_hash_fields_select_group(ctx, group);
3325 /* Parsing of groups should ensure this never happens */
3331 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3333 bool was_in_group = ctx->in_group;
3334 ctx->in_group = true;
3336 switch (group_dpif_get_type(group)) {
3338 case OFPGT11_INDIRECT:
3339 xlate_all_group(ctx, group);
3341 case OFPGT11_SELECT:
3342 xlate_select_group(ctx, group);
3345 xlate_ff_group(ctx, group);
3350 group_dpif_unref(group);
3352 ctx->in_group = was_in_group;
3356 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3358 if (xlate_resubmit_resource_check(ctx)) {
3359 struct group_dpif *group;
3362 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
3364 xlate_group_action__(ctx, group);
3374 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
3375 const struct ofpact_resubmit *resubmit)
3379 bool may_packet_in = false;
3380 bool honor_table_miss = false;
3382 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
3383 /* Still allow missed packets to be sent to the controller
3384 * if resubmitting from an internal table. */
3385 may_packet_in = true;
3386 honor_table_miss = true;
3389 in_port = resubmit->in_port;
3390 if (in_port == OFPP_IN_PORT) {
3391 in_port = ctx->xin->flow.in_port.ofp_port;
3394 table_id = resubmit->table_id;
3395 if (table_id == 255) {
3396 table_id = ctx->table_id;
3399 xlate_table_action(ctx, in_port, table_id, may_packet_in,
3404 flood_packets(struct xlate_ctx *ctx, bool all)
3406 const struct xport *xport;
3408 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
3409 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
3414 compose_output_action__(ctx, xport->ofp_port, NULL, false);
3415 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
3416 compose_output_action(ctx, xport->ofp_port, NULL);
3420 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3424 execute_controller_action(struct xlate_ctx *ctx, int len,
3425 enum ofp_packet_in_reason reason,
3426 uint16_t controller_id)
3428 struct ofproto_packet_in *pin;
3429 struct dp_packet *packet;
3432 ctx->xout->slow |= SLOW_CONTROLLER;
3433 if (!ctx->xin->packet) {
3437 packet = dp_packet_clone(ctx->xin->packet);
3439 use_masked = ctx->xbridge->support.masked_set_action;
3440 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3441 ctx->xout->odp_actions,
3442 &ctx->xout->wc, use_masked);
3444 odp_execute_actions(NULL, &packet, 1, false,
3445 ctx->xout->odp_actions->data,
3446 ctx->xout->odp_actions->size, NULL);
3448 pin = xmalloc(sizeof *pin);
3449 pin->up.packet_len = dp_packet_size(packet);
3450 pin->up.packet = dp_packet_steal_data(packet);
3451 pin->up.reason = reason;
3452 pin->up.table_id = ctx->table_id;
3453 pin->up.cookie = ctx->rule_cookie;
3455 flow_get_metadata(&ctx->xin->flow, &pin->up.flow_metadata);
3457 pin->controller_id = controller_id;
3458 pin->send_len = len;
3459 /* If a rule is a table-miss rule then this is
3460 * a table-miss handled by a table-miss rule.
3462 * Else, if rule is internal and has a controller action,
3463 * the later being implied by the rule being processed here,
3464 * then this is a table-miss handled without a table-miss rule.
3466 * Otherwise this is not a table-miss. */
3467 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
3469 if (rule_dpif_is_table_miss(ctx->rule)) {
3470 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
3471 } else if (rule_dpif_is_internal(ctx->rule)) {
3472 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
3475 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
3476 dp_packet_delete(packet);
3479 /* Called only when ctx->recirc_action_offset is set. */
3481 compose_recirculate_action(struct xlate_ctx *ctx)
3483 struct recirc_metadata md;
3487 use_masked = ctx->xbridge->support.masked_set_action;
3488 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3489 ctx->xout->odp_actions,
3490 &ctx->xout->wc, use_masked);
3492 recirc_metadata_from_flow(&md, &ctx->xin->flow);
3494 ovs_assert(ctx->recirc_action_offset >= 0);
3496 /* Only allocate recirculation ID if we have a packet. */
3497 if (ctx->xin->packet) {
3498 /* Allocate a unique recirc id for the given metadata state in the
3499 * flow. The life-cycle of this recirc id is managed by associating it
3500 * with the udpif key ('ukey') created for each new datapath flow. */
3501 id = recirc_alloc_id_ctx(ctx->xbridge->ofproto, 0, &md, &ctx->stack,
3502 ctx->recirc_action_offset,
3503 ctx->action_set.size, ctx->action_set.data);
3505 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3506 VLOG_ERR_RL(&rl, "Failed to allocate recirculation id");
3509 xlate_out_add_recirc(ctx->xout, id);
3511 /* Look up an existing recirc id for the given metadata state in the
3512 * flow. No new reference is taken, as the ID is RCU protected and is
3513 * only required temporarily for verification. */
3514 id = recirc_find_id(ctx->xbridge->ofproto, 0, &md, &ctx->stack,
3515 ctx->recirc_action_offset,
3516 ctx->action_set.size, ctx->action_set.data);
3517 /* We let zero 'id' to be used in the RECIRC action below, which will
3518 * fail all revalidations as zero is not a valid recirculation ID. */
3521 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
3523 /* Undo changes done by recirculation. */
3524 ctx->action_set.size = ctx->recirc_action_offset;
3525 ctx->recirc_action_offset = -1;
3526 ctx->last_unroll_offset = -1;
3530 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
3532 struct flow_wildcards *wc = &ctx->xout->wc;
3533 struct flow *flow = &ctx->xin->flow;
3536 ovs_assert(eth_type_mpls(mpls->ethertype));
3538 n = flow_count_mpls_labels(flow, wc);
3540 bool use_masked = ctx->xbridge->support.masked_set_action;
3542 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
3543 ctx->xout->odp_actions,
3544 &ctx->xout->wc, use_masked);
3545 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3546 if (ctx->xin->packet != NULL) {
3547 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3548 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3549 "MPLS push action can't be performed as it would "
3550 "have more MPLS LSEs than the %d supported.",
3551 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3557 flow_push_mpls(flow, n, mpls->ethertype, wc);
3561 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
3563 struct flow_wildcards *wc = &ctx->xout->wc;
3564 struct flow *flow = &ctx->xin->flow;
3565 int n = flow_count_mpls_labels(flow, wc);
3567 if (flow_pop_mpls(flow, n, eth_type, wc)) {
3568 if (ctx->xbridge->support.recirc) {
3569 ctx->was_mpls = true;
3571 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3572 if (ctx->xin->packet != NULL) {
3573 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3574 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3575 "MPLS pop action can't be performed as it has "
3576 "more MPLS LSEs than the %d supported.",
3577 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3580 ofpbuf_clear(ctx->xout->odp_actions);
3585 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3587 struct flow *flow = &ctx->xin->flow;
3589 if (!is_ip_any(flow)) {
3593 ctx->xout->wc.masks.nw_ttl = 0xff;
3594 if (flow->nw_ttl > 1) {
3600 for (i = 0; i < ids->n_controllers; i++) {
3601 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3605 /* Stop processing for current table. */
3611 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3613 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3614 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
3615 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
3620 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3622 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3623 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
3624 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
3629 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
3631 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3632 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3633 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
3638 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
3640 struct flow *flow = &ctx->xin->flow;
3641 struct flow_wildcards *wc = &ctx->xout->wc;
3643 if (eth_type_mpls(flow->dl_type)) {
3644 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
3646 wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3649 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3652 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
3656 /* Stop processing for current table. */
3661 xlate_output_action(struct xlate_ctx *ctx,
3662 ofp_port_t port, uint16_t max_len, bool may_packet_in)
3664 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
3666 ctx->xout->nf_output_iface = NF_OUT_DROP;
3670 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port, NULL);
3673 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3674 0, may_packet_in, true);
3680 flood_packets(ctx, false);
3683 flood_packets(ctx, true);
3685 case OFPP_CONTROLLER:
3686 execute_controller_action(ctx, max_len,
3687 (ctx->in_group ? OFPR_GROUP
3688 : ctx->in_action_set ? OFPR_ACTION_SET
3696 if (port != ctx->xin->flow.in_port.ofp_port) {
3697 compose_output_action(ctx, port, NULL);
3699 xlate_report(ctx, "skipping output to input port");
3704 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3705 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3706 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3707 ctx->xout->nf_output_iface = prev_nf_output_iface;
3708 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3709 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3710 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3715 xlate_output_reg_action(struct xlate_ctx *ctx,
3716 const struct ofpact_output_reg *or)
3718 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3719 if (port <= UINT16_MAX) {
3720 union mf_subvalue value;
3722 memset(&value, 0xff, sizeof value);
3723 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
3724 xlate_output_action(ctx, u16_to_ofp(port),
3725 or->max_len, false);
3730 xlate_enqueue_action(struct xlate_ctx *ctx,
3731 const struct ofpact_enqueue *enqueue)
3733 ofp_port_t ofp_port = enqueue->port;
3734 uint32_t queue_id = enqueue->queue;
3735 uint32_t flow_priority, priority;
3738 /* Translate queue to priority. */
3739 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
3741 /* Fall back to ordinary output action. */
3742 xlate_output_action(ctx, enqueue->port, 0, false);
3746 /* Check output port. */
3747 if (ofp_port == OFPP_IN_PORT) {
3748 ofp_port = ctx->xin->flow.in_port.ofp_port;
3749 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
3753 /* Add datapath actions. */
3754 flow_priority = ctx->xin->flow.skb_priority;
3755 ctx->xin->flow.skb_priority = priority;
3756 compose_output_action(ctx, ofp_port, NULL);
3757 ctx->xin->flow.skb_priority = flow_priority;
3759 /* Update NetFlow output port. */
3760 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3761 ctx->xout->nf_output_iface = ofp_port;
3762 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3763 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3768 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3770 uint32_t skb_priority;
3772 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
3773 ctx->xin->flow.skb_priority = skb_priority;
3775 /* Couldn't translate queue to a priority. Nothing to do. A warning
3776 * has already been logged. */
3781 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
3783 const struct xbridge *xbridge = xbridge_;
3794 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3797 port = get_ofp_port(xbridge, ofp_port);
3798 return port ? port->may_enable : false;
3803 xlate_bundle_action(struct xlate_ctx *ctx,
3804 const struct ofpact_bundle *bundle)
3808 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
3810 CONST_CAST(struct xbridge *, ctx->xbridge));
3811 if (bundle->dst.field) {
3812 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
3815 xlate_output_action(ctx, port, 0, false);
3820 xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
3821 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
3823 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
3824 if (ctx->xin->may_learn) {
3825 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
3830 xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
3832 ctx->xout->has_learn = true;
3833 learn_mask(learn, &ctx->xout->wc);
3835 if (ctx->xin->xcache) {
3836 struct xc_entry *entry;
3838 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
3839 entry->u.learn.ofproto = ctx->xbridge->ofproto;
3840 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
3841 entry->u.learn.ofpacts = ofpbuf_new(64);
3842 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
3843 entry->u.learn.ofpacts);
3844 } else if (ctx->xin->may_learn) {
3845 uint64_t ofpacts_stub[1024 / 8];
3846 struct ofputil_flow_mod fm;
3847 struct ofpbuf ofpacts;
3849 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3850 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
3851 ofpbuf_uninit(&ofpacts);
3856 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
3857 uint16_t idle_timeout, uint16_t hard_timeout)
3859 if (tcp_flags & (TCP_FIN | TCP_RST)) {
3860 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
3865 xlate_fin_timeout(struct xlate_ctx *ctx,
3866 const struct ofpact_fin_timeout *oft)
3869 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
3870 oft->fin_idle_timeout, oft->fin_hard_timeout);
3871 if (ctx->xin->xcache) {
3872 struct xc_entry *entry;
3874 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
3875 /* XC_RULE already holds a reference on the rule, none is taken
3877 entry->u.fin.rule = ctx->rule;
3878 entry->u.fin.idle = oft->fin_idle_timeout;
3879 entry->u.fin.hard = oft->fin_hard_timeout;
3885 xlate_sample_action(struct xlate_ctx *ctx,
3886 const struct ofpact_sample *os)
3888 union user_action_cookie cookie;
3889 /* Scale the probability from 16-bit to 32-bit while representing
3890 * the same percentage. */
3891 uint32_t probability = (os->probability << 16) | os->probability;
3894 if (!ctx->xbridge->support.variable_length_userdata) {
3895 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3897 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
3898 "lacks support (needs Linux 3.10+ or kernel module from "
3903 use_masked = ctx->xbridge->support.masked_set_action;
3904 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3905 ctx->xout->odp_actions,
3906 &ctx->xout->wc, use_masked);
3908 compose_flow_sample_cookie(os->probability, os->collector_set_id,
3909 os->obs_domain_id, os->obs_point_id, &cookie);
3910 compose_sample_action(ctx->xbridge, ctx->xout->odp_actions,
3911 &ctx->xin->flow, probability, &cookie,
3912 sizeof cookie.flow_sample, ODPP_NONE);
3916 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
3918 if (xport->config & (is_stp(&ctx->xin->flow)
3919 ? OFPUTIL_PC_NO_RECV_STP
3920 : OFPUTIL_PC_NO_RECV)) {
3924 /* Only drop packets here if both forwarding and learning are
3925 * disabled. If just learning is enabled, we need to have
3926 * OFPP_NORMAL and the learning action have a look at the packet
3927 * before we can drop it. */
3928 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
3929 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
3937 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
3939 const struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3940 size_t on_len = ofpact_nest_get_action_len(on);
3941 const struct ofpact *inner;
3943 /* Maintain actset_output depending on the contents of the action set:
3945 * - OFPP_UNSET, if there is no "output" action.
3947 * - The output port, if there is an "output" action and no "group"
3950 * - OFPP_UNSET, if there is a "group" action.
3952 if (!ctx->action_set_has_group) {
3953 OFPACT_FOR_EACH (inner, on->actions, on_len) {
3954 if (inner->type == OFPACT_OUTPUT) {
3955 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(inner)->port;
3956 } else if (inner->type == OFPACT_GROUP) {
3957 ctx->xin->flow.actset_output = OFPP_UNSET;
3958 ctx->action_set_has_group = true;
3963 ofpbuf_put(&ctx->action_set, on->actions, on_len);
3964 ofpact_pad(&ctx->action_set);
3968 xlate_action_set(struct xlate_ctx *ctx)
3970 uint64_t action_list_stub[1024 / 64];
3971 struct ofpbuf action_list;
3973 ctx->in_action_set = true;
3974 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3975 ofpacts_execute_action_set(&action_list, &ctx->action_set);
3976 /* Clear the action set, as it is not needed any more. */
3977 ofpbuf_clear(&ctx->action_set);
3978 do_xlate_actions(action_list.data, action_list.size, ctx);
3979 ctx->in_action_set = false;
3980 ofpbuf_uninit(&action_list);
3984 recirc_put_unroll_xlate(struct xlate_ctx *ctx)
3986 struct ofpact_unroll_xlate *unroll;
3988 unroll = ctx->last_unroll_offset < 0
3990 : ALIGNED_CAST(struct ofpact_unroll_xlate *,
3991 (char *)ctx->action_set.data + ctx->last_unroll_offset);
3993 /* Restore the table_id and rule cookie for a potential PACKET
3996 (ctx->table_id != unroll->rule_table_id
3997 || ctx->rule_cookie != unroll->rule_cookie)) {
3999 ctx->last_unroll_offset = ctx->action_set.size;
4000 unroll = ofpact_put_UNROLL_XLATE(&ctx->action_set);
4001 unroll->rule_table_id = ctx->table_id;
4002 unroll->rule_cookie = ctx->rule_cookie;
4007 /* Copy remaining actions to the action_set to be executed after recirculation.
4008 * UNROLL_XLATE action is inserted, if not already done so, before actions that
4009 * may generate PACKET_INs from the current table and without matching another
4012 recirc_unroll_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4013 struct xlate_ctx *ctx)
4015 const struct ofpact *a;
4017 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4019 /* May generate PACKET INs. */
4020 case OFPACT_OUTPUT_REG:
4023 case OFPACT_CONTROLLER:
4024 case OFPACT_DEC_MPLS_TTL:
4025 case OFPACT_DEC_TTL:
4026 recirc_put_unroll_xlate(ctx);
4029 /* These may not generate PACKET INs. */
4030 case OFPACT_SET_TUNNEL:
4031 case OFPACT_REG_MOVE:
4032 case OFPACT_SET_FIELD:
4033 case OFPACT_STACK_PUSH:
4034 case OFPACT_STACK_POP:
4036 case OFPACT_WRITE_METADATA:
4037 case OFPACT_RESUBMIT: /* May indirectly generate PACKET INs, */
4038 case OFPACT_GOTO_TABLE: /* but from a different table and rule. */
4039 case OFPACT_ENQUEUE:
4040 case OFPACT_SET_VLAN_VID:
4041 case OFPACT_SET_VLAN_PCP:
4042 case OFPACT_STRIP_VLAN:
4043 case OFPACT_PUSH_VLAN:
4044 case OFPACT_SET_ETH_SRC:
4045 case OFPACT_SET_ETH_DST:
4046 case OFPACT_SET_IPV4_SRC:
4047 case OFPACT_SET_IPV4_DST:
4048 case OFPACT_SET_IP_DSCP:
4049 case OFPACT_SET_IP_ECN:
4050 case OFPACT_SET_IP_TTL:
4051 case OFPACT_SET_L4_SRC_PORT:
4052 case OFPACT_SET_L4_DST_PORT:
4053 case OFPACT_SET_QUEUE:
4054 case OFPACT_POP_QUEUE:
4055 case OFPACT_PUSH_MPLS:
4056 case OFPACT_POP_MPLS:
4057 case OFPACT_SET_MPLS_LABEL:
4058 case OFPACT_SET_MPLS_TC:
4059 case OFPACT_SET_MPLS_TTL:
4060 case OFPACT_MULTIPATH:
4063 case OFPACT_UNROLL_XLATE:
4064 case OFPACT_FIN_TIMEOUT:
4065 case OFPACT_CLEAR_ACTIONS:
4066 case OFPACT_WRITE_ACTIONS:
4071 /* These need not be copied for restoration. */
4073 case OFPACT_CONJUNCTION:
4076 /* Copy the action over. */
4077 ofpbuf_put(&ctx->action_set, a, OFPACT_ALIGN(a->len));
4081 #define CHECK_MPLS_RECIRCULATION() \
4082 if (ctx->was_mpls) { \
4083 ctx_trigger_recirculation(ctx); \
4086 #define CHECK_MPLS_RECIRCULATION_IF(COND) \
4088 CHECK_MPLS_RECIRCULATION(); \
4092 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4093 struct xlate_ctx *ctx)
4095 struct flow_wildcards *wc = &ctx->xout->wc;
4096 struct flow *flow = &ctx->xin->flow;
4097 const struct ofpact *a;
4099 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
4100 tnl_arp_snoop(flow, wc, ctx->xbridge->name);
4102 /* dl_type already in the mask, not set below. */
4104 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4105 struct ofpact_controller *controller;
4106 const struct ofpact_metadata *metadata;
4107 const struct ofpact_set_field *set_field;
4108 const struct mf_field *mf;
4111 /* Check if need to store the remaining actions for later
4113 if (exit_recirculates(ctx)) {
4114 recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
4116 (uint8_t *)ofpacts)),
4124 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
4125 ofpact_get_OUTPUT(a)->max_len, true);
4129 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
4130 /* Group could not be found. */
4135 case OFPACT_CONTROLLER:
4136 controller = ofpact_get_CONTROLLER(a);
4137 execute_controller_action(ctx, controller->max_len,
4139 controller->controller_id);
4142 case OFPACT_ENQUEUE:
4143 memset(&wc->masks.skb_priority, 0xff,
4144 sizeof wc->masks.skb_priority);
4145 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
4148 case OFPACT_SET_VLAN_VID:
4149 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
4150 if (flow->vlan_tci & htons(VLAN_CFI) ||
4151 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
4152 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
4153 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
4158 case OFPACT_SET_VLAN_PCP:
4159 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
4160 if (flow->vlan_tci & htons(VLAN_CFI) ||
4161 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
4162 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
4163 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
4164 << VLAN_PCP_SHIFT) | VLAN_CFI);
4168 case OFPACT_STRIP_VLAN:
4169 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4170 flow->vlan_tci = htons(0);
4173 case OFPACT_PUSH_VLAN:
4174 /* XXX 802.1AD(QinQ) */
4175 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4176 flow->vlan_tci = htons(VLAN_CFI);
4179 case OFPACT_SET_ETH_SRC:
4180 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
4181 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
4184 case OFPACT_SET_ETH_DST:
4185 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
4186 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
4189 case OFPACT_SET_IPV4_SRC:
4190 CHECK_MPLS_RECIRCULATION();
4191 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4192 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
4193 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
4197 case OFPACT_SET_IPV4_DST:
4198 CHECK_MPLS_RECIRCULATION();
4199 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4200 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
4201 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
4205 case OFPACT_SET_IP_DSCP:
4206 CHECK_MPLS_RECIRCULATION();
4207 if (is_ip_any(flow)) {
4208 wc->masks.nw_tos |= IP_DSCP_MASK;
4209 flow->nw_tos &= ~IP_DSCP_MASK;
4210 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
4214 case OFPACT_SET_IP_ECN:
4215 CHECK_MPLS_RECIRCULATION();
4216 if (is_ip_any(flow)) {
4217 wc->masks.nw_tos |= IP_ECN_MASK;
4218 flow->nw_tos &= ~IP_ECN_MASK;
4219 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
4223 case OFPACT_SET_IP_TTL:
4224 CHECK_MPLS_RECIRCULATION();
4225 if (is_ip_any(flow)) {
4226 wc->masks.nw_ttl = 0xff;
4227 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
4231 case OFPACT_SET_L4_SRC_PORT:
4232 CHECK_MPLS_RECIRCULATION();
4233 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4234 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4235 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
4236 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
4240 case OFPACT_SET_L4_DST_PORT:
4241 CHECK_MPLS_RECIRCULATION();
4242 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4243 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4244 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
4245 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
4249 case OFPACT_RESUBMIT:
4250 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
4253 case OFPACT_SET_TUNNEL:
4254 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
4257 case OFPACT_SET_QUEUE:
4258 memset(&wc->masks.skb_priority, 0xff,
4259 sizeof wc->masks.skb_priority);
4260 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
4263 case OFPACT_POP_QUEUE:
4264 memset(&wc->masks.skb_priority, 0xff,
4265 sizeof wc->masks.skb_priority);
4266 flow->skb_priority = ctx->orig_skb_priority;
4269 case OFPACT_REG_MOVE:
4270 CHECK_MPLS_RECIRCULATION_IF(
4271 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
4272 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
4273 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
4276 case OFPACT_SET_FIELD:
4277 CHECK_MPLS_RECIRCULATION_IF(
4278 mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field));
4279 set_field = ofpact_get_SET_FIELD(a);
4280 mf = set_field->field;
4282 /* Set field action only ever overwrites packet's outermost
4283 * applicable header fields. Do nothing if no header exists. */
4284 if (mf->id == MFF_VLAN_VID) {
4285 wc->masks.vlan_tci |= htons(VLAN_CFI);
4286 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4289 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
4290 /* 'dl_type' is already unwildcarded. */
4291 && !eth_type_mpls(flow->dl_type)) {
4294 /* A flow may wildcard nw_frag. Do nothing if setting a trasport
4295 * header field on a packet that does not have them. */
4296 mf_mask_field_and_prereqs(mf, &wc->masks);
4297 if (mf_are_prereqs_ok(mf, flow)) {
4298 mf_set_flow_value_masked(mf, &set_field->value,
4299 &set_field->mask, flow);
4303 case OFPACT_STACK_PUSH:
4304 CHECK_MPLS_RECIRCULATION_IF(
4305 mf_is_l3_or_higher(ofpact_get_STACK_PUSH(a)->subfield.field));
4306 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
4310 case OFPACT_STACK_POP:
4311 CHECK_MPLS_RECIRCULATION_IF(
4312 mf_is_l3_or_higher(ofpact_get_STACK_POP(a)->subfield.field));
4313 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
4317 case OFPACT_PUSH_MPLS:
4318 /* Recirculate if it is an IP packet with a zero ttl. This may
4319 * indicate that the packet was previously MPLS and an MPLS pop
4320 * action converted it to IP. In this case recirculating should
4321 * reveal the IP TTL which is used as the basis for a new MPLS
4323 CHECK_MPLS_RECIRCULATION_IF(
4324 !flow_count_mpls_labels(flow, wc)
4325 && flow->nw_ttl == 0
4326 && is_ip_any(flow));
4327 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
4330 case OFPACT_POP_MPLS:
4331 CHECK_MPLS_RECIRCULATION();
4332 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
4335 case OFPACT_SET_MPLS_LABEL:
4336 CHECK_MPLS_RECIRCULATION();
4337 compose_set_mpls_label_action(
4338 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
4341 case OFPACT_SET_MPLS_TC:
4342 CHECK_MPLS_RECIRCULATION();
4343 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
4346 case OFPACT_SET_MPLS_TTL:
4347 CHECK_MPLS_RECIRCULATION();
4348 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
4351 case OFPACT_DEC_MPLS_TTL:
4352 CHECK_MPLS_RECIRCULATION();
4353 if (compose_dec_mpls_ttl_action(ctx)) {
4358 case OFPACT_DEC_TTL:
4359 CHECK_MPLS_RECIRCULATION();
4360 wc->masks.nw_ttl = 0xff;
4361 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
4367 /* Nothing to do. */
4370 case OFPACT_MULTIPATH:
4371 CHECK_MPLS_RECIRCULATION();
4372 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
4376 CHECK_MPLS_RECIRCULATION();
4377 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
4380 case OFPACT_OUTPUT_REG:
4381 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
4385 CHECK_MPLS_RECIRCULATION();
4386 xlate_learn_action(ctx, ofpact_get_LEARN(a));
4389 case OFPACT_CONJUNCTION: {
4390 /* A flow with a "conjunction" action represents part of a special
4391 * kind of "set membership match". Such a flow should not actually
4392 * get executed, but it could via, say, a "packet-out", even though
4393 * that wouldn't be useful. Log it to help debugging. */
4394 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4395 VLOG_INFO_RL(&rl, "executing no-op conjunction action");
4403 case OFPACT_UNROLL_XLATE: {
4404 struct ofpact_unroll_xlate *unroll = ofpact_get_UNROLL_XLATE(a);
4406 /* Restore translation context data that was stored earlier. */
4407 ctx->table_id = unroll->rule_table_id;
4408 ctx->rule_cookie = unroll->rule_cookie;
4411 case OFPACT_FIN_TIMEOUT:
4412 CHECK_MPLS_RECIRCULATION();
4413 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4414 ctx->xout->has_fin_timeout = true;
4415 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
4418 case OFPACT_CLEAR_ACTIONS:
4419 ofpbuf_clear(&ctx->action_set);
4420 ctx->xin->flow.actset_output = OFPP_UNSET;
4421 ctx->action_set_has_group = false;
4424 case OFPACT_WRITE_ACTIONS:
4425 xlate_write_actions(ctx, a);
4428 case OFPACT_WRITE_METADATA:
4429 metadata = ofpact_get_WRITE_METADATA(a);
4430 flow->metadata &= ~metadata->mask;
4431 flow->metadata |= metadata->metadata & metadata->mask;
4435 /* Not implemented yet. */
4438 case OFPACT_GOTO_TABLE: {
4439 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
4441 /* Allow ctx->table_id == TBL_INTERNAL, which will be greater
4442 * than ogt->table_id. This is to allow goto_table actions that
4443 * triggered recirculation: ctx->table_id will be TBL_INTERNAL
4444 * after recirculation. */
4445 ovs_assert(ctx->table_id == TBL_INTERNAL
4446 || ctx->table_id < ogt->table_id);
4447 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
4448 ogt->table_id, true, true);
4453 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
4457 /* Check if need to store this and the remaining actions for later
4459 if (ctx->exit && ctx_first_recirculation_action(ctx)) {
4460 recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
4462 (uint8_t *)ofpacts)),
4470 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
4471 const struct flow *flow, ofp_port_t in_port,
4472 struct rule_dpif *rule, uint16_t tcp_flags,
4473 const struct dp_packet *packet)
4475 xin->ofproto = ofproto;
4477 xin->flow.in_port.ofp_port = in_port;
4478 xin->flow.actset_output = OFPP_UNSET;
4479 xin->packet = packet;
4480 xin->may_learn = packet != NULL;
4483 xin->ofpacts = NULL;
4484 xin->ofpacts_len = 0;
4485 xin->tcp_flags = tcp_flags;
4486 xin->resubmit_hook = NULL;
4487 xin->report_hook = NULL;
4488 xin->resubmit_stats = NULL;
4489 xin->skip_wildcards = false;
4490 xin->odp_actions = NULL;
4492 /* Do recirc lookup. */
4493 xin->recirc = flow->recirc_id
4494 ? recirc_id_node_find(flow->recirc_id)
4499 xlate_out_uninit(struct xlate_out *xout)
4502 if (xout->odp_actions == &xout->odp_actions_buf) {
4503 ofpbuf_uninit(xout->odp_actions);
4505 xlate_out_free_recircs(xout);
4509 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
4510 * into datapath actions, using 'ctx', and discards the datapath actions. */
4512 xlate_actions_for_side_effects(struct xlate_in *xin)
4514 struct xlate_out xout;
4516 xlate_actions(xin, &xout);
4517 xlate_out_uninit(&xout);
4521 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
4524 dst->slow = src->slow;
4525 dst->has_learn = src->has_learn;
4526 dst->has_normal = src->has_normal;
4527 dst->has_fin_timeout = src->has_fin_timeout;
4528 dst->nf_output_iface = src->nf_output_iface;
4529 dst->mirrors = src->mirrors;
4531 dst->odp_actions = &dst->odp_actions_buf;
4532 ofpbuf_use_stub(dst->odp_actions, dst->odp_actions_stub,
4533 sizeof dst->odp_actions_stub);
4534 ofpbuf_put(dst->odp_actions, src->odp_actions->data, src->odp_actions->size);
4537 static struct skb_priority_to_dscp *
4538 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
4540 struct skb_priority_to_dscp *pdscp;
4543 hash = hash_int(skb_priority, 0);
4544 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
4545 if (pdscp->skb_priority == skb_priority) {
4553 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
4556 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
4557 *dscp = pdscp ? pdscp->dscp : 0;
4558 return pdscp != NULL;
4562 count_skb_priorities(const struct xport *xport)
4564 return hmap_count(&xport->skb_priorities);
4568 clear_skb_priorities(struct xport *xport)
4570 struct skb_priority_to_dscp *pdscp, *next;
4572 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
4573 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
4579 actions_output_to_local_port(const struct xlate_ctx *ctx)
4581 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
4582 const struct nlattr *a;
4585 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions->data,
4586 ctx->xout->odp_actions->size) {
4587 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
4588 && nl_attr_get_odp_port(a) == local_odp_port) {
4595 #if defined(__linux__)
4596 /* Returns the maximum number of packets that the Linux kernel is willing to
4597 * queue up internally to certain kinds of software-implemented ports, or the
4598 * default (and rarely modified) value if it cannot be determined. */
4600 netdev_max_backlog(void)
4602 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
4603 static int max_backlog = 1000; /* The normal default value. */
4605 if (ovsthread_once_start(&once)) {
4606 static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
4610 stream = fopen(filename, "r");
4612 VLOG_WARN("%s: open failed (%s)", filename, ovs_strerror(errno));
4614 if (fscanf(stream, "%d", &n) != 1) {
4615 VLOG_WARN("%s: read error", filename);
4616 } else if (n <= 100) {
4617 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
4623 ovsthread_once_done(&once);
4625 VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
4631 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
4634 count_output_actions(const struct ofpbuf *odp_actions)
4636 const struct nlattr *a;
4640 NL_ATTR_FOR_EACH_UNSAFE (a, left, odp_actions->data, odp_actions->size) {
4641 if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
4647 #endif /* defined(__linux__) */
4649 /* Returns true if 'odp_actions' contains more output actions than the datapath
4650 * can reliably handle in one go. On Linux, this is the value of the
4651 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
4652 * packets that the kernel is willing to queue up for processing while the
4653 * datapath is processing a set of actions. */
4655 too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
4658 return (odp_actions->size / NL_A_U32_SIZE > netdev_max_backlog()
4659 && count_output_actions(odp_actions) > netdev_max_backlog());
4661 /* OSes other than Linux might have similar limits, but we don't know how
4662 * to determine them.*/
4667 /* Translates the flow, actions, or rule in 'xin' into datapath actions in
4669 * The caller must take responsibility for eventually freeing 'xout', with
4670 * xlate_out_uninit(). */
4672 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
4674 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4675 struct flow_wildcards *wc = NULL;
4676 struct flow *flow = &xin->flow;
4677 struct rule_dpif *rule = NULL;
4679 enum slow_path_reason special;
4680 const struct ofpact *ofpacts;
4681 struct xbridge *xbridge;
4682 struct xport *in_port;
4683 struct flow orig_flow;
4684 struct xlate_ctx ctx;
4689 COVERAGE_INC(xlate_actions);
4691 /* Flow initialization rules:
4692 * - 'base_flow' must match the kernel's view of the packet at the
4693 * time that action processing starts. 'flow' represents any
4694 * transformations we wish to make through actions.
4695 * - By default 'base_flow' and 'flow' are the same since the input
4696 * packet matches the output before any actions are applied.
4697 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
4698 * of the received packet as seen by the kernel. If we later output
4699 * to another device without any modifications this will cause us to
4700 * insert a new tag since the original one was stripped off by the
4702 * - Tunnel metadata as received is retained in 'flow'. This allows
4703 * tunnel metadata matching also in later tables.
4704 * Since a kernel action for setting the tunnel metadata will only be
4705 * generated with actual tunnel output, changing the tunnel metadata
4706 * values in 'flow' (such as tun_id) will only have effect with a later
4707 * tunnel output action.
4708 * - Tunnel 'base_flow' is completely cleared since that is what the
4709 * kernel does. If we wish to maintain the original values an action
4710 * needs to be generated. */
4715 ctx.xout->has_learn = false;
4716 ctx.xout->has_normal = false;
4717 ctx.xout->has_fin_timeout = false;
4718 ctx.xout->nf_output_iface = NF_OUT_DROP;
4719 ctx.xout->mirrors = 0;
4720 ctx.xout->n_recircs = 0;
4722 xout->odp_actions = xin->odp_actions;
4723 if (!xout->odp_actions) {
4724 xout->odp_actions = &xout->odp_actions_buf;
4725 ofpbuf_use_stub(xout->odp_actions, xout->odp_actions_stub,
4726 sizeof xout->odp_actions_stub);
4728 ofpbuf_reserve(xout->odp_actions, NL_A_U32_SIZE);
4730 xbridge = xbridge_lookup(xcfg, xin->ofproto);
4734 /* 'ctx.xbridge' may be changed by action processing, whereas 'xbridge'
4735 * will remain set on the original input bridge. */
4736 ctx.xbridge = xbridge;
4737 ctx.rule = xin->rule;
4739 ctx.base_flow = *flow;
4740 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
4741 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
4743 if (!xin->skip_wildcards) {
4745 flow_wildcards_init_catchall(wc);
4746 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
4747 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
4748 if (is_ip_any(flow)) {
4749 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
4751 if (xbridge->support.recirc) {
4752 /* Always exactly match recirc_id when datapath supports
4754 wc->masks.recirc_id = UINT32_MAX;
4756 if (xbridge->netflow) {
4757 netflow_mask_wc(flow, wc);
4760 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
4762 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
4766 ctx.in_group = false;
4767 ctx.in_action_set = false;
4768 ctx.orig_skb_priority = flow->skb_priority;
4770 ctx.rule_cookie = OVS_BE64_MAX;
4772 ctx.was_mpls = false;
4773 ctx.recirc_action_offset = -1;
4774 ctx.last_unroll_offset = -1;
4776 ctx.action_set_has_group = false;
4777 ofpbuf_use_stub(&ctx.action_set,
4778 ctx.action_set_stub, sizeof ctx.action_set_stub);
4780 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
4782 /* The in_port of the original packet before recirculation. */
4783 in_port = get_ofp_port(xbridge, flow->in_port.ofp_port);
4786 const struct recirc_id_node *recirc = xin->recirc;
4788 if (xin->ofpacts_len > 0 || ctx.rule) {
4789 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4791 VLOG_WARN_RL(&rl, "Recirculation conflict (%s)!",
4792 xin->ofpacts_len > 0
4798 /* Set the bridge for post-recirculation processing if needed. */
4799 if (ctx.xbridge->ofproto != recirc->ofproto) {
4800 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4801 const struct xbridge *new_bridge = xbridge_lookup(xcfg,
4804 if (OVS_UNLIKELY(!new_bridge)) {
4805 /* Drop the packet if the bridge cannot be found. */
4806 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4807 VLOG_WARN_RL(&rl, "Recirculation bridge no longer exists.");
4810 ctx.xbridge = new_bridge;
4813 /* Set the post-recirculation table id. Note: A table lookup is done
4814 * only if there are no post-recirculation actions. */
4815 ctx.table_id = recirc->table_id;
4817 /* Restore pipeline metadata. May change flow's in_port and other
4818 * metadata to the values that existed when recirculation was
4820 recirc_metadata_to_flow(&recirc->metadata, flow);
4822 /* Restore stack, if any. */
4823 if (recirc->stack) {
4824 ofpbuf_put(&ctx.stack, recirc->stack->data, recirc->stack->size);
4827 /* Restore action set, if any. */
4828 if (recirc->action_set_len) {
4829 const struct ofpact *a;
4831 ofpbuf_put(&ctx.action_set, recirc->ofpacts,
4832 recirc->action_set_len);
4834 OFPACT_FOR_EACH(a, recirc->ofpacts, recirc->action_set_len) {
4835 if (a->type == OFPACT_GROUP) {
4836 ctx.action_set_has_group = true;
4842 /* Restore recirculation actions. If there are no actions, processing
4843 * will start with a lookup in the table set above. */
4844 if (recirc->ofpacts_len > recirc->action_set_len) {
4845 xin->ofpacts_len = recirc->ofpacts_len - recirc->action_set_len;
4846 xin->ofpacts = recirc->ofpacts +
4847 recirc->action_set_len / sizeof *recirc->ofpacts;
4849 } else if (OVS_UNLIKELY(flow->recirc_id)) {
4850 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4852 VLOG_WARN_RL(&rl, "Recirculation context not found for ID %"PRIx32,
4856 /* The bridge is now known so obtain its table version. */
4857 ctx.tables_version = ofproto_dpif_get_tables_version(ctx.xbridge->ofproto);
4859 if (!xin->ofpacts && !ctx.rule) {
4860 rule = rule_dpif_lookup_from_table(ctx.xbridge->ofproto,
4861 ctx.tables_version, flow, wc,
4862 ctx.xin->xcache != NULL,
4863 ctx.xin->resubmit_stats,
4865 flow->in_port.ofp_port, true, true);
4866 if (ctx.xin->resubmit_stats) {
4867 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
4869 if (ctx.xin->xcache) {
4870 struct xc_entry *entry;
4872 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
4873 entry->u.rule = rule;
4877 if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
4878 ctx.xin->resubmit_hook(ctx.xin, rule, 0);
4881 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
4884 ofpacts = xin->ofpacts;
4885 ofpacts_len = xin->ofpacts_len;
4886 } else if (ctx.rule) {
4887 const struct rule_actions *actions = rule_dpif_get_actions(ctx.rule);
4889 ofpacts = actions->ofpacts;
4890 ofpacts_len = actions->ofpacts_len;
4892 ctx.rule_cookie = rule_dpif_get_flow_cookie(ctx.rule);
4897 if (mbridge_has_mirrors(xbridge->mbridge)) {
4898 /* Do this conditionally because the copy is expensive enough that it
4899 * shows up in profiles. */
4903 /* Tunnel stats only for non-recirculated packets. */
4904 if (!xin->recirc && in_port && in_port->is_tunnel) {
4905 if (ctx.xin->resubmit_stats) {
4906 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
4908 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
4911 if (ctx.xin->xcache) {
4912 struct xc_entry *entry;
4914 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
4915 entry->u.dev.rx = netdev_ref(in_port->netdev);
4916 entry->u.dev.bfd = bfd_ref(in_port->bfd);
4920 /* Do not perform special processing on recirculated packets,
4921 * as recirculated packets are not really received by the bridge. */
4923 (special = process_special(&ctx, flow, in_port, ctx.xin->packet))) {
4924 ctx.xout->slow |= special;
4926 size_t sample_actions_len;
4928 if (flow->in_port.ofp_port
4929 != vsp_realdev_to_vlandev(xbridge->ofproto,
4930 flow->in_port.ofp_port,
4932 ctx.base_flow.vlan_tci = 0;
4935 /* Sampling is done only for packets really received by the bridge. */
4937 add_sflow_action(&ctx);
4938 add_ipfix_action(&ctx);
4939 sample_actions_len = ctx.xout->odp_actions->size;
4941 sample_actions_len = 0;
4944 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
4945 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
4947 /* We've let OFPP_NORMAL and the learning action look at the
4948 * packet, so drop it now if forwarding is disabled. */
4949 if (in_port && (!xport_stp_forward_state(in_port) ||
4950 !xport_rstp_forward_state(in_port))) {
4951 /* Drop all actions added by do_xlate_actions() above. */
4952 ctx.xout->odp_actions->size = sample_actions_len;
4954 /* Undo changes that may have been done for recirculation. */
4955 if (exit_recirculates(&ctx)) {
4956 ctx.action_set.size = ctx.recirc_action_offset;
4957 ctx.recirc_action_offset = -1;
4958 ctx.last_unroll_offset = -1;
4960 } else if (ctx.action_set.size) {
4961 /* Translate action set only if not dropping the packet and
4962 * not recirculating. */
4963 if (!exit_recirculates(&ctx)) {
4964 xlate_action_set(&ctx);
4967 /* Check if need to recirculate. */
4968 if (exit_recirculates(&ctx)) {
4969 compose_recirculate_action(&ctx);
4973 /* Output only fully processed packets. */
4974 if (!exit_recirculates(&ctx)
4975 && xbridge->has_in_band
4976 && in_band_must_output_to_local_port(flow)
4977 && !actions_output_to_local_port(&ctx)) {
4978 compose_output_action(&ctx, OFPP_LOCAL, NULL);
4982 fix_sflow_action(&ctx);
4984 /* Only mirror fully processed packets. */
4985 if (!exit_recirculates(&ctx)
4986 && mbridge_has_mirrors(xbridge->mbridge)) {
4987 add_mirror_actions(&ctx, &orig_flow);
4991 if (nl_attr_oversized(ctx.xout->odp_actions->size)) {
4992 /* These datapath actions are too big for a Netlink attribute, so we
4993 * can't hand them to the kernel directly. dpif_execute() can execute
4994 * them one by one with help, so just mark the result as SLOW_ACTION to
4995 * prevent the flow from being installed. */
4996 COVERAGE_INC(xlate_actions_oversize);
4997 ctx.xout->slow |= SLOW_ACTION;
4998 } else if (too_many_output_actions(ctx.xout->odp_actions)) {
4999 COVERAGE_INC(xlate_actions_too_many_output);
5000 ctx.xout->slow |= SLOW_ACTION;
5003 /* Update mirror stats only for packets really received by the bridge. */
5004 if (!xin->recirc && mbridge_has_mirrors(xbridge->mbridge)) {
5005 if (ctx.xin->resubmit_stats) {
5006 mirror_update_stats(xbridge->mbridge, xout->mirrors,
5007 ctx.xin->resubmit_stats->n_packets,
5008 ctx.xin->resubmit_stats->n_bytes);
5010 if (ctx.xin->xcache) {
5011 struct xc_entry *entry;
5013 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_MIRROR);
5014 entry->u.mirror.mbridge = mbridge_ref(xbridge->mbridge);
5015 entry->u.mirror.mirrors = xout->mirrors;
5019 /* Do netflow only for packets really received by the bridge. */
5020 if (!xin->recirc && xbridge->netflow) {
5021 /* Only update netflow if we don't have controller flow. We don't
5022 * report NetFlow expiration messages for such facets because they
5023 * are just part of the control logic for the network, not real
5025 if (ofpacts_len == 0
5026 || ofpacts->type != OFPACT_CONTROLLER
5027 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
5028 if (ctx.xin->resubmit_stats) {
5029 netflow_flow_update(xbridge->netflow, flow,
5030 xout->nf_output_iface,
5031 ctx.xin->resubmit_stats);
5033 if (ctx.xin->xcache) {
5034 struct xc_entry *entry;
5036 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
5037 entry->u.nf.netflow = netflow_ref(xbridge->netflow);
5038 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
5039 entry->u.nf.iface = xout->nf_output_iface;
5044 ofpbuf_uninit(&ctx.stack);
5045 ofpbuf_uninit(&ctx.action_set);
5048 /* Clear the metadata and register wildcard masks, because we won't
5049 * use non-header fields as part of the cache. */
5050 flow_wildcards_clear_non_packet_fields(wc);
5052 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
5053 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
5054 * represent these fields. The datapath interface, on the other hand,
5055 * represents them with just 8 bits each. This means that if the high
5056 * 8 bits of the masks for these fields somehow become set, then they
5057 * will get chopped off by a round trip through the datapath, and
5058 * revalidation will spot that as an inconsistency and delete the flow.
5059 * Avoid the problem here by making sure that only the low 8 bits of
5060 * either field can be unwildcarded for ICMP.
5063 wc->masks.tp_src &= htons(UINT8_MAX);
5064 wc->masks.tp_dst &= htons(UINT8_MAX);
5066 /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
5067 if (wc->masks.vlan_tci) {
5068 wc->masks.vlan_tci |= htons(VLAN_CFI);
5073 /* Sends 'packet' out 'ofport'.
5074 * May modify 'packet'.
5075 * Returns 0 if successful, otherwise a positive errno value. */
5077 xlate_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
5079 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5080 struct xport *xport;
5081 struct ofpact_output output;
5084 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
5085 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
5086 flow_extract(packet, &flow);
5087 flow.in_port.ofp_port = OFPP_NONE;
5089 xport = xport_lookup(xcfg, ofport);
5093 output.port = xport->ofp_port;
5096 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
5097 &output.ofpact, sizeof output,
5101 struct xlate_cache *
5102 xlate_cache_new(void)
5104 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
5106 ofpbuf_init(&xcache->entries, 512);
5110 static struct xc_entry *
5111 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
5113 struct xc_entry *entry;
5115 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
5122 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
5124 if (entry->u.dev.tx) {
5125 netdev_vport_inc_tx(entry->u.dev.tx, stats);
5127 if (entry->u.dev.rx) {
5128 netdev_vport_inc_rx(entry->u.dev.rx, stats);
5130 if (entry->u.dev.bfd) {
5131 bfd_account_rx(entry->u.dev.bfd, stats);
5136 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
5138 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
5139 struct xbridge *xbridge;
5140 struct xbundle *xbundle;
5141 struct flow_wildcards wc;
5143 xbridge = xbridge_lookup(xcfg, ofproto);
5148 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
5154 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
5157 /* Push stats and perform side effects of flow translation. */
5159 xlate_push_stats(struct xlate_cache *xcache,
5160 const struct dpif_flow_stats *stats)
5162 struct xc_entry *entry;
5163 struct ofpbuf entries = xcache->entries;
5164 uint8_t dmac[ETH_ADDR_LEN];
5166 if (!stats->n_packets) {
5170 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5171 switch (entry->type) {
5173 rule_dpif_credit_stats(entry->u.rule, stats);
5176 bond_account(entry->u.bond.bond, entry->u.bond.flow,
5177 entry->u.bond.vid, stats->n_bytes);
5180 xlate_cache_netdev(entry, stats);
5183 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
5184 entry->u.nf.iface, stats);
5187 mirror_update_stats(entry->u.mirror.mbridge,
5188 entry->u.mirror.mirrors,
5189 stats->n_packets, stats->n_bytes);
5192 ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
5195 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
5196 entry->u.normal.vlan);
5198 case XC_FIN_TIMEOUT:
5199 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
5200 entry->u.fin.idle, entry->u.fin.hard);
5203 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
5207 /* Lookup arp to avoid arp timeout. */
5208 tnl_arp_lookup(entry->u.tnl_arp_cache.br_name, entry->u.tnl_arp_cache.d_ip, dmac);
5217 xlate_dev_unref(struct xc_entry *entry)
5219 if (entry->u.dev.tx) {
5220 netdev_close(entry->u.dev.tx);
5222 if (entry->u.dev.rx) {
5223 netdev_close(entry->u.dev.rx);
5225 if (entry->u.dev.bfd) {
5226 bfd_unref(entry->u.dev.bfd);
5231 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
5233 netflow_flow_clear(netflow, flow);
5234 netflow_unref(netflow);
5239 xlate_cache_clear(struct xlate_cache *xcache)
5241 struct xc_entry *entry;
5242 struct ofpbuf entries;
5248 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5249 switch (entry->type) {
5251 rule_dpif_unref(entry->u.rule);
5254 free(entry->u.bond.flow);
5255 bond_unref(entry->u.bond.bond);
5258 xlate_dev_unref(entry);
5261 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
5264 mbridge_unref(entry->u.mirror.mbridge);
5267 free(entry->u.learn.fm);
5268 ofpbuf_delete(entry->u.learn.ofpacts);
5271 free(entry->u.normal.flow);
5273 case XC_FIN_TIMEOUT:
5274 /* 'u.fin.rule' is always already held as a XC_RULE, which
5275 * has already released it's reference above. */
5278 group_dpif_unref(entry->u.group.group);
5287 ofpbuf_clear(&xcache->entries);
5291 xlate_cache_delete(struct xlate_cache *xcache)
5293 xlate_cache_clear(xcache);
5294 ofpbuf_uninit(&xcache->entries);