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 /* Maximum number of resubmit actions in a flow translation, whether they are
67 * recursive or not. */
68 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
71 struct hmap_node hmap_node
; /* Node in global 'xbridges' map. */
72 struct ofproto_dpif
*ofproto
; /* Key in global 'xbridges' map. */
74 struct list xbundles
; /* Owned xbundles. */
75 struct hmap xports
; /* Indexed by ofp_port. */
77 char *name
; /* Name used in log messages. */
78 struct dpif
*dpif
; /* Datapath interface. */
79 struct mac_learning
*ml
; /* Mac learning handle. */
80 struct mcast_snooping
*ms
; /* Multicast Snooping handle. */
81 struct mbridge
*mbridge
; /* Mirroring. */
82 struct dpif_sflow
*sflow
; /* SFlow handle, or null. */
83 struct dpif_ipfix
*ipfix
; /* Ipfix handle, or null. */
84 struct netflow
*netflow
; /* Netflow handle, or null. */
85 struct stp
*stp
; /* STP or null if disabled. */
87 /* Special rules installed by ofproto-dpif. */
88 struct rule_dpif
*miss_rule
;
89 struct rule_dpif
*no_packet_in_rule
;
91 enum ofp_config_flags frag
; /* Fragmentation handling. */
92 bool has_in_band
; /* Bridge has in band control? */
93 bool forward_bpdu
; /* Bridge forwards STP BPDUs? */
95 /* True if the datapath supports recirculation. */
98 /* True if the datapath supports variable-length
99 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
100 * False if the datapath supports only 8-byte (or shorter) userdata. */
101 bool variable_length_userdata
;
103 /* Number of MPLS label stack entries that the datapath supports
105 size_t max_mpls_depth
;
109 struct hmap_node hmap_node
; /* In global 'xbundles' map. */
110 struct ofbundle
*ofbundle
; /* Key in global 'xbundles' map. */
112 struct list list_node
; /* In parent 'xbridges' list. */
113 struct xbridge
*xbridge
; /* Parent xbridge. */
115 struct list xports
; /* Contains "struct xport"s. */
117 char *name
; /* Name used in log messages. */
118 struct bond
*bond
; /* Nonnull iff more than one port. */
119 struct lacp
*lacp
; /* LACP handle or null. */
121 enum port_vlan_mode vlan_mode
; /* VLAN mode. */
122 int vlan
; /* -1=trunk port, else a 12-bit VLAN ID. */
123 unsigned long *trunks
; /* Bitmap of trunked VLANs, if 'vlan' == -1.
124 * NULL if all VLANs are trunked. */
125 bool use_priority_tags
; /* Use 802.1p tag for frames in VLAN 0? */
126 bool floodable
; /* No port has OFPUTIL_PC_NO_FLOOD set? */
130 struct hmap_node hmap_node
; /* Node in global 'xports' map. */
131 struct ofport_dpif
*ofport
; /* Key in global 'xports map. */
133 struct hmap_node ofp_node
; /* Node in parent xbridge 'xports' map. */
134 ofp_port_t ofp_port
; /* Key in parent xbridge 'xports' map. */
136 odp_port_t odp_port
; /* Datapath port number or ODPP_NONE. */
138 struct list bundle_node
; /* In parent xbundle (if it exists). */
139 struct xbundle
*xbundle
; /* Parent xbundle or null. */
141 struct netdev
*netdev
; /* 'ofport''s netdev. */
143 struct xbridge
*xbridge
; /* Parent bridge. */
144 struct xport
*peer
; /* Patch port peer or null. */
146 enum ofputil_port_config config
; /* OpenFlow port configuration. */
147 enum ofputil_port_state state
; /* OpenFlow port state. */
148 int stp_port_no
; /* STP port number or -1 if not in use. */
150 struct hmap skb_priorities
; /* Map of 'skb_priority_to_dscp's. */
152 bool may_enable
; /* May be enabled in bonds. */
153 bool is_tunnel
; /* Is a tunnel port. */
155 struct cfm
*cfm
; /* CFM handle or null. */
156 struct bfd
*bfd
; /* BFD handle or null. */
160 struct xlate_in
*xin
;
161 struct xlate_out
*xout
;
163 const struct xbridge
*xbridge
;
165 /* Flow at the last commit. */
166 struct flow base_flow
;
168 /* Tunnel IP destination address as received. This is stored separately
169 * as the base_flow.tunnel is cleared on init to reflect the datapath
170 * behavior. Used to make sure not to send tunneled output to ourselves,
171 * which might lead to an infinite loop. This could happen easily
172 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
173 * actually set the tun_dst field. */
174 ovs_be32 orig_tunnel_ip_dst
;
176 /* Stack for the push and pop actions. Each stack element is of type
177 * "union mf_subvalue". */
178 union mf_subvalue init_stack
[1024 / sizeof(union mf_subvalue
)];
181 /* The rule that we are currently translating, or NULL. */
182 struct rule_dpif
*rule
;
184 /* Resubmit statistics, via xlate_table_action(). */
185 int recurse
; /* Current resubmit nesting depth. */
186 int resubmits
; /* Total number of resubmits. */
187 bool in_group
; /* Currently translating ofgroup, if true. */
189 uint32_t orig_skb_priority
; /* Priority when packet arrived. */
190 uint8_t table_id
; /* OpenFlow table ID where flow was found. */
191 uint32_t sflow_n_outputs
; /* Number of output ports. */
192 odp_port_t sflow_odp_port
; /* Output port for composing sFlow action. */
193 uint16_t user_cookie_offset
;/* Used for user_action_cookie fixup. */
194 bool exit
; /* No further actions should be processed. */
196 bool use_recirc
; /* Should generate recirc? */
197 struct xlate_recirc recirc
; /* Information used for generating
198 * recirculation actions */
200 /* OpenFlow 1.1+ action set.
202 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
203 * When translation is otherwise complete, ofpacts_execute_action_set()
204 * converts it to a set of "struct ofpact"s that can be translated into
205 * datapath actions. */
206 struct ofpbuf action_set
; /* Action set. */
207 uint64_t action_set_stub
[1024 / 8];
210 /* A controller may use OFPP_NONE as the ingress port to indicate that
211 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
212 * when an input bundle is needed for validation (e.g., mirroring or
213 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
214 * any 'port' structs, so care must be taken when dealing with it. */
215 static struct xbundle ofpp_none_bundle
= {
217 .vlan_mode
= PORT_VLAN_TRUNK
220 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
221 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
222 * traffic egressing the 'ofport' with that priority should be marked with. */
223 struct skb_priority_to_dscp
{
224 struct hmap_node hmap_node
; /* Node in 'ofport_dpif''s 'skb_priorities'. */
225 uint32_t skb_priority
; /* Priority of this queue (see struct flow). */
227 uint8_t dscp
; /* DSCP bits to mark outgoing traffic with. */
242 /* xlate_cache entries hold enough information to perform the side effects of
243 * xlate_actions() for a rule, without needing to perform rule translation
244 * from scratch. The primary usage of these is to submit statistics to objects
245 * that a flow relates to, although they may be used for other effects as well
246 * (for instance, refreshing hard timeouts for learned flows). */
250 struct rule_dpif
*rule
;
257 struct netflow
*netflow
;
262 struct mbridge
*mbridge
;
263 mirror_mask_t mirrors
;
271 struct ofproto_dpif
*ofproto
;
272 struct ofputil_flow_mod
*fm
;
273 struct ofpbuf
*ofpacts
;
276 struct ofproto_dpif
*ofproto
;
281 struct rule_dpif
*rule
;
286 struct group_dpif
*group
;
287 struct ofputil_bucket
*bucket
;
292 #define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
293 entries = xcache->entries; \
294 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
296 entry = ofpbuf_try_pull(&entries, sizeof *entry))
299 struct ofpbuf entries
;
302 /* Xlate config contains hash maps of all bridges, bundles and ports.
303 * Xcfgp contains the pointer to the current xlate configuration.
304 * When the main thread needs to change the configuration, it copies xcfgp to
305 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
306 * does not block handler and revalidator threads. */
308 struct hmap xbridges
;
309 struct hmap xbundles
;
312 static OVSRCU_TYPE(struct xlate_cfg
*) xcfgp
= OVSRCU_TYPE_INITIALIZER
;
313 static struct xlate_cfg
*new_xcfg
= NULL
;
315 static bool may_receive(const struct xport
*, struct xlate_ctx
*);
316 static void do_xlate_actions(const struct ofpact
*, size_t ofpacts_len
,
318 static void xlate_normal(struct xlate_ctx
*);
319 static void xlate_report(struct xlate_ctx
*, const char *);
320 static void xlate_table_action(struct xlate_ctx
*, ofp_port_t in_port
,
321 uint8_t table_id
, bool may_packet_in
,
322 bool honor_table_miss
);
323 static bool input_vid_is_valid(uint16_t vid
, struct xbundle
*, bool warn
);
324 static uint16_t input_vid_to_vlan(const struct xbundle
*, uint16_t vid
);
325 static void output_normal(struct xlate_ctx
*, const struct xbundle
*,
327 static void compose_output_action(struct xlate_ctx
*, ofp_port_t ofp_port
);
329 static struct xbridge
*xbridge_lookup(struct xlate_cfg
*,
330 const struct ofproto_dpif
*);
331 static struct xbundle
*xbundle_lookup(struct xlate_cfg
*,
332 const struct ofbundle
*);
333 static struct xport
*xport_lookup(struct xlate_cfg
*,
334 const struct ofport_dpif
*);
335 static struct xport
*get_ofp_port(const struct xbridge
*, ofp_port_t ofp_port
);
336 static struct skb_priority_to_dscp
*get_skb_priority(const struct xport
*,
337 uint32_t skb_priority
);
338 static void clear_skb_priorities(struct xport
*);
339 static bool dscp_from_skb_priority(const struct xport
*, uint32_t skb_priority
,
342 static struct xc_entry
*xlate_cache_add_entry(struct xlate_cache
*xc
,
344 static void xlate_xbridge_init(struct xlate_cfg
*, struct xbridge
*);
345 static void xlate_xbundle_init(struct xlate_cfg
*, struct xbundle
*);
346 static void xlate_xport_init(struct xlate_cfg
*, struct xport
*);
347 static void xlate_xbridge_set(struct xbridge
*xbridge
,
349 struct rule_dpif
*miss_rule
,
350 struct rule_dpif
*no_packet_in_rule
,
351 const struct mac_learning
*ml
, struct stp
*stp
,
352 const struct mcast_snooping
*ms
,
353 const struct mbridge
*mbridge
,
354 const struct dpif_sflow
*sflow
,
355 const struct dpif_ipfix
*ipfix
,
356 const struct netflow
*netflow
,
357 enum ofp_config_flags frag
,
358 bool forward_bpdu
, bool has_in_band
,
360 bool variable_length_userdata
,
361 size_t max_mpls_depth
);
362 static void xlate_xbundle_set(struct xbundle
*xbundle
,
363 enum port_vlan_mode vlan_mode
, int vlan
,
364 unsigned long *trunks
, bool use_priority_tags
,
365 const struct bond
*bond
, const struct lacp
*lacp
,
367 static void xlate_xport_set(struct xport
*xport
, odp_port_t odp_port
,
368 const struct netdev
*netdev
, const struct cfm
*cfm
,
369 const struct bfd
*bfd
, int stp_port_no
,
370 enum ofputil_port_config config
,
371 enum ofputil_port_state state
, bool is_tunnel
,
373 static void xlate_xbridge_remove(struct xlate_cfg
*, struct xbridge
*);
374 static void xlate_xbundle_remove(struct xlate_cfg
*, struct xbundle
*);
375 static void xlate_xport_remove(struct xlate_cfg
*, struct xport
*);
376 static void xlate_xbridge_copy(struct xbridge
*);
377 static void xlate_xbundle_copy(struct xbridge
*, struct xbundle
*);
378 static void xlate_xport_copy(struct xbridge
*, struct xbundle
*,
380 static void xlate_xcfg_free(struct xlate_cfg
*);
384 xlate_xbridge_init(struct xlate_cfg
*xcfg
, struct xbridge
*xbridge
)
386 list_init(&xbridge
->xbundles
);
387 hmap_init(&xbridge
->xports
);
388 hmap_insert(&xcfg
->xbridges
, &xbridge
->hmap_node
,
389 hash_pointer(xbridge
->ofproto
, 0));
393 xlate_xbundle_init(struct xlate_cfg
*xcfg
, struct xbundle
*xbundle
)
395 list_init(&xbundle
->xports
);
396 list_insert(&xbundle
->xbridge
->xbundles
, &xbundle
->list_node
);
397 hmap_insert(&xcfg
->xbundles
, &xbundle
->hmap_node
,
398 hash_pointer(xbundle
->ofbundle
, 0));
402 xlate_xport_init(struct xlate_cfg
*xcfg
, struct xport
*xport
)
404 hmap_init(&xport
->skb_priorities
);
405 hmap_insert(&xcfg
->xports
, &xport
->hmap_node
,
406 hash_pointer(xport
->ofport
, 0));
407 hmap_insert(&xport
->xbridge
->xports
, &xport
->ofp_node
,
408 hash_ofp_port(xport
->ofp_port
));
412 xlate_xbridge_set(struct xbridge
*xbridge
,
414 struct rule_dpif
*miss_rule
,
415 struct rule_dpif
*no_packet_in_rule
,
416 const struct mac_learning
*ml
, struct stp
*stp
,
417 const struct mcast_snooping
*ms
,
418 const struct mbridge
*mbridge
,
419 const struct dpif_sflow
*sflow
,
420 const struct dpif_ipfix
*ipfix
,
421 const struct netflow
*netflow
, enum ofp_config_flags frag
,
422 bool forward_bpdu
, bool has_in_band
,
424 bool variable_length_userdata
,
425 size_t max_mpls_depth
)
427 if (xbridge
->ml
!= ml
) {
428 mac_learning_unref(xbridge
->ml
);
429 xbridge
->ml
= mac_learning_ref(ml
);
432 if (xbridge
->ms
!= ms
) {
433 mcast_snooping_unref(xbridge
->ms
);
434 xbridge
->ms
= mcast_snooping_ref(ms
);
437 if (xbridge
->mbridge
!= mbridge
) {
438 mbridge_unref(xbridge
->mbridge
);
439 xbridge
->mbridge
= mbridge_ref(mbridge
);
442 if (xbridge
->sflow
!= sflow
) {
443 dpif_sflow_unref(xbridge
->sflow
);
444 xbridge
->sflow
= dpif_sflow_ref(sflow
);
447 if (xbridge
->ipfix
!= ipfix
) {
448 dpif_ipfix_unref(xbridge
->ipfix
);
449 xbridge
->ipfix
= dpif_ipfix_ref(ipfix
);
452 if (xbridge
->stp
!= stp
) {
453 stp_unref(xbridge
->stp
);
454 xbridge
->stp
= stp_ref(stp
);
457 if (xbridge
->netflow
!= netflow
) {
458 netflow_unref(xbridge
->netflow
);
459 xbridge
->netflow
= netflow_ref(netflow
);
462 xbridge
->dpif
= dpif
;
463 xbridge
->forward_bpdu
= forward_bpdu
;
464 xbridge
->has_in_band
= has_in_band
;
465 xbridge
->frag
= frag
;
466 xbridge
->miss_rule
= miss_rule
;
467 xbridge
->no_packet_in_rule
= no_packet_in_rule
;
468 xbridge
->enable_recirc
= enable_recirc
;
469 xbridge
->variable_length_userdata
= variable_length_userdata
;
470 xbridge
->max_mpls_depth
= max_mpls_depth
;
474 xlate_xbundle_set(struct xbundle
*xbundle
,
475 enum port_vlan_mode vlan_mode
, int vlan
,
476 unsigned long *trunks
, bool use_priority_tags
,
477 const struct bond
*bond
, const struct lacp
*lacp
,
480 ovs_assert(xbundle
->xbridge
);
482 xbundle
->vlan_mode
= vlan_mode
;
483 xbundle
->vlan
= vlan
;
484 xbundle
->trunks
= trunks
;
485 xbundle
->use_priority_tags
= use_priority_tags
;
486 xbundle
->floodable
= floodable
;
488 if (xbundle
->bond
!= bond
) {
489 bond_unref(xbundle
->bond
);
490 xbundle
->bond
= bond_ref(bond
);
493 if (xbundle
->lacp
!= lacp
) {
494 lacp_unref(xbundle
->lacp
);
495 xbundle
->lacp
= lacp_ref(lacp
);
500 xlate_xport_set(struct xport
*xport
, odp_port_t odp_port
,
501 const struct netdev
*netdev
, const struct cfm
*cfm
,
502 const struct bfd
*bfd
, int stp_port_no
,
503 enum ofputil_port_config config
, enum ofputil_port_state state
,
504 bool is_tunnel
, bool may_enable
)
506 xport
->config
= config
;
507 xport
->state
= state
;
508 xport
->stp_port_no
= stp_port_no
;
509 xport
->is_tunnel
= is_tunnel
;
510 xport
->may_enable
= may_enable
;
511 xport
->odp_port
= odp_port
;
513 if (xport
->cfm
!= cfm
) {
514 cfm_unref(xport
->cfm
);
515 xport
->cfm
= cfm_ref(cfm
);
518 if (xport
->bfd
!= bfd
) {
519 bfd_unref(xport
->bfd
);
520 xport
->bfd
= bfd_ref(bfd
);
523 if (xport
->netdev
!= netdev
) {
524 netdev_close(xport
->netdev
);
525 xport
->netdev
= netdev_ref(netdev
);
530 xlate_xbridge_copy(struct xbridge
*xbridge
)
532 struct xbundle
*xbundle
;
534 struct xbridge
*new_xbridge
= xzalloc(sizeof *xbridge
);
535 new_xbridge
->ofproto
= xbridge
->ofproto
;
536 new_xbridge
->name
= xstrdup(xbridge
->name
);
537 xlate_xbridge_init(new_xcfg
, new_xbridge
);
539 xlate_xbridge_set(new_xbridge
,
540 xbridge
->dpif
, xbridge
->miss_rule
,
541 xbridge
->no_packet_in_rule
, xbridge
->ml
, xbridge
->stp
,
542 xbridge
->ms
, xbridge
->mbridge
, xbridge
->sflow
,
543 xbridge
->ipfix
, xbridge
->netflow
, xbridge
->frag
,
544 xbridge
->forward_bpdu
, xbridge
->has_in_band
,
545 xbridge
->enable_recirc
, xbridge
->variable_length_userdata
,
546 xbridge
->max_mpls_depth
);
547 LIST_FOR_EACH (xbundle
, list_node
, &xbridge
->xbundles
) {
548 xlate_xbundle_copy(new_xbridge
, xbundle
);
551 /* Copy xports which are not part of a xbundle */
552 HMAP_FOR_EACH (xport
, ofp_node
, &xbridge
->xports
) {
553 if (!xport
->xbundle
) {
554 xlate_xport_copy(new_xbridge
, NULL
, xport
);
560 xlate_xbundle_copy(struct xbridge
*xbridge
, struct xbundle
*xbundle
)
563 struct xbundle
*new_xbundle
= xzalloc(sizeof *xbundle
);
564 new_xbundle
->ofbundle
= xbundle
->ofbundle
;
565 new_xbundle
->xbridge
= xbridge
;
566 new_xbundle
->name
= xstrdup(xbundle
->name
);
567 xlate_xbundle_init(new_xcfg
, new_xbundle
);
569 xlate_xbundle_set(new_xbundle
, xbundle
->vlan_mode
,
570 xbundle
->vlan
, xbundle
->trunks
,
571 xbundle
->use_priority_tags
, xbundle
->bond
, xbundle
->lacp
,
573 LIST_FOR_EACH (xport
, bundle_node
, &xbundle
->xports
) {
574 xlate_xport_copy(xbridge
, new_xbundle
, xport
);
579 xlate_xport_copy(struct xbridge
*xbridge
, struct xbundle
*xbundle
,
582 struct skb_priority_to_dscp
*pdscp
, *new_pdscp
;
583 struct xport
*new_xport
= xzalloc(sizeof *xport
);
584 new_xport
->ofport
= xport
->ofport
;
585 new_xport
->ofp_port
= xport
->ofp_port
;
586 new_xport
->xbridge
= xbridge
;
587 xlate_xport_init(new_xcfg
, new_xport
);
589 xlate_xport_set(new_xport
, xport
->odp_port
, xport
->netdev
, xport
->cfm
,
590 xport
->bfd
, xport
->stp_port_no
, xport
->config
, xport
->state
,
591 xport
->is_tunnel
, xport
->may_enable
);
594 struct xport
*peer
= xport_lookup(new_xcfg
, xport
->peer
->ofport
);
596 new_xport
->peer
= peer
;
597 new_xport
->peer
->peer
= new_xport
;
602 new_xport
->xbundle
= xbundle
;
603 list_insert(&new_xport
->xbundle
->xports
, &new_xport
->bundle_node
);
606 HMAP_FOR_EACH (pdscp
, hmap_node
, &xport
->skb_priorities
) {
607 new_pdscp
= xmalloc(sizeof *pdscp
);
608 new_pdscp
->skb_priority
= pdscp
->skb_priority
;
609 new_pdscp
->dscp
= pdscp
->dscp
;
610 hmap_insert(&new_xport
->skb_priorities
, &new_pdscp
->hmap_node
,
611 hash_int(new_pdscp
->skb_priority
, 0));
615 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
616 * configuration in xcfgp.
618 * This needs to be called after editing the xlate configuration.
620 * Functions that edit the new xlate configuration are
621 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
627 * edit_xlate_configuration();
629 * xlate_txn_commit(); */
631 xlate_txn_commit(void)
633 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
635 ovsrcu_set(&xcfgp
, new_xcfg
);
636 ovsrcu_postpone(xlate_xcfg_free
, xcfg
);
641 /* Copies the current xlate configuration in xcfgp to new_xcfg.
643 * This needs to be called prior to editing the xlate configuration. */
645 xlate_txn_start(void)
647 struct xbridge
*xbridge
;
648 struct xlate_cfg
*xcfg
;
650 ovs_assert(!new_xcfg
);
652 new_xcfg
= xmalloc(sizeof *new_xcfg
);
653 hmap_init(&new_xcfg
->xbridges
);
654 hmap_init(&new_xcfg
->xbundles
);
655 hmap_init(&new_xcfg
->xports
);
657 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
662 HMAP_FOR_EACH (xbridge
, hmap_node
, &xcfg
->xbridges
) {
663 xlate_xbridge_copy(xbridge
);
669 xlate_xcfg_free(struct xlate_cfg
*xcfg
)
671 struct xbridge
*xbridge
, *next_xbridge
;
677 HMAP_FOR_EACH_SAFE (xbridge
, next_xbridge
, hmap_node
, &xcfg
->xbridges
) {
678 xlate_xbridge_remove(xcfg
, xbridge
);
681 hmap_destroy(&xcfg
->xbridges
);
682 hmap_destroy(&xcfg
->xbundles
);
683 hmap_destroy(&xcfg
->xports
);
688 xlate_ofproto_set(struct ofproto_dpif
*ofproto
, const char *name
,
689 struct dpif
*dpif
, struct rule_dpif
*miss_rule
,
690 struct rule_dpif
*no_packet_in_rule
,
691 const struct mac_learning
*ml
, struct stp
*stp
,
692 const struct mcast_snooping
*ms
,
693 const struct mbridge
*mbridge
,
694 const struct dpif_sflow
*sflow
,
695 const struct dpif_ipfix
*ipfix
,
696 const struct netflow
*netflow
, enum ofp_config_flags frag
,
697 bool forward_bpdu
, bool has_in_band
,
699 bool variable_length_userdata
,
700 size_t max_mpls_depth
)
702 struct xbridge
*xbridge
;
704 ovs_assert(new_xcfg
);
706 xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
708 xbridge
= xzalloc(sizeof *xbridge
);
709 xbridge
->ofproto
= ofproto
;
711 xlate_xbridge_init(new_xcfg
, xbridge
);
715 xbridge
->name
= xstrdup(name
);
717 xlate_xbridge_set(xbridge
, dpif
, miss_rule
, no_packet_in_rule
, ml
, stp
,
718 ms
, mbridge
, sflow
, ipfix
, netflow
, frag
, forward_bpdu
,
719 has_in_band
, enable_recirc
, variable_length_userdata
,
724 xlate_xbridge_remove(struct xlate_cfg
*xcfg
, struct xbridge
*xbridge
)
726 struct xbundle
*xbundle
, *next_xbundle
;
727 struct xport
*xport
, *next_xport
;
733 HMAP_FOR_EACH_SAFE (xport
, next_xport
, ofp_node
, &xbridge
->xports
) {
734 xlate_xport_remove(xcfg
, xport
);
737 LIST_FOR_EACH_SAFE (xbundle
, next_xbundle
, list_node
, &xbridge
->xbundles
) {
738 xlate_xbundle_remove(xcfg
, xbundle
);
741 hmap_remove(&xcfg
->xbridges
, &xbridge
->hmap_node
);
742 mac_learning_unref(xbridge
->ml
);
743 mcast_snooping_unref(xbridge
->ms
);
744 mbridge_unref(xbridge
->mbridge
);
745 dpif_sflow_unref(xbridge
->sflow
);
746 dpif_ipfix_unref(xbridge
->ipfix
);
747 stp_unref(xbridge
->stp
);
748 hmap_destroy(&xbridge
->xports
);
754 xlate_remove_ofproto(struct ofproto_dpif
*ofproto
)
756 struct xbridge
*xbridge
;
758 ovs_assert(new_xcfg
);
760 xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
761 xlate_xbridge_remove(new_xcfg
, xbridge
);
765 xlate_bundle_set(struct ofproto_dpif
*ofproto
, struct ofbundle
*ofbundle
,
766 const char *name
, enum port_vlan_mode vlan_mode
, int vlan
,
767 unsigned long *trunks
, bool use_priority_tags
,
768 const struct bond
*bond
, const struct lacp
*lacp
,
771 struct xbundle
*xbundle
;
773 ovs_assert(new_xcfg
);
775 xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
777 xbundle
= xzalloc(sizeof *xbundle
);
778 xbundle
->ofbundle
= ofbundle
;
779 xbundle
->xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
781 xlate_xbundle_init(new_xcfg
, xbundle
);
785 xbundle
->name
= xstrdup(name
);
787 xlate_xbundle_set(xbundle
, vlan_mode
, vlan
, trunks
,
788 use_priority_tags
, bond
, lacp
, floodable
);
792 xlate_xbundle_remove(struct xlate_cfg
*xcfg
, struct xbundle
*xbundle
)
794 struct xport
*xport
, *next
;
800 LIST_FOR_EACH_SAFE (xport
, next
, bundle_node
, &xbundle
->xports
) {
801 list_remove(&xport
->bundle_node
);
802 xport
->xbundle
= NULL
;
805 hmap_remove(&xcfg
->xbundles
, &xbundle
->hmap_node
);
806 list_remove(&xbundle
->list_node
);
807 bond_unref(xbundle
->bond
);
808 lacp_unref(xbundle
->lacp
);
814 xlate_bundle_remove(struct ofbundle
*ofbundle
)
816 struct xbundle
*xbundle
;
818 ovs_assert(new_xcfg
);
820 xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
821 xlate_xbundle_remove(new_xcfg
, xbundle
);
825 xlate_ofport_set(struct ofproto_dpif
*ofproto
, struct ofbundle
*ofbundle
,
826 struct ofport_dpif
*ofport
, ofp_port_t ofp_port
,
827 odp_port_t odp_port
, const struct netdev
*netdev
,
828 const struct cfm
*cfm
, const struct bfd
*bfd
,
829 struct ofport_dpif
*peer
, int stp_port_no
,
830 const struct ofproto_port_queue
*qdscp_list
, size_t n_qdscp
,
831 enum ofputil_port_config config
,
832 enum ofputil_port_state state
, bool is_tunnel
,
838 ovs_assert(new_xcfg
);
840 xport
= xport_lookup(new_xcfg
, ofport
);
842 xport
= xzalloc(sizeof *xport
);
843 xport
->ofport
= ofport
;
844 xport
->xbridge
= xbridge_lookup(new_xcfg
, ofproto
);
845 xport
->ofp_port
= ofp_port
;
847 xlate_xport_init(new_xcfg
, xport
);
850 ovs_assert(xport
->ofp_port
== ofp_port
);
852 xlate_xport_set(xport
, odp_port
, netdev
, cfm
, bfd
, stp_port_no
, config
,
853 state
, is_tunnel
, may_enable
);
856 xport
->peer
->peer
= NULL
;
858 xport
->peer
= xport_lookup(new_xcfg
, peer
);
860 xport
->peer
->peer
= xport
;
863 if (xport
->xbundle
) {
864 list_remove(&xport
->bundle_node
);
866 xport
->xbundle
= xbundle_lookup(new_xcfg
, ofbundle
);
867 if (xport
->xbundle
) {
868 list_insert(&xport
->xbundle
->xports
, &xport
->bundle_node
);
871 clear_skb_priorities(xport
);
872 for (i
= 0; i
< n_qdscp
; i
++) {
873 struct skb_priority_to_dscp
*pdscp
;
874 uint32_t skb_priority
;
876 if (dpif_queue_to_priority(xport
->xbridge
->dpif
, qdscp_list
[i
].queue
,
881 pdscp
= xmalloc(sizeof *pdscp
);
882 pdscp
->skb_priority
= skb_priority
;
883 pdscp
->dscp
= (qdscp_list
[i
].dscp
<< 2) & IP_DSCP_MASK
;
884 hmap_insert(&xport
->skb_priorities
, &pdscp
->hmap_node
,
885 hash_int(pdscp
->skb_priority
, 0));
890 xlate_xport_remove(struct xlate_cfg
*xcfg
, struct xport
*xport
)
897 xport
->peer
->peer
= NULL
;
901 if (xport
->xbundle
) {
902 list_remove(&xport
->bundle_node
);
905 clear_skb_priorities(xport
);
906 hmap_destroy(&xport
->skb_priorities
);
908 hmap_remove(&xcfg
->xports
, &xport
->hmap_node
);
909 hmap_remove(&xport
->xbridge
->xports
, &xport
->ofp_node
);
911 netdev_close(xport
->netdev
);
912 cfm_unref(xport
->cfm
);
913 bfd_unref(xport
->bfd
);
918 xlate_ofport_remove(struct ofport_dpif
*ofport
)
922 ovs_assert(new_xcfg
);
924 xport
= xport_lookup(new_xcfg
, ofport
);
925 xlate_xport_remove(new_xcfg
, xport
);
928 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
929 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
930 * Optionally populates 'ofproto' with the ofproto_dpif, 'odp_in_port' with
931 * the datapath in_port, that 'packet' ingressed, and 'ipfix', 'sflow', and
932 * 'netflow' with the appropriate handles for those protocols if they're
933 * enabled. Caller is responsible for unrefing them.
935 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
936 * 'flow''s in_port to OFPP_NONE.
938 * This function does post-processing on data returned from
939 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
940 * of the upcall processing logic. In particular, if the extracted in_port is
941 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
942 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
943 * a VLAN header onto 'packet' (if it is nonnull).
945 * Similarly, this function also includes some logic to help with tunnels. It
946 * may modify 'flow' as necessary to make the tunneling implementation
947 * transparent to the upcall processing logic.
949 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
950 * or some other positive errno if there are other problems. */
952 xlate_receive(const struct dpif_backer
*backer
, struct ofpbuf
*packet
,
953 const struct nlattr
*key
, size_t key_len
, struct flow
*flow
,
954 struct ofproto_dpif
**ofproto
, struct dpif_ipfix
**ipfix
,
955 struct dpif_sflow
**sflow
, struct netflow
**netflow
,
956 odp_port_t
*odp_in_port
)
958 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
960 const struct xport
*xport
;
962 if (odp_flow_key_to_flow(key
, key_len
, flow
) == ODP_FIT_ERROR
) {
968 *odp_in_port
= flow
->in_port
.odp_port
;
971 xport
= xport_lookup(xcfg
, tnl_port_should_receive(flow
)
972 ? tnl_port_receive(flow
)
973 : odp_port_to_ofport(backer
, flow
->in_port
.odp_port
));
975 flow
->in_port
.ofp_port
= xport
? xport
->ofp_port
: OFPP_NONE
;
980 if (vsp_adjust_flow(xport
->xbridge
->ofproto
, flow
)) {
982 /* Make the packet resemble the flow, so that it gets sent to
983 * an OpenFlow controller properly, so that it looks correct
984 * for sFlow, and so that flow_extract() will get the correct
985 * vlan_tci if it is called on 'packet'. */
986 eth_push_vlan(packet
, htons(ETH_TYPE_VLAN
), flow
->vlan_tci
);
992 *ofproto
= xport
->xbridge
->ofproto
;
996 *ipfix
= dpif_ipfix_ref(xport
->xbridge
->ipfix
);
1000 *sflow
= dpif_sflow_ref(xport
->xbridge
->sflow
);
1004 *netflow
= netflow_ref(xport
->xbridge
->netflow
);
1009 static struct xbridge
*
1010 xbridge_lookup(struct xlate_cfg
*xcfg
, const struct ofproto_dpif
*ofproto
)
1012 struct hmap
*xbridges
;
1013 struct xbridge
*xbridge
;
1015 if (!ofproto
|| !xcfg
) {
1019 xbridges
= &xcfg
->xbridges
;
1021 HMAP_FOR_EACH_IN_BUCKET (xbridge
, hmap_node
, hash_pointer(ofproto
, 0),
1023 if (xbridge
->ofproto
== ofproto
) {
1030 static struct xbundle
*
1031 xbundle_lookup(struct xlate_cfg
*xcfg
, const struct ofbundle
*ofbundle
)
1033 struct hmap
*xbundles
;
1034 struct xbundle
*xbundle
;
1036 if (!ofbundle
|| !xcfg
) {
1040 xbundles
= &xcfg
->xbundles
;
1042 HMAP_FOR_EACH_IN_BUCKET (xbundle
, hmap_node
, hash_pointer(ofbundle
, 0),
1044 if (xbundle
->ofbundle
== ofbundle
) {
1051 static struct xport
*
1052 xport_lookup(struct xlate_cfg
*xcfg
, const struct ofport_dpif
*ofport
)
1054 struct hmap
*xports
;
1055 struct xport
*xport
;
1057 if (!ofport
|| !xcfg
) {
1061 xports
= &xcfg
->xports
;
1063 HMAP_FOR_EACH_IN_BUCKET (xport
, hmap_node
, hash_pointer(ofport
, 0),
1065 if (xport
->ofport
== ofport
) {
1072 static struct stp_port
*
1073 xport_get_stp_port(const struct xport
*xport
)
1075 return xport
->xbridge
->stp
&& xport
->stp_port_no
!= -1
1076 ? stp_get_port(xport
->xbridge
->stp
, xport
->stp_port_no
)
1081 xport_stp_learn_state(const struct xport
*xport
)
1083 struct stp_port
*sp
= xport_get_stp_port(xport
);
1084 return stp_learn_in_state(sp
? stp_port_get_state(sp
) : STP_DISABLED
);
1088 xport_stp_forward_state(const struct xport
*xport
)
1090 struct stp_port
*sp
= xport_get_stp_port(xport
);
1091 return stp_forward_in_state(sp
? stp_port_get_state(sp
) : STP_DISABLED
);
1095 xport_stp_listen_state(const struct xport
*xport
)
1097 struct stp_port
*sp
= xport_get_stp_port(xport
);
1098 return stp_listen_in_state(sp
? stp_port_get_state(sp
) : STP_DISABLED
);
1101 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1102 * were used to make the determination.*/
1104 stp_should_process_flow(const struct flow
*flow
, struct flow_wildcards
*wc
)
1106 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1107 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
1108 return is_stp(flow
);
1112 stp_process_packet(const struct xport
*xport
, const struct ofpbuf
*packet
)
1114 struct stp_port
*sp
= xport_get_stp_port(xport
);
1115 struct ofpbuf payload
= *packet
;
1116 struct eth_header
*eth
= ofpbuf_data(&payload
);
1118 /* Sink packets on ports that have STP disabled when the bridge has
1120 if (!sp
|| stp_port_get_state(sp
) == STP_DISABLED
) {
1124 /* Trim off padding on payload. */
1125 if (ofpbuf_size(&payload
) > ntohs(eth
->eth_type
) + ETH_HEADER_LEN
) {
1126 ofpbuf_set_size(&payload
, ntohs(eth
->eth_type
) + ETH_HEADER_LEN
);
1129 if (ofpbuf_try_pull(&payload
, ETH_HEADER_LEN
+ LLC_HEADER_LEN
)) {
1130 stp_received_bpdu(sp
, ofpbuf_data(&payload
), ofpbuf_size(&payload
));
1134 static struct xport
*
1135 get_ofp_port(const struct xbridge
*xbridge
, ofp_port_t ofp_port
)
1137 struct xport
*xport
;
1139 HMAP_FOR_EACH_IN_BUCKET (xport
, ofp_node
, hash_ofp_port(ofp_port
),
1141 if (xport
->ofp_port
== ofp_port
) {
1149 ofp_port_to_odp_port(const struct xbridge
*xbridge
, ofp_port_t ofp_port
)
1151 const struct xport
*xport
= get_ofp_port(xbridge
, ofp_port
);
1152 return xport
? xport
->odp_port
: ODPP_NONE
;
1156 odp_port_is_alive(const struct xlate_ctx
*ctx
, ofp_port_t ofp_port
)
1158 struct xport
*xport
;
1160 xport
= get_ofp_port(ctx
->xbridge
, ofp_port
);
1161 if (!xport
|| xport
->config
& OFPUTIL_PC_PORT_DOWN
||
1162 xport
->state
& OFPUTIL_PS_LINK_DOWN
) {
1169 static struct ofputil_bucket
*
1170 group_first_live_bucket(const struct xlate_ctx
*, const struct group_dpif
*,
1174 group_is_alive(const struct xlate_ctx
*ctx
, uint32_t group_id
, int depth
)
1176 struct group_dpif
*group
;
1178 if (group_dpif_lookup(ctx
->xbridge
->ofproto
, group_id
, &group
)) {
1179 struct ofputil_bucket
*bucket
;
1181 bucket
= group_first_live_bucket(ctx
, group
, depth
);
1182 group_dpif_unref(group
);
1183 return bucket
== NULL
;
1189 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1192 bucket_is_alive(const struct xlate_ctx
*ctx
,
1193 struct ofputil_bucket
*bucket
, int depth
)
1195 if (depth
>= MAX_LIVENESS_RECURSION
) {
1196 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
1198 VLOG_WARN_RL(&rl
, "bucket chaining exceeded %d links",
1199 MAX_LIVENESS_RECURSION
);
1203 return (!ofputil_bucket_has_liveness(bucket
)
1204 || (bucket
->watch_port
!= OFPP_ANY
1205 && odp_port_is_alive(ctx
, bucket
->watch_port
))
1206 || (bucket
->watch_group
!= OFPG_ANY
1207 && group_is_alive(ctx
, bucket
->watch_group
, depth
+ 1)));
1210 static struct ofputil_bucket
*
1211 group_first_live_bucket(const struct xlate_ctx
*ctx
,
1212 const struct group_dpif
*group
, int depth
)
1214 struct ofputil_bucket
*bucket
;
1215 const struct list
*buckets
;
1217 group_dpif_get_buckets(group
, &buckets
);
1218 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
1219 if (bucket_is_alive(ctx
, bucket
, depth
)) {
1227 static struct ofputil_bucket
*
1228 group_best_live_bucket(const struct xlate_ctx
*ctx
,
1229 const struct group_dpif
*group
,
1232 struct ofputil_bucket
*best_bucket
= NULL
;
1233 uint32_t best_score
= 0;
1236 struct ofputil_bucket
*bucket
;
1237 const struct list
*buckets
;
1239 group_dpif_get_buckets(group
, &buckets
);
1240 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
1241 if (bucket_is_alive(ctx
, bucket
, 0)) {
1242 uint32_t score
= (hash_int(i
, basis
) & 0xffff) * bucket
->weight
;
1243 if (score
>= best_score
) {
1244 best_bucket
= bucket
;
1255 xbundle_trunks_vlan(const struct xbundle
*bundle
, uint16_t vlan
)
1257 return (bundle
->vlan_mode
!= PORT_VLAN_ACCESS
1258 && (!bundle
->trunks
|| bitmap_is_set(bundle
->trunks
, vlan
)));
1262 xbundle_includes_vlan(const struct xbundle
*xbundle
, uint16_t vlan
)
1264 return vlan
== xbundle
->vlan
|| xbundle_trunks_vlan(xbundle
, vlan
);
1267 static mirror_mask_t
1268 xbundle_mirror_out(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1270 return xbundle
!= &ofpp_none_bundle
1271 ? mirror_bundle_out(xbridge
->mbridge
, xbundle
->ofbundle
)
1275 static mirror_mask_t
1276 xbundle_mirror_src(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1278 return xbundle
!= &ofpp_none_bundle
1279 ? mirror_bundle_src(xbridge
->mbridge
, xbundle
->ofbundle
)
1283 static mirror_mask_t
1284 xbundle_mirror_dst(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
1286 return xbundle
!= &ofpp_none_bundle
1287 ? mirror_bundle_dst(xbridge
->mbridge
, xbundle
->ofbundle
)
1291 static struct xbundle
*
1292 lookup_input_bundle(const struct xbridge
*xbridge
, ofp_port_t in_port
,
1293 bool warn
, struct xport
**in_xportp
)
1295 struct xport
*xport
;
1297 /* Find the port and bundle for the received packet. */
1298 xport
= get_ofp_port(xbridge
, in_port
);
1302 if (xport
&& xport
->xbundle
) {
1303 return xport
->xbundle
;
1306 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1307 * which a controller may use as the ingress port for traffic that
1308 * it is sourcing. */
1309 if (in_port
== OFPP_CONTROLLER
|| in_port
== OFPP_NONE
) {
1310 return &ofpp_none_bundle
;
1313 /* Odd. A few possible reasons here:
1315 * - We deleted a port but there are still a few packets queued up
1318 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1319 * we don't know about.
1321 * - The ofproto client didn't configure the port as part of a bundle.
1322 * This is particularly likely to happen if a packet was received on the
1323 * port after it was created, but before the client had a chance to
1324 * configure its bundle.
1327 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1329 VLOG_WARN_RL(&rl
, "bridge %s: received packet on unknown "
1330 "port %"PRIu16
, xbridge
->name
, in_port
);
1336 add_mirror_actions(struct xlate_ctx
*ctx
, const struct flow
*orig_flow
)
1338 const struct xbridge
*xbridge
= ctx
->xbridge
;
1339 mirror_mask_t mirrors
;
1340 struct xbundle
*in_xbundle
;
1344 mirrors
= ctx
->xout
->mirrors
;
1345 ctx
->xout
->mirrors
= 0;
1347 in_xbundle
= lookup_input_bundle(xbridge
, orig_flow
->in_port
.ofp_port
,
1348 ctx
->xin
->packet
!= NULL
, NULL
);
1352 mirrors
|= xbundle_mirror_src(xbridge
, in_xbundle
);
1354 /* Drop frames on bundles reserved for mirroring. */
1355 if (xbundle_mirror_out(xbridge
, in_xbundle
)) {
1356 if (ctx
->xin
->packet
!= NULL
) {
1357 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1358 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet received on port "
1359 "%s, which is reserved exclusively for mirroring",
1360 ctx
->xbridge
->name
, in_xbundle
->name
);
1362 ofpbuf_clear(&ctx
->xout
->odp_actions
);
1367 vid
= vlan_tci_to_vid(orig_flow
->vlan_tci
);
1368 if (!input_vid_is_valid(vid
, in_xbundle
, ctx
->xin
->packet
!= NULL
)) {
1371 vlan
= input_vid_to_vlan(in_xbundle
, vid
);
1377 /* Restore the original packet before adding the mirror actions. */
1378 ctx
->xin
->flow
= *orig_flow
;
1381 mirror_mask_t dup_mirrors
;
1382 struct ofbundle
*out
;
1383 unsigned long *vlans
;
1388 has_mirror
= mirror_get(xbridge
->mbridge
, raw_ctz(mirrors
),
1389 &vlans
, &dup_mirrors
, &out
, &out_vlan
);
1390 ovs_assert(has_mirror
);
1393 ctx
->xout
->wc
.masks
.vlan_tci
|= htons(VLAN_CFI
| VLAN_VID_MASK
);
1395 vlan_mirrored
= !vlans
|| bitmap_is_set(vlans
, vlan
);
1398 if (!vlan_mirrored
) {
1399 mirrors
= zero_rightmost_1bit(mirrors
);
1403 mirrors
&= ~dup_mirrors
;
1404 ctx
->xout
->mirrors
|= dup_mirrors
;
1406 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1407 struct xbundle
*out_xbundle
= xbundle_lookup(xcfg
, out
);
1409 output_normal(ctx
, out_xbundle
, vlan
);
1411 } else if (vlan
!= out_vlan
1412 && !eth_addr_is_reserved(orig_flow
->dl_dst
)) {
1413 struct xbundle
*xbundle
;
1415 LIST_FOR_EACH (xbundle
, list_node
, &xbridge
->xbundles
) {
1416 if (xbundle_includes_vlan(xbundle
, out_vlan
)
1417 && !xbundle_mirror_out(xbridge
, xbundle
)) {
1418 output_normal(ctx
, xbundle
, out_vlan
);
1425 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1426 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1427 * the bundle on which the packet was received, returns the VLAN to which the
1430 * Both 'vid' and the return value are in the range 0...4095. */
1432 input_vid_to_vlan(const struct xbundle
*in_xbundle
, uint16_t vid
)
1434 switch (in_xbundle
->vlan_mode
) {
1435 case PORT_VLAN_ACCESS
:
1436 return in_xbundle
->vlan
;
1439 case PORT_VLAN_TRUNK
:
1442 case PORT_VLAN_NATIVE_UNTAGGED
:
1443 case PORT_VLAN_NATIVE_TAGGED
:
1444 return vid
? vid
: in_xbundle
->vlan
;
1451 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1452 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1455 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1456 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1459 input_vid_is_valid(uint16_t vid
, struct xbundle
*in_xbundle
, bool warn
)
1461 /* Allow any VID on the OFPP_NONE port. */
1462 if (in_xbundle
== &ofpp_none_bundle
) {
1466 switch (in_xbundle
->vlan_mode
) {
1467 case PORT_VLAN_ACCESS
:
1470 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1471 VLOG_WARN_RL(&rl
, "dropping VLAN %"PRIu16
" tagged "
1472 "packet received on port %s configured as VLAN "
1473 "%"PRIu16
" access port", vid
, in_xbundle
->name
,
1480 case PORT_VLAN_NATIVE_UNTAGGED
:
1481 case PORT_VLAN_NATIVE_TAGGED
:
1483 /* Port must always carry its native VLAN. */
1487 case PORT_VLAN_TRUNK
:
1488 if (!xbundle_includes_vlan(in_xbundle
, vid
)) {
1490 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1491 VLOG_WARN_RL(&rl
, "dropping VLAN %"PRIu16
" packet "
1492 "received on port %s not configured for trunking "
1493 "VLAN %"PRIu16
, vid
, in_xbundle
->name
, vid
);
1505 /* Given 'vlan', the VLAN that a packet belongs to, and
1506 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1507 * that should be included in the 802.1Q header. (If the return value is 0,
1508 * then the 802.1Q header should only be included in the packet if there is a
1511 * Both 'vlan' and the return value are in the range 0...4095. */
1513 output_vlan_to_vid(const struct xbundle
*out_xbundle
, uint16_t vlan
)
1515 switch (out_xbundle
->vlan_mode
) {
1516 case PORT_VLAN_ACCESS
:
1519 case PORT_VLAN_TRUNK
:
1520 case PORT_VLAN_NATIVE_TAGGED
:
1523 case PORT_VLAN_NATIVE_UNTAGGED
:
1524 return vlan
== out_xbundle
->vlan
? 0 : vlan
;
1532 output_normal(struct xlate_ctx
*ctx
, const struct xbundle
*out_xbundle
,
1535 ovs_be16
*flow_tci
= &ctx
->xin
->flow
.vlan_tci
;
1537 ovs_be16 tci
, old_tci
;
1538 struct xport
*xport
;
1540 vid
= output_vlan_to_vid(out_xbundle
, vlan
);
1541 if (list_is_empty(&out_xbundle
->xports
)) {
1542 /* Partially configured bundle with no slaves. Drop the packet. */
1544 } else if (!out_xbundle
->bond
) {
1545 ctx
->use_recirc
= false;
1546 xport
= CONTAINER_OF(list_front(&out_xbundle
->xports
), struct xport
,
1549 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1550 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
1551 struct xlate_recirc
*xr
= &ctx
->recirc
;
1552 struct ofport_dpif
*ofport
;
1554 if (ctx
->xbridge
->enable_recirc
) {
1555 ctx
->use_recirc
= bond_may_recirc(
1556 out_xbundle
->bond
, &xr
->recirc_id
, &xr
->hash_basis
);
1558 if (ctx
->use_recirc
) {
1559 /* Only TCP mode uses recirculation. */
1560 xr
->hash_alg
= OVS_HASH_ALG_L4
;
1561 bond_update_post_recirc_rules(out_xbundle
->bond
, false);
1563 /* Recirculation does not require unmasking hash fields. */
1568 ofport
= bond_choose_output_slave(out_xbundle
->bond
,
1569 &ctx
->xin
->flow
, wc
, vid
);
1570 xport
= xport_lookup(xcfg
, ofport
);
1573 /* No slaves enabled, so drop packet. */
1577 /* If ctx->xout->use_recirc is set, the main thread will handle stats
1578 * accounting for this bond. */
1579 if (!ctx
->use_recirc
) {
1580 if (ctx
->xin
->resubmit_stats
) {
1581 bond_account(out_xbundle
->bond
, &ctx
->xin
->flow
, vid
,
1582 ctx
->xin
->resubmit_stats
->n_bytes
);
1584 if (ctx
->xin
->xcache
) {
1585 struct xc_entry
*entry
;
1588 flow
= &ctx
->xin
->flow
;
1589 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_BOND
);
1590 entry
->u
.bond
.bond
= bond_ref(out_xbundle
->bond
);
1591 entry
->u
.bond
.flow
= xmemdup(flow
, sizeof *flow
);
1592 entry
->u
.bond
.vid
= vid
;
1597 old_tci
= *flow_tci
;
1599 if (tci
|| out_xbundle
->use_priority_tags
) {
1600 tci
|= *flow_tci
& htons(VLAN_PCP_MASK
);
1602 tci
|= htons(VLAN_CFI
);
1607 compose_output_action(ctx
, xport
->ofp_port
);
1608 *flow_tci
= old_tci
;
1611 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1612 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1613 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1615 is_gratuitous_arp(const struct flow
*flow
, struct flow_wildcards
*wc
)
1617 if (flow
->dl_type
!= htons(ETH_TYPE_ARP
)) {
1621 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
1622 if (!eth_addr_is_broadcast(flow
->dl_dst
)) {
1626 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
1627 if (flow
->nw_proto
== ARP_OP_REPLY
) {
1629 } else if (flow
->nw_proto
== ARP_OP_REQUEST
) {
1630 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
1631 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
1633 return flow
->nw_src
== flow
->nw_dst
;
1639 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1640 * dropped. Returns true if they may be forwarded, false if they should be
1643 * 'in_port' must be the xport that corresponds to flow->in_port.
1644 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1646 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1647 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1648 * checked by input_vid_is_valid().
1650 * May also add tags to '*tags', although the current implementation only does
1651 * so in one special case.
1654 is_admissible(struct xlate_ctx
*ctx
, struct xport
*in_port
,
1657 struct xbundle
*in_xbundle
= in_port
->xbundle
;
1658 const struct xbridge
*xbridge
= ctx
->xbridge
;
1659 struct flow
*flow
= &ctx
->xin
->flow
;
1661 /* Drop frames for reserved multicast addresses
1662 * only if forward_bpdu option is absent. */
1663 if (!xbridge
->forward_bpdu
&& eth_addr_is_reserved(flow
->dl_dst
)) {
1664 xlate_report(ctx
, "packet has reserved destination MAC, dropping");
1668 if (in_xbundle
->bond
) {
1669 struct mac_entry
*mac
;
1671 switch (bond_check_admissibility(in_xbundle
->bond
, in_port
->ofport
,
1677 xlate_report(ctx
, "bonding refused admissibility, dropping");
1680 case BV_DROP_IF_MOVED
:
1681 ovs_rwlock_rdlock(&xbridge
->ml
->rwlock
);
1682 mac
= mac_learning_lookup(xbridge
->ml
, flow
->dl_src
, vlan
);
1683 if (mac
&& mac
->port
.p
!= in_xbundle
->ofbundle
&&
1684 (!is_gratuitous_arp(flow
, &ctx
->xout
->wc
)
1685 || mac_entry_is_grat_arp_locked(mac
))) {
1686 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
1687 xlate_report(ctx
, "SLB bond thinks this packet looped back, "
1691 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
1699 /* Checks whether a MAC learning update is necessary for MAC learning table
1700 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1703 * Most packets processed through the MAC learning table do not actually
1704 * change it in any way. This function requires only a read lock on the MAC
1705 * learning table, so it is much cheaper in this common case.
1707 * Keep the code here synchronized with that in update_learning_table__()
1710 is_mac_learning_update_needed(const struct mac_learning
*ml
,
1711 const struct flow
*flow
,
1712 struct flow_wildcards
*wc
,
1713 int vlan
, struct xbundle
*in_xbundle
)
1714 OVS_REQ_RDLOCK(ml
->rwlock
)
1716 struct mac_entry
*mac
;
1718 if (!mac_learning_may_learn(ml
, flow
->dl_src
, vlan
)) {
1722 mac
= mac_learning_lookup(ml
, flow
->dl_src
, vlan
);
1723 if (!mac
|| mac_entry_age(ml
, mac
)) {
1727 if (is_gratuitous_arp(flow
, wc
)) {
1728 /* We don't want to learn from gratuitous ARP packets that are
1729 * reflected back over bond slaves so we lock the learning table. */
1730 if (!in_xbundle
->bond
) {
1732 } else if (mac_entry_is_grat_arp_locked(mac
)) {
1737 return mac
->port
.p
!= in_xbundle
->ofbundle
;
1741 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1742 * received on 'in_xbundle' in 'vlan'.
1744 * This code repeats all the checks in is_mac_learning_update_needed() because
1745 * the lock was released between there and here and thus the MAC learning state
1746 * could have changed.
1748 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1751 update_learning_table__(const struct xbridge
*xbridge
,
1752 const struct flow
*flow
, struct flow_wildcards
*wc
,
1753 int vlan
, struct xbundle
*in_xbundle
)
1754 OVS_REQ_WRLOCK(xbridge
->ml
->rwlock
)
1756 struct mac_entry
*mac
;
1758 if (!mac_learning_may_learn(xbridge
->ml
, flow
->dl_src
, vlan
)) {
1762 mac
= mac_learning_insert(xbridge
->ml
, flow
->dl_src
, vlan
);
1763 if (is_gratuitous_arp(flow
, wc
)) {
1764 /* We don't want to learn from gratuitous ARP packets that are
1765 * reflected back over bond slaves so we lock the learning table. */
1766 if (!in_xbundle
->bond
) {
1767 mac_entry_set_grat_arp_lock(mac
);
1768 } else if (mac_entry_is_grat_arp_locked(mac
)) {
1773 if (mac
->port
.p
!= in_xbundle
->ofbundle
) {
1774 /* The log messages here could actually be useful in debugging,
1775 * so keep the rate limit relatively high. */
1776 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(30, 300);
1778 VLOG_DBG_RL(&rl
, "bridge %s: learned that "ETH_ADDR_FMT
" is "
1779 "on port %s in VLAN %d",
1780 xbridge
->name
, ETH_ADDR_ARGS(flow
->dl_src
),
1781 in_xbundle
->name
, vlan
);
1783 mac
->port
.p
= in_xbundle
->ofbundle
;
1784 mac_learning_changed(xbridge
->ml
);
1789 update_learning_table(const struct xbridge
*xbridge
,
1790 const struct flow
*flow
, struct flow_wildcards
*wc
,
1791 int vlan
, struct xbundle
*in_xbundle
)
1795 /* Don't learn the OFPP_NONE port. */
1796 if (in_xbundle
== &ofpp_none_bundle
) {
1800 /* First try the common case: no change to MAC learning table. */
1801 ovs_rwlock_rdlock(&xbridge
->ml
->rwlock
);
1802 need_update
= is_mac_learning_update_needed(xbridge
->ml
, flow
, wc
, vlan
,
1804 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
1807 /* Slow path: MAC learning table might need an update. */
1808 ovs_rwlock_wrlock(&xbridge
->ml
->rwlock
);
1809 update_learning_table__(xbridge
, flow
, wc
, vlan
, in_xbundle
);
1810 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
1814 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1815 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
1817 update_mcast_snooping_table__(const struct xbridge
*xbridge
,
1818 const struct flow
*flow
,
1819 struct mcast_snooping
*ms
,
1820 ovs_be32 ip4
, int vlan
,
1821 struct xbundle
*in_xbundle
)
1822 OVS_REQ_WRLOCK(ms
->rwlock
)
1824 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(60, 30);
1826 switch (ntohs(flow
->tp_src
)) {
1827 case IGMP_HOST_MEMBERSHIP_REPORT
:
1828 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1829 if (mcast_snooping_add_group(ms
, ip4
, vlan
, in_xbundle
->ofbundle
)) {
1830 VLOG_DBG_RL(&rl
, "bridge %s: multicast snooping learned that "
1831 IP_FMT
" is on port %s in VLAN %d",
1832 xbridge
->name
, IP_ARGS(ip4
), in_xbundle
->name
, vlan
);
1835 case IGMP_HOST_LEAVE_MESSAGE
:
1836 if (mcast_snooping_leave_group(ms
, ip4
, vlan
, in_xbundle
->ofbundle
)) {
1837 VLOG_DBG_RL(&rl
, "bridge %s: multicast snooping leaving "
1838 IP_FMT
" is on port %s in VLAN %d",
1839 xbridge
->name
, IP_ARGS(ip4
), in_xbundle
->name
, vlan
);
1842 case IGMP_HOST_MEMBERSHIP_QUERY
:
1843 if (flow
->nw_src
&& mcast_snooping_add_mrouter(ms
, vlan
,
1844 in_xbundle
->ofbundle
)) {
1845 VLOG_DBG_RL(&rl
, "bridge %s: multicast snooping query from "
1846 IP_FMT
" is on port %s in VLAN %d",
1847 xbridge
->name
, IP_ARGS(flow
->nw_src
),
1848 in_xbundle
->name
, vlan
);
1854 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1855 * was received on 'in_xbundle' in 'vlan'. */
1857 update_mcast_snooping_table(const struct xbridge
*xbridge
,
1858 const struct flow
*flow
, int vlan
,
1859 struct xbundle
*in_xbundle
)
1861 struct mcast_snooping
*ms
= xbridge
->ms
;
1862 struct xlate_cfg
*xcfg
;
1863 struct xbundle
*mcast_xbundle
;
1864 struct mcast_fport_bundle
*fport
;
1866 /* Don't learn the OFPP_NONE port. */
1867 if (in_xbundle
== &ofpp_none_bundle
) {
1871 /* Don't learn from flood ports */
1872 mcast_xbundle
= NULL
;
1873 ovs_rwlock_wrlock(&ms
->rwlock
);
1874 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1875 LIST_FOR_EACH(fport
, fport_node
, &ms
->fport_list
) {
1876 mcast_xbundle
= xbundle_lookup(xcfg
, fport
->port
);
1877 if (mcast_xbundle
== in_xbundle
) {
1882 if (!mcast_xbundle
|| mcast_xbundle
!= in_xbundle
) {
1883 update_mcast_snooping_table__(xbridge
, flow
, ms
, flow
->igmp_group_ip4
,
1886 ovs_rwlock_unlock(&ms
->rwlock
);
1889 /* send the packet to ports having the multicast group learned */
1891 xlate_normal_mcast_send_group(struct xlate_ctx
*ctx
,
1892 struct mcast_snooping
*ms OVS_UNUSED
,
1893 struct mcast_group
*grp
,
1894 struct xbundle
*in_xbundle
, uint16_t vlan
)
1895 OVS_REQ_RDLOCK(ms
->rwlock
)
1897 struct xlate_cfg
*xcfg
;
1898 struct mcast_group_bundle
*b
;
1899 struct xbundle
*mcast_xbundle
;
1901 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1902 LIST_FOR_EACH(b
, bundle_node
, &grp
->bundle_lru
) {
1903 mcast_xbundle
= xbundle_lookup(xcfg
, b
->port
);
1904 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
) {
1905 xlate_report(ctx
, "forwarding to mcast group port");
1906 output_normal(ctx
, mcast_xbundle
, vlan
);
1907 } else if (!mcast_xbundle
) {
1908 xlate_report(ctx
, "mcast group port is unknown, dropping");
1910 xlate_report(ctx
, "mcast group port is input port, dropping");
1915 /* send the packet to ports connected to multicast routers */
1917 xlate_normal_mcast_send_mrouters(struct xlate_ctx
*ctx
,
1918 struct mcast_snooping
*ms
,
1919 struct xbundle
*in_xbundle
, uint16_t vlan
)
1920 OVS_REQ_RDLOCK(ms
->rwlock
)
1922 struct xlate_cfg
*xcfg
;
1923 struct mcast_mrouter_bundle
*mrouter
;
1924 struct xbundle
*mcast_xbundle
;
1926 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1927 LIST_FOR_EACH(mrouter
, mrouter_node
, &ms
->mrouter_lru
) {
1928 mcast_xbundle
= xbundle_lookup(xcfg
, mrouter
->port
);
1929 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
) {
1930 xlate_report(ctx
, "forwarding to mcast router port");
1931 output_normal(ctx
, mcast_xbundle
, vlan
);
1932 } else if (!mcast_xbundle
) {
1933 xlate_report(ctx
, "mcast router port is unknown, dropping");
1935 xlate_report(ctx
, "mcast router port is input port, dropping");
1940 /* send the packet to ports flagged to be flooded */
1942 xlate_normal_mcast_send_fports(struct xlate_ctx
*ctx
,
1943 struct mcast_snooping
*ms
,
1944 struct xbundle
*in_xbundle
, uint16_t vlan
)
1945 OVS_REQ_RDLOCK(ms
->rwlock
)
1947 struct xlate_cfg
*xcfg
;
1948 struct mcast_fport_bundle
*fport
;
1949 struct xbundle
*mcast_xbundle
;
1951 xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
1952 LIST_FOR_EACH(fport
, fport_node
, &ms
->fport_list
) {
1953 mcast_xbundle
= xbundle_lookup(xcfg
, fport
->port
);
1954 if (mcast_xbundle
&& mcast_xbundle
!= in_xbundle
) {
1955 xlate_report(ctx
, "forwarding to mcast flood port");
1956 output_normal(ctx
, mcast_xbundle
, vlan
);
1957 } else if (!mcast_xbundle
) {
1958 xlate_report(ctx
, "mcast flood port is unknown, dropping");
1960 xlate_report(ctx
, "mcast flood port is input port, dropping");
1966 xlate_normal_flood(struct xlate_ctx
*ctx
, struct xbundle
*in_xbundle
,
1969 struct xbundle
*xbundle
;
1971 LIST_FOR_EACH (xbundle
, list_node
, &ctx
->xbridge
->xbundles
) {
1972 if (xbundle
!= in_xbundle
1973 && xbundle_includes_vlan(xbundle
, vlan
)
1974 && xbundle
->floodable
1975 && !xbundle_mirror_out(ctx
->xbridge
, xbundle
)) {
1976 output_normal(ctx
, xbundle
, vlan
);
1979 ctx
->xout
->nf_output_iface
= NF_OUT_FLOOD
;
1983 xlate_normal(struct xlate_ctx
*ctx
)
1985 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
1986 struct flow
*flow
= &ctx
->xin
->flow
;
1987 struct xbundle
*in_xbundle
;
1988 struct xport
*in_port
;
1989 struct mac_entry
*mac
;
1994 ctx
->xout
->has_normal
= true;
1996 memset(&wc
->masks
.dl_src
, 0xff, sizeof wc
->masks
.dl_src
);
1997 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
1998 wc
->masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
2000 in_xbundle
= lookup_input_bundle(ctx
->xbridge
, flow
->in_port
.ofp_port
,
2001 ctx
->xin
->packet
!= NULL
, &in_port
);
2003 xlate_report(ctx
, "no input bundle, dropping");
2007 /* Drop malformed frames. */
2008 if (flow
->dl_type
== htons(ETH_TYPE_VLAN
) &&
2009 !(flow
->vlan_tci
& htons(VLAN_CFI
))) {
2010 if (ctx
->xin
->packet
!= NULL
) {
2011 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2012 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet with partial "
2013 "VLAN tag received on port %s",
2014 ctx
->xbridge
->name
, in_xbundle
->name
);
2016 xlate_report(ctx
, "partial VLAN tag, dropping");
2020 /* Drop frames on bundles reserved for mirroring. */
2021 if (xbundle_mirror_out(ctx
->xbridge
, in_xbundle
)) {
2022 if (ctx
->xin
->packet
!= NULL
) {
2023 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2024 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet received on port "
2025 "%s, which is reserved exclusively for mirroring",
2026 ctx
->xbridge
->name
, in_xbundle
->name
);
2028 xlate_report(ctx
, "input port is mirror output port, dropping");
2033 vid
= vlan_tci_to_vid(flow
->vlan_tci
);
2034 if (!input_vid_is_valid(vid
, in_xbundle
, ctx
->xin
->packet
!= NULL
)) {
2035 xlate_report(ctx
, "disallowed VLAN VID for this input port, dropping");
2038 vlan
= input_vid_to_vlan(in_xbundle
, vid
);
2040 /* Check other admissibility requirements. */
2041 if (in_port
&& !is_admissible(ctx
, in_port
, vlan
)) {
2045 /* Learn source MAC. */
2046 if (ctx
->xin
->may_learn
) {
2047 update_learning_table(ctx
->xbridge
, flow
, wc
, vlan
, in_xbundle
);
2049 if (ctx
->xin
->xcache
) {
2050 struct xc_entry
*entry
;
2052 /* Save enough info to update mac learning table later. */
2053 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NORMAL
);
2054 entry
->u
.normal
.ofproto
= ctx
->xbridge
->ofproto
;
2055 entry
->u
.normal
.flow
= xmemdup(flow
, sizeof *flow
);
2056 entry
->u
.normal
.vlan
= vlan
;
2059 /* Determine output bundle. */
2060 if (mcast_snooping_enabled(ctx
->xbridge
->ms
)
2061 && !eth_addr_is_broadcast(flow
->dl_dst
)
2062 && eth_addr_is_multicast(flow
->dl_dst
)
2063 && flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2064 struct mcast_snooping
*ms
= ctx
->xbridge
->ms
;
2065 struct mcast_group
*grp
;
2067 if (flow
->nw_proto
== IPPROTO_IGMP
) {
2068 if (ctx
->xin
->may_learn
) {
2069 if (mcast_snooping_is_membership(flow
->tp_src
) ||
2070 mcast_snooping_is_query(flow
->tp_src
)) {
2071 update_mcast_snooping_table(ctx
->xbridge
, flow
, vlan
,
2076 if (mcast_snooping_is_membership(flow
->tp_src
)) {
2077 ovs_rwlock_rdlock(&ms
->rwlock
);
2078 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, vlan
);
2079 ovs_rwlock_unlock(&ms
->rwlock
);
2081 xlate_report(ctx
, "multicast traffic, flooding");
2082 xlate_normal_flood(ctx
, in_xbundle
, vlan
);
2086 if (ip_is_local_multicast(flow
->nw_dst
)) {
2087 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2088 * address in the 224.0.0.x range which are not IGMP must
2089 * be forwarded on all ports */
2090 xlate_report(ctx
, "RFC4541: section 2.1.2, item 2, flooding");
2091 xlate_normal_flood(ctx
, in_xbundle
, vlan
);
2096 /* forwarding to group base ports */
2097 ovs_rwlock_rdlock(&ms
->rwlock
);
2098 grp
= mcast_snooping_lookup(ms
, flow
->nw_dst
, vlan
);
2100 xlate_normal_mcast_send_group(ctx
, ms
, grp
, in_xbundle
, vlan
);
2101 xlate_normal_mcast_send_fports(ctx
, ms
, in_xbundle
, vlan
);
2102 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, vlan
);
2104 if (mcast_snooping_flood_unreg(ms
)) {
2105 xlate_report(ctx
, "unregistered multicast, flooding");
2106 xlate_normal_flood(ctx
, in_xbundle
, vlan
);
2108 xlate_normal_mcast_send_mrouters(ctx
, ms
, in_xbundle
, vlan
);
2109 xlate_normal_mcast_send_fports(ctx
, ms
, in_xbundle
, vlan
);
2112 ovs_rwlock_unlock(&ms
->rwlock
);
2114 ovs_rwlock_rdlock(&ctx
->xbridge
->ml
->rwlock
);
2115 mac
= mac_learning_lookup(ctx
->xbridge
->ml
, flow
->dl_dst
, vlan
);
2116 mac_port
= mac
? mac
->port
.p
: NULL
;
2117 ovs_rwlock_unlock(&ctx
->xbridge
->ml
->rwlock
);
2120 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
2121 struct xbundle
*mac_xbundle
= xbundle_lookup(xcfg
, mac_port
);
2122 if (mac_xbundle
&& mac_xbundle
!= in_xbundle
) {
2123 xlate_report(ctx
, "forwarding to learned port");
2124 output_normal(ctx
, mac_xbundle
, vlan
);
2125 } else if (!mac_xbundle
) {
2126 xlate_report(ctx
, "learned port is unknown, dropping");
2128 xlate_report(ctx
, "learned port is input port, dropping");
2131 xlate_report(ctx
, "no learned MAC for destination, flooding");
2132 xlate_normal_flood(ctx
, in_xbundle
, vlan
);
2137 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
2138 * the number of packets out of UINT32_MAX to sample. The given
2139 * cookie is passed back in the callback for each sampled packet.
2142 compose_sample_action(const struct xbridge
*xbridge
,
2143 struct ofpbuf
*odp_actions
,
2144 const struct flow
*flow
,
2145 const uint32_t probability
,
2146 const union user_action_cookie
*cookie
,
2147 const size_t cookie_size
)
2149 size_t sample_offset
, actions_offset
;
2150 odp_port_t odp_port
;
2154 sample_offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SAMPLE
);
2156 nl_msg_put_u32(odp_actions
, OVS_SAMPLE_ATTR_PROBABILITY
, probability
);
2158 actions_offset
= nl_msg_start_nested(odp_actions
, OVS_SAMPLE_ATTR_ACTIONS
);
2160 odp_port
= ofp_port_to_odp_port(xbridge
, flow
->in_port
.ofp_port
);
2161 pid
= dpif_port_get_pid(xbridge
->dpif
, odp_port
,
2162 flow_hash_5tuple(flow
, 0));
2163 cookie_offset
= odp_put_userspace_action(pid
, cookie
, cookie_size
,
2166 nl_msg_end_nested(odp_actions
, actions_offset
);
2167 nl_msg_end_nested(odp_actions
, sample_offset
);
2168 return cookie_offset
;
2172 compose_sflow_cookie(const struct xbridge
*xbridge
, ovs_be16 vlan_tci
,
2173 odp_port_t odp_port
, unsigned int n_outputs
,
2174 union user_action_cookie
*cookie
)
2178 cookie
->type
= USER_ACTION_COOKIE_SFLOW
;
2179 cookie
->sflow
.vlan_tci
= vlan_tci
;
2181 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2182 * port information") for the interpretation of cookie->output. */
2183 switch (n_outputs
) {
2185 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2186 cookie
->sflow
.output
= 0x40000000 | 256;
2190 ifindex
= dpif_sflow_odp_port_to_ifindex(xbridge
->sflow
, odp_port
);
2192 cookie
->sflow
.output
= ifindex
;
2197 /* 0x80000000 means "multiple output ports. */
2198 cookie
->sflow
.output
= 0x80000000 | n_outputs
;
2203 /* Compose SAMPLE action for sFlow bridge sampling. */
2205 compose_sflow_action(const struct xbridge
*xbridge
,
2206 struct ofpbuf
*odp_actions
,
2207 const struct flow
*flow
,
2208 odp_port_t odp_port
)
2210 uint32_t probability
;
2211 union user_action_cookie cookie
;
2213 if (!xbridge
->sflow
|| flow
->in_port
.ofp_port
== OFPP_NONE
) {
2217 probability
= dpif_sflow_get_probability(xbridge
->sflow
);
2218 compose_sflow_cookie(xbridge
, htons(0), odp_port
,
2219 odp_port
== ODPP_NONE
? 0 : 1, &cookie
);
2221 return compose_sample_action(xbridge
, odp_actions
, flow
, probability
,
2222 &cookie
, sizeof cookie
.sflow
);
2226 compose_flow_sample_cookie(uint16_t probability
, uint32_t collector_set_id
,
2227 uint32_t obs_domain_id
, uint32_t obs_point_id
,
2228 union user_action_cookie
*cookie
)
2230 cookie
->type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
2231 cookie
->flow_sample
.probability
= probability
;
2232 cookie
->flow_sample
.collector_set_id
= collector_set_id
;
2233 cookie
->flow_sample
.obs_domain_id
= obs_domain_id
;
2234 cookie
->flow_sample
.obs_point_id
= obs_point_id
;
2238 compose_ipfix_cookie(union user_action_cookie
*cookie
)
2240 cookie
->type
= USER_ACTION_COOKIE_IPFIX
;
2243 /* Compose SAMPLE action for IPFIX bridge sampling. */
2245 compose_ipfix_action(const struct xbridge
*xbridge
,
2246 struct ofpbuf
*odp_actions
,
2247 const struct flow
*flow
)
2249 uint32_t probability
;
2250 union user_action_cookie cookie
;
2252 if (!xbridge
->ipfix
|| flow
->in_port
.ofp_port
== OFPP_NONE
) {
2256 probability
= dpif_ipfix_get_bridge_exporter_probability(xbridge
->ipfix
);
2257 compose_ipfix_cookie(&cookie
);
2259 compose_sample_action(xbridge
, odp_actions
, flow
, probability
,
2260 &cookie
, sizeof cookie
.ipfix
);
2263 /* SAMPLE action for sFlow must be first action in any given list of
2264 * actions. At this point we do not have all information required to
2265 * build it. So try to build sample action as complete as possible. */
2267 add_sflow_action(struct xlate_ctx
*ctx
)
2269 ctx
->user_cookie_offset
= compose_sflow_action(ctx
->xbridge
,
2270 &ctx
->xout
->odp_actions
,
2271 &ctx
->xin
->flow
, ODPP_NONE
);
2272 ctx
->sflow_odp_port
= 0;
2273 ctx
->sflow_n_outputs
= 0;
2276 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
2277 * of actions, eventually after the SAMPLE action for sFlow. */
2279 add_ipfix_action(struct xlate_ctx
*ctx
)
2281 compose_ipfix_action(ctx
->xbridge
, &ctx
->xout
->odp_actions
,
2285 /* Fix SAMPLE action according to data collected while composing ODP actions.
2286 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
2287 * USERSPACE action's user-cookie which is required for sflow. */
2289 fix_sflow_action(struct xlate_ctx
*ctx
)
2291 const struct flow
*base
= &ctx
->base_flow
;
2292 union user_action_cookie
*cookie
;
2294 if (!ctx
->user_cookie_offset
) {
2298 cookie
= ofpbuf_at(&ctx
->xout
->odp_actions
, ctx
->user_cookie_offset
,
2299 sizeof cookie
->sflow
);
2300 ovs_assert(cookie
->type
== USER_ACTION_COOKIE_SFLOW
);
2302 compose_sflow_cookie(ctx
->xbridge
, base
->vlan_tci
,
2303 ctx
->sflow_odp_port
, ctx
->sflow_n_outputs
, cookie
);
2306 static enum slow_path_reason
2307 process_special(struct xlate_ctx
*ctx
, const struct flow
*flow
,
2308 const struct xport
*xport
, const struct ofpbuf
*packet
)
2310 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
2311 const struct xbridge
*xbridge
= ctx
->xbridge
;
2315 } else if (xport
->cfm
&& cfm_should_process_flow(xport
->cfm
, flow
, wc
)) {
2317 cfm_process_heartbeat(xport
->cfm
, packet
);
2320 } else if (xport
->bfd
&& bfd_should_process_flow(xport
->bfd
, flow
, wc
)) {
2322 bfd_process_packet(xport
->bfd
, flow
, packet
);
2323 /* If POLL received, immediately sends FINAL back. */
2324 if (bfd_should_send_packet(xport
->bfd
)) {
2325 ofproto_dpif_monitor_port_send_soon(xport
->ofport
);
2329 } else if (xport
->xbundle
&& xport
->xbundle
->lacp
2330 && flow
->dl_type
== htons(ETH_TYPE_LACP
)) {
2332 lacp_process_packet(xport
->xbundle
->lacp
, xport
->ofport
, packet
);
2335 } else if (xbridge
->stp
&& stp_should_process_flow(flow
, wc
)) {
2337 stp_process_packet(xport
, packet
);
2346 compose_output_action__(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
,
2349 const struct xport
*xport
= get_ofp_port(ctx
->xbridge
, ofp_port
);
2350 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
2351 struct flow
*flow
= &ctx
->xin
->flow
;
2352 ovs_be16 flow_vlan_tci
;
2353 uint32_t flow_pkt_mark
;
2354 uint8_t flow_nw_tos
;
2355 odp_port_t out_port
, odp_port
;
2358 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2359 * before traversing a patch port. */
2360 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 27);
2363 xlate_report(ctx
, "Nonexistent output port");
2365 } else if (xport
->config
& OFPUTIL_PC_NO_FWD
) {
2366 xlate_report(ctx
, "OFPPC_NO_FWD set, skipping output");
2368 } else if (check_stp
) {
2369 if (is_stp(&ctx
->base_flow
)) {
2370 if (!xport_stp_listen_state(xport
)) {
2371 xlate_report(ctx
, "STP not in listening state, "
2372 "skipping bpdu output");
2375 } else if (!xport_stp_forward_state(xport
)) {
2376 xlate_report(ctx
, "STP not in forwarding state, "
2382 if (mbridge_has_mirrors(ctx
->xbridge
->mbridge
) && xport
->xbundle
) {
2383 ctx
->xout
->mirrors
|= xbundle_mirror_dst(xport
->xbundle
->xbridge
,
2388 const struct xport
*peer
= xport
->peer
;
2389 struct flow old_flow
= ctx
->xin
->flow
;
2390 enum slow_path_reason special
;
2392 ctx
->xbridge
= peer
->xbridge
;
2393 flow
->in_port
.ofp_port
= peer
->ofp_port
;
2394 flow
->metadata
= htonll(0);
2395 memset(&flow
->tunnel
, 0, sizeof flow
->tunnel
);
2396 memset(flow
->regs
, 0, sizeof flow
->regs
);
2398 special
= process_special(ctx
, &ctx
->xin
->flow
, peer
,
2401 ctx
->xout
->slow
|= special
;
2402 } else if (may_receive(peer
, ctx
)) {
2403 if (xport_stp_forward_state(peer
)) {
2404 xlate_table_action(ctx
, flow
->in_port
.ofp_port
, 0, true, true);
2406 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
2407 * learning action look at the packet, then drop it. */
2408 struct flow old_base_flow
= ctx
->base_flow
;
2409 size_t old_size
= ofpbuf_size(&ctx
->xout
->odp_actions
);
2410 mirror_mask_t old_mirrors
= ctx
->xout
->mirrors
;
2411 xlate_table_action(ctx
, flow
->in_port
.ofp_port
, 0, true, true);
2412 ctx
->xout
->mirrors
= old_mirrors
;
2413 ctx
->base_flow
= old_base_flow
;
2414 ofpbuf_set_size(&ctx
->xout
->odp_actions
, old_size
);
2418 ctx
->xin
->flow
= old_flow
;
2419 ctx
->xbridge
= xport
->xbridge
;
2421 if (ctx
->xin
->resubmit_stats
) {
2422 netdev_vport_inc_tx(xport
->netdev
, ctx
->xin
->resubmit_stats
);
2423 netdev_vport_inc_rx(peer
->netdev
, ctx
->xin
->resubmit_stats
);
2425 bfd_account_rx(peer
->bfd
, ctx
->xin
->resubmit_stats
);
2428 if (ctx
->xin
->xcache
) {
2429 struct xc_entry
*entry
;
2431 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NETDEV
);
2432 entry
->u
.dev
.tx
= netdev_ref(xport
->netdev
);
2433 entry
->u
.dev
.rx
= netdev_ref(peer
->netdev
);
2434 entry
->u
.dev
.bfd
= bfd_ref(peer
->bfd
);
2439 flow_vlan_tci
= flow
->vlan_tci
;
2440 flow_pkt_mark
= flow
->pkt_mark
;
2441 flow_nw_tos
= flow
->nw_tos
;
2443 if (dscp_from_skb_priority(xport
, flow
->skb_priority
, &dscp
)) {
2444 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
2445 flow
->nw_tos
&= ~IP_DSCP_MASK
;
2446 flow
->nw_tos
|= dscp
;
2449 if (xport
->is_tunnel
) {
2450 /* Save tunnel metadata so that changes made due to
2451 * the Logical (tunnel) Port are not visible for any further
2452 * matches, while explicit set actions on tunnel metadata are.
2454 struct flow_tnl flow_tnl
= flow
->tunnel
;
2455 odp_port
= tnl_port_send(xport
->ofport
, flow
, &ctx
->xout
->wc
);
2456 if (odp_port
== ODPP_NONE
) {
2457 xlate_report(ctx
, "Tunneling decided against output");
2458 goto out
; /* restore flow_nw_tos */
2460 if (flow
->tunnel
.ip_dst
== ctx
->orig_tunnel_ip_dst
) {
2461 xlate_report(ctx
, "Not tunneling to our own address");
2462 goto out
; /* restore flow_nw_tos */
2464 if (ctx
->xin
->resubmit_stats
) {
2465 netdev_vport_inc_tx(xport
->netdev
, ctx
->xin
->resubmit_stats
);
2467 if (ctx
->xin
->xcache
) {
2468 struct xc_entry
*entry
;
2470 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_NETDEV
);
2471 entry
->u
.dev
.tx
= netdev_ref(xport
->netdev
);
2473 out_port
= odp_port
;
2474 commit_odp_tunnel_action(flow
, &ctx
->base_flow
,
2475 &ctx
->xout
->odp_actions
);
2476 flow
->tunnel
= flow_tnl
; /* Restore tunnel metadata */
2478 odp_port
= xport
->odp_port
;
2479 out_port
= odp_port
;
2480 if (ofproto_has_vlan_splinters(ctx
->xbridge
->ofproto
)) {
2481 ofp_port_t vlandev_port
;
2483 wc
->masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
2484 vlandev_port
= vsp_realdev_to_vlandev(ctx
->xbridge
->ofproto
,
2485 ofp_port
, flow
->vlan_tci
);
2486 if (vlandev_port
!= ofp_port
) {
2487 out_port
= ofp_port_to_odp_port(ctx
->xbridge
, vlandev_port
);
2488 flow
->vlan_tci
= htons(0);
2493 if (out_port
!= ODPP_NONE
) {
2494 ctx
->xout
->slow
|= commit_odp_actions(flow
, &ctx
->base_flow
,
2495 &ctx
->xout
->odp_actions
,
2498 if (ctx
->use_recirc
) {
2499 struct ovs_action_hash
*act_hash
;
2500 struct xlate_recirc
*xr
= &ctx
->recirc
;
2503 act_hash
= nl_msg_put_unspec_uninit(&ctx
->xout
->odp_actions
,
2504 OVS_ACTION_ATTR_HASH
,
2506 act_hash
->hash_alg
= xr
->hash_alg
;
2507 act_hash
->hash_basis
= xr
->hash_basis
;
2509 /* Recirc action. */
2510 nl_msg_put_u32(&ctx
->xout
->odp_actions
, OVS_ACTION_ATTR_RECIRC
,
2513 nl_msg_put_odp_port(&ctx
->xout
->odp_actions
, OVS_ACTION_ATTR_OUTPUT
,
2517 ctx
->sflow_odp_port
= odp_port
;
2518 ctx
->sflow_n_outputs
++;
2519 ctx
->xout
->nf_output_iface
= ofp_port
;
2524 flow
->vlan_tci
= flow_vlan_tci
;
2525 flow
->pkt_mark
= flow_pkt_mark
;
2526 flow
->nw_tos
= flow_nw_tos
;
2530 compose_output_action(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
)
2532 compose_output_action__(ctx
, ofp_port
, true);
2536 xlate_recursively(struct xlate_ctx
*ctx
, struct rule_dpif
*rule
)
2538 struct rule_dpif
*old_rule
= ctx
->rule
;
2539 const struct rule_actions
*actions
;
2541 if (ctx
->xin
->resubmit_stats
) {
2542 rule_dpif_credit_stats(rule
, ctx
->xin
->resubmit_stats
);
2548 actions
= rule_dpif_get_actions(rule
);
2549 do_xlate_actions(actions
->ofpacts
, actions
->ofpacts_len
, ctx
);
2550 ctx
->rule
= old_rule
;
2555 xlate_resubmit_resource_check(struct xlate_ctx
*ctx
)
2557 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
2559 if (ctx
->recurse
>= MAX_RESUBMIT_RECURSION
+ MAX_INTERNAL_RESUBMITS
) {
2560 VLOG_ERR_RL(&rl
, "resubmit actions recursed over %d times",
2561 MAX_RESUBMIT_RECURSION
);
2562 } else if (ctx
->resubmits
>= MAX_RESUBMITS
+ MAX_INTERNAL_RESUBMITS
) {
2563 VLOG_ERR_RL(&rl
, "over %d resubmit actions", MAX_RESUBMITS
);
2564 } else if (ofpbuf_size(&ctx
->xout
->odp_actions
) > UINT16_MAX
) {
2565 VLOG_ERR_RL(&rl
, "resubmits yielded over 64 kB of actions");
2566 } else if (ofpbuf_size(&ctx
->stack
) >= 65536) {
2567 VLOG_ERR_RL(&rl
, "resubmits yielded over 64 kB of stack");
2576 xlate_table_action(struct xlate_ctx
*ctx
, ofp_port_t in_port
, uint8_t table_id
,
2577 bool may_packet_in
, bool honor_table_miss
)
2579 if (xlate_resubmit_resource_check(ctx
)) {
2580 ofp_port_t old_in_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
2581 bool skip_wildcards
= ctx
->xin
->skip_wildcards
;
2582 uint8_t old_table_id
= ctx
->table_id
;
2583 struct rule_dpif
*rule
;
2584 enum rule_dpif_lookup_verdict verdict
;
2585 enum ofputil_port_config config
= 0;
2587 ctx
->table_id
= table_id
;
2589 /* Look up a flow with 'in_port' as the input port. Then restore the
2590 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2591 * have surprising behavior). */
2592 ctx
->xin
->flow
.in_port
.ofp_port
= in_port
;
2593 verdict
= rule_dpif_lookup_from_table(ctx
->xbridge
->ofproto
,
2596 ? &ctx
->xout
->wc
: NULL
,
2598 &ctx
->table_id
, &rule
,
2599 ctx
->xin
->xcache
!= NULL
,
2600 ctx
->xin
->resubmit_stats
);
2601 ctx
->xin
->flow
.in_port
.ofp_port
= old_in_port
;
2603 if (ctx
->xin
->resubmit_hook
) {
2604 ctx
->xin
->resubmit_hook(ctx
->xin
, rule
, ctx
->recurse
);
2608 case RULE_DPIF_LOOKUP_VERDICT_MATCH
:
2610 case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER
:
2611 if (may_packet_in
) {
2612 struct xport
*xport
;
2614 xport
= get_ofp_port(ctx
->xbridge
,
2615 ctx
->xin
->flow
.in_port
.ofp_port
);
2616 config
= xport
? xport
->config
: 0;
2619 /* Fall through to drop */
2620 case RULE_DPIF_LOOKUP_VERDICT_DROP
:
2621 config
= OFPUTIL_PC_NO_PACKET_IN
;
2623 case RULE_DPIF_LOOKUP_VERDICT_DEFAULT
:
2624 if (!ofproto_dpif_wants_packet_in_on_miss(ctx
->xbridge
->ofproto
)) {
2625 config
= OFPUTIL_PC_NO_PACKET_IN
;
2632 choose_miss_rule(config
, ctx
->xbridge
->miss_rule
,
2633 ctx
->xbridge
->no_packet_in_rule
, &rule
,
2634 ctx
->xin
->xcache
!= NULL
);
2638 /* Fill in the cache entry here instead of xlate_recursively
2639 * to make the reference counting more explicit. We take a
2640 * reference in the lookups above if we are going to cache the
2642 if (ctx
->xin
->xcache
) {
2643 struct xc_entry
*entry
;
2645 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_RULE
);
2646 entry
->u
.rule
= rule
;
2648 xlate_recursively(ctx
, rule
);
2651 ctx
->table_id
= old_table_id
;
2659 xlate_group_stats(struct xlate_ctx
*ctx
, struct group_dpif
*group
,
2660 struct ofputil_bucket
*bucket
)
2662 if (ctx
->xin
->resubmit_stats
) {
2663 group_dpif_credit_stats(group
, bucket
, ctx
->xin
->resubmit_stats
);
2665 if (ctx
->xin
->xcache
) {
2666 struct xc_entry
*entry
;
2668 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_GROUP
);
2669 entry
->u
.group
.group
= group_dpif_ref(group
);
2670 entry
->u
.group
.bucket
= bucket
;
2675 xlate_group_bucket(struct xlate_ctx
*ctx
, struct ofputil_bucket
*bucket
)
2677 uint64_t action_list_stub
[1024 / 8];
2678 struct ofpbuf action_list
, action_set
;
2680 ofpbuf_use_const(&action_set
, bucket
->ofpacts
, bucket
->ofpacts_len
);
2681 ofpbuf_use_stub(&action_list
, action_list_stub
, sizeof action_list_stub
);
2683 ofpacts_execute_action_set(&action_list
, &action_set
);
2685 do_xlate_actions(ofpbuf_data(&action_list
), ofpbuf_size(&action_list
), ctx
);
2688 ofpbuf_uninit(&action_set
);
2689 ofpbuf_uninit(&action_list
);
2693 xlate_all_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
2695 struct ofputil_bucket
*bucket
;
2696 const struct list
*buckets
;
2697 struct flow old_flow
= ctx
->xin
->flow
;
2699 group_dpif_get_buckets(group
, &buckets
);
2701 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
2702 xlate_group_bucket(ctx
, bucket
);
2703 /* Roll back flow to previous state.
2704 * This is equivalent to cloning the packet for each bucket.
2706 * As a side effect any subsequently applied actions will
2707 * also effectively be applied to a clone of the packet taken
2708 * just before applying the all or indirect group. */
2709 ctx
->xin
->flow
= old_flow
;
2711 xlate_group_stats(ctx
, group
, NULL
);
2715 xlate_ff_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
2717 struct ofputil_bucket
*bucket
;
2719 bucket
= group_first_live_bucket(ctx
, group
, 0);
2721 xlate_group_bucket(ctx
, bucket
);
2722 xlate_group_stats(ctx
, group
, bucket
);
2727 xlate_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
2729 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
2730 struct ofputil_bucket
*bucket
;
2733 basis
= hash_mac(ctx
->xin
->flow
.dl_dst
, 0, 0);
2734 bucket
= group_best_live_bucket(ctx
, group
, basis
);
2736 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
2737 xlate_group_bucket(ctx
, bucket
);
2738 xlate_group_stats(ctx
, group
, bucket
);
2743 xlate_group_action__(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
2745 ctx
->in_group
= true;
2747 switch (group_dpif_get_type(group
)) {
2749 case OFPGT11_INDIRECT
:
2750 xlate_all_group(ctx
, group
);
2752 case OFPGT11_SELECT
:
2753 xlate_select_group(ctx
, group
);
2756 xlate_ff_group(ctx
, group
);
2761 group_dpif_unref(group
);
2763 ctx
->in_group
= false;
2767 xlate_group_resource_check(struct xlate_ctx
*ctx
)
2769 if (!xlate_resubmit_resource_check(ctx
)) {
2771 } else if (ctx
->in_group
) {
2772 /* Prevent nested translation of OpenFlow groups.
2774 * OpenFlow allows this restriction. We enforce this restriction only
2775 * because, with the current architecture, we would otherwise have to
2776 * take a possibly recursive read lock on the ofgroup rwlock, which is
2777 * unsafe given that POSIX allows taking a read lock to block if there
2778 * is a thread blocked on taking the write lock. Other solutions
2779 * without this restriction are also possible, but seem unwarranted
2780 * given the current limited use of groups. */
2781 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
2783 VLOG_ERR_RL(&rl
, "cannot recursively translate OpenFlow group");
2791 xlate_group_action(struct xlate_ctx
*ctx
, uint32_t group_id
)
2793 if (xlate_group_resource_check(ctx
)) {
2794 struct group_dpif
*group
;
2797 got_group
= group_dpif_lookup(ctx
->xbridge
->ofproto
, group_id
, &group
);
2799 xlate_group_action__(ctx
, group
);
2809 xlate_ofpact_resubmit(struct xlate_ctx
*ctx
,
2810 const struct ofpact_resubmit
*resubmit
)
2814 bool may_packet_in
= false;
2815 bool honor_table_miss
= false;
2817 if (ctx
->rule
&& rule_dpif_is_internal(ctx
->rule
)) {
2818 /* Still allow missed packets to be sent to the controller
2819 * if resubmitting from an internal table. */
2820 may_packet_in
= true;
2821 honor_table_miss
= true;
2824 in_port
= resubmit
->in_port
;
2825 if (in_port
== OFPP_IN_PORT
) {
2826 in_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
2829 table_id
= resubmit
->table_id
;
2830 if (table_id
== 255) {
2831 table_id
= ctx
->table_id
;
2834 xlate_table_action(ctx
, in_port
, table_id
, may_packet_in
,
2839 flood_packets(struct xlate_ctx
*ctx
, bool all
)
2841 const struct xport
*xport
;
2843 HMAP_FOR_EACH (xport
, ofp_node
, &ctx
->xbridge
->xports
) {
2844 if (xport
->ofp_port
== ctx
->xin
->flow
.in_port
.ofp_port
) {
2849 compose_output_action__(ctx
, xport
->ofp_port
, false);
2850 } else if (!(xport
->config
& OFPUTIL_PC_NO_FLOOD
)) {
2851 compose_output_action(ctx
, xport
->ofp_port
);
2855 ctx
->xout
->nf_output_iface
= NF_OUT_FLOOD
;
2859 execute_controller_action(struct xlate_ctx
*ctx
, int len
,
2860 enum ofp_packet_in_reason reason
,
2861 uint16_t controller_id
)
2863 struct ofproto_packet_in
*pin
;
2864 struct dpif_packet
*packet
;
2865 struct pkt_metadata md
= PKT_METADATA_INITIALIZER(0);
2867 ctx
->xout
->slow
|= SLOW_CONTROLLER
;
2868 if (!ctx
->xin
->packet
) {
2872 packet
= dpif_packet_clone_from_ofpbuf(ctx
->xin
->packet
);
2874 ctx
->xout
->slow
|= commit_odp_actions(&ctx
->xin
->flow
, &ctx
->base_flow
,
2875 &ctx
->xout
->odp_actions
,
2878 odp_execute_actions(NULL
, &packet
, 1, false, &md
,
2879 ofpbuf_data(&ctx
->xout
->odp_actions
),
2880 ofpbuf_size(&ctx
->xout
->odp_actions
), NULL
);
2882 pin
= xmalloc(sizeof *pin
);
2883 pin
->up
.packet_len
= ofpbuf_size(&packet
->ofpbuf
);
2884 pin
->up
.packet
= ofpbuf_steal_data(&packet
->ofpbuf
);
2885 pin
->up
.reason
= reason
;
2886 pin
->up
.table_id
= ctx
->table_id
;
2887 pin
->up
.cookie
= (ctx
->rule
2888 ? rule_dpif_get_flow_cookie(ctx
->rule
)
2891 flow_get_metadata(&ctx
->xin
->flow
, &pin
->up
.fmd
);
2893 pin
->controller_id
= controller_id
;
2894 pin
->send_len
= len
;
2895 /* If a rule is a table-miss rule then this is
2896 * a table-miss handled by a table-miss rule.
2898 * Else, if rule is internal and has a controller action,
2899 * the later being implied by the rule being processed here,
2900 * then this is a table-miss handled without a table-miss rule.
2902 * Otherwise this is not a table-miss. */
2903 pin
->miss_type
= OFPROTO_PACKET_IN_NO_MISS
;
2905 if (rule_dpif_is_table_miss(ctx
->rule
)) {
2906 pin
->miss_type
= OFPROTO_PACKET_IN_MISS_FLOW
;
2907 } else if (rule_dpif_is_internal(ctx
->rule
)) {
2908 pin
->miss_type
= OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW
;
2911 ofproto_dpif_send_packet_in(ctx
->xbridge
->ofproto
, pin
);
2912 dpif_packet_delete(packet
);
2916 compose_mpls_push_action(struct xlate_ctx
*ctx
, struct ofpact_push_mpls
*mpls
)
2918 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
2919 struct flow
*flow
= &ctx
->xin
->flow
;
2922 ovs_assert(eth_type_mpls(mpls
->ethertype
));
2924 n
= flow_count_mpls_labels(flow
, wc
);
2926 ctx
->xout
->slow
|= commit_odp_actions(flow
, &ctx
->base_flow
,
2927 &ctx
->xout
->odp_actions
,
2929 } else if (n
>= FLOW_MAX_MPLS_LABELS
) {
2930 if (ctx
->xin
->packet
!= NULL
) {
2931 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2932 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet on which an "
2933 "MPLS push action can't be performed as it would "
2934 "have more MPLS LSEs than the %d supported.",
2935 ctx
->xbridge
->name
, FLOW_MAX_MPLS_LABELS
);
2939 } else if (n
>= ctx
->xbridge
->max_mpls_depth
) {
2940 COVERAGE_INC(xlate_actions_mpls_overflow
);
2941 ctx
->xout
->slow
|= SLOW_ACTION
;
2944 flow_push_mpls(flow
, n
, mpls
->ethertype
, wc
);
2948 compose_mpls_pop_action(struct xlate_ctx
*ctx
, ovs_be16 eth_type
)
2950 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
2951 struct flow
*flow
= &ctx
->xin
->flow
;
2952 int n
= flow_count_mpls_labels(flow
, wc
);
2954 if (!flow_pop_mpls(flow
, n
, eth_type
, wc
) && n
>= FLOW_MAX_MPLS_LABELS
) {
2955 if (ctx
->xin
->packet
!= NULL
) {
2956 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2957 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet on which an "
2958 "MPLS pop action can't be performed as it has "
2959 "more MPLS LSEs than the %d supported.",
2960 ctx
->xbridge
->name
, FLOW_MAX_MPLS_LABELS
);
2963 ofpbuf_clear(&ctx
->xout
->odp_actions
);
2968 compose_dec_ttl(struct xlate_ctx
*ctx
, struct ofpact_cnt_ids
*ids
)
2970 struct flow
*flow
= &ctx
->xin
->flow
;
2972 if (!is_ip_any(flow
)) {
2976 ctx
->xout
->wc
.masks
.nw_ttl
= 0xff;
2977 if (flow
->nw_ttl
> 1) {
2983 for (i
= 0; i
< ids
->n_controllers
; i
++) {
2984 execute_controller_action(ctx
, UINT16_MAX
, OFPR_INVALID_TTL
,
2988 /* Stop processing for current table. */
2994 compose_set_mpls_label_action(struct xlate_ctx
*ctx
, ovs_be32 label
)
2996 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
2997 ctx
->xout
->wc
.masks
.mpls_lse
[0] |= htonl(MPLS_LABEL_MASK
);
2998 set_mpls_lse_label(&ctx
->xin
->flow
.mpls_lse
[0], label
);
3003 compose_set_mpls_tc_action(struct xlate_ctx
*ctx
, uint8_t tc
)
3005 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
3006 ctx
->xout
->wc
.masks
.mpls_lse
[0] |= htonl(MPLS_TC_MASK
);
3007 set_mpls_lse_tc(&ctx
->xin
->flow
.mpls_lse
[0], tc
);
3012 compose_set_mpls_ttl_action(struct xlate_ctx
*ctx
, uint8_t ttl
)
3014 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
3015 ctx
->xout
->wc
.masks
.mpls_lse
[0] |= htonl(MPLS_TTL_MASK
);
3016 set_mpls_lse_ttl(&ctx
->xin
->flow
.mpls_lse
[0], ttl
);
3021 compose_dec_mpls_ttl_action(struct xlate_ctx
*ctx
)
3023 struct flow
*flow
= &ctx
->xin
->flow
;
3024 uint8_t ttl
= mpls_lse_to_ttl(flow
->mpls_lse
[0]);
3025 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
3027 memset(&wc
->masks
.mpls_lse
, 0xff, sizeof wc
->masks
.mpls_lse
);
3028 if (eth_type_mpls(flow
->dl_type
)) {
3031 set_mpls_lse_ttl(&flow
->mpls_lse
[0], ttl
);
3034 execute_controller_action(ctx
, UINT16_MAX
, OFPR_INVALID_TTL
, 0);
3036 /* Stop processing for current table. */
3045 xlate_output_action(struct xlate_ctx
*ctx
,
3046 ofp_port_t port
, uint16_t max_len
, bool may_packet_in
)
3048 ofp_port_t prev_nf_output_iface
= ctx
->xout
->nf_output_iface
;
3050 ctx
->xout
->nf_output_iface
= NF_OUT_DROP
;
3054 compose_output_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
);
3057 xlate_table_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
,
3058 0, may_packet_in
, true);
3064 flood_packets(ctx
, false);
3067 flood_packets(ctx
, true);
3069 case OFPP_CONTROLLER
:
3070 execute_controller_action(ctx
, max_len
, OFPR_ACTION
, 0);
3076 if (port
!= ctx
->xin
->flow
.in_port
.ofp_port
) {
3077 compose_output_action(ctx
, port
);
3079 xlate_report(ctx
, "skipping output to input port");
3084 if (prev_nf_output_iface
== NF_OUT_FLOOD
) {
3085 ctx
->xout
->nf_output_iface
= NF_OUT_FLOOD
;
3086 } else if (ctx
->xout
->nf_output_iface
== NF_OUT_DROP
) {
3087 ctx
->xout
->nf_output_iface
= prev_nf_output_iface
;
3088 } else if (prev_nf_output_iface
!= NF_OUT_DROP
&&
3089 ctx
->xout
->nf_output_iface
!= NF_OUT_FLOOD
) {
3090 ctx
->xout
->nf_output_iface
= NF_OUT_MULTI
;
3095 xlate_output_reg_action(struct xlate_ctx
*ctx
,
3096 const struct ofpact_output_reg
*or)
3098 uint64_t port
= mf_get_subfield(&or->src
, &ctx
->xin
->flow
);
3099 if (port
<= UINT16_MAX
) {
3100 union mf_subvalue value
;
3102 memset(&value
, 0xff, sizeof value
);
3103 mf_write_subfield_flow(&or->src
, &value
, &ctx
->xout
->wc
.masks
);
3104 xlate_output_action(ctx
, u16_to_ofp(port
),
3105 or->max_len
, false);
3110 xlate_enqueue_action(struct xlate_ctx
*ctx
,
3111 const struct ofpact_enqueue
*enqueue
)
3113 ofp_port_t ofp_port
= enqueue
->port
;
3114 uint32_t queue_id
= enqueue
->queue
;
3115 uint32_t flow_priority
, priority
;
3118 /* Translate queue to priority. */
3119 error
= dpif_queue_to_priority(ctx
->xbridge
->dpif
, queue_id
, &priority
);
3121 /* Fall back to ordinary output action. */
3122 xlate_output_action(ctx
, enqueue
->port
, 0, false);
3126 /* Check output port. */
3127 if (ofp_port
== OFPP_IN_PORT
) {
3128 ofp_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
3129 } else if (ofp_port
== ctx
->xin
->flow
.in_port
.ofp_port
) {
3133 /* Add datapath actions. */
3134 flow_priority
= ctx
->xin
->flow
.skb_priority
;
3135 ctx
->xin
->flow
.skb_priority
= priority
;
3136 compose_output_action(ctx
, ofp_port
);
3137 ctx
->xin
->flow
.skb_priority
= flow_priority
;
3139 /* Update NetFlow output port. */
3140 if (ctx
->xout
->nf_output_iface
== NF_OUT_DROP
) {
3141 ctx
->xout
->nf_output_iface
= ofp_port
;
3142 } else if (ctx
->xout
->nf_output_iface
!= NF_OUT_FLOOD
) {
3143 ctx
->xout
->nf_output_iface
= NF_OUT_MULTI
;
3148 xlate_set_queue_action(struct xlate_ctx
*ctx
, uint32_t queue_id
)
3150 uint32_t skb_priority
;
3152 if (!dpif_queue_to_priority(ctx
->xbridge
->dpif
, queue_id
, &skb_priority
)) {
3153 ctx
->xin
->flow
.skb_priority
= skb_priority
;
3155 /* Couldn't translate queue to a priority. Nothing to do. A warning
3156 * has already been logged. */
3161 slave_enabled_cb(ofp_port_t ofp_port
, void *xbridge_
)
3163 const struct xbridge
*xbridge
= xbridge_
;
3174 case OFPP_CONTROLLER
: /* Not supported by the bundle action. */
3177 port
= get_ofp_port(xbridge
, ofp_port
);
3178 return port
? port
->may_enable
: false;
3183 xlate_bundle_action(struct xlate_ctx
*ctx
,
3184 const struct ofpact_bundle
*bundle
)
3188 port
= bundle_execute(bundle
, &ctx
->xin
->flow
, &ctx
->xout
->wc
,
3190 CONST_CAST(struct xbridge
*, ctx
->xbridge
));
3191 if (bundle
->dst
.field
) {
3192 nxm_reg_load(&bundle
->dst
, ofp_to_u16(port
), &ctx
->xin
->flow
,
3195 xlate_output_action(ctx
, port
, 0, false);
3200 xlate_learn_action__(struct xlate_ctx
*ctx
, const struct ofpact_learn
*learn
,
3201 struct ofputil_flow_mod
*fm
, struct ofpbuf
*ofpacts
)
3203 learn_execute(learn
, &ctx
->xin
->flow
, fm
, ofpacts
);
3204 if (ctx
->xin
->may_learn
) {
3205 ofproto_dpif_flow_mod(ctx
->xbridge
->ofproto
, fm
);
3210 xlate_learn_action(struct xlate_ctx
*ctx
, const struct ofpact_learn
*learn
)
3212 ctx
->xout
->has_learn
= true;
3213 learn_mask(learn
, &ctx
->xout
->wc
);
3215 if (ctx
->xin
->xcache
) {
3216 struct xc_entry
*entry
;
3218 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_LEARN
);
3219 entry
->u
.learn
.ofproto
= ctx
->xbridge
->ofproto
;
3220 entry
->u
.learn
.fm
= xmalloc(sizeof *entry
->u
.learn
.fm
);
3221 entry
->u
.learn
.ofpacts
= ofpbuf_new(64);
3222 xlate_learn_action__(ctx
, learn
, entry
->u
.learn
.fm
,
3223 entry
->u
.learn
.ofpacts
);
3224 } else if (ctx
->xin
->may_learn
) {
3225 uint64_t ofpacts_stub
[1024 / 8];
3226 struct ofputil_flow_mod fm
;
3227 struct ofpbuf ofpacts
;
3229 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
3230 xlate_learn_action__(ctx
, learn
, &fm
, &ofpacts
);
3231 ofpbuf_uninit(&ofpacts
);
3236 xlate_fin_timeout__(struct rule_dpif
*rule
, uint16_t tcp_flags
,
3237 uint16_t idle_timeout
, uint16_t hard_timeout
)
3239 if (tcp_flags
& (TCP_FIN
| TCP_RST
)) {
3240 rule_dpif_reduce_timeouts(rule
, idle_timeout
, hard_timeout
);
3245 xlate_fin_timeout(struct xlate_ctx
*ctx
,
3246 const struct ofpact_fin_timeout
*oft
)
3249 xlate_fin_timeout__(ctx
->rule
, ctx
->xin
->tcp_flags
,
3250 oft
->fin_idle_timeout
, oft
->fin_hard_timeout
);
3251 if (ctx
->xin
->xcache
) {
3252 struct xc_entry
*entry
;
3254 entry
= xlate_cache_add_entry(ctx
->xin
->xcache
, XC_FIN_TIMEOUT
);
3255 /* XC_RULE already holds a reference on the rule, none is taken
3257 entry
->u
.fin
.rule
= ctx
->rule
;
3258 entry
->u
.fin
.idle
= oft
->fin_idle_timeout
;
3259 entry
->u
.fin
.hard
= oft
->fin_hard_timeout
;
3265 xlate_sample_action(struct xlate_ctx
*ctx
,
3266 const struct ofpact_sample
*os
)
3268 union user_action_cookie cookie
;
3269 /* Scale the probability from 16-bit to 32-bit while representing
3270 * the same percentage. */
3271 uint32_t probability
= (os
->probability
<< 16) | os
->probability
;
3273 if (!ctx
->xbridge
->variable_length_userdata
) {
3274 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
3276 VLOG_ERR_RL(&rl
, "ignoring NXAST_SAMPLE action because datapath "
3277 "lacks support (needs Linux 3.10+ or kernel module from "
3282 ctx
->xout
->slow
|= commit_odp_actions(&ctx
->xin
->flow
, &ctx
->base_flow
,
3283 &ctx
->xout
->odp_actions
,
3286 compose_flow_sample_cookie(os
->probability
, os
->collector_set_id
,
3287 os
->obs_domain_id
, os
->obs_point_id
, &cookie
);
3288 compose_sample_action(ctx
->xbridge
, &ctx
->xout
->odp_actions
, &ctx
->xin
->flow
,
3289 probability
, &cookie
, sizeof cookie
.flow_sample
);
3293 may_receive(const struct xport
*xport
, struct xlate_ctx
*ctx
)
3295 if (xport
->config
& (is_stp(&ctx
->xin
->flow
)
3296 ? OFPUTIL_PC_NO_RECV_STP
3297 : OFPUTIL_PC_NO_RECV
)) {
3301 /* Only drop packets here if both forwarding and learning are
3302 * disabled. If just learning is enabled, we need to have
3303 * OFPP_NORMAL and the learning action have a look at the packet
3304 * before we can drop it. */
3305 if (!xport_stp_forward_state(xport
) && !xport_stp_learn_state(xport
)) {
3313 xlate_write_actions(struct xlate_ctx
*ctx
, const struct ofpact
*a
)
3315 struct ofpact_nest
*on
= ofpact_get_WRITE_ACTIONS(a
);
3316 ofpbuf_put(&ctx
->action_set
, on
->actions
, ofpact_nest_get_action_len(on
));
3317 ofpact_pad(&ctx
->action_set
);
3321 xlate_action_set(struct xlate_ctx
*ctx
)
3323 uint64_t action_list_stub
[1024 / 64];
3324 struct ofpbuf action_list
;
3326 ofpbuf_use_stub(&action_list
, action_list_stub
, sizeof action_list_stub
);
3327 ofpacts_execute_action_set(&action_list
, &ctx
->action_set
);
3328 do_xlate_actions(ofpbuf_data(&action_list
), ofpbuf_size(&action_list
), ctx
);
3329 ofpbuf_uninit(&action_list
);
3333 do_xlate_actions(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
3334 struct xlate_ctx
*ctx
)
3336 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
3337 struct flow
*flow
= &ctx
->xin
->flow
;
3338 const struct ofpact
*a
;
3340 /* dl_type already in the mask, not set below. */
3342 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
3343 struct ofpact_controller
*controller
;
3344 const struct ofpact_metadata
*metadata
;
3345 const struct ofpact_set_field
*set_field
;
3346 const struct mf_field
*mf
;
3354 xlate_output_action(ctx
, ofpact_get_OUTPUT(a
)->port
,
3355 ofpact_get_OUTPUT(a
)->max_len
, true);
3359 if (xlate_group_action(ctx
, ofpact_get_GROUP(a
)->group_id
)) {
3364 case OFPACT_CONTROLLER
:
3365 controller
= ofpact_get_CONTROLLER(a
);
3366 execute_controller_action(ctx
, controller
->max_len
,
3368 controller
->controller_id
);
3371 case OFPACT_ENQUEUE
:
3372 xlate_enqueue_action(ctx
, ofpact_get_ENQUEUE(a
));
3375 case OFPACT_SET_VLAN_VID
:
3376 wc
->masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
3377 if (flow
->vlan_tci
& htons(VLAN_CFI
) ||
3378 ofpact_get_SET_VLAN_VID(a
)->push_vlan_if_needed
) {
3379 flow
->vlan_tci
&= ~htons(VLAN_VID_MASK
);
3380 flow
->vlan_tci
|= (htons(ofpact_get_SET_VLAN_VID(a
)->vlan_vid
)
3385 case OFPACT_SET_VLAN_PCP
:
3386 wc
->masks
.vlan_tci
|= htons(VLAN_PCP_MASK
| VLAN_CFI
);
3387 if (flow
->vlan_tci
& htons(VLAN_CFI
) ||
3388 ofpact_get_SET_VLAN_PCP(a
)->push_vlan_if_needed
) {
3389 flow
->vlan_tci
&= ~htons(VLAN_PCP_MASK
);
3390 flow
->vlan_tci
|= htons((ofpact_get_SET_VLAN_PCP(a
)->vlan_pcp
3391 << VLAN_PCP_SHIFT
) | VLAN_CFI
);
3395 case OFPACT_STRIP_VLAN
:
3396 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
3397 flow
->vlan_tci
= htons(0);
3400 case OFPACT_PUSH_VLAN
:
3401 /* XXX 802.1AD(QinQ) */
3402 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
3403 flow
->vlan_tci
= htons(VLAN_CFI
);
3406 case OFPACT_SET_ETH_SRC
:
3407 memset(&wc
->masks
.dl_src
, 0xff, sizeof wc
->masks
.dl_src
);
3408 memcpy(flow
->dl_src
, ofpact_get_SET_ETH_SRC(a
)->mac
, ETH_ADDR_LEN
);
3411 case OFPACT_SET_ETH_DST
:
3412 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
3413 memcpy(flow
->dl_dst
, ofpact_get_SET_ETH_DST(a
)->mac
, ETH_ADDR_LEN
);
3416 case OFPACT_SET_IPV4_SRC
:
3417 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
3418 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
3419 flow
->nw_src
= ofpact_get_SET_IPV4_SRC(a
)->ipv4
;
3423 case OFPACT_SET_IPV4_DST
:
3424 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
3425 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
3426 flow
->nw_dst
= ofpact_get_SET_IPV4_DST(a
)->ipv4
;
3430 case OFPACT_SET_IP_DSCP
:
3431 if (is_ip_any(flow
)) {
3432 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
3433 flow
->nw_tos
&= ~IP_DSCP_MASK
;
3434 flow
->nw_tos
|= ofpact_get_SET_IP_DSCP(a
)->dscp
;
3438 case OFPACT_SET_IP_ECN
:
3439 if (is_ip_any(flow
)) {
3440 wc
->masks
.nw_tos
|= IP_ECN_MASK
;
3441 flow
->nw_tos
&= ~IP_ECN_MASK
;
3442 flow
->nw_tos
|= ofpact_get_SET_IP_ECN(a
)->ecn
;
3446 case OFPACT_SET_IP_TTL
:
3447 if (is_ip_any(flow
)) {
3448 wc
->masks
.nw_ttl
= 0xff;
3449 flow
->nw_ttl
= ofpact_get_SET_IP_TTL(a
)->ttl
;
3453 case OFPACT_SET_L4_SRC_PORT
:
3454 if (is_ip_any(flow
)) {
3455 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
3456 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
3457 flow
->tp_src
= htons(ofpact_get_SET_L4_SRC_PORT(a
)->port
);
3461 case OFPACT_SET_L4_DST_PORT
:
3462 if (is_ip_any(flow
)) {
3463 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
3464 memset(&wc
->masks
.tp_dst
, 0xff, sizeof wc
->masks
.tp_dst
);
3465 flow
->tp_dst
= htons(ofpact_get_SET_L4_DST_PORT(a
)->port
);
3469 case OFPACT_RESUBMIT
:
3470 xlate_ofpact_resubmit(ctx
, ofpact_get_RESUBMIT(a
));
3473 case OFPACT_SET_TUNNEL
:
3474 flow
->tunnel
.tun_id
= htonll(ofpact_get_SET_TUNNEL(a
)->tun_id
);
3477 case OFPACT_SET_QUEUE
:
3478 xlate_set_queue_action(ctx
, ofpact_get_SET_QUEUE(a
)->queue_id
);
3481 case OFPACT_POP_QUEUE
:
3482 flow
->skb_priority
= ctx
->orig_skb_priority
;
3485 case OFPACT_REG_MOVE
:
3486 nxm_execute_reg_move(ofpact_get_REG_MOVE(a
), flow
, wc
);
3489 case OFPACT_REG_LOAD
:
3490 nxm_execute_reg_load(ofpact_get_REG_LOAD(a
), flow
, wc
);
3493 case OFPACT_SET_FIELD
:
3494 set_field
= ofpact_get_SET_FIELD(a
);
3495 mf
= set_field
->field
;
3497 /* Set field action only ever overwrites packet's outermost
3498 * applicable header fields. Do nothing if no header exists. */
3499 if (mf
->id
== MFF_VLAN_VID
) {
3500 wc
->masks
.vlan_tci
|= htons(VLAN_CFI
);
3501 if (!(flow
->vlan_tci
& htons(VLAN_CFI
))) {
3504 } else if ((mf
->id
== MFF_MPLS_LABEL
|| mf
->id
== MFF_MPLS_TC
)
3505 /* 'dl_type' is already unwildcarded. */
3506 && !eth_type_mpls(flow
->dl_type
)) {
3510 mf_mask_field_and_prereqs(mf
, &wc
->masks
);
3511 mf_set_flow_value(mf
, &set_field
->value
, flow
);
3514 case OFPACT_STACK_PUSH
:
3515 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a
), flow
, wc
,
3519 case OFPACT_STACK_POP
:
3520 nxm_execute_stack_pop(ofpact_get_STACK_POP(a
), flow
, wc
,
3524 case OFPACT_PUSH_MPLS
:
3525 compose_mpls_push_action(ctx
, ofpact_get_PUSH_MPLS(a
));
3528 case OFPACT_POP_MPLS
:
3529 compose_mpls_pop_action(ctx
, ofpact_get_POP_MPLS(a
)->ethertype
);
3532 case OFPACT_SET_MPLS_LABEL
:
3533 compose_set_mpls_label_action(
3534 ctx
, ofpact_get_SET_MPLS_LABEL(a
)->label
);
3537 case OFPACT_SET_MPLS_TC
:
3538 compose_set_mpls_tc_action(ctx
, ofpact_get_SET_MPLS_TC(a
)->tc
);
3541 case OFPACT_SET_MPLS_TTL
:
3542 compose_set_mpls_ttl_action(ctx
, ofpact_get_SET_MPLS_TTL(a
)->ttl
);
3545 case OFPACT_DEC_MPLS_TTL
:
3546 if (compose_dec_mpls_ttl_action(ctx
)) {
3551 case OFPACT_DEC_TTL
:
3552 wc
->masks
.nw_ttl
= 0xff;
3553 if (compose_dec_ttl(ctx
, ofpact_get_DEC_TTL(a
))) {
3559 /* Nothing to do. */
3562 case OFPACT_MULTIPATH
:
3563 multipath_execute(ofpact_get_MULTIPATH(a
), flow
, wc
);
3567 xlate_bundle_action(ctx
, ofpact_get_BUNDLE(a
));
3570 case OFPACT_OUTPUT_REG
:
3571 xlate_output_reg_action(ctx
, ofpact_get_OUTPUT_REG(a
));
3575 xlate_learn_action(ctx
, ofpact_get_LEARN(a
));
3582 case OFPACT_FIN_TIMEOUT
:
3583 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
3584 ctx
->xout
->has_fin_timeout
= true;
3585 xlate_fin_timeout(ctx
, ofpact_get_FIN_TIMEOUT(a
));
3588 case OFPACT_CLEAR_ACTIONS
:
3589 ofpbuf_clear(&ctx
->action_set
);
3592 case OFPACT_WRITE_ACTIONS
:
3593 xlate_write_actions(ctx
, a
);
3596 case OFPACT_WRITE_METADATA
:
3597 metadata
= ofpact_get_WRITE_METADATA(a
);
3598 flow
->metadata
&= ~metadata
->mask
;
3599 flow
->metadata
|= metadata
->metadata
& metadata
->mask
;
3603 /* Not implemented yet. */
3606 case OFPACT_GOTO_TABLE
: {
3607 struct ofpact_goto_table
*ogt
= ofpact_get_GOTO_TABLE(a
);
3609 ovs_assert(ctx
->table_id
< ogt
->table_id
);
3610 xlate_table_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
,
3611 ogt
->table_id
, true, true);
3616 xlate_sample_action(ctx
, ofpact_get_SAMPLE(a
));
3623 xlate_in_init(struct xlate_in
*xin
, struct ofproto_dpif
*ofproto
,
3624 const struct flow
*flow
, struct rule_dpif
*rule
,
3625 uint16_t tcp_flags
, const struct ofpbuf
*packet
)
3627 xin
->ofproto
= ofproto
;
3629 xin
->packet
= packet
;
3630 xin
->may_learn
= packet
!= NULL
;
3633 xin
->ofpacts
= NULL
;
3634 xin
->ofpacts_len
= 0;
3635 xin
->tcp_flags
= tcp_flags
;
3636 xin
->resubmit_hook
= NULL
;
3637 xin
->report_hook
= NULL
;
3638 xin
->resubmit_stats
= NULL
;
3639 xin
->skip_wildcards
= false;
3643 xlate_out_uninit(struct xlate_out
*xout
)
3646 ofpbuf_uninit(&xout
->odp_actions
);
3650 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
3651 * into datapath actions, using 'ctx', and discards the datapath actions. */
3653 xlate_actions_for_side_effects(struct xlate_in
*xin
)
3655 struct xlate_out xout
;
3657 xlate_actions(xin
, &xout
);
3658 xlate_out_uninit(&xout
);
3662 xlate_report(struct xlate_ctx
*ctx
, const char *s
)
3664 if (ctx
->xin
->report_hook
) {
3665 ctx
->xin
->report_hook(ctx
->xin
, s
, ctx
->recurse
);
3670 xlate_out_copy(struct xlate_out
*dst
, const struct xlate_out
*src
)
3673 dst
->slow
= src
->slow
;
3674 dst
->has_learn
= src
->has_learn
;
3675 dst
->has_normal
= src
->has_normal
;
3676 dst
->has_fin_timeout
= src
->has_fin_timeout
;
3677 dst
->nf_output_iface
= src
->nf_output_iface
;
3678 dst
->mirrors
= src
->mirrors
;
3680 ofpbuf_use_stub(&dst
->odp_actions
, dst
->odp_actions_stub
,
3681 sizeof dst
->odp_actions_stub
);
3682 ofpbuf_put(&dst
->odp_actions
, ofpbuf_data(&src
->odp_actions
),
3683 ofpbuf_size(&src
->odp_actions
));
3686 static struct skb_priority_to_dscp
*
3687 get_skb_priority(const struct xport
*xport
, uint32_t skb_priority
)
3689 struct skb_priority_to_dscp
*pdscp
;
3692 hash
= hash_int(skb_priority
, 0);
3693 HMAP_FOR_EACH_IN_BUCKET (pdscp
, hmap_node
, hash
, &xport
->skb_priorities
) {
3694 if (pdscp
->skb_priority
== skb_priority
) {
3702 dscp_from_skb_priority(const struct xport
*xport
, uint32_t skb_priority
,
3705 struct skb_priority_to_dscp
*pdscp
= get_skb_priority(xport
, skb_priority
);
3706 *dscp
= pdscp
? pdscp
->dscp
: 0;
3707 return pdscp
!= NULL
;
3711 clear_skb_priorities(struct xport
*xport
)
3713 struct skb_priority_to_dscp
*pdscp
, *next
;
3715 HMAP_FOR_EACH_SAFE (pdscp
, next
, hmap_node
, &xport
->skb_priorities
) {
3716 hmap_remove(&xport
->skb_priorities
, &pdscp
->hmap_node
);
3722 actions_output_to_local_port(const struct xlate_ctx
*ctx
)
3724 odp_port_t local_odp_port
= ofp_port_to_odp_port(ctx
->xbridge
, OFPP_LOCAL
);
3725 const struct nlattr
*a
;
3728 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, ofpbuf_data(&ctx
->xout
->odp_actions
),
3729 ofpbuf_size(&ctx
->xout
->odp_actions
)) {
3730 if (nl_attr_type(a
) == OVS_ACTION_ATTR_OUTPUT
3731 && nl_attr_get_odp_port(a
) == local_odp_port
) {
3738 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
3739 * into datapath actions in 'odp_actions', using 'ctx'.
3741 * The caller must take responsibility for eventually freeing 'xout', with
3742 * xlate_out_uninit(). */
3744 xlate_actions(struct xlate_in
*xin
, struct xlate_out
*xout
)
3746 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
3747 struct flow_wildcards
*wc
= &xout
->wc
;
3748 struct flow
*flow
= &xin
->flow
;
3749 struct rule_dpif
*rule
= NULL
;
3751 const struct rule_actions
*actions
= NULL
;
3752 enum slow_path_reason special
;
3753 const struct ofpact
*ofpacts
;
3754 struct xport
*in_port
;
3755 struct flow orig_flow
;
3756 struct xlate_ctx ctx
;
3761 COVERAGE_INC(xlate_actions
);
3763 /* Flow initialization rules:
3764 * - 'base_flow' must match the kernel's view of the packet at the
3765 * time that action processing starts. 'flow' represents any
3766 * transformations we wish to make through actions.
3767 * - By default 'base_flow' and 'flow' are the same since the input
3768 * packet matches the output before any actions are applied.
3769 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
3770 * of the received packet as seen by the kernel. If we later output
3771 * to another device without any modifications this will cause us to
3772 * insert a new tag since the original one was stripped off by the
3774 * - Tunnel metadata as received is retained in 'flow'. This allows
3775 * tunnel metadata matching also in later tables.
3776 * Since a kernel action for setting the tunnel metadata will only be
3777 * generated with actual tunnel output, changing the tunnel metadata
3778 * values in 'flow' (such as tun_id) will only have effect with a later
3779 * tunnel output action.
3780 * - Tunnel 'base_flow' is completely cleared since that is what the
3781 * kernel does. If we wish to maintain the original values an action
3782 * needs to be generated. */
3787 ctx
.xout
->has_learn
= false;
3788 ctx
.xout
->has_normal
= false;
3789 ctx
.xout
->has_fin_timeout
= false;
3790 ctx
.xout
->nf_output_iface
= NF_OUT_DROP
;
3791 ctx
.xout
->mirrors
= 0;
3792 ofpbuf_use_stub(&ctx
.xout
->odp_actions
, ctx
.xout
->odp_actions_stub
,
3793 sizeof ctx
.xout
->odp_actions_stub
);
3794 ofpbuf_reserve(&ctx
.xout
->odp_actions
, NL_A_U32_SIZE
);
3796 ctx
.xbridge
= xbridge_lookup(xcfg
, xin
->ofproto
);
3801 ctx
.rule
= xin
->rule
;
3803 ctx
.base_flow
= *flow
;
3804 memset(&ctx
.base_flow
.tunnel
, 0, sizeof ctx
.base_flow
.tunnel
);
3805 ctx
.orig_tunnel_ip_dst
= flow
->tunnel
.ip_dst
;
3807 flow_wildcards_init_catchall(wc
);
3808 memset(&wc
->masks
.in_port
, 0xff, sizeof wc
->masks
.in_port
);
3809 memset(&wc
->masks
.skb_priority
, 0xff, sizeof wc
->masks
.skb_priority
);
3810 memset(&wc
->masks
.dl_type
, 0xff, sizeof wc
->masks
.dl_type
);
3811 if (is_ip_any(flow
)) {
3812 wc
->masks
.nw_frag
|= FLOW_NW_FRAG_MASK
;
3814 is_icmp
= is_icmpv4(flow
) || is_icmpv6(flow
);
3816 tnl_may_send
= tnl_xlate_init(&ctx
.base_flow
, flow
, wc
);
3817 if (ctx
.xbridge
->netflow
) {
3818 netflow_mask_wc(flow
, wc
);
3823 ctx
.in_group
= false;
3824 ctx
.orig_skb_priority
= flow
->skb_priority
;
3827 ctx
.use_recirc
= false;
3829 if (!xin
->ofpacts
&& !ctx
.rule
) {
3830 ctx
.table_id
= rule_dpif_lookup(ctx
.xbridge
->ofproto
, flow
,
3831 !xin
->skip_wildcards
? wc
: NULL
,
3832 &rule
, ctx
.xin
->xcache
!= NULL
,
3833 ctx
.xin
->resubmit_stats
);
3834 if (ctx
.xin
->resubmit_stats
) {
3835 rule_dpif_credit_stats(rule
, ctx
.xin
->resubmit_stats
);
3837 if (ctx
.xin
->xcache
) {
3838 struct xc_entry
*entry
;
3840 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_RULE
);
3841 entry
->u
.rule
= rule
;
3845 xout
->fail_open
= ctx
.rule
&& rule_dpif_is_fail_open(ctx
.rule
);
3848 ofpacts
= xin
->ofpacts
;
3849 ofpacts_len
= xin
->ofpacts_len
;
3850 } else if (ctx
.rule
) {
3851 actions
= rule_dpif_get_actions(ctx
.rule
);
3852 ofpacts
= actions
->ofpacts
;
3853 ofpacts_len
= actions
->ofpacts_len
;
3858 ofpbuf_use_stub(&ctx
.stack
, ctx
.init_stack
, sizeof ctx
.init_stack
);
3859 ofpbuf_use_stub(&ctx
.action_set
,
3860 ctx
.action_set_stub
, sizeof ctx
.action_set_stub
);
3862 if (mbridge_has_mirrors(ctx
.xbridge
->mbridge
)) {
3863 /* Do this conditionally because the copy is expensive enough that it
3864 * shows up in profiles. */
3868 if (flow
->nw_frag
& FLOW_NW_FRAG_ANY
) {
3869 switch (ctx
.xbridge
->frag
) {
3870 case OFPC_FRAG_NORMAL
:
3871 /* We must pretend that transport ports are unavailable. */
3872 flow
->tp_src
= ctx
.base_flow
.tp_src
= htons(0);
3873 flow
->tp_dst
= ctx
.base_flow
.tp_dst
= htons(0);
3876 case OFPC_FRAG_DROP
:
3879 case OFPC_FRAG_REASM
:
3882 case OFPC_FRAG_NX_MATCH
:
3883 /* Nothing to do. */
3886 case OFPC_INVALID_TTL_TO_CONTROLLER
:
3891 in_port
= get_ofp_port(ctx
.xbridge
, flow
->in_port
.ofp_port
);
3892 if (in_port
&& in_port
->is_tunnel
) {
3893 if (ctx
.xin
->resubmit_stats
) {
3894 netdev_vport_inc_rx(in_port
->netdev
, ctx
.xin
->resubmit_stats
);
3896 bfd_account_rx(in_port
->bfd
, ctx
.xin
->resubmit_stats
);
3899 if (ctx
.xin
->xcache
) {
3900 struct xc_entry
*entry
;
3902 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_NETDEV
);
3903 entry
->u
.dev
.rx
= netdev_ref(in_port
->netdev
);
3904 entry
->u
.dev
.bfd
= bfd_ref(in_port
->bfd
);
3908 special
= process_special(&ctx
, flow
, in_port
, ctx
.xin
->packet
);
3910 ctx
.xout
->slow
|= special
;
3912 size_t sample_actions_len
;
3914 if (flow
->in_port
.ofp_port
3915 != vsp_realdev_to_vlandev(ctx
.xbridge
->ofproto
,
3916 flow
->in_port
.ofp_port
,
3918 ctx
.base_flow
.vlan_tci
= 0;
3921 add_sflow_action(&ctx
);
3922 add_ipfix_action(&ctx
);
3923 sample_actions_len
= ofpbuf_size(&ctx
.xout
->odp_actions
);
3925 if (tnl_may_send
&& (!in_port
|| may_receive(in_port
, &ctx
))) {
3926 do_xlate_actions(ofpacts
, ofpacts_len
, &ctx
);
3928 /* We've let OFPP_NORMAL and the learning action look at the
3929 * packet, so drop it now if forwarding is disabled. */
3930 if (in_port
&& !xport_stp_forward_state(in_port
)) {
3931 ofpbuf_set_size(&ctx
.xout
->odp_actions
, sample_actions_len
);
3935 if (ofpbuf_size(&ctx
.action_set
)) {
3936 xlate_action_set(&ctx
);
3939 if (ctx
.xbridge
->has_in_band
3940 && in_band_must_output_to_local_port(flow
)
3941 && !actions_output_to_local_port(&ctx
)) {
3942 compose_output_action(&ctx
, OFPP_LOCAL
);
3945 fix_sflow_action(&ctx
);
3947 if (mbridge_has_mirrors(ctx
.xbridge
->mbridge
)) {
3948 add_mirror_actions(&ctx
, &orig_flow
);
3952 if (nl_attr_oversized(ofpbuf_size(&ctx
.xout
->odp_actions
))) {
3953 /* These datapath actions are too big for a Netlink attribute, so we
3954 * can't hand them to the kernel directly. dpif_execute() can execute
3955 * them one by one with help, so just mark the result as SLOW_ACTION to
3956 * prevent the flow from being installed. */
3957 COVERAGE_INC(xlate_actions_oversize
);
3958 ctx
.xout
->slow
|= SLOW_ACTION
;
3961 if (mbridge_has_mirrors(ctx
.xbridge
->mbridge
)) {
3962 if (ctx
.xin
->resubmit_stats
) {
3963 mirror_update_stats(ctx
.xbridge
->mbridge
, xout
->mirrors
,
3964 ctx
.xin
->resubmit_stats
->n_packets
,
3965 ctx
.xin
->resubmit_stats
->n_bytes
);
3967 if (ctx
.xin
->xcache
) {
3968 struct xc_entry
*entry
;
3970 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_MIRROR
);
3971 entry
->u
.mirror
.mbridge
= mbridge_ref(ctx
.xbridge
->mbridge
);
3972 entry
->u
.mirror
.mirrors
= xout
->mirrors
;
3976 if (ctx
.xbridge
->netflow
) {
3977 /* Only update netflow if we don't have controller flow. We don't
3978 * report NetFlow expiration messages for such facets because they
3979 * are just part of the control logic for the network, not real
3981 if (ofpacts_len
== 0
3982 || ofpacts
->type
!= OFPACT_CONTROLLER
3983 || ofpact_next(ofpacts
) < ofpact_end(ofpacts
, ofpacts_len
)) {
3984 if (ctx
.xin
->resubmit_stats
) {
3985 netflow_flow_update(ctx
.xbridge
->netflow
, flow
,
3986 xout
->nf_output_iface
,
3987 ctx
.xin
->resubmit_stats
);
3989 if (ctx
.xin
->xcache
) {
3990 struct xc_entry
*entry
;
3992 entry
= xlate_cache_add_entry(ctx
.xin
->xcache
, XC_NETFLOW
);
3993 entry
->u
.nf
.netflow
= netflow_ref(ctx
.xbridge
->netflow
);
3994 entry
->u
.nf
.flow
= xmemdup(flow
, sizeof *flow
);
3995 entry
->u
.nf
.iface
= xout
->nf_output_iface
;
4000 ofpbuf_uninit(&ctx
.stack
);
4001 ofpbuf_uninit(&ctx
.action_set
);
4003 /* Clear the metadata and register wildcard masks, because we won't
4004 * use non-header fields as part of the cache. */
4005 flow_wildcards_clear_non_packet_fields(wc
);
4007 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow uses
4008 * the low 8 bits of the 16-bit tp_src and tp_dst members to represent
4009 * these fields. The datapath interface, on the other hand, represents
4010 * them with just 8 bits each. This means that if the high 8 bits of the
4011 * masks for these fields somehow become set, then they will get chopped
4012 * off by a round trip through the datapath, and revalidation will spot
4013 * that as an inconsistency and delete the flow. Avoid the problem here by
4014 * making sure that only the low 8 bits of either field can be unwildcarded
4018 wc
->masks
.tp_src
&= htons(UINT8_MAX
);
4019 wc
->masks
.tp_dst
&= htons(UINT8_MAX
);
4023 /* Sends 'packet' out 'ofport'.
4024 * May modify 'packet'.
4025 * Returns 0 if successful, otherwise a positive errno value. */
4027 xlate_send_packet(const struct ofport_dpif
*ofport
, struct ofpbuf
*packet
)
4029 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
4030 struct xport
*xport
;
4031 struct ofpact_output output
;
4034 ofpact_init(&output
.ofpact
, OFPACT_OUTPUT
, sizeof output
);
4035 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
4036 flow_extract(packet
, NULL
, &flow
);
4037 flow
.in_port
.ofp_port
= OFPP_NONE
;
4039 xport
= xport_lookup(xcfg
, ofport
);
4043 output
.port
= xport
->ofp_port
;
4046 return ofproto_dpif_execute_actions(xport
->xbridge
->ofproto
, &flow
, NULL
,
4047 &output
.ofpact
, sizeof output
,
4051 struct xlate_cache
*
4052 xlate_cache_new(void)
4054 struct xlate_cache
*xcache
= xmalloc(sizeof *xcache
);
4056 ofpbuf_init(&xcache
->entries
, 512);
4060 static struct xc_entry
*
4061 xlate_cache_add_entry(struct xlate_cache
*xcache
, enum xc_type type
)
4063 struct xc_entry
*entry
;
4065 entry
= ofpbuf_put_zeros(&xcache
->entries
, sizeof *entry
);
4072 xlate_cache_netdev(struct xc_entry
*entry
, const struct dpif_flow_stats
*stats
)
4074 if (entry
->u
.dev
.tx
) {
4075 netdev_vport_inc_tx(entry
->u
.dev
.tx
, stats
);
4077 if (entry
->u
.dev
.rx
) {
4078 netdev_vport_inc_rx(entry
->u
.dev
.rx
, stats
);
4080 if (entry
->u
.dev
.bfd
) {
4081 bfd_account_rx(entry
->u
.dev
.bfd
, stats
);
4086 xlate_cache_normal(struct ofproto_dpif
*ofproto
, struct flow
*flow
, int vlan
)
4088 struct xlate_cfg
*xcfg
= ovsrcu_get(struct xlate_cfg
*, &xcfgp
);
4089 struct xbridge
*xbridge
;
4090 struct xbundle
*xbundle
;
4091 struct flow_wildcards wc
;
4093 xbridge
= xbridge_lookup(xcfg
, ofproto
);
4098 xbundle
= lookup_input_bundle(xbridge
, flow
->in_port
.ofp_port
, false,
4104 update_learning_table(xbridge
, flow
, &wc
, vlan
, xbundle
);
4107 /* Push stats and perform side effects of flow translation. */
4109 xlate_push_stats(struct xlate_cache
*xcache
, bool may_learn
,
4110 const struct dpif_flow_stats
*stats
)
4112 struct xc_entry
*entry
;
4113 struct ofpbuf entries
= xcache
->entries
;
4115 XC_ENTRY_FOR_EACH (entry
, entries
, xcache
) {
4116 switch (entry
->type
) {
4118 rule_dpif_credit_stats(entry
->u
.rule
, stats
);
4121 bond_account(entry
->u
.bond
.bond
, entry
->u
.bond
.flow
,
4122 entry
->u
.bond
.vid
, stats
->n_bytes
);
4125 xlate_cache_netdev(entry
, stats
);
4128 netflow_flow_update(entry
->u
.nf
.netflow
, entry
->u
.nf
.flow
,
4129 entry
->u
.nf
.iface
, stats
);
4132 mirror_update_stats(entry
->u
.mirror
.mbridge
,
4133 entry
->u
.mirror
.mirrors
,
4134 stats
->n_packets
, stats
->n_bytes
);
4138 ofproto_dpif_flow_mod(entry
->u
.learn
.ofproto
,
4143 xlate_cache_normal(entry
->u
.normal
.ofproto
, entry
->u
.normal
.flow
,
4144 entry
->u
.normal
.vlan
);
4146 case XC_FIN_TIMEOUT
:
4147 xlate_fin_timeout__(entry
->u
.fin
.rule
, stats
->tcp_flags
,
4148 entry
->u
.fin
.idle
, entry
->u
.fin
.hard
);
4151 group_dpif_credit_stats(entry
->u
.group
.group
, entry
->u
.group
.bucket
,
4161 xlate_dev_unref(struct xc_entry
*entry
)
4163 if (entry
->u
.dev
.tx
) {
4164 netdev_close(entry
->u
.dev
.tx
);
4166 if (entry
->u
.dev
.rx
) {
4167 netdev_close(entry
->u
.dev
.rx
);
4169 if (entry
->u
.dev
.bfd
) {
4170 bfd_unref(entry
->u
.dev
.bfd
);
4175 xlate_cache_clear_netflow(struct netflow
*netflow
, struct flow
*flow
)
4177 netflow_flow_clear(netflow
, flow
);
4178 netflow_unref(netflow
);
4183 xlate_cache_clear(struct xlate_cache
*xcache
)
4185 struct xc_entry
*entry
;
4186 struct ofpbuf entries
;
4192 XC_ENTRY_FOR_EACH (entry
, entries
, xcache
) {
4193 switch (entry
->type
) {
4195 rule_dpif_unref(entry
->u
.rule
);
4198 free(entry
->u
.bond
.flow
);
4199 bond_unref(entry
->u
.bond
.bond
);
4202 xlate_dev_unref(entry
);
4205 xlate_cache_clear_netflow(entry
->u
.nf
.netflow
, entry
->u
.nf
.flow
);
4208 mbridge_unref(entry
->u
.mirror
.mbridge
);
4211 free(entry
->u
.learn
.fm
);
4212 ofpbuf_delete(entry
->u
.learn
.ofpacts
);
4215 free(entry
->u
.normal
.flow
);
4217 case XC_FIN_TIMEOUT
:
4218 /* 'u.fin.rule' is always already held as a XC_RULE, which
4219 * has already released it's reference above. */
4222 group_dpif_unref(entry
->u
.group
.group
);
4229 ofpbuf_clear(&xcache
->entries
);
4233 xlate_cache_delete(struct xlate_cache
*xcache
)
4235 xlate_cache_clear(xcache
);
4236 ofpbuf_uninit(&xcache
->entries
);