1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. */
17 #include "ofproto/ofproto-dpif-xlate.h"
25 #include "byte-order.h"
30 #include "dynamic-string.h"
35 #include "mac-learning.h"
36 #include "meta-flow.h"
37 #include "multipath.h"
38 #include "netdev-vport.h"
41 #include "odp-execute.h"
42 #include "ofp-actions.h"
43 #include "ofproto/ofproto-dpif-ipfix.h"
44 #include "ofproto/ofproto-dpif-mirror.h"
45 #include "ofproto/ofproto-dpif-monitor.h"
46 #include "ofproto/ofproto-dpif-sflow.h"
47 #include "ofproto/ofproto-dpif.h"
48 #include "ofproto/ofproto-provider.h"
52 COVERAGE_DEFINE(xlate_actions);
53 COVERAGE_DEFINE(xlate_actions_oversize);
54 COVERAGE_DEFINE(xlate_actions_mpls_overflow);
56 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
58 /* Maximum depth of flow table recursion (due to resubmit actions) in a
59 * flow translation. */
60 #define MAX_RESUBMIT_RECURSION 64
61 #define MAX_INTERNAL_RESUBMITS 1 /* Max resbmits allowed using rules in
64 /* Maximum number of resubmit actions in a flow translation, whether they are
65 * recursive or not. */
66 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
69 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
70 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
72 struct list xbundles; /* Owned xbundles. */
73 struct hmap xports; /* Indexed by ofp_port. */
75 char *name; /* Name used in log messages. */
76 struct dpif *dpif; /* Datapath interface. */
77 struct mac_learning *ml; /* Mac learning handle. */
78 struct mbridge *mbridge; /* Mirroring. */
79 struct dpif_sflow *sflow; /* SFlow handle, or null. */
80 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
81 struct netflow *netflow; /* Netflow handle, or null. */
82 struct stp *stp; /* STP or null if disabled. */
84 /* Special rules installed by ofproto-dpif. */
85 struct rule_dpif *miss_rule;
86 struct rule_dpif *no_packet_in_rule;
88 enum ofp_config_flags frag; /* Fragmentation handling. */
89 bool has_in_band; /* Bridge has in band control? */
90 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
92 /* True if the datapath supports recirculation. */
95 /* True if the datapath supports variable-length
96 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
97 * False if the datapath supports only 8-byte (or shorter) userdata. */
98 bool variable_length_userdata;
100 /* Number of MPLS label stack entries that the datapath supports
102 size_t max_mpls_depth;
106 struct hmap_node hmap_node; /* In global 'xbundles' map. */
107 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
109 struct list list_node; /* In parent 'xbridges' list. */
110 struct xbridge *xbridge; /* Parent xbridge. */
112 struct list xports; /* Contains "struct xport"s. */
114 char *name; /* Name used in log messages. */
115 struct bond *bond; /* Nonnull iff more than one port. */
116 struct lacp *lacp; /* LACP handle or null. */
118 enum port_vlan_mode vlan_mode; /* VLAN mode. */
119 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
120 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
121 * NULL if all VLANs are trunked. */
122 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
123 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
127 struct hmap_node hmap_node; /* Node in global 'xports' map. */
128 struct ofport_dpif *ofport; /* Key in global 'xports map. */
130 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
131 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
133 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
135 struct list bundle_node; /* In parent xbundle (if it exists). */
136 struct xbundle *xbundle; /* Parent xbundle or null. */
138 struct netdev *netdev; /* 'ofport''s netdev. */
140 struct xbridge *xbridge; /* Parent bridge. */
141 struct xport *peer; /* Patch port peer or null. */
143 enum ofputil_port_config config; /* OpenFlow port configuration. */
144 enum ofputil_port_state state; /* OpenFlow port state. */
145 int stp_port_no; /* STP port number or -1 if not in use. */
147 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
149 bool may_enable; /* May be enabled in bonds. */
150 bool is_tunnel; /* Is a tunnel port. */
152 struct cfm *cfm; /* CFM handle or null. */
153 struct bfd *bfd; /* BFD handle or null. */
157 struct xlate_in *xin;
158 struct xlate_out *xout;
160 const struct xbridge *xbridge;
162 /* Flow at the last commit. */
163 struct flow base_flow;
165 /* Tunnel IP destination address as received. This is stored separately
166 * as the base_flow.tunnel is cleared on init to reflect the datapath
167 * behavior. Used to make sure not to send tunneled output to ourselves,
168 * which might lead to an infinite loop. This could happen easily
169 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
170 * actually set the tun_dst field. */
171 ovs_be32 orig_tunnel_ip_dst;
173 /* Stack for the push and pop actions. Each stack element is of type
174 * "union mf_subvalue". */
175 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
178 /* The rule that we are currently translating, or NULL. */
179 struct rule_dpif *rule;
181 /* Resubmit statistics, via xlate_table_action(). */
182 int recurse; /* Current resubmit nesting depth. */
183 int resubmits; /* Total number of resubmits. */
184 bool in_group; /* Currently translating ofgroup, if true. */
186 uint32_t orig_skb_priority; /* Priority when packet arrived. */
187 uint8_t table_id; /* OpenFlow table ID where flow was found. */
188 uint32_t sflow_n_outputs; /* Number of output ports. */
189 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
190 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
191 bool exit; /* No further actions should be processed. */
193 bool use_recirc; /* Should generate recirc? */
194 struct xlate_recirc recirc; /* Information used for generating
195 * recirculation actions */
197 /* OpenFlow 1.1+ action set.
199 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
200 * When translation is otherwise complete, ofpacts_execute_action_set()
201 * converts it to a set of "struct ofpact"s that can be translated into
202 * datapath actions. */
203 struct ofpbuf action_set; /* Action set. */
204 uint64_t action_set_stub[1024 / 8];
207 /* A controller may use OFPP_NONE as the ingress port to indicate that
208 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
209 * when an input bundle is needed for validation (e.g., mirroring or
210 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
211 * any 'port' structs, so care must be taken when dealing with it. */
212 static struct xbundle ofpp_none_bundle = {
214 .vlan_mode = PORT_VLAN_TRUNK
217 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
218 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
219 * traffic egressing the 'ofport' with that priority should be marked with. */
220 struct skb_priority_to_dscp {
221 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
222 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
224 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
239 /* xlate_cache entries hold enough information to perform the side effects of
240 * xlate_actions() for a rule, without needing to perform rule translation
241 * from scratch. The primary usage of these is to submit statistics to objects
242 * that a flow relates to, although they may be used for other effects as well
243 * (for instance, refreshing hard timeouts for learned flows). */
247 struct rule_dpif *rule;
254 struct netflow *netflow;
259 struct mbridge *mbridge;
260 mirror_mask_t mirrors;
268 struct rule_dpif *rule;
271 struct ofproto_dpif *ofproto;
276 struct rule_dpif *rule;
281 struct group_dpif *group;
282 struct ofputil_bucket *bucket;
287 #define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
288 entries = xcache->entries; \
289 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
291 entry = ofpbuf_try_pull(&entries, sizeof *entry))
294 struct ofpbuf entries;
297 /* Xlate config contains hash maps of all bridges, bundles and ports.
298 * Xcfgp contains the pointer to the current xlate configuration.
299 * When the main thread needs to change the configuration, it copies xcfgp to
300 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
301 * does not block handler and revalidator threads. */
303 struct hmap xbridges;
304 struct hmap xbundles;
307 OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_TYPE_INITIALIZER;
308 struct xlate_cfg *new_xcfg = NULL;
310 static bool may_receive(const struct xport *, struct xlate_ctx *);
311 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
313 static void xlate_normal(struct xlate_ctx *);
314 static void xlate_report(struct xlate_ctx *, const char *);
315 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
316 uint8_t table_id, bool may_packet_in,
317 bool honor_table_miss);
318 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
319 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
320 static void output_normal(struct xlate_ctx *, const struct xbundle *,
322 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
324 static struct xbridge *xbridge_lookup(struct xlate_cfg *,
325 const struct ofproto_dpif *);
326 static struct xbundle *xbundle_lookup(struct xlate_cfg *,
327 const struct ofbundle *);
328 static struct xport *xport_lookup(struct xlate_cfg *,
329 const struct ofport_dpif *);
330 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
331 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
332 uint32_t skb_priority);
333 static void clear_skb_priorities(struct xport *);
334 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
337 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
339 static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
340 static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
341 static void xlate_xport_init(struct xlate_cfg *, struct xport *);
342 static void xlate_xbridge_set(struct xbridge *xbridge,
344 struct rule_dpif *miss_rule,
345 struct rule_dpif *no_packet_in_rule,
346 const struct mac_learning *ml, struct stp *stp,
347 const struct mbridge *mbridge,
348 const struct dpif_sflow *sflow,
349 const struct dpif_ipfix *ipfix,
350 const struct netflow *netflow,
351 enum ofp_config_flags frag,
352 bool forward_bpdu, bool has_in_band,
354 bool variable_length_userdata,
355 size_t max_mpls_depth);
356 static void xlate_xbundle_set(struct xbundle *xbundle,
357 enum port_vlan_mode vlan_mode, int vlan,
358 unsigned long *trunks, bool use_priority_tags,
359 const struct bond *bond, const struct lacp *lacp,
361 static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
362 const struct netdev *netdev, const struct cfm *cfm,
363 const struct bfd *bfd, int stp_port_no,
364 enum ofputil_port_config config,
365 enum ofputil_port_state state, bool is_tunnel,
367 static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
368 static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
369 static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
370 static void xlate_xbridge_copy(struct xbridge *);
371 static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
372 static void xlate_xport_copy(struct xbridge *, struct xbundle *,
374 static void xlate_xcfg_free(struct xlate_cfg *);
378 xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
380 list_init(&xbridge->xbundles);
381 hmap_init(&xbridge->xports);
382 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
383 hash_pointer(xbridge->ofproto, 0));
387 xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
389 list_init(&xbundle->xports);
390 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
391 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
392 hash_pointer(xbundle->ofbundle, 0));
396 xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
398 hmap_init(&xport->skb_priorities);
399 hmap_insert(&xcfg->xports, &xport->hmap_node,
400 hash_pointer(xport->ofport, 0));
401 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
402 hash_ofp_port(xport->ofp_port));
406 xlate_xbridge_set(struct xbridge *xbridge,
408 struct rule_dpif *miss_rule,
409 struct rule_dpif *no_packet_in_rule,
410 const struct mac_learning *ml, struct stp *stp,
411 const struct mbridge *mbridge,
412 const struct dpif_sflow *sflow,
413 const struct dpif_ipfix *ipfix,
414 const struct netflow *netflow, enum ofp_config_flags frag,
415 bool forward_bpdu, bool has_in_band,
417 bool variable_length_userdata,
418 size_t max_mpls_depth)
420 if (xbridge->ml != ml) {
421 mac_learning_unref(xbridge->ml);
422 xbridge->ml = mac_learning_ref(ml);
425 if (xbridge->mbridge != mbridge) {
426 mbridge_unref(xbridge->mbridge);
427 xbridge->mbridge = mbridge_ref(mbridge);
430 if (xbridge->sflow != sflow) {
431 dpif_sflow_unref(xbridge->sflow);
432 xbridge->sflow = dpif_sflow_ref(sflow);
435 if (xbridge->ipfix != ipfix) {
436 dpif_ipfix_unref(xbridge->ipfix);
437 xbridge->ipfix = dpif_ipfix_ref(ipfix);
440 if (xbridge->stp != stp) {
441 stp_unref(xbridge->stp);
442 xbridge->stp = stp_ref(stp);
445 if (xbridge->netflow != netflow) {
446 netflow_unref(xbridge->netflow);
447 xbridge->netflow = netflow_ref(netflow);
450 xbridge->dpif = dpif;
451 xbridge->forward_bpdu = forward_bpdu;
452 xbridge->has_in_band = has_in_band;
453 xbridge->frag = frag;
454 xbridge->miss_rule = miss_rule;
455 xbridge->no_packet_in_rule = no_packet_in_rule;
456 xbridge->enable_recirc = enable_recirc;
457 xbridge->variable_length_userdata = variable_length_userdata;
458 xbridge->max_mpls_depth = max_mpls_depth;
462 xlate_xbundle_set(struct xbundle *xbundle,
463 enum port_vlan_mode vlan_mode, int vlan,
464 unsigned long *trunks, bool use_priority_tags,
465 const struct bond *bond, const struct lacp *lacp,
468 ovs_assert(xbundle->xbridge);
470 xbundle->vlan_mode = vlan_mode;
471 xbundle->vlan = vlan;
472 xbundle->trunks = trunks;
473 xbundle->use_priority_tags = use_priority_tags;
474 xbundle->floodable = floodable;
476 if (xbundle->bond != bond) {
477 bond_unref(xbundle->bond);
478 xbundle->bond = bond_ref(bond);
481 if (xbundle->lacp != lacp) {
482 lacp_unref(xbundle->lacp);
483 xbundle->lacp = lacp_ref(lacp);
488 xlate_xport_set(struct xport *xport, odp_port_t odp_port,
489 const struct netdev *netdev, const struct cfm *cfm,
490 const struct bfd *bfd, int stp_port_no,
491 enum ofputil_port_config config, enum ofputil_port_state state,
492 bool is_tunnel, bool may_enable)
494 xport->config = config;
495 xport->state = state;
496 xport->stp_port_no = stp_port_no;
497 xport->is_tunnel = is_tunnel;
498 xport->may_enable = may_enable;
499 xport->odp_port = odp_port;
501 if (xport->cfm != cfm) {
502 cfm_unref(xport->cfm);
503 xport->cfm = cfm_ref(cfm);
506 if (xport->bfd != bfd) {
507 bfd_unref(xport->bfd);
508 xport->bfd = bfd_ref(bfd);
511 if (xport->netdev != netdev) {
512 netdev_close(xport->netdev);
513 xport->netdev = netdev_ref(netdev);
518 xlate_xbridge_copy(struct xbridge *xbridge)
520 struct xbundle *xbundle;
522 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
523 new_xbridge->ofproto = xbridge->ofproto;
524 new_xbridge->name = xstrdup(xbridge->name);
525 xlate_xbridge_init(new_xcfg, new_xbridge);
527 xlate_xbridge_set(new_xbridge,
528 xbridge->dpif, xbridge->miss_rule,
529 xbridge->no_packet_in_rule, xbridge->ml, xbridge->stp,
530 xbridge->mbridge, xbridge->sflow, xbridge->ipfix,
531 xbridge->netflow, xbridge->frag, xbridge->forward_bpdu,
532 xbridge->has_in_band, xbridge->enable_recirc,
533 xbridge->variable_length_userdata,
534 xbridge->max_mpls_depth);
535 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
536 xlate_xbundle_copy(new_xbridge, xbundle);
539 /* Copy xports which are not part of a xbundle */
540 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
541 if (!xport->xbundle) {
542 xlate_xport_copy(new_xbridge, NULL, xport);
548 xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
551 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
552 new_xbundle->ofbundle = xbundle->ofbundle;
553 new_xbundle->xbridge = xbridge;
554 new_xbundle->name = xstrdup(xbundle->name);
555 xlate_xbundle_init(new_xcfg, new_xbundle);
557 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
558 xbundle->vlan, xbundle->trunks,
559 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
561 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
562 xlate_xport_copy(xbridge, new_xbundle, xport);
567 xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
570 struct skb_priority_to_dscp *pdscp, *new_pdscp;
571 struct xport *new_xport = xzalloc(sizeof *xport);
572 new_xport->ofport = xport->ofport;
573 new_xport->ofp_port = xport->ofp_port;
574 new_xport->xbridge = xbridge;
575 xlate_xport_init(new_xcfg, new_xport);
577 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
578 xport->bfd, xport->stp_port_no, xport->config, xport->state,
579 xport->is_tunnel, xport->may_enable);
582 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
584 new_xport->peer = peer;
585 new_xport->peer->peer = new_xport;
590 new_xport->xbundle = xbundle;
591 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
594 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
595 new_pdscp = xmalloc(sizeof *pdscp);
596 new_pdscp->skb_priority = pdscp->skb_priority;
597 new_pdscp->dscp = pdscp->dscp;
598 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
599 hash_int(new_pdscp->skb_priority, 0));
603 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
604 * configuration in xcfgp.
606 * This needs to be called after editing the xlate configuration.
608 * Functions that edit the new xlate configuration are
609 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
615 * edit_xlate_configuration();
617 * xlate_txn_commit(); */
619 xlate_txn_commit(void)
621 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
623 ovsrcu_set(&xcfgp, new_xcfg);
624 ovsrcu_postpone(xlate_xcfg_free, xcfg);
629 /* Copies the current xlate configuration in xcfgp to new_xcfg.
631 * This needs to be called prior to editing the xlate configuration. */
633 xlate_txn_start(void)
635 struct xbridge *xbridge;
636 struct xlate_cfg *xcfg;
638 ovs_assert(!new_xcfg);
640 new_xcfg = xmalloc(sizeof *new_xcfg);
641 hmap_init(&new_xcfg->xbridges);
642 hmap_init(&new_xcfg->xbundles);
643 hmap_init(&new_xcfg->xports);
645 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
650 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
651 xlate_xbridge_copy(xbridge);
657 xlate_xcfg_free(struct xlate_cfg *xcfg)
659 struct xbridge *xbridge, *next_xbridge;
665 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
666 xlate_xbridge_remove(xcfg, xbridge);
669 hmap_destroy(&xcfg->xbridges);
670 hmap_destroy(&xcfg->xbundles);
671 hmap_destroy(&xcfg->xports);
676 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
677 struct dpif *dpif, struct rule_dpif *miss_rule,
678 struct rule_dpif *no_packet_in_rule,
679 const struct mac_learning *ml, struct stp *stp,
680 const struct mbridge *mbridge,
681 const struct dpif_sflow *sflow,
682 const struct dpif_ipfix *ipfix,
683 const struct netflow *netflow, enum ofp_config_flags frag,
684 bool forward_bpdu, bool has_in_band,
686 bool variable_length_userdata,
687 size_t max_mpls_depth)
689 struct xbridge *xbridge;
691 ovs_assert(new_xcfg);
693 xbridge = xbridge_lookup(new_xcfg, ofproto);
695 xbridge = xzalloc(sizeof *xbridge);
696 xbridge->ofproto = ofproto;
698 xlate_xbridge_init(new_xcfg, xbridge);
702 xbridge->name = xstrdup(name);
704 xlate_xbridge_set(xbridge, dpif, miss_rule, no_packet_in_rule, ml, stp,
705 mbridge, sflow, ipfix, netflow, frag, forward_bpdu,
706 has_in_band, enable_recirc, variable_length_userdata,
711 xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
713 struct xbundle *xbundle, *next_xbundle;
714 struct xport *xport, *next_xport;
720 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
721 xlate_xport_remove(xcfg, xport);
724 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
725 xlate_xbundle_remove(xcfg, xbundle);
728 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
729 mac_learning_unref(xbridge->ml);
730 mbridge_unref(xbridge->mbridge);
731 dpif_sflow_unref(xbridge->sflow);
732 dpif_ipfix_unref(xbridge->ipfix);
733 stp_unref(xbridge->stp);
734 hmap_destroy(&xbridge->xports);
740 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
742 struct xbridge *xbridge;
744 ovs_assert(new_xcfg);
746 xbridge = xbridge_lookup(new_xcfg, ofproto);
747 xlate_xbridge_remove(new_xcfg, xbridge);
751 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
752 const char *name, enum port_vlan_mode vlan_mode, int vlan,
753 unsigned long *trunks, bool use_priority_tags,
754 const struct bond *bond, const struct lacp *lacp,
757 struct xbundle *xbundle;
759 ovs_assert(new_xcfg);
761 xbundle = xbundle_lookup(new_xcfg, ofbundle);
763 xbundle = xzalloc(sizeof *xbundle);
764 xbundle->ofbundle = ofbundle;
765 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
767 xlate_xbundle_init(new_xcfg, xbundle);
771 xbundle->name = xstrdup(name);
773 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
774 use_priority_tags, bond, lacp, floodable);
778 xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
780 struct xport *xport, *next;
786 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
787 list_remove(&xport->bundle_node);
788 xport->xbundle = NULL;
791 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
792 list_remove(&xbundle->list_node);
793 bond_unref(xbundle->bond);
794 lacp_unref(xbundle->lacp);
800 xlate_bundle_remove(struct ofbundle *ofbundle)
802 struct xbundle *xbundle;
804 ovs_assert(new_xcfg);
806 xbundle = xbundle_lookup(new_xcfg, ofbundle);
807 xlate_xbundle_remove(new_xcfg, xbundle);
811 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
812 struct ofport_dpif *ofport, ofp_port_t ofp_port,
813 odp_port_t odp_port, const struct netdev *netdev,
814 const struct cfm *cfm, const struct bfd *bfd,
815 struct ofport_dpif *peer, int stp_port_no,
816 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
817 enum ofputil_port_config config,
818 enum ofputil_port_state state, bool is_tunnel,
824 ovs_assert(new_xcfg);
826 xport = xport_lookup(new_xcfg, ofport);
828 xport = xzalloc(sizeof *xport);
829 xport->ofport = ofport;
830 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
831 xport->ofp_port = ofp_port;
833 xlate_xport_init(new_xcfg, xport);
836 ovs_assert(xport->ofp_port == ofp_port);
838 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, stp_port_no, config,
839 state, is_tunnel, may_enable);
842 xport->peer->peer = NULL;
844 xport->peer = xport_lookup(new_xcfg, peer);
846 xport->peer->peer = xport;
849 if (xport->xbundle) {
850 list_remove(&xport->bundle_node);
852 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
853 if (xport->xbundle) {
854 list_insert(&xport->xbundle->xports, &xport->bundle_node);
857 clear_skb_priorities(xport);
858 for (i = 0; i < n_qdscp; i++) {
859 struct skb_priority_to_dscp *pdscp;
860 uint32_t skb_priority;
862 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
867 pdscp = xmalloc(sizeof *pdscp);
868 pdscp->skb_priority = skb_priority;
869 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
870 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
871 hash_int(pdscp->skb_priority, 0));
876 xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
883 xport->peer->peer = NULL;
887 if (xport->xbundle) {
888 list_remove(&xport->bundle_node);
891 clear_skb_priorities(xport);
892 hmap_destroy(&xport->skb_priorities);
894 hmap_remove(&xcfg->xports, &xport->hmap_node);
895 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
897 netdev_close(xport->netdev);
898 cfm_unref(xport->cfm);
899 bfd_unref(xport->bfd);
904 xlate_ofport_remove(struct ofport_dpif *ofport)
908 ovs_assert(new_xcfg);
910 xport = xport_lookup(new_xcfg, ofport);
911 xlate_xport_remove(new_xcfg, xport);
914 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
915 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
916 * Optionally populates 'ofproto' with the ofproto_dpif, 'odp_in_port' with
917 * the datapath in_port, that 'packet' ingressed, and 'ipfix', 'sflow', and
918 * 'netflow' with the appropriate handles for those protocols if they're
919 * enabled. Caller is responsible for unrefing them.
921 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
922 * 'flow''s in_port to OFPP_NONE.
924 * This function does post-processing on data returned from
925 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
926 * of the upcall processing logic. In particular, if the extracted in_port is
927 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
928 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
929 * a VLAN header onto 'packet' (if it is nonnull).
931 * Similarly, this function also includes some logic to help with tunnels. It
932 * may modify 'flow' as necessary to make the tunneling implementation
933 * transparent to the upcall processing logic.
935 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
936 * or some other positive errno if there are other problems. */
938 xlate_receive(const struct dpif_backer *backer, struct ofpbuf *packet,
939 const struct nlattr *key, size_t key_len, struct flow *flow,
940 struct ofproto_dpif **ofproto, struct dpif_ipfix **ipfix,
941 struct dpif_sflow **sflow, struct netflow **netflow,
942 odp_port_t *odp_in_port)
944 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
946 const struct xport *xport;
948 if (odp_flow_key_to_flow(key, key_len, flow) == ODP_FIT_ERROR) {
954 *odp_in_port = flow->in_port.odp_port;
957 xport = xport_lookup(xcfg, tnl_port_should_receive(flow)
958 ? tnl_port_receive(flow)
959 : odp_port_to_ofport(backer, flow->in_port.odp_port));
961 flow->in_port.ofp_port = xport ? xport->ofp_port : OFPP_NONE;
966 if (vsp_adjust_flow(xport->xbridge->ofproto, flow)) {
968 /* Make the packet resemble the flow, so that it gets sent to
969 * an OpenFlow controller properly, so that it looks correct
970 * for sFlow, and so that flow_extract() will get the correct
971 * vlan_tci if it is called on 'packet'. */
972 eth_push_vlan(packet, htons(ETH_TYPE_VLAN), flow->vlan_tci);
978 *ofproto = xport->xbridge->ofproto;
982 *ipfix = dpif_ipfix_ref(xport->xbridge->ipfix);
986 *sflow = dpif_sflow_ref(xport->xbridge->sflow);
990 *netflow = netflow_ref(xport->xbridge->netflow);
995 static struct xbridge *
996 xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
998 struct hmap *xbridges;
999 struct xbridge *xbridge;
1001 if (!ofproto || !xcfg) {
1005 xbridges = &xcfg->xbridges;
1007 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
1009 if (xbridge->ofproto == ofproto) {
1016 static struct xbundle *
1017 xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
1019 struct hmap *xbundles;
1020 struct xbundle *xbundle;
1022 if (!ofbundle || !xcfg) {
1026 xbundles = &xcfg->xbundles;
1028 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
1030 if (xbundle->ofbundle == ofbundle) {
1037 static struct xport *
1038 xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
1040 struct hmap *xports;
1041 struct xport *xport;
1043 if (!ofport || !xcfg) {
1047 xports = &xcfg->xports;
1049 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
1051 if (xport->ofport == ofport) {
1058 static struct stp_port *
1059 xport_get_stp_port(const struct xport *xport)
1061 return xport->xbridge->stp && xport->stp_port_no != -1
1062 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1067 xport_stp_learn_state(const struct xport *xport)
1069 struct stp_port *sp = xport_get_stp_port(xport);
1070 return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
1074 xport_stp_forward_state(const struct xport *xport)
1076 struct stp_port *sp = xport_get_stp_port(xport);
1077 return stp_forward_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
1081 xport_stp_listen_state(const struct xport *xport)
1083 struct stp_port *sp = xport_get_stp_port(xport);
1084 return stp_listen_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
1087 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1088 * were used to make the determination.*/
1090 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1092 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1093 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1094 return is_stp(flow);
1098 stp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
1100 struct stp_port *sp = xport_get_stp_port(xport);
1101 struct ofpbuf payload = *packet;
1102 struct eth_header *eth = ofpbuf_data(&payload);
1104 /* Sink packets on ports that have STP disabled when the bridge has
1106 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1110 /* Trim off padding on payload. */
1111 if (ofpbuf_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1112 ofpbuf_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1115 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1116 stp_received_bpdu(sp, ofpbuf_data(&payload), ofpbuf_size(&payload));
1120 static struct xport *
1121 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1123 struct xport *xport;
1125 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1127 if (xport->ofp_port == ofp_port) {
1135 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1137 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1138 return xport ? xport->odp_port : ODPP_NONE;
1142 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1144 struct xport *xport;
1146 xport = get_ofp_port(ctx->xbridge, ofp_port);
1147 if (!xport || xport->config & OFPUTIL_PC_PORT_DOWN ||
1148 xport->state & OFPUTIL_PS_LINK_DOWN) {
1155 static struct ofputil_bucket *
1156 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1160 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1162 struct group_dpif *group;
1164 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1165 struct ofputil_bucket *bucket;
1167 bucket = group_first_live_bucket(ctx, group, depth);
1168 group_dpif_unref(group);
1169 return bucket == NULL;
1175 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1178 bucket_is_alive(const struct xlate_ctx *ctx,
1179 struct ofputil_bucket *bucket, int depth)
1181 if (depth >= MAX_LIVENESS_RECURSION) {
1182 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1184 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1185 MAX_LIVENESS_RECURSION);
1189 return (!ofputil_bucket_has_liveness(bucket)
1190 || (bucket->watch_port != OFPP_ANY
1191 && odp_port_is_alive(ctx, bucket->watch_port))
1192 || (bucket->watch_group != OFPG_ANY
1193 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
1196 static struct ofputil_bucket *
1197 group_first_live_bucket(const struct xlate_ctx *ctx,
1198 const struct group_dpif *group, int depth)
1200 struct ofputil_bucket *bucket;
1201 const struct list *buckets;
1203 group_dpif_get_buckets(group, &buckets);
1204 LIST_FOR_EACH (bucket, list_node, buckets) {
1205 if (bucket_is_alive(ctx, bucket, depth)) {
1213 static struct ofputil_bucket *
1214 group_best_live_bucket(const struct xlate_ctx *ctx,
1215 const struct group_dpif *group,
1218 struct ofputil_bucket *best_bucket = NULL;
1219 uint32_t best_score = 0;
1222 struct ofputil_bucket *bucket;
1223 const struct list *buckets;
1225 group_dpif_get_buckets(group, &buckets);
1226 LIST_FOR_EACH (bucket, list_node, buckets) {
1227 if (bucket_is_alive(ctx, bucket, 0)) {
1228 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
1229 if (score >= best_score) {
1230 best_bucket = bucket;
1241 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
1243 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1244 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1248 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1250 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1253 static mirror_mask_t
1254 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1256 return xbundle != &ofpp_none_bundle
1257 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1261 static mirror_mask_t
1262 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
1264 return xbundle != &ofpp_none_bundle
1265 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1269 static mirror_mask_t
1270 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
1272 return xbundle != &ofpp_none_bundle
1273 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1277 static struct xbundle *
1278 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1279 bool warn, struct xport **in_xportp)
1281 struct xport *xport;
1283 /* Find the port and bundle for the received packet. */
1284 xport = get_ofp_port(xbridge, in_port);
1288 if (xport && xport->xbundle) {
1289 return xport->xbundle;
1292 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1293 * which a controller may use as the ingress port for traffic that
1294 * it is sourcing. */
1295 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
1296 return &ofpp_none_bundle;
1299 /* Odd. A few possible reasons here:
1301 * - We deleted a port but there are still a few packets queued up
1304 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1305 * we don't know about.
1307 * - The ofproto client didn't configure the port as part of a bundle.
1308 * This is particularly likely to happen if a packet was received on the
1309 * port after it was created, but before the client had a chance to
1310 * configure its bundle.
1313 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1315 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1316 "port %"PRIu16, xbridge->name, in_port);
1322 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
1324 const struct xbridge *xbridge = ctx->xbridge;
1325 mirror_mask_t mirrors;
1326 struct xbundle *in_xbundle;
1330 mirrors = ctx->xout->mirrors;
1331 ctx->xout->mirrors = 0;
1333 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
1334 ctx->xin->packet != NULL, NULL);
1338 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
1340 /* Drop frames on bundles reserved for mirroring. */
1341 if (xbundle_mirror_out(xbridge, in_xbundle)) {
1342 if (ctx->xin->packet != NULL) {
1343 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1344 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1345 "%s, which is reserved exclusively for mirroring",
1346 ctx->xbridge->name, in_xbundle->name);
1348 ofpbuf_clear(&ctx->xout->odp_actions);
1353 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
1354 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1357 vlan = input_vid_to_vlan(in_xbundle, vid);
1363 /* Restore the original packet before adding the mirror actions. */
1364 ctx->xin->flow = *orig_flow;
1367 mirror_mask_t dup_mirrors;
1368 struct ofbundle *out;
1369 unsigned long *vlans;
1374 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1375 &vlans, &dup_mirrors, &out, &out_vlan);
1376 ovs_assert(has_mirror);
1379 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1381 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
1384 if (!vlan_mirrored) {
1385 mirrors = zero_rightmost_1bit(mirrors);
1389 mirrors &= ~dup_mirrors;
1390 ctx->xout->mirrors |= dup_mirrors;
1392 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1393 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1395 output_normal(ctx, out_xbundle, vlan);
1397 } else if (vlan != out_vlan
1398 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
1399 struct xbundle *xbundle;
1401 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1402 if (xbundle_includes_vlan(xbundle, out_vlan)
1403 && !xbundle_mirror_out(xbridge, xbundle)) {
1404 output_normal(ctx, xbundle, out_vlan);
1411 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1412 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1413 * the bundle on which the packet was received, returns the VLAN to which the
1416 * Both 'vid' and the return value are in the range 0...4095. */
1418 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1420 switch (in_xbundle->vlan_mode) {
1421 case PORT_VLAN_ACCESS:
1422 return in_xbundle->vlan;
1425 case PORT_VLAN_TRUNK:
1428 case PORT_VLAN_NATIVE_UNTAGGED:
1429 case PORT_VLAN_NATIVE_TAGGED:
1430 return vid ? vid : in_xbundle->vlan;
1437 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1438 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1441 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1442 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1445 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1447 /* Allow any VID on the OFPP_NONE port. */
1448 if (in_xbundle == &ofpp_none_bundle) {
1452 switch (in_xbundle->vlan_mode) {
1453 case PORT_VLAN_ACCESS:
1456 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1457 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1458 "packet received on port %s configured as VLAN "
1459 "%"PRIu16" access port", vid, in_xbundle->name,
1466 case PORT_VLAN_NATIVE_UNTAGGED:
1467 case PORT_VLAN_NATIVE_TAGGED:
1469 /* Port must always carry its native VLAN. */
1473 case PORT_VLAN_TRUNK:
1474 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1476 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1477 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1478 "received on port %s not configured for trunking "
1479 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1491 /* Given 'vlan', the VLAN that a packet belongs to, and
1492 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1493 * that should be included in the 802.1Q header. (If the return value is 0,
1494 * then the 802.1Q header should only be included in the packet if there is a
1497 * Both 'vlan' and the return value are in the range 0...4095. */
1499 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1501 switch (out_xbundle->vlan_mode) {
1502 case PORT_VLAN_ACCESS:
1505 case PORT_VLAN_TRUNK:
1506 case PORT_VLAN_NATIVE_TAGGED:
1509 case PORT_VLAN_NATIVE_UNTAGGED:
1510 return vlan == out_xbundle->vlan ? 0 : vlan;
1518 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1521 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1523 ovs_be16 tci, old_tci;
1524 struct xport *xport;
1526 vid = output_vlan_to_vid(out_xbundle, vlan);
1527 if (list_is_empty(&out_xbundle->xports)) {
1528 /* Partially configured bundle with no slaves. Drop the packet. */
1530 } else if (!out_xbundle->bond) {
1531 ctx->use_recirc = false;
1532 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1535 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1536 struct flow_wildcards *wc = &ctx->xout->wc;
1537 struct xlate_recirc *xr = &ctx->recirc;
1538 struct ofport_dpif *ofport;
1540 if (ctx->xbridge->enable_recirc) {
1541 ctx->use_recirc = bond_may_recirc(
1542 out_xbundle->bond, &xr->recirc_id, &xr->hash_basis);
1544 if (ctx->use_recirc) {
1545 /* Only TCP mode uses recirculation. */
1546 xr->hash_alg = OVS_HASH_ALG_L4;
1547 bond_update_post_recirc_rules(out_xbundle->bond, false);
1549 /* Recirculation does not require unmasking hash fields. */
1554 ofport = bond_choose_output_slave(out_xbundle->bond,
1555 &ctx->xin->flow, wc, vid);
1556 xport = xport_lookup(xcfg, ofport);
1559 /* No slaves enabled, so drop packet. */
1563 /* If ctx->xout->use_recirc is set, the main thread will handle stats
1564 * accounting for this bond. */
1565 if (!ctx->use_recirc) {
1566 if (ctx->xin->resubmit_stats) {
1567 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1568 ctx->xin->resubmit_stats->n_bytes);
1570 if (ctx->xin->xcache) {
1571 struct xc_entry *entry;
1574 flow = &ctx->xin->flow;
1575 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1576 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1577 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1578 entry->u.bond.vid = vid;
1583 old_tci = *flow_tci;
1585 if (tci || out_xbundle->use_priority_tags) {
1586 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1588 tci |= htons(VLAN_CFI);
1593 compose_output_action(ctx, xport->ofp_port);
1594 *flow_tci = old_tci;
1597 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1598 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1599 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1601 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1603 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1607 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1608 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1612 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1613 if (flow->nw_proto == ARP_OP_REPLY) {
1615 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1616 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1617 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1619 return flow->nw_src == flow->nw_dst;
1625 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1626 * dropped. Returns true if they may be forwarded, false if they should be
1629 * 'in_port' must be the xport that corresponds to flow->in_port.
1630 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1632 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1633 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1634 * checked by input_vid_is_valid().
1636 * May also add tags to '*tags', although the current implementation only does
1637 * so in one special case.
1640 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1643 struct xbundle *in_xbundle = in_port->xbundle;
1644 const struct xbridge *xbridge = ctx->xbridge;
1645 struct flow *flow = &ctx->xin->flow;
1647 /* Drop frames for reserved multicast addresses
1648 * only if forward_bpdu option is absent. */
1649 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1650 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1654 if (in_xbundle->bond) {
1655 struct mac_entry *mac;
1657 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1663 xlate_report(ctx, "bonding refused admissibility, dropping");
1666 case BV_DROP_IF_MOVED:
1667 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1668 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1669 if (mac && mac->port.p != in_xbundle->ofbundle &&
1670 (!is_gratuitous_arp(flow, &ctx->xout->wc)
1671 || mac_entry_is_grat_arp_locked(mac))) {
1672 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1673 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1677 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1685 /* Checks whether a MAC learning update is necessary for MAC learning table
1686 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1689 * Most packets processed through the MAC learning table do not actually
1690 * change it in any way. This function requires only a read lock on the MAC
1691 * learning table, so it is much cheaper in this common case.
1693 * Keep the code here synchronized with that in update_learning_table__()
1696 is_mac_learning_update_needed(const struct mac_learning *ml,
1697 const struct flow *flow,
1698 struct flow_wildcards *wc,
1699 int vlan, struct xbundle *in_xbundle)
1700 OVS_REQ_RDLOCK(ml->rwlock)
1702 struct mac_entry *mac;
1704 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1708 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1709 if (!mac || mac_entry_age(ml, mac)) {
1713 if (is_gratuitous_arp(flow, wc)) {
1714 /* We don't want to learn from gratuitous ARP packets that are
1715 * reflected back over bond slaves so we lock the learning table. */
1716 if (!in_xbundle->bond) {
1718 } else if (mac_entry_is_grat_arp_locked(mac)) {
1723 return mac->port.p != in_xbundle->ofbundle;
1727 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1728 * received on 'in_xbundle' in 'vlan'.
1730 * This code repeats all the checks in is_mac_learning_update_needed() because
1731 * the lock was released between there and here and thus the MAC learning state
1732 * could have changed.
1734 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1737 update_learning_table__(const struct xbridge *xbridge,
1738 const struct flow *flow, struct flow_wildcards *wc,
1739 int vlan, struct xbundle *in_xbundle)
1740 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1742 struct mac_entry *mac;
1744 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1748 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1749 if (is_gratuitous_arp(flow, wc)) {
1750 /* We don't want to learn from gratuitous ARP packets that are
1751 * reflected back over bond slaves so we lock the learning table. */
1752 if (!in_xbundle->bond) {
1753 mac_entry_set_grat_arp_lock(mac);
1754 } else if (mac_entry_is_grat_arp_locked(mac)) {
1759 if (mac->port.p != in_xbundle->ofbundle) {
1760 /* The log messages here could actually be useful in debugging,
1761 * so keep the rate limit relatively high. */
1762 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1764 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1765 "on port %s in VLAN %d",
1766 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1767 in_xbundle->name, vlan);
1769 mac->port.p = in_xbundle->ofbundle;
1770 mac_learning_changed(xbridge->ml);
1775 update_learning_table(const struct xbridge *xbridge,
1776 const struct flow *flow, struct flow_wildcards *wc,
1777 int vlan, struct xbundle *in_xbundle)
1781 /* Don't learn the OFPP_NONE port. */
1782 if (in_xbundle == &ofpp_none_bundle) {
1786 /* First try the common case: no change to MAC learning table. */
1787 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1788 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1790 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1793 /* Slow path: MAC learning table might need an update. */
1794 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1795 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1796 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1801 xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
1804 struct xbundle *xbundle;
1806 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1807 if (xbundle != in_xbundle
1808 && xbundle_includes_vlan(xbundle, vlan)
1809 && xbundle->floodable
1810 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1811 output_normal(ctx, xbundle, vlan);
1814 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1818 xlate_normal(struct xlate_ctx *ctx)
1820 struct flow_wildcards *wc = &ctx->xout->wc;
1821 struct flow *flow = &ctx->xin->flow;
1822 struct xbundle *in_xbundle;
1823 struct xport *in_port;
1824 struct mac_entry *mac;
1829 ctx->xout->has_normal = true;
1831 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1832 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1833 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1835 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
1836 ctx->xin->packet != NULL, &in_port);
1838 xlate_report(ctx, "no input bundle, dropping");
1842 /* Drop malformed frames. */
1843 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
1844 !(flow->vlan_tci & htons(VLAN_CFI))) {
1845 if (ctx->xin->packet != NULL) {
1846 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1847 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
1848 "VLAN tag received on port %s",
1849 ctx->xbridge->name, in_xbundle->name);
1851 xlate_report(ctx, "partial VLAN tag, dropping");
1855 /* Drop frames on bundles reserved for mirroring. */
1856 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
1857 if (ctx->xin->packet != NULL) {
1858 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1859 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1860 "%s, which is reserved exclusively for mirroring",
1861 ctx->xbridge->name, in_xbundle->name);
1863 xlate_report(ctx, "input port is mirror output port, dropping");
1868 vid = vlan_tci_to_vid(flow->vlan_tci);
1869 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1870 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
1873 vlan = input_vid_to_vlan(in_xbundle, vid);
1875 /* Check other admissibility requirements. */
1876 if (in_port && !is_admissible(ctx, in_port, vlan)) {
1880 /* Learn source MAC. */
1881 if (ctx->xin->may_learn) {
1882 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
1884 if (ctx->xin->xcache) {
1885 struct xc_entry *entry;
1887 /* Save enough info to update mac learning table later. */
1888 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
1889 entry->u.normal.ofproto = ctx->xbridge->ofproto;
1890 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
1891 entry->u.normal.vlan = vlan;
1894 /* Determine output bundle. */
1895 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
1896 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
1897 mac_port = mac ? mac->port.p : NULL;
1898 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
1901 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1902 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
1903 if (mac_xbundle && mac_xbundle != in_xbundle) {
1904 xlate_report(ctx, "forwarding to learned port");
1905 output_normal(ctx, mac_xbundle, vlan);
1906 } else if (!mac_xbundle) {
1907 xlate_report(ctx, "learned port is unknown, dropping");
1909 xlate_report(ctx, "learned port is input port, dropping");
1912 xlate_report(ctx, "no learned MAC for destination, flooding");
1913 xlate_normal_flood(ctx, in_xbundle, vlan);
1917 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1918 * the number of packets out of UINT32_MAX to sample. The given
1919 * cookie is passed back in the callback for each sampled packet.
1922 compose_sample_action(const struct xbridge *xbridge,
1923 struct ofpbuf *odp_actions,
1924 const struct flow *flow,
1925 const uint32_t probability,
1926 const union user_action_cookie *cookie,
1927 const size_t cookie_size)
1929 size_t sample_offset, actions_offset;
1930 odp_port_t odp_port;
1934 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
1936 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
1938 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
1940 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
1941 pid = dpif_port_get_pid(xbridge->dpif, odp_port,
1942 flow_hash_5tuple(flow, 0));
1943 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
1946 nl_msg_end_nested(odp_actions, actions_offset);
1947 nl_msg_end_nested(odp_actions, sample_offset);
1948 return cookie_offset;
1952 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
1953 odp_port_t odp_port, unsigned int n_outputs,
1954 union user_action_cookie *cookie)
1958 cookie->type = USER_ACTION_COOKIE_SFLOW;
1959 cookie->sflow.vlan_tci = vlan_tci;
1961 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1962 * port information") for the interpretation of cookie->output. */
1963 switch (n_outputs) {
1965 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1966 cookie->sflow.output = 0x40000000 | 256;
1970 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
1972 cookie->sflow.output = ifindex;
1977 /* 0x80000000 means "multiple output ports. */
1978 cookie->sflow.output = 0x80000000 | n_outputs;
1983 /* Compose SAMPLE action for sFlow bridge sampling. */
1985 compose_sflow_action(const struct xbridge *xbridge,
1986 struct ofpbuf *odp_actions,
1987 const struct flow *flow,
1988 odp_port_t odp_port)
1990 uint32_t probability;
1991 union user_action_cookie cookie;
1993 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
1997 probability = dpif_sflow_get_probability(xbridge->sflow);
1998 compose_sflow_cookie(xbridge, htons(0), odp_port,
1999 odp_port == ODPP_NONE ? 0 : 1, &cookie);
2001 return compose_sample_action(xbridge, odp_actions, flow, probability,
2002 &cookie, sizeof cookie.sflow);
2006 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
2007 uint32_t obs_domain_id, uint32_t obs_point_id,
2008 union user_action_cookie *cookie)
2010 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
2011 cookie->flow_sample.probability = probability;
2012 cookie->flow_sample.collector_set_id = collector_set_id;
2013 cookie->flow_sample.obs_domain_id = obs_domain_id;
2014 cookie->flow_sample.obs_point_id = obs_point_id;
2018 compose_ipfix_cookie(union user_action_cookie *cookie)
2020 cookie->type = USER_ACTION_COOKIE_IPFIX;
2023 /* Compose SAMPLE action for IPFIX bridge sampling. */
2025 compose_ipfix_action(const struct xbridge *xbridge,
2026 struct ofpbuf *odp_actions,
2027 const struct flow *flow)
2029 uint32_t probability;
2030 union user_action_cookie cookie;
2032 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
2036 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
2037 compose_ipfix_cookie(&cookie);
2039 compose_sample_action(xbridge, odp_actions, flow, probability,
2040 &cookie, sizeof cookie.ipfix);
2043 /* SAMPLE action for sFlow must be first action in any given list of
2044 * actions. At this point we do not have all information required to
2045 * build it. So try to build sample action as complete as possible. */
2047 add_sflow_action(struct xlate_ctx *ctx)
2049 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
2050 &ctx->xout->odp_actions,
2051 &ctx->xin->flow, ODPP_NONE);
2052 ctx->sflow_odp_port = 0;
2053 ctx->sflow_n_outputs = 0;
2056 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
2057 * of actions, eventually after the SAMPLE action for sFlow. */
2059 add_ipfix_action(struct xlate_ctx *ctx)
2061 compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions,
2065 /* Fix SAMPLE action according to data collected while composing ODP actions.
2066 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
2067 * USERSPACE action's user-cookie which is required for sflow. */
2069 fix_sflow_action(struct xlate_ctx *ctx)
2071 const struct flow *base = &ctx->base_flow;
2072 union user_action_cookie *cookie;
2074 if (!ctx->user_cookie_offset) {
2078 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
2079 sizeof cookie->sflow);
2080 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2082 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
2083 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
2086 static enum slow_path_reason
2087 process_special(struct xlate_ctx *ctx, const struct flow *flow,
2088 const struct xport *xport, const struct ofpbuf *packet)
2090 struct flow_wildcards *wc = &ctx->xout->wc;
2091 const struct xbridge *xbridge = ctx->xbridge;
2095 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
2097 cfm_process_heartbeat(xport->cfm, packet);
2100 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
2102 bfd_process_packet(xport->bfd, flow, packet);
2103 /* If POLL received, immediately sends FINAL back. */
2104 if (bfd_should_send_packet(xport->bfd)) {
2105 ofproto_dpif_monitor_port_send_soon(xport->ofport);
2109 } else if (xport->xbundle && xport->xbundle->lacp
2110 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2112 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
2115 } else if (xbridge->stp && stp_should_process_flow(flow, wc)) {
2117 stp_process_packet(xport, packet);
2126 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
2129 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
2130 struct flow_wildcards *wc = &ctx->xout->wc;
2131 struct flow *flow = &ctx->xin->flow;
2132 ovs_be16 flow_vlan_tci;
2133 uint32_t flow_pkt_mark;
2134 uint8_t flow_nw_tos;
2135 odp_port_t out_port, odp_port;
2138 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2139 * before traversing a patch port. */
2140 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 26);
2143 xlate_report(ctx, "Nonexistent output port");
2145 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
2146 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2148 } else if (check_stp) {
2149 if (is_stp(&ctx->base_flow)) {
2150 if (!xport_stp_listen_state(xport)) {
2151 xlate_report(ctx, "STP not in listening state, "
2152 "skipping bpdu output");
2155 } else if (!xport_stp_forward_state(xport)) {
2156 xlate_report(ctx, "STP not in forwarding state, "
2162 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
2163 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
2168 const struct xport *peer = xport->peer;
2169 struct flow old_flow = ctx->xin->flow;
2170 enum slow_path_reason special;
2172 ctx->xbridge = peer->xbridge;
2173 flow->in_port.ofp_port = peer->ofp_port;
2174 flow->metadata = htonll(0);
2175 memset(&flow->tunnel, 0, sizeof flow->tunnel);
2176 memset(flow->regs, 0, sizeof flow->regs);
2178 special = process_special(ctx, &ctx->xin->flow, peer,
2181 ctx->xout->slow |= special;
2182 } else if (may_receive(peer, ctx)) {
2183 if (xport_stp_forward_state(peer)) {
2184 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2186 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
2187 * learning action look at the packet, then drop it. */
2188 struct flow old_base_flow = ctx->base_flow;
2189 size_t old_size = ofpbuf_size(&ctx->xout->odp_actions);
2190 mirror_mask_t old_mirrors = ctx->xout->mirrors;
2191 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
2192 ctx->xout->mirrors = old_mirrors;
2193 ctx->base_flow = old_base_flow;
2194 ofpbuf_set_size(&ctx->xout->odp_actions, old_size);
2198 ctx->xin->flow = old_flow;
2199 ctx->xbridge = xport->xbridge;
2201 if (ctx->xin->resubmit_stats) {
2202 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2203 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
2205 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
2208 if (ctx->xin->xcache) {
2209 struct xc_entry *entry;
2211 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2212 entry->u.dev.tx = netdev_ref(xport->netdev);
2213 entry->u.dev.rx = netdev_ref(peer->netdev);
2214 entry->u.dev.bfd = bfd_ref(peer->bfd);
2219 flow_vlan_tci = flow->vlan_tci;
2220 flow_pkt_mark = flow->pkt_mark;
2221 flow_nw_tos = flow->nw_tos;
2223 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
2224 wc->masks.nw_tos |= IP_DSCP_MASK;
2225 flow->nw_tos &= ~IP_DSCP_MASK;
2226 flow->nw_tos |= dscp;
2229 if (xport->is_tunnel) {
2230 /* Save tunnel metadata so that changes made due to
2231 * the Logical (tunnel) Port are not visible for any further
2232 * matches, while explicit set actions on tunnel metadata are.
2234 struct flow_tnl flow_tnl = flow->tunnel;
2235 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
2236 if (odp_port == ODPP_NONE) {
2237 xlate_report(ctx, "Tunneling decided against output");
2238 goto out; /* restore flow_nw_tos */
2240 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
2241 xlate_report(ctx, "Not tunneling to our own address");
2242 goto out; /* restore flow_nw_tos */
2244 if (ctx->xin->resubmit_stats) {
2245 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2247 if (ctx->xin->xcache) {
2248 struct xc_entry *entry;
2250 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2251 entry->u.dev.tx = netdev_ref(xport->netdev);
2253 out_port = odp_port;
2254 commit_odp_tunnel_action(flow, &ctx->base_flow,
2255 &ctx->xout->odp_actions);
2256 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2258 odp_port = xport->odp_port;
2259 out_port = odp_port;
2260 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
2261 ofp_port_t vlandev_port;
2263 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2264 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
2265 ofp_port, flow->vlan_tci);
2266 if (vlandev_port != ofp_port) {
2267 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
2268 flow->vlan_tci = htons(0);
2273 if (out_port != ODPP_NONE) {
2274 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
2275 &ctx->xout->odp_actions,
2278 if (ctx->use_recirc) {
2279 struct ovs_action_hash *act_hash;
2280 struct xlate_recirc *xr = &ctx->recirc;
2283 act_hash = nl_msg_put_unspec_uninit(&ctx->xout->odp_actions,
2284 OVS_ACTION_ATTR_HASH,
2286 act_hash->hash_alg = xr->hash_alg;
2287 act_hash->hash_basis = xr->hash_basis;
2289 /* Recirc action. */
2290 nl_msg_put_u32(&ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC,
2293 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
2297 ctx->sflow_odp_port = odp_port;
2298 ctx->sflow_n_outputs++;
2299 ctx->xout->nf_output_iface = ofp_port;
2304 flow->vlan_tci = flow_vlan_tci;
2305 flow->pkt_mark = flow_pkt_mark;
2306 flow->nw_tos = flow_nw_tos;
2310 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
2312 compose_output_action__(ctx, ofp_port, true);
2316 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
2318 struct rule_dpif *old_rule = ctx->rule;
2319 const struct rule_actions *actions;
2321 if (ctx->xin->resubmit_stats) {
2322 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
2328 actions = rule_dpif_get_actions(rule);
2329 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
2330 ctx->rule = old_rule;
2335 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
2337 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2339 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
2340 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
2341 MAX_RESUBMIT_RECURSION);
2342 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
2343 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
2344 } else if (ofpbuf_size(&ctx->xout->odp_actions) > UINT16_MAX) {
2345 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
2346 } else if (ofpbuf_size(&ctx->stack) >= 65536) {
2347 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
2356 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
2357 bool may_packet_in, bool honor_table_miss)
2359 if (xlate_resubmit_resource_check(ctx)) {
2360 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
2361 bool skip_wildcards = ctx->xin->skip_wildcards;
2362 uint8_t old_table_id = ctx->table_id;
2363 struct rule_dpif *rule;
2364 enum rule_dpif_lookup_verdict verdict;
2365 enum ofputil_port_config config = 0;
2367 ctx->table_id = table_id;
2369 /* Look up a flow with 'in_port' as the input port. Then restore the
2370 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2371 * have surprising behavior). */
2372 ctx->xin->flow.in_port.ofp_port = in_port;
2373 verdict = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
2376 ? &ctx->xout->wc : NULL,
2378 &ctx->table_id, &rule,
2379 ctx->xin->xcache != NULL,
2380 ctx->xin->resubmit_stats);
2381 ctx->xin->flow.in_port.ofp_port = old_in_port;
2383 if (ctx->xin->resubmit_hook) {
2384 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
2388 case RULE_DPIF_LOOKUP_VERDICT_MATCH:
2390 case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER:
2391 if (may_packet_in) {
2392 struct xport *xport;
2394 xport = get_ofp_port(ctx->xbridge,
2395 ctx->xin->flow.in_port.ofp_port);
2396 config = xport ? xport->config : 0;
2399 /* Fall through to drop */
2400 case RULE_DPIF_LOOKUP_VERDICT_DROP:
2401 config = OFPUTIL_PC_NO_PACKET_IN;
2403 case RULE_DPIF_LOOKUP_VERDICT_DEFAULT:
2404 if (!ofproto_dpif_wants_packet_in_on_miss(ctx->xbridge->ofproto)) {
2405 config = OFPUTIL_PC_NO_PACKET_IN;
2412 choose_miss_rule(config, ctx->xbridge->miss_rule,
2413 ctx->xbridge->no_packet_in_rule, &rule,
2414 ctx->xin->xcache != NULL);
2418 /* Fill in the cache entry here instead of xlate_recursively
2419 * to make the reference counting more explicit. We take a
2420 * reference in the lookups above if we are going to cache the
2422 if (ctx->xin->xcache) {
2423 struct xc_entry *entry;
2425 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
2426 entry->u.rule = rule;
2428 xlate_recursively(ctx, rule);
2431 ctx->table_id = old_table_id;
2439 xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
2440 struct ofputil_bucket *bucket)
2442 if (ctx->xin->resubmit_stats) {
2443 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
2445 if (ctx->xin->xcache) {
2446 struct xc_entry *entry;
2448 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
2449 entry->u.group.group = group_dpif_ref(group);
2450 entry->u.group.bucket = bucket;
2455 xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
2457 uint64_t action_list_stub[1024 / 8];
2458 struct ofpbuf action_list, action_set;
2460 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
2461 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2463 ofpacts_execute_action_set(&action_list, &action_set);
2465 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
2468 ofpbuf_uninit(&action_set);
2469 ofpbuf_uninit(&action_list);
2473 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
2475 struct ofputil_bucket *bucket;
2476 const struct list *buckets;
2477 struct flow old_flow = ctx->xin->flow;
2479 group_dpif_get_buckets(group, &buckets);
2481 LIST_FOR_EACH (bucket, list_node, buckets) {
2482 xlate_group_bucket(ctx, bucket);
2483 /* Roll back flow to previous state.
2484 * This is equivalent to cloning the packet for each bucket.
2486 * As a side effect any subsequently applied actions will
2487 * also effectively be applied to a clone of the packet taken
2488 * just before applying the all or indirect group. */
2489 ctx->xin->flow = old_flow;
2491 xlate_group_stats(ctx, group, NULL);
2495 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
2497 struct ofputil_bucket *bucket;
2499 bucket = group_first_live_bucket(ctx, group, 0);
2501 xlate_group_bucket(ctx, bucket);
2502 xlate_group_stats(ctx, group, bucket);
2507 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
2509 struct flow_wildcards *wc = &ctx->xout->wc;
2510 struct ofputil_bucket *bucket;
2513 basis = hash_mac(ctx->xin->flow.dl_dst, 0, 0);
2514 bucket = group_best_live_bucket(ctx, group, basis);
2516 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2517 xlate_group_bucket(ctx, bucket);
2518 xlate_group_stats(ctx, group, bucket);
2523 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
2525 ctx->in_group = true;
2527 switch (group_dpif_get_type(group)) {
2529 case OFPGT11_INDIRECT:
2530 xlate_all_group(ctx, group);
2532 case OFPGT11_SELECT:
2533 xlate_select_group(ctx, group);
2536 xlate_ff_group(ctx, group);
2541 group_dpif_unref(group);
2543 ctx->in_group = false;
2547 xlate_group_resource_check(struct xlate_ctx *ctx)
2549 if (!xlate_resubmit_resource_check(ctx)) {
2551 } else if (ctx->in_group) {
2552 /* Prevent nested translation of OpenFlow groups.
2554 * OpenFlow allows this restriction. We enforce this restriction only
2555 * because, with the current architecture, we would otherwise have to
2556 * take a possibly recursive read lock on the ofgroup rwlock, which is
2557 * unsafe given that POSIX allows taking a read lock to block if there
2558 * is a thread blocked on taking the write lock. Other solutions
2559 * without this restriction are also possible, but seem unwarranted
2560 * given the current limited use of groups. */
2561 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2563 VLOG_ERR_RL(&rl, "cannot recursively translate OpenFlow group");
2571 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
2573 if (xlate_group_resource_check(ctx)) {
2574 struct group_dpif *group;
2577 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
2579 xlate_group_action__(ctx, group);
2589 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
2590 const struct ofpact_resubmit *resubmit)
2594 bool may_packet_in = false;
2595 bool honor_table_miss = false;
2597 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
2598 /* Still allow missed packets to be sent to the controller
2599 * if resubmitting from an internal table. */
2600 may_packet_in = true;
2601 honor_table_miss = true;
2604 in_port = resubmit->in_port;
2605 if (in_port == OFPP_IN_PORT) {
2606 in_port = ctx->xin->flow.in_port.ofp_port;
2609 table_id = resubmit->table_id;
2610 if (table_id == 255) {
2611 table_id = ctx->table_id;
2614 xlate_table_action(ctx, in_port, table_id, may_packet_in,
2619 flood_packets(struct xlate_ctx *ctx, bool all)
2621 const struct xport *xport;
2623 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
2624 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
2629 compose_output_action__(ctx, xport->ofp_port, false);
2630 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
2631 compose_output_action(ctx, xport->ofp_port);
2635 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2639 execute_controller_action(struct xlate_ctx *ctx, int len,
2640 enum ofp_packet_in_reason reason,
2641 uint16_t controller_id)
2643 struct ofproto_packet_in *pin;
2644 struct ofpbuf *packet;
2645 struct pkt_metadata md = PKT_METADATA_INITIALIZER(0);
2647 ctx->xout->slow |= SLOW_CONTROLLER;
2648 if (!ctx->xin->packet) {
2652 packet = ofpbuf_clone(ctx->xin->packet);
2654 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2655 &ctx->xout->odp_actions,
2658 odp_execute_actions(NULL, packet, false, &md,
2659 ofpbuf_data(&ctx->xout->odp_actions),
2660 ofpbuf_size(&ctx->xout->odp_actions), NULL);
2662 pin = xmalloc(sizeof *pin);
2663 pin->up.packet_len = ofpbuf_size(packet);
2664 pin->up.packet = ofpbuf_steal_data(packet);
2665 pin->up.reason = reason;
2666 pin->up.table_id = ctx->table_id;
2667 pin->up.cookie = (ctx->rule
2668 ? rule_dpif_get_flow_cookie(ctx->rule)
2671 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
2673 pin->controller_id = controller_id;
2674 pin->send_len = len;
2675 /* If a rule is a table-miss rule then this is
2676 * a table-miss handled by a table-miss rule.
2678 * Else, if rule is internal and has a controller action,
2679 * the later being implied by the rule being processed here,
2680 * then this is a table-miss handled without a table-miss rule.
2682 * Otherwise this is not a table-miss. */
2683 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
2685 if (rule_dpif_is_table_miss(ctx->rule)) {
2686 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
2687 } else if (rule_dpif_is_internal(ctx->rule)) {
2688 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
2691 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
2692 ofpbuf_delete(packet);
2696 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
2698 struct flow_wildcards *wc = &ctx->xout->wc;
2699 struct flow *flow = &ctx->xin->flow;
2702 ovs_assert(eth_type_mpls(mpls->ethertype));
2704 n = flow_count_mpls_labels(flow, wc);
2706 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
2707 &ctx->xout->odp_actions,
2709 } else if (n >= FLOW_MAX_MPLS_LABELS) {
2710 if (ctx->xin->packet != NULL) {
2711 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2712 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
2713 "MPLS push action can't be performed as it would "
2714 "have more MPLS LSEs than the %d supported.",
2715 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
2719 } else if (n >= ctx->xbridge->max_mpls_depth) {
2720 COVERAGE_INC(xlate_actions_mpls_overflow);
2721 ctx->xout->slow |= SLOW_ACTION;
2724 flow_push_mpls(flow, n, mpls->ethertype, wc);
2728 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
2730 struct flow_wildcards *wc = &ctx->xout->wc;
2731 struct flow *flow = &ctx->xin->flow;
2732 int n = flow_count_mpls_labels(flow, wc);
2734 if (!flow_pop_mpls(flow, n, eth_type, wc) && n >= FLOW_MAX_MPLS_LABELS) {
2735 if (ctx->xin->packet != NULL) {
2736 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2737 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
2738 "MPLS pop action can't be performed as it has "
2739 "more MPLS LSEs than the %d supported.",
2740 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
2743 ofpbuf_clear(&ctx->xout->odp_actions);
2748 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
2750 struct flow *flow = &ctx->xin->flow;
2752 if (!is_ip_any(flow)) {
2756 ctx->xout->wc.masks.nw_ttl = 0xff;
2757 if (flow->nw_ttl > 1) {
2763 for (i = 0; i < ids->n_controllers; i++) {
2764 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
2768 /* Stop processing for current table. */
2774 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
2776 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2777 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
2778 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
2783 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
2785 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2786 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
2787 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
2792 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
2794 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2795 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
2796 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
2801 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
2803 struct flow *flow = &ctx->xin->flow;
2804 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
2805 struct flow_wildcards *wc = &ctx->xout->wc;
2807 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2808 if (eth_type_mpls(flow->dl_type)) {
2811 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
2814 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
2816 /* Stop processing for current table. */
2825 xlate_output_action(struct xlate_ctx *ctx,
2826 ofp_port_t port, uint16_t max_len, bool may_packet_in)
2828 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
2830 ctx->xout->nf_output_iface = NF_OUT_DROP;
2834 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
2837 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2838 0, may_packet_in, true);
2844 flood_packets(ctx, false);
2847 flood_packets(ctx, true);
2849 case OFPP_CONTROLLER:
2850 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
2856 if (port != ctx->xin->flow.in_port.ofp_port) {
2857 compose_output_action(ctx, port);
2859 xlate_report(ctx, "skipping output to input port");
2864 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2865 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2866 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2867 ctx->xout->nf_output_iface = prev_nf_output_iface;
2868 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2869 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2870 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2875 xlate_output_reg_action(struct xlate_ctx *ctx,
2876 const struct ofpact_output_reg *or)
2878 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
2879 if (port <= UINT16_MAX) {
2880 union mf_subvalue value;
2882 memset(&value, 0xff, sizeof value);
2883 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
2884 xlate_output_action(ctx, u16_to_ofp(port),
2885 or->max_len, false);
2890 xlate_enqueue_action(struct xlate_ctx *ctx,
2891 const struct ofpact_enqueue *enqueue)
2893 ofp_port_t ofp_port = enqueue->port;
2894 uint32_t queue_id = enqueue->queue;
2895 uint32_t flow_priority, priority;
2898 /* Translate queue to priority. */
2899 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
2901 /* Fall back to ordinary output action. */
2902 xlate_output_action(ctx, enqueue->port, 0, false);
2906 /* Check output port. */
2907 if (ofp_port == OFPP_IN_PORT) {
2908 ofp_port = ctx->xin->flow.in_port.ofp_port;
2909 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
2913 /* Add datapath actions. */
2914 flow_priority = ctx->xin->flow.skb_priority;
2915 ctx->xin->flow.skb_priority = priority;
2916 compose_output_action(ctx, ofp_port);
2917 ctx->xin->flow.skb_priority = flow_priority;
2919 /* Update NetFlow output port. */
2920 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2921 ctx->xout->nf_output_iface = ofp_port;
2922 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2923 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2928 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
2930 uint32_t skb_priority;
2932 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
2933 ctx->xin->flow.skb_priority = skb_priority;
2935 /* Couldn't translate queue to a priority. Nothing to do. A warning
2936 * has already been logged. */
2941 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
2943 const struct xbridge *xbridge = xbridge_;
2954 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
2957 port = get_ofp_port(xbridge, ofp_port);
2958 return port ? port->may_enable : false;
2963 xlate_bundle_action(struct xlate_ctx *ctx,
2964 const struct ofpact_bundle *bundle)
2968 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
2970 CONST_CAST(struct xbridge *, ctx->xbridge));
2971 if (bundle->dst.field) {
2972 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
2975 xlate_output_action(ctx, port, 0, false);
2980 xlate_learn_action(struct xlate_ctx *ctx,
2981 const struct ofpact_learn *learn)
2983 uint64_t ofpacts_stub[1024 / 8];
2984 struct ofputil_flow_mod fm;
2985 struct ofpbuf ofpacts;
2987 ctx->xout->has_learn = true;
2989 learn_mask(learn, &ctx->xout->wc);
2991 if (!ctx->xin->may_learn) {
2995 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2996 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
2997 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, &fm);
2998 ofpbuf_uninit(&ofpacts);
3000 if (ctx->xin->xcache) {
3001 struct xc_entry *entry;
3003 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
3004 /* Lookup the learned rule, taking a reference on it. The reference
3005 * is released when this cache entry is deleted. */
3006 rule_dpif_lookup(ctx->xbridge->ofproto, &ctx->xin->flow, NULL,
3007 &entry->u.learn.rule, true, NULL);
3012 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
3013 uint16_t idle_timeout, uint16_t hard_timeout)
3015 if (tcp_flags & (TCP_FIN | TCP_RST)) {
3016 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
3021 xlate_fin_timeout(struct xlate_ctx *ctx,
3022 const struct ofpact_fin_timeout *oft)
3025 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
3026 oft->fin_idle_timeout, oft->fin_hard_timeout);
3027 if (ctx->xin->xcache) {
3028 struct xc_entry *entry;
3030 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
3031 /* XC_RULE already holds a reference on the rule, none is taken
3033 entry->u.fin.rule = ctx->rule;
3034 entry->u.fin.idle = oft->fin_idle_timeout;
3035 entry->u.fin.hard = oft->fin_hard_timeout;
3041 xlate_sample_action(struct xlate_ctx *ctx,
3042 const struct ofpact_sample *os)
3044 union user_action_cookie cookie;
3045 /* Scale the probability from 16-bit to 32-bit while representing
3046 * the same percentage. */
3047 uint32_t probability = (os->probability << 16) | os->probability;
3049 if (!ctx->xbridge->variable_length_userdata) {
3050 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3052 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
3053 "lacks support (needs Linux 3.10+ or kernel module from "
3058 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3059 &ctx->xout->odp_actions,
3062 compose_flow_sample_cookie(os->probability, os->collector_set_id,
3063 os->obs_domain_id, os->obs_point_id, &cookie);
3064 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
3065 probability, &cookie, sizeof cookie.flow_sample);
3069 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
3071 if (xport->config & (is_stp(&ctx->xin->flow)
3072 ? OFPUTIL_PC_NO_RECV_STP
3073 : OFPUTIL_PC_NO_RECV)) {
3077 /* Only drop packets here if both forwarding and learning are
3078 * disabled. If just learning is enabled, we need to have
3079 * OFPP_NORMAL and the learning action have a look at the packet
3080 * before we can drop it. */
3081 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
3089 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
3091 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3092 ofpbuf_put(&ctx->action_set, on->actions, ofpact_nest_get_action_len(on));
3093 ofpact_pad(&ctx->action_set);
3097 xlate_action_set(struct xlate_ctx *ctx)
3099 uint64_t action_list_stub[1024 / 64];
3100 struct ofpbuf action_list;
3102 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3103 ofpacts_execute_action_set(&action_list, &ctx->action_set);
3104 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
3105 ofpbuf_uninit(&action_list);
3109 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
3110 struct xlate_ctx *ctx)
3112 struct flow_wildcards *wc = &ctx->xout->wc;
3113 struct flow *flow = &ctx->xin->flow;
3114 const struct ofpact *a;
3116 /* dl_type already in the mask, not set below. */
3118 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3119 struct ofpact_controller *controller;
3120 const struct ofpact_metadata *metadata;
3121 const struct ofpact_set_field *set_field;
3122 const struct mf_field *mf;
3130 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
3131 ofpact_get_OUTPUT(a)->max_len, true);
3135 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
3140 case OFPACT_CONTROLLER:
3141 controller = ofpact_get_CONTROLLER(a);
3142 execute_controller_action(ctx, controller->max_len,
3144 controller->controller_id);
3147 case OFPACT_ENQUEUE:
3148 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
3151 case OFPACT_SET_VLAN_VID:
3152 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
3153 if (flow->vlan_tci & htons(VLAN_CFI) ||
3154 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
3155 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
3156 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
3161 case OFPACT_SET_VLAN_PCP:
3162 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
3163 if (flow->vlan_tci & htons(VLAN_CFI) ||
3164 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
3165 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
3166 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
3167 << VLAN_PCP_SHIFT) | VLAN_CFI);
3171 case OFPACT_STRIP_VLAN:
3172 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3173 flow->vlan_tci = htons(0);
3176 case OFPACT_PUSH_VLAN:
3177 /* XXX 802.1AD(QinQ) */
3178 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3179 flow->vlan_tci = htons(VLAN_CFI);
3182 case OFPACT_SET_ETH_SRC:
3183 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3184 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
3187 case OFPACT_SET_ETH_DST:
3188 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3189 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
3192 case OFPACT_SET_IPV4_SRC:
3193 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3194 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3195 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
3199 case OFPACT_SET_IPV4_DST:
3200 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3201 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3202 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
3206 case OFPACT_SET_IP_DSCP:
3207 if (is_ip_any(flow)) {
3208 wc->masks.nw_tos |= IP_DSCP_MASK;
3209 flow->nw_tos &= ~IP_DSCP_MASK;
3210 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
3214 case OFPACT_SET_IP_ECN:
3215 if (is_ip_any(flow)) {
3216 wc->masks.nw_tos |= IP_ECN_MASK;
3217 flow->nw_tos &= ~IP_ECN_MASK;
3218 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
3222 case OFPACT_SET_IP_TTL:
3223 if (is_ip_any(flow)) {
3224 wc->masks.nw_ttl = 0xff;
3225 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
3229 case OFPACT_SET_L4_SRC_PORT:
3230 if (is_ip_any(flow)) {
3231 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3232 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3233 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
3237 case OFPACT_SET_L4_DST_PORT:
3238 if (is_ip_any(flow)) {
3239 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3240 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3241 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
3245 case OFPACT_RESUBMIT:
3246 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
3249 case OFPACT_SET_TUNNEL:
3250 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
3253 case OFPACT_SET_QUEUE:
3254 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
3257 case OFPACT_POP_QUEUE:
3258 flow->skb_priority = ctx->orig_skb_priority;
3261 case OFPACT_REG_MOVE:
3262 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
3265 case OFPACT_REG_LOAD:
3266 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow, wc);
3269 case OFPACT_SET_FIELD:
3270 set_field = ofpact_get_SET_FIELD(a);
3271 mf = set_field->field;
3273 /* Set field action only ever overwrites packet's outermost
3274 * applicable header fields. Do nothing if no header exists. */
3275 if (mf->id == MFF_VLAN_VID) {
3276 wc->masks.vlan_tci |= htons(VLAN_CFI);
3277 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
3280 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
3281 /* 'dl_type' is already unwildcarded. */
3282 && !eth_type_mpls(flow->dl_type)) {
3286 mf_mask_field_and_prereqs(mf, &wc->masks);
3287 mf_set_flow_value(mf, &set_field->value, flow);
3290 case OFPACT_STACK_PUSH:
3291 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
3295 case OFPACT_STACK_POP:
3296 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
3300 case OFPACT_PUSH_MPLS:
3301 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
3304 case OFPACT_POP_MPLS:
3305 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
3308 case OFPACT_SET_MPLS_LABEL:
3309 compose_set_mpls_label_action(
3310 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
3313 case OFPACT_SET_MPLS_TC:
3314 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
3317 case OFPACT_SET_MPLS_TTL:
3318 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
3321 case OFPACT_DEC_MPLS_TTL:
3322 if (compose_dec_mpls_ttl_action(ctx)) {
3327 case OFPACT_DEC_TTL:
3328 wc->masks.nw_ttl = 0xff;
3329 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
3335 /* Nothing to do. */
3338 case OFPACT_MULTIPATH:
3339 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
3343 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
3346 case OFPACT_OUTPUT_REG:
3347 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
3351 xlate_learn_action(ctx, ofpact_get_LEARN(a));
3358 case OFPACT_FIN_TIMEOUT:
3359 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3360 ctx->xout->has_fin_timeout = true;
3361 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
3364 case OFPACT_CLEAR_ACTIONS:
3365 ofpbuf_clear(&ctx->action_set);
3368 case OFPACT_WRITE_ACTIONS:
3369 xlate_write_actions(ctx, a);
3372 case OFPACT_WRITE_METADATA:
3373 metadata = ofpact_get_WRITE_METADATA(a);
3374 flow->metadata &= ~metadata->mask;
3375 flow->metadata |= metadata->metadata & metadata->mask;
3379 /* Not implemented yet. */
3382 case OFPACT_GOTO_TABLE: {
3383 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
3385 ovs_assert(ctx->table_id < ogt->table_id);
3386 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3387 ogt->table_id, true, true);
3392 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
3399 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
3400 const struct flow *flow, struct rule_dpif *rule,
3401 uint16_t tcp_flags, const struct ofpbuf *packet)
3403 xin->ofproto = ofproto;
3405 xin->packet = packet;
3406 xin->may_learn = packet != NULL;
3409 xin->ofpacts = NULL;
3410 xin->ofpacts_len = 0;
3411 xin->tcp_flags = tcp_flags;
3412 xin->resubmit_hook = NULL;
3413 xin->report_hook = NULL;
3414 xin->resubmit_stats = NULL;
3415 xin->skip_wildcards = false;
3419 xlate_out_uninit(struct xlate_out *xout)
3422 ofpbuf_uninit(&xout->odp_actions);
3426 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
3427 * into datapath actions, using 'ctx', and discards the datapath actions. */
3429 xlate_actions_for_side_effects(struct xlate_in *xin)
3431 struct xlate_out xout;
3433 xlate_actions(xin, &xout);
3434 xlate_out_uninit(&xout);
3438 xlate_report(struct xlate_ctx *ctx, const char *s)
3440 if (ctx->xin->report_hook) {
3441 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
3446 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
3449 dst->slow = src->slow;
3450 dst->has_learn = src->has_learn;
3451 dst->has_normal = src->has_normal;
3452 dst->has_fin_timeout = src->has_fin_timeout;
3453 dst->nf_output_iface = src->nf_output_iface;
3454 dst->mirrors = src->mirrors;
3456 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
3457 sizeof dst->odp_actions_stub);
3458 ofpbuf_put(&dst->odp_actions, ofpbuf_data(&src->odp_actions),
3459 ofpbuf_size(&src->odp_actions));
3462 static struct skb_priority_to_dscp *
3463 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
3465 struct skb_priority_to_dscp *pdscp;
3468 hash = hash_int(skb_priority, 0);
3469 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
3470 if (pdscp->skb_priority == skb_priority) {
3478 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
3481 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
3482 *dscp = pdscp ? pdscp->dscp : 0;
3483 return pdscp != NULL;
3487 clear_skb_priorities(struct xport *xport)
3489 struct skb_priority_to_dscp *pdscp, *next;
3491 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
3492 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
3498 actions_output_to_local_port(const struct xlate_ctx *ctx)
3500 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
3501 const struct nlattr *a;
3504 NL_ATTR_FOR_EACH_UNSAFE (a, left, ofpbuf_data(&ctx->xout->odp_actions),
3505 ofpbuf_size(&ctx->xout->odp_actions)) {
3506 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
3507 && nl_attr_get_odp_port(a) == local_odp_port) {
3514 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
3515 * into datapath actions in 'odp_actions', using 'ctx'.
3517 * The caller must take responsibility for eventually freeing 'xout', with
3518 * xlate_out_uninit(). */
3520 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
3522 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
3523 struct flow_wildcards *wc = &xout->wc;
3524 struct flow *flow = &xin->flow;
3525 struct rule_dpif *rule = NULL;
3527 const struct rule_actions *actions = NULL;
3528 enum slow_path_reason special;
3529 const struct ofpact *ofpacts;
3530 struct xport *in_port;
3531 struct flow orig_flow;
3532 struct xlate_ctx ctx;
3537 COVERAGE_INC(xlate_actions);
3539 /* Flow initialization rules:
3540 * - 'base_flow' must match the kernel's view of the packet at the
3541 * time that action processing starts. 'flow' represents any
3542 * transformations we wish to make through actions.
3543 * - By default 'base_flow' and 'flow' are the same since the input
3544 * packet matches the output before any actions are applied.
3545 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
3546 * of the received packet as seen by the kernel. If we later output
3547 * to another device without any modifications this will cause us to
3548 * insert a new tag since the original one was stripped off by the
3550 * - Tunnel metadata as received is retained in 'flow'. This allows
3551 * tunnel metadata matching also in later tables.
3552 * Since a kernel action for setting the tunnel metadata will only be
3553 * generated with actual tunnel output, changing the tunnel metadata
3554 * values in 'flow' (such as tun_id) will only have effect with a later
3555 * tunnel output action.
3556 * - Tunnel 'base_flow' is completely cleared since that is what the
3557 * kernel does. If we wish to maintain the original values an action
3558 * needs to be generated. */
3563 ctx.xout->has_learn = false;
3564 ctx.xout->has_normal = false;
3565 ctx.xout->has_fin_timeout = false;
3566 ctx.xout->nf_output_iface = NF_OUT_DROP;
3567 ctx.xout->mirrors = 0;
3568 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
3569 sizeof ctx.xout->odp_actions_stub);
3570 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
3572 ctx.xbridge = xbridge_lookup(xcfg, xin->ofproto);
3577 ctx.rule = xin->rule;
3579 ctx.base_flow = *flow;
3580 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
3581 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
3583 flow_wildcards_init_catchall(wc);
3584 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
3585 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3586 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3587 if (is_ip_any(flow)) {
3588 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3590 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
3592 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
3593 if (ctx.xbridge->netflow) {
3594 netflow_mask_wc(flow, wc);
3599 ctx.in_group = false;
3600 ctx.orig_skb_priority = flow->skb_priority;
3603 ctx.use_recirc = false;
3605 if (!xin->ofpacts && !ctx.rule) {
3606 ctx.table_id = rule_dpif_lookup(ctx.xbridge->ofproto, flow,
3607 !xin->skip_wildcards ? wc : NULL,
3608 &rule, ctx.xin->xcache != NULL,
3609 ctx.xin->resubmit_stats);
3610 if (ctx.xin->resubmit_stats) {
3611 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
3613 if (ctx.xin->xcache) {
3614 struct xc_entry *entry;
3616 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
3617 entry->u.rule = rule;
3621 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
3624 ofpacts = xin->ofpacts;
3625 ofpacts_len = xin->ofpacts_len;
3626 } else if (ctx.rule) {
3627 actions = rule_dpif_get_actions(ctx.rule);
3628 ofpacts = actions->ofpacts;
3629 ofpacts_len = actions->ofpacts_len;
3634 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
3635 ofpbuf_use_stub(&ctx.action_set,
3636 ctx.action_set_stub, sizeof ctx.action_set_stub);
3638 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3639 /* Do this conditionally because the copy is expensive enough that it
3640 * shows up in profiles. */
3644 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3645 switch (ctx.xbridge->frag) {
3646 case OFPC_FRAG_NORMAL:
3647 /* We must pretend that transport ports are unavailable. */
3648 flow->tp_src = ctx.base_flow.tp_src = htons(0);
3649 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
3652 case OFPC_FRAG_DROP:
3655 case OFPC_FRAG_REASM:
3658 case OFPC_FRAG_NX_MATCH:
3659 /* Nothing to do. */
3662 case OFPC_INVALID_TTL_TO_CONTROLLER:
3667 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
3668 if (in_port && in_port->is_tunnel) {
3669 if (ctx.xin->resubmit_stats) {
3670 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
3672 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
3675 if (ctx.xin->xcache) {
3676 struct xc_entry *entry;
3678 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
3679 entry->u.dev.rx = netdev_ref(in_port->netdev);
3680 entry->u.dev.bfd = bfd_ref(in_port->bfd);
3684 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
3686 ctx.xout->slow |= special;
3688 size_t sample_actions_len;
3690 if (flow->in_port.ofp_port
3691 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
3692 flow->in_port.ofp_port,
3694 ctx.base_flow.vlan_tci = 0;
3697 add_sflow_action(&ctx);
3698 add_ipfix_action(&ctx);
3699 sample_actions_len = ofpbuf_size(&ctx.xout->odp_actions);
3701 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
3702 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
3704 /* We've let OFPP_NORMAL and the learning action look at the
3705 * packet, so drop it now if forwarding is disabled. */
3706 if (in_port && !xport_stp_forward_state(in_port)) {
3707 ofpbuf_set_size(&ctx.xout->odp_actions, sample_actions_len);
3711 if (ofpbuf_size(&ctx.action_set)) {
3712 xlate_action_set(&ctx);
3715 if (ctx.xbridge->has_in_band
3716 && in_band_must_output_to_local_port(flow)
3717 && !actions_output_to_local_port(&ctx)) {
3718 compose_output_action(&ctx, OFPP_LOCAL);
3721 fix_sflow_action(&ctx);
3723 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3724 add_mirror_actions(&ctx, &orig_flow);
3728 if (nl_attr_oversized(ofpbuf_size(&ctx.xout->odp_actions))) {
3729 /* These datapath actions are too big for a Netlink attribute, so we
3730 * can't hand them to the kernel directly. dpif_execute() can execute
3731 * them one by one with help, so just mark the result as SLOW_ACTION to
3732 * prevent the flow from being installed. */
3733 COVERAGE_INC(xlate_actions_oversize);
3734 ctx.xout->slow |= SLOW_ACTION;
3737 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3738 if (ctx.xin->resubmit_stats) {
3739 mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
3740 ctx.xin->resubmit_stats->n_packets,
3741 ctx.xin->resubmit_stats->n_bytes);
3743 if (ctx.xin->xcache) {
3744 struct xc_entry *entry;
3746 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_MIRROR);
3747 entry->u.mirror.mbridge = mbridge_ref(ctx.xbridge->mbridge);
3748 entry->u.mirror.mirrors = xout->mirrors;
3752 if (ctx.xbridge->netflow) {
3753 /* Only update netflow if we don't have controller flow. We don't
3754 * report NetFlow expiration messages for such facets because they
3755 * are just part of the control logic for the network, not real
3757 if (ofpacts_len == 0
3758 || ofpacts->type != OFPACT_CONTROLLER
3759 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
3760 if (ctx.xin->resubmit_stats) {
3761 netflow_flow_update(ctx.xbridge->netflow, flow,
3762 xout->nf_output_iface,
3763 ctx.xin->resubmit_stats);
3765 if (ctx.xin->xcache) {
3766 struct xc_entry *entry;
3768 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
3769 entry->u.nf.netflow = netflow_ref(ctx.xbridge->netflow);
3770 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
3771 entry->u.nf.iface = xout->nf_output_iface;
3776 ofpbuf_uninit(&ctx.stack);
3777 ofpbuf_uninit(&ctx.action_set);
3779 /* Clear the metadata and register wildcard masks, because we won't
3780 * use non-header fields as part of the cache. */
3781 flow_wildcards_clear_non_packet_fields(wc);
3783 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow uses
3784 * the low 8 bits of the 16-bit tp_src and tp_dst members to represent
3785 * these fields. The datapath interface, on the other hand, represents
3786 * them with just 8 bits each. This means that if the high 8 bits of the
3787 * masks for these fields somehow become set, then they will get chopped
3788 * off by a round trip through the datapath, and revalidation will spot
3789 * that as an inconsistency and delete the flow. Avoid the problem here by
3790 * making sure that only the low 8 bits of either field can be unwildcarded
3794 wc->masks.tp_src &= htons(UINT8_MAX);
3795 wc->masks.tp_dst &= htons(UINT8_MAX);
3799 /* Sends 'packet' out 'ofport'.
3800 * May modify 'packet'.
3801 * Returns 0 if successful, otherwise a positive errno value. */
3803 xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3805 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
3806 struct xport *xport;
3807 struct ofpact_output output;
3810 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
3811 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
3812 flow_extract(packet, NULL, &flow);
3813 flow.in_port.ofp_port = OFPP_NONE;
3815 xport = xport_lookup(xcfg, ofport);
3819 output.port = xport->ofp_port;
3822 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
3823 &output.ofpact, sizeof output,
3827 struct xlate_cache *
3828 xlate_cache_new(void)
3830 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
3832 ofpbuf_init(&xcache->entries, 512);
3836 static struct xc_entry *
3837 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
3839 struct xc_entry *entry;
3841 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
3848 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
3850 if (entry->u.dev.tx) {
3851 netdev_vport_inc_tx(entry->u.dev.tx, stats);
3853 if (entry->u.dev.rx) {
3854 netdev_vport_inc_rx(entry->u.dev.rx, stats);
3856 if (entry->u.dev.bfd) {
3857 bfd_account_rx(entry->u.dev.bfd, stats);
3862 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
3864 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
3865 struct xbridge *xbridge;
3866 struct xbundle *xbundle;
3867 struct flow_wildcards wc;
3869 xbridge = xbridge_lookup(xcfg, ofproto);
3874 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
3880 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
3883 /* Push stats and perform side effects of flow translation. */
3885 xlate_push_stats(struct xlate_cache *xcache, bool may_learn,
3886 const struct dpif_flow_stats *stats)
3888 struct xc_entry *entry;
3889 struct ofpbuf entries = xcache->entries;
3891 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
3892 switch (entry->type) {
3894 rule_dpif_credit_stats(entry->u.rule, stats);
3897 bond_account(entry->u.bond.bond, entry->u.bond.flow,
3898 entry->u.bond.vid, stats->n_bytes);
3901 xlate_cache_netdev(entry, stats);
3904 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
3905 entry->u.nf.iface, stats);
3908 mirror_update_stats(entry->u.mirror.mbridge,
3909 entry->u.mirror.mirrors,
3910 stats->n_packets, stats->n_bytes);
3914 struct rule_dpif *rule = entry->u.learn.rule;
3916 /* Reset the modified time for a rule that is equivalent to
3917 * the currently cached rule. If the rule is not the exact
3918 * rule we have cached, update the reference that we have. */
3919 entry->u.learn.rule = ofproto_dpif_refresh_rule(rule);
3923 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
3924 entry->u.normal.vlan);
3926 case XC_FIN_TIMEOUT:
3927 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
3928 entry->u.fin.idle, entry->u.fin.hard);
3931 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
3941 xlate_dev_unref(struct xc_entry *entry)
3943 if (entry->u.dev.tx) {
3944 netdev_close(entry->u.dev.tx);
3946 if (entry->u.dev.rx) {
3947 netdev_close(entry->u.dev.rx);
3949 if (entry->u.dev.bfd) {
3950 bfd_unref(entry->u.dev.bfd);
3955 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
3957 netflow_expire(netflow, flow);
3958 netflow_flow_clear(netflow, flow);
3959 netflow_unref(netflow);
3964 xlate_cache_clear(struct xlate_cache *xcache)
3966 struct xc_entry *entry;
3967 struct ofpbuf entries;
3973 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
3974 switch (entry->type) {
3976 rule_dpif_unref(entry->u.rule);
3979 free(entry->u.bond.flow);
3980 bond_unref(entry->u.bond.bond);
3983 xlate_dev_unref(entry);
3986 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
3989 mbridge_unref(entry->u.mirror.mbridge);
3992 /* 'u.learn.rule' is the learned rule. */
3993 rule_dpif_unref(entry->u.learn.rule);
3996 free(entry->u.normal.flow);
3998 case XC_FIN_TIMEOUT:
3999 /* 'u.fin.rule' is always already held as a XC_RULE, which
4000 * has already released it's reference above. */
4003 group_dpif_unref(entry->u.group.group);
4010 ofpbuf_clear(&xcache->entries);
4014 xlate_cache_delete(struct xlate_cache *xcache)
4016 xlate_cache_clear(xcache);
4017 ofpbuf_uninit(&xcache->entries);