1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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"
25 #include "byte-order.h"
30 #include "dynamic-string.h"
35 #include "mac-learning.h"
36 #include "meta-flow.h"
37 #include "multipath.h"
38 #include "netdev-vport.h"
41 #include "odp-execute.h"
42 #include "ofp-actions.h"
43 #include "ofproto/ofproto-dpif-ipfix.h"
44 #include "ofproto/ofproto-dpif-mirror.h"
45 #include "ofproto/ofproto-dpif-monitor.h"
46 #include "ofproto/ofproto-dpif-sflow.h"
47 #include "ofproto/ofproto-dpif.h"
48 #include "ofproto/ofproto-provider.h"
49 #include "packet-dpif.h"
53 COVERAGE_DEFINE(xlate_actions);
54 COVERAGE_DEFINE(xlate_actions_oversize);
55 COVERAGE_DEFINE(xlate_actions_mpls_overflow);
57 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
59 /* Maximum depth of flow table recursion (due to resubmit actions) in a
60 * flow translation. */
61 #define MAX_RESUBMIT_RECURSION 64
62 #define MAX_INTERNAL_RESUBMITS 1 /* Max resbmits allowed using rules in
65 /* Maximum number of resubmit actions in a flow translation, whether they are
66 * recursive or not. */
67 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
70 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
71 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
73 struct list xbundles; /* Owned xbundles. */
74 struct hmap xports; /* Indexed by ofp_port. */
76 char *name; /* Name used in log messages. */
77 struct dpif *dpif; /* Datapath interface. */
78 struct mac_learning *ml; /* Mac learning handle. */
79 struct mbridge *mbridge; /* Mirroring. */
80 struct dpif_sflow *sflow; /* SFlow handle, or null. */
81 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
82 struct netflow *netflow; /* Netflow handle, or null. */
83 struct stp *stp; /* STP or null if disabled. */
85 /* Special rules installed by ofproto-dpif. */
86 struct rule_dpif *miss_rule;
87 struct rule_dpif *no_packet_in_rule;
89 enum ofp_config_flags frag; /* Fragmentation handling. */
90 bool has_in_band; /* Bridge has in band control? */
91 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
93 /* True if the datapath supports recirculation. */
96 /* True if the datapath supports variable-length
97 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
98 * False if the datapath supports only 8-byte (or shorter) userdata. */
99 bool variable_length_userdata;
101 /* Number of MPLS label stack entries that the datapath supports
103 size_t max_mpls_depth;
107 struct hmap_node hmap_node; /* In global 'xbundles' map. */
108 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
110 struct list list_node; /* In parent 'xbridges' list. */
111 struct xbridge *xbridge; /* Parent xbridge. */
113 struct list xports; /* Contains "struct xport"s. */
115 char *name; /* Name used in log messages. */
116 struct bond *bond; /* Nonnull iff more than one port. */
117 struct lacp *lacp; /* LACP handle or null. */
119 enum port_vlan_mode vlan_mode; /* VLAN mode. */
120 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
121 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
122 * NULL if all VLANs are trunked. */
123 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
124 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
128 struct hmap_node hmap_node; /* Node in global 'xports' map. */
129 struct ofport_dpif *ofport; /* Key in global 'xports map. */
131 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
132 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
134 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
136 struct list bundle_node; /* In parent xbundle (if it exists). */
137 struct xbundle *xbundle; /* Parent xbundle or null. */
139 struct netdev *netdev; /* 'ofport''s netdev. */
141 struct xbridge *xbridge; /* Parent bridge. */
142 struct xport *peer; /* Patch port peer or null. */
144 enum ofputil_port_config config; /* OpenFlow port configuration. */
145 enum ofputil_port_state state; /* OpenFlow port state. */
146 int stp_port_no; /* STP port number or -1 if not in use. */
148 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
150 bool may_enable; /* May be enabled in bonds. */
151 bool is_tunnel; /* Is a tunnel port. */
153 struct cfm *cfm; /* CFM handle or null. */
154 struct bfd *bfd; /* BFD handle or null. */
158 struct xlate_in *xin;
159 struct xlate_out *xout;
161 const struct xbridge *xbridge;
163 /* Flow at the last commit. */
164 struct flow base_flow;
166 /* Tunnel IP destination address as received. This is stored separately
167 * as the base_flow.tunnel is cleared on init to reflect the datapath
168 * behavior. Used to make sure not to send tunneled output to ourselves,
169 * which might lead to an infinite loop. This could happen easily
170 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
171 * actually set the tun_dst field. */
172 ovs_be32 orig_tunnel_ip_dst;
174 /* Stack for the push and pop actions. Each stack element is of type
175 * "union mf_subvalue". */
176 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
179 /* The rule that we are currently translating, or NULL. */
180 struct rule_dpif *rule;
182 /* Resubmit statistics, via xlate_table_action(). */
183 int recurse; /* Current resubmit nesting depth. */
184 int resubmits; /* Total number of resubmits. */
185 bool in_group; /* Currently translating ofgroup, if true. */
187 uint32_t orig_skb_priority; /* Priority when packet arrived. */
188 uint8_t table_id; /* OpenFlow table ID where flow was found. */
189 uint32_t sflow_n_outputs; /* Number of output ports. */
190 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
191 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
192 bool exit; /* No further actions should be processed. */
194 bool use_recirc; /* Should generate recirc? */
195 struct xlate_recirc recirc; /* Information used for generating
196 * recirculation actions */
198 /* OpenFlow 1.1+ action set.
200 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
201 * When translation is otherwise complete, ofpacts_execute_action_set()
202 * converts it to a set of "struct ofpact"s that can be translated into
203 * datapath actions. */
204 struct ofpbuf action_set; /* Action set. */
205 uint64_t action_set_stub[1024 / 8];
208 /* A controller may use OFPP_NONE as the ingress port to indicate that
209 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
210 * when an input bundle is needed for validation (e.g., mirroring or
211 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
212 * any 'port' structs, so care must be taken when dealing with it. */
213 static struct xbundle ofpp_none_bundle = {
215 .vlan_mode = PORT_VLAN_TRUNK
218 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
219 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
220 * traffic egressing the 'ofport' with that priority should be marked with. */
221 struct skb_priority_to_dscp {
222 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
223 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
225 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
240 /* xlate_cache entries hold enough information to perform the side effects of
241 * xlate_actions() for a rule, without needing to perform rule translation
242 * from scratch. The primary usage of these is to submit statistics to objects
243 * that a flow relates to, although they may be used for other effects as well
244 * (for instance, refreshing hard timeouts for learned flows). */
248 struct rule_dpif *rule;
255 struct netflow *netflow;
260 struct mbridge *mbridge;
261 mirror_mask_t mirrors;
269 struct ofproto_dpif *ofproto;
270 struct ofputil_flow_mod *fm;
271 struct ofpbuf *ofpacts;
274 struct ofproto_dpif *ofproto;
279 struct rule_dpif *rule;
284 struct group_dpif *group;
285 struct ofputil_bucket *bucket;
290 #define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
291 entries = xcache->entries; \
292 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
294 entry = ofpbuf_try_pull(&entries, sizeof *entry))
297 struct ofpbuf entries;
300 /* Xlate config contains hash maps of all bridges, bundles and ports.
301 * Xcfgp contains the pointer to the current xlate configuration.
302 * When the main thread needs to change the configuration, it copies xcfgp to
303 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
304 * does not block handler and revalidator threads. */
306 struct hmap xbridges;
307 struct hmap xbundles;
310 static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_TYPE_INITIALIZER;
311 static struct xlate_cfg *new_xcfg = NULL;
313 static bool may_receive(const struct xport *, struct xlate_ctx *);
314 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
316 static void xlate_normal(struct xlate_ctx *);
317 static void xlate_report(struct xlate_ctx *, const char *);
318 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
319 uint8_t table_id, bool may_packet_in,
320 bool honor_table_miss);
321 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
322 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
323 static void output_normal(struct xlate_ctx *, const struct xbundle *,
325 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
327 static struct xbridge *xbridge_lookup(struct xlate_cfg *,
328 const struct ofproto_dpif *);
329 static struct xbundle *xbundle_lookup(struct xlate_cfg *,
330 const struct ofbundle *);
331 static struct xport *xport_lookup(struct xlate_cfg *,
332 const struct ofport_dpif *);
333 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
334 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
335 uint32_t skb_priority);
336 static void clear_skb_priorities(struct xport *);
337 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
340 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
342 static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
343 static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
344 static void xlate_xport_init(struct xlate_cfg *, struct xport *);
345 static void xlate_xbridge_set(struct xbridge *xbridge,
347 struct rule_dpif *miss_rule,
348 struct rule_dpif *no_packet_in_rule,
349 const struct mac_learning *ml, struct stp *stp,
350 const struct mbridge *mbridge,
351 const struct dpif_sflow *sflow,
352 const struct dpif_ipfix *ipfix,
353 const struct netflow *netflow,
354 enum ofp_config_flags frag,
355 bool forward_bpdu, bool has_in_band,
357 bool variable_length_userdata,
358 size_t max_mpls_depth);
359 static void xlate_xbundle_set(struct xbundle *xbundle,
360 enum port_vlan_mode vlan_mode, int vlan,
361 unsigned long *trunks, bool use_priority_tags,
362 const struct bond *bond, const struct lacp *lacp,
364 static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
365 const struct netdev *netdev, const struct cfm *cfm,
366 const struct bfd *bfd, int stp_port_no,
367 enum ofputil_port_config config,
368 enum ofputil_port_state state, bool is_tunnel,
370 static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
371 static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
372 static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
373 static void xlate_xbridge_copy(struct xbridge *);
374 static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
375 static void xlate_xport_copy(struct xbridge *, struct xbundle *,
377 static void xlate_xcfg_free(struct xlate_cfg *);
381 xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
383 list_init(&xbridge->xbundles);
384 hmap_init(&xbridge->xports);
385 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
386 hash_pointer(xbridge->ofproto, 0));
390 xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
392 list_init(&xbundle->xports);
393 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
394 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
395 hash_pointer(xbundle->ofbundle, 0));
399 xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
401 hmap_init(&xport->skb_priorities);
402 hmap_insert(&xcfg->xports, &xport->hmap_node,
403 hash_pointer(xport->ofport, 0));
404 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
405 hash_ofp_port(xport->ofp_port));
409 xlate_xbridge_set(struct xbridge *xbridge,
411 struct rule_dpif *miss_rule,
412 struct rule_dpif *no_packet_in_rule,
413 const struct mac_learning *ml, struct stp *stp,
414 const struct mbridge *mbridge,
415 const struct dpif_sflow *sflow,
416 const struct dpif_ipfix *ipfix,
417 const struct netflow *netflow, enum ofp_config_flags frag,
418 bool forward_bpdu, bool has_in_band,
420 bool variable_length_userdata,
421 size_t max_mpls_depth)
423 if (xbridge->ml != ml) {
424 mac_learning_unref(xbridge->ml);
425 xbridge->ml = mac_learning_ref(ml);
428 if (xbridge->mbridge != mbridge) {
429 mbridge_unref(xbridge->mbridge);
430 xbridge->mbridge = mbridge_ref(mbridge);
433 if (xbridge->sflow != sflow) {
434 dpif_sflow_unref(xbridge->sflow);
435 xbridge->sflow = dpif_sflow_ref(sflow);
438 if (xbridge->ipfix != ipfix) {
439 dpif_ipfix_unref(xbridge->ipfix);
440 xbridge->ipfix = dpif_ipfix_ref(ipfix);
443 if (xbridge->stp != stp) {
444 stp_unref(xbridge->stp);
445 xbridge->stp = stp_ref(stp);
448 if (xbridge->netflow != netflow) {
449 netflow_unref(xbridge->netflow);
450 xbridge->netflow = netflow_ref(netflow);
453 xbridge->dpif = dpif;
454 xbridge->forward_bpdu = forward_bpdu;
455 xbridge->has_in_band = has_in_band;
456 xbridge->frag = frag;
457 xbridge->miss_rule = miss_rule;
458 xbridge->no_packet_in_rule = no_packet_in_rule;
459 xbridge->enable_recirc = enable_recirc;
460 xbridge->variable_length_userdata = variable_length_userdata;
461 xbridge->max_mpls_depth = max_mpls_depth;
465 xlate_xbundle_set(struct xbundle *xbundle,
466 enum port_vlan_mode vlan_mode, int vlan,
467 unsigned long *trunks, bool use_priority_tags,
468 const struct bond *bond, const struct lacp *lacp,
471 ovs_assert(xbundle->xbridge);
473 xbundle->vlan_mode = vlan_mode;
474 xbundle->vlan = vlan;
475 xbundle->trunks = trunks;
476 xbundle->use_priority_tags = use_priority_tags;
477 xbundle->floodable = floodable;
479 if (xbundle->bond != bond) {
480 bond_unref(xbundle->bond);
481 xbundle->bond = bond_ref(bond);
484 if (xbundle->lacp != lacp) {
485 lacp_unref(xbundle->lacp);
486 xbundle->lacp = lacp_ref(lacp);
491 xlate_xport_set(struct xport *xport, odp_port_t odp_port,
492 const struct netdev *netdev, const struct cfm *cfm,
493 const struct bfd *bfd, int stp_port_no,
494 enum ofputil_port_config config, enum ofputil_port_state state,
495 bool is_tunnel, bool may_enable)
497 xport->config = config;
498 xport->state = state;
499 xport->stp_port_no = stp_port_no;
500 xport->is_tunnel = is_tunnel;
501 xport->may_enable = may_enable;
502 xport->odp_port = odp_port;
504 if (xport->cfm != cfm) {
505 cfm_unref(xport->cfm);
506 xport->cfm = cfm_ref(cfm);
509 if (xport->bfd != bfd) {
510 bfd_unref(xport->bfd);
511 xport->bfd = bfd_ref(bfd);
514 if (xport->netdev != netdev) {
515 netdev_close(xport->netdev);
516 xport->netdev = netdev_ref(netdev);
521 xlate_xbridge_copy(struct xbridge *xbridge)
523 struct xbundle *xbundle;
525 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
526 new_xbridge->ofproto = xbridge->ofproto;
527 new_xbridge->name = xstrdup(xbridge->name);
528 xlate_xbridge_init(new_xcfg, new_xbridge);
530 xlate_xbridge_set(new_xbridge,
531 xbridge->dpif, xbridge->miss_rule,
532 xbridge->no_packet_in_rule, xbridge->ml, xbridge->stp,
533 xbridge->mbridge, xbridge->sflow, xbridge->ipfix,
534 xbridge->netflow, xbridge->frag, xbridge->forward_bpdu,
535 xbridge->has_in_band, xbridge->enable_recirc,
536 xbridge->variable_length_userdata,
537 xbridge->max_mpls_depth);
538 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
539 xlate_xbundle_copy(new_xbridge, xbundle);
542 /* Copy xports which are not part of a xbundle */
543 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
544 if (!xport->xbundle) {
545 xlate_xport_copy(new_xbridge, NULL, xport);
551 xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
554 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
555 new_xbundle->ofbundle = xbundle->ofbundle;
556 new_xbundle->xbridge = xbridge;
557 new_xbundle->name = xstrdup(xbundle->name);
558 xlate_xbundle_init(new_xcfg, new_xbundle);
560 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
561 xbundle->vlan, xbundle->trunks,
562 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
564 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
565 xlate_xport_copy(xbridge, new_xbundle, xport);
570 xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
573 struct skb_priority_to_dscp *pdscp, *new_pdscp;
574 struct xport *new_xport = xzalloc(sizeof *xport);
575 new_xport->ofport = xport->ofport;
576 new_xport->ofp_port = xport->ofp_port;
577 new_xport->xbridge = xbridge;
578 xlate_xport_init(new_xcfg, new_xport);
580 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
581 xport->bfd, xport->stp_port_no, xport->config, xport->state,
582 xport->is_tunnel, xport->may_enable);
585 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
587 new_xport->peer = peer;
588 new_xport->peer->peer = new_xport;
593 new_xport->xbundle = xbundle;
594 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
597 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
598 new_pdscp = xmalloc(sizeof *pdscp);
599 new_pdscp->skb_priority = pdscp->skb_priority;
600 new_pdscp->dscp = pdscp->dscp;
601 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
602 hash_int(new_pdscp->skb_priority, 0));
606 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
607 * configuration in xcfgp.
609 * This needs to be called after editing the xlate configuration.
611 * Functions that edit the new xlate configuration are
612 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
618 * edit_xlate_configuration();
620 * xlate_txn_commit(); */
622 xlate_txn_commit(void)
624 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
626 ovsrcu_set(&xcfgp, new_xcfg);
627 ovsrcu_postpone(xlate_xcfg_free, xcfg);
632 /* Copies the current xlate configuration in xcfgp to new_xcfg.
634 * This needs to be called prior to editing the xlate configuration. */
636 xlate_txn_start(void)
638 struct xbridge *xbridge;
639 struct xlate_cfg *xcfg;
641 ovs_assert(!new_xcfg);
643 new_xcfg = xmalloc(sizeof *new_xcfg);
644 hmap_init(&new_xcfg->xbridges);
645 hmap_init(&new_xcfg->xbundles);
646 hmap_init(&new_xcfg->xports);
648 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
653 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
654 xlate_xbridge_copy(xbridge);
660 xlate_xcfg_free(struct xlate_cfg *xcfg)
662 struct xbridge *xbridge, *next_xbridge;
668 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
669 xlate_xbridge_remove(xcfg, xbridge);
672 hmap_destroy(&xcfg->xbridges);
673 hmap_destroy(&xcfg->xbundles);
674 hmap_destroy(&xcfg->xports);
679 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
680 struct dpif *dpif, struct rule_dpif *miss_rule,
681 struct rule_dpif *no_packet_in_rule,
682 const struct mac_learning *ml, struct stp *stp,
683 const struct mbridge *mbridge,
684 const struct dpif_sflow *sflow,
685 const struct dpif_ipfix *ipfix,
686 const struct netflow *netflow, enum ofp_config_flags frag,
687 bool forward_bpdu, bool has_in_band,
689 bool variable_length_userdata,
690 size_t max_mpls_depth)
692 struct xbridge *xbridge;
694 ovs_assert(new_xcfg);
696 xbridge = xbridge_lookup(new_xcfg, ofproto);
698 xbridge = xzalloc(sizeof *xbridge);
699 xbridge->ofproto = ofproto;
701 xlate_xbridge_init(new_xcfg, xbridge);
705 xbridge->name = xstrdup(name);
707 xlate_xbridge_set(xbridge, dpif, miss_rule, no_packet_in_rule, ml, stp,
708 mbridge, sflow, ipfix, netflow, frag, forward_bpdu,
709 has_in_band, enable_recirc, variable_length_userdata,
714 xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
716 struct xbundle *xbundle, *next_xbundle;
717 struct xport *xport, *next_xport;
723 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
724 xlate_xport_remove(xcfg, xport);
727 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
728 xlate_xbundle_remove(xcfg, xbundle);
731 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
732 mac_learning_unref(xbridge->ml);
733 mbridge_unref(xbridge->mbridge);
734 dpif_sflow_unref(xbridge->sflow);
735 dpif_ipfix_unref(xbridge->ipfix);
736 stp_unref(xbridge->stp);
737 hmap_destroy(&xbridge->xports);
743 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
745 struct xbridge *xbridge;
747 ovs_assert(new_xcfg);
749 xbridge = xbridge_lookup(new_xcfg, ofproto);
750 xlate_xbridge_remove(new_xcfg, xbridge);
754 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
755 const char *name, enum port_vlan_mode vlan_mode, int vlan,
756 unsigned long *trunks, bool use_priority_tags,
757 const struct bond *bond, const struct lacp *lacp,
760 struct xbundle *xbundle;
762 ovs_assert(new_xcfg);
764 xbundle = xbundle_lookup(new_xcfg, ofbundle);
766 xbundle = xzalloc(sizeof *xbundle);
767 xbundle->ofbundle = ofbundle;
768 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
770 xlate_xbundle_init(new_xcfg, xbundle);
774 xbundle->name = xstrdup(name);
776 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
777 use_priority_tags, bond, lacp, floodable);
781 xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
783 struct xport *xport, *next;
789 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
790 list_remove(&xport->bundle_node);
791 xport->xbundle = NULL;
794 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
795 list_remove(&xbundle->list_node);
796 bond_unref(xbundle->bond);
797 lacp_unref(xbundle->lacp);
803 xlate_bundle_remove(struct ofbundle *ofbundle)
805 struct xbundle *xbundle;
807 ovs_assert(new_xcfg);
809 xbundle = xbundle_lookup(new_xcfg, ofbundle);
810 xlate_xbundle_remove(new_xcfg, xbundle);
814 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
815 struct ofport_dpif *ofport, ofp_port_t ofp_port,
816 odp_port_t odp_port, const struct netdev *netdev,
817 const struct cfm *cfm, const struct bfd *bfd,
818 struct ofport_dpif *peer, int stp_port_no,
819 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
820 enum ofputil_port_config config,
821 enum ofputil_port_state state, bool is_tunnel,
827 ovs_assert(new_xcfg);
829 xport = xport_lookup(new_xcfg, ofport);
831 xport = xzalloc(sizeof *xport);
832 xport->ofport = ofport;
833 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
834 xport->ofp_port = ofp_port;
836 xlate_xport_init(new_xcfg, xport);
839 ovs_assert(xport->ofp_port == ofp_port);
841 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, stp_port_no, config,
842 state, is_tunnel, may_enable);
845 xport->peer->peer = NULL;
847 xport->peer = xport_lookup(new_xcfg, peer);
849 xport->peer->peer = xport;
852 if (xport->xbundle) {
853 list_remove(&xport->bundle_node);
855 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
856 if (xport->xbundle) {
857 list_insert(&xport->xbundle->xports, &xport->bundle_node);
860 clear_skb_priorities(xport);
861 for (i = 0; i < n_qdscp; i++) {
862 struct skb_priority_to_dscp *pdscp;
863 uint32_t skb_priority;
865 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
870 pdscp = xmalloc(sizeof *pdscp);
871 pdscp->skb_priority = skb_priority;
872 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
873 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
874 hash_int(pdscp->skb_priority, 0));
879 xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
886 xport->peer->peer = NULL;
890 if (xport->xbundle) {
891 list_remove(&xport->bundle_node);
894 clear_skb_priorities(xport);
895 hmap_destroy(&xport->skb_priorities);
897 hmap_remove(&xcfg->xports, &xport->hmap_node);
898 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
900 netdev_close(xport->netdev);
901 cfm_unref(xport->cfm);
902 bfd_unref(xport->bfd);
907 xlate_ofport_remove(struct ofport_dpif *ofport)
911 ovs_assert(new_xcfg);
913 xport = xport_lookup(new_xcfg, ofport);
914 xlate_xport_remove(new_xcfg, xport);
917 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
918 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
919 * Optionally populates 'ofproto' with the ofproto_dpif, 'odp_in_port' with
920 * the datapath in_port, that 'packet' ingressed, and 'ipfix', 'sflow', and
921 * 'netflow' with the appropriate handles for those protocols if they're
922 * enabled. Caller is responsible for unrefing them.
924 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
925 * 'flow''s in_port to OFPP_NONE.
927 * This function does post-processing on data returned from
928 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
929 * of the upcall processing logic. In particular, if the extracted in_port is
930 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
931 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
932 * a VLAN header onto 'packet' (if it is nonnull).
934 * Similarly, this function also includes some logic to help with tunnels. It
935 * may modify 'flow' as necessary to make the tunneling implementation
936 * transparent to the upcall processing logic.
938 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
939 * or some other positive errno if there are other problems. */
941 xlate_receive(const struct dpif_backer *backer, struct ofpbuf *packet,
942 const struct nlattr *key, size_t key_len, struct flow *flow,
943 struct ofproto_dpif **ofproto, struct dpif_ipfix **ipfix,
944 struct dpif_sflow **sflow, struct netflow **netflow,
945 odp_port_t *odp_in_port)
947 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
949 const struct xport *xport;
951 if (odp_flow_key_to_flow(key, key_len, flow) == ODP_FIT_ERROR) {
957 *odp_in_port = flow->in_port.odp_port;
960 xport = xport_lookup(xcfg, tnl_port_should_receive(flow)
961 ? tnl_port_receive(flow)
962 : odp_port_to_ofport(backer, flow->in_port.odp_port));
964 flow->in_port.ofp_port = xport ? xport->ofp_port : OFPP_NONE;
969 if (vsp_adjust_flow(xport->xbridge->ofproto, flow)) {
971 /* Make the packet resemble the flow, so that it gets sent to
972 * an OpenFlow controller properly, so that it looks correct
973 * for sFlow, and so that flow_extract() will get the correct
974 * vlan_tci if it is called on 'packet'. */
975 eth_push_vlan(packet, htons(ETH_TYPE_VLAN), flow->vlan_tci);
981 *ofproto = xport->xbridge->ofproto;
985 *ipfix = dpif_ipfix_ref(xport->xbridge->ipfix);
989 *sflow = dpif_sflow_ref(xport->xbridge->sflow);
993 *netflow = netflow_ref(xport->xbridge->netflow);
998 static struct xbridge *
999 xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
1001 struct hmap *xbridges;
1002 struct xbridge *xbridge;
1004 if (!ofproto || !xcfg) {
1008 xbridges = &xcfg->xbridges;
1010 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
1012 if (xbridge->ofproto == ofproto) {
1019 static struct xbundle *
1020 xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
1022 struct hmap *xbundles;
1023 struct xbundle *xbundle;
1025 if (!ofbundle || !xcfg) {
1029 xbundles = &xcfg->xbundles;
1031 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
1033 if (xbundle->ofbundle == ofbundle) {
1040 static struct xport *
1041 xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
1043 struct hmap *xports;
1044 struct xport *xport;
1046 if (!ofport || !xcfg) {
1050 xports = &xcfg->xports;
1052 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
1054 if (xport->ofport == ofport) {
1061 static struct stp_port *
1062 xport_get_stp_port(const struct xport *xport)
1064 return xport->xbridge->stp && xport->stp_port_no != -1
1065 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1070 xport_stp_learn_state(const struct xport *xport)
1072 struct stp_port *sp = xport_get_stp_port(xport);
1073 return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
1077 xport_stp_forward_state(const struct xport *xport)
1079 struct stp_port *sp = xport_get_stp_port(xport);
1080 return stp_forward_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
1084 xport_stp_listen_state(const struct xport *xport)
1086 struct stp_port *sp = xport_get_stp_port(xport);
1087 return stp_listen_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
1090 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1091 * were used to make the determination.*/
1093 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1095 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1096 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1097 return is_stp(flow);
1101 stp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
1103 struct stp_port *sp = xport_get_stp_port(xport);
1104 struct ofpbuf payload = *packet;
1105 struct eth_header *eth = ofpbuf_data(&payload);
1107 /* Sink packets on ports that have STP disabled when the bridge has
1109 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1113 /* Trim off padding on payload. */
1114 if (ofpbuf_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1115 ofpbuf_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1118 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1119 stp_received_bpdu(sp, ofpbuf_data(&payload), ofpbuf_size(&payload));
1123 static struct xport *
1124 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1126 struct xport *xport;
1128 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1130 if (xport->ofp_port == ofp_port) {
1138 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1140 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1141 return xport ? xport->odp_port : ODPP_NONE;
1145 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1147 struct xport *xport;
1149 xport = get_ofp_port(ctx->xbridge, ofp_port);
1150 if (!xport || xport->config & OFPUTIL_PC_PORT_DOWN ||
1151 xport->state & OFPUTIL_PS_LINK_DOWN) {
1158 static struct ofputil_bucket *
1159 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1163 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1165 struct group_dpif *group;
1167 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1168 struct ofputil_bucket *bucket;
1170 bucket = group_first_live_bucket(ctx, group, depth);
1171 group_dpif_unref(group);
1172 return bucket == NULL;
1178 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1181 bucket_is_alive(const struct xlate_ctx *ctx,
1182 struct ofputil_bucket *bucket, int depth)
1184 if (depth >= MAX_LIVENESS_RECURSION) {
1185 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1187 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1188 MAX_LIVENESS_RECURSION);
1192 return (!ofputil_bucket_has_liveness(bucket)
1193 || (bucket->watch_port != OFPP_ANY
1194 && odp_port_is_alive(ctx, bucket->watch_port))
1195 || (bucket->watch_group != OFPG_ANY
1196 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
1199 static struct ofputil_bucket *
1200 group_first_live_bucket(const struct xlate_ctx *ctx,
1201 const struct group_dpif *group, int depth)
1203 struct ofputil_bucket *bucket;
1204 const struct list *buckets;
1206 group_dpif_get_buckets(group, &buckets);
1207 LIST_FOR_EACH (bucket, list_node, buckets) {
1208 if (bucket_is_alive(ctx, bucket, depth)) {
1216 static struct ofputil_bucket *
1217 group_best_live_bucket(const struct xlate_ctx *ctx,
1218 const struct group_dpif *group,
1221 struct ofputil_bucket *best_bucket = NULL;
1222 uint32_t best_score = 0;
1225 struct ofputil_bucket *bucket;
1226 const struct list *buckets;
1228 group_dpif_get_buckets(group, &buckets);
1229 LIST_FOR_EACH (bucket, list_node, buckets) {
1230 if (bucket_is_alive(ctx, bucket, 0)) {
1231 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
1232 if (score >= best_score) {
1233 best_bucket = bucket;
1244 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
1246 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1247 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1251 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1253 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1256 static mirror_mask_t
1257 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1259 return xbundle != &ofpp_none_bundle
1260 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1264 static mirror_mask_t
1265 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
1267 return xbundle != &ofpp_none_bundle
1268 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1272 static mirror_mask_t
1273 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
1275 return xbundle != &ofpp_none_bundle
1276 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1280 static struct xbundle *
1281 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1282 bool warn, struct xport **in_xportp)
1284 struct xport *xport;
1286 /* Find the port and bundle for the received packet. */
1287 xport = get_ofp_port(xbridge, in_port);
1291 if (xport && xport->xbundle) {
1292 return xport->xbundle;
1295 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1296 * which a controller may use as the ingress port for traffic that
1297 * it is sourcing. */
1298 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
1299 return &ofpp_none_bundle;
1302 /* Odd. A few possible reasons here:
1304 * - We deleted a port but there are still a few packets queued up
1307 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1308 * we don't know about.
1310 * - The ofproto client didn't configure the port as part of a bundle.
1311 * This is particularly likely to happen if a packet was received on the
1312 * port after it was created, but before the client had a chance to
1313 * configure its bundle.
1316 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1318 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1319 "port %"PRIu16, xbridge->name, in_port);
1325 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
1327 const struct xbridge *xbridge = ctx->xbridge;
1328 mirror_mask_t mirrors;
1329 struct xbundle *in_xbundle;
1333 mirrors = ctx->xout->mirrors;
1334 ctx->xout->mirrors = 0;
1336 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
1337 ctx->xin->packet != NULL, NULL);
1341 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
1343 /* Drop frames on bundles reserved for mirroring. */
1344 if (xbundle_mirror_out(xbridge, in_xbundle)) {
1345 if (ctx->xin->packet != NULL) {
1346 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1347 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1348 "%s, which is reserved exclusively for mirroring",
1349 ctx->xbridge->name, in_xbundle->name);
1351 ofpbuf_clear(&ctx->xout->odp_actions);
1356 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
1357 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1360 vlan = input_vid_to_vlan(in_xbundle, vid);
1366 /* Restore the original packet before adding the mirror actions. */
1367 ctx->xin->flow = *orig_flow;
1370 mirror_mask_t dup_mirrors;
1371 struct ofbundle *out;
1372 unsigned long *vlans;
1377 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1378 &vlans, &dup_mirrors, &out, &out_vlan);
1379 ovs_assert(has_mirror);
1382 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1384 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
1387 if (!vlan_mirrored) {
1388 mirrors = zero_rightmost_1bit(mirrors);
1392 mirrors &= ~dup_mirrors;
1393 ctx->xout->mirrors |= dup_mirrors;
1395 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1396 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1398 output_normal(ctx, out_xbundle, vlan);
1400 } else if (vlan != out_vlan
1401 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
1402 struct xbundle *xbundle;
1404 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1405 if (xbundle_includes_vlan(xbundle, out_vlan)
1406 && !xbundle_mirror_out(xbridge, xbundle)) {
1407 output_normal(ctx, xbundle, out_vlan);
1414 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1415 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1416 * the bundle on which the packet was received, returns the VLAN to which the
1419 * Both 'vid' and the return value are in the range 0...4095. */
1421 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1423 switch (in_xbundle->vlan_mode) {
1424 case PORT_VLAN_ACCESS:
1425 return in_xbundle->vlan;
1428 case PORT_VLAN_TRUNK:
1431 case PORT_VLAN_NATIVE_UNTAGGED:
1432 case PORT_VLAN_NATIVE_TAGGED:
1433 return vid ? vid : in_xbundle->vlan;
1440 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1441 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1444 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1445 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1448 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1450 /* Allow any VID on the OFPP_NONE port. */
1451 if (in_xbundle == &ofpp_none_bundle) {
1455 switch (in_xbundle->vlan_mode) {
1456 case PORT_VLAN_ACCESS:
1459 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1460 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1461 "packet received on port %s configured as VLAN "
1462 "%"PRIu16" access port", vid, in_xbundle->name,
1469 case PORT_VLAN_NATIVE_UNTAGGED:
1470 case PORT_VLAN_NATIVE_TAGGED:
1472 /* Port must always carry its native VLAN. */
1476 case PORT_VLAN_TRUNK:
1477 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1479 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1480 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1481 "received on port %s not configured for trunking "
1482 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1494 /* Given 'vlan', the VLAN that a packet belongs to, and
1495 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1496 * that should be included in the 802.1Q header. (If the return value is 0,
1497 * then the 802.1Q header should only be included in the packet if there is a
1500 * Both 'vlan' and the return value are in the range 0...4095. */
1502 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1504 switch (out_xbundle->vlan_mode) {
1505 case PORT_VLAN_ACCESS:
1508 case PORT_VLAN_TRUNK:
1509 case PORT_VLAN_NATIVE_TAGGED:
1512 case PORT_VLAN_NATIVE_UNTAGGED:
1513 return vlan == out_xbundle->vlan ? 0 : vlan;
1521 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1524 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1526 ovs_be16 tci, old_tci;
1527 struct xport *xport;
1529 vid = output_vlan_to_vid(out_xbundle, vlan);
1530 if (list_is_empty(&out_xbundle->xports)) {
1531 /* Partially configured bundle with no slaves. Drop the packet. */
1533 } else if (!out_xbundle->bond) {
1534 ctx->use_recirc = false;
1535 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1538 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1539 struct flow_wildcards *wc = &ctx->xout->wc;
1540 struct xlate_recirc *xr = &ctx->recirc;
1541 struct ofport_dpif *ofport;
1543 if (ctx->xbridge->enable_recirc) {
1544 ctx->use_recirc = bond_may_recirc(
1545 out_xbundle->bond, &xr->recirc_id, &xr->hash_basis);
1547 if (ctx->use_recirc) {
1548 /* Only TCP mode uses recirculation. */
1549 xr->hash_alg = OVS_HASH_ALG_L4;
1550 bond_update_post_recirc_rules(out_xbundle->bond, false);
1552 /* Recirculation does not require unmasking hash fields. */
1557 ofport = bond_choose_output_slave(out_xbundle->bond,
1558 &ctx->xin->flow, wc, vid);
1559 xport = xport_lookup(xcfg, ofport);
1562 /* No slaves enabled, so drop packet. */
1566 /* If ctx->xout->use_recirc is set, the main thread will handle stats
1567 * accounting for this bond. */
1568 if (!ctx->use_recirc) {
1569 if (ctx->xin->resubmit_stats) {
1570 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1571 ctx->xin->resubmit_stats->n_bytes);
1573 if (ctx->xin->xcache) {
1574 struct xc_entry *entry;
1577 flow = &ctx->xin->flow;
1578 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1579 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1580 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1581 entry->u.bond.vid = vid;
1586 old_tci = *flow_tci;
1588 if (tci || out_xbundle->use_priority_tags) {
1589 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1591 tci |= htons(VLAN_CFI);
1596 compose_output_action(ctx, xport->ofp_port);
1597 *flow_tci = old_tci;
1600 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1601 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1602 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1604 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1606 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1610 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1611 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1615 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1616 if (flow->nw_proto == ARP_OP_REPLY) {
1618 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1619 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1620 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1622 return flow->nw_src == flow->nw_dst;
1628 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1629 * dropped. Returns true if they may be forwarded, false if they should be
1632 * 'in_port' must be the xport that corresponds to flow->in_port.
1633 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1635 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1636 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1637 * checked by input_vid_is_valid().
1639 * May also add tags to '*tags', although the current implementation only does
1640 * so in one special case.
1643 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1646 struct xbundle *in_xbundle = in_port->xbundle;
1647 const struct xbridge *xbridge = ctx->xbridge;
1648 struct flow *flow = &ctx->xin->flow;
1650 /* Drop frames for reserved multicast addresses
1651 * only if forward_bpdu option is absent. */
1652 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1653 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1657 if (in_xbundle->bond) {
1658 struct mac_entry *mac;
1660 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1666 xlate_report(ctx, "bonding refused admissibility, dropping");
1669 case BV_DROP_IF_MOVED:
1670 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1671 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1672 if (mac && mac->port.p != in_xbundle->ofbundle &&
1673 (!is_gratuitous_arp(flow, &ctx->xout->wc)
1674 || mac_entry_is_grat_arp_locked(mac))) {
1675 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1676 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1680 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1688 /* Checks whether a MAC learning update is necessary for MAC learning table
1689 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1692 * Most packets processed through the MAC learning table do not actually
1693 * change it in any way. This function requires only a read lock on the MAC
1694 * learning table, so it is much cheaper in this common case.
1696 * Keep the code here synchronized with that in update_learning_table__()
1699 is_mac_learning_update_needed(const struct mac_learning *ml,
1700 const struct flow *flow,
1701 struct flow_wildcards *wc,
1702 int vlan, struct xbundle *in_xbundle)
1703 OVS_REQ_RDLOCK(ml->rwlock)
1705 struct mac_entry *mac;
1707 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1711 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1712 if (!mac || mac_entry_age(ml, mac)) {
1716 if (is_gratuitous_arp(flow, wc)) {
1717 /* We don't want to learn from gratuitous ARP packets that are
1718 * reflected back over bond slaves so we lock the learning table. */
1719 if (!in_xbundle->bond) {
1721 } else if (mac_entry_is_grat_arp_locked(mac)) {
1726 return mac->port.p != in_xbundle->ofbundle;
1730 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1731 * received on 'in_xbundle' in 'vlan'.
1733 * This code repeats all the checks in is_mac_learning_update_needed() because
1734 * the lock was released between there and here and thus the MAC learning state
1735 * could have changed.
1737 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1740 update_learning_table__(const struct xbridge *xbridge,
1741 const struct flow *flow, struct flow_wildcards *wc,
1742 int vlan, struct xbundle *in_xbundle)
1743 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1745 struct mac_entry *mac;
1747 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1751 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1752 if (is_gratuitous_arp(flow, wc)) {
1753 /* We don't want to learn from gratuitous ARP packets that are
1754 * reflected back over bond slaves so we lock the learning table. */
1755 if (!in_xbundle->bond) {
1756 mac_entry_set_grat_arp_lock(mac);
1757 } else if (mac_entry_is_grat_arp_locked(mac)) {
1762 if (mac->port.p != in_xbundle->ofbundle) {
1763 /* The log messages here could actually be useful in debugging,
1764 * so keep the rate limit relatively high. */
1765 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1767 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1768 "on port %s in VLAN %d",
1769 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1770 in_xbundle->name, vlan);
1772 mac->port.p = in_xbundle->ofbundle;
1773 mac_learning_changed(xbridge->ml);
1778 update_learning_table(const struct xbridge *xbridge,
1779 const struct flow *flow, struct flow_wildcards *wc,
1780 int vlan, struct xbundle *in_xbundle)
1784 /* Don't learn the OFPP_NONE port. */
1785 if (in_xbundle == &ofpp_none_bundle) {
1789 /* First try the common case: no change to MAC learning table. */
1790 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1791 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1793 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1796 /* Slow path: MAC learning table might need an update. */
1797 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1798 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1799 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1804 xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
1807 struct xbundle *xbundle;
1809 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1810 if (xbundle != in_xbundle
1811 && xbundle_includes_vlan(xbundle, vlan)
1812 && xbundle->floodable
1813 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1814 output_normal(ctx, xbundle, vlan);
1817 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1821 xlate_normal(struct xlate_ctx *ctx)
1823 struct flow_wildcards *wc = &ctx->xout->wc;
1824 struct flow *flow = &ctx->xin->flow;
1825 struct xbundle *in_xbundle;
1826 struct xport *in_port;
1827 struct mac_entry *mac;
1832 ctx->xout->has_normal = true;
1834 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1835 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1836 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1838 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
1839 ctx->xin->packet != NULL, &in_port);
1841 xlate_report(ctx, "no input bundle, dropping");
1845 /* Drop malformed frames. */
1846 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
1847 !(flow->vlan_tci & htons(VLAN_CFI))) {
1848 if (ctx->xin->packet != NULL) {
1849 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1850 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
1851 "VLAN tag received on port %s",
1852 ctx->xbridge->name, in_xbundle->name);
1854 xlate_report(ctx, "partial VLAN tag, dropping");
1858 /* Drop frames on bundles reserved for mirroring. */
1859 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
1860 if (ctx->xin->packet != NULL) {
1861 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1862 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1863 "%s, which is reserved exclusively for mirroring",
1864 ctx->xbridge->name, in_xbundle->name);
1866 xlate_report(ctx, "input port is mirror output port, dropping");
1871 vid = vlan_tci_to_vid(flow->vlan_tci);
1872 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1873 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
1876 vlan = input_vid_to_vlan(in_xbundle, vid);
1878 /* Check other admissibility requirements. */
1879 if (in_port && !is_admissible(ctx, in_port, vlan)) {
1883 /* Learn source MAC. */
1884 if (ctx->xin->may_learn) {
1885 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
1887 if (ctx->xin->xcache) {
1888 struct xc_entry *entry;
1890 /* Save enough info to update mac learning table later. */
1891 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
1892 entry->u.normal.ofproto = ctx->xbridge->ofproto;
1893 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
1894 entry->u.normal.vlan = vlan;
1897 /* Determine output bundle. */
1898 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
1899 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
1900 mac_port = mac ? mac->port.p : NULL;
1901 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
1904 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1905 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
1906 if (mac_xbundle && mac_xbundle != in_xbundle) {
1907 xlate_report(ctx, "forwarding to learned port");
1908 output_normal(ctx, mac_xbundle, vlan);
1909 } else if (!mac_xbundle) {
1910 xlate_report(ctx, "learned port is unknown, dropping");
1912 xlate_report(ctx, "learned port is input port, dropping");
1915 xlate_report(ctx, "no learned MAC for destination, flooding");
1916 xlate_normal_flood(ctx, in_xbundle, vlan);
1920 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1921 * the number of packets out of UINT32_MAX to sample. The given
1922 * cookie is passed back in the callback for each sampled packet.
1925 compose_sample_action(const struct xbridge *xbridge,
1926 struct ofpbuf *odp_actions,
1927 const struct flow *flow,
1928 const uint32_t probability,
1929 const union user_action_cookie *cookie,
1930 const size_t cookie_size)
1932 size_t sample_offset, actions_offset;
1933 odp_port_t odp_port;
1937 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
1939 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
1941 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
1943 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
1944 pid = dpif_port_get_pid(xbridge->dpif, odp_port,
1945 flow_hash_5tuple(flow, 0));
1946 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
1949 nl_msg_end_nested(odp_actions, actions_offset);
1950 nl_msg_end_nested(odp_actions, sample_offset);
1951 return cookie_offset;
1955 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
1956 odp_port_t odp_port, unsigned int n_outputs,
1957 union user_action_cookie *cookie)
1961 cookie->type = USER_ACTION_COOKIE_SFLOW;
1962 cookie->sflow.vlan_tci = vlan_tci;
1964 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1965 * port information") for the interpretation of cookie->output. */
1966 switch (n_outputs) {
1968 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1969 cookie->sflow.output = 0x40000000 | 256;
1973 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
1975 cookie->sflow.output = ifindex;
1980 /* 0x80000000 means "multiple output ports. */
1981 cookie->sflow.output = 0x80000000 | n_outputs;
1986 /* Compose SAMPLE action for sFlow bridge sampling. */
1988 compose_sflow_action(const struct xbridge *xbridge,
1989 struct ofpbuf *odp_actions,
1990 const struct flow *flow,
1991 odp_port_t odp_port)
1993 uint32_t probability;
1994 union user_action_cookie cookie;
1996 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
2000 probability = dpif_sflow_get_probability(xbridge->sflow);
2001 compose_sflow_cookie(xbridge, htons(0), odp_port,
2002 odp_port == ODPP_NONE ? 0 : 1, &cookie);
2004 return compose_sample_action(xbridge, odp_actions, flow, probability,
2005 &cookie, sizeof cookie.sflow);
2009 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
2010 uint32_t obs_domain_id, uint32_t obs_point_id,
2011 union user_action_cookie *cookie)
2013 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
2014 cookie->flow_sample.probability = probability;
2015 cookie->flow_sample.collector_set_id = collector_set_id;
2016 cookie->flow_sample.obs_domain_id = obs_domain_id;
2017 cookie->flow_sample.obs_point_id = obs_point_id;
2021 compose_ipfix_cookie(union user_action_cookie *cookie)
2023 cookie->type = USER_ACTION_COOKIE_IPFIX;
2026 /* Compose SAMPLE action for IPFIX bridge sampling. */
2028 compose_ipfix_action(const struct xbridge *xbridge,
2029 struct ofpbuf *odp_actions,
2030 const struct flow *flow)
2032 uint32_t probability;
2033 union user_action_cookie cookie;
2035 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
2039 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
2040 compose_ipfix_cookie(&cookie);
2042 compose_sample_action(xbridge, odp_actions, flow, probability,
2043 &cookie, sizeof cookie.ipfix);
2046 /* SAMPLE action for sFlow must be first action in any given list of
2047 * actions. At this point we do not have all information required to
2048 * build it. So try to build sample action as complete as possible. */
2050 add_sflow_action(struct xlate_ctx *ctx)
2052 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
2053 &ctx->xout->odp_actions,
2054 &ctx->xin->flow, ODPP_NONE);
2055 ctx->sflow_odp_port = 0;
2056 ctx->sflow_n_outputs = 0;
2059 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
2060 * of actions, eventually after the SAMPLE action for sFlow. */
2062 add_ipfix_action(struct xlate_ctx *ctx)
2064 compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions,
2068 /* Fix SAMPLE action according to data collected while composing ODP actions.
2069 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
2070 * USERSPACE action's user-cookie which is required for sflow. */
2072 fix_sflow_action(struct xlate_ctx *ctx)
2074 const struct flow *base = &ctx->base_flow;
2075 union user_action_cookie *cookie;
2077 if (!ctx->user_cookie_offset) {
2081 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
2082 sizeof cookie->sflow);
2083 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2085 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
2086 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
2089 static enum slow_path_reason
2090 process_special(struct xlate_ctx *ctx, const struct flow *flow,
2091 const struct xport *xport, const struct ofpbuf *packet)
2093 struct flow_wildcards *wc = &ctx->xout->wc;
2094 const struct xbridge *xbridge = ctx->xbridge;
2098 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
2100 cfm_process_heartbeat(xport->cfm, packet);
2103 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
2105 bfd_process_packet(xport->bfd, flow, packet);
2106 /* If POLL received, immediately sends FINAL back. */
2107 if (bfd_should_send_packet(xport->bfd)) {
2108 ofproto_dpif_monitor_port_send_soon(xport->ofport);
2112 } else if (xport->xbundle && xport->xbundle->lacp
2113 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2115 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
2118 } else if (xbridge->stp && stp_should_process_flow(flow, wc)) {
2120 stp_process_packet(xport, packet);
2129 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
2132 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
2133 struct flow_wildcards *wc = &ctx->xout->wc;
2134 struct flow *flow = &ctx->xin->flow;
2135 ovs_be16 flow_vlan_tci;
2136 uint32_t flow_pkt_mark;
2137 uint8_t flow_nw_tos;
2138 odp_port_t out_port, odp_port;
2141 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2142 * before traversing a patch port. */
2143 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 27);
2146 xlate_report(ctx, "Nonexistent output port");
2148 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
2149 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2151 } else if (check_stp) {
2152 if (is_stp(&ctx->base_flow)) {
2153 if (!xport_stp_listen_state(xport)) {
2154 xlate_report(ctx, "STP not in listening state, "
2155 "skipping bpdu output");
2158 } else if (!xport_stp_forward_state(xport)) {
2159 xlate_report(ctx, "STP not in forwarding state, "
2165 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
2166 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
2171 const struct xport *peer = xport->peer;
2172 struct flow old_flow = ctx->xin->flow;
2173 enum slow_path_reason special;
2175 ctx->xbridge = peer->xbridge;
2176 flow->in_port.ofp_port = peer->ofp_port;
2177 flow->metadata = htonll(0);
2178 memset(&flow->tunnel, 0, sizeof flow->tunnel);
2179 memset(flow->regs, 0, sizeof flow->regs);
2181 special = process_special(ctx, &ctx->xin->flow, peer,
2184 ctx->xout->slow |= special;
2185 } else if (may_receive(peer, ctx)) {
2186 if (xport_stp_forward_state(peer)) {
2187 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2189 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
2190 * learning action look at the packet, then drop it. */
2191 struct flow old_base_flow = ctx->base_flow;
2192 size_t old_size = ofpbuf_size(&ctx->xout->odp_actions);
2193 mirror_mask_t old_mirrors = ctx->xout->mirrors;
2194 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2195 ctx->xout->mirrors = old_mirrors;
2196 ctx->base_flow = old_base_flow;
2197 ofpbuf_set_size(&ctx->xout->odp_actions, old_size);
2201 ctx->xin->flow = old_flow;
2202 ctx->xbridge = xport->xbridge;
2204 if (ctx->xin->resubmit_stats) {
2205 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2206 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
2208 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
2211 if (ctx->xin->xcache) {
2212 struct xc_entry *entry;
2214 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2215 entry->u.dev.tx = netdev_ref(xport->netdev);
2216 entry->u.dev.rx = netdev_ref(peer->netdev);
2217 entry->u.dev.bfd = bfd_ref(peer->bfd);
2222 flow_vlan_tci = flow->vlan_tci;
2223 flow_pkt_mark = flow->pkt_mark;
2224 flow_nw_tos = flow->nw_tos;
2226 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
2227 wc->masks.nw_tos |= IP_DSCP_MASK;
2228 flow->nw_tos &= ~IP_DSCP_MASK;
2229 flow->nw_tos |= dscp;
2232 if (xport->is_tunnel) {
2233 /* Save tunnel metadata so that changes made due to
2234 * the Logical (tunnel) Port are not visible for any further
2235 * matches, while explicit set actions on tunnel metadata are.
2237 struct flow_tnl flow_tnl = flow->tunnel;
2238 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
2239 if (odp_port == ODPP_NONE) {
2240 xlate_report(ctx, "Tunneling decided against output");
2241 goto out; /* restore flow_nw_tos */
2243 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
2244 xlate_report(ctx, "Not tunneling to our own address");
2245 goto out; /* restore flow_nw_tos */
2247 if (ctx->xin->resubmit_stats) {
2248 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2250 if (ctx->xin->xcache) {
2251 struct xc_entry *entry;
2253 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2254 entry->u.dev.tx = netdev_ref(xport->netdev);
2256 out_port = odp_port;
2257 commit_odp_tunnel_action(flow, &ctx->base_flow,
2258 &ctx->xout->odp_actions);
2259 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2261 odp_port = xport->odp_port;
2262 out_port = odp_port;
2263 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
2264 ofp_port_t vlandev_port;
2266 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2267 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
2268 ofp_port, flow->vlan_tci);
2269 if (vlandev_port != ofp_port) {
2270 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
2271 flow->vlan_tci = htons(0);
2276 if (out_port != ODPP_NONE) {
2277 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
2278 &ctx->xout->odp_actions,
2281 if (ctx->use_recirc) {
2282 struct ovs_action_hash *act_hash;
2283 struct xlate_recirc *xr = &ctx->recirc;
2286 act_hash = nl_msg_put_unspec_uninit(&ctx->xout->odp_actions,
2287 OVS_ACTION_ATTR_HASH,
2289 act_hash->hash_alg = xr->hash_alg;
2290 act_hash->hash_basis = xr->hash_basis;
2292 /* Recirc action. */
2293 nl_msg_put_u32(&ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC,
2296 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
2300 ctx->sflow_odp_port = odp_port;
2301 ctx->sflow_n_outputs++;
2302 ctx->xout->nf_output_iface = ofp_port;
2307 flow->vlan_tci = flow_vlan_tci;
2308 flow->pkt_mark = flow_pkt_mark;
2309 flow->nw_tos = flow_nw_tos;
2313 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
2315 compose_output_action__(ctx, ofp_port, true);
2319 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
2321 struct rule_dpif *old_rule = ctx->rule;
2322 const struct rule_actions *actions;
2324 if (ctx->xin->resubmit_stats) {
2325 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
2331 actions = rule_dpif_get_actions(rule);
2332 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
2333 ctx->rule = old_rule;
2338 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
2340 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2342 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
2343 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
2344 MAX_RESUBMIT_RECURSION);
2345 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
2346 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
2347 } else if (ofpbuf_size(&ctx->xout->odp_actions) > UINT16_MAX) {
2348 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
2349 } else if (ofpbuf_size(&ctx->stack) >= 65536) {
2350 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
2359 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
2360 bool may_packet_in, bool honor_table_miss)
2362 if (xlate_resubmit_resource_check(ctx)) {
2363 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
2364 bool skip_wildcards = ctx->xin->skip_wildcards;
2365 uint8_t old_table_id = ctx->table_id;
2366 struct rule_dpif *rule;
2367 enum rule_dpif_lookup_verdict verdict;
2368 enum ofputil_port_config config = 0;
2370 ctx->table_id = table_id;
2372 /* Look up a flow with 'in_port' as the input port. Then restore the
2373 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2374 * have surprising behavior). */
2375 ctx->xin->flow.in_port.ofp_port = in_port;
2376 verdict = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
2379 ? &ctx->xout->wc : NULL,
2381 &ctx->table_id, &rule,
2382 ctx->xin->xcache != NULL,
2383 ctx->xin->resubmit_stats);
2384 ctx->xin->flow.in_port.ofp_port = old_in_port;
2386 if (ctx->xin->resubmit_hook) {
2387 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
2391 case RULE_DPIF_LOOKUP_VERDICT_MATCH:
2393 case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER:
2394 if (may_packet_in) {
2395 struct xport *xport;
2397 xport = get_ofp_port(ctx->xbridge,
2398 ctx->xin->flow.in_port.ofp_port);
2399 config = xport ? xport->config : 0;
2402 /* Fall through to drop */
2403 case RULE_DPIF_LOOKUP_VERDICT_DROP:
2404 config = OFPUTIL_PC_NO_PACKET_IN;
2406 case RULE_DPIF_LOOKUP_VERDICT_DEFAULT:
2407 if (!ofproto_dpif_wants_packet_in_on_miss(ctx->xbridge->ofproto)) {
2408 config = OFPUTIL_PC_NO_PACKET_IN;
2415 choose_miss_rule(config, ctx->xbridge->miss_rule,
2416 ctx->xbridge->no_packet_in_rule, &rule,
2417 ctx->xin->xcache != NULL);
2421 /* Fill in the cache entry here instead of xlate_recursively
2422 * to make the reference counting more explicit. We take a
2423 * reference in the lookups above if we are going to cache the
2425 if (ctx->xin->xcache) {
2426 struct xc_entry *entry;
2428 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
2429 entry->u.rule = rule;
2431 xlate_recursively(ctx, rule);
2434 ctx->table_id = old_table_id;
2442 xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
2443 struct ofputil_bucket *bucket)
2445 if (ctx->xin->resubmit_stats) {
2446 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
2448 if (ctx->xin->xcache) {
2449 struct xc_entry *entry;
2451 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
2452 entry->u.group.group = group_dpif_ref(group);
2453 entry->u.group.bucket = bucket;
2458 xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
2460 uint64_t action_list_stub[1024 / 8];
2461 struct ofpbuf action_list, action_set;
2463 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
2464 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2466 ofpacts_execute_action_set(&action_list, &action_set);
2468 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
2471 ofpbuf_uninit(&action_set);
2472 ofpbuf_uninit(&action_list);
2476 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
2478 struct ofputil_bucket *bucket;
2479 const struct list *buckets;
2480 struct flow old_flow = ctx->xin->flow;
2482 group_dpif_get_buckets(group, &buckets);
2484 LIST_FOR_EACH (bucket, list_node, buckets) {
2485 xlate_group_bucket(ctx, bucket);
2486 /* Roll back flow to previous state.
2487 * This is equivalent to cloning the packet for each bucket.
2489 * As a side effect any subsequently applied actions will
2490 * also effectively be applied to a clone of the packet taken
2491 * just before applying the all or indirect group. */
2492 ctx->xin->flow = old_flow;
2494 xlate_group_stats(ctx, group, NULL);
2498 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
2500 struct ofputil_bucket *bucket;
2502 bucket = group_first_live_bucket(ctx, group, 0);
2504 xlate_group_bucket(ctx, bucket);
2505 xlate_group_stats(ctx, group, bucket);
2510 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
2512 struct flow_wildcards *wc = &ctx->xout->wc;
2513 struct ofputil_bucket *bucket;
2516 basis = hash_mac(ctx->xin->flow.dl_dst, 0, 0);
2517 bucket = group_best_live_bucket(ctx, group, basis);
2519 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2520 xlate_group_bucket(ctx, bucket);
2521 xlate_group_stats(ctx, group, bucket);
2526 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
2528 ctx->in_group = true;
2530 switch (group_dpif_get_type(group)) {
2532 case OFPGT11_INDIRECT:
2533 xlate_all_group(ctx, group);
2535 case OFPGT11_SELECT:
2536 xlate_select_group(ctx, group);
2539 xlate_ff_group(ctx, group);
2544 group_dpif_unref(group);
2546 ctx->in_group = false;
2550 xlate_group_resource_check(struct xlate_ctx *ctx)
2552 if (!xlate_resubmit_resource_check(ctx)) {
2554 } else if (ctx->in_group) {
2555 /* Prevent nested translation of OpenFlow groups.
2557 * OpenFlow allows this restriction. We enforce this restriction only
2558 * because, with the current architecture, we would otherwise have to
2559 * take a possibly recursive read lock on the ofgroup rwlock, which is
2560 * unsafe given that POSIX allows taking a read lock to block if there
2561 * is a thread blocked on taking the write lock. Other solutions
2562 * without this restriction are also possible, but seem unwarranted
2563 * given the current limited use of groups. */
2564 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2566 VLOG_ERR_RL(&rl, "cannot recursively translate OpenFlow group");
2574 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
2576 if (xlate_group_resource_check(ctx)) {
2577 struct group_dpif *group;
2580 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
2582 xlate_group_action__(ctx, group);
2592 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
2593 const struct ofpact_resubmit *resubmit)
2597 bool may_packet_in = false;
2598 bool honor_table_miss = false;
2600 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
2601 /* Still allow missed packets to be sent to the controller
2602 * if resubmitting from an internal table. */
2603 may_packet_in = true;
2604 honor_table_miss = true;
2607 in_port = resubmit->in_port;
2608 if (in_port == OFPP_IN_PORT) {
2609 in_port = ctx->xin->flow.in_port.ofp_port;
2612 table_id = resubmit->table_id;
2613 if (table_id == 255) {
2614 table_id = ctx->table_id;
2617 xlate_table_action(ctx, in_port, table_id, may_packet_in,
2622 flood_packets(struct xlate_ctx *ctx, bool all)
2624 const struct xport *xport;
2626 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
2627 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
2632 compose_output_action__(ctx, xport->ofp_port, false);
2633 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
2634 compose_output_action(ctx, xport->ofp_port);
2638 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2642 execute_controller_action(struct xlate_ctx *ctx, int len,
2643 enum ofp_packet_in_reason reason,
2644 uint16_t controller_id)
2646 struct ofproto_packet_in *pin;
2647 struct dpif_packet *packet;
2648 struct pkt_metadata md = PKT_METADATA_INITIALIZER(0);
2650 ctx->xout->slow |= SLOW_CONTROLLER;
2651 if (!ctx->xin->packet) {
2655 packet = dpif_packet_clone_from_ofpbuf(ctx->xin->packet);
2657 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2658 &ctx->xout->odp_actions,
2661 odp_execute_actions(NULL, &packet, 1, false, &md,
2662 ofpbuf_data(&ctx->xout->odp_actions),
2663 ofpbuf_size(&ctx->xout->odp_actions), NULL);
2665 pin = xmalloc(sizeof *pin);
2666 pin->up.packet_len = ofpbuf_size(&packet->ofpbuf);
2667 pin->up.packet = ofpbuf_steal_data(&packet->ofpbuf);
2668 pin->up.reason = reason;
2669 pin->up.table_id = ctx->table_id;
2670 pin->up.cookie = (ctx->rule
2671 ? rule_dpif_get_flow_cookie(ctx->rule)
2674 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
2676 pin->controller_id = controller_id;
2677 pin->send_len = len;
2678 /* If a rule is a table-miss rule then this is
2679 * a table-miss handled by a table-miss rule.
2681 * Else, if rule is internal and has a controller action,
2682 * the later being implied by the rule being processed here,
2683 * then this is a table-miss handled without a table-miss rule.
2685 * Otherwise this is not a table-miss. */
2686 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
2688 if (rule_dpif_is_table_miss(ctx->rule)) {
2689 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
2690 } else if (rule_dpif_is_internal(ctx->rule)) {
2691 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
2694 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
2695 dpif_packet_delete(packet);
2699 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
2701 struct flow_wildcards *wc = &ctx->xout->wc;
2702 struct flow *flow = &ctx->xin->flow;
2705 ovs_assert(eth_type_mpls(mpls->ethertype));
2707 n = flow_count_mpls_labels(flow, wc);
2709 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
2710 &ctx->xout->odp_actions,
2712 } else if (n >= FLOW_MAX_MPLS_LABELS) {
2713 if (ctx->xin->packet != NULL) {
2714 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2715 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
2716 "MPLS push action can't be performed as it would "
2717 "have more MPLS LSEs than the %d supported.",
2718 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
2722 } else if (n >= ctx->xbridge->max_mpls_depth) {
2723 COVERAGE_INC(xlate_actions_mpls_overflow);
2724 ctx->xout->slow |= SLOW_ACTION;
2727 flow_push_mpls(flow, n, mpls->ethertype, wc);
2731 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
2733 struct flow_wildcards *wc = &ctx->xout->wc;
2734 struct flow *flow = &ctx->xin->flow;
2735 int n = flow_count_mpls_labels(flow, wc);
2737 if (!flow_pop_mpls(flow, n, eth_type, wc) && n >= FLOW_MAX_MPLS_LABELS) {
2738 if (ctx->xin->packet != NULL) {
2739 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2740 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
2741 "MPLS pop action can't be performed as it has "
2742 "more MPLS LSEs than the %d supported.",
2743 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
2746 ofpbuf_clear(&ctx->xout->odp_actions);
2751 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
2753 struct flow *flow = &ctx->xin->flow;
2755 if (!is_ip_any(flow)) {
2759 ctx->xout->wc.masks.nw_ttl = 0xff;
2760 if (flow->nw_ttl > 1) {
2766 for (i = 0; i < ids->n_controllers; i++) {
2767 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
2771 /* Stop processing for current table. */
2777 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
2779 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2780 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
2781 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
2786 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
2788 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2789 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
2790 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
2795 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
2797 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2798 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
2799 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
2804 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
2806 struct flow *flow = &ctx->xin->flow;
2807 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
2808 struct flow_wildcards *wc = &ctx->xout->wc;
2810 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2811 if (eth_type_mpls(flow->dl_type)) {
2814 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
2817 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
2819 /* Stop processing for current table. */
2828 xlate_output_action(struct xlate_ctx *ctx,
2829 ofp_port_t port, uint16_t max_len, bool may_packet_in)
2831 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
2833 ctx->xout->nf_output_iface = NF_OUT_DROP;
2837 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
2840 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2841 0, may_packet_in, true);
2847 flood_packets(ctx, false);
2850 flood_packets(ctx, true);
2852 case OFPP_CONTROLLER:
2853 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
2859 if (port != ctx->xin->flow.in_port.ofp_port) {
2860 compose_output_action(ctx, port);
2862 xlate_report(ctx, "skipping output to input port");
2867 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2868 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2869 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2870 ctx->xout->nf_output_iface = prev_nf_output_iface;
2871 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2872 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2873 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2878 xlate_output_reg_action(struct xlate_ctx *ctx,
2879 const struct ofpact_output_reg *or)
2881 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
2882 if (port <= UINT16_MAX) {
2883 union mf_subvalue value;
2885 memset(&value, 0xff, sizeof value);
2886 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
2887 xlate_output_action(ctx, u16_to_ofp(port),
2888 or->max_len, false);
2893 xlate_enqueue_action(struct xlate_ctx *ctx,
2894 const struct ofpact_enqueue *enqueue)
2896 ofp_port_t ofp_port = enqueue->port;
2897 uint32_t queue_id = enqueue->queue;
2898 uint32_t flow_priority, priority;
2901 /* Translate queue to priority. */
2902 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
2904 /* Fall back to ordinary output action. */
2905 xlate_output_action(ctx, enqueue->port, 0, false);
2909 /* Check output port. */
2910 if (ofp_port == OFPP_IN_PORT) {
2911 ofp_port = ctx->xin->flow.in_port.ofp_port;
2912 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
2916 /* Add datapath actions. */
2917 flow_priority = ctx->xin->flow.skb_priority;
2918 ctx->xin->flow.skb_priority = priority;
2919 compose_output_action(ctx, ofp_port);
2920 ctx->xin->flow.skb_priority = flow_priority;
2922 /* Update NetFlow output port. */
2923 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2924 ctx->xout->nf_output_iface = ofp_port;
2925 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2926 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2931 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
2933 uint32_t skb_priority;
2935 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
2936 ctx->xin->flow.skb_priority = skb_priority;
2938 /* Couldn't translate queue to a priority. Nothing to do. A warning
2939 * has already been logged. */
2944 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
2946 const struct xbridge *xbridge = xbridge_;
2957 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
2960 port = get_ofp_port(xbridge, ofp_port);
2961 return port ? port->may_enable : false;
2966 xlate_bundle_action(struct xlate_ctx *ctx,
2967 const struct ofpact_bundle *bundle)
2971 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
2973 CONST_CAST(struct xbridge *, ctx->xbridge));
2974 if (bundle->dst.field) {
2975 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
2978 xlate_output_action(ctx, port, 0, false);
2983 xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
2984 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
2986 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
2987 if (ctx->xin->may_learn) {
2988 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
2993 xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
2995 ctx->xout->has_learn = true;
2996 learn_mask(learn, &ctx->xout->wc);
2998 if (ctx->xin->xcache) {
2999 struct xc_entry *entry;
3001 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
3002 entry->u.learn.ofproto = ctx->xbridge->ofproto;
3003 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
3004 entry->u.learn.ofpacts = ofpbuf_new(64);
3005 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
3006 entry->u.learn.ofpacts);
3007 } else if (ctx->xin->may_learn) {
3008 uint64_t ofpacts_stub[1024 / 8];
3009 struct ofputil_flow_mod fm;
3010 struct ofpbuf ofpacts;
3012 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3013 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
3014 ofpbuf_uninit(&ofpacts);
3019 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
3020 uint16_t idle_timeout, uint16_t hard_timeout)
3022 if (tcp_flags & (TCP_FIN | TCP_RST)) {
3023 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
3028 xlate_fin_timeout(struct xlate_ctx *ctx,
3029 const struct ofpact_fin_timeout *oft)
3032 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
3033 oft->fin_idle_timeout, oft->fin_hard_timeout);
3034 if (ctx->xin->xcache) {
3035 struct xc_entry *entry;
3037 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
3038 /* XC_RULE already holds a reference on the rule, none is taken
3040 entry->u.fin.rule = ctx->rule;
3041 entry->u.fin.idle = oft->fin_idle_timeout;
3042 entry->u.fin.hard = oft->fin_hard_timeout;
3048 xlate_sample_action(struct xlate_ctx *ctx,
3049 const struct ofpact_sample *os)
3051 union user_action_cookie cookie;
3052 /* Scale the probability from 16-bit to 32-bit while representing
3053 * the same percentage. */
3054 uint32_t probability = (os->probability << 16) | os->probability;
3056 if (!ctx->xbridge->variable_length_userdata) {
3057 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3059 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
3060 "lacks support (needs Linux 3.10+ or kernel module from "
3065 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3066 &ctx->xout->odp_actions,
3069 compose_flow_sample_cookie(os->probability, os->collector_set_id,
3070 os->obs_domain_id, os->obs_point_id, &cookie);
3071 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
3072 probability, &cookie, sizeof cookie.flow_sample);
3076 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
3078 if (xport->config & (is_stp(&ctx->xin->flow)
3079 ? OFPUTIL_PC_NO_RECV_STP
3080 : OFPUTIL_PC_NO_RECV)) {
3084 /* Only drop packets here if both forwarding and learning are
3085 * disabled. If just learning is enabled, we need to have
3086 * OFPP_NORMAL and the learning action have a look at the packet
3087 * before we can drop it. */
3088 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
3096 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
3098 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3099 ofpbuf_put(&ctx->action_set, on->actions, ofpact_nest_get_action_len(on));
3100 ofpact_pad(&ctx->action_set);
3104 xlate_action_set(struct xlate_ctx *ctx)
3106 uint64_t action_list_stub[1024 / 64];
3107 struct ofpbuf action_list;
3109 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3110 ofpacts_execute_action_set(&action_list, &ctx->action_set);
3111 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
3112 ofpbuf_uninit(&action_list);
3116 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
3117 struct xlate_ctx *ctx)
3119 struct flow_wildcards *wc = &ctx->xout->wc;
3120 struct flow *flow = &ctx->xin->flow;
3121 const struct ofpact *a;
3123 /* dl_type already in the mask, not set below. */
3125 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3126 struct ofpact_controller *controller;
3127 const struct ofpact_metadata *metadata;
3128 const struct ofpact_set_field *set_field;
3129 const struct mf_field *mf;
3137 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
3138 ofpact_get_OUTPUT(a)->max_len, true);
3142 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
3147 case OFPACT_CONTROLLER:
3148 controller = ofpact_get_CONTROLLER(a);
3149 execute_controller_action(ctx, controller->max_len,
3151 controller->controller_id);
3154 case OFPACT_ENQUEUE:
3155 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
3158 case OFPACT_SET_VLAN_VID:
3159 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
3160 if (flow->vlan_tci & htons(VLAN_CFI) ||
3161 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
3162 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
3163 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
3168 case OFPACT_SET_VLAN_PCP:
3169 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
3170 if (flow->vlan_tci & htons(VLAN_CFI) ||
3171 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
3172 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
3173 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
3174 << VLAN_PCP_SHIFT) | VLAN_CFI);
3178 case OFPACT_STRIP_VLAN:
3179 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3180 flow->vlan_tci = htons(0);
3183 case OFPACT_PUSH_VLAN:
3184 /* XXX 802.1AD(QinQ) */
3185 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3186 flow->vlan_tci = htons(VLAN_CFI);
3189 case OFPACT_SET_ETH_SRC:
3190 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3191 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
3194 case OFPACT_SET_ETH_DST:
3195 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3196 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
3199 case OFPACT_SET_IPV4_SRC:
3200 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3201 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3202 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
3206 case OFPACT_SET_IPV4_DST:
3207 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3208 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3209 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
3213 case OFPACT_SET_IP_DSCP:
3214 if (is_ip_any(flow)) {
3215 wc->masks.nw_tos |= IP_DSCP_MASK;
3216 flow->nw_tos &= ~IP_DSCP_MASK;
3217 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
3221 case OFPACT_SET_IP_ECN:
3222 if (is_ip_any(flow)) {
3223 wc->masks.nw_tos |= IP_ECN_MASK;
3224 flow->nw_tos &= ~IP_ECN_MASK;
3225 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
3229 case OFPACT_SET_IP_TTL:
3230 if (is_ip_any(flow)) {
3231 wc->masks.nw_ttl = 0xff;
3232 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
3236 case OFPACT_SET_L4_SRC_PORT:
3237 if (is_ip_any(flow)) {
3238 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3239 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3240 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
3244 case OFPACT_SET_L4_DST_PORT:
3245 if (is_ip_any(flow)) {
3246 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3247 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3248 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
3252 case OFPACT_RESUBMIT:
3253 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
3256 case OFPACT_SET_TUNNEL:
3257 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
3260 case OFPACT_SET_QUEUE:
3261 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
3264 case OFPACT_POP_QUEUE:
3265 flow->skb_priority = ctx->orig_skb_priority;
3268 case OFPACT_REG_MOVE:
3269 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
3272 case OFPACT_REG_LOAD:
3273 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow, wc);
3276 case OFPACT_SET_FIELD:
3277 set_field = ofpact_get_SET_FIELD(a);
3278 mf = set_field->field;
3280 /* Set field action only ever overwrites packet's outermost
3281 * applicable header fields. Do nothing if no header exists. */
3282 if (mf->id == MFF_VLAN_VID) {
3283 wc->masks.vlan_tci |= htons(VLAN_CFI);
3284 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
3287 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
3288 /* 'dl_type' is already unwildcarded. */
3289 && !eth_type_mpls(flow->dl_type)) {
3293 mf_mask_field_and_prereqs(mf, &wc->masks);
3294 mf_set_flow_value(mf, &set_field->value, flow);
3297 case OFPACT_STACK_PUSH:
3298 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
3302 case OFPACT_STACK_POP:
3303 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
3307 case OFPACT_PUSH_MPLS:
3308 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
3311 case OFPACT_POP_MPLS:
3312 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
3315 case OFPACT_SET_MPLS_LABEL:
3316 compose_set_mpls_label_action(
3317 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
3320 case OFPACT_SET_MPLS_TC:
3321 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
3324 case OFPACT_SET_MPLS_TTL:
3325 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
3328 case OFPACT_DEC_MPLS_TTL:
3329 if (compose_dec_mpls_ttl_action(ctx)) {
3334 case OFPACT_DEC_TTL:
3335 wc->masks.nw_ttl = 0xff;
3336 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
3342 /* Nothing to do. */
3345 case OFPACT_MULTIPATH:
3346 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
3350 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
3353 case OFPACT_OUTPUT_REG:
3354 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
3358 xlate_learn_action(ctx, ofpact_get_LEARN(a));
3365 case OFPACT_FIN_TIMEOUT:
3366 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3367 ctx->xout->has_fin_timeout = true;
3368 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
3371 case OFPACT_CLEAR_ACTIONS:
3372 ofpbuf_clear(&ctx->action_set);
3375 case OFPACT_WRITE_ACTIONS:
3376 xlate_write_actions(ctx, a);
3379 case OFPACT_WRITE_METADATA:
3380 metadata = ofpact_get_WRITE_METADATA(a);
3381 flow->metadata &= ~metadata->mask;
3382 flow->metadata |= metadata->metadata & metadata->mask;
3386 /* Not implemented yet. */
3389 case OFPACT_GOTO_TABLE: {
3390 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
3392 ovs_assert(ctx->table_id < ogt->table_id);
3393 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3394 ogt->table_id, true, true);
3399 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
3406 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
3407 const struct flow *flow, struct rule_dpif *rule,
3408 uint16_t tcp_flags, const struct ofpbuf *packet)
3410 xin->ofproto = ofproto;
3412 xin->packet = packet;
3413 xin->may_learn = packet != NULL;
3416 xin->ofpacts = NULL;
3417 xin->ofpacts_len = 0;
3418 xin->tcp_flags = tcp_flags;
3419 xin->resubmit_hook = NULL;
3420 xin->report_hook = NULL;
3421 xin->resubmit_stats = NULL;
3422 xin->skip_wildcards = false;
3426 xlate_out_uninit(struct xlate_out *xout)
3429 ofpbuf_uninit(&xout->odp_actions);
3433 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
3434 * into datapath actions, using 'ctx', and discards the datapath actions. */
3436 xlate_actions_for_side_effects(struct xlate_in *xin)
3438 struct xlate_out xout;
3440 xlate_actions(xin, &xout);
3441 xlate_out_uninit(&xout);
3445 xlate_report(struct xlate_ctx *ctx, const char *s)
3447 if (ctx->xin->report_hook) {
3448 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
3453 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
3456 dst->slow = src->slow;
3457 dst->has_learn = src->has_learn;
3458 dst->has_normal = src->has_normal;
3459 dst->has_fin_timeout = src->has_fin_timeout;
3460 dst->nf_output_iface = src->nf_output_iface;
3461 dst->mirrors = src->mirrors;
3463 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
3464 sizeof dst->odp_actions_stub);
3465 ofpbuf_put(&dst->odp_actions, ofpbuf_data(&src->odp_actions),
3466 ofpbuf_size(&src->odp_actions));
3469 static struct skb_priority_to_dscp *
3470 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
3472 struct skb_priority_to_dscp *pdscp;
3475 hash = hash_int(skb_priority, 0);
3476 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
3477 if (pdscp->skb_priority == skb_priority) {
3485 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
3488 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
3489 *dscp = pdscp ? pdscp->dscp : 0;
3490 return pdscp != NULL;
3494 clear_skb_priorities(struct xport *xport)
3496 struct skb_priority_to_dscp *pdscp, *next;
3498 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
3499 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
3505 actions_output_to_local_port(const struct xlate_ctx *ctx)
3507 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
3508 const struct nlattr *a;
3511 NL_ATTR_FOR_EACH_UNSAFE (a, left, ofpbuf_data(&ctx->xout->odp_actions),
3512 ofpbuf_size(&ctx->xout->odp_actions)) {
3513 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
3514 && nl_attr_get_odp_port(a) == local_odp_port) {
3521 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
3522 * into datapath actions in 'odp_actions', using 'ctx'.
3524 * The caller must take responsibility for eventually freeing 'xout', with
3525 * xlate_out_uninit(). */
3527 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
3529 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
3530 struct flow_wildcards *wc = &xout->wc;
3531 struct flow *flow = &xin->flow;
3532 struct rule_dpif *rule = NULL;
3534 const struct rule_actions *actions = NULL;
3535 enum slow_path_reason special;
3536 const struct ofpact *ofpacts;
3537 struct xport *in_port;
3538 struct flow orig_flow;
3539 struct xlate_ctx ctx;
3544 COVERAGE_INC(xlate_actions);
3546 /* Flow initialization rules:
3547 * - 'base_flow' must match the kernel's view of the packet at the
3548 * time that action processing starts. 'flow' represents any
3549 * transformations we wish to make through actions.
3550 * - By default 'base_flow' and 'flow' are the same since the input
3551 * packet matches the output before any actions are applied.
3552 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
3553 * of the received packet as seen by the kernel. If we later output
3554 * to another device without any modifications this will cause us to
3555 * insert a new tag since the original one was stripped off by the
3557 * - Tunnel metadata as received is retained in 'flow'. This allows
3558 * tunnel metadata matching also in later tables.
3559 * Since a kernel action for setting the tunnel metadata will only be
3560 * generated with actual tunnel output, changing the tunnel metadata
3561 * values in 'flow' (such as tun_id) will only have effect with a later
3562 * tunnel output action.
3563 * - Tunnel 'base_flow' is completely cleared since that is what the
3564 * kernel does. If we wish to maintain the original values an action
3565 * needs to be generated. */
3570 ctx.xout->has_learn = false;
3571 ctx.xout->has_normal = false;
3572 ctx.xout->has_fin_timeout = false;
3573 ctx.xout->nf_output_iface = NF_OUT_DROP;
3574 ctx.xout->mirrors = 0;
3575 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
3576 sizeof ctx.xout->odp_actions_stub);
3577 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
3579 ctx.xbridge = xbridge_lookup(xcfg, xin->ofproto);
3584 ctx.rule = xin->rule;
3586 ctx.base_flow = *flow;
3587 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
3588 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
3590 flow_wildcards_init_catchall(wc);
3591 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
3592 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3593 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3594 if (is_ip_any(flow)) {
3595 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3597 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
3599 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
3600 if (ctx.xbridge->netflow) {
3601 netflow_mask_wc(flow, wc);
3606 ctx.in_group = false;
3607 ctx.orig_skb_priority = flow->skb_priority;
3610 ctx.use_recirc = false;
3612 if (!xin->ofpacts && !ctx.rule) {
3613 ctx.table_id = rule_dpif_lookup(ctx.xbridge->ofproto, flow,
3614 !xin->skip_wildcards ? wc : NULL,
3615 &rule, ctx.xin->xcache != NULL,
3616 ctx.xin->resubmit_stats);
3617 if (ctx.xin->resubmit_stats) {
3618 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
3620 if (ctx.xin->xcache) {
3621 struct xc_entry *entry;
3623 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
3624 entry->u.rule = rule;
3628 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
3631 ofpacts = xin->ofpacts;
3632 ofpacts_len = xin->ofpacts_len;
3633 } else if (ctx.rule) {
3634 actions = rule_dpif_get_actions(ctx.rule);
3635 ofpacts = actions->ofpacts;
3636 ofpacts_len = actions->ofpacts_len;
3641 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
3642 ofpbuf_use_stub(&ctx.action_set,
3643 ctx.action_set_stub, sizeof ctx.action_set_stub);
3645 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3646 /* Do this conditionally because the copy is expensive enough that it
3647 * shows up in profiles. */
3651 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3652 switch (ctx.xbridge->frag) {
3653 case OFPC_FRAG_NORMAL:
3654 /* We must pretend that transport ports are unavailable. */
3655 flow->tp_src = ctx.base_flow.tp_src = htons(0);
3656 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
3659 case OFPC_FRAG_DROP:
3662 case OFPC_FRAG_REASM:
3665 case OFPC_FRAG_NX_MATCH:
3666 /* Nothing to do. */
3669 case OFPC_INVALID_TTL_TO_CONTROLLER:
3674 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
3675 if (in_port && in_port->is_tunnel) {
3676 if (ctx.xin->resubmit_stats) {
3677 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
3679 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
3682 if (ctx.xin->xcache) {
3683 struct xc_entry *entry;
3685 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
3686 entry->u.dev.rx = netdev_ref(in_port->netdev);
3687 entry->u.dev.bfd = bfd_ref(in_port->bfd);
3691 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
3693 ctx.xout->slow |= special;
3695 size_t sample_actions_len;
3697 if (flow->in_port.ofp_port
3698 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
3699 flow->in_port.ofp_port,
3701 ctx.base_flow.vlan_tci = 0;
3704 add_sflow_action(&ctx);
3705 add_ipfix_action(&ctx);
3706 sample_actions_len = ofpbuf_size(&ctx.xout->odp_actions);
3708 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
3709 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
3711 /* We've let OFPP_NORMAL and the learning action look at the
3712 * packet, so drop it now if forwarding is disabled. */
3713 if (in_port && !xport_stp_forward_state(in_port)) {
3714 ofpbuf_set_size(&ctx.xout->odp_actions, sample_actions_len);
3718 if (ofpbuf_size(&ctx.action_set)) {
3719 xlate_action_set(&ctx);
3722 if (ctx.xbridge->has_in_band
3723 && in_band_must_output_to_local_port(flow)
3724 && !actions_output_to_local_port(&ctx)) {
3725 compose_output_action(&ctx, OFPP_LOCAL);
3728 fix_sflow_action(&ctx);
3730 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3731 add_mirror_actions(&ctx, &orig_flow);
3735 if (nl_attr_oversized(ofpbuf_size(&ctx.xout->odp_actions))) {
3736 /* These datapath actions are too big for a Netlink attribute, so we
3737 * can't hand them to the kernel directly. dpif_execute() can execute
3738 * them one by one with help, so just mark the result as SLOW_ACTION to
3739 * prevent the flow from being installed. */
3740 COVERAGE_INC(xlate_actions_oversize);
3741 ctx.xout->slow |= SLOW_ACTION;
3744 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3745 if (ctx.xin->resubmit_stats) {
3746 mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
3747 ctx.xin->resubmit_stats->n_packets,
3748 ctx.xin->resubmit_stats->n_bytes);
3750 if (ctx.xin->xcache) {
3751 struct xc_entry *entry;
3753 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_MIRROR);
3754 entry->u.mirror.mbridge = mbridge_ref(ctx.xbridge->mbridge);
3755 entry->u.mirror.mirrors = xout->mirrors;
3759 if (ctx.xbridge->netflow) {
3760 /* Only update netflow if we don't have controller flow. We don't
3761 * report NetFlow expiration messages for such facets because they
3762 * are just part of the control logic for the network, not real
3764 if (ofpacts_len == 0
3765 || ofpacts->type != OFPACT_CONTROLLER
3766 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
3767 if (ctx.xin->resubmit_stats) {
3768 netflow_flow_update(ctx.xbridge->netflow, flow,
3769 xout->nf_output_iface,
3770 ctx.xin->resubmit_stats);
3772 if (ctx.xin->xcache) {
3773 struct xc_entry *entry;
3775 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
3776 entry->u.nf.netflow = netflow_ref(ctx.xbridge->netflow);
3777 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
3778 entry->u.nf.iface = xout->nf_output_iface;
3783 ofpbuf_uninit(&ctx.stack);
3784 ofpbuf_uninit(&ctx.action_set);
3786 /* Clear the metadata and register wildcard masks, because we won't
3787 * use non-header fields as part of the cache. */
3788 flow_wildcards_clear_non_packet_fields(wc);
3790 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow uses
3791 * the low 8 bits of the 16-bit tp_src and tp_dst members to represent
3792 * these fields. The datapath interface, on the other hand, represents
3793 * them with just 8 bits each. This means that if the high 8 bits of the
3794 * masks for these fields somehow become set, then they will get chopped
3795 * off by a round trip through the datapath, and revalidation will spot
3796 * that as an inconsistency and delete the flow. Avoid the problem here by
3797 * making sure that only the low 8 bits of either field can be unwildcarded
3801 wc->masks.tp_src &= htons(UINT8_MAX);
3802 wc->masks.tp_dst &= htons(UINT8_MAX);
3806 /* Sends 'packet' out 'ofport'.
3807 * May modify 'packet'.
3808 * Returns 0 if successful, otherwise a positive errno value. */
3810 xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3812 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
3813 struct xport *xport;
3814 struct ofpact_output output;
3817 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
3818 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
3819 flow_extract(packet, NULL, &flow);
3820 flow.in_port.ofp_port = OFPP_NONE;
3822 xport = xport_lookup(xcfg, ofport);
3826 output.port = xport->ofp_port;
3829 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
3830 &output.ofpact, sizeof output,
3834 struct xlate_cache *
3835 xlate_cache_new(void)
3837 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
3839 ofpbuf_init(&xcache->entries, 512);
3843 static struct xc_entry *
3844 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
3846 struct xc_entry *entry;
3848 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
3855 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
3857 if (entry->u.dev.tx) {
3858 netdev_vport_inc_tx(entry->u.dev.tx, stats);
3860 if (entry->u.dev.rx) {
3861 netdev_vport_inc_rx(entry->u.dev.rx, stats);
3863 if (entry->u.dev.bfd) {
3864 bfd_account_rx(entry->u.dev.bfd, stats);
3869 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
3871 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
3872 struct xbridge *xbridge;
3873 struct xbundle *xbundle;
3874 struct flow_wildcards wc;
3876 xbridge = xbridge_lookup(xcfg, ofproto);
3881 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
3887 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
3890 /* Push stats and perform side effects of flow translation. */
3892 xlate_push_stats(struct xlate_cache *xcache, bool may_learn,
3893 const struct dpif_flow_stats *stats)
3895 struct xc_entry *entry;
3896 struct ofpbuf entries = xcache->entries;
3898 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
3899 switch (entry->type) {
3901 rule_dpif_credit_stats(entry->u.rule, stats);
3904 bond_account(entry->u.bond.bond, entry->u.bond.flow,
3905 entry->u.bond.vid, stats->n_bytes);
3908 xlate_cache_netdev(entry, stats);
3911 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
3912 entry->u.nf.iface, stats);
3915 mirror_update_stats(entry->u.mirror.mbridge,
3916 entry->u.mirror.mirrors,
3917 stats->n_packets, stats->n_bytes);
3921 ofproto_dpif_flow_mod(entry->u.learn.ofproto,
3926 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
3927 entry->u.normal.vlan);
3929 case XC_FIN_TIMEOUT:
3930 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
3931 entry->u.fin.idle, entry->u.fin.hard);
3934 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
3944 xlate_dev_unref(struct xc_entry *entry)
3946 if (entry->u.dev.tx) {
3947 netdev_close(entry->u.dev.tx);
3949 if (entry->u.dev.rx) {
3950 netdev_close(entry->u.dev.rx);
3952 if (entry->u.dev.bfd) {
3953 bfd_unref(entry->u.dev.bfd);
3958 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
3960 netflow_flow_clear(netflow, flow);
3961 netflow_unref(netflow);
3966 xlate_cache_clear(struct xlate_cache *xcache)
3968 struct xc_entry *entry;
3969 struct ofpbuf entries;
3975 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
3976 switch (entry->type) {
3978 rule_dpif_unref(entry->u.rule);
3981 free(entry->u.bond.flow);
3982 bond_unref(entry->u.bond.bond);
3985 xlate_dev_unref(entry);
3988 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
3991 mbridge_unref(entry->u.mirror.mbridge);
3994 free(entry->u.learn.fm);
3995 ofpbuf_delete(entry->u.learn.ofpacts);
3998 free(entry->u.normal.flow);
4000 case XC_FIN_TIMEOUT:
4001 /* 'u.fin.rule' is always already held as a XC_RULE, which
4002 * has already released it's reference above. */
4005 group_dpif_unref(entry->u.group.group);
4012 ofpbuf_clear(&xcache->entries);
4016 xlate_cache_delete(struct xlate_cache *xcache)
4018 xlate_cache_clear(xcache);
4019 ofpbuf_uninit(&xcache->entries);