1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. */
17 #include "ofproto/ofproto-dpif-xlate.h"
25 #include "byte-order.h"
30 #include "dynamic-string.h"
35 #include "mac-learning.h"
36 #include "meta-flow.h"
37 #include "multipath.h"
38 #include "netdev-vport.h"
41 #include "odp-execute.h"
42 #include "ofp-actions.h"
43 #include "ofproto/ofproto-dpif-ipfix.h"
44 #include "ofproto/ofproto-dpif-mirror.h"
45 #include "ofproto/ofproto-dpif-monitor.h"
46 #include "ofproto/ofproto-dpif-sflow.h"
47 #include "ofproto/ofproto-dpif.h"
48 #include "ofproto/ofproto-provider.h"
52 COVERAGE_DEFINE(xlate_actions
);
53 COVERAGE_DEFINE(xlate_actions_oversize
);
55 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate
);
57 /* Maximum depth of flow table recursion (due to resubmit actions) in a
58 * flow translation. */
59 #define MAX_RESUBMIT_RECURSION 64
61 /* Maximum number of resubmit actions in a flow translation, whether they are
62 * recursive or not. */
63 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
65 struct ovs_rwlock xlate_rwlock
= OVS_RWLOCK_INITIALIZER
;
68 struct hmap_node hmap_node
; /* Node in global 'xbridges' map. */
69 struct ofproto_dpif
*ofproto
; /* Key in global 'xbridges' map. */
71 struct list xbundles
; /* Owned xbundles. */
72 struct hmap xports
; /* Indexed by ofp_port. */
74 char *name
; /* Name used in log messages. */
75 struct dpif
*dpif
; /* Datapath interface. */
76 struct mac_learning
*ml
; /* Mac learning handle. */
77 struct mbridge
*mbridge
; /* Mirroring. */
78 struct dpif_sflow
*sflow
; /* SFlow handle, or null. */
79 struct dpif_ipfix
*ipfix
; /* Ipfix handle, or null. */
80 struct netflow
*netflow
; /* Netflow handle, or null. */
81 struct stp
*stp
; /* STP or null if disabled. */
83 /* Special rules installed by ofproto-dpif. */
84 struct rule_dpif
*miss_rule
;
85 struct rule_dpif
*no_packet_in_rule
;
87 enum ofp_config_flags frag
; /* Fragmentation handling. */
88 bool has_in_band
; /* Bridge has in band control? */
89 bool forward_bpdu
; /* Bridge forwards STP BPDUs? */
93 struct hmap_node hmap_node
; /* In global 'xbundles' map. */
94 struct ofbundle
*ofbundle
; /* Key in global 'xbundles' map. */
96 struct list list_node
; /* In parent 'xbridges' list. */
97 struct xbridge
*xbridge
; /* Parent xbridge. */
99 struct list xports
; /* Contains "struct xport"s. */
101 char *name
; /* Name used in log messages. */
102 struct bond
*bond
; /* Nonnull iff more than one port. */
103 struct lacp
*lacp
; /* LACP handle or null. */
105 enum port_vlan_mode vlan_mode
; /* VLAN mode. */
106 int vlan
; /* -1=trunk port, else a 12-bit VLAN ID. */
107 unsigned long *trunks
; /* Bitmap of trunked VLANs, if 'vlan' == -1.
108 * NULL if all VLANs are trunked. */
109 bool use_priority_tags
; /* Use 802.1p tag for frames in VLAN 0? */
110 bool floodable
; /* No port has OFPUTIL_PC_NO_FLOOD set? */
114 struct hmap_node hmap_node
; /* Node in global 'xports' map. */
115 struct ofport_dpif
*ofport
; /* Key in global 'xports map. */
117 struct hmap_node ofp_node
; /* Node in parent xbridge 'xports' map. */
118 ofp_port_t ofp_port
; /* Key in parent xbridge 'xports' map. */
120 odp_port_t odp_port
; /* Datapath port number or ODPP_NONE. */
122 struct list bundle_node
; /* In parent xbundle (if it exists). */
123 struct xbundle
*xbundle
; /* Parent xbundle or null. */
125 struct netdev
*netdev
; /* 'ofport''s netdev. */
127 struct xbridge
*xbridge
; /* Parent bridge. */
128 struct xport
*peer
; /* Patch port peer or null. */
130 enum ofputil_port_config config
; /* OpenFlow port configuration. */
131 enum ofputil_port_state state
; /* OpenFlow port state. */
132 int stp_port_no
; /* STP port number or -1 if not in use. */
134 struct hmap skb_priorities
; /* Map of 'skb_priority_to_dscp's. */
136 bool may_enable
; /* May be enabled in bonds. */
137 bool is_tunnel
; /* Is a tunnel port. */
139 struct cfm
*cfm
; /* CFM handle or null. */
140 struct bfd
*bfd
; /* BFD handle or null. */
144 struct xlate_in
*xin
;
145 struct xlate_out
*xout
;
147 const struct xbridge
*xbridge
;
149 /* Flow at the last commit. */
150 struct flow base_flow
;
152 /* Tunnel IP destination address as received. This is stored separately
153 * as the base_flow.tunnel is cleared on init to reflect the datapath
154 * behavior. Used to make sure not to send tunneled output to ourselves,
155 * which might lead to an infinite loop. This could happen easily
156 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
157 * actually set the tun_dst field. */
158 ovs_be32 orig_tunnel_ip_dst
;
160 /* Stack for the push and pop actions. Each stack element is of type
161 * "union mf_subvalue". */
162 union mf_subvalue init_stack
[1024 / sizeof(union mf_subvalue
)];
165 /* The rule that we are currently translating, or NULL. */
166 struct rule_dpif
*rule
;
168 int mpls_depth_delta
; /* Delta of the mpls stack depth since
169 * actions were last committed.
170 * Must be between -1 and 1 inclusive. */
171 ovs_be32 pre_push_mpls_lse
; /* Used to record the top-most MPLS LSE
172 * prior to an mpls_push so that it may be
173 * used for a subsequent mpls_pop. */
175 /* Resubmit statistics, via xlate_table_action(). */
176 int recurse
; /* Current resubmit nesting depth. */
177 int resubmits
; /* Total number of resubmits. */
179 uint32_t orig_skb_priority
; /* Priority when packet arrived. */
180 uint8_t table_id
; /* OpenFlow table ID where flow was found. */
181 uint32_t sflow_n_outputs
; /* Number of output ports. */
182 odp_port_t sflow_odp_port
; /* Output port for composing sFlow action. */
183 uint16_t user_cookie_offset
;/* Used for user_action_cookie fixup. */
184 bool exit
; /* No further actions should be processed. */
186 /* OpenFlow 1.1+ action set.
188 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
189 * When translation is otherwise complete, ofpacts_execute_action_set()
190 * converts it to a set of "struct ofpact"s that can be translated into
191 * datapath actions. */
192 struct ofpbuf action_set
; /* Action set. */
193 uint64_t action_set_stub
[1024 / 8];
196 /* A controller may use OFPP_NONE as the ingress port to indicate that
197 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
198 * when an input bundle is needed for validation (e.g., mirroring or
199 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
200 * any 'port' structs, so care must be taken when dealing with it.
201 * The bundle's name and vlan mode are initialized in lookup_input_bundle() */
202 static struct xbundle ofpp_none_bundle
;
204 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
205 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
206 * traffic egressing the 'ofport' with that priority should be marked with. */
207 struct skb_priority_to_dscp
{
208 struct hmap_node hmap_node
; /* Node in 'ofport_dpif''s 'skb_priorities'. */
209 uint32_t skb_priority
; /* Priority of this queue (see struct flow). */
211 uint8_t dscp
; /* DSCP bits to mark outgoing traffic with. */
214 static struct hmap xbridges
= HMAP_INITIALIZER(&xbridges
);
215 static struct hmap xbundles
= HMAP_INITIALIZER(&xbundles
);
216 static struct hmap xports
= HMAP_INITIALIZER(&xports
);
218 static bool may_receive(const struct xport
*, struct xlate_ctx
*);
219 static void do_xlate_actions(const struct ofpact
*, size_t ofpacts_len
,
221 static void xlate_actions__(struct xlate_in
*, struct xlate_out
*)
222 OVS_REQ_RDLOCK(xlate_rwlock
);
223 static void xlate_normal(struct xlate_ctx
*);
224 static void xlate_report(struct xlate_ctx
*, const char *);
225 static void xlate_table_action(struct xlate_ctx
*, ofp_port_t in_port
,
226 uint8_t table_id
, bool may_packet_in
);
227 static bool input_vid_is_valid(uint16_t vid
, struct xbundle
*, bool warn
);
228 static uint16_t input_vid_to_vlan(const struct xbundle
*, uint16_t vid
);
229 static void output_normal(struct xlate_ctx
*, const struct xbundle
*,
231 static void compose_output_action(struct xlate_ctx
*, ofp_port_t ofp_port
);
233 static struct xbridge
*xbridge_lookup(const struct ofproto_dpif
*);
234 static struct xbundle
*xbundle_lookup(const struct ofbundle
*);
235 static struct xport
*xport_lookup(const struct ofport_dpif
*);
236 static struct xport
*get_ofp_port(const struct xbridge
*, ofp_port_t ofp_port
);
237 static struct skb_priority_to_dscp
*get_skb_priority(const struct xport
*,
238 uint32_t skb_priority
);
239 static void clear_skb_priorities(struct xport
*);
240 static bool dscp_from_skb_priority(const struct xport
*, uint32_t skb_priority
,
244 xlate_ofproto_set(struct ofproto_dpif
*ofproto
, const char *name
,
245 struct dpif
*dpif
, struct rule_dpif
*miss_rule
,
246 struct rule_dpif
*no_packet_in_rule
,
247 const struct mac_learning
*ml
, struct stp
*stp
,
248 const struct mbridge
*mbridge
,
249 const struct dpif_sflow
*sflow
,
250 const struct dpif_ipfix
*ipfix
,
251 const struct netflow
*netflow
, enum ofp_config_flags frag
,
252 bool forward_bpdu
, bool has_in_band
)
254 struct xbridge
*xbridge
= xbridge_lookup(ofproto
);
257 xbridge
= xzalloc(sizeof *xbridge
);
258 xbridge
->ofproto
= ofproto
;
260 hmap_insert(&xbridges
, &xbridge
->hmap_node
, hash_pointer(ofproto
, 0));
261 hmap_init(&xbridge
->xports
);
262 list_init(&xbridge
->xbundles
);
265 if (xbridge
->ml
!= ml
) {
266 mac_learning_unref(xbridge
->ml
);
267 xbridge
->ml
= mac_learning_ref(ml
);
270 if (xbridge
->mbridge
!= mbridge
) {
271 mbridge_unref(xbridge
->mbridge
);
272 xbridge
->mbridge
= mbridge_ref(mbridge
);
275 if (xbridge
->sflow
!= sflow
) {
276 dpif_sflow_unref(xbridge
->sflow
);
277 xbridge
->sflow
= dpif_sflow_ref(sflow
);
280 if (xbridge
->ipfix
!= ipfix
) {
281 dpif_ipfix_unref(xbridge
->ipfix
);
282 xbridge
->ipfix
= dpif_ipfix_ref(ipfix
);
285 if (xbridge
->stp
!= stp
) {
286 stp_unref(xbridge
->stp
);
287 xbridge
->stp
= stp_ref(stp
);
290 if (xbridge
->netflow
!= netflow
) {
291 netflow_unref(xbridge
->netflow
);
292 xbridge
->netflow
= netflow_ref(netflow
);
296 xbridge
->name
= xstrdup(name
);
298 xbridge
->dpif
= dpif
;
299 xbridge
->forward_bpdu
= forward_bpdu
;
300 xbridge
->has_in_band
= has_in_band
;
301 xbridge
->frag
= frag
;
302 xbridge
->miss_rule
= miss_rule
;
303 xbridge
->no_packet_in_rule
= no_packet_in_rule
;
307 xlate_remove_ofproto(struct ofproto_dpif
*ofproto
)
309 struct xbridge
*xbridge
= xbridge_lookup(ofproto
);
310 struct xbundle
*xbundle
, *next_xbundle
;
311 struct xport
*xport
, *next_xport
;
317 HMAP_FOR_EACH_SAFE (xport
, next_xport
, ofp_node
, &xbridge
->xports
) {
318 xlate_ofport_remove(xport
->ofport
);
321 LIST_FOR_EACH_SAFE (xbundle
, next_xbundle
, list_node
, &xbridge
->xbundles
) {
322 xlate_bundle_remove(xbundle
->ofbundle
);
325 hmap_remove(&xbridges
, &xbridge
->hmap_node
);
326 mac_learning_unref(xbridge
->ml
);
327 mbridge_unref(xbridge
->mbridge
);
328 dpif_sflow_unref(xbridge
->sflow
);
329 dpif_ipfix_unref(xbridge
->ipfix
);
330 stp_unref(xbridge
->stp
);
331 hmap_destroy(&xbridge
->xports
);
337 xlate_bundle_set(struct ofproto_dpif
*ofproto
, struct ofbundle
*ofbundle
,
338 const char *name
, enum port_vlan_mode vlan_mode
, int vlan
,
339 unsigned long *trunks
, bool use_priority_tags
,
340 const struct bond
*bond
, const struct lacp
*lacp
,
343 struct xbundle
*xbundle
= xbundle_lookup(ofbundle
);
346 xbundle
= xzalloc(sizeof *xbundle
);
347 xbundle
->ofbundle
= ofbundle
;
348 xbundle
->xbridge
= xbridge_lookup(ofproto
);
350 hmap_insert(&xbundles
, &xbundle
->hmap_node
, hash_pointer(ofbundle
, 0));
351 list_insert(&xbundle
->xbridge
->xbundles
, &xbundle
->list_node
);
352 list_init(&xbundle
->xports
);
355 ovs_assert(xbundle
->xbridge
);
358 xbundle
->name
= xstrdup(name
);
360 xbundle
->vlan_mode
= vlan_mode
;
361 xbundle
->vlan
= vlan
;
362 xbundle
->trunks
= trunks
;
363 xbundle
->use_priority_tags
= use_priority_tags
;
364 xbundle
->floodable
= floodable
;
366 if (xbundle
->bond
!= bond
) {
367 bond_unref(xbundle
->bond
);
368 xbundle
->bond
= bond_ref(bond
);
371 if (xbundle
->lacp
!= lacp
) {
372 lacp_unref(xbundle
->lacp
);
373 xbundle
->lacp
= lacp_ref(lacp
);
378 xlate_bundle_remove(struct ofbundle
*ofbundle
)
380 struct xbundle
*xbundle
= xbundle_lookup(ofbundle
);
381 struct xport
*xport
, *next
;
387 LIST_FOR_EACH_SAFE (xport
, next
, bundle_node
, &xbundle
->xports
) {
388 list_remove(&xport
->bundle_node
);
389 xport
->xbundle
= NULL
;
392 hmap_remove(&xbundles
, &xbundle
->hmap_node
);
393 list_remove(&xbundle
->list_node
);
394 bond_unref(xbundle
->bond
);
395 lacp_unref(xbundle
->lacp
);
401 xlate_ofport_set(struct ofproto_dpif
*ofproto
, struct ofbundle
*ofbundle
,
402 struct ofport_dpif
*ofport
, ofp_port_t ofp_port
,
403 odp_port_t odp_port
, const struct netdev
*netdev
,
404 const struct cfm
*cfm
, const struct bfd
*bfd
,
405 struct ofport_dpif
*peer
, int stp_port_no
,
406 const struct ofproto_port_queue
*qdscp_list
, size_t n_qdscp
,
407 enum ofputil_port_config config
,
408 enum ofputil_port_state state
, bool is_tunnel
,
411 struct xport
*xport
= xport_lookup(ofport
);
415 xport
= xzalloc(sizeof *xport
);
416 xport
->ofport
= ofport
;
417 xport
->xbridge
= xbridge_lookup(ofproto
);
418 xport
->ofp_port
= ofp_port
;
420 hmap_init(&xport
->skb_priorities
);
421 hmap_insert(&xports
, &xport
->hmap_node
, hash_pointer(ofport
, 0));
422 hmap_insert(&xport
->xbridge
->xports
, &xport
->ofp_node
,
423 hash_ofp_port(xport
->ofp_port
));
426 ovs_assert(xport
->ofp_port
== ofp_port
);
428 xport
->config
= config
;
429 xport
->state
= state
;
430 xport
->stp_port_no
= stp_port_no
;
431 xport
->is_tunnel
= is_tunnel
;
432 xport
->may_enable
= may_enable
;
433 xport
->odp_port
= odp_port
;
435 if (xport
->netdev
!= netdev
) {
436 netdev_close(xport
->netdev
);
437 xport
->netdev
= netdev_ref(netdev
);
440 if (xport
->cfm
!= cfm
) {
441 cfm_unref(xport
->cfm
);
442 xport
->cfm
= cfm_ref(cfm
);
445 if (xport
->bfd
!= bfd
) {
446 bfd_unref(xport
->bfd
);
447 xport
->bfd
= bfd_ref(bfd
);
451 xport
->peer
->peer
= NULL
;
453 xport
->peer
= xport_lookup(peer
);
455 xport
->peer
->peer
= xport
;
458 if (xport
->xbundle
) {
459 list_remove(&xport
->bundle_node
);
461 xport
->xbundle
= xbundle_lookup(ofbundle
);
462 if (xport
->xbundle
) {
463 list_insert(&xport
->xbundle
->xports
, &xport
->bundle_node
);
466 clear_skb_priorities(xport
);
467 for (i
= 0; i
< n_qdscp
; i
++) {
468 struct skb_priority_to_dscp
*pdscp
;
469 uint32_t skb_priority
;
471 if (dpif_queue_to_priority(xport
->xbridge
->dpif
, qdscp_list
[i
].queue
,
476 pdscp
= xmalloc(sizeof *pdscp
);
477 pdscp
->skb_priority
= skb_priority
;
478 pdscp
->dscp
= (qdscp_list
[i
].dscp
<< 2) & IP_DSCP_MASK
;
479 hmap_insert(&xport
->skb_priorities
, &pdscp
->hmap_node
,
480 hash_int(pdscp
->skb_priority
, 0));
485 xlate_ofport_remove(struct ofport_dpif
*ofport
)
487 struct xport
*xport
= xport_lookup(ofport
);
494 xport
->peer
->peer
= NULL
;
498 if (xport
->xbundle
) {
499 list_remove(&xport
->bundle_node
);
502 clear_skb_priorities(xport
);
503 hmap_destroy(&xport
->skb_priorities
);
505 hmap_remove(&xports
, &xport
->hmap_node
);
506 hmap_remove(&xport
->xbridge
->xports
, &xport
->ofp_node
);
508 netdev_close(xport
->netdev
);
509 cfm_unref(xport
->cfm
);
510 bfd_unref(xport
->bfd
);
514 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
515 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
516 * Optionally, if nonnull, populates 'fitnessp' with the fitness of 'flow' as
517 * returned by odp_flow_key_to_flow(). Also, optionally populates 'ofproto'
518 * with the ofproto_dpif, 'odp_in_port' with the datapath in_port, that
519 * 'packet' ingressed, and 'ipfix', 'sflow', and 'netflow' with the appropriate
520 * handles for those protocols if they're enabled. Caller is responsible for
523 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
524 * 'flow''s in_port to OFPP_NONE.
526 * This function does post-processing on data returned from
527 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
528 * of the upcall processing logic. In particular, if the extracted in_port is
529 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
530 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
531 * a VLAN header onto 'packet' (if it is nonnull).
533 * Similarly, this function also includes some logic to help with tunnels. It
534 * may modify 'flow' as necessary to make the tunneling implementation
535 * transparent to the upcall processing logic.
537 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
538 * or some other positive errno if there are other problems. */
540 xlate_receive(const struct dpif_backer
*backer
, struct ofpbuf
*packet
,
541 const struct nlattr
*key
, size_t key_len
,
542 struct flow
*flow
, enum odp_key_fitness
*fitnessp
,
543 struct ofproto_dpif
**ofproto
, struct dpif_ipfix
**ipfix
,
544 struct dpif_sflow
**sflow
, struct netflow
**netflow
,
545 odp_port_t
*odp_in_port
)
547 enum odp_key_fitness fitness
;
548 const struct xport
*xport
;
551 ovs_rwlock_rdlock(&xlate_rwlock
);
552 fitness
= odp_flow_key_to_flow(key
, key_len
, flow
);
553 if (fitness
== ODP_FIT_ERROR
) {
559 *odp_in_port
= flow
->in_port
.odp_port
;
562 xport
= xport_lookup(tnl_port_should_receive(flow
)
563 ? tnl_port_receive(flow
)
564 : odp_port_to_ofport(backer
, flow
->in_port
.odp_port
));
566 flow
->in_port
.ofp_port
= xport
? xport
->ofp_port
: OFPP_NONE
;
571 if (vsp_adjust_flow(xport
->xbridge
->ofproto
, flow
)) {
573 /* Make the packet resemble the flow, so that it gets sent to
574 * an OpenFlow controller properly, so that it looks correct
575 * for sFlow, and so that flow_extract() will get the correct
576 * vlan_tci if it is called on 'packet'. */
577 eth_push_vlan(packet
, flow
->vlan_tci
);
579 /* We can't reproduce 'key' from 'flow'. */
580 fitness
= fitness
== ODP_FIT_PERFECT
? ODP_FIT_TOO_MUCH
: fitness
;
585 *ofproto
= xport
->xbridge
->ofproto
;
589 *ipfix
= dpif_ipfix_ref(xport
->xbridge
->ipfix
);
593 *sflow
= dpif_sflow_ref(xport
->xbridge
->sflow
);
597 *netflow
= netflow_ref(xport
->xbridge
->netflow
);
604 ovs_rwlock_unlock(&xlate_rwlock
);
608 static struct xbridge
*
609 xbridge_lookup(const struct ofproto_dpif
*ofproto
)
611 struct xbridge
*xbridge
;
617 HMAP_FOR_EACH_IN_BUCKET (xbridge
, hmap_node
, hash_pointer(ofproto
, 0),
619 if (xbridge
->ofproto
== ofproto
) {
626 static struct xbundle
*
627 xbundle_lookup(const struct ofbundle
*ofbundle
)
629 struct xbundle
*xbundle
;
635 HMAP_FOR_EACH_IN_BUCKET (xbundle
, hmap_node
, hash_pointer(ofbundle
, 0),
637 if (xbundle
->ofbundle
== ofbundle
) {
644 static struct xport
*
645 xport_lookup(const struct ofport_dpif
*ofport
)
653 HMAP_FOR_EACH_IN_BUCKET (xport
, hmap_node
, hash_pointer(ofport
, 0),
655 if (xport
->ofport
== ofport
) {
662 static struct stp_port
*
663 xport_get_stp_port(const struct xport
*xport
)
665 return xport
->xbridge
->stp
&& xport
->stp_port_no
!= -1
666 ? stp_get_port(xport
->xbridge
->stp
, xport
->stp_port_no
)
670 static enum stp_state
671 xport_stp_learn_state(const struct xport
*xport
)
673 struct stp_port
*sp
= xport_get_stp_port(xport
);
674 return stp_learn_in_state(sp
? stp_port_get_state(sp
) : STP_DISABLED
);
678 xport_stp_forward_state(const struct xport
*xport
)
680 struct stp_port
*sp
= xport_get_stp_port(xport
);
681 return stp_forward_in_state(sp
? stp_port_get_state(sp
) : STP_DISABLED
);
684 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
685 * were used to make the determination.*/
687 stp_should_process_flow(const struct flow
*flow
, struct flow_wildcards
*wc
)
689 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
690 return eth_addr_equals(flow
->dl_dst
, eth_addr_stp
);
694 stp_process_packet(const struct xport
*xport
, const struct ofpbuf
*packet
)
696 struct stp_port
*sp
= xport_get_stp_port(xport
);
697 struct ofpbuf payload
= *packet
;
698 struct eth_header
*eth
= payload
.data
;
700 /* Sink packets on ports that have STP disabled when the bridge has
702 if (!sp
|| stp_port_get_state(sp
) == STP_DISABLED
) {
706 /* Trim off padding on payload. */
707 if (payload
.size
> ntohs(eth
->eth_type
) + ETH_HEADER_LEN
) {
708 payload
.size
= ntohs(eth
->eth_type
) + ETH_HEADER_LEN
;
711 if (ofpbuf_try_pull(&payload
, ETH_HEADER_LEN
+ LLC_HEADER_LEN
)) {
712 stp_received_bpdu(sp
, payload
.data
, payload
.size
);
716 static struct xport
*
717 get_ofp_port(const struct xbridge
*xbridge
, ofp_port_t ofp_port
)
721 HMAP_FOR_EACH_IN_BUCKET (xport
, ofp_node
, hash_ofp_port(ofp_port
),
723 if (xport
->ofp_port
== ofp_port
) {
731 ofp_port_to_odp_port(const struct xbridge
*xbridge
, ofp_port_t ofp_port
)
733 const struct xport
*xport
= get_ofp_port(xbridge
, ofp_port
);
734 return xport
? xport
->odp_port
: ODPP_NONE
;
738 odp_port_is_alive(const struct xlate_ctx
*ctx
, ofp_port_t ofp_port
)
742 xport
= get_ofp_port(ctx
->xbridge
, ofp_port
);
743 if (!xport
|| xport
->config
& OFPUTIL_PC_PORT_DOWN
||
744 xport
->state
& OFPUTIL_PS_LINK_DOWN
) {
751 static const struct ofputil_bucket
*
752 group_first_live_bucket(const struct xlate_ctx
*, const struct group_dpif
*,
756 group_is_alive(const struct xlate_ctx
*ctx
, uint32_t group_id
, int depth
)
758 struct group_dpif
*group
;
761 hit
= group_dpif_lookup(ctx
->xbridge
->ofproto
, group_id
, &group
);
766 hit
= group_first_live_bucket(ctx
, group
, depth
) != NULL
;
768 group_dpif_release(group
);
772 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
775 bucket_is_alive(const struct xlate_ctx
*ctx
,
776 const struct ofputil_bucket
*bucket
, int depth
)
778 if (depth
>= MAX_LIVENESS_RECURSION
) {
779 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
781 VLOG_WARN_RL(&rl
, "bucket chaining exceeded %d links",
782 MAX_LIVENESS_RECURSION
);
786 return !ofputil_bucket_has_liveness(bucket
) ||
787 (bucket
->watch_port
!= OFPP_ANY
&&
788 odp_port_is_alive(ctx
, bucket
->watch_port
)) ||
789 (bucket
->watch_group
!= OFPG_ANY
&&
790 group_is_alive(ctx
, bucket
->watch_group
, depth
+ 1));
793 static const struct ofputil_bucket
*
794 group_first_live_bucket(const struct xlate_ctx
*ctx
,
795 const struct group_dpif
*group
, int depth
)
797 struct ofputil_bucket
*bucket
;
798 const struct list
*buckets
;
800 group_dpif_get_buckets(group
, &buckets
);
801 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
802 if (bucket_is_alive(ctx
, bucket
, depth
)) {
810 static const struct ofputil_bucket
*
811 group_best_live_bucket(const struct xlate_ctx
*ctx
,
812 const struct group_dpif
*group
,
815 const struct ofputil_bucket
*best_bucket
= NULL
;
816 uint32_t best_score
= 0;
819 const struct ofputil_bucket
*bucket
;
820 const struct list
*buckets
;
822 group_dpif_get_buckets(group
, &buckets
);
823 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
824 if (bucket_is_alive(ctx
, bucket
, 0)) {
825 uint32_t score
= (hash_int(i
, basis
) & 0xffff) * bucket
->weight
;
826 if (score
>= best_score
) {
827 best_bucket
= bucket
;
838 xbundle_trunks_vlan(const struct xbundle
*bundle
, uint16_t vlan
)
840 return (bundle
->vlan_mode
!= PORT_VLAN_ACCESS
841 && (!bundle
->trunks
|| bitmap_is_set(bundle
->trunks
, vlan
)));
845 xbundle_includes_vlan(const struct xbundle
*xbundle
, uint16_t vlan
)
847 return vlan
== xbundle
->vlan
|| xbundle_trunks_vlan(xbundle
, vlan
);
851 xbundle_mirror_out(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
853 return xbundle
!= &ofpp_none_bundle
854 ? mirror_bundle_out(xbridge
->mbridge
, xbundle
->ofbundle
)
859 xbundle_mirror_src(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
861 return xbundle
!= &ofpp_none_bundle
862 ? mirror_bundle_src(xbridge
->mbridge
, xbundle
->ofbundle
)
867 xbundle_mirror_dst(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
869 return xbundle
!= &ofpp_none_bundle
870 ? mirror_bundle_dst(xbridge
->mbridge
, xbundle
->ofbundle
)
874 static struct xbundle
*
875 lookup_input_bundle(const struct xbridge
*xbridge
, ofp_port_t in_port
,
876 bool warn
, struct xport
**in_xportp
)
880 /* Find the port and bundle for the received packet. */
881 xport
= get_ofp_port(xbridge
, in_port
);
885 if (xport
&& xport
->xbundle
) {
886 return xport
->xbundle
;
889 /* Special-case OFPP_NONE, which a controller may use as the ingress
890 * port for traffic that it is sourcing. */
891 if (in_port
== OFPP_NONE
) {
892 ofpp_none_bundle
.name
= "OFPP_NONE";
893 ofpp_none_bundle
.vlan_mode
= PORT_VLAN_TRUNK
;
894 return &ofpp_none_bundle
;
897 /* Odd. A few possible reasons here:
899 * - We deleted a port but there are still a few packets queued up
902 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
903 * we don't know about.
905 * - The ofproto client didn't configure the port as part of a bundle.
906 * This is particularly likely to happen if a packet was received on the
907 * port after it was created, but before the client had a chance to
908 * configure its bundle.
911 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
913 VLOG_WARN_RL(&rl
, "bridge %s: received packet on unknown "
914 "port %"PRIu16
, xbridge
->name
, in_port
);
920 add_mirror_actions(struct xlate_ctx
*ctx
, const struct flow
*orig_flow
)
922 const struct xbridge
*xbridge
= ctx
->xbridge
;
923 mirror_mask_t mirrors
;
924 struct xbundle
*in_xbundle
;
928 mirrors
= ctx
->xout
->mirrors
;
929 ctx
->xout
->mirrors
= 0;
931 in_xbundle
= lookup_input_bundle(xbridge
, orig_flow
->in_port
.ofp_port
,
932 ctx
->xin
->packet
!= NULL
, NULL
);
936 mirrors
|= xbundle_mirror_src(xbridge
, in_xbundle
);
938 /* Drop frames on bundles reserved for mirroring. */
939 if (xbundle_mirror_out(xbridge
, in_xbundle
)) {
940 if (ctx
->xin
->packet
!= NULL
) {
941 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
942 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet received on port "
943 "%s, which is reserved exclusively for mirroring",
944 ctx
->xbridge
->name
, in_xbundle
->name
);
946 ofpbuf_clear(&ctx
->xout
->odp_actions
);
951 vid
= vlan_tci_to_vid(orig_flow
->vlan_tci
);
952 if (!input_vid_is_valid(vid
, in_xbundle
, ctx
->xin
->packet
!= NULL
)) {
955 vlan
= input_vid_to_vlan(in_xbundle
, vid
);
961 /* Restore the original packet before adding the mirror actions. */
962 ctx
->xin
->flow
= *orig_flow
;
965 mirror_mask_t dup_mirrors
;
966 struct ofbundle
*out
;
967 unsigned long *vlans
;
972 has_mirror
= mirror_get(xbridge
->mbridge
, raw_ctz(mirrors
),
973 &vlans
, &dup_mirrors
, &out
, &out_vlan
);
974 ovs_assert(has_mirror
);
977 ctx
->xout
->wc
.masks
.vlan_tci
|= htons(VLAN_CFI
| VLAN_VID_MASK
);
979 vlan_mirrored
= !vlans
|| bitmap_is_set(vlans
, vlan
);
982 if (!vlan_mirrored
) {
983 mirrors
= zero_rightmost_1bit(mirrors
);
987 mirrors
&= ~dup_mirrors
;
988 ctx
->xout
->mirrors
|= dup_mirrors
;
990 struct xbundle
*out_xbundle
= xbundle_lookup(out
);
992 output_normal(ctx
, out_xbundle
, vlan
);
994 } else if (vlan
!= out_vlan
995 && !eth_addr_is_reserved(orig_flow
->dl_dst
)) {
996 struct xbundle
*xbundle
;
998 LIST_FOR_EACH (xbundle
, list_node
, &xbridge
->xbundles
) {
999 if (xbundle_includes_vlan(xbundle
, out_vlan
)
1000 && !xbundle_mirror_out(xbridge
, xbundle
)) {
1001 output_normal(ctx
, xbundle
, out_vlan
);
1008 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1009 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1010 * the bundle on which the packet was received, returns the VLAN to which the
1013 * Both 'vid' and the return value are in the range 0...4095. */
1015 input_vid_to_vlan(const struct xbundle
*in_xbundle
, uint16_t vid
)
1017 switch (in_xbundle
->vlan_mode
) {
1018 case PORT_VLAN_ACCESS
:
1019 return in_xbundle
->vlan
;
1022 case PORT_VLAN_TRUNK
:
1025 case PORT_VLAN_NATIVE_UNTAGGED
:
1026 case PORT_VLAN_NATIVE_TAGGED
:
1027 return vid
? vid
: in_xbundle
->vlan
;
1034 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1035 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1038 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1039 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1042 input_vid_is_valid(uint16_t vid
, struct xbundle
*in_xbundle
, bool warn
)
1044 /* Allow any VID on the OFPP_NONE port. */
1045 if (in_xbundle
== &ofpp_none_bundle
) {
1049 switch (in_xbundle
->vlan_mode
) {
1050 case PORT_VLAN_ACCESS
:
1053 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1054 VLOG_WARN_RL(&rl
, "dropping VLAN %"PRIu16
" tagged "
1055 "packet received on port %s configured as VLAN "
1056 "%"PRIu16
" access port", vid
, in_xbundle
->name
,
1063 case PORT_VLAN_NATIVE_UNTAGGED
:
1064 case PORT_VLAN_NATIVE_TAGGED
:
1066 /* Port must always carry its native VLAN. */
1070 case PORT_VLAN_TRUNK
:
1071 if (!xbundle_includes_vlan(in_xbundle
, vid
)) {
1073 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1074 VLOG_WARN_RL(&rl
, "dropping VLAN %"PRIu16
" packet "
1075 "received on port %s not configured for trunking "
1076 "VLAN %"PRIu16
, vid
, in_xbundle
->name
, vid
);
1088 /* Given 'vlan', the VLAN that a packet belongs to, and
1089 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1090 * that should be included in the 802.1Q header. (If the return value is 0,
1091 * then the 802.1Q header should only be included in the packet if there is a
1094 * Both 'vlan' and the return value are in the range 0...4095. */
1096 output_vlan_to_vid(const struct xbundle
*out_xbundle
, uint16_t vlan
)
1098 switch (out_xbundle
->vlan_mode
) {
1099 case PORT_VLAN_ACCESS
:
1102 case PORT_VLAN_TRUNK
:
1103 case PORT_VLAN_NATIVE_TAGGED
:
1106 case PORT_VLAN_NATIVE_UNTAGGED
:
1107 return vlan
== out_xbundle
->vlan
? 0 : vlan
;
1115 output_normal(struct xlate_ctx
*ctx
, const struct xbundle
*out_xbundle
,
1118 ovs_be16
*flow_tci
= &ctx
->xin
->flow
.vlan_tci
;
1120 ovs_be16 tci
, old_tci
;
1121 struct xport
*xport
;
1123 vid
= output_vlan_to_vid(out_xbundle
, vlan
);
1124 if (list_is_empty(&out_xbundle
->xports
)) {
1125 /* Partially configured bundle with no slaves. Drop the packet. */
1127 } else if (!out_xbundle
->bond
) {
1128 xport
= CONTAINER_OF(list_front(&out_xbundle
->xports
), struct xport
,
1131 struct ofport_dpif
*ofport
;
1133 ofport
= bond_choose_output_slave(out_xbundle
->bond
, &ctx
->xin
->flow
,
1134 &ctx
->xout
->wc
, vid
);
1135 xport
= xport_lookup(ofport
);
1138 /* No slaves enabled, so drop packet. */
1142 if (ctx
->xin
->resubmit_stats
) {
1143 bond_account(out_xbundle
->bond
, &ctx
->xin
->flow
, vid
,
1144 ctx
->xin
->resubmit_stats
->n_bytes
);
1148 old_tci
= *flow_tci
;
1150 if (tci
|| out_xbundle
->use_priority_tags
) {
1151 tci
|= *flow_tci
& htons(VLAN_PCP_MASK
);
1153 tci
|= htons(VLAN_CFI
);
1158 compose_output_action(ctx
, xport
->ofp_port
);
1159 *flow_tci
= old_tci
;
1162 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1163 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1164 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1166 is_gratuitous_arp(const struct flow
*flow
, struct flow_wildcards
*wc
)
1168 if (flow
->dl_type
!= htons(ETH_TYPE_ARP
)) {
1172 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
1173 if (!eth_addr_is_broadcast(flow
->dl_dst
)) {
1177 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
1178 if (flow
->nw_proto
== ARP_OP_REPLY
) {
1180 } else if (flow
->nw_proto
== ARP_OP_REQUEST
) {
1181 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
1182 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
1184 return flow
->nw_src
== flow
->nw_dst
;
1190 /* Checks whether a MAC learning update is necessary for MAC learning table
1191 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1194 * Most packets processed through the MAC learning table do not actually
1195 * change it in any way. This function requires only a read lock on the MAC
1196 * learning table, so it is much cheaper in this common case.
1198 * Keep the code here synchronized with that in update_learning_table__()
1201 is_mac_learning_update_needed(const struct mac_learning
*ml
,
1202 const struct flow
*flow
,
1203 struct flow_wildcards
*wc
,
1204 int vlan
, struct xbundle
*in_xbundle
)
1205 OVS_REQ_RDLOCK(ml
->rwlock
)
1207 struct mac_entry
*mac
;
1209 if (!mac_learning_may_learn(ml
, flow
->dl_src
, vlan
)) {
1213 mac
= mac_learning_lookup(ml
, flow
->dl_src
, vlan
);
1214 if (!mac
|| mac_entry_age(ml
, mac
)) {
1218 if (is_gratuitous_arp(flow
, wc
)) {
1219 /* We don't want to learn from gratuitous ARP packets that are
1220 * reflected back over bond slaves so we lock the learning table. */
1221 if (!in_xbundle
->bond
) {
1223 } else if (mac_entry_is_grat_arp_locked(mac
)) {
1228 return mac
->port
.p
!= in_xbundle
->ofbundle
;
1232 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1233 * received on 'in_xbundle' in 'vlan'.
1235 * This code repeats all the checks in is_mac_learning_update_needed() because
1236 * the lock was released between there and here and thus the MAC learning state
1237 * could have changed.
1239 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1242 update_learning_table__(const struct xbridge
*xbridge
,
1243 const struct flow
*flow
, struct flow_wildcards
*wc
,
1244 int vlan
, struct xbundle
*in_xbundle
)
1245 OVS_REQ_WRLOCK(xbridge
->ml
->rwlock
)
1247 struct mac_entry
*mac
;
1249 if (!mac_learning_may_learn(xbridge
->ml
, flow
->dl_src
, vlan
)) {
1253 mac
= mac_learning_insert(xbridge
->ml
, flow
->dl_src
, vlan
);
1254 if (is_gratuitous_arp(flow
, wc
)) {
1255 /* We don't want to learn from gratuitous ARP packets that are
1256 * reflected back over bond slaves so we lock the learning table. */
1257 if (!in_xbundle
->bond
) {
1258 mac_entry_set_grat_arp_lock(mac
);
1259 } else if (mac_entry_is_grat_arp_locked(mac
)) {
1264 if (mac
->port
.p
!= in_xbundle
->ofbundle
) {
1265 /* The log messages here could actually be useful in debugging,
1266 * so keep the rate limit relatively high. */
1267 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(30, 300);
1269 VLOG_DBG_RL(&rl
, "bridge %s: learned that "ETH_ADDR_FMT
" is "
1270 "on port %s in VLAN %d",
1271 xbridge
->name
, ETH_ADDR_ARGS(flow
->dl_src
),
1272 in_xbundle
->name
, vlan
);
1274 mac
->port
.p
= in_xbundle
->ofbundle
;
1275 mac_learning_changed(xbridge
->ml
);
1280 update_learning_table(const struct xbridge
*xbridge
,
1281 const struct flow
*flow
, struct flow_wildcards
*wc
,
1282 int vlan
, struct xbundle
*in_xbundle
)
1286 /* Don't learn the OFPP_NONE port. */
1287 if (in_xbundle
== &ofpp_none_bundle
) {
1291 /* First try the common case: no change to MAC learning table. */
1292 ovs_rwlock_rdlock(&xbridge
->ml
->rwlock
);
1293 need_update
= is_mac_learning_update_needed(xbridge
->ml
, flow
, wc
, vlan
,
1295 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
1298 /* Slow path: MAC learning table might need an update. */
1299 ovs_rwlock_wrlock(&xbridge
->ml
->rwlock
);
1300 update_learning_table__(xbridge
, flow
, wc
, vlan
, in_xbundle
);
1301 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
1305 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1306 * dropped. Returns true if they may be forwarded, false if they should be
1309 * 'in_port' must be the xport that corresponds to flow->in_port.
1310 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1312 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1313 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1314 * checked by input_vid_is_valid().
1316 * May also add tags to '*tags', although the current implementation only does
1317 * so in one special case.
1320 is_admissible(struct xlate_ctx
*ctx
, struct xport
*in_port
,
1323 struct xbundle
*in_xbundle
= in_port
->xbundle
;
1324 const struct xbridge
*xbridge
= ctx
->xbridge
;
1325 struct flow
*flow
= &ctx
->xin
->flow
;
1327 /* Drop frames for reserved multicast addresses
1328 * only if forward_bpdu option is absent. */
1329 if (!xbridge
->forward_bpdu
&& eth_addr_is_reserved(flow
->dl_dst
)) {
1330 xlate_report(ctx
, "packet has reserved destination MAC, dropping");
1334 if (in_xbundle
->bond
) {
1335 struct mac_entry
*mac
;
1337 switch (bond_check_admissibility(in_xbundle
->bond
, in_port
->ofport
,
1343 xlate_report(ctx
, "bonding refused admissibility, dropping");
1346 case BV_DROP_IF_MOVED
:
1347 ovs_rwlock_rdlock(&xbridge
->ml
->rwlock
);
1348 mac
= mac_learning_lookup(xbridge
->ml
, flow
->dl_src
, vlan
);
1349 if (mac
&& mac
->port
.p
!= in_xbundle
->ofbundle
&&
1350 (!is_gratuitous_arp(flow
, &ctx
->xout
->wc
)
1351 || mac_entry_is_grat_arp_locked(mac
))) {
1352 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
1353 xlate_report(ctx
, "SLB bond thinks this packet looped back, "
1357 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
1366 xlate_normal(struct xlate_ctx
*ctx
)
1368 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
1369 struct flow
*flow
= &ctx
->xin
->flow
;
1370 struct xbundle
*in_xbundle
;
1371 struct xport
*in_port
;
1372 struct mac_entry
*mac
;
1377 ctx
->xout
->has_normal
= true;
1379 memset(&wc
->masks
.dl_src
, 0xff, sizeof wc
->masks
.dl_src
);
1380 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
1381 wc
->masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
1383 in_xbundle
= lookup_input_bundle(ctx
->xbridge
, flow
->in_port
.ofp_port
,
1384 ctx
->xin
->packet
!= NULL
, &in_port
);
1386 xlate_report(ctx
, "no input bundle, dropping");
1390 /* Drop malformed frames. */
1391 if (flow
->dl_type
== htons(ETH_TYPE_VLAN
) &&
1392 !(flow
->vlan_tci
& htons(VLAN_CFI
))) {
1393 if (ctx
->xin
->packet
!= NULL
) {
1394 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1395 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet with partial "
1396 "VLAN tag received on port %s",
1397 ctx
->xbridge
->name
, in_xbundle
->name
);
1399 xlate_report(ctx
, "partial VLAN tag, dropping");
1403 /* Drop frames on bundles reserved for mirroring. */
1404 if (xbundle_mirror_out(ctx
->xbridge
, in_xbundle
)) {
1405 if (ctx
->xin
->packet
!= NULL
) {
1406 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1407 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet received on port "
1408 "%s, which is reserved exclusively for mirroring",
1409 ctx
->xbridge
->name
, in_xbundle
->name
);
1411 xlate_report(ctx
, "input port is mirror output port, dropping");
1416 vid
= vlan_tci_to_vid(flow
->vlan_tci
);
1417 if (!input_vid_is_valid(vid
, in_xbundle
, ctx
->xin
->packet
!= NULL
)) {
1418 xlate_report(ctx
, "disallowed VLAN VID for this input port, dropping");
1421 vlan
= input_vid_to_vlan(in_xbundle
, vid
);
1423 /* Check other admissibility requirements. */
1424 if (in_port
&& !is_admissible(ctx
, in_port
, vlan
)) {
1428 /* Learn source MAC. */
1429 if (ctx
->xin
->may_learn
) {
1430 update_learning_table(ctx
->xbridge
, flow
, wc
, vlan
, in_xbundle
);
1433 /* Determine output bundle. */
1434 ovs_rwlock_rdlock(&ctx
->xbridge
->ml
->rwlock
);
1435 mac
= mac_learning_lookup(ctx
->xbridge
->ml
, flow
->dl_dst
, vlan
);
1436 mac_port
= mac
? mac
->port
.p
: NULL
;
1437 ovs_rwlock_unlock(&ctx
->xbridge
->ml
->rwlock
);
1440 struct xbundle
*mac_xbundle
= xbundle_lookup(mac_port
);
1441 if (mac_xbundle
&& mac_xbundle
!= in_xbundle
) {
1442 xlate_report(ctx
, "forwarding to learned port");
1443 output_normal(ctx
, mac_xbundle
, vlan
);
1444 } else if (!mac_xbundle
) {
1445 xlate_report(ctx
, "learned port is unknown, dropping");
1447 xlate_report(ctx
, "learned port is input port, dropping");
1450 struct xbundle
*xbundle
;
1452 xlate_report(ctx
, "no learned MAC for destination, flooding");
1453 LIST_FOR_EACH (xbundle
, list_node
, &ctx
->xbridge
->xbundles
) {
1454 if (xbundle
!= in_xbundle
1455 && xbundle_includes_vlan(xbundle
, vlan
)
1456 && xbundle
->floodable
1457 && !xbundle_mirror_out(ctx
->xbridge
, xbundle
)) {
1458 output_normal(ctx
, xbundle
, vlan
);
1461 ctx
->xout
->nf_output_iface
= NF_OUT_FLOOD
;
1465 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1466 * the number of packets out of UINT32_MAX to sample. The given
1467 * cookie is passed back in the callback for each sampled packet.
1470 compose_sample_action(const struct xbridge
*xbridge
,
1471 struct ofpbuf
*odp_actions
,
1472 const struct flow
*flow
,
1473 const uint32_t probability
,
1474 const union user_action_cookie
*cookie
,
1475 const size_t cookie_size
)
1477 size_t sample_offset
, actions_offset
;
1478 odp_port_t odp_port
;
1482 sample_offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SAMPLE
);
1484 nl_msg_put_u32(odp_actions
, OVS_SAMPLE_ATTR_PROBABILITY
, probability
);
1486 actions_offset
= nl_msg_start_nested(odp_actions
, OVS_SAMPLE_ATTR_ACTIONS
);
1488 odp_port
= ofp_port_to_odp_port(xbridge
, flow
->in_port
.ofp_port
);
1489 pid
= dpif_port_get_pid(xbridge
->dpif
, odp_port
);
1490 cookie_offset
= odp_put_userspace_action(pid
, cookie
, cookie_size
, odp_actions
);
1492 nl_msg_end_nested(odp_actions
, actions_offset
);
1493 nl_msg_end_nested(odp_actions
, sample_offset
);
1494 return cookie_offset
;
1498 compose_sflow_cookie(const struct xbridge
*xbridge
, ovs_be16 vlan_tci
,
1499 odp_port_t odp_port
, unsigned int n_outputs
,
1500 union user_action_cookie
*cookie
)
1504 cookie
->type
= USER_ACTION_COOKIE_SFLOW
;
1505 cookie
->sflow
.vlan_tci
= vlan_tci
;
1507 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1508 * port information") for the interpretation of cookie->output. */
1509 switch (n_outputs
) {
1511 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1512 cookie
->sflow
.output
= 0x40000000 | 256;
1516 ifindex
= dpif_sflow_odp_port_to_ifindex(xbridge
->sflow
, odp_port
);
1518 cookie
->sflow
.output
= ifindex
;
1523 /* 0x80000000 means "multiple output ports. */
1524 cookie
->sflow
.output
= 0x80000000 | n_outputs
;
1529 /* Compose SAMPLE action for sFlow bridge sampling. */
1531 compose_sflow_action(const struct xbridge
*xbridge
,
1532 struct ofpbuf
*odp_actions
,
1533 const struct flow
*flow
,
1534 odp_port_t odp_port
)
1536 uint32_t probability
;
1537 union user_action_cookie cookie
;
1539 if (!xbridge
->sflow
|| flow
->in_port
.ofp_port
== OFPP_NONE
) {
1543 probability
= dpif_sflow_get_probability(xbridge
->sflow
);
1544 compose_sflow_cookie(xbridge
, htons(0), odp_port
,
1545 odp_port
== ODPP_NONE
? 0 : 1, &cookie
);
1547 return compose_sample_action(xbridge
, odp_actions
, flow
, probability
,
1548 &cookie
, sizeof cookie
.sflow
);
1552 compose_flow_sample_cookie(uint16_t probability
, uint32_t collector_set_id
,
1553 uint32_t obs_domain_id
, uint32_t obs_point_id
,
1554 union user_action_cookie
*cookie
)
1556 cookie
->type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
1557 cookie
->flow_sample
.probability
= probability
;
1558 cookie
->flow_sample
.collector_set_id
= collector_set_id
;
1559 cookie
->flow_sample
.obs_domain_id
= obs_domain_id
;
1560 cookie
->flow_sample
.obs_point_id
= obs_point_id
;
1564 compose_ipfix_cookie(union user_action_cookie
*cookie
)
1566 cookie
->type
= USER_ACTION_COOKIE_IPFIX
;
1569 /* Compose SAMPLE action for IPFIX bridge sampling. */
1571 compose_ipfix_action(const struct xbridge
*xbridge
,
1572 struct ofpbuf
*odp_actions
,
1573 const struct flow
*flow
)
1575 uint32_t probability
;
1576 union user_action_cookie cookie
;
1578 if (!xbridge
->ipfix
|| flow
->in_port
.ofp_port
== OFPP_NONE
) {
1582 probability
= dpif_ipfix_get_bridge_exporter_probability(xbridge
->ipfix
);
1583 compose_ipfix_cookie(&cookie
);
1585 compose_sample_action(xbridge
, odp_actions
, flow
, probability
,
1586 &cookie
, sizeof cookie
.ipfix
);
1589 /* SAMPLE action for sFlow must be first action in any given list of
1590 * actions. At this point we do not have all information required to
1591 * build it. So try to build sample action as complete as possible. */
1593 add_sflow_action(struct xlate_ctx
*ctx
)
1595 ctx
->user_cookie_offset
= compose_sflow_action(ctx
->xbridge
,
1596 &ctx
->xout
->odp_actions
,
1597 &ctx
->xin
->flow
, ODPP_NONE
);
1598 ctx
->sflow_odp_port
= 0;
1599 ctx
->sflow_n_outputs
= 0;
1602 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
1603 * of actions, eventually after the SAMPLE action for sFlow. */
1605 add_ipfix_action(struct xlate_ctx
*ctx
)
1607 compose_ipfix_action(ctx
->xbridge
, &ctx
->xout
->odp_actions
,
1611 /* Fix SAMPLE action according to data collected while composing ODP actions.
1612 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
1613 * USERSPACE action's user-cookie which is required for sflow. */
1615 fix_sflow_action(struct xlate_ctx
*ctx
)
1617 const struct flow
*base
= &ctx
->base_flow
;
1618 union user_action_cookie
*cookie
;
1620 if (!ctx
->user_cookie_offset
) {
1624 cookie
= ofpbuf_at(&ctx
->xout
->odp_actions
, ctx
->user_cookie_offset
,
1625 sizeof cookie
->sflow
);
1626 ovs_assert(cookie
->type
== USER_ACTION_COOKIE_SFLOW
);
1628 compose_sflow_cookie(ctx
->xbridge
, base
->vlan_tci
,
1629 ctx
->sflow_odp_port
, ctx
->sflow_n_outputs
, cookie
);
1632 static enum slow_path_reason
1633 process_special(struct xlate_ctx
*ctx
, const struct flow
*flow
,
1634 const struct xport
*xport
, const struct ofpbuf
*packet
)
1636 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
1637 const struct xbridge
*xbridge
= ctx
->xbridge
;
1641 } else if (xport
->cfm
&& cfm_should_process_flow(xport
->cfm
, flow
, wc
)) {
1643 cfm_process_heartbeat(xport
->cfm
, packet
);
1646 } else if (xport
->bfd
&& bfd_should_process_flow(xport
->bfd
, flow
, wc
)) {
1648 bfd_process_packet(xport
->bfd
, flow
, packet
);
1649 /* If POLL received, immediately sends FINAL back. */
1650 if (bfd_should_send_packet(xport
->bfd
)) {
1652 ofproto_dpif_monitor_port_send_soon(xport
->ofport
);
1654 ofproto_dpif_monitor_port_send_soon_safe(xport
->ofport
);
1659 } else if (xport
->xbundle
&& xport
->xbundle
->lacp
1660 && flow
->dl_type
== htons(ETH_TYPE_LACP
)) {
1662 lacp_process_packet(xport
->xbundle
->lacp
, xport
->ofport
, packet
);
1665 } else if (xbridge
->stp
&& stp_should_process_flow(flow
, wc
)) {
1667 stp_process_packet(xport
, packet
);
1676 compose_output_action__(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
,
1679 const struct xport
*xport
= get_ofp_port(ctx
->xbridge
, ofp_port
);
1680 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
1681 struct flow
*flow
= &ctx
->xin
->flow
;
1682 ovs_be16 flow_vlan_tci
;
1683 uint32_t flow_pkt_mark
;
1684 uint8_t flow_nw_tos
;
1685 odp_port_t out_port
, odp_port
;
1688 /* If 'struct flow' gets additional metadata, we'll need to zero it out
1689 * before traversing a patch port. */
1690 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 23);
1693 xlate_report(ctx
, "Nonexistent output port");
1695 } else if (xport
->config
& OFPUTIL_PC_NO_FWD
) {
1696 xlate_report(ctx
, "OFPPC_NO_FWD set, skipping output");
1698 } else if (check_stp
&& !xport_stp_forward_state(xport
)) {
1699 xlate_report(ctx
, "STP not in forwarding state, skipping output");
1703 if (mbridge_has_mirrors(ctx
->xbridge
->mbridge
) && xport
->xbundle
) {
1704 ctx
->xout
->mirrors
|= xbundle_mirror_dst(xport
->xbundle
->xbridge
,
1709 const struct xport
*peer
= xport
->peer
;
1710 struct flow old_flow
= ctx
->xin
->flow
;
1711 enum slow_path_reason special
;
1713 ctx
->xbridge
= peer
->xbridge
;
1714 flow
->in_port
.ofp_port
= peer
->ofp_port
;
1715 flow
->metadata
= htonll(0);
1716 memset(&flow
->tunnel
, 0, sizeof flow
->tunnel
);
1717 memset(flow
->regs
, 0, sizeof flow
->regs
);
1719 special
= process_special(ctx
, &ctx
->xin
->flow
, peer
,
1722 ctx
->xout
->slow
|= special
;
1723 } else if (may_receive(peer
, ctx
)) {
1724 if (xport_stp_forward_state(peer
)) {
1725 xlate_table_action(ctx
, flow
->in_port
.ofp_port
, 0, true);
1727 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
1728 * learning action look at the packet, then drop it. */
1729 struct flow old_base_flow
= ctx
->base_flow
;
1730 size_t old_size
= ctx
->xout
->odp_actions
.size
;
1731 mirror_mask_t old_mirrors
= ctx
->xout
->mirrors
;
1732 xlate_table_action(ctx
, flow
->in_port
.ofp_port
, 0, true);
1733 ctx
->xout
->mirrors
= old_mirrors
;
1734 ctx
->base_flow
= old_base_flow
;
1735 ctx
->xout
->odp_actions
.size
= old_size
;
1739 ctx
->xin
->flow
= old_flow
;
1740 ctx
->xbridge
= xport
->xbridge
;
1742 if (ctx
->xin
->resubmit_stats
) {
1743 netdev_vport_inc_tx(xport
->netdev
, ctx
->xin
->resubmit_stats
);
1744 netdev_vport_inc_rx(peer
->netdev
, ctx
->xin
->resubmit_stats
);
1746 bfd_account_rx(peer
->bfd
, ctx
->xin
->resubmit_stats
);
1753 flow_vlan_tci
= flow
->vlan_tci
;
1754 flow_pkt_mark
= flow
->pkt_mark
;
1755 flow_nw_tos
= flow
->nw_tos
;
1757 if (dscp_from_skb_priority(xport
, flow
->skb_priority
, &dscp
)) {
1758 wc
->masks
.nw_tos
|= IP_ECN_MASK
;
1759 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1760 flow
->nw_tos
|= dscp
;
1763 if (xport
->is_tunnel
) {
1764 /* Save tunnel metadata so that changes made due to
1765 * the Logical (tunnel) Port are not visible for any further
1766 * matches, while explicit set actions on tunnel metadata are.
1768 struct flow_tnl flow_tnl
= flow
->tunnel
;
1769 odp_port
= tnl_port_send(xport
->ofport
, flow
, &ctx
->xout
->wc
);
1770 if (odp_port
== ODPP_NONE
) {
1771 xlate_report(ctx
, "Tunneling decided against output");
1772 goto out
; /* restore flow_nw_tos */
1774 if (flow
->tunnel
.ip_dst
== ctx
->orig_tunnel_ip_dst
) {
1775 xlate_report(ctx
, "Not tunneling to our own address");
1776 goto out
; /* restore flow_nw_tos */
1778 if (ctx
->xin
->resubmit_stats
) {
1779 netdev_vport_inc_tx(xport
->netdev
, ctx
->xin
->resubmit_stats
);
1781 out_port
= odp_port
;
1782 commit_odp_tunnel_action(flow
, &ctx
->base_flow
,
1783 &ctx
->xout
->odp_actions
);
1784 flow
->tunnel
= flow_tnl
; /* Restore tunnel metadata */
1786 ofp_port_t vlandev_port
;
1788 odp_port
= xport
->odp_port
;
1789 if (ofproto_has_vlan_splinters(ctx
->xbridge
->ofproto
)) {
1790 wc
->masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
1792 vlandev_port
= vsp_realdev_to_vlandev(ctx
->xbridge
->ofproto
, ofp_port
,
1794 if (vlandev_port
== ofp_port
) {
1795 out_port
= odp_port
;
1797 out_port
= ofp_port_to_odp_port(ctx
->xbridge
, vlandev_port
);
1798 flow
->vlan_tci
= htons(0);
1802 if (out_port
!= ODPP_NONE
) {
1803 ctx
->xout
->slow
|= commit_odp_actions(flow
, &ctx
->base_flow
,
1804 &ctx
->xout
->odp_actions
,
1806 &ctx
->mpls_depth_delta
);
1807 nl_msg_put_odp_port(&ctx
->xout
->odp_actions
, OVS_ACTION_ATTR_OUTPUT
,
1810 ctx
->sflow_odp_port
= odp_port
;
1811 ctx
->sflow_n_outputs
++;
1812 ctx
->xout
->nf_output_iface
= ofp_port
;
1817 flow
->vlan_tci
= flow_vlan_tci
;
1818 flow
->pkt_mark
= flow_pkt_mark
;
1819 flow
->nw_tos
= flow_nw_tos
;
1823 compose_output_action(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
)
1825 compose_output_action__(ctx
, ofp_port
, true);
1829 xlate_recursively(struct xlate_ctx
*ctx
, struct rule_dpif
*rule
)
1831 struct rule_dpif
*old_rule
= ctx
->rule
;
1832 struct rule_actions
*actions
;
1834 if (ctx
->xin
->resubmit_stats
) {
1835 rule_dpif_credit_stats(rule
, ctx
->xin
->resubmit_stats
);
1841 actions
= rule_dpif_get_actions(rule
);
1842 do_xlate_actions(actions
->ofpacts
, actions
->ofpacts_len
, ctx
);
1843 rule_actions_unref(actions
);
1844 ctx
->rule
= old_rule
;
1849 xlate_resubmit_resource_check(struct xlate_ctx
*ctx
)
1851 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
1853 if (ctx
->recurse
>= MAX_RESUBMIT_RECURSION
) {
1854 VLOG_ERR_RL(&rl
, "resubmit actions recursed over %d times",
1855 MAX_RESUBMIT_RECURSION
);
1856 } else if (ctx
->resubmits
>= MAX_RESUBMITS
) {
1857 VLOG_ERR_RL(&rl
, "over %d resubmit actions", MAX_RESUBMITS
);
1858 } else if (ctx
->xout
->odp_actions
.size
> UINT16_MAX
) {
1859 VLOG_ERR_RL(&rl
, "resubmits yielded over 64 kB of actions");
1860 } else if (ctx
->stack
.size
>= 65536) {
1861 VLOG_ERR_RL(&rl
, "resubmits yielded over 64 kB of stack");
1870 xlate_table_action(struct xlate_ctx
*ctx
,
1871 ofp_port_t in_port
, uint8_t table_id
, bool may_packet_in
)
1873 if (xlate_resubmit_resource_check(ctx
)) {
1874 ofp_port_t old_in_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
1875 bool skip_wildcards
= ctx
->xin
->skip_wildcards
;
1876 uint8_t old_table_id
= ctx
->table_id
;
1877 struct rule_dpif
*rule
;
1879 ctx
->table_id
= table_id
;
1881 /* Look up a flow with 'in_port' as the input port. Then restore the
1882 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
1883 * have surprising behavior). */
1884 ctx
->xin
->flow
.in_port
.ofp_port
= in_port
;
1885 rule_dpif_lookup_in_table(ctx
->xbridge
->ofproto
, &ctx
->xin
->flow
,
1886 !skip_wildcards
? &ctx
->xout
->wc
: NULL
,
1888 ctx
->xin
->flow
.in_port
.ofp_port
= old_in_port
;
1890 if (ctx
->xin
->resubmit_hook
) {
1891 ctx
->xin
->resubmit_hook(ctx
->xin
, rule
, ctx
->recurse
);
1894 if (!rule
&& may_packet_in
) {
1895 struct xport
*xport
;
1898 * check if table configuration flags
1899 * OFPTC11_TABLE_MISS_CONTROLLER, default.
1900 * OFPTC11_TABLE_MISS_CONTINUE,
1901 * OFPTC11_TABLE_MISS_DROP
1902 * When OF1.0, OFPTC11_TABLE_MISS_CONTINUE is used. What to do? */
1903 xport
= get_ofp_port(ctx
->xbridge
, ctx
->xin
->flow
.in_port
.ofp_port
);
1904 choose_miss_rule(xport
? xport
->config
: 0,
1905 ctx
->xbridge
->miss_rule
,
1906 ctx
->xbridge
->no_packet_in_rule
, &rule
);
1909 xlate_recursively(ctx
, rule
);
1910 rule_dpif_unref(rule
);
1913 ctx
->table_id
= old_table_id
;
1921 xlate_group_bucket(struct xlate_ctx
*ctx
, const struct ofputil_bucket
*bucket
)
1923 uint64_t action_list_stub
[1024 / 8];
1924 struct ofpbuf action_list
, action_set
;
1926 ofpbuf_use_const(&action_set
, bucket
->ofpacts
, bucket
->ofpacts_len
);
1927 ofpbuf_use_stub(&action_list
, action_list_stub
, sizeof action_list_stub
);
1929 ofpacts_execute_action_set(&action_list
, &action_set
);
1931 do_xlate_actions(action_list
.data
, action_list
.size
, ctx
);
1934 ofpbuf_uninit(&action_set
);
1935 ofpbuf_uninit(&action_list
);
1939 xlate_all_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
1941 const struct ofputil_bucket
*bucket
;
1942 const struct list
*buckets
;
1943 struct flow old_flow
= ctx
->xin
->flow
;
1945 group_dpif_get_buckets(group
, &buckets
);
1947 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
1948 xlate_group_bucket(ctx
, bucket
);
1949 /* Roll back flow to previous state.
1950 * This is equivalent to cloning the packet for each bucket.
1952 * As a side effect any subsequently applied actions will
1953 * also effectively be applied to a clone of the packet taken
1954 * just before applying the all or indirect group. */
1955 ctx
->xin
->flow
= old_flow
;
1960 xlate_ff_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
1962 const struct ofputil_bucket
*bucket
;
1964 bucket
= group_first_live_bucket(ctx
, group
, 0);
1966 xlate_group_bucket(ctx
, bucket
);
1971 xlate_select_group(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
1973 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
1974 const struct ofputil_bucket
*bucket
;
1977 basis
= hash_bytes(ctx
->xin
->flow
.dl_dst
, sizeof ctx
->xin
->flow
.dl_dst
, 0);
1978 bucket
= group_best_live_bucket(ctx
, group
, basis
);
1980 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
1981 xlate_group_bucket(ctx
, bucket
);
1986 xlate_group_action__(struct xlate_ctx
*ctx
, struct group_dpif
*group
)
1988 switch (group_dpif_get_type(group
)) {
1990 case OFPGT11_INDIRECT
:
1991 xlate_all_group(ctx
, group
);
1993 case OFPGT11_SELECT
:
1994 xlate_select_group(ctx
, group
);
1997 xlate_ff_group(ctx
, group
);
2002 group_dpif_release(group
);
2006 xlate_group_action(struct xlate_ctx
*ctx
, uint32_t group_id
)
2008 if (xlate_resubmit_resource_check(ctx
)) {
2009 struct group_dpif
*group
;
2012 got_group
= group_dpif_lookup(ctx
->xbridge
->ofproto
, group_id
, &group
);
2014 xlate_group_action__(ctx
, group
);
2024 xlate_ofpact_resubmit(struct xlate_ctx
*ctx
,
2025 const struct ofpact_resubmit
*resubmit
)
2030 in_port
= resubmit
->in_port
;
2031 if (in_port
== OFPP_IN_PORT
) {
2032 in_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
2035 table_id
= resubmit
->table_id
;
2036 if (table_id
== 255) {
2037 table_id
= ctx
->table_id
;
2040 xlate_table_action(ctx
, in_port
, table_id
, false);
2044 flood_packets(struct xlate_ctx
*ctx
, bool all
)
2046 const struct xport
*xport
;
2048 HMAP_FOR_EACH (xport
, ofp_node
, &ctx
->xbridge
->xports
) {
2049 if (xport
->ofp_port
== ctx
->xin
->flow
.in_port
.ofp_port
) {
2054 compose_output_action__(ctx
, xport
->ofp_port
, false);
2055 } else if (!(xport
->config
& OFPUTIL_PC_NO_FLOOD
)) {
2056 compose_output_action(ctx
, xport
->ofp_port
);
2060 ctx
->xout
->nf_output_iface
= NF_OUT_FLOOD
;
2064 execute_controller_action(struct xlate_ctx
*ctx
, int len
,
2065 enum ofp_packet_in_reason reason
,
2066 uint16_t controller_id
)
2068 struct ofproto_packet_in
*pin
;
2069 struct ofpbuf
*packet
;
2072 ctx
->xout
->slow
|= SLOW_CONTROLLER
;
2073 if (!ctx
->xin
->packet
) {
2077 packet
= ofpbuf_clone(ctx
->xin
->packet
);
2079 key
.skb_priority
= 0;
2081 memset(&key
.tunnel
, 0, sizeof key
.tunnel
);
2083 ctx
->xout
->slow
|= commit_odp_actions(&ctx
->xin
->flow
, &ctx
->base_flow
,
2084 &ctx
->xout
->odp_actions
,
2086 &ctx
->mpls_depth_delta
);
2088 odp_execute_actions(NULL
, packet
, &key
, ctx
->xout
->odp_actions
.data
,
2089 ctx
->xout
->odp_actions
.size
, NULL
, NULL
);
2091 pin
= xmalloc(sizeof *pin
);
2092 pin
->up
.packet_len
= packet
->size
;
2093 pin
->up
.packet
= ofpbuf_steal_data(packet
);
2094 pin
->up
.reason
= reason
;
2095 pin
->up
.table_id
= ctx
->table_id
;
2096 pin
->up
.cookie
= (ctx
->rule
2097 ? rule_dpif_get_flow_cookie(ctx
->rule
)
2100 flow_get_metadata(&ctx
->xin
->flow
, &pin
->up
.fmd
);
2102 pin
->controller_id
= controller_id
;
2103 pin
->send_len
= len
;
2104 pin
->generated_by_table_miss
= (ctx
->rule
2105 && rule_dpif_is_table_miss(ctx
->rule
));
2106 ofproto_dpif_send_packet_in(ctx
->xbridge
->ofproto
, pin
);
2107 ofpbuf_delete(packet
);
2111 compose_mpls_push_action(struct xlate_ctx
*ctx
, ovs_be16 eth_type
)
2113 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
2114 struct flow
*flow
= &ctx
->xin
->flow
;
2116 ovs_assert(eth_type_mpls(eth_type
));
2118 /* If mpls_depth_delta is negative then an MPLS POP action has been
2119 * composed and the resulting MPLS label stack is unknown. This means
2120 * an MPLS PUSH action can't be composed as it needs to know either the
2121 * top-most MPLS LSE to use as a template for the new MPLS LSE, or that
2122 * there is no MPLS label stack present. Thus, stop processing.
2124 * If mpls_depth_delta is positive then an MPLS PUSH action has been
2125 * composed and no further MPLS PUSH action may be performed without
2126 * losing MPLS LSE and ether type information held in xtx->xin->flow.
2127 * Thus, stop processing.
2129 * If the MPLS LSE of the flow and base_flow differ then the MPLS LSE
2130 * has been updated. Performing a MPLS PUSH action may be would result in
2131 * losing MPLS LSE and ether type information held in xtx->xin->flow.
2132 * Thus, stop processing.
2134 * It is planned that in the future this case will be handled
2135 * by recirculation */
2136 if (ctx
->mpls_depth_delta
||
2137 ctx
->xin
->flow
.mpls_lse
!= ctx
->base_flow
.mpls_lse
) {
2141 memset(&wc
->masks
.mpls_lse
, 0xff, sizeof wc
->masks
.mpls_lse
);
2143 ctx
->pre_push_mpls_lse
= ctx
->xin
->flow
.mpls_lse
;
2145 if (eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
2146 flow
->mpls_lse
&= ~htonl(MPLS_BOS_MASK
);
2151 if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
2152 label
= htonl(0x2); /* IPV6 Explicit Null. */
2154 label
= htonl(0x0); /* IPV4 Explicit Null. */
2156 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
2157 wc
->masks
.nw_ttl
= 0xff;
2158 tc
= (flow
->nw_tos
& IP_DSCP_MASK
) >> 2;
2159 ttl
= flow
->nw_ttl
? flow
->nw_ttl
: 0x40;
2160 flow
->mpls_lse
= set_mpls_lse_values(ttl
, tc
, 1, label
);
2162 flow
->dl_type
= eth_type
;
2163 ctx
->mpls_depth_delta
++;
2169 compose_mpls_pop_action(struct xlate_ctx
*ctx
, ovs_be16 eth_type
)
2171 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
2173 if (!eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
2177 /* If mpls_depth_delta is negative then an MPLS POP action has been
2178 * composed. Performing another MPLS POP action
2179 * would result in losing ether type that results from
2180 * the already composed MPLS POP. Thus, stop processing.
2182 * It is planned that in the future this case will be handled
2183 * by recirculation */
2184 if (ctx
->mpls_depth_delta
< 0) {
2188 memset(&wc
->masks
.mpls_lse
, 0xff, sizeof wc
->masks
.mpls_lse
);
2190 /* If mpls_depth_delta is positive then an MPLS PUSH action has been
2191 * executed and the previous MPLS LSE saved in ctx->pre_push_mpls_lse. The
2192 * flow's MPLS LSE should be restored to that value to allow any
2193 * subsequent actions that update of the LSE to be executed correctly.
2195 if (ctx
->mpls_depth_delta
> 0) {
2196 ctx
->xin
->flow
.mpls_lse
= ctx
->pre_push_mpls_lse
;
2199 ctx
->xin
->flow
.dl_type
= eth_type
;
2200 ctx
->mpls_depth_delta
--;
2206 compose_dec_ttl(struct xlate_ctx
*ctx
, struct ofpact_cnt_ids
*ids
)
2208 struct flow
*flow
= &ctx
->xin
->flow
;
2210 if (!is_ip_any(flow
)) {
2214 ctx
->xout
->wc
.masks
.nw_ttl
= 0xff;
2215 if (flow
->nw_ttl
> 1) {
2221 for (i
= 0; i
< ids
->n_controllers
; i
++) {
2222 execute_controller_action(ctx
, UINT16_MAX
, OFPR_INVALID_TTL
,
2226 /* Stop processing for current table. */
2232 compose_set_mpls_label_action(struct xlate_ctx
*ctx
, ovs_be32 label
)
2234 if (!eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
2238 /* If mpls_depth_delta is negative then an MPLS POP action has been
2239 * executed and the resulting MPLS label stack is unknown. This means
2240 * a SET MPLS LABEL action can't be executed as it needs to manipulate
2241 * the top-most MPLS LSE. Thus, stop processing.
2243 * It is planned that in the future this case will be handled
2246 if (ctx
->mpls_depth_delta
< 0) {
2250 ctx
->xout
->wc
.masks
.mpls_lse
|= htonl(MPLS_LABEL_MASK
);
2251 set_mpls_lse_label(&ctx
->xin
->flow
.mpls_lse
, label
);
2256 compose_set_mpls_tc_action(struct xlate_ctx
*ctx
, uint8_t tc
)
2258 if (!eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
2262 /* If mpls_depth_delta is negative then an MPLS POP action has been
2263 * executed and the resulting MPLS label stack is unknown. This means
2264 * a SET MPLS TC action can't be executed as it needs to manipulate
2265 * the top-most MPLS LSE. Thus, stop processing.
2267 * It is planned that in the future this case will be handled
2270 if (ctx
->mpls_depth_delta
< 0) {
2274 ctx
->xout
->wc
.masks
.mpls_lse
|= htonl(MPLS_TC_MASK
);
2275 set_mpls_lse_tc(&ctx
->xin
->flow
.mpls_lse
, tc
);
2280 compose_set_mpls_ttl_action(struct xlate_ctx
*ctx
, uint8_t ttl
)
2282 if (!eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
2286 /* If mpls_depth_delta is negative then an MPLS POP action has been
2287 * executed and the resulting MPLS label stack is unknown. This means
2288 * a SET MPLS TTL push action can't be executed as it needs to manipulate
2289 * the top-most MPLS LSE. Thus, stop processing.
2291 * It is planned that in the future this case will be handled
2294 if (ctx
->mpls_depth_delta
< 0) {
2298 ctx
->xout
->wc
.masks
.mpls_lse
|= htonl(MPLS_TTL_MASK
);
2299 set_mpls_lse_ttl(&ctx
->xin
->flow
.mpls_lse
, ttl
);
2304 compose_dec_mpls_ttl_action(struct xlate_ctx
*ctx
)
2306 struct flow
*flow
= &ctx
->xin
->flow
;
2307 uint8_t ttl
= mpls_lse_to_ttl(flow
->mpls_lse
);
2308 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
2310 memset(&wc
->masks
.mpls_lse
, 0xff, sizeof wc
->masks
.mpls_lse
);
2312 if (!eth_type_mpls(flow
->dl_type
)) {
2318 set_mpls_lse_ttl(&flow
->mpls_lse
, ttl
);
2321 execute_controller_action(ctx
, UINT16_MAX
, OFPR_INVALID_TTL
, 0);
2323 /* Stop processing for current table. */
2329 xlate_output_action(struct xlate_ctx
*ctx
,
2330 ofp_port_t port
, uint16_t max_len
, bool may_packet_in
)
2332 ofp_port_t prev_nf_output_iface
= ctx
->xout
->nf_output_iface
;
2334 ctx
->xout
->nf_output_iface
= NF_OUT_DROP
;
2338 compose_output_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
);
2341 xlate_table_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
,
2348 flood_packets(ctx
, false);
2351 flood_packets(ctx
, true);
2353 case OFPP_CONTROLLER
:
2354 execute_controller_action(ctx
, max_len
, OFPR_ACTION
, 0);
2360 if (port
!= ctx
->xin
->flow
.in_port
.ofp_port
) {
2361 compose_output_action(ctx
, port
);
2363 xlate_report(ctx
, "skipping output to input port");
2368 if (prev_nf_output_iface
== NF_OUT_FLOOD
) {
2369 ctx
->xout
->nf_output_iface
= NF_OUT_FLOOD
;
2370 } else if (ctx
->xout
->nf_output_iface
== NF_OUT_DROP
) {
2371 ctx
->xout
->nf_output_iface
= prev_nf_output_iface
;
2372 } else if (prev_nf_output_iface
!= NF_OUT_DROP
&&
2373 ctx
->xout
->nf_output_iface
!= NF_OUT_FLOOD
) {
2374 ctx
->xout
->nf_output_iface
= NF_OUT_MULTI
;
2379 xlate_output_reg_action(struct xlate_ctx
*ctx
,
2380 const struct ofpact_output_reg
*or)
2382 uint64_t port
= mf_get_subfield(&or->src
, &ctx
->xin
->flow
);
2383 if (port
<= UINT16_MAX
) {
2384 union mf_subvalue value
;
2386 memset(&value
, 0xff, sizeof value
);
2387 mf_write_subfield_flow(&or->src
, &value
, &ctx
->xout
->wc
.masks
);
2388 xlate_output_action(ctx
, u16_to_ofp(port
),
2389 or->max_len
, false);
2394 xlate_enqueue_action(struct xlate_ctx
*ctx
,
2395 const struct ofpact_enqueue
*enqueue
)
2397 ofp_port_t ofp_port
= enqueue
->port
;
2398 uint32_t queue_id
= enqueue
->queue
;
2399 uint32_t flow_priority
, priority
;
2402 /* Translate queue to priority. */
2403 error
= dpif_queue_to_priority(ctx
->xbridge
->dpif
, queue_id
, &priority
);
2405 /* Fall back to ordinary output action. */
2406 xlate_output_action(ctx
, enqueue
->port
, 0, false);
2410 /* Check output port. */
2411 if (ofp_port
== OFPP_IN_PORT
) {
2412 ofp_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
2413 } else if (ofp_port
== ctx
->xin
->flow
.in_port
.ofp_port
) {
2417 /* Add datapath actions. */
2418 flow_priority
= ctx
->xin
->flow
.skb_priority
;
2419 ctx
->xin
->flow
.skb_priority
= priority
;
2420 compose_output_action(ctx
, ofp_port
);
2421 ctx
->xin
->flow
.skb_priority
= flow_priority
;
2423 /* Update NetFlow output port. */
2424 if (ctx
->xout
->nf_output_iface
== NF_OUT_DROP
) {
2425 ctx
->xout
->nf_output_iface
= ofp_port
;
2426 } else if (ctx
->xout
->nf_output_iface
!= NF_OUT_FLOOD
) {
2427 ctx
->xout
->nf_output_iface
= NF_OUT_MULTI
;
2432 xlate_set_queue_action(struct xlate_ctx
*ctx
, uint32_t queue_id
)
2434 uint32_t skb_priority
;
2436 if (!dpif_queue_to_priority(ctx
->xbridge
->dpif
, queue_id
, &skb_priority
)) {
2437 ctx
->xin
->flow
.skb_priority
= skb_priority
;
2439 /* Couldn't translate queue to a priority. Nothing to do. A warning
2440 * has already been logged. */
2445 slave_enabled_cb(ofp_port_t ofp_port
, void *xbridge_
)
2447 const struct xbridge
*xbridge
= xbridge_
;
2458 case OFPP_CONTROLLER
: /* Not supported by the bundle action. */
2461 port
= get_ofp_port(xbridge
, ofp_port
);
2462 return port
? port
->may_enable
: false;
2467 xlate_bundle_action(struct xlate_ctx
*ctx
,
2468 const struct ofpact_bundle
*bundle
)
2472 port
= bundle_execute(bundle
, &ctx
->xin
->flow
, &ctx
->xout
->wc
,
2474 CONST_CAST(struct xbridge
*, ctx
->xbridge
));
2475 if (bundle
->dst
.field
) {
2476 nxm_reg_load(&bundle
->dst
, ofp_to_u16(port
), &ctx
->xin
->flow
,
2479 xlate_output_action(ctx
, port
, 0, false);
2484 xlate_learn_action(struct xlate_ctx
*ctx
,
2485 const struct ofpact_learn
*learn
)
2487 uint64_t ofpacts_stub
[1024 / 8];
2488 struct ofputil_flow_mod fm
;
2489 struct ofpbuf ofpacts
;
2491 ctx
->xout
->has_learn
= true;
2493 learn_mask(learn
, &ctx
->xout
->wc
);
2495 if (!ctx
->xin
->may_learn
) {
2499 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
2500 learn_execute(learn
, &ctx
->xin
->flow
, &fm
, &ofpacts
);
2501 ofproto_dpif_flow_mod(ctx
->xbridge
->ofproto
, &fm
);
2502 ofpbuf_uninit(&ofpacts
);
2506 xlate_fin_timeout(struct xlate_ctx
*ctx
,
2507 const struct ofpact_fin_timeout
*oft
)
2509 if (ctx
->xin
->tcp_flags
& (TCP_FIN
| TCP_RST
) && ctx
->rule
) {
2510 rule_dpif_reduce_timeouts(ctx
->rule
, oft
->fin_idle_timeout
,
2511 oft
->fin_hard_timeout
);
2516 xlate_sample_action(struct xlate_ctx
*ctx
,
2517 const struct ofpact_sample
*os
)
2519 union user_action_cookie cookie
;
2520 /* Scale the probability from 16-bit to 32-bit while representing
2521 * the same percentage. */
2522 uint32_t probability
= (os
->probability
<< 16) | os
->probability
;
2524 ctx
->xout
->slow
|= commit_odp_actions(&ctx
->xin
->flow
, &ctx
->base_flow
,
2525 &ctx
->xout
->odp_actions
,
2527 &ctx
->mpls_depth_delta
);
2529 compose_flow_sample_cookie(os
->probability
, os
->collector_set_id
,
2530 os
->obs_domain_id
, os
->obs_point_id
, &cookie
);
2531 compose_sample_action(ctx
->xbridge
, &ctx
->xout
->odp_actions
, &ctx
->xin
->flow
,
2532 probability
, &cookie
, sizeof cookie
.flow_sample
);
2536 may_receive(const struct xport
*xport
, struct xlate_ctx
*ctx
)
2538 if (xport
->config
& (eth_addr_equals(ctx
->xin
->flow
.dl_dst
, eth_addr_stp
)
2539 ? OFPUTIL_PC_NO_RECV_STP
2540 : OFPUTIL_PC_NO_RECV
)) {
2544 /* Only drop packets here if both forwarding and learning are
2545 * disabled. If just learning is enabled, we need to have
2546 * OFPP_NORMAL and the learning action have a look at the packet
2547 * before we can drop it. */
2548 if (!xport_stp_forward_state(xport
) && !xport_stp_learn_state(xport
)) {
2556 xlate_write_actions(struct xlate_ctx
*ctx
, const struct ofpact
*a
)
2558 struct ofpact_nest
*on
= ofpact_get_WRITE_ACTIONS(a
);
2559 ofpbuf_put(&ctx
->action_set
, on
->actions
, ofpact_nest_get_action_len(on
));
2560 ofpact_pad(&ctx
->action_set
);
2564 xlate_action_set(struct xlate_ctx
*ctx
)
2566 uint64_t action_list_stub
[1024 / 64];
2567 struct ofpbuf action_list
;
2569 ofpbuf_use_stub(&action_list
, action_list_stub
, sizeof action_list_stub
);
2570 ofpacts_execute_action_set(&action_list
, &ctx
->action_set
);
2571 do_xlate_actions(action_list
.data
, action_list
.size
, ctx
);
2572 ofpbuf_uninit(&action_list
);
2576 do_xlate_actions(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
2577 struct xlate_ctx
*ctx
)
2579 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
2580 struct flow
*flow
= &ctx
->xin
->flow
;
2581 const struct ofpact
*a
;
2583 /* dl_type already in the mask, not set below. */
2585 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
2586 struct ofpact_controller
*controller
;
2587 const struct ofpact_metadata
*metadata
;
2588 const struct ofpact_set_field
*set_field
;
2589 const struct mf_field
*mf
;
2597 xlate_output_action(ctx
, ofpact_get_OUTPUT(a
)->port
,
2598 ofpact_get_OUTPUT(a
)->max_len
, true);
2602 if (xlate_group_action(ctx
, ofpact_get_GROUP(a
)->group_id
)) {
2607 case OFPACT_CONTROLLER
:
2608 controller
= ofpact_get_CONTROLLER(a
);
2609 execute_controller_action(ctx
, controller
->max_len
,
2611 controller
->controller_id
);
2614 case OFPACT_ENQUEUE
:
2615 xlate_enqueue_action(ctx
, ofpact_get_ENQUEUE(a
));
2618 case OFPACT_SET_VLAN_VID
:
2619 wc
->masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
2620 if (flow
->vlan_tci
& htons(VLAN_CFI
) ||
2621 ofpact_get_SET_VLAN_VID(a
)->push_vlan_if_needed
) {
2622 flow
->vlan_tci
&= ~htons(VLAN_VID_MASK
);
2623 flow
->vlan_tci
|= (htons(ofpact_get_SET_VLAN_VID(a
)->vlan_vid
)
2628 case OFPACT_SET_VLAN_PCP
:
2629 wc
->masks
.vlan_tci
|= htons(VLAN_PCP_MASK
| VLAN_CFI
);
2630 if (flow
->vlan_tci
& htons(VLAN_CFI
) ||
2631 ofpact_get_SET_VLAN_PCP(a
)->push_vlan_if_needed
) {
2632 flow
->vlan_tci
&= ~htons(VLAN_PCP_MASK
);
2633 flow
->vlan_tci
|= htons((ofpact_get_SET_VLAN_PCP(a
)->vlan_pcp
2634 << VLAN_PCP_SHIFT
) | VLAN_CFI
);
2638 case OFPACT_STRIP_VLAN
:
2639 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
2640 flow
->vlan_tci
= htons(0);
2643 case OFPACT_PUSH_VLAN
:
2644 /* XXX 802.1AD(QinQ) */
2645 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
2646 flow
->vlan_tci
= htons(VLAN_CFI
);
2649 case OFPACT_SET_ETH_SRC
:
2650 memset(&wc
->masks
.dl_src
, 0xff, sizeof wc
->masks
.dl_src
);
2651 memcpy(flow
->dl_src
, ofpact_get_SET_ETH_SRC(a
)->mac
, ETH_ADDR_LEN
);
2654 case OFPACT_SET_ETH_DST
:
2655 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
2656 memcpy(flow
->dl_dst
, ofpact_get_SET_ETH_DST(a
)->mac
, ETH_ADDR_LEN
);
2659 case OFPACT_SET_IPV4_SRC
:
2660 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2661 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
2662 flow
->nw_src
= ofpact_get_SET_IPV4_SRC(a
)->ipv4
;
2666 case OFPACT_SET_IPV4_DST
:
2667 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2668 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
2669 flow
->nw_dst
= ofpact_get_SET_IPV4_DST(a
)->ipv4
;
2673 case OFPACT_SET_IP_DSCP
:
2674 if (is_ip_any(flow
)) {
2675 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
2676 flow
->nw_tos
&= ~IP_DSCP_MASK
;
2677 flow
->nw_tos
|= ofpact_get_SET_IP_DSCP(a
)->dscp
;
2681 case OFPACT_SET_IP_ECN
:
2682 if (is_ip_any(flow
)) {
2683 wc
->masks
.nw_tos
|= IP_ECN_MASK
;
2684 flow
->nw_tos
&= ~IP_ECN_MASK
;
2685 flow
->nw_tos
|= ofpact_get_SET_IP_ECN(a
)->ecn
;
2689 case OFPACT_SET_IP_TTL
:
2690 if (is_ip_any(flow
)) {
2691 wc
->masks
.nw_ttl
= 0xff;
2692 flow
->nw_ttl
= ofpact_get_SET_IP_TTL(a
)->ttl
;
2696 case OFPACT_SET_L4_SRC_PORT
:
2697 if (is_ip_any(flow
)) {
2698 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
2699 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
2700 flow
->tp_src
= htons(ofpact_get_SET_L4_SRC_PORT(a
)->port
);
2704 case OFPACT_SET_L4_DST_PORT
:
2705 if (is_ip_any(flow
)) {
2706 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
2707 memset(&wc
->masks
.tp_dst
, 0xff, sizeof wc
->masks
.tp_dst
);
2708 flow
->tp_dst
= htons(ofpact_get_SET_L4_DST_PORT(a
)->port
);
2712 case OFPACT_RESUBMIT
:
2713 xlate_ofpact_resubmit(ctx
, ofpact_get_RESUBMIT(a
));
2716 case OFPACT_SET_TUNNEL
:
2717 flow
->tunnel
.tun_id
= htonll(ofpact_get_SET_TUNNEL(a
)->tun_id
);
2720 case OFPACT_SET_QUEUE
:
2721 xlate_set_queue_action(ctx
, ofpact_get_SET_QUEUE(a
)->queue_id
);
2724 case OFPACT_POP_QUEUE
:
2725 flow
->skb_priority
= ctx
->orig_skb_priority
;
2728 case OFPACT_REG_MOVE
:
2729 nxm_execute_reg_move(ofpact_get_REG_MOVE(a
), flow
, wc
);
2732 case OFPACT_REG_LOAD
:
2733 nxm_execute_reg_load(ofpact_get_REG_LOAD(a
), flow
, wc
);
2736 case OFPACT_SET_FIELD
:
2737 set_field
= ofpact_get_SET_FIELD(a
);
2738 mf
= set_field
->field
;
2739 mf_mask_field_and_prereqs(mf
, &wc
->masks
);
2741 /* Set field action only ever overwrites packet's outermost
2742 * applicable header fields. Do nothing if no header exists. */
2743 if ((mf
->id
!= MFF_VLAN_VID
|| flow
->vlan_tci
& htons(VLAN_CFI
))
2744 && ((mf
->id
!= MFF_MPLS_LABEL
&& mf
->id
!= MFF_MPLS_TC
)
2745 || flow
->mpls_lse
)) {
2746 mf_set_flow_value(mf
, &set_field
->value
, flow
);
2750 case OFPACT_STACK_PUSH
:
2751 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a
), flow
, wc
,
2755 case OFPACT_STACK_POP
:
2756 nxm_execute_stack_pop(ofpact_get_STACK_POP(a
), flow
, wc
,
2760 case OFPACT_PUSH_MPLS
:
2761 if (compose_mpls_push_action(ctx
,
2762 ofpact_get_PUSH_MPLS(a
)->ethertype
)) {
2767 case OFPACT_POP_MPLS
:
2768 if (compose_mpls_pop_action(ctx
,
2769 ofpact_get_POP_MPLS(a
)->ethertype
)) {
2774 case OFPACT_SET_MPLS_LABEL
:
2775 if (compose_set_mpls_label_action(ctx
,
2776 ofpact_get_SET_MPLS_LABEL(a
)->label
)) {
2781 case OFPACT_SET_MPLS_TC
:
2782 if (compose_set_mpls_tc_action(ctx
,
2783 ofpact_get_SET_MPLS_TC(a
)->tc
)) {
2788 case OFPACT_SET_MPLS_TTL
:
2789 if (compose_set_mpls_ttl_action(ctx
,
2790 ofpact_get_SET_MPLS_TTL(a
)->ttl
)) {
2795 case OFPACT_DEC_MPLS_TTL
:
2796 if (compose_dec_mpls_ttl_action(ctx
)) {
2801 case OFPACT_DEC_TTL
:
2802 wc
->masks
.nw_ttl
= 0xff;
2803 if (compose_dec_ttl(ctx
, ofpact_get_DEC_TTL(a
))) {
2809 /* Nothing to do. */
2812 case OFPACT_MULTIPATH
:
2813 multipath_execute(ofpact_get_MULTIPATH(a
), flow
, wc
);
2817 xlate_bundle_action(ctx
, ofpact_get_BUNDLE(a
));
2820 case OFPACT_OUTPUT_REG
:
2821 xlate_output_reg_action(ctx
, ofpact_get_OUTPUT_REG(a
));
2825 xlate_learn_action(ctx
, ofpact_get_LEARN(a
));
2832 case OFPACT_FIN_TIMEOUT
:
2833 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
2834 ctx
->xout
->has_fin_timeout
= true;
2835 xlate_fin_timeout(ctx
, ofpact_get_FIN_TIMEOUT(a
));
2838 case OFPACT_CLEAR_ACTIONS
:
2839 ofpbuf_clear(&ctx
->action_set
);
2842 case OFPACT_WRITE_ACTIONS
:
2843 xlate_write_actions(ctx
, a
);
2846 case OFPACT_WRITE_METADATA
:
2847 metadata
= ofpact_get_WRITE_METADATA(a
);
2848 flow
->metadata
&= ~metadata
->mask
;
2849 flow
->metadata
|= metadata
->metadata
& metadata
->mask
;
2853 /* Not implemented yet. */
2856 case OFPACT_GOTO_TABLE
: {
2857 struct ofpact_goto_table
*ogt
= ofpact_get_GOTO_TABLE(a
);
2859 ovs_assert(ctx
->table_id
< ogt
->table_id
);
2860 xlate_table_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
,
2861 ogt
->table_id
, true);
2866 xlate_sample_action(ctx
, ofpact_get_SAMPLE(a
));
2873 xlate_in_init(struct xlate_in
*xin
, struct ofproto_dpif
*ofproto
,
2874 const struct flow
*flow
, struct rule_dpif
*rule
,
2875 uint16_t tcp_flags
, const struct ofpbuf
*packet
)
2877 xin
->ofproto
= ofproto
;
2879 xin
->packet
= packet
;
2880 xin
->may_learn
= packet
!= NULL
;
2882 xin
->ofpacts
= NULL
;
2883 xin
->ofpacts_len
= 0;
2884 xin
->tcp_flags
= tcp_flags
;
2885 xin
->resubmit_hook
= NULL
;
2886 xin
->report_hook
= NULL
;
2887 xin
->resubmit_stats
= NULL
;
2888 xin
->skip_wildcards
= false;
2892 xlate_out_uninit(struct xlate_out
*xout
)
2895 ofpbuf_uninit(&xout
->odp_actions
);
2899 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
2900 * into datapath actions, using 'ctx', and discards the datapath actions. */
2902 xlate_actions_for_side_effects(struct xlate_in
*xin
)
2904 struct xlate_out xout
;
2906 xlate_actions(xin
, &xout
);
2907 xlate_out_uninit(&xout
);
2911 xlate_report(struct xlate_ctx
*ctx
, const char *s
)
2913 if (ctx
->xin
->report_hook
) {
2914 ctx
->xin
->report_hook(ctx
->xin
, s
, ctx
->recurse
);
2919 xlate_out_copy(struct xlate_out
*dst
, const struct xlate_out
*src
)
2922 dst
->slow
= src
->slow
;
2923 dst
->has_learn
= src
->has_learn
;
2924 dst
->has_normal
= src
->has_normal
;
2925 dst
->has_fin_timeout
= src
->has_fin_timeout
;
2926 dst
->nf_output_iface
= src
->nf_output_iface
;
2927 dst
->mirrors
= src
->mirrors
;
2929 ofpbuf_use_stub(&dst
->odp_actions
, dst
->odp_actions_stub
,
2930 sizeof dst
->odp_actions_stub
);
2931 ofpbuf_put(&dst
->odp_actions
, src
->odp_actions
.data
,
2932 src
->odp_actions
.size
);
2935 static struct skb_priority_to_dscp
*
2936 get_skb_priority(const struct xport
*xport
, uint32_t skb_priority
)
2938 struct skb_priority_to_dscp
*pdscp
;
2941 hash
= hash_int(skb_priority
, 0);
2942 HMAP_FOR_EACH_IN_BUCKET (pdscp
, hmap_node
, hash
, &xport
->skb_priorities
) {
2943 if (pdscp
->skb_priority
== skb_priority
) {
2951 dscp_from_skb_priority(const struct xport
*xport
, uint32_t skb_priority
,
2954 struct skb_priority_to_dscp
*pdscp
= get_skb_priority(xport
, skb_priority
);
2955 *dscp
= pdscp
? pdscp
->dscp
: 0;
2956 return pdscp
!= NULL
;
2960 clear_skb_priorities(struct xport
*xport
)
2962 struct skb_priority_to_dscp
*pdscp
, *next
;
2964 HMAP_FOR_EACH_SAFE (pdscp
, next
, hmap_node
, &xport
->skb_priorities
) {
2965 hmap_remove(&xport
->skb_priorities
, &pdscp
->hmap_node
);
2971 actions_output_to_local_port(const struct xlate_ctx
*ctx
)
2973 odp_port_t local_odp_port
= ofp_port_to_odp_port(ctx
->xbridge
, OFPP_LOCAL
);
2974 const struct nlattr
*a
;
2977 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, ctx
->xout
->odp_actions
.data
,
2978 ctx
->xout
->odp_actions
.size
) {
2979 if (nl_attr_type(a
) == OVS_ACTION_ATTR_OUTPUT
2980 && nl_attr_get_odp_port(a
) == local_odp_port
) {
2987 /* Thread safe call to xlate_actions__(). */
2989 xlate_actions(struct xlate_in
*xin
, struct xlate_out
*xout
)
2991 ovs_rwlock_rdlock(&xlate_rwlock
);
2992 xlate_actions__(xin
, xout
);
2993 ovs_rwlock_unlock(&xlate_rwlock
);
2996 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
2997 * into datapath actions in 'odp_actions', using 'ctx'.
2999 * The caller must take responsibility for eventually freeing 'xout', with
3000 * xlate_out_uninit(). */
3002 xlate_actions__(struct xlate_in
*xin
, struct xlate_out
*xout
)
3003 OVS_REQ_RDLOCK(xlate_rwlock
)
3005 struct flow_wildcards
*wc
= &xout
->wc
;
3006 struct flow
*flow
= &xin
->flow
;
3007 struct rule_dpif
*rule
= NULL
;
3009 struct rule_actions
*actions
= NULL
;
3010 enum slow_path_reason special
;
3011 const struct ofpact
*ofpacts
;
3012 struct xport
*in_port
;
3013 struct flow orig_flow
;
3014 struct xlate_ctx ctx
;
3018 COVERAGE_INC(xlate_actions
);
3020 /* Flow initialization rules:
3021 * - 'base_flow' must match the kernel's view of the packet at the
3022 * time that action processing starts. 'flow' represents any
3023 * transformations we wish to make through actions.
3024 * - By default 'base_flow' and 'flow' are the same since the input
3025 * packet matches the output before any actions are applied.
3026 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
3027 * of the received packet as seen by the kernel. If we later output
3028 * to another device without any modifications this will cause us to
3029 * insert a new tag since the original one was stripped off by the
3031 * - Tunnel metadata as received is retained in 'flow'. This allows
3032 * tunnel metadata matching also in later tables.
3033 * Since a kernel action for setting the tunnel metadata will only be
3034 * generated with actual tunnel output, changing the tunnel metadata
3035 * values in 'flow' (such as tun_id) will only have effect with a later
3036 * tunnel output action.
3037 * - Tunnel 'base_flow' is completely cleared since that is what the
3038 * kernel does. If we wish to maintain the original values an action
3039 * needs to be generated. */
3044 ctx
.xout
->has_learn
= false;
3045 ctx
.xout
->has_normal
= false;
3046 ctx
.xout
->has_fin_timeout
= false;
3047 ctx
.xout
->nf_output_iface
= NF_OUT_DROP
;
3048 ctx
.xout
->mirrors
= 0;
3049 ofpbuf_use_stub(&ctx
.xout
->odp_actions
, ctx
.xout
->odp_actions_stub
,
3050 sizeof ctx
.xout
->odp_actions_stub
);
3051 ofpbuf_reserve(&ctx
.xout
->odp_actions
, NL_A_U32_SIZE
);
3053 ctx
.xbridge
= xbridge_lookup(xin
->ofproto
);
3058 ctx
.rule
= xin
->rule
;
3060 ctx
.base_flow
= *flow
;
3061 memset(&ctx
.base_flow
.tunnel
, 0, sizeof ctx
.base_flow
.tunnel
);
3062 ctx
.orig_tunnel_ip_dst
= flow
->tunnel
.ip_dst
;
3064 flow_wildcards_init_catchall(wc
);
3065 memset(&wc
->masks
.in_port
, 0xff, sizeof wc
->masks
.in_port
);
3066 memset(&wc
->masks
.skb_priority
, 0xff, sizeof wc
->masks
.skb_priority
);
3067 memset(&wc
->masks
.dl_type
, 0xff, sizeof wc
->masks
.dl_type
);
3068 wc
->masks
.nw_frag
|= FLOW_NW_FRAG_MASK
;
3070 tnl_may_send
= tnl_xlate_init(&ctx
.base_flow
, flow
, wc
);
3071 if (ctx
.xbridge
->netflow
) {
3072 netflow_mask_wc(flow
, wc
);
3077 ctx
.orig_skb_priority
= flow
->skb_priority
;
3080 ctx
.mpls_depth_delta
= 0;
3082 if (!xin
->ofpacts
&& !ctx
.rule
) {
3083 rule_dpif_lookup(ctx
.xbridge
->ofproto
, flow
,
3084 !xin
->skip_wildcards
? wc
: NULL
, &rule
);
3085 if (ctx
.xin
->resubmit_stats
) {
3086 rule_dpif_credit_stats(rule
, ctx
.xin
->resubmit_stats
);
3090 xout
->fail_open
= ctx
.rule
&& rule_dpif_is_fail_open(ctx
.rule
);
3093 ofpacts
= xin
->ofpacts
;
3094 ofpacts_len
= xin
->ofpacts_len
;
3095 } else if (ctx
.rule
) {
3096 actions
= rule_dpif_get_actions(ctx
.rule
);
3097 ofpacts
= actions
->ofpacts
;
3098 ofpacts_len
= actions
->ofpacts_len
;
3103 ofpbuf_use_stub(&ctx
.stack
, ctx
.init_stack
, sizeof ctx
.init_stack
);
3104 ofpbuf_use_stub(&ctx
.action_set
,
3105 ctx
.action_set_stub
, sizeof ctx
.action_set_stub
);
3107 if (mbridge_has_mirrors(ctx
.xbridge
->mbridge
)) {
3108 /* Do this conditionally because the copy is expensive enough that it
3109 * shows up in profiles. */
3113 if (flow
->nw_frag
& FLOW_NW_FRAG_ANY
) {
3114 switch (ctx
.xbridge
->frag
) {
3115 case OFPC_FRAG_NORMAL
:
3116 /* We must pretend that transport ports are unavailable. */
3117 flow
->tp_src
= ctx
.base_flow
.tp_src
= htons(0);
3118 flow
->tp_dst
= ctx
.base_flow
.tp_dst
= htons(0);
3121 case OFPC_FRAG_DROP
:
3124 case OFPC_FRAG_REASM
:
3127 case OFPC_FRAG_NX_MATCH
:
3128 /* Nothing to do. */
3131 case OFPC_INVALID_TTL_TO_CONTROLLER
:
3136 in_port
= get_ofp_port(ctx
.xbridge
, flow
->in_port
.ofp_port
);
3137 if (in_port
&& in_port
->is_tunnel
&& ctx
.xin
->resubmit_stats
) {
3138 netdev_vport_inc_rx(in_port
->netdev
, ctx
.xin
->resubmit_stats
);
3140 bfd_account_rx(in_port
->bfd
, ctx
.xin
->resubmit_stats
);
3144 special
= process_special(&ctx
, flow
, in_port
, ctx
.xin
->packet
);
3146 ctx
.xout
->slow
|= special
;
3148 size_t sample_actions_len
;
3150 if (flow
->in_port
.ofp_port
3151 != vsp_realdev_to_vlandev(ctx
.xbridge
->ofproto
,
3152 flow
->in_port
.ofp_port
,
3154 ctx
.base_flow
.vlan_tci
= 0;
3157 add_sflow_action(&ctx
);
3158 add_ipfix_action(&ctx
);
3159 sample_actions_len
= ctx
.xout
->odp_actions
.size
;
3161 if (tnl_may_send
&& (!in_port
|| may_receive(in_port
, &ctx
))) {
3162 do_xlate_actions(ofpacts
, ofpacts_len
, &ctx
);
3164 /* We've let OFPP_NORMAL and the learning action look at the
3165 * packet, so drop it now if forwarding is disabled. */
3166 if (in_port
&& !xport_stp_forward_state(in_port
)) {
3167 ctx
.xout
->odp_actions
.size
= sample_actions_len
;
3171 if (ctx
.action_set
.size
) {
3172 xlate_action_set(&ctx
);
3175 if (ctx
.xbridge
->has_in_band
3176 && in_band_must_output_to_local_port(flow
)
3177 && !actions_output_to_local_port(&ctx
)) {
3178 compose_output_action(&ctx
, OFPP_LOCAL
);
3181 fix_sflow_action(&ctx
);
3183 if (mbridge_has_mirrors(ctx
.xbridge
->mbridge
)) {
3184 add_mirror_actions(&ctx
, &orig_flow
);
3188 if (nl_attr_oversized(ctx
.xout
->odp_actions
.size
)) {
3189 /* These datapath actions are too big for a Netlink attribute, so we
3190 * can't hand them to the kernel directly. dpif_execute() can execute
3191 * them one by one with help, so just mark the result as SLOW_ACTION to
3192 * prevent the flow from being installed. */
3193 COVERAGE_INC(xlate_actions_oversize
);
3194 ctx
.xout
->slow
|= SLOW_ACTION
;
3197 if (ctx
.xin
->resubmit_stats
) {
3198 mirror_update_stats(ctx
.xbridge
->mbridge
, xout
->mirrors
,
3199 ctx
.xin
->resubmit_stats
->n_packets
,
3200 ctx
.xin
->resubmit_stats
->n_bytes
);
3202 if (ctx
.xbridge
->netflow
) {
3203 const struct ofpact
*ofpacts
;
3206 ofpacts_len
= actions
->ofpacts_len
;
3207 ofpacts
= actions
->ofpacts
;
3208 if (ofpacts_len
== 0
3209 || ofpacts
->type
!= OFPACT_CONTROLLER
3210 || ofpact_next(ofpacts
) < ofpact_end(ofpacts
, ofpacts_len
)) {
3211 /* Only update netflow if we don't have controller flow. We don't
3212 * report NetFlow expiration messages for such facets because they
3213 * are just part of the control logic for the network, not real
3215 netflow_flow_update(ctx
.xbridge
->netflow
, flow
,
3216 xout
->nf_output_iface
,
3217 ctx
.xin
->resubmit_stats
);
3222 ofpbuf_uninit(&ctx
.stack
);
3223 ofpbuf_uninit(&ctx
.action_set
);
3225 /* Clear the metadata and register wildcard masks, because we won't
3226 * use non-header fields as part of the cache. */
3227 flow_wildcards_clear_non_packet_fields(wc
);
3230 rule_actions_unref(actions
);
3231 rule_dpif_unref(rule
);
3234 /* Sends 'packet' out 'ofport'.
3235 * May modify 'packet'.
3236 * Returns 0 if successful, otherwise a positive errno value. */
3238 xlate_send_packet(const struct ofport_dpif
*ofport
, struct ofpbuf
*packet
)
3240 struct xport
*xport
;
3241 struct ofpact_output output
;
3243 union flow_in_port in_port_
;
3246 ofpact_init(&output
.ofpact
, OFPACT_OUTPUT
, sizeof output
);
3247 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
3248 in_port_
.ofp_port
= OFPP_NONE
;
3249 flow_extract(packet
, 0, 0, NULL
, &in_port_
, &flow
);
3251 ovs_rwlock_rdlock(&xlate_rwlock
);
3252 xport
= xport_lookup(ofport
);
3254 ovs_rwlock_unlock(&xlate_rwlock
);
3257 output
.port
= xport
->ofp_port
;
3259 error
= ofproto_dpif_execute_actions(xport
->xbridge
->ofproto
, &flow
, NULL
,
3260 &output
.ofpact
, sizeof output
,
3262 ovs_rwlock_unlock(&xlate_rwlock
);