1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. */
17 #include "ofproto/ofproto-dpif-xlate.h"
25 #include "byte-order.h"
30 #include "dynamic-string.h"
35 #include "mac-learning.h"
36 #include "mcast-snooping.h"
37 #include "meta-flow.h"
38 #include "multipath.h"
39 #include "netdev-vport.h"
42 #include "odp-execute.h"
43 #include "ofp-actions.h"
44 #include "ofproto/ofproto-dpif-ipfix.h"
45 #include "ofproto/ofproto-dpif-mirror.h"
46 #include "ofproto/ofproto-dpif-monitor.h"
47 #include "ofproto/ofproto-dpif-sflow.h"
48 #include "ofproto/ofproto-dpif.h"
49 #include "ofproto/ofproto-provider.h"
50 #include "packet-dpif.h"
54 COVERAGE_DEFINE(xlate_actions
);
55 COVERAGE_DEFINE(xlate_actions_oversize
);
56 COVERAGE_DEFINE(xlate_actions_mpls_overflow
);
58 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate
);
60 /* Maximum depth of flow table recursion (due to resubmit actions) in a
61 * flow translation. */
62 #define MAX_RESUBMIT_RECURSION 64
63 #define MAX_INTERNAL_RESUBMITS 1 /* Max resbmits allowed using rules in
66 /* Timeout for internal rules created to handle recirculation */
67 #define RECIRC_TIMEOUT 60
69 /* Maximum number of resubmit actions in a flow translation, whether they are
70 * recursive or not. */
71 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
74 struct hmap_node hmap_node
; /* Node in global 'xbridges' map. */
75 struct ofproto_dpif
*ofproto
; /* Key in global 'xbridges' map. */
77 struct list xbundles
; /* Owned xbundles. */
78 struct hmap xports
; /* Indexed by ofp_port. */
80 char *name
; /* Name used in log messages. */
81 struct dpif
*dpif
; /* Datapath interface. */
82 struct mac_learning
*ml
; /* Mac learning handle. */
83 struct mcast_snooping
*ms
; /* Multicast Snooping handle. */
84 struct mbridge
*mbridge
; /* Mirroring. */
85 struct dpif_sflow
*sflow
; /* SFlow handle, or null. */
86 struct dpif_ipfix
*ipfix
; /* Ipfix handle, or null. */
87 struct netflow
*netflow
; /* Netflow handle, or null. */
88 struct stp
*stp
; /* STP or null if disabled. */
89 struct rstp
*rstp
; /* RSTP or null if disabled. */
91 /* Special rules installed by ofproto-dpif. */
92 struct rule_dpif
*miss_rule
;
93 struct rule_dpif
*no_packet_in_rule
;
95 enum ofp_config_flags frag
; /* Fragmentation handling. */
96 bool has_in_band
; /* Bridge has in band control? */
97 bool forward_bpdu
; /* Bridge forwards STP BPDUs? */
99 /* True if the datapath supports recirculation. */
102 /* True if the datapath supports variable-length
103 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
104 * False if the datapath supports only 8-byte (or shorter) userdata. */
105 bool variable_length_userdata
;
107 /* Number of MPLS label stack entries that the datapath supports
109 size_t max_mpls_depth
;
113 struct hmap_node hmap_node
; /* In global 'xbundles' map. */
114 struct ofbundle
*ofbundle
; /* Key in global 'xbundles' map. */
116 struct list list_node
; /* In parent 'xbridges' list. */
117 struct xbridge
*xbridge
; /* Parent xbridge. */
119 struct list xports
; /* Contains "struct xport"s. */
121 char *name
; /* Name used in log messages. */
122 struct bond
*bond
; /* Nonnull iff more than one port. */
123 struct lacp
*lacp
; /* LACP handle or null. */
125 enum port_vlan_mode vlan_mode
; /* VLAN mode. */
126 int vlan
; /* -1=trunk port, else a 12-bit VLAN ID. */
127 unsigned long *trunks
; /* Bitmap of trunked VLANs, if 'vlan' == -1.
128 * NULL if all VLANs are trunked. */
129 bool use_priority_tags
; /* Use 802.1p tag for frames in VLAN 0? */
130 bool floodable
; /* No port has OFPUTIL_PC_NO_FLOOD set? */
134 struct hmap_node hmap_node
; /* Node in global 'xports' map. */
135 struct ofport_dpif
*ofport
; /* Key in global 'xports map. */
137 struct hmap_node ofp_node
; /* Node in parent xbridge 'xports' map. */
138 ofp_port_t ofp_port
; /* Key in parent xbridge 'xports' map. */
140 odp_port_t odp_port
; /* Datapath port number or ODPP_NONE. */
142 struct list bundle_node
; /* In parent xbundle (if it exists). */
143 struct xbundle
*xbundle
; /* Parent xbundle or null. */
145 struct netdev
*netdev
; /* 'ofport''s netdev. */
147 struct xbridge
*xbridge
; /* Parent bridge. */
148 struct xport
*peer
; /* Patch port peer or null. */
150 enum ofputil_port_config config
; /* OpenFlow port configuration. */
151 enum ofputil_port_state state
; /* OpenFlow port state. */
152 int stp_port_no
; /* STP port number or -1 if not in use. */
153 struct rstp_port
*rstp_port
; /* RSTP port or null. */
155 struct hmap skb_priorities
; /* Map of 'skb_priority_to_dscp's. */
157 bool may_enable
; /* May be enabled in bonds. */
158 bool is_tunnel
; /* Is a tunnel port. */
160 struct cfm
*cfm
; /* CFM handle or null. */
161 struct bfd
*bfd
; /* BFD handle or null. */
165 struct xlate_in
*xin
;
166 struct xlate_out
*xout
;
168 const struct xbridge
*xbridge
;
170 /* Flow at the last commit. */
171 struct flow base_flow
;
173 /* Tunnel IP destination address as received. This is stored separately
174 * as the base_flow.tunnel is cleared on init to reflect the datapath
175 * behavior. Used to make sure not to send tunneled output to ourselves,
176 * which might lead to an infinite loop. This could happen easily
177 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
178 * actually set the tun_dst field. */
179 ovs_be32 orig_tunnel_ip_dst
;
181 /* Stack for the push and pop actions. Each stack element is of type
182 * "union mf_subvalue". */
183 union mf_subvalue init_stack
[1024 / sizeof(union mf_subvalue
)];
186 /* The rule that we are currently translating, or NULL. */
187 struct rule_dpif
*rule
;
189 /* Resubmit statistics, via xlate_table_action(). */
190 int recurse
; /* Current resubmit nesting depth. */
191 int resubmits
; /* Total number of resubmits. */
192 bool in_group
; /* Currently translating ofgroup, if true. */
194 uint32_t orig_skb_priority
; /* Priority when packet arrived. */
195 uint8_t table_id
; /* OpenFlow table ID where flow was found. */
196 uint32_t sflow_n_outputs
; /* Number of output ports. */
197 odp_port_t sflow_odp_port
; /* Output port for composing sFlow action. */
198 uint16_t user_cookie_offset
;/* Used for user_action_cookie fixup. */
199 bool exit
; /* No further actions should be processed. */
201 bool use_recirc
; /* Should generate recirc? */
202 struct xlate_recirc recirc
; /* Information used for generating
203 * recirculation actions */
205 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
206 * This is a trigger for recirculation in cases where translating an action
207 * or looking up a flow requires access to the fields of the packet after
208 * the MPLS label stack that was originally present. */
211 /* OpenFlow 1.1+ action set.
213 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
214 * When translation is otherwise complete, ofpacts_execute_action_set()
215 * converts it to a set of "struct ofpact"s that can be translated into
216 * datapath actions. */
217 struct ofpbuf action_set
; /* Action set. */
218 uint64_t action_set_stub
[1024 / 8];
221 /* A controller may use OFPP_NONE as the ingress port to indicate that
222 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
223 * when an input bundle is needed for validation (e.g., mirroring or
224 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
225 * any 'port' structs, so care must be taken when dealing with it. */
226 static struct xbundle ofpp_none_bundle
= {
228 .vlan_mode
= PORT_VLAN_TRUNK
231 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
232 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
233 * traffic egressing the 'ofport' with that priority should be marked with. */
234 struct skb_priority_to_dscp
{
235 struct hmap_node hmap_node
; /* Node in 'ofport_dpif''s 'skb_priorities'. */
236 uint32_t skb_priority
; /* Priority of this queue (see struct flow). */
238 uint8_t dscp
; /* DSCP bits to mark outgoing traffic with. */
253 /* xlate_cache entries hold enough information to perform the side effects of
254 * xlate_actions() for a rule, without needing to perform rule translation
255 * from scratch. The primary usage of these is to submit statistics to objects
256 * that a flow relates to, although they may be used for other effects as well
257 * (for instance, refreshing hard timeouts for learned flows). */
261 struct rule_dpif
*rule
;
268 struct netflow
*netflow
;
273 struct mbridge
*mbridge
;
274 mirror_mask_t mirrors
;
282 struct ofproto_dpif
*ofproto
;
283 struct ofputil_flow_mod
*fm
;
284 struct ofpbuf
*ofpacts
;
287 struct ofproto_dpif
*ofproto
;
292 struct rule_dpif
*rule
;
297 struct group_dpif
*group
;
298 struct ofputil_bucket
*bucket
;
303 #define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
304 entries = xcache->entries; \
305 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
307 entry = ofpbuf_try_pull(&entries, sizeof *entry))
310 struct ofpbuf entries
;
313 /* Xlate config contains hash maps of all bridges, bundles and ports.
314 * Xcfgp contains the pointer to the current xlate configuration.
315 * When the main thread needs to change the configuration, it copies xcfgp to
316 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
317 * does not block handler and revalidator threads. */
319 struct hmap xbridges
;
320 struct hmap xbundles
;
323 static OVSRCU_TYPE(struct xlate_cfg
*) xcfgp
= OVSRCU_TYPE_INITIALIZER
;
324 static struct xlate_cfg
*new_xcfg
= NULL
;
326 static bool may_receive(const struct xport
*, struct xlate_ctx
*);
327 static void do_xlate_actions(const struct ofpact
*, size_t ofpacts_len
,
329 static void xlate_normal(struct xlate_ctx
*);
330 static void xlate_report(struct xlate_ctx
*, const char *);
331 static void xlate_table_action(struct xlate_ctx
*, ofp_port_t in_port
,
332 uint8_t table_id
, bool may_packet_in
,
333 bool honor_table_miss
);
334 static bool input_vid_is_valid(uint16_t vid
, struct xbundle
*, bool warn
);
335 static uint16_t input_vid_to_vlan(const struct xbundle
*, uint16_t vid
);
336 static void output_normal(struct xlate_ctx
*, const struct xbundle
*,
338 static void compose_output_action(struct xlate_ctx
*, ofp_port_t ofp_port
);
340 static struct xbridge
*xbridge_lookup(struct xlate_cfg
*,
341 const struct ofproto_dpif
*);
342 static struct xbundle
*xbundle_lookup(struct xlate_cfg
*,
343 const struct ofbundle
*);
344 static struct xport
*xport_lookup(struct xlate_cfg
*,
345 const struct ofport_dpif
*);
346 static struct xport
*get_ofp_port(const struct xbridge
*, ofp_port_t ofp_port
);
347 static struct skb_priority_to_dscp
*get_skb_priority(const struct xport
*,
348 uint32_t skb_priority
);
349 static void clear_skb_priorities(struct xport
*);
350 static bool dscp_from_skb_priority(const struct xport
*, uint32_t skb_priority
,
353 static struct xc_entry
*xlate_cache_add_entry(struct xlate_cache
*xc
,
355 static void xlate_xbridge_init(struct xlate_cfg
*, struct xbridge
*);
356 static void xlate_xbundle_init(struct xlate_cfg
*, struct xbundle
*);
357 static void xlate_xport_init(struct xlate_cfg
*, struct xport
*);
358 static void xlate_xbridge_set(struct xbridge
*, struct dpif
*,
359 struct rule_dpif
*miss_rule
,
360 struct rule_dpif
*no_packet_in_rule
,
361 const struct mac_learning
*, struct stp
*,
362 struct rstp
*, const struct mcast_snooping
*,
363 const struct mbridge
*,
364 const struct dpif_sflow
*,
365 const struct dpif_ipfix
*,
366 const struct netflow
*, enum ofp_config_flags
,
367 bool forward_bpdu
, bool has_in_band
,
369 bool variable_length_userdata
,
370 size_t max_mpls_depth
);
371 static void xlate_xbundle_set(struct xbundle
*xbundle
,
372 enum port_vlan_mode vlan_mode
, int vlan
,
373 unsigned long *trunks
, bool use_priority_tags
,
374 const struct bond
*bond
, const struct lacp
*lacp
,
376 static void xlate_xport_set(struct xport
*xport
, odp_port_t odp_port
,
377 const struct netdev
*netdev
, const struct cfm
*cfm
,
378 const struct bfd
*bfd
, int stp_port_no
,
379 const struct rstp_port
*rstp_port
,
380 enum ofputil_port_config config
,
381 enum ofputil_port_state state
, bool is_tunnel
,
383 static void xlate_xbridge_remove(struct xlate_cfg
*, struct xbridge
*);
384 static void xlate_xbundle_remove(struct xlate_cfg
*, struct xbundle
*);
385 static void xlate_xport_remove(struct xlate_cfg
*, struct xport
*);
386 static void xlate_xbridge_copy(struct xbridge
*);
387 static void xlate_xbundle_copy(struct xbridge
*, struct xbundle
*);
388 static void xlate_xport_copy(struct xbridge
*, struct xbundle
*,
390 static void xlate_xcfg_free(struct xlate_cfg
*);
394 xlate_xbridge_init(struct xlate_cfg
*xcfg
, struct xbridge
*xbridge
)
396 list_init(&xbridge
->xbundles
);
397 hmap_init(&xbridge
->xports
);
398 hmap_insert(&xcfg
->xbridges
, &xbridge
->hmap_node
,
399 hash_pointer(xbridge
->ofproto
, 0));
403 xlate_xbundle_init(struct xlate_cfg
*xcfg
, struct xbundle
*xbundle
)
405 list_init(&xbundle
->xports
);
406 list_insert(&xbundle
->xbridge
->xbundles
, &xbundle
->list_node
);
407 hmap_insert(&xcfg
->xbundles
, &xbundle
->hmap_node
,
408 hash_pointer(xbundle
->ofbundle
, 0));
412 xlate_xport_init(struct xlate_cfg
*xcfg
, struct xport
*xport
)
414 hmap_init(&xport
->skb_priorities
);
415 hmap_insert(&xcfg
->xports
, &xport
->hmap_node
,
416 hash_pointer(xport
->ofport
, 0));
417 hmap_insert(&xport
->xbridge
->xports
, &xport
->ofp_node
,
418 hash_ofp_port(xport
->ofp_port
));
422 xlate_xbridge_set(struct xbridge
*xbridge
,
424 struct rule_dpif
*miss_rule
,
425 struct rule_dpif
*no_packet_in_rule
,
426 const struct mac_learning
*ml
, struct stp
*stp
,
427 struct rstp
*rstp
, const struct mcast_snooping
*ms
,
428 const struct mbridge
*mbridge
,
429 const struct dpif_sflow
*sflow
,
430 const struct dpif_ipfix
*ipfix
,
431 const struct netflow
*netflow
, enum ofp_config_flags frag
,
432 bool forward_bpdu
, bool has_in_band
,
434 bool variable_length_userdata
,
435 size_t max_mpls_depth
)
437 if (xbridge
->ml
!= ml
) {
438 mac_learning_unref(xbridge
->ml
);
439 xbridge
->ml
= mac_learning_ref(ml
);
442 if (xbridge
->ms
!= ms
) {
443 mcast_snooping_unref(xbridge
->ms
);
444 xbridge
->ms
= mcast_snooping_ref(ms
);
447 if (xbridge
->mbridge
!= mbridge
) {
448 mbridge_unref(xbridge
->mbridge
);
449 xbridge
->mbridge
= mbridge_ref(mbridge
);
452 if (xbridge
->sflow
!= sflow
) {
453 dpif_sflow_unref(xbridge
->sflow
);
454 xbridge
->sflow
= dpif_sflow_ref(sflow
);
457 if (xbridge
->ipfix
!= ipfix
) {
458 dpif_ipfix_unref(xbridge
->ipfix
);
459 xbridge
->ipfix
= dpif_ipfix_ref(ipfix
);
462 if (xbridge
->stp
!= stp
) {
463 stp_unref(xbridge
->stp
);
464 xbridge
->stp
= stp_ref(stp
);
467 if (xbridge
->rstp
!= rstp
) {
468 rstp_unref(xbridge
->rstp
);
469 xbridge
->rstp
= rstp_ref(rstp
);
472 if (xbridge
->netflow
!= netflow
) {
473 netflow_unref(xbridge
->netflow
);
474 xbridge
->netflow
= netflow_ref(netflow
);
477 xbridge
->dpif
= dpif
;
478 xbridge
->forward_bpdu
= forward_bpdu
;
479 xbridge
->has_in_band
= has_in_band
;
480 xbridge
->frag
= frag
;
481 xbridge
->miss_rule
= miss_rule
;
482 xbridge
->no_packet_in_rule
= no_packet_in_rule
;
483 xbridge
->enable_recirc
= enable_recirc
;
484 xbridge
->variable_length_userdata
= variable_length_userdata
;
485 xbridge
->max_mpls_depth
= max_mpls_depth
;
489 xlate_xbundle_set(struct xbundle
*xbundle
,
490 enum port_vlan_mode vlan_mode
, int vlan
,
491 unsigned long *trunks
, bool use_priority_tags
,
492 const struct bond
*bond
, const struct lacp
*lacp
,
495 ovs_assert(xbundle
->xbridge
);
497 xbundle
->vlan_mode
= vlan_mode
;
498 xbundle
->vlan
= vlan
;
499 xbundle
->trunks
= trunks
;
500 xbundle
->use_priority_tags
= use_priority_tags
;
501 xbundle
->floodable
= floodable
;
503 if (xbundle
->bond
!= bond
) {
504 bond_unref(xbundle
->bond
);
505 xbundle
->bond
= bond_ref(bond
);
508 if (xbundle
->lacp
!= lacp
) {
509 lacp_unref(xbundle
->lacp
);
510 xbundle
->lacp
= lacp_ref(lacp
);
515 xlate_xport_set(struct xport
*xport
, odp_port_t odp_port
,
516 const struct netdev
*netdev
, const struct cfm
*cfm
,
517 const struct bfd
*bfd
, int stp_port_no
,
518 const struct rstp_port
* rstp_port
,
519 enum ofputil_port_config config
, enum ofputil_port_state state
,
520 bool is_tunnel
, bool may_enable
)
522 xport
->config
= config
;
523 xport
->state
= state
;
524 xport
->stp_port_no
= stp_port_no
;
525 xport
->is_tunnel
= is_tunnel
;
526 xport
->may_enable
= may_enable
;
527 xport
->odp_port
= odp_port
;
529 if (xport
->rstp_port
!= rstp_port
) {
530 rstp_port_unref(xport
->rstp_port
);
531 xport
->rstp_port
= rstp_port_ref(rstp_port
);
534 if (xport
->cfm
!= cfm
) {
535 cfm_unref(xport
->cfm
);
536 xport
->cfm
= cfm_ref(cfm
);
539 if (xport
->bfd
!= bfd
) {
540 bfd_unref(xport
->bfd
);
541 xport
->bfd
= bfd_ref(bfd
);
544 if (xport
->netdev
!= netdev
) {
545 netdev_close(xport
->netdev
);
546 xport
->netdev
= netdev_ref(netdev
);
551 xlate_xbridge_copy(struct xbridge
*xbridge
)
553 struct xbundle
*xbundle
;
555 struct xbridge
*new_xbridge
= xzalloc(sizeof *xbridge
);
556 new_xbridge
->ofproto
= xbridge
->ofproto
;
557 new_xbridge
->name
= xstrdup(xbridge
->name
);
558 xlate_xbridge_init(new_xcfg
, new_xbridge
);
560 xlate_xbridge_set(new_xbridge
,
561 xbridge
->dpif
, xbridge
->miss_rule
,
562 xbridge
->no_packet_in_rule
, xbridge
->ml
, xbridge
->stp
,
563 xbridge
->rstp
, xbridge
->ms
, xbridge
->mbridge
,
564 xbridge
->sflow
, xbridge
->ipfix
, xbridge
->netflow
,
565 xbridge
->frag
, xbridge
->forward_bpdu
,
566 xbridge
->has_in_band
, xbridge
->enable_recirc
,
567 xbridge
->variable_length_userdata
,
568 xbridge
->max_mpls_depth
);
569 LIST_FOR_EACH (xbundle
, list_node
, &xbridge
->xbundles
) {
570 xlate_xbundle_copy(new_xbridge
, xbundle
);
573 /* Copy xports which are not part of a xbundle */
574 HMAP_FOR_EACH (xport
, ofp_node
, &xbridge
->xports
) {
575 if (!xport
->xbundle
) {
576 xlate_xport_copy(new_xbridge
, NULL
, xport
);
582 xlate_xbundle_copy(struct xbridge
*xbridge
, struct xbundle
*xbundle
)
585 struct xbundle
*new_xbundle
= xzalloc(sizeof *xbundle
);
586 new_xbundle
->ofbundle
= xbundle
->ofbundle
;
587 new_xbundle
->xbridge
= xbridge
;
588 new_xbundle
->name
= xstrdup(xbundle
->name
);
589 xlate_xbundle_init(new_xcfg
, new_xbundle
);
591 xlate_xbundle_set(new_xbundle
, xbundle
->vlan_mode
,
592 xbundle
->vlan
, xbundle
->trunks
,
593 xbundle
->use_priority_tags
, xbundle
->bond
, xbundle
->lacp
,
595 LIST_FOR_EACH (xport
, bundle_node
, &xbundle
->xports
) {
596 xlate_xport_copy(xbridge
, new_xbundle
, xport
);
601 xlate_xport_copy(struct xbridge
*xbridge
, struct xbundle
*xbundle
,
604 struct skb_priority_to_dscp
*pdscp
, *new_pdscp
;
605 struct xport
*new_xport
= xzalloc(sizeof *xport
);
606 new_xport
->ofport
= xport
->ofport
;
607 new_xport
->ofp_port
= xport
->ofp_port
;
608 new_xport
->xbridge
= xbridge
;
609 xlate_xport_init(new_xcfg
, new_xport
);
611 xlate_xport_set(new_xport
, xport
->odp_port
, xport
->netdev
, xport
->cfm
,
612 xport
->bfd
, xport
->stp_port_no
, xport
->rstp_port
,
613 xport
->config
, xport
->state
, xport
->is_tunnel
,
617 struct xport
*peer
= xport_lookup(new_xcfg
, xport
->peer
->ofport
);
619 new_xport
->peer
= peer
;
620 new_xport
->peer
->peer
= new_xport
;
625 new_xport
->xbundle
= xbundle
;
626 list_insert(&new_xport
->xbundle
->xports
, &new_xport
->bundle_node
);
629 HMAP_FOR_EACH (pdscp
, hmap_node
, &xport
->skb_priorities
) {
630 new_pdscp
= xmalloc(sizeof *pdscp
);
631 new_pdscp
->skb_priority
= pdscp
->skb_priority
;
632 new_pdscp
->dscp
= pdscp
->dscp
;
633 hmap_insert(&new_xport
->skb_priorities
, &new_pdscp
->hmap_node
,
634 hash_int(new_pdscp
->skb_priority
, 0));
638 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
639 * configuration in xcfgp.
641 * This needs to be called after editing the xlate configuration.
643 * Functions that edit the new xlate configuration are
644 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
650 * edit_xlate_configuration();
652 * xlate_txn_commit(); */
654 xlate_txn_commit(void)
656 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
658 ovsrcu_set(&xcfgp
, new_xcfg
);
659 ovsrcu_postpone(xlate_xcfg_free
, xcfg
);
664 /* Copies the current xlate configuration in xcfgp to new_xcfg.
666 * This needs to be called prior to editing the xlate configuration. */
668 xlate_txn_start(void)
670 struct xbridge
*xbridge
;
671 struct xlate_cfg
*xcfg
;
673 ovs_assert(!new_xcfg
);
675 new_xcfg
= xmalloc(sizeof *new_xcfg
);
676 hmap_init(&new_xcfg
->xbridges
);
677 hmap_init(&new_xcfg
->xbundles
);
678 hmap_init(&new_xcfg
->xports
);
680 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
685 HMAP_FOR_EACH (xbridge
, hmap_node
, &xcfg
->xbridges
) {
686 xlate_xbridge_copy(xbridge
);
692 xlate_xcfg_free(struct xlate_cfg
*xcfg
)
694 struct xbridge
*xbridge
, *next_xbridge
;
700 HMAP_FOR_EACH_SAFE (xbridge
, next_xbridge
, hmap_node
, &xcfg
->xbridges
) {
701 xlate_xbridge_remove(xcfg
, xbridge
);
704 hmap_destroy(&xcfg
->xbridges
);
705 hmap_destroy(&xcfg
->xbundles
);
706 hmap_destroy(&xcfg
->xports
);
711 xlate_ofproto_set(struct ofproto_dpif
*ofproto
, const char *name
,
712 struct dpif
*dpif
, struct rule_dpif
*miss_rule
,
713 struct rule_dpif
*no_packet_in_rule
,
714 const struct mac_learning
*ml
, struct stp
*stp
,
715 struct rstp
*rstp
, const struct mcast_snooping
*ms
,
716 const struct mbridge
*mbridge
,
717 const struct dpif_sflow
*sflow
,
718 const struct dpif_ipfix
*ipfix
,
719 const struct netflow
*netflow
, enum ofp_config_flags frag
,
720 bool forward_bpdu
, bool has_in_band
, bool enable_recirc
,
721 bool variable_length_userdata
, size_t max_mpls_depth
)
723 struct xbridge
*xbridge
;
725 ovs_assert(new_xcfg
);
727 xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
729 xbridge
= xzalloc(sizeof *xbridge
);
730 xbridge
->ofproto
= ofproto
;
732 xlate_xbridge_init(new_xcfg
, xbridge
);
736 xbridge
->name
= xstrdup(name
);
738 xlate_xbridge_set(xbridge
, dpif
, miss_rule
, no_packet_in_rule
, ml
, stp
,
739 rstp
, ms
, mbridge
, sflow
, ipfix
, netflow
, frag
,
740 forward_bpdu
, has_in_band
, enable_recirc
,
741 variable_length_userdata
, max_mpls_depth
);
745 xlate_xbridge_remove(struct xlate_cfg
*xcfg
, struct xbridge
*xbridge
)
747 struct xbundle
*xbundle
, *next_xbundle
;
748 struct xport
*xport
, *next_xport
;
754 HMAP_FOR_EACH_SAFE (xport
, next_xport
, ofp_node
, &xbridge
->xports
) {
755 xlate_xport_remove(xcfg
, xport
);
758 LIST_FOR_EACH_SAFE (xbundle
, next_xbundle
, list_node
, &xbridge
->xbundles
) {
759 xlate_xbundle_remove(xcfg
, xbundle
);
762 hmap_remove(&xcfg
->xbridges
, &xbridge
->hmap_node
);
763 mac_learning_unref(xbridge
->ml
);
764 mcast_snooping_unref(xbridge
->ms
);
765 mbridge_unref(xbridge
->mbridge
);
766 dpif_sflow_unref(xbridge
->sflow
);
767 dpif_ipfix_unref(xbridge
->ipfix
);
768 stp_unref(xbridge
->stp
);
769 rstp_unref(xbridge
->rstp
);
770 hmap_destroy(&xbridge
->xports
);
776 xlate_remove_ofproto(struct ofproto_dpif
*ofproto
)
778 struct xbridge
*xbridge
;
780 ovs_assert(new_xcfg
);
782 xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
783 xlate_xbridge_remove(new_xcfg
, xbridge
);
787 xlate_bundle_set(struct ofproto_dpif
*ofproto
, struct ofbundle
*ofbundle
,
788 const char *name
, enum port_vlan_mode vlan_mode
, int vlan
,
789 unsigned long *trunks
, bool use_priority_tags
,
790 const struct bond
*bond
, const struct lacp
*lacp
,
793 struct xbundle
*xbundle
;
795 ovs_assert(new_xcfg
);
797 xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
799 xbundle
= xzalloc(sizeof *xbundle
);
800 xbundle
->ofbundle
= ofbundle
;
801 xbundle
->xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
803 xlate_xbundle_init(new_xcfg
, xbundle
);
807 xbundle
->name
= xstrdup(name
);
809 xlate_xbundle_set(xbundle
, vlan_mode
, vlan
, trunks
,
810 use_priority_tags
, bond
, lacp
, floodable
);
814 xlate_xbundle_remove(struct xlate_cfg
*xcfg
, struct xbundle
*xbundle
)
816 struct xport
*xport
, *next
;
822 LIST_FOR_EACH_SAFE (xport
, next
, bundle_node
, &xbundle
->xports
) {
823 list_remove(&xport
->bundle_node
);
824 xport
->xbundle
= NULL
;
827 hmap_remove(&xcfg
->xbundles
, &xbundle
->hmap_node
);
828 list_remove(&xbundle
->list_node
);
829 bond_unref(xbundle
->bond
);
830 lacp_unref(xbundle
->lacp
);
836 xlate_bundle_remove(struct ofbundle
*ofbundle
)
838 struct xbundle
*xbundle
;
840 ovs_assert(new_xcfg
);
842 xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
843 xlate_xbundle_remove(new_xcfg
, xbundle
);
847 xlate_ofport_set(struct ofproto_dpif
*ofproto
, struct ofbundle
*ofbundle
,
848 struct ofport_dpif
*ofport
, ofp_port_t ofp_port
,
849 odp_port_t odp_port
, const struct netdev
*netdev
,
850 const struct cfm
*cfm
, const struct bfd
*bfd
,
851 struct ofport_dpif
*peer
, int stp_port_no
,
852 const struct rstp_port
*rstp_port
,
853 const struct ofproto_port_queue
*qdscp_list
, size_t n_qdscp
,
854 enum ofputil_port_config config
,
855 enum ofputil_port_state state
, bool is_tunnel
,
861 ovs_assert(new_xcfg
);
863 xport
= xport_lookup(new_xcfg
, ofport
);
865 xport
= xzalloc(sizeof *xport
);
866 xport
->ofport
= ofport
;
867 xport
->xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
868 xport
->ofp_port
= ofp_port
;
870 xlate_xport_init(new_xcfg
, xport
);
873 ovs_assert(xport
->ofp_port
== ofp_port
);
875 xlate_xport_set(xport
, odp_port
, netdev
, cfm
, bfd
, stp_port_no
,
876 rstp_port
, config
, state
, is_tunnel
, may_enable
);
879 xport
->peer
->peer
= NULL
;
881 xport
->peer
= xport_lookup(new_xcfg
, peer
);
883 xport
->peer
->peer
= xport
;
886 if (xport
->xbundle
) {
887 list_remove(&xport
->bundle_node
);
889 xport
->xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
890 if (xport
->xbundle
) {
891 list_insert(&xport
->xbundle
->xports
, &xport
->bundle_node
);
894 clear_skb_priorities(xport
);
895 for (i
= 0; i
< n_qdscp
; i
++) {
896 struct skb_priority_to_dscp
*pdscp
;
897 uint32_t skb_priority
;
899 if (dpif_queue_to_priority(xport
->xbridge
->dpif
, qdscp_list
[i
].queue
,
904 pdscp
= xmalloc(sizeof *pdscp
);
905 pdscp
->skb_priority
= skb_priority
;
906 pdscp
->dscp
= (qdscp_list
[i
].dscp
<< 2) & IP_DSCP_MASK
;
907 hmap_insert(&xport
->skb_priorities
, &pdscp
->hmap_node
,
908 hash_int(pdscp
->skb_priority
, 0));
913 xlate_xport_remove(struct xlate_cfg
*xcfg
, struct xport
*xport
)
920 xport
->peer
->peer
= NULL
;
924 if (xport
->xbundle
) {
925 list_remove(&xport
->bundle_node
);
928 clear_skb_priorities(xport
);
929 hmap_destroy(&xport
->skb_priorities
);
931 hmap_remove(&xcfg
->xports
, &xport
->hmap_node
);
932 hmap_remove(&xport
->xbridge
->xports
, &xport
->ofp_node
);
934 netdev_close(xport
->netdev
);
935 rstp_port_unref(xport
->rstp_port
);
936 cfm_unref(xport
->cfm
);
937 bfd_unref(xport
->bfd
);
942 xlate_ofport_remove(struct ofport_dpif
*ofport
)
946 ovs_assert(new_xcfg
);
948 xport
= xport_lookup(new_xcfg
, ofport
);
949 xlate_xport_remove(new_xcfg
, xport
);
952 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
953 * returns the corresponding struct xport, or NULL if none is found. */
954 static struct xport
*
955 xlate_lookup_xport(const struct dpif_backer
*backer
, const struct flow
*flow
)
957 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
959 return xport_lookup(xcfg
, tnl_port_should_receive(flow
)
960 ? tnl_port_receive(flow
)
961 : odp_port_to_ofport(backer
, flow
->in_port
.odp_port
));
964 static struct ofproto_dpif
*
965 xlate_lookup_ofproto_(const struct dpif_backer
*backer
, const struct flow
*flow
,
966 ofp_port_t
*ofp_in_port
, const struct xport
**xportp
)
968 const struct xport
*xport
;
970 *xportp
= xport
= xlate_lookup_xport(backer
, flow
);
974 *ofp_in_port
= xport
->ofp_port
;
976 return xport
->xbridge
->ofproto
;
982 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
983 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
984 struct ofproto_dpif
*
985 xlate_lookup_ofproto(const struct dpif_backer
*backer
, const struct flow
*flow
,
986 ofp_port_t
*ofp_in_port
)
988 const struct xport
*xport
;
990 return xlate_lookup_ofproto_(backer
, flow
, ofp_in_port
, &xport
);
993 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
994 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
995 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
996 * handles for those protocols if they're enabled. Caller may use the returned
997 * pointers until quiescing, for longer term use additional references must
1000 * '*ofp_in_port' is set to OFPP_NONE if 'flow''s in_port does not exist.
1002 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport.
1005 xlate_lookup(const struct dpif_backer
*backer
, const struct flow
*flow
,
1006 struct ofproto_dpif
**ofprotop
, struct dpif_ipfix
**ipfix
,
1007 struct dpif_sflow
**sflow
, struct netflow
**netflow
,
1008 ofp_port_t
*ofp_in_port
)
1010 struct ofproto_dpif
*ofproto
;
1011 const struct xport
*xport
;
1013 ofproto
= xlate_lookup_ofproto_(backer
, flow
, ofp_in_port
, &xport
);
1015 if (ofp_in_port
&& !xport
) {
1016 *ofp_in_port
= OFPP_NONE
;
1024 *ofprotop
= ofproto
;
1028 *ipfix
= xport
->xbridge
->ipfix
;
1032 *sflow
= xport
->xbridge
->sflow
;
1036 *netflow
= xport
->xbridge
->netflow
;
1041 static struct xbridge
*
1042 xbridge_lookup(struct xlate_cfg
*xcfg
, const struct ofproto_dpif
*ofproto
)
1044 struct hmap
*xbridges
;
1045 struct xbridge
*xbridge
;
1047 if (!ofproto
|| !xcfg
) {
1051 xbridges
= &xcfg
->xbridges
;
1053 HMAP_FOR_EACH_IN_BUCKET (xbridge
, hmap_node
, hash_pointer(ofproto
, 0),
1055 if (xbridge
->ofproto
== ofproto
) {
1062 static struct xbundle
*
1063 xbundle_lookup(struct xlate_cfg
*xcfg
, const struct ofbundle
*ofbundle
)
1065 struct hmap
*xbundles
;
1066 struct xbundle
*xbundle
;
1068 if (!ofbundle
|| !xcfg
) {
1072 xbundles
= &xcfg
->xbundles
;
1074 HMAP_FOR_EACH_IN_BUCKET (xbundle
, hmap_node
, hash_pointer(ofbundle
, 0),
1076 if (xbundle
->ofbundle
== ofbundle
) {
1083 static struct xport
*
1084 xport_lookup(struct xlate_cfg
*xcfg
, const struct ofport_dpif
*ofport
)
1086 struct hmap
*xports
;
1087 struct xport
*xport
;
1089 if (!ofport
|| !xcfg
) {
1093 xports
= &xcfg
->xports
;
1095 HMAP_FOR_EACH_IN_BUCKET (xport
, hmap_node
, hash_pointer(ofport
, 0),
1097 if (xport
->ofport
== ofport
) {
1104 static struct stp_port
*
1105 xport_get_stp_port(const struct xport
*xport
)
1107 return xport
->xbridge
->stp
&& xport
->stp_port_no
!= -1
1108 ? stp_get_port(xport
->xbridge
->stp
, xport
->stp_port_no
)
1113 xport_stp_learn_state(const struct xport
*xport
)
1115 struct stp_port
*sp
= xport_get_stp_port(xport
);
1116 return stp_learn_in_state(sp
? stp_port_get_state(sp
) : STP_DISABLED
);
1120 xport_stp_forward_state(const struct xport
*xport
)
1122 struct stp_port
*sp
= xport_get_stp_port(xport
);
1123 return stp_forward_in_state(sp
? stp_port_get_state(sp
) : STP_DISABLED
);
1127 xport_stp_should_forward_bpdu(const struct xport
*xport
)
1129 struct stp_port
*sp
= xport_get_stp_port(xport
);
1130 return stp_should_forward_bpdu(sp
? stp_port_get_state(sp
) : STP_DISABLED
);
1133 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1134 * were used to make the determination.*/
1136 stp_should_process_flow(const struct flow
*flow
, struct flow_wildcards
*wc
)
1138 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1139 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
1140 return is_stp(flow
);
1144 stp_process_packet(const struct xport
*xport
, const struct ofpbuf
*packet
)
1146 struct stp_port
*sp
= xport_get_stp_port(xport
);
1147 struct ofpbuf payload
= *packet
;
1148 struct eth_header
*eth
= ofpbuf_data(&payload
);
1150 /* Sink packets on ports that have STP disabled when the bridge has
1152 if (!sp
|| stp_port_get_state(sp
) == STP_DISABLED
) {
1156 /* Trim off padding on payload. */
1157 if (ofpbuf_size(&payload
) > ntohs(eth
->eth_type
) + ETH_HEADER_LEN
) {
1158 ofpbuf_set_size(&payload
, ntohs(eth
->eth_type
) + ETH_HEADER_LEN
);
1161 if (ofpbuf_try_pull(&payload
, ETH_HEADER_LEN
+ LLC_HEADER_LEN
)) {
1162 stp_received_bpdu(sp
, ofpbuf_data(&payload
), ofpbuf_size(&payload
));
1166 static enum rstp_state
1167 xport_get_rstp_port_state(const struct xport
*xport
)
1169 return xport
->rstp_port
1170 ? rstp_port_get_state(xport
->rstp_port
)
1175 xport_rstp_learn_state(const struct xport
*xport
)
1177 return rstp_learn_in_state(xport_get_rstp_port_state(xport
));
1181 xport_rstp_forward_state(const struct xport
*xport
)
1183 return rstp_forward_in_state(xport_get_rstp_port_state(xport
));
1187 xport_rstp_should_manage_bpdu(const struct xport
*xport
)
1189 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport
));
1193 rstp_process_packet(const struct xport
*xport
, const struct ofpbuf
*packet
)
1195 struct ofpbuf payload
= *packet
;
1196 struct eth_header
*eth
= ofpbuf_data(&payload
);
1198 /* Sink packets on ports that have no RSTP. */
1199 if (!xport
->rstp_port
) {
1203 /* Trim off padding on payload. */
1204 if (ofpbuf_size(&payload
) > ntohs(eth
->eth_type
) + ETH_HEADER_LEN
) {
1205 ofpbuf_set_size(&payload
, ntohs(eth
->eth_type
) + ETH_HEADER_LEN
);
1208 if (ofpbuf_try_pull(&payload
, ETH_HEADER_LEN
+ LLC_HEADER_LEN
)) {
1209 rstp_port_received_bpdu(xport
->rstp_port
, ofpbuf_data(&payload
),
1210 ofpbuf_size(&payload
));
1214 static struct xport
*
1215 get_ofp_port(const struct xbridge
*xbridge
, ofp_port_t ofp_port
)
1217 struct xport
*xport
;
1219 HMAP_FOR_EACH_IN_BUCKET (xport
, ofp_node
, hash_ofp_port(ofp_port
),
1221 if (xport
->ofp_port
== ofp_port
) {
1229 ofp_port_to_odp_port(const struct xbridge
*xbridge
, ofp_port_t ofp_port
)
1231 const struct xport
*xport
= get_ofp_port(xbridge
, ofp_port
);
1232 return xport
? xport
->odp_port
: ODPP_NONE
;
1236 odp_port_is_alive(const struct xlate_ctx
*ctx
, ofp_port_t ofp_port
)
1238 struct xport
*xport
;
1240 xport
= get_ofp_port(ctx
->xbridge
, ofp_port
);
1241 if (!xport
|| xport
->config
& OFPUTIL_PC_PORT_DOWN
||
1242 xport
->state
& OFPUTIL_PS_LINK_DOWN
) {
1249 static struct ofputil_bucket
*
1250 group_first_live_bucket(const struct xlate_ctx
*, const struct group_dpif
*,
1254 group_is_alive(const struct xlate_ctx
*ctx
, uint32_t group_id
, int depth
)
1256 struct group_dpif
*group
;
1258 if (group_dpif_lookup(ctx
->xbridge
->ofproto
, group_id
, &group
)) {
1259 struct ofputil_bucket
*bucket
;
1261 bucket
= group_first_live_bucket(ctx
, group
, depth
);
1262 group_dpif_unref(group
);
1263 return bucket
== NULL
;
1269 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1272 bucket_is_alive(const struct xlate_ctx
*ctx
,
1273 struct ofputil_bucket
*bucket
, int depth
)
1275 if (depth
>= MAX_LIVENESS_RECURSION
) {
1276 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
1278 VLOG_WARN_RL(&rl
, "bucket chaining exceeded %d links",
1279 MAX_LIVENESS_RECURSION
);
1283 return (!ofputil_bucket_has_liveness(bucket
)
1284 || (bucket
->watch_port
!= OFPP_ANY
1285 && odp_port_is_alive(ctx
, bucket
->watch_port
))
1286 || (bucket
->watch_group
!= OFPG_ANY
1287 && group_is_alive(ctx
, bucket
->watch_group
, depth
+ 1)));
1290 static struct ofputil_bucket
*
1291 group_first_live_bucket(const struct xlate_ctx
*ctx
,
1292 const struct group_dpif
*group
, int depth
)
1294 struct ofputil_bucket
*bucket
;
1295 const struct list
*buckets
;
1297 group_dpif_get_buckets(group
, &buckets
);
1298 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
1299 if (bucket_is_alive(ctx
, bucket
, depth
)) {
1307 static struct ofputil_bucket
*
1308 group_best_live_bucket(const struct xlate_ctx
*ctx
,
1309 const struct group_dpif
*group
,
1312 struct ofputil_bucket
*best_bucket
= NULL
;
1313 uint32_t best_score
= 0;
1316 struct ofputil_bucket
*bucket
;
1317 const struct list
*buckets
;
1319 group_dpif_get_buckets(group
, &buckets
);
1320 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
1321 if (bucket_is_alive(ctx
, bucket
, 0)) {
1322 uint32_t score
= (hash_int(i
, basis
) & 0xffff) * bucket
->weight
;
1323 if (score
>= best_score
) {
1324 best_bucket
= bucket
;
1335 xbundle_trunks_vlan(const struct xbundle
*bundle
, uint16_t vlan
)
1337 return (bundle
->vlan_mode
!= PORT_VLAN_ACCESS
1338 && (!bundle
->trunks
|| bitmap_is_set(bundle
->trunks
, vlan
)));
1342 xbundle_includes_vlan(const struct xbundle
*xbundle
, uint16_t vlan
)
1344 return vlan
== xbundle
->vlan
|| xbundle_trunks_vlan(xbundle
, vlan
);
1347 static mirror_mask_t
1348 xbundle_mirror_out(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1350 return xbundle
!= &ofpp_none_bundle
1351 ? mirror_bundle_out(xbridge
->mbridge
, xbundle
->ofbundle
)
1355 static mirror_mask_t
1356 xbundle_mirror_src(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1358 return xbundle
!= &ofpp_none_bundle
1359 ? mirror_bundle_src(xbridge
->mbridge
, xbundle
->ofbundle
)
1363 static mirror_mask_t
1364 xbundle_mirror_dst(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1366 return xbundle
!= &ofpp_none_bundle
1367 ? mirror_bundle_dst(xbridge
->mbridge
, xbundle
->ofbundle
)
1371 static struct xbundle
*
1372 lookup_input_bundle(const struct xbridge
*xbridge
, ofp_port_t in_port
,
1373 bool warn
, struct xport
**in_xportp
)
1375 struct xport
*xport
;
1377 /* Find the port and bundle for the received packet. */
1378 xport
= get_ofp_port(xbridge
, in_port
);
1382 if (xport
&& xport
->xbundle
) {
1383 return xport
->xbundle
;
1386 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1387 * which a controller may use as the ingress port for traffic that
1388 * it is sourcing. */
1389 if (in_port
== OFPP_CONTROLLER
|| in_port
== OFPP_NONE
) {
1390 return &ofpp_none_bundle
;
1393 /* Odd. A few possible reasons here:
1395 * - We deleted a port but there are still a few packets queued up
1398 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1399 * we don't know about.
1401 * - The ofproto client didn't configure the port as part of a bundle.
1402 * This is particularly likely to happen if a packet was received on the
1403 * port after it was created, but before the client had a chance to
1404 * configure its bundle.
1407 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1409 VLOG_WARN_RL(&rl
, "bridge %s: received packet on unknown "
1410 "port %"PRIu16
, xbridge
->name
, in_port
);
1416 add_mirror_actions(struct xlate_ctx
*ctx
, const struct flow
*orig_flow
)
1418 const struct xbridge
*xbridge
= ctx
->xbridge
;
1419 mirror_mask_t mirrors
;
1420 struct xbundle
*in_xbundle
;
1424 mirrors
= ctx
->xout
->mirrors
;
1425 ctx
->xout
->mirrors
= 0;
1427 in_xbundle
= lookup_input_bundle(xbridge
, orig_flow
->in_port
.ofp_port
,
1428 ctx
->xin
->packet
!= NULL
, NULL
);
1432 mirrors
|= xbundle_mirror_src(xbridge
, in_xbundle
);
1434 /* Drop frames on bundles reserved for mirroring. */
1435 if (xbundle_mirror_out(xbridge
, in_xbundle
)) {
1436 if (ctx
->xin
->packet
!= NULL
) {
1437 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1438 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet received on port "
1439 "%s, which is reserved exclusively for mirroring",
1440 ctx
->xbridge
->name
, in_xbundle
->name
);
1442 ofpbuf_clear(ctx
->xout
->odp_actions
);
1447 vid
= vlan_tci_to_vid(orig_flow
->vlan_tci
);
1448 if (!input_vid_is_valid(vid
, in_xbundle
, ctx
->xin
->packet
!= NULL
)) {
1451 vlan
= input_vid_to_vlan(in_xbundle
, vid
);
1457 /* Restore the original packet before adding the mirror actions. */
1458 ctx
->xin
->flow
= *orig_flow
;
1461 mirror_mask_t dup_mirrors
;
1462 struct ofbundle
*out
;
1463 unsigned long *vlans
;
1468 has_mirror
= mirror_get(xbridge
->mbridge
, raw_ctz(mirrors
),
1469 &vlans
, &dup_mirrors
, &out
, &out_vlan
);
1470 ovs_assert(has_mirror
);
1473 ctx
->xout
->wc
.masks
.vlan_tci
|= htons(VLAN_CFI
| VLAN_VID_MASK
);
1475 vlan_mirrored
= !vlans
|| bitmap_is_set(vlans
, vlan
);
1478 if (!vlan_mirrored
) {
1479 mirrors
= zero_rightmost_1bit(mirrors
);
1483 mirrors
&= ~dup_mirrors
;
1484 ctx
->xout
->mirrors
|= dup_mirrors
;
1486 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1487 struct xbundle
*out_xbundle
= xbundle_lookup(xcfg
, out
);
1489 output_normal(ctx
, out_xbundle
, vlan
);
1491 } else if (vlan
!= out_vlan
1492 && !eth_addr_is_reserved(orig_flow
->dl_dst
)) {
1493 struct xbundle
*xbundle
;
1495 LIST_FOR_EACH (xbundle
, list_node
, &xbridge
->xbundles
) {
1496 if (xbundle_includes_vlan(xbundle
, out_vlan
)
1497 && !xbundle_mirror_out(xbridge
, xbundle
)) {
1498 output_normal(ctx
, xbundle
, out_vlan
);
1505 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1506 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1507 * the bundle on which the packet was received, returns the VLAN to which the
1510 * Both 'vid' and the return value are in the range 0...4095. */
1512 input_vid_to_vlan(const struct xbundle
*in_xbundle
, uint16_t vid
)
1514 switch (in_xbundle
->vlan_mode
) {
1515 case PORT_VLAN_ACCESS
:
1516 return in_xbundle
->vlan
;
1519 case PORT_VLAN_TRUNK
:
1522 case PORT_VLAN_NATIVE_UNTAGGED
:
1523 case PORT_VLAN_NATIVE_TAGGED
:
1524 return vid
? vid
: in_xbundle
->vlan
;
1531 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1532 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1535 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1536 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1539 input_vid_is_valid(uint16_t vid
, struct xbundle
*in_xbundle
, bool warn
)
1541 /* Allow any VID on the OFPP_NONE port. */
1542 if (in_xbundle
== &ofpp_none_bundle
) {
1546 switch (in_xbundle
->vlan_mode
) {
1547 case PORT_VLAN_ACCESS
:
1550 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1551 VLOG_WARN_RL(&rl
, "dropping VLAN %"PRIu16
" tagged "
1552 "packet received on port %s configured as VLAN "
1553 "%"PRIu16
" access port", vid
, in_xbundle
->name
,
1560 case PORT_VLAN_NATIVE_UNTAGGED
:
1561 case PORT_VLAN_NATIVE_TAGGED
:
1563 /* Port must always carry its native VLAN. */
1567 case PORT_VLAN_TRUNK
:
1568 if (!xbundle_includes_vlan(in_xbundle
, vid
)) {
1570 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1571 VLOG_WARN_RL(&rl
, "dropping VLAN %"PRIu16
" packet "
1572 "received on port %s not configured for trunking "
1573 "VLAN %"PRIu16
, vid
, in_xbundle
->name
, vid
);
1585 /* Given 'vlan', the VLAN that a packet belongs to, and
1586 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1587 * that should be included in the 802.1Q header. (If the return value is 0,
1588 * then the 802.1Q header should only be included in the packet if there is a
1591 * Both 'vlan' and the return value are in the range 0...4095. */
1593 output_vlan_to_vid(const struct xbundle
*out_xbundle
, uint16_t vlan
)
1595 switch (out_xbundle
->vlan_mode
) {
1596 case PORT_VLAN_ACCESS
:
1599 case PORT_VLAN_TRUNK
:
1600 case PORT_VLAN_NATIVE_TAGGED
:
1603 case PORT_VLAN_NATIVE_UNTAGGED
:
1604 return vlan
== out_xbundle
->vlan
? 0 : vlan
;
1612 output_normal(struct xlate_ctx
*ctx
, const struct xbundle
*out_xbundle
,
1615 ovs_be16
*flow_tci
= &ctx
->xin
->flow
.vlan_tci
;
1617 ovs_be16 tci
, old_tci
;
1618 struct xport
*xport
;
1620 vid
= output_vlan_to_vid(out_xbundle
, vlan
);
1621 if (list_is_empty(&out_xbundle
->xports
)) {
1622 /* Partially configured bundle with no slaves. Drop the packet. */
1624 } else if (!out_xbundle
->bond
) {
1625 ctx
->use_recirc
= false;
1626 xport
= CONTAINER_OF(list_front(&out_xbundle
->xports
), struct xport
,
1629 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1630 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
1631 struct xlate_recirc
*xr
= &ctx
->recirc
;
1632 struct ofport_dpif
*ofport
;
1634 if (ctx
->xbridge
->enable_recirc
) {
1635 ctx
->use_recirc
= bond_may_recirc(
1636 out_xbundle
->bond
, &xr
->recirc_id
, &xr
->hash_basis
);
1638 if (ctx
->use_recirc
) {
1639 /* Only TCP mode uses recirculation. */
1640 xr
->hash_alg
= OVS_HASH_ALG_L4
;
1641 bond_update_post_recirc_rules(out_xbundle
->bond
, false);
1643 /* Recirculation does not require unmasking hash fields. */
1648 ofport
= bond_choose_output_slave(out_xbundle
->bond
,
1649 &ctx
->xin
->flow
, wc
, vid
);
1650 xport
= xport_lookup(xcfg
, ofport
);
1653 /* No slaves enabled, so drop packet. */
1657 /* If ctx->xout->use_recirc is set, the main thread will handle stats
1658 * accounting for this bond. */
1659 if (!ctx
->use_recirc
) {
1660 if (ctx
->xin
->resubmit_stats
) {
1661 bond_account(out_xbundle
->bond
, &ctx
->xin
->flow
, vid
,
1662 ctx
->xin
->resubmit_stats
->n_bytes
);
1664 if (ctx
->xin
->xcache
) {
1665 struct xc_entry
*entry
;
1668 flow
= &ctx
->xin
->flow
;
1669 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_BOND
);
1670 entry
->u
.bond
.bond
= bond_ref(out_xbundle
->bond
);
1671 entry
->u
.bond
.flow
= xmemdup(flow
, sizeof *flow
);
1672 entry
->u
.bond
.vid
= vid
;
1677 old_tci
= *flow_tci
;
1679 if (tci
|| out_xbundle
->use_priority_tags
) {
1680 tci
|= *flow_tci
& htons(VLAN_PCP_MASK
);
1682 tci
|= htons(VLAN_CFI
);
1687 compose_output_action(ctx
, xport
->ofp_port
);
1688 *flow_tci
= old_tci
;
1691 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1692 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1693 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1695 is_gratuitous_arp(const struct flow
*flow
, struct flow_wildcards
*wc
)
1697 if (flow
->dl_type
!= htons(ETH_TYPE_ARP
)) {
1701 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
1702 if (!eth_addr_is_broadcast(flow
->dl_dst
)) {
1706 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
1707 if (flow
->nw_proto
== ARP_OP_REPLY
) {
1709 } else if (flow
->nw_proto
== ARP_OP_REQUEST
) {
1710 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
1711 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
1713 return flow
->nw_src
== flow
->nw_dst
;
1719 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1720 * dropped. Returns true if they may be forwarded, false if they should be
1723 * 'in_port' must be the xport that corresponds to flow->in_port.
1724 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1726 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1727 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1728 * checked by input_vid_is_valid().
1730 * May also add tags to '*tags', although the current implementation only does
1731 * so in one special case.
1734 is_admissible(struct xlate_ctx
*ctx
, struct xport
*in_port
,
1737 struct xbundle
*in_xbundle
= in_port
->xbundle
;
1738 const struct xbridge
*xbridge
= ctx
->xbridge
;
1739 struct flow
*flow
= &ctx
->xin
->flow
;
1741 /* Drop frames for reserved multicast addresses
1742 * only if forward_bpdu option is absent. */
1743 if (!xbridge
->forward_bpdu
&& eth_addr_is_reserved(flow
->dl_dst
)) {
1744 xlate_report(ctx
, "packet has reserved destination MAC, dropping");
1748 if (in_xbundle
->bond
) {
1749 struct mac_entry
*mac
;
1751 switch (bond_check_admissibility(in_xbundle
->bond
, in_port
->ofport
,
1757 xlate_report(ctx
, "bonding refused admissibility, dropping");
1760 case BV_DROP_IF_MOVED
:
1761 ovs_rwlock_rdlock(&xbridge
->ml
->rwlock
);
1762 mac
= mac_learning_lookup(xbridge
->ml
, flow
->dl_src
, vlan
);
1763 if (mac
&& mac
->port
.p
!= in_xbundle
->ofbundle
&&
1764 (!is_gratuitous_arp(flow
, &ctx
->xout
->wc
)
1765 || mac_entry_is_grat_arp_locked(mac
))) {
1766 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
1767 xlate_report(ctx
, "SLB bond thinks this packet looped back, "
1771 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
1779 /* Checks whether a MAC learning update is necessary for MAC learning table
1780 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1783 * Most packets processed through the MAC learning table do not actually
1784 * change it in any way. This function requires only a read lock on the MAC
1785 * learning table, so it is much cheaper in this common case.
1787 * Keep the code here synchronized with that in update_learning_table__()
1790 is_mac_learning_update_needed(const struct mac_learning
*ml
,
1791 const struct flow
*flow
,
1792 struct flow_wildcards
*wc
,
1793 int vlan
, struct xbundle
*in_xbundle
)
1794 OVS_REQ_RDLOCK(ml
->rwlock
)
1796 struct mac_entry
*mac
;
1798 if (!mac_learning_may_learn(ml
, flow
->dl_src
, vlan
)) {
1802 mac
= mac_learning_lookup(ml
, flow
->dl_src
, vlan
);
1803 if (!mac
|| mac_entry_age(ml
, mac
)) {
1807 if (is_gratuitous_arp(flow
, wc
)) {
1808 /* We don't want to learn from gratuitous ARP packets that are
1809 * reflected back over bond slaves so we lock the learning table. */
1810 if (!in_xbundle
->bond
) {
1812 } else if (mac_entry_is_grat_arp_locked(mac
)) {
1817 return mac
->port
.p
!= in_xbundle
->ofbundle
;
1821 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1822 * received on 'in_xbundle' in 'vlan'.
1824 * This code repeats all the checks in is_mac_learning_update_needed() because
1825 * the lock was released between there and here and thus the MAC learning state
1826 * could have changed.
1828 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1831 update_learning_table__(const struct xbridge
*xbridge
,
1832 const struct flow
*flow
, struct flow_wildcards
*wc
,
1833 int vlan
, struct xbundle
*in_xbundle
)
1834 OVS_REQ_WRLOCK(xbridge
->ml
->rwlock
)
1836 struct mac_entry
*mac
;
1838 if (!mac_learning_may_learn(xbridge
->ml
, flow
->dl_src
, vlan
)) {
1842 mac
= mac_learning_insert(xbridge
->ml
, flow
->dl_src
, vlan
);
1843 if (is_gratuitous_arp(flow
, wc
)) {
1844 /* We don't want to learn from gratuitous ARP packets that are
1845 * reflected back over bond slaves so we lock the learning table. */
1846 if (!in_xbundle
->bond
) {
1847 mac_entry_set_grat_arp_lock(mac
);
1848 } else if (mac_entry_is_grat_arp_locked(mac
)) {
1853 if (mac
->port
.p
!= in_xbundle
->ofbundle
) {
1854 /* The log messages here could actually be useful in debugging,
1855 * so keep the rate limit relatively high. */
1856 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(30, 300);
1858 VLOG_DBG_RL(&rl
, "bridge %s: learned that "ETH_ADDR_FMT
" is "
1859 "on port %s in VLAN %d",
1860 xbridge
->name
, ETH_ADDR_ARGS(flow
->dl_src
),
1861 in_xbundle
->name
, vlan
);
1863 mac
->port
.p
= in_xbundle
->ofbundle
;
1864 mac_learning_changed(xbridge
->ml
);
1869 update_learning_table(const struct xbridge
*xbridge
,
1870 const struct flow
*flow
, struct flow_wildcards
*wc
,
1871 int vlan
, struct xbundle
*in_xbundle
)
1875 /* Don't learn the OFPP_NONE port. */
1876 if (in_xbundle
== &ofpp_none_bundle
) {
1880 /* First try the common case: no change to MAC learning table. */
1881 ovs_rwlock_rdlock(&xbridge
->ml
->rwlock
);
1882 need_update
= is_mac_learning_update_needed(xbridge
->ml
, flow
, wc
, vlan
,
1884 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
1887 /* Slow path: MAC learning table might need an update. */
1888 ovs_rwlock_wrlock(&xbridge
->ml
->rwlock
);
1889 update_learning_table__(xbridge
, flow
, wc
, vlan
, in_xbundle
);
1890 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
1894 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1895 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
1897 update_mcast_snooping_table__(const struct xbridge
*xbridge
,
1898 const struct flow
*flow
,
1899 struct mcast_snooping
*ms
,
1900 ovs_be32 ip4
, int vlan
,
1901 struct xbundle
*in_xbundle
)
1902 OVS_REQ_WRLOCK(ms
->rwlock
)
1904 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(60, 30);
1906 switch (ntohs(flow
->tp_src
)) {
1907 case IGMP_HOST_MEMBERSHIP_REPORT
:
1908 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1909 if (mcast_snooping_add_group(ms
, ip4
, vlan
, in_xbundle
->ofbundle
)) {
1910 VLOG_DBG_RL(&rl
, "bridge %s: multicast snooping learned that "
1911 IP_FMT
" is on port %s in VLAN %d",
1912 xbridge
->name
, IP_ARGS(ip4
), in_xbundle
->name
, vlan
);
1915 case IGMP_HOST_LEAVE_MESSAGE
:
1916 if (mcast_snooping_leave_group(ms
, ip4
, vlan
, in_xbundle
->ofbundle
)) {
1917 VLOG_DBG_RL(&rl
, "bridge %s: multicast snooping leaving "
1918 IP_FMT
" is on port %s in VLAN %d",
1919 xbridge
->name
, IP_ARGS(ip4
), in_xbundle
->name
, vlan
);
1922 case IGMP_HOST_MEMBERSHIP_QUERY
:
1923 if (flow
->nw_src
&& mcast_snooping_add_mrouter(ms
, vlan
,
1924 in_xbundle
->ofbundle
)) {
1925 VLOG_DBG_RL(&rl
, "bridge %s: multicast snooping query from "
1926 IP_FMT
" is on port %s in VLAN %d",
1927 xbridge
->name
, IP_ARGS(flow
->nw_src
),
1928 in_xbundle
->name
, vlan
);
1934 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1935 * was received on 'in_xbundle' in 'vlan'. */
1937 update_mcast_snooping_table(const struct xbridge
*xbridge
,
1938 const struct flow
*flow
, int vlan
,
1939 struct xbundle
*in_xbundle
)
1941 struct mcast_snooping
*ms
= xbridge
->ms
;
1942 struct xlate_cfg
*xcfg
;
1943 struct xbundle
*mcast_xbundle
;
1944 struct mcast_fport_bundle
*fport
;
1946 /* Don't learn the OFPP_NONE port. */
1947 if (in_xbundle
== &ofpp_none_bundle
) {
1951 /* Don't learn from flood ports */
1952 mcast_xbundle
= NULL
;
1953 ovs_rwlock_wrlock(&ms
->rwlock
);
1954 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1955 LIST_FOR_EACH(fport
, fport_node
, &ms
->fport_list
) {
1956 mcast_xbundle
= xbundle_lookup(xcfg
, fport
->port
);
1957 if (mcast_xbundle
== in_xbundle
) {
1962 if (!mcast_xbundle
|| mcast_xbundle
!= in_xbundle
) {
1963 update_mcast_snooping_table__(xbridge
, flow
, ms
, flow
->igmp_group_ip4
,
1966 ovs_rwlock_unlock(&ms
->rwlock
);
1969 /* send the packet to ports having the multicast group learned */
1971 xlate_normal_mcast_send_group(struct xlate_ctx
*ctx
,
1972 struct mcast_snooping
*ms OVS_UNUSED
,
1973 struct mcast_group
*grp
,
1974 struct xbundle
*in_xbundle
, uint16_t vlan
)
1975 OVS_REQ_RDLOCK(ms
->rwlock
)
1977 struct xlate_cfg
*xcfg
;
1978 struct mcast_group_bundle
*b
;
1979 struct xbundle
*mcast_xbundle
;
1981 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1982 LIST_FOR_EACH(b
, bundle_node
, &grp
->bundle_lru
) {
1983 mcast_xbundle
= xbundle_lookup(xcfg
, b
->port
);
1984 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
) {
1985 xlate_report(ctx
, "forwarding to mcast group port");
1986 output_normal(ctx
, mcast_xbundle
, vlan
);
1987 } else if (!mcast_xbundle
) {
1988 xlate_report(ctx
, "mcast group port is unknown, dropping");
1990 xlate_report(ctx
, "mcast group port is input port, dropping");
1995 /* send the packet to ports connected to multicast routers */
1997 xlate_normal_mcast_send_mrouters(struct xlate_ctx
*ctx
,
1998 struct mcast_snooping
*ms
,
1999 struct xbundle
*in_xbundle
, uint16_t vlan
)
2000 OVS_REQ_RDLOCK(ms
->rwlock
)
2002 struct xlate_cfg
*xcfg
;
2003 struct mcast_mrouter_bundle
*mrouter
;
2004 struct xbundle
*mcast_xbundle
;
2006 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2007 LIST_FOR_EACH(mrouter
, mrouter_node
, &ms
->mrouter_lru
) {
2008 mcast_xbundle
= xbundle_lookup(xcfg
, mrouter
->port
);
2009 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
) {
2010 xlate_report(ctx
, "forwarding to mcast router port");
2011 output_normal(ctx
, mcast_xbundle
, vlan
);
2012 } else if (!mcast_xbundle
) {
2013 xlate_report(ctx
, "mcast router port is unknown, dropping");
2015 xlate_report(ctx
, "mcast router port is input port, dropping");
2020 /* send the packet to ports flagged to be flooded */
2022 xlate_normal_mcast_send_fports(struct xlate_ctx
*ctx
,
2023 struct mcast_snooping
*ms
,
2024 struct xbundle
*in_xbundle
, uint16_t vlan
)
2025 OVS_REQ_RDLOCK(ms
->rwlock
)
2027 struct xlate_cfg
*xcfg
;
2028 struct mcast_fport_bundle
*fport
;
2029 struct xbundle
*mcast_xbundle
;
2031 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2032 LIST_FOR_EACH(fport
, fport_node
, &ms
->fport_list
) {
2033 mcast_xbundle
= xbundle_lookup(xcfg
, fport
->port
);
2034 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
) {
2035 xlate_report(ctx
, "forwarding to mcast flood port");
2036 output_normal(ctx
, mcast_xbundle
, vlan
);
2037 } else if (!mcast_xbundle
) {
2038 xlate_report(ctx
, "mcast flood port is unknown, dropping");
2040 xlate_report(ctx
, "mcast flood port is input port, dropping");
2046 xlate_normal_flood(struct xlate_ctx
*ctx
, struct xbundle
*in_xbundle
,
2049 struct xbundle
*xbundle
;
2051 LIST_FOR_EACH (xbundle
, list_node
, &ctx
->xbridge
->xbundles
) {
2052 if (xbundle
!= in_xbundle
2053 && xbundle_includes_vlan(xbundle
, vlan
)
2054 && xbundle
->floodable
2055 && !xbundle_mirror_out(ctx
->xbridge
, xbundle
)) {
2056 output_normal(ctx
, xbundle
, vlan
);
2059 ctx
->xout
->nf_output_iface
= NF_OUT_FLOOD
;
2063 xlate_normal(struct xlate_ctx
*ctx
)
2065 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
2066 struct flow
*flow
= &ctx
->xin
->flow
;
2067 struct xbundle
*in_xbundle
;
2068 struct xport
*in_port
;
2069 struct mac_entry
*mac
;
2074 ctx
->xout
->has_normal
= true;
2076 memset(&wc
->masks
.dl_src
, 0xff, sizeof wc
->masks
.dl_src
);
2077 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
2078 wc
->masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
2080 in_xbundle
= lookup_input_bundle(ctx
->xbridge
, flow
->in_port
.ofp_port
,
2081 ctx
->xin
->packet
!= NULL
, &in_port
);
2083 xlate_report(ctx
, "no input bundle, dropping");
2087 /* Drop malformed frames. */
2088 if (flow
->dl_type
== htons(ETH_TYPE_VLAN
) &&
2089 !(flow
->vlan_tci
& htons(VLAN_CFI
))) {
2090 if (ctx
->xin
->packet
!= NULL
) {
2091 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2092 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet with partial "
2093 "VLAN tag received on port %s",
2094 ctx
->xbridge
->name
, in_xbundle
->name
);
2096 xlate_report(ctx
, "partial VLAN tag, dropping");
2100 /* Drop frames on bundles reserved for mirroring. */
2101 if (xbundle_mirror_out(ctx
->xbridge
, in_xbundle
)) {
2102 if (ctx
->xin
->packet
!= NULL
) {
2103 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2104 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet received on port "
2105 "%s, which is reserved exclusively for mirroring",
2106 ctx
->xbridge
->name
, in_xbundle
->name
);
2108 xlate_report(ctx
, "input port is mirror output port, dropping");
2113 vid
= vlan_tci_to_vid(flow
->vlan_tci
);
2114 if (!input_vid_is_valid(vid
, in_xbundle
, ctx
->xin
->packet
!= NULL
)) {
2115 xlate_report(ctx
, "disallowed VLAN VID for this input port, dropping");
2118 vlan
= input_vid_to_vlan(in_xbundle
, vid
);
2120 /* Check other admissibility requirements. */
2121 if (in_port
&& !is_admissible(ctx
, in_port
, vlan
)) {
2125 /* Learn source MAC. */
2126 if (ctx
->xin
->may_learn
) {
2127 update_learning_table(ctx
->xbridge
, flow
, wc
, vlan
, in_xbundle
);
2129 if (ctx
->xin
->xcache
) {
2130 struct xc_entry
*entry
;
2132 /* Save enough info to update mac learning table later. */
2133 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NORMAL
);
2134 entry
->u
.normal
.ofproto
= ctx
->xbridge
->ofproto
;
2135 entry
->u
.normal
.flow
= xmemdup(flow
, sizeof *flow
);
2136 entry
->u
.normal
.vlan
= vlan
;
2139 /* Determine output bundle. */
2140 if (mcast_snooping_enabled(ctx
->xbridge
->ms
)
2141 && !eth_addr_is_broadcast(flow
->dl_dst
)
2142 && eth_addr_is_multicast(flow
->dl_dst
)
2143 && flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2144 struct mcast_snooping
*ms
= ctx
->xbridge
->ms
;
2145 struct mcast_group
*grp
;
2147 if (flow
->nw_proto
== IPPROTO_IGMP
) {
2148 if (ctx
->xin
->may_learn
) {
2149 if (mcast_snooping_is_membership(flow
->tp_src
) ||
2150 mcast_snooping_is_query(flow
->tp_src
)) {
2151 update_mcast_snooping_table(ctx
->xbridge
, flow
, vlan
,
2156 if (mcast_snooping_is_membership(flow
->tp_src
)) {
2157 ovs_rwlock_rdlock(&ms
->rwlock
);
2158 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, vlan
);
2159 ovs_rwlock_unlock(&ms
->rwlock
);
2161 xlate_report(ctx
, "multicast traffic, flooding");
2162 xlate_normal_flood(ctx
, in_xbundle
, vlan
);
2166 if (ip_is_local_multicast(flow
->nw_dst
)) {
2167 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2168 * address in the 224.0.0.x range which are not IGMP must
2169 * be forwarded on all ports */
2170 xlate_report(ctx
, "RFC4541: section 2.1.2, item 2, flooding");
2171 xlate_normal_flood(ctx
, in_xbundle
, vlan
);
2176 /* forwarding to group base ports */
2177 ovs_rwlock_rdlock(&ms
->rwlock
);
2178 grp
= mcast_snooping_lookup(ms
, flow
->nw_dst
, vlan
);
2180 xlate_normal_mcast_send_group(ctx
, ms
, grp
, in_xbundle
, vlan
);
2181 xlate_normal_mcast_send_fports(ctx
, ms
, in_xbundle
, vlan
);
2182 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, vlan
);
2184 if (mcast_snooping_flood_unreg(ms
)) {
2185 xlate_report(ctx
, "unregistered multicast, flooding");
2186 xlate_normal_flood(ctx
, in_xbundle
, vlan
);
2188 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, vlan
);
2189 xlate_normal_mcast_send_fports(ctx
, ms
, in_xbundle
, vlan
);
2192 ovs_rwlock_unlock(&ms
->rwlock
);
2194 ovs_rwlock_rdlock(&ctx
->xbridge
->ml
->rwlock
);
2195 mac
= mac_learning_lookup(ctx
->xbridge
->ml
, flow
->dl_dst
, vlan
);
2196 mac_port
= mac
? mac
->port
.p
: NULL
;
2197 ovs_rwlock_unlock(&ctx
->xbridge
->ml
->rwlock
);
2200 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2201 struct xbundle
*mac_xbundle
= xbundle_lookup(xcfg
, mac_port
);
2202 if (mac_xbundle
&& mac_xbundle
!= in_xbundle
) {
2203 xlate_report(ctx
, "forwarding to learned port");
2204 output_normal(ctx
, mac_xbundle
, vlan
);
2205 } else if (!mac_xbundle
) {
2206 xlate_report(ctx
, "learned port is unknown, dropping");
2208 xlate_report(ctx
, "learned port is input port, dropping");
2211 xlate_report(ctx
, "no learned MAC for destination, flooding");
2212 xlate_normal_flood(ctx
, in_xbundle
, vlan
);
2217 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
2218 * the number of packets out of UINT32_MAX to sample. The given
2219 * cookie is passed back in the callback for each sampled packet.
2222 compose_sample_action(const struct xbridge
*xbridge
,
2223 struct ofpbuf
*odp_actions
,
2224 const struct flow
*flow
,
2225 const uint32_t probability
,
2226 const union user_action_cookie
*cookie
,
2227 const size_t cookie_size
,
2228 const odp_port_t tunnel_out_port
)
2230 size_t sample_offset
, actions_offset
;
2231 odp_port_t odp_port
;
2235 sample_offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SAMPLE
);
2237 nl_msg_put_u32(odp_actions
, OVS_SAMPLE_ATTR_PROBABILITY
, probability
);
2239 actions_offset
= nl_msg_start_nested(odp_actions
, OVS_SAMPLE_ATTR_ACTIONS
);
2241 odp_port
= ofp_port_to_odp_port(xbridge
, flow
->in_port
.ofp_port
);
2242 pid
= dpif_port_get_pid(xbridge
->dpif
, odp_port
,
2243 flow_hash_5tuple(flow
, 0));
2244 cookie_offset
= odp_put_userspace_action(pid
, cookie
, cookie_size
,
2245 tunnel_out_port
, odp_actions
);
2247 nl_msg_end_nested(odp_actions
, actions_offset
);
2248 nl_msg_end_nested(odp_actions
, sample_offset
);
2249 return cookie_offset
;
2253 compose_sflow_cookie(const struct xbridge
*xbridge
, ovs_be16 vlan_tci
,
2254 odp_port_t odp_port
, unsigned int n_outputs
,
2255 union user_action_cookie
*cookie
)
2259 cookie
->type
= USER_ACTION_COOKIE_SFLOW
;
2260 cookie
->sflow
.vlan_tci
= vlan_tci
;
2262 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2263 * port information") for the interpretation of cookie->output. */
2264 switch (n_outputs
) {
2266 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2267 cookie
->sflow
.output
= 0x40000000 | 256;
2271 ifindex
= dpif_sflow_odp_port_to_ifindex(xbridge
->sflow
, odp_port
);
2273 cookie
->sflow
.output
= ifindex
;
2278 /* 0x80000000 means "multiple output ports. */
2279 cookie
->sflow
.output
= 0x80000000 | n_outputs
;
2284 /* Compose SAMPLE action for sFlow bridge sampling. */
2286 compose_sflow_action(const struct xbridge
*xbridge
,
2287 struct ofpbuf
*odp_actions
,
2288 const struct flow
*flow
,
2289 odp_port_t odp_port
)
2291 uint32_t probability
;
2292 union user_action_cookie cookie
;
2294 if (!xbridge
->sflow
|| flow
->in_port
.ofp_port
== OFPP_NONE
) {
2298 probability
= dpif_sflow_get_probability(xbridge
->sflow
);
2299 compose_sflow_cookie(xbridge
, htons(0), odp_port
,
2300 odp_port
== ODPP_NONE
? 0 : 1, &cookie
);
2302 return compose_sample_action(xbridge
, odp_actions
, flow
, probability
,
2303 &cookie
, sizeof cookie
.sflow
, ODPP_NONE
);
2307 compose_flow_sample_cookie(uint16_t probability
, uint32_t collector_set_id
,
2308 uint32_t obs_domain_id
, uint32_t obs_point_id
,
2309 union user_action_cookie
*cookie
)
2311 cookie
->type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
2312 cookie
->flow_sample
.probability
= probability
;
2313 cookie
->flow_sample
.collector_set_id
= collector_set_id
;
2314 cookie
->flow_sample
.obs_domain_id
= obs_domain_id
;
2315 cookie
->flow_sample
.obs_point_id
= obs_point_id
;
2319 compose_ipfix_cookie(union user_action_cookie
*cookie
,
2320 odp_port_t output_odp_port
)
2322 cookie
->type
= USER_ACTION_COOKIE_IPFIX
;
2323 cookie
->ipfix
.output_odp_port
= output_odp_port
;
2326 /* Compose SAMPLE action for IPFIX bridge sampling. */
2328 compose_ipfix_action(const struct xbridge
*xbridge
,
2329 struct ofpbuf
*odp_actions
,
2330 const struct flow
*flow
,
2331 odp_port_t output_odp_port
)
2333 uint32_t probability
;
2334 union user_action_cookie cookie
;
2335 odp_port_t tunnel_out_port
= ODPP_NONE
;
2337 if (!xbridge
->ipfix
|| flow
->in_port
.ofp_port
== OFPP_NONE
) {
2341 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2343 if (output_odp_port
== ODPP_NONE
&&
2344 !dpif_ipfix_get_bridge_exporter_input_sampling(xbridge
->ipfix
)) {
2348 /* For output case, output_odp_port is valid*/
2349 if (output_odp_port
!= ODPP_NONE
) {
2350 if (!dpif_ipfix_get_bridge_exporter_output_sampling(xbridge
->ipfix
)) {
2353 /* If tunnel sampling is enabled, put an additional option attribute:
2354 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2356 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(xbridge
->ipfix
) &&
2357 dpif_ipfix_get_tunnel_port(xbridge
->ipfix
, output_odp_port
) ) {
2358 tunnel_out_port
= output_odp_port
;
2362 probability
= dpif_ipfix_get_bridge_exporter_probability(xbridge
->ipfix
);
2363 compose_ipfix_cookie(&cookie
, output_odp_port
);
2365 compose_sample_action(xbridge
, odp_actions
, flow
, probability
,
2366 &cookie
, sizeof cookie
.ipfix
, tunnel_out_port
);
2369 /* SAMPLE action for sFlow must be first action in any given list of
2370 * actions. At this point we do not have all information required to
2371 * build it. So try to build sample action as complete as possible. */
2373 add_sflow_action(struct xlate_ctx
*ctx
)
2375 ctx
->user_cookie_offset
= compose_sflow_action(ctx
->xbridge
,
2376 ctx
->xout
->odp_actions
,
2377 &ctx
->xin
->flow
, ODPP_NONE
);
2378 ctx
->sflow_odp_port
= 0;
2379 ctx
->sflow_n_outputs
= 0;
2382 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
2383 * of actions, eventually after the SAMPLE action for sFlow. */
2385 add_ipfix_action(struct xlate_ctx
*ctx
)
2387 compose_ipfix_action(ctx
->xbridge
, ctx
->xout
->odp_actions
,
2388 &ctx
->xin
->flow
, ODPP_NONE
);
2392 add_ipfix_output_action(struct xlate_ctx
*ctx
, odp_port_t port
)
2394 compose_ipfix_action(ctx
->xbridge
, ctx
->xout
->odp_actions
,
2395 &ctx
->xin
->flow
, port
);
2398 /* Fix SAMPLE action according to data collected while composing ODP actions.
2399 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
2400 * USERSPACE action's user-cookie which is required for sflow. */
2402 fix_sflow_action(struct xlate_ctx
*ctx
)
2404 const struct flow
*base
= &ctx
->base_flow
;
2405 union user_action_cookie
*cookie
;
2407 if (!ctx
->user_cookie_offset
) {
2411 cookie
= ofpbuf_at(ctx
->xout
->odp_actions
, ctx
->user_cookie_offset
,
2412 sizeof cookie
->sflow
);
2413 ovs_assert(cookie
->type
== USER_ACTION_COOKIE_SFLOW
);
2415 compose_sflow_cookie(ctx
->xbridge
, base
->vlan_tci
,
2416 ctx
->sflow_odp_port
, ctx
->sflow_n_outputs
, cookie
);
2419 static enum slow_path_reason
2420 process_special(struct xlate_ctx
*ctx
, const struct flow
*flow
,
2421 const struct xport
*xport
, const struct ofpbuf
*packet
)
2423 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
2424 const struct xbridge
*xbridge
= ctx
->xbridge
;
2428 } else if (xport
->cfm
&& cfm_should_process_flow(xport
->cfm
, flow
, wc
)) {
2430 cfm_process_heartbeat(xport
->cfm
, packet
);
2433 } else if (xport
->bfd
&& bfd_should_process_flow(xport
->bfd
, flow
, wc
)) {
2435 bfd_process_packet(xport
->bfd
, flow
, packet
);
2436 /* If POLL received, immediately sends FINAL back. */
2437 if (bfd_should_send_packet(xport
->bfd
)) {
2438 ofproto_dpif_monitor_port_send_soon(xport
->ofport
);
2442 } else if (xport
->xbundle
&& xport
->xbundle
->lacp
2443 && flow
->dl_type
== htons(ETH_TYPE_LACP
)) {
2445 lacp_process_packet(xport
->xbundle
->lacp
, xport
->ofport
, packet
);
2448 } else if ((xbridge
->stp
|| xbridge
->rstp
) &&
2449 stp_should_process_flow(flow
, wc
)) {
2452 ? stp_process_packet(xport
, packet
)
2453 : rstp_process_packet(xport
, packet
);
2462 compose_output_action__(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
,
2465 const struct xport
*xport
= get_ofp_port(ctx
->xbridge
, ofp_port
);
2466 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
2467 struct flow
*flow
= &ctx
->xin
->flow
;
2468 ovs_be16 flow_vlan_tci
;
2469 uint32_t flow_pkt_mark
;
2470 uint8_t flow_nw_tos
;
2471 odp_port_t out_port
, odp_port
;
2474 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2475 * before traversing a patch port. */
2476 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 27);
2479 xlate_report(ctx
, "Nonexistent output port");
2481 } else if (xport
->config
& OFPUTIL_PC_NO_FWD
) {
2482 xlate_report(ctx
, "OFPPC_NO_FWD set, skipping output");
2484 } else if (check_stp
) {
2485 if (is_stp(&ctx
->base_flow
)) {
2486 if (!xport_stp_should_forward_bpdu(xport
) &&
2487 !xport_rstp_should_manage_bpdu(xport
)) {
2488 if (ctx
->xbridge
->stp
!= NULL
) {
2489 xlate_report(ctx
, "STP not in listening state, "
2490 "skipping bpdu output");
2491 } else if (ctx
->xbridge
->rstp
!= NULL
) {
2492 xlate_report(ctx
, "RSTP not managing BPDU in this state, "
2493 "skipping bpdu output");
2497 } else if (!xport_stp_forward_state(xport
) ||
2498 !xport_rstp_forward_state(xport
)) {
2499 if (ctx
->xbridge
->stp
!= NULL
) {
2500 xlate_report(ctx
, "STP not in forwarding state, "
2502 } else if (ctx
->xbridge
->rstp
!= NULL
) {
2503 xlate_report(ctx
, "RSTP not in forwarding state, "
2510 if (mbridge_has_mirrors(ctx
->xbridge
->mbridge
) && xport
->xbundle
) {
2511 ctx
->xout
->mirrors
|= xbundle_mirror_dst(xport
->xbundle
->xbridge
,
2516 const struct xport
*peer
= xport
->peer
;
2517 struct flow old_flow
= ctx
->xin
->flow
;
2518 enum slow_path_reason special
;
2520 ctx
->xbridge
= peer
->xbridge
;
2521 flow
->in_port
.ofp_port
= peer
->ofp_port
;
2522 flow
->metadata
= htonll(0);
2523 memset(&flow
->tunnel
, 0, sizeof flow
->tunnel
);
2524 memset(flow
->regs
, 0, sizeof flow
->regs
);
2526 special
= process_special(ctx
, &ctx
->xin
->flow
, peer
,
2529 ctx
->xout
->slow
|= special
;
2530 } else if (may_receive(peer
, ctx
)) {
2531 if (xport_stp_forward_state(peer
) && xport_rstp_forward_state(peer
)) {
2532 xlate_table_action(ctx
, flow
->in_port
.ofp_port
, 0, true, true);
2534 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
2535 * the learning action look at the packet, then drop it. */
2536 struct flow old_base_flow
= ctx
->base_flow
;
2537 size_t old_size
= ofpbuf_size(ctx
->xout
->odp_actions
);
2538 mirror_mask_t old_mirrors
= ctx
->xout
->mirrors
;
2539 xlate_table_action(ctx
, flow
->in_port
.ofp_port
, 0, true, true);
2540 ctx
->xout
->mirrors
= old_mirrors
;
2541 ctx
->base_flow
= old_base_flow
;
2542 ofpbuf_set_size(ctx
->xout
->odp_actions
, old_size
);
2546 ctx
->xin
->flow
= old_flow
;
2547 ctx
->xbridge
= xport
->xbridge
;
2549 if (ctx
->xin
->resubmit_stats
) {
2550 netdev_vport_inc_tx(xport
->netdev
, ctx
->xin
->resubmit_stats
);
2551 netdev_vport_inc_rx(peer
->netdev
, ctx
->xin
->resubmit_stats
);
2553 bfd_account_rx(peer
->bfd
, ctx
->xin
->resubmit_stats
);
2556 if (ctx
->xin
->xcache
) {
2557 struct xc_entry
*entry
;
2559 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NETDEV
);
2560 entry
->u
.dev
.tx
= netdev_ref(xport
->netdev
);
2561 entry
->u
.dev
.rx
= netdev_ref(peer
->netdev
);
2562 entry
->u
.dev
.bfd
= bfd_ref(peer
->bfd
);
2567 flow_vlan_tci
= flow
->vlan_tci
;
2568 flow_pkt_mark
= flow
->pkt_mark
;
2569 flow_nw_tos
= flow
->nw_tos
;
2571 if (dscp_from_skb_priority(xport
, flow
->skb_priority
, &dscp
)) {
2572 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
2573 flow
->nw_tos
&= ~IP_DSCP_MASK
;
2574 flow
->nw_tos
|= dscp
;
2577 if (xport
->is_tunnel
) {
2578 /* Save tunnel metadata so that changes made due to
2579 * the Logical (tunnel) Port are not visible for any further
2580 * matches, while explicit set actions on tunnel metadata are.
2582 struct flow_tnl flow_tnl
= flow
->tunnel
;
2583 odp_port
= tnl_port_send(xport
->ofport
, flow
, &ctx
->xout
->wc
);
2584 if (odp_port
== ODPP_NONE
) {
2585 xlate_report(ctx
, "Tunneling decided against output");
2586 goto out
; /* restore flow_nw_tos */
2588 if (flow
->tunnel
.ip_dst
== ctx
->orig_tunnel_ip_dst
) {
2589 xlate_report(ctx
, "Not tunneling to our own address");
2590 goto out
; /* restore flow_nw_tos */
2592 if (ctx
->xin
->resubmit_stats
) {
2593 netdev_vport_inc_tx(xport
->netdev
, ctx
->xin
->resubmit_stats
);
2595 if (ctx
->xin
->xcache
) {
2596 struct xc_entry
*entry
;
2598 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NETDEV
);
2599 entry
->u
.dev
.tx
= netdev_ref(xport
->netdev
);
2601 out_port
= odp_port
;
2602 commit_odp_tunnel_action(flow
, &ctx
->base_flow
,
2603 ctx
->xout
->odp_actions
);
2604 flow
->tunnel
= flow_tnl
; /* Restore tunnel metadata */
2606 odp_port
= xport
->odp_port
;
2607 out_port
= odp_port
;
2608 if (ofproto_has_vlan_splinters(ctx
->xbridge
->ofproto
)) {
2609 ofp_port_t vlandev_port
;
2611 wc
->masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
2612 vlandev_port
= vsp_realdev_to_vlandev(ctx
->xbridge
->ofproto
,
2613 ofp_port
, flow
->vlan_tci
);
2614 if (vlandev_port
!= ofp_port
) {
2615 out_port
= ofp_port_to_odp_port(ctx
->xbridge
, vlandev_port
);
2616 flow
->vlan_tci
= htons(0);
2621 if (out_port
!= ODPP_NONE
) {
2622 ctx
->xout
->slow
|= commit_odp_actions(flow
, &ctx
->base_flow
,
2623 ctx
->xout
->odp_actions
,
2626 if (ctx
->use_recirc
) {
2627 struct ovs_action_hash
*act_hash
;
2628 struct xlate_recirc
*xr
= &ctx
->recirc
;
2631 act_hash
= nl_msg_put_unspec_uninit(ctx
->xout
->odp_actions
,
2632 OVS_ACTION_ATTR_HASH
,
2634 act_hash
->hash_alg
= xr
->hash_alg
;
2635 act_hash
->hash_basis
= xr
->hash_basis
;
2637 /* Recirc action. */
2638 nl_msg_put_u32(ctx
->xout
->odp_actions
, OVS_ACTION_ATTR_RECIRC
,
2641 add_ipfix_output_action(ctx
, out_port
);
2642 nl_msg_put_odp_port(ctx
->xout
->odp_actions
, OVS_ACTION_ATTR_OUTPUT
,
2646 ctx
->sflow_odp_port
= odp_port
;
2647 ctx
->sflow_n_outputs
++;
2648 ctx
->xout
->nf_output_iface
= ofp_port
;
2653 flow
->vlan_tci
= flow_vlan_tci
;
2654 flow
->pkt_mark
= flow_pkt_mark
;
2655 flow
->nw_tos
= flow_nw_tos
;
2659 compose_output_action(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
)
2661 compose_output_action__(ctx
, ofp_port
, true);
2665 xlate_recursively(struct xlate_ctx
*ctx
, struct rule_dpif
*rule
)
2667 struct rule_dpif
*old_rule
= ctx
->rule
;
2668 const struct rule_actions
*actions
;
2670 if (ctx
->xin
->resubmit_stats
) {
2671 rule_dpif_credit_stats(rule
, ctx
->xin
->resubmit_stats
);
2677 actions
= rule_dpif_get_actions(rule
);
2678 do_xlate_actions(actions
->ofpacts
, actions
->ofpacts_len
, ctx
);
2679 ctx
->rule
= old_rule
;
2684 xlate_resubmit_resource_check(struct xlate_ctx
*ctx
)
2686 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
2688 if (ctx
->recurse
>= MAX_RESUBMIT_RECURSION
+ MAX_INTERNAL_RESUBMITS
) {
2689 VLOG_ERR_RL(&rl
, "resubmit actions recursed over %d times",
2690 MAX_RESUBMIT_RECURSION
);
2691 } else if (ctx
->resubmits
>= MAX_RESUBMITS
+ MAX_INTERNAL_RESUBMITS
) {
2692 VLOG_ERR_RL(&rl
, "over %d resubmit actions", MAX_RESUBMITS
);
2693 } else if (ofpbuf_size(ctx
->xout
->odp_actions
) > UINT16_MAX
) {
2694 VLOG_ERR_RL(&rl
, "resubmits yielded over 64 kB of actions");
2695 } else if (ofpbuf_size(&ctx
->stack
) >= 65536) {
2696 VLOG_ERR_RL(&rl
, "resubmits yielded over 64 kB of stack");
2705 xlate_table_action(struct xlate_ctx
*ctx
, ofp_port_t in_port
, uint8_t table_id
,
2706 bool may_packet_in
, bool honor_table_miss
)
2708 if (xlate_resubmit_resource_check(ctx
)) {
2709 ofp_port_t old_in_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
2710 bool skip_wildcards
= ctx
->xin
->skip_wildcards
;
2711 uint8_t old_table_id
= ctx
->table_id
;
2712 struct rule_dpif
*rule
;
2713 enum rule_dpif_lookup_verdict verdict
;
2714 enum ofputil_port_config config
= 0;
2716 ctx
->table_id
= table_id
;
2718 /* Look up a flow with 'in_port' as the input port. Then restore the
2719 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2720 * have surprising behavior). */
2721 ctx
->xin
->flow
.in_port
.ofp_port
= in_port
;
2722 verdict
= rule_dpif_lookup_from_table(ctx
->xbridge
->ofproto
,
2725 ? &ctx
->xout
->wc
: NULL
,
2727 &ctx
->table_id
, &rule
,
2728 ctx
->xin
->xcache
!= NULL
,
2729 ctx
->xin
->resubmit_stats
);
2730 ctx
->xin
->flow
.in_port
.ofp_port
= old_in_port
;
2732 if (ctx
->xin
->resubmit_hook
) {
2733 ctx
->xin
->resubmit_hook(ctx
->xin
, rule
, ctx
->recurse
);
2737 case RULE_DPIF_LOOKUP_VERDICT_MATCH
:
2739 case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER
:
2740 if (may_packet_in
) {
2741 struct xport
*xport
;
2743 xport
= get_ofp_port(ctx
->xbridge
,
2744 ctx
->xin
->flow
.in_port
.ofp_port
);
2745 config
= xport
? xport
->config
: 0;
2748 /* Fall through to drop */
2749 case RULE_DPIF_LOOKUP_VERDICT_DROP
:
2750 config
= OFPUTIL_PC_NO_PACKET_IN
;
2752 case RULE_DPIF_LOOKUP_VERDICT_DEFAULT
:
2753 if (!ofproto_dpif_wants_packet_in_on_miss(ctx
->xbridge
->ofproto
)) {
2754 config
= OFPUTIL_PC_NO_PACKET_IN
;
2761 choose_miss_rule(config
, ctx
->xbridge
->miss_rule
,
2762 ctx
->xbridge
->no_packet_in_rule
, &rule
,
2763 ctx
->xin
->xcache
!= NULL
);
2767 /* Fill in the cache entry here instead of xlate_recursively
2768 * to make the reference counting more explicit. We take a
2769 * reference in the lookups above if we are going to cache the
2771 if (ctx
->xin
->xcache
) {
2772 struct xc_entry
*entry
;
2774 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_RULE
);
2775 entry
->u
.rule
= rule
;
2777 xlate_recursively(ctx
, rule
);
2780 ctx
->table_id
= old_table_id
;
2788 xlate_group_stats(struct xlate_ctx
*ctx
, struct group_dpif
*group
,
2789 struct ofputil_bucket
*bucket
)
2791 if (ctx
->xin
->resubmit_stats
) {
2792 group_dpif_credit_stats(group
, bucket
, ctx
->xin
->resubmit_stats
);
2794 if (ctx
->xin
->xcache
) {
2795 struct xc_entry
*entry
;
2797 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_GROUP
);
2798 entry
->u
.group
.group
= group_dpif_ref(group
);
2799 entry
->u
.group
.bucket
= bucket
;
2804 xlate_group_bucket(struct xlate_ctx
*ctx
, struct ofputil_bucket
*bucket
)
2806 uint64_t action_list_stub
[1024 / 8];
2807 struct ofpbuf action_list
, action_set
;
2809 ofpbuf_use_const(&action_set
, bucket
->ofpacts
, bucket
->ofpacts_len
);
2810 ofpbuf_use_stub(&action_list
, action_list_stub
, sizeof action_list_stub
);
2812 ofpacts_execute_action_set(&action_list
, &action_set
);
2814 do_xlate_actions(ofpbuf_data(&action_list
), ofpbuf_size(&action_list
), ctx
);
2817 ofpbuf_uninit(&action_set
);
2818 ofpbuf_uninit(&action_list
);
2822 xlate_all_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
2824 struct ofputil_bucket
*bucket
;
2825 const struct list
*buckets
;
2826 struct flow old_flow
= ctx
->xin
->flow
;
2828 group_dpif_get_buckets(group
, &buckets
);
2830 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
2831 xlate_group_bucket(ctx
, bucket
);
2832 /* Roll back flow to previous state.
2833 * This is equivalent to cloning the packet for each bucket.
2835 * As a side effect any subsequently applied actions will
2836 * also effectively be applied to a clone of the packet taken
2837 * just before applying the all or indirect group. */
2838 ctx
->xin
->flow
= old_flow
;
2840 xlate_group_stats(ctx
, group
, NULL
);
2844 xlate_ff_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
2846 struct ofputil_bucket
*bucket
;
2848 bucket
= group_first_live_bucket(ctx
, group
, 0);
2850 xlate_group_bucket(ctx
, bucket
);
2851 xlate_group_stats(ctx
, group
, bucket
);
2856 xlate_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
2858 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
2859 struct ofputil_bucket
*bucket
;
2862 basis
= flow_hash_symmetric_l4(&ctx
->xin
->flow
, 0);
2863 bucket
= group_best_live_bucket(ctx
, group
, basis
);
2865 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
2866 memset(&wc
->masks
.dl_src
, 0xff, sizeof wc
->masks
.dl_src
);
2867 memset(&wc
->masks
.dl_type
, 0xff, sizeof wc
->masks
.dl_type
);
2868 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
2869 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
2870 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
2871 memset(&wc
->masks
.tp_dst
, 0xff, sizeof wc
->masks
.tp_dst
);
2872 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
2873 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
2875 xlate_group_bucket(ctx
, bucket
);
2876 xlate_group_stats(ctx
, group
, bucket
);
2881 xlate_group_action__(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
2883 ctx
->in_group
= true;
2885 switch (group_dpif_get_type(group
)) {
2887 case OFPGT11_INDIRECT
:
2888 xlate_all_group(ctx
, group
);
2890 case OFPGT11_SELECT
:
2891 xlate_select_group(ctx
, group
);
2894 xlate_ff_group(ctx
, group
);
2899 group_dpif_unref(group
);
2901 ctx
->in_group
= false;
2905 xlate_group_resource_check(struct xlate_ctx
*ctx
)
2907 if (!xlate_resubmit_resource_check(ctx
)) {
2909 } else if (ctx
->in_group
) {
2910 /* Prevent nested translation of OpenFlow groups.
2912 * OpenFlow allows this restriction. We enforce this restriction only
2913 * because, with the current architecture, we would otherwise have to
2914 * take a possibly recursive read lock on the ofgroup rwlock, which is
2915 * unsafe given that POSIX allows taking a read lock to block if there
2916 * is a thread blocked on taking the write lock. Other solutions
2917 * without this restriction are also possible, but seem unwarranted
2918 * given the current limited use of groups. */
2919 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
2921 VLOG_ERR_RL(&rl
, "cannot recursively translate OpenFlow group");
2929 xlate_group_action(struct xlate_ctx
*ctx
, uint32_t group_id
)
2931 if (xlate_group_resource_check(ctx
)) {
2932 struct group_dpif
*group
;
2935 got_group
= group_dpif_lookup(ctx
->xbridge
->ofproto
, group_id
, &group
);
2937 xlate_group_action__(ctx
, group
);
2947 xlate_ofpact_resubmit(struct xlate_ctx
*ctx
,
2948 const struct ofpact_resubmit
*resubmit
)
2952 bool may_packet_in
= false;
2953 bool honor_table_miss
= false;
2955 if (ctx
->rule
&& rule_dpif_is_internal(ctx
->rule
)) {
2956 /* Still allow missed packets to be sent to the controller
2957 * if resubmitting from an internal table. */
2958 may_packet_in
= true;
2959 honor_table_miss
= true;
2962 in_port
= resubmit
->in_port
;
2963 if (in_port
== OFPP_IN_PORT
) {
2964 in_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
2967 table_id
= resubmit
->table_id
;
2968 if (table_id
== 255) {
2969 table_id
= ctx
->table_id
;
2972 xlate_table_action(ctx
, in_port
, table_id
, may_packet_in
,
2977 flood_packets(struct xlate_ctx
*ctx
, bool all
)
2979 const struct xport
*xport
;
2981 HMAP_FOR_EACH (xport
, ofp_node
, &ctx
->xbridge
->xports
) {
2982 if (xport
->ofp_port
== ctx
->xin
->flow
.in_port
.ofp_port
) {
2987 compose_output_action__(ctx
, xport
->ofp_port
, false);
2988 } else if (!(xport
->config
& OFPUTIL_PC_NO_FLOOD
)) {
2989 compose_output_action(ctx
, xport
->ofp_port
);
2993 ctx
->xout
->nf_output_iface
= NF_OUT_FLOOD
;
2997 execute_controller_action(struct xlate_ctx
*ctx
, int len
,
2998 enum ofp_packet_in_reason reason
,
2999 uint16_t controller_id
)
3001 struct ofproto_packet_in
*pin
;
3002 struct dpif_packet
*packet
;
3003 struct pkt_metadata md
= PKT_METADATA_INITIALIZER(0);
3005 ctx
->xout
->slow
|= SLOW_CONTROLLER
;
3006 if (!ctx
->xin
->packet
) {
3010 packet
= dpif_packet_clone_from_ofpbuf(ctx
->xin
->packet
);
3012 ctx
->xout
->slow
|= commit_odp_actions(&ctx
->xin
->flow
, &ctx
->base_flow
,
3013 ctx
->xout
->odp_actions
,
3016 odp_execute_actions(NULL
, &packet
, 1, false, &md
,
3017 ofpbuf_data(ctx
->xout
->odp_actions
),
3018 ofpbuf_size(ctx
->xout
->odp_actions
), NULL
);
3020 pin
= xmalloc(sizeof *pin
);
3021 pin
->up
.packet_len
= ofpbuf_size(&packet
->ofpbuf
);
3022 pin
->up
.packet
= ofpbuf_steal_data(&packet
->ofpbuf
);
3023 pin
->up
.reason
= reason
;
3024 pin
->up
.table_id
= ctx
->table_id
;
3025 pin
->up
.cookie
= (ctx
->rule
3026 ? rule_dpif_get_flow_cookie(ctx
->rule
)
3029 flow_get_metadata(&ctx
->xin
->flow
, &pin
->up
.fmd
);
3031 pin
->controller_id
= controller_id
;
3032 pin
->send_len
= len
;
3033 /* If a rule is a table-miss rule then this is
3034 * a table-miss handled by a table-miss rule.
3036 * Else, if rule is internal and has a controller action,
3037 * the later being implied by the rule being processed here,
3038 * then this is a table-miss handled without a table-miss rule.
3040 * Otherwise this is not a table-miss. */
3041 pin
->miss_type
= OFPROTO_PACKET_IN_NO_MISS
;
3043 if (rule_dpif_is_table_miss(ctx
->rule
)) {
3044 pin
->miss_type
= OFPROTO_PACKET_IN_MISS_FLOW
;
3045 } else if (rule_dpif_is_internal(ctx
->rule
)) {
3046 pin
->miss_type
= OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW
;
3049 ofproto_dpif_send_packet_in(ctx
->xbridge
->ofproto
, pin
);
3050 dpif_packet_delete(packet
);
3054 compose_recirculate_action(struct xlate_ctx
*ctx
,
3055 const struct ofpact
*ofpacts_base
,
3056 const struct ofpact
*ofpact_current
,
3057 size_t ofpacts_base_len
)
3061 unsigned ofpacts_len
;
3064 struct ofpbuf ofpacts
;
3068 ofpacts_len
= ofpacts_base_len
-
3069 ((uint8_t *)ofpact_current
- (uint8_t *)ofpacts_base
);
3072 id
= rule_dpif_get_recirc_id(ctx
->rule
);
3074 /* In the case where ctx has no rule then allocate a recirc id.
3075 * The life-cycle of this recirc id is managed by associating it
3076 * with the internal rule that is created to to handle
3077 * recirculation below.
3079 * The known use-case of this is packet_out which
3080 * translates actions without a rule */
3081 id
= ofproto_dpif_alloc_recirc_id(ctx
->xbridge
->ofproto
);
3084 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3085 VLOG_ERR_RL(&rl
, "Failed to allocate recirculation id");
3089 match_init_catchall(&match
);
3090 match_set_recirc_id(&match
, id
);
3091 ofpbuf_use_const(&ofpacts
, ofpact_current
, ofpacts_len
);
3092 error
= ofproto_dpif_add_internal_flow(ctx
->xbridge
->ofproto
, &match
,
3093 RECIRC_RULE_PRIORITY
,
3094 RECIRC_TIMEOUT
, &ofpacts
, &rule
);
3096 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3097 VLOG_ERR_RL(&rl
, "Failed to add post recirculation flow %s",
3098 match_to_string(&match
, 0));
3101 /* If ctx has no rule then associate the recirc id, which
3102 * was allocated above, with the internal rule. This allows
3103 * the recirc id to be released when the internal rule times out. */
3105 rule_set_recirc_id(rule
, id
);
3108 ctx
->xout
->slow
|= commit_odp_actions(&ctx
->xin
->flow
, &ctx
->base_flow
,
3109 ctx
->xout
->odp_actions
,
3111 nl_msg_put_u32(ctx
->xout
->odp_actions
, OVS_ACTION_ATTR_RECIRC
, id
);
3115 compose_mpls_push_action(struct xlate_ctx
*ctx
, struct ofpact_push_mpls
*mpls
)
3117 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
3118 struct flow
*flow
= &ctx
->xin
->flow
;
3121 ovs_assert(eth_type_mpls(mpls
->ethertype
));
3123 n
= flow_count_mpls_labels(flow
, wc
);
3125 ctx
->xout
->slow
|= commit_odp_actions(flow
, &ctx
->base_flow
,
3126 ctx
->xout
->odp_actions
,
3128 } else if (n
>= FLOW_MAX_MPLS_LABELS
) {
3129 if (ctx
->xin
->packet
!= NULL
) {
3130 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3131 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet on which an "
3132 "MPLS push action can't be performed as it would "
3133 "have more MPLS LSEs than the %d supported.",
3134 ctx
->xbridge
->name
, FLOW_MAX_MPLS_LABELS
);
3138 } else if (n
>= ctx
->xbridge
->max_mpls_depth
) {
3139 COVERAGE_INC(xlate_actions_mpls_overflow
);
3140 ctx
->xout
->slow
|= SLOW_ACTION
;
3143 flow_push_mpls(flow
, n
, mpls
->ethertype
, wc
);
3147 compose_mpls_pop_action(struct xlate_ctx
*ctx
, ovs_be16 eth_type
)
3149 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
3150 struct flow
*flow
= &ctx
->xin
->flow
;
3151 int n
= flow_count_mpls_labels(flow
, wc
);
3153 if (flow_pop_mpls(flow
, n
, eth_type
, wc
)) {
3154 if (ctx
->xbridge
->enable_recirc
&& !eth_type_mpls(eth_type
)) {
3155 ctx
->was_mpls
= true;
3157 } else if (n
>= FLOW_MAX_MPLS_LABELS
) {
3158 if (ctx
->xin
->packet
!= NULL
) {
3159 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3160 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet on which an "
3161 "MPLS pop action can't be performed as it has "
3162 "more MPLS LSEs than the %d supported.",
3163 ctx
->xbridge
->name
, FLOW_MAX_MPLS_LABELS
);
3166 ofpbuf_clear(ctx
->xout
->odp_actions
);
3171 compose_dec_ttl(struct xlate_ctx
*ctx
, struct ofpact_cnt_ids
*ids
)
3173 struct flow
*flow
= &ctx
->xin
->flow
;
3175 if (!is_ip_any(flow
)) {
3179 ctx
->xout
->wc
.masks
.nw_ttl
= 0xff;
3180 if (flow
->nw_ttl
> 1) {
3186 for (i
= 0; i
< ids
->n_controllers
; i
++) {
3187 execute_controller_action(ctx
, UINT16_MAX
, OFPR_INVALID_TTL
,
3191 /* Stop processing for current table. */
3197 compose_set_mpls_label_action(struct xlate_ctx
*ctx
, ovs_be32 label
)
3199 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
3200 ctx
->xout
->wc
.masks
.mpls_lse
[0] |= htonl(MPLS_LABEL_MASK
);
3201 set_mpls_lse_label(&ctx
->xin
->flow
.mpls_lse
[0], label
);
3206 compose_set_mpls_tc_action(struct xlate_ctx
*ctx
, uint8_t tc
)
3208 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
3209 ctx
->xout
->wc
.masks
.mpls_lse
[0] |= htonl(MPLS_TC_MASK
);
3210 set_mpls_lse_tc(&ctx
->xin
->flow
.mpls_lse
[0], tc
);
3215 compose_set_mpls_ttl_action(struct xlate_ctx
*ctx
, uint8_t ttl
)
3217 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
3218 ctx
->xout
->wc
.masks
.mpls_lse
[0] |= htonl(MPLS_TTL_MASK
);
3219 set_mpls_lse_ttl(&ctx
->xin
->flow
.mpls_lse
[0], ttl
);
3224 compose_dec_mpls_ttl_action(struct xlate_ctx
*ctx
)
3226 struct flow
*flow
= &ctx
->xin
->flow
;
3227 uint8_t ttl
= mpls_lse_to_ttl(flow
->mpls_lse
[0]);
3228 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
3230 memset(&wc
->masks
.mpls_lse
, 0xff, sizeof wc
->masks
.mpls_lse
);
3231 if (eth_type_mpls(flow
->dl_type
)) {
3234 set_mpls_lse_ttl(&flow
->mpls_lse
[0], ttl
);
3237 execute_controller_action(ctx
, UINT16_MAX
, OFPR_INVALID_TTL
, 0);
3239 /* Stop processing for current table. */
3248 xlate_output_action(struct xlate_ctx
*ctx
,
3249 ofp_port_t port
, uint16_t max_len
, bool may_packet_in
)
3251 ofp_port_t prev_nf_output_iface
= ctx
->xout
->nf_output_iface
;
3253 ctx
->xout
->nf_output_iface
= NF_OUT_DROP
;
3257 compose_output_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
);
3260 xlate_table_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
,
3261 0, may_packet_in
, true);
3267 flood_packets(ctx
, false);
3270 flood_packets(ctx
, true);
3272 case OFPP_CONTROLLER
:
3273 execute_controller_action(ctx
, max_len
, OFPR_ACTION
, 0);
3279 if (port
!= ctx
->xin
->flow
.in_port
.ofp_port
) {
3280 compose_output_action(ctx
, port
);
3282 xlate_report(ctx
, "skipping output to input port");
3287 if (prev_nf_output_iface
== NF_OUT_FLOOD
) {
3288 ctx
->xout
->nf_output_iface
= NF_OUT_FLOOD
;
3289 } else if (ctx
->xout
->nf_output_iface
== NF_OUT_DROP
) {
3290 ctx
->xout
->nf_output_iface
= prev_nf_output_iface
;
3291 } else if (prev_nf_output_iface
!= NF_OUT_DROP
&&
3292 ctx
->xout
->nf_output_iface
!= NF_OUT_FLOOD
) {
3293 ctx
->xout
->nf_output_iface
= NF_OUT_MULTI
;
3298 xlate_output_reg_action(struct xlate_ctx
*ctx
,
3299 const struct ofpact_output_reg
*or)
3301 uint64_t port
= mf_get_subfield(&or->src
, &ctx
->xin
->flow
);
3302 if (port
<= UINT16_MAX
) {
3303 union mf_subvalue value
;
3305 memset(&value
, 0xff, sizeof value
);
3306 mf_write_subfield_flow(&or->src
, &value
, &ctx
->xout
->wc
.masks
);
3307 xlate_output_action(ctx
, u16_to_ofp(port
),
3308 or->max_len
, false);
3313 xlate_enqueue_action(struct xlate_ctx
*ctx
,
3314 const struct ofpact_enqueue
*enqueue
)
3316 ofp_port_t ofp_port
= enqueue
->port
;
3317 uint32_t queue_id
= enqueue
->queue
;
3318 uint32_t flow_priority
, priority
;
3321 /* Translate queue to priority. */
3322 error
= dpif_queue_to_priority(ctx
->xbridge
->dpif
, queue_id
, &priority
);
3324 /* Fall back to ordinary output action. */
3325 xlate_output_action(ctx
, enqueue
->port
, 0, false);
3329 /* Check output port. */
3330 if (ofp_port
== OFPP_IN_PORT
) {
3331 ofp_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
3332 } else if (ofp_port
== ctx
->xin
->flow
.in_port
.ofp_port
) {
3336 /* Add datapath actions. */
3337 flow_priority
= ctx
->xin
->flow
.skb_priority
;
3338 ctx
->xin
->flow
.skb_priority
= priority
;
3339 compose_output_action(ctx
, ofp_port
);
3340 ctx
->xin
->flow
.skb_priority
= flow_priority
;
3342 /* Update NetFlow output port. */
3343 if (ctx
->xout
->nf_output_iface
== NF_OUT_DROP
) {
3344 ctx
->xout
->nf_output_iface
= ofp_port
;
3345 } else if (ctx
->xout
->nf_output_iface
!= NF_OUT_FLOOD
) {
3346 ctx
->xout
->nf_output_iface
= NF_OUT_MULTI
;
3351 xlate_set_queue_action(struct xlate_ctx
*ctx
, uint32_t queue_id
)
3353 uint32_t skb_priority
;
3355 if (!dpif_queue_to_priority(ctx
->xbridge
->dpif
, queue_id
, &skb_priority
)) {
3356 ctx
->xin
->flow
.skb_priority
= skb_priority
;
3358 /* Couldn't translate queue to a priority. Nothing to do. A warning
3359 * has already been logged. */
3364 slave_enabled_cb(ofp_port_t ofp_port
, void *xbridge_
)
3366 const struct xbridge
*xbridge
= xbridge_
;
3377 case OFPP_CONTROLLER
: /* Not supported by the bundle action. */
3380 port
= get_ofp_port(xbridge
, ofp_port
);
3381 return port
? port
->may_enable
: false;
3386 xlate_bundle_action(struct xlate_ctx
*ctx
,
3387 const struct ofpact_bundle
*bundle
)
3391 port
= bundle_execute(bundle
, &ctx
->xin
->flow
, &ctx
->xout
->wc
,
3393 CONST_CAST(struct xbridge
*, ctx
->xbridge
));
3394 if (bundle
->dst
.field
) {
3395 nxm_reg_load(&bundle
->dst
, ofp_to_u16(port
), &ctx
->xin
->flow
,
3398 xlate_output_action(ctx
, port
, 0, false);
3403 xlate_learn_action__(struct xlate_ctx
*ctx
, const struct ofpact_learn
*learn
,
3404 struct ofputil_flow_mod
*fm
, struct ofpbuf
*ofpacts
)
3406 learn_execute(learn
, &ctx
->xin
->flow
, fm
, ofpacts
);
3407 if (ctx
->xin
->may_learn
) {
3408 ofproto_dpif_flow_mod(ctx
->xbridge
->ofproto
, fm
);
3413 xlate_learn_action(struct xlate_ctx
*ctx
, const struct ofpact_learn
*learn
)
3415 ctx
->xout
->has_learn
= true;
3416 learn_mask(learn
, &ctx
->xout
->wc
);
3418 if (ctx
->xin
->xcache
) {
3419 struct xc_entry
*entry
;
3421 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_LEARN
);
3422 entry
->u
.learn
.ofproto
= ctx
->xbridge
->ofproto
;
3423 entry
->u
.learn
.fm
= xmalloc(sizeof *entry
->u
.learn
.fm
);
3424 entry
->u
.learn
.ofpacts
= ofpbuf_new(64);
3425 xlate_learn_action__(ctx
, learn
, entry
->u
.learn
.fm
,
3426 entry
->u
.learn
.ofpacts
);
3427 } else if (ctx
->xin
->may_learn
) {
3428 uint64_t ofpacts_stub
[1024 / 8];
3429 struct ofputil_flow_mod fm
;
3430 struct ofpbuf ofpacts
;
3432 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
3433 xlate_learn_action__(ctx
, learn
, &fm
, &ofpacts
);
3434 ofpbuf_uninit(&ofpacts
);
3439 xlate_fin_timeout__(struct rule_dpif
*rule
, uint16_t tcp_flags
,
3440 uint16_t idle_timeout
, uint16_t hard_timeout
)
3442 if (tcp_flags
& (TCP_FIN
| TCP_RST
)) {
3443 rule_dpif_reduce_timeouts(rule
, idle_timeout
, hard_timeout
);
3448 xlate_fin_timeout(struct xlate_ctx
*ctx
,
3449 const struct ofpact_fin_timeout
*oft
)
3452 xlate_fin_timeout__(ctx
->rule
, ctx
->xin
->tcp_flags
,
3453 oft
->fin_idle_timeout
, oft
->fin_hard_timeout
);
3454 if (ctx
->xin
->xcache
) {
3455 struct xc_entry
*entry
;
3457 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_FIN_TIMEOUT
);
3458 /* XC_RULE already holds a reference on the rule, none is taken
3460 entry
->u
.fin
.rule
= ctx
->rule
;
3461 entry
->u
.fin
.idle
= oft
->fin_idle_timeout
;
3462 entry
->u
.fin
.hard
= oft
->fin_hard_timeout
;
3468 xlate_sample_action(struct xlate_ctx
*ctx
,
3469 const struct ofpact_sample
*os
)
3471 union user_action_cookie cookie
;
3472 /* Scale the probability from 16-bit to 32-bit while representing
3473 * the same percentage. */
3474 uint32_t probability
= (os
->probability
<< 16) | os
->probability
;
3476 if (!ctx
->xbridge
->variable_length_userdata
) {
3477 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
3479 VLOG_ERR_RL(&rl
, "ignoring NXAST_SAMPLE action because datapath "
3480 "lacks support (needs Linux 3.10+ or kernel module from "
3485 ctx
->xout
->slow
|= commit_odp_actions(&ctx
->xin
->flow
, &ctx
->base_flow
,
3486 ctx
->xout
->odp_actions
,
3489 compose_flow_sample_cookie(os
->probability
, os
->collector_set_id
,
3490 os
->obs_domain_id
, os
->obs_point_id
, &cookie
);
3491 compose_sample_action(ctx
->xbridge
, ctx
->xout
->odp_actions
, &ctx
->xin
->flow
,
3492 probability
, &cookie
, sizeof cookie
.flow_sample
,
3497 may_receive(const struct xport
*xport
, struct xlate_ctx
*ctx
)
3499 if (xport
->config
& (is_stp(&ctx
->xin
->flow
)
3500 ? OFPUTIL_PC_NO_RECV_STP
3501 : OFPUTIL_PC_NO_RECV
)) {
3505 /* Only drop packets here if both forwarding and learning are
3506 * disabled. If just learning is enabled, we need to have
3507 * OFPP_NORMAL and the learning action have a look at the packet
3508 * before we can drop it. */
3509 if ((!xport_stp_forward_state(xport
) && !xport_stp_learn_state(xport
)) ||
3510 (!xport_rstp_forward_state(xport
) && !xport_rstp_learn_state(xport
))) {
3518 xlate_write_actions(struct xlate_ctx
*ctx
, const struct ofpact
*a
)
3520 struct ofpact_nest
*on
= ofpact_get_WRITE_ACTIONS(a
);
3521 ofpbuf_put(&ctx
->action_set
, on
->actions
, ofpact_nest_get_action_len(on
));
3522 ofpact_pad(&ctx
->action_set
);
3526 xlate_action_set(struct xlate_ctx
*ctx
)
3528 uint64_t action_list_stub
[1024 / 64];
3529 struct ofpbuf action_list
;
3531 ofpbuf_use_stub(&action_list
, action_list_stub
, sizeof action_list_stub
);
3532 ofpacts_execute_action_set(&action_list
, &ctx
->action_set
);
3533 do_xlate_actions(ofpbuf_data(&action_list
), ofpbuf_size(&action_list
), ctx
);
3534 ofpbuf_uninit(&action_list
);
3538 ofpact_needs_recirculation_after_mpls(const struct xlate_ctx
*ctx
,
3539 const struct ofpact
*a
)
3541 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
3542 struct flow
*flow
= &ctx
->xin
->flow
;
3547 case OFPACT_CONTROLLER
:
3548 case OFPACT_STRIP_VLAN
:
3549 case OFPACT_SET_VLAN_PCP
:
3550 case OFPACT_SET_VLAN_VID
:
3551 case OFPACT_ENQUEUE
:
3552 case OFPACT_PUSH_VLAN
:
3553 case OFPACT_SET_ETH_SRC
:
3554 case OFPACT_SET_ETH_DST
:
3555 case OFPACT_SET_TUNNEL
:
3556 case OFPACT_SET_QUEUE
:
3557 case OFPACT_POP_QUEUE
:
3558 case OFPACT_POP_MPLS
:
3559 case OFPACT_DEC_MPLS_TTL
:
3560 case OFPACT_SET_MPLS_TTL
:
3561 case OFPACT_SET_MPLS_TC
:
3562 case OFPACT_SET_MPLS_LABEL
:
3564 case OFPACT_OUTPUT_REG
:
3567 case OFPACT_WRITE_METADATA
:
3568 case OFPACT_WRITE_ACTIONS
:
3569 case OFPACT_CLEAR_ACTIONS
:
3573 case OFPACT_SET_IPV4_SRC
:
3574 case OFPACT_SET_IPV4_DST
:
3575 case OFPACT_SET_IP_DSCP
:
3576 case OFPACT_SET_IP_ECN
:
3577 case OFPACT_SET_IP_TTL
:
3578 case OFPACT_SET_L4_SRC_PORT
:
3579 case OFPACT_SET_L4_DST_PORT
:
3580 case OFPACT_RESUBMIT
:
3581 case OFPACT_STACK_PUSH
:
3582 case OFPACT_STACK_POP
:
3583 case OFPACT_DEC_TTL
:
3584 case OFPACT_MULTIPATH
:
3587 case OFPACT_FIN_TIMEOUT
:
3588 case OFPACT_GOTO_TABLE
:
3591 case OFPACT_REG_MOVE
:
3592 return (mf_is_l3_or_higher(ofpact_get_REG_MOVE(a
)->dst
.field
) ||
3593 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a
)->src
.field
));
3595 case OFPACT_REG_LOAD
:
3596 return mf_is_l3_or_higher(ofpact_get_REG_LOAD(a
)->dst
.field
);
3598 case OFPACT_SET_FIELD
:
3599 return mf_is_l3_or_higher(ofpact_get_SET_FIELD(a
)->field
);
3601 case OFPACT_PUSH_MPLS
:
3602 /* Recirculate if it is an IP packet with a zero ttl. This may
3603 * indicate that the packet was previously MPLS and an MPLS pop action
3604 * converted it to IP. In this case recirculating should reveal the IP
3605 * TTL which is used as the basis for a new MPLS LSE. */
3606 return (!flow_count_mpls_labels(flow
, wc
)
3607 && flow
->nw_ttl
== 0
3608 && is_ip_any(flow
));
3615 do_xlate_actions(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
3616 struct xlate_ctx
*ctx
)
3618 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
3619 struct flow
*flow
= &ctx
->xin
->flow
;
3620 const struct ofpact
*a
;
3622 /* dl_type already in the mask, not set below. */
3624 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
3625 struct ofpact_controller
*controller
;
3626 const struct ofpact_metadata
*metadata
;
3627 const struct ofpact_set_field
*set_field
;
3628 const struct mf_field
*mf
;
3634 if (ctx
->was_mpls
&& ofpact_needs_recirculation_after_mpls(ctx
, a
)) {
3635 compose_recirculate_action(ctx
, ofpacts
, a
, ofpacts_len
);
3641 xlate_output_action(ctx
, ofpact_get_OUTPUT(a
)->port
,
3642 ofpact_get_OUTPUT(a
)->max_len
, true);
3646 if (xlate_group_action(ctx
, ofpact_get_GROUP(a
)->group_id
)) {
3651 case OFPACT_CONTROLLER
:
3652 controller
= ofpact_get_CONTROLLER(a
);
3653 execute_controller_action(ctx
, controller
->max_len
,
3655 controller
->controller_id
);
3658 case OFPACT_ENQUEUE
:
3659 xlate_enqueue_action(ctx
, ofpact_get_ENQUEUE(a
));
3662 case OFPACT_SET_VLAN_VID
:
3663 wc
->masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
3664 if (flow
->vlan_tci
& htons(VLAN_CFI
) ||
3665 ofpact_get_SET_VLAN_VID(a
)->push_vlan_if_needed
) {
3666 flow
->vlan_tci
&= ~htons(VLAN_VID_MASK
);
3667 flow
->vlan_tci
|= (htons(ofpact_get_SET_VLAN_VID(a
)->vlan_vid
)
3672 case OFPACT_SET_VLAN_PCP
:
3673 wc
->masks
.vlan_tci
|= htons(VLAN_PCP_MASK
| VLAN_CFI
);
3674 if (flow
->vlan_tci
& htons(VLAN_CFI
) ||
3675 ofpact_get_SET_VLAN_PCP(a
)->push_vlan_if_needed
) {
3676 flow
->vlan_tci
&= ~htons(VLAN_PCP_MASK
);
3677 flow
->vlan_tci
|= htons((ofpact_get_SET_VLAN_PCP(a
)->vlan_pcp
3678 << VLAN_PCP_SHIFT
) | VLAN_CFI
);
3682 case OFPACT_STRIP_VLAN
:
3683 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
3684 flow
->vlan_tci
= htons(0);
3687 case OFPACT_PUSH_VLAN
:
3688 /* XXX 802.1AD(QinQ) */
3689 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
3690 flow
->vlan_tci
= htons(VLAN_CFI
);
3693 case OFPACT_SET_ETH_SRC
:
3694 memset(&wc
->masks
.dl_src
, 0xff, sizeof wc
->masks
.dl_src
);
3695 memcpy(flow
->dl_src
, ofpact_get_SET_ETH_SRC(a
)->mac
, ETH_ADDR_LEN
);
3698 case OFPACT_SET_ETH_DST
:
3699 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
3700 memcpy(flow
->dl_dst
, ofpact_get_SET_ETH_DST(a
)->mac
, ETH_ADDR_LEN
);
3703 case OFPACT_SET_IPV4_SRC
:
3704 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
3705 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
3706 flow
->nw_src
= ofpact_get_SET_IPV4_SRC(a
)->ipv4
;
3710 case OFPACT_SET_IPV4_DST
:
3711 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
3712 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
3713 flow
->nw_dst
= ofpact_get_SET_IPV4_DST(a
)->ipv4
;
3717 case OFPACT_SET_IP_DSCP
:
3718 if (is_ip_any(flow
)) {
3719 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
3720 flow
->nw_tos
&= ~IP_DSCP_MASK
;
3721 flow
->nw_tos
|= ofpact_get_SET_IP_DSCP(a
)->dscp
;
3725 case OFPACT_SET_IP_ECN
:
3726 if (is_ip_any(flow
)) {
3727 wc
->masks
.nw_tos
|= IP_ECN_MASK
;
3728 flow
->nw_tos
&= ~IP_ECN_MASK
;
3729 flow
->nw_tos
|= ofpact_get_SET_IP_ECN(a
)->ecn
;
3733 case OFPACT_SET_IP_TTL
:
3734 if (is_ip_any(flow
)) {
3735 wc
->masks
.nw_ttl
= 0xff;
3736 flow
->nw_ttl
= ofpact_get_SET_IP_TTL(a
)->ttl
;
3740 case OFPACT_SET_L4_SRC_PORT
:
3741 if (is_ip_any(flow
)) {
3742 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
3743 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
3744 flow
->tp_src
= htons(ofpact_get_SET_L4_SRC_PORT(a
)->port
);
3748 case OFPACT_SET_L4_DST_PORT
:
3749 if (is_ip_any(flow
)) {
3750 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
3751 memset(&wc
->masks
.tp_dst
, 0xff, sizeof wc
->masks
.tp_dst
);
3752 flow
->tp_dst
= htons(ofpact_get_SET_L4_DST_PORT(a
)->port
);
3756 case OFPACT_RESUBMIT
:
3757 xlate_ofpact_resubmit(ctx
, ofpact_get_RESUBMIT(a
));
3760 case OFPACT_SET_TUNNEL
:
3761 flow
->tunnel
.tun_id
= htonll(ofpact_get_SET_TUNNEL(a
)->tun_id
);
3764 case OFPACT_SET_QUEUE
:
3765 xlate_set_queue_action(ctx
, ofpact_get_SET_QUEUE(a
)->queue_id
);
3768 case OFPACT_POP_QUEUE
:
3769 flow
->skb_priority
= ctx
->orig_skb_priority
;
3772 case OFPACT_REG_MOVE
:
3773 nxm_execute_reg_move(ofpact_get_REG_MOVE(a
), flow
, wc
);
3776 case OFPACT_REG_LOAD
:
3777 nxm_execute_reg_load(ofpact_get_REG_LOAD(a
), flow
, wc
);
3780 case OFPACT_SET_FIELD
:
3781 set_field
= ofpact_get_SET_FIELD(a
);
3782 mf
= set_field
->field
;
3784 /* Set field action only ever overwrites packet's outermost
3785 * applicable header fields. Do nothing if no header exists. */
3786 if (mf
->id
== MFF_VLAN_VID
) {
3787 wc
->masks
.vlan_tci
|= htons(VLAN_CFI
);
3788 if (!(flow
->vlan_tci
& htons(VLAN_CFI
))) {
3791 } else if ((mf
->id
== MFF_MPLS_LABEL
|| mf
->id
== MFF_MPLS_TC
)
3792 /* 'dl_type' is already unwildcarded. */
3793 && !eth_type_mpls(flow
->dl_type
)) {
3797 mf_mask_field_and_prereqs(mf
, &wc
->masks
);
3798 mf_set_flow_value(mf
, &set_field
->value
, flow
);
3801 case OFPACT_STACK_PUSH
:
3802 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a
), flow
, wc
,
3806 case OFPACT_STACK_POP
:
3807 nxm_execute_stack_pop(ofpact_get_STACK_POP(a
), flow
, wc
,
3811 case OFPACT_PUSH_MPLS
:
3812 compose_mpls_push_action(ctx
, ofpact_get_PUSH_MPLS(a
));
3815 case OFPACT_POP_MPLS
:
3816 compose_mpls_pop_action(ctx
, ofpact_get_POP_MPLS(a
)->ethertype
);
3819 case OFPACT_SET_MPLS_LABEL
:
3820 compose_set_mpls_label_action(
3821 ctx
, ofpact_get_SET_MPLS_LABEL(a
)->label
);
3824 case OFPACT_SET_MPLS_TC
:
3825 compose_set_mpls_tc_action(ctx
, ofpact_get_SET_MPLS_TC(a
)->tc
);
3828 case OFPACT_SET_MPLS_TTL
:
3829 compose_set_mpls_ttl_action(ctx
, ofpact_get_SET_MPLS_TTL(a
)->ttl
);
3832 case OFPACT_DEC_MPLS_TTL
:
3833 if (compose_dec_mpls_ttl_action(ctx
)) {
3838 case OFPACT_DEC_TTL
:
3839 wc
->masks
.nw_ttl
= 0xff;
3840 if (compose_dec_ttl(ctx
, ofpact_get_DEC_TTL(a
))) {
3846 /* Nothing to do. */
3849 case OFPACT_MULTIPATH
:
3850 multipath_execute(ofpact_get_MULTIPATH(a
), flow
, wc
);
3854 xlate_bundle_action(ctx
, ofpact_get_BUNDLE(a
));
3857 case OFPACT_OUTPUT_REG
:
3858 xlate_output_reg_action(ctx
, ofpact_get_OUTPUT_REG(a
));
3862 xlate_learn_action(ctx
, ofpact_get_LEARN(a
));
3869 case OFPACT_FIN_TIMEOUT
:
3870 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
3871 ctx
->xout
->has_fin_timeout
= true;
3872 xlate_fin_timeout(ctx
, ofpact_get_FIN_TIMEOUT(a
));
3875 case OFPACT_CLEAR_ACTIONS
:
3876 ofpbuf_clear(&ctx
->action_set
);
3879 case OFPACT_WRITE_ACTIONS
:
3880 xlate_write_actions(ctx
, a
);
3883 case OFPACT_WRITE_METADATA
:
3884 metadata
= ofpact_get_WRITE_METADATA(a
);
3885 flow
->metadata
&= ~metadata
->mask
;
3886 flow
->metadata
|= metadata
->metadata
& metadata
->mask
;
3890 /* Not implemented yet. */
3893 case OFPACT_GOTO_TABLE
: {
3894 struct ofpact_goto_table
*ogt
= ofpact_get_GOTO_TABLE(a
);
3896 /* Allow ctx->table_id == TBL_INTERNAL, which will be greater
3897 * than ogt->table_id. This is to allow goto_table actions that
3898 * triggered recirculation: ctx->table_id will be TBL_INTERNAL
3899 * after recirculation. */
3900 ovs_assert(ctx
->table_id
== TBL_INTERNAL
3901 || ctx
->table_id
< ogt
->table_id
);
3902 xlate_table_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
,
3903 ogt
->table_id
, true, true);
3908 xlate_sample_action(ctx
, ofpact_get_SAMPLE(a
));
3915 xlate_in_init(struct xlate_in
*xin
, struct ofproto_dpif
*ofproto
,
3916 const struct flow
*flow
, ofp_port_t in_port
,
3917 struct rule_dpif
*rule
, uint16_t tcp_flags
,
3918 const struct ofpbuf
*packet
)
3920 xin
->ofproto
= ofproto
;
3922 xin
->flow
.in_port
.ofp_port
= in_port
;
3923 xin
->packet
= packet
;
3924 xin
->may_learn
= packet
!= NULL
;
3927 xin
->ofpacts
= NULL
;
3928 xin
->ofpacts_len
= 0;
3929 xin
->tcp_flags
= tcp_flags
;
3930 xin
->resubmit_hook
= NULL
;
3931 xin
->report_hook
= NULL
;
3932 xin
->resubmit_stats
= NULL
;
3933 xin
->skip_wildcards
= false;
3934 xin
->odp_actions
= NULL
;
3938 xlate_out_uninit(struct xlate_out
*xout
)
3940 if (xout
&& xout
->odp_actions
== &xout
->odp_actions_buf
) {
3941 ofpbuf_uninit(xout
->odp_actions
);
3945 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
3946 * into datapath actions, using 'ctx', and discards the datapath actions. */
3948 xlate_actions_for_side_effects(struct xlate_in
*xin
)
3950 struct xlate_out xout
;
3952 xlate_actions(xin
, &xout
);
3953 xlate_out_uninit(&xout
);
3957 xlate_report(struct xlate_ctx
*ctx
, const char *s
)
3959 if (ctx
->xin
->report_hook
) {
3960 ctx
->xin
->report_hook(ctx
->xin
, s
, ctx
->recurse
);
3965 xlate_out_copy(struct xlate_out
*dst
, const struct xlate_out
*src
)
3968 dst
->slow
= src
->slow
;
3969 dst
->has_learn
= src
->has_learn
;
3970 dst
->has_normal
= src
->has_normal
;
3971 dst
->has_fin_timeout
= src
->has_fin_timeout
;
3972 dst
->nf_output_iface
= src
->nf_output_iface
;
3973 dst
->mirrors
= src
->mirrors
;
3975 dst
->odp_actions
= &dst
->odp_actions_buf
;
3976 ofpbuf_use_stub(dst
->odp_actions
, dst
->odp_actions_stub
,
3977 sizeof dst
->odp_actions_stub
);
3978 ofpbuf_put(dst
->odp_actions
, ofpbuf_data(src
->odp_actions
),
3979 ofpbuf_size(src
->odp_actions
));
3982 static struct skb_priority_to_dscp
*
3983 get_skb_priority(const struct xport
*xport
, uint32_t skb_priority
)
3985 struct skb_priority_to_dscp
*pdscp
;
3988 hash
= hash_int(skb_priority
, 0);
3989 HMAP_FOR_EACH_IN_BUCKET (pdscp
, hmap_node
, hash
, &xport
->skb_priorities
) {
3990 if (pdscp
->skb_priority
== skb_priority
) {
3998 dscp_from_skb_priority(const struct xport
*xport
, uint32_t skb_priority
,
4001 struct skb_priority_to_dscp
*pdscp
= get_skb_priority(xport
, skb_priority
);
4002 *dscp
= pdscp
? pdscp
->dscp
: 0;
4003 return pdscp
!= NULL
;
4007 clear_skb_priorities(struct xport
*xport
)
4009 struct skb_priority_to_dscp
*pdscp
, *next
;
4011 HMAP_FOR_EACH_SAFE (pdscp
, next
, hmap_node
, &xport
->skb_priorities
) {
4012 hmap_remove(&xport
->skb_priorities
, &pdscp
->hmap_node
);
4018 actions_output_to_local_port(const struct xlate_ctx
*ctx
)
4020 odp_port_t local_odp_port
= ofp_port_to_odp_port(ctx
->xbridge
, OFPP_LOCAL
);
4021 const struct nlattr
*a
;
4024 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, ofpbuf_data(ctx
->xout
->odp_actions
),
4025 ofpbuf_size(ctx
->xout
->odp_actions
)) {
4026 if (nl_attr_type(a
) == OVS_ACTION_ATTR_OUTPUT
4027 && nl_attr_get_odp_port(a
) == local_odp_port
) {
4034 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
4035 * into datapath actions in 'odp_actions', using 'ctx'.
4037 * The caller must take responsibility for eventually freeing 'xout', with
4038 * xlate_out_uninit(). */
4040 xlate_actions(struct xlate_in
*xin
, struct xlate_out
*xout
)
4042 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
4043 struct flow_wildcards
*wc
= &xout
->wc
;
4044 struct flow
*flow
= &xin
->flow
;
4045 struct rule_dpif
*rule
= NULL
;
4047 const struct rule_actions
*actions
= NULL
;
4048 enum slow_path_reason special
;
4049 const struct ofpact
*ofpacts
;
4050 struct xport
*in_port
;
4051 struct flow orig_flow
;
4052 struct xlate_ctx ctx
;
4057 COVERAGE_INC(xlate_actions
);
4059 /* Flow initialization rules:
4060 * - 'base_flow' must match the kernel's view of the packet at the
4061 * time that action processing starts. 'flow' represents any
4062 * transformations we wish to make through actions.
4063 * - By default 'base_flow' and 'flow' are the same since the input
4064 * packet matches the output before any actions are applied.
4065 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
4066 * of the received packet as seen by the kernel. If we later output
4067 * to another device without any modifications this will cause us to
4068 * insert a new tag since the original one was stripped off by the
4070 * - Tunnel metadata as received is retained in 'flow'. This allows
4071 * tunnel metadata matching also in later tables.
4072 * Since a kernel action for setting the tunnel metadata will only be
4073 * generated with actual tunnel output, changing the tunnel metadata
4074 * values in 'flow' (such as tun_id) will only have effect with a later
4075 * tunnel output action.
4076 * - Tunnel 'base_flow' is completely cleared since that is what the
4077 * kernel does. If we wish to maintain the original values an action
4078 * needs to be generated. */
4083 ctx
.xout
->has_learn
= false;
4084 ctx
.xout
->has_normal
= false;
4085 ctx
.xout
->has_fin_timeout
= false;
4086 ctx
.xout
->nf_output_iface
= NF_OUT_DROP
;
4087 ctx
.xout
->mirrors
= 0;
4089 xout
->odp_actions
= xin
->odp_actions
;
4090 if (!xout
->odp_actions
) {
4091 xout
->odp_actions
= &xout
->odp_actions_buf
;
4092 ofpbuf_use_stub(xout
->odp_actions
, xout
->odp_actions_stub
,
4093 sizeof xout
->odp_actions_stub
);
4095 ofpbuf_reserve(xout
->odp_actions
, NL_A_U32_SIZE
);
4097 ctx
.xbridge
= xbridge_lookup(xcfg
, xin
->ofproto
);
4102 ctx
.rule
= xin
->rule
;
4104 ctx
.base_flow
= *flow
;
4105 memset(&ctx
.base_flow
.tunnel
, 0, sizeof ctx
.base_flow
.tunnel
);
4106 ctx
.orig_tunnel_ip_dst
= flow
->tunnel
.ip_dst
;
4108 flow_wildcards_init_catchall(wc
);
4109 memset(&wc
->masks
.in_port
, 0xff, sizeof wc
->masks
.in_port
);
4110 memset(&wc
->masks
.skb_priority
, 0xff, sizeof wc
->masks
.skb_priority
);
4111 memset(&wc
->masks
.dl_type
, 0xff, sizeof wc
->masks
.dl_type
);
4112 if (is_ip_any(flow
)) {
4113 wc
->masks
.nw_frag
|= FLOW_NW_FRAG_MASK
;
4115 is_icmp
= is_icmpv4(flow
) || is_icmpv6(flow
);
4117 tnl_may_send
= tnl_xlate_init(&ctx
.base_flow
, flow
, wc
);
4118 if (ctx
.xbridge
->netflow
) {
4119 netflow_mask_wc(flow
, wc
);
4124 ctx
.in_group
= false;
4125 ctx
.orig_skb_priority
= flow
->skb_priority
;
4128 ctx
.use_recirc
= false;
4129 ctx
.was_mpls
= false;
4131 if (!xin
->ofpacts
&& !ctx
.rule
) {
4132 ctx
.table_id
= rule_dpif_lookup(ctx
.xbridge
->ofproto
, flow
,
4133 !xin
->skip_wildcards
? wc
: NULL
,
4134 &rule
, ctx
.xin
->xcache
!= NULL
,
4135 ctx
.xin
->resubmit_stats
);
4136 if (ctx
.xin
->resubmit_stats
) {
4137 rule_dpif_credit_stats(rule
, ctx
.xin
->resubmit_stats
);
4139 if (ctx
.xin
->xcache
) {
4140 struct xc_entry
*entry
;
4142 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_RULE
);
4143 entry
->u
.rule
= rule
;
4147 xout
->fail_open
= ctx
.rule
&& rule_dpif_is_fail_open(ctx
.rule
);
4150 ofpacts
= xin
->ofpacts
;
4151 ofpacts_len
= xin
->ofpacts_len
;
4152 } else if (ctx
.rule
) {
4153 actions
= rule_dpif_get_actions(ctx
.rule
);
4154 ofpacts
= actions
->ofpacts
;
4155 ofpacts_len
= actions
->ofpacts_len
;
4160 ofpbuf_use_stub(&ctx
.stack
, ctx
.init_stack
, sizeof ctx
.init_stack
);
4161 ofpbuf_use_stub(&ctx
.action_set
,
4162 ctx
.action_set_stub
, sizeof ctx
.action_set_stub
);
4164 if (mbridge_has_mirrors(ctx
.xbridge
->mbridge
)) {
4165 /* Do this conditionally because the copy is expensive enough that it
4166 * shows up in profiles. */
4170 if (flow
->nw_frag
& FLOW_NW_FRAG_ANY
) {
4171 switch (ctx
.xbridge
->frag
) {
4172 case OFPC_FRAG_NORMAL
:
4173 /* We must pretend that transport ports are unavailable. */
4174 flow
->tp_src
= ctx
.base_flow
.tp_src
= htons(0);
4175 flow
->tp_dst
= ctx
.base_flow
.tp_dst
= htons(0);
4178 case OFPC_FRAG_DROP
:
4181 case OFPC_FRAG_REASM
:
4184 case OFPC_FRAG_NX_MATCH
:
4185 /* Nothing to do. */
4188 case OFPC_INVALID_TTL_TO_CONTROLLER
:
4193 in_port
= get_ofp_port(ctx
.xbridge
, flow
->in_port
.ofp_port
);
4194 if (in_port
&& in_port
->is_tunnel
) {
4195 if (ctx
.xin
->resubmit_stats
) {
4196 netdev_vport_inc_rx(in_port
->netdev
, ctx
.xin
->resubmit_stats
);
4198 bfd_account_rx(in_port
->bfd
, ctx
.xin
->resubmit_stats
);
4201 if (ctx
.xin
->xcache
) {
4202 struct xc_entry
*entry
;
4204 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_NETDEV
);
4205 entry
->u
.dev
.rx
= netdev_ref(in_port
->netdev
);
4206 entry
->u
.dev
.bfd
= bfd_ref(in_port
->bfd
);
4210 special
= process_special(&ctx
, flow
, in_port
, ctx
.xin
->packet
);
4212 ctx
.xout
->slow
|= special
;
4214 size_t sample_actions_len
;
4216 if (flow
->in_port
.ofp_port
4217 != vsp_realdev_to_vlandev(ctx
.xbridge
->ofproto
,
4218 flow
->in_port
.ofp_port
,
4220 ctx
.base_flow
.vlan_tci
= 0;
4223 add_sflow_action(&ctx
);
4224 add_ipfix_action(&ctx
);
4225 sample_actions_len
= ofpbuf_size(ctx
.xout
->odp_actions
);
4227 if (tnl_may_send
&& (!in_port
|| may_receive(in_port
, &ctx
))) {
4228 do_xlate_actions(ofpacts
, ofpacts_len
, &ctx
);
4230 /* We've let OFPP_NORMAL and the learning action look at the
4231 * packet, so drop it now if forwarding is disabled. */
4232 if (in_port
&& (!xport_stp_forward_state(in_port
) ||
4233 !xport_rstp_forward_state(in_port
))) {
4234 ofpbuf_set_size(ctx
.xout
->odp_actions
, sample_actions_len
);
4238 if (ofpbuf_size(&ctx
.action_set
)) {
4239 xlate_action_set(&ctx
);
4242 if (ctx
.xbridge
->has_in_band
4243 && in_band_must_output_to_local_port(flow
)
4244 && !actions_output_to_local_port(&ctx
)) {
4245 compose_output_action(&ctx
, OFPP_LOCAL
);
4248 fix_sflow_action(&ctx
);
4250 if (mbridge_has_mirrors(ctx
.xbridge
->mbridge
)) {
4251 add_mirror_actions(&ctx
, &orig_flow
);
4255 if (nl_attr_oversized(ofpbuf_size(ctx
.xout
->odp_actions
))) {
4256 /* These datapath actions are too big for a Netlink attribute, so we
4257 * can't hand them to the kernel directly. dpif_execute() can execute
4258 * them one by one with help, so just mark the result as SLOW_ACTION to
4259 * prevent the flow from being installed. */
4260 COVERAGE_INC(xlate_actions_oversize
);
4261 ctx
.xout
->slow
|= SLOW_ACTION
;
4264 if (mbridge_has_mirrors(ctx
.xbridge
->mbridge
)) {
4265 if (ctx
.xin
->resubmit_stats
) {
4266 mirror_update_stats(ctx
.xbridge
->mbridge
, xout
->mirrors
,
4267 ctx
.xin
->resubmit_stats
->n_packets
,
4268 ctx
.xin
->resubmit_stats
->n_bytes
);
4270 if (ctx
.xin
->xcache
) {
4271 struct xc_entry
*entry
;
4273 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_MIRROR
);
4274 entry
->u
.mirror
.mbridge
= mbridge_ref(ctx
.xbridge
->mbridge
);
4275 entry
->u
.mirror
.mirrors
= xout
->mirrors
;
4279 if (ctx
.xbridge
->netflow
) {
4280 /* Only update netflow if we don't have controller flow. We don't
4281 * report NetFlow expiration messages for such facets because they
4282 * are just part of the control logic for the network, not real
4284 if (ofpacts_len
== 0
4285 || ofpacts
->type
!= OFPACT_CONTROLLER
4286 || ofpact_next(ofpacts
) < ofpact_end(ofpacts
, ofpacts_len
)) {
4287 if (ctx
.xin
->resubmit_stats
) {
4288 netflow_flow_update(ctx
.xbridge
->netflow
, flow
,
4289 xout
->nf_output_iface
,
4290 ctx
.xin
->resubmit_stats
);
4292 if (ctx
.xin
->xcache
) {
4293 struct xc_entry
*entry
;
4295 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_NETFLOW
);
4296 entry
->u
.nf
.netflow
= netflow_ref(ctx
.xbridge
->netflow
);
4297 entry
->u
.nf
.flow
= xmemdup(flow
, sizeof *flow
);
4298 entry
->u
.nf
.iface
= xout
->nf_output_iface
;
4303 ofpbuf_uninit(&ctx
.stack
);
4304 ofpbuf_uninit(&ctx
.action_set
);
4306 /* Clear the metadata and register wildcard masks, because we won't
4307 * use non-header fields as part of the cache. */
4308 flow_wildcards_clear_non_packet_fields(wc
);
4310 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow uses
4311 * the low 8 bits of the 16-bit tp_src and tp_dst members to represent
4312 * these fields. The datapath interface, on the other hand, represents
4313 * them with just 8 bits each. This means that if the high 8 bits of the
4314 * masks for these fields somehow become set, then they will get chopped
4315 * off by a round trip through the datapath, and revalidation will spot
4316 * that as an inconsistency and delete the flow. Avoid the problem here by
4317 * making sure that only the low 8 bits of either field can be unwildcarded
4321 wc
->masks
.tp_src
&= htons(UINT8_MAX
);
4322 wc
->masks
.tp_dst
&= htons(UINT8_MAX
);
4326 /* Sends 'packet' out 'ofport'.
4327 * May modify 'packet'.
4328 * Returns 0 if successful, otherwise a positive errno value. */
4330 xlate_send_packet(const struct ofport_dpif
*ofport
, struct ofpbuf
*packet
)
4332 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
4333 struct xport
*xport
;
4334 struct ofpact_output output
;
4337 ofpact_init(&output
.ofpact
, OFPACT_OUTPUT
, sizeof output
);
4338 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
4339 flow_extract(packet
, NULL
, &flow
);
4340 flow
.in_port
.ofp_port
= OFPP_NONE
;
4342 xport
= xport_lookup(xcfg
, ofport
);
4346 output
.port
= xport
->ofp_port
;
4349 return ofproto_dpif_execute_actions(xport
->xbridge
->ofproto
, &flow
, NULL
,
4350 &output
.ofpact
, sizeof output
,
4354 struct xlate_cache
*
4355 xlate_cache_new(void)
4357 struct xlate_cache
*xcache
= xmalloc(sizeof *xcache
);
4359 ofpbuf_init(&xcache
->entries
, 512);
4363 static struct xc_entry
*
4364 xlate_cache_add_entry(struct xlate_cache
*xcache
, enum xc_type type
)
4366 struct xc_entry
*entry
;
4368 entry
= ofpbuf_put_zeros(&xcache
->entries
, sizeof *entry
);
4375 xlate_cache_netdev(struct xc_entry
*entry
, const struct dpif_flow_stats
*stats
)
4377 if (entry
->u
.dev
.tx
) {
4378 netdev_vport_inc_tx(entry
->u
.dev
.tx
, stats
);
4380 if (entry
->u
.dev
.rx
) {
4381 netdev_vport_inc_rx(entry
->u
.dev
.rx
, stats
);
4383 if (entry
->u
.dev
.bfd
) {
4384 bfd_account_rx(entry
->u
.dev
.bfd
, stats
);
4389 xlate_cache_normal(struct ofproto_dpif
*ofproto
, struct flow
*flow
, int vlan
)
4391 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
4392 struct xbridge
*xbridge
;
4393 struct xbundle
*xbundle
;
4394 struct flow_wildcards wc
;
4396 xbridge
= xbridge_lookup(xcfg
, ofproto
);
4401 xbundle
= lookup_input_bundle(xbridge
, flow
->in_port
.ofp_port
, false,
4407 update_learning_table(xbridge
, flow
, &wc
, vlan
, xbundle
);
4410 /* Push stats and perform side effects of flow translation. */
4412 xlate_push_stats(struct xlate_cache
*xcache
,
4413 const struct dpif_flow_stats
*stats
)
4415 struct xc_entry
*entry
;
4416 struct ofpbuf entries
= xcache
->entries
;
4418 if (!stats
->n_packets
) {
4422 XC_ENTRY_FOR_EACH (entry
, entries
, xcache
) {
4423 switch (entry
->type
) {
4425 rule_dpif_credit_stats(entry
->u
.rule
, stats
);
4428 bond_account(entry
->u
.bond
.bond
, entry
->u
.bond
.flow
,
4429 entry
->u
.bond
.vid
, stats
->n_bytes
);
4432 xlate_cache_netdev(entry
, stats
);
4435 netflow_flow_update(entry
->u
.nf
.netflow
, entry
->u
.nf
.flow
,
4436 entry
->u
.nf
.iface
, stats
);
4439 mirror_update_stats(entry
->u
.mirror
.mbridge
,
4440 entry
->u
.mirror
.mirrors
,
4441 stats
->n_packets
, stats
->n_bytes
);
4444 ofproto_dpif_flow_mod(entry
->u
.learn
.ofproto
, entry
->u
.learn
.fm
);
4447 xlate_cache_normal(entry
->u
.normal
.ofproto
, entry
->u
.normal
.flow
,
4448 entry
->u
.normal
.vlan
);
4450 case XC_FIN_TIMEOUT
:
4451 xlate_fin_timeout__(entry
->u
.fin
.rule
, stats
->tcp_flags
,
4452 entry
->u
.fin
.idle
, entry
->u
.fin
.hard
);
4455 group_dpif_credit_stats(entry
->u
.group
.group
, entry
->u
.group
.bucket
,
4465 xlate_dev_unref(struct xc_entry
*entry
)
4467 if (entry
->u
.dev
.tx
) {
4468 netdev_close(entry
->u
.dev
.tx
);
4470 if (entry
->u
.dev
.rx
) {
4471 netdev_close(entry
->u
.dev
.rx
);
4473 if (entry
->u
.dev
.bfd
) {
4474 bfd_unref(entry
->u
.dev
.bfd
);
4479 xlate_cache_clear_netflow(struct netflow
*netflow
, struct flow
*flow
)
4481 netflow_flow_clear(netflow
, flow
);
4482 netflow_unref(netflow
);
4487 xlate_cache_clear(struct xlate_cache
*xcache
)
4489 struct xc_entry
*entry
;
4490 struct ofpbuf entries
;
4496 XC_ENTRY_FOR_EACH (entry
, entries
, xcache
) {
4497 switch (entry
->type
) {
4499 rule_dpif_unref(entry
->u
.rule
);
4502 free(entry
->u
.bond
.flow
);
4503 bond_unref(entry
->u
.bond
.bond
);
4506 xlate_dev_unref(entry
);
4509 xlate_cache_clear_netflow(entry
->u
.nf
.netflow
, entry
->u
.nf
.flow
);
4512 mbridge_unref(entry
->u
.mirror
.mbridge
);
4515 free(entry
->u
.learn
.fm
);
4516 ofpbuf_delete(entry
->u
.learn
.ofpacts
);
4519 free(entry
->u
.normal
.flow
);
4521 case XC_FIN_TIMEOUT
:
4522 /* 'u.fin.rule' is always already held as a XC_RULE, which
4523 * has already released it's reference above. */
4526 group_dpif_unref(entry
->u
.group
.group
);
4533 ofpbuf_clear(&xcache
->entries
);
4537 xlate_cache_delete(struct xlate_cache
*xcache
)
4539 xlate_cache_clear(xcache
);
4540 ofpbuf_uninit(&xcache
->entries
);