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-sflow.h"
46 #include "ofproto/ofproto-dpif.h"
50 COVERAGE_DEFINE(xlate_actions
);
52 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate
);
54 /* Maximum depth of flow table recursion (due to resubmit actions) in a
55 * flow translation. */
56 #define MAX_RESUBMIT_RECURSION 64
58 struct ovs_rwlock xlate_rwlock
= OVS_RWLOCK_INITIALIZER
;
61 struct hmap_node hmap_node
; /* Node in global 'xbridges' map. */
62 struct ofproto_dpif
*ofproto
; /* Key in global 'xbridges' map. */
64 struct list xbundles
; /* Owned xbundles. */
65 struct hmap xports
; /* Indexed by ofp_port. */
67 char *name
; /* Name used in log messages. */
68 struct dpif
*dpif
; /* Datapath interface. */
69 struct mac_learning
*ml
; /* Mac learning handle. */
70 struct mbridge
*mbridge
; /* Mirroring. */
71 struct dpif_sflow
*sflow
; /* SFlow handle, or null. */
72 struct dpif_ipfix
*ipfix
; /* Ipfix handle, or null. */
73 struct stp
*stp
; /* STP or null if disabled. */
75 /* Special rules installed by ofproto-dpif. */
76 struct rule_dpif
*miss_rule
;
77 struct rule_dpif
*no_packet_in_rule
;
79 enum ofp_config_flags frag
; /* Fragmentation handling. */
80 bool has_netflow
; /* Bridge runs netflow? */
81 bool has_in_band
; /* Bridge has in band control? */
82 bool forward_bpdu
; /* Bridge forwards STP BPDUs? */
86 struct hmap_node hmap_node
; /* In global 'xbundles' map. */
87 struct ofbundle
*ofbundle
; /* Key in global 'xbundles' map. */
89 struct list list_node
; /* In parent 'xbridges' list. */
90 struct xbridge
*xbridge
; /* Parent xbridge. */
92 struct list xports
; /* Contains "struct xport"s. */
94 char *name
; /* Name used in log messages. */
95 struct bond
*bond
; /* Nonnull iff more than one port. */
96 struct lacp
*lacp
; /* LACP handle or null. */
98 enum port_vlan_mode vlan_mode
; /* VLAN mode. */
99 int vlan
; /* -1=trunk port, else a 12-bit VLAN ID. */
100 unsigned long *trunks
; /* Bitmap of trunked VLANs, if 'vlan' == -1.
101 * NULL if all VLANs are trunked. */
102 bool use_priority_tags
; /* Use 802.1p tag for frames in VLAN 0? */
103 bool floodable
; /* No port has OFPUTIL_PC_NO_FLOOD set? */
107 struct hmap_node hmap_node
; /* Node in global 'xports' map. */
108 struct ofport_dpif
*ofport
; /* Key in global 'xports map. */
110 struct hmap_node ofp_node
; /* Node in parent xbridge 'xports' map. */
111 ofp_port_t ofp_port
; /* Key in parent xbridge 'xports' map. */
113 odp_port_t odp_port
; /* Datapath port number or ODPP_NONE. */
115 struct list bundle_node
; /* In parent xbundle (if it exists). */
116 struct xbundle
*xbundle
; /* Parent xbundle or null. */
118 struct netdev
*netdev
; /* 'ofport''s netdev. */
120 struct xbridge
*xbridge
; /* Parent bridge. */
121 struct xport
*peer
; /* Patch port peer or null. */
123 enum ofputil_port_config config
; /* OpenFlow port configuration. */
124 int stp_port_no
; /* STP port number or 0 if not in use. */
126 struct hmap skb_priorities
; /* Map of 'skb_priority_to_dscp's. */
128 bool may_enable
; /* May be enabled in bonds. */
129 bool is_tunnel
; /* Is a tunnel port. */
131 struct cfm
*cfm
; /* CFM handle or null. */
132 struct bfd
*bfd
; /* BFD handle or null. */
136 struct xlate_in
*xin
;
137 struct xlate_out
*xout
;
139 const struct xbridge
*xbridge
;
141 /* Flow at the last commit. */
142 struct flow base_flow
;
144 /* Tunnel IP destination address as received. This is stored separately
145 * as the base_flow.tunnel is cleared on init to reflect the datapath
146 * behavior. Used to make sure not to send tunneled output to ourselves,
147 * which might lead to an infinite loop. This could happen easily
148 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
149 * actually set the tun_dst field. */
150 ovs_be32 orig_tunnel_ip_dst
;
152 /* Stack for the push and pop actions. Each stack element is of type
153 * "union mf_subvalue". */
154 union mf_subvalue init_stack
[1024 / sizeof(union mf_subvalue
)];
157 /* The rule that we are currently translating, or NULL. */
158 struct rule_dpif
*rule
;
160 int recurse
; /* Recursion level, via xlate_table_action. */
161 uint32_t orig_skb_priority
; /* Priority when packet arrived. */
162 uint8_t table_id
; /* OpenFlow table ID where flow was found. */
163 uint32_t sflow_n_outputs
; /* Number of output ports. */
164 odp_port_t sflow_odp_port
; /* Output port for composing sFlow action. */
165 uint16_t user_cookie_offset
;/* Used for user_action_cookie fixup. */
166 bool exit
; /* No further actions should be processed. */
169 /* A controller may use OFPP_NONE as the ingress port to indicate that
170 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
171 * when an input bundle is needed for validation (e.g., mirroring or
172 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
173 * any 'port' structs, so care must be taken when dealing with it.
174 * The bundle's name and vlan mode are initialized in lookup_input_bundle() */
175 static struct xbundle ofpp_none_bundle
;
177 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
178 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
179 * traffic egressing the 'ofport' with that priority should be marked with. */
180 struct skb_priority_to_dscp
{
181 struct hmap_node hmap_node
; /* Node in 'ofport_dpif''s 'skb_priorities'. */
182 uint32_t skb_priority
; /* Priority of this queue (see struct flow). */
184 uint8_t dscp
; /* DSCP bits to mark outgoing traffic with. */
187 static struct hmap xbridges
= HMAP_INITIALIZER(&xbridges
);
188 static struct hmap xbundles
= HMAP_INITIALIZER(&xbundles
);
189 static struct hmap xports
= HMAP_INITIALIZER(&xports
);
191 static bool may_receive(const struct xport
*, struct xlate_ctx
*);
192 static void do_xlate_actions(const struct ofpact
*, size_t ofpacts_len
,
194 static void xlate_normal(struct xlate_ctx
*);
195 static void xlate_report(struct xlate_ctx
*, const char *);
196 static void xlate_table_action(struct xlate_ctx
*, ofp_port_t in_port
,
197 uint8_t table_id
, bool may_packet_in
);
198 static bool input_vid_is_valid(uint16_t vid
, struct xbundle
*, bool warn
);
199 static uint16_t input_vid_to_vlan(const struct xbundle
*, uint16_t vid
);
200 static void output_normal(struct xlate_ctx
*, const struct xbundle
*,
202 static void compose_output_action(struct xlate_ctx
*, ofp_port_t ofp_port
);
204 static struct xbridge
*xbridge_lookup(const struct ofproto_dpif
*);
205 static struct xbundle
*xbundle_lookup(const struct ofbundle
*);
206 static struct xport
*xport_lookup(const struct ofport_dpif
*);
207 static struct xport
*get_ofp_port(const struct xbridge
*, ofp_port_t ofp_port
);
208 static struct skb_priority_to_dscp
*get_skb_priority(const struct xport
*,
209 uint32_t skb_priority
);
210 static void clear_skb_priorities(struct xport
*);
211 static bool dscp_from_skb_priority(const struct xport
*, uint32_t skb_priority
,
215 xlate_ofproto_set(struct ofproto_dpif
*ofproto
, const char *name
,
216 struct dpif
*dpif
, struct rule_dpif
*miss_rule
,
217 struct rule_dpif
*no_packet_in_rule
,
218 const struct mac_learning
*ml
, struct stp
*stp
,
219 const struct mbridge
*mbridge
,
220 const struct dpif_sflow
*sflow
,
221 const struct dpif_ipfix
*ipfix
, enum ofp_config_flags frag
,
222 bool forward_bpdu
, bool has_in_band
, bool has_netflow
)
224 struct xbridge
*xbridge
= xbridge_lookup(ofproto
);
227 xbridge
= xzalloc(sizeof *xbridge
);
228 xbridge
->ofproto
= ofproto
;
230 hmap_insert(&xbridges
, &xbridge
->hmap_node
, hash_pointer(ofproto
, 0));
231 hmap_init(&xbridge
->xports
);
232 list_init(&xbridge
->xbundles
);
235 if (xbridge
->ml
!= ml
) {
236 mac_learning_unref(xbridge
->ml
);
237 xbridge
->ml
= mac_learning_ref(ml
);
240 if (xbridge
->mbridge
!= mbridge
) {
241 mbridge_unref(xbridge
->mbridge
);
242 xbridge
->mbridge
= mbridge_ref(mbridge
);
245 if (xbridge
->sflow
!= sflow
) {
246 dpif_sflow_unref(xbridge
->sflow
);
247 xbridge
->sflow
= dpif_sflow_ref(sflow
);
250 if (xbridge
->ipfix
!= ipfix
) {
251 dpif_ipfix_unref(xbridge
->ipfix
);
252 xbridge
->ipfix
= dpif_ipfix_ref(ipfix
);
255 if (xbridge
->stp
!= stp
) {
256 stp_unref(xbridge
->stp
);
257 xbridge
->stp
= stp_ref(stp
);
261 xbridge
->name
= xstrdup(name
);
263 xbridge
->dpif
= dpif
;
264 xbridge
->forward_bpdu
= forward_bpdu
;
265 xbridge
->has_in_band
= has_in_band
;
266 xbridge
->has_netflow
= has_netflow
;
267 xbridge
->frag
= frag
;
268 xbridge
->miss_rule
= miss_rule
;
269 xbridge
->no_packet_in_rule
= no_packet_in_rule
;
273 xlate_remove_ofproto(struct ofproto_dpif
*ofproto
)
275 struct xbridge
*xbridge
= xbridge_lookup(ofproto
);
276 struct xbundle
*xbundle
, *next_xbundle
;
277 struct xport
*xport
, *next_xport
;
283 HMAP_FOR_EACH_SAFE (xport
, next_xport
, ofp_node
, &xbridge
->xports
) {
284 xlate_ofport_remove(xport
->ofport
);
287 LIST_FOR_EACH_SAFE (xbundle
, next_xbundle
, list_node
, &xbridge
->xbundles
) {
288 xlate_bundle_remove(xbundle
->ofbundle
);
291 hmap_remove(&xbridges
, &xbridge
->hmap_node
);
292 mac_learning_unref(xbridge
->ml
);
293 mbridge_unref(xbridge
->mbridge
);
294 dpif_sflow_unref(xbridge
->sflow
);
295 dpif_ipfix_unref(xbridge
->ipfix
);
296 stp_unref(xbridge
->stp
);
297 hmap_destroy(&xbridge
->xports
);
303 xlate_bundle_set(struct ofproto_dpif
*ofproto
, struct ofbundle
*ofbundle
,
304 const char *name
, enum port_vlan_mode vlan_mode
, int vlan
,
305 unsigned long *trunks
, bool use_priority_tags
,
306 const struct bond
*bond
, const struct lacp
*lacp
,
309 struct xbundle
*xbundle
= xbundle_lookup(ofbundle
);
312 xbundle
= xzalloc(sizeof *xbundle
);
313 xbundle
->ofbundle
= ofbundle
;
314 xbundle
->xbridge
= xbridge_lookup(ofproto
);
316 hmap_insert(&xbundles
, &xbundle
->hmap_node
, hash_pointer(ofbundle
, 0));
317 list_insert(&xbundle
->xbridge
->xbundles
, &xbundle
->list_node
);
318 list_init(&xbundle
->xports
);
321 ovs_assert(xbundle
->xbridge
);
324 xbundle
->name
= xstrdup(name
);
326 xbundle
->vlan_mode
= vlan_mode
;
327 xbundle
->vlan
= vlan
;
328 xbundle
->trunks
= trunks
;
329 xbundle
->use_priority_tags
= use_priority_tags
;
330 xbundle
->floodable
= floodable
;
332 if (xbundle
->bond
!= bond
) {
333 bond_unref(xbundle
->bond
);
334 xbundle
->bond
= bond_ref(bond
);
337 if (xbundle
->lacp
!= lacp
) {
338 lacp_unref(xbundle
->lacp
);
339 xbundle
->lacp
= lacp_ref(lacp
);
344 xlate_bundle_remove(struct ofbundle
*ofbundle
)
346 struct xbundle
*xbundle
= xbundle_lookup(ofbundle
);
347 struct xport
*xport
, *next
;
353 LIST_FOR_EACH_SAFE (xport
, next
, bundle_node
, &xbundle
->xports
) {
354 list_remove(&xport
->bundle_node
);
355 xport
->xbundle
= NULL
;
358 hmap_remove(&xbundles
, &xbundle
->hmap_node
);
359 list_remove(&xbundle
->list_node
);
360 bond_unref(xbundle
->bond
);
361 lacp_unref(xbundle
->lacp
);
367 xlate_ofport_set(struct ofproto_dpif
*ofproto
, struct ofbundle
*ofbundle
,
368 struct ofport_dpif
*ofport
, ofp_port_t ofp_port
,
369 odp_port_t odp_port
, const struct netdev
*netdev
,
370 const struct cfm
*cfm
, const struct bfd
*bfd
,
371 struct ofport_dpif
*peer
, int stp_port_no
,
372 const struct ofproto_port_queue
*qdscp_list
, size_t n_qdscp
,
373 enum ofputil_port_config config
, bool is_tunnel
,
376 struct xport
*xport
= xport_lookup(ofport
);
380 xport
= xzalloc(sizeof *xport
);
381 xport
->ofport
= ofport
;
382 xport
->xbridge
= xbridge_lookup(ofproto
);
383 xport
->ofp_port
= ofp_port
;
385 hmap_init(&xport
->skb_priorities
);
386 hmap_insert(&xports
, &xport
->hmap_node
, hash_pointer(ofport
, 0));
387 hmap_insert(&xport
->xbridge
->xports
, &xport
->ofp_node
,
388 hash_ofp_port(xport
->ofp_port
));
391 ovs_assert(xport
->ofp_port
== ofp_port
);
393 xport
->config
= config
;
394 xport
->stp_port_no
= stp_port_no
;
395 xport
->is_tunnel
= is_tunnel
;
396 xport
->may_enable
= may_enable
;
397 xport
->odp_port
= odp_port
;
399 if (xport
->netdev
!= netdev
) {
400 netdev_close(xport
->netdev
);
401 xport
->netdev
= netdev_ref(netdev
);
404 if (xport
->cfm
!= cfm
) {
405 cfm_unref(xport
->cfm
);
406 xport
->cfm
= cfm_ref(cfm
);
409 if (xport
->bfd
!= bfd
) {
410 bfd_unref(xport
->bfd
);
411 xport
->bfd
= bfd_ref(bfd
);
415 xport
->peer
->peer
= NULL
;
417 xport
->peer
= xport_lookup(peer
);
419 xport
->peer
->peer
= xport
;
422 if (xport
->xbundle
) {
423 list_remove(&xport
->bundle_node
);
425 xport
->xbundle
= xbundle_lookup(ofbundle
);
426 if (xport
->xbundle
) {
427 list_insert(&xport
->xbundle
->xports
, &xport
->bundle_node
);
430 clear_skb_priorities(xport
);
431 for (i
= 0; i
< n_qdscp
; i
++) {
432 struct skb_priority_to_dscp
*pdscp
;
433 uint32_t skb_priority
;
435 if (dpif_queue_to_priority(xport
->xbridge
->dpif
, qdscp_list
[i
].queue
,
440 pdscp
= xmalloc(sizeof *pdscp
);
441 pdscp
->skb_priority
= skb_priority
;
442 pdscp
->dscp
= (qdscp_list
[i
].dscp
<< 2) & IP_DSCP_MASK
;
443 hmap_insert(&xport
->skb_priorities
, &pdscp
->hmap_node
,
444 hash_int(pdscp
->skb_priority
, 0));
449 xlate_ofport_remove(struct ofport_dpif
*ofport
)
451 struct xport
*xport
= xport_lookup(ofport
);
458 xport
->peer
->peer
= NULL
;
462 if (xport
->xbundle
) {
463 list_remove(&xport
->bundle_node
);
466 clear_skb_priorities(xport
);
467 hmap_destroy(&xport
->skb_priorities
);
469 hmap_remove(&xports
, &xport
->hmap_node
);
470 hmap_remove(&xport
->xbridge
->xports
, &xport
->ofp_node
);
472 netdev_close(xport
->netdev
);
473 cfm_unref(xport
->cfm
);
474 bfd_unref(xport
->bfd
);
478 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
479 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
480 * Optionally, if nonnull, populates 'fitnessp' with the fitness of 'flow' as
481 * returned by odp_flow_key_to_flow(). Also, optionally populates 'ofproto'
482 * with the ofproto_dpif, and 'odp_in_port' with the datapath in_port, that
483 * 'packet' ingressed.
485 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
486 * 'flow''s in_port to OFPP_NONE.
488 * This function does post-processing on data returned from
489 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
490 * of the upcall processing logic. In particular, if the extracted in_port is
491 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
492 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
493 * a VLAN header onto 'packet' (if it is nonnull).
495 * Similarly, this function also includes some logic to help with tunnels. It
496 * may modify 'flow' as necessary to make the tunneling implementation
497 * transparent to the upcall processing logic.
499 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
500 * or some other positive errno if there are other problems. */
502 xlate_receive(const struct dpif_backer
*backer
, struct ofpbuf
*packet
,
503 const struct nlattr
*key
, size_t key_len
,
504 struct flow
*flow
, enum odp_key_fitness
*fitnessp
,
505 struct ofproto_dpif
**ofproto
, odp_port_t
*odp_in_port
)
507 enum odp_key_fitness fitness
;
508 const struct xport
*xport
;
511 ovs_rwlock_rdlock(&xlate_rwlock
);
512 fitness
= odp_flow_key_to_flow(key
, key_len
, flow
);
513 if (fitness
== ODP_FIT_ERROR
) {
519 *odp_in_port
= flow
->in_port
.odp_port
;
522 xport
= xport_lookup(tnl_port_should_receive(flow
)
523 ? tnl_port_receive(flow
)
524 : odp_port_to_ofport(backer
, flow
->in_port
.odp_port
));
526 flow
->in_port
.ofp_port
= xport
? xport
->ofp_port
: OFPP_NONE
;
531 if (vsp_adjust_flow(xport
->xbridge
->ofproto
, flow
)) {
533 /* Make the packet resemble the flow, so that it gets sent to
534 * an OpenFlow controller properly, so that it looks correct
535 * for sFlow, and so that flow_extract() will get the correct
536 * vlan_tci if it is called on 'packet'.
538 * The allocated space inside 'packet' probably also contains
539 * 'key', that is, both 'packet' and 'key' are probably part of
540 * a struct dpif_upcall (see the large comment on that
541 * structure definition), so pushing data on 'packet' is in
542 * general not a good idea since it could overwrite 'key' or
543 * free it as a side effect. However, it's OK in this special
544 * case because we know that 'packet' is inside a Netlink
545 * attribute: pushing 4 bytes will just overwrite the 4-byte
546 * "struct nlattr", which is fine since we don't need that
548 eth_push_vlan(packet
, flow
->vlan_tci
);
550 /* We can't reproduce 'key' from 'flow'. */
551 fitness
= fitness
== ODP_FIT_PERFECT
? ODP_FIT_TOO_MUCH
: fitness
;
556 *ofproto
= xport
->xbridge
->ofproto
;
563 ovs_rwlock_unlock(&xlate_rwlock
);
567 static struct xbridge
*
568 xbridge_lookup(const struct ofproto_dpif
*ofproto
)
570 struct xbridge
*xbridge
;
576 HMAP_FOR_EACH_IN_BUCKET (xbridge
, hmap_node
, hash_pointer(ofproto
, 0),
578 if (xbridge
->ofproto
== ofproto
) {
585 static struct xbundle
*
586 xbundle_lookup(const struct ofbundle
*ofbundle
)
588 struct xbundle
*xbundle
;
594 HMAP_FOR_EACH_IN_BUCKET (xbundle
, hmap_node
, hash_pointer(ofbundle
, 0),
596 if (xbundle
->ofbundle
== ofbundle
) {
603 static struct xport
*
604 xport_lookup(const struct ofport_dpif
*ofport
)
612 HMAP_FOR_EACH_IN_BUCKET (xport
, hmap_node
, hash_pointer(ofport
, 0),
614 if (xport
->ofport
== ofport
) {
621 static struct stp_port
*
622 xport_get_stp_port(const struct xport
*xport
)
624 return xport
->xbridge
->stp
&& xport
->stp_port_no
625 ? stp_get_port(xport
->xbridge
->stp
, xport
->stp_port_no
)
629 static enum stp_state
630 xport_stp_learn_state(const struct xport
*xport
)
632 struct stp_port
*sp
= xport_get_stp_port(xport
);
633 return stp_learn_in_state(sp
? stp_port_get_state(sp
) : STP_DISABLED
);
637 xport_stp_forward_state(const struct xport
*xport
)
639 struct stp_port
*sp
= xport_get_stp_port(xport
);
640 return stp_forward_in_state(sp
? stp_port_get_state(sp
) : STP_DISABLED
);
643 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
644 * were used to make the determination.*/
646 stp_should_process_flow(const struct flow
*flow
, struct flow_wildcards
*wc
)
648 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
649 return eth_addr_equals(flow
->dl_dst
, eth_addr_stp
);
653 stp_process_packet(const struct xport
*xport
, const struct ofpbuf
*packet
)
655 struct stp_port
*sp
= xport_get_stp_port(xport
);
656 struct ofpbuf payload
= *packet
;
657 struct eth_header
*eth
= payload
.data
;
659 /* Sink packets on ports that have STP disabled when the bridge has
661 if (!sp
|| stp_port_get_state(sp
) == STP_DISABLED
) {
665 /* Trim off padding on payload. */
666 if (payload
.size
> ntohs(eth
->eth_type
) + ETH_HEADER_LEN
) {
667 payload
.size
= ntohs(eth
->eth_type
) + ETH_HEADER_LEN
;
670 if (ofpbuf_try_pull(&payload
, ETH_HEADER_LEN
+ LLC_HEADER_LEN
)) {
671 stp_received_bpdu(sp
, payload
.data
, payload
.size
);
675 static struct xport
*
676 get_ofp_port(const struct xbridge
*xbridge
, ofp_port_t ofp_port
)
680 HMAP_FOR_EACH_IN_BUCKET (xport
, ofp_node
, hash_ofp_port(ofp_port
),
682 if (xport
->ofp_port
== ofp_port
) {
690 ofp_port_to_odp_port(const struct xbridge
*xbridge
, ofp_port_t ofp_port
)
692 const struct xport
*xport
= get_ofp_port(xbridge
, ofp_port
);
693 return xport
? xport
->odp_port
: ODPP_NONE
;
697 xbundle_trunks_vlan(const struct xbundle
*bundle
, uint16_t vlan
)
699 return (bundle
->vlan_mode
!= PORT_VLAN_ACCESS
700 && (!bundle
->trunks
|| bitmap_is_set(bundle
->trunks
, vlan
)));
704 xbundle_includes_vlan(const struct xbundle
*xbundle
, uint16_t vlan
)
706 return vlan
== xbundle
->vlan
|| xbundle_trunks_vlan(xbundle
, vlan
);
710 xbundle_mirror_out(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
712 return xbundle
!= &ofpp_none_bundle
713 ? mirror_bundle_out(xbridge
->mbridge
, xbundle
->ofbundle
)
718 xbundle_mirror_src(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
720 return xbundle
!= &ofpp_none_bundle
721 ? mirror_bundle_src(xbridge
->mbridge
, xbundle
->ofbundle
)
726 xbundle_mirror_dst(const struct xbridge
*xbridge
, struct xbundle
*xbundle
)
728 return xbundle
!= &ofpp_none_bundle
729 ? mirror_bundle_dst(xbridge
->mbridge
, xbundle
->ofbundle
)
733 static struct xbundle
*
734 lookup_input_bundle(const struct xbridge
*xbridge
, ofp_port_t in_port
,
735 bool warn
, struct xport
**in_xportp
)
739 /* Find the port and bundle for the received packet. */
740 xport
= get_ofp_port(xbridge
, in_port
);
744 if (xport
&& xport
->xbundle
) {
745 return xport
->xbundle
;
748 /* Special-case OFPP_NONE, which a controller may use as the ingress
749 * port for traffic that it is sourcing. */
750 if (in_port
== OFPP_NONE
) {
751 ofpp_none_bundle
.name
= "OFPP_NONE";
752 ofpp_none_bundle
.vlan_mode
= PORT_VLAN_TRUNK
;
753 return &ofpp_none_bundle
;
756 /* Odd. A few possible reasons here:
758 * - We deleted a port but there are still a few packets queued up
761 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
762 * we don't know about.
764 * - The ofproto client didn't configure the port as part of a bundle.
765 * This is particularly likely to happen if a packet was received on the
766 * port after it was created, but before the client had a chance to
767 * configure its bundle.
770 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
772 VLOG_WARN_RL(&rl
, "bridge %s: received packet on unknown "
773 "port %"PRIu16
, xbridge
->name
, in_port
);
779 add_mirror_actions(struct xlate_ctx
*ctx
, const struct flow
*orig_flow
)
781 const struct xbridge
*xbridge
= ctx
->xbridge
;
782 mirror_mask_t mirrors
;
783 struct xbundle
*in_xbundle
;
787 mirrors
= ctx
->xout
->mirrors
;
788 ctx
->xout
->mirrors
= 0;
790 in_xbundle
= lookup_input_bundle(xbridge
, orig_flow
->in_port
.ofp_port
,
791 ctx
->xin
->packet
!= NULL
, NULL
);
795 mirrors
|= xbundle_mirror_src(xbridge
, in_xbundle
);
797 /* Drop frames on bundles reserved for mirroring. */
798 if (xbundle_mirror_out(xbridge
, in_xbundle
)) {
799 if (ctx
->xin
->packet
!= NULL
) {
800 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
801 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet received on port "
802 "%s, which is reserved exclusively for mirroring",
803 ctx
->xbridge
->name
, in_xbundle
->name
);
805 ofpbuf_clear(&ctx
->xout
->odp_actions
);
810 vid
= vlan_tci_to_vid(orig_flow
->vlan_tci
);
811 if (!input_vid_is_valid(vid
, in_xbundle
, ctx
->xin
->packet
!= NULL
)) {
814 vlan
= input_vid_to_vlan(in_xbundle
, vid
);
820 /* Restore the original packet before adding the mirror actions. */
821 ctx
->xin
->flow
= *orig_flow
;
824 mirror_mask_t dup_mirrors
;
825 struct ofbundle
*out
;
826 unsigned long *vlans
;
831 has_mirror
= mirror_get(xbridge
->mbridge
, mirror_mask_ffs(mirrors
) - 1,
832 &vlans
, &dup_mirrors
, &out
, &out_vlan
);
833 ovs_assert(has_mirror
);
836 ctx
->xout
->wc
.masks
.vlan_tci
|= htons(VLAN_CFI
| VLAN_VID_MASK
);
838 vlan_mirrored
= !vlans
|| bitmap_is_set(vlans
, vlan
);
841 if (!vlan_mirrored
) {
842 mirrors
= zero_rightmost_1bit(mirrors
);
846 mirrors
&= ~dup_mirrors
;
847 ctx
->xout
->mirrors
|= dup_mirrors
;
849 struct xbundle
*out_xbundle
= xbundle_lookup(out
);
851 output_normal(ctx
, out_xbundle
, vlan
);
853 } else if (vlan
!= out_vlan
854 && !eth_addr_is_reserved(orig_flow
->dl_dst
)) {
855 struct xbundle
*xbundle
;
857 LIST_FOR_EACH (xbundle
, list_node
, &xbridge
->xbundles
) {
858 if (xbundle_includes_vlan(xbundle
, out_vlan
)
859 && !xbundle_mirror_out(xbridge
, xbundle
)) {
860 output_normal(ctx
, xbundle
, out_vlan
);
867 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
868 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
869 * the bundle on which the packet was received, returns the VLAN to which the
872 * Both 'vid' and the return value are in the range 0...4095. */
874 input_vid_to_vlan(const struct xbundle
*in_xbundle
, uint16_t vid
)
876 switch (in_xbundle
->vlan_mode
) {
877 case PORT_VLAN_ACCESS
:
878 return in_xbundle
->vlan
;
881 case PORT_VLAN_TRUNK
:
884 case PORT_VLAN_NATIVE_UNTAGGED
:
885 case PORT_VLAN_NATIVE_TAGGED
:
886 return vid
? vid
: in_xbundle
->vlan
;
893 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
894 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
897 * 'vid' should be the VID obtained from the 802.1Q header that was received as
898 * part of a packet (specify 0 if there was no 802.1Q header), in the range
901 input_vid_is_valid(uint16_t vid
, struct xbundle
*in_xbundle
, bool warn
)
903 /* Allow any VID on the OFPP_NONE port. */
904 if (in_xbundle
== &ofpp_none_bundle
) {
908 switch (in_xbundle
->vlan_mode
) {
909 case PORT_VLAN_ACCESS
:
912 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
913 VLOG_WARN_RL(&rl
, "dropping VLAN %"PRIu16
" tagged "
914 "packet received on port %s configured as VLAN "
915 "%"PRIu16
" access port", vid
, in_xbundle
->name
,
922 case PORT_VLAN_NATIVE_UNTAGGED
:
923 case PORT_VLAN_NATIVE_TAGGED
:
925 /* Port must always carry its native VLAN. */
929 case PORT_VLAN_TRUNK
:
930 if (!xbundle_includes_vlan(in_xbundle
, vid
)) {
932 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
933 VLOG_WARN_RL(&rl
, "dropping VLAN %"PRIu16
" packet "
934 "received on port %s not configured for trunking "
935 "VLAN %"PRIu16
, vid
, in_xbundle
->name
, vid
);
947 /* Given 'vlan', the VLAN that a packet belongs to, and
948 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
949 * that should be included in the 802.1Q header. (If the return value is 0,
950 * then the 802.1Q header should only be included in the packet if there is a
953 * Both 'vlan' and the return value are in the range 0...4095. */
955 output_vlan_to_vid(const struct xbundle
*out_xbundle
, uint16_t vlan
)
957 switch (out_xbundle
->vlan_mode
) {
958 case PORT_VLAN_ACCESS
:
961 case PORT_VLAN_TRUNK
:
962 case PORT_VLAN_NATIVE_TAGGED
:
965 case PORT_VLAN_NATIVE_UNTAGGED
:
966 return vlan
== out_xbundle
->vlan
? 0 : vlan
;
974 output_normal(struct xlate_ctx
*ctx
, const struct xbundle
*out_xbundle
,
977 ovs_be16
*flow_tci
= &ctx
->xin
->flow
.vlan_tci
;
979 ovs_be16 tci
, old_tci
;
982 vid
= output_vlan_to_vid(out_xbundle
, vlan
);
983 if (list_is_empty(&out_xbundle
->xports
)) {
984 /* Partially configured bundle with no slaves. Drop the packet. */
986 } else if (!out_xbundle
->bond
) {
987 xport
= CONTAINER_OF(list_front(&out_xbundle
->xports
), struct xport
,
990 struct ofport_dpif
*ofport
;
992 ofport
= bond_choose_output_slave(out_xbundle
->bond
, &ctx
->xin
->flow
,
993 &ctx
->xout
->wc
, vid
);
994 xport
= xport_lookup(ofport
);
997 /* No slaves enabled, so drop packet. */
1002 old_tci
= *flow_tci
;
1004 if (tci
|| out_xbundle
->use_priority_tags
) {
1005 tci
|= *flow_tci
& htons(VLAN_PCP_MASK
);
1007 tci
|= htons(VLAN_CFI
);
1012 compose_output_action(ctx
, xport
->ofp_port
);
1013 *flow_tci
= old_tci
;
1016 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1017 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1018 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1020 is_gratuitous_arp(const struct flow
*flow
, struct flow_wildcards
*wc
)
1022 if (flow
->dl_type
!= htons(ETH_TYPE_ARP
)) {
1026 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
1027 if (!eth_addr_is_broadcast(flow
->dl_dst
)) {
1031 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
1032 if (flow
->nw_proto
== ARP_OP_REPLY
) {
1034 } else if (flow
->nw_proto
== ARP_OP_REQUEST
) {
1035 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
1036 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
1038 return flow
->nw_src
== flow
->nw_dst
;
1044 /* Checks whether a MAC learning update is necessary for MAC learning table
1045 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1048 * Most packets processed through the MAC learning table do not actually
1049 * change it in any way. This function requires only a read lock on the MAC
1050 * learning table, so it is much cheaper in this common case.
1052 * Keep the code here synchronized with that in update_learning_table__()
1055 is_mac_learning_update_needed(const struct mac_learning
*ml
,
1056 const struct flow
*flow
,
1057 struct flow_wildcards
*wc
,
1058 int vlan
, struct xbundle
*in_xbundle
)
1059 OVS_REQ_RDLOCK(ml
->rwlock
)
1061 struct mac_entry
*mac
;
1063 if (!mac_learning_may_learn(ml
, flow
->dl_src
, vlan
)) {
1067 mac
= mac_learning_lookup(ml
, flow
->dl_src
, vlan
);
1068 if (!mac
|| mac_entry_age(ml
, mac
)) {
1072 if (is_gratuitous_arp(flow
, wc
)) {
1073 /* We don't want to learn from gratuitous ARP packets that are
1074 * reflected back over bond slaves so we lock the learning table. */
1075 if (!in_xbundle
->bond
) {
1077 } else if (mac_entry_is_grat_arp_locked(mac
)) {
1082 return mac
->port
.p
!= in_xbundle
->ofbundle
;
1086 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1087 * received on 'in_xbundle' in 'vlan'.
1089 * This code repeats all the checks in is_mac_learning_update_needed() because
1090 * the lock was released between there and here and thus the MAC learning state
1091 * could have changed.
1093 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1096 update_learning_table__(const struct xbridge
*xbridge
,
1097 const struct flow
*flow
, struct flow_wildcards
*wc
,
1098 int vlan
, struct xbundle
*in_xbundle
)
1099 OVS_REQ_WRLOCK(xbridge
->ml
->rwlock
)
1101 struct mac_entry
*mac
;
1103 if (!mac_learning_may_learn(xbridge
->ml
, flow
->dl_src
, vlan
)) {
1107 mac
= mac_learning_insert(xbridge
->ml
, flow
->dl_src
, vlan
);
1108 if (is_gratuitous_arp(flow
, wc
)) {
1109 /* We don't want to learn from gratuitous ARP packets that are
1110 * reflected back over bond slaves so we lock the learning table. */
1111 if (!in_xbundle
->bond
) {
1112 mac_entry_set_grat_arp_lock(mac
);
1113 } else if (mac_entry_is_grat_arp_locked(mac
)) {
1118 if (mac
->port
.p
!= in_xbundle
->ofbundle
) {
1119 /* The log messages here could actually be useful in debugging,
1120 * so keep the rate limit relatively high. */
1121 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(30, 300);
1123 VLOG_DBG_RL(&rl
, "bridge %s: learned that "ETH_ADDR_FMT
" is "
1124 "on port %s in VLAN %d",
1125 xbridge
->name
, ETH_ADDR_ARGS(flow
->dl_src
),
1126 in_xbundle
->name
, vlan
);
1128 mac
->port
.p
= in_xbundle
->ofbundle
;
1129 mac_learning_changed(xbridge
->ml
);
1134 update_learning_table(const struct xbridge
*xbridge
,
1135 const struct flow
*flow
, struct flow_wildcards
*wc
,
1136 int vlan
, struct xbundle
*in_xbundle
)
1140 /* Don't learn the OFPP_NONE port. */
1141 if (in_xbundle
== &ofpp_none_bundle
) {
1145 /* First try the common case: no change to MAC learning table. */
1146 ovs_rwlock_rdlock(&xbridge
->ml
->rwlock
);
1147 need_update
= is_mac_learning_update_needed(xbridge
->ml
, flow
, wc
, vlan
,
1149 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
1152 /* Slow path: MAC learning table might need an update. */
1153 ovs_rwlock_wrlock(&xbridge
->ml
->rwlock
);
1154 update_learning_table__(xbridge
, flow
, wc
, vlan
, in_xbundle
);
1155 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
1159 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1160 * dropped. Returns true if they may be forwarded, false if they should be
1163 * 'in_port' must be the xport that corresponds to flow->in_port.
1164 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1166 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1167 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1168 * checked by input_vid_is_valid().
1170 * May also add tags to '*tags', although the current implementation only does
1171 * so in one special case.
1174 is_admissible(struct xlate_ctx
*ctx
, struct xport
*in_port
,
1177 struct xbundle
*in_xbundle
= in_port
->xbundle
;
1178 const struct xbridge
*xbridge
= ctx
->xbridge
;
1179 struct flow
*flow
= &ctx
->xin
->flow
;
1181 /* Drop frames for reserved multicast addresses
1182 * only if forward_bpdu option is absent. */
1183 if (!xbridge
->forward_bpdu
&& eth_addr_is_reserved(flow
->dl_dst
)) {
1184 xlate_report(ctx
, "packet has reserved destination MAC, dropping");
1188 if (in_xbundle
->bond
) {
1189 struct mac_entry
*mac
;
1191 switch (bond_check_admissibility(in_xbundle
->bond
, in_port
->ofport
,
1197 xlate_report(ctx
, "bonding refused admissibility, dropping");
1200 case BV_DROP_IF_MOVED
:
1201 ovs_rwlock_rdlock(&xbridge
->ml
->rwlock
);
1202 mac
= mac_learning_lookup(xbridge
->ml
, flow
->dl_src
, vlan
);
1203 if (mac
&& mac
->port
.p
!= in_xbundle
->ofbundle
&&
1204 (!is_gratuitous_arp(flow
, &ctx
->xout
->wc
)
1205 || mac_entry_is_grat_arp_locked(mac
))) {
1206 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
1207 xlate_report(ctx
, "SLB bond thinks this packet looped back, "
1211 ovs_rwlock_unlock(&xbridge
->ml
->rwlock
);
1220 xlate_normal(struct xlate_ctx
*ctx
)
1222 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
1223 struct flow
*flow
= &ctx
->xin
->flow
;
1224 struct xbundle
*in_xbundle
;
1225 struct xport
*in_port
;
1226 struct mac_entry
*mac
;
1231 ctx
->xout
->has_normal
= true;
1233 memset(&wc
->masks
.dl_src
, 0xff, sizeof wc
->masks
.dl_src
);
1234 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
1235 wc
->masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
1237 in_xbundle
= lookup_input_bundle(ctx
->xbridge
, flow
->in_port
.ofp_port
,
1238 ctx
->xin
->packet
!= NULL
, &in_port
);
1240 xlate_report(ctx
, "no input bundle, dropping");
1244 /* Drop malformed frames. */
1245 if (flow
->dl_type
== htons(ETH_TYPE_VLAN
) &&
1246 !(flow
->vlan_tci
& htons(VLAN_CFI
))) {
1247 if (ctx
->xin
->packet
!= NULL
) {
1248 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1249 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet with partial "
1250 "VLAN tag received on port %s",
1251 ctx
->xbridge
->name
, in_xbundle
->name
);
1253 xlate_report(ctx
, "partial VLAN tag, dropping");
1257 /* Drop frames on bundles reserved for mirroring. */
1258 if (xbundle_mirror_out(ctx
->xbridge
, in_xbundle
)) {
1259 if (ctx
->xin
->packet
!= NULL
) {
1260 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1261 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet received on port "
1262 "%s, which is reserved exclusively for mirroring",
1263 ctx
->xbridge
->name
, in_xbundle
->name
);
1265 xlate_report(ctx
, "input port is mirror output port, dropping");
1270 vid
= vlan_tci_to_vid(flow
->vlan_tci
);
1271 if (!input_vid_is_valid(vid
, in_xbundle
, ctx
->xin
->packet
!= NULL
)) {
1272 xlate_report(ctx
, "disallowed VLAN VID for this input port, dropping");
1275 vlan
= input_vid_to_vlan(in_xbundle
, vid
);
1277 /* Check other admissibility requirements. */
1278 if (in_port
&& !is_admissible(ctx
, in_port
, vlan
)) {
1282 /* Learn source MAC. */
1283 if (ctx
->xin
->may_learn
) {
1284 update_learning_table(ctx
->xbridge
, flow
, wc
, vlan
, in_xbundle
);
1287 /* Determine output bundle. */
1288 ovs_rwlock_rdlock(&ctx
->xbridge
->ml
->rwlock
);
1289 mac
= mac_learning_lookup(ctx
->xbridge
->ml
, flow
->dl_dst
, vlan
);
1290 mac_port
= mac
? mac
->port
.p
: NULL
;
1291 ovs_rwlock_unlock(&ctx
->xbridge
->ml
->rwlock
);
1294 struct xbundle
*mac_xbundle
= xbundle_lookup(mac_port
);
1295 if (mac_xbundle
&& mac_xbundle
!= in_xbundle
) {
1296 xlate_report(ctx
, "forwarding to learned port");
1297 output_normal(ctx
, mac_xbundle
, vlan
);
1298 } else if (!mac_xbundle
) {
1299 xlate_report(ctx
, "learned port is unknown, dropping");
1301 xlate_report(ctx
, "learned port is input port, dropping");
1304 struct xbundle
*xbundle
;
1306 xlate_report(ctx
, "no learned MAC for destination, flooding");
1307 LIST_FOR_EACH (xbundle
, list_node
, &ctx
->xbridge
->xbundles
) {
1308 if (xbundle
!= in_xbundle
1309 && xbundle_includes_vlan(xbundle
, vlan
)
1310 && xbundle
->floodable
1311 && !xbundle_mirror_out(ctx
->xbridge
, xbundle
)) {
1312 output_normal(ctx
, xbundle
, vlan
);
1315 ctx
->xout
->nf_output_iface
= NF_OUT_FLOOD
;
1319 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1320 * the number of packets out of UINT32_MAX to sample. The given
1321 * cookie is passed back in the callback for each sampled packet.
1324 compose_sample_action(const struct xbridge
*xbridge
,
1325 struct ofpbuf
*odp_actions
,
1326 const struct flow
*flow
,
1327 const uint32_t probability
,
1328 const union user_action_cookie
*cookie
,
1329 const size_t cookie_size
)
1331 size_t sample_offset
, actions_offset
;
1332 odp_port_t odp_port
;
1336 sample_offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SAMPLE
);
1338 nl_msg_put_u32(odp_actions
, OVS_SAMPLE_ATTR_PROBABILITY
, probability
);
1340 actions_offset
= nl_msg_start_nested(odp_actions
, OVS_SAMPLE_ATTR_ACTIONS
);
1342 odp_port
= ofp_port_to_odp_port(xbridge
, flow
->in_port
.ofp_port
);
1343 pid
= dpif_port_get_pid(xbridge
->dpif
, odp_port
);
1344 cookie_offset
= odp_put_userspace_action(pid
, cookie
, cookie_size
, odp_actions
);
1346 nl_msg_end_nested(odp_actions
, actions_offset
);
1347 nl_msg_end_nested(odp_actions
, sample_offset
);
1348 return cookie_offset
;
1352 compose_sflow_cookie(const struct xbridge
*xbridge
, ovs_be16 vlan_tci
,
1353 odp_port_t odp_port
, unsigned int n_outputs
,
1354 union user_action_cookie
*cookie
)
1358 cookie
->type
= USER_ACTION_COOKIE_SFLOW
;
1359 cookie
->sflow
.vlan_tci
= vlan_tci
;
1361 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1362 * port information") for the interpretation of cookie->output. */
1363 switch (n_outputs
) {
1365 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1366 cookie
->sflow
.output
= 0x40000000 | 256;
1370 ifindex
= dpif_sflow_odp_port_to_ifindex(xbridge
->sflow
, odp_port
);
1372 cookie
->sflow
.output
= ifindex
;
1377 /* 0x80000000 means "multiple output ports. */
1378 cookie
->sflow
.output
= 0x80000000 | n_outputs
;
1383 /* Compose SAMPLE action for sFlow bridge sampling. */
1385 compose_sflow_action(const struct xbridge
*xbridge
,
1386 struct ofpbuf
*odp_actions
,
1387 const struct flow
*flow
,
1388 odp_port_t odp_port
)
1390 uint32_t probability
;
1391 union user_action_cookie cookie
;
1393 if (!xbridge
->sflow
|| flow
->in_port
.ofp_port
== OFPP_NONE
) {
1397 probability
= dpif_sflow_get_probability(xbridge
->sflow
);
1398 compose_sflow_cookie(xbridge
, htons(0), odp_port
,
1399 odp_port
== ODPP_NONE
? 0 : 1, &cookie
);
1401 return compose_sample_action(xbridge
, odp_actions
, flow
, probability
,
1402 &cookie
, sizeof cookie
.sflow
);
1406 compose_flow_sample_cookie(uint16_t probability
, uint32_t collector_set_id
,
1407 uint32_t obs_domain_id
, uint32_t obs_point_id
,
1408 union user_action_cookie
*cookie
)
1410 cookie
->type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
1411 cookie
->flow_sample
.probability
= probability
;
1412 cookie
->flow_sample
.collector_set_id
= collector_set_id
;
1413 cookie
->flow_sample
.obs_domain_id
= obs_domain_id
;
1414 cookie
->flow_sample
.obs_point_id
= obs_point_id
;
1418 compose_ipfix_cookie(union user_action_cookie
*cookie
)
1420 cookie
->type
= USER_ACTION_COOKIE_IPFIX
;
1423 /* Compose SAMPLE action for IPFIX bridge sampling. */
1425 compose_ipfix_action(const struct xbridge
*xbridge
,
1426 struct ofpbuf
*odp_actions
,
1427 const struct flow
*flow
)
1429 uint32_t probability
;
1430 union user_action_cookie cookie
;
1432 if (!xbridge
->ipfix
|| flow
->in_port
.ofp_port
== OFPP_NONE
) {
1436 probability
= dpif_ipfix_get_bridge_exporter_probability(xbridge
->ipfix
);
1437 compose_ipfix_cookie(&cookie
);
1439 compose_sample_action(xbridge
, odp_actions
, flow
, probability
,
1440 &cookie
, sizeof cookie
.ipfix
);
1443 /* SAMPLE action for sFlow must be first action in any given list of
1444 * actions. At this point we do not have all information required to
1445 * build it. So try to build sample action as complete as possible. */
1447 add_sflow_action(struct xlate_ctx
*ctx
)
1449 ctx
->user_cookie_offset
= compose_sflow_action(ctx
->xbridge
,
1450 &ctx
->xout
->odp_actions
,
1451 &ctx
->xin
->flow
, ODPP_NONE
);
1452 ctx
->sflow_odp_port
= 0;
1453 ctx
->sflow_n_outputs
= 0;
1456 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
1457 * of actions, eventually after the SAMPLE action for sFlow. */
1459 add_ipfix_action(struct xlate_ctx
*ctx
)
1461 compose_ipfix_action(ctx
->xbridge
, &ctx
->xout
->odp_actions
,
1465 /* Fix SAMPLE action according to data collected while composing ODP actions.
1466 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
1467 * USERSPACE action's user-cookie which is required for sflow. */
1469 fix_sflow_action(struct xlate_ctx
*ctx
)
1471 const struct flow
*base
= &ctx
->base_flow
;
1472 union user_action_cookie
*cookie
;
1474 if (!ctx
->user_cookie_offset
) {
1478 cookie
= ofpbuf_at(&ctx
->xout
->odp_actions
, ctx
->user_cookie_offset
,
1479 sizeof cookie
->sflow
);
1480 ovs_assert(cookie
->type
== USER_ACTION_COOKIE_SFLOW
);
1482 compose_sflow_cookie(ctx
->xbridge
, base
->vlan_tci
,
1483 ctx
->sflow_odp_port
, ctx
->sflow_n_outputs
, cookie
);
1486 static enum slow_path_reason
1487 process_special(struct xlate_ctx
*ctx
, const struct flow
*flow
,
1488 const struct xport
*xport
, const struct ofpbuf
*packet
)
1490 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
1491 const struct xbridge
*xbridge
= ctx
->xbridge
;
1495 } else if (xport
->cfm
&& cfm_should_process_flow(xport
->cfm
, flow
, wc
)) {
1497 cfm_process_heartbeat(xport
->cfm
, packet
);
1500 } else if (xport
->bfd
&& bfd_should_process_flow(xport
->bfd
, flow
, wc
)) {
1502 bfd_process_packet(xport
->bfd
, flow
, packet
);
1505 } else if (xport
->xbundle
&& xport
->xbundle
->lacp
1506 && flow
->dl_type
== htons(ETH_TYPE_LACP
)) {
1508 lacp_process_packet(xport
->xbundle
->lacp
, xport
->ofport
, packet
);
1511 } else if (xbridge
->stp
&& stp_should_process_flow(flow
, wc
)) {
1513 stp_process_packet(xport
, packet
);
1522 compose_output_action__(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
,
1525 const struct xport
*xport
= get_ofp_port(ctx
->xbridge
, ofp_port
);
1526 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
1527 struct flow
*flow
= &ctx
->xin
->flow
;
1528 ovs_be16 flow_vlan_tci
;
1529 uint32_t flow_pkt_mark
;
1530 uint8_t flow_nw_tos
;
1531 odp_port_t out_port
, odp_port
;
1534 /* If 'struct flow' gets additional metadata, we'll need to zero it out
1535 * before traversing a patch port. */
1536 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 20);
1539 xlate_report(ctx
, "Nonexistent output port");
1541 } else if (xport
->config
& OFPUTIL_PC_NO_FWD
) {
1542 xlate_report(ctx
, "OFPPC_NO_FWD set, skipping output");
1544 } else if (check_stp
&& !xport_stp_forward_state(xport
)) {
1545 xlate_report(ctx
, "STP not in forwarding state, skipping output");
1549 if (mbridge_has_mirrors(ctx
->xbridge
->mbridge
) && xport
->xbundle
) {
1550 ctx
->xout
->mirrors
|= xbundle_mirror_dst(xport
->xbundle
->xbridge
,
1555 const struct xport
*peer
= xport
->peer
;
1556 struct flow old_flow
= ctx
->xin
->flow
;
1557 enum slow_path_reason special
;
1559 ctx
->xbridge
= peer
->xbridge
;
1560 flow
->in_port
.ofp_port
= peer
->ofp_port
;
1561 flow
->metadata
= htonll(0);
1562 memset(&flow
->tunnel
, 0, sizeof flow
->tunnel
);
1563 memset(flow
->regs
, 0, sizeof flow
->regs
);
1565 special
= process_special(ctx
, &ctx
->xin
->flow
, peer
,
1568 ctx
->xout
->slow
= special
;
1569 } else if (may_receive(peer
, ctx
)) {
1570 if (xport_stp_forward_state(peer
)) {
1571 xlate_table_action(ctx
, flow
->in_port
.ofp_port
, 0, true);
1573 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
1574 * learning action look at the packet, then drop it. */
1575 struct flow old_base_flow
= ctx
->base_flow
;
1576 size_t old_size
= ctx
->xout
->odp_actions
.size
;
1577 mirror_mask_t old_mirrors
= ctx
->xout
->mirrors
;
1578 xlate_table_action(ctx
, flow
->in_port
.ofp_port
, 0, true);
1579 ctx
->xout
->mirrors
= old_mirrors
;
1580 ctx
->base_flow
= old_base_flow
;
1581 ctx
->xout
->odp_actions
.size
= old_size
;
1585 ctx
->xin
->flow
= old_flow
;
1586 ctx
->xbridge
= xport
->xbridge
;
1588 if (ctx
->xin
->resubmit_stats
) {
1589 netdev_vport_inc_tx(xport
->netdev
, ctx
->xin
->resubmit_stats
);
1590 netdev_vport_inc_rx(peer
->netdev
, ctx
->xin
->resubmit_stats
);
1596 flow_vlan_tci
= flow
->vlan_tci
;
1597 flow_pkt_mark
= flow
->pkt_mark
;
1598 flow_nw_tos
= flow
->nw_tos
;
1600 if (dscp_from_skb_priority(xport
, flow
->skb_priority
, &dscp
)) {
1601 wc
->masks
.nw_tos
|= IP_ECN_MASK
;
1602 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1603 flow
->nw_tos
|= dscp
;
1606 if (xport
->is_tunnel
) {
1607 /* Save tunnel metadata so that changes made due to
1608 * the Logical (tunnel) Port are not visible for any further
1609 * matches, while explicit set actions on tunnel metadata are.
1611 struct flow_tnl flow_tnl
= flow
->tunnel
;
1612 odp_port
= tnl_port_send(xport
->ofport
, flow
, &ctx
->xout
->wc
);
1613 if (odp_port
== ODPP_NONE
) {
1614 xlate_report(ctx
, "Tunneling decided against output");
1615 goto out
; /* restore flow_nw_tos */
1617 if (flow
->tunnel
.ip_dst
== ctx
->orig_tunnel_ip_dst
) {
1618 xlate_report(ctx
, "Not tunneling to our own address");
1619 goto out
; /* restore flow_nw_tos */
1621 if (ctx
->xin
->resubmit_stats
) {
1622 netdev_vport_inc_tx(xport
->netdev
, ctx
->xin
->resubmit_stats
);
1624 out_port
= odp_port
;
1625 commit_odp_tunnel_action(flow
, &ctx
->base_flow
,
1626 &ctx
->xout
->odp_actions
);
1627 flow
->tunnel
= flow_tnl
; /* Restore tunnel metadata */
1629 ofp_port_t vlandev_port
;
1631 odp_port
= xport
->odp_port
;
1632 if (ofproto_has_vlan_splinters(ctx
->xbridge
->ofproto
)) {
1633 wc
->masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
1635 vlandev_port
= vsp_realdev_to_vlandev(ctx
->xbridge
->ofproto
, ofp_port
,
1637 if (vlandev_port
== ofp_port
) {
1638 out_port
= odp_port
;
1640 out_port
= ofp_port_to_odp_port(ctx
->xbridge
, vlandev_port
);
1641 flow
->vlan_tci
= htons(0);
1645 if (out_port
!= ODPP_NONE
) {
1646 commit_odp_actions(flow
, &ctx
->base_flow
,
1647 &ctx
->xout
->odp_actions
, &ctx
->xout
->wc
);
1648 nl_msg_put_odp_port(&ctx
->xout
->odp_actions
, OVS_ACTION_ATTR_OUTPUT
,
1651 ctx
->sflow_odp_port
= odp_port
;
1652 ctx
->sflow_n_outputs
++;
1653 ctx
->xout
->nf_output_iface
= ofp_port
;
1658 flow
->vlan_tci
= flow_vlan_tci
;
1659 flow
->pkt_mark
= flow_pkt_mark
;
1660 flow
->nw_tos
= flow_nw_tos
;
1664 compose_output_action(struct xlate_ctx
*ctx
, ofp_port_t ofp_port
)
1666 compose_output_action__(ctx
, ofp_port
, true);
1670 xlate_recursively(struct xlate_ctx
*ctx
, struct rule_dpif
*rule
)
1671 OVS_RELEASES(rule
->up
.evict
)
1673 struct rule_dpif
*old_rule
= ctx
->rule
;
1675 if (ctx
->xin
->resubmit_stats
) {
1676 rule_credit_stats(rule
, ctx
->xin
->resubmit_stats
);
1681 do_xlate_actions(rule
->up
.ofpacts
, rule
->up
.ofpacts_len
, ctx
);
1682 ctx
->rule
= old_rule
;
1689 xlate_table_action(struct xlate_ctx
*ctx
,
1690 ofp_port_t in_port
, uint8_t table_id
, bool may_packet_in
)
1692 if (ctx
->recurse
< MAX_RESUBMIT_RECURSION
) {
1693 struct rule_dpif
*rule
;
1694 ofp_port_t old_in_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
1695 uint8_t old_table_id
= ctx
->table_id
;
1698 ctx
->table_id
= table_id
;
1700 /* Look up a flow with 'in_port' as the input port. Then restore the
1701 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
1702 * have surprising behavior). */
1703 ctx
->xin
->flow
.in_port
.ofp_port
= in_port
;
1704 got_rule
= rule_dpif_lookup_in_table(ctx
->xbridge
->ofproto
,
1705 &ctx
->xin
->flow
, &ctx
->xout
->wc
,
1707 ctx
->xin
->flow
.in_port
.ofp_port
= old_in_port
;
1709 if (ctx
->xin
->resubmit_hook
) {
1710 ctx
->xin
->resubmit_hook(ctx
->xin
, rule
, ctx
->recurse
);
1714 xlate_recursively(ctx
, rule
);
1715 } else if (may_packet_in
) {
1716 struct xport
*xport
;
1719 * check if table configuration flags
1720 * OFPTC_TABLE_MISS_CONTROLLER, default.
1721 * OFPTC_TABLE_MISS_CONTINUE,
1722 * OFPTC_TABLE_MISS_DROP
1723 * When OF1.0, OFPTC_TABLE_MISS_CONTINUE is used. What to do? */
1724 xport
= get_ofp_port(ctx
->xbridge
, ctx
->xin
->flow
.in_port
.ofp_port
);
1725 rule
= choose_miss_rule(xport
? xport
->config
: 0,
1726 ctx
->xbridge
->miss_rule
,
1727 ctx
->xbridge
->no_packet_in_rule
);
1728 ovs_rwlock_rdlock(&rule
->up
.evict
);
1729 xlate_recursively(ctx
, rule
);
1732 ctx
->table_id
= old_table_id
;
1734 static struct vlog_rate_limit recurse_rl
= VLOG_RATE_LIMIT_INIT(1, 1);
1736 VLOG_ERR_RL(&recurse_rl
, "resubmit actions recursed over %d times",
1737 MAX_RESUBMIT_RECURSION
);
1742 xlate_ofpact_resubmit(struct xlate_ctx
*ctx
,
1743 const struct ofpact_resubmit
*resubmit
)
1748 in_port
= resubmit
->in_port
;
1749 if (in_port
== OFPP_IN_PORT
) {
1750 in_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
1753 table_id
= resubmit
->table_id
;
1754 if (table_id
== 255) {
1755 table_id
= ctx
->table_id
;
1758 xlate_table_action(ctx
, in_port
, table_id
, false);
1762 flood_packets(struct xlate_ctx
*ctx
, bool all
)
1764 const struct xport
*xport
;
1766 HMAP_FOR_EACH (xport
, ofp_node
, &ctx
->xbridge
->xports
) {
1767 if (xport
->ofp_port
== ctx
->xin
->flow
.in_port
.ofp_port
) {
1772 compose_output_action__(ctx
, xport
->ofp_port
, false);
1773 } else if (!(xport
->config
& OFPUTIL_PC_NO_FLOOD
)) {
1774 compose_output_action(ctx
, xport
->ofp_port
);
1778 ctx
->xout
->nf_output_iface
= NF_OUT_FLOOD
;
1782 execute_controller_action(struct xlate_ctx
*ctx
, int len
,
1783 enum ofp_packet_in_reason reason
,
1784 uint16_t controller_id
)
1786 struct ofputil_packet_in
*pin
;
1787 struct ofpbuf
*packet
;
1790 ovs_assert(!ctx
->xout
->slow
|| ctx
->xout
->slow
== SLOW_CONTROLLER
);
1791 ctx
->xout
->slow
= SLOW_CONTROLLER
;
1792 if (!ctx
->xin
->packet
) {
1796 packet
= ofpbuf_clone(ctx
->xin
->packet
);
1798 key
.skb_priority
= 0;
1800 memset(&key
.tunnel
, 0, sizeof key
.tunnel
);
1802 commit_odp_actions(&ctx
->xin
->flow
, &ctx
->base_flow
,
1803 &ctx
->xout
->odp_actions
, &ctx
->xout
->wc
);
1805 odp_execute_actions(NULL
, packet
, &key
, ctx
->xout
->odp_actions
.data
,
1806 ctx
->xout
->odp_actions
.size
, NULL
, NULL
);
1808 pin
= xmalloc(sizeof *pin
);
1809 pin
->packet_len
= packet
->size
;
1810 pin
->packet
= ofpbuf_steal_data(packet
);
1811 pin
->reason
= reason
;
1812 pin
->controller_id
= controller_id
;
1813 pin
->table_id
= ctx
->table_id
;
1814 pin
->cookie
= ctx
->rule
? ctx
->rule
->up
.flow_cookie
: 0;
1816 pin
->send_len
= len
;
1817 flow_get_metadata(&ctx
->xin
->flow
, &pin
->fmd
);
1819 ofproto_dpif_send_packet_in(ctx
->xbridge
->ofproto
, pin
);
1820 ofpbuf_delete(packet
);
1824 compose_mpls_push_action(struct xlate_ctx
*ctx
, ovs_be16 eth_type
)
1826 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
1827 struct flow
*flow
= &ctx
->xin
->flow
;
1829 ovs_assert(eth_type_mpls(eth_type
));
1831 memset(&wc
->masks
.mpls_lse
, 0xff, sizeof wc
->masks
.mpls_lse
);
1832 memset(&wc
->masks
.mpls_depth
, 0xff, sizeof wc
->masks
.mpls_depth
);
1834 if (flow
->mpls_depth
) {
1835 flow
->mpls_lse
&= ~htonl(MPLS_BOS_MASK
);
1841 if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1842 label
= htonl(0x2); /* IPV6 Explicit Null. */
1844 label
= htonl(0x0); /* IPV4 Explicit Null. */
1846 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
1847 wc
->masks
.nw_ttl
= 0xff;
1848 tc
= (flow
->nw_tos
& IP_DSCP_MASK
) >> 2;
1849 ttl
= flow
->nw_ttl
? flow
->nw_ttl
: 0x40;
1850 flow
->mpls_lse
= set_mpls_lse_values(ttl
, tc
, 1, label
);
1851 flow
->mpls_depth
= 1;
1853 flow
->dl_type
= eth_type
;
1857 compose_mpls_pop_action(struct xlate_ctx
*ctx
, ovs_be16 eth_type
)
1859 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
1860 struct flow
*flow
= &ctx
->xin
->flow
;
1862 ovs_assert(eth_type_mpls(ctx
->xin
->flow
.dl_type
));
1863 ovs_assert(!eth_type_mpls(eth_type
));
1865 memset(&wc
->masks
.mpls_lse
, 0xff, sizeof wc
->masks
.mpls_lse
);
1866 memset(&wc
->masks
.mpls_depth
, 0xff, sizeof wc
->masks
.mpls_depth
);
1868 if (flow
->mpls_depth
) {
1870 flow
->mpls_lse
= htonl(0);
1871 if (!flow
->mpls_depth
) {
1872 flow
->dl_type
= eth_type
;
1878 compose_dec_ttl(struct xlate_ctx
*ctx
, struct ofpact_cnt_ids
*ids
)
1880 struct flow
*flow
= &ctx
->xin
->flow
;
1882 if (!is_ip_any(flow
)) {
1886 ctx
->xout
->wc
.masks
.nw_ttl
= 0xff;
1887 if (flow
->nw_ttl
> 1) {
1893 for (i
= 0; i
< ids
->n_controllers
; i
++) {
1894 execute_controller_action(ctx
, UINT16_MAX
, OFPR_INVALID_TTL
,
1898 /* Stop processing for current table. */
1904 compose_set_mpls_ttl_action(struct xlate_ctx
*ctx
, uint8_t ttl
)
1906 if (!eth_type_mpls(ctx
->xin
->flow
.dl_type
)) {
1910 ctx
->xout
->wc
.masks
.mpls_lse
|= htonl(MPLS_TTL_MASK
);
1911 set_mpls_lse_ttl(&ctx
->xin
->flow
.mpls_lse
, ttl
);
1916 compose_dec_mpls_ttl_action(struct xlate_ctx
*ctx
)
1918 struct flow
*flow
= &ctx
->xin
->flow
;
1919 uint8_t ttl
= mpls_lse_to_ttl(flow
->mpls_lse
);
1920 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
1922 memset(&wc
->masks
.mpls_lse
, 0xff, sizeof wc
->masks
.mpls_lse
);
1924 if (!eth_type_mpls(flow
->dl_type
)) {
1930 set_mpls_lse_ttl(&flow
->mpls_lse
, ttl
);
1933 execute_controller_action(ctx
, UINT16_MAX
, OFPR_INVALID_TTL
, 0);
1935 /* Stop processing for current table. */
1941 xlate_output_action(struct xlate_ctx
*ctx
,
1942 ofp_port_t port
, uint16_t max_len
, bool may_packet_in
)
1944 ofp_port_t prev_nf_output_iface
= ctx
->xout
->nf_output_iface
;
1946 ctx
->xout
->nf_output_iface
= NF_OUT_DROP
;
1950 compose_output_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
);
1953 xlate_table_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
,
1960 flood_packets(ctx
, false);
1963 flood_packets(ctx
, true);
1965 case OFPP_CONTROLLER
:
1966 execute_controller_action(ctx
, max_len
, OFPR_ACTION
, 0);
1972 if (port
!= ctx
->xin
->flow
.in_port
.ofp_port
) {
1973 compose_output_action(ctx
, port
);
1975 xlate_report(ctx
, "skipping output to input port");
1980 if (prev_nf_output_iface
== NF_OUT_FLOOD
) {
1981 ctx
->xout
->nf_output_iface
= NF_OUT_FLOOD
;
1982 } else if (ctx
->xout
->nf_output_iface
== NF_OUT_DROP
) {
1983 ctx
->xout
->nf_output_iface
= prev_nf_output_iface
;
1984 } else if (prev_nf_output_iface
!= NF_OUT_DROP
&&
1985 ctx
->xout
->nf_output_iface
!= NF_OUT_FLOOD
) {
1986 ctx
->xout
->nf_output_iface
= NF_OUT_MULTI
;
1991 xlate_output_reg_action(struct xlate_ctx
*ctx
,
1992 const struct ofpact_output_reg
*or)
1994 uint64_t port
= mf_get_subfield(&or->src
, &ctx
->xin
->flow
);
1995 if (port
<= UINT16_MAX
) {
1996 union mf_subvalue value
;
1998 memset(&value
, 0xff, sizeof value
);
1999 mf_write_subfield_flow(&or->src
, &value
, &ctx
->xout
->wc
.masks
);
2000 xlate_output_action(ctx
, u16_to_ofp(port
),
2001 or->max_len
, false);
2006 xlate_enqueue_action(struct xlate_ctx
*ctx
,
2007 const struct ofpact_enqueue
*enqueue
)
2009 ofp_port_t ofp_port
= enqueue
->port
;
2010 uint32_t queue_id
= enqueue
->queue
;
2011 uint32_t flow_priority
, priority
;
2014 /* Translate queue to priority. */
2015 error
= dpif_queue_to_priority(ctx
->xbridge
->dpif
, queue_id
, &priority
);
2017 /* Fall back to ordinary output action. */
2018 xlate_output_action(ctx
, enqueue
->port
, 0, false);
2022 /* Check output port. */
2023 if (ofp_port
== OFPP_IN_PORT
) {
2024 ofp_port
= ctx
->xin
->flow
.in_port
.ofp_port
;
2025 } else if (ofp_port
== ctx
->xin
->flow
.in_port
.ofp_port
) {
2029 /* Add datapath actions. */
2030 flow_priority
= ctx
->xin
->flow
.skb_priority
;
2031 ctx
->xin
->flow
.skb_priority
= priority
;
2032 compose_output_action(ctx
, ofp_port
);
2033 ctx
->xin
->flow
.skb_priority
= flow_priority
;
2035 /* Update NetFlow output port. */
2036 if (ctx
->xout
->nf_output_iface
== NF_OUT_DROP
) {
2037 ctx
->xout
->nf_output_iface
= ofp_port
;
2038 } else if (ctx
->xout
->nf_output_iface
!= NF_OUT_FLOOD
) {
2039 ctx
->xout
->nf_output_iface
= NF_OUT_MULTI
;
2044 xlate_set_queue_action(struct xlate_ctx
*ctx
, uint32_t queue_id
)
2046 uint32_t skb_priority
;
2048 if (!dpif_queue_to_priority(ctx
->xbridge
->dpif
, queue_id
, &skb_priority
)) {
2049 ctx
->xin
->flow
.skb_priority
= skb_priority
;
2051 /* Couldn't translate queue to a priority. Nothing to do. A warning
2052 * has already been logged. */
2057 slave_enabled_cb(ofp_port_t ofp_port
, void *xbridge_
)
2059 const struct xbridge
*xbridge
= xbridge_
;
2070 case OFPP_CONTROLLER
: /* Not supported by the bundle action. */
2073 port
= get_ofp_port(xbridge
, ofp_port
);
2074 return port
? port
->may_enable
: false;
2079 xlate_bundle_action(struct xlate_ctx
*ctx
,
2080 const struct ofpact_bundle
*bundle
)
2084 port
= bundle_execute(bundle
, &ctx
->xin
->flow
, &ctx
->xout
->wc
,
2086 CONST_CAST(struct xbridge
*, ctx
->xbridge
));
2087 if (bundle
->dst
.field
) {
2088 nxm_reg_load(&bundle
->dst
, ofp_to_u16(port
), &ctx
->xin
->flow
,
2091 xlate_output_action(ctx
, port
, 0, false);
2096 xlate_learn_action(struct xlate_ctx
*ctx
,
2097 const struct ofpact_learn
*learn
)
2099 struct ofputil_flow_mod
*fm
;
2100 struct ofpbuf ofpacts
;
2102 ctx
->xout
->has_learn
= true;
2104 learn_mask(learn
, &ctx
->xout
->wc
);
2106 if (!ctx
->xin
->may_learn
) {
2110 fm
= xmalloc(sizeof *fm
);
2111 ofpbuf_init(&ofpacts
, 0);
2112 learn_execute(learn
, &ctx
->xin
->flow
, fm
, &ofpacts
);
2114 ofproto_dpif_flow_mod(ctx
->xbridge
->ofproto
, fm
);
2118 xlate_fin_timeout(struct xlate_ctx
*ctx
,
2119 const struct ofpact_fin_timeout
*oft
)
2121 if (ctx
->xin
->tcp_flags
& (TCP_FIN
| TCP_RST
) && ctx
->rule
) {
2122 ofproto_rule_reduce_timeouts(&ctx
->rule
->up
, oft
->fin_idle_timeout
,
2123 oft
->fin_hard_timeout
);
2128 xlate_sample_action(struct xlate_ctx
*ctx
,
2129 const struct ofpact_sample
*os
)
2131 union user_action_cookie cookie
;
2132 /* Scale the probability from 16-bit to 32-bit while representing
2133 * the same percentage. */
2134 uint32_t probability
= (os
->probability
<< 16) | os
->probability
;
2136 commit_odp_actions(&ctx
->xin
->flow
, &ctx
->base_flow
,
2137 &ctx
->xout
->odp_actions
, &ctx
->xout
->wc
);
2139 compose_flow_sample_cookie(os
->probability
, os
->collector_set_id
,
2140 os
->obs_domain_id
, os
->obs_point_id
, &cookie
);
2141 compose_sample_action(ctx
->xbridge
, &ctx
->xout
->odp_actions
, &ctx
->xin
->flow
,
2142 probability
, &cookie
, sizeof cookie
.flow_sample
);
2146 may_receive(const struct xport
*xport
, struct xlate_ctx
*ctx
)
2148 if (xport
->config
& (eth_addr_equals(ctx
->xin
->flow
.dl_dst
, eth_addr_stp
)
2149 ? OFPUTIL_PC_NO_RECV_STP
2150 : OFPUTIL_PC_NO_RECV
)) {
2154 /* Only drop packets here if both forwarding and learning are
2155 * disabled. If just learning is enabled, we need to have
2156 * OFPP_NORMAL and the learning action have a look at the packet
2157 * before we can drop it. */
2158 if (!xport_stp_forward_state(xport
) && !xport_stp_learn_state(xport
)) {
2166 do_xlate_actions(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
2167 struct xlate_ctx
*ctx
)
2169 struct flow_wildcards
*wc
= &ctx
->xout
->wc
;
2170 struct flow
*flow
= &ctx
->xin
->flow
;
2171 const struct ofpact
*a
;
2173 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
2174 struct ofpact_controller
*controller
;
2175 const struct ofpact_metadata
*metadata
;
2183 xlate_output_action(ctx
, ofpact_get_OUTPUT(a
)->port
,
2184 ofpact_get_OUTPUT(a
)->max_len
, true);
2188 /* XXX not yet implemented */
2191 case OFPACT_CONTROLLER
:
2192 controller
= ofpact_get_CONTROLLER(a
);
2193 execute_controller_action(ctx
, controller
->max_len
,
2195 controller
->controller_id
);
2198 case OFPACT_ENQUEUE
:
2199 xlate_enqueue_action(ctx
, ofpact_get_ENQUEUE(a
));
2202 case OFPACT_SET_VLAN_VID
:
2203 wc
->masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
2204 flow
->vlan_tci
&= ~htons(VLAN_VID_MASK
);
2205 flow
->vlan_tci
|= (htons(ofpact_get_SET_VLAN_VID(a
)->vlan_vid
)
2209 case OFPACT_SET_VLAN_PCP
:
2210 wc
->masks
.vlan_tci
|= htons(VLAN_PCP_MASK
| VLAN_CFI
);
2211 flow
->vlan_tci
&= ~htons(VLAN_PCP_MASK
);
2213 htons((ofpact_get_SET_VLAN_PCP(a
)->vlan_pcp
<< VLAN_PCP_SHIFT
)
2217 case OFPACT_STRIP_VLAN
:
2218 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
2219 flow
->vlan_tci
= htons(0);
2222 case OFPACT_PUSH_VLAN
:
2223 /* XXX 802.1AD(QinQ) */
2224 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
2225 flow
->vlan_tci
= htons(VLAN_CFI
);
2228 case OFPACT_SET_ETH_SRC
:
2229 memset(&wc
->masks
.dl_src
, 0xff, sizeof wc
->masks
.dl_src
);
2230 memcpy(flow
->dl_src
, ofpact_get_SET_ETH_SRC(a
)->mac
, ETH_ADDR_LEN
);
2233 case OFPACT_SET_ETH_DST
:
2234 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
2235 memcpy(flow
->dl_dst
, ofpact_get_SET_ETH_DST(a
)->mac
, ETH_ADDR_LEN
);
2238 case OFPACT_SET_IPV4_SRC
:
2239 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
2240 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2241 flow
->nw_src
= ofpact_get_SET_IPV4_SRC(a
)->ipv4
;
2245 case OFPACT_SET_IPV4_DST
:
2246 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
2247 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2248 flow
->nw_dst
= ofpact_get_SET_IPV4_DST(a
)->ipv4
;
2252 case OFPACT_SET_IPV4_DSCP
:
2253 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
2254 /* OpenFlow 1.0 only supports IPv4. */
2255 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2256 flow
->nw_tos
&= ~IP_DSCP_MASK
;
2257 flow
->nw_tos
|= ofpact_get_SET_IPV4_DSCP(a
)->dscp
;
2261 case OFPACT_SET_L4_SRC_PORT
:
2262 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
2263 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
2264 if (is_ip_any(flow
)) {
2265 flow
->tp_src
= htons(ofpact_get_SET_L4_SRC_PORT(a
)->port
);
2269 case OFPACT_SET_L4_DST_PORT
:
2270 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
2271 memset(&wc
->masks
.tp_dst
, 0xff, sizeof wc
->masks
.tp_dst
);
2272 if (is_ip_any(flow
)) {
2273 flow
->tp_dst
= htons(ofpact_get_SET_L4_DST_PORT(a
)->port
);
2277 case OFPACT_RESUBMIT
:
2278 xlate_ofpact_resubmit(ctx
, ofpact_get_RESUBMIT(a
));
2281 case OFPACT_SET_TUNNEL
:
2282 flow
->tunnel
.tun_id
= htonll(ofpact_get_SET_TUNNEL(a
)->tun_id
);
2285 case OFPACT_SET_QUEUE
:
2286 xlate_set_queue_action(ctx
, ofpact_get_SET_QUEUE(a
)->queue_id
);
2289 case OFPACT_POP_QUEUE
:
2290 flow
->skb_priority
= ctx
->orig_skb_priority
;
2293 case OFPACT_REG_MOVE
:
2294 nxm_execute_reg_move(ofpact_get_REG_MOVE(a
), flow
, wc
);
2297 case OFPACT_REG_LOAD
:
2298 nxm_execute_reg_load(ofpact_get_REG_LOAD(a
), flow
);
2301 case OFPACT_STACK_PUSH
:
2302 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a
), flow
, wc
,
2306 case OFPACT_STACK_POP
:
2307 nxm_execute_stack_pop(ofpact_get_STACK_POP(a
), flow
, wc
,
2311 case OFPACT_PUSH_MPLS
:
2312 compose_mpls_push_action(ctx
, ofpact_get_PUSH_MPLS(a
)->ethertype
);
2315 case OFPACT_POP_MPLS
:
2316 compose_mpls_pop_action(ctx
, ofpact_get_POP_MPLS(a
)->ethertype
);
2319 case OFPACT_SET_MPLS_TTL
:
2320 if (compose_set_mpls_ttl_action(ctx
,
2321 ofpact_get_SET_MPLS_TTL(a
)->ttl
)) {
2326 case OFPACT_DEC_MPLS_TTL
:
2327 if (compose_dec_mpls_ttl_action(ctx
)) {
2332 case OFPACT_DEC_TTL
:
2333 wc
->masks
.nw_ttl
= 0xff;
2334 if (compose_dec_ttl(ctx
, ofpact_get_DEC_TTL(a
))) {
2340 /* Nothing to do. */
2343 case OFPACT_MULTIPATH
:
2344 multipath_execute(ofpact_get_MULTIPATH(a
), flow
, wc
);
2348 xlate_bundle_action(ctx
, ofpact_get_BUNDLE(a
));
2351 case OFPACT_OUTPUT_REG
:
2352 xlate_output_reg_action(ctx
, ofpact_get_OUTPUT_REG(a
));
2356 xlate_learn_action(ctx
, ofpact_get_LEARN(a
));
2363 case OFPACT_FIN_TIMEOUT
:
2364 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
2365 ctx
->xout
->has_fin_timeout
= true;
2366 xlate_fin_timeout(ctx
, ofpact_get_FIN_TIMEOUT(a
));
2369 case OFPACT_CLEAR_ACTIONS
:
2371 * Nothing to do because writa-actions is not supported for now.
2372 * When writa-actions is supported, clear-actions also must
2373 * be supported at the same time.
2377 case OFPACT_WRITE_METADATA
:
2378 metadata
= ofpact_get_WRITE_METADATA(a
);
2379 flow
->metadata
&= ~metadata
->mask
;
2380 flow
->metadata
|= metadata
->metadata
& metadata
->mask
;
2384 /* Not implemented yet. */
2387 case OFPACT_GOTO_TABLE
: {
2388 /* It is assumed that goto-table is the last action. */
2389 struct ofpact_goto_table
*ogt
= ofpact_get_GOTO_TABLE(a
);
2391 ovs_assert(ctx
->table_id
< ogt
->table_id
);
2392 xlate_table_action(ctx
, ctx
->xin
->flow
.in_port
.ofp_port
,
2393 ogt
->table_id
, true);
2398 xlate_sample_action(ctx
, ofpact_get_SAMPLE(a
));
2405 xlate_in_init(struct xlate_in
*xin
, struct ofproto_dpif
*ofproto
,
2406 const struct flow
*flow
, struct rule_dpif
*rule
,
2407 uint8_t tcp_flags
, const struct ofpbuf
*packet
)
2409 xin
->ofproto
= ofproto
;
2411 xin
->packet
= packet
;
2412 xin
->may_learn
= packet
!= NULL
;
2414 xin
->ofpacts
= NULL
;
2415 xin
->ofpacts_len
= 0;
2416 xin
->tcp_flags
= tcp_flags
;
2417 xin
->resubmit_hook
= NULL
;
2418 xin
->report_hook
= NULL
;
2419 xin
->resubmit_stats
= NULL
;
2423 xlate_out_uninit(struct xlate_out
*xout
)
2426 ofpbuf_uninit(&xout
->odp_actions
);
2430 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
2431 * into datapath actions, using 'ctx', and discards the datapath actions. */
2433 xlate_actions_for_side_effects(struct xlate_in
*xin
)
2435 struct xlate_out xout
;
2437 xlate_actions(xin
, &xout
);
2438 xlate_out_uninit(&xout
);
2442 xlate_report(struct xlate_ctx
*ctx
, const char *s
)
2444 if (ctx
->xin
->report_hook
) {
2445 ctx
->xin
->report_hook(ctx
->xin
, s
, ctx
->recurse
);
2450 xlate_out_copy(struct xlate_out
*dst
, const struct xlate_out
*src
)
2453 dst
->slow
= src
->slow
;
2454 dst
->has_learn
= src
->has_learn
;
2455 dst
->has_normal
= src
->has_normal
;
2456 dst
->has_fin_timeout
= src
->has_fin_timeout
;
2457 dst
->nf_output_iface
= src
->nf_output_iface
;
2458 dst
->mirrors
= src
->mirrors
;
2460 ofpbuf_use_stub(&dst
->odp_actions
, dst
->odp_actions_stub
,
2461 sizeof dst
->odp_actions_stub
);
2462 ofpbuf_put(&dst
->odp_actions
, src
->odp_actions
.data
,
2463 src
->odp_actions
.size
);
2466 static struct skb_priority_to_dscp
*
2467 get_skb_priority(const struct xport
*xport
, uint32_t skb_priority
)
2469 struct skb_priority_to_dscp
*pdscp
;
2472 hash
= hash_int(skb_priority
, 0);
2473 HMAP_FOR_EACH_IN_BUCKET (pdscp
, hmap_node
, hash
, &xport
->skb_priorities
) {
2474 if (pdscp
->skb_priority
== skb_priority
) {
2482 dscp_from_skb_priority(const struct xport
*xport
, uint32_t skb_priority
,
2485 struct skb_priority_to_dscp
*pdscp
= get_skb_priority(xport
, skb_priority
);
2486 *dscp
= pdscp
? pdscp
->dscp
: 0;
2487 return pdscp
!= NULL
;
2491 clear_skb_priorities(struct xport
*xport
)
2493 struct skb_priority_to_dscp
*pdscp
, *next
;
2495 HMAP_FOR_EACH_SAFE (pdscp
, next
, hmap_node
, &xport
->skb_priorities
) {
2496 hmap_remove(&xport
->skb_priorities
, &pdscp
->hmap_node
);
2502 actions_output_to_local_port(const struct xlate_ctx
*ctx
)
2504 odp_port_t local_odp_port
= ofp_port_to_odp_port(ctx
->xbridge
, OFPP_LOCAL
);
2505 const struct nlattr
*a
;
2508 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, ctx
->xout
->odp_actions
.data
,
2509 ctx
->xout
->odp_actions
.size
) {
2510 if (nl_attr_type(a
) == OVS_ACTION_ATTR_OUTPUT
2511 && nl_attr_get_odp_port(a
) == local_odp_port
) {
2518 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
2519 * into datapath actions in 'odp_actions', using 'ctx'.
2521 * The caller must take responsibility for eventually freeing 'xout', with
2522 * xlate_out_uninit(). */
2524 xlate_actions(struct xlate_in
*xin
, struct xlate_out
*xout
)
2526 struct flow_wildcards
*wc
= &xout
->wc
;
2527 struct flow
*flow
= &xin
->flow
;
2529 enum slow_path_reason special
;
2530 const struct ofpact
*ofpacts
;
2531 struct xport
*in_port
;
2532 struct flow orig_flow
;
2533 struct xlate_ctx ctx
;
2537 COVERAGE_INC(xlate_actions
);
2539 ovs_rwlock_rdlock(&xlate_rwlock
);
2541 /* Flow initialization rules:
2542 * - 'base_flow' must match the kernel's view of the packet at the
2543 * time that action processing starts. 'flow' represents any
2544 * transformations we wish to make through actions.
2545 * - By default 'base_flow' and 'flow' are the same since the input
2546 * packet matches the output before any actions are applied.
2547 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
2548 * of the received packet as seen by the kernel. If we later output
2549 * to another device without any modifications this will cause us to
2550 * insert a new tag since the original one was stripped off by the
2552 * - Tunnel metadata as received is retained in 'flow'. This allows
2553 * tunnel metadata matching also in later tables.
2554 * Since a kernel action for setting the tunnel metadata will only be
2555 * generated with actual tunnel output, changing the tunnel metadata
2556 * values in 'flow' (such as tun_id) will only have effect with a later
2557 * tunnel output action.
2558 * - Tunnel 'base_flow' is completely cleared since that is what the
2559 * kernel does. If we wish to maintain the original values an action
2560 * needs to be generated. */
2565 ctx
.xout
->has_learn
= false;
2566 ctx
.xout
->has_normal
= false;
2567 ctx
.xout
->has_fin_timeout
= false;
2568 ctx
.xout
->nf_output_iface
= NF_OUT_DROP
;
2569 ctx
.xout
->mirrors
= 0;
2570 ofpbuf_use_stub(&ctx
.xout
->odp_actions
, ctx
.xout
->odp_actions_stub
,
2571 sizeof ctx
.xout
->odp_actions_stub
);
2572 ofpbuf_reserve(&ctx
.xout
->odp_actions
, NL_A_U32_SIZE
);
2574 ctx
.xbridge
= xbridge_lookup(xin
->ofproto
);
2579 ctx
.rule
= xin
->rule
;
2581 ctx
.base_flow
= *flow
;
2582 memset(&ctx
.base_flow
.tunnel
, 0, sizeof ctx
.base_flow
.tunnel
);
2583 ctx
.orig_tunnel_ip_dst
= flow
->tunnel
.ip_dst
;
2585 flow_wildcards_init_catchall(wc
);
2586 memset(&wc
->masks
.in_port
, 0xff, sizeof wc
->masks
.in_port
);
2587 memset(&wc
->masks
.skb_priority
, 0xff, sizeof wc
->masks
.skb_priority
);
2588 memset(&wc
->masks
.dl_type
, 0xff, sizeof wc
->masks
.dl_type
);
2589 wc
->masks
.nw_frag
|= FLOW_NW_FRAG_MASK
;
2591 tnl_may_send
= tnl_xlate_init(&ctx
.base_flow
, flow
, wc
);
2592 if (ctx
.xbridge
->has_netflow
) {
2593 netflow_mask_wc(flow
, wc
);
2597 ctx
.orig_skb_priority
= flow
->skb_priority
;
2602 ofpacts
= xin
->ofpacts
;
2603 ofpacts_len
= xin
->ofpacts_len
;
2604 } else if (xin
->rule
) {
2605 ofpacts
= xin
->rule
->up
.ofpacts
;
2606 ofpacts_len
= xin
->rule
->up
.ofpacts_len
;
2611 ofpbuf_use_stub(&ctx
.stack
, ctx
.init_stack
, sizeof ctx
.init_stack
);
2613 if (mbridge_has_mirrors(ctx
.xbridge
->mbridge
)) {
2614 /* Do this conditionally because the copy is expensive enough that it
2615 * shows up in profiles. */
2619 if (flow
->nw_frag
& FLOW_NW_FRAG_ANY
) {
2620 switch (ctx
.xbridge
->frag
) {
2621 case OFPC_FRAG_NORMAL
:
2622 /* We must pretend that transport ports are unavailable. */
2623 flow
->tp_src
= ctx
.base_flow
.tp_src
= htons(0);
2624 flow
->tp_dst
= ctx
.base_flow
.tp_dst
= htons(0);
2627 case OFPC_FRAG_DROP
:
2630 case OFPC_FRAG_REASM
:
2633 case OFPC_FRAG_NX_MATCH
:
2634 /* Nothing to do. */
2637 case OFPC_INVALID_TTL_TO_CONTROLLER
:
2642 in_port
= get_ofp_port(ctx
.xbridge
, flow
->in_port
.ofp_port
);
2643 special
= process_special(&ctx
, flow
, in_port
, ctx
.xin
->packet
);
2645 ctx
.xout
->slow
= special
;
2647 size_t sample_actions_len
;
2649 if (flow
->in_port
.ofp_port
2650 != vsp_realdev_to_vlandev(ctx
.xbridge
->ofproto
,
2651 flow
->in_port
.ofp_port
,
2653 ctx
.base_flow
.vlan_tci
= 0;
2656 add_sflow_action(&ctx
);
2657 add_ipfix_action(&ctx
);
2658 sample_actions_len
= ctx
.xout
->odp_actions
.size
;
2660 if (tnl_may_send
&& (!in_port
|| may_receive(in_port
, &ctx
))) {
2661 do_xlate_actions(ofpacts
, ofpacts_len
, &ctx
);
2663 /* We've let OFPP_NORMAL and the learning action look at the
2664 * packet, so drop it now if forwarding is disabled. */
2665 if (in_port
&& !xport_stp_forward_state(in_port
)) {
2666 ctx
.xout
->odp_actions
.size
= sample_actions_len
;
2670 if (ctx
.xbridge
->has_in_band
2671 && in_band_must_output_to_local_port(flow
)
2672 && !actions_output_to_local_port(&ctx
)) {
2673 compose_output_action(&ctx
, OFPP_LOCAL
);
2676 fix_sflow_action(&ctx
);
2678 if (mbridge_has_mirrors(ctx
.xbridge
->mbridge
)) {
2679 add_mirror_actions(&ctx
, &orig_flow
);
2683 ofpbuf_uninit(&ctx
.stack
);
2685 /* Clear the metadata and register wildcard masks, because we won't
2686 * use non-header fields as part of the cache. */
2687 memset(&wc
->masks
.metadata
, 0, sizeof wc
->masks
.metadata
);
2688 memset(&wc
->masks
.regs
, 0, sizeof wc
->masks
.regs
);
2691 ovs_rwlock_unlock(&xlate_rwlock
);