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"
41 #include "mac-learning.h"
42 #include "mcast-snooping.h"
43 #include "meta-flow.h"
44 #include "multipath.h"
45 #include "netdev-vport.h"
48 #include "odp-execute.h"
49 #include "ofp-actions.h"
50 #include "ofproto/ofproto-dpif-ipfix.h"
51 #include "ofproto/ofproto-dpif-mirror.h"
52 #include "ofproto/ofproto-dpif-monitor.h"
53 #include "ofproto/ofproto-dpif-sflow.h"
54 #include "ofproto/ofproto-dpif.h"
55 #include "ofproto/ofproto-provider.h"
56 #include "ovs-router.h"
57 #include "tnl-ports.h"
59 #include "openvswitch/vlog.h"
61 COVERAGE_DEFINE(xlate_actions);
62 COVERAGE_DEFINE(xlate_actions_oversize);
63 COVERAGE_DEFINE(xlate_actions_too_many_output);
65 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
67 /* Maximum depth of flow table recursion (due to resubmit actions) in a
68 * flow translation. */
69 #define MAX_RESUBMIT_RECURSION 64
70 #define MAX_INTERNAL_RESUBMITS 1 /* Max resbmits allowed using rules in
73 /* Timeout for internal rules created to handle recirculation */
74 #define RECIRC_TIMEOUT 60
76 /* Maximum number of resubmit actions in a flow translation, whether they are
77 * recursive or not. */
78 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
81 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
82 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
84 struct ovs_list xbundles; /* Owned xbundles. */
85 struct hmap xports; /* Indexed by ofp_port. */
87 char *name; /* Name used in log messages. */
88 struct dpif *dpif; /* Datapath interface. */
89 struct mac_learning *ml; /* Mac learning handle. */
90 struct mcast_snooping *ms; /* Multicast Snooping handle. */
91 struct mbridge *mbridge; /* Mirroring. */
92 struct dpif_sflow *sflow; /* SFlow handle, or null. */
93 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
94 struct netflow *netflow; /* Netflow handle, or null. */
95 struct stp *stp; /* STP or null if disabled. */
96 struct rstp *rstp; /* RSTP or null if disabled. */
98 bool has_in_band; /* Bridge has in band control? */
99 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
101 /* True if the datapath supports recirculation. */
104 /* True if the datapath supports variable-length
105 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
106 * False if the datapath supports only 8-byte (or shorter) userdata. */
107 bool variable_length_userdata;
109 /* Number of MPLS label stack entries that the datapath supports
111 size_t max_mpls_depth;
113 /* True if the datapath supports masked data in OVS_ACTION_ATTR_SET
115 bool masked_set_action;
119 struct hmap_node hmap_node; /* In global 'xbundles' map. */
120 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
122 struct ovs_list list_node; /* In parent 'xbridges' list. */
123 struct xbridge *xbridge; /* Parent xbridge. */
125 struct ovs_list xports; /* Contains "struct xport"s. */
127 char *name; /* Name used in log messages. */
128 struct bond *bond; /* Nonnull iff more than one port. */
129 struct lacp *lacp; /* LACP handle or null. */
131 enum port_vlan_mode vlan_mode; /* VLAN mode. */
132 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
133 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
134 * NULL if all VLANs are trunked. */
135 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
136 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
140 struct hmap_node hmap_node; /* Node in global 'xports' map. */
141 struct ofport_dpif *ofport; /* Key in global 'xports map. */
143 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
144 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
146 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
148 struct ovs_list bundle_node; /* In parent xbundle (if it exists). */
149 struct xbundle *xbundle; /* Parent xbundle or null. */
151 struct netdev *netdev; /* 'ofport''s netdev. */
153 struct xbridge *xbridge; /* Parent bridge. */
154 struct xport *peer; /* Patch port peer or null. */
156 enum ofputil_port_config config; /* OpenFlow port configuration. */
157 enum ofputil_port_state state; /* OpenFlow port state. */
158 int stp_port_no; /* STP port number or -1 if not in use. */
159 struct rstp_port *rstp_port; /* RSTP port or null. */
161 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
163 bool may_enable; /* May be enabled in bonds. */
164 bool is_tunnel; /* Is a tunnel port. */
166 struct cfm *cfm; /* CFM handle or null. */
167 struct bfd *bfd; /* BFD handle or null. */
171 struct xlate_in *xin;
172 struct xlate_out *xout;
174 const struct xbridge *xbridge;
176 /* Flow at the last commit. */
177 struct flow base_flow;
179 /* Tunnel IP destination address as received. This is stored separately
180 * as the base_flow.tunnel is cleared on init to reflect the datapath
181 * behavior. Used to make sure not to send tunneled output to ourselves,
182 * which might lead to an infinite loop. This could happen easily
183 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
184 * actually set the tun_dst field. */
185 ovs_be32 orig_tunnel_ip_dst;
187 /* Stack for the push and pop actions. Each stack element is of type
188 * "union mf_subvalue". */
189 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
192 /* The rule that we are currently translating, or NULL. */
193 struct rule_dpif *rule;
195 /* Resubmit statistics, via xlate_table_action(). */
196 int recurse; /* Current resubmit nesting depth. */
197 int resubmits; /* Total number of resubmits. */
198 bool in_group; /* Currently translating ofgroup, if true. */
199 bool in_action_set; /* Currently translating action_set, if true. */
201 uint32_t orig_skb_priority; /* Priority when packet arrived. */
202 uint8_t table_id; /* OpenFlow table ID where flow was found. */
203 uint32_t sflow_n_outputs; /* Number of output ports. */
204 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
205 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
206 bool exit; /* No further actions should be processed. */
208 bool use_recirc; /* Should generate recirc? */
209 struct xlate_recirc recirc; /* Information used for generating
210 * recirculation actions */
212 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
213 * This is a trigger for recirculation in cases where translating an action
214 * or looking up a flow requires access to the fields of the packet after
215 * the MPLS label stack that was originally present. */
218 /* OpenFlow 1.1+ action set.
220 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
221 * When translation is otherwise complete, ofpacts_execute_action_set()
222 * converts it to a set of "struct ofpact"s that can be translated into
223 * datapath actions. */
224 bool action_set_has_group; /* Action set contains OFPACT_GROUP? */
225 struct ofpbuf action_set; /* Action set. */
226 uint64_t action_set_stub[1024 / 8];
229 /* A controller may use OFPP_NONE as the ingress port to indicate that
230 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
231 * when an input bundle is needed for validation (e.g., mirroring or
232 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
233 * any 'port' structs, so care must be taken when dealing with it. */
234 static struct xbundle ofpp_none_bundle = {
236 .vlan_mode = PORT_VLAN_TRUNK
239 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
240 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
241 * traffic egressing the 'ofport' with that priority should be marked with. */
242 struct skb_priority_to_dscp {
243 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
244 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
246 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
262 /* xlate_cache entries hold enough information to perform the side effects of
263 * xlate_actions() for a rule, without needing to perform rule translation
264 * from scratch. The primary usage of these is to submit statistics to objects
265 * that a flow relates to, although they may be used for other effects as well
266 * (for instance, refreshing hard timeouts for learned flows). */
270 struct rule_dpif *rule;
277 struct netflow *netflow;
282 struct mbridge *mbridge;
283 mirror_mask_t mirrors;
291 struct ofproto_dpif *ofproto;
292 struct ofputil_flow_mod *fm;
293 struct ofpbuf *ofpacts;
296 struct ofproto_dpif *ofproto;
301 struct rule_dpif *rule;
306 struct group_dpif *group;
307 struct ofputil_bucket *bucket;
310 char br_name[IFNAMSIZ];
316 #define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
317 entries = xcache->entries; \
318 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
320 entry = ofpbuf_try_pull(&entries, sizeof *entry))
323 struct ofpbuf entries;
326 /* Xlate config contains hash maps of all bridges, bundles and ports.
327 * Xcfgp contains the pointer to the current xlate configuration.
328 * When the main thread needs to change the configuration, it copies xcfgp to
329 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
330 * does not block handler and revalidator threads. */
332 struct hmap xbridges;
333 struct hmap xbundles;
336 static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
337 static struct xlate_cfg *new_xcfg = NULL;
339 static bool may_receive(const struct xport *, struct xlate_ctx *);
340 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
342 static void xlate_normal(struct xlate_ctx *);
343 static inline void xlate_report(struct xlate_ctx *, const char *);
344 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
345 uint8_t table_id, bool may_packet_in,
346 bool honor_table_miss);
347 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
348 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
349 static void output_normal(struct xlate_ctx *, const struct xbundle *,
351 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
353 static struct xbridge *xbridge_lookup(struct xlate_cfg *,
354 const struct ofproto_dpif *);
355 static struct xbundle *xbundle_lookup(struct xlate_cfg *,
356 const struct ofbundle *);
357 static struct xport *xport_lookup(struct xlate_cfg *,
358 const struct ofport_dpif *);
359 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
360 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
361 uint32_t skb_priority);
362 static void clear_skb_priorities(struct xport *);
363 static size_t count_skb_priorities(const struct xport *);
364 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
367 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
369 static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
370 static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
371 static void xlate_xport_init(struct xlate_cfg *, struct xport *);
372 static void xlate_xbridge_set(struct xbridge *, struct dpif *,
373 const struct mac_learning *, struct stp *,
374 struct rstp *, const struct mcast_snooping *,
375 const struct mbridge *,
376 const struct dpif_sflow *,
377 const struct dpif_ipfix *,
378 const struct netflow *,
379 bool forward_bpdu, bool has_in_band,
381 bool variable_length_userdata,
382 size_t max_mpls_depth,
383 bool masked_set_action);
384 static void xlate_xbundle_set(struct xbundle *xbundle,
385 enum port_vlan_mode vlan_mode, int vlan,
386 unsigned long *trunks, bool use_priority_tags,
387 const struct bond *bond, const struct lacp *lacp,
389 static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
390 const struct netdev *netdev, const struct cfm *cfm,
391 const struct bfd *bfd, int stp_port_no,
392 const struct rstp_port *rstp_port,
393 enum ofputil_port_config config,
394 enum ofputil_port_state state, bool is_tunnel,
396 static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
397 static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
398 static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
399 static void xlate_xbridge_copy(struct xbridge *);
400 static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
401 static void xlate_xport_copy(struct xbridge *, struct xbundle *,
403 static void xlate_xcfg_free(struct xlate_cfg *);
406 xlate_report(struct xlate_ctx *ctx, const char *s)
408 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
409 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
414 xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
416 list_init(&xbridge->xbundles);
417 hmap_init(&xbridge->xports);
418 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
419 hash_pointer(xbridge->ofproto, 0));
423 xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
425 list_init(&xbundle->xports);
426 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
427 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
428 hash_pointer(xbundle->ofbundle, 0));
432 xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
434 hmap_init(&xport->skb_priorities);
435 hmap_insert(&xcfg->xports, &xport->hmap_node,
436 hash_pointer(xport->ofport, 0));
437 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
438 hash_ofp_port(xport->ofp_port));
442 xlate_xbridge_set(struct xbridge *xbridge,
444 const struct mac_learning *ml, struct stp *stp,
445 struct rstp *rstp, const struct mcast_snooping *ms,
446 const struct mbridge *mbridge,
447 const struct dpif_sflow *sflow,
448 const struct dpif_ipfix *ipfix,
449 const struct netflow *netflow,
450 bool forward_bpdu, bool has_in_band,
452 bool variable_length_userdata,
453 size_t max_mpls_depth,
454 bool masked_set_action)
456 if (xbridge->ml != ml) {
457 mac_learning_unref(xbridge->ml);
458 xbridge->ml = mac_learning_ref(ml);
461 if (xbridge->ms != ms) {
462 mcast_snooping_unref(xbridge->ms);
463 xbridge->ms = mcast_snooping_ref(ms);
466 if (xbridge->mbridge != mbridge) {
467 mbridge_unref(xbridge->mbridge);
468 xbridge->mbridge = mbridge_ref(mbridge);
471 if (xbridge->sflow != sflow) {
472 dpif_sflow_unref(xbridge->sflow);
473 xbridge->sflow = dpif_sflow_ref(sflow);
476 if (xbridge->ipfix != ipfix) {
477 dpif_ipfix_unref(xbridge->ipfix);
478 xbridge->ipfix = dpif_ipfix_ref(ipfix);
481 if (xbridge->stp != stp) {
482 stp_unref(xbridge->stp);
483 xbridge->stp = stp_ref(stp);
486 if (xbridge->rstp != rstp) {
487 rstp_unref(xbridge->rstp);
488 xbridge->rstp = rstp_ref(rstp);
491 if (xbridge->netflow != netflow) {
492 netflow_unref(xbridge->netflow);
493 xbridge->netflow = netflow_ref(netflow);
496 xbridge->dpif = dpif;
497 xbridge->forward_bpdu = forward_bpdu;
498 xbridge->has_in_band = has_in_band;
499 xbridge->enable_recirc = enable_recirc;
500 xbridge->variable_length_userdata = variable_length_userdata;
501 xbridge->max_mpls_depth = max_mpls_depth;
502 xbridge->masked_set_action = masked_set_action;
506 xlate_xbundle_set(struct xbundle *xbundle,
507 enum port_vlan_mode vlan_mode, int vlan,
508 unsigned long *trunks, bool use_priority_tags,
509 const struct bond *bond, const struct lacp *lacp,
512 ovs_assert(xbundle->xbridge);
514 xbundle->vlan_mode = vlan_mode;
515 xbundle->vlan = vlan;
516 xbundle->trunks = trunks;
517 xbundle->use_priority_tags = use_priority_tags;
518 xbundle->floodable = floodable;
520 if (xbundle->bond != bond) {
521 bond_unref(xbundle->bond);
522 xbundle->bond = bond_ref(bond);
525 if (xbundle->lacp != lacp) {
526 lacp_unref(xbundle->lacp);
527 xbundle->lacp = lacp_ref(lacp);
532 xlate_xport_set(struct xport *xport, odp_port_t odp_port,
533 const struct netdev *netdev, const struct cfm *cfm,
534 const struct bfd *bfd, int stp_port_no,
535 const struct rstp_port* rstp_port,
536 enum ofputil_port_config config, enum ofputil_port_state state,
537 bool is_tunnel, bool may_enable)
539 xport->config = config;
540 xport->state = state;
541 xport->stp_port_no = stp_port_no;
542 xport->is_tunnel = is_tunnel;
543 xport->may_enable = may_enable;
544 xport->odp_port = odp_port;
546 if (xport->rstp_port != rstp_port) {
547 rstp_port_unref(xport->rstp_port);
548 xport->rstp_port = rstp_port_ref(rstp_port);
551 if (xport->cfm != cfm) {
552 cfm_unref(xport->cfm);
553 xport->cfm = cfm_ref(cfm);
556 if (xport->bfd != bfd) {
557 bfd_unref(xport->bfd);
558 xport->bfd = bfd_ref(bfd);
561 if (xport->netdev != netdev) {
562 netdev_close(xport->netdev);
563 xport->netdev = netdev_ref(netdev);
568 xlate_xbridge_copy(struct xbridge *xbridge)
570 struct xbundle *xbundle;
572 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
573 new_xbridge->ofproto = xbridge->ofproto;
574 new_xbridge->name = xstrdup(xbridge->name);
575 xlate_xbridge_init(new_xcfg, new_xbridge);
577 xlate_xbridge_set(new_xbridge,
578 xbridge->dpif, xbridge->ml, xbridge->stp,
579 xbridge->rstp, xbridge->ms, xbridge->mbridge,
580 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
581 xbridge->forward_bpdu,
582 xbridge->has_in_band, xbridge->enable_recirc,
583 xbridge->variable_length_userdata,
584 xbridge->max_mpls_depth, xbridge->masked_set_action);
585 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
586 xlate_xbundle_copy(new_xbridge, xbundle);
589 /* Copy xports which are not part of a xbundle */
590 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
591 if (!xport->xbundle) {
592 xlate_xport_copy(new_xbridge, NULL, xport);
598 xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
601 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
602 new_xbundle->ofbundle = xbundle->ofbundle;
603 new_xbundle->xbridge = xbridge;
604 new_xbundle->name = xstrdup(xbundle->name);
605 xlate_xbundle_init(new_xcfg, new_xbundle);
607 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
608 xbundle->vlan, xbundle->trunks,
609 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
611 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
612 xlate_xport_copy(xbridge, new_xbundle, xport);
617 xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
620 struct skb_priority_to_dscp *pdscp, *new_pdscp;
621 struct xport *new_xport = xzalloc(sizeof *xport);
622 new_xport->ofport = xport->ofport;
623 new_xport->ofp_port = xport->ofp_port;
624 new_xport->xbridge = xbridge;
625 xlate_xport_init(new_xcfg, new_xport);
627 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
628 xport->bfd, xport->stp_port_no, xport->rstp_port,
629 xport->config, xport->state, xport->is_tunnel,
633 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
635 new_xport->peer = peer;
636 new_xport->peer->peer = new_xport;
641 new_xport->xbundle = xbundle;
642 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
645 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
646 new_pdscp = xmalloc(sizeof *pdscp);
647 new_pdscp->skb_priority = pdscp->skb_priority;
648 new_pdscp->dscp = pdscp->dscp;
649 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
650 hash_int(new_pdscp->skb_priority, 0));
654 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
655 * configuration in xcfgp.
657 * This needs to be called after editing the xlate configuration.
659 * Functions that edit the new xlate configuration are
660 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
666 * edit_xlate_configuration();
668 * xlate_txn_commit(); */
670 xlate_txn_commit(void)
672 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
674 ovsrcu_set(&xcfgp, new_xcfg);
675 ovsrcu_synchronize();
676 xlate_xcfg_free(xcfg);
680 /* Copies the current xlate configuration in xcfgp to new_xcfg.
682 * This needs to be called prior to editing the xlate configuration. */
684 xlate_txn_start(void)
686 struct xbridge *xbridge;
687 struct xlate_cfg *xcfg;
689 ovs_assert(!new_xcfg);
691 new_xcfg = xmalloc(sizeof *new_xcfg);
692 hmap_init(&new_xcfg->xbridges);
693 hmap_init(&new_xcfg->xbundles);
694 hmap_init(&new_xcfg->xports);
696 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
701 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
702 xlate_xbridge_copy(xbridge);
708 xlate_xcfg_free(struct xlate_cfg *xcfg)
710 struct xbridge *xbridge, *next_xbridge;
716 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
717 xlate_xbridge_remove(xcfg, xbridge);
720 hmap_destroy(&xcfg->xbridges);
721 hmap_destroy(&xcfg->xbundles);
722 hmap_destroy(&xcfg->xports);
727 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
729 const struct mac_learning *ml, struct stp *stp,
730 struct rstp *rstp, const struct mcast_snooping *ms,
731 const struct mbridge *mbridge,
732 const struct dpif_sflow *sflow,
733 const struct dpif_ipfix *ipfix,
734 const struct netflow *netflow,
735 bool forward_bpdu, bool has_in_band, bool enable_recirc,
736 bool variable_length_userdata, size_t max_mpls_depth,
737 bool masked_set_action)
739 struct xbridge *xbridge;
741 ovs_assert(new_xcfg);
743 xbridge = xbridge_lookup(new_xcfg, ofproto);
745 xbridge = xzalloc(sizeof *xbridge);
746 xbridge->ofproto = ofproto;
748 xlate_xbridge_init(new_xcfg, xbridge);
752 xbridge->name = xstrdup(name);
754 xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
755 netflow, forward_bpdu, has_in_band, enable_recirc,
756 variable_length_userdata, max_mpls_depth,
761 xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
763 struct xbundle *xbundle, *next_xbundle;
764 struct xport *xport, *next_xport;
770 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
771 xlate_xport_remove(xcfg, xport);
774 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
775 xlate_xbundle_remove(xcfg, xbundle);
778 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
779 mac_learning_unref(xbridge->ml);
780 mcast_snooping_unref(xbridge->ms);
781 mbridge_unref(xbridge->mbridge);
782 dpif_sflow_unref(xbridge->sflow);
783 dpif_ipfix_unref(xbridge->ipfix);
784 stp_unref(xbridge->stp);
785 rstp_unref(xbridge->rstp);
786 hmap_destroy(&xbridge->xports);
792 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
794 struct xbridge *xbridge;
796 ovs_assert(new_xcfg);
798 xbridge = xbridge_lookup(new_xcfg, ofproto);
799 xlate_xbridge_remove(new_xcfg, xbridge);
803 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
804 const char *name, enum port_vlan_mode vlan_mode, int vlan,
805 unsigned long *trunks, bool use_priority_tags,
806 const struct bond *bond, const struct lacp *lacp,
809 struct xbundle *xbundle;
811 ovs_assert(new_xcfg);
813 xbundle = xbundle_lookup(new_xcfg, ofbundle);
815 xbundle = xzalloc(sizeof *xbundle);
816 xbundle->ofbundle = ofbundle;
817 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
819 xlate_xbundle_init(new_xcfg, xbundle);
823 xbundle->name = xstrdup(name);
825 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
826 use_priority_tags, bond, lacp, floodable);
830 xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
832 struct xport *xport, *next;
838 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
839 list_remove(&xport->bundle_node);
840 xport->xbundle = NULL;
843 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
844 list_remove(&xbundle->list_node);
845 bond_unref(xbundle->bond);
846 lacp_unref(xbundle->lacp);
852 xlate_bundle_remove(struct ofbundle *ofbundle)
854 struct xbundle *xbundle;
856 ovs_assert(new_xcfg);
858 xbundle = xbundle_lookup(new_xcfg, ofbundle);
859 xlate_xbundle_remove(new_xcfg, xbundle);
863 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
864 struct ofport_dpif *ofport, ofp_port_t ofp_port,
865 odp_port_t odp_port, const struct netdev *netdev,
866 const struct cfm *cfm, const struct bfd *bfd,
867 struct ofport_dpif *peer, int stp_port_no,
868 const struct rstp_port *rstp_port,
869 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
870 enum ofputil_port_config config,
871 enum ofputil_port_state state, bool is_tunnel,
877 ovs_assert(new_xcfg);
879 xport = xport_lookup(new_xcfg, ofport);
881 xport = xzalloc(sizeof *xport);
882 xport->ofport = ofport;
883 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
884 xport->ofp_port = ofp_port;
886 xlate_xport_init(new_xcfg, xport);
889 ovs_assert(xport->ofp_port == ofp_port);
891 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, stp_port_no,
892 rstp_port, config, state, is_tunnel, may_enable);
895 xport->peer->peer = NULL;
897 xport->peer = xport_lookup(new_xcfg, peer);
899 xport->peer->peer = xport;
902 if (xport->xbundle) {
903 list_remove(&xport->bundle_node);
905 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
906 if (xport->xbundle) {
907 list_insert(&xport->xbundle->xports, &xport->bundle_node);
910 clear_skb_priorities(xport);
911 for (i = 0; i < n_qdscp; i++) {
912 struct skb_priority_to_dscp *pdscp;
913 uint32_t skb_priority;
915 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
920 pdscp = xmalloc(sizeof *pdscp);
921 pdscp->skb_priority = skb_priority;
922 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
923 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
924 hash_int(pdscp->skb_priority, 0));
929 xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
936 xport->peer->peer = NULL;
940 if (xport->xbundle) {
941 list_remove(&xport->bundle_node);
944 clear_skb_priorities(xport);
945 hmap_destroy(&xport->skb_priorities);
947 hmap_remove(&xcfg->xports, &xport->hmap_node);
948 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
950 netdev_close(xport->netdev);
951 rstp_port_unref(xport->rstp_port);
952 cfm_unref(xport->cfm);
953 bfd_unref(xport->bfd);
958 xlate_ofport_remove(struct ofport_dpif *ofport)
962 ovs_assert(new_xcfg);
964 xport = xport_lookup(new_xcfg, ofport);
965 xlate_xport_remove(new_xcfg, xport);
968 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
969 * returns the corresponding struct xport, or NULL if none is found. */
970 static struct xport *
971 xlate_lookup_xport(const struct dpif_backer *backer, const struct flow *flow)
973 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
975 return xport_lookup(xcfg, tnl_port_should_receive(flow)
976 ? tnl_port_receive(flow)
977 : odp_port_to_ofport(backer, flow->in_port.odp_port));
980 static struct ofproto_dpif *
981 xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
982 ofp_port_t *ofp_in_port, const struct xport **xportp)
984 struct ofproto_dpif *recv_ofproto = NULL;
985 struct ofproto_dpif *recirc_ofproto = NULL;
986 const struct xport *xport;
987 ofp_port_t in_port = OFPP_NONE;
989 *xportp = xport = xlate_lookup_xport(backer, flow);
992 recv_ofproto = xport->xbridge->ofproto;
993 in_port = xport->ofp_port;
996 /* When recirc_id is set in 'flow', checks whether the ofproto_dpif that
997 * corresponds to the recirc_id is same as the receiving bridge. If they
998 * are the same, uses the 'recv_ofproto' and keeps the 'ofp_in_port' as
999 * assigned. Otherwise, uses the 'recirc_ofproto' that owns recirc_id and
1000 * assigns OFPP_NONE to 'ofp_in_port'. Doing this is in that, the
1001 * recirculated flow must be processced by the ofproto which originates
1002 * the recirculation, and as bridges can only see their own ports, the
1003 * in_port of the 'recv_ofproto' should not be passed to the
1006 * Admittedly, setting the 'ofp_in_port' to OFPP_NONE limits the
1007 * 'recirc_ofproto' from meaningfully matching on in_port of recirculated
1008 * flow, and should be fixed in the near future.
1010 * TODO: Restore the original patch port.
1012 if (recv_ofproto && flow->recirc_id) {
1013 recirc_ofproto = ofproto_dpif_recirc_get_ofproto(backer,
1015 if (recv_ofproto != recirc_ofproto) {
1016 *xportp = xport = NULL;
1017 in_port = OFPP_NONE;
1022 *ofp_in_port = in_port;
1025 return xport ? recv_ofproto : recirc_ofproto;
1028 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1029 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1030 struct ofproto_dpif *
1031 xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1032 ofp_port_t *ofp_in_port)
1034 const struct xport *xport;
1036 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1039 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1040 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
1041 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1042 * handles for those protocols if they're enabled. Caller may use the returned
1043 * pointers until quiescing, for longer term use additional references must
1046 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1049 xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1050 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1051 struct dpif_sflow **sflow, struct netflow **netflow,
1052 ofp_port_t *ofp_in_port)
1054 struct ofproto_dpif *ofproto;
1055 const struct xport *xport;
1057 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1064 *ofprotop = ofproto;
1068 *ipfix = xport ? xport->xbridge->ipfix : NULL;
1072 *sflow = xport ? xport->xbridge->sflow : NULL;
1076 *netflow = xport ? xport->xbridge->netflow : NULL;
1082 static struct xbridge *
1083 xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
1085 struct hmap *xbridges;
1086 struct xbridge *xbridge;
1088 if (!ofproto || !xcfg) {
1092 xbridges = &xcfg->xbridges;
1094 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
1096 if (xbridge->ofproto == ofproto) {
1103 static struct xbundle *
1104 xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
1106 struct hmap *xbundles;
1107 struct xbundle *xbundle;
1109 if (!ofbundle || !xcfg) {
1113 xbundles = &xcfg->xbundles;
1115 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
1117 if (xbundle->ofbundle == ofbundle) {
1124 static struct xport *
1125 xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
1127 struct hmap *xports;
1128 struct xport *xport;
1130 if (!ofport || !xcfg) {
1134 xports = &xcfg->xports;
1136 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
1138 if (xport->ofport == ofport) {
1145 static struct stp_port *
1146 xport_get_stp_port(const struct xport *xport)
1148 return xport->xbridge->stp && xport->stp_port_no != -1
1149 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1154 xport_stp_learn_state(const struct xport *xport)
1156 struct stp_port *sp = xport_get_stp_port(xport);
1158 ? stp_learn_in_state(stp_port_get_state(sp))
1163 xport_stp_forward_state(const struct xport *xport)
1165 struct stp_port *sp = xport_get_stp_port(xport);
1167 ? stp_forward_in_state(stp_port_get_state(sp))
1172 xport_stp_should_forward_bpdu(const struct xport *xport)
1174 struct stp_port *sp = xport_get_stp_port(xport);
1175 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
1178 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1179 * were used to make the determination.*/
1181 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1183 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1184 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1185 return is_stp(flow);
1189 stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1191 struct stp_port *sp = xport_get_stp_port(xport);
1192 struct dp_packet payload = *packet;
1193 struct eth_header *eth = dp_packet_data(&payload);
1195 /* Sink packets on ports that have STP disabled when the bridge has
1197 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1201 /* Trim off padding on payload. */
1202 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1203 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1206 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1207 stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
1211 static enum rstp_state
1212 xport_get_rstp_port_state(const struct xport *xport)
1214 return xport->rstp_port
1215 ? rstp_port_get_state(xport->rstp_port)
1220 xport_rstp_learn_state(const struct xport *xport)
1222 return xport->xbridge->rstp && xport->rstp_port
1223 ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1228 xport_rstp_forward_state(const struct xport *xport)
1230 return xport->xbridge->rstp && xport->rstp_port
1231 ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1236 xport_rstp_should_manage_bpdu(const struct xport *xport)
1238 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
1242 rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1244 struct dp_packet payload = *packet;
1245 struct eth_header *eth = dp_packet_data(&payload);
1247 /* Sink packets on ports that have no RSTP. */
1248 if (!xport->rstp_port) {
1252 /* Trim off padding on payload. */
1253 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1254 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1257 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1258 rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
1259 dp_packet_size(&payload));
1263 static struct xport *
1264 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1266 struct xport *xport;
1268 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1270 if (xport->ofp_port == ofp_port) {
1278 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1280 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1281 return xport ? xport->odp_port : ODPP_NONE;
1285 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1287 struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1288 return xport && xport->may_enable;
1291 static struct ofputil_bucket *
1292 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1296 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1298 struct group_dpif *group;
1300 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1301 struct ofputil_bucket *bucket;
1303 bucket = group_first_live_bucket(ctx, group, depth);
1304 group_dpif_unref(group);
1305 return bucket == NULL;
1311 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1314 bucket_is_alive(const struct xlate_ctx *ctx,
1315 struct ofputil_bucket *bucket, int depth)
1317 if (depth >= MAX_LIVENESS_RECURSION) {
1318 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1320 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1321 MAX_LIVENESS_RECURSION);
1325 return (!ofputil_bucket_has_liveness(bucket)
1326 || (bucket->watch_port != OFPP_ANY
1327 && odp_port_is_alive(ctx, bucket->watch_port))
1328 || (bucket->watch_group != OFPG_ANY
1329 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
1332 static struct ofputil_bucket *
1333 group_first_live_bucket(const struct xlate_ctx *ctx,
1334 const struct group_dpif *group, int depth)
1336 struct ofputil_bucket *bucket;
1337 const struct ovs_list *buckets;
1339 group_dpif_get_buckets(group, &buckets);
1340 LIST_FOR_EACH (bucket, list_node, buckets) {
1341 if (bucket_is_alive(ctx, bucket, depth)) {
1349 static struct ofputil_bucket *
1350 group_best_live_bucket(const struct xlate_ctx *ctx,
1351 const struct group_dpif *group,
1354 struct ofputil_bucket *best_bucket = NULL;
1355 uint32_t best_score = 0;
1358 struct ofputil_bucket *bucket;
1359 const struct ovs_list *buckets;
1361 group_dpif_get_buckets(group, &buckets);
1362 LIST_FOR_EACH (bucket, list_node, buckets) {
1363 if (bucket_is_alive(ctx, bucket, 0)) {
1364 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
1365 if (score >= best_score) {
1366 best_bucket = bucket;
1377 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
1379 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1380 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1384 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1386 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1389 static mirror_mask_t
1390 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1392 return xbundle != &ofpp_none_bundle
1393 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1397 static mirror_mask_t
1398 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
1400 return xbundle != &ofpp_none_bundle
1401 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1405 static mirror_mask_t
1406 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
1408 return xbundle != &ofpp_none_bundle
1409 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1413 static struct xbundle *
1414 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1415 bool warn, struct xport **in_xportp)
1417 struct xport *xport;
1419 /* Find the port and bundle for the received packet. */
1420 xport = get_ofp_port(xbridge, in_port);
1424 if (xport && xport->xbundle) {
1425 return xport->xbundle;
1428 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1429 * which a controller may use as the ingress port for traffic that
1430 * it is sourcing. */
1431 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
1432 return &ofpp_none_bundle;
1435 /* Odd. A few possible reasons here:
1437 * - We deleted a port but there are still a few packets queued up
1440 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1441 * we don't know about.
1443 * - The ofproto client didn't configure the port as part of a bundle.
1444 * This is particularly likely to happen if a packet was received on the
1445 * port after it was created, but before the client had a chance to
1446 * configure its bundle.
1449 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1451 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1452 "port %"PRIu16, xbridge->name, in_port);
1458 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
1460 const struct xbridge *xbridge = ctx->xbridge;
1461 mirror_mask_t mirrors;
1462 struct xbundle *in_xbundle;
1466 mirrors = ctx->xout->mirrors;
1467 ctx->xout->mirrors = 0;
1469 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
1470 ctx->xin->packet != NULL, NULL);
1474 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
1476 /* Drop frames on bundles reserved for mirroring. */
1477 if (xbundle_mirror_out(xbridge, in_xbundle)) {
1478 if (ctx->xin->packet != NULL) {
1479 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1480 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1481 "%s, which is reserved exclusively for mirroring",
1482 ctx->xbridge->name, in_xbundle->name);
1484 ofpbuf_clear(ctx->xout->odp_actions);
1489 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
1490 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1493 vlan = input_vid_to_vlan(in_xbundle, vid);
1499 /* Restore the original packet before adding the mirror actions. */
1500 ctx->xin->flow = *orig_flow;
1503 mirror_mask_t dup_mirrors;
1504 struct ofbundle *out;
1505 unsigned long *vlans;
1510 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1511 &vlans, &dup_mirrors, &out, &out_vlan);
1512 ovs_assert(has_mirror);
1515 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1517 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
1520 if (!vlan_mirrored) {
1521 mirrors = zero_rightmost_1bit(mirrors);
1525 mirrors &= ~dup_mirrors;
1526 ctx->xout->mirrors |= dup_mirrors;
1528 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1529 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1531 output_normal(ctx, out_xbundle, vlan);
1533 } else if (vlan != out_vlan
1534 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
1535 struct xbundle *xbundle;
1537 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1538 if (xbundle_includes_vlan(xbundle, out_vlan)
1539 && !xbundle_mirror_out(xbridge, xbundle)) {
1540 output_normal(ctx, xbundle, out_vlan);
1547 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1548 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1549 * the bundle on which the packet was received, returns the VLAN to which the
1552 * Both 'vid' and the return value are in the range 0...4095. */
1554 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1556 switch (in_xbundle->vlan_mode) {
1557 case PORT_VLAN_ACCESS:
1558 return in_xbundle->vlan;
1561 case PORT_VLAN_TRUNK:
1564 case PORT_VLAN_NATIVE_UNTAGGED:
1565 case PORT_VLAN_NATIVE_TAGGED:
1566 return vid ? vid : in_xbundle->vlan;
1573 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1574 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1577 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1578 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1581 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1583 /* Allow any VID on the OFPP_NONE port. */
1584 if (in_xbundle == &ofpp_none_bundle) {
1588 switch (in_xbundle->vlan_mode) {
1589 case PORT_VLAN_ACCESS:
1592 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1593 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1594 "packet received on port %s configured as VLAN "
1595 "%"PRIu16" access port", vid, in_xbundle->name,
1602 case PORT_VLAN_NATIVE_UNTAGGED:
1603 case PORT_VLAN_NATIVE_TAGGED:
1605 /* Port must always carry its native VLAN. */
1609 case PORT_VLAN_TRUNK:
1610 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1612 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1613 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1614 "received on port %s not configured for trunking "
1615 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1627 /* Given 'vlan', the VLAN that a packet belongs to, and
1628 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1629 * that should be included in the 802.1Q header. (If the return value is 0,
1630 * then the 802.1Q header should only be included in the packet if there is a
1633 * Both 'vlan' and the return value are in the range 0...4095. */
1635 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1637 switch (out_xbundle->vlan_mode) {
1638 case PORT_VLAN_ACCESS:
1641 case PORT_VLAN_TRUNK:
1642 case PORT_VLAN_NATIVE_TAGGED:
1645 case PORT_VLAN_NATIVE_UNTAGGED:
1646 return vlan == out_xbundle->vlan ? 0 : vlan;
1654 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1657 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1659 ovs_be16 tci, old_tci;
1660 struct xport *xport;
1662 vid = output_vlan_to_vid(out_xbundle, vlan);
1663 if (list_is_empty(&out_xbundle->xports)) {
1664 /* Partially configured bundle with no slaves. Drop the packet. */
1666 } else if (!out_xbundle->bond) {
1667 ctx->use_recirc = false;
1668 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1671 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1672 struct flow_wildcards *wc = &ctx->xout->wc;
1673 struct xlate_recirc *xr = &ctx->recirc;
1674 struct ofport_dpif *ofport;
1676 if (ctx->xbridge->enable_recirc) {
1677 ctx->use_recirc = bond_may_recirc(
1678 out_xbundle->bond, &xr->recirc_id, &xr->hash_basis);
1680 if (ctx->use_recirc) {
1681 /* Only TCP mode uses recirculation. */
1682 xr->hash_alg = OVS_HASH_ALG_L4;
1683 bond_update_post_recirc_rules(out_xbundle->bond, false);
1685 /* Recirculation does not require unmasking hash fields. */
1690 ofport = bond_choose_output_slave(out_xbundle->bond,
1691 &ctx->xin->flow, wc, vid);
1692 xport = xport_lookup(xcfg, ofport);
1695 /* No slaves enabled, so drop packet. */
1699 /* If ctx->xout->use_recirc is set, the main thread will handle stats
1700 * accounting for this bond. */
1701 if (!ctx->use_recirc) {
1702 if (ctx->xin->resubmit_stats) {
1703 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1704 ctx->xin->resubmit_stats->n_bytes);
1706 if (ctx->xin->xcache) {
1707 struct xc_entry *entry;
1710 flow = &ctx->xin->flow;
1711 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1712 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1713 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1714 entry->u.bond.vid = vid;
1719 old_tci = *flow_tci;
1721 if (tci || out_xbundle->use_priority_tags) {
1722 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1724 tci |= htons(VLAN_CFI);
1729 compose_output_action(ctx, xport->ofp_port);
1730 *flow_tci = old_tci;
1733 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1734 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1735 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1737 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1739 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1743 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1744 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1748 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1749 if (flow->nw_proto == ARP_OP_REPLY) {
1751 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1752 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1753 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1755 return flow->nw_src == flow->nw_dst;
1761 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1762 * dropped. Returns true if they may be forwarded, false if they should be
1765 * 'in_port' must be the xport that corresponds to flow->in_port.
1766 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1768 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1769 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1770 * checked by input_vid_is_valid().
1772 * May also add tags to '*tags', although the current implementation only does
1773 * so in one special case.
1776 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1779 struct xbundle *in_xbundle = in_port->xbundle;
1780 const struct xbridge *xbridge = ctx->xbridge;
1781 struct flow *flow = &ctx->xin->flow;
1783 /* Drop frames for reserved multicast addresses
1784 * only if forward_bpdu option is absent. */
1785 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1786 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1790 if (in_xbundle->bond) {
1791 struct mac_entry *mac;
1793 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1799 xlate_report(ctx, "bonding refused admissibility, dropping");
1802 case BV_DROP_IF_MOVED:
1803 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1804 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1806 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
1807 && (!is_gratuitous_arp(flow, &ctx->xout->wc)
1808 || mac_entry_is_grat_arp_locked(mac))) {
1809 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1810 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1814 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1822 /* Checks whether a MAC learning update is necessary for MAC learning table
1823 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1826 * Most packets processed through the MAC learning table do not actually
1827 * change it in any way. This function requires only a read lock on the MAC
1828 * learning table, so it is much cheaper in this common case.
1830 * Keep the code here synchronized with that in update_learning_table__()
1833 is_mac_learning_update_needed(const struct mac_learning *ml,
1834 const struct flow *flow,
1835 struct flow_wildcards *wc,
1836 int vlan, struct xbundle *in_xbundle)
1837 OVS_REQ_RDLOCK(ml->rwlock)
1839 struct mac_entry *mac;
1841 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1845 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1846 if (!mac || mac_entry_age(ml, mac)) {
1850 if (is_gratuitous_arp(flow, wc)) {
1851 /* We don't want to learn from gratuitous ARP packets that are
1852 * reflected back over bond slaves so we lock the learning table. */
1853 if (!in_xbundle->bond) {
1855 } else if (mac_entry_is_grat_arp_locked(mac)) {
1860 return mac_entry_get_port(ml, mac) != in_xbundle->ofbundle;
1864 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1865 * received on 'in_xbundle' in 'vlan'.
1867 * This code repeats all the checks in is_mac_learning_update_needed() because
1868 * the lock was released between there and here and thus the MAC learning state
1869 * could have changed.
1871 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1874 update_learning_table__(const struct xbridge *xbridge,
1875 const struct flow *flow, struct flow_wildcards *wc,
1876 int vlan, struct xbundle *in_xbundle)
1877 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1879 struct mac_entry *mac;
1881 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1885 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1886 if (is_gratuitous_arp(flow, wc)) {
1887 /* We don't want to learn from gratuitous ARP packets that are
1888 * reflected back over bond slaves so we lock the learning table. */
1889 if (!in_xbundle->bond) {
1890 mac_entry_set_grat_arp_lock(mac);
1891 } else if (mac_entry_is_grat_arp_locked(mac)) {
1896 if (mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle) {
1897 /* The log messages here could actually be useful in debugging,
1898 * so keep the rate limit relatively high. */
1899 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1901 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1902 "on port %s in VLAN %d",
1903 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1904 in_xbundle->name, vlan);
1906 mac_entry_set_port(xbridge->ml, mac, in_xbundle->ofbundle);
1911 update_learning_table(const struct xbridge *xbridge,
1912 const struct flow *flow, struct flow_wildcards *wc,
1913 int vlan, struct xbundle *in_xbundle)
1917 /* Don't learn the OFPP_NONE port. */
1918 if (in_xbundle == &ofpp_none_bundle) {
1922 /* First try the common case: no change to MAC learning table. */
1923 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1924 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1926 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1929 /* Slow path: MAC learning table might need an update. */
1930 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1931 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1932 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1936 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1937 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
1939 update_mcast_snooping_table__(const struct xbridge *xbridge,
1940 const struct flow *flow,
1941 struct mcast_snooping *ms,
1942 ovs_be32 ip4, int vlan,
1943 struct xbundle *in_xbundle)
1944 OVS_REQ_WRLOCK(ms->rwlock)
1946 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
1948 switch (ntohs(flow->tp_src)) {
1949 case IGMP_HOST_MEMBERSHIP_REPORT:
1950 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1951 if (mcast_snooping_add_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
1952 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
1953 IP_FMT" is on port %s in VLAN %d",
1954 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
1957 case IGMP_HOST_LEAVE_MESSAGE:
1958 if (mcast_snooping_leave_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
1959 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
1960 IP_FMT" is on port %s in VLAN %d",
1961 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
1964 case IGMP_HOST_MEMBERSHIP_QUERY:
1965 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
1966 in_xbundle->ofbundle)) {
1967 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
1968 IP_FMT" is on port %s in VLAN %d",
1969 xbridge->name, IP_ARGS(flow->nw_src),
1970 in_xbundle->name, vlan);
1976 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1977 * was received on 'in_xbundle' in 'vlan'. */
1979 update_mcast_snooping_table(const struct xbridge *xbridge,
1980 const struct flow *flow, int vlan,
1981 struct xbundle *in_xbundle)
1983 struct mcast_snooping *ms = xbridge->ms;
1984 struct xlate_cfg *xcfg;
1985 struct xbundle *mcast_xbundle;
1986 struct mcast_port_bundle *fport;
1988 /* Don't learn the OFPP_NONE port. */
1989 if (in_xbundle == &ofpp_none_bundle) {
1993 /* Don't learn from flood ports */
1994 mcast_xbundle = NULL;
1995 ovs_rwlock_wrlock(&ms->rwlock);
1996 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1997 LIST_FOR_EACH(fport, node, &ms->fport_list) {
1998 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
1999 if (mcast_xbundle == in_xbundle) {
2004 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
2005 update_mcast_snooping_table__(xbridge, flow, ms, flow->igmp_group_ip4,
2008 ovs_rwlock_unlock(&ms->rwlock);
2011 /* send the packet to ports having the multicast group learned */
2013 xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2014 struct mcast_snooping *ms OVS_UNUSED,
2015 struct mcast_group *grp,
2016 struct xbundle *in_xbundle, uint16_t vlan)
2017 OVS_REQ_RDLOCK(ms->rwlock)
2019 struct xlate_cfg *xcfg;
2020 struct mcast_group_bundle *b;
2021 struct xbundle *mcast_xbundle;
2023 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2024 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2025 mcast_xbundle = xbundle_lookup(xcfg, b->port);
2026 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2027 xlate_report(ctx, "forwarding to mcast group port");
2028 output_normal(ctx, mcast_xbundle, vlan);
2029 } else if (!mcast_xbundle) {
2030 xlate_report(ctx, "mcast group port is unknown, dropping");
2032 xlate_report(ctx, "mcast group port is input port, dropping");
2037 /* send the packet to ports connected to multicast routers */
2039 xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2040 struct mcast_snooping *ms,
2041 struct xbundle *in_xbundle, uint16_t vlan)
2042 OVS_REQ_RDLOCK(ms->rwlock)
2044 struct xlate_cfg *xcfg;
2045 struct mcast_mrouter_bundle *mrouter;
2046 struct xbundle *mcast_xbundle;
2048 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2049 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2050 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2051 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2052 xlate_report(ctx, "forwarding to mcast router port");
2053 output_normal(ctx, mcast_xbundle, vlan);
2054 } else if (!mcast_xbundle) {
2055 xlate_report(ctx, "mcast router port is unknown, dropping");
2057 xlate_report(ctx, "mcast router port is input port, dropping");
2062 /* send the packet to ports flagged to be flooded */
2064 xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2065 struct mcast_snooping *ms,
2066 struct xbundle *in_xbundle, uint16_t vlan)
2067 OVS_REQ_RDLOCK(ms->rwlock)
2069 struct xlate_cfg *xcfg;
2070 struct mcast_port_bundle *fport;
2071 struct xbundle *mcast_xbundle;
2073 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2074 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2075 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2076 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2077 xlate_report(ctx, "forwarding to mcast flood port");
2078 output_normal(ctx, mcast_xbundle, vlan);
2079 } else if (!mcast_xbundle) {
2080 xlate_report(ctx, "mcast flood port is unknown, dropping");
2082 xlate_report(ctx, "mcast flood port is input port, dropping");
2087 /* forward the Reports to configured ports */
2089 xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
2090 struct mcast_snooping *ms,
2091 struct xbundle *in_xbundle, uint16_t vlan)
2092 OVS_REQ_RDLOCK(ms->rwlock)
2094 struct xlate_cfg *xcfg;
2095 struct mcast_port_bundle *rport;
2096 struct xbundle *mcast_xbundle;
2098 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2099 LIST_FOR_EACH(rport, node, &ms->rport_list) {
2100 mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2101 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2102 xlate_report(ctx, "forwarding Report to mcast flagged port");
2103 output_normal(ctx, mcast_xbundle, vlan);
2104 } else if (!mcast_xbundle) {
2105 xlate_report(ctx, "mcast port is unknown, dropping the Report");
2107 xlate_report(ctx, "mcast port is input port, dropping the Report");
2113 xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2116 struct xbundle *xbundle;
2118 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2119 if (xbundle != in_xbundle
2120 && xbundle_includes_vlan(xbundle, vlan)
2121 && xbundle->floodable
2122 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2123 output_normal(ctx, xbundle, vlan);
2126 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2130 xlate_normal(struct xlate_ctx *ctx)
2132 struct flow_wildcards *wc = &ctx->xout->wc;
2133 struct flow *flow = &ctx->xin->flow;
2134 struct xbundle *in_xbundle;
2135 struct xport *in_port;
2136 struct mac_entry *mac;
2141 ctx->xout->has_normal = true;
2143 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2144 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2145 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2147 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
2148 ctx->xin->packet != NULL, &in_port);
2150 xlate_report(ctx, "no input bundle, dropping");
2154 /* Drop malformed frames. */
2155 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
2156 !(flow->vlan_tci & htons(VLAN_CFI))) {
2157 if (ctx->xin->packet != NULL) {
2158 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2159 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
2160 "VLAN tag received on port %s",
2161 ctx->xbridge->name, in_xbundle->name);
2163 xlate_report(ctx, "partial VLAN tag, dropping");
2167 /* Drop frames on bundles reserved for mirroring. */
2168 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
2169 if (ctx->xin->packet != NULL) {
2170 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2171 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2172 "%s, which is reserved exclusively for mirroring",
2173 ctx->xbridge->name, in_xbundle->name);
2175 xlate_report(ctx, "input port is mirror output port, dropping");
2180 vid = vlan_tci_to_vid(flow->vlan_tci);
2181 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
2182 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2185 vlan = input_vid_to_vlan(in_xbundle, vid);
2187 /* Check other admissibility requirements. */
2188 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2192 /* Learn source MAC. */
2193 if (ctx->xin->may_learn) {
2194 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
2196 if (ctx->xin->xcache) {
2197 struct xc_entry *entry;
2199 /* Save enough info to update mac learning table later. */
2200 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
2201 entry->u.normal.ofproto = ctx->xbridge->ofproto;
2202 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2203 entry->u.normal.vlan = vlan;
2206 /* Determine output bundle. */
2207 if (mcast_snooping_enabled(ctx->xbridge->ms)
2208 && !eth_addr_is_broadcast(flow->dl_dst)
2209 && eth_addr_is_multicast(flow->dl_dst)
2210 && flow->dl_type == htons(ETH_TYPE_IP)) {
2211 struct mcast_snooping *ms = ctx->xbridge->ms;
2212 struct mcast_group *grp;
2214 if (flow->nw_proto == IPPROTO_IGMP) {
2215 if (ctx->xin->may_learn) {
2216 if (mcast_snooping_is_membership(flow->tp_src) ||
2217 mcast_snooping_is_query(flow->tp_src)) {
2218 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2223 if (mcast_snooping_is_membership(flow->tp_src)) {
2224 ovs_rwlock_rdlock(&ms->rwlock);
2225 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2226 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2227 * forward IGMP Membership Reports only to those ports where
2228 * multicast routers are attached. Alternatively stated: a
2229 * snooping switch should not forward IGMP Membership Reports
2230 * to ports on which only hosts are attached.
2231 * An administrative control may be provided to override this
2232 * restriction, allowing the report messages to be flooded to
2234 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2235 ovs_rwlock_unlock(&ms->rwlock);
2237 xlate_report(ctx, "multicast traffic, flooding");
2238 xlate_normal_flood(ctx, in_xbundle, vlan);
2242 if (ip_is_local_multicast(flow->nw_dst)) {
2243 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2244 * address in the 224.0.0.x range which are not IGMP must
2245 * be forwarded on all ports */
2246 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2247 xlate_normal_flood(ctx, in_xbundle, vlan);
2252 /* forwarding to group base ports */
2253 ovs_rwlock_rdlock(&ms->rwlock);
2254 grp = mcast_snooping_lookup(ms, flow->nw_dst, vlan);
2256 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2257 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2258 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2260 if (mcast_snooping_flood_unreg(ms)) {
2261 xlate_report(ctx, "unregistered multicast, flooding");
2262 xlate_normal_flood(ctx, in_xbundle, vlan);
2264 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2265 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2268 ovs_rwlock_unlock(&ms->rwlock);
2270 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2271 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
2272 mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
2273 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2276 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2277 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2278 if (mac_xbundle && mac_xbundle != in_xbundle) {
2279 xlate_report(ctx, "forwarding to learned port");
2280 output_normal(ctx, mac_xbundle, vlan);
2281 } else if (!mac_xbundle) {
2282 xlate_report(ctx, "learned port is unknown, dropping");
2284 xlate_report(ctx, "learned port is input port, dropping");
2287 xlate_report(ctx, "no learned MAC for destination, flooding");
2288 xlate_normal_flood(ctx, in_xbundle, vlan);
2293 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
2294 * the number of packets out of UINT32_MAX to sample. The given
2295 * cookie is passed back in the callback for each sampled packet.
2298 compose_sample_action(const struct xbridge *xbridge,
2299 struct ofpbuf *odp_actions,
2300 const struct flow *flow,
2301 const uint32_t probability,
2302 const union user_action_cookie *cookie,
2303 const size_t cookie_size,
2304 const odp_port_t tunnel_out_port)
2306 size_t sample_offset, actions_offset;
2307 odp_port_t odp_port;
2311 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
2313 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
2315 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
2317 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
2318 pid = dpif_port_get_pid(xbridge->dpif, odp_port,
2319 flow_hash_5tuple(flow, 0));
2320 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2321 tunnel_out_port, odp_actions);
2323 nl_msg_end_nested(odp_actions, actions_offset);
2324 nl_msg_end_nested(odp_actions, sample_offset);
2325 return cookie_offset;
2329 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
2330 odp_port_t odp_port, unsigned int n_outputs,
2331 union user_action_cookie *cookie)
2335 cookie->type = USER_ACTION_COOKIE_SFLOW;
2336 cookie->sflow.vlan_tci = vlan_tci;
2338 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2339 * port information") for the interpretation of cookie->output. */
2340 switch (n_outputs) {
2342 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2343 cookie->sflow.output = 0x40000000 | 256;
2347 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
2349 cookie->sflow.output = ifindex;
2354 /* 0x80000000 means "multiple output ports. */
2355 cookie->sflow.output = 0x80000000 | n_outputs;
2360 /* Compose SAMPLE action for sFlow bridge sampling. */
2362 compose_sflow_action(const struct xbridge *xbridge,
2363 struct ofpbuf *odp_actions,
2364 const struct flow *flow,
2365 odp_port_t odp_port)
2367 uint32_t probability;
2368 union user_action_cookie cookie;
2370 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
2374 probability = dpif_sflow_get_probability(xbridge->sflow);
2375 compose_sflow_cookie(xbridge, htons(0), odp_port,
2376 odp_port == ODPP_NONE ? 0 : 1, &cookie);
2378 return compose_sample_action(xbridge, odp_actions, flow, probability,
2379 &cookie, sizeof cookie.sflow, ODPP_NONE);
2383 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
2384 uint32_t obs_domain_id, uint32_t obs_point_id,
2385 union user_action_cookie *cookie)
2387 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
2388 cookie->flow_sample.probability = probability;
2389 cookie->flow_sample.collector_set_id = collector_set_id;
2390 cookie->flow_sample.obs_domain_id = obs_domain_id;
2391 cookie->flow_sample.obs_point_id = obs_point_id;
2395 compose_ipfix_cookie(union user_action_cookie *cookie,
2396 odp_port_t output_odp_port)
2398 cookie->type = USER_ACTION_COOKIE_IPFIX;
2399 cookie->ipfix.output_odp_port = output_odp_port;
2402 /* Compose SAMPLE action for IPFIX bridge sampling. */
2404 compose_ipfix_action(const struct xbridge *xbridge,
2405 struct ofpbuf *odp_actions,
2406 const struct flow *flow,
2407 odp_port_t output_odp_port)
2409 uint32_t probability;
2410 union user_action_cookie cookie;
2411 odp_port_t tunnel_out_port = ODPP_NONE;
2413 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
2417 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2419 if (output_odp_port == ODPP_NONE &&
2420 !dpif_ipfix_get_bridge_exporter_input_sampling(xbridge->ipfix)) {
2424 /* For output case, output_odp_port is valid*/
2425 if (output_odp_port != ODPP_NONE) {
2426 if (!dpif_ipfix_get_bridge_exporter_output_sampling(xbridge->ipfix)) {
2429 /* If tunnel sampling is enabled, put an additional option attribute:
2430 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2432 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(xbridge->ipfix) &&
2433 dpif_ipfix_get_tunnel_port(xbridge->ipfix, output_odp_port) ) {
2434 tunnel_out_port = output_odp_port;
2438 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
2439 compose_ipfix_cookie(&cookie, output_odp_port);
2441 compose_sample_action(xbridge, odp_actions, flow, probability,
2442 &cookie, sizeof cookie.ipfix, tunnel_out_port);
2445 /* SAMPLE action for sFlow must be first action in any given list of
2446 * actions. At this point we do not have all information required to
2447 * build it. So try to build sample action as complete as possible. */
2449 add_sflow_action(struct xlate_ctx *ctx)
2451 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
2452 ctx->xout->odp_actions,
2453 &ctx->xin->flow, ODPP_NONE);
2454 ctx->sflow_odp_port = 0;
2455 ctx->sflow_n_outputs = 0;
2458 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
2459 * of actions, eventually after the SAMPLE action for sFlow. */
2461 add_ipfix_action(struct xlate_ctx *ctx)
2463 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2464 &ctx->xin->flow, ODPP_NONE);
2468 add_ipfix_output_action(struct xlate_ctx *ctx, odp_port_t port)
2470 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2471 &ctx->xin->flow, port);
2474 /* Fix SAMPLE action according to data collected while composing ODP actions.
2475 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
2476 * USERSPACE action's user-cookie which is required for sflow. */
2478 fix_sflow_action(struct xlate_ctx *ctx)
2480 const struct flow *base = &ctx->base_flow;
2481 union user_action_cookie *cookie;
2483 if (!ctx->user_cookie_offset) {
2487 cookie = ofpbuf_at(ctx->xout->odp_actions, ctx->user_cookie_offset,
2488 sizeof cookie->sflow);
2489 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2491 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
2492 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
2495 static enum slow_path_reason
2496 process_special(struct xlate_ctx *ctx, const struct flow *flow,
2497 const struct xport *xport, const struct dp_packet *packet)
2499 struct flow_wildcards *wc = &ctx->xout->wc;
2500 const struct xbridge *xbridge = ctx->xbridge;
2504 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
2506 cfm_process_heartbeat(xport->cfm, packet);
2509 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
2511 bfd_process_packet(xport->bfd, flow, packet);
2512 /* If POLL received, immediately sends FINAL back. */
2513 if (bfd_should_send_packet(xport->bfd)) {
2514 ofproto_dpif_monitor_port_send_soon(xport->ofport);
2518 } else if (xport->xbundle && xport->xbundle->lacp
2519 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2521 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
2524 } else if ((xbridge->stp || xbridge->rstp) &&
2525 stp_should_process_flow(flow, wc)) {
2528 ? stp_process_packet(xport, packet)
2529 : rstp_process_packet(xport, packet);
2538 tnl_route_lookup_flow(const struct flow *oflow,
2539 ovs_be32 *ip, struct xport **out_port)
2541 char out_dev[IFNAMSIZ];
2542 struct xbridge *xbridge;
2543 struct xlate_cfg *xcfg;
2546 if (!ovs_router_lookup(oflow->tunnel.ip_dst, out_dev, &gw)) {
2553 *ip = oflow->tunnel.ip_dst;
2556 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2559 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
2560 if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
2563 HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
2564 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
2575 xlate_flood_packet(struct xbridge *xbridge, struct dp_packet *packet)
2577 struct ofpact_output output;
2580 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
2581 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
2582 flow_extract(packet, &flow);
2583 flow.in_port.ofp_port = OFPP_NONE;
2584 output.port = OFPP_FLOOD;
2587 return ofproto_dpif_execute_actions(xbridge->ofproto, &flow, NULL,
2588 &output.ofpact, sizeof output,
2593 tnl_send_arp_request(const struct xport *out_dev, const uint8_t eth_src[ETH_ADDR_LEN],
2594 ovs_be32 ip_src, ovs_be32 ip_dst)
2596 struct xbridge *xbridge = out_dev->xbridge;
2597 struct dp_packet packet;
2599 dp_packet_init(&packet, 0);
2600 compose_arp(&packet, eth_src, ip_src, ip_dst);
2602 xlate_flood_packet(xbridge, &packet);
2603 dp_packet_uninit(&packet);
2607 build_tunnel_send(const struct xlate_ctx *ctx, const struct xport *xport,
2608 const struct flow *flow, odp_port_t tunnel_odp_port)
2610 struct ovs_action_push_tnl tnl_push_data;
2611 struct xport *out_dev = NULL;
2612 ovs_be32 s_ip, d_ip = 0;
2613 uint8_t smac[ETH_ADDR_LEN];
2614 uint8_t dmac[ETH_ADDR_LEN];
2617 err = tnl_route_lookup_flow(flow, &d_ip, &out_dev);
2622 /* Use mac addr of bridge port of the peer. */
2623 err = netdev_get_etheraddr(out_dev->netdev, smac);
2628 err = netdev_get_in4(out_dev->netdev, (struct in_addr *) &s_ip, NULL);
2633 err = tnl_arp_lookup(out_dev->xbridge->name, d_ip, dmac);
2635 tnl_send_arp_request(out_dev, smac, s_ip, d_ip);
2638 if (ctx->xin->xcache) {
2639 struct xc_entry *entry;
2641 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_ARP);
2642 ovs_strlcpy(entry->u.tnl_arp_cache.br_name, out_dev->xbridge->name,
2643 sizeof entry->u.tnl_arp_cache.br_name);
2644 entry->u.tnl_arp_cache.d_ip = d_ip;
2646 err = tnl_port_build_header(xport->ofport, flow,
2647 dmac, smac, s_ip, &tnl_push_data);
2651 tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
2652 tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
2653 odp_put_tnl_push_action(ctx->xout->odp_actions, &tnl_push_data);
2658 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
2661 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
2662 struct flow_wildcards *wc = &ctx->xout->wc;
2663 struct flow *flow = &ctx->xin->flow;
2664 struct flow_tnl flow_tnl;
2665 ovs_be16 flow_vlan_tci;
2666 uint32_t flow_pkt_mark;
2667 uint8_t flow_nw_tos;
2668 odp_port_t out_port, odp_port;
2669 bool tnl_push_pop_send = false;
2672 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2673 * before traversing a patch port. */
2674 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 31);
2675 memset(&flow_tnl, 0, sizeof flow_tnl);
2678 xlate_report(ctx, "Nonexistent output port");
2680 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
2681 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2683 } else if (check_stp) {
2684 if (is_stp(&ctx->base_flow)) {
2685 if (!xport_stp_should_forward_bpdu(xport) &&
2686 !xport_rstp_should_manage_bpdu(xport)) {
2687 if (ctx->xbridge->stp != NULL) {
2688 xlate_report(ctx, "STP not in listening state, "
2689 "skipping bpdu output");
2690 } else if (ctx->xbridge->rstp != NULL) {
2691 xlate_report(ctx, "RSTP not managing BPDU in this state, "
2692 "skipping bpdu output");
2696 } else if (!xport_stp_forward_state(xport) ||
2697 !xport_rstp_forward_state(xport)) {
2698 if (ctx->xbridge->stp != NULL) {
2699 xlate_report(ctx, "STP not in forwarding state, "
2701 } else if (ctx->xbridge->rstp != NULL) {
2702 xlate_report(ctx, "RSTP not in forwarding state, "
2709 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
2710 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
2715 const struct xport *peer = xport->peer;
2716 struct flow old_flow = ctx->xin->flow;
2717 enum slow_path_reason special;
2718 uint8_t table_id = rule_dpif_lookup_get_init_table_id(&ctx->xin->flow);
2720 ctx->xbridge = peer->xbridge;
2721 flow->in_port.ofp_port = peer->ofp_port;
2722 flow->metadata = htonll(0);
2723 memset(&flow->tunnel, 0, sizeof flow->tunnel);
2724 memset(flow->regs, 0, sizeof flow->regs);
2725 flow->actset_output = OFPP_UNSET;
2727 special = process_special(ctx, &ctx->xin->flow, peer,
2730 ctx->xout->slow |= special;
2731 } else if (may_receive(peer, ctx)) {
2732 if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
2733 xlate_table_action(ctx, flow->in_port.ofp_port, table_id,
2736 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
2737 * the learning action look at the packet, then drop it. */
2738 struct flow old_base_flow = ctx->base_flow;
2739 size_t old_size = ofpbuf_size(ctx->xout->odp_actions);
2740 mirror_mask_t old_mirrors = ctx->xout->mirrors;
2741 xlate_table_action(ctx, flow->in_port.ofp_port, table_id,
2743 ctx->xout->mirrors = old_mirrors;
2744 ctx->base_flow = old_base_flow;
2745 ofpbuf_set_size(ctx->xout->odp_actions, old_size);
2749 ctx->xin->flow = old_flow;
2750 ctx->xbridge = xport->xbridge;
2752 if (ctx->xin->resubmit_stats) {
2753 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2754 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
2756 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
2759 if (ctx->xin->xcache) {
2760 struct xc_entry *entry;
2762 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2763 entry->u.dev.tx = netdev_ref(xport->netdev);
2764 entry->u.dev.rx = netdev_ref(peer->netdev);
2765 entry->u.dev.bfd = bfd_ref(peer->bfd);
2770 flow_vlan_tci = flow->vlan_tci;
2771 flow_pkt_mark = flow->pkt_mark;
2772 flow_nw_tos = flow->nw_tos;
2774 if (count_skb_priorities(xport)) {
2775 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
2776 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
2777 wc->masks.nw_tos |= IP_DSCP_MASK;
2778 flow->nw_tos &= ~IP_DSCP_MASK;
2779 flow->nw_tos |= dscp;
2783 if (xport->is_tunnel) {
2784 /* Save tunnel metadata so that changes made due to
2785 * the Logical (tunnel) Port are not visible for any further
2786 * matches, while explicit set actions on tunnel metadata are.
2788 flow_tnl = flow->tunnel;
2789 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
2790 if (odp_port == ODPP_NONE) {
2791 xlate_report(ctx, "Tunneling decided against output");
2792 goto out; /* restore flow_nw_tos */
2794 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
2795 xlate_report(ctx, "Not tunneling to our own address");
2796 goto out; /* restore flow_nw_tos */
2798 if (ctx->xin->resubmit_stats) {
2799 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2801 if (ctx->xin->xcache) {
2802 struct xc_entry *entry;
2804 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2805 entry->u.dev.tx = netdev_ref(xport->netdev);
2807 out_port = odp_port;
2808 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
2809 tnl_push_pop_send = true;
2811 commit_odp_tunnel_action(flow, &ctx->base_flow,
2812 ctx->xout->odp_actions);
2813 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2816 odp_port = xport->odp_port;
2817 out_port = odp_port;
2818 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
2819 ofp_port_t vlandev_port;
2821 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2822 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
2823 ofp_port, flow->vlan_tci);
2824 if (vlandev_port != ofp_port) {
2825 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
2826 flow->vlan_tci = htons(0);
2831 if (out_port != ODPP_NONE) {
2832 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
2833 ctx->xout->odp_actions,
2835 ctx->xbridge->masked_set_action);
2837 if (ctx->use_recirc) {
2838 struct ovs_action_hash *act_hash;
2839 struct xlate_recirc *xr = &ctx->recirc;
2842 act_hash = nl_msg_put_unspec_uninit(ctx->xout->odp_actions,
2843 OVS_ACTION_ATTR_HASH,
2845 act_hash->hash_alg = xr->hash_alg;
2846 act_hash->hash_basis = xr->hash_basis;
2848 /* Recirc action. */
2849 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC,
2853 if (tnl_push_pop_send) {
2854 build_tunnel_send(ctx, xport, flow, odp_port);
2855 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2857 odp_port_t odp_tnl_port = ODPP_NONE;
2859 /* XXX: Write better Filter for tunnel port. We can use inport
2860 * int tunnel-port flow to avoid these checks completely. */
2861 if (ofp_port == OFPP_LOCAL &&
2862 ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
2864 odp_tnl_port = tnl_port_map_lookup(flow, wc);
2867 if (odp_tnl_port != ODPP_NONE) {
2868 nl_msg_put_odp_port(ctx->xout->odp_actions,
2869 OVS_ACTION_ATTR_TUNNEL_POP,
2872 /* Tunnel push-pop action is not compatible with
2874 add_ipfix_output_action(ctx, out_port);
2875 nl_msg_put_odp_port(ctx->xout->odp_actions,
2876 OVS_ACTION_ATTR_OUTPUT,
2882 ctx->sflow_odp_port = odp_port;
2883 ctx->sflow_n_outputs++;
2884 ctx->xout->nf_output_iface = ofp_port;
2889 flow->vlan_tci = flow_vlan_tci;
2890 flow->pkt_mark = flow_pkt_mark;
2891 flow->nw_tos = flow_nw_tos;
2895 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
2897 compose_output_action__(ctx, ofp_port, true);
2901 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
2903 struct rule_dpif *old_rule = ctx->rule;
2904 const struct rule_actions *actions;
2906 if (ctx->xin->resubmit_stats) {
2907 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
2913 actions = rule_dpif_get_actions(rule);
2914 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
2915 ctx->rule = old_rule;
2920 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
2922 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2924 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
2925 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
2926 MAX_RESUBMIT_RECURSION);
2927 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
2928 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
2929 } else if (ofpbuf_size(ctx->xout->odp_actions) > UINT16_MAX) {
2930 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
2931 } else if (ofpbuf_size(&ctx->stack) >= 65536) {
2932 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
2941 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
2942 bool may_packet_in, bool honor_table_miss)
2944 if (xlate_resubmit_resource_check(ctx)) {
2945 struct flow_wildcards *wc;
2946 uint8_t old_table_id = ctx->table_id;
2947 struct rule_dpif *rule;
2949 ctx->table_id = table_id;
2950 wc = (ctx->xin->skip_wildcards) ? NULL : &ctx->xout->wc;
2952 rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
2953 &ctx->xin->flow, wc,
2954 ctx->xin->xcache != NULL,
2955 ctx->xin->resubmit_stats,
2956 &ctx->table_id, in_port,
2957 may_packet_in, honor_table_miss);
2959 if (OVS_UNLIKELY(ctx->xin->resubmit_hook)) {
2960 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse + 1);
2964 /* Fill in the cache entry here instead of xlate_recursively
2965 * to make the reference counting more explicit. We take a
2966 * reference in the lookups above if we are going to cache the
2968 if (ctx->xin->xcache) {
2969 struct xc_entry *entry;
2971 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
2972 entry->u.rule = rule;
2974 xlate_recursively(ctx, rule);
2977 ctx->table_id = old_table_id;
2985 xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
2986 struct ofputil_bucket *bucket)
2988 if (ctx->xin->resubmit_stats) {
2989 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
2991 if (ctx->xin->xcache) {
2992 struct xc_entry *entry;
2994 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
2995 entry->u.group.group = group_dpif_ref(group);
2996 entry->u.group.bucket = bucket;
3001 xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
3003 uint64_t action_list_stub[1024 / 8];
3004 struct ofpbuf action_list, action_set;
3006 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
3007 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3009 ofpacts_execute_action_set(&action_list, &action_set);
3011 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
3014 ofpbuf_uninit(&action_set);
3015 ofpbuf_uninit(&action_list);
3019 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3021 struct ofputil_bucket *bucket;
3022 const struct ovs_list *buckets;
3023 struct flow old_flow = ctx->xin->flow;
3025 group_dpif_get_buckets(group, &buckets);
3027 LIST_FOR_EACH (bucket, list_node, buckets) {
3028 xlate_group_bucket(ctx, bucket);
3029 /* Roll back flow to previous state.
3030 * This is equivalent to cloning the packet for each bucket.
3032 * As a side effect any subsequently applied actions will
3033 * also effectively be applied to a clone of the packet taken
3034 * just before applying the all or indirect group. */
3035 ctx->xin->flow = old_flow;
3037 xlate_group_stats(ctx, group, NULL);
3041 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3043 struct ofputil_bucket *bucket;
3045 bucket = group_first_live_bucket(ctx, group, 0);
3047 xlate_group_bucket(ctx, bucket);
3048 xlate_group_stats(ctx, group, bucket);
3053 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3055 struct flow_wildcards *wc = &ctx->xout->wc;
3056 struct ofputil_bucket *bucket;
3059 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
3060 bucket = group_best_live_bucket(ctx, group, basis);
3062 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3063 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3064 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3065 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3066 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3067 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3068 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3069 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3070 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3072 xlate_group_bucket(ctx, bucket);
3073 xlate_group_stats(ctx, group, bucket);
3078 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3080 ctx->in_group = true;
3082 switch (group_dpif_get_type(group)) {
3084 case OFPGT11_INDIRECT:
3085 xlate_all_group(ctx, group);
3087 case OFPGT11_SELECT:
3088 xlate_select_group(ctx, group);
3091 xlate_ff_group(ctx, group);
3096 group_dpif_unref(group);
3098 ctx->in_group = false;
3102 xlate_group_resource_check(struct xlate_ctx *ctx)
3104 if (!xlate_resubmit_resource_check(ctx)) {
3106 } else if (ctx->in_group) {
3107 /* Prevent nested translation of OpenFlow groups.
3109 * OpenFlow allows this restriction. We enforce this restriction only
3110 * because, with the current architecture, we would otherwise have to
3111 * take a possibly recursive read lock on the ofgroup rwlock, which is
3112 * unsafe given that POSIX allows taking a read lock to block if there
3113 * is a thread blocked on taking the write lock. Other solutions
3114 * without this restriction are also possible, but seem unwarranted
3115 * given the current limited use of groups. */
3116 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3118 VLOG_ERR_RL(&rl, "cannot recursively translate OpenFlow group");
3126 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3128 if (xlate_group_resource_check(ctx)) {
3129 struct group_dpif *group;
3132 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
3134 xlate_group_action__(ctx, group);
3144 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
3145 const struct ofpact_resubmit *resubmit)
3149 bool may_packet_in = false;
3150 bool honor_table_miss = false;
3152 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
3153 /* Still allow missed packets to be sent to the controller
3154 * if resubmitting from an internal table. */
3155 may_packet_in = true;
3156 honor_table_miss = true;
3159 in_port = resubmit->in_port;
3160 if (in_port == OFPP_IN_PORT) {
3161 in_port = ctx->xin->flow.in_port.ofp_port;
3164 table_id = resubmit->table_id;
3165 if (table_id == 255) {
3166 table_id = ctx->table_id;
3169 xlate_table_action(ctx, in_port, table_id, may_packet_in,
3174 flood_packets(struct xlate_ctx *ctx, bool all)
3176 const struct xport *xport;
3178 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
3179 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
3184 compose_output_action__(ctx, xport->ofp_port, false);
3185 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
3186 compose_output_action(ctx, xport->ofp_port);
3190 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3194 execute_controller_action(struct xlate_ctx *ctx, int len,
3195 enum ofp_packet_in_reason reason,
3196 uint16_t controller_id)
3198 struct ofproto_packet_in *pin;
3199 struct dp_packet *packet;
3201 ctx->xout->slow |= SLOW_CONTROLLER;
3202 if (!ctx->xin->packet) {
3206 packet = dp_packet_clone(ctx->xin->packet);
3208 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3209 ctx->xout->odp_actions,
3211 ctx->xbridge->masked_set_action);
3213 odp_execute_actions(NULL, &packet, 1, false,
3214 ofpbuf_data(ctx->xout->odp_actions),
3215 ofpbuf_size(ctx->xout->odp_actions), NULL);
3217 pin = xmalloc(sizeof *pin);
3218 pin->up.packet_len = dp_packet_size(packet);
3219 pin->up.packet = dp_packet_steal_data(packet);
3220 pin->up.reason = reason;
3221 pin->up.table_id = ctx->table_id;
3222 pin->up.cookie = (ctx->rule
3223 ? rule_dpif_get_flow_cookie(ctx->rule)
3226 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
3228 pin->controller_id = controller_id;
3229 pin->send_len = len;
3230 /* If a rule is a table-miss rule then this is
3231 * a table-miss handled by a table-miss rule.
3233 * Else, if rule is internal and has a controller action,
3234 * the later being implied by the rule being processed here,
3235 * then this is a table-miss handled without a table-miss rule.
3237 * Otherwise this is not a table-miss. */
3238 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
3240 if (rule_dpif_is_table_miss(ctx->rule)) {
3241 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
3242 } else if (rule_dpif_is_internal(ctx->rule)) {
3243 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
3246 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
3247 dp_packet_delete(packet);
3251 compose_recirculate_action(struct xlate_ctx *ctx,
3252 const struct ofpact *ofpacts_base,
3253 const struct ofpact *ofpact_current,
3254 size_t ofpacts_base_len)
3258 unsigned ofpacts_len;
3261 struct ofpbuf ofpacts;
3265 ofpacts_len = ofpacts_base_len -
3266 ((uint8_t *)ofpact_current - (uint8_t *)ofpacts_base);
3269 id = rule_dpif_get_recirc_id(ctx->rule);
3271 /* In the case where ctx has no rule then allocate a recirc id.
3272 * The life-cycle of this recirc id is managed by associating it
3273 * with the internal rule that is created to to handle
3274 * recirculation below.
3276 * The known use-case of this is packet_out which
3277 * translates actions without a rule */
3278 id = ofproto_dpif_alloc_recirc_id(ctx->xbridge->ofproto);
3281 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3282 VLOG_ERR_RL(&rl, "Failed to allocate recirculation id");
3286 match_init_catchall(&match);
3287 match_set_recirc_id(&match, id);
3288 ofpbuf_use_const(&ofpacts, ofpact_current, ofpacts_len);
3289 error = ofproto_dpif_add_internal_flow(ctx->xbridge->ofproto, &match,
3290 RECIRC_RULE_PRIORITY,
3291 RECIRC_TIMEOUT, &ofpacts, &rule);
3293 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3294 VLOG_ERR_RL(&rl, "Failed to add post recirculation flow %s",
3295 match_to_string(&match, 0));
3298 /* If ctx has no rule then associate the recirc id, which
3299 * was allocated above, with the internal rule. This allows
3300 * the recirc id to be released when the internal rule times out. */
3302 rule_set_recirc_id(rule, id);
3305 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3306 ctx->xout->odp_actions,
3308 ctx->xbridge->masked_set_action);
3309 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
3313 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
3315 struct flow_wildcards *wc = &ctx->xout->wc;
3316 struct flow *flow = &ctx->xin->flow;
3319 ovs_assert(eth_type_mpls(mpls->ethertype));
3321 n = flow_count_mpls_labels(flow, wc);
3323 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
3324 ctx->xout->odp_actions,
3326 ctx->xbridge->masked_set_action);
3327 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3328 if (ctx->xin->packet != NULL) {
3329 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3330 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3331 "MPLS push action can't be performed as it would "
3332 "have more MPLS LSEs than the %d supported.",
3333 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3339 flow_push_mpls(flow, n, mpls->ethertype, wc);
3343 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
3345 struct flow_wildcards *wc = &ctx->xout->wc;
3346 struct flow *flow = &ctx->xin->flow;
3347 int n = flow_count_mpls_labels(flow, wc);
3349 if (flow_pop_mpls(flow, n, eth_type, wc)) {
3350 if (ctx->xbridge->enable_recirc) {
3351 ctx->was_mpls = true;
3353 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3354 if (ctx->xin->packet != NULL) {
3355 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3356 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3357 "MPLS pop action can't be performed as it has "
3358 "more MPLS LSEs than the %d supported.",
3359 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3362 ofpbuf_clear(ctx->xout->odp_actions);
3367 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3369 struct flow *flow = &ctx->xin->flow;
3371 if (!is_ip_any(flow)) {
3375 ctx->xout->wc.masks.nw_ttl = 0xff;
3376 if (flow->nw_ttl > 1) {
3382 for (i = 0; i < ids->n_controllers; i++) {
3383 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3387 /* Stop processing for current table. */
3393 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3395 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3396 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
3397 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
3402 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3404 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3405 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
3406 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
3411 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
3413 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3414 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3415 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
3420 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
3422 struct flow *flow = &ctx->xin->flow;
3423 struct flow_wildcards *wc = &ctx->xout->wc;
3425 if (eth_type_mpls(flow->dl_type)) {
3426 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
3428 wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3431 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3434 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
3438 /* Stop processing for current table. */
3443 xlate_output_action(struct xlate_ctx *ctx,
3444 ofp_port_t port, uint16_t max_len, bool may_packet_in)
3446 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
3448 ctx->xout->nf_output_iface = NF_OUT_DROP;
3452 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
3455 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3456 0, may_packet_in, true);
3462 flood_packets(ctx, false);
3465 flood_packets(ctx, true);
3467 case OFPP_CONTROLLER:
3468 execute_controller_action(ctx, max_len,
3469 (ctx->in_group ? OFPR_GROUP
3470 : ctx->in_action_set ? OFPR_ACTION_SET
3478 if (port != ctx->xin->flow.in_port.ofp_port) {
3479 compose_output_action(ctx, port);
3481 xlate_report(ctx, "skipping output to input port");
3486 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3487 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3488 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3489 ctx->xout->nf_output_iface = prev_nf_output_iface;
3490 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3491 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3492 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3497 xlate_output_reg_action(struct xlate_ctx *ctx,
3498 const struct ofpact_output_reg *or)
3500 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3501 if (port <= UINT16_MAX) {
3502 union mf_subvalue value;
3504 memset(&value, 0xff, sizeof value);
3505 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
3506 xlate_output_action(ctx, u16_to_ofp(port),
3507 or->max_len, false);
3512 xlate_enqueue_action(struct xlate_ctx *ctx,
3513 const struct ofpact_enqueue *enqueue)
3515 ofp_port_t ofp_port = enqueue->port;
3516 uint32_t queue_id = enqueue->queue;
3517 uint32_t flow_priority, priority;
3520 /* Translate queue to priority. */
3521 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
3523 /* Fall back to ordinary output action. */
3524 xlate_output_action(ctx, enqueue->port, 0, false);
3528 /* Check output port. */
3529 if (ofp_port == OFPP_IN_PORT) {
3530 ofp_port = ctx->xin->flow.in_port.ofp_port;
3531 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
3535 /* Add datapath actions. */
3536 flow_priority = ctx->xin->flow.skb_priority;
3537 ctx->xin->flow.skb_priority = priority;
3538 compose_output_action(ctx, ofp_port);
3539 ctx->xin->flow.skb_priority = flow_priority;
3541 /* Update NetFlow output port. */
3542 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3543 ctx->xout->nf_output_iface = ofp_port;
3544 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3545 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3550 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3552 uint32_t skb_priority;
3554 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
3555 ctx->xin->flow.skb_priority = skb_priority;
3557 /* Couldn't translate queue to a priority. Nothing to do. A warning
3558 * has already been logged. */
3563 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
3565 const struct xbridge *xbridge = xbridge_;
3576 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3579 port = get_ofp_port(xbridge, ofp_port);
3580 return port ? port->may_enable : false;
3585 xlate_bundle_action(struct xlate_ctx *ctx,
3586 const struct ofpact_bundle *bundle)
3590 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
3592 CONST_CAST(struct xbridge *, ctx->xbridge));
3593 if (bundle->dst.field) {
3594 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
3597 xlate_output_action(ctx, port, 0, false);
3602 xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
3603 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
3605 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
3606 if (ctx->xin->may_learn) {
3607 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
3612 xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
3614 ctx->xout->has_learn = true;
3615 learn_mask(learn, &ctx->xout->wc);
3617 if (ctx->xin->xcache) {
3618 struct xc_entry *entry;
3620 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
3621 entry->u.learn.ofproto = ctx->xbridge->ofproto;
3622 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
3623 entry->u.learn.ofpacts = ofpbuf_new(64);
3624 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
3625 entry->u.learn.ofpacts);
3626 } else if (ctx->xin->may_learn) {
3627 uint64_t ofpacts_stub[1024 / 8];
3628 struct ofputil_flow_mod fm;
3629 struct ofpbuf ofpacts;
3631 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3632 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
3633 ofpbuf_uninit(&ofpacts);
3638 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
3639 uint16_t idle_timeout, uint16_t hard_timeout)
3641 if (tcp_flags & (TCP_FIN | TCP_RST)) {
3642 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
3647 xlate_fin_timeout(struct xlate_ctx *ctx,
3648 const struct ofpact_fin_timeout *oft)
3651 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
3652 oft->fin_idle_timeout, oft->fin_hard_timeout);
3653 if (ctx->xin->xcache) {
3654 struct xc_entry *entry;
3656 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
3657 /* XC_RULE already holds a reference on the rule, none is taken
3659 entry->u.fin.rule = ctx->rule;
3660 entry->u.fin.idle = oft->fin_idle_timeout;
3661 entry->u.fin.hard = oft->fin_hard_timeout;
3667 xlate_sample_action(struct xlate_ctx *ctx,
3668 const struct ofpact_sample *os)
3670 union user_action_cookie cookie;
3671 /* Scale the probability from 16-bit to 32-bit while representing
3672 * the same percentage. */
3673 uint32_t probability = (os->probability << 16) | os->probability;
3675 if (!ctx->xbridge->variable_length_userdata) {
3676 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3678 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
3679 "lacks support (needs Linux 3.10+ or kernel module from "
3684 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3685 ctx->xout->odp_actions,
3687 ctx->xbridge->masked_set_action);
3689 compose_flow_sample_cookie(os->probability, os->collector_set_id,
3690 os->obs_domain_id, os->obs_point_id, &cookie);
3691 compose_sample_action(ctx->xbridge, ctx->xout->odp_actions, &ctx->xin->flow,
3692 probability, &cookie, sizeof cookie.flow_sample,
3697 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
3699 if (xport->config & (is_stp(&ctx->xin->flow)
3700 ? OFPUTIL_PC_NO_RECV_STP
3701 : OFPUTIL_PC_NO_RECV)) {
3705 /* Only drop packets here if both forwarding and learning are
3706 * disabled. If just learning is enabled, we need to have
3707 * OFPP_NORMAL and the learning action have a look at the packet
3708 * before we can drop it. */
3709 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
3710 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
3718 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
3720 const struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3721 size_t on_len = ofpact_nest_get_action_len(on);
3722 const struct ofpact *inner;
3724 /* Maintain actset_output depending on the contents of the action set:
3726 * - OFPP_UNSET, if there is no "output" action.
3728 * - The output port, if there is an "output" action and no "group"
3731 * - OFPP_UNSET, if there is a "group" action.
3733 if (!ctx->action_set_has_group) {
3734 OFPACT_FOR_EACH (inner, on->actions, on_len) {
3735 if (inner->type == OFPACT_OUTPUT) {
3736 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(inner)->port;
3737 } else if (inner->type == OFPACT_GROUP) {
3738 ctx->xin->flow.actset_output = OFPP_UNSET;
3739 ctx->action_set_has_group = true;
3744 ofpbuf_put(&ctx->action_set, on->actions, on_len);
3745 ofpact_pad(&ctx->action_set);
3749 xlate_action_set(struct xlate_ctx *ctx)
3751 uint64_t action_list_stub[1024 / 64];
3752 struct ofpbuf action_list;
3754 ctx->in_action_set = true;
3755 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3756 ofpacts_execute_action_set(&action_list, &ctx->action_set);
3757 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
3758 ctx->in_action_set = false;
3759 ofpbuf_uninit(&action_list);
3763 ofpact_needs_recirculation_after_mpls(const struct ofpact *a, struct xlate_ctx *ctx)
3765 struct flow_wildcards *wc = &ctx->xout->wc;
3766 struct flow *flow = &ctx->xin->flow;
3768 if (!ctx->was_mpls) {
3775 case OFPACT_CONTROLLER:
3776 case OFPACT_STRIP_VLAN:
3777 case OFPACT_SET_VLAN_PCP:
3778 case OFPACT_SET_VLAN_VID:
3779 case OFPACT_ENQUEUE:
3780 case OFPACT_PUSH_VLAN:
3781 case OFPACT_SET_ETH_SRC:
3782 case OFPACT_SET_ETH_DST:
3783 case OFPACT_SET_TUNNEL:
3784 case OFPACT_SET_QUEUE:
3785 case OFPACT_POP_QUEUE:
3786 case OFPACT_CONJUNCTION:
3788 case OFPACT_OUTPUT_REG:
3791 case OFPACT_WRITE_METADATA:
3792 case OFPACT_WRITE_ACTIONS:
3793 case OFPACT_CLEAR_ACTIONS:
3797 case OFPACT_POP_MPLS:
3798 case OFPACT_DEC_MPLS_TTL:
3799 case OFPACT_SET_MPLS_TTL:
3800 case OFPACT_SET_MPLS_TC:
3801 case OFPACT_SET_MPLS_LABEL:
3802 case OFPACT_SET_IPV4_SRC:
3803 case OFPACT_SET_IPV4_DST:
3804 case OFPACT_SET_IP_DSCP:
3805 case OFPACT_SET_IP_ECN:
3806 case OFPACT_SET_IP_TTL:
3807 case OFPACT_SET_L4_SRC_PORT:
3808 case OFPACT_SET_L4_DST_PORT:
3809 case OFPACT_RESUBMIT:
3810 case OFPACT_STACK_PUSH:
3811 case OFPACT_STACK_POP:
3812 case OFPACT_DEC_TTL:
3813 case OFPACT_MULTIPATH:
3816 case OFPACT_FIN_TIMEOUT:
3817 case OFPACT_GOTO_TABLE:
3820 case OFPACT_REG_MOVE:
3821 return (mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
3822 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
3824 case OFPACT_SET_FIELD:
3825 return mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field);
3827 case OFPACT_PUSH_MPLS:
3828 /* Recirculate if it is an IP packet with a zero ttl. This may
3829 * indicate that the packet was previously MPLS and an MPLS pop action
3830 * converted it to IP. In this case recirculating should reveal the IP
3831 * TTL which is used as the basis for a new MPLS LSE. */
3832 return (!flow_count_mpls_labels(flow, wc)
3833 && flow->nw_ttl == 0
3834 && is_ip_any(flow));
3841 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
3842 struct xlate_ctx *ctx)
3844 struct flow_wildcards *wc = &ctx->xout->wc;
3845 struct flow *flow = &ctx->xin->flow;
3846 const struct ofpact *a;
3848 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3849 tnl_arp_snoop(flow, wc, ctx->xbridge->name);
3851 /* dl_type already in the mask, not set below. */
3853 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3854 struct ofpact_controller *controller;
3855 const struct ofpact_metadata *metadata;
3856 const struct ofpact_set_field *set_field;
3857 const struct mf_field *mf;
3863 if (ofpact_needs_recirculation_after_mpls(a, ctx)) {
3864 compose_recirculate_action(ctx, ofpacts, a, ofpacts_len);
3870 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
3871 ofpact_get_OUTPUT(a)->max_len, true);
3875 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
3880 case OFPACT_CONTROLLER:
3881 controller = ofpact_get_CONTROLLER(a);
3882 execute_controller_action(ctx, controller->max_len,
3884 controller->controller_id);
3887 case OFPACT_ENQUEUE:
3888 memset(&wc->masks.skb_priority, 0xff,
3889 sizeof wc->masks.skb_priority);
3890 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
3893 case OFPACT_SET_VLAN_VID:
3894 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
3895 if (flow->vlan_tci & htons(VLAN_CFI) ||
3896 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
3897 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
3898 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
3903 case OFPACT_SET_VLAN_PCP:
3904 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
3905 if (flow->vlan_tci & htons(VLAN_CFI) ||
3906 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
3907 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
3908 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
3909 << VLAN_PCP_SHIFT) | VLAN_CFI);
3913 case OFPACT_STRIP_VLAN:
3914 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3915 flow->vlan_tci = htons(0);
3918 case OFPACT_PUSH_VLAN:
3919 /* XXX 802.1AD(QinQ) */
3920 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3921 flow->vlan_tci = htons(VLAN_CFI);
3924 case OFPACT_SET_ETH_SRC:
3925 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3926 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
3929 case OFPACT_SET_ETH_DST:
3930 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3931 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
3934 case OFPACT_SET_IPV4_SRC:
3935 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3936 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3937 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
3941 case OFPACT_SET_IPV4_DST:
3942 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3943 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3944 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
3948 case OFPACT_SET_IP_DSCP:
3949 if (is_ip_any(flow)) {
3950 wc->masks.nw_tos |= IP_DSCP_MASK;
3951 flow->nw_tos &= ~IP_DSCP_MASK;
3952 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
3956 case OFPACT_SET_IP_ECN:
3957 if (is_ip_any(flow)) {
3958 wc->masks.nw_tos |= IP_ECN_MASK;
3959 flow->nw_tos &= ~IP_ECN_MASK;
3960 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
3964 case OFPACT_SET_IP_TTL:
3965 if (is_ip_any(flow)) {
3966 wc->masks.nw_ttl = 0xff;
3967 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
3971 case OFPACT_SET_L4_SRC_PORT:
3972 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3973 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3974 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3975 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
3979 case OFPACT_SET_L4_DST_PORT:
3980 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3981 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3982 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3983 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
3987 case OFPACT_RESUBMIT:
3988 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
3991 case OFPACT_SET_TUNNEL:
3992 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
3995 case OFPACT_SET_QUEUE:
3996 memset(&wc->masks.skb_priority, 0xff,
3997 sizeof wc->masks.skb_priority);
3998 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
4001 case OFPACT_POP_QUEUE:
4002 memset(&wc->masks.skb_priority, 0xff,
4003 sizeof wc->masks.skb_priority);
4004 flow->skb_priority = ctx->orig_skb_priority;
4007 case OFPACT_REG_MOVE:
4008 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
4011 case OFPACT_SET_FIELD:
4012 set_field = ofpact_get_SET_FIELD(a);
4013 mf = set_field->field;
4015 /* Set field action only ever overwrites packet's outermost
4016 * applicable header fields. Do nothing if no header exists. */
4017 if (mf->id == MFF_VLAN_VID) {
4018 wc->masks.vlan_tci |= htons(VLAN_CFI);
4019 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4022 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
4023 /* 'dl_type' is already unwildcarded. */
4024 && !eth_type_mpls(flow->dl_type)) {
4027 /* A flow may wildcard nw_frag. Do nothing if setting a trasport
4028 * header field on a packet that does not have them. */
4029 mf_mask_field_and_prereqs(mf, &wc->masks);
4030 if (mf_are_prereqs_ok(mf, flow)) {
4031 mf_set_flow_value_masked(mf, &set_field->value,
4032 &set_field->mask, flow);
4036 case OFPACT_STACK_PUSH:
4037 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
4041 case OFPACT_STACK_POP:
4042 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
4046 case OFPACT_PUSH_MPLS:
4047 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
4050 case OFPACT_POP_MPLS:
4051 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
4054 case OFPACT_SET_MPLS_LABEL:
4055 compose_set_mpls_label_action(
4056 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
4059 case OFPACT_SET_MPLS_TC:
4060 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
4063 case OFPACT_SET_MPLS_TTL:
4064 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
4067 case OFPACT_DEC_MPLS_TTL:
4068 if (compose_dec_mpls_ttl_action(ctx)) {
4073 case OFPACT_DEC_TTL:
4074 wc->masks.nw_ttl = 0xff;
4075 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
4081 /* Nothing to do. */
4084 case OFPACT_MULTIPATH:
4085 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
4089 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
4092 case OFPACT_OUTPUT_REG:
4093 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
4097 xlate_learn_action(ctx, ofpact_get_LEARN(a));
4100 case OFPACT_CONJUNCTION: {
4101 /* A flow with a "conjunction" action represents part of a special
4102 * kind of "set membership match". Such a flow should not actually
4103 * get executed, but it could via, say, a "packet-out", even though
4104 * that wouldn't be useful. Log it to help debugging. */
4105 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4106 VLOG_INFO_RL(&rl, "executing no-op conjunction action");
4114 case OFPACT_FIN_TIMEOUT:
4115 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4116 ctx->xout->has_fin_timeout = true;
4117 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
4120 case OFPACT_CLEAR_ACTIONS:
4121 ofpbuf_clear(&ctx->action_set);
4122 ctx->xin->flow.actset_output = OFPP_UNSET;
4123 ctx->action_set_has_group = false;
4126 case OFPACT_WRITE_ACTIONS:
4127 xlate_write_actions(ctx, a);
4130 case OFPACT_WRITE_METADATA:
4131 metadata = ofpact_get_WRITE_METADATA(a);
4132 flow->metadata &= ~metadata->mask;
4133 flow->metadata |= metadata->metadata & metadata->mask;
4137 /* Not implemented yet. */
4140 case OFPACT_GOTO_TABLE: {
4141 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
4143 /* Allow ctx->table_id == TBL_INTERNAL, which will be greater
4144 * than ogt->table_id. This is to allow goto_table actions that
4145 * triggered recirculation: ctx->table_id will be TBL_INTERNAL
4146 * after recirculation. */
4147 ovs_assert(ctx->table_id == TBL_INTERNAL
4148 || ctx->table_id < ogt->table_id);
4149 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
4150 ogt->table_id, true, true);
4155 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
4162 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
4163 const struct flow *flow, ofp_port_t in_port,
4164 struct rule_dpif *rule, uint16_t tcp_flags,
4165 const struct dp_packet *packet)
4167 xin->ofproto = ofproto;
4169 xin->flow.in_port.ofp_port = in_port;
4170 xin->flow.actset_output = OFPP_UNSET;
4171 xin->packet = packet;
4172 xin->may_learn = packet != NULL;
4175 xin->ofpacts = NULL;
4176 xin->ofpacts_len = 0;
4177 xin->tcp_flags = tcp_flags;
4178 xin->resubmit_hook = NULL;
4179 xin->report_hook = NULL;
4180 xin->resubmit_stats = NULL;
4181 xin->skip_wildcards = false;
4182 xin->odp_actions = NULL;
4186 xlate_out_uninit(struct xlate_out *xout)
4188 if (xout && xout->odp_actions == &xout->odp_actions_buf) {
4189 ofpbuf_uninit(xout->odp_actions);
4193 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
4194 * into datapath actions, using 'ctx', and discards the datapath actions. */
4196 xlate_actions_for_side_effects(struct xlate_in *xin)
4198 struct xlate_out xout;
4200 xlate_actions(xin, &xout);
4201 xlate_out_uninit(&xout);
4205 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
4208 dst->slow = src->slow;
4209 dst->has_learn = src->has_learn;
4210 dst->has_normal = src->has_normal;
4211 dst->has_fin_timeout = src->has_fin_timeout;
4212 dst->nf_output_iface = src->nf_output_iface;
4213 dst->mirrors = src->mirrors;
4215 dst->odp_actions = &dst->odp_actions_buf;
4216 ofpbuf_use_stub(dst->odp_actions, dst->odp_actions_stub,
4217 sizeof dst->odp_actions_stub);
4218 ofpbuf_put(dst->odp_actions, ofpbuf_data(src->odp_actions),
4219 ofpbuf_size(src->odp_actions));
4222 static struct skb_priority_to_dscp *
4223 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
4225 struct skb_priority_to_dscp *pdscp;
4228 hash = hash_int(skb_priority, 0);
4229 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
4230 if (pdscp->skb_priority == skb_priority) {
4238 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
4241 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
4242 *dscp = pdscp ? pdscp->dscp : 0;
4243 return pdscp != NULL;
4247 count_skb_priorities(const struct xport *xport)
4249 return hmap_count(&xport->skb_priorities);
4253 clear_skb_priorities(struct xport *xport)
4255 struct skb_priority_to_dscp *pdscp, *next;
4257 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
4258 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
4264 actions_output_to_local_port(const struct xlate_ctx *ctx)
4266 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
4267 const struct nlattr *a;
4270 NL_ATTR_FOR_EACH_UNSAFE (a, left, ofpbuf_data(ctx->xout->odp_actions),
4271 ofpbuf_size(ctx->xout->odp_actions)) {
4272 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
4273 && nl_attr_get_odp_port(a) == local_odp_port) {
4280 #if defined(__linux__)
4281 /* Returns the maximum number of packets that the Linux kernel is willing to
4282 * queue up internally to certain kinds of software-implemented ports, or the
4283 * default (and rarely modified) value if it cannot be determined. */
4285 netdev_max_backlog(void)
4287 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
4288 static int max_backlog = 1000; /* The normal default value. */
4290 if (ovsthread_once_start(&once)) {
4291 static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
4295 stream = fopen(filename, "r");
4297 VLOG_WARN("%s: open failed (%s)", filename, ovs_strerror(errno));
4299 if (fscanf(stream, "%d", &n) != 1) {
4300 VLOG_WARN("%s: read error", filename);
4301 } else if (n <= 100) {
4302 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
4308 ovsthread_once_done(&once);
4310 VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
4316 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
4319 count_output_actions(const struct ofpbuf *odp_actions)
4321 const struct nlattr *a;
4325 NL_ATTR_FOR_EACH_UNSAFE (a, left, ofpbuf_data(odp_actions),
4326 ofpbuf_size(odp_actions)) {
4327 if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
4333 #endif /* defined(__linux__) */
4335 /* Returns true if 'odp_actions' contains more output actions than the datapath
4336 * can reliably handle in one go. On Linux, this is the value of the
4337 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
4338 * packets that the kernel is willing to queue up for processing while the
4339 * datapath is processing a set of actions. */
4341 too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
4344 return (ofpbuf_size(odp_actions) / NL_A_U32_SIZE > netdev_max_backlog()
4345 && count_output_actions(odp_actions) > netdev_max_backlog());
4347 /* OSes other than Linux might have similar limits, but we don't know how
4348 * to determine them.*/
4353 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
4354 * into datapath actions in 'odp_actions', using 'ctx'.
4356 * The caller must take responsibility for eventually freeing 'xout', with
4357 * xlate_out_uninit(). */
4359 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
4361 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4362 struct flow_wildcards *wc = &xout->wc;
4363 struct flow *flow = &xin->flow;
4364 struct rule_dpif *rule = NULL;
4366 enum slow_path_reason special;
4367 const struct ofpact *ofpacts;
4368 struct xport *in_port;
4369 struct flow orig_flow;
4370 struct xlate_ctx ctx;
4375 COVERAGE_INC(xlate_actions);
4377 /* Flow initialization rules:
4378 * - 'base_flow' must match the kernel's view of the packet at the
4379 * time that action processing starts. 'flow' represents any
4380 * transformations we wish to make through actions.
4381 * - By default 'base_flow' and 'flow' are the same since the input
4382 * packet matches the output before any actions are applied.
4383 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
4384 * of the received packet as seen by the kernel. If we later output
4385 * to another device without any modifications this will cause us to
4386 * insert a new tag since the original one was stripped off by the
4388 * - Tunnel metadata as received is retained in 'flow'. This allows
4389 * tunnel metadata matching also in later tables.
4390 * Since a kernel action for setting the tunnel metadata will only be
4391 * generated with actual tunnel output, changing the tunnel metadata
4392 * values in 'flow' (such as tun_id) will only have effect with a later
4393 * tunnel output action.
4394 * - Tunnel 'base_flow' is completely cleared since that is what the
4395 * kernel does. If we wish to maintain the original values an action
4396 * needs to be generated. */
4401 ctx.xout->has_learn = false;
4402 ctx.xout->has_normal = false;
4403 ctx.xout->has_fin_timeout = false;
4404 ctx.xout->nf_output_iface = NF_OUT_DROP;
4405 ctx.xout->mirrors = 0;
4407 xout->odp_actions = xin->odp_actions;
4408 if (!xout->odp_actions) {
4409 xout->odp_actions = &xout->odp_actions_buf;
4410 ofpbuf_use_stub(xout->odp_actions, xout->odp_actions_stub,
4411 sizeof xout->odp_actions_stub);
4413 ofpbuf_reserve(xout->odp_actions, NL_A_U32_SIZE);
4415 ctx.xbridge = xbridge_lookup(xcfg, xin->ofproto);
4420 ctx.rule = xin->rule;
4422 ctx.base_flow = *flow;
4423 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
4424 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
4426 flow_wildcards_init_catchall(wc);
4427 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
4428 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
4429 if (is_ip_any(flow)) {
4430 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
4432 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
4434 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
4435 if (ctx.xbridge->netflow) {
4436 netflow_mask_wc(flow, wc);
4441 ctx.in_group = false;
4442 ctx.in_action_set = false;
4443 ctx.orig_skb_priority = flow->skb_priority;
4446 ctx.use_recirc = false;
4447 ctx.was_mpls = false;
4449 if (!xin->ofpacts && !ctx.rule) {
4450 rule = rule_dpif_lookup(ctx.xbridge->ofproto, flow,
4451 xin->skip_wildcards ? NULL : wc,
4452 ctx.xin->xcache != NULL,
4453 ctx.xin->resubmit_stats, &ctx.table_id);
4454 if (ctx.xin->resubmit_stats) {
4455 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
4457 if (ctx.xin->xcache) {
4458 struct xc_entry *entry;
4460 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
4461 entry->u.rule = rule;
4465 if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
4466 ctx.xin->resubmit_hook(ctx.xin, rule, 0);
4469 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
4472 ofpacts = xin->ofpacts;
4473 ofpacts_len = xin->ofpacts_len;
4474 } else if (ctx.rule) {
4475 const struct rule_actions *actions = rule_dpif_get_actions(ctx.rule);
4477 ofpacts = actions->ofpacts;
4478 ofpacts_len = actions->ofpacts_len;
4483 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
4485 ctx.action_set_has_group = false;
4486 ofpbuf_use_stub(&ctx.action_set,
4487 ctx.action_set_stub, sizeof ctx.action_set_stub);
4489 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
4490 /* Do this conditionally because the copy is expensive enough that it
4491 * shows up in profiles. */
4495 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
4496 if (in_port && in_port->is_tunnel) {
4497 if (ctx.xin->resubmit_stats) {
4498 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
4500 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
4503 if (ctx.xin->xcache) {
4504 struct xc_entry *entry;
4506 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
4507 entry->u.dev.rx = netdev_ref(in_port->netdev);
4508 entry->u.dev.bfd = bfd_ref(in_port->bfd);
4512 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
4514 ctx.xout->slow |= special;
4516 size_t sample_actions_len;
4518 if (flow->in_port.ofp_port
4519 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
4520 flow->in_port.ofp_port,
4522 ctx.base_flow.vlan_tci = 0;
4525 add_sflow_action(&ctx);
4526 add_ipfix_action(&ctx);
4527 sample_actions_len = ofpbuf_size(ctx.xout->odp_actions);
4529 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
4530 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
4532 /* We've let OFPP_NORMAL and the learning action look at the
4533 * packet, so drop it now if forwarding is disabled. */
4534 if (in_port && (!xport_stp_forward_state(in_port) ||
4535 !xport_rstp_forward_state(in_port))) {
4536 ofpbuf_set_size(ctx.xout->odp_actions, sample_actions_len);
4540 if (ofpbuf_size(&ctx.action_set)) {
4541 xlate_action_set(&ctx);
4544 if (ctx.xbridge->has_in_band
4545 && in_band_must_output_to_local_port(flow)
4546 && !actions_output_to_local_port(&ctx)) {
4547 compose_output_action(&ctx, OFPP_LOCAL);
4550 fix_sflow_action(&ctx);
4552 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
4553 add_mirror_actions(&ctx, &orig_flow);
4557 if (nl_attr_oversized(ofpbuf_size(ctx.xout->odp_actions))) {
4558 /* These datapath actions are too big for a Netlink attribute, so we
4559 * can't hand them to the kernel directly. dpif_execute() can execute
4560 * them one by one with help, so just mark the result as SLOW_ACTION to
4561 * prevent the flow from being installed. */
4562 COVERAGE_INC(xlate_actions_oversize);
4563 ctx.xout->slow |= SLOW_ACTION;
4564 } else if (too_many_output_actions(ctx.xout->odp_actions)) {
4565 COVERAGE_INC(xlate_actions_too_many_output);
4566 ctx.xout->slow |= SLOW_ACTION;
4569 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
4570 if (ctx.xin->resubmit_stats) {
4571 mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
4572 ctx.xin->resubmit_stats->n_packets,
4573 ctx.xin->resubmit_stats->n_bytes);
4575 if (ctx.xin->xcache) {
4576 struct xc_entry *entry;
4578 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_MIRROR);
4579 entry->u.mirror.mbridge = mbridge_ref(ctx.xbridge->mbridge);
4580 entry->u.mirror.mirrors = xout->mirrors;
4584 if (ctx.xbridge->netflow) {
4585 /* Only update netflow if we don't have controller flow. We don't
4586 * report NetFlow expiration messages for such facets because they
4587 * are just part of the control logic for the network, not real
4589 if (ofpacts_len == 0
4590 || ofpacts->type != OFPACT_CONTROLLER
4591 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
4592 if (ctx.xin->resubmit_stats) {
4593 netflow_flow_update(ctx.xbridge->netflow, flow,
4594 xout->nf_output_iface,
4595 ctx.xin->resubmit_stats);
4597 if (ctx.xin->xcache) {
4598 struct xc_entry *entry;
4600 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
4601 entry->u.nf.netflow = netflow_ref(ctx.xbridge->netflow);
4602 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
4603 entry->u.nf.iface = xout->nf_output_iface;
4608 ofpbuf_uninit(&ctx.stack);
4609 ofpbuf_uninit(&ctx.action_set);
4611 /* Clear the metadata and register wildcard masks, because we won't
4612 * use non-header fields as part of the cache. */
4613 flow_wildcards_clear_non_packet_fields(wc);
4615 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow uses
4616 * the low 8 bits of the 16-bit tp_src and tp_dst members to represent
4617 * these fields. The datapath interface, on the other hand, represents
4618 * them with just 8 bits each. This means that if the high 8 bits of the
4619 * masks for these fields somehow become set, then they will get chopped
4620 * off by a round trip through the datapath, and revalidation will spot
4621 * that as an inconsistency and delete the flow. Avoid the problem here by
4622 * making sure that only the low 8 bits of either field can be unwildcarded
4626 wc->masks.tp_src &= htons(UINT8_MAX);
4627 wc->masks.tp_dst &= htons(UINT8_MAX);
4631 /* Sends 'packet' out 'ofport'.
4632 * May modify 'packet'.
4633 * Returns 0 if successful, otherwise a positive errno value. */
4635 xlate_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
4637 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4638 struct xport *xport;
4639 struct ofpact_output output;
4642 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
4643 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
4644 flow_extract(packet, &flow);
4645 flow.in_port.ofp_port = OFPP_NONE;
4647 xport = xport_lookup(xcfg, ofport);
4651 output.port = xport->ofp_port;
4654 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
4655 &output.ofpact, sizeof output,
4659 struct xlate_cache *
4660 xlate_cache_new(void)
4662 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
4664 ofpbuf_init(&xcache->entries, 512);
4668 static struct xc_entry *
4669 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
4671 struct xc_entry *entry;
4673 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
4680 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
4682 if (entry->u.dev.tx) {
4683 netdev_vport_inc_tx(entry->u.dev.tx, stats);
4685 if (entry->u.dev.rx) {
4686 netdev_vport_inc_rx(entry->u.dev.rx, stats);
4688 if (entry->u.dev.bfd) {
4689 bfd_account_rx(entry->u.dev.bfd, stats);
4694 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
4696 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4697 struct xbridge *xbridge;
4698 struct xbundle *xbundle;
4699 struct flow_wildcards wc;
4701 xbridge = xbridge_lookup(xcfg, ofproto);
4706 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
4712 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
4715 /* Push stats and perform side effects of flow translation. */
4717 xlate_push_stats(struct xlate_cache *xcache,
4718 const struct dpif_flow_stats *stats)
4720 struct xc_entry *entry;
4721 struct ofpbuf entries = xcache->entries;
4722 uint8_t dmac[ETH_ADDR_LEN];
4724 if (!stats->n_packets) {
4728 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
4729 switch (entry->type) {
4731 rule_dpif_credit_stats(entry->u.rule, stats);
4734 bond_account(entry->u.bond.bond, entry->u.bond.flow,
4735 entry->u.bond.vid, stats->n_bytes);
4738 xlate_cache_netdev(entry, stats);
4741 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
4742 entry->u.nf.iface, stats);
4745 mirror_update_stats(entry->u.mirror.mbridge,
4746 entry->u.mirror.mirrors,
4747 stats->n_packets, stats->n_bytes);
4750 ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
4753 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
4754 entry->u.normal.vlan);
4756 case XC_FIN_TIMEOUT:
4757 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
4758 entry->u.fin.idle, entry->u.fin.hard);
4761 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
4765 /* Lookup arp to avoid arp timeout. */
4766 tnl_arp_lookup(entry->u.tnl_arp_cache.br_name, entry->u.tnl_arp_cache.d_ip, dmac);
4775 xlate_dev_unref(struct xc_entry *entry)
4777 if (entry->u.dev.tx) {
4778 netdev_close(entry->u.dev.tx);
4780 if (entry->u.dev.rx) {
4781 netdev_close(entry->u.dev.rx);
4783 if (entry->u.dev.bfd) {
4784 bfd_unref(entry->u.dev.bfd);
4789 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
4791 netflow_flow_clear(netflow, flow);
4792 netflow_unref(netflow);
4797 xlate_cache_clear(struct xlate_cache *xcache)
4799 struct xc_entry *entry;
4800 struct ofpbuf entries;
4806 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
4807 switch (entry->type) {
4809 rule_dpif_unref(entry->u.rule);
4812 free(entry->u.bond.flow);
4813 bond_unref(entry->u.bond.bond);
4816 xlate_dev_unref(entry);
4819 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
4822 mbridge_unref(entry->u.mirror.mbridge);
4825 free(entry->u.learn.fm);
4826 ofpbuf_delete(entry->u.learn.ofpacts);
4829 free(entry->u.normal.flow);
4831 case XC_FIN_TIMEOUT:
4832 /* 'u.fin.rule' is always already held as a XC_RULE, which
4833 * has already released it's reference above. */
4836 group_dpif_unref(entry->u.group.group);
4845 ofpbuf_clear(&xcache->entries);
4849 xlate_cache_delete(struct xlate_cache *xcache)
4851 xlate_cache_clear(xcache);
4852 ofpbuf_uninit(&xcache->entries);