1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. */
17 #include "ofproto/ofproto-dpif-xlate.h"
25 #include "byte-order.h"
30 #include "dynamic-string.h"
35 #include "mac-learning.h"
36 #include "meta-flow.h"
37 #include "multipath.h"
38 #include "netdev-vport.h"
41 #include "odp-execute.h"
42 #include "ofp-actions.h"
43 #include "ofproto/ofproto-dpif-ipfix.h"
44 #include "ofproto/ofproto-dpif-mirror.h"
45 #include "ofproto/ofproto-dpif-monitor.h"
46 #include "ofproto/ofproto-dpif-sflow.h"
47 #include "ofproto/ofproto-dpif.h"
48 #include "ofproto/ofproto-provider.h"
52 COVERAGE_DEFINE(xlate_actions);
53 COVERAGE_DEFINE(xlate_actions_oversize);
54 COVERAGE_DEFINE(xlate_actions_mpls_overflow);
56 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
58 /* Maximum depth of flow table recursion (due to resubmit actions) in a
59 * flow translation. */
60 #define MAX_RESUBMIT_RECURSION 64
61 #define MAX_INTERNAL_RESUBMITS 1 /* Max resbmits allowed using rules in
64 /* Maximum number of resubmit actions in a flow translation, whether they are
65 * recursive or not. */
66 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
68 struct ovs_rwlock xlate_rwlock = OVS_RWLOCK_INITIALIZER;
71 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
72 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
74 struct list xbundles; /* Owned xbundles. */
75 struct hmap xports; /* Indexed by ofp_port. */
77 char *name; /* Name used in log messages. */
78 struct dpif *dpif; /* Datapath interface. */
79 struct mac_learning *ml; /* Mac learning handle. */
80 struct mbridge *mbridge; /* Mirroring. */
81 struct dpif_sflow *sflow; /* SFlow handle, or null. */
82 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
83 struct netflow *netflow; /* Netflow handle, or null. */
84 struct stp *stp; /* STP or null if disabled. */
86 /* Special rules installed by ofproto-dpif. */
87 struct rule_dpif *miss_rule;
88 struct rule_dpif *no_packet_in_rule;
90 enum ofp_config_flags frag; /* Fragmentation handling. */
91 bool has_in_band; /* Bridge has in band control? */
92 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
94 /* True if the datapath supports recirculation. */
97 /* True if the datapath supports variable-length
98 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
99 * False if the datapath supports only 8-byte (or shorter) userdata. */
100 bool variable_length_userdata;
102 /* Number of MPLS label stack entries that the datapath supports
104 size_t max_mpls_depth;
108 struct hmap_node hmap_node; /* In global 'xbundles' map. */
109 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
111 struct list list_node; /* In parent 'xbridges' list. */
112 struct xbridge *xbridge; /* Parent xbridge. */
114 struct list xports; /* Contains "struct xport"s. */
116 char *name; /* Name used in log messages. */
117 struct bond *bond; /* Nonnull iff more than one port. */
118 struct lacp *lacp; /* LACP handle or null. */
120 enum port_vlan_mode vlan_mode; /* VLAN mode. */
121 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
122 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
123 * NULL if all VLANs are trunked. */
124 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
125 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
129 struct hmap_node hmap_node; /* Node in global 'xports' map. */
130 struct ofport_dpif *ofport; /* Key in global 'xports map. */
132 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
133 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
135 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
137 struct list bundle_node; /* In parent xbundle (if it exists). */
138 struct xbundle *xbundle; /* Parent xbundle or null. */
140 struct netdev *netdev; /* 'ofport''s netdev. */
142 struct xbridge *xbridge; /* Parent bridge. */
143 struct xport *peer; /* Patch port peer or null. */
145 enum ofputil_port_config config; /* OpenFlow port configuration. */
146 enum ofputil_port_state state; /* OpenFlow port state. */
147 int stp_port_no; /* STP port number or -1 if not in use. */
149 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
151 bool may_enable; /* May be enabled in bonds. */
152 bool is_tunnel; /* Is a tunnel port. */
154 struct cfm *cfm; /* CFM handle or null. */
155 struct bfd *bfd; /* BFD handle or null. */
159 struct xlate_in *xin;
160 struct xlate_out *xout;
162 const struct xbridge *xbridge;
164 /* Flow at the last commit. */
165 struct flow base_flow;
167 /* Tunnel IP destination address as received. This is stored separately
168 * as the base_flow.tunnel is cleared on init to reflect the datapath
169 * behavior. Used to make sure not to send tunneled output to ourselves,
170 * which might lead to an infinite loop. This could happen easily
171 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
172 * actually set the tun_dst field. */
173 ovs_be32 orig_tunnel_ip_dst;
175 /* Stack for the push and pop actions. Each stack element is of type
176 * "union mf_subvalue". */
177 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
180 /* The rule that we are currently translating, or NULL. */
181 struct rule_dpif *rule;
183 /* Resubmit statistics, via xlate_table_action(). */
184 int recurse; /* Current resubmit nesting depth. */
185 int resubmits; /* Total number of resubmits. */
186 bool in_group; /* Currently translating ofgroup, if true. */
188 uint32_t orig_skb_priority; /* Priority when packet arrived. */
189 uint8_t table_id; /* OpenFlow table ID where flow was found. */
190 uint32_t sflow_n_outputs; /* Number of output ports. */
191 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
192 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
193 bool exit; /* No further actions should be processed. */
195 /* OpenFlow 1.1+ action set.
197 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
198 * When translation is otherwise complete, ofpacts_execute_action_set()
199 * converts it to a set of "struct ofpact"s that can be translated into
200 * datapath actions. */
201 struct ofpbuf action_set; /* Action set. */
202 uint64_t action_set_stub[1024 / 8];
205 /* A controller may use OFPP_NONE as the ingress port to indicate that
206 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
207 * when an input bundle is needed for validation (e.g., mirroring or
208 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
209 * any 'port' structs, so care must be taken when dealing with it. */
210 static struct xbundle ofpp_none_bundle = {
212 .vlan_mode = PORT_VLAN_TRUNK
215 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
216 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
217 * traffic egressing the 'ofport' with that priority should be marked with. */
218 struct skb_priority_to_dscp {
219 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
220 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
222 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
225 static struct hmap xbridges = HMAP_INITIALIZER(&xbridges);
226 static struct hmap xbundles = HMAP_INITIALIZER(&xbundles);
227 static struct hmap xports = HMAP_INITIALIZER(&xports);
229 static bool may_receive(const struct xport *, struct xlate_ctx *);
230 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
232 static void xlate_actions__(struct xlate_in *, struct xlate_out *)
233 OVS_REQ_RDLOCK(xlate_rwlock);
234 static void xlate_normal(struct xlate_ctx *);
235 static void xlate_report(struct xlate_ctx *, const char *);
236 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
237 uint8_t table_id, bool may_packet_in,
238 bool honor_table_miss);
239 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
240 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
241 static void output_normal(struct xlate_ctx *, const struct xbundle *,
243 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
245 static struct xbridge *xbridge_lookup(const struct ofproto_dpif *);
246 static struct xbundle *xbundle_lookup(const struct ofbundle *);
247 static struct xport *xport_lookup(const struct ofport_dpif *);
248 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
249 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
250 uint32_t skb_priority);
251 static void clear_skb_priorities(struct xport *);
252 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
256 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
257 struct dpif *dpif, struct rule_dpif *miss_rule,
258 struct rule_dpif *no_packet_in_rule,
259 const struct mac_learning *ml, struct stp *stp,
260 const struct mbridge *mbridge,
261 const struct dpif_sflow *sflow,
262 const struct dpif_ipfix *ipfix,
263 const struct netflow *netflow, enum ofp_config_flags frag,
264 bool forward_bpdu, bool has_in_band,
266 bool variable_length_userdata,
267 size_t max_mpls_depth)
269 struct xbridge *xbridge = xbridge_lookup(ofproto);
272 xbridge = xzalloc(sizeof *xbridge);
273 xbridge->ofproto = ofproto;
275 hmap_insert(&xbridges, &xbridge->hmap_node, hash_pointer(ofproto, 0));
276 hmap_init(&xbridge->xports);
277 list_init(&xbridge->xbundles);
280 if (xbridge->ml != ml) {
281 mac_learning_unref(xbridge->ml);
282 xbridge->ml = mac_learning_ref(ml);
285 if (xbridge->mbridge != mbridge) {
286 mbridge_unref(xbridge->mbridge);
287 xbridge->mbridge = mbridge_ref(mbridge);
290 if (xbridge->sflow != sflow) {
291 dpif_sflow_unref(xbridge->sflow);
292 xbridge->sflow = dpif_sflow_ref(sflow);
295 if (xbridge->ipfix != ipfix) {
296 dpif_ipfix_unref(xbridge->ipfix);
297 xbridge->ipfix = dpif_ipfix_ref(ipfix);
300 if (xbridge->stp != stp) {
301 stp_unref(xbridge->stp);
302 xbridge->stp = stp_ref(stp);
305 if (xbridge->netflow != netflow) {
306 netflow_unref(xbridge->netflow);
307 xbridge->netflow = netflow_ref(netflow);
311 xbridge->name = xstrdup(name);
313 xbridge->dpif = dpif;
314 xbridge->forward_bpdu = forward_bpdu;
315 xbridge->has_in_band = has_in_band;
316 xbridge->frag = frag;
317 xbridge->miss_rule = miss_rule;
318 xbridge->no_packet_in_rule = no_packet_in_rule;
319 xbridge->enable_recirc = enable_recirc;
320 xbridge->variable_length_userdata = variable_length_userdata;
321 xbridge->max_mpls_depth = max_mpls_depth;
325 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
327 struct xbridge *xbridge = xbridge_lookup(ofproto);
328 struct xbundle *xbundle, *next_xbundle;
329 struct xport *xport, *next_xport;
335 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
336 xlate_ofport_remove(xport->ofport);
339 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
340 xlate_bundle_remove(xbundle->ofbundle);
343 hmap_remove(&xbridges, &xbridge->hmap_node);
344 mac_learning_unref(xbridge->ml);
345 mbridge_unref(xbridge->mbridge);
346 dpif_sflow_unref(xbridge->sflow);
347 dpif_ipfix_unref(xbridge->ipfix);
348 stp_unref(xbridge->stp);
349 hmap_destroy(&xbridge->xports);
355 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
356 const char *name, enum port_vlan_mode vlan_mode, int vlan,
357 unsigned long *trunks, bool use_priority_tags,
358 const struct bond *bond, const struct lacp *lacp,
361 struct xbundle *xbundle = xbundle_lookup(ofbundle);
364 xbundle = xzalloc(sizeof *xbundle);
365 xbundle->ofbundle = ofbundle;
366 xbundle->xbridge = xbridge_lookup(ofproto);
368 hmap_insert(&xbundles, &xbundle->hmap_node, hash_pointer(ofbundle, 0));
369 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
370 list_init(&xbundle->xports);
373 ovs_assert(xbundle->xbridge);
376 xbundle->name = xstrdup(name);
378 xbundle->vlan_mode = vlan_mode;
379 xbundle->vlan = vlan;
380 xbundle->trunks = trunks;
381 xbundle->use_priority_tags = use_priority_tags;
382 xbundle->floodable = floodable;
384 if (xbundle->bond != bond) {
385 bond_unref(xbundle->bond);
386 xbundle->bond = bond_ref(bond);
389 if (xbundle->lacp != lacp) {
390 lacp_unref(xbundle->lacp);
391 xbundle->lacp = lacp_ref(lacp);
396 xlate_bundle_remove(struct ofbundle *ofbundle)
398 struct xbundle *xbundle = xbundle_lookup(ofbundle);
399 struct xport *xport, *next;
405 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
406 list_remove(&xport->bundle_node);
407 xport->xbundle = NULL;
410 hmap_remove(&xbundles, &xbundle->hmap_node);
411 list_remove(&xbundle->list_node);
412 bond_unref(xbundle->bond);
413 lacp_unref(xbundle->lacp);
419 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
420 struct ofport_dpif *ofport, ofp_port_t ofp_port,
421 odp_port_t odp_port, const struct netdev *netdev,
422 const struct cfm *cfm, const struct bfd *bfd,
423 struct ofport_dpif *peer, int stp_port_no,
424 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
425 enum ofputil_port_config config,
426 enum ofputil_port_state state, bool is_tunnel,
429 struct xport *xport = xport_lookup(ofport);
433 xport = xzalloc(sizeof *xport);
434 xport->ofport = ofport;
435 xport->xbridge = xbridge_lookup(ofproto);
436 xport->ofp_port = ofp_port;
438 hmap_init(&xport->skb_priorities);
439 hmap_insert(&xports, &xport->hmap_node, hash_pointer(ofport, 0));
440 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
441 hash_ofp_port(xport->ofp_port));
444 ovs_assert(xport->ofp_port == ofp_port);
446 xport->config = config;
447 xport->state = state;
448 xport->stp_port_no = stp_port_no;
449 xport->is_tunnel = is_tunnel;
450 xport->may_enable = may_enable;
451 xport->odp_port = odp_port;
453 if (xport->netdev != netdev) {
454 netdev_close(xport->netdev);
455 xport->netdev = netdev_ref(netdev);
458 if (xport->cfm != cfm) {
459 cfm_unref(xport->cfm);
460 xport->cfm = cfm_ref(cfm);
463 if (xport->bfd != bfd) {
464 bfd_unref(xport->bfd);
465 xport->bfd = bfd_ref(bfd);
469 xport->peer->peer = NULL;
471 xport->peer = xport_lookup(peer);
473 xport->peer->peer = xport;
476 if (xport->xbundle) {
477 list_remove(&xport->bundle_node);
479 xport->xbundle = xbundle_lookup(ofbundle);
480 if (xport->xbundle) {
481 list_insert(&xport->xbundle->xports, &xport->bundle_node);
484 clear_skb_priorities(xport);
485 for (i = 0; i < n_qdscp; i++) {
486 struct skb_priority_to_dscp *pdscp;
487 uint32_t skb_priority;
489 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
494 pdscp = xmalloc(sizeof *pdscp);
495 pdscp->skb_priority = skb_priority;
496 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
497 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
498 hash_int(pdscp->skb_priority, 0));
503 xlate_ofport_remove(struct ofport_dpif *ofport)
505 struct xport *xport = xport_lookup(ofport);
512 xport->peer->peer = NULL;
516 if (xport->xbundle) {
517 list_remove(&xport->bundle_node);
520 clear_skb_priorities(xport);
521 hmap_destroy(&xport->skb_priorities);
523 hmap_remove(&xports, &xport->hmap_node);
524 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
526 netdev_close(xport->netdev);
527 cfm_unref(xport->cfm);
528 bfd_unref(xport->bfd);
532 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
533 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
534 * Optionally populates 'ofproto' with the ofproto_dpif, 'odp_in_port' with
535 * the datapath in_port, that 'packet' ingressed, and 'ipfix', 'sflow', and
536 * 'netflow' with the appropriate handles for those protocols if they're
537 * enabled. Caller is responsible for unrefing them.
539 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
540 * 'flow''s in_port to OFPP_NONE.
542 * This function does post-processing on data returned from
543 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
544 * of the upcall processing logic. In particular, if the extracted in_port is
545 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
546 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
547 * a VLAN header onto 'packet' (if it is nonnull).
549 * Similarly, this function also includes some logic to help with tunnels. It
550 * may modify 'flow' as necessary to make the tunneling implementation
551 * transparent to the upcall processing logic.
553 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
554 * or some other positive errno if there are other problems. */
556 xlate_receive(const struct dpif_backer *backer, struct ofpbuf *packet,
557 const struct nlattr *key, size_t key_len, struct flow *flow,
558 struct ofproto_dpif **ofproto, struct dpif_ipfix **ipfix,
559 struct dpif_sflow **sflow, struct netflow **netflow,
560 odp_port_t *odp_in_port)
562 const struct xport *xport;
565 ovs_rwlock_rdlock(&xlate_rwlock);
566 if (odp_flow_key_to_flow(key, key_len, flow) == ODP_FIT_ERROR) {
572 *odp_in_port = flow->in_port.odp_port;
575 xport = xport_lookup(tnl_port_should_receive(flow)
576 ? tnl_port_receive(flow)
577 : odp_port_to_ofport(backer, flow->in_port.odp_port));
579 flow->in_port.ofp_port = xport ? xport->ofp_port : OFPP_NONE;
584 if (vsp_adjust_flow(xport->xbridge->ofproto, flow)) {
586 /* Make the packet resemble the flow, so that it gets sent to
587 * an OpenFlow controller properly, so that it looks correct
588 * for sFlow, and so that flow_extract() will get the correct
589 * vlan_tci if it is called on 'packet'. */
590 eth_push_vlan(packet, htons(ETH_TYPE_VLAN), flow->vlan_tci);
596 *ofproto = xport->xbridge->ofproto;
600 *ipfix = dpif_ipfix_ref(xport->xbridge->ipfix);
604 *sflow = dpif_sflow_ref(xport->xbridge->sflow);
608 *netflow = netflow_ref(xport->xbridge->netflow);
612 ovs_rwlock_unlock(&xlate_rwlock);
616 static struct xbridge *
617 xbridge_lookup(const struct ofproto_dpif *ofproto)
619 struct xbridge *xbridge;
625 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
627 if (xbridge->ofproto == ofproto) {
634 static struct xbundle *
635 xbundle_lookup(const struct ofbundle *ofbundle)
637 struct xbundle *xbundle;
643 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
645 if (xbundle->ofbundle == ofbundle) {
652 static struct xport *
653 xport_lookup(const struct ofport_dpif *ofport)
661 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
663 if (xport->ofport == ofport) {
670 static struct stp_port *
671 xport_get_stp_port(const struct xport *xport)
673 return xport->xbridge->stp && xport->stp_port_no != -1
674 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
679 xport_stp_learn_state(const struct xport *xport)
681 struct stp_port *sp = xport_get_stp_port(xport);
682 return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
686 xport_stp_forward_state(const struct xport *xport)
688 struct stp_port *sp = xport_get_stp_port(xport);
689 return stp_forward_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
693 xport_stp_listen_state(const struct xport *xport)
695 struct stp_port *sp = xport_get_stp_port(xport);
696 return stp_listen_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
699 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
700 * were used to make the determination.*/
702 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
704 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
705 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
709 stp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
711 struct stp_port *sp = xport_get_stp_port(xport);
712 struct ofpbuf payload = *packet;
713 struct eth_header *eth = ofpbuf_data(&payload);
715 /* Sink packets on ports that have STP disabled when the bridge has
717 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
721 /* Trim off padding on payload. */
722 if (ofpbuf_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
723 ofpbuf_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
726 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
727 stp_received_bpdu(sp, ofpbuf_data(&payload), ofpbuf_size(&payload));
731 static struct xport *
732 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
736 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
738 if (xport->ofp_port == ofp_port) {
746 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
748 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
749 return xport ? xport->odp_port : ODPP_NONE;
753 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
757 xport = get_ofp_port(ctx->xbridge, ofp_port);
758 if (!xport || xport->config & OFPUTIL_PC_PORT_DOWN ||
759 xport->state & OFPUTIL_PS_LINK_DOWN) {
766 static const struct ofputil_bucket *
767 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
771 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
773 struct group_dpif *group;
776 hit = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
781 hit = group_first_live_bucket(ctx, group, depth) != NULL;
783 group_dpif_release(group);
787 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
790 bucket_is_alive(const struct xlate_ctx *ctx,
791 const struct ofputil_bucket *bucket, int depth)
793 if (depth >= MAX_LIVENESS_RECURSION) {
794 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
796 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
797 MAX_LIVENESS_RECURSION);
801 return !ofputil_bucket_has_liveness(bucket) ||
802 (bucket->watch_port != OFPP_ANY &&
803 odp_port_is_alive(ctx, bucket->watch_port)) ||
804 (bucket->watch_group != OFPG_ANY &&
805 group_is_alive(ctx, bucket->watch_group, depth + 1));
808 static const struct ofputil_bucket *
809 group_first_live_bucket(const struct xlate_ctx *ctx,
810 const struct group_dpif *group, int depth)
812 struct ofputil_bucket *bucket;
813 const struct list *buckets;
815 group_dpif_get_buckets(group, &buckets);
816 LIST_FOR_EACH (bucket, list_node, buckets) {
817 if (bucket_is_alive(ctx, bucket, depth)) {
825 static const struct ofputil_bucket *
826 group_best_live_bucket(const struct xlate_ctx *ctx,
827 const struct group_dpif *group,
830 const struct ofputil_bucket *best_bucket = NULL;
831 uint32_t best_score = 0;
834 const struct ofputil_bucket *bucket;
835 const struct list *buckets;
837 group_dpif_get_buckets(group, &buckets);
838 LIST_FOR_EACH (bucket, list_node, buckets) {
839 if (bucket_is_alive(ctx, bucket, 0)) {
840 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
841 if (score >= best_score) {
842 best_bucket = bucket;
853 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
855 return (bundle->vlan_mode != PORT_VLAN_ACCESS
856 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
860 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
862 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
866 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
868 return xbundle != &ofpp_none_bundle
869 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
874 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
876 return xbundle != &ofpp_none_bundle
877 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
882 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
884 return xbundle != &ofpp_none_bundle
885 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
889 static struct xbundle *
890 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
891 bool warn, struct xport **in_xportp)
895 /* Find the port and bundle for the received packet. */
896 xport = get_ofp_port(xbridge, in_port);
900 if (xport && xport->xbundle) {
901 return xport->xbundle;
904 /* Special-case OFPP_NONE, which a controller may use as the ingress
905 * port for traffic that it is sourcing. */
906 if (in_port == OFPP_NONE) {
907 return &ofpp_none_bundle;
910 /* Odd. A few possible reasons here:
912 * - We deleted a port but there are still a few packets queued up
915 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
916 * we don't know about.
918 * - The ofproto client didn't configure the port as part of a bundle.
919 * This is particularly likely to happen if a packet was received on the
920 * port after it was created, but before the client had a chance to
921 * configure its bundle.
924 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
926 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
927 "port %"PRIu16, xbridge->name, in_port);
933 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
935 const struct xbridge *xbridge = ctx->xbridge;
936 mirror_mask_t mirrors;
937 struct xbundle *in_xbundle;
941 mirrors = ctx->xout->mirrors;
942 ctx->xout->mirrors = 0;
944 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
945 ctx->xin->packet != NULL, NULL);
949 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
951 /* Drop frames on bundles reserved for mirroring. */
952 if (xbundle_mirror_out(xbridge, in_xbundle)) {
953 if (ctx->xin->packet != NULL) {
954 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
955 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
956 "%s, which is reserved exclusively for mirroring",
957 ctx->xbridge->name, in_xbundle->name);
959 ofpbuf_clear(&ctx->xout->odp_actions);
964 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
965 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
968 vlan = input_vid_to_vlan(in_xbundle, vid);
974 /* Restore the original packet before adding the mirror actions. */
975 ctx->xin->flow = *orig_flow;
978 mirror_mask_t dup_mirrors;
979 struct ofbundle *out;
980 unsigned long *vlans;
985 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
986 &vlans, &dup_mirrors, &out, &out_vlan);
987 ovs_assert(has_mirror);
990 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
992 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
995 if (!vlan_mirrored) {
996 mirrors = zero_rightmost_1bit(mirrors);
1000 mirrors &= ~dup_mirrors;
1001 ctx->xout->mirrors |= dup_mirrors;
1003 struct xbundle *out_xbundle = xbundle_lookup(out);
1005 output_normal(ctx, out_xbundle, vlan);
1007 } else if (vlan != out_vlan
1008 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
1009 struct xbundle *xbundle;
1011 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1012 if (xbundle_includes_vlan(xbundle, out_vlan)
1013 && !xbundle_mirror_out(xbridge, xbundle)) {
1014 output_normal(ctx, xbundle, out_vlan);
1021 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1022 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1023 * the bundle on which the packet was received, returns the VLAN to which the
1026 * Both 'vid' and the return value are in the range 0...4095. */
1028 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1030 switch (in_xbundle->vlan_mode) {
1031 case PORT_VLAN_ACCESS:
1032 return in_xbundle->vlan;
1035 case PORT_VLAN_TRUNK:
1038 case PORT_VLAN_NATIVE_UNTAGGED:
1039 case PORT_VLAN_NATIVE_TAGGED:
1040 return vid ? vid : in_xbundle->vlan;
1047 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1048 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1051 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1052 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1055 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1057 /* Allow any VID on the OFPP_NONE port. */
1058 if (in_xbundle == &ofpp_none_bundle) {
1062 switch (in_xbundle->vlan_mode) {
1063 case PORT_VLAN_ACCESS:
1066 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1067 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1068 "packet received on port %s configured as VLAN "
1069 "%"PRIu16" access port", vid, in_xbundle->name,
1076 case PORT_VLAN_NATIVE_UNTAGGED:
1077 case PORT_VLAN_NATIVE_TAGGED:
1079 /* Port must always carry its native VLAN. */
1083 case PORT_VLAN_TRUNK:
1084 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1086 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1087 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1088 "received on port %s not configured for trunking "
1089 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1101 /* Given 'vlan', the VLAN that a packet belongs to, and
1102 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1103 * that should be included in the 802.1Q header. (If the return value is 0,
1104 * then the 802.1Q header should only be included in the packet if there is a
1107 * Both 'vlan' and the return value are in the range 0...4095. */
1109 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1111 switch (out_xbundle->vlan_mode) {
1112 case PORT_VLAN_ACCESS:
1115 case PORT_VLAN_TRUNK:
1116 case PORT_VLAN_NATIVE_TAGGED:
1119 case PORT_VLAN_NATIVE_UNTAGGED:
1120 return vlan == out_xbundle->vlan ? 0 : vlan;
1128 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1131 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1133 ovs_be16 tci, old_tci;
1134 struct xport *xport;
1136 vid = output_vlan_to_vid(out_xbundle, vlan);
1137 if (list_is_empty(&out_xbundle->xports)) {
1138 /* Partially configured bundle with no slaves. Drop the packet. */
1140 } else if (!out_xbundle->bond) {
1141 ctx->xout->use_recirc = false;
1142 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1145 struct ofport_dpif *ofport;
1146 struct xlate_recirc *xr = &ctx->xout->recirc;
1148 if (ctx->xbridge->enable_recirc) {
1149 ctx->xout->use_recirc = bond_may_recirc(
1150 out_xbundle->bond, &xr->recirc_id, &xr->hash_bias);
1152 if (ctx->xout->use_recirc) {
1153 /* Only TCP mode uses recirculation. */
1154 xr->hash_alg = OVS_RECIRC_HASH_ALG_L4;
1155 bond_update_post_recirc_rules(out_xbundle->bond, false);
1159 ofport = bond_choose_output_slave(out_xbundle->bond, &ctx->xin->flow,
1160 &ctx->xout->wc, vid);
1161 xport = xport_lookup(ofport);
1164 /* No slaves enabled, so drop packet. */
1168 if (ctx->xin->resubmit_stats) {
1169 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1170 ctx->xin->resubmit_stats->n_bytes);
1174 old_tci = *flow_tci;
1176 if (tci || out_xbundle->use_priority_tags) {
1177 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1179 tci |= htons(VLAN_CFI);
1184 compose_output_action(ctx, xport->ofp_port);
1185 *flow_tci = old_tci;
1188 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1189 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1190 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1192 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1194 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1198 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1199 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1203 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1204 if (flow->nw_proto == ARP_OP_REPLY) {
1206 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1207 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1208 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1210 return flow->nw_src == flow->nw_dst;
1216 /* Checks whether a MAC learning update is necessary for MAC learning table
1217 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1220 * Most packets processed through the MAC learning table do not actually
1221 * change it in any way. This function requires only a read lock on the MAC
1222 * learning table, so it is much cheaper in this common case.
1224 * Keep the code here synchronized with that in update_learning_table__()
1227 is_mac_learning_update_needed(const struct mac_learning *ml,
1228 const struct flow *flow,
1229 struct flow_wildcards *wc,
1230 int vlan, struct xbundle *in_xbundle)
1231 OVS_REQ_RDLOCK(ml->rwlock)
1233 struct mac_entry *mac;
1235 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1239 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1240 if (!mac || mac_entry_age(ml, mac)) {
1244 if (is_gratuitous_arp(flow, wc)) {
1245 /* We don't want to learn from gratuitous ARP packets that are
1246 * reflected back over bond slaves so we lock the learning table. */
1247 if (!in_xbundle->bond) {
1249 } else if (mac_entry_is_grat_arp_locked(mac)) {
1254 return mac->port.p != in_xbundle->ofbundle;
1258 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1259 * received on 'in_xbundle' in 'vlan'.
1261 * This code repeats all the checks in is_mac_learning_update_needed() because
1262 * the lock was released between there and here and thus the MAC learning state
1263 * could have changed.
1265 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1268 update_learning_table__(const struct xbridge *xbridge,
1269 const struct flow *flow, struct flow_wildcards *wc,
1270 int vlan, struct xbundle *in_xbundle)
1271 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1273 struct mac_entry *mac;
1275 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1279 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1280 if (is_gratuitous_arp(flow, wc)) {
1281 /* We don't want to learn from gratuitous ARP packets that are
1282 * reflected back over bond slaves so we lock the learning table. */
1283 if (!in_xbundle->bond) {
1284 mac_entry_set_grat_arp_lock(mac);
1285 } else if (mac_entry_is_grat_arp_locked(mac)) {
1290 if (mac->port.p != in_xbundle->ofbundle) {
1291 /* The log messages here could actually be useful in debugging,
1292 * so keep the rate limit relatively high. */
1293 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1295 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1296 "on port %s in VLAN %d",
1297 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1298 in_xbundle->name, vlan);
1300 mac->port.p = in_xbundle->ofbundle;
1301 mac_learning_changed(xbridge->ml);
1306 update_learning_table(const struct xbridge *xbridge,
1307 const struct flow *flow, struct flow_wildcards *wc,
1308 int vlan, struct xbundle *in_xbundle)
1312 /* Don't learn the OFPP_NONE port. */
1313 if (in_xbundle == &ofpp_none_bundle) {
1317 /* First try the common case: no change to MAC learning table. */
1318 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1319 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1321 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1324 /* Slow path: MAC learning table might need an update. */
1325 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1326 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1327 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1331 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1332 * dropped. Returns true if they may be forwarded, false if they should be
1335 * 'in_port' must be the xport that corresponds to flow->in_port.
1336 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1338 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1339 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1340 * checked by input_vid_is_valid().
1342 * May also add tags to '*tags', although the current implementation only does
1343 * so in one special case.
1346 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1349 struct xbundle *in_xbundle = in_port->xbundle;
1350 const struct xbridge *xbridge = ctx->xbridge;
1351 struct flow *flow = &ctx->xin->flow;
1353 /* Drop frames for reserved multicast addresses
1354 * only if forward_bpdu option is absent. */
1355 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1356 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1360 if (in_xbundle->bond) {
1361 struct mac_entry *mac;
1363 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1369 xlate_report(ctx, "bonding refused admissibility, dropping");
1372 case BV_DROP_IF_MOVED:
1373 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1374 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1375 if (mac && mac->port.p != in_xbundle->ofbundle &&
1376 (!is_gratuitous_arp(flow, &ctx->xout->wc)
1377 || mac_entry_is_grat_arp_locked(mac))) {
1378 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1379 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1383 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1392 xlate_normal(struct xlate_ctx *ctx)
1394 struct flow_wildcards *wc = &ctx->xout->wc;
1395 struct flow *flow = &ctx->xin->flow;
1396 struct xbundle *in_xbundle;
1397 struct xport *in_port;
1398 struct mac_entry *mac;
1403 ctx->xout->has_normal = true;
1405 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1406 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1407 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1409 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
1410 ctx->xin->packet != NULL, &in_port);
1412 xlate_report(ctx, "no input bundle, dropping");
1416 /* Drop malformed frames. */
1417 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
1418 !(flow->vlan_tci & htons(VLAN_CFI))) {
1419 if (ctx->xin->packet != NULL) {
1420 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1421 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
1422 "VLAN tag received on port %s",
1423 ctx->xbridge->name, in_xbundle->name);
1425 xlate_report(ctx, "partial VLAN tag, dropping");
1429 /* Drop frames on bundles reserved for mirroring. */
1430 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
1431 if (ctx->xin->packet != NULL) {
1432 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1433 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1434 "%s, which is reserved exclusively for mirroring",
1435 ctx->xbridge->name, in_xbundle->name);
1437 xlate_report(ctx, "input port is mirror output port, dropping");
1442 vid = vlan_tci_to_vid(flow->vlan_tci);
1443 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1444 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
1447 vlan = input_vid_to_vlan(in_xbundle, vid);
1449 /* Check other admissibility requirements. */
1450 if (in_port && !is_admissible(ctx, in_port, vlan)) {
1454 /* Learn source MAC. */
1455 if (ctx->xin->may_learn) {
1456 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
1459 /* Determine output bundle. */
1460 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
1461 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
1462 mac_port = mac ? mac->port.p : NULL;
1463 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
1466 struct xbundle *mac_xbundle = xbundle_lookup(mac_port);
1467 if (mac_xbundle && mac_xbundle != in_xbundle) {
1468 xlate_report(ctx, "forwarding to learned port");
1469 output_normal(ctx, mac_xbundle, vlan);
1470 } else if (!mac_xbundle) {
1471 xlate_report(ctx, "learned port is unknown, dropping");
1473 xlate_report(ctx, "learned port is input port, dropping");
1476 struct xbundle *xbundle;
1478 xlate_report(ctx, "no learned MAC for destination, flooding");
1479 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1480 if (xbundle != in_xbundle
1481 && xbundle_includes_vlan(xbundle, vlan)
1482 && xbundle->floodable
1483 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1484 output_normal(ctx, xbundle, vlan);
1487 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1491 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1492 * the number of packets out of UINT32_MAX to sample. The given
1493 * cookie is passed back in the callback for each sampled packet.
1496 compose_sample_action(const struct xbridge *xbridge,
1497 struct ofpbuf *odp_actions,
1498 const struct flow *flow,
1499 const uint32_t probability,
1500 const union user_action_cookie *cookie,
1501 const size_t cookie_size)
1503 size_t sample_offset, actions_offset;
1504 odp_port_t odp_port;
1508 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
1510 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
1512 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
1514 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
1515 pid = dpif_port_get_pid(xbridge->dpif, odp_port, 0);
1516 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size, odp_actions);
1518 nl_msg_end_nested(odp_actions, actions_offset);
1519 nl_msg_end_nested(odp_actions, sample_offset);
1520 return cookie_offset;
1524 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
1525 odp_port_t odp_port, unsigned int n_outputs,
1526 union user_action_cookie *cookie)
1530 cookie->type = USER_ACTION_COOKIE_SFLOW;
1531 cookie->sflow.vlan_tci = vlan_tci;
1533 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1534 * port information") for the interpretation of cookie->output. */
1535 switch (n_outputs) {
1537 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1538 cookie->sflow.output = 0x40000000 | 256;
1542 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
1544 cookie->sflow.output = ifindex;
1549 /* 0x80000000 means "multiple output ports. */
1550 cookie->sflow.output = 0x80000000 | n_outputs;
1555 /* Compose SAMPLE action for sFlow bridge sampling. */
1557 compose_sflow_action(const struct xbridge *xbridge,
1558 struct ofpbuf *odp_actions,
1559 const struct flow *flow,
1560 odp_port_t odp_port)
1562 uint32_t probability;
1563 union user_action_cookie cookie;
1565 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
1569 probability = dpif_sflow_get_probability(xbridge->sflow);
1570 compose_sflow_cookie(xbridge, htons(0), odp_port,
1571 odp_port == ODPP_NONE ? 0 : 1, &cookie);
1573 return compose_sample_action(xbridge, odp_actions, flow, probability,
1574 &cookie, sizeof cookie.sflow);
1578 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
1579 uint32_t obs_domain_id, uint32_t obs_point_id,
1580 union user_action_cookie *cookie)
1582 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
1583 cookie->flow_sample.probability = probability;
1584 cookie->flow_sample.collector_set_id = collector_set_id;
1585 cookie->flow_sample.obs_domain_id = obs_domain_id;
1586 cookie->flow_sample.obs_point_id = obs_point_id;
1590 compose_ipfix_cookie(union user_action_cookie *cookie)
1592 cookie->type = USER_ACTION_COOKIE_IPFIX;
1595 /* Compose SAMPLE action for IPFIX bridge sampling. */
1597 compose_ipfix_action(const struct xbridge *xbridge,
1598 struct ofpbuf *odp_actions,
1599 const struct flow *flow)
1601 uint32_t probability;
1602 union user_action_cookie cookie;
1604 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
1608 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
1609 compose_ipfix_cookie(&cookie);
1611 compose_sample_action(xbridge, odp_actions, flow, probability,
1612 &cookie, sizeof cookie.ipfix);
1615 /* SAMPLE action for sFlow must be first action in any given list of
1616 * actions. At this point we do not have all information required to
1617 * build it. So try to build sample action as complete as possible. */
1619 add_sflow_action(struct xlate_ctx *ctx)
1621 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
1622 &ctx->xout->odp_actions,
1623 &ctx->xin->flow, ODPP_NONE);
1624 ctx->sflow_odp_port = 0;
1625 ctx->sflow_n_outputs = 0;
1628 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
1629 * of actions, eventually after the SAMPLE action for sFlow. */
1631 add_ipfix_action(struct xlate_ctx *ctx)
1633 compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions,
1637 /* Fix SAMPLE action according to data collected while composing ODP actions.
1638 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
1639 * USERSPACE action's user-cookie which is required for sflow. */
1641 fix_sflow_action(struct xlate_ctx *ctx)
1643 const struct flow *base = &ctx->base_flow;
1644 union user_action_cookie *cookie;
1646 if (!ctx->user_cookie_offset) {
1650 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
1651 sizeof cookie->sflow);
1652 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
1654 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
1655 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
1658 static enum slow_path_reason
1659 process_special(struct xlate_ctx *ctx, const struct flow *flow,
1660 const struct xport *xport, const struct ofpbuf *packet)
1662 struct flow_wildcards *wc = &ctx->xout->wc;
1663 const struct xbridge *xbridge = ctx->xbridge;
1667 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
1669 cfm_process_heartbeat(xport->cfm, packet);
1672 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
1674 bfd_process_packet(xport->bfd, flow, packet);
1675 /* If POLL received, immediately sends FINAL back. */
1676 if (bfd_should_send_packet(xport->bfd)) {
1678 ofproto_dpif_monitor_port_send_soon(xport->ofport);
1680 ofproto_dpif_monitor_port_send_soon_safe(xport->ofport);
1685 } else if (xport->xbundle && xport->xbundle->lacp
1686 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1688 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
1691 } else if (xbridge->stp && stp_should_process_flow(flow, wc)) {
1693 stp_process_packet(xport, packet);
1702 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
1705 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1706 struct flow_wildcards *wc = &ctx->xout->wc;
1707 struct flow *flow = &ctx->xin->flow;
1708 ovs_be16 flow_vlan_tci;
1709 uint32_t flow_pkt_mark;
1710 uint8_t flow_nw_tos;
1711 odp_port_t out_port, odp_port;
1714 /* If 'struct flow' gets additional metadata, we'll need to zero it out
1715 * before traversing a patch port. */
1716 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 25);
1719 xlate_report(ctx, "Nonexistent output port");
1721 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
1722 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
1724 } else if (check_stp) {
1725 if (eth_addr_equals(ctx->base_flow.dl_dst, eth_addr_stp)) {
1726 if (!xport_stp_listen_state(xport)) {
1727 xlate_report(ctx, "STP not in listening state, "
1728 "skipping bpdu output");
1731 } else if (!xport_stp_forward_state(xport)) {
1732 xlate_report(ctx, "STP not in forwarding state, "
1738 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
1739 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
1744 const struct xport *peer = xport->peer;
1745 struct flow old_flow = ctx->xin->flow;
1746 enum slow_path_reason special;
1748 ctx->xbridge = peer->xbridge;
1749 flow->in_port.ofp_port = peer->ofp_port;
1750 flow->metadata = htonll(0);
1751 memset(&flow->tunnel, 0, sizeof flow->tunnel);
1752 memset(flow->regs, 0, sizeof flow->regs);
1754 special = process_special(ctx, &ctx->xin->flow, peer,
1757 ctx->xout->slow |= special;
1758 } else if (may_receive(peer, ctx)) {
1759 if (xport_stp_forward_state(peer)) {
1760 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
1762 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
1763 * learning action look at the packet, then drop it. */
1764 struct flow old_base_flow = ctx->base_flow;
1765 size_t old_size = ofpbuf_size(&ctx->xout->odp_actions);
1766 mirror_mask_t old_mirrors = ctx->xout->mirrors;
1767 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
1768 ctx->xout->mirrors = old_mirrors;
1769 ctx->base_flow = old_base_flow;
1770 ofpbuf_set_size(&ctx->xout->odp_actions, old_size);
1774 ctx->xin->flow = old_flow;
1775 ctx->xbridge = xport->xbridge;
1777 if (ctx->xin->resubmit_stats) {
1778 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1779 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
1781 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
1788 flow_vlan_tci = flow->vlan_tci;
1789 flow_pkt_mark = flow->pkt_mark;
1790 flow_nw_tos = flow->nw_tos;
1792 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
1793 wc->masks.nw_tos |= IP_ECN_MASK;
1794 flow->nw_tos &= ~IP_DSCP_MASK;
1795 flow->nw_tos |= dscp;
1798 if (xport->is_tunnel) {
1799 /* Save tunnel metadata so that changes made due to
1800 * the Logical (tunnel) Port are not visible for any further
1801 * matches, while explicit set actions on tunnel metadata are.
1803 struct flow_tnl flow_tnl = flow->tunnel;
1804 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
1805 if (odp_port == ODPP_NONE) {
1806 xlate_report(ctx, "Tunneling decided against output");
1807 goto out; /* restore flow_nw_tos */
1809 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
1810 xlate_report(ctx, "Not tunneling to our own address");
1811 goto out; /* restore flow_nw_tos */
1813 if (ctx->xin->resubmit_stats) {
1814 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1816 out_port = odp_port;
1817 commit_odp_tunnel_action(flow, &ctx->base_flow,
1818 &ctx->xout->odp_actions);
1819 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
1821 odp_port = xport->odp_port;
1822 out_port = odp_port;
1823 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
1824 ofp_port_t vlandev_port;
1826 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1827 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
1828 ofp_port, flow->vlan_tci);
1829 if (vlandev_port != ofp_port) {
1830 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
1831 flow->vlan_tci = htons(0);
1836 if (out_port != ODPP_NONE) {
1837 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
1838 &ctx->xout->odp_actions,
1841 if (ctx->xout->use_recirc) {
1842 struct ovs_action_recirc *act_recirc;
1843 struct xlate_recirc *xr = &ctx->xout->recirc;
1845 act_recirc = nl_msg_put_unspec_uninit(&ctx->xout->odp_actions,
1846 OVS_ACTION_ATTR_RECIRC, sizeof *act_recirc);
1847 act_recirc->recirc_id = xr->recirc_id;
1848 act_recirc->hash_alg = xr->hash_alg;
1849 act_recirc->hash_bias = xr->hash_bias;
1851 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
1855 ctx->sflow_odp_port = odp_port;
1856 ctx->sflow_n_outputs++;
1857 ctx->xout->nf_output_iface = ofp_port;
1862 flow->vlan_tci = flow_vlan_tci;
1863 flow->pkt_mark = flow_pkt_mark;
1864 flow->nw_tos = flow_nw_tos;
1868 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
1870 compose_output_action__(ctx, ofp_port, true);
1874 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
1876 struct rule_dpif *old_rule = ctx->rule;
1877 struct rule_actions *actions;
1879 if (ctx->xin->resubmit_stats) {
1880 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
1886 actions = rule_dpif_get_actions(rule);
1887 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
1888 ctx->rule = old_rule;
1893 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
1895 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1897 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
1898 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
1899 MAX_RESUBMIT_RECURSION);
1900 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
1901 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
1902 } else if (ofpbuf_size(&ctx->xout->odp_actions) > UINT16_MAX) {
1903 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
1904 } else if (ofpbuf_size(&ctx->stack) >= 65536) {
1905 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
1914 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
1915 bool may_packet_in, bool honor_table_miss)
1917 if (xlate_resubmit_resource_check(ctx)) {
1918 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
1919 bool skip_wildcards = ctx->xin->skip_wildcards;
1920 uint8_t old_table_id = ctx->table_id;
1921 struct rule_dpif *rule;
1922 enum rule_dpif_lookup_verdict verdict;
1923 enum ofputil_port_config config = 0;
1925 ctx->table_id = table_id;
1927 /* Look up a flow with 'in_port' as the input port. Then restore the
1928 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
1929 * have surprising behavior). */
1930 ctx->xin->flow.in_port.ofp_port = in_port;
1931 verdict = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
1934 ? &ctx->xout->wc : NULL,
1936 &ctx->table_id, &rule);
1937 ctx->xin->flow.in_port.ofp_port = old_in_port;
1939 if (ctx->xin->resubmit_hook) {
1940 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
1944 case RULE_DPIF_LOOKUP_VERDICT_MATCH:
1946 case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER:
1947 if (may_packet_in) {
1948 struct xport *xport;
1950 xport = get_ofp_port(ctx->xbridge,
1951 ctx->xin->flow.in_port.ofp_port);
1952 config = xport ? xport->config : 0;
1955 /* Fall through to drop */
1956 case RULE_DPIF_LOOKUP_VERDICT_DROP:
1957 config = OFPUTIL_PC_NO_PACKET_IN;
1959 case RULE_DPIF_LOOKUP_VERDICT_DEFAULT:
1960 if (!ofproto_dpif_wants_packet_in_on_miss(ctx->xbridge->ofproto)) {
1961 config = OFPUTIL_PC_NO_PACKET_IN;
1968 choose_miss_rule(config, ctx->xbridge->miss_rule,
1969 ctx->xbridge->no_packet_in_rule, &rule);
1973 xlate_recursively(ctx, rule);
1974 rule_dpif_unref(rule);
1977 ctx->table_id = old_table_id;
1985 xlate_group_bucket(struct xlate_ctx *ctx, const struct ofputil_bucket *bucket)
1987 uint64_t action_list_stub[1024 / 8];
1988 struct ofpbuf action_list, action_set;
1990 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
1991 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
1993 ofpacts_execute_action_set(&action_list, &action_set);
1995 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
1998 ofpbuf_uninit(&action_set);
1999 ofpbuf_uninit(&action_list);
2003 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
2005 const struct ofputil_bucket *bucket;
2006 const struct list *buckets;
2007 struct flow old_flow = ctx->xin->flow;
2009 group_dpif_get_buckets(group, &buckets);
2011 LIST_FOR_EACH (bucket, list_node, buckets) {
2012 xlate_group_bucket(ctx, bucket);
2013 /* Roll back flow to previous state.
2014 * This is equivalent to cloning the packet for each bucket.
2016 * As a side effect any subsequently applied actions will
2017 * also effectively be applied to a clone of the packet taken
2018 * just before applying the all or indirect group. */
2019 ctx->xin->flow = old_flow;
2024 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
2026 const struct ofputil_bucket *bucket;
2028 bucket = group_first_live_bucket(ctx, group, 0);
2030 xlate_group_bucket(ctx, bucket);
2035 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
2037 struct flow_wildcards *wc = &ctx->xout->wc;
2038 const struct ofputil_bucket *bucket;
2041 basis = hash_mac(ctx->xin->flow.dl_dst, 0, 0);
2042 bucket = group_best_live_bucket(ctx, group, basis);
2044 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2045 xlate_group_bucket(ctx, bucket);
2050 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
2052 ctx->in_group = true;
2054 switch (group_dpif_get_type(group)) {
2056 case OFPGT11_INDIRECT:
2057 xlate_all_group(ctx, group);
2059 case OFPGT11_SELECT:
2060 xlate_select_group(ctx, group);
2063 xlate_ff_group(ctx, group);
2068 group_dpif_release(group);
2070 ctx->in_group = false;
2074 xlate_group_resource_check(struct xlate_ctx *ctx)
2076 if (!xlate_resubmit_resource_check(ctx)) {
2078 } else if (ctx->in_group) {
2079 /* Prevent nested translation of OpenFlow groups.
2081 * OpenFlow allows this restriction. We enforce this restriction only
2082 * because, with the current architecture, we would otherwise have to
2083 * take a possibly recursive read lock on the ofgroup rwlock, which is
2084 * unsafe given that POSIX allows taking a read lock to block if there
2085 * is a thread blocked on taking the write lock. Other solutions
2086 * without this restriction are also possible, but seem unwarranted
2087 * given the current limited use of groups. */
2088 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2090 VLOG_ERR_RL(&rl, "cannot recursively translate OpenFlow group");
2098 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
2100 if (xlate_group_resource_check(ctx)) {
2101 struct group_dpif *group;
2104 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
2106 xlate_group_action__(ctx, group);
2116 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
2117 const struct ofpact_resubmit *resubmit)
2121 bool may_packet_in = false;
2122 bool honor_table_miss = false;
2124 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
2125 /* Still allow missed packets to be sent to the controller
2126 * if resubmitting from an internal table. */
2127 may_packet_in = true;
2128 honor_table_miss = true;
2131 in_port = resubmit->in_port;
2132 if (in_port == OFPP_IN_PORT) {
2133 in_port = ctx->xin->flow.in_port.ofp_port;
2136 table_id = resubmit->table_id;
2137 if (table_id == 255) {
2138 table_id = ctx->table_id;
2141 xlate_table_action(ctx, in_port, table_id, may_packet_in,
2146 flood_packets(struct xlate_ctx *ctx, bool all)
2148 const struct xport *xport;
2150 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
2151 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
2156 compose_output_action__(ctx, xport->ofp_port, false);
2157 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
2158 compose_output_action(ctx, xport->ofp_port);
2162 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2166 execute_controller_action(struct xlate_ctx *ctx, int len,
2167 enum ofp_packet_in_reason reason,
2168 uint16_t controller_id)
2170 struct ofproto_packet_in *pin;
2171 struct ofpbuf *packet;
2172 struct pkt_metadata md = PKT_METADATA_INITIALIZER(0);
2174 ctx->xout->slow |= SLOW_CONTROLLER;
2175 if (!ctx->xin->packet) {
2179 packet = ofpbuf_clone(ctx->xin->packet);
2181 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2182 &ctx->xout->odp_actions,
2185 odp_execute_actions(NULL, packet, false, &md,
2186 ofpbuf_data(&ctx->xout->odp_actions),
2187 ofpbuf_size(&ctx->xout->odp_actions), NULL);
2189 pin = xmalloc(sizeof *pin);
2190 pin->up.packet_len = ofpbuf_size(packet);
2191 pin->up.packet = ofpbuf_steal_data(packet);
2192 pin->up.reason = reason;
2193 pin->up.table_id = ctx->table_id;
2194 pin->up.cookie = (ctx->rule
2195 ? rule_dpif_get_flow_cookie(ctx->rule)
2198 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
2200 pin->controller_id = controller_id;
2201 pin->send_len = len;
2202 /* If a rule is a table-miss rule then this is
2203 * a table-miss handled by a table-miss rule.
2205 * Else, if rule is internal and has a controller action,
2206 * the later being implied by the rule being processed here,
2207 * then this is a table-miss handled without a table-miss rule.
2209 * Otherwise this is not a table-miss. */
2210 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
2212 if (rule_dpif_is_table_miss(ctx->rule)) {
2213 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
2214 } else if (rule_dpif_is_internal(ctx->rule)) {
2215 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
2218 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
2219 ofpbuf_delete(packet);
2223 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
2225 struct flow_wildcards *wc = &ctx->xout->wc;
2226 struct flow *flow = &ctx->xin->flow;
2229 ovs_assert(eth_type_mpls(mpls->ethertype));
2231 n = flow_count_mpls_labels(flow, wc);
2233 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
2234 &ctx->xout->odp_actions,
2236 } else if (n >= FLOW_MAX_MPLS_LABELS) {
2237 if (ctx->xin->packet != NULL) {
2238 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2239 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
2240 "MPLS push action can't be performed as it would "
2241 "have more MPLS LSEs than the %d supported.",
2242 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
2246 } else if (n >= ctx->xbridge->max_mpls_depth) {
2247 COVERAGE_INC(xlate_actions_mpls_overflow);
2248 ctx->xout->slow |= SLOW_ACTION;
2251 flow_push_mpls(flow, n, mpls->ethertype, wc);
2255 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
2257 struct flow_wildcards *wc = &ctx->xout->wc;
2258 struct flow *flow = &ctx->xin->flow;
2259 int n = flow_count_mpls_labels(flow, wc);
2261 if (!flow_pop_mpls(flow, n, eth_type, wc) && n >= FLOW_MAX_MPLS_LABELS) {
2262 if (ctx->xin->packet != NULL) {
2263 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2264 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
2265 "MPLS pop action can't be performed as it has "
2266 "more MPLS LSEs than the %d supported.",
2267 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
2270 ofpbuf_clear(&ctx->xout->odp_actions);
2275 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
2277 struct flow *flow = &ctx->xin->flow;
2279 if (!is_ip_any(flow)) {
2283 ctx->xout->wc.masks.nw_ttl = 0xff;
2284 if (flow->nw_ttl > 1) {
2290 for (i = 0; i < ids->n_controllers; i++) {
2291 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
2295 /* Stop processing for current table. */
2301 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
2303 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2304 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
2305 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
2310 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
2312 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2313 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
2314 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
2319 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
2321 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2322 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
2323 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
2328 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
2330 struct flow *flow = &ctx->xin->flow;
2331 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
2332 struct flow_wildcards *wc = &ctx->xout->wc;
2334 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2335 if (eth_type_mpls(flow->dl_type)) {
2338 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
2341 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
2343 /* Stop processing for current table. */
2352 xlate_output_action(struct xlate_ctx *ctx,
2353 ofp_port_t port, uint16_t max_len, bool may_packet_in)
2355 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
2357 ctx->xout->nf_output_iface = NF_OUT_DROP;
2361 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
2364 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2365 0, may_packet_in, true);
2371 flood_packets(ctx, false);
2374 flood_packets(ctx, true);
2376 case OFPP_CONTROLLER:
2377 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
2383 if (port != ctx->xin->flow.in_port.ofp_port) {
2384 compose_output_action(ctx, port);
2386 xlate_report(ctx, "skipping output to input port");
2391 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2392 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2393 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2394 ctx->xout->nf_output_iface = prev_nf_output_iface;
2395 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2396 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2397 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2402 xlate_output_reg_action(struct xlate_ctx *ctx,
2403 const struct ofpact_output_reg *or)
2405 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
2406 if (port <= UINT16_MAX) {
2407 union mf_subvalue value;
2409 memset(&value, 0xff, sizeof value);
2410 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
2411 xlate_output_action(ctx, u16_to_ofp(port),
2412 or->max_len, false);
2417 xlate_enqueue_action(struct xlate_ctx *ctx,
2418 const struct ofpact_enqueue *enqueue)
2420 ofp_port_t ofp_port = enqueue->port;
2421 uint32_t queue_id = enqueue->queue;
2422 uint32_t flow_priority, priority;
2425 /* Translate queue to priority. */
2426 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
2428 /* Fall back to ordinary output action. */
2429 xlate_output_action(ctx, enqueue->port, 0, false);
2433 /* Check output port. */
2434 if (ofp_port == OFPP_IN_PORT) {
2435 ofp_port = ctx->xin->flow.in_port.ofp_port;
2436 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
2440 /* Add datapath actions. */
2441 flow_priority = ctx->xin->flow.skb_priority;
2442 ctx->xin->flow.skb_priority = priority;
2443 compose_output_action(ctx, ofp_port);
2444 ctx->xin->flow.skb_priority = flow_priority;
2446 /* Update NetFlow output port. */
2447 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2448 ctx->xout->nf_output_iface = ofp_port;
2449 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2450 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2455 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
2457 uint32_t skb_priority;
2459 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
2460 ctx->xin->flow.skb_priority = skb_priority;
2462 /* Couldn't translate queue to a priority. Nothing to do. A warning
2463 * has already been logged. */
2468 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
2470 const struct xbridge *xbridge = xbridge_;
2481 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
2484 port = get_ofp_port(xbridge, ofp_port);
2485 return port ? port->may_enable : false;
2490 xlate_bundle_action(struct xlate_ctx *ctx,
2491 const struct ofpact_bundle *bundle)
2495 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
2497 CONST_CAST(struct xbridge *, ctx->xbridge));
2498 if (bundle->dst.field) {
2499 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
2502 xlate_output_action(ctx, port, 0, false);
2507 xlate_learn_action(struct xlate_ctx *ctx,
2508 const struct ofpact_learn *learn)
2510 uint64_t ofpacts_stub[1024 / 8];
2511 struct ofputil_flow_mod fm;
2512 struct ofpbuf ofpacts;
2514 ctx->xout->has_learn = true;
2516 learn_mask(learn, &ctx->xout->wc);
2518 if (!ctx->xin->may_learn) {
2522 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2523 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
2524 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, &fm);
2525 ofpbuf_uninit(&ofpacts);
2529 xlate_fin_timeout(struct xlate_ctx *ctx,
2530 const struct ofpact_fin_timeout *oft)
2532 if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
2533 rule_dpif_reduce_timeouts(ctx->rule, oft->fin_idle_timeout,
2534 oft->fin_hard_timeout);
2539 xlate_sample_action(struct xlate_ctx *ctx,
2540 const struct ofpact_sample *os)
2542 union user_action_cookie cookie;
2543 /* Scale the probability from 16-bit to 32-bit while representing
2544 * the same percentage. */
2545 uint32_t probability = (os->probability << 16) | os->probability;
2547 if (!ctx->xbridge->variable_length_userdata) {
2548 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2550 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
2551 "lacks support (needs Linux 3.10+ or kernel module from "
2556 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2557 &ctx->xout->odp_actions,
2560 compose_flow_sample_cookie(os->probability, os->collector_set_id,
2561 os->obs_domain_id, os->obs_point_id, &cookie);
2562 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
2563 probability, &cookie, sizeof cookie.flow_sample);
2567 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
2569 if (xport->config & (eth_addr_equals(ctx->xin->flow.dl_dst, eth_addr_stp)
2570 ? OFPUTIL_PC_NO_RECV_STP
2571 : OFPUTIL_PC_NO_RECV)) {
2575 /* Only drop packets here if both forwarding and learning are
2576 * disabled. If just learning is enabled, we need to have
2577 * OFPP_NORMAL and the learning action have a look at the packet
2578 * before we can drop it. */
2579 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
2587 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
2589 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2590 ofpbuf_put(&ctx->action_set, on->actions, ofpact_nest_get_action_len(on));
2591 ofpact_pad(&ctx->action_set);
2595 xlate_action_set(struct xlate_ctx *ctx)
2597 uint64_t action_list_stub[1024 / 64];
2598 struct ofpbuf action_list;
2600 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2601 ofpacts_execute_action_set(&action_list, &ctx->action_set);
2602 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
2603 ofpbuf_uninit(&action_list);
2607 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
2608 struct xlate_ctx *ctx)
2610 struct flow_wildcards *wc = &ctx->xout->wc;
2611 struct flow *flow = &ctx->xin->flow;
2612 const struct ofpact *a;
2614 /* dl_type already in the mask, not set below. */
2616 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2617 struct ofpact_controller *controller;
2618 const struct ofpact_metadata *metadata;
2619 const struct ofpact_set_field *set_field;
2620 const struct mf_field *mf;
2628 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
2629 ofpact_get_OUTPUT(a)->max_len, true);
2633 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
2638 case OFPACT_CONTROLLER:
2639 controller = ofpact_get_CONTROLLER(a);
2640 execute_controller_action(ctx, controller->max_len,
2642 controller->controller_id);
2645 case OFPACT_ENQUEUE:
2646 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
2649 case OFPACT_SET_VLAN_VID:
2650 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2651 if (flow->vlan_tci & htons(VLAN_CFI) ||
2652 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2653 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
2654 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
2659 case OFPACT_SET_VLAN_PCP:
2660 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
2661 if (flow->vlan_tci & htons(VLAN_CFI) ||
2662 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2663 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
2664 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
2665 << VLAN_PCP_SHIFT) | VLAN_CFI);
2669 case OFPACT_STRIP_VLAN:
2670 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2671 flow->vlan_tci = htons(0);
2674 case OFPACT_PUSH_VLAN:
2675 /* XXX 802.1AD(QinQ) */
2676 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2677 flow->vlan_tci = htons(VLAN_CFI);
2680 case OFPACT_SET_ETH_SRC:
2681 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2682 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2685 case OFPACT_SET_ETH_DST:
2686 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2687 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2690 case OFPACT_SET_IPV4_SRC:
2691 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2692 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2693 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2697 case OFPACT_SET_IPV4_DST:
2698 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2699 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2700 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
2704 case OFPACT_SET_IP_DSCP:
2705 if (is_ip_any(flow)) {
2706 wc->masks.nw_tos |= IP_DSCP_MASK;
2707 flow->nw_tos &= ~IP_DSCP_MASK;
2708 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
2712 case OFPACT_SET_IP_ECN:
2713 if (is_ip_any(flow)) {
2714 wc->masks.nw_tos |= IP_ECN_MASK;
2715 flow->nw_tos &= ~IP_ECN_MASK;
2716 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
2720 case OFPACT_SET_IP_TTL:
2721 if (is_ip_any(flow)) {
2722 wc->masks.nw_ttl = 0xff;
2723 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
2727 case OFPACT_SET_L4_SRC_PORT:
2728 if (is_ip_any(flow)) {
2729 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2730 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2731 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2735 case OFPACT_SET_L4_DST_PORT:
2736 if (is_ip_any(flow)) {
2737 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2738 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2739 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2743 case OFPACT_RESUBMIT:
2744 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
2747 case OFPACT_SET_TUNNEL:
2748 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2751 case OFPACT_SET_QUEUE:
2752 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2755 case OFPACT_POP_QUEUE:
2756 flow->skb_priority = ctx->orig_skb_priority;
2759 case OFPACT_REG_MOVE:
2760 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
2763 case OFPACT_REG_LOAD:
2764 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow, wc);
2767 case OFPACT_SET_FIELD:
2768 set_field = ofpact_get_SET_FIELD(a);
2769 mf = set_field->field;
2770 mf_mask_field_and_prereqs(mf, &wc->masks);
2772 /* Set field action only ever overwrites packet's outermost
2773 * applicable header fields. Do nothing if no header exists. */
2774 if ((mf->id != MFF_VLAN_VID || flow->vlan_tci & htons(VLAN_CFI))
2775 && ((mf->id != MFF_MPLS_LABEL && mf->id != MFF_MPLS_TC)
2776 || eth_type_mpls(flow->dl_type))) {
2777 mf_set_flow_value(mf, &set_field->value, flow);
2781 case OFPACT_STACK_PUSH:
2782 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
2786 case OFPACT_STACK_POP:
2787 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
2791 case OFPACT_PUSH_MPLS:
2792 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
2795 case OFPACT_POP_MPLS:
2796 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
2799 case OFPACT_SET_MPLS_LABEL:
2800 compose_set_mpls_label_action(
2801 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
2804 case OFPACT_SET_MPLS_TC:
2805 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
2808 case OFPACT_SET_MPLS_TTL:
2809 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
2812 case OFPACT_DEC_MPLS_TTL:
2813 if (compose_dec_mpls_ttl_action(ctx)) {
2818 case OFPACT_DEC_TTL:
2819 wc->masks.nw_ttl = 0xff;
2820 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
2826 /* Nothing to do. */
2829 case OFPACT_MULTIPATH:
2830 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
2834 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
2837 case OFPACT_OUTPUT_REG:
2838 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
2842 xlate_learn_action(ctx, ofpact_get_LEARN(a));
2849 case OFPACT_FIN_TIMEOUT:
2850 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2851 ctx->xout->has_fin_timeout = true;
2852 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
2855 case OFPACT_CLEAR_ACTIONS:
2856 ofpbuf_clear(&ctx->action_set);
2859 case OFPACT_WRITE_ACTIONS:
2860 xlate_write_actions(ctx, a);
2863 case OFPACT_WRITE_METADATA:
2864 metadata = ofpact_get_WRITE_METADATA(a);
2865 flow->metadata &= ~metadata->mask;
2866 flow->metadata |= metadata->metadata & metadata->mask;
2870 /* Not implemented yet. */
2873 case OFPACT_GOTO_TABLE: {
2874 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
2876 ovs_assert(ctx->table_id < ogt->table_id);
2877 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2878 ogt->table_id, true, true);
2883 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
2890 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
2891 const struct flow *flow, struct rule_dpif *rule,
2892 uint16_t tcp_flags, const struct ofpbuf *packet)
2894 xin->ofproto = ofproto;
2896 xin->packet = packet;
2897 xin->may_learn = packet != NULL;
2899 xin->ofpacts = NULL;
2900 xin->ofpacts_len = 0;
2901 xin->tcp_flags = tcp_flags;
2902 xin->resubmit_hook = NULL;
2903 xin->report_hook = NULL;
2904 xin->resubmit_stats = NULL;
2905 xin->skip_wildcards = false;
2909 xlate_out_uninit(struct xlate_out *xout)
2912 ofpbuf_uninit(&xout->odp_actions);
2916 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
2917 * into datapath actions, using 'ctx', and discards the datapath actions. */
2919 xlate_actions_for_side_effects(struct xlate_in *xin)
2921 struct xlate_out xout;
2923 xlate_actions(xin, &xout);
2924 xlate_out_uninit(&xout);
2928 xlate_report(struct xlate_ctx *ctx, const char *s)
2930 if (ctx->xin->report_hook) {
2931 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
2936 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
2939 dst->slow = src->slow;
2940 dst->has_learn = src->has_learn;
2941 dst->has_normal = src->has_normal;
2942 dst->has_fin_timeout = src->has_fin_timeout;
2943 dst->nf_output_iface = src->nf_output_iface;
2944 dst->mirrors = src->mirrors;
2946 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
2947 sizeof dst->odp_actions_stub);
2948 ofpbuf_put(&dst->odp_actions, ofpbuf_data(&src->odp_actions),
2949 ofpbuf_size(&src->odp_actions));
2952 static struct skb_priority_to_dscp *
2953 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
2955 struct skb_priority_to_dscp *pdscp;
2958 hash = hash_int(skb_priority, 0);
2959 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
2960 if (pdscp->skb_priority == skb_priority) {
2968 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
2971 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
2972 *dscp = pdscp ? pdscp->dscp : 0;
2973 return pdscp != NULL;
2977 clear_skb_priorities(struct xport *xport)
2979 struct skb_priority_to_dscp *pdscp, *next;
2981 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
2982 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
2988 actions_output_to_local_port(const struct xlate_ctx *ctx)
2990 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
2991 const struct nlattr *a;
2994 NL_ATTR_FOR_EACH_UNSAFE (a, left, ofpbuf_data(&ctx->xout->odp_actions),
2995 ofpbuf_size(&ctx->xout->odp_actions)) {
2996 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
2997 && nl_attr_get_odp_port(a) == local_odp_port) {
3004 /* Thread safe call to xlate_actions__(). */
3006 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
3007 OVS_EXCLUDED(xlate_rwlock)
3009 ovs_rwlock_rdlock(&xlate_rwlock);
3010 xlate_actions__(xin, xout);
3011 ovs_rwlock_unlock(&xlate_rwlock);
3014 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
3015 * into datapath actions in 'odp_actions', using 'ctx'.
3017 * The caller must take responsibility for eventually freeing 'xout', with
3018 * xlate_out_uninit(). */
3020 xlate_actions__(struct xlate_in *xin, struct xlate_out *xout)
3021 OVS_REQ_RDLOCK(xlate_rwlock)
3023 struct flow_wildcards *wc = &xout->wc;
3024 struct flow *flow = &xin->flow;
3025 struct rule_dpif *rule = NULL;
3027 struct rule_actions *actions = NULL;
3028 enum slow_path_reason special;
3029 const struct ofpact *ofpacts;
3030 struct xport *in_port;
3031 struct flow orig_flow;
3032 struct xlate_ctx ctx;
3037 COVERAGE_INC(xlate_actions);
3039 /* Flow initialization rules:
3040 * - 'base_flow' must match the kernel's view of the packet at the
3041 * time that action processing starts. 'flow' represents any
3042 * transformations we wish to make through actions.
3043 * - By default 'base_flow' and 'flow' are the same since the input
3044 * packet matches the output before any actions are applied.
3045 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
3046 * of the received packet as seen by the kernel. If we later output
3047 * to another device without any modifications this will cause us to
3048 * insert a new tag since the original one was stripped off by the
3050 * - Tunnel metadata as received is retained in 'flow'. This allows
3051 * tunnel metadata matching also in later tables.
3052 * Since a kernel action for setting the tunnel metadata will only be
3053 * generated with actual tunnel output, changing the tunnel metadata
3054 * values in 'flow' (such as tun_id) will only have effect with a later
3055 * tunnel output action.
3056 * - Tunnel 'base_flow' is completely cleared since that is what the
3057 * kernel does. If we wish to maintain the original values an action
3058 * needs to be generated. */
3063 ctx.xout->has_learn = false;
3064 ctx.xout->has_normal = false;
3065 ctx.xout->has_fin_timeout = false;
3066 ctx.xout->nf_output_iface = NF_OUT_DROP;
3067 ctx.xout->mirrors = 0;
3068 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
3069 sizeof ctx.xout->odp_actions_stub);
3070 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
3072 ctx.xbridge = xbridge_lookup(xin->ofproto);
3077 ctx.rule = xin->rule;
3079 ctx.base_flow = *flow;
3080 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
3081 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
3083 flow_wildcards_init_catchall(wc);
3084 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
3085 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3086 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3087 if (is_ip_any(flow)) {
3088 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3090 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
3092 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
3093 if (ctx.xbridge->netflow) {
3094 netflow_mask_wc(flow, wc);
3099 ctx.in_group = false;
3100 ctx.orig_skb_priority = flow->skb_priority;
3104 if (!xin->ofpacts && !ctx.rule) {
3105 ctx.table_id = rule_dpif_lookup(ctx.xbridge->ofproto, flow,
3106 !xin->skip_wildcards ? wc : NULL,
3108 if (ctx.xin->resubmit_stats) {
3109 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
3113 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
3114 xout->use_recirc = false;
3117 ofpacts = xin->ofpacts;
3118 ofpacts_len = xin->ofpacts_len;
3119 } else if (ctx.rule) {
3120 actions = rule_dpif_get_actions(ctx.rule);
3121 ofpacts = actions->ofpacts;
3122 ofpacts_len = actions->ofpacts_len;
3127 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
3128 ofpbuf_use_stub(&ctx.action_set,
3129 ctx.action_set_stub, sizeof ctx.action_set_stub);
3131 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3132 /* Do this conditionally because the copy is expensive enough that it
3133 * shows up in profiles. */
3137 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3138 switch (ctx.xbridge->frag) {
3139 case OFPC_FRAG_NORMAL:
3140 /* We must pretend that transport ports are unavailable. */
3141 flow->tp_src = ctx.base_flow.tp_src = htons(0);
3142 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
3145 case OFPC_FRAG_DROP:
3148 case OFPC_FRAG_REASM:
3151 case OFPC_FRAG_NX_MATCH:
3152 /* Nothing to do. */
3155 case OFPC_INVALID_TTL_TO_CONTROLLER:
3160 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
3161 if (in_port && in_port->is_tunnel && ctx.xin->resubmit_stats) {
3162 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
3164 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
3168 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
3170 ctx.xout->slow |= special;
3172 size_t sample_actions_len;
3174 if (flow->in_port.ofp_port
3175 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
3176 flow->in_port.ofp_port,
3178 ctx.base_flow.vlan_tci = 0;
3181 add_sflow_action(&ctx);
3182 add_ipfix_action(&ctx);
3183 sample_actions_len = ofpbuf_size(&ctx.xout->odp_actions);
3185 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
3186 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
3188 /* We've let OFPP_NORMAL and the learning action look at the
3189 * packet, so drop it now if forwarding is disabled. */
3190 if (in_port && !xport_stp_forward_state(in_port)) {
3191 ofpbuf_set_size(&ctx.xout->odp_actions, sample_actions_len);
3195 if (ofpbuf_size(&ctx.action_set)) {
3196 xlate_action_set(&ctx);
3199 if (ctx.xbridge->has_in_band
3200 && in_band_must_output_to_local_port(flow)
3201 && !actions_output_to_local_port(&ctx)) {
3202 compose_output_action(&ctx, OFPP_LOCAL);
3205 fix_sflow_action(&ctx);
3207 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3208 add_mirror_actions(&ctx, &orig_flow);
3212 if (nl_attr_oversized(ofpbuf_size(&ctx.xout->odp_actions))) {
3213 /* These datapath actions are too big for a Netlink attribute, so we
3214 * can't hand them to the kernel directly. dpif_execute() can execute
3215 * them one by one with help, so just mark the result as SLOW_ACTION to
3216 * prevent the flow from being installed. */
3217 COVERAGE_INC(xlate_actions_oversize);
3218 ctx.xout->slow |= SLOW_ACTION;
3221 if (ctx.xin->resubmit_stats) {
3222 mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
3223 ctx.xin->resubmit_stats->n_packets,
3224 ctx.xin->resubmit_stats->n_bytes);
3226 if (ctx.xbridge->netflow) {
3227 const struct ofpact *ofpacts;
3230 ofpacts_len = actions->ofpacts_len;
3231 ofpacts = actions->ofpacts;
3232 if (ofpacts_len == 0
3233 || ofpacts->type != OFPACT_CONTROLLER
3234 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
3235 /* Only update netflow if we don't have controller flow. We don't
3236 * report NetFlow expiration messages for such facets because they
3237 * are just part of the control logic for the network, not real
3239 netflow_flow_update(ctx.xbridge->netflow, flow,
3240 xout->nf_output_iface,
3241 ctx.xin->resubmit_stats);
3246 ofpbuf_uninit(&ctx.stack);
3247 ofpbuf_uninit(&ctx.action_set);
3249 /* Clear the metadata and register wildcard masks, because we won't
3250 * use non-header fields as part of the cache. */
3251 flow_wildcards_clear_non_packet_fields(wc);
3253 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow uses
3254 * the low 8 bits of the 16-bit tp_src and tp_dst members to represent
3255 * these fields. The datapath interface, on the other hand, represents
3256 * them with just 8 bits each. This means that if the high 8 bits of the
3257 * masks for these fields somehow become set, then they will get chopped
3258 * off by a round trip through the datapath, and revalidation will spot
3259 * that as an inconsistency and delete the flow. Avoid the problem here by
3260 * making sure that only the low 8 bits of either field can be unwildcarded
3264 wc->masks.tp_src &= htons(UINT8_MAX);
3265 wc->masks.tp_dst &= htons(UINT8_MAX);
3269 rule_dpif_unref(rule);
3272 /* Sends 'packet' out 'ofport'.
3273 * May modify 'packet'.
3274 * Returns 0 if successful, otherwise a positive errno value. */
3276 xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3278 struct xport *xport;
3279 struct ofpact_output output;
3282 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
3283 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
3284 flow_extract(packet, NULL, &flow);
3285 flow.in_port.ofp_port = OFPP_NONE;
3287 ovs_rwlock_rdlock(&xlate_rwlock);
3288 xport = xport_lookup(ofport);
3290 ovs_rwlock_unlock(&xlate_rwlock);
3293 output.port = xport->ofp_port;
3295 ovs_rwlock_unlock(&xlate_rwlock);
3297 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
3298 &output.ofpact, sizeof output,