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 /* A controller may use OFPP_NONE as the ingress port to indicate that
233 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
234 * when an input bundle is needed for validation (e.g., mirroring or
235 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
236 * any 'port' structs, so care must be taken when dealing with it. */
237 static struct xbundle ofpp_none_bundle = {
239 .vlan_mode = PORT_VLAN_TRUNK
242 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
243 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
244 * traffic egressing the 'ofport' with that priority should be marked with. */
245 struct skb_priority_to_dscp {
246 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
247 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
249 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
265 /* xlate_cache entries hold enough information to perform the side effects of
266 * xlate_actions() for a rule, without needing to perform rule translation
267 * from scratch. The primary usage of these is to submit statistics to objects
268 * that a flow relates to, although they may be used for other effects as well
269 * (for instance, refreshing hard timeouts for learned flows). */
273 struct rule_dpif *rule;
280 struct netflow *netflow;
285 struct mbridge *mbridge;
286 mirror_mask_t mirrors;
294 struct ofproto_dpif *ofproto;
295 struct ofputil_flow_mod *fm;
296 struct ofpbuf *ofpacts;
299 struct ofproto_dpif *ofproto;
304 struct rule_dpif *rule;
309 struct group_dpif *group;
310 struct ofputil_bucket *bucket;
313 char br_name[IFNAMSIZ];
319 #define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
320 entries = xcache->entries; \
321 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
323 entry = ofpbuf_try_pull(&entries, sizeof *entry))
326 struct ofpbuf entries;
329 /* Xlate config contains hash maps of all bridges, bundles and ports.
330 * Xcfgp contains the pointer to the current xlate configuration.
331 * When the main thread needs to change the configuration, it copies xcfgp to
332 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
333 * does not block handler and revalidator threads. */
335 struct hmap xbridges;
336 struct hmap xbundles;
339 static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
340 static struct xlate_cfg *new_xcfg = NULL;
342 static bool may_receive(const struct xport *, struct xlate_ctx *);
343 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
345 static void xlate_normal(struct xlate_ctx *);
346 static inline void xlate_report(struct xlate_ctx *, const char *);
347 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
348 uint8_t table_id, bool may_packet_in,
349 bool honor_table_miss);
350 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
351 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
352 static void output_normal(struct xlate_ctx *, const struct xbundle *,
354 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
356 static struct xbridge *xbridge_lookup(struct xlate_cfg *,
357 const struct ofproto_dpif *);
358 static struct xbundle *xbundle_lookup(struct xlate_cfg *,
359 const struct ofbundle *);
360 static struct xport *xport_lookup(struct xlate_cfg *,
361 const struct ofport_dpif *);
362 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
363 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
364 uint32_t skb_priority);
365 static void clear_skb_priorities(struct xport *);
366 static size_t count_skb_priorities(const struct xport *);
367 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
370 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
372 static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
373 static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
374 static void xlate_xport_init(struct xlate_cfg *, struct xport *);
375 static void xlate_xbridge_set(struct xbridge *, struct dpif *,
376 const struct mac_learning *, struct stp *,
377 struct rstp *, const struct mcast_snooping *,
378 const struct mbridge *,
379 const struct dpif_sflow *,
380 const struct dpif_ipfix *,
381 const struct netflow *,
382 bool forward_bpdu, bool has_in_band,
384 bool variable_length_userdata,
385 size_t max_mpls_depth,
386 bool masked_set_action);
387 static void xlate_xbundle_set(struct xbundle *xbundle,
388 enum port_vlan_mode vlan_mode, int vlan,
389 unsigned long *trunks, bool use_priority_tags,
390 const struct bond *bond, const struct lacp *lacp,
392 static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
393 const struct netdev *netdev, const struct cfm *cfm,
394 const struct bfd *bfd, const struct lldp *lldp,
395 int stp_port_no, const struct rstp_port *rstp_port,
396 enum ofputil_port_config config,
397 enum ofputil_port_state state, bool is_tunnel,
399 static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
400 static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
401 static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
402 static void xlate_xbridge_copy(struct xbridge *);
403 static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
404 static void xlate_xport_copy(struct xbridge *, struct xbundle *,
406 static void xlate_xcfg_free(struct xlate_cfg *);
409 xlate_report(struct xlate_ctx *ctx, const char *s)
411 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
412 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
417 xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
419 list_init(&xbridge->xbundles);
420 hmap_init(&xbridge->xports);
421 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
422 hash_pointer(xbridge->ofproto, 0));
426 xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
428 list_init(&xbundle->xports);
429 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
430 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
431 hash_pointer(xbundle->ofbundle, 0));
435 xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
437 hmap_init(&xport->skb_priorities);
438 hmap_insert(&xcfg->xports, &xport->hmap_node,
439 hash_pointer(xport->ofport, 0));
440 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
441 hash_ofp_port(xport->ofp_port));
445 xlate_xbridge_set(struct xbridge *xbridge,
447 const struct mac_learning *ml, struct stp *stp,
448 struct rstp *rstp, const struct mcast_snooping *ms,
449 const struct mbridge *mbridge,
450 const struct dpif_sflow *sflow,
451 const struct dpif_ipfix *ipfix,
452 const struct netflow *netflow,
453 bool forward_bpdu, bool has_in_band,
455 bool variable_length_userdata,
456 size_t max_mpls_depth,
457 bool masked_set_action)
459 if (xbridge->ml != ml) {
460 mac_learning_unref(xbridge->ml);
461 xbridge->ml = mac_learning_ref(ml);
464 if (xbridge->ms != ms) {
465 mcast_snooping_unref(xbridge->ms);
466 xbridge->ms = mcast_snooping_ref(ms);
469 if (xbridge->mbridge != mbridge) {
470 mbridge_unref(xbridge->mbridge);
471 xbridge->mbridge = mbridge_ref(mbridge);
474 if (xbridge->sflow != sflow) {
475 dpif_sflow_unref(xbridge->sflow);
476 xbridge->sflow = dpif_sflow_ref(sflow);
479 if (xbridge->ipfix != ipfix) {
480 dpif_ipfix_unref(xbridge->ipfix);
481 xbridge->ipfix = dpif_ipfix_ref(ipfix);
484 if (xbridge->stp != stp) {
485 stp_unref(xbridge->stp);
486 xbridge->stp = stp_ref(stp);
489 if (xbridge->rstp != rstp) {
490 rstp_unref(xbridge->rstp);
491 xbridge->rstp = rstp_ref(rstp);
494 if (xbridge->netflow != netflow) {
495 netflow_unref(xbridge->netflow);
496 xbridge->netflow = netflow_ref(netflow);
499 xbridge->dpif = dpif;
500 xbridge->forward_bpdu = forward_bpdu;
501 xbridge->has_in_band = has_in_band;
502 xbridge->enable_recirc = enable_recirc;
503 xbridge->variable_length_userdata = variable_length_userdata;
504 xbridge->max_mpls_depth = max_mpls_depth;
505 xbridge->masked_set_action = masked_set_action;
509 xlate_xbundle_set(struct xbundle *xbundle,
510 enum port_vlan_mode vlan_mode, int vlan,
511 unsigned long *trunks, bool use_priority_tags,
512 const struct bond *bond, const struct lacp *lacp,
515 ovs_assert(xbundle->xbridge);
517 xbundle->vlan_mode = vlan_mode;
518 xbundle->vlan = vlan;
519 xbundle->trunks = trunks;
520 xbundle->use_priority_tags = use_priority_tags;
521 xbundle->floodable = floodable;
523 if (xbundle->bond != bond) {
524 bond_unref(xbundle->bond);
525 xbundle->bond = bond_ref(bond);
528 if (xbundle->lacp != lacp) {
529 lacp_unref(xbundle->lacp);
530 xbundle->lacp = lacp_ref(lacp);
535 xlate_xport_set(struct xport *xport, odp_port_t odp_port,
536 const struct netdev *netdev, const struct cfm *cfm,
537 const struct bfd *bfd, const struct lldp *lldp, int stp_port_no,
538 const struct rstp_port* rstp_port,
539 enum ofputil_port_config config, enum ofputil_port_state state,
540 bool is_tunnel, bool may_enable)
542 xport->config = config;
543 xport->state = state;
544 xport->stp_port_no = stp_port_no;
545 xport->is_tunnel = is_tunnel;
546 xport->may_enable = may_enable;
547 xport->odp_port = odp_port;
549 if (xport->rstp_port != rstp_port) {
550 rstp_port_unref(xport->rstp_port);
551 xport->rstp_port = rstp_port_ref(rstp_port);
554 if (xport->cfm != cfm) {
555 cfm_unref(xport->cfm);
556 xport->cfm = cfm_ref(cfm);
559 if (xport->bfd != bfd) {
560 bfd_unref(xport->bfd);
561 xport->bfd = bfd_ref(bfd);
564 if (xport->lldp != lldp) {
565 lldp_unref(xport->lldp);
566 xport->lldp = lldp_ref(lldp);
569 if (xport->netdev != netdev) {
570 netdev_close(xport->netdev);
571 xport->netdev = netdev_ref(netdev);
576 xlate_xbridge_copy(struct xbridge *xbridge)
578 struct xbundle *xbundle;
580 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
581 new_xbridge->ofproto = xbridge->ofproto;
582 new_xbridge->name = xstrdup(xbridge->name);
583 xlate_xbridge_init(new_xcfg, new_xbridge);
585 xlate_xbridge_set(new_xbridge,
586 xbridge->dpif, xbridge->ml, xbridge->stp,
587 xbridge->rstp, xbridge->ms, xbridge->mbridge,
588 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
589 xbridge->forward_bpdu,
590 xbridge->has_in_band, xbridge->enable_recirc,
591 xbridge->variable_length_userdata,
592 xbridge->max_mpls_depth, xbridge->masked_set_action);
593 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
594 xlate_xbundle_copy(new_xbridge, xbundle);
597 /* Copy xports which are not part of a xbundle */
598 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
599 if (!xport->xbundle) {
600 xlate_xport_copy(new_xbridge, NULL, xport);
606 xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
609 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
610 new_xbundle->ofbundle = xbundle->ofbundle;
611 new_xbundle->xbridge = xbridge;
612 new_xbundle->name = xstrdup(xbundle->name);
613 xlate_xbundle_init(new_xcfg, new_xbundle);
615 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
616 xbundle->vlan, xbundle->trunks,
617 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
619 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
620 xlate_xport_copy(xbridge, new_xbundle, xport);
625 xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
628 struct skb_priority_to_dscp *pdscp, *new_pdscp;
629 struct xport *new_xport = xzalloc(sizeof *xport);
630 new_xport->ofport = xport->ofport;
631 new_xport->ofp_port = xport->ofp_port;
632 new_xport->xbridge = xbridge;
633 xlate_xport_init(new_xcfg, new_xport);
635 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
636 xport->bfd, xport->lldp, xport->stp_port_no,
637 xport->rstp_port, xport->config, xport->state,
638 xport->is_tunnel, xport->may_enable);
641 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
643 new_xport->peer = peer;
644 new_xport->peer->peer = new_xport;
649 new_xport->xbundle = xbundle;
650 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
653 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
654 new_pdscp = xmalloc(sizeof *pdscp);
655 new_pdscp->skb_priority = pdscp->skb_priority;
656 new_pdscp->dscp = pdscp->dscp;
657 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
658 hash_int(new_pdscp->skb_priority, 0));
662 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
663 * configuration in xcfgp.
665 * This needs to be called after editing the xlate configuration.
667 * Functions that edit the new xlate configuration are
668 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
674 * edit_xlate_configuration();
676 * xlate_txn_commit(); */
678 xlate_txn_commit(void)
680 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
682 ovsrcu_set(&xcfgp, new_xcfg);
683 ovsrcu_synchronize();
684 xlate_xcfg_free(xcfg);
688 /* Copies the current xlate configuration in xcfgp to new_xcfg.
690 * This needs to be called prior to editing the xlate configuration. */
692 xlate_txn_start(void)
694 struct xbridge *xbridge;
695 struct xlate_cfg *xcfg;
697 ovs_assert(!new_xcfg);
699 new_xcfg = xmalloc(sizeof *new_xcfg);
700 hmap_init(&new_xcfg->xbridges);
701 hmap_init(&new_xcfg->xbundles);
702 hmap_init(&new_xcfg->xports);
704 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
709 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
710 xlate_xbridge_copy(xbridge);
716 xlate_xcfg_free(struct xlate_cfg *xcfg)
718 struct xbridge *xbridge, *next_xbridge;
724 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
725 xlate_xbridge_remove(xcfg, xbridge);
728 hmap_destroy(&xcfg->xbridges);
729 hmap_destroy(&xcfg->xbundles);
730 hmap_destroy(&xcfg->xports);
735 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
737 const struct mac_learning *ml, struct stp *stp,
738 struct rstp *rstp, const struct mcast_snooping *ms,
739 const struct mbridge *mbridge,
740 const struct dpif_sflow *sflow,
741 const struct dpif_ipfix *ipfix,
742 const struct netflow *netflow,
743 bool forward_bpdu, bool has_in_band, bool enable_recirc,
744 bool variable_length_userdata, size_t max_mpls_depth,
745 bool masked_set_action)
747 struct xbridge *xbridge;
749 ovs_assert(new_xcfg);
751 xbridge = xbridge_lookup(new_xcfg, ofproto);
753 xbridge = xzalloc(sizeof *xbridge);
754 xbridge->ofproto = ofproto;
756 xlate_xbridge_init(new_xcfg, xbridge);
760 xbridge->name = xstrdup(name);
762 xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
763 netflow, forward_bpdu, has_in_band, enable_recirc,
764 variable_length_userdata, max_mpls_depth,
769 xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
771 struct xbundle *xbundle, *next_xbundle;
772 struct xport *xport, *next_xport;
778 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
779 xlate_xport_remove(xcfg, xport);
782 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
783 xlate_xbundle_remove(xcfg, xbundle);
786 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
787 mac_learning_unref(xbridge->ml);
788 mcast_snooping_unref(xbridge->ms);
789 mbridge_unref(xbridge->mbridge);
790 dpif_sflow_unref(xbridge->sflow);
791 dpif_ipfix_unref(xbridge->ipfix);
792 stp_unref(xbridge->stp);
793 rstp_unref(xbridge->rstp);
794 hmap_destroy(&xbridge->xports);
800 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
802 struct xbridge *xbridge;
804 ovs_assert(new_xcfg);
806 xbridge = xbridge_lookup(new_xcfg, ofproto);
807 xlate_xbridge_remove(new_xcfg, xbridge);
811 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
812 const char *name, enum port_vlan_mode vlan_mode, int vlan,
813 unsigned long *trunks, bool use_priority_tags,
814 const struct bond *bond, const struct lacp *lacp,
817 struct xbundle *xbundle;
819 ovs_assert(new_xcfg);
821 xbundle = xbundle_lookup(new_xcfg, ofbundle);
823 xbundle = xzalloc(sizeof *xbundle);
824 xbundle->ofbundle = ofbundle;
825 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
827 xlate_xbundle_init(new_xcfg, xbundle);
831 xbundle->name = xstrdup(name);
833 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
834 use_priority_tags, bond, lacp, floodable);
838 xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
840 struct xport *xport, *next;
846 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
847 list_remove(&xport->bundle_node);
848 xport->xbundle = NULL;
851 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
852 list_remove(&xbundle->list_node);
853 bond_unref(xbundle->bond);
854 lacp_unref(xbundle->lacp);
860 xlate_bundle_remove(struct ofbundle *ofbundle)
862 struct xbundle *xbundle;
864 ovs_assert(new_xcfg);
866 xbundle = xbundle_lookup(new_xcfg, ofbundle);
867 xlate_xbundle_remove(new_xcfg, xbundle);
871 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
872 struct ofport_dpif *ofport, ofp_port_t ofp_port,
873 odp_port_t odp_port, const struct netdev *netdev,
874 const struct cfm *cfm, const struct bfd *bfd,
875 const struct lldp *lldp, struct ofport_dpif *peer,
876 int stp_port_no, const struct rstp_port *rstp_port,
877 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
878 enum ofputil_port_config config,
879 enum ofputil_port_state state, bool is_tunnel,
885 ovs_assert(new_xcfg);
887 xport = xport_lookup(new_xcfg, ofport);
889 xport = xzalloc(sizeof *xport);
890 xport->ofport = ofport;
891 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
892 xport->ofp_port = ofp_port;
894 xlate_xport_init(new_xcfg, xport);
897 ovs_assert(xport->ofp_port == ofp_port);
899 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, lldp,
900 stp_port_no, rstp_port, config, state, is_tunnel,
904 xport->peer->peer = NULL;
906 xport->peer = xport_lookup(new_xcfg, peer);
908 xport->peer->peer = xport;
911 if (xport->xbundle) {
912 list_remove(&xport->bundle_node);
914 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
915 if (xport->xbundle) {
916 list_insert(&xport->xbundle->xports, &xport->bundle_node);
919 clear_skb_priorities(xport);
920 for (i = 0; i < n_qdscp; i++) {
921 struct skb_priority_to_dscp *pdscp;
922 uint32_t skb_priority;
924 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
929 pdscp = xmalloc(sizeof *pdscp);
930 pdscp->skb_priority = skb_priority;
931 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
932 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
933 hash_int(pdscp->skb_priority, 0));
938 xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
945 xport->peer->peer = NULL;
949 if (xport->xbundle) {
950 list_remove(&xport->bundle_node);
953 clear_skb_priorities(xport);
954 hmap_destroy(&xport->skb_priorities);
956 hmap_remove(&xcfg->xports, &xport->hmap_node);
957 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
959 netdev_close(xport->netdev);
960 rstp_port_unref(xport->rstp_port);
961 cfm_unref(xport->cfm);
962 bfd_unref(xport->bfd);
963 lldp_unref(xport->lldp);
968 xlate_ofport_remove(struct ofport_dpif *ofport)
972 ovs_assert(new_xcfg);
974 xport = xport_lookup(new_xcfg, ofport);
975 xlate_xport_remove(new_xcfg, xport);
978 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
979 * returns the corresponding struct xport, or NULL if none is found. */
980 static struct xport *
981 xlate_lookup_xport(const struct dpif_backer *backer, const struct flow *flow)
983 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
985 return xport_lookup(xcfg, tnl_port_should_receive(flow)
986 ? tnl_port_receive(flow)
987 : odp_port_to_ofport(backer, flow->in_port.odp_port));
990 static struct ofproto_dpif *
991 xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
992 ofp_port_t *ofp_in_port, const struct xport **xportp)
994 struct ofproto_dpif *recv_ofproto = NULL;
995 struct ofproto_dpif *recirc_ofproto = NULL;
996 const struct xport *xport;
997 ofp_port_t in_port = OFPP_NONE;
999 *xportp = xport = xlate_lookup_xport(backer, flow);
1002 recv_ofproto = xport->xbridge->ofproto;
1003 in_port = xport->ofp_port;
1006 /* When recirc_id is set in 'flow', checks whether the ofproto_dpif that
1007 * corresponds to the recirc_id is same as the receiving bridge. If they
1008 * are the same, uses the 'recv_ofproto' and keeps the 'ofp_in_port' as
1009 * assigned. Otherwise, uses the 'recirc_ofproto' that owns recirc_id and
1010 * assigns OFPP_NONE to 'ofp_in_port'. Doing this is in that, the
1011 * recirculated flow must be processced by the ofproto which originates
1012 * the recirculation, and as bridges can only see their own ports, the
1013 * in_port of the 'recv_ofproto' should not be passed to the
1016 * Admittedly, setting the 'ofp_in_port' to OFPP_NONE limits the
1017 * 'recirc_ofproto' from meaningfully matching on in_port of recirculated
1018 * flow, and should be fixed in the near future.
1020 * TODO: Restore the original patch port.
1022 if (recv_ofproto && flow->recirc_id) {
1023 recirc_ofproto = ofproto_dpif_recirc_get_ofproto(backer,
1025 if (recv_ofproto != recirc_ofproto) {
1026 *xportp = xport = NULL;
1027 in_port = OFPP_NONE;
1032 *ofp_in_port = in_port;
1035 return xport ? recv_ofproto : recirc_ofproto;
1038 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1039 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1040 struct ofproto_dpif *
1041 xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1042 ofp_port_t *ofp_in_port)
1044 const struct xport *xport;
1046 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1049 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1050 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
1051 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1052 * handles for those protocols if they're enabled. Caller may use the returned
1053 * pointers until quiescing, for longer term use additional references must
1056 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1059 xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1060 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1061 struct dpif_sflow **sflow, struct netflow **netflow,
1062 ofp_port_t *ofp_in_port)
1064 struct ofproto_dpif *ofproto;
1065 const struct xport *xport;
1067 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1074 *ofprotop = ofproto;
1078 *ipfix = xport ? xport->xbridge->ipfix : NULL;
1082 *sflow = xport ? xport->xbridge->sflow : NULL;
1086 *netflow = xport ? xport->xbridge->netflow : NULL;
1092 static struct xbridge *
1093 xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
1095 struct hmap *xbridges;
1096 struct xbridge *xbridge;
1098 if (!ofproto || !xcfg) {
1102 xbridges = &xcfg->xbridges;
1104 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
1106 if (xbridge->ofproto == ofproto) {
1113 static struct xbundle *
1114 xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
1116 struct hmap *xbundles;
1117 struct xbundle *xbundle;
1119 if (!ofbundle || !xcfg) {
1123 xbundles = &xcfg->xbundles;
1125 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
1127 if (xbundle->ofbundle == ofbundle) {
1134 static struct xport *
1135 xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
1137 struct hmap *xports;
1138 struct xport *xport;
1140 if (!ofport || !xcfg) {
1144 xports = &xcfg->xports;
1146 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
1148 if (xport->ofport == ofport) {
1155 static struct stp_port *
1156 xport_get_stp_port(const struct xport *xport)
1158 return xport->xbridge->stp && xport->stp_port_no != -1
1159 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1164 xport_stp_learn_state(const struct xport *xport)
1166 struct stp_port *sp = xport_get_stp_port(xport);
1168 ? stp_learn_in_state(stp_port_get_state(sp))
1173 xport_stp_forward_state(const struct xport *xport)
1175 struct stp_port *sp = xport_get_stp_port(xport);
1177 ? stp_forward_in_state(stp_port_get_state(sp))
1182 xport_stp_should_forward_bpdu(const struct xport *xport)
1184 struct stp_port *sp = xport_get_stp_port(xport);
1185 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
1188 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1189 * were used to make the determination.*/
1191 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1193 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1194 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1195 return is_stp(flow);
1199 stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1201 struct stp_port *sp = xport_get_stp_port(xport);
1202 struct dp_packet payload = *packet;
1203 struct eth_header *eth = dp_packet_data(&payload);
1205 /* Sink packets on ports that have STP disabled when the bridge has
1207 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1211 /* Trim off padding on payload. */
1212 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1213 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1216 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1217 stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
1221 static enum rstp_state
1222 xport_get_rstp_port_state(const struct xport *xport)
1224 return xport->rstp_port
1225 ? rstp_port_get_state(xport->rstp_port)
1230 xport_rstp_learn_state(const struct xport *xport)
1232 return xport->xbridge->rstp && xport->rstp_port
1233 ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1238 xport_rstp_forward_state(const struct xport *xport)
1240 return xport->xbridge->rstp && xport->rstp_port
1241 ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1246 xport_rstp_should_manage_bpdu(const struct xport *xport)
1248 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
1252 rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1254 struct dp_packet payload = *packet;
1255 struct eth_header *eth = dp_packet_data(&payload);
1257 /* Sink packets on ports that have no RSTP. */
1258 if (!xport->rstp_port) {
1262 /* Trim off padding on payload. */
1263 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1264 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1267 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1268 rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
1269 dp_packet_size(&payload));
1273 static struct xport *
1274 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1276 struct xport *xport;
1278 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1280 if (xport->ofp_port == ofp_port) {
1288 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1290 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1291 return xport ? xport->odp_port : ODPP_NONE;
1295 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1297 struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1298 return xport && xport->may_enable;
1301 static struct ofputil_bucket *
1302 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1306 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1308 struct group_dpif *group;
1310 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1311 struct ofputil_bucket *bucket;
1313 bucket = group_first_live_bucket(ctx, group, depth);
1314 group_dpif_unref(group);
1315 return bucket == NULL;
1321 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1324 bucket_is_alive(const struct xlate_ctx *ctx,
1325 struct ofputil_bucket *bucket, int depth)
1327 if (depth >= MAX_LIVENESS_RECURSION) {
1328 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1330 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1331 MAX_LIVENESS_RECURSION);
1335 return (!ofputil_bucket_has_liveness(bucket)
1336 || (bucket->watch_port != OFPP_ANY
1337 && odp_port_is_alive(ctx, bucket->watch_port))
1338 || (bucket->watch_group != OFPG_ANY
1339 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
1342 static struct ofputil_bucket *
1343 group_first_live_bucket(const struct xlate_ctx *ctx,
1344 const struct group_dpif *group, int depth)
1346 struct ofputil_bucket *bucket;
1347 const struct ovs_list *buckets;
1349 group_dpif_get_buckets(group, &buckets);
1350 LIST_FOR_EACH (bucket, list_node, buckets) {
1351 if (bucket_is_alive(ctx, bucket, depth)) {
1359 static struct ofputil_bucket *
1360 group_best_live_bucket(const struct xlate_ctx *ctx,
1361 const struct group_dpif *group,
1364 struct ofputil_bucket *best_bucket = NULL;
1365 uint32_t best_score = 0;
1368 struct ofputil_bucket *bucket;
1369 const struct ovs_list *buckets;
1371 group_dpif_get_buckets(group, &buckets);
1372 LIST_FOR_EACH (bucket, list_node, buckets) {
1373 if (bucket_is_alive(ctx, bucket, 0)) {
1374 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
1375 if (score >= best_score) {
1376 best_bucket = bucket;
1387 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
1389 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1390 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1394 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1396 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1399 static mirror_mask_t
1400 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1402 return xbundle != &ofpp_none_bundle
1403 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1407 static mirror_mask_t
1408 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
1410 return xbundle != &ofpp_none_bundle
1411 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1415 static mirror_mask_t
1416 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
1418 return xbundle != &ofpp_none_bundle
1419 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1423 static struct xbundle *
1424 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1425 bool warn, struct xport **in_xportp)
1427 struct xport *xport;
1429 /* Find the port and bundle for the received packet. */
1430 xport = get_ofp_port(xbridge, in_port);
1434 if (xport && xport->xbundle) {
1435 return xport->xbundle;
1438 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1439 * which a controller may use as the ingress port for traffic that
1440 * it is sourcing. */
1441 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
1442 return &ofpp_none_bundle;
1445 /* Odd. A few possible reasons here:
1447 * - We deleted a port but there are still a few packets queued up
1450 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1451 * we don't know about.
1453 * - The ofproto client didn't configure the port as part of a bundle.
1454 * This is particularly likely to happen if a packet was received on the
1455 * port after it was created, but before the client had a chance to
1456 * configure its bundle.
1459 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1461 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1462 "port %"PRIu16, xbridge->name, in_port);
1468 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
1470 const struct xbridge *xbridge = ctx->xbridge;
1471 mirror_mask_t mirrors;
1472 struct xbundle *in_xbundle;
1476 mirrors = ctx->xout->mirrors;
1477 ctx->xout->mirrors = 0;
1479 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
1480 ctx->xin->packet != NULL, NULL);
1484 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
1486 /* Drop frames on bundles reserved for mirroring. */
1487 if (xbundle_mirror_out(xbridge, in_xbundle)) {
1488 if (ctx->xin->packet != NULL) {
1489 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1490 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1491 "%s, which is reserved exclusively for mirroring",
1492 ctx->xbridge->name, in_xbundle->name);
1494 ofpbuf_clear(ctx->xout->odp_actions);
1499 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
1500 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1503 vlan = input_vid_to_vlan(in_xbundle, vid);
1509 /* Restore the original packet before adding the mirror actions. */
1510 ctx->xin->flow = *orig_flow;
1513 mirror_mask_t dup_mirrors;
1514 struct ofbundle *out;
1515 unsigned long *vlans;
1520 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1521 &vlans, &dup_mirrors, &out, &out_vlan);
1522 ovs_assert(has_mirror);
1525 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1527 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
1530 if (!vlan_mirrored) {
1531 mirrors = zero_rightmost_1bit(mirrors);
1535 mirrors &= ~dup_mirrors;
1536 ctx->xout->mirrors |= dup_mirrors;
1538 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1539 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1541 output_normal(ctx, out_xbundle, vlan);
1543 } else if (vlan != out_vlan
1544 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
1545 struct xbundle *xbundle;
1547 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1548 if (xbundle_includes_vlan(xbundle, out_vlan)
1549 && !xbundle_mirror_out(xbridge, xbundle)) {
1550 output_normal(ctx, xbundle, out_vlan);
1557 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1558 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1559 * the bundle on which the packet was received, returns the VLAN to which the
1562 * Both 'vid' and the return value are in the range 0...4095. */
1564 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1566 switch (in_xbundle->vlan_mode) {
1567 case PORT_VLAN_ACCESS:
1568 return in_xbundle->vlan;
1571 case PORT_VLAN_TRUNK:
1574 case PORT_VLAN_NATIVE_UNTAGGED:
1575 case PORT_VLAN_NATIVE_TAGGED:
1576 return vid ? vid : in_xbundle->vlan;
1583 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1584 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1587 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1588 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1591 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1593 /* Allow any VID on the OFPP_NONE port. */
1594 if (in_xbundle == &ofpp_none_bundle) {
1598 switch (in_xbundle->vlan_mode) {
1599 case PORT_VLAN_ACCESS:
1602 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1603 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1604 "packet received on port %s configured as VLAN "
1605 "%"PRIu16" access port", vid, in_xbundle->name,
1612 case PORT_VLAN_NATIVE_UNTAGGED:
1613 case PORT_VLAN_NATIVE_TAGGED:
1615 /* Port must always carry its native VLAN. */
1619 case PORT_VLAN_TRUNK:
1620 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1622 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1623 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1624 "received on port %s not configured for trunking "
1625 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1637 /* Given 'vlan', the VLAN that a packet belongs to, and
1638 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1639 * that should be included in the 802.1Q header. (If the return value is 0,
1640 * then the 802.1Q header should only be included in the packet if there is a
1643 * Both 'vlan' and the return value are in the range 0...4095. */
1645 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1647 switch (out_xbundle->vlan_mode) {
1648 case PORT_VLAN_ACCESS:
1651 case PORT_VLAN_TRUNK:
1652 case PORT_VLAN_NATIVE_TAGGED:
1655 case PORT_VLAN_NATIVE_UNTAGGED:
1656 return vlan == out_xbundle->vlan ? 0 : vlan;
1664 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1667 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1669 ovs_be16 tci, old_tci;
1670 struct xport *xport;
1672 vid = output_vlan_to_vid(out_xbundle, vlan);
1673 if (list_is_empty(&out_xbundle->xports)) {
1674 /* Partially configured bundle with no slaves. Drop the packet. */
1676 } else if (!out_xbundle->bond) {
1677 ctx->use_recirc = false;
1678 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1681 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1682 struct flow_wildcards *wc = &ctx->xout->wc;
1683 struct xlate_recirc *xr = &ctx->recirc;
1684 struct ofport_dpif *ofport;
1686 if (ctx->xbridge->enable_recirc) {
1687 ctx->use_recirc = bond_may_recirc(
1688 out_xbundle->bond, &xr->recirc_id, &xr->hash_basis);
1690 if (ctx->use_recirc) {
1691 /* Only TCP mode uses recirculation. */
1692 xr->hash_alg = OVS_HASH_ALG_L4;
1693 bond_update_post_recirc_rules(out_xbundle->bond, false);
1695 /* Recirculation does not require unmasking hash fields. */
1700 ofport = bond_choose_output_slave(out_xbundle->bond,
1701 &ctx->xin->flow, wc, vid);
1702 xport = xport_lookup(xcfg, ofport);
1705 /* No slaves enabled, so drop packet. */
1709 /* If ctx->xout->use_recirc is set, the main thread will handle stats
1710 * accounting for this bond. */
1711 if (!ctx->use_recirc) {
1712 if (ctx->xin->resubmit_stats) {
1713 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1714 ctx->xin->resubmit_stats->n_bytes);
1716 if (ctx->xin->xcache) {
1717 struct xc_entry *entry;
1720 flow = &ctx->xin->flow;
1721 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1722 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1723 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1724 entry->u.bond.vid = vid;
1729 old_tci = *flow_tci;
1731 if (tci || out_xbundle->use_priority_tags) {
1732 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1734 tci |= htons(VLAN_CFI);
1739 compose_output_action(ctx, xport->ofp_port);
1740 *flow_tci = old_tci;
1743 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1744 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1745 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1747 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1749 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1753 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1754 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1758 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1759 if (flow->nw_proto == ARP_OP_REPLY) {
1761 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1762 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1763 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1765 return flow->nw_src == flow->nw_dst;
1771 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1772 * dropped. Returns true if they may be forwarded, false if they should be
1775 * 'in_port' must be the xport that corresponds to flow->in_port.
1776 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1778 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1779 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1780 * checked by input_vid_is_valid().
1782 * May also add tags to '*tags', although the current implementation only does
1783 * so in one special case.
1786 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1789 struct xbundle *in_xbundle = in_port->xbundle;
1790 const struct xbridge *xbridge = ctx->xbridge;
1791 struct flow *flow = &ctx->xin->flow;
1793 /* Drop frames for reserved multicast addresses
1794 * only if forward_bpdu option is absent. */
1795 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1796 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1800 if (in_xbundle->bond) {
1801 struct mac_entry *mac;
1803 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1809 xlate_report(ctx, "bonding refused admissibility, dropping");
1812 case BV_DROP_IF_MOVED:
1813 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1814 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1816 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
1817 && (!is_gratuitous_arp(flow, &ctx->xout->wc)
1818 || mac_entry_is_grat_arp_locked(mac))) {
1819 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1820 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1824 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1832 /* Checks whether a MAC learning update is necessary for MAC learning table
1833 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1836 * Most packets processed through the MAC learning table do not actually
1837 * change it in any way. This function requires only a read lock on the MAC
1838 * learning table, so it is much cheaper in this common case.
1840 * Keep the code here synchronized with that in update_learning_table__()
1843 is_mac_learning_update_needed(const struct mac_learning *ml,
1844 const struct flow *flow,
1845 struct flow_wildcards *wc,
1846 int vlan, struct xbundle *in_xbundle)
1847 OVS_REQ_RDLOCK(ml->rwlock)
1849 struct mac_entry *mac;
1851 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1855 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1856 if (!mac || mac_entry_age(ml, mac)) {
1860 if (is_gratuitous_arp(flow, wc)) {
1861 /* We don't want to learn from gratuitous ARP packets that are
1862 * reflected back over bond slaves so we lock the learning table. */
1863 if (!in_xbundle->bond) {
1865 } else if (mac_entry_is_grat_arp_locked(mac)) {
1870 return mac_entry_get_port(ml, mac) != in_xbundle->ofbundle;
1874 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1875 * received on 'in_xbundle' in 'vlan'.
1877 * This code repeats all the checks in is_mac_learning_update_needed() because
1878 * the lock was released between there and here and thus the MAC learning state
1879 * could have changed.
1881 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1884 update_learning_table__(const struct xbridge *xbridge,
1885 const struct flow *flow, struct flow_wildcards *wc,
1886 int vlan, struct xbundle *in_xbundle)
1887 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1889 struct mac_entry *mac;
1891 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1895 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1896 if (is_gratuitous_arp(flow, wc)) {
1897 /* We don't want to learn from gratuitous ARP packets that are
1898 * reflected back over bond slaves so we lock the learning table. */
1899 if (!in_xbundle->bond) {
1900 mac_entry_set_grat_arp_lock(mac);
1901 } else if (mac_entry_is_grat_arp_locked(mac)) {
1906 if (mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle) {
1907 /* The log messages here could actually be useful in debugging,
1908 * so keep the rate limit relatively high. */
1909 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1911 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1912 "on port %s in VLAN %d",
1913 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1914 in_xbundle->name, vlan);
1916 mac_entry_set_port(xbridge->ml, mac, in_xbundle->ofbundle);
1921 update_learning_table(const struct xbridge *xbridge,
1922 const struct flow *flow, struct flow_wildcards *wc,
1923 int vlan, struct xbundle *in_xbundle)
1927 /* Don't learn the OFPP_NONE port. */
1928 if (in_xbundle == &ofpp_none_bundle) {
1932 /* First try the common case: no change to MAC learning table. */
1933 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1934 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1936 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1939 /* Slow path: MAC learning table might need an update. */
1940 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1941 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1942 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1946 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1947 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
1949 update_mcast_snooping_table__(const struct xbridge *xbridge,
1950 const struct flow *flow,
1951 struct mcast_snooping *ms,
1952 ovs_be32 ip4, int vlan,
1953 struct xbundle *in_xbundle)
1954 OVS_REQ_WRLOCK(ms->rwlock)
1956 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
1958 switch (ntohs(flow->tp_src)) {
1959 case IGMP_HOST_MEMBERSHIP_REPORT:
1960 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1961 if (mcast_snooping_add_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
1962 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
1963 IP_FMT" is on port %s in VLAN %d",
1964 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
1967 case IGMP_HOST_LEAVE_MESSAGE:
1968 if (mcast_snooping_leave_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
1969 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
1970 IP_FMT" is on port %s in VLAN %d",
1971 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
1974 case IGMP_HOST_MEMBERSHIP_QUERY:
1975 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
1976 in_xbundle->ofbundle)) {
1977 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
1978 IP_FMT" is on port %s in VLAN %d",
1979 xbridge->name, IP_ARGS(flow->nw_src),
1980 in_xbundle->name, vlan);
1986 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1987 * was received on 'in_xbundle' in 'vlan'. */
1989 update_mcast_snooping_table(const struct xbridge *xbridge,
1990 const struct flow *flow, int vlan,
1991 struct xbundle *in_xbundle)
1993 struct mcast_snooping *ms = xbridge->ms;
1994 struct xlate_cfg *xcfg;
1995 struct xbundle *mcast_xbundle;
1996 struct mcast_port_bundle *fport;
1998 /* Don't learn the OFPP_NONE port. */
1999 if (in_xbundle == &ofpp_none_bundle) {
2003 /* Don't learn from flood ports */
2004 mcast_xbundle = NULL;
2005 ovs_rwlock_wrlock(&ms->rwlock);
2006 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2007 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2008 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2009 if (mcast_xbundle == in_xbundle) {
2014 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
2015 update_mcast_snooping_table__(xbridge, flow, ms, flow->igmp_group_ip4,
2018 ovs_rwlock_unlock(&ms->rwlock);
2021 /* send the packet to ports having the multicast group learned */
2023 xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2024 struct mcast_snooping *ms OVS_UNUSED,
2025 struct mcast_group *grp,
2026 struct xbundle *in_xbundle, uint16_t vlan)
2027 OVS_REQ_RDLOCK(ms->rwlock)
2029 struct xlate_cfg *xcfg;
2030 struct mcast_group_bundle *b;
2031 struct xbundle *mcast_xbundle;
2033 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2034 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2035 mcast_xbundle = xbundle_lookup(xcfg, b->port);
2036 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2037 xlate_report(ctx, "forwarding to mcast group port");
2038 output_normal(ctx, mcast_xbundle, vlan);
2039 } else if (!mcast_xbundle) {
2040 xlate_report(ctx, "mcast group port is unknown, dropping");
2042 xlate_report(ctx, "mcast group port is input port, dropping");
2047 /* send the packet to ports connected to multicast routers */
2049 xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2050 struct mcast_snooping *ms,
2051 struct xbundle *in_xbundle, uint16_t vlan)
2052 OVS_REQ_RDLOCK(ms->rwlock)
2054 struct xlate_cfg *xcfg;
2055 struct mcast_mrouter_bundle *mrouter;
2056 struct xbundle *mcast_xbundle;
2058 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2059 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2060 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2061 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2062 xlate_report(ctx, "forwarding to mcast router port");
2063 output_normal(ctx, mcast_xbundle, vlan);
2064 } else if (!mcast_xbundle) {
2065 xlate_report(ctx, "mcast router port is unknown, dropping");
2067 xlate_report(ctx, "mcast router port is input port, dropping");
2072 /* send the packet to ports flagged to be flooded */
2074 xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2075 struct mcast_snooping *ms,
2076 struct xbundle *in_xbundle, uint16_t vlan)
2077 OVS_REQ_RDLOCK(ms->rwlock)
2079 struct xlate_cfg *xcfg;
2080 struct mcast_port_bundle *fport;
2081 struct xbundle *mcast_xbundle;
2083 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2084 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2085 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2086 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2087 xlate_report(ctx, "forwarding to mcast flood port");
2088 output_normal(ctx, mcast_xbundle, vlan);
2089 } else if (!mcast_xbundle) {
2090 xlate_report(ctx, "mcast flood port is unknown, dropping");
2092 xlate_report(ctx, "mcast flood port is input port, dropping");
2097 /* forward the Reports to configured ports */
2099 xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
2100 struct mcast_snooping *ms,
2101 struct xbundle *in_xbundle, uint16_t vlan)
2102 OVS_REQ_RDLOCK(ms->rwlock)
2104 struct xlate_cfg *xcfg;
2105 struct mcast_port_bundle *rport;
2106 struct xbundle *mcast_xbundle;
2108 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2109 LIST_FOR_EACH(rport, node, &ms->rport_list) {
2110 mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2111 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2112 xlate_report(ctx, "forwarding Report to mcast flagged port");
2113 output_normal(ctx, mcast_xbundle, vlan);
2114 } else if (!mcast_xbundle) {
2115 xlate_report(ctx, "mcast port is unknown, dropping the Report");
2117 xlate_report(ctx, "mcast port is input port, dropping the Report");
2123 xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2126 struct xbundle *xbundle;
2128 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2129 if (xbundle != in_xbundle
2130 && xbundle_includes_vlan(xbundle, vlan)
2131 && xbundle->floodable
2132 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2133 output_normal(ctx, xbundle, vlan);
2136 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2140 xlate_normal(struct xlate_ctx *ctx)
2142 struct flow_wildcards *wc = &ctx->xout->wc;
2143 struct flow *flow = &ctx->xin->flow;
2144 struct xbundle *in_xbundle;
2145 struct xport *in_port;
2146 struct mac_entry *mac;
2151 ctx->xout->has_normal = true;
2153 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2154 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2155 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2157 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
2158 ctx->xin->packet != NULL, &in_port);
2160 xlate_report(ctx, "no input bundle, dropping");
2164 /* Drop malformed frames. */
2165 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
2166 !(flow->vlan_tci & htons(VLAN_CFI))) {
2167 if (ctx->xin->packet != NULL) {
2168 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2169 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
2170 "VLAN tag received on port %s",
2171 ctx->xbridge->name, in_xbundle->name);
2173 xlate_report(ctx, "partial VLAN tag, dropping");
2177 /* Drop frames on bundles reserved for mirroring. */
2178 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
2179 if (ctx->xin->packet != NULL) {
2180 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2181 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2182 "%s, which is reserved exclusively for mirroring",
2183 ctx->xbridge->name, in_xbundle->name);
2185 xlate_report(ctx, "input port is mirror output port, dropping");
2190 vid = vlan_tci_to_vid(flow->vlan_tci);
2191 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
2192 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2195 vlan = input_vid_to_vlan(in_xbundle, vid);
2197 /* Check other admissibility requirements. */
2198 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2202 /* Learn source MAC. */
2203 if (ctx->xin->may_learn) {
2204 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
2206 if (ctx->xin->xcache) {
2207 struct xc_entry *entry;
2209 /* Save enough info to update mac learning table later. */
2210 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
2211 entry->u.normal.ofproto = ctx->xbridge->ofproto;
2212 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2213 entry->u.normal.vlan = vlan;
2216 /* Determine output bundle. */
2217 if (mcast_snooping_enabled(ctx->xbridge->ms)
2218 && !eth_addr_is_broadcast(flow->dl_dst)
2219 && eth_addr_is_multicast(flow->dl_dst)
2220 && flow->dl_type == htons(ETH_TYPE_IP)) {
2221 struct mcast_snooping *ms = ctx->xbridge->ms;
2222 struct mcast_group *grp;
2224 if (flow->nw_proto == IPPROTO_IGMP) {
2225 if (ctx->xin->may_learn) {
2226 if (mcast_snooping_is_membership(flow->tp_src) ||
2227 mcast_snooping_is_query(flow->tp_src)) {
2228 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2233 if (mcast_snooping_is_membership(flow->tp_src)) {
2234 ovs_rwlock_rdlock(&ms->rwlock);
2235 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2236 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2237 * forward IGMP Membership Reports only to those ports where
2238 * multicast routers are attached. Alternatively stated: a
2239 * snooping switch should not forward IGMP Membership Reports
2240 * to ports on which only hosts are attached.
2241 * An administrative control may be provided to override this
2242 * restriction, allowing the report messages to be flooded to
2244 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2245 ovs_rwlock_unlock(&ms->rwlock);
2247 xlate_report(ctx, "multicast traffic, flooding");
2248 xlate_normal_flood(ctx, in_xbundle, vlan);
2252 if (ip_is_local_multicast(flow->nw_dst)) {
2253 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2254 * address in the 224.0.0.x range which are not IGMP must
2255 * be forwarded on all ports */
2256 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2257 xlate_normal_flood(ctx, in_xbundle, vlan);
2262 /* forwarding to group base ports */
2263 ovs_rwlock_rdlock(&ms->rwlock);
2264 grp = mcast_snooping_lookup(ms, flow->nw_dst, vlan);
2266 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2267 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2268 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2270 if (mcast_snooping_flood_unreg(ms)) {
2271 xlate_report(ctx, "unregistered multicast, flooding");
2272 xlate_normal_flood(ctx, in_xbundle, vlan);
2274 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2275 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2278 ovs_rwlock_unlock(&ms->rwlock);
2280 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2281 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
2282 mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
2283 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2286 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2287 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2288 if (mac_xbundle && mac_xbundle != in_xbundle) {
2289 xlate_report(ctx, "forwarding to learned port");
2290 output_normal(ctx, mac_xbundle, vlan);
2291 } else if (!mac_xbundle) {
2292 xlate_report(ctx, "learned port is unknown, dropping");
2294 xlate_report(ctx, "learned port is input port, dropping");
2297 xlate_report(ctx, "no learned MAC for destination, flooding");
2298 xlate_normal_flood(ctx, in_xbundle, vlan);
2303 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
2304 * the number of packets out of UINT32_MAX to sample. The given
2305 * cookie is passed back in the callback for each sampled packet.
2308 compose_sample_action(const struct xbridge *xbridge,
2309 struct ofpbuf *odp_actions,
2310 const struct flow *flow,
2311 const uint32_t probability,
2312 const union user_action_cookie *cookie,
2313 const size_t cookie_size,
2314 const odp_port_t tunnel_out_port)
2316 size_t sample_offset, actions_offset;
2317 odp_port_t odp_port;
2321 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
2323 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
2325 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
2327 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
2328 pid = dpif_port_get_pid(xbridge->dpif, odp_port,
2329 flow_hash_5tuple(flow, 0));
2330 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2331 tunnel_out_port, odp_actions);
2333 nl_msg_end_nested(odp_actions, actions_offset);
2334 nl_msg_end_nested(odp_actions, sample_offset);
2335 return cookie_offset;
2339 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
2340 odp_port_t odp_port, unsigned int n_outputs,
2341 union user_action_cookie *cookie)
2345 cookie->type = USER_ACTION_COOKIE_SFLOW;
2346 cookie->sflow.vlan_tci = vlan_tci;
2348 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2349 * port information") for the interpretation of cookie->output. */
2350 switch (n_outputs) {
2352 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2353 cookie->sflow.output = 0x40000000 | 256;
2357 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
2359 cookie->sflow.output = ifindex;
2364 /* 0x80000000 means "multiple output ports. */
2365 cookie->sflow.output = 0x80000000 | n_outputs;
2370 /* Compose SAMPLE action for sFlow bridge sampling. */
2372 compose_sflow_action(const struct xbridge *xbridge,
2373 struct ofpbuf *odp_actions,
2374 const struct flow *flow,
2375 odp_port_t odp_port)
2377 uint32_t probability;
2378 union user_action_cookie cookie;
2380 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
2384 probability = dpif_sflow_get_probability(xbridge->sflow);
2385 compose_sflow_cookie(xbridge, htons(0), odp_port,
2386 odp_port == ODPP_NONE ? 0 : 1, &cookie);
2388 return compose_sample_action(xbridge, odp_actions, flow, probability,
2389 &cookie, sizeof cookie.sflow, ODPP_NONE);
2393 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
2394 uint32_t obs_domain_id, uint32_t obs_point_id,
2395 union user_action_cookie *cookie)
2397 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
2398 cookie->flow_sample.probability = probability;
2399 cookie->flow_sample.collector_set_id = collector_set_id;
2400 cookie->flow_sample.obs_domain_id = obs_domain_id;
2401 cookie->flow_sample.obs_point_id = obs_point_id;
2405 compose_ipfix_cookie(union user_action_cookie *cookie,
2406 odp_port_t output_odp_port)
2408 cookie->type = USER_ACTION_COOKIE_IPFIX;
2409 cookie->ipfix.output_odp_port = output_odp_port;
2412 /* Compose SAMPLE action for IPFIX bridge sampling. */
2414 compose_ipfix_action(const struct xbridge *xbridge,
2415 struct ofpbuf *odp_actions,
2416 const struct flow *flow,
2417 odp_port_t output_odp_port)
2419 uint32_t probability;
2420 union user_action_cookie cookie;
2421 odp_port_t tunnel_out_port = ODPP_NONE;
2423 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
2427 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2429 if (output_odp_port == ODPP_NONE &&
2430 !dpif_ipfix_get_bridge_exporter_input_sampling(xbridge->ipfix)) {
2434 /* For output case, output_odp_port is valid*/
2435 if (output_odp_port != ODPP_NONE) {
2436 if (!dpif_ipfix_get_bridge_exporter_output_sampling(xbridge->ipfix)) {
2439 /* If tunnel sampling is enabled, put an additional option attribute:
2440 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2442 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(xbridge->ipfix) &&
2443 dpif_ipfix_get_tunnel_port(xbridge->ipfix, output_odp_port) ) {
2444 tunnel_out_port = output_odp_port;
2448 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
2449 compose_ipfix_cookie(&cookie, output_odp_port);
2451 compose_sample_action(xbridge, odp_actions, flow, probability,
2452 &cookie, sizeof cookie.ipfix, tunnel_out_port);
2455 /* SAMPLE action for sFlow must be first action in any given list of
2456 * actions. At this point we do not have all information required to
2457 * build it. So try to build sample action as complete as possible. */
2459 add_sflow_action(struct xlate_ctx *ctx)
2461 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
2462 ctx->xout->odp_actions,
2463 &ctx->xin->flow, ODPP_NONE);
2464 ctx->sflow_odp_port = 0;
2465 ctx->sflow_n_outputs = 0;
2468 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
2469 * of actions, eventually after the SAMPLE action for sFlow. */
2471 add_ipfix_action(struct xlate_ctx *ctx)
2473 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2474 &ctx->xin->flow, ODPP_NONE);
2478 add_ipfix_output_action(struct xlate_ctx *ctx, odp_port_t port)
2480 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2481 &ctx->xin->flow, port);
2484 /* Fix SAMPLE action according to data collected while composing ODP actions.
2485 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
2486 * USERSPACE action's user-cookie which is required for sflow. */
2488 fix_sflow_action(struct xlate_ctx *ctx)
2490 const struct flow *base = &ctx->base_flow;
2491 union user_action_cookie *cookie;
2493 if (!ctx->user_cookie_offset) {
2497 cookie = ofpbuf_at(ctx->xout->odp_actions, ctx->user_cookie_offset,
2498 sizeof cookie->sflow);
2499 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2501 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
2502 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
2505 static enum slow_path_reason
2506 process_special(struct xlate_ctx *ctx, const struct flow *flow,
2507 const struct xport *xport, const struct dp_packet *packet)
2509 struct flow_wildcards *wc = &ctx->xout->wc;
2510 const struct xbridge *xbridge = ctx->xbridge;
2514 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
2516 cfm_process_heartbeat(xport->cfm, packet);
2519 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
2521 bfd_process_packet(xport->bfd, flow, packet);
2522 /* If POLL received, immediately sends FINAL back. */
2523 if (bfd_should_send_packet(xport->bfd)) {
2524 ofproto_dpif_monitor_port_send_soon(xport->ofport);
2528 } else if (xport->xbundle && xport->xbundle->lacp
2529 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2531 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
2534 } else if ((xbridge->stp || xbridge->rstp) &&
2535 stp_should_process_flow(flow, wc)) {
2538 ? stp_process_packet(xport, packet)
2539 : rstp_process_packet(xport, packet);
2542 } else if (xport->lldp && lldp_should_process_flow(flow)) {
2544 lldp_process_packet(xport->lldp, packet);
2553 tnl_route_lookup_flow(const struct flow *oflow,
2554 ovs_be32 *ip, struct xport **out_port)
2556 char out_dev[IFNAMSIZ];
2557 struct xbridge *xbridge;
2558 struct xlate_cfg *xcfg;
2561 if (!ovs_router_lookup(oflow->tunnel.ip_dst, out_dev, &gw)) {
2568 *ip = oflow->tunnel.ip_dst;
2571 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2574 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
2575 if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
2578 HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
2579 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
2590 xlate_flood_packet(struct xbridge *xbridge, struct dp_packet *packet)
2592 struct ofpact_output output;
2595 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
2596 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
2597 flow_extract(packet, &flow);
2598 flow.in_port.ofp_port = OFPP_NONE;
2599 output.port = OFPP_FLOOD;
2602 return ofproto_dpif_execute_actions(xbridge->ofproto, &flow, NULL,
2603 &output.ofpact, sizeof output,
2608 tnl_send_arp_request(const struct xport *out_dev, const uint8_t eth_src[ETH_ADDR_LEN],
2609 ovs_be32 ip_src, ovs_be32 ip_dst)
2611 struct xbridge *xbridge = out_dev->xbridge;
2612 struct dp_packet packet;
2614 dp_packet_init(&packet, 0);
2615 compose_arp(&packet, eth_src, ip_src, ip_dst);
2617 xlate_flood_packet(xbridge, &packet);
2618 dp_packet_uninit(&packet);
2622 build_tunnel_send(const struct xlate_ctx *ctx, const struct xport *xport,
2623 const struct flow *flow, odp_port_t tunnel_odp_port)
2625 struct ovs_action_push_tnl tnl_push_data;
2626 struct xport *out_dev = NULL;
2627 ovs_be32 s_ip, d_ip = 0;
2628 uint8_t smac[ETH_ADDR_LEN];
2629 uint8_t dmac[ETH_ADDR_LEN];
2632 err = tnl_route_lookup_flow(flow, &d_ip, &out_dev);
2637 /* Use mac addr of bridge port of the peer. */
2638 err = netdev_get_etheraddr(out_dev->netdev, smac);
2643 err = netdev_get_in4(out_dev->netdev, (struct in_addr *) &s_ip, NULL);
2648 err = tnl_arp_lookup(out_dev->xbridge->name, d_ip, dmac);
2650 tnl_send_arp_request(out_dev, smac, s_ip, d_ip);
2653 if (ctx->xin->xcache) {
2654 struct xc_entry *entry;
2656 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_ARP);
2657 ovs_strlcpy(entry->u.tnl_arp_cache.br_name, out_dev->xbridge->name,
2658 sizeof entry->u.tnl_arp_cache.br_name);
2659 entry->u.tnl_arp_cache.d_ip = d_ip;
2661 err = tnl_port_build_header(xport->ofport, flow,
2662 dmac, smac, s_ip, &tnl_push_data);
2666 tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
2667 tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
2668 odp_put_tnl_push_action(ctx->xout->odp_actions, &tnl_push_data);
2673 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
2676 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
2677 struct flow_wildcards *wc = &ctx->xout->wc;
2678 struct flow *flow = &ctx->xin->flow;
2679 struct flow_tnl flow_tnl;
2680 ovs_be16 flow_vlan_tci;
2681 uint32_t flow_pkt_mark;
2682 uint8_t flow_nw_tos;
2683 odp_port_t out_port, odp_port;
2684 bool tnl_push_pop_send = false;
2687 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2688 * before traversing a patch port. */
2689 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 31);
2690 memset(&flow_tnl, 0, sizeof flow_tnl);
2693 xlate_report(ctx, "Nonexistent output port");
2695 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
2696 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2698 } else if (check_stp) {
2699 if (is_stp(&ctx->base_flow)) {
2700 if (!xport_stp_should_forward_bpdu(xport) &&
2701 !xport_rstp_should_manage_bpdu(xport)) {
2702 if (ctx->xbridge->stp != NULL) {
2703 xlate_report(ctx, "STP not in listening state, "
2704 "skipping bpdu output");
2705 } else if (ctx->xbridge->rstp != NULL) {
2706 xlate_report(ctx, "RSTP not managing BPDU in this state, "
2707 "skipping bpdu output");
2711 } else if (!xport_stp_forward_state(xport) ||
2712 !xport_rstp_forward_state(xport)) {
2713 if (ctx->xbridge->stp != NULL) {
2714 xlate_report(ctx, "STP not in forwarding state, "
2716 } else if (ctx->xbridge->rstp != NULL) {
2717 xlate_report(ctx, "RSTP not in forwarding state, "
2724 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
2725 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
2730 const struct xport *peer = xport->peer;
2731 struct flow old_flow = ctx->xin->flow;
2732 enum slow_path_reason special;
2733 uint8_t table_id = rule_dpif_lookup_get_init_table_id(&ctx->xin->flow);
2735 ctx->xbridge = peer->xbridge;
2736 flow->in_port.ofp_port = peer->ofp_port;
2737 flow->metadata = htonll(0);
2738 memset(&flow->tunnel, 0, sizeof flow->tunnel);
2739 memset(flow->regs, 0, sizeof flow->regs);
2740 flow->actset_output = OFPP_UNSET;
2742 special = process_special(ctx, &ctx->xin->flow, peer,
2745 ctx->xout->slow |= special;
2746 } else if (may_receive(peer, ctx)) {
2747 if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
2748 xlate_table_action(ctx, flow->in_port.ofp_port, table_id,
2751 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
2752 * the learning action look at the packet, then drop it. */
2753 struct flow old_base_flow = ctx->base_flow;
2754 size_t old_size = ctx->xout->odp_actions->size;
2755 mirror_mask_t old_mirrors = ctx->xout->mirrors;
2756 xlate_table_action(ctx, flow->in_port.ofp_port, table_id,
2758 ctx->xout->mirrors = old_mirrors;
2759 ctx->base_flow = old_base_flow;
2760 ctx->xout->odp_actions->size = old_size;
2764 ctx->xin->flow = old_flow;
2765 ctx->xbridge = xport->xbridge;
2767 if (ctx->xin->resubmit_stats) {
2768 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2769 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
2771 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
2774 if (ctx->xin->xcache) {
2775 struct xc_entry *entry;
2777 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2778 entry->u.dev.tx = netdev_ref(xport->netdev);
2779 entry->u.dev.rx = netdev_ref(peer->netdev);
2780 entry->u.dev.bfd = bfd_ref(peer->bfd);
2785 flow_vlan_tci = flow->vlan_tci;
2786 flow_pkt_mark = flow->pkt_mark;
2787 flow_nw_tos = flow->nw_tos;
2789 if (count_skb_priorities(xport)) {
2790 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
2791 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
2792 wc->masks.nw_tos |= IP_DSCP_MASK;
2793 flow->nw_tos &= ~IP_DSCP_MASK;
2794 flow->nw_tos |= dscp;
2798 if (xport->is_tunnel) {
2799 /* Save tunnel metadata so that changes made due to
2800 * the Logical (tunnel) Port are not visible for any further
2801 * matches, while explicit set actions on tunnel metadata are.
2803 flow_tnl = flow->tunnel;
2804 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
2805 if (odp_port == ODPP_NONE) {
2806 xlate_report(ctx, "Tunneling decided against output");
2807 goto out; /* restore flow_nw_tos */
2809 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
2810 xlate_report(ctx, "Not tunneling to our own address");
2811 goto out; /* restore flow_nw_tos */
2813 if (ctx->xin->resubmit_stats) {
2814 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2816 if (ctx->xin->xcache) {
2817 struct xc_entry *entry;
2819 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2820 entry->u.dev.tx = netdev_ref(xport->netdev);
2822 out_port = odp_port;
2823 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
2824 tnl_push_pop_send = true;
2826 commit_odp_tunnel_action(flow, &ctx->base_flow,
2827 ctx->xout->odp_actions);
2828 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2831 odp_port = xport->odp_port;
2832 out_port = odp_port;
2833 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
2834 ofp_port_t vlandev_port;
2836 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2837 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
2838 ofp_port, flow->vlan_tci);
2839 if (vlandev_port != ofp_port) {
2840 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
2841 flow->vlan_tci = htons(0);
2846 if (out_port != ODPP_NONE) {
2847 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
2848 ctx->xout->odp_actions,
2850 ctx->xbridge->masked_set_action);
2852 if (ctx->use_recirc) {
2853 struct ovs_action_hash *act_hash;
2854 struct xlate_recirc *xr = &ctx->recirc;
2857 act_hash = nl_msg_put_unspec_uninit(ctx->xout->odp_actions,
2858 OVS_ACTION_ATTR_HASH,
2860 act_hash->hash_alg = xr->hash_alg;
2861 act_hash->hash_basis = xr->hash_basis;
2863 /* Recirc action. */
2864 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC,
2868 if (tnl_push_pop_send) {
2869 build_tunnel_send(ctx, xport, flow, odp_port);
2870 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2872 odp_port_t odp_tnl_port = ODPP_NONE;
2874 /* XXX: Write better Filter for tunnel port. We can use inport
2875 * int tunnel-port flow to avoid these checks completely. */
2876 if (ofp_port == OFPP_LOCAL &&
2877 ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
2879 odp_tnl_port = tnl_port_map_lookup(flow, wc);
2882 if (odp_tnl_port != ODPP_NONE) {
2883 nl_msg_put_odp_port(ctx->xout->odp_actions,
2884 OVS_ACTION_ATTR_TUNNEL_POP,
2887 /* Tunnel push-pop action is not compatible with
2889 add_ipfix_output_action(ctx, out_port);
2890 nl_msg_put_odp_port(ctx->xout->odp_actions,
2891 OVS_ACTION_ATTR_OUTPUT,
2897 ctx->sflow_odp_port = odp_port;
2898 ctx->sflow_n_outputs++;
2899 ctx->xout->nf_output_iface = ofp_port;
2904 flow->vlan_tci = flow_vlan_tci;
2905 flow->pkt_mark = flow_pkt_mark;
2906 flow->nw_tos = flow_nw_tos;
2910 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
2912 compose_output_action__(ctx, ofp_port, true);
2916 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
2918 struct rule_dpif *old_rule = ctx->rule;
2919 ovs_be64 old_cookie = ctx->rule_cookie;
2920 const struct rule_actions *actions;
2922 if (ctx->xin->resubmit_stats) {
2923 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
2929 ctx->rule_cookie = rule_dpif_get_flow_cookie(rule);
2930 actions = rule_dpif_get_actions(rule);
2931 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
2932 ctx->rule_cookie = old_cookie;
2933 ctx->rule = old_rule;
2938 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
2940 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2942 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
2943 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
2944 MAX_RESUBMIT_RECURSION);
2945 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
2946 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
2947 } else if (ctx->xout->odp_actions->size > UINT16_MAX) {
2948 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
2949 } else if (ctx->stack.size >= 65536) {
2950 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
2959 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
2960 bool may_packet_in, bool honor_table_miss)
2962 if (xlate_resubmit_resource_check(ctx)) {
2963 struct flow_wildcards *wc;
2964 uint8_t old_table_id = ctx->table_id;
2965 struct rule_dpif *rule;
2967 ctx->table_id = table_id;
2968 wc = (ctx->xin->skip_wildcards) ? NULL : &ctx->xout->wc;
2970 rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
2971 &ctx->xin->flow, wc,
2972 ctx->xin->xcache != NULL,
2973 ctx->xin->resubmit_stats,
2974 &ctx->table_id, in_port,
2975 may_packet_in, honor_table_miss);
2977 if (OVS_UNLIKELY(ctx->xin->resubmit_hook)) {
2978 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse + 1);
2982 /* Fill in the cache entry here instead of xlate_recursively
2983 * to make the reference counting more explicit. We take a
2984 * reference in the lookups above if we are going to cache the
2986 if (ctx->xin->xcache) {
2987 struct xc_entry *entry;
2989 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
2990 entry->u.rule = rule;
2992 xlate_recursively(ctx, rule);
2995 ctx->table_id = old_table_id;
3003 xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
3004 struct ofputil_bucket *bucket)
3006 if (ctx->xin->resubmit_stats) {
3007 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
3009 if (ctx->xin->xcache) {
3010 struct xc_entry *entry;
3012 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
3013 entry->u.group.group = group_dpif_ref(group);
3014 entry->u.group.bucket = bucket;
3019 xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
3021 uint64_t action_list_stub[1024 / 8];
3022 struct ofpbuf action_list, action_set;
3024 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
3025 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3027 ofpacts_execute_action_set(&action_list, &action_set);
3029 do_xlate_actions(action_list.data, action_list.size, ctx);
3032 ofpbuf_uninit(&action_set);
3033 ofpbuf_uninit(&action_list);
3037 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3039 struct ofputil_bucket *bucket;
3040 const struct ovs_list *buckets;
3041 struct flow old_flow = ctx->xin->flow;
3043 group_dpif_get_buckets(group, &buckets);
3045 LIST_FOR_EACH (bucket, list_node, buckets) {
3046 xlate_group_bucket(ctx, bucket);
3047 /* Roll back flow to previous state.
3048 * This is equivalent to cloning the packet for each bucket.
3050 * As a side effect any subsequently applied actions will
3051 * also effectively be applied to a clone of the packet taken
3052 * just before applying the all or indirect group. */
3053 ctx->xin->flow = old_flow;
3055 xlate_group_stats(ctx, group, NULL);
3059 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3061 struct ofputil_bucket *bucket;
3063 bucket = group_first_live_bucket(ctx, group, 0);
3065 xlate_group_bucket(ctx, bucket);
3066 xlate_group_stats(ctx, group, bucket);
3071 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3073 struct flow_wildcards *wc = &ctx->xout->wc;
3074 struct ofputil_bucket *bucket;
3077 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
3078 bucket = group_best_live_bucket(ctx, group, basis);
3080 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3081 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3082 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3083 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3084 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3085 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3086 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3087 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3088 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3090 xlate_group_bucket(ctx, bucket);
3091 xlate_group_stats(ctx, group, bucket);
3096 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3098 ctx->in_group = true;
3100 switch (group_dpif_get_type(group)) {
3102 case OFPGT11_INDIRECT:
3103 xlate_all_group(ctx, group);
3105 case OFPGT11_SELECT:
3106 xlate_select_group(ctx, group);
3109 xlate_ff_group(ctx, group);
3114 group_dpif_unref(group);
3116 ctx->in_group = false;
3120 xlate_group_resource_check(struct xlate_ctx *ctx)
3122 if (!xlate_resubmit_resource_check(ctx)) {
3124 } else if (ctx->in_group) {
3125 /* Prevent nested translation of OpenFlow groups.
3127 * OpenFlow allows this restriction. We enforce this restriction only
3128 * because, with the current architecture, we would otherwise have to
3129 * take a possibly recursive read lock on the ofgroup rwlock, which is
3130 * unsafe given that POSIX allows taking a read lock to block if there
3131 * is a thread blocked on taking the write lock. Other solutions
3132 * without this restriction are also possible, but seem unwarranted
3133 * given the current limited use of groups. */
3134 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3136 VLOG_ERR_RL(&rl, "cannot recursively translate OpenFlow group");
3144 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3146 if (xlate_group_resource_check(ctx)) {
3147 struct group_dpif *group;
3150 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
3152 xlate_group_action__(ctx, group);
3162 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
3163 const struct ofpact_resubmit *resubmit)
3167 bool may_packet_in = false;
3168 bool honor_table_miss = false;
3170 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
3171 /* Still allow missed packets to be sent to the controller
3172 * if resubmitting from an internal table. */
3173 may_packet_in = true;
3174 honor_table_miss = true;
3177 in_port = resubmit->in_port;
3178 if (in_port == OFPP_IN_PORT) {
3179 in_port = ctx->xin->flow.in_port.ofp_port;
3182 table_id = resubmit->table_id;
3183 if (table_id == 255) {
3184 table_id = ctx->table_id;
3187 xlate_table_action(ctx, in_port, table_id, may_packet_in,
3192 flood_packets(struct xlate_ctx *ctx, bool all)
3194 const struct xport *xport;
3196 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
3197 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
3202 compose_output_action__(ctx, xport->ofp_port, false);
3203 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
3204 compose_output_action(ctx, xport->ofp_port);
3208 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3212 execute_controller_action(struct xlate_ctx *ctx, int len,
3213 enum ofp_packet_in_reason reason,
3214 uint16_t controller_id)
3216 struct ofproto_packet_in *pin;
3217 struct dp_packet *packet;
3219 ctx->xout->slow |= SLOW_CONTROLLER;
3220 if (!ctx->xin->packet) {
3224 packet = dp_packet_clone(ctx->xin->packet);
3226 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3227 ctx->xout->odp_actions,
3229 ctx->xbridge->masked_set_action);
3231 odp_execute_actions(NULL, &packet, 1, false,
3232 ctx->xout->odp_actions->data,
3233 ctx->xout->odp_actions->size, NULL);
3235 pin = xmalloc(sizeof *pin);
3236 pin->up.packet_len = dp_packet_size(packet);
3237 pin->up.packet = dp_packet_steal_data(packet);
3238 pin->up.reason = reason;
3239 pin->up.table_id = ctx->table_id;
3240 pin->up.cookie = ctx->rule_cookie;
3242 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
3244 pin->controller_id = controller_id;
3245 pin->send_len = len;
3246 /* If a rule is a table-miss rule then this is
3247 * a table-miss handled by a table-miss rule.
3249 * Else, if rule is internal and has a controller action,
3250 * the later being implied by the rule being processed here,
3251 * then this is a table-miss handled without a table-miss rule.
3253 * Otherwise this is not a table-miss. */
3254 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
3256 if (rule_dpif_is_table_miss(ctx->rule)) {
3257 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
3258 } else if (rule_dpif_is_internal(ctx->rule)) {
3259 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
3262 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
3263 dp_packet_delete(packet);
3267 compose_recirculate_action(struct xlate_ctx *ctx,
3268 const struct ofpact *ofpacts_base,
3269 const struct ofpact *ofpact_current,
3270 size_t ofpacts_base_len)
3274 unsigned ofpacts_len;
3277 struct ofpbuf ofpacts;
3281 ofpacts_len = ofpacts_base_len -
3282 ((uint8_t *)ofpact_current - (uint8_t *)ofpacts_base);
3285 id = rule_dpif_get_recirc_id(ctx->rule);
3287 /* In the case where ctx has no rule then allocate a recirc id.
3288 * The life-cycle of this recirc id is managed by associating it
3289 * with the internal rule that is created to to handle
3290 * recirculation below.
3292 * The known use-case of this is packet_out which
3293 * translates actions without a rule */
3294 id = ofproto_dpif_alloc_recirc_id(ctx->xbridge->ofproto);
3297 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3298 VLOG_ERR_RL(&rl, "Failed to allocate recirculation id");
3302 match_init_catchall(&match);
3303 match_set_recirc_id(&match, id);
3304 ofpbuf_use_const(&ofpacts, ofpact_current, ofpacts_len);
3305 error = ofproto_dpif_add_internal_flow(ctx->xbridge->ofproto, &match,
3306 RECIRC_RULE_PRIORITY,
3307 RECIRC_TIMEOUT, &ofpacts, &rule);
3309 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3310 VLOG_ERR_RL(&rl, "Failed to add post recirculation flow %s",
3311 match_to_string(&match, 0));
3313 ofproto_dpif_free_recirc_id(ctx->xbridge->ofproto, id);
3317 /* If ctx has no rule then associate the recirc id, which
3318 * was allocated above, with the internal rule. This allows
3319 * the recirc id to be released when the internal rule times out. */
3321 rule_set_recirc_id(rule, id);
3324 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3325 ctx->xout->odp_actions,
3327 ctx->xbridge->masked_set_action);
3328 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
3332 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
3334 struct flow_wildcards *wc = &ctx->xout->wc;
3335 struct flow *flow = &ctx->xin->flow;
3338 ovs_assert(eth_type_mpls(mpls->ethertype));
3340 n = flow_count_mpls_labels(flow, wc);
3342 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
3343 ctx->xout->odp_actions,
3345 ctx->xbridge->masked_set_action);
3346 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3347 if (ctx->xin->packet != NULL) {
3348 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3349 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3350 "MPLS push action can't be performed as it would "
3351 "have more MPLS LSEs than the %d supported.",
3352 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3358 flow_push_mpls(flow, n, mpls->ethertype, wc);
3362 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
3364 struct flow_wildcards *wc = &ctx->xout->wc;
3365 struct flow *flow = &ctx->xin->flow;
3366 int n = flow_count_mpls_labels(flow, wc);
3368 if (flow_pop_mpls(flow, n, eth_type, wc)) {
3369 if (ctx->xbridge->enable_recirc) {
3370 ctx->was_mpls = true;
3372 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3373 if (ctx->xin->packet != NULL) {
3374 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3375 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3376 "MPLS pop action can't be performed as it has "
3377 "more MPLS LSEs than the %d supported.",
3378 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3381 ofpbuf_clear(ctx->xout->odp_actions);
3386 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3388 struct flow *flow = &ctx->xin->flow;
3390 if (!is_ip_any(flow)) {
3394 ctx->xout->wc.masks.nw_ttl = 0xff;
3395 if (flow->nw_ttl > 1) {
3401 for (i = 0; i < ids->n_controllers; i++) {
3402 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3406 /* Stop processing for current table. */
3412 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3414 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3415 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
3416 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
3421 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3423 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3424 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
3425 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
3430 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
3432 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3433 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3434 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
3439 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
3441 struct flow *flow = &ctx->xin->flow;
3442 struct flow_wildcards *wc = &ctx->xout->wc;
3444 if (eth_type_mpls(flow->dl_type)) {
3445 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
3447 wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3450 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3453 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
3457 /* Stop processing for current table. */
3462 xlate_output_action(struct xlate_ctx *ctx,
3463 ofp_port_t port, uint16_t max_len, bool may_packet_in)
3465 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
3467 ctx->xout->nf_output_iface = NF_OUT_DROP;
3471 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
3474 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3475 0, may_packet_in, true);
3481 flood_packets(ctx, false);
3484 flood_packets(ctx, true);
3486 case OFPP_CONTROLLER:
3487 execute_controller_action(ctx, max_len,
3488 (ctx->in_group ? OFPR_GROUP
3489 : ctx->in_action_set ? OFPR_ACTION_SET
3497 if (port != ctx->xin->flow.in_port.ofp_port) {
3498 compose_output_action(ctx, port);
3500 xlate_report(ctx, "skipping output to input port");
3505 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3506 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3507 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3508 ctx->xout->nf_output_iface = prev_nf_output_iface;
3509 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3510 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3511 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3516 xlate_output_reg_action(struct xlate_ctx *ctx,
3517 const struct ofpact_output_reg *or)
3519 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3520 if (port <= UINT16_MAX) {
3521 union mf_subvalue value;
3523 memset(&value, 0xff, sizeof value);
3524 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
3525 xlate_output_action(ctx, u16_to_ofp(port),
3526 or->max_len, false);
3531 xlate_enqueue_action(struct xlate_ctx *ctx,
3532 const struct ofpact_enqueue *enqueue)
3534 ofp_port_t ofp_port = enqueue->port;
3535 uint32_t queue_id = enqueue->queue;
3536 uint32_t flow_priority, priority;
3539 /* Translate queue to priority. */
3540 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
3542 /* Fall back to ordinary output action. */
3543 xlate_output_action(ctx, enqueue->port, 0, false);
3547 /* Check output port. */
3548 if (ofp_port == OFPP_IN_PORT) {
3549 ofp_port = ctx->xin->flow.in_port.ofp_port;
3550 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
3554 /* Add datapath actions. */
3555 flow_priority = ctx->xin->flow.skb_priority;
3556 ctx->xin->flow.skb_priority = priority;
3557 compose_output_action(ctx, ofp_port);
3558 ctx->xin->flow.skb_priority = flow_priority;
3560 /* Update NetFlow output port. */
3561 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3562 ctx->xout->nf_output_iface = ofp_port;
3563 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3564 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3569 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3571 uint32_t skb_priority;
3573 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
3574 ctx->xin->flow.skb_priority = skb_priority;
3576 /* Couldn't translate queue to a priority. Nothing to do. A warning
3577 * has already been logged. */
3582 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
3584 const struct xbridge *xbridge = xbridge_;
3595 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3598 port = get_ofp_port(xbridge, ofp_port);
3599 return port ? port->may_enable : false;
3604 xlate_bundle_action(struct xlate_ctx *ctx,
3605 const struct ofpact_bundle *bundle)
3609 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
3611 CONST_CAST(struct xbridge *, ctx->xbridge));
3612 if (bundle->dst.field) {
3613 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
3616 xlate_output_action(ctx, port, 0, false);
3621 xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
3622 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
3624 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
3625 if (ctx->xin->may_learn) {
3626 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
3631 xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
3633 ctx->xout->has_learn = true;
3634 learn_mask(learn, &ctx->xout->wc);
3636 if (ctx->xin->xcache) {
3637 struct xc_entry *entry;
3639 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
3640 entry->u.learn.ofproto = ctx->xbridge->ofproto;
3641 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
3642 entry->u.learn.ofpacts = ofpbuf_new(64);
3643 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
3644 entry->u.learn.ofpacts);
3645 } else if (ctx->xin->may_learn) {
3646 uint64_t ofpacts_stub[1024 / 8];
3647 struct ofputil_flow_mod fm;
3648 struct ofpbuf ofpacts;
3650 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3651 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
3652 ofpbuf_uninit(&ofpacts);
3657 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
3658 uint16_t idle_timeout, uint16_t hard_timeout)
3660 if (tcp_flags & (TCP_FIN | TCP_RST)) {
3661 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
3666 xlate_fin_timeout(struct xlate_ctx *ctx,
3667 const struct ofpact_fin_timeout *oft)
3670 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
3671 oft->fin_idle_timeout, oft->fin_hard_timeout);
3672 if (ctx->xin->xcache) {
3673 struct xc_entry *entry;
3675 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
3676 /* XC_RULE already holds a reference on the rule, none is taken
3678 entry->u.fin.rule = ctx->rule;
3679 entry->u.fin.idle = oft->fin_idle_timeout;
3680 entry->u.fin.hard = oft->fin_hard_timeout;
3686 xlate_sample_action(struct xlate_ctx *ctx,
3687 const struct ofpact_sample *os)
3689 union user_action_cookie cookie;
3690 /* Scale the probability from 16-bit to 32-bit while representing
3691 * the same percentage. */
3692 uint32_t probability = (os->probability << 16) | os->probability;
3694 if (!ctx->xbridge->variable_length_userdata) {
3695 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3697 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
3698 "lacks support (needs Linux 3.10+ or kernel module from "
3703 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3704 ctx->xout->odp_actions,
3706 ctx->xbridge->masked_set_action);
3708 compose_flow_sample_cookie(os->probability, os->collector_set_id,
3709 os->obs_domain_id, os->obs_point_id, &cookie);
3710 compose_sample_action(ctx->xbridge, ctx->xout->odp_actions,
3711 &ctx->xin->flow, probability, &cookie,
3712 sizeof cookie.flow_sample, ODPP_NONE);
3716 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
3718 if (xport->config & (is_stp(&ctx->xin->flow)
3719 ? OFPUTIL_PC_NO_RECV_STP
3720 : OFPUTIL_PC_NO_RECV)) {
3724 /* Only drop packets here if both forwarding and learning are
3725 * disabled. If just learning is enabled, we need to have
3726 * OFPP_NORMAL and the learning action have a look at the packet
3727 * before we can drop it. */
3728 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
3729 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
3737 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
3739 const struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3740 size_t on_len = ofpact_nest_get_action_len(on);
3741 const struct ofpact *inner;
3743 /* Maintain actset_output depending on the contents of the action set:
3745 * - OFPP_UNSET, if there is no "output" action.
3747 * - The output port, if there is an "output" action and no "group"
3750 * - OFPP_UNSET, if there is a "group" action.
3752 if (!ctx->action_set_has_group) {
3753 OFPACT_FOR_EACH (inner, on->actions, on_len) {
3754 if (inner->type == OFPACT_OUTPUT) {
3755 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(inner)->port;
3756 } else if (inner->type == OFPACT_GROUP) {
3757 ctx->xin->flow.actset_output = OFPP_UNSET;
3758 ctx->action_set_has_group = true;
3763 ofpbuf_put(&ctx->action_set, on->actions, on_len);
3764 ofpact_pad(&ctx->action_set);
3768 xlate_action_set(struct xlate_ctx *ctx)
3770 uint64_t action_list_stub[1024 / 64];
3771 struct ofpbuf action_list;
3773 ctx->in_action_set = true;
3774 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3775 ofpacts_execute_action_set(&action_list, &ctx->action_set);
3776 do_xlate_actions(action_list.data, action_list.size, ctx);
3777 ctx->in_action_set = false;
3778 ofpbuf_uninit(&action_list);
3782 ofpact_needs_recirculation_after_mpls(const struct ofpact *a, struct xlate_ctx *ctx)
3784 struct flow_wildcards *wc = &ctx->xout->wc;
3785 struct flow *flow = &ctx->xin->flow;
3787 if (!ctx->was_mpls) {
3794 case OFPACT_CONTROLLER:
3795 case OFPACT_STRIP_VLAN:
3796 case OFPACT_SET_VLAN_PCP:
3797 case OFPACT_SET_VLAN_VID:
3798 case OFPACT_ENQUEUE:
3799 case OFPACT_PUSH_VLAN:
3800 case OFPACT_SET_ETH_SRC:
3801 case OFPACT_SET_ETH_DST:
3802 case OFPACT_SET_TUNNEL:
3803 case OFPACT_SET_QUEUE:
3804 case OFPACT_POP_QUEUE:
3805 case OFPACT_CONJUNCTION:
3807 case OFPACT_OUTPUT_REG:
3810 case OFPACT_WRITE_METADATA:
3811 case OFPACT_WRITE_ACTIONS:
3812 case OFPACT_CLEAR_ACTIONS:
3816 case OFPACT_POP_MPLS:
3817 case OFPACT_DEC_MPLS_TTL:
3818 case OFPACT_SET_MPLS_TTL:
3819 case OFPACT_SET_MPLS_TC:
3820 case OFPACT_SET_MPLS_LABEL:
3821 case OFPACT_SET_IPV4_SRC:
3822 case OFPACT_SET_IPV4_DST:
3823 case OFPACT_SET_IP_DSCP:
3824 case OFPACT_SET_IP_ECN:
3825 case OFPACT_SET_IP_TTL:
3826 case OFPACT_SET_L4_SRC_PORT:
3827 case OFPACT_SET_L4_DST_PORT:
3828 case OFPACT_RESUBMIT:
3829 case OFPACT_STACK_PUSH:
3830 case OFPACT_STACK_POP:
3831 case OFPACT_DEC_TTL:
3832 case OFPACT_MULTIPATH:
3835 case OFPACT_FIN_TIMEOUT:
3836 case OFPACT_GOTO_TABLE:
3839 case OFPACT_REG_MOVE:
3840 return (mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
3841 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
3843 case OFPACT_SET_FIELD:
3844 return mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field);
3846 case OFPACT_PUSH_MPLS:
3847 /* Recirculate if it is an IP packet with a zero ttl. This may
3848 * indicate that the packet was previously MPLS and an MPLS pop action
3849 * converted it to IP. In this case recirculating should reveal the IP
3850 * TTL which is used as the basis for a new MPLS LSE. */
3851 return (!flow_count_mpls_labels(flow, wc)
3852 && flow->nw_ttl == 0
3853 && is_ip_any(flow));
3860 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
3861 struct xlate_ctx *ctx)
3863 struct flow_wildcards *wc = &ctx->xout->wc;
3864 struct flow *flow = &ctx->xin->flow;
3865 const struct ofpact *a;
3867 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3868 tnl_arp_snoop(flow, wc, ctx->xbridge->name);
3870 /* dl_type already in the mask, not set below. */
3872 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3873 struct ofpact_controller *controller;
3874 const struct ofpact_metadata *metadata;
3875 const struct ofpact_set_field *set_field;
3876 const struct mf_field *mf;
3882 if (ofpact_needs_recirculation_after_mpls(a, ctx)) {
3883 compose_recirculate_action(ctx, ofpacts, a, ofpacts_len);
3889 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
3890 ofpact_get_OUTPUT(a)->max_len, true);
3894 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
3899 case OFPACT_CONTROLLER:
3900 controller = ofpact_get_CONTROLLER(a);
3901 execute_controller_action(ctx, controller->max_len,
3903 controller->controller_id);
3906 case OFPACT_ENQUEUE:
3907 memset(&wc->masks.skb_priority, 0xff,
3908 sizeof wc->masks.skb_priority);
3909 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
3912 case OFPACT_SET_VLAN_VID:
3913 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
3914 if (flow->vlan_tci & htons(VLAN_CFI) ||
3915 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
3916 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
3917 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
3922 case OFPACT_SET_VLAN_PCP:
3923 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
3924 if (flow->vlan_tci & htons(VLAN_CFI) ||
3925 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
3926 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
3927 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
3928 << VLAN_PCP_SHIFT) | VLAN_CFI);
3932 case OFPACT_STRIP_VLAN:
3933 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3934 flow->vlan_tci = htons(0);
3937 case OFPACT_PUSH_VLAN:
3938 /* XXX 802.1AD(QinQ) */
3939 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3940 flow->vlan_tci = htons(VLAN_CFI);
3943 case OFPACT_SET_ETH_SRC:
3944 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3945 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
3948 case OFPACT_SET_ETH_DST:
3949 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3950 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
3953 case OFPACT_SET_IPV4_SRC:
3954 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3955 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3956 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
3960 case OFPACT_SET_IPV4_DST:
3961 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3962 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3963 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
3967 case OFPACT_SET_IP_DSCP:
3968 if (is_ip_any(flow)) {
3969 wc->masks.nw_tos |= IP_DSCP_MASK;
3970 flow->nw_tos &= ~IP_DSCP_MASK;
3971 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
3975 case OFPACT_SET_IP_ECN:
3976 if (is_ip_any(flow)) {
3977 wc->masks.nw_tos |= IP_ECN_MASK;
3978 flow->nw_tos &= ~IP_ECN_MASK;
3979 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
3983 case OFPACT_SET_IP_TTL:
3984 if (is_ip_any(flow)) {
3985 wc->masks.nw_ttl = 0xff;
3986 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
3990 case OFPACT_SET_L4_SRC_PORT:
3991 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3992 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3993 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3994 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
3998 case OFPACT_SET_L4_DST_PORT:
3999 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4000 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4001 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
4002 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
4006 case OFPACT_RESUBMIT:
4007 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
4010 case OFPACT_SET_TUNNEL:
4011 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
4014 case OFPACT_SET_QUEUE:
4015 memset(&wc->masks.skb_priority, 0xff,
4016 sizeof wc->masks.skb_priority);
4017 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
4020 case OFPACT_POP_QUEUE:
4021 memset(&wc->masks.skb_priority, 0xff,
4022 sizeof wc->masks.skb_priority);
4023 flow->skb_priority = ctx->orig_skb_priority;
4026 case OFPACT_REG_MOVE:
4027 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
4030 case OFPACT_SET_FIELD:
4031 set_field = ofpact_get_SET_FIELD(a);
4032 mf = set_field->field;
4034 /* Set field action only ever overwrites packet's outermost
4035 * applicable header fields. Do nothing if no header exists. */
4036 if (mf->id == MFF_VLAN_VID) {
4037 wc->masks.vlan_tci |= htons(VLAN_CFI);
4038 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4041 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
4042 /* 'dl_type' is already unwildcarded. */
4043 && !eth_type_mpls(flow->dl_type)) {
4046 /* A flow may wildcard nw_frag. Do nothing if setting a trasport
4047 * header field on a packet that does not have them. */
4048 mf_mask_field_and_prereqs(mf, &wc->masks);
4049 if (mf_are_prereqs_ok(mf, flow)) {
4050 mf_set_flow_value_masked(mf, &set_field->value,
4051 &set_field->mask, flow);
4055 case OFPACT_STACK_PUSH:
4056 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
4060 case OFPACT_STACK_POP:
4061 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
4065 case OFPACT_PUSH_MPLS:
4066 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
4069 case OFPACT_POP_MPLS:
4070 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
4073 case OFPACT_SET_MPLS_LABEL:
4074 compose_set_mpls_label_action(
4075 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
4078 case OFPACT_SET_MPLS_TC:
4079 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
4082 case OFPACT_SET_MPLS_TTL:
4083 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
4086 case OFPACT_DEC_MPLS_TTL:
4087 if (compose_dec_mpls_ttl_action(ctx)) {
4092 case OFPACT_DEC_TTL:
4093 wc->masks.nw_ttl = 0xff;
4094 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
4100 /* Nothing to do. */
4103 case OFPACT_MULTIPATH:
4104 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
4108 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
4111 case OFPACT_OUTPUT_REG:
4112 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
4116 xlate_learn_action(ctx, ofpact_get_LEARN(a));
4119 case OFPACT_CONJUNCTION: {
4120 /* A flow with a "conjunction" action represents part of a special
4121 * kind of "set membership match". Such a flow should not actually
4122 * get executed, but it could via, say, a "packet-out", even though
4123 * that wouldn't be useful. Log it to help debugging. */
4124 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4125 VLOG_INFO_RL(&rl, "executing no-op conjunction action");
4133 case OFPACT_FIN_TIMEOUT:
4134 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4135 ctx->xout->has_fin_timeout = true;
4136 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
4139 case OFPACT_CLEAR_ACTIONS:
4140 ofpbuf_clear(&ctx->action_set);
4141 ctx->xin->flow.actset_output = OFPP_UNSET;
4142 ctx->action_set_has_group = false;
4145 case OFPACT_WRITE_ACTIONS:
4146 xlate_write_actions(ctx, a);
4149 case OFPACT_WRITE_METADATA:
4150 metadata = ofpact_get_WRITE_METADATA(a);
4151 flow->metadata &= ~metadata->mask;
4152 flow->metadata |= metadata->metadata & metadata->mask;
4156 /* Not implemented yet. */
4159 case OFPACT_GOTO_TABLE: {
4160 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
4162 /* Allow ctx->table_id == TBL_INTERNAL, which will be greater
4163 * than ogt->table_id. This is to allow goto_table actions that
4164 * triggered recirculation: ctx->table_id will be TBL_INTERNAL
4165 * after recirculation. */
4166 ovs_assert(ctx->table_id == TBL_INTERNAL
4167 || ctx->table_id < ogt->table_id);
4168 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
4169 ogt->table_id, true, true);
4174 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
4181 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
4182 const struct flow *flow, ofp_port_t in_port,
4183 struct rule_dpif *rule, uint16_t tcp_flags,
4184 const struct dp_packet *packet)
4186 xin->ofproto = ofproto;
4188 xin->flow.in_port.ofp_port = in_port;
4189 xin->flow.actset_output = OFPP_UNSET;
4190 xin->packet = packet;
4191 xin->may_learn = packet != NULL;
4194 xin->ofpacts = NULL;
4195 xin->ofpacts_len = 0;
4196 xin->tcp_flags = tcp_flags;
4197 xin->resubmit_hook = NULL;
4198 xin->report_hook = NULL;
4199 xin->resubmit_stats = NULL;
4200 xin->skip_wildcards = false;
4201 xin->odp_actions = NULL;
4205 xlate_out_uninit(struct xlate_out *xout)
4207 if (xout && xout->odp_actions == &xout->odp_actions_buf) {
4208 ofpbuf_uninit(xout->odp_actions);
4212 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
4213 * into datapath actions, using 'ctx', and discards the datapath actions. */
4215 xlate_actions_for_side_effects(struct xlate_in *xin)
4217 struct xlate_out xout;
4219 xlate_actions(xin, &xout);
4220 xlate_out_uninit(&xout);
4224 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
4227 dst->slow = src->slow;
4228 dst->has_learn = src->has_learn;
4229 dst->has_normal = src->has_normal;
4230 dst->has_fin_timeout = src->has_fin_timeout;
4231 dst->nf_output_iface = src->nf_output_iface;
4232 dst->mirrors = src->mirrors;
4234 dst->odp_actions = &dst->odp_actions_buf;
4235 ofpbuf_use_stub(dst->odp_actions, dst->odp_actions_stub,
4236 sizeof dst->odp_actions_stub);
4237 ofpbuf_put(dst->odp_actions, src->odp_actions->data, src->odp_actions->size);
4240 static struct skb_priority_to_dscp *
4241 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
4243 struct skb_priority_to_dscp *pdscp;
4246 hash = hash_int(skb_priority, 0);
4247 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
4248 if (pdscp->skb_priority == skb_priority) {
4256 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
4259 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
4260 *dscp = pdscp ? pdscp->dscp : 0;
4261 return pdscp != NULL;
4265 count_skb_priorities(const struct xport *xport)
4267 return hmap_count(&xport->skb_priorities);
4271 clear_skb_priorities(struct xport *xport)
4273 struct skb_priority_to_dscp *pdscp, *next;
4275 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
4276 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
4282 actions_output_to_local_port(const struct xlate_ctx *ctx)
4284 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
4285 const struct nlattr *a;
4288 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions->data,
4289 ctx->xout->odp_actions->size) {
4290 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
4291 && nl_attr_get_odp_port(a) == local_odp_port) {
4298 #if defined(__linux__)
4299 /* Returns the maximum number of packets that the Linux kernel is willing to
4300 * queue up internally to certain kinds of software-implemented ports, or the
4301 * default (and rarely modified) value if it cannot be determined. */
4303 netdev_max_backlog(void)
4305 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
4306 static int max_backlog = 1000; /* The normal default value. */
4308 if (ovsthread_once_start(&once)) {
4309 static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
4313 stream = fopen(filename, "r");
4315 VLOG_WARN("%s: open failed (%s)", filename, ovs_strerror(errno));
4317 if (fscanf(stream, "%d", &n) != 1) {
4318 VLOG_WARN("%s: read error", filename);
4319 } else if (n <= 100) {
4320 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
4326 ovsthread_once_done(&once);
4328 VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
4334 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
4337 count_output_actions(const struct ofpbuf *odp_actions)
4339 const struct nlattr *a;
4343 NL_ATTR_FOR_EACH_UNSAFE (a, left, odp_actions->data, odp_actions->size) {
4344 if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
4350 #endif /* defined(__linux__) */
4352 /* Returns true if 'odp_actions' contains more output actions than the datapath
4353 * can reliably handle in one go. On Linux, this is the value of the
4354 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
4355 * packets that the kernel is willing to queue up for processing while the
4356 * datapath is processing a set of actions. */
4358 too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
4361 return (odp_actions->size / NL_A_U32_SIZE > netdev_max_backlog()
4362 && count_output_actions(odp_actions) > netdev_max_backlog());
4364 /* OSes other than Linux might have similar limits, but we don't know how
4365 * to determine them.*/
4370 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
4371 * into datapath actions in 'odp_actions', using 'ctx'.
4373 * The caller must take responsibility for eventually freeing 'xout', with
4374 * xlate_out_uninit(). */
4376 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
4378 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4379 struct flow_wildcards *wc = NULL;
4380 struct flow *flow = &xin->flow;
4381 struct rule_dpif *rule = NULL;
4383 enum slow_path_reason special;
4384 const struct ofpact *ofpacts;
4385 struct xport *in_port;
4386 struct flow orig_flow;
4387 struct xlate_ctx ctx;
4392 COVERAGE_INC(xlate_actions);
4394 /* Flow initialization rules:
4395 * - 'base_flow' must match the kernel's view of the packet at the
4396 * time that action processing starts. 'flow' represents any
4397 * transformations we wish to make through actions.
4398 * - By default 'base_flow' and 'flow' are the same since the input
4399 * packet matches the output before any actions are applied.
4400 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
4401 * of the received packet as seen by the kernel. If we later output
4402 * to another device without any modifications this will cause us to
4403 * insert a new tag since the original one was stripped off by the
4405 * - Tunnel metadata as received is retained in 'flow'. This allows
4406 * tunnel metadata matching also in later tables.
4407 * Since a kernel action for setting the tunnel metadata will only be
4408 * generated with actual tunnel output, changing the tunnel metadata
4409 * values in 'flow' (such as tun_id) will only have effect with a later
4410 * tunnel output action.
4411 * - Tunnel 'base_flow' is completely cleared since that is what the
4412 * kernel does. If we wish to maintain the original values an action
4413 * needs to be generated. */
4418 ctx.xout->has_learn = false;
4419 ctx.xout->has_normal = false;
4420 ctx.xout->has_fin_timeout = false;
4421 ctx.xout->nf_output_iface = NF_OUT_DROP;
4422 ctx.xout->mirrors = 0;
4424 xout->odp_actions = xin->odp_actions;
4425 if (!xout->odp_actions) {
4426 xout->odp_actions = &xout->odp_actions_buf;
4427 ofpbuf_use_stub(xout->odp_actions, xout->odp_actions_stub,
4428 sizeof xout->odp_actions_stub);
4430 ofpbuf_reserve(xout->odp_actions, NL_A_U32_SIZE);
4432 ctx.xbridge = xbridge_lookup(xcfg, xin->ofproto);
4436 ctx.rule = xin->rule;
4438 ctx.base_flow = *flow;
4439 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
4440 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
4442 if (!xin->skip_wildcards) {
4444 flow_wildcards_init_catchall(wc);
4445 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
4446 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
4447 if (is_ip_any(flow)) {
4448 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
4450 if (ctx.xbridge->enable_recirc) {
4451 /* Always exactly match recirc_id when datapath supports
4453 wc->masks.recirc_id = UINT32_MAX;
4455 if (ctx.xbridge->netflow) {
4456 netflow_mask_wc(flow, wc);
4459 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
4461 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
4465 ctx.in_group = false;
4466 ctx.in_action_set = false;
4467 ctx.orig_skb_priority = flow->skb_priority;
4469 ctx.rule_cookie = OVS_BE64_MAX;
4471 ctx.use_recirc = false;
4472 ctx.was_mpls = false;
4474 if (!xin->ofpacts && !ctx.rule) {
4475 rule = rule_dpif_lookup(ctx.xbridge->ofproto, flow, wc,
4476 ctx.xin->xcache != NULL,
4477 ctx.xin->resubmit_stats, &ctx.table_id);
4478 if (ctx.xin->resubmit_stats) {
4479 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
4481 if (ctx.xin->xcache) {
4482 struct xc_entry *entry;
4484 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
4485 entry->u.rule = rule;
4489 if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
4490 ctx.xin->resubmit_hook(ctx.xin, rule, 0);
4493 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
4496 ofpacts = xin->ofpacts;
4497 ofpacts_len = xin->ofpacts_len;
4498 } else if (ctx.rule) {
4499 const struct rule_actions *actions = rule_dpif_get_actions(ctx.rule);
4501 ofpacts = actions->ofpacts;
4502 ofpacts_len = actions->ofpacts_len;
4504 ctx.rule_cookie = rule_dpif_get_flow_cookie(ctx.rule);
4509 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
4511 ctx.action_set_has_group = false;
4512 ofpbuf_use_stub(&ctx.action_set,
4513 ctx.action_set_stub, sizeof ctx.action_set_stub);
4515 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
4516 /* Do this conditionally because the copy is expensive enough that it
4517 * shows up in profiles. */
4521 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
4522 if (in_port && in_port->is_tunnel) {
4523 if (ctx.xin->resubmit_stats) {
4524 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
4526 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
4529 if (ctx.xin->xcache) {
4530 struct xc_entry *entry;
4532 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
4533 entry->u.dev.rx = netdev_ref(in_port->netdev);
4534 entry->u.dev.bfd = bfd_ref(in_port->bfd);
4538 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
4540 ctx.xout->slow |= special;
4542 size_t sample_actions_len;
4544 if (flow->in_port.ofp_port
4545 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
4546 flow->in_port.ofp_port,
4548 ctx.base_flow.vlan_tci = 0;
4551 add_sflow_action(&ctx);
4552 add_ipfix_action(&ctx);
4553 sample_actions_len = ctx.xout->odp_actions->size;
4555 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
4556 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
4558 /* We've let OFPP_NORMAL and the learning action look at the
4559 * packet, so drop it now if forwarding is disabled. */
4560 if (in_port && (!xport_stp_forward_state(in_port) ||
4561 !xport_rstp_forward_state(in_port))) {
4562 /* Drop all actions added by do_xlate_actions() above. */
4563 ctx.xout->odp_actions->size = sample_actions_len;
4564 } else if (ctx.action_set.size) {
4565 /* Translate action set only if not dropping the packet. */
4566 xlate_action_set(&ctx);
4570 if (ctx.xbridge->has_in_band
4571 && in_band_must_output_to_local_port(flow)
4572 && !actions_output_to_local_port(&ctx)) {
4573 compose_output_action(&ctx, OFPP_LOCAL);
4576 fix_sflow_action(&ctx);
4578 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
4579 add_mirror_actions(&ctx, &orig_flow);
4583 if (nl_attr_oversized(ctx.xout->odp_actions->size)) {
4584 /* These datapath actions are too big for a Netlink attribute, so we
4585 * can't hand them to the kernel directly. dpif_execute() can execute
4586 * them one by one with help, so just mark the result as SLOW_ACTION to
4587 * prevent the flow from being installed. */
4588 COVERAGE_INC(xlate_actions_oversize);
4589 ctx.xout->slow |= SLOW_ACTION;
4590 } else if (too_many_output_actions(ctx.xout->odp_actions)) {
4591 COVERAGE_INC(xlate_actions_too_many_output);
4592 ctx.xout->slow |= SLOW_ACTION;
4595 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
4596 if (ctx.xin->resubmit_stats) {
4597 mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
4598 ctx.xin->resubmit_stats->n_packets,
4599 ctx.xin->resubmit_stats->n_bytes);
4601 if (ctx.xin->xcache) {
4602 struct xc_entry *entry;
4604 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_MIRROR);
4605 entry->u.mirror.mbridge = mbridge_ref(ctx.xbridge->mbridge);
4606 entry->u.mirror.mirrors = xout->mirrors;
4610 if (ctx.xbridge->netflow) {
4611 /* Only update netflow if we don't have controller flow. We don't
4612 * report NetFlow expiration messages for such facets because they
4613 * are just part of the control logic for the network, not real
4615 if (ofpacts_len == 0
4616 || ofpacts->type != OFPACT_CONTROLLER
4617 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
4618 if (ctx.xin->resubmit_stats) {
4619 netflow_flow_update(ctx.xbridge->netflow, flow,
4620 xout->nf_output_iface,
4621 ctx.xin->resubmit_stats);
4623 if (ctx.xin->xcache) {
4624 struct xc_entry *entry;
4626 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
4627 entry->u.nf.netflow = netflow_ref(ctx.xbridge->netflow);
4628 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
4629 entry->u.nf.iface = xout->nf_output_iface;
4634 ofpbuf_uninit(&ctx.stack);
4635 ofpbuf_uninit(&ctx.action_set);
4638 /* Clear the metadata and register wildcard masks, because we won't
4639 * use non-header fields as part of the cache. */
4640 flow_wildcards_clear_non_packet_fields(wc);
4642 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
4643 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
4644 * represent these fields. The datapath interface, on the other hand,
4645 * represents them with just 8 bits each. This means that if the high
4646 * 8 bits of the masks for these fields somehow become set, then they
4647 * will get chopped off by a round trip through the datapath, and
4648 * revalidation will spot that as an inconsistency and delete the flow.
4649 * Avoid the problem here by making sure that only the low 8 bits of
4650 * either field can be unwildcarded for ICMP.
4653 wc->masks.tp_src &= htons(UINT8_MAX);
4654 wc->masks.tp_dst &= htons(UINT8_MAX);
4659 /* Sends 'packet' out 'ofport'.
4660 * May modify 'packet'.
4661 * Returns 0 if successful, otherwise a positive errno value. */
4663 xlate_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
4665 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4666 struct xport *xport;
4667 struct ofpact_output output;
4670 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
4671 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
4672 flow_extract(packet, &flow);
4673 flow.in_port.ofp_port = OFPP_NONE;
4675 xport = xport_lookup(xcfg, ofport);
4679 output.port = xport->ofp_port;
4682 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
4683 &output.ofpact, sizeof output,
4687 struct xlate_cache *
4688 xlate_cache_new(void)
4690 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
4692 ofpbuf_init(&xcache->entries, 512);
4696 static struct xc_entry *
4697 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
4699 struct xc_entry *entry;
4701 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
4708 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
4710 if (entry->u.dev.tx) {
4711 netdev_vport_inc_tx(entry->u.dev.tx, stats);
4713 if (entry->u.dev.rx) {
4714 netdev_vport_inc_rx(entry->u.dev.rx, stats);
4716 if (entry->u.dev.bfd) {
4717 bfd_account_rx(entry->u.dev.bfd, stats);
4722 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
4724 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4725 struct xbridge *xbridge;
4726 struct xbundle *xbundle;
4727 struct flow_wildcards wc;
4729 xbridge = xbridge_lookup(xcfg, ofproto);
4734 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
4740 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
4743 /* Push stats and perform side effects of flow translation. */
4745 xlate_push_stats(struct xlate_cache *xcache,
4746 const struct dpif_flow_stats *stats)
4748 struct xc_entry *entry;
4749 struct ofpbuf entries = xcache->entries;
4750 uint8_t dmac[ETH_ADDR_LEN];
4752 if (!stats->n_packets) {
4756 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
4757 switch (entry->type) {
4759 rule_dpif_credit_stats(entry->u.rule, stats);
4762 bond_account(entry->u.bond.bond, entry->u.bond.flow,
4763 entry->u.bond.vid, stats->n_bytes);
4766 xlate_cache_netdev(entry, stats);
4769 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
4770 entry->u.nf.iface, stats);
4773 mirror_update_stats(entry->u.mirror.mbridge,
4774 entry->u.mirror.mirrors,
4775 stats->n_packets, stats->n_bytes);
4778 ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
4781 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
4782 entry->u.normal.vlan);
4784 case XC_FIN_TIMEOUT:
4785 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
4786 entry->u.fin.idle, entry->u.fin.hard);
4789 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
4793 /* Lookup arp to avoid arp timeout. */
4794 tnl_arp_lookup(entry->u.tnl_arp_cache.br_name, entry->u.tnl_arp_cache.d_ip, dmac);
4803 xlate_dev_unref(struct xc_entry *entry)
4805 if (entry->u.dev.tx) {
4806 netdev_close(entry->u.dev.tx);
4808 if (entry->u.dev.rx) {
4809 netdev_close(entry->u.dev.rx);
4811 if (entry->u.dev.bfd) {
4812 bfd_unref(entry->u.dev.bfd);
4817 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
4819 netflow_flow_clear(netflow, flow);
4820 netflow_unref(netflow);
4825 xlate_cache_clear(struct xlate_cache *xcache)
4827 struct xc_entry *entry;
4828 struct ofpbuf entries;
4834 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
4835 switch (entry->type) {
4837 rule_dpif_unref(entry->u.rule);
4840 free(entry->u.bond.flow);
4841 bond_unref(entry->u.bond.bond);
4844 xlate_dev_unref(entry);
4847 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
4850 mbridge_unref(entry->u.mirror.mbridge);
4853 free(entry->u.learn.fm);
4854 ofpbuf_delete(entry->u.learn.ofpacts);
4857 free(entry->u.normal.flow);
4859 case XC_FIN_TIMEOUT:
4860 /* 'u.fin.rule' is always already held as a XC_RULE, which
4861 * has already released it's reference above. */
4864 group_dpif_unref(entry->u.group.group);
4873 ofpbuf_clear(&xcache->entries);
4877 xlate_cache_delete(struct xlate_cache *xcache)
4879 xlate_cache_clear(xcache);
4880 ofpbuf_uninit(&xcache->entries);