1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. */
17 #include "ofproto/ofproto-dpif-xlate.h"
20 #include <arpa/inet.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
25 #include "tnl-arp-cache.h"
30 #include "byte-order.h"
34 #include "dp-packet.h"
36 #include "dynamic-string.h"
42 #include "mac-learning.h"
43 #include "mcast-snooping.h"
44 #include "meta-flow.h"
45 #include "multipath.h"
46 #include "netdev-vport.h"
49 #include "odp-execute.h"
50 #include "ofp-actions.h"
51 #include "ofproto/ofproto-dpif-ipfix.h"
52 #include "ofproto/ofproto-dpif-mirror.h"
53 #include "ofproto/ofproto-dpif-monitor.h"
54 #include "ofproto/ofproto-dpif-sflow.h"
55 #include "ofproto/ofproto-dpif.h"
56 #include "ofproto/ofproto-provider.h"
57 #include "ovs-router.h"
58 #include "tnl-ports.h"
60 #include "openvswitch/vlog.h"
62 COVERAGE_DEFINE(xlate_actions);
63 COVERAGE_DEFINE(xlate_actions_oversize);
64 COVERAGE_DEFINE(xlate_actions_too_many_output);
66 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
68 /* Maximum depth of flow table recursion (due to resubmit actions) in a
69 * flow translation. */
70 #define MAX_RESUBMIT_RECURSION 64
71 #define MAX_INTERNAL_RESUBMITS 1 /* Max resbmits allowed using rules in
74 /* Timeout for internal rules created to handle recirculation */
75 #define RECIRC_TIMEOUT 60
77 /* Maximum number of resubmit actions in a flow translation, whether they are
78 * recursive or not. */
79 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
82 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
83 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
85 struct ovs_list xbundles; /* Owned xbundles. */
86 struct hmap xports; /* Indexed by ofp_port. */
88 char *name; /* Name used in log messages. */
89 struct dpif *dpif; /* Datapath interface. */
90 struct mac_learning *ml; /* Mac learning handle. */
91 struct mcast_snooping *ms; /* Multicast Snooping handle. */
92 struct mbridge *mbridge; /* Mirroring. */
93 struct dpif_sflow *sflow; /* SFlow handle, or null. */
94 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
95 struct netflow *netflow; /* Netflow handle, or null. */
96 struct stp *stp; /* STP or null if disabled. */
97 struct rstp *rstp; /* RSTP or null if disabled. */
99 bool has_in_band; /* Bridge has in band control? */
100 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
102 /* True if the datapath supports recirculation. */
105 /* True if the datapath supports variable-length
106 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
107 * False if the datapath supports only 8-byte (or shorter) userdata. */
108 bool variable_length_userdata;
110 /* Number of MPLS label stack entries that the datapath supports
112 size_t max_mpls_depth;
114 /* True if the datapath supports masked data in OVS_ACTION_ATTR_SET
116 bool masked_set_action;
120 struct hmap_node hmap_node; /* In global 'xbundles' map. */
121 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
123 struct ovs_list list_node; /* In parent 'xbridges' list. */
124 struct xbridge *xbridge; /* Parent xbridge. */
126 struct ovs_list xports; /* Contains "struct xport"s. */
128 char *name; /* Name used in log messages. */
129 struct bond *bond; /* Nonnull iff more than one port. */
130 struct lacp *lacp; /* LACP handle or null. */
132 enum port_vlan_mode vlan_mode; /* VLAN mode. */
133 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
134 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
135 * NULL if all VLANs are trunked. */
136 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
137 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
141 struct hmap_node hmap_node; /* Node in global 'xports' map. */
142 struct ofport_dpif *ofport; /* Key in global 'xports map. */
144 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
145 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
147 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
149 struct ovs_list bundle_node; /* In parent xbundle (if it exists). */
150 struct xbundle *xbundle; /* Parent xbundle or null. */
152 struct netdev *netdev; /* 'ofport''s netdev. */
154 struct xbridge *xbridge; /* Parent bridge. */
155 struct xport *peer; /* Patch port peer or null. */
157 enum ofputil_port_config config; /* OpenFlow port configuration. */
158 enum ofputil_port_state state; /* OpenFlow port state. */
159 int stp_port_no; /* STP port number or -1 if not in use. */
160 struct rstp_port *rstp_port; /* RSTP port or null. */
162 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
164 bool may_enable; /* May be enabled in bonds. */
165 bool is_tunnel; /* Is a tunnel port. */
167 struct cfm *cfm; /* CFM handle or null. */
168 struct bfd *bfd; /* BFD handle or null. */
169 struct lldp *lldp; /* LLDP handle or null. */
173 struct xlate_in *xin;
174 struct xlate_out *xout;
176 const struct xbridge *xbridge;
178 /* Flow at the last commit. */
179 struct flow base_flow;
181 /* Tunnel IP destination address as received. This is stored separately
182 * as the base_flow.tunnel is cleared on init to reflect the datapath
183 * behavior. Used to make sure not to send tunneled output to ourselves,
184 * which might lead to an infinite loop. This could happen easily
185 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
186 * actually set the tun_dst field. */
187 ovs_be32 orig_tunnel_ip_dst;
189 /* Stack for the push and pop actions. Each stack element is of type
190 * "union mf_subvalue". */
191 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
194 /* The rule that we are currently translating, or NULL. */
195 struct rule_dpif *rule;
197 /* Resubmit statistics, via xlate_table_action(). */
198 int recurse; /* Current resubmit nesting depth. */
199 int resubmits; /* Total number of resubmits. */
200 bool in_group; /* Currently translating ofgroup, if true. */
201 bool in_action_set; /* Currently translating action_set, if true. */
203 uint8_t table_id; /* OpenFlow table ID where flow was found. */
204 ovs_be64 rule_cookie; /* Cookie of the rule being translated. */
205 uint32_t orig_skb_priority; /* Priority when packet arrived. */
206 uint32_t sflow_n_outputs; /* Number of output ports. */
207 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
208 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
209 bool exit; /* No further actions should be processed. */
211 bool use_recirc; /* Should generate recirc? */
212 struct xlate_recirc recirc; /* Information used for generating
213 * recirculation actions */
215 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
216 * This is a trigger for recirculation in cases where translating an action
217 * or looking up a flow requires access to the fields of the packet after
218 * the MPLS label stack that was originally present. */
221 /* OpenFlow 1.1+ action set.
223 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
224 * When translation is otherwise complete, ofpacts_execute_action_set()
225 * converts it to a set of "struct ofpact"s that can be translated into
226 * datapath actions. */
227 bool action_set_has_group; /* Action set contains OFPACT_GROUP? */
228 struct ofpbuf action_set; /* Action set. */
229 uint64_t action_set_stub[1024 / 8];
232 static void xlate_action_set(struct xlate_ctx *ctx);
234 /* A controller may use OFPP_NONE as the ingress port to indicate that
235 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
236 * when an input bundle is needed for validation (e.g., mirroring or
237 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
238 * any 'port' structs, so care must be taken when dealing with it. */
239 static struct xbundle ofpp_none_bundle = {
241 .vlan_mode = PORT_VLAN_TRUNK
244 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
245 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
246 * traffic egressing the 'ofport' with that priority should be marked with. */
247 struct skb_priority_to_dscp {
248 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
249 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
251 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
267 /* xlate_cache entries hold enough information to perform the side effects of
268 * xlate_actions() for a rule, without needing to perform rule translation
269 * from scratch. The primary usage of these is to submit statistics to objects
270 * that a flow relates to, although they may be used for other effects as well
271 * (for instance, refreshing hard timeouts for learned flows). */
275 struct rule_dpif *rule;
282 struct netflow *netflow;
287 struct mbridge *mbridge;
288 mirror_mask_t mirrors;
296 struct ofproto_dpif *ofproto;
297 struct ofputil_flow_mod *fm;
298 struct ofpbuf *ofpacts;
301 struct ofproto_dpif *ofproto;
306 struct rule_dpif *rule;
311 struct group_dpif *group;
312 struct ofputil_bucket *bucket;
315 char br_name[IFNAMSIZ];
321 #define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
322 entries = xcache->entries; \
323 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
325 entry = ofpbuf_try_pull(&entries, sizeof *entry))
328 struct ofpbuf entries;
331 /* Xlate config contains hash maps of all bridges, bundles and ports.
332 * Xcfgp contains the pointer to the current xlate configuration.
333 * When the main thread needs to change the configuration, it copies xcfgp to
334 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
335 * does not block handler and revalidator threads. */
337 struct hmap xbridges;
338 struct hmap xbundles;
341 static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
342 static struct xlate_cfg *new_xcfg = NULL;
344 static bool may_receive(const struct xport *, struct xlate_ctx *);
345 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
347 static void xlate_normal(struct xlate_ctx *);
348 static inline void xlate_report(struct xlate_ctx *, const char *);
349 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
350 uint8_t table_id, bool may_packet_in,
351 bool honor_table_miss);
352 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
353 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
354 static void output_normal(struct xlate_ctx *, const struct xbundle *,
356 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
358 static struct xbridge *xbridge_lookup(struct xlate_cfg *,
359 const struct ofproto_dpif *);
360 static struct xbundle *xbundle_lookup(struct xlate_cfg *,
361 const struct ofbundle *);
362 static struct xport *xport_lookup(struct xlate_cfg *,
363 const struct ofport_dpif *);
364 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
365 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
366 uint32_t skb_priority);
367 static void clear_skb_priorities(struct xport *);
368 static size_t count_skb_priorities(const struct xport *);
369 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
372 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
374 static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
375 static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
376 static void xlate_xport_init(struct xlate_cfg *, struct xport *);
377 static void xlate_xbridge_set(struct xbridge *, struct dpif *,
378 const struct mac_learning *, struct stp *,
379 struct rstp *, const struct mcast_snooping *,
380 const struct mbridge *,
381 const struct dpif_sflow *,
382 const struct dpif_ipfix *,
383 const struct netflow *,
384 bool forward_bpdu, bool has_in_band,
386 bool variable_length_userdata,
387 size_t max_mpls_depth,
388 bool masked_set_action);
389 static void xlate_xbundle_set(struct xbundle *xbundle,
390 enum port_vlan_mode vlan_mode, int vlan,
391 unsigned long *trunks, bool use_priority_tags,
392 const struct bond *bond, const struct lacp *lacp,
394 static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
395 const struct netdev *netdev, const struct cfm *cfm,
396 const struct bfd *bfd, const struct lldp *lldp,
397 int stp_port_no, const struct rstp_port *rstp_port,
398 enum ofputil_port_config config,
399 enum ofputil_port_state state, bool is_tunnel,
401 static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
402 static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
403 static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
404 static void xlate_xbridge_copy(struct xbridge *);
405 static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
406 static void xlate_xport_copy(struct xbridge *, struct xbundle *,
408 static void xlate_xcfg_free(struct xlate_cfg *);
411 xlate_report(struct xlate_ctx *ctx, const char *s)
413 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
414 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
419 xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
421 list_init(&xbridge->xbundles);
422 hmap_init(&xbridge->xports);
423 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
424 hash_pointer(xbridge->ofproto, 0));
428 xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
430 list_init(&xbundle->xports);
431 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
432 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
433 hash_pointer(xbundle->ofbundle, 0));
437 xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
439 hmap_init(&xport->skb_priorities);
440 hmap_insert(&xcfg->xports, &xport->hmap_node,
441 hash_pointer(xport->ofport, 0));
442 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
443 hash_ofp_port(xport->ofp_port));
447 xlate_xbridge_set(struct xbridge *xbridge,
449 const struct mac_learning *ml, struct stp *stp,
450 struct rstp *rstp, const struct mcast_snooping *ms,
451 const struct mbridge *mbridge,
452 const struct dpif_sflow *sflow,
453 const struct dpif_ipfix *ipfix,
454 const struct netflow *netflow,
455 bool forward_bpdu, bool has_in_band,
457 bool variable_length_userdata,
458 size_t max_mpls_depth,
459 bool masked_set_action)
461 if (xbridge->ml != ml) {
462 mac_learning_unref(xbridge->ml);
463 xbridge->ml = mac_learning_ref(ml);
466 if (xbridge->ms != ms) {
467 mcast_snooping_unref(xbridge->ms);
468 xbridge->ms = mcast_snooping_ref(ms);
471 if (xbridge->mbridge != mbridge) {
472 mbridge_unref(xbridge->mbridge);
473 xbridge->mbridge = mbridge_ref(mbridge);
476 if (xbridge->sflow != sflow) {
477 dpif_sflow_unref(xbridge->sflow);
478 xbridge->sflow = dpif_sflow_ref(sflow);
481 if (xbridge->ipfix != ipfix) {
482 dpif_ipfix_unref(xbridge->ipfix);
483 xbridge->ipfix = dpif_ipfix_ref(ipfix);
486 if (xbridge->stp != stp) {
487 stp_unref(xbridge->stp);
488 xbridge->stp = stp_ref(stp);
491 if (xbridge->rstp != rstp) {
492 rstp_unref(xbridge->rstp);
493 xbridge->rstp = rstp_ref(rstp);
496 if (xbridge->netflow != netflow) {
497 netflow_unref(xbridge->netflow);
498 xbridge->netflow = netflow_ref(netflow);
501 xbridge->dpif = dpif;
502 xbridge->forward_bpdu = forward_bpdu;
503 xbridge->has_in_band = has_in_band;
504 xbridge->enable_recirc = enable_recirc;
505 xbridge->variable_length_userdata = variable_length_userdata;
506 xbridge->max_mpls_depth = max_mpls_depth;
507 xbridge->masked_set_action = masked_set_action;
511 xlate_xbundle_set(struct xbundle *xbundle,
512 enum port_vlan_mode vlan_mode, int vlan,
513 unsigned long *trunks, bool use_priority_tags,
514 const struct bond *bond, const struct lacp *lacp,
517 ovs_assert(xbundle->xbridge);
519 xbundle->vlan_mode = vlan_mode;
520 xbundle->vlan = vlan;
521 xbundle->trunks = trunks;
522 xbundle->use_priority_tags = use_priority_tags;
523 xbundle->floodable = floodable;
525 if (xbundle->bond != bond) {
526 bond_unref(xbundle->bond);
527 xbundle->bond = bond_ref(bond);
530 if (xbundle->lacp != lacp) {
531 lacp_unref(xbundle->lacp);
532 xbundle->lacp = lacp_ref(lacp);
537 xlate_xport_set(struct xport *xport, odp_port_t odp_port,
538 const struct netdev *netdev, const struct cfm *cfm,
539 const struct bfd *bfd, const struct lldp *lldp, int stp_port_no,
540 const struct rstp_port* rstp_port,
541 enum ofputil_port_config config, enum ofputil_port_state state,
542 bool is_tunnel, bool may_enable)
544 xport->config = config;
545 xport->state = state;
546 xport->stp_port_no = stp_port_no;
547 xport->is_tunnel = is_tunnel;
548 xport->may_enable = may_enable;
549 xport->odp_port = odp_port;
551 if (xport->rstp_port != rstp_port) {
552 rstp_port_unref(xport->rstp_port);
553 xport->rstp_port = rstp_port_ref(rstp_port);
556 if (xport->cfm != cfm) {
557 cfm_unref(xport->cfm);
558 xport->cfm = cfm_ref(cfm);
561 if (xport->bfd != bfd) {
562 bfd_unref(xport->bfd);
563 xport->bfd = bfd_ref(bfd);
566 if (xport->lldp != lldp) {
567 lldp_unref(xport->lldp);
568 xport->lldp = lldp_ref(lldp);
571 if (xport->netdev != netdev) {
572 netdev_close(xport->netdev);
573 xport->netdev = netdev_ref(netdev);
578 xlate_xbridge_copy(struct xbridge *xbridge)
580 struct xbundle *xbundle;
582 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
583 new_xbridge->ofproto = xbridge->ofproto;
584 new_xbridge->name = xstrdup(xbridge->name);
585 xlate_xbridge_init(new_xcfg, new_xbridge);
587 xlate_xbridge_set(new_xbridge,
588 xbridge->dpif, xbridge->ml, xbridge->stp,
589 xbridge->rstp, xbridge->ms, xbridge->mbridge,
590 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
591 xbridge->forward_bpdu,
592 xbridge->has_in_band, xbridge->enable_recirc,
593 xbridge->variable_length_userdata,
594 xbridge->max_mpls_depth, xbridge->masked_set_action);
595 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
596 xlate_xbundle_copy(new_xbridge, xbundle);
599 /* Copy xports which are not part of a xbundle */
600 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
601 if (!xport->xbundle) {
602 xlate_xport_copy(new_xbridge, NULL, xport);
608 xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
611 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
612 new_xbundle->ofbundle = xbundle->ofbundle;
613 new_xbundle->xbridge = xbridge;
614 new_xbundle->name = xstrdup(xbundle->name);
615 xlate_xbundle_init(new_xcfg, new_xbundle);
617 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
618 xbundle->vlan, xbundle->trunks,
619 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
621 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
622 xlate_xport_copy(xbridge, new_xbundle, xport);
627 xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
630 struct skb_priority_to_dscp *pdscp, *new_pdscp;
631 struct xport *new_xport = xzalloc(sizeof *xport);
632 new_xport->ofport = xport->ofport;
633 new_xport->ofp_port = xport->ofp_port;
634 new_xport->xbridge = xbridge;
635 xlate_xport_init(new_xcfg, new_xport);
637 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
638 xport->bfd, xport->lldp, xport->stp_port_no,
639 xport->rstp_port, xport->config, xport->state,
640 xport->is_tunnel, xport->may_enable);
643 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
645 new_xport->peer = peer;
646 new_xport->peer->peer = new_xport;
651 new_xport->xbundle = xbundle;
652 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
655 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
656 new_pdscp = xmalloc(sizeof *pdscp);
657 new_pdscp->skb_priority = pdscp->skb_priority;
658 new_pdscp->dscp = pdscp->dscp;
659 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
660 hash_int(new_pdscp->skb_priority, 0));
664 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
665 * configuration in xcfgp.
667 * This needs to be called after editing the xlate configuration.
669 * Functions that edit the new xlate configuration are
670 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
676 * edit_xlate_configuration();
678 * xlate_txn_commit(); */
680 xlate_txn_commit(void)
682 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
684 ovsrcu_set(&xcfgp, new_xcfg);
685 ovsrcu_synchronize();
686 xlate_xcfg_free(xcfg);
690 /* Copies the current xlate configuration in xcfgp to new_xcfg.
692 * This needs to be called prior to editing the xlate configuration. */
694 xlate_txn_start(void)
696 struct xbridge *xbridge;
697 struct xlate_cfg *xcfg;
699 ovs_assert(!new_xcfg);
701 new_xcfg = xmalloc(sizeof *new_xcfg);
702 hmap_init(&new_xcfg->xbridges);
703 hmap_init(&new_xcfg->xbundles);
704 hmap_init(&new_xcfg->xports);
706 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
711 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
712 xlate_xbridge_copy(xbridge);
718 xlate_xcfg_free(struct xlate_cfg *xcfg)
720 struct xbridge *xbridge, *next_xbridge;
726 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
727 xlate_xbridge_remove(xcfg, xbridge);
730 hmap_destroy(&xcfg->xbridges);
731 hmap_destroy(&xcfg->xbundles);
732 hmap_destroy(&xcfg->xports);
737 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
739 const struct mac_learning *ml, struct stp *stp,
740 struct rstp *rstp, const struct mcast_snooping *ms,
741 const struct mbridge *mbridge,
742 const struct dpif_sflow *sflow,
743 const struct dpif_ipfix *ipfix,
744 const struct netflow *netflow,
745 bool forward_bpdu, bool has_in_band, bool enable_recirc,
746 bool variable_length_userdata, size_t max_mpls_depth,
747 bool masked_set_action)
749 struct xbridge *xbridge;
751 ovs_assert(new_xcfg);
753 xbridge = xbridge_lookup(new_xcfg, ofproto);
755 xbridge = xzalloc(sizeof *xbridge);
756 xbridge->ofproto = ofproto;
758 xlate_xbridge_init(new_xcfg, xbridge);
762 xbridge->name = xstrdup(name);
764 xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
765 netflow, forward_bpdu, has_in_band, enable_recirc,
766 variable_length_userdata, max_mpls_depth,
771 xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
773 struct xbundle *xbundle, *next_xbundle;
774 struct xport *xport, *next_xport;
780 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
781 xlate_xport_remove(xcfg, xport);
784 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
785 xlate_xbundle_remove(xcfg, xbundle);
788 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
789 mac_learning_unref(xbridge->ml);
790 mcast_snooping_unref(xbridge->ms);
791 mbridge_unref(xbridge->mbridge);
792 dpif_sflow_unref(xbridge->sflow);
793 dpif_ipfix_unref(xbridge->ipfix);
794 stp_unref(xbridge->stp);
795 rstp_unref(xbridge->rstp);
796 hmap_destroy(&xbridge->xports);
802 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
804 struct xbridge *xbridge;
806 ovs_assert(new_xcfg);
808 xbridge = xbridge_lookup(new_xcfg, ofproto);
809 xlate_xbridge_remove(new_xcfg, xbridge);
813 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
814 const char *name, enum port_vlan_mode vlan_mode, int vlan,
815 unsigned long *trunks, bool use_priority_tags,
816 const struct bond *bond, const struct lacp *lacp,
819 struct xbundle *xbundle;
821 ovs_assert(new_xcfg);
823 xbundle = xbundle_lookup(new_xcfg, ofbundle);
825 xbundle = xzalloc(sizeof *xbundle);
826 xbundle->ofbundle = ofbundle;
827 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
829 xlate_xbundle_init(new_xcfg, xbundle);
833 xbundle->name = xstrdup(name);
835 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
836 use_priority_tags, bond, lacp, floodable);
840 xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
842 struct xport *xport, *next;
848 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
849 list_remove(&xport->bundle_node);
850 xport->xbundle = NULL;
853 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
854 list_remove(&xbundle->list_node);
855 bond_unref(xbundle->bond);
856 lacp_unref(xbundle->lacp);
862 xlate_bundle_remove(struct ofbundle *ofbundle)
864 struct xbundle *xbundle;
866 ovs_assert(new_xcfg);
868 xbundle = xbundle_lookup(new_xcfg, ofbundle);
869 xlate_xbundle_remove(new_xcfg, xbundle);
873 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
874 struct ofport_dpif *ofport, ofp_port_t ofp_port,
875 odp_port_t odp_port, const struct netdev *netdev,
876 const struct cfm *cfm, const struct bfd *bfd,
877 const struct lldp *lldp, struct ofport_dpif *peer,
878 int stp_port_no, const struct rstp_port *rstp_port,
879 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
880 enum ofputil_port_config config,
881 enum ofputil_port_state state, bool is_tunnel,
887 ovs_assert(new_xcfg);
889 xport = xport_lookup(new_xcfg, ofport);
891 xport = xzalloc(sizeof *xport);
892 xport->ofport = ofport;
893 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
894 xport->ofp_port = ofp_port;
896 xlate_xport_init(new_xcfg, xport);
899 ovs_assert(xport->ofp_port == ofp_port);
901 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, lldp,
902 stp_port_no, rstp_port, config, state, is_tunnel,
906 xport->peer->peer = NULL;
908 xport->peer = xport_lookup(new_xcfg, peer);
910 xport->peer->peer = xport;
913 if (xport->xbundle) {
914 list_remove(&xport->bundle_node);
916 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
917 if (xport->xbundle) {
918 list_insert(&xport->xbundle->xports, &xport->bundle_node);
921 clear_skb_priorities(xport);
922 for (i = 0; i < n_qdscp; i++) {
923 struct skb_priority_to_dscp *pdscp;
924 uint32_t skb_priority;
926 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
931 pdscp = xmalloc(sizeof *pdscp);
932 pdscp->skb_priority = skb_priority;
933 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
934 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
935 hash_int(pdscp->skb_priority, 0));
940 xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
947 xport->peer->peer = NULL;
951 if (xport->xbundle) {
952 list_remove(&xport->bundle_node);
955 clear_skb_priorities(xport);
956 hmap_destroy(&xport->skb_priorities);
958 hmap_remove(&xcfg->xports, &xport->hmap_node);
959 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
961 netdev_close(xport->netdev);
962 rstp_port_unref(xport->rstp_port);
963 cfm_unref(xport->cfm);
964 bfd_unref(xport->bfd);
965 lldp_unref(xport->lldp);
970 xlate_ofport_remove(struct ofport_dpif *ofport)
974 ovs_assert(new_xcfg);
976 xport = xport_lookup(new_xcfg, ofport);
977 xlate_xport_remove(new_xcfg, xport);
980 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
981 * returns the corresponding struct xport, or NULL if none is found. */
982 static struct xport *
983 xlate_lookup_xport(const struct dpif_backer *backer, const struct flow *flow)
985 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
987 return xport_lookup(xcfg, tnl_port_should_receive(flow)
988 ? tnl_port_receive(flow)
989 : odp_port_to_ofport(backer, flow->in_port.odp_port));
992 static struct ofproto_dpif *
993 xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
994 ofp_port_t *ofp_in_port, const struct xport **xportp)
996 struct ofproto_dpif *recv_ofproto = NULL;
997 struct ofproto_dpif *recirc_ofproto = NULL;
998 const struct xport *xport;
999 ofp_port_t in_port = OFPP_NONE;
1001 *xportp = xport = xlate_lookup_xport(backer, flow);
1004 recv_ofproto = xport->xbridge->ofproto;
1005 in_port = xport->ofp_port;
1008 /* When recirc_id is set in 'flow', checks whether the ofproto_dpif that
1009 * corresponds to the recirc_id is same as the receiving bridge. If they
1010 * are the same, uses the 'recv_ofproto' and keeps the 'ofp_in_port' as
1011 * assigned. Otherwise, uses the 'recirc_ofproto' that owns recirc_id and
1012 * assigns OFPP_NONE to 'ofp_in_port'. Doing this is in that, the
1013 * recirculated flow must be processced by the ofproto which originates
1014 * the recirculation, and as bridges can only see their own ports, the
1015 * in_port of the 'recv_ofproto' should not be passed to the
1018 * Admittedly, setting the 'ofp_in_port' to OFPP_NONE limits the
1019 * 'recirc_ofproto' from meaningfully matching on in_port of recirculated
1020 * flow, and should be fixed in the near future.
1022 * TODO: Restore the original patch port.
1024 if (recv_ofproto && flow->recirc_id) {
1025 recirc_ofproto = ofproto_dpif_recirc_get_ofproto(backer,
1027 if (recv_ofproto != recirc_ofproto) {
1028 *xportp = xport = NULL;
1029 in_port = OFPP_NONE;
1034 *ofp_in_port = in_port;
1037 return xport ? recv_ofproto : recirc_ofproto;
1040 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1041 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1042 struct ofproto_dpif *
1043 xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1044 ofp_port_t *ofp_in_port)
1046 const struct xport *xport;
1048 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1051 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1052 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
1053 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1054 * handles for those protocols if they're enabled. Caller may use the returned
1055 * pointers until quiescing, for longer term use additional references must
1058 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1061 xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1062 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1063 struct dpif_sflow **sflow, struct netflow **netflow,
1064 ofp_port_t *ofp_in_port)
1066 struct ofproto_dpif *ofproto;
1067 const struct xport *xport;
1069 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1076 *ofprotop = ofproto;
1080 *ipfix = xport ? xport->xbridge->ipfix : NULL;
1084 *sflow = xport ? xport->xbridge->sflow : NULL;
1088 *netflow = xport ? xport->xbridge->netflow : NULL;
1094 static struct xbridge *
1095 xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
1097 struct hmap *xbridges;
1098 struct xbridge *xbridge;
1100 if (!ofproto || !xcfg) {
1104 xbridges = &xcfg->xbridges;
1106 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
1108 if (xbridge->ofproto == ofproto) {
1115 static struct xbundle *
1116 xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
1118 struct hmap *xbundles;
1119 struct xbundle *xbundle;
1121 if (!ofbundle || !xcfg) {
1125 xbundles = &xcfg->xbundles;
1127 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
1129 if (xbundle->ofbundle == ofbundle) {
1136 static struct xport *
1137 xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
1139 struct hmap *xports;
1140 struct xport *xport;
1142 if (!ofport || !xcfg) {
1146 xports = &xcfg->xports;
1148 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
1150 if (xport->ofport == ofport) {
1157 static struct stp_port *
1158 xport_get_stp_port(const struct xport *xport)
1160 return xport->xbridge->stp && xport->stp_port_no != -1
1161 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1166 xport_stp_learn_state(const struct xport *xport)
1168 struct stp_port *sp = xport_get_stp_port(xport);
1170 ? stp_learn_in_state(stp_port_get_state(sp))
1175 xport_stp_forward_state(const struct xport *xport)
1177 struct stp_port *sp = xport_get_stp_port(xport);
1179 ? stp_forward_in_state(stp_port_get_state(sp))
1184 xport_stp_should_forward_bpdu(const struct xport *xport)
1186 struct stp_port *sp = xport_get_stp_port(xport);
1187 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
1190 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1191 * were used to make the determination.*/
1193 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1195 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1196 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1197 return is_stp(flow);
1201 stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1203 struct stp_port *sp = xport_get_stp_port(xport);
1204 struct dp_packet payload = *packet;
1205 struct eth_header *eth = dp_packet_data(&payload);
1207 /* Sink packets on ports that have STP disabled when the bridge has
1209 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1213 /* Trim off padding on payload. */
1214 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1215 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1218 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1219 stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
1223 static enum rstp_state
1224 xport_get_rstp_port_state(const struct xport *xport)
1226 return xport->rstp_port
1227 ? rstp_port_get_state(xport->rstp_port)
1232 xport_rstp_learn_state(const struct xport *xport)
1234 return xport->xbridge->rstp && xport->rstp_port
1235 ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1240 xport_rstp_forward_state(const struct xport *xport)
1242 return xport->xbridge->rstp && xport->rstp_port
1243 ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1248 xport_rstp_should_manage_bpdu(const struct xport *xport)
1250 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
1254 rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1256 struct dp_packet payload = *packet;
1257 struct eth_header *eth = dp_packet_data(&payload);
1259 /* Sink packets on ports that have no RSTP. */
1260 if (!xport->rstp_port) {
1264 /* Trim off padding on payload. */
1265 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1266 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1269 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1270 rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
1271 dp_packet_size(&payload));
1275 static struct xport *
1276 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1278 struct xport *xport;
1280 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1282 if (xport->ofp_port == ofp_port) {
1290 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1292 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1293 return xport ? xport->odp_port : ODPP_NONE;
1297 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1299 struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1300 return xport && xport->may_enable;
1303 static struct ofputil_bucket *
1304 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1308 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1310 struct group_dpif *group;
1312 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1313 struct ofputil_bucket *bucket;
1315 bucket = group_first_live_bucket(ctx, group, depth);
1316 group_dpif_unref(group);
1317 return bucket == NULL;
1323 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1326 bucket_is_alive(const struct xlate_ctx *ctx,
1327 struct ofputil_bucket *bucket, int depth)
1329 if (depth >= MAX_LIVENESS_RECURSION) {
1330 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1332 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1333 MAX_LIVENESS_RECURSION);
1337 return (!ofputil_bucket_has_liveness(bucket)
1338 || (bucket->watch_port != OFPP_ANY
1339 && odp_port_is_alive(ctx, bucket->watch_port))
1340 || (bucket->watch_group != OFPG_ANY
1341 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
1344 static struct ofputil_bucket *
1345 group_first_live_bucket(const struct xlate_ctx *ctx,
1346 const struct group_dpif *group, int depth)
1348 struct ofputil_bucket *bucket;
1349 const struct ovs_list *buckets;
1351 group_dpif_get_buckets(group, &buckets);
1352 LIST_FOR_EACH (bucket, list_node, buckets) {
1353 if (bucket_is_alive(ctx, bucket, depth)) {
1361 static struct ofputil_bucket *
1362 group_best_live_bucket(const struct xlate_ctx *ctx,
1363 const struct group_dpif *group,
1366 struct ofputil_bucket *best_bucket = NULL;
1367 uint32_t best_score = 0;
1370 struct ofputil_bucket *bucket;
1371 const struct ovs_list *buckets;
1373 group_dpif_get_buckets(group, &buckets);
1374 LIST_FOR_EACH (bucket, list_node, buckets) {
1375 if (bucket_is_alive(ctx, bucket, 0)) {
1376 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
1377 if (score >= best_score) {
1378 best_bucket = bucket;
1389 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
1391 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1392 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1396 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1398 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1401 static mirror_mask_t
1402 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1404 return xbundle != &ofpp_none_bundle
1405 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1409 static mirror_mask_t
1410 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
1412 return xbundle != &ofpp_none_bundle
1413 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1417 static mirror_mask_t
1418 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
1420 return xbundle != &ofpp_none_bundle
1421 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1425 static struct xbundle *
1426 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1427 bool warn, struct xport **in_xportp)
1429 struct xport *xport;
1431 /* Find the port and bundle for the received packet. */
1432 xport = get_ofp_port(xbridge, in_port);
1436 if (xport && xport->xbundle) {
1437 return xport->xbundle;
1440 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1441 * which a controller may use as the ingress port for traffic that
1442 * it is sourcing. */
1443 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
1444 return &ofpp_none_bundle;
1447 /* Odd. A few possible reasons here:
1449 * - We deleted a port but there are still a few packets queued up
1452 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1453 * we don't know about.
1455 * - The ofproto client didn't configure the port as part of a bundle.
1456 * This is particularly likely to happen if a packet was received on the
1457 * port after it was created, but before the client had a chance to
1458 * configure its bundle.
1461 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1463 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1464 "port %"PRIu16, xbridge->name, in_port);
1470 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
1472 const struct xbridge *xbridge = ctx->xbridge;
1473 mirror_mask_t mirrors;
1474 struct xbundle *in_xbundle;
1478 mirrors = ctx->xout->mirrors;
1479 ctx->xout->mirrors = 0;
1481 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
1482 ctx->xin->packet != NULL, NULL);
1486 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
1488 /* Drop frames on bundles reserved for mirroring. */
1489 if (xbundle_mirror_out(xbridge, in_xbundle)) {
1490 if (ctx->xin->packet != NULL) {
1491 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1492 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1493 "%s, which is reserved exclusively for mirroring",
1494 ctx->xbridge->name, in_xbundle->name);
1496 ofpbuf_clear(ctx->xout->odp_actions);
1501 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
1502 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1505 vlan = input_vid_to_vlan(in_xbundle, vid);
1511 /* Restore the original packet before adding the mirror actions. */
1512 ctx->xin->flow = *orig_flow;
1515 mirror_mask_t dup_mirrors;
1516 struct ofbundle *out;
1517 unsigned long *vlans;
1522 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1523 &vlans, &dup_mirrors, &out, &out_vlan);
1524 ovs_assert(has_mirror);
1527 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1529 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
1532 if (!vlan_mirrored) {
1533 mirrors = zero_rightmost_1bit(mirrors);
1537 mirrors &= ~dup_mirrors;
1538 ctx->xout->mirrors |= dup_mirrors;
1540 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1541 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1543 output_normal(ctx, out_xbundle, vlan);
1545 } else if (vlan != out_vlan
1546 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
1547 struct xbundle *xbundle;
1549 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1550 if (xbundle_includes_vlan(xbundle, out_vlan)
1551 && !xbundle_mirror_out(xbridge, xbundle)) {
1552 output_normal(ctx, xbundle, out_vlan);
1559 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1560 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1561 * the bundle on which the packet was received, returns the VLAN to which the
1564 * Both 'vid' and the return value are in the range 0...4095. */
1566 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1568 switch (in_xbundle->vlan_mode) {
1569 case PORT_VLAN_ACCESS:
1570 return in_xbundle->vlan;
1573 case PORT_VLAN_TRUNK:
1576 case PORT_VLAN_NATIVE_UNTAGGED:
1577 case PORT_VLAN_NATIVE_TAGGED:
1578 return vid ? vid : in_xbundle->vlan;
1585 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1586 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1589 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1590 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1593 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1595 /* Allow any VID on the OFPP_NONE port. */
1596 if (in_xbundle == &ofpp_none_bundle) {
1600 switch (in_xbundle->vlan_mode) {
1601 case PORT_VLAN_ACCESS:
1604 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1605 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1606 "packet received on port %s configured as VLAN "
1607 "%"PRIu16" access port", vid, in_xbundle->name,
1614 case PORT_VLAN_NATIVE_UNTAGGED:
1615 case PORT_VLAN_NATIVE_TAGGED:
1617 /* Port must always carry its native VLAN. */
1621 case PORT_VLAN_TRUNK:
1622 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1624 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1625 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1626 "received on port %s not configured for trunking "
1627 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1639 /* Given 'vlan', the VLAN that a packet belongs to, and
1640 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1641 * that should be included in the 802.1Q header. (If the return value is 0,
1642 * then the 802.1Q header should only be included in the packet if there is a
1645 * Both 'vlan' and the return value are in the range 0...4095. */
1647 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1649 switch (out_xbundle->vlan_mode) {
1650 case PORT_VLAN_ACCESS:
1653 case PORT_VLAN_TRUNK:
1654 case PORT_VLAN_NATIVE_TAGGED:
1657 case PORT_VLAN_NATIVE_UNTAGGED:
1658 return vlan == out_xbundle->vlan ? 0 : vlan;
1666 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1669 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1671 ovs_be16 tci, old_tci;
1672 struct xport *xport;
1674 vid = output_vlan_to_vid(out_xbundle, vlan);
1675 if (list_is_empty(&out_xbundle->xports)) {
1676 /* Partially configured bundle with no slaves. Drop the packet. */
1678 } else if (!out_xbundle->bond) {
1679 ctx->use_recirc = false;
1680 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1683 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1684 struct flow_wildcards *wc = &ctx->xout->wc;
1685 struct xlate_recirc *xr = &ctx->recirc;
1686 struct ofport_dpif *ofport;
1688 if (ctx->xbridge->enable_recirc) {
1689 ctx->use_recirc = bond_may_recirc(
1690 out_xbundle->bond, &xr->recirc_id, &xr->hash_basis);
1692 if (ctx->use_recirc) {
1693 /* Only TCP mode uses recirculation. */
1694 xr->hash_alg = OVS_HASH_ALG_L4;
1695 bond_update_post_recirc_rules(out_xbundle->bond, false);
1697 /* Recirculation does not require unmasking hash fields. */
1702 ofport = bond_choose_output_slave(out_xbundle->bond,
1703 &ctx->xin->flow, wc, vid);
1704 xport = xport_lookup(xcfg, ofport);
1707 /* No slaves enabled, so drop packet. */
1711 /* If ctx->xout->use_recirc is set, the main thread will handle stats
1712 * accounting for this bond. */
1713 if (!ctx->use_recirc) {
1714 if (ctx->xin->resubmit_stats) {
1715 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1716 ctx->xin->resubmit_stats->n_bytes);
1718 if (ctx->xin->xcache) {
1719 struct xc_entry *entry;
1722 flow = &ctx->xin->flow;
1723 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1724 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1725 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1726 entry->u.bond.vid = vid;
1731 old_tci = *flow_tci;
1733 if (tci || out_xbundle->use_priority_tags) {
1734 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1736 tci |= htons(VLAN_CFI);
1741 compose_output_action(ctx, xport->ofp_port);
1742 *flow_tci = old_tci;
1745 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1746 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1747 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1749 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1751 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1755 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1756 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1760 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1761 if (flow->nw_proto == ARP_OP_REPLY) {
1763 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1764 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1765 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1767 return flow->nw_src == flow->nw_dst;
1773 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1774 * dropped. Returns true if they may be forwarded, false if they should be
1777 * 'in_port' must be the xport that corresponds to flow->in_port.
1778 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1780 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1781 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1782 * checked by input_vid_is_valid().
1784 * May also add tags to '*tags', although the current implementation only does
1785 * so in one special case.
1788 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1791 struct xbundle *in_xbundle = in_port->xbundle;
1792 const struct xbridge *xbridge = ctx->xbridge;
1793 struct flow *flow = &ctx->xin->flow;
1795 /* Drop frames for reserved multicast addresses
1796 * only if forward_bpdu option is absent. */
1797 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1798 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1802 if (in_xbundle->bond) {
1803 struct mac_entry *mac;
1805 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1811 xlate_report(ctx, "bonding refused admissibility, dropping");
1814 case BV_DROP_IF_MOVED:
1815 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1816 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1818 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
1819 && (!is_gratuitous_arp(flow, &ctx->xout->wc)
1820 || mac_entry_is_grat_arp_locked(mac))) {
1821 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1822 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1826 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1834 /* Checks whether a MAC learning update is necessary for MAC learning table
1835 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1838 * Most packets processed through the MAC learning table do not actually
1839 * change it in any way. This function requires only a read lock on the MAC
1840 * learning table, so it is much cheaper in this common case.
1842 * Keep the code here synchronized with that in update_learning_table__()
1845 is_mac_learning_update_needed(const struct mac_learning *ml,
1846 const struct flow *flow,
1847 struct flow_wildcards *wc,
1848 int vlan, struct xbundle *in_xbundle)
1849 OVS_REQ_RDLOCK(ml->rwlock)
1851 struct mac_entry *mac;
1853 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1857 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1858 if (!mac || mac_entry_age(ml, mac)) {
1862 if (is_gratuitous_arp(flow, wc)) {
1863 /* We don't want to learn from gratuitous ARP packets that are
1864 * reflected back over bond slaves so we lock the learning table. */
1865 if (!in_xbundle->bond) {
1867 } else if (mac_entry_is_grat_arp_locked(mac)) {
1872 return mac_entry_get_port(ml, mac) != in_xbundle->ofbundle;
1876 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1877 * received on 'in_xbundle' in 'vlan'.
1879 * This code repeats all the checks in is_mac_learning_update_needed() because
1880 * the lock was released between there and here and thus the MAC learning state
1881 * could have changed.
1883 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1886 update_learning_table__(const struct xbridge *xbridge,
1887 const struct flow *flow, struct flow_wildcards *wc,
1888 int vlan, struct xbundle *in_xbundle)
1889 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1891 struct mac_entry *mac;
1893 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1897 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1898 if (is_gratuitous_arp(flow, wc)) {
1899 /* We don't want to learn from gratuitous ARP packets that are
1900 * reflected back over bond slaves so we lock the learning table. */
1901 if (!in_xbundle->bond) {
1902 mac_entry_set_grat_arp_lock(mac);
1903 } else if (mac_entry_is_grat_arp_locked(mac)) {
1908 if (mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle) {
1909 /* The log messages here could actually be useful in debugging,
1910 * so keep the rate limit relatively high. */
1911 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1913 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1914 "on port %s in VLAN %d",
1915 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1916 in_xbundle->name, vlan);
1918 mac_entry_set_port(xbridge->ml, mac, in_xbundle->ofbundle);
1923 update_learning_table(const struct xbridge *xbridge,
1924 const struct flow *flow, struct flow_wildcards *wc,
1925 int vlan, struct xbundle *in_xbundle)
1929 /* Don't learn the OFPP_NONE port. */
1930 if (in_xbundle == &ofpp_none_bundle) {
1934 /* First try the common case: no change to MAC learning table. */
1935 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1936 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1938 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1941 /* Slow path: MAC learning table might need an update. */
1942 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1943 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1944 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1948 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1949 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
1951 update_mcast_snooping_table__(const struct xbridge *xbridge,
1952 const struct flow *flow,
1953 struct mcast_snooping *ms,
1954 ovs_be32 ip4, int vlan,
1955 struct xbundle *in_xbundle)
1956 OVS_REQ_WRLOCK(ms->rwlock)
1958 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
1960 switch (ntohs(flow->tp_src)) {
1961 case IGMP_HOST_MEMBERSHIP_REPORT:
1962 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1963 if (mcast_snooping_add_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
1964 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
1965 IP_FMT" is on port %s in VLAN %d",
1966 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
1969 case IGMP_HOST_LEAVE_MESSAGE:
1970 if (mcast_snooping_leave_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
1971 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
1972 IP_FMT" is on port %s in VLAN %d",
1973 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
1976 case IGMP_HOST_MEMBERSHIP_QUERY:
1977 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
1978 in_xbundle->ofbundle)) {
1979 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
1980 IP_FMT" is on port %s in VLAN %d",
1981 xbridge->name, IP_ARGS(flow->nw_src),
1982 in_xbundle->name, vlan);
1988 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1989 * was received on 'in_xbundle' in 'vlan'. */
1991 update_mcast_snooping_table(const struct xbridge *xbridge,
1992 const struct flow *flow, int vlan,
1993 struct xbundle *in_xbundle)
1995 struct mcast_snooping *ms = xbridge->ms;
1996 struct xlate_cfg *xcfg;
1997 struct xbundle *mcast_xbundle;
1998 struct mcast_port_bundle *fport;
2000 /* Don't learn the OFPP_NONE port. */
2001 if (in_xbundle == &ofpp_none_bundle) {
2005 /* Don't learn from flood ports */
2006 mcast_xbundle = NULL;
2007 ovs_rwlock_wrlock(&ms->rwlock);
2008 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2009 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2010 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2011 if (mcast_xbundle == in_xbundle) {
2016 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
2017 update_mcast_snooping_table__(xbridge, flow, ms, flow->igmp_group_ip4,
2020 ovs_rwlock_unlock(&ms->rwlock);
2023 /* send the packet to ports having the multicast group learned */
2025 xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2026 struct mcast_snooping *ms OVS_UNUSED,
2027 struct mcast_group *grp,
2028 struct xbundle *in_xbundle, uint16_t vlan)
2029 OVS_REQ_RDLOCK(ms->rwlock)
2031 struct xlate_cfg *xcfg;
2032 struct mcast_group_bundle *b;
2033 struct xbundle *mcast_xbundle;
2035 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2036 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2037 mcast_xbundle = xbundle_lookup(xcfg, b->port);
2038 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2039 xlate_report(ctx, "forwarding to mcast group port");
2040 output_normal(ctx, mcast_xbundle, vlan);
2041 } else if (!mcast_xbundle) {
2042 xlate_report(ctx, "mcast group port is unknown, dropping");
2044 xlate_report(ctx, "mcast group port is input port, dropping");
2049 /* send the packet to ports connected to multicast routers */
2051 xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2052 struct mcast_snooping *ms,
2053 struct xbundle *in_xbundle, uint16_t vlan)
2054 OVS_REQ_RDLOCK(ms->rwlock)
2056 struct xlate_cfg *xcfg;
2057 struct mcast_mrouter_bundle *mrouter;
2058 struct xbundle *mcast_xbundle;
2060 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2061 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2062 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2063 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2064 xlate_report(ctx, "forwarding to mcast router port");
2065 output_normal(ctx, mcast_xbundle, vlan);
2066 } else if (!mcast_xbundle) {
2067 xlate_report(ctx, "mcast router port is unknown, dropping");
2069 xlate_report(ctx, "mcast router port is input port, dropping");
2074 /* send the packet to ports flagged to be flooded */
2076 xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2077 struct mcast_snooping *ms,
2078 struct xbundle *in_xbundle, uint16_t vlan)
2079 OVS_REQ_RDLOCK(ms->rwlock)
2081 struct xlate_cfg *xcfg;
2082 struct mcast_port_bundle *fport;
2083 struct xbundle *mcast_xbundle;
2085 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2086 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2087 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2088 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2089 xlate_report(ctx, "forwarding to mcast flood port");
2090 output_normal(ctx, mcast_xbundle, vlan);
2091 } else if (!mcast_xbundle) {
2092 xlate_report(ctx, "mcast flood port is unknown, dropping");
2094 xlate_report(ctx, "mcast flood port is input port, dropping");
2099 /* forward the Reports to configured ports */
2101 xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
2102 struct mcast_snooping *ms,
2103 struct xbundle *in_xbundle, uint16_t vlan)
2104 OVS_REQ_RDLOCK(ms->rwlock)
2106 struct xlate_cfg *xcfg;
2107 struct mcast_port_bundle *rport;
2108 struct xbundle *mcast_xbundle;
2110 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2111 LIST_FOR_EACH(rport, node, &ms->rport_list) {
2112 mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2113 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2114 xlate_report(ctx, "forwarding Report to mcast flagged port");
2115 output_normal(ctx, mcast_xbundle, vlan);
2116 } else if (!mcast_xbundle) {
2117 xlate_report(ctx, "mcast port is unknown, dropping the Report");
2119 xlate_report(ctx, "mcast port is input port, dropping the Report");
2125 xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2128 struct xbundle *xbundle;
2130 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2131 if (xbundle != in_xbundle
2132 && xbundle_includes_vlan(xbundle, vlan)
2133 && xbundle->floodable
2134 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2135 output_normal(ctx, xbundle, vlan);
2138 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2142 xlate_normal(struct xlate_ctx *ctx)
2144 struct flow_wildcards *wc = &ctx->xout->wc;
2145 struct flow *flow = &ctx->xin->flow;
2146 struct xbundle *in_xbundle;
2147 struct xport *in_port;
2148 struct mac_entry *mac;
2153 ctx->xout->has_normal = true;
2155 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2156 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2157 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2159 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
2160 ctx->xin->packet != NULL, &in_port);
2162 xlate_report(ctx, "no input bundle, dropping");
2166 /* Drop malformed frames. */
2167 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
2168 !(flow->vlan_tci & htons(VLAN_CFI))) {
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 with partial "
2172 "VLAN tag received on port %s",
2173 ctx->xbridge->name, in_xbundle->name);
2175 xlate_report(ctx, "partial VLAN tag, dropping");
2179 /* Drop frames on bundles reserved for mirroring. */
2180 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
2181 if (ctx->xin->packet != NULL) {
2182 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2183 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2184 "%s, which is reserved exclusively for mirroring",
2185 ctx->xbridge->name, in_xbundle->name);
2187 xlate_report(ctx, "input port is mirror output port, dropping");
2192 vid = vlan_tci_to_vid(flow->vlan_tci);
2193 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
2194 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2197 vlan = input_vid_to_vlan(in_xbundle, vid);
2199 /* Check other admissibility requirements. */
2200 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2204 /* Learn source MAC. */
2205 if (ctx->xin->may_learn) {
2206 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
2208 if (ctx->xin->xcache) {
2209 struct xc_entry *entry;
2211 /* Save enough info to update mac learning table later. */
2212 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
2213 entry->u.normal.ofproto = ctx->xbridge->ofproto;
2214 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2215 entry->u.normal.vlan = vlan;
2218 /* Determine output bundle. */
2219 if (mcast_snooping_enabled(ctx->xbridge->ms)
2220 && !eth_addr_is_broadcast(flow->dl_dst)
2221 && eth_addr_is_multicast(flow->dl_dst)
2222 && flow->dl_type == htons(ETH_TYPE_IP)) {
2223 struct mcast_snooping *ms = ctx->xbridge->ms;
2224 struct mcast_group *grp;
2226 if (flow->nw_proto == IPPROTO_IGMP) {
2227 if (ctx->xin->may_learn) {
2228 if (mcast_snooping_is_membership(flow->tp_src) ||
2229 mcast_snooping_is_query(flow->tp_src)) {
2230 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2235 if (mcast_snooping_is_membership(flow->tp_src)) {
2236 ovs_rwlock_rdlock(&ms->rwlock);
2237 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2238 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2239 * forward IGMP Membership Reports only to those ports where
2240 * multicast routers are attached. Alternatively stated: a
2241 * snooping switch should not forward IGMP Membership Reports
2242 * to ports on which only hosts are attached.
2243 * An administrative control may be provided to override this
2244 * restriction, allowing the report messages to be flooded to
2246 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2247 ovs_rwlock_unlock(&ms->rwlock);
2249 xlate_report(ctx, "multicast traffic, flooding");
2250 xlate_normal_flood(ctx, in_xbundle, vlan);
2254 if (ip_is_local_multicast(flow->nw_dst)) {
2255 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2256 * address in the 224.0.0.x range which are not IGMP must
2257 * be forwarded on all ports */
2258 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2259 xlate_normal_flood(ctx, in_xbundle, vlan);
2264 /* forwarding to group base ports */
2265 ovs_rwlock_rdlock(&ms->rwlock);
2266 grp = mcast_snooping_lookup(ms, flow->nw_dst, vlan);
2268 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2269 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2270 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2272 if (mcast_snooping_flood_unreg(ms)) {
2273 xlate_report(ctx, "unregistered multicast, flooding");
2274 xlate_normal_flood(ctx, in_xbundle, vlan);
2276 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2277 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2280 ovs_rwlock_unlock(&ms->rwlock);
2282 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2283 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
2284 mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
2285 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2288 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2289 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2290 if (mac_xbundle && mac_xbundle != in_xbundle) {
2291 xlate_report(ctx, "forwarding to learned port");
2292 output_normal(ctx, mac_xbundle, vlan);
2293 } else if (!mac_xbundle) {
2294 xlate_report(ctx, "learned port is unknown, dropping");
2296 xlate_report(ctx, "learned port is input port, dropping");
2299 xlate_report(ctx, "no learned MAC for destination, flooding");
2300 xlate_normal_flood(ctx, in_xbundle, vlan);
2305 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
2306 * the number of packets out of UINT32_MAX to sample. The given
2307 * cookie is passed back in the callback for each sampled packet.
2310 compose_sample_action(const struct xbridge *xbridge,
2311 struct ofpbuf *odp_actions,
2312 const struct flow *flow,
2313 const uint32_t probability,
2314 const union user_action_cookie *cookie,
2315 const size_t cookie_size,
2316 const odp_port_t tunnel_out_port)
2318 size_t sample_offset, actions_offset;
2319 odp_port_t odp_port;
2323 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
2325 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
2327 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
2329 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
2330 pid = dpif_port_get_pid(xbridge->dpif, odp_port,
2331 flow_hash_5tuple(flow, 0));
2332 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2333 tunnel_out_port, odp_actions);
2335 nl_msg_end_nested(odp_actions, actions_offset);
2336 nl_msg_end_nested(odp_actions, sample_offset);
2337 return cookie_offset;
2341 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
2342 odp_port_t odp_port, unsigned int n_outputs,
2343 union user_action_cookie *cookie)
2347 cookie->type = USER_ACTION_COOKIE_SFLOW;
2348 cookie->sflow.vlan_tci = vlan_tci;
2350 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2351 * port information") for the interpretation of cookie->output. */
2352 switch (n_outputs) {
2354 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2355 cookie->sflow.output = 0x40000000 | 256;
2359 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
2361 cookie->sflow.output = ifindex;
2366 /* 0x80000000 means "multiple output ports. */
2367 cookie->sflow.output = 0x80000000 | n_outputs;
2372 /* Compose SAMPLE action for sFlow bridge sampling. */
2374 compose_sflow_action(const struct xbridge *xbridge,
2375 struct ofpbuf *odp_actions,
2376 const struct flow *flow,
2377 odp_port_t odp_port)
2379 uint32_t probability;
2380 union user_action_cookie cookie;
2382 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
2386 probability = dpif_sflow_get_probability(xbridge->sflow);
2387 compose_sflow_cookie(xbridge, htons(0), odp_port,
2388 odp_port == ODPP_NONE ? 0 : 1, &cookie);
2390 return compose_sample_action(xbridge, odp_actions, flow, probability,
2391 &cookie, sizeof cookie.sflow, ODPP_NONE);
2395 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
2396 uint32_t obs_domain_id, uint32_t obs_point_id,
2397 union user_action_cookie *cookie)
2399 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
2400 cookie->flow_sample.probability = probability;
2401 cookie->flow_sample.collector_set_id = collector_set_id;
2402 cookie->flow_sample.obs_domain_id = obs_domain_id;
2403 cookie->flow_sample.obs_point_id = obs_point_id;
2407 compose_ipfix_cookie(union user_action_cookie *cookie,
2408 odp_port_t output_odp_port)
2410 cookie->type = USER_ACTION_COOKIE_IPFIX;
2411 cookie->ipfix.output_odp_port = output_odp_port;
2414 /* Compose SAMPLE action for IPFIX bridge sampling. */
2416 compose_ipfix_action(const struct xbridge *xbridge,
2417 struct ofpbuf *odp_actions,
2418 const struct flow *flow,
2419 odp_port_t output_odp_port)
2421 uint32_t probability;
2422 union user_action_cookie cookie;
2423 odp_port_t tunnel_out_port = ODPP_NONE;
2425 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
2429 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2431 if (output_odp_port == ODPP_NONE &&
2432 !dpif_ipfix_get_bridge_exporter_input_sampling(xbridge->ipfix)) {
2436 /* For output case, output_odp_port is valid*/
2437 if (output_odp_port != ODPP_NONE) {
2438 if (!dpif_ipfix_get_bridge_exporter_output_sampling(xbridge->ipfix)) {
2441 /* If tunnel sampling is enabled, put an additional option attribute:
2442 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2444 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(xbridge->ipfix) &&
2445 dpif_ipfix_get_tunnel_port(xbridge->ipfix, output_odp_port) ) {
2446 tunnel_out_port = output_odp_port;
2450 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
2451 compose_ipfix_cookie(&cookie, output_odp_port);
2453 compose_sample_action(xbridge, odp_actions, flow, probability,
2454 &cookie, sizeof cookie.ipfix, tunnel_out_port);
2457 /* SAMPLE action for sFlow must be first action in any given list of
2458 * actions. At this point we do not have all information required to
2459 * build it. So try to build sample action as complete as possible. */
2461 add_sflow_action(struct xlate_ctx *ctx)
2463 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
2464 ctx->xout->odp_actions,
2465 &ctx->xin->flow, ODPP_NONE);
2466 ctx->sflow_odp_port = 0;
2467 ctx->sflow_n_outputs = 0;
2470 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
2471 * of actions, eventually after the SAMPLE action for sFlow. */
2473 add_ipfix_action(struct xlate_ctx *ctx)
2475 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2476 &ctx->xin->flow, ODPP_NONE);
2480 add_ipfix_output_action(struct xlate_ctx *ctx, odp_port_t port)
2482 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2483 &ctx->xin->flow, port);
2486 /* Fix SAMPLE action according to data collected while composing ODP actions.
2487 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
2488 * USERSPACE action's user-cookie which is required for sflow. */
2490 fix_sflow_action(struct xlate_ctx *ctx)
2492 const struct flow *base = &ctx->base_flow;
2493 union user_action_cookie *cookie;
2495 if (!ctx->user_cookie_offset) {
2499 cookie = ofpbuf_at(ctx->xout->odp_actions, ctx->user_cookie_offset,
2500 sizeof cookie->sflow);
2501 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2503 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
2504 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
2507 static enum slow_path_reason
2508 process_special(struct xlate_ctx *ctx, const struct flow *flow,
2509 const struct xport *xport, const struct dp_packet *packet)
2511 struct flow_wildcards *wc = &ctx->xout->wc;
2512 const struct xbridge *xbridge = ctx->xbridge;
2516 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
2518 cfm_process_heartbeat(xport->cfm, packet);
2521 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
2523 bfd_process_packet(xport->bfd, flow, packet);
2524 /* If POLL received, immediately sends FINAL back. */
2525 if (bfd_should_send_packet(xport->bfd)) {
2526 ofproto_dpif_monitor_port_send_soon(xport->ofport);
2530 } else if (xport->xbundle && xport->xbundle->lacp
2531 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2533 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
2536 } else if ((xbridge->stp || xbridge->rstp) &&
2537 stp_should_process_flow(flow, wc)) {
2540 ? stp_process_packet(xport, packet)
2541 : rstp_process_packet(xport, packet);
2544 } else if (xport->lldp && lldp_should_process_flow(flow)) {
2546 lldp_process_packet(xport->lldp, packet);
2555 tnl_route_lookup_flow(const struct flow *oflow,
2556 ovs_be32 *ip, struct xport **out_port)
2558 char out_dev[IFNAMSIZ];
2559 struct xbridge *xbridge;
2560 struct xlate_cfg *xcfg;
2563 if (!ovs_router_lookup(oflow->tunnel.ip_dst, out_dev, &gw)) {
2570 *ip = oflow->tunnel.ip_dst;
2573 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2576 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
2577 if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
2580 HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
2581 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
2592 xlate_flood_packet(struct xbridge *xbridge, struct dp_packet *packet)
2594 struct ofpact_output output;
2597 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
2598 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
2599 flow_extract(packet, &flow);
2600 flow.in_port.ofp_port = OFPP_NONE;
2601 output.port = OFPP_FLOOD;
2604 return ofproto_dpif_execute_actions(xbridge->ofproto, &flow, NULL,
2605 &output.ofpact, sizeof output,
2610 tnl_send_arp_request(const struct xport *out_dev, const uint8_t eth_src[ETH_ADDR_LEN],
2611 ovs_be32 ip_src, ovs_be32 ip_dst)
2613 struct xbridge *xbridge = out_dev->xbridge;
2614 struct dp_packet packet;
2616 dp_packet_init(&packet, 0);
2617 compose_arp(&packet, eth_src, ip_src, ip_dst);
2619 xlate_flood_packet(xbridge, &packet);
2620 dp_packet_uninit(&packet);
2624 build_tunnel_send(const struct xlate_ctx *ctx, const struct xport *xport,
2625 const struct flow *flow, odp_port_t tunnel_odp_port)
2627 struct ovs_action_push_tnl tnl_push_data;
2628 struct xport *out_dev = NULL;
2629 ovs_be32 s_ip, d_ip = 0;
2630 uint8_t smac[ETH_ADDR_LEN];
2631 uint8_t dmac[ETH_ADDR_LEN];
2634 err = tnl_route_lookup_flow(flow, &d_ip, &out_dev);
2639 /* Use mac addr of bridge port of the peer. */
2640 err = netdev_get_etheraddr(out_dev->netdev, smac);
2645 err = netdev_get_in4(out_dev->netdev, (struct in_addr *) &s_ip, NULL);
2650 err = tnl_arp_lookup(out_dev->xbridge->name, d_ip, dmac);
2652 tnl_send_arp_request(out_dev, smac, s_ip, d_ip);
2655 if (ctx->xin->xcache) {
2656 struct xc_entry *entry;
2658 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_ARP);
2659 ovs_strlcpy(entry->u.tnl_arp_cache.br_name, out_dev->xbridge->name,
2660 sizeof entry->u.tnl_arp_cache.br_name);
2661 entry->u.tnl_arp_cache.d_ip = d_ip;
2663 err = tnl_port_build_header(xport->ofport, flow,
2664 dmac, smac, s_ip, &tnl_push_data);
2668 tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
2669 tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
2670 odp_put_tnl_push_action(ctx->xout->odp_actions, &tnl_push_data);
2675 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
2678 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
2679 struct flow_wildcards *wc = &ctx->xout->wc;
2680 struct flow *flow = &ctx->xin->flow;
2681 struct flow_tnl flow_tnl;
2682 ovs_be16 flow_vlan_tci;
2683 uint32_t flow_pkt_mark;
2684 uint8_t flow_nw_tos;
2685 odp_port_t out_port, odp_port;
2686 bool tnl_push_pop_send = false;
2689 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2690 * before traversing a patch port. */
2691 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 31);
2692 memset(&flow_tnl, 0, sizeof flow_tnl);
2695 xlate_report(ctx, "Nonexistent output port");
2697 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
2698 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2700 } else if (check_stp) {
2701 if (is_stp(&ctx->base_flow)) {
2702 if (!xport_stp_should_forward_bpdu(xport) &&
2703 !xport_rstp_should_manage_bpdu(xport)) {
2704 if (ctx->xbridge->stp != NULL) {
2705 xlate_report(ctx, "STP not in listening state, "
2706 "skipping bpdu output");
2707 } else if (ctx->xbridge->rstp != NULL) {
2708 xlate_report(ctx, "RSTP not managing BPDU in this state, "
2709 "skipping bpdu output");
2713 } else if (!xport_stp_forward_state(xport) ||
2714 !xport_rstp_forward_state(xport)) {
2715 if (ctx->xbridge->stp != NULL) {
2716 xlate_report(ctx, "STP not in forwarding state, "
2718 } else if (ctx->xbridge->rstp != NULL) {
2719 xlate_report(ctx, "RSTP not in forwarding state, "
2726 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
2727 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
2732 const struct xport *peer = xport->peer;
2733 struct flow old_flow = ctx->xin->flow;
2734 enum slow_path_reason special;
2735 uint8_t table_id = rule_dpif_lookup_get_init_table_id(&ctx->xin->flow);
2736 struct ofpbuf old_stack = ctx->stack;
2737 union mf_subvalue new_stack[1024 / sizeof(union mf_subvalue)];
2738 struct ofpbuf old_action_set = ctx->action_set;
2739 uint64_t actset_stub[1024 / 8];
2741 ofpbuf_use_stub(&ctx->stack, new_stack, sizeof new_stack);
2742 ofpbuf_use_stub(&ctx->action_set, actset_stub, sizeof actset_stub);
2743 ctx->xbridge = peer->xbridge;
2744 flow->in_port.ofp_port = peer->ofp_port;
2745 flow->metadata = htonll(0);
2746 memset(&flow->tunnel, 0, sizeof flow->tunnel);
2747 memset(flow->regs, 0, sizeof flow->regs);
2748 flow->actset_output = OFPP_UNSET;
2750 special = process_special(ctx, &ctx->xin->flow, peer,
2753 ctx->xout->slow |= special;
2754 } else if (may_receive(peer, ctx)) {
2755 if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
2756 xlate_table_action(ctx, flow->in_port.ofp_port, table_id,
2758 if (ctx->action_set.size) {
2759 /* Translate action set only if not dropping the packet. */
2760 xlate_action_set(ctx);
2763 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
2764 * the learning action look at the packet, then drop it. */
2765 struct flow old_base_flow = ctx->base_flow;
2766 size_t old_size = ctx->xout->odp_actions->size;
2767 mirror_mask_t old_mirrors = ctx->xout->mirrors;
2769 xlate_table_action(ctx, flow->in_port.ofp_port, table_id,
2771 ctx->xout->mirrors = old_mirrors;
2772 ctx->base_flow = old_base_flow;
2773 ctx->xout->odp_actions->size = old_size;
2777 ctx->xin->flow = old_flow;
2778 ctx->xbridge = xport->xbridge;
2779 ofpbuf_uninit(&ctx->action_set);
2780 ctx->action_set = old_action_set;
2781 ofpbuf_uninit(&ctx->stack);
2782 ctx->stack = old_stack;
2784 /* The fact that the peer bridge exits (for any reason) does not mean
2785 * that the original bridge should exit. Specifically, if the peer
2786 * bridge recirculates (which typically modifies the packet), the
2787 * original bridge must continue processing with the original, not the
2788 * recirculated packet! */
2791 if (ctx->xin->resubmit_stats) {
2792 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2793 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
2795 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
2798 if (ctx->xin->xcache) {
2799 struct xc_entry *entry;
2801 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2802 entry->u.dev.tx = netdev_ref(xport->netdev);
2803 entry->u.dev.rx = netdev_ref(peer->netdev);
2804 entry->u.dev.bfd = bfd_ref(peer->bfd);
2809 flow_vlan_tci = flow->vlan_tci;
2810 flow_pkt_mark = flow->pkt_mark;
2811 flow_nw_tos = flow->nw_tos;
2813 if (count_skb_priorities(xport)) {
2814 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
2815 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
2816 wc->masks.nw_tos |= IP_DSCP_MASK;
2817 flow->nw_tos &= ~IP_DSCP_MASK;
2818 flow->nw_tos |= dscp;
2822 if (xport->is_tunnel) {
2823 /* Save tunnel metadata so that changes made due to
2824 * the Logical (tunnel) Port are not visible for any further
2825 * matches, while explicit set actions on tunnel metadata are.
2827 flow_tnl = flow->tunnel;
2828 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
2829 if (odp_port == ODPP_NONE) {
2830 xlate_report(ctx, "Tunneling decided against output");
2831 goto out; /* restore flow_nw_tos */
2833 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
2834 xlate_report(ctx, "Not tunneling to our own address");
2835 goto out; /* restore flow_nw_tos */
2837 if (ctx->xin->resubmit_stats) {
2838 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2840 if (ctx->xin->xcache) {
2841 struct xc_entry *entry;
2843 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2844 entry->u.dev.tx = netdev_ref(xport->netdev);
2846 out_port = odp_port;
2847 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
2848 tnl_push_pop_send = true;
2850 commit_odp_tunnel_action(flow, &ctx->base_flow,
2851 ctx->xout->odp_actions);
2852 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2855 odp_port = xport->odp_port;
2856 out_port = odp_port;
2857 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
2858 ofp_port_t vlandev_port;
2860 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2861 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
2862 ofp_port, flow->vlan_tci);
2863 if (vlandev_port != ofp_port) {
2864 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
2865 flow->vlan_tci = htons(0);
2870 if (out_port != ODPP_NONE) {
2871 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
2872 ctx->xout->odp_actions,
2874 ctx->xbridge->masked_set_action);
2876 if (ctx->use_recirc) {
2877 struct ovs_action_hash *act_hash;
2878 struct xlate_recirc *xr = &ctx->recirc;
2881 act_hash = nl_msg_put_unspec_uninit(ctx->xout->odp_actions,
2882 OVS_ACTION_ATTR_HASH,
2884 act_hash->hash_alg = xr->hash_alg;
2885 act_hash->hash_basis = xr->hash_basis;
2887 /* Recirc action. */
2888 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC,
2892 if (tnl_push_pop_send) {
2893 build_tunnel_send(ctx, xport, flow, odp_port);
2894 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2896 odp_port_t odp_tnl_port = ODPP_NONE;
2898 /* XXX: Write better Filter for tunnel port. We can use inport
2899 * int tunnel-port flow to avoid these checks completely. */
2900 if (ofp_port == OFPP_LOCAL &&
2901 ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
2903 odp_tnl_port = tnl_port_map_lookup(flow, wc);
2906 if (odp_tnl_port != ODPP_NONE) {
2907 nl_msg_put_odp_port(ctx->xout->odp_actions,
2908 OVS_ACTION_ATTR_TUNNEL_POP,
2911 /* Tunnel push-pop action is not compatible with
2913 add_ipfix_output_action(ctx, out_port);
2914 nl_msg_put_odp_port(ctx->xout->odp_actions,
2915 OVS_ACTION_ATTR_OUTPUT,
2921 ctx->sflow_odp_port = odp_port;
2922 ctx->sflow_n_outputs++;
2923 ctx->xout->nf_output_iface = ofp_port;
2928 flow->vlan_tci = flow_vlan_tci;
2929 flow->pkt_mark = flow_pkt_mark;
2930 flow->nw_tos = flow_nw_tos;
2934 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
2936 compose_output_action__(ctx, ofp_port, true);
2940 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
2942 struct rule_dpif *old_rule = ctx->rule;
2943 ovs_be64 old_cookie = ctx->rule_cookie;
2944 const struct rule_actions *actions;
2946 if (ctx->xin->resubmit_stats) {
2947 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
2953 ctx->rule_cookie = rule_dpif_get_flow_cookie(rule);
2954 actions = rule_dpif_get_actions(rule);
2955 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
2956 ctx->rule_cookie = old_cookie;
2957 ctx->rule = old_rule;
2962 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
2964 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2966 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
2967 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
2968 MAX_RESUBMIT_RECURSION);
2969 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
2970 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
2971 } else if (ctx->xout->odp_actions->size > UINT16_MAX) {
2972 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
2973 } else if (ctx->stack.size >= 65536) {
2974 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
2983 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
2984 bool may_packet_in, bool honor_table_miss)
2986 if (xlate_resubmit_resource_check(ctx)) {
2987 struct flow_wildcards *wc;
2988 uint8_t old_table_id = ctx->table_id;
2989 struct rule_dpif *rule;
2991 ctx->table_id = table_id;
2992 wc = (ctx->xin->skip_wildcards) ? NULL : &ctx->xout->wc;
2994 rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
2995 &ctx->xin->flow, wc,
2996 ctx->xin->xcache != NULL,
2997 ctx->xin->resubmit_stats,
2998 &ctx->table_id, in_port,
2999 may_packet_in, honor_table_miss);
3001 if (OVS_UNLIKELY(ctx->xin->resubmit_hook)) {
3002 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse + 1);
3006 /* Fill in the cache entry here instead of xlate_recursively
3007 * to make the reference counting more explicit. We take a
3008 * reference in the lookups above if we are going to cache the
3010 if (ctx->xin->xcache) {
3011 struct xc_entry *entry;
3013 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
3014 entry->u.rule = rule;
3016 xlate_recursively(ctx, rule);
3019 ctx->table_id = old_table_id;
3027 xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
3028 struct ofputil_bucket *bucket)
3030 if (ctx->xin->resubmit_stats) {
3031 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
3033 if (ctx->xin->xcache) {
3034 struct xc_entry *entry;
3036 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
3037 entry->u.group.group = group_dpif_ref(group);
3038 entry->u.group.bucket = bucket;
3043 xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
3045 uint64_t action_list_stub[1024 / 8];
3046 struct ofpbuf action_list, action_set;
3048 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
3049 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3051 ofpacts_execute_action_set(&action_list, &action_set);
3053 do_xlate_actions(action_list.data, action_list.size, ctx);
3056 ofpbuf_uninit(&action_set);
3057 ofpbuf_uninit(&action_list);
3061 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3063 struct ofputil_bucket *bucket;
3064 const struct ovs_list *buckets;
3065 struct flow old_flow = ctx->xin->flow;
3067 group_dpif_get_buckets(group, &buckets);
3069 LIST_FOR_EACH (bucket, list_node, buckets) {
3070 xlate_group_bucket(ctx, bucket);
3071 /* Roll back flow to previous state.
3072 * This is equivalent to cloning the packet for each bucket.
3074 * As a side effect any subsequently applied actions will
3075 * also effectively be applied to a clone of the packet taken
3076 * just before applying the all or indirect group. */
3077 ctx->xin->flow = old_flow;
3079 xlate_group_stats(ctx, group, NULL);
3083 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3085 struct ofputil_bucket *bucket;
3087 bucket = group_first_live_bucket(ctx, group, 0);
3089 xlate_group_bucket(ctx, bucket);
3090 xlate_group_stats(ctx, group, bucket);
3095 xlate_default_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3097 struct flow_wildcards *wc = &ctx->xout->wc;
3098 struct ofputil_bucket *bucket;
3101 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
3102 bucket = group_best_live_bucket(ctx, group, basis);
3104 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3105 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3106 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3107 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3108 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3109 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3110 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3111 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3112 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3114 xlate_group_bucket(ctx, bucket);
3115 xlate_group_stats(ctx, group, bucket);
3120 xlate_hash_fields_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3122 struct mf_bitmap hash_fields = MF_BITMAP_INITIALIZER;
3123 struct flow_wildcards *wc = &ctx->xout->wc;
3124 const struct field_array *fields;
3125 struct ofputil_bucket *bucket;
3129 fields = group_dpif_get_fields(group);
3130 basis = hash_uint64(group_dpif_get_selection_method_param(group));
3132 /* Determine which fields to hash */
3133 for (i = 0; i < MFF_N_IDS; i++) {
3134 if (bitmap_is_set(fields->used.bm, i)) {
3135 const struct mf_field *mf;
3137 /* If the field is already present in 'hash_fields' then
3138 * this loop has already checked that it and its pre-requisites
3139 * are present in the flow and its pre-requisites have
3140 * already been added to 'hash_fields'. There is nothing more
3141 * to do here and as an optimisation the loop can continue. */
3142 if (bitmap_is_set(hash_fields.bm, i)) {
3148 /* Only hash a field if it and its pre-requisites are present
3150 if (!mf_are_prereqs_ok(mf, &ctx->xin->flow)) {
3154 /* Hash both the field and its pre-requisites */
3155 mf_bitmap_set_field_and_prereqs(mf, &hash_fields);
3159 /* Hash the fields */
3160 for (i = 0; i < MFF_N_IDS; i++) {
3161 if (bitmap_is_set(hash_fields.bm, i)) {
3162 const struct mf_field *mf = mf_from_id(i);
3163 union mf_value value;
3166 mf_get_value(mf, &ctx->xin->flow, &value);
3167 /* This seems inefficient but so does apply_mask() */
3168 for (j = 0; j < mf->n_bytes; j++) {
3169 ((uint8_t *) &value)[j] &= ((uint8_t *) &fields->value[i])[j];
3171 basis = hash_bytes(&value, mf->n_bytes, basis);
3173 mf_mask_field(mf, &wc->masks);
3177 bucket = group_best_live_bucket(ctx, group, basis);
3179 xlate_group_bucket(ctx, bucket);
3180 xlate_group_stats(ctx, group, bucket);
3185 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3187 const char *selection_method = group_dpif_get_selection_method(group);
3189 if (selection_method[0] == '\0') {
3190 xlate_default_select_group(ctx, group);
3191 } else if (!strcasecmp("hash", selection_method)) {
3192 xlate_hash_fields_select_group(ctx, group);
3194 /* Parsing of groups should ensure this never happens */
3200 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3202 ctx->in_group = true;
3204 switch (group_dpif_get_type(group)) {
3206 case OFPGT11_INDIRECT:
3207 xlate_all_group(ctx, group);
3209 case OFPGT11_SELECT:
3210 xlate_select_group(ctx, group);
3213 xlate_ff_group(ctx, group);
3218 group_dpif_unref(group);
3220 ctx->in_group = false;
3224 xlate_group_resource_check(struct xlate_ctx *ctx)
3226 if (!xlate_resubmit_resource_check(ctx)) {
3228 } else if (ctx->in_group) {
3229 /* Prevent nested translation of OpenFlow groups.
3231 * OpenFlow allows this restriction. We enforce this restriction only
3232 * because, with the current architecture, we would otherwise have to
3233 * take a possibly recursive read lock on the ofgroup rwlock, which is
3234 * unsafe given that POSIX allows taking a read lock to block if there
3235 * is a thread blocked on taking the write lock. Other solutions
3236 * without this restriction are also possible, but seem unwarranted
3237 * given the current limited use of groups. */
3238 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3240 VLOG_ERR_RL(&rl, "cannot recursively translate OpenFlow group");
3248 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3250 if (xlate_group_resource_check(ctx)) {
3251 struct group_dpif *group;
3254 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
3256 xlate_group_action__(ctx, group);
3266 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
3267 const struct ofpact_resubmit *resubmit)
3271 bool may_packet_in = false;
3272 bool honor_table_miss = false;
3274 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
3275 /* Still allow missed packets to be sent to the controller
3276 * if resubmitting from an internal table. */
3277 may_packet_in = true;
3278 honor_table_miss = true;
3281 in_port = resubmit->in_port;
3282 if (in_port == OFPP_IN_PORT) {
3283 in_port = ctx->xin->flow.in_port.ofp_port;
3286 table_id = resubmit->table_id;
3287 if (table_id == 255) {
3288 table_id = ctx->table_id;
3291 xlate_table_action(ctx, in_port, table_id, may_packet_in,
3296 flood_packets(struct xlate_ctx *ctx, bool all)
3298 const struct xport *xport;
3300 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
3301 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
3306 compose_output_action__(ctx, xport->ofp_port, false);
3307 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
3308 compose_output_action(ctx, xport->ofp_port);
3312 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3316 execute_controller_action(struct xlate_ctx *ctx, int len,
3317 enum ofp_packet_in_reason reason,
3318 uint16_t controller_id)
3320 struct ofproto_packet_in *pin;
3321 struct dp_packet *packet;
3323 ctx->xout->slow |= SLOW_CONTROLLER;
3324 if (!ctx->xin->packet) {
3328 packet = dp_packet_clone(ctx->xin->packet);
3330 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3331 ctx->xout->odp_actions,
3333 ctx->xbridge->masked_set_action);
3335 odp_execute_actions(NULL, &packet, 1, false,
3336 ctx->xout->odp_actions->data,
3337 ctx->xout->odp_actions->size, NULL);
3339 pin = xmalloc(sizeof *pin);
3340 pin->up.packet_len = dp_packet_size(packet);
3341 pin->up.packet = dp_packet_steal_data(packet);
3342 pin->up.reason = reason;
3343 pin->up.table_id = ctx->table_id;
3344 pin->up.cookie = ctx->rule_cookie;
3346 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
3348 pin->controller_id = controller_id;
3349 pin->send_len = len;
3350 /* If a rule is a table-miss rule then this is
3351 * a table-miss handled by a table-miss rule.
3353 * Else, if rule is internal and has a controller action,
3354 * the later being implied by the rule being processed here,
3355 * then this is a table-miss handled without a table-miss rule.
3357 * Otherwise this is not a table-miss. */
3358 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
3360 if (rule_dpif_is_table_miss(ctx->rule)) {
3361 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
3362 } else if (rule_dpif_is_internal(ctx->rule)) {
3363 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
3366 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
3367 dp_packet_delete(packet);
3371 compose_recirculate_action(struct xlate_ctx *ctx,
3372 const struct ofpact *ofpacts_base,
3373 const struct ofpact *ofpact_current,
3374 size_t ofpacts_base_len)
3378 unsigned ofpacts_len;
3381 struct ofpbuf ofpacts;
3385 ofpacts_len = ofpacts_base_len -
3386 ((uint8_t *)ofpact_current - (uint8_t *)ofpacts_base);
3389 id = rule_dpif_get_recirc_id(ctx->rule);
3391 /* In the case where ctx has no rule then allocate a recirc id.
3392 * The life-cycle of this recirc id is managed by associating it
3393 * with the internal rule that is created to to handle
3394 * recirculation below.
3396 * The known use-case of this is packet_out which
3397 * translates actions without a rule */
3398 id = ofproto_dpif_alloc_recirc_id(ctx->xbridge->ofproto);
3401 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3402 VLOG_ERR_RL(&rl, "Failed to allocate recirculation id");
3406 match_init_catchall(&match);
3407 match_set_recirc_id(&match, id);
3408 ofpbuf_use_const(&ofpacts, ofpact_current, ofpacts_len);
3409 error = ofproto_dpif_add_internal_flow(ctx->xbridge->ofproto, &match,
3410 RECIRC_RULE_PRIORITY,
3411 RECIRC_TIMEOUT, &ofpacts, &rule);
3413 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3414 VLOG_ERR_RL(&rl, "Failed to add post recirculation flow %s",
3415 match_to_string(&match, 0));
3417 ofproto_dpif_free_recirc_id(ctx->xbridge->ofproto, id);
3421 /* If ctx has no rule then associate the recirc id, which
3422 * was allocated above, with the internal rule. This allows
3423 * the recirc id to be released when the internal rule times out. */
3425 rule_set_recirc_id(rule, id);
3428 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3429 ctx->xout->odp_actions,
3431 ctx->xbridge->masked_set_action);
3432 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
3436 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
3438 struct flow_wildcards *wc = &ctx->xout->wc;
3439 struct flow *flow = &ctx->xin->flow;
3442 ovs_assert(eth_type_mpls(mpls->ethertype));
3444 n = flow_count_mpls_labels(flow, wc);
3446 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
3447 ctx->xout->odp_actions,
3449 ctx->xbridge->masked_set_action);
3450 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3451 if (ctx->xin->packet != NULL) {
3452 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3453 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3454 "MPLS push action can't be performed as it would "
3455 "have more MPLS LSEs than the %d supported.",
3456 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3462 flow_push_mpls(flow, n, mpls->ethertype, wc);
3466 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
3468 struct flow_wildcards *wc = &ctx->xout->wc;
3469 struct flow *flow = &ctx->xin->flow;
3470 int n = flow_count_mpls_labels(flow, wc);
3472 if (flow_pop_mpls(flow, n, eth_type, wc)) {
3473 if (ctx->xbridge->enable_recirc) {
3474 ctx->was_mpls = true;
3476 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3477 if (ctx->xin->packet != NULL) {
3478 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3479 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3480 "MPLS pop action can't be performed as it has "
3481 "more MPLS LSEs than the %d supported.",
3482 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3485 ofpbuf_clear(ctx->xout->odp_actions);
3490 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3492 struct flow *flow = &ctx->xin->flow;
3494 if (!is_ip_any(flow)) {
3498 ctx->xout->wc.masks.nw_ttl = 0xff;
3499 if (flow->nw_ttl > 1) {
3505 for (i = 0; i < ids->n_controllers; i++) {
3506 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3510 /* Stop processing for current table. */
3516 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3518 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3519 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
3520 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
3525 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3527 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3528 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
3529 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
3534 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
3536 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3537 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3538 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
3543 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
3545 struct flow *flow = &ctx->xin->flow;
3546 struct flow_wildcards *wc = &ctx->xout->wc;
3548 if (eth_type_mpls(flow->dl_type)) {
3549 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
3551 wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3554 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3557 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
3561 /* Stop processing for current table. */
3566 xlate_output_action(struct xlate_ctx *ctx,
3567 ofp_port_t port, uint16_t max_len, bool may_packet_in)
3569 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
3571 ctx->xout->nf_output_iface = NF_OUT_DROP;
3575 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
3578 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3579 0, may_packet_in, true);
3585 flood_packets(ctx, false);
3588 flood_packets(ctx, true);
3590 case OFPP_CONTROLLER:
3591 execute_controller_action(ctx, max_len,
3592 (ctx->in_group ? OFPR_GROUP
3593 : ctx->in_action_set ? OFPR_ACTION_SET
3601 if (port != ctx->xin->flow.in_port.ofp_port) {
3602 compose_output_action(ctx, port);
3604 xlate_report(ctx, "skipping output to input port");
3609 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3610 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3611 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3612 ctx->xout->nf_output_iface = prev_nf_output_iface;
3613 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3614 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3615 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3620 xlate_output_reg_action(struct xlate_ctx *ctx,
3621 const struct ofpact_output_reg *or)
3623 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3624 if (port <= UINT16_MAX) {
3625 union mf_subvalue value;
3627 memset(&value, 0xff, sizeof value);
3628 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
3629 xlate_output_action(ctx, u16_to_ofp(port),
3630 or->max_len, false);
3635 xlate_enqueue_action(struct xlate_ctx *ctx,
3636 const struct ofpact_enqueue *enqueue)
3638 ofp_port_t ofp_port = enqueue->port;
3639 uint32_t queue_id = enqueue->queue;
3640 uint32_t flow_priority, priority;
3643 /* Translate queue to priority. */
3644 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
3646 /* Fall back to ordinary output action. */
3647 xlate_output_action(ctx, enqueue->port, 0, false);
3651 /* Check output port. */
3652 if (ofp_port == OFPP_IN_PORT) {
3653 ofp_port = ctx->xin->flow.in_port.ofp_port;
3654 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
3658 /* Add datapath actions. */
3659 flow_priority = ctx->xin->flow.skb_priority;
3660 ctx->xin->flow.skb_priority = priority;
3661 compose_output_action(ctx, ofp_port);
3662 ctx->xin->flow.skb_priority = flow_priority;
3664 /* Update NetFlow output port. */
3665 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3666 ctx->xout->nf_output_iface = ofp_port;
3667 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3668 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3673 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3675 uint32_t skb_priority;
3677 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
3678 ctx->xin->flow.skb_priority = skb_priority;
3680 /* Couldn't translate queue to a priority. Nothing to do. A warning
3681 * has already been logged. */
3686 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
3688 const struct xbridge *xbridge = xbridge_;
3699 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3702 port = get_ofp_port(xbridge, ofp_port);
3703 return port ? port->may_enable : false;
3708 xlate_bundle_action(struct xlate_ctx *ctx,
3709 const struct ofpact_bundle *bundle)
3713 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
3715 CONST_CAST(struct xbridge *, ctx->xbridge));
3716 if (bundle->dst.field) {
3717 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
3720 xlate_output_action(ctx, port, 0, false);
3725 xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
3726 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
3728 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
3729 if (ctx->xin->may_learn) {
3730 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
3735 xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
3737 ctx->xout->has_learn = true;
3738 learn_mask(learn, &ctx->xout->wc);
3740 if (ctx->xin->xcache) {
3741 struct xc_entry *entry;
3743 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
3744 entry->u.learn.ofproto = ctx->xbridge->ofproto;
3745 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
3746 entry->u.learn.ofpacts = ofpbuf_new(64);
3747 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
3748 entry->u.learn.ofpacts);
3749 } else if (ctx->xin->may_learn) {
3750 uint64_t ofpacts_stub[1024 / 8];
3751 struct ofputil_flow_mod fm;
3752 struct ofpbuf ofpacts;
3754 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3755 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
3756 ofpbuf_uninit(&ofpacts);
3761 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
3762 uint16_t idle_timeout, uint16_t hard_timeout)
3764 if (tcp_flags & (TCP_FIN | TCP_RST)) {
3765 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
3770 xlate_fin_timeout(struct xlate_ctx *ctx,
3771 const struct ofpact_fin_timeout *oft)
3774 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
3775 oft->fin_idle_timeout, oft->fin_hard_timeout);
3776 if (ctx->xin->xcache) {
3777 struct xc_entry *entry;
3779 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
3780 /* XC_RULE already holds a reference on the rule, none is taken
3782 entry->u.fin.rule = ctx->rule;
3783 entry->u.fin.idle = oft->fin_idle_timeout;
3784 entry->u.fin.hard = oft->fin_hard_timeout;
3790 xlate_sample_action(struct xlate_ctx *ctx,
3791 const struct ofpact_sample *os)
3793 union user_action_cookie cookie;
3794 /* Scale the probability from 16-bit to 32-bit while representing
3795 * the same percentage. */
3796 uint32_t probability = (os->probability << 16) | os->probability;
3798 if (!ctx->xbridge->variable_length_userdata) {
3799 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3801 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
3802 "lacks support (needs Linux 3.10+ or kernel module from "
3807 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3808 ctx->xout->odp_actions,
3810 ctx->xbridge->masked_set_action);
3812 compose_flow_sample_cookie(os->probability, os->collector_set_id,
3813 os->obs_domain_id, os->obs_point_id, &cookie);
3814 compose_sample_action(ctx->xbridge, ctx->xout->odp_actions,
3815 &ctx->xin->flow, probability, &cookie,
3816 sizeof cookie.flow_sample, ODPP_NONE);
3820 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
3822 if (xport->config & (is_stp(&ctx->xin->flow)
3823 ? OFPUTIL_PC_NO_RECV_STP
3824 : OFPUTIL_PC_NO_RECV)) {
3828 /* Only drop packets here if both forwarding and learning are
3829 * disabled. If just learning is enabled, we need to have
3830 * OFPP_NORMAL and the learning action have a look at the packet
3831 * before we can drop it. */
3832 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
3833 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
3841 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
3843 const struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3844 size_t on_len = ofpact_nest_get_action_len(on);
3845 const struct ofpact *inner;
3847 /* Maintain actset_output depending on the contents of the action set:
3849 * - OFPP_UNSET, if there is no "output" action.
3851 * - The output port, if there is an "output" action and no "group"
3854 * - OFPP_UNSET, if there is a "group" action.
3856 if (!ctx->action_set_has_group) {
3857 OFPACT_FOR_EACH (inner, on->actions, on_len) {
3858 if (inner->type == OFPACT_OUTPUT) {
3859 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(inner)->port;
3860 } else if (inner->type == OFPACT_GROUP) {
3861 ctx->xin->flow.actset_output = OFPP_UNSET;
3862 ctx->action_set_has_group = true;
3867 ofpbuf_put(&ctx->action_set, on->actions, on_len);
3868 ofpact_pad(&ctx->action_set);
3872 xlate_action_set(struct xlate_ctx *ctx)
3874 uint64_t action_list_stub[1024 / 64];
3875 struct ofpbuf action_list;
3877 ctx->in_action_set = true;
3878 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3879 ofpacts_execute_action_set(&action_list, &ctx->action_set);
3880 /* Clear the action set, as it is not needed any more. */
3881 ofpbuf_clear(&ctx->action_set);
3882 do_xlate_actions(action_list.data, action_list.size, ctx);
3883 ctx->in_action_set = false;
3884 ofpbuf_uninit(&action_list);
3888 ofpact_needs_recirculation_after_mpls(const struct ofpact *a, struct xlate_ctx *ctx)
3890 struct flow_wildcards *wc = &ctx->xout->wc;
3891 struct flow *flow = &ctx->xin->flow;
3893 if (!ctx->was_mpls) {
3900 case OFPACT_CONTROLLER:
3901 case OFPACT_STRIP_VLAN:
3902 case OFPACT_SET_VLAN_PCP:
3903 case OFPACT_SET_VLAN_VID:
3904 case OFPACT_ENQUEUE:
3905 case OFPACT_PUSH_VLAN:
3906 case OFPACT_SET_ETH_SRC:
3907 case OFPACT_SET_ETH_DST:
3908 case OFPACT_SET_TUNNEL:
3909 case OFPACT_SET_QUEUE:
3910 case OFPACT_POP_QUEUE:
3911 case OFPACT_CONJUNCTION:
3913 case OFPACT_OUTPUT_REG:
3916 case OFPACT_WRITE_METADATA:
3917 case OFPACT_WRITE_ACTIONS:
3918 case OFPACT_CLEAR_ACTIONS:
3922 case OFPACT_POP_MPLS:
3923 case OFPACT_DEC_MPLS_TTL:
3924 case OFPACT_SET_MPLS_TTL:
3925 case OFPACT_SET_MPLS_TC:
3926 case OFPACT_SET_MPLS_LABEL:
3927 case OFPACT_SET_IPV4_SRC:
3928 case OFPACT_SET_IPV4_DST:
3929 case OFPACT_SET_IP_DSCP:
3930 case OFPACT_SET_IP_ECN:
3931 case OFPACT_SET_IP_TTL:
3932 case OFPACT_SET_L4_SRC_PORT:
3933 case OFPACT_SET_L4_DST_PORT:
3934 case OFPACT_RESUBMIT:
3935 case OFPACT_STACK_PUSH:
3936 case OFPACT_STACK_POP:
3937 case OFPACT_DEC_TTL:
3938 case OFPACT_MULTIPATH:
3941 case OFPACT_FIN_TIMEOUT:
3942 case OFPACT_GOTO_TABLE:
3945 case OFPACT_REG_MOVE:
3946 return (mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
3947 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
3949 case OFPACT_SET_FIELD:
3950 return mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field);
3952 case OFPACT_PUSH_MPLS:
3953 /* Recirculate if it is an IP packet with a zero ttl. This may
3954 * indicate that the packet was previously MPLS and an MPLS pop action
3955 * converted it to IP. In this case recirculating should reveal the IP
3956 * TTL which is used as the basis for a new MPLS LSE. */
3957 return (!flow_count_mpls_labels(flow, wc)
3958 && flow->nw_ttl == 0
3959 && is_ip_any(flow));
3966 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
3967 struct xlate_ctx *ctx)
3969 struct flow_wildcards *wc = &ctx->xout->wc;
3970 struct flow *flow = &ctx->xin->flow;
3971 const struct ofpact *a;
3973 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3974 tnl_arp_snoop(flow, wc, ctx->xbridge->name);
3976 /* dl_type already in the mask, not set below. */
3978 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3979 struct ofpact_controller *controller;
3980 const struct ofpact_metadata *metadata;
3981 const struct ofpact_set_field *set_field;
3982 const struct mf_field *mf;
3988 if (ofpact_needs_recirculation_after_mpls(a, ctx)) {
3989 compose_recirculate_action(ctx, ofpacts, a, ofpacts_len);
3995 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
3996 ofpact_get_OUTPUT(a)->max_len, true);
4000 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
4005 case OFPACT_CONTROLLER:
4006 controller = ofpact_get_CONTROLLER(a);
4007 execute_controller_action(ctx, controller->max_len,
4009 controller->controller_id);
4012 case OFPACT_ENQUEUE:
4013 memset(&wc->masks.skb_priority, 0xff,
4014 sizeof wc->masks.skb_priority);
4015 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
4018 case OFPACT_SET_VLAN_VID:
4019 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
4020 if (flow->vlan_tci & htons(VLAN_CFI) ||
4021 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
4022 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
4023 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
4028 case OFPACT_SET_VLAN_PCP:
4029 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
4030 if (flow->vlan_tci & htons(VLAN_CFI) ||
4031 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
4032 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
4033 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
4034 << VLAN_PCP_SHIFT) | VLAN_CFI);
4038 case OFPACT_STRIP_VLAN:
4039 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4040 flow->vlan_tci = htons(0);
4043 case OFPACT_PUSH_VLAN:
4044 /* XXX 802.1AD(QinQ) */
4045 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4046 flow->vlan_tci = htons(VLAN_CFI);
4049 case OFPACT_SET_ETH_SRC:
4050 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
4051 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
4054 case OFPACT_SET_ETH_DST:
4055 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
4056 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
4059 case OFPACT_SET_IPV4_SRC:
4060 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4061 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
4062 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
4066 case OFPACT_SET_IPV4_DST:
4067 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4068 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
4069 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
4073 case OFPACT_SET_IP_DSCP:
4074 if (is_ip_any(flow)) {
4075 wc->masks.nw_tos |= IP_DSCP_MASK;
4076 flow->nw_tos &= ~IP_DSCP_MASK;
4077 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
4081 case OFPACT_SET_IP_ECN:
4082 if (is_ip_any(flow)) {
4083 wc->masks.nw_tos |= IP_ECN_MASK;
4084 flow->nw_tos &= ~IP_ECN_MASK;
4085 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
4089 case OFPACT_SET_IP_TTL:
4090 if (is_ip_any(flow)) {
4091 wc->masks.nw_ttl = 0xff;
4092 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
4096 case OFPACT_SET_L4_SRC_PORT:
4097 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4098 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4099 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
4100 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
4104 case OFPACT_SET_L4_DST_PORT:
4105 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4106 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4107 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
4108 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
4112 case OFPACT_RESUBMIT:
4113 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
4116 case OFPACT_SET_TUNNEL:
4117 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
4120 case OFPACT_SET_QUEUE:
4121 memset(&wc->masks.skb_priority, 0xff,
4122 sizeof wc->masks.skb_priority);
4123 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
4126 case OFPACT_POP_QUEUE:
4127 memset(&wc->masks.skb_priority, 0xff,
4128 sizeof wc->masks.skb_priority);
4129 flow->skb_priority = ctx->orig_skb_priority;
4132 case OFPACT_REG_MOVE:
4133 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
4136 case OFPACT_SET_FIELD:
4137 set_field = ofpact_get_SET_FIELD(a);
4138 mf = set_field->field;
4140 /* Set field action only ever overwrites packet's outermost
4141 * applicable header fields. Do nothing if no header exists. */
4142 if (mf->id == MFF_VLAN_VID) {
4143 wc->masks.vlan_tci |= htons(VLAN_CFI);
4144 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4147 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
4148 /* 'dl_type' is already unwildcarded. */
4149 && !eth_type_mpls(flow->dl_type)) {
4152 /* A flow may wildcard nw_frag. Do nothing if setting a trasport
4153 * header field on a packet that does not have them. */
4154 mf_mask_field_and_prereqs(mf, &wc->masks);
4155 if (mf_are_prereqs_ok(mf, flow)) {
4156 mf_set_flow_value_masked(mf, &set_field->value,
4157 &set_field->mask, flow);
4161 case OFPACT_STACK_PUSH:
4162 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
4166 case OFPACT_STACK_POP:
4167 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
4171 case OFPACT_PUSH_MPLS:
4172 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
4175 case OFPACT_POP_MPLS:
4176 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
4179 case OFPACT_SET_MPLS_LABEL:
4180 compose_set_mpls_label_action(
4181 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
4184 case OFPACT_SET_MPLS_TC:
4185 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
4188 case OFPACT_SET_MPLS_TTL:
4189 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
4192 case OFPACT_DEC_MPLS_TTL:
4193 if (compose_dec_mpls_ttl_action(ctx)) {
4198 case OFPACT_DEC_TTL:
4199 wc->masks.nw_ttl = 0xff;
4200 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
4206 /* Nothing to do. */
4209 case OFPACT_MULTIPATH:
4210 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
4214 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
4217 case OFPACT_OUTPUT_REG:
4218 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
4222 xlate_learn_action(ctx, ofpact_get_LEARN(a));
4225 case OFPACT_CONJUNCTION: {
4226 /* A flow with a "conjunction" action represents part of a special
4227 * kind of "set membership match". Such a flow should not actually
4228 * get executed, but it could via, say, a "packet-out", even though
4229 * that wouldn't be useful. Log it to help debugging. */
4230 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4231 VLOG_INFO_RL(&rl, "executing no-op conjunction action");
4239 case OFPACT_FIN_TIMEOUT:
4240 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4241 ctx->xout->has_fin_timeout = true;
4242 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
4245 case OFPACT_CLEAR_ACTIONS:
4246 ofpbuf_clear(&ctx->action_set);
4247 ctx->xin->flow.actset_output = OFPP_UNSET;
4248 ctx->action_set_has_group = false;
4251 case OFPACT_WRITE_ACTIONS:
4252 xlate_write_actions(ctx, a);
4255 case OFPACT_WRITE_METADATA:
4256 metadata = ofpact_get_WRITE_METADATA(a);
4257 flow->metadata &= ~metadata->mask;
4258 flow->metadata |= metadata->metadata & metadata->mask;
4262 /* Not implemented yet. */
4265 case OFPACT_GOTO_TABLE: {
4266 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
4268 /* Allow ctx->table_id == TBL_INTERNAL, which will be greater
4269 * than ogt->table_id. This is to allow goto_table actions that
4270 * triggered recirculation: ctx->table_id will be TBL_INTERNAL
4271 * after recirculation. */
4272 ovs_assert(ctx->table_id == TBL_INTERNAL
4273 || ctx->table_id < ogt->table_id);
4274 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
4275 ogt->table_id, true, true);
4280 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
4287 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
4288 const struct flow *flow, ofp_port_t in_port,
4289 struct rule_dpif *rule, uint16_t tcp_flags,
4290 const struct dp_packet *packet)
4292 xin->ofproto = ofproto;
4294 xin->flow.in_port.ofp_port = in_port;
4295 xin->flow.actset_output = OFPP_UNSET;
4296 xin->packet = packet;
4297 xin->may_learn = packet != NULL;
4300 xin->ofpacts = NULL;
4301 xin->ofpacts_len = 0;
4302 xin->tcp_flags = tcp_flags;
4303 xin->resubmit_hook = NULL;
4304 xin->report_hook = NULL;
4305 xin->resubmit_stats = NULL;
4306 xin->skip_wildcards = false;
4307 xin->odp_actions = NULL;
4311 xlate_out_uninit(struct xlate_out *xout)
4313 if (xout && xout->odp_actions == &xout->odp_actions_buf) {
4314 ofpbuf_uninit(xout->odp_actions);
4318 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
4319 * into datapath actions, using 'ctx', and discards the datapath actions. */
4321 xlate_actions_for_side_effects(struct xlate_in *xin)
4323 struct xlate_out xout;
4325 xlate_actions(xin, &xout);
4326 xlate_out_uninit(&xout);
4330 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
4333 dst->slow = src->slow;
4334 dst->has_learn = src->has_learn;
4335 dst->has_normal = src->has_normal;
4336 dst->has_fin_timeout = src->has_fin_timeout;
4337 dst->nf_output_iface = src->nf_output_iface;
4338 dst->mirrors = src->mirrors;
4340 dst->odp_actions = &dst->odp_actions_buf;
4341 ofpbuf_use_stub(dst->odp_actions, dst->odp_actions_stub,
4342 sizeof dst->odp_actions_stub);
4343 ofpbuf_put(dst->odp_actions, src->odp_actions->data, src->odp_actions->size);
4346 static struct skb_priority_to_dscp *
4347 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
4349 struct skb_priority_to_dscp *pdscp;
4352 hash = hash_int(skb_priority, 0);
4353 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
4354 if (pdscp->skb_priority == skb_priority) {
4362 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
4365 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
4366 *dscp = pdscp ? pdscp->dscp : 0;
4367 return pdscp != NULL;
4371 count_skb_priorities(const struct xport *xport)
4373 return hmap_count(&xport->skb_priorities);
4377 clear_skb_priorities(struct xport *xport)
4379 struct skb_priority_to_dscp *pdscp, *next;
4381 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
4382 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
4388 actions_output_to_local_port(const struct xlate_ctx *ctx)
4390 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
4391 const struct nlattr *a;
4394 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions->data,
4395 ctx->xout->odp_actions->size) {
4396 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
4397 && nl_attr_get_odp_port(a) == local_odp_port) {
4404 #if defined(__linux__)
4405 /* Returns the maximum number of packets that the Linux kernel is willing to
4406 * queue up internally to certain kinds of software-implemented ports, or the
4407 * default (and rarely modified) value if it cannot be determined. */
4409 netdev_max_backlog(void)
4411 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
4412 static int max_backlog = 1000; /* The normal default value. */
4414 if (ovsthread_once_start(&once)) {
4415 static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
4419 stream = fopen(filename, "r");
4421 VLOG_WARN("%s: open failed (%s)", filename, ovs_strerror(errno));
4423 if (fscanf(stream, "%d", &n) != 1) {
4424 VLOG_WARN("%s: read error", filename);
4425 } else if (n <= 100) {
4426 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
4432 ovsthread_once_done(&once);
4434 VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
4440 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
4443 count_output_actions(const struct ofpbuf *odp_actions)
4445 const struct nlattr *a;
4449 NL_ATTR_FOR_EACH_UNSAFE (a, left, odp_actions->data, odp_actions->size) {
4450 if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
4456 #endif /* defined(__linux__) */
4458 /* Returns true if 'odp_actions' contains more output actions than the datapath
4459 * can reliably handle in one go. On Linux, this is the value of the
4460 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
4461 * packets that the kernel is willing to queue up for processing while the
4462 * datapath is processing a set of actions. */
4464 too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
4467 return (odp_actions->size / NL_A_U32_SIZE > netdev_max_backlog()
4468 && count_output_actions(odp_actions) > netdev_max_backlog());
4470 /* OSes other than Linux might have similar limits, but we don't know how
4471 * to determine them.*/
4476 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
4477 * into datapath actions in 'odp_actions', using 'ctx'.
4479 * The caller must take responsibility for eventually freeing 'xout', with
4480 * xlate_out_uninit(). */
4482 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
4484 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4485 struct flow_wildcards *wc = NULL;
4486 struct flow *flow = &xin->flow;
4487 struct rule_dpif *rule = NULL;
4489 enum slow_path_reason special;
4490 const struct ofpact *ofpacts;
4491 struct xport *in_port;
4492 struct flow orig_flow;
4493 struct xlate_ctx ctx;
4498 COVERAGE_INC(xlate_actions);
4500 /* Flow initialization rules:
4501 * - 'base_flow' must match the kernel's view of the packet at the
4502 * time that action processing starts. 'flow' represents any
4503 * transformations we wish to make through actions.
4504 * - By default 'base_flow' and 'flow' are the same since the input
4505 * packet matches the output before any actions are applied.
4506 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
4507 * of the received packet as seen by the kernel. If we later output
4508 * to another device without any modifications this will cause us to
4509 * insert a new tag since the original one was stripped off by the
4511 * - Tunnel metadata as received is retained in 'flow'. This allows
4512 * tunnel metadata matching also in later tables.
4513 * Since a kernel action for setting the tunnel metadata will only be
4514 * generated with actual tunnel output, changing the tunnel metadata
4515 * values in 'flow' (such as tun_id) will only have effect with a later
4516 * tunnel output action.
4517 * - Tunnel 'base_flow' is completely cleared since that is what the
4518 * kernel does. If we wish to maintain the original values an action
4519 * needs to be generated. */
4524 ctx.xout->has_learn = false;
4525 ctx.xout->has_normal = false;
4526 ctx.xout->has_fin_timeout = false;
4527 ctx.xout->nf_output_iface = NF_OUT_DROP;
4528 ctx.xout->mirrors = 0;
4530 xout->odp_actions = xin->odp_actions;
4531 if (!xout->odp_actions) {
4532 xout->odp_actions = &xout->odp_actions_buf;
4533 ofpbuf_use_stub(xout->odp_actions, xout->odp_actions_stub,
4534 sizeof xout->odp_actions_stub);
4536 ofpbuf_reserve(xout->odp_actions, NL_A_U32_SIZE);
4538 ctx.xbridge = xbridge_lookup(xcfg, xin->ofproto);
4542 ctx.rule = xin->rule;
4544 ctx.base_flow = *flow;
4545 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
4546 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
4548 if (!xin->skip_wildcards) {
4550 flow_wildcards_init_catchall(wc);
4551 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
4552 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
4553 if (is_ip_any(flow)) {
4554 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
4556 if (ctx.xbridge->enable_recirc) {
4557 /* Always exactly match recirc_id when datapath supports
4559 wc->masks.recirc_id = UINT32_MAX;
4561 if (ctx.xbridge->netflow) {
4562 netflow_mask_wc(flow, wc);
4565 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
4567 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
4571 ctx.in_group = false;
4572 ctx.in_action_set = false;
4573 ctx.orig_skb_priority = flow->skb_priority;
4575 ctx.rule_cookie = OVS_BE64_MAX;
4577 ctx.use_recirc = false;
4578 ctx.was_mpls = false;
4580 if (!xin->ofpacts && !ctx.rule) {
4581 rule = rule_dpif_lookup(ctx.xbridge->ofproto, flow, wc,
4582 ctx.xin->xcache != NULL,
4583 ctx.xin->resubmit_stats, &ctx.table_id);
4584 if (ctx.xin->resubmit_stats) {
4585 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
4587 if (ctx.xin->xcache) {
4588 struct xc_entry *entry;
4590 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
4591 entry->u.rule = rule;
4595 if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
4596 ctx.xin->resubmit_hook(ctx.xin, rule, 0);
4599 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
4602 ofpacts = xin->ofpacts;
4603 ofpacts_len = xin->ofpacts_len;
4604 } else if (ctx.rule) {
4605 const struct rule_actions *actions = rule_dpif_get_actions(ctx.rule);
4607 ofpacts = actions->ofpacts;
4608 ofpacts_len = actions->ofpacts_len;
4610 ctx.rule_cookie = rule_dpif_get_flow_cookie(ctx.rule);
4615 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
4617 ctx.action_set_has_group = false;
4618 ofpbuf_use_stub(&ctx.action_set,
4619 ctx.action_set_stub, sizeof ctx.action_set_stub);
4621 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
4622 /* Do this conditionally because the copy is expensive enough that it
4623 * shows up in profiles. */
4627 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
4628 if (in_port && in_port->is_tunnel) {
4629 if (ctx.xin->resubmit_stats) {
4630 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
4632 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
4635 if (ctx.xin->xcache) {
4636 struct xc_entry *entry;
4638 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
4639 entry->u.dev.rx = netdev_ref(in_port->netdev);
4640 entry->u.dev.bfd = bfd_ref(in_port->bfd);
4644 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
4646 ctx.xout->slow |= special;
4648 size_t sample_actions_len;
4650 if (flow->in_port.ofp_port
4651 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
4652 flow->in_port.ofp_port,
4654 ctx.base_flow.vlan_tci = 0;
4657 add_sflow_action(&ctx);
4658 add_ipfix_action(&ctx);
4659 sample_actions_len = ctx.xout->odp_actions->size;
4661 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
4662 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
4664 /* We've let OFPP_NORMAL and the learning action look at the
4665 * packet, so drop it now if forwarding is disabled. */
4666 if (in_port && (!xport_stp_forward_state(in_port) ||
4667 !xport_rstp_forward_state(in_port))) {
4668 /* Drop all actions added by do_xlate_actions() above. */
4669 ctx.xout->odp_actions->size = sample_actions_len;
4670 } else if (ctx.action_set.size) {
4671 /* Translate action set only if not dropping the packet. */
4672 xlate_action_set(&ctx);
4676 if (ctx.xbridge->has_in_band
4677 && in_band_must_output_to_local_port(flow)
4678 && !actions_output_to_local_port(&ctx)) {
4679 compose_output_action(&ctx, OFPP_LOCAL);
4682 fix_sflow_action(&ctx);
4684 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
4685 add_mirror_actions(&ctx, &orig_flow);
4689 if (nl_attr_oversized(ctx.xout->odp_actions->size)) {
4690 /* These datapath actions are too big for a Netlink attribute, so we
4691 * can't hand them to the kernel directly. dpif_execute() can execute
4692 * them one by one with help, so just mark the result as SLOW_ACTION to
4693 * prevent the flow from being installed. */
4694 COVERAGE_INC(xlate_actions_oversize);
4695 ctx.xout->slow |= SLOW_ACTION;
4696 } else if (too_many_output_actions(ctx.xout->odp_actions)) {
4697 COVERAGE_INC(xlate_actions_too_many_output);
4698 ctx.xout->slow |= SLOW_ACTION;
4701 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
4702 if (ctx.xin->resubmit_stats) {
4703 mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
4704 ctx.xin->resubmit_stats->n_packets,
4705 ctx.xin->resubmit_stats->n_bytes);
4707 if (ctx.xin->xcache) {
4708 struct xc_entry *entry;
4710 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_MIRROR);
4711 entry->u.mirror.mbridge = mbridge_ref(ctx.xbridge->mbridge);
4712 entry->u.mirror.mirrors = xout->mirrors;
4716 if (ctx.xbridge->netflow) {
4717 /* Only update netflow if we don't have controller flow. We don't
4718 * report NetFlow expiration messages for such facets because they
4719 * are just part of the control logic for the network, not real
4721 if (ofpacts_len == 0
4722 || ofpacts->type != OFPACT_CONTROLLER
4723 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
4724 if (ctx.xin->resubmit_stats) {
4725 netflow_flow_update(ctx.xbridge->netflow, flow,
4726 xout->nf_output_iface,
4727 ctx.xin->resubmit_stats);
4729 if (ctx.xin->xcache) {
4730 struct xc_entry *entry;
4732 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
4733 entry->u.nf.netflow = netflow_ref(ctx.xbridge->netflow);
4734 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
4735 entry->u.nf.iface = xout->nf_output_iface;
4740 ofpbuf_uninit(&ctx.stack);
4741 ofpbuf_uninit(&ctx.action_set);
4744 /* Clear the metadata and register wildcard masks, because we won't
4745 * use non-header fields as part of the cache. */
4746 flow_wildcards_clear_non_packet_fields(wc);
4748 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
4749 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
4750 * represent these fields. The datapath interface, on the other hand,
4751 * represents them with just 8 bits each. This means that if the high
4752 * 8 bits of the masks for these fields somehow become set, then they
4753 * will get chopped off by a round trip through the datapath, and
4754 * revalidation will spot that as an inconsistency and delete the flow.
4755 * Avoid the problem here by making sure that only the low 8 bits of
4756 * either field can be unwildcarded for ICMP.
4759 wc->masks.tp_src &= htons(UINT8_MAX);
4760 wc->masks.tp_dst &= htons(UINT8_MAX);
4765 /* Sends 'packet' out 'ofport'.
4766 * May modify 'packet'.
4767 * Returns 0 if successful, otherwise a positive errno value. */
4769 xlate_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
4771 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4772 struct xport *xport;
4773 struct ofpact_output output;
4776 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
4777 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
4778 flow_extract(packet, &flow);
4779 flow.in_port.ofp_port = OFPP_NONE;
4781 xport = xport_lookup(xcfg, ofport);
4785 output.port = xport->ofp_port;
4788 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
4789 &output.ofpact, sizeof output,
4793 struct xlate_cache *
4794 xlate_cache_new(void)
4796 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
4798 ofpbuf_init(&xcache->entries, 512);
4802 static struct xc_entry *
4803 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
4805 struct xc_entry *entry;
4807 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
4814 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
4816 if (entry->u.dev.tx) {
4817 netdev_vport_inc_tx(entry->u.dev.tx, stats);
4819 if (entry->u.dev.rx) {
4820 netdev_vport_inc_rx(entry->u.dev.rx, stats);
4822 if (entry->u.dev.bfd) {
4823 bfd_account_rx(entry->u.dev.bfd, stats);
4828 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
4830 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4831 struct xbridge *xbridge;
4832 struct xbundle *xbundle;
4833 struct flow_wildcards wc;
4835 xbridge = xbridge_lookup(xcfg, ofproto);
4840 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
4846 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
4849 /* Push stats and perform side effects of flow translation. */
4851 xlate_push_stats(struct xlate_cache *xcache,
4852 const struct dpif_flow_stats *stats)
4854 struct xc_entry *entry;
4855 struct ofpbuf entries = xcache->entries;
4856 uint8_t dmac[ETH_ADDR_LEN];
4858 if (!stats->n_packets) {
4862 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
4863 switch (entry->type) {
4865 rule_dpif_credit_stats(entry->u.rule, stats);
4868 bond_account(entry->u.bond.bond, entry->u.bond.flow,
4869 entry->u.bond.vid, stats->n_bytes);
4872 xlate_cache_netdev(entry, stats);
4875 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
4876 entry->u.nf.iface, stats);
4879 mirror_update_stats(entry->u.mirror.mbridge,
4880 entry->u.mirror.mirrors,
4881 stats->n_packets, stats->n_bytes);
4884 ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
4887 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
4888 entry->u.normal.vlan);
4890 case XC_FIN_TIMEOUT:
4891 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
4892 entry->u.fin.idle, entry->u.fin.hard);
4895 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
4899 /* Lookup arp to avoid arp timeout. */
4900 tnl_arp_lookup(entry->u.tnl_arp_cache.br_name, entry->u.tnl_arp_cache.d_ip, dmac);
4909 xlate_dev_unref(struct xc_entry *entry)
4911 if (entry->u.dev.tx) {
4912 netdev_close(entry->u.dev.tx);
4914 if (entry->u.dev.rx) {
4915 netdev_close(entry->u.dev.rx);
4917 if (entry->u.dev.bfd) {
4918 bfd_unref(entry->u.dev.bfd);
4923 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
4925 netflow_flow_clear(netflow, flow);
4926 netflow_unref(netflow);
4931 xlate_cache_clear(struct xlate_cache *xcache)
4933 struct xc_entry *entry;
4934 struct ofpbuf entries;
4940 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
4941 switch (entry->type) {
4943 rule_dpif_unref(entry->u.rule);
4946 free(entry->u.bond.flow);
4947 bond_unref(entry->u.bond.bond);
4950 xlate_dev_unref(entry);
4953 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
4956 mbridge_unref(entry->u.mirror.mbridge);
4959 free(entry->u.learn.fm);
4960 ofpbuf_delete(entry->u.learn.ofpacts);
4963 free(entry->u.normal.flow);
4965 case XC_FIN_TIMEOUT:
4966 /* 'u.fin.rule' is always already held as a XC_RULE, which
4967 * has already released it's reference above. */
4970 group_dpif_unref(entry->u.group.group);
4979 ofpbuf_clear(&xcache->entries);
4983 xlate_cache_delete(struct xlate_cache *xcache)
4985 xlate_cache_clear(xcache);
4986 ofpbuf_uninit(&xcache->entries);