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 "mcast-snooping.h"
37 #include "meta-flow.h"
38 #include "multipath.h"
39 #include "netdev-vport.h"
42 #include "odp-execute.h"
43 #include "ofp-actions.h"
44 #include "ofproto/ofproto-dpif-ipfix.h"
45 #include "ofproto/ofproto-dpif-mirror.h"
46 #include "ofproto/ofproto-dpif-monitor.h"
47 #include "ofproto/ofproto-dpif-sflow.h"
48 #include "ofproto/ofproto-dpif.h"
49 #include "ofproto/ofproto-provider.h"
50 #include "packet-dpif.h"
54 COVERAGE_DEFINE(xlate_actions);
55 COVERAGE_DEFINE(xlate_actions_oversize);
56 COVERAGE_DEFINE(xlate_actions_mpls_overflow);
58 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
60 /* Maximum depth of flow table recursion (due to resubmit actions) in a
61 * flow translation. */
62 #define MAX_RESUBMIT_RECURSION 64
63 #define MAX_INTERNAL_RESUBMITS 1 /* Max resbmits allowed using rules in
66 /* Timeout for internal rules created to handle recirculation */
67 #define RECIRC_TIMEOUT 60
69 /* Maximum number of resubmit actions in a flow translation, whether they are
70 * recursive or not. */
71 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
74 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
75 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
77 struct list xbundles; /* Owned xbundles. */
78 struct hmap xports; /* Indexed by ofp_port. */
80 char *name; /* Name used in log messages. */
81 struct dpif *dpif; /* Datapath interface. */
82 struct mac_learning *ml; /* Mac learning handle. */
83 struct mcast_snooping *ms; /* Multicast Snooping handle. */
84 struct mbridge *mbridge; /* Mirroring. */
85 struct dpif_sflow *sflow; /* SFlow handle, or null. */
86 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
87 struct netflow *netflow; /* Netflow handle, or null. */
88 struct stp *stp; /* STP or null if disabled. */
90 /* Special rules installed by ofproto-dpif. */
91 struct rule_dpif *miss_rule;
92 struct rule_dpif *no_packet_in_rule;
94 enum ofp_config_flags frag; /* Fragmentation handling. */
95 bool has_in_band; /* Bridge has in band control? */
96 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
98 /* True if the datapath supports recirculation. */
101 /* True if the datapath supports variable-length
102 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
103 * False if the datapath supports only 8-byte (or shorter) userdata. */
104 bool variable_length_userdata;
106 /* Number of MPLS label stack entries that the datapath supports
108 size_t max_mpls_depth;
112 struct hmap_node hmap_node; /* In global 'xbundles' map. */
113 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
115 struct list list_node; /* In parent 'xbridges' list. */
116 struct xbridge *xbridge; /* Parent xbridge. */
118 struct list xports; /* Contains "struct xport"s. */
120 char *name; /* Name used in log messages. */
121 struct bond *bond; /* Nonnull iff more than one port. */
122 struct lacp *lacp; /* LACP handle or null. */
124 enum port_vlan_mode vlan_mode; /* VLAN mode. */
125 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
126 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
127 * NULL if all VLANs are trunked. */
128 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
129 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
133 struct hmap_node hmap_node; /* Node in global 'xports' map. */
134 struct ofport_dpif *ofport; /* Key in global 'xports map. */
136 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
137 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
139 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
141 struct list bundle_node; /* In parent xbundle (if it exists). */
142 struct xbundle *xbundle; /* Parent xbundle or null. */
144 struct netdev *netdev; /* 'ofport''s netdev. */
146 struct xbridge *xbridge; /* Parent bridge. */
147 struct xport *peer; /* Patch port peer or null. */
149 enum ofputil_port_config config; /* OpenFlow port configuration. */
150 enum ofputil_port_state state; /* OpenFlow port state. */
151 int stp_port_no; /* STP port number or -1 if not in use. */
153 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
155 bool may_enable; /* May be enabled in bonds. */
156 bool is_tunnel; /* Is a tunnel port. */
158 struct cfm *cfm; /* CFM handle or null. */
159 struct bfd *bfd; /* BFD handle or null. */
163 struct xlate_in *xin;
164 struct xlate_out *xout;
166 const struct xbridge *xbridge;
168 /* Flow at the last commit. */
169 struct flow base_flow;
171 /* Tunnel IP destination address as received. This is stored separately
172 * as the base_flow.tunnel is cleared on init to reflect the datapath
173 * behavior. Used to make sure not to send tunneled output to ourselves,
174 * which might lead to an infinite loop. This could happen easily
175 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
176 * actually set the tun_dst field. */
177 ovs_be32 orig_tunnel_ip_dst;
179 /* Stack for the push and pop actions. Each stack element is of type
180 * "union mf_subvalue". */
181 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
184 /* The rule that we are currently translating, or NULL. */
185 struct rule_dpif *rule;
187 /* Resubmit statistics, via xlate_table_action(). */
188 int recurse; /* Current resubmit nesting depth. */
189 int resubmits; /* Total number of resubmits. */
190 bool in_group; /* Currently translating ofgroup, if true. */
192 uint32_t orig_skb_priority; /* Priority when packet arrived. */
193 uint8_t table_id; /* OpenFlow table ID where flow was found. */
194 uint32_t sflow_n_outputs; /* Number of output ports. */
195 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
196 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
197 bool exit; /* No further actions should be processed. */
199 bool use_recirc; /* Should generate recirc? */
200 struct xlate_recirc recirc; /* Information used for generating
201 * recirculation actions */
203 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
204 * This is a trigger for recirculation in cases where translating an action
205 * or looking up a flow requires access to the fields of the packet after
206 * the MPLS label stack that was originally present. */
209 /* OpenFlow 1.1+ action set.
211 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
212 * When translation is otherwise complete, ofpacts_execute_action_set()
213 * converts it to a set of "struct ofpact"s that can be translated into
214 * datapath actions. */
215 struct ofpbuf action_set; /* Action set. */
216 uint64_t action_set_stub[1024 / 8];
219 /* A controller may use OFPP_NONE as the ingress port to indicate that
220 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
221 * when an input bundle is needed for validation (e.g., mirroring or
222 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
223 * any 'port' structs, so care must be taken when dealing with it. */
224 static struct xbundle ofpp_none_bundle = {
226 .vlan_mode = PORT_VLAN_TRUNK
229 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
230 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
231 * traffic egressing the 'ofport' with that priority should be marked with. */
232 struct skb_priority_to_dscp {
233 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
234 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
236 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
251 /* xlate_cache entries hold enough information to perform the side effects of
252 * xlate_actions() for a rule, without needing to perform rule translation
253 * from scratch. The primary usage of these is to submit statistics to objects
254 * that a flow relates to, although they may be used for other effects as well
255 * (for instance, refreshing hard timeouts for learned flows). */
259 struct rule_dpif *rule;
266 struct netflow *netflow;
271 struct mbridge *mbridge;
272 mirror_mask_t mirrors;
280 struct ofproto_dpif *ofproto;
281 struct ofputil_flow_mod *fm;
282 struct ofpbuf *ofpacts;
285 struct ofproto_dpif *ofproto;
290 struct rule_dpif *rule;
295 struct group_dpif *group;
296 struct ofputil_bucket *bucket;
301 #define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
302 entries = xcache->entries; \
303 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
305 entry = ofpbuf_try_pull(&entries, sizeof *entry))
308 struct ofpbuf entries;
311 /* Xlate config contains hash maps of all bridges, bundles and ports.
312 * Xcfgp contains the pointer to the current xlate configuration.
313 * When the main thread needs to change the configuration, it copies xcfgp to
314 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
315 * does not block handler and revalidator threads. */
317 struct hmap xbridges;
318 struct hmap xbundles;
321 static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_TYPE_INITIALIZER;
322 static struct xlate_cfg *new_xcfg = NULL;
324 static bool may_receive(const struct xport *, struct xlate_ctx *);
325 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
327 static void xlate_normal(struct xlate_ctx *);
328 static void xlate_report(struct xlate_ctx *, const char *);
329 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
330 uint8_t table_id, bool may_packet_in,
331 bool honor_table_miss);
332 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
333 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
334 static void output_normal(struct xlate_ctx *, const struct xbundle *,
336 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
338 static struct xbridge *xbridge_lookup(struct xlate_cfg *,
339 const struct ofproto_dpif *);
340 static struct xbundle *xbundle_lookup(struct xlate_cfg *,
341 const struct ofbundle *);
342 static struct xport *xport_lookup(struct xlate_cfg *,
343 const struct ofport_dpif *);
344 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
345 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
346 uint32_t skb_priority);
347 static void clear_skb_priorities(struct xport *);
348 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
351 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
353 static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
354 static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
355 static void xlate_xport_init(struct xlate_cfg *, struct xport *);
356 static void xlate_xbridge_set(struct xbridge *xbridge,
358 struct rule_dpif *miss_rule,
359 struct rule_dpif *no_packet_in_rule,
360 const struct mac_learning *ml, struct stp *stp,
361 const struct mcast_snooping *ms,
362 const struct mbridge *mbridge,
363 const struct dpif_sflow *sflow,
364 const struct dpif_ipfix *ipfix,
365 const struct netflow *netflow,
366 enum ofp_config_flags frag,
367 bool forward_bpdu, bool has_in_band,
369 bool variable_length_userdata,
370 size_t max_mpls_depth);
371 static void xlate_xbundle_set(struct xbundle *xbundle,
372 enum port_vlan_mode vlan_mode, int vlan,
373 unsigned long *trunks, bool use_priority_tags,
374 const struct bond *bond, const struct lacp *lacp,
376 static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
377 const struct netdev *netdev, const struct cfm *cfm,
378 const struct bfd *bfd, int stp_port_no,
379 enum ofputil_port_config config,
380 enum ofputil_port_state state, bool is_tunnel,
382 static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
383 static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
384 static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
385 static void xlate_xbridge_copy(struct xbridge *);
386 static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
387 static void xlate_xport_copy(struct xbridge *, struct xbundle *,
389 static void xlate_xcfg_free(struct xlate_cfg *);
393 xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
395 list_init(&xbridge->xbundles);
396 hmap_init(&xbridge->xports);
397 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
398 hash_pointer(xbridge->ofproto, 0));
402 xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
404 list_init(&xbundle->xports);
405 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
406 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
407 hash_pointer(xbundle->ofbundle, 0));
411 xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
413 hmap_init(&xport->skb_priorities);
414 hmap_insert(&xcfg->xports, &xport->hmap_node,
415 hash_pointer(xport->ofport, 0));
416 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
417 hash_ofp_port(xport->ofp_port));
421 xlate_xbridge_set(struct xbridge *xbridge,
423 struct rule_dpif *miss_rule,
424 struct rule_dpif *no_packet_in_rule,
425 const struct mac_learning *ml, struct stp *stp,
426 const struct mcast_snooping *ms,
427 const struct mbridge *mbridge,
428 const struct dpif_sflow *sflow,
429 const struct dpif_ipfix *ipfix,
430 const struct netflow *netflow, enum ofp_config_flags frag,
431 bool forward_bpdu, bool has_in_band,
433 bool variable_length_userdata,
434 size_t max_mpls_depth)
436 if (xbridge->ml != ml) {
437 mac_learning_unref(xbridge->ml);
438 xbridge->ml = mac_learning_ref(ml);
441 if (xbridge->ms != ms) {
442 mcast_snooping_unref(xbridge->ms);
443 xbridge->ms = mcast_snooping_ref(ms);
446 if (xbridge->mbridge != mbridge) {
447 mbridge_unref(xbridge->mbridge);
448 xbridge->mbridge = mbridge_ref(mbridge);
451 if (xbridge->sflow != sflow) {
452 dpif_sflow_unref(xbridge->sflow);
453 xbridge->sflow = dpif_sflow_ref(sflow);
456 if (xbridge->ipfix != ipfix) {
457 dpif_ipfix_unref(xbridge->ipfix);
458 xbridge->ipfix = dpif_ipfix_ref(ipfix);
461 if (xbridge->stp != stp) {
462 stp_unref(xbridge->stp);
463 xbridge->stp = stp_ref(stp);
466 if (xbridge->netflow != netflow) {
467 netflow_unref(xbridge->netflow);
468 xbridge->netflow = netflow_ref(netflow);
471 xbridge->dpif = dpif;
472 xbridge->forward_bpdu = forward_bpdu;
473 xbridge->has_in_band = has_in_band;
474 xbridge->frag = frag;
475 xbridge->miss_rule = miss_rule;
476 xbridge->no_packet_in_rule = no_packet_in_rule;
477 xbridge->enable_recirc = enable_recirc;
478 xbridge->variable_length_userdata = variable_length_userdata;
479 xbridge->max_mpls_depth = max_mpls_depth;
483 xlate_xbundle_set(struct xbundle *xbundle,
484 enum port_vlan_mode vlan_mode, int vlan,
485 unsigned long *trunks, bool use_priority_tags,
486 const struct bond *bond, const struct lacp *lacp,
489 ovs_assert(xbundle->xbridge);
491 xbundle->vlan_mode = vlan_mode;
492 xbundle->vlan = vlan;
493 xbundle->trunks = trunks;
494 xbundle->use_priority_tags = use_priority_tags;
495 xbundle->floodable = floodable;
497 if (xbundle->bond != bond) {
498 bond_unref(xbundle->bond);
499 xbundle->bond = bond_ref(bond);
502 if (xbundle->lacp != lacp) {
503 lacp_unref(xbundle->lacp);
504 xbundle->lacp = lacp_ref(lacp);
509 xlate_xport_set(struct xport *xport, odp_port_t odp_port,
510 const struct netdev *netdev, const struct cfm *cfm,
511 const struct bfd *bfd, int stp_port_no,
512 enum ofputil_port_config config, enum ofputil_port_state state,
513 bool is_tunnel, bool may_enable)
515 xport->config = config;
516 xport->state = state;
517 xport->stp_port_no = stp_port_no;
518 xport->is_tunnel = is_tunnel;
519 xport->may_enable = may_enable;
520 xport->odp_port = odp_port;
522 if (xport->cfm != cfm) {
523 cfm_unref(xport->cfm);
524 xport->cfm = cfm_ref(cfm);
527 if (xport->bfd != bfd) {
528 bfd_unref(xport->bfd);
529 xport->bfd = bfd_ref(bfd);
532 if (xport->netdev != netdev) {
533 netdev_close(xport->netdev);
534 xport->netdev = netdev_ref(netdev);
539 xlate_xbridge_copy(struct xbridge *xbridge)
541 struct xbundle *xbundle;
543 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
544 new_xbridge->ofproto = xbridge->ofproto;
545 new_xbridge->name = xstrdup(xbridge->name);
546 xlate_xbridge_init(new_xcfg, new_xbridge);
548 xlate_xbridge_set(new_xbridge,
549 xbridge->dpif, xbridge->miss_rule,
550 xbridge->no_packet_in_rule, xbridge->ml, xbridge->stp,
551 xbridge->ms, xbridge->mbridge, xbridge->sflow,
552 xbridge->ipfix, xbridge->netflow, xbridge->frag,
553 xbridge->forward_bpdu, xbridge->has_in_band,
554 xbridge->enable_recirc, xbridge->variable_length_userdata,
555 xbridge->max_mpls_depth);
556 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
557 xlate_xbundle_copy(new_xbridge, xbundle);
560 /* Copy xports which are not part of a xbundle */
561 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
562 if (!xport->xbundle) {
563 xlate_xport_copy(new_xbridge, NULL, xport);
569 xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
572 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
573 new_xbundle->ofbundle = xbundle->ofbundle;
574 new_xbundle->xbridge = xbridge;
575 new_xbundle->name = xstrdup(xbundle->name);
576 xlate_xbundle_init(new_xcfg, new_xbundle);
578 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
579 xbundle->vlan, xbundle->trunks,
580 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
582 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
583 xlate_xport_copy(xbridge, new_xbundle, xport);
588 xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
591 struct skb_priority_to_dscp *pdscp, *new_pdscp;
592 struct xport *new_xport = xzalloc(sizeof *xport);
593 new_xport->ofport = xport->ofport;
594 new_xport->ofp_port = xport->ofp_port;
595 new_xport->xbridge = xbridge;
596 xlate_xport_init(new_xcfg, new_xport);
598 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
599 xport->bfd, xport->stp_port_no, xport->config, xport->state,
600 xport->is_tunnel, xport->may_enable);
603 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
605 new_xport->peer = peer;
606 new_xport->peer->peer = new_xport;
611 new_xport->xbundle = xbundle;
612 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
615 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
616 new_pdscp = xmalloc(sizeof *pdscp);
617 new_pdscp->skb_priority = pdscp->skb_priority;
618 new_pdscp->dscp = pdscp->dscp;
619 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
620 hash_int(new_pdscp->skb_priority, 0));
624 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
625 * configuration in xcfgp.
627 * This needs to be called after editing the xlate configuration.
629 * Functions that edit the new xlate configuration are
630 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
636 * edit_xlate_configuration();
638 * xlate_txn_commit(); */
640 xlate_txn_commit(void)
642 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
644 ovsrcu_set(&xcfgp, new_xcfg);
645 ovsrcu_postpone(xlate_xcfg_free, xcfg);
650 /* Copies the current xlate configuration in xcfgp to new_xcfg.
652 * This needs to be called prior to editing the xlate configuration. */
654 xlate_txn_start(void)
656 struct xbridge *xbridge;
657 struct xlate_cfg *xcfg;
659 ovs_assert(!new_xcfg);
661 new_xcfg = xmalloc(sizeof *new_xcfg);
662 hmap_init(&new_xcfg->xbridges);
663 hmap_init(&new_xcfg->xbundles);
664 hmap_init(&new_xcfg->xports);
666 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
671 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
672 xlate_xbridge_copy(xbridge);
678 xlate_xcfg_free(struct xlate_cfg *xcfg)
680 struct xbridge *xbridge, *next_xbridge;
686 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
687 xlate_xbridge_remove(xcfg, xbridge);
690 hmap_destroy(&xcfg->xbridges);
691 hmap_destroy(&xcfg->xbundles);
692 hmap_destroy(&xcfg->xports);
697 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
698 struct dpif *dpif, struct rule_dpif *miss_rule,
699 struct rule_dpif *no_packet_in_rule,
700 const struct mac_learning *ml, struct stp *stp,
701 const struct mcast_snooping *ms,
702 const struct mbridge *mbridge,
703 const struct dpif_sflow *sflow,
704 const struct dpif_ipfix *ipfix,
705 const struct netflow *netflow, enum ofp_config_flags frag,
706 bool forward_bpdu, bool has_in_band,
708 bool variable_length_userdata,
709 size_t max_mpls_depth)
711 struct xbridge *xbridge;
713 ovs_assert(new_xcfg);
715 xbridge = xbridge_lookup(new_xcfg, ofproto);
717 xbridge = xzalloc(sizeof *xbridge);
718 xbridge->ofproto = ofproto;
720 xlate_xbridge_init(new_xcfg, xbridge);
724 xbridge->name = xstrdup(name);
726 xlate_xbridge_set(xbridge, dpif, miss_rule, no_packet_in_rule, ml, stp,
727 ms, mbridge, sflow, ipfix, netflow, frag, forward_bpdu,
728 has_in_band, enable_recirc, variable_length_userdata,
733 xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
735 struct xbundle *xbundle, *next_xbundle;
736 struct xport *xport, *next_xport;
742 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
743 xlate_xport_remove(xcfg, xport);
746 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
747 xlate_xbundle_remove(xcfg, xbundle);
750 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
751 mac_learning_unref(xbridge->ml);
752 mcast_snooping_unref(xbridge->ms);
753 mbridge_unref(xbridge->mbridge);
754 dpif_sflow_unref(xbridge->sflow);
755 dpif_ipfix_unref(xbridge->ipfix);
756 stp_unref(xbridge->stp);
757 hmap_destroy(&xbridge->xports);
763 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
765 struct xbridge *xbridge;
767 ovs_assert(new_xcfg);
769 xbridge = xbridge_lookup(new_xcfg, ofproto);
770 xlate_xbridge_remove(new_xcfg, xbridge);
774 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
775 const char *name, enum port_vlan_mode vlan_mode, int vlan,
776 unsigned long *trunks, bool use_priority_tags,
777 const struct bond *bond, const struct lacp *lacp,
780 struct xbundle *xbundle;
782 ovs_assert(new_xcfg);
784 xbundle = xbundle_lookup(new_xcfg, ofbundle);
786 xbundle = xzalloc(sizeof *xbundle);
787 xbundle->ofbundle = ofbundle;
788 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
790 xlate_xbundle_init(new_xcfg, xbundle);
794 xbundle->name = xstrdup(name);
796 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
797 use_priority_tags, bond, lacp, floodable);
801 xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
803 struct xport *xport, *next;
809 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
810 list_remove(&xport->bundle_node);
811 xport->xbundle = NULL;
814 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
815 list_remove(&xbundle->list_node);
816 bond_unref(xbundle->bond);
817 lacp_unref(xbundle->lacp);
823 xlate_bundle_remove(struct ofbundle *ofbundle)
825 struct xbundle *xbundle;
827 ovs_assert(new_xcfg);
829 xbundle = xbundle_lookup(new_xcfg, ofbundle);
830 xlate_xbundle_remove(new_xcfg, xbundle);
834 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
835 struct ofport_dpif *ofport, ofp_port_t ofp_port,
836 odp_port_t odp_port, const struct netdev *netdev,
837 const struct cfm *cfm, const struct bfd *bfd,
838 struct ofport_dpif *peer, int stp_port_no,
839 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
840 enum ofputil_port_config config,
841 enum ofputil_port_state state, bool is_tunnel,
847 ovs_assert(new_xcfg);
849 xport = xport_lookup(new_xcfg, ofport);
851 xport = xzalloc(sizeof *xport);
852 xport->ofport = ofport;
853 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
854 xport->ofp_port = ofp_port;
856 xlate_xport_init(new_xcfg, xport);
859 ovs_assert(xport->ofp_port == ofp_port);
861 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, stp_port_no, config,
862 state, is_tunnel, may_enable);
865 xport->peer->peer = NULL;
867 xport->peer = xport_lookup(new_xcfg, peer);
869 xport->peer->peer = xport;
872 if (xport->xbundle) {
873 list_remove(&xport->bundle_node);
875 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
876 if (xport->xbundle) {
877 list_insert(&xport->xbundle->xports, &xport->bundle_node);
880 clear_skb_priorities(xport);
881 for (i = 0; i < n_qdscp; i++) {
882 struct skb_priority_to_dscp *pdscp;
883 uint32_t skb_priority;
885 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
890 pdscp = xmalloc(sizeof *pdscp);
891 pdscp->skb_priority = skb_priority;
892 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
893 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
894 hash_int(pdscp->skb_priority, 0));
899 xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
906 xport->peer->peer = NULL;
910 if (xport->xbundle) {
911 list_remove(&xport->bundle_node);
914 clear_skb_priorities(xport);
915 hmap_destroy(&xport->skb_priorities);
917 hmap_remove(&xcfg->xports, &xport->hmap_node);
918 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
920 netdev_close(xport->netdev);
921 cfm_unref(xport->cfm);
922 bfd_unref(xport->bfd);
927 xlate_ofport_remove(struct ofport_dpif *ofport)
931 ovs_assert(new_xcfg);
933 xport = xport_lookup(new_xcfg, ofport);
934 xlate_xport_remove(new_xcfg, xport);
937 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
938 * Optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
939 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
940 * handles for those protocols if they're enabled. Caller is responsible for
943 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
944 * 'flow''s in_port to OFPP_NONE.
946 * Similarly, this function also includes some logic to help with tunnels. It
947 * may modify 'flow' as necessary to make the tunneling implementation
948 * transparent to the upcall processing logic.
950 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
951 * or some other positive errno if there are other problems. */
953 xlate_receive(const struct dpif_backer *backer, const struct flow *flow,
954 struct ofproto_dpif **ofproto, struct dpif_ipfix **ipfix,
955 struct dpif_sflow **sflow, struct netflow **netflow,
956 ofp_port_t *ofp_in_port)
958 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
959 const struct xport *xport;
961 xport = xport_lookup(xcfg, tnl_port_should_receive(flow)
962 ? tnl_port_receive(flow)
963 : odp_port_to_ofport(backer, flow->in_port.odp_port));
966 *ofp_in_port = xport ? xport->ofp_port : OFPP_NONE;
974 *ofproto = xport->xbridge->ofproto;
978 *ipfix = dpif_ipfix_ref(xport->xbridge->ipfix);
982 *sflow = dpif_sflow_ref(xport->xbridge->sflow);
986 *netflow = netflow_ref(xport->xbridge->netflow);
991 static struct xbridge *
992 xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
994 struct hmap *xbridges;
995 struct xbridge *xbridge;
997 if (!ofproto || !xcfg) {
1001 xbridges = &xcfg->xbridges;
1003 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
1005 if (xbridge->ofproto == ofproto) {
1012 static struct xbundle *
1013 xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
1015 struct hmap *xbundles;
1016 struct xbundle *xbundle;
1018 if (!ofbundle || !xcfg) {
1022 xbundles = &xcfg->xbundles;
1024 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
1026 if (xbundle->ofbundle == ofbundle) {
1033 static struct xport *
1034 xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
1036 struct hmap *xports;
1037 struct xport *xport;
1039 if (!ofport || !xcfg) {
1043 xports = &xcfg->xports;
1045 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
1047 if (xport->ofport == ofport) {
1054 static struct stp_port *
1055 xport_get_stp_port(const struct xport *xport)
1057 return xport->xbridge->stp && xport->stp_port_no != -1
1058 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1063 xport_stp_learn_state(const struct xport *xport)
1065 struct stp_port *sp = xport_get_stp_port(xport);
1066 return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
1070 xport_stp_forward_state(const struct xport *xport)
1072 struct stp_port *sp = xport_get_stp_port(xport);
1073 return stp_forward_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
1077 xport_stp_should_forward_bpdu(const struct xport *xport)
1079 struct stp_port *sp = xport_get_stp_port(xport);
1080 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
1083 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1084 * were used to make the determination.*/
1086 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1088 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1089 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1090 return is_stp(flow);
1094 stp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
1096 struct stp_port *sp = xport_get_stp_port(xport);
1097 struct ofpbuf payload = *packet;
1098 struct eth_header *eth = ofpbuf_data(&payload);
1100 /* Sink packets on ports that have STP disabled when the bridge has
1102 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1106 /* Trim off padding on payload. */
1107 if (ofpbuf_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1108 ofpbuf_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1111 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1112 stp_received_bpdu(sp, ofpbuf_data(&payload), ofpbuf_size(&payload));
1116 static struct xport *
1117 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1119 struct xport *xport;
1121 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1123 if (xport->ofp_port == ofp_port) {
1131 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1133 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1134 return xport ? xport->odp_port : ODPP_NONE;
1138 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1140 struct xport *xport;
1142 xport = get_ofp_port(ctx->xbridge, ofp_port);
1143 if (!xport || xport->config & OFPUTIL_PC_PORT_DOWN ||
1144 xport->state & OFPUTIL_PS_LINK_DOWN) {
1151 static struct ofputil_bucket *
1152 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1156 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1158 struct group_dpif *group;
1160 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1161 struct ofputil_bucket *bucket;
1163 bucket = group_first_live_bucket(ctx, group, depth);
1164 group_dpif_unref(group);
1165 return bucket == NULL;
1171 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1174 bucket_is_alive(const struct xlate_ctx *ctx,
1175 struct ofputil_bucket *bucket, int depth)
1177 if (depth >= MAX_LIVENESS_RECURSION) {
1178 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1180 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1181 MAX_LIVENESS_RECURSION);
1185 return (!ofputil_bucket_has_liveness(bucket)
1186 || (bucket->watch_port != OFPP_ANY
1187 && odp_port_is_alive(ctx, bucket->watch_port))
1188 || (bucket->watch_group != OFPG_ANY
1189 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
1192 static struct ofputil_bucket *
1193 group_first_live_bucket(const struct xlate_ctx *ctx,
1194 const struct group_dpif *group, int depth)
1196 struct ofputil_bucket *bucket;
1197 const struct list *buckets;
1199 group_dpif_get_buckets(group, &buckets);
1200 LIST_FOR_EACH (bucket, list_node, buckets) {
1201 if (bucket_is_alive(ctx, bucket, depth)) {
1209 static struct ofputil_bucket *
1210 group_best_live_bucket(const struct xlate_ctx *ctx,
1211 const struct group_dpif *group,
1214 struct ofputil_bucket *best_bucket = NULL;
1215 uint32_t best_score = 0;
1218 struct ofputil_bucket *bucket;
1219 const struct list *buckets;
1221 group_dpif_get_buckets(group, &buckets);
1222 LIST_FOR_EACH (bucket, list_node, buckets) {
1223 if (bucket_is_alive(ctx, bucket, 0)) {
1224 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
1225 if (score >= best_score) {
1226 best_bucket = bucket;
1237 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
1239 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1240 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1244 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1246 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1249 static mirror_mask_t
1250 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1252 return xbundle != &ofpp_none_bundle
1253 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1257 static mirror_mask_t
1258 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
1260 return xbundle != &ofpp_none_bundle
1261 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1265 static mirror_mask_t
1266 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
1268 return xbundle != &ofpp_none_bundle
1269 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1273 static struct xbundle *
1274 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1275 bool warn, struct xport **in_xportp)
1277 struct xport *xport;
1279 /* Find the port and bundle for the received packet. */
1280 xport = get_ofp_port(xbridge, in_port);
1284 if (xport && xport->xbundle) {
1285 return xport->xbundle;
1288 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1289 * which a controller may use as the ingress port for traffic that
1290 * it is sourcing. */
1291 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
1292 return &ofpp_none_bundle;
1295 /* Odd. A few possible reasons here:
1297 * - We deleted a port but there are still a few packets queued up
1300 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1301 * we don't know about.
1303 * - The ofproto client didn't configure the port as part of a bundle.
1304 * This is particularly likely to happen if a packet was received on the
1305 * port after it was created, but before the client had a chance to
1306 * configure its bundle.
1309 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1311 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1312 "port %"PRIu16, xbridge->name, in_port);
1318 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
1320 const struct xbridge *xbridge = ctx->xbridge;
1321 mirror_mask_t mirrors;
1322 struct xbundle *in_xbundle;
1326 mirrors = ctx->xout->mirrors;
1327 ctx->xout->mirrors = 0;
1329 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
1330 ctx->xin->packet != NULL, NULL);
1334 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
1336 /* Drop frames on bundles reserved for mirroring. */
1337 if (xbundle_mirror_out(xbridge, in_xbundle)) {
1338 if (ctx->xin->packet != NULL) {
1339 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1340 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1341 "%s, which is reserved exclusively for mirroring",
1342 ctx->xbridge->name, in_xbundle->name);
1344 ofpbuf_clear(ctx->xout->odp_actions);
1349 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
1350 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1353 vlan = input_vid_to_vlan(in_xbundle, vid);
1359 /* Restore the original packet before adding the mirror actions. */
1360 ctx->xin->flow = *orig_flow;
1363 mirror_mask_t dup_mirrors;
1364 struct ofbundle *out;
1365 unsigned long *vlans;
1370 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1371 &vlans, &dup_mirrors, &out, &out_vlan);
1372 ovs_assert(has_mirror);
1375 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1377 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
1380 if (!vlan_mirrored) {
1381 mirrors = zero_rightmost_1bit(mirrors);
1385 mirrors &= ~dup_mirrors;
1386 ctx->xout->mirrors |= dup_mirrors;
1388 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1389 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1391 output_normal(ctx, out_xbundle, vlan);
1393 } else if (vlan != out_vlan
1394 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
1395 struct xbundle *xbundle;
1397 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1398 if (xbundle_includes_vlan(xbundle, out_vlan)
1399 && !xbundle_mirror_out(xbridge, xbundle)) {
1400 output_normal(ctx, xbundle, out_vlan);
1407 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1408 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1409 * the bundle on which the packet was received, returns the VLAN to which the
1412 * Both 'vid' and the return value are in the range 0...4095. */
1414 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1416 switch (in_xbundle->vlan_mode) {
1417 case PORT_VLAN_ACCESS:
1418 return in_xbundle->vlan;
1421 case PORT_VLAN_TRUNK:
1424 case PORT_VLAN_NATIVE_UNTAGGED:
1425 case PORT_VLAN_NATIVE_TAGGED:
1426 return vid ? vid : in_xbundle->vlan;
1433 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1434 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1437 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1438 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1441 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1443 /* Allow any VID on the OFPP_NONE port. */
1444 if (in_xbundle == &ofpp_none_bundle) {
1448 switch (in_xbundle->vlan_mode) {
1449 case PORT_VLAN_ACCESS:
1452 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1453 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1454 "packet received on port %s configured as VLAN "
1455 "%"PRIu16" access port", vid, in_xbundle->name,
1462 case PORT_VLAN_NATIVE_UNTAGGED:
1463 case PORT_VLAN_NATIVE_TAGGED:
1465 /* Port must always carry its native VLAN. */
1469 case PORT_VLAN_TRUNK:
1470 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1472 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1473 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1474 "received on port %s not configured for trunking "
1475 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1487 /* Given 'vlan', the VLAN that a packet belongs to, and
1488 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1489 * that should be included in the 802.1Q header. (If the return value is 0,
1490 * then the 802.1Q header should only be included in the packet if there is a
1493 * Both 'vlan' and the return value are in the range 0...4095. */
1495 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1497 switch (out_xbundle->vlan_mode) {
1498 case PORT_VLAN_ACCESS:
1501 case PORT_VLAN_TRUNK:
1502 case PORT_VLAN_NATIVE_TAGGED:
1505 case PORT_VLAN_NATIVE_UNTAGGED:
1506 return vlan == out_xbundle->vlan ? 0 : vlan;
1514 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1517 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1519 ovs_be16 tci, old_tci;
1520 struct xport *xport;
1522 vid = output_vlan_to_vid(out_xbundle, vlan);
1523 if (list_is_empty(&out_xbundle->xports)) {
1524 /* Partially configured bundle with no slaves. Drop the packet. */
1526 } else if (!out_xbundle->bond) {
1527 ctx->use_recirc = false;
1528 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1531 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1532 struct flow_wildcards *wc = &ctx->xout->wc;
1533 struct xlate_recirc *xr = &ctx->recirc;
1534 struct ofport_dpif *ofport;
1536 if (ctx->xbridge->enable_recirc) {
1537 ctx->use_recirc = bond_may_recirc(
1538 out_xbundle->bond, &xr->recirc_id, &xr->hash_basis);
1540 if (ctx->use_recirc) {
1541 /* Only TCP mode uses recirculation. */
1542 xr->hash_alg = OVS_HASH_ALG_L4;
1543 bond_update_post_recirc_rules(out_xbundle->bond, false);
1545 /* Recirculation does not require unmasking hash fields. */
1550 ofport = bond_choose_output_slave(out_xbundle->bond,
1551 &ctx->xin->flow, wc, vid);
1552 xport = xport_lookup(xcfg, ofport);
1555 /* No slaves enabled, so drop packet. */
1559 /* If ctx->xout->use_recirc is set, the main thread will handle stats
1560 * accounting for this bond. */
1561 if (!ctx->use_recirc) {
1562 if (ctx->xin->resubmit_stats) {
1563 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1564 ctx->xin->resubmit_stats->n_bytes);
1566 if (ctx->xin->xcache) {
1567 struct xc_entry *entry;
1570 flow = &ctx->xin->flow;
1571 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1572 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1573 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1574 entry->u.bond.vid = vid;
1579 old_tci = *flow_tci;
1581 if (tci || out_xbundle->use_priority_tags) {
1582 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1584 tci |= htons(VLAN_CFI);
1589 compose_output_action(ctx, xport->ofp_port);
1590 *flow_tci = old_tci;
1593 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1594 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1595 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1597 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1599 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1603 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1604 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1608 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1609 if (flow->nw_proto == ARP_OP_REPLY) {
1611 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1612 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1613 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1615 return flow->nw_src == flow->nw_dst;
1621 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1622 * dropped. Returns true if they may be forwarded, false if they should be
1625 * 'in_port' must be the xport that corresponds to flow->in_port.
1626 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1628 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1629 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1630 * checked by input_vid_is_valid().
1632 * May also add tags to '*tags', although the current implementation only does
1633 * so in one special case.
1636 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1639 struct xbundle *in_xbundle = in_port->xbundle;
1640 const struct xbridge *xbridge = ctx->xbridge;
1641 struct flow *flow = &ctx->xin->flow;
1643 /* Drop frames for reserved multicast addresses
1644 * only if forward_bpdu option is absent. */
1645 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1646 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1650 if (in_xbundle->bond) {
1651 struct mac_entry *mac;
1653 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1659 xlate_report(ctx, "bonding refused admissibility, dropping");
1662 case BV_DROP_IF_MOVED:
1663 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1664 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1665 if (mac && mac->port.p != in_xbundle->ofbundle &&
1666 (!is_gratuitous_arp(flow, &ctx->xout->wc)
1667 || mac_entry_is_grat_arp_locked(mac))) {
1668 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1669 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1673 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1681 /* Checks whether a MAC learning update is necessary for MAC learning table
1682 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1685 * Most packets processed through the MAC learning table do not actually
1686 * change it in any way. This function requires only a read lock on the MAC
1687 * learning table, so it is much cheaper in this common case.
1689 * Keep the code here synchronized with that in update_learning_table__()
1692 is_mac_learning_update_needed(const struct mac_learning *ml,
1693 const struct flow *flow,
1694 struct flow_wildcards *wc,
1695 int vlan, struct xbundle *in_xbundle)
1696 OVS_REQ_RDLOCK(ml->rwlock)
1698 struct mac_entry *mac;
1700 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1704 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1705 if (!mac || mac_entry_age(ml, mac)) {
1709 if (is_gratuitous_arp(flow, wc)) {
1710 /* We don't want to learn from gratuitous ARP packets that are
1711 * reflected back over bond slaves so we lock the learning table. */
1712 if (!in_xbundle->bond) {
1714 } else if (mac_entry_is_grat_arp_locked(mac)) {
1719 return mac->port.p != in_xbundle->ofbundle;
1723 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1724 * received on 'in_xbundle' in 'vlan'.
1726 * This code repeats all the checks in is_mac_learning_update_needed() because
1727 * the lock was released between there and here and thus the MAC learning state
1728 * could have changed.
1730 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1733 update_learning_table__(const struct xbridge *xbridge,
1734 const struct flow *flow, struct flow_wildcards *wc,
1735 int vlan, struct xbundle *in_xbundle)
1736 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1738 struct mac_entry *mac;
1740 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1744 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1745 if (is_gratuitous_arp(flow, wc)) {
1746 /* We don't want to learn from gratuitous ARP packets that are
1747 * reflected back over bond slaves so we lock the learning table. */
1748 if (!in_xbundle->bond) {
1749 mac_entry_set_grat_arp_lock(mac);
1750 } else if (mac_entry_is_grat_arp_locked(mac)) {
1755 if (mac->port.p != in_xbundle->ofbundle) {
1756 /* The log messages here could actually be useful in debugging,
1757 * so keep the rate limit relatively high. */
1758 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1760 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1761 "on port %s in VLAN %d",
1762 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1763 in_xbundle->name, vlan);
1765 mac->port.p = in_xbundle->ofbundle;
1766 mac_learning_changed(xbridge->ml);
1771 update_learning_table(const struct xbridge *xbridge,
1772 const struct flow *flow, struct flow_wildcards *wc,
1773 int vlan, struct xbundle *in_xbundle)
1777 /* Don't learn the OFPP_NONE port. */
1778 if (in_xbundle == &ofpp_none_bundle) {
1782 /* First try the common case: no change to MAC learning table. */
1783 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1784 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1786 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1789 /* Slow path: MAC learning table might need an update. */
1790 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1791 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1792 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1796 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1797 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
1799 update_mcast_snooping_table__(const struct xbridge *xbridge,
1800 const struct flow *flow,
1801 struct mcast_snooping *ms,
1802 ovs_be32 ip4, int vlan,
1803 struct xbundle *in_xbundle)
1804 OVS_REQ_WRLOCK(ms->rwlock)
1806 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
1808 switch (ntohs(flow->tp_src)) {
1809 case IGMP_HOST_MEMBERSHIP_REPORT:
1810 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1811 if (mcast_snooping_add_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
1812 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
1813 IP_FMT" is on port %s in VLAN %d",
1814 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
1817 case IGMP_HOST_LEAVE_MESSAGE:
1818 if (mcast_snooping_leave_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
1819 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
1820 IP_FMT" is on port %s in VLAN %d",
1821 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
1824 case IGMP_HOST_MEMBERSHIP_QUERY:
1825 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
1826 in_xbundle->ofbundle)) {
1827 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
1828 IP_FMT" is on port %s in VLAN %d",
1829 xbridge->name, IP_ARGS(flow->nw_src),
1830 in_xbundle->name, vlan);
1836 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1837 * was received on 'in_xbundle' in 'vlan'. */
1839 update_mcast_snooping_table(const struct xbridge *xbridge,
1840 const struct flow *flow, int vlan,
1841 struct xbundle *in_xbundle)
1843 struct mcast_snooping *ms = xbridge->ms;
1844 struct xlate_cfg *xcfg;
1845 struct xbundle *mcast_xbundle;
1846 struct mcast_fport_bundle *fport;
1848 /* Don't learn the OFPP_NONE port. */
1849 if (in_xbundle == &ofpp_none_bundle) {
1853 /* Don't learn from flood ports */
1854 mcast_xbundle = NULL;
1855 ovs_rwlock_wrlock(&ms->rwlock);
1856 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1857 LIST_FOR_EACH(fport, fport_node, &ms->fport_list) {
1858 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
1859 if (mcast_xbundle == in_xbundle) {
1864 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
1865 update_mcast_snooping_table__(xbridge, flow, ms, flow->igmp_group_ip4,
1868 ovs_rwlock_unlock(&ms->rwlock);
1871 /* send the packet to ports having the multicast group learned */
1873 xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
1874 struct mcast_snooping *ms OVS_UNUSED,
1875 struct mcast_group *grp,
1876 struct xbundle *in_xbundle, uint16_t vlan)
1877 OVS_REQ_RDLOCK(ms->rwlock)
1879 struct xlate_cfg *xcfg;
1880 struct mcast_group_bundle *b;
1881 struct xbundle *mcast_xbundle;
1883 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1884 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
1885 mcast_xbundle = xbundle_lookup(xcfg, b->port);
1886 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
1887 xlate_report(ctx, "forwarding to mcast group port");
1888 output_normal(ctx, mcast_xbundle, vlan);
1889 } else if (!mcast_xbundle) {
1890 xlate_report(ctx, "mcast group port is unknown, dropping");
1892 xlate_report(ctx, "mcast group port is input port, dropping");
1897 /* send the packet to ports connected to multicast routers */
1899 xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
1900 struct mcast_snooping *ms,
1901 struct xbundle *in_xbundle, uint16_t vlan)
1902 OVS_REQ_RDLOCK(ms->rwlock)
1904 struct xlate_cfg *xcfg;
1905 struct mcast_mrouter_bundle *mrouter;
1906 struct xbundle *mcast_xbundle;
1908 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1909 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
1910 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
1911 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
1912 xlate_report(ctx, "forwarding to mcast router port");
1913 output_normal(ctx, mcast_xbundle, vlan);
1914 } else if (!mcast_xbundle) {
1915 xlate_report(ctx, "mcast router port is unknown, dropping");
1917 xlate_report(ctx, "mcast router port is input port, dropping");
1922 /* send the packet to ports flagged to be flooded */
1924 xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
1925 struct mcast_snooping *ms,
1926 struct xbundle *in_xbundle, uint16_t vlan)
1927 OVS_REQ_RDLOCK(ms->rwlock)
1929 struct xlate_cfg *xcfg;
1930 struct mcast_fport_bundle *fport;
1931 struct xbundle *mcast_xbundle;
1933 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1934 LIST_FOR_EACH(fport, fport_node, &ms->fport_list) {
1935 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
1936 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
1937 xlate_report(ctx, "forwarding to mcast flood port");
1938 output_normal(ctx, mcast_xbundle, vlan);
1939 } else if (!mcast_xbundle) {
1940 xlate_report(ctx, "mcast flood port is unknown, dropping");
1942 xlate_report(ctx, "mcast flood port is input port, dropping");
1948 xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
1951 struct xbundle *xbundle;
1953 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1954 if (xbundle != in_xbundle
1955 && xbundle_includes_vlan(xbundle, vlan)
1956 && xbundle->floodable
1957 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1958 output_normal(ctx, xbundle, vlan);
1961 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1965 xlate_normal(struct xlate_ctx *ctx)
1967 struct flow_wildcards *wc = &ctx->xout->wc;
1968 struct flow *flow = &ctx->xin->flow;
1969 struct xbundle *in_xbundle;
1970 struct xport *in_port;
1971 struct mac_entry *mac;
1976 ctx->xout->has_normal = true;
1978 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1979 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1980 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1982 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
1983 ctx->xin->packet != NULL, &in_port);
1985 xlate_report(ctx, "no input bundle, dropping");
1989 /* Drop malformed frames. */
1990 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
1991 !(flow->vlan_tci & htons(VLAN_CFI))) {
1992 if (ctx->xin->packet != NULL) {
1993 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1994 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
1995 "VLAN tag received on port %s",
1996 ctx->xbridge->name, in_xbundle->name);
1998 xlate_report(ctx, "partial VLAN tag, dropping");
2002 /* Drop frames on bundles reserved for mirroring. */
2003 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
2004 if (ctx->xin->packet != NULL) {
2005 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2006 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2007 "%s, which is reserved exclusively for mirroring",
2008 ctx->xbridge->name, in_xbundle->name);
2010 xlate_report(ctx, "input port is mirror output port, dropping");
2015 vid = vlan_tci_to_vid(flow->vlan_tci);
2016 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
2017 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2020 vlan = input_vid_to_vlan(in_xbundle, vid);
2022 /* Check other admissibility requirements. */
2023 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2027 /* Learn source MAC. */
2028 if (ctx->xin->may_learn) {
2029 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
2031 if (ctx->xin->xcache) {
2032 struct xc_entry *entry;
2034 /* Save enough info to update mac learning table later. */
2035 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
2036 entry->u.normal.ofproto = ctx->xbridge->ofproto;
2037 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2038 entry->u.normal.vlan = vlan;
2041 /* Determine output bundle. */
2042 if (mcast_snooping_enabled(ctx->xbridge->ms)
2043 && !eth_addr_is_broadcast(flow->dl_dst)
2044 && eth_addr_is_multicast(flow->dl_dst)
2045 && flow->dl_type == htons(ETH_TYPE_IP)) {
2046 struct mcast_snooping *ms = ctx->xbridge->ms;
2047 struct mcast_group *grp;
2049 if (flow->nw_proto == IPPROTO_IGMP) {
2050 if (ctx->xin->may_learn) {
2051 if (mcast_snooping_is_membership(flow->tp_src) ||
2052 mcast_snooping_is_query(flow->tp_src)) {
2053 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2058 if (mcast_snooping_is_membership(flow->tp_src)) {
2059 ovs_rwlock_rdlock(&ms->rwlock);
2060 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2061 ovs_rwlock_unlock(&ms->rwlock);
2063 xlate_report(ctx, "multicast traffic, flooding");
2064 xlate_normal_flood(ctx, in_xbundle, vlan);
2068 if (ip_is_local_multicast(flow->nw_dst)) {
2069 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2070 * address in the 224.0.0.x range which are not IGMP must
2071 * be forwarded on all ports */
2072 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2073 xlate_normal_flood(ctx, in_xbundle, vlan);
2078 /* forwarding to group base ports */
2079 ovs_rwlock_rdlock(&ms->rwlock);
2080 grp = mcast_snooping_lookup(ms, flow->nw_dst, vlan);
2082 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2083 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2084 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2086 if (mcast_snooping_flood_unreg(ms)) {
2087 xlate_report(ctx, "unregistered multicast, flooding");
2088 xlate_normal_flood(ctx, in_xbundle, vlan);
2090 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2091 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2094 ovs_rwlock_unlock(&ms->rwlock);
2096 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2097 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
2098 mac_port = mac ? mac->port.p : NULL;
2099 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2102 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2103 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2104 if (mac_xbundle && mac_xbundle != in_xbundle) {
2105 xlate_report(ctx, "forwarding to learned port");
2106 output_normal(ctx, mac_xbundle, vlan);
2107 } else if (!mac_xbundle) {
2108 xlate_report(ctx, "learned port is unknown, dropping");
2110 xlate_report(ctx, "learned port is input port, dropping");
2113 xlate_report(ctx, "no learned MAC for destination, flooding");
2114 xlate_normal_flood(ctx, in_xbundle, vlan);
2119 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
2120 * the number of packets out of UINT32_MAX to sample. The given
2121 * cookie is passed back in the callback for each sampled packet.
2124 compose_sample_action(const struct xbridge *xbridge,
2125 struct ofpbuf *odp_actions,
2126 const struct flow *flow,
2127 const uint32_t probability,
2128 const union user_action_cookie *cookie,
2129 const size_t cookie_size,
2130 const odp_port_t tunnel_out_port)
2132 size_t sample_offset, actions_offset;
2133 odp_port_t odp_port;
2137 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
2139 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
2141 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
2143 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
2144 pid = dpif_port_get_pid(xbridge->dpif, odp_port,
2145 flow_hash_5tuple(flow, 0));
2146 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2147 tunnel_out_port, odp_actions);
2149 nl_msg_end_nested(odp_actions, actions_offset);
2150 nl_msg_end_nested(odp_actions, sample_offset);
2151 return cookie_offset;
2155 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
2156 odp_port_t odp_port, unsigned int n_outputs,
2157 union user_action_cookie *cookie)
2161 cookie->type = USER_ACTION_COOKIE_SFLOW;
2162 cookie->sflow.vlan_tci = vlan_tci;
2164 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2165 * port information") for the interpretation of cookie->output. */
2166 switch (n_outputs) {
2168 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2169 cookie->sflow.output = 0x40000000 | 256;
2173 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
2175 cookie->sflow.output = ifindex;
2180 /* 0x80000000 means "multiple output ports. */
2181 cookie->sflow.output = 0x80000000 | n_outputs;
2186 /* Compose SAMPLE action for sFlow bridge sampling. */
2188 compose_sflow_action(const struct xbridge *xbridge,
2189 struct ofpbuf *odp_actions,
2190 const struct flow *flow,
2191 odp_port_t odp_port)
2193 uint32_t probability;
2194 union user_action_cookie cookie;
2196 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
2200 probability = dpif_sflow_get_probability(xbridge->sflow);
2201 compose_sflow_cookie(xbridge, htons(0), odp_port,
2202 odp_port == ODPP_NONE ? 0 : 1, &cookie);
2204 return compose_sample_action(xbridge, odp_actions, flow, probability,
2205 &cookie, sizeof cookie.sflow, ODPP_NONE);
2209 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
2210 uint32_t obs_domain_id, uint32_t obs_point_id,
2211 union user_action_cookie *cookie)
2213 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
2214 cookie->flow_sample.probability = probability;
2215 cookie->flow_sample.collector_set_id = collector_set_id;
2216 cookie->flow_sample.obs_domain_id = obs_domain_id;
2217 cookie->flow_sample.obs_point_id = obs_point_id;
2221 compose_ipfix_cookie(union user_action_cookie *cookie,
2222 odp_port_t output_odp_port)
2224 cookie->type = USER_ACTION_COOKIE_IPFIX;
2225 cookie->ipfix.output_odp_port = output_odp_port;
2228 /* Compose SAMPLE action for IPFIX bridge sampling. */
2230 compose_ipfix_action(const struct xbridge *xbridge,
2231 struct ofpbuf *odp_actions,
2232 const struct flow *flow,
2233 odp_port_t output_odp_port)
2235 uint32_t probability;
2236 union user_action_cookie cookie;
2237 odp_port_t tunnel_out_port = ODPP_NONE;
2239 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
2243 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2245 if (output_odp_port == ODPP_NONE &&
2246 !dpif_ipfix_get_bridge_exporter_input_sampling(xbridge->ipfix)) {
2250 /* For output case, output_odp_port is valid*/
2251 if (output_odp_port != ODPP_NONE) {
2252 if (!dpif_ipfix_get_bridge_exporter_output_sampling(xbridge->ipfix)) {
2255 /* If tunnel sampling is enabled, put an additional option attribute:
2256 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2258 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(xbridge->ipfix) &&
2259 dpif_ipfix_get_tunnel_port(xbridge->ipfix, output_odp_port) ) {
2260 tunnel_out_port = output_odp_port;
2264 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
2265 compose_ipfix_cookie(&cookie, output_odp_port);
2267 compose_sample_action(xbridge, odp_actions, flow, probability,
2268 &cookie, sizeof cookie.ipfix, tunnel_out_port);
2271 /* SAMPLE action for sFlow must be first action in any given list of
2272 * actions. At this point we do not have all information required to
2273 * build it. So try to build sample action as complete as possible. */
2275 add_sflow_action(struct xlate_ctx *ctx)
2277 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
2278 ctx->xout->odp_actions,
2279 &ctx->xin->flow, ODPP_NONE);
2280 ctx->sflow_odp_port = 0;
2281 ctx->sflow_n_outputs = 0;
2284 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
2285 * of actions, eventually after the SAMPLE action for sFlow. */
2287 add_ipfix_action(struct xlate_ctx *ctx)
2289 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2290 &ctx->xin->flow, ODPP_NONE);
2294 add_ipfix_output_action(struct xlate_ctx *ctx, odp_port_t port)
2296 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2297 &ctx->xin->flow, port);
2300 /* Fix SAMPLE action according to data collected while composing ODP actions.
2301 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
2302 * USERSPACE action's user-cookie which is required for sflow. */
2304 fix_sflow_action(struct xlate_ctx *ctx)
2306 const struct flow *base = &ctx->base_flow;
2307 union user_action_cookie *cookie;
2309 if (!ctx->user_cookie_offset) {
2313 cookie = ofpbuf_at(ctx->xout->odp_actions, ctx->user_cookie_offset,
2314 sizeof cookie->sflow);
2315 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2317 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
2318 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
2321 static enum slow_path_reason
2322 process_special(struct xlate_ctx *ctx, const struct flow *flow,
2323 const struct xport *xport, const struct ofpbuf *packet)
2325 struct flow_wildcards *wc = &ctx->xout->wc;
2326 const struct xbridge *xbridge = ctx->xbridge;
2330 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
2332 cfm_process_heartbeat(xport->cfm, packet);
2335 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
2337 bfd_process_packet(xport->bfd, flow, packet);
2338 /* If POLL received, immediately sends FINAL back. */
2339 if (bfd_should_send_packet(xport->bfd)) {
2340 ofproto_dpif_monitor_port_send_soon(xport->ofport);
2344 } else if (xport->xbundle && xport->xbundle->lacp
2345 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2347 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
2350 } else if (xbridge->stp && stp_should_process_flow(flow, wc)) {
2352 stp_process_packet(xport, packet);
2361 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
2364 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
2365 struct flow_wildcards *wc = &ctx->xout->wc;
2366 struct flow *flow = &ctx->xin->flow;
2367 ovs_be16 flow_vlan_tci;
2368 uint32_t flow_pkt_mark;
2369 uint8_t flow_nw_tos;
2370 odp_port_t out_port, odp_port;
2373 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2374 * before traversing a patch port. */
2375 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 27);
2378 xlate_report(ctx, "Nonexistent output port");
2380 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
2381 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2383 } else if (check_stp) {
2384 if (is_stp(&ctx->base_flow)) {
2385 if (!xport_stp_should_forward_bpdu(xport)) {
2386 xlate_report(ctx, "STP not in listening state, "
2387 "skipping bpdu output");
2390 } else if (!xport_stp_forward_state(xport)) {
2391 xlate_report(ctx, "STP not in forwarding state, "
2397 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
2398 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
2403 const struct xport *peer = xport->peer;
2404 struct flow old_flow = ctx->xin->flow;
2405 enum slow_path_reason special;
2407 ctx->xbridge = peer->xbridge;
2408 flow->in_port.ofp_port = peer->ofp_port;
2409 flow->metadata = htonll(0);
2410 memset(&flow->tunnel, 0, sizeof flow->tunnel);
2411 memset(flow->regs, 0, sizeof flow->regs);
2413 special = process_special(ctx, &ctx->xin->flow, peer,
2416 ctx->xout->slow |= special;
2417 } else if (may_receive(peer, ctx)) {
2418 if (xport_stp_forward_state(peer)) {
2419 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2421 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
2422 * learning action look at the packet, then drop it. */
2423 struct flow old_base_flow = ctx->base_flow;
2424 size_t old_size = ofpbuf_size(ctx->xout->odp_actions);
2425 mirror_mask_t old_mirrors = ctx->xout->mirrors;
2426 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2427 ctx->xout->mirrors = old_mirrors;
2428 ctx->base_flow = old_base_flow;
2429 ofpbuf_set_size(ctx->xout->odp_actions, old_size);
2433 ctx->xin->flow = old_flow;
2434 ctx->xbridge = xport->xbridge;
2436 if (ctx->xin->resubmit_stats) {
2437 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2438 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
2440 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
2443 if (ctx->xin->xcache) {
2444 struct xc_entry *entry;
2446 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2447 entry->u.dev.tx = netdev_ref(xport->netdev);
2448 entry->u.dev.rx = netdev_ref(peer->netdev);
2449 entry->u.dev.bfd = bfd_ref(peer->bfd);
2454 flow_vlan_tci = flow->vlan_tci;
2455 flow_pkt_mark = flow->pkt_mark;
2456 flow_nw_tos = flow->nw_tos;
2458 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
2459 wc->masks.nw_tos |= IP_DSCP_MASK;
2460 flow->nw_tos &= ~IP_DSCP_MASK;
2461 flow->nw_tos |= dscp;
2464 if (xport->is_tunnel) {
2465 /* Save tunnel metadata so that changes made due to
2466 * the Logical (tunnel) Port are not visible for any further
2467 * matches, while explicit set actions on tunnel metadata are.
2469 struct flow_tnl flow_tnl = flow->tunnel;
2470 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
2471 if (odp_port == ODPP_NONE) {
2472 xlate_report(ctx, "Tunneling decided against output");
2473 goto out; /* restore flow_nw_tos */
2475 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
2476 xlate_report(ctx, "Not tunneling to our own address");
2477 goto out; /* restore flow_nw_tos */
2479 if (ctx->xin->resubmit_stats) {
2480 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2482 if (ctx->xin->xcache) {
2483 struct xc_entry *entry;
2485 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2486 entry->u.dev.tx = netdev_ref(xport->netdev);
2488 out_port = odp_port;
2489 commit_odp_tunnel_action(flow, &ctx->base_flow,
2490 ctx->xout->odp_actions);
2491 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2493 odp_port = xport->odp_port;
2494 out_port = odp_port;
2495 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
2496 ofp_port_t vlandev_port;
2498 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2499 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
2500 ofp_port, flow->vlan_tci);
2501 if (vlandev_port != ofp_port) {
2502 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
2503 flow->vlan_tci = htons(0);
2508 if (out_port != ODPP_NONE) {
2509 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
2510 ctx->xout->odp_actions,
2513 if (ctx->use_recirc) {
2514 struct ovs_action_hash *act_hash;
2515 struct xlate_recirc *xr = &ctx->recirc;
2518 act_hash = nl_msg_put_unspec_uninit(ctx->xout->odp_actions,
2519 OVS_ACTION_ATTR_HASH,
2521 act_hash->hash_alg = xr->hash_alg;
2522 act_hash->hash_basis = xr->hash_basis;
2524 /* Recirc action. */
2525 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC,
2528 add_ipfix_output_action(ctx, out_port);
2529 nl_msg_put_odp_port(ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
2533 ctx->sflow_odp_port = odp_port;
2534 ctx->sflow_n_outputs++;
2535 ctx->xout->nf_output_iface = ofp_port;
2540 flow->vlan_tci = flow_vlan_tci;
2541 flow->pkt_mark = flow_pkt_mark;
2542 flow->nw_tos = flow_nw_tos;
2546 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
2548 compose_output_action__(ctx, ofp_port, true);
2552 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
2554 struct rule_dpif *old_rule = ctx->rule;
2555 const struct rule_actions *actions;
2557 if (ctx->xin->resubmit_stats) {
2558 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
2564 actions = rule_dpif_get_actions(rule);
2565 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
2566 ctx->rule = old_rule;
2571 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
2573 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2575 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
2576 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
2577 MAX_RESUBMIT_RECURSION);
2578 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
2579 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
2580 } else if (ofpbuf_size(ctx->xout->odp_actions) > UINT16_MAX) {
2581 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
2582 } else if (ofpbuf_size(&ctx->stack) >= 65536) {
2583 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
2592 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
2593 bool may_packet_in, bool honor_table_miss)
2595 if (xlate_resubmit_resource_check(ctx)) {
2596 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
2597 bool skip_wildcards = ctx->xin->skip_wildcards;
2598 uint8_t old_table_id = ctx->table_id;
2599 struct rule_dpif *rule;
2600 enum rule_dpif_lookup_verdict verdict;
2601 enum ofputil_port_config config = 0;
2603 ctx->table_id = table_id;
2605 /* Look up a flow with 'in_port' as the input port. Then restore the
2606 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2607 * have surprising behavior). */
2608 ctx->xin->flow.in_port.ofp_port = in_port;
2609 verdict = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
2612 ? &ctx->xout->wc : NULL,
2614 &ctx->table_id, &rule,
2615 ctx->xin->xcache != NULL,
2616 ctx->xin->resubmit_stats);
2617 ctx->xin->flow.in_port.ofp_port = old_in_port;
2619 if (ctx->xin->resubmit_hook) {
2620 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
2624 case RULE_DPIF_LOOKUP_VERDICT_MATCH:
2626 case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER:
2627 if (may_packet_in) {
2628 struct xport *xport;
2630 xport = get_ofp_port(ctx->xbridge,
2631 ctx->xin->flow.in_port.ofp_port);
2632 config = xport ? xport->config : 0;
2635 /* Fall through to drop */
2636 case RULE_DPIF_LOOKUP_VERDICT_DROP:
2637 config = OFPUTIL_PC_NO_PACKET_IN;
2639 case RULE_DPIF_LOOKUP_VERDICT_DEFAULT:
2640 if (!ofproto_dpif_wants_packet_in_on_miss(ctx->xbridge->ofproto)) {
2641 config = OFPUTIL_PC_NO_PACKET_IN;
2648 choose_miss_rule(config, ctx->xbridge->miss_rule,
2649 ctx->xbridge->no_packet_in_rule, &rule,
2650 ctx->xin->xcache != NULL);
2654 /* Fill in the cache entry here instead of xlate_recursively
2655 * to make the reference counting more explicit. We take a
2656 * reference in the lookups above if we are going to cache the
2658 if (ctx->xin->xcache) {
2659 struct xc_entry *entry;
2661 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
2662 entry->u.rule = rule;
2664 xlate_recursively(ctx, rule);
2667 ctx->table_id = old_table_id;
2675 xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
2676 struct ofputil_bucket *bucket)
2678 if (ctx->xin->resubmit_stats) {
2679 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
2681 if (ctx->xin->xcache) {
2682 struct xc_entry *entry;
2684 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
2685 entry->u.group.group = group_dpif_ref(group);
2686 entry->u.group.bucket = bucket;
2691 xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
2693 uint64_t action_list_stub[1024 / 8];
2694 struct ofpbuf action_list, action_set;
2696 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
2697 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2699 ofpacts_execute_action_set(&action_list, &action_set);
2701 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
2704 ofpbuf_uninit(&action_set);
2705 ofpbuf_uninit(&action_list);
2709 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
2711 struct ofputil_bucket *bucket;
2712 const struct list *buckets;
2713 struct flow old_flow = ctx->xin->flow;
2715 group_dpif_get_buckets(group, &buckets);
2717 LIST_FOR_EACH (bucket, list_node, buckets) {
2718 xlate_group_bucket(ctx, bucket);
2719 /* Roll back flow to previous state.
2720 * This is equivalent to cloning the packet for each bucket.
2722 * As a side effect any subsequently applied actions will
2723 * also effectively be applied to a clone of the packet taken
2724 * just before applying the all or indirect group. */
2725 ctx->xin->flow = old_flow;
2727 xlate_group_stats(ctx, group, NULL);
2731 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
2733 struct ofputil_bucket *bucket;
2735 bucket = group_first_live_bucket(ctx, group, 0);
2737 xlate_group_bucket(ctx, bucket);
2738 xlate_group_stats(ctx, group, bucket);
2743 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
2745 struct flow_wildcards *wc = &ctx->xout->wc;
2746 struct ofputil_bucket *bucket;
2749 basis = hash_mac(ctx->xin->flow.dl_dst, 0, 0);
2750 bucket = group_best_live_bucket(ctx, group, basis);
2752 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2753 xlate_group_bucket(ctx, bucket);
2754 xlate_group_stats(ctx, group, bucket);
2759 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
2761 ctx->in_group = true;
2763 switch (group_dpif_get_type(group)) {
2765 case OFPGT11_INDIRECT:
2766 xlate_all_group(ctx, group);
2768 case OFPGT11_SELECT:
2769 xlate_select_group(ctx, group);
2772 xlate_ff_group(ctx, group);
2777 group_dpif_unref(group);
2779 ctx->in_group = false;
2783 xlate_group_resource_check(struct xlate_ctx *ctx)
2785 if (!xlate_resubmit_resource_check(ctx)) {
2787 } else if (ctx->in_group) {
2788 /* Prevent nested translation of OpenFlow groups.
2790 * OpenFlow allows this restriction. We enforce this restriction only
2791 * because, with the current architecture, we would otherwise have to
2792 * take a possibly recursive read lock on the ofgroup rwlock, which is
2793 * unsafe given that POSIX allows taking a read lock to block if there
2794 * is a thread blocked on taking the write lock. Other solutions
2795 * without this restriction are also possible, but seem unwarranted
2796 * given the current limited use of groups. */
2797 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2799 VLOG_ERR_RL(&rl, "cannot recursively translate OpenFlow group");
2807 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
2809 if (xlate_group_resource_check(ctx)) {
2810 struct group_dpif *group;
2813 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
2815 xlate_group_action__(ctx, group);
2825 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
2826 const struct ofpact_resubmit *resubmit)
2830 bool may_packet_in = false;
2831 bool honor_table_miss = false;
2833 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
2834 /* Still allow missed packets to be sent to the controller
2835 * if resubmitting from an internal table. */
2836 may_packet_in = true;
2837 honor_table_miss = true;
2840 in_port = resubmit->in_port;
2841 if (in_port == OFPP_IN_PORT) {
2842 in_port = ctx->xin->flow.in_port.ofp_port;
2845 table_id = resubmit->table_id;
2846 if (table_id == 255) {
2847 table_id = ctx->table_id;
2850 xlate_table_action(ctx, in_port, table_id, may_packet_in,
2855 flood_packets(struct xlate_ctx *ctx, bool all)
2857 const struct xport *xport;
2859 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
2860 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
2865 compose_output_action__(ctx, xport->ofp_port, false);
2866 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
2867 compose_output_action(ctx, xport->ofp_port);
2871 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2875 execute_controller_action(struct xlate_ctx *ctx, int len,
2876 enum ofp_packet_in_reason reason,
2877 uint16_t controller_id)
2879 struct ofproto_packet_in *pin;
2880 struct dpif_packet *packet;
2881 struct pkt_metadata md = PKT_METADATA_INITIALIZER(0);
2883 ctx->xout->slow |= SLOW_CONTROLLER;
2884 if (!ctx->xin->packet) {
2888 packet = dpif_packet_clone_from_ofpbuf(ctx->xin->packet);
2890 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2891 ctx->xout->odp_actions,
2894 odp_execute_actions(NULL, &packet, 1, false, &md,
2895 ofpbuf_data(ctx->xout->odp_actions),
2896 ofpbuf_size(ctx->xout->odp_actions), NULL);
2898 pin = xmalloc(sizeof *pin);
2899 pin->up.packet_len = ofpbuf_size(&packet->ofpbuf);
2900 pin->up.packet = ofpbuf_steal_data(&packet->ofpbuf);
2901 pin->up.reason = reason;
2902 pin->up.table_id = ctx->table_id;
2903 pin->up.cookie = (ctx->rule
2904 ? rule_dpif_get_flow_cookie(ctx->rule)
2907 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
2909 pin->controller_id = controller_id;
2910 pin->send_len = len;
2911 /* If a rule is a table-miss rule then this is
2912 * a table-miss handled by a table-miss rule.
2914 * Else, if rule is internal and has a controller action,
2915 * the later being implied by the rule being processed here,
2916 * then this is a table-miss handled without a table-miss rule.
2918 * Otherwise this is not a table-miss. */
2919 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
2921 if (rule_dpif_is_table_miss(ctx->rule)) {
2922 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
2923 } else if (rule_dpif_is_internal(ctx->rule)) {
2924 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
2927 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
2928 dpif_packet_delete(packet);
2932 compose_recirculate_action(struct xlate_ctx *ctx,
2933 const struct ofpact *ofpacts_base,
2934 const struct ofpact *ofpact_current,
2935 size_t ofpacts_base_len)
2939 unsigned ofpacts_len;
2942 struct ofpbuf ofpacts;
2946 ofpacts_len = ofpacts_base_len -
2947 ((uint8_t *)ofpact_current - (uint8_t *)ofpacts_base);
2950 id = rule_dpif_get_recirc_id(ctx->rule);
2952 /* In the case where ctx has no rule then allocate a recirc id.
2953 * The life-cycle of this recirc id is managed by associating it
2954 * with the internal rule that is created to to handle
2955 * recirculation below.
2957 * The known use-case of this is packet_out which
2958 * translates actions without a rule */
2959 id = ofproto_dpif_alloc_recirc_id(ctx->xbridge->ofproto);
2962 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2963 VLOG_ERR_RL(&rl, "Failed to allocate recirculation id");
2967 match_init_catchall(&match);
2968 match_set_recirc_id(&match, id);
2969 ofpbuf_use_const(&ofpacts, ofpact_current, ofpacts_len);
2970 error = ofproto_dpif_add_internal_flow(ctx->xbridge->ofproto, &match,
2971 RECIRC_RULE_PRIORITY,
2972 RECIRC_TIMEOUT, &ofpacts, &rule);
2974 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2975 VLOG_ERR_RL(&rl, "Failed to add post recirculation flow %s",
2976 match_to_string(&match, 0));
2979 /* If ctx has no rule then associate the recirc id, which
2980 * was allocated above, with the internal rule. This allows
2981 * the recirc id to be released when the internal rule times out. */
2983 rule_set_recirc_id(rule, id);
2986 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2987 ctx->xout->odp_actions,
2989 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
2993 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
2995 struct flow_wildcards *wc = &ctx->xout->wc;
2996 struct flow *flow = &ctx->xin->flow;
2999 ovs_assert(eth_type_mpls(mpls->ethertype));
3001 n = flow_count_mpls_labels(flow, wc);
3003 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
3004 ctx->xout->odp_actions,
3006 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3007 if (ctx->xin->packet != NULL) {
3008 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3009 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3010 "MPLS push action can't be performed as it would "
3011 "have more MPLS LSEs than the %d supported.",
3012 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3016 } else if (n >= ctx->xbridge->max_mpls_depth) {
3017 COVERAGE_INC(xlate_actions_mpls_overflow);
3018 ctx->xout->slow |= SLOW_ACTION;
3021 flow_push_mpls(flow, n, mpls->ethertype, wc);
3025 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
3027 struct flow_wildcards *wc = &ctx->xout->wc;
3028 struct flow *flow = &ctx->xin->flow;
3029 int n = flow_count_mpls_labels(flow, wc);
3031 if (flow_pop_mpls(flow, n, eth_type, wc)) {
3032 if (ctx->xbridge->enable_recirc && !eth_type_mpls(eth_type)) {
3033 ctx->was_mpls = true;
3035 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3036 if (ctx->xin->packet != NULL) {
3037 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3038 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3039 "MPLS pop action can't be performed as it has "
3040 "more MPLS LSEs than the %d supported.",
3041 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3044 ofpbuf_clear(ctx->xout->odp_actions);
3049 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3051 struct flow *flow = &ctx->xin->flow;
3053 if (!is_ip_any(flow)) {
3057 ctx->xout->wc.masks.nw_ttl = 0xff;
3058 if (flow->nw_ttl > 1) {
3064 for (i = 0; i < ids->n_controllers; i++) {
3065 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3069 /* Stop processing for current table. */
3075 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3077 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3078 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
3079 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
3084 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3086 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3087 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
3088 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
3093 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
3095 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3096 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3097 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
3102 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
3104 struct flow *flow = &ctx->xin->flow;
3105 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
3106 struct flow_wildcards *wc = &ctx->xout->wc;
3108 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
3109 if (eth_type_mpls(flow->dl_type)) {
3112 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3115 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
3117 /* Stop processing for current table. */
3126 xlate_output_action(struct xlate_ctx *ctx,
3127 ofp_port_t port, uint16_t max_len, bool may_packet_in)
3129 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
3131 ctx->xout->nf_output_iface = NF_OUT_DROP;
3135 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
3138 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3139 0, may_packet_in, true);
3145 flood_packets(ctx, false);
3148 flood_packets(ctx, true);
3150 case OFPP_CONTROLLER:
3151 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
3157 if (port != ctx->xin->flow.in_port.ofp_port) {
3158 compose_output_action(ctx, port);
3160 xlate_report(ctx, "skipping output to input port");
3165 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3166 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3167 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3168 ctx->xout->nf_output_iface = prev_nf_output_iface;
3169 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3170 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3171 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3176 xlate_output_reg_action(struct xlate_ctx *ctx,
3177 const struct ofpact_output_reg *or)
3179 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3180 if (port <= UINT16_MAX) {
3181 union mf_subvalue value;
3183 memset(&value, 0xff, sizeof value);
3184 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
3185 xlate_output_action(ctx, u16_to_ofp(port),
3186 or->max_len, false);
3191 xlate_enqueue_action(struct xlate_ctx *ctx,
3192 const struct ofpact_enqueue *enqueue)
3194 ofp_port_t ofp_port = enqueue->port;
3195 uint32_t queue_id = enqueue->queue;
3196 uint32_t flow_priority, priority;
3199 /* Translate queue to priority. */
3200 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
3202 /* Fall back to ordinary output action. */
3203 xlate_output_action(ctx, enqueue->port, 0, false);
3207 /* Check output port. */
3208 if (ofp_port == OFPP_IN_PORT) {
3209 ofp_port = ctx->xin->flow.in_port.ofp_port;
3210 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
3214 /* Add datapath actions. */
3215 flow_priority = ctx->xin->flow.skb_priority;
3216 ctx->xin->flow.skb_priority = priority;
3217 compose_output_action(ctx, ofp_port);
3218 ctx->xin->flow.skb_priority = flow_priority;
3220 /* Update NetFlow output port. */
3221 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3222 ctx->xout->nf_output_iface = ofp_port;
3223 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3224 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3229 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3231 uint32_t skb_priority;
3233 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
3234 ctx->xin->flow.skb_priority = skb_priority;
3236 /* Couldn't translate queue to a priority. Nothing to do. A warning
3237 * has already been logged. */
3242 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
3244 const struct xbridge *xbridge = xbridge_;
3255 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3258 port = get_ofp_port(xbridge, ofp_port);
3259 return port ? port->may_enable : false;
3264 xlate_bundle_action(struct xlate_ctx *ctx,
3265 const struct ofpact_bundle *bundle)
3269 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
3271 CONST_CAST(struct xbridge *, ctx->xbridge));
3272 if (bundle->dst.field) {
3273 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
3276 xlate_output_action(ctx, port, 0, false);
3281 xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
3282 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
3284 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
3285 if (ctx->xin->may_learn) {
3286 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
3291 xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
3293 ctx->xout->has_learn = true;
3294 learn_mask(learn, &ctx->xout->wc);
3296 if (ctx->xin->xcache) {
3297 struct xc_entry *entry;
3299 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
3300 entry->u.learn.ofproto = ctx->xbridge->ofproto;
3301 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
3302 entry->u.learn.ofpacts = ofpbuf_new(64);
3303 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
3304 entry->u.learn.ofpacts);
3305 } else if (ctx->xin->may_learn) {
3306 uint64_t ofpacts_stub[1024 / 8];
3307 struct ofputil_flow_mod fm;
3308 struct ofpbuf ofpacts;
3310 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3311 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
3312 ofpbuf_uninit(&ofpacts);
3317 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
3318 uint16_t idle_timeout, uint16_t hard_timeout)
3320 if (tcp_flags & (TCP_FIN | TCP_RST)) {
3321 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
3326 xlate_fin_timeout(struct xlate_ctx *ctx,
3327 const struct ofpact_fin_timeout *oft)
3330 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
3331 oft->fin_idle_timeout, oft->fin_hard_timeout);
3332 if (ctx->xin->xcache) {
3333 struct xc_entry *entry;
3335 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
3336 /* XC_RULE already holds a reference on the rule, none is taken
3338 entry->u.fin.rule = ctx->rule;
3339 entry->u.fin.idle = oft->fin_idle_timeout;
3340 entry->u.fin.hard = oft->fin_hard_timeout;
3346 xlate_sample_action(struct xlate_ctx *ctx,
3347 const struct ofpact_sample *os)
3349 union user_action_cookie cookie;
3350 /* Scale the probability from 16-bit to 32-bit while representing
3351 * the same percentage. */
3352 uint32_t probability = (os->probability << 16) | os->probability;
3354 if (!ctx->xbridge->variable_length_userdata) {
3355 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3357 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
3358 "lacks support (needs Linux 3.10+ or kernel module from "
3363 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3364 ctx->xout->odp_actions,
3367 compose_flow_sample_cookie(os->probability, os->collector_set_id,
3368 os->obs_domain_id, os->obs_point_id, &cookie);
3369 compose_sample_action(ctx->xbridge, ctx->xout->odp_actions, &ctx->xin->flow,
3370 probability, &cookie, sizeof cookie.flow_sample,
3375 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
3377 if (xport->config & (is_stp(&ctx->xin->flow)
3378 ? OFPUTIL_PC_NO_RECV_STP
3379 : OFPUTIL_PC_NO_RECV)) {
3383 /* Only drop packets here if both forwarding and learning are
3384 * disabled. If just learning is enabled, we need to have
3385 * OFPP_NORMAL and the learning action have a look at the packet
3386 * before we can drop it. */
3387 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
3395 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
3397 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3398 ofpbuf_put(&ctx->action_set, on->actions, ofpact_nest_get_action_len(on));
3399 ofpact_pad(&ctx->action_set);
3403 xlate_action_set(struct xlate_ctx *ctx)
3405 uint64_t action_list_stub[1024 / 64];
3406 struct ofpbuf action_list;
3408 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3409 ofpacts_execute_action_set(&action_list, &ctx->action_set);
3410 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
3411 ofpbuf_uninit(&action_list);
3415 ofpact_needs_recirculation_after_mpls(const struct xlate_ctx *ctx,
3416 const struct ofpact *a)
3418 struct flow_wildcards *wc = &ctx->xout->wc;
3419 struct flow *flow = &ctx->xin->flow;
3424 case OFPACT_CONTROLLER:
3425 case OFPACT_STRIP_VLAN:
3426 case OFPACT_SET_VLAN_PCP:
3427 case OFPACT_SET_VLAN_VID:
3428 case OFPACT_ENQUEUE:
3429 case OFPACT_PUSH_VLAN:
3430 case OFPACT_SET_ETH_SRC:
3431 case OFPACT_SET_ETH_DST:
3432 case OFPACT_SET_TUNNEL:
3433 case OFPACT_SET_QUEUE:
3434 case OFPACT_POP_QUEUE:
3435 case OFPACT_POP_MPLS:
3436 case OFPACT_DEC_MPLS_TTL:
3437 case OFPACT_SET_MPLS_TTL:
3438 case OFPACT_SET_MPLS_TC:
3439 case OFPACT_SET_MPLS_LABEL:
3441 case OFPACT_OUTPUT_REG:
3444 case OFPACT_WRITE_METADATA:
3445 case OFPACT_WRITE_ACTIONS:
3446 case OFPACT_CLEAR_ACTIONS:
3450 case OFPACT_SET_IPV4_SRC:
3451 case OFPACT_SET_IPV4_DST:
3452 case OFPACT_SET_IP_DSCP:
3453 case OFPACT_SET_IP_ECN:
3454 case OFPACT_SET_IP_TTL:
3455 case OFPACT_SET_L4_SRC_PORT:
3456 case OFPACT_SET_L4_DST_PORT:
3457 case OFPACT_RESUBMIT:
3458 case OFPACT_STACK_PUSH:
3459 case OFPACT_STACK_POP:
3460 case OFPACT_DEC_TTL:
3461 case OFPACT_MULTIPATH:
3464 case OFPACT_FIN_TIMEOUT:
3465 case OFPACT_GOTO_TABLE:
3468 case OFPACT_REG_MOVE:
3469 return (mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
3470 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
3472 case OFPACT_REG_LOAD:
3473 return mf_is_l3_or_higher(ofpact_get_REG_LOAD(a)->dst.field);
3475 case OFPACT_SET_FIELD:
3476 return mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field);
3478 case OFPACT_PUSH_MPLS:
3479 /* Recirculate if it is an IP packet with a zero ttl. This may
3480 * indicate that the packet was previously MPLS and an MPLS pop action
3481 * converted it to IP. In this case recirculating should reveal the IP
3482 * TTL which is used as the basis for a new MPLS LSE. */
3483 return (!flow_count_mpls_labels(flow, wc)
3484 && flow->nw_ttl == 0
3485 && is_ip_any(flow));
3492 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
3493 struct xlate_ctx *ctx)
3495 struct flow_wildcards *wc = &ctx->xout->wc;
3496 struct flow *flow = &ctx->xin->flow;
3497 const struct ofpact *a;
3499 /* dl_type already in the mask, not set below. */
3501 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3502 struct ofpact_controller *controller;
3503 const struct ofpact_metadata *metadata;
3504 const struct ofpact_set_field *set_field;
3505 const struct mf_field *mf;
3511 if (ctx->was_mpls && ofpact_needs_recirculation_after_mpls(ctx, a)) {
3512 compose_recirculate_action(ctx, ofpacts, a, ofpacts_len);
3518 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
3519 ofpact_get_OUTPUT(a)->max_len, true);
3523 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
3528 case OFPACT_CONTROLLER:
3529 controller = ofpact_get_CONTROLLER(a);
3530 execute_controller_action(ctx, controller->max_len,
3532 controller->controller_id);
3535 case OFPACT_ENQUEUE:
3536 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
3539 case OFPACT_SET_VLAN_VID:
3540 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
3541 if (flow->vlan_tci & htons(VLAN_CFI) ||
3542 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
3543 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
3544 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
3549 case OFPACT_SET_VLAN_PCP:
3550 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
3551 if (flow->vlan_tci & htons(VLAN_CFI) ||
3552 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
3553 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
3554 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
3555 << VLAN_PCP_SHIFT) | VLAN_CFI);
3559 case OFPACT_STRIP_VLAN:
3560 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3561 flow->vlan_tci = htons(0);
3564 case OFPACT_PUSH_VLAN:
3565 /* XXX 802.1AD(QinQ) */
3566 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3567 flow->vlan_tci = htons(VLAN_CFI);
3570 case OFPACT_SET_ETH_SRC:
3571 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3572 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
3575 case OFPACT_SET_ETH_DST:
3576 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3577 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
3580 case OFPACT_SET_IPV4_SRC:
3581 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3582 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3583 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
3587 case OFPACT_SET_IPV4_DST:
3588 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3589 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3590 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
3594 case OFPACT_SET_IP_DSCP:
3595 if (is_ip_any(flow)) {
3596 wc->masks.nw_tos |= IP_DSCP_MASK;
3597 flow->nw_tos &= ~IP_DSCP_MASK;
3598 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
3602 case OFPACT_SET_IP_ECN:
3603 if (is_ip_any(flow)) {
3604 wc->masks.nw_tos |= IP_ECN_MASK;
3605 flow->nw_tos &= ~IP_ECN_MASK;
3606 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
3610 case OFPACT_SET_IP_TTL:
3611 if (is_ip_any(flow)) {
3612 wc->masks.nw_ttl = 0xff;
3613 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
3617 case OFPACT_SET_L4_SRC_PORT:
3618 if (is_ip_any(flow)) {
3619 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3620 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3621 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
3625 case OFPACT_SET_L4_DST_PORT:
3626 if (is_ip_any(flow)) {
3627 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3628 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3629 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
3633 case OFPACT_RESUBMIT:
3634 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
3637 case OFPACT_SET_TUNNEL:
3638 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
3641 case OFPACT_SET_QUEUE:
3642 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
3645 case OFPACT_POP_QUEUE:
3646 flow->skb_priority = ctx->orig_skb_priority;
3649 case OFPACT_REG_MOVE:
3650 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
3653 case OFPACT_REG_LOAD:
3654 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow, wc);
3657 case OFPACT_SET_FIELD:
3658 set_field = ofpact_get_SET_FIELD(a);
3659 mf = set_field->field;
3661 /* Set field action only ever overwrites packet's outermost
3662 * applicable header fields. Do nothing if no header exists. */
3663 if (mf->id == MFF_VLAN_VID) {
3664 wc->masks.vlan_tci |= htons(VLAN_CFI);
3665 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
3668 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
3669 /* 'dl_type' is already unwildcarded. */
3670 && !eth_type_mpls(flow->dl_type)) {
3674 mf_mask_field_and_prereqs(mf, &wc->masks);
3675 mf_set_flow_value(mf, &set_field->value, flow);
3678 case OFPACT_STACK_PUSH:
3679 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
3683 case OFPACT_STACK_POP:
3684 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
3688 case OFPACT_PUSH_MPLS:
3689 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
3692 case OFPACT_POP_MPLS:
3693 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
3696 case OFPACT_SET_MPLS_LABEL:
3697 compose_set_mpls_label_action(
3698 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
3701 case OFPACT_SET_MPLS_TC:
3702 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
3705 case OFPACT_SET_MPLS_TTL:
3706 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
3709 case OFPACT_DEC_MPLS_TTL:
3710 if (compose_dec_mpls_ttl_action(ctx)) {
3715 case OFPACT_DEC_TTL:
3716 wc->masks.nw_ttl = 0xff;
3717 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
3723 /* Nothing to do. */
3726 case OFPACT_MULTIPATH:
3727 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
3731 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
3734 case OFPACT_OUTPUT_REG:
3735 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
3739 xlate_learn_action(ctx, ofpact_get_LEARN(a));
3746 case OFPACT_FIN_TIMEOUT:
3747 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3748 ctx->xout->has_fin_timeout = true;
3749 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
3752 case OFPACT_CLEAR_ACTIONS:
3753 ofpbuf_clear(&ctx->action_set);
3756 case OFPACT_WRITE_ACTIONS:
3757 xlate_write_actions(ctx, a);
3760 case OFPACT_WRITE_METADATA:
3761 metadata = ofpact_get_WRITE_METADATA(a);
3762 flow->metadata &= ~metadata->mask;
3763 flow->metadata |= metadata->metadata & metadata->mask;
3767 /* Not implemented yet. */
3770 case OFPACT_GOTO_TABLE: {
3771 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
3773 /* Allow ctx->table_id == TBL_INTERNAL, which will be greater
3774 * than ogt->table_id. This is to allow goto_table actions that
3775 * triggered recirculation: ctx->table_id will be TBL_INTERNAL
3776 * after recirculation. */
3777 ovs_assert(ctx->table_id == TBL_INTERNAL
3778 || ctx->table_id < ogt->table_id);
3779 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3780 ogt->table_id, true, true);
3785 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
3792 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
3793 const struct flow *flow, ofp_port_t in_port,
3794 struct rule_dpif *rule, uint16_t tcp_flags,
3795 const struct ofpbuf *packet)
3797 xin->ofproto = ofproto;
3799 xin->flow.in_port.ofp_port = in_port;
3800 xin->packet = packet;
3801 xin->may_learn = packet != NULL;
3804 xin->ofpacts = NULL;
3805 xin->ofpacts_len = 0;
3806 xin->tcp_flags = tcp_flags;
3807 xin->resubmit_hook = NULL;
3808 xin->report_hook = NULL;
3809 xin->resubmit_stats = NULL;
3810 xin->skip_wildcards = false;
3811 xin->odp_actions = NULL;
3815 xlate_out_uninit(struct xlate_out *xout)
3817 if (xout && xout->odp_actions == &xout->odp_actions_buf) {
3818 ofpbuf_uninit(xout->odp_actions);
3822 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
3823 * into datapath actions, using 'ctx', and discards the datapath actions. */
3825 xlate_actions_for_side_effects(struct xlate_in *xin)
3827 struct xlate_out xout;
3829 xlate_actions(xin, &xout);
3830 xlate_out_uninit(&xout);
3834 xlate_report(struct xlate_ctx *ctx, const char *s)
3836 if (ctx->xin->report_hook) {
3837 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
3842 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
3845 dst->slow = src->slow;
3846 dst->has_learn = src->has_learn;
3847 dst->has_normal = src->has_normal;
3848 dst->has_fin_timeout = src->has_fin_timeout;
3849 dst->nf_output_iface = src->nf_output_iface;
3850 dst->mirrors = src->mirrors;
3852 dst->odp_actions = &dst->odp_actions_buf;
3853 ofpbuf_use_stub(dst->odp_actions, dst->odp_actions_stub,
3854 sizeof dst->odp_actions_stub);
3855 ofpbuf_put(dst->odp_actions, ofpbuf_data(src->odp_actions),
3856 ofpbuf_size(src->odp_actions));
3859 static struct skb_priority_to_dscp *
3860 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
3862 struct skb_priority_to_dscp *pdscp;
3865 hash = hash_int(skb_priority, 0);
3866 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
3867 if (pdscp->skb_priority == skb_priority) {
3875 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
3878 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
3879 *dscp = pdscp ? pdscp->dscp : 0;
3880 return pdscp != NULL;
3884 clear_skb_priorities(struct xport *xport)
3886 struct skb_priority_to_dscp *pdscp, *next;
3888 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
3889 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
3895 actions_output_to_local_port(const struct xlate_ctx *ctx)
3897 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
3898 const struct nlattr *a;
3901 NL_ATTR_FOR_EACH_UNSAFE (a, left, ofpbuf_data(ctx->xout->odp_actions),
3902 ofpbuf_size(ctx->xout->odp_actions)) {
3903 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
3904 && nl_attr_get_odp_port(a) == local_odp_port) {
3911 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
3912 * into datapath actions in 'odp_actions', using 'ctx'.
3914 * The caller must take responsibility for eventually freeing 'xout', with
3915 * xlate_out_uninit(). */
3917 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
3919 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
3920 struct flow_wildcards *wc = &xout->wc;
3921 struct flow *flow = &xin->flow;
3922 struct rule_dpif *rule = NULL;
3924 const struct rule_actions *actions = NULL;
3925 enum slow_path_reason special;
3926 const struct ofpact *ofpacts;
3927 struct xport *in_port;
3928 struct flow orig_flow;
3929 struct xlate_ctx ctx;
3934 COVERAGE_INC(xlate_actions);
3936 /* Flow initialization rules:
3937 * - 'base_flow' must match the kernel's view of the packet at the
3938 * time that action processing starts. 'flow' represents any
3939 * transformations we wish to make through actions.
3940 * - By default 'base_flow' and 'flow' are the same since the input
3941 * packet matches the output before any actions are applied.
3942 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
3943 * of the received packet as seen by the kernel. If we later output
3944 * to another device without any modifications this will cause us to
3945 * insert a new tag since the original one was stripped off by the
3947 * - Tunnel metadata as received is retained in 'flow'. This allows
3948 * tunnel metadata matching also in later tables.
3949 * Since a kernel action for setting the tunnel metadata will only be
3950 * generated with actual tunnel output, changing the tunnel metadata
3951 * values in 'flow' (such as tun_id) will only have effect with a later
3952 * tunnel output action.
3953 * - Tunnel 'base_flow' is completely cleared since that is what the
3954 * kernel does. If we wish to maintain the original values an action
3955 * needs to be generated. */
3960 ctx.xout->has_learn = false;
3961 ctx.xout->has_normal = false;
3962 ctx.xout->has_fin_timeout = false;
3963 ctx.xout->nf_output_iface = NF_OUT_DROP;
3964 ctx.xout->mirrors = 0;
3966 xout->odp_actions = xin->odp_actions;
3967 if (!xout->odp_actions) {
3968 xout->odp_actions = &xout->odp_actions_buf;
3969 ofpbuf_use_stub(xout->odp_actions, xout->odp_actions_stub,
3970 sizeof xout->odp_actions_stub);
3972 ofpbuf_reserve(xout->odp_actions, NL_A_U32_SIZE);
3974 ctx.xbridge = xbridge_lookup(xcfg, xin->ofproto);
3979 ctx.rule = xin->rule;
3981 ctx.base_flow = *flow;
3982 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
3983 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
3985 flow_wildcards_init_catchall(wc);
3986 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
3987 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3988 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3989 if (is_ip_any(flow)) {
3990 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3992 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
3994 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
3995 if (ctx.xbridge->netflow) {
3996 netflow_mask_wc(flow, wc);
4001 ctx.in_group = false;
4002 ctx.orig_skb_priority = flow->skb_priority;
4005 ctx.use_recirc = false;
4006 ctx.was_mpls = false;
4008 if (!xin->ofpacts && !ctx.rule) {
4009 ctx.table_id = rule_dpif_lookup(ctx.xbridge->ofproto, flow,
4010 !xin->skip_wildcards ? wc : NULL,
4011 &rule, ctx.xin->xcache != NULL,
4012 ctx.xin->resubmit_stats);
4013 if (ctx.xin->resubmit_stats) {
4014 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
4016 if (ctx.xin->xcache) {
4017 struct xc_entry *entry;
4019 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
4020 entry->u.rule = rule;
4024 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
4027 ofpacts = xin->ofpacts;
4028 ofpacts_len = xin->ofpacts_len;
4029 } else if (ctx.rule) {
4030 actions = rule_dpif_get_actions(ctx.rule);
4031 ofpacts = actions->ofpacts;
4032 ofpacts_len = actions->ofpacts_len;
4037 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
4038 ofpbuf_use_stub(&ctx.action_set,
4039 ctx.action_set_stub, sizeof ctx.action_set_stub);
4041 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
4042 /* Do this conditionally because the copy is expensive enough that it
4043 * shows up in profiles. */
4047 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
4048 switch (ctx.xbridge->frag) {
4049 case OFPC_FRAG_NORMAL:
4050 /* We must pretend that transport ports are unavailable. */
4051 flow->tp_src = ctx.base_flow.tp_src = htons(0);
4052 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
4055 case OFPC_FRAG_DROP:
4058 case OFPC_FRAG_REASM:
4061 case OFPC_FRAG_NX_MATCH:
4062 /* Nothing to do. */
4065 case OFPC_INVALID_TTL_TO_CONTROLLER:
4070 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
4071 if (in_port && in_port->is_tunnel) {
4072 if (ctx.xin->resubmit_stats) {
4073 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
4075 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
4078 if (ctx.xin->xcache) {
4079 struct xc_entry *entry;
4081 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
4082 entry->u.dev.rx = netdev_ref(in_port->netdev);
4083 entry->u.dev.bfd = bfd_ref(in_port->bfd);
4087 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
4089 ctx.xout->slow |= special;
4091 size_t sample_actions_len;
4093 if (flow->in_port.ofp_port
4094 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
4095 flow->in_port.ofp_port,
4097 ctx.base_flow.vlan_tci = 0;
4100 add_sflow_action(&ctx);
4101 add_ipfix_action(&ctx);
4102 sample_actions_len = ofpbuf_size(ctx.xout->odp_actions);
4104 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
4105 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
4107 /* We've let OFPP_NORMAL and the learning action look at the
4108 * packet, so drop it now if forwarding is disabled. */
4109 if (in_port && !xport_stp_forward_state(in_port)) {
4110 ofpbuf_set_size(ctx.xout->odp_actions, sample_actions_len);
4114 if (ofpbuf_size(&ctx.action_set)) {
4115 xlate_action_set(&ctx);
4118 if (ctx.xbridge->has_in_band
4119 && in_band_must_output_to_local_port(flow)
4120 && !actions_output_to_local_port(&ctx)) {
4121 compose_output_action(&ctx, OFPP_LOCAL);
4124 fix_sflow_action(&ctx);
4126 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
4127 add_mirror_actions(&ctx, &orig_flow);
4131 if (nl_attr_oversized(ofpbuf_size(ctx.xout->odp_actions))) {
4132 /* These datapath actions are too big for a Netlink attribute, so we
4133 * can't hand them to the kernel directly. dpif_execute() can execute
4134 * them one by one with help, so just mark the result as SLOW_ACTION to
4135 * prevent the flow from being installed. */
4136 COVERAGE_INC(xlate_actions_oversize);
4137 ctx.xout->slow |= SLOW_ACTION;
4140 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
4141 if (ctx.xin->resubmit_stats) {
4142 mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
4143 ctx.xin->resubmit_stats->n_packets,
4144 ctx.xin->resubmit_stats->n_bytes);
4146 if (ctx.xin->xcache) {
4147 struct xc_entry *entry;
4149 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_MIRROR);
4150 entry->u.mirror.mbridge = mbridge_ref(ctx.xbridge->mbridge);
4151 entry->u.mirror.mirrors = xout->mirrors;
4155 if (ctx.xbridge->netflow) {
4156 /* Only update netflow if we don't have controller flow. We don't
4157 * report NetFlow expiration messages for such facets because they
4158 * are just part of the control logic for the network, not real
4160 if (ofpacts_len == 0
4161 || ofpacts->type != OFPACT_CONTROLLER
4162 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
4163 if (ctx.xin->resubmit_stats) {
4164 netflow_flow_update(ctx.xbridge->netflow, flow,
4165 xout->nf_output_iface,
4166 ctx.xin->resubmit_stats);
4168 if (ctx.xin->xcache) {
4169 struct xc_entry *entry;
4171 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
4172 entry->u.nf.netflow = netflow_ref(ctx.xbridge->netflow);
4173 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
4174 entry->u.nf.iface = xout->nf_output_iface;
4179 ofpbuf_uninit(&ctx.stack);
4180 ofpbuf_uninit(&ctx.action_set);
4182 /* Clear the metadata and register wildcard masks, because we won't
4183 * use non-header fields as part of the cache. */
4184 flow_wildcards_clear_non_packet_fields(wc);
4186 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow uses
4187 * the low 8 bits of the 16-bit tp_src and tp_dst members to represent
4188 * these fields. The datapath interface, on the other hand, represents
4189 * them with just 8 bits each. This means that if the high 8 bits of the
4190 * masks for these fields somehow become set, then they will get chopped
4191 * off by a round trip through the datapath, and revalidation will spot
4192 * that as an inconsistency and delete the flow. Avoid the problem here by
4193 * making sure that only the low 8 bits of either field can be unwildcarded
4197 wc->masks.tp_src &= htons(UINT8_MAX);
4198 wc->masks.tp_dst &= htons(UINT8_MAX);
4202 /* Sends 'packet' out 'ofport'.
4203 * May modify 'packet'.
4204 * Returns 0 if successful, otherwise a positive errno value. */
4206 xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
4208 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4209 struct xport *xport;
4210 struct ofpact_output output;
4213 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
4214 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
4215 flow_extract(packet, NULL, &flow);
4216 flow.in_port.ofp_port = OFPP_NONE;
4218 xport = xport_lookup(xcfg, ofport);
4222 output.port = xport->ofp_port;
4225 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
4226 &output.ofpact, sizeof output,
4230 struct xlate_cache *
4231 xlate_cache_new(void)
4233 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
4235 ofpbuf_init(&xcache->entries, 512);
4239 static struct xc_entry *
4240 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
4242 struct xc_entry *entry;
4244 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
4251 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
4253 if (entry->u.dev.tx) {
4254 netdev_vport_inc_tx(entry->u.dev.tx, stats);
4256 if (entry->u.dev.rx) {
4257 netdev_vport_inc_rx(entry->u.dev.rx, stats);
4259 if (entry->u.dev.bfd) {
4260 bfd_account_rx(entry->u.dev.bfd, stats);
4265 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
4267 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
4268 struct xbridge *xbridge;
4269 struct xbundle *xbundle;
4270 struct flow_wildcards wc;
4272 xbridge = xbridge_lookup(xcfg, ofproto);
4277 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
4283 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
4286 /* Push stats and perform side effects of flow translation. */
4288 xlate_push_stats(struct xlate_cache *xcache, bool may_learn,
4289 const struct dpif_flow_stats *stats)
4291 struct xc_entry *entry;
4292 struct ofpbuf entries = xcache->entries;
4294 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
4295 switch (entry->type) {
4297 rule_dpif_credit_stats(entry->u.rule, stats);
4300 bond_account(entry->u.bond.bond, entry->u.bond.flow,
4301 entry->u.bond.vid, stats->n_bytes);
4304 xlate_cache_netdev(entry, stats);
4307 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
4308 entry->u.nf.iface, stats);
4311 mirror_update_stats(entry->u.mirror.mbridge,
4312 entry->u.mirror.mirrors,
4313 stats->n_packets, stats->n_bytes);
4317 ofproto_dpif_flow_mod(entry->u.learn.ofproto,
4322 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
4323 entry->u.normal.vlan);
4325 case XC_FIN_TIMEOUT:
4326 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
4327 entry->u.fin.idle, entry->u.fin.hard);
4330 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
4340 xlate_dev_unref(struct xc_entry *entry)
4342 if (entry->u.dev.tx) {
4343 netdev_close(entry->u.dev.tx);
4345 if (entry->u.dev.rx) {
4346 netdev_close(entry->u.dev.rx);
4348 if (entry->u.dev.bfd) {
4349 bfd_unref(entry->u.dev.bfd);
4354 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
4356 netflow_flow_clear(netflow, flow);
4357 netflow_unref(netflow);
4362 xlate_cache_clear(struct xlate_cache *xcache)
4364 struct xc_entry *entry;
4365 struct ofpbuf entries;
4371 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
4372 switch (entry->type) {
4374 rule_dpif_unref(entry->u.rule);
4377 free(entry->u.bond.flow);
4378 bond_unref(entry->u.bond.bond);
4381 xlate_dev_unref(entry);
4384 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
4387 mbridge_unref(entry->u.mirror.mbridge);
4390 free(entry->u.learn.fm);
4391 ofpbuf_delete(entry->u.learn.ofpacts);
4394 free(entry->u.normal.flow);
4396 case XC_FIN_TIMEOUT:
4397 /* 'u.fin.rule' is always already held as a XC_RULE, which
4398 * has already released it's reference above. */
4401 group_dpif_unref(entry->u.group.group);
4408 ofpbuf_clear(&xcache->entries);
4412 xlate_cache_delete(struct xlate_cache *xcache)
4414 xlate_cache_clear(xcache);
4415 ofpbuf_uninit(&xcache->entries);