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);
55 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
57 /* Maximum depth of flow table recursion (due to resubmit actions) in a
58 * flow translation. */
59 #define MAX_RESUBMIT_RECURSION 64
61 /* Maximum number of resubmit actions in a flow translation, whether they are
62 * recursive or not. */
63 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
65 struct ovs_rwlock xlate_rwlock = OVS_RWLOCK_INITIALIZER;
68 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
69 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
71 struct list xbundles; /* Owned xbundles. */
72 struct hmap xports; /* Indexed by ofp_port. */
74 char *name; /* Name used in log messages. */
75 struct dpif *dpif; /* Datapath interface. */
76 struct mac_learning *ml; /* Mac learning handle. */
77 struct mbridge *mbridge; /* Mirroring. */
78 struct dpif_sflow *sflow; /* SFlow handle, or null. */
79 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
80 struct netflow *netflow; /* Netflow handle, or null. */
81 struct stp *stp; /* STP or null if disabled. */
83 /* Special rules installed by ofproto-dpif. */
84 struct rule_dpif *miss_rule;
85 struct rule_dpif *no_packet_in_rule;
87 enum ofp_config_flags frag; /* Fragmentation handling. */
88 bool has_in_band; /* Bridge has in band control? */
89 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
93 struct hmap_node hmap_node; /* In global 'xbundles' map. */
94 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
96 struct list list_node; /* In parent 'xbridges' list. */
97 struct xbridge *xbridge; /* Parent xbridge. */
99 struct list xports; /* Contains "struct xport"s. */
101 char *name; /* Name used in log messages. */
102 struct bond *bond; /* Nonnull iff more than one port. */
103 struct lacp *lacp; /* LACP handle or null. */
105 enum port_vlan_mode vlan_mode; /* VLAN mode. */
106 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
107 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
108 * NULL if all VLANs are trunked. */
109 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
110 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
114 struct hmap_node hmap_node; /* Node in global 'xports' map. */
115 struct ofport_dpif *ofport; /* Key in global 'xports map. */
117 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
118 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
120 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
122 struct list bundle_node; /* In parent xbundle (if it exists). */
123 struct xbundle *xbundle; /* Parent xbundle or null. */
125 struct netdev *netdev; /* 'ofport''s netdev. */
127 struct xbridge *xbridge; /* Parent bridge. */
128 struct xport *peer; /* Patch port peer or null. */
130 enum ofputil_port_config config; /* OpenFlow port configuration. */
131 enum ofputil_port_state state; /* OpenFlow port state. */
132 int stp_port_no; /* STP port number or -1 if not in use. */
134 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
136 bool may_enable; /* May be enabled in bonds. */
137 bool is_tunnel; /* Is a tunnel port. */
139 struct cfm *cfm; /* CFM handle or null. */
140 struct bfd *bfd; /* BFD handle or null. */
144 struct xlate_in *xin;
145 struct xlate_out *xout;
147 const struct xbridge *xbridge;
149 /* Flow at the last commit. */
150 struct flow base_flow;
152 /* Tunnel IP destination address as received. This is stored separately
153 * as the base_flow.tunnel is cleared on init to reflect the datapath
154 * behavior. Used to make sure not to send tunneled output to ourselves,
155 * which might lead to an infinite loop. This could happen easily
156 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
157 * actually set the tun_dst field. */
158 ovs_be32 orig_tunnel_ip_dst;
160 /* Stack for the push and pop actions. Each stack element is of type
161 * "union mf_subvalue". */
162 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
165 /* The rule that we are currently translating, or NULL. */
166 struct rule_dpif *rule;
168 int mpls_depth_delta; /* Delta of the mpls stack depth since
169 * actions were last committed.
170 * Must be between -1 and 1 inclusive. */
171 ovs_be32 pre_push_mpls_lse; /* Used to record the top-most MPLS LSE
172 * prior to an mpls_push so that it may be
173 * used for a subsequent mpls_pop. */
175 /* Resubmit statistics, via xlate_table_action(). */
176 int recurse; /* Current resubmit nesting depth. */
177 int resubmits; /* Total number of resubmits. */
179 uint32_t orig_skb_priority; /* Priority when packet arrived. */
180 uint8_t table_id; /* OpenFlow table ID where flow was found. */
181 uint32_t sflow_n_outputs; /* Number of output ports. */
182 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
183 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
184 bool exit; /* No further actions should be processed. */
186 /* OpenFlow 1.1+ action set.
188 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
189 * When translation is otherwise complete, ofpacts_execute_action_set()
190 * converts it to a set of "struct ofpact"s that can be translated into
191 * datapath actions. */
192 struct ofpbuf action_set; /* Action set. */
193 uint64_t action_set_stub[1024 / 8];
196 /* A controller may use OFPP_NONE as the ingress port to indicate that
197 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
198 * when an input bundle is needed for validation (e.g., mirroring or
199 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
200 * any 'port' structs, so care must be taken when dealing with it.
201 * The bundle's name and vlan mode are initialized in lookup_input_bundle() */
202 static struct xbundle ofpp_none_bundle;
204 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
205 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
206 * traffic egressing the 'ofport' with that priority should be marked with. */
207 struct skb_priority_to_dscp {
208 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
209 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
211 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
214 static struct hmap xbridges = HMAP_INITIALIZER(&xbridges);
215 static struct hmap xbundles = HMAP_INITIALIZER(&xbundles);
216 static struct hmap xports = HMAP_INITIALIZER(&xports);
218 static bool may_receive(const struct xport *, struct xlate_ctx *);
219 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
221 static void xlate_actions__(struct xlate_in *, struct xlate_out *)
222 OVS_REQ_RDLOCK(xlate_rwlock);
223 static void xlate_normal(struct xlate_ctx *);
224 static void xlate_report(struct xlate_ctx *, const char *);
225 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
226 uint8_t table_id, bool may_packet_in);
227 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
228 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
229 static void output_normal(struct xlate_ctx *, const struct xbundle *,
231 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
233 static struct xbridge *xbridge_lookup(const struct ofproto_dpif *);
234 static struct xbundle *xbundle_lookup(const struct ofbundle *);
235 static struct xport *xport_lookup(const struct ofport_dpif *);
236 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
237 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
238 uint32_t skb_priority);
239 static void clear_skb_priorities(struct xport *);
240 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
244 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
245 struct dpif *dpif, struct rule_dpif *miss_rule,
246 struct rule_dpif *no_packet_in_rule,
247 const struct mac_learning *ml, struct stp *stp,
248 const struct mbridge *mbridge,
249 const struct dpif_sflow *sflow,
250 const struct dpif_ipfix *ipfix,
251 const struct netflow *netflow, enum ofp_config_flags frag,
252 bool forward_bpdu, bool has_in_band)
254 struct xbridge *xbridge = xbridge_lookup(ofproto);
257 xbridge = xzalloc(sizeof *xbridge);
258 xbridge->ofproto = ofproto;
260 hmap_insert(&xbridges, &xbridge->hmap_node, hash_pointer(ofproto, 0));
261 hmap_init(&xbridge->xports);
262 list_init(&xbridge->xbundles);
265 if (xbridge->ml != ml) {
266 mac_learning_unref(xbridge->ml);
267 xbridge->ml = mac_learning_ref(ml);
270 if (xbridge->mbridge != mbridge) {
271 mbridge_unref(xbridge->mbridge);
272 xbridge->mbridge = mbridge_ref(mbridge);
275 if (xbridge->sflow != sflow) {
276 dpif_sflow_unref(xbridge->sflow);
277 xbridge->sflow = dpif_sflow_ref(sflow);
280 if (xbridge->ipfix != ipfix) {
281 dpif_ipfix_unref(xbridge->ipfix);
282 xbridge->ipfix = dpif_ipfix_ref(ipfix);
285 if (xbridge->stp != stp) {
286 stp_unref(xbridge->stp);
287 xbridge->stp = stp_ref(stp);
290 if (xbridge->netflow != netflow) {
291 netflow_unref(xbridge->netflow);
292 xbridge->netflow = netflow_ref(netflow);
296 xbridge->name = xstrdup(name);
298 xbridge->dpif = dpif;
299 xbridge->forward_bpdu = forward_bpdu;
300 xbridge->has_in_band = has_in_band;
301 xbridge->frag = frag;
302 xbridge->miss_rule = miss_rule;
303 xbridge->no_packet_in_rule = no_packet_in_rule;
307 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
309 struct xbridge *xbridge = xbridge_lookup(ofproto);
310 struct xbundle *xbundle, *next_xbundle;
311 struct xport *xport, *next_xport;
317 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
318 xlate_ofport_remove(xport->ofport);
321 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
322 xlate_bundle_remove(xbundle->ofbundle);
325 hmap_remove(&xbridges, &xbridge->hmap_node);
326 mac_learning_unref(xbridge->ml);
327 mbridge_unref(xbridge->mbridge);
328 dpif_sflow_unref(xbridge->sflow);
329 dpif_ipfix_unref(xbridge->ipfix);
330 stp_unref(xbridge->stp);
331 hmap_destroy(&xbridge->xports);
337 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
338 const char *name, enum port_vlan_mode vlan_mode, int vlan,
339 unsigned long *trunks, bool use_priority_tags,
340 const struct bond *bond, const struct lacp *lacp,
343 struct xbundle *xbundle = xbundle_lookup(ofbundle);
346 xbundle = xzalloc(sizeof *xbundle);
347 xbundle->ofbundle = ofbundle;
348 xbundle->xbridge = xbridge_lookup(ofproto);
350 hmap_insert(&xbundles, &xbundle->hmap_node, hash_pointer(ofbundle, 0));
351 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
352 list_init(&xbundle->xports);
355 ovs_assert(xbundle->xbridge);
358 xbundle->name = xstrdup(name);
360 xbundle->vlan_mode = vlan_mode;
361 xbundle->vlan = vlan;
362 xbundle->trunks = trunks;
363 xbundle->use_priority_tags = use_priority_tags;
364 xbundle->floodable = floodable;
366 if (xbundle->bond != bond) {
367 bond_unref(xbundle->bond);
368 xbundle->bond = bond_ref(bond);
371 if (xbundle->lacp != lacp) {
372 lacp_unref(xbundle->lacp);
373 xbundle->lacp = lacp_ref(lacp);
378 xlate_bundle_remove(struct ofbundle *ofbundle)
380 struct xbundle *xbundle = xbundle_lookup(ofbundle);
381 struct xport *xport, *next;
387 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
388 list_remove(&xport->bundle_node);
389 xport->xbundle = NULL;
392 hmap_remove(&xbundles, &xbundle->hmap_node);
393 list_remove(&xbundle->list_node);
394 bond_unref(xbundle->bond);
395 lacp_unref(xbundle->lacp);
401 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
402 struct ofport_dpif *ofport, ofp_port_t ofp_port,
403 odp_port_t odp_port, const struct netdev *netdev,
404 const struct cfm *cfm, const struct bfd *bfd,
405 struct ofport_dpif *peer, int stp_port_no,
406 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
407 enum ofputil_port_config config,
408 enum ofputil_port_state state, bool is_tunnel,
411 struct xport *xport = xport_lookup(ofport);
415 xport = xzalloc(sizeof *xport);
416 xport->ofport = ofport;
417 xport->xbridge = xbridge_lookup(ofproto);
418 xport->ofp_port = ofp_port;
420 hmap_init(&xport->skb_priorities);
421 hmap_insert(&xports, &xport->hmap_node, hash_pointer(ofport, 0));
422 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
423 hash_ofp_port(xport->ofp_port));
426 ovs_assert(xport->ofp_port == ofp_port);
428 xport->config = config;
429 xport->state = state;
430 xport->stp_port_no = stp_port_no;
431 xport->is_tunnel = is_tunnel;
432 xport->may_enable = may_enable;
433 xport->odp_port = odp_port;
435 if (xport->netdev != netdev) {
436 netdev_close(xport->netdev);
437 xport->netdev = netdev_ref(netdev);
440 if (xport->cfm != cfm) {
441 cfm_unref(xport->cfm);
442 xport->cfm = cfm_ref(cfm);
445 if (xport->bfd != bfd) {
446 bfd_unref(xport->bfd);
447 xport->bfd = bfd_ref(bfd);
451 xport->peer->peer = NULL;
453 xport->peer = xport_lookup(peer);
455 xport->peer->peer = xport;
458 if (xport->xbundle) {
459 list_remove(&xport->bundle_node);
461 xport->xbundle = xbundle_lookup(ofbundle);
462 if (xport->xbundle) {
463 list_insert(&xport->xbundle->xports, &xport->bundle_node);
466 clear_skb_priorities(xport);
467 for (i = 0; i < n_qdscp; i++) {
468 struct skb_priority_to_dscp *pdscp;
469 uint32_t skb_priority;
471 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
476 pdscp = xmalloc(sizeof *pdscp);
477 pdscp->skb_priority = skb_priority;
478 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
479 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
480 hash_int(pdscp->skb_priority, 0));
485 xlate_ofport_remove(struct ofport_dpif *ofport)
487 struct xport *xport = xport_lookup(ofport);
494 xport->peer->peer = NULL;
498 if (xport->xbundle) {
499 list_remove(&xport->bundle_node);
502 clear_skb_priorities(xport);
503 hmap_destroy(&xport->skb_priorities);
505 hmap_remove(&xports, &xport->hmap_node);
506 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
508 netdev_close(xport->netdev);
509 cfm_unref(xport->cfm);
510 bfd_unref(xport->bfd);
514 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
515 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
516 * Optionally, if nonnull, populates 'fitnessp' with the fitness of 'flow' as
517 * returned by odp_flow_key_to_flow(). Also, optionally populates 'ofproto'
518 * with the ofproto_dpif, 'odp_in_port' with the datapath in_port, that
519 * 'packet' ingressed, and 'ipfix', 'sflow', and 'netflow' with the appropriate
520 * handles for those protocols if they're enabled. Caller is responsible for
523 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
524 * 'flow''s in_port to OFPP_NONE.
526 * This function does post-processing on data returned from
527 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
528 * of the upcall processing logic. In particular, if the extracted in_port is
529 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
530 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
531 * a VLAN header onto 'packet' (if it is nonnull).
533 * Similarly, this function also includes some logic to help with tunnels. It
534 * may modify 'flow' as necessary to make the tunneling implementation
535 * transparent to the upcall processing logic.
537 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
538 * or some other positive errno if there are other problems. */
540 xlate_receive(const struct dpif_backer *backer, struct ofpbuf *packet,
541 const struct nlattr *key, size_t key_len,
542 struct flow *flow, enum odp_key_fitness *fitnessp,
543 struct ofproto_dpif **ofproto, struct dpif_ipfix **ipfix,
544 struct dpif_sflow **sflow, struct netflow **netflow,
545 odp_port_t *odp_in_port)
547 enum odp_key_fitness fitness;
548 const struct xport *xport;
551 ovs_rwlock_rdlock(&xlate_rwlock);
552 fitness = odp_flow_key_to_flow(key, key_len, flow);
553 if (fitness == ODP_FIT_ERROR) {
559 *odp_in_port = flow->in_port.odp_port;
562 xport = xport_lookup(tnl_port_should_receive(flow)
563 ? tnl_port_receive(flow)
564 : odp_port_to_ofport(backer, flow->in_port.odp_port));
566 flow->in_port.ofp_port = xport ? xport->ofp_port : OFPP_NONE;
571 if (vsp_adjust_flow(xport->xbridge->ofproto, flow)) {
573 /* Make the packet resemble the flow, so that it gets sent to
574 * an OpenFlow controller properly, so that it looks correct
575 * for sFlow, and so that flow_extract() will get the correct
576 * vlan_tci if it is called on 'packet'. */
577 eth_push_vlan(packet, flow->vlan_tci);
579 /* We can't reproduce 'key' from 'flow'. */
580 fitness = fitness == ODP_FIT_PERFECT ? ODP_FIT_TOO_MUCH : fitness;
585 *ofproto = xport->xbridge->ofproto;
589 *ipfix = dpif_ipfix_ref(xport->xbridge->ipfix);
593 *sflow = dpif_sflow_ref(xport->xbridge->sflow);
597 *netflow = netflow_ref(xport->xbridge->netflow);
604 ovs_rwlock_unlock(&xlate_rwlock);
608 static struct xbridge *
609 xbridge_lookup(const struct ofproto_dpif *ofproto)
611 struct xbridge *xbridge;
617 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
619 if (xbridge->ofproto == ofproto) {
626 static struct xbundle *
627 xbundle_lookup(const struct ofbundle *ofbundle)
629 struct xbundle *xbundle;
635 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
637 if (xbundle->ofbundle == ofbundle) {
644 static struct xport *
645 xport_lookup(const struct ofport_dpif *ofport)
653 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
655 if (xport->ofport == ofport) {
662 static struct stp_port *
663 xport_get_stp_port(const struct xport *xport)
665 return xport->xbridge->stp && xport->stp_port_no != -1
666 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
671 xport_stp_learn_state(const struct xport *xport)
673 struct stp_port *sp = xport_get_stp_port(xport);
674 return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
678 xport_stp_forward_state(const struct xport *xport)
680 struct stp_port *sp = xport_get_stp_port(xport);
681 return stp_forward_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
685 xport_stp_listen_state(const struct xport *xport)
687 struct stp_port *sp = xport_get_stp_port(xport);
688 return stp_listen_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
691 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
692 * were used to make the determination.*/
694 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
696 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
697 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
701 stp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
703 struct stp_port *sp = xport_get_stp_port(xport);
704 struct ofpbuf payload = *packet;
705 struct eth_header *eth = payload.data;
707 /* Sink packets on ports that have STP disabled when the bridge has
709 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
713 /* Trim off padding on payload. */
714 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
715 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
718 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
719 stp_received_bpdu(sp, payload.data, payload.size);
723 static struct xport *
724 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
728 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
730 if (xport->ofp_port == ofp_port) {
738 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
740 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
741 return xport ? xport->odp_port : ODPP_NONE;
745 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
749 xport = get_ofp_port(ctx->xbridge, ofp_port);
750 if (!xport || xport->config & OFPUTIL_PC_PORT_DOWN ||
751 xport->state & OFPUTIL_PS_LINK_DOWN) {
758 static const struct ofputil_bucket *
759 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
763 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
765 struct group_dpif *group;
768 hit = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
773 hit = group_first_live_bucket(ctx, group, depth) != NULL;
775 group_dpif_release(group);
779 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
782 bucket_is_alive(const struct xlate_ctx *ctx,
783 const struct ofputil_bucket *bucket, int depth)
785 if (depth >= MAX_LIVENESS_RECURSION) {
786 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
788 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
789 MAX_LIVENESS_RECURSION);
793 return !ofputil_bucket_has_liveness(bucket) ||
794 (bucket->watch_port != OFPP_ANY &&
795 odp_port_is_alive(ctx, bucket->watch_port)) ||
796 (bucket->watch_group != OFPG_ANY &&
797 group_is_alive(ctx, bucket->watch_group, depth + 1));
800 static const struct ofputil_bucket *
801 group_first_live_bucket(const struct xlate_ctx *ctx,
802 const struct group_dpif *group, int depth)
804 struct ofputil_bucket *bucket;
805 const struct list *buckets;
807 group_dpif_get_buckets(group, &buckets);
808 LIST_FOR_EACH (bucket, list_node, buckets) {
809 if (bucket_is_alive(ctx, bucket, depth)) {
817 static const struct ofputil_bucket *
818 group_best_live_bucket(const struct xlate_ctx *ctx,
819 const struct group_dpif *group,
822 const struct ofputil_bucket *best_bucket = NULL;
823 uint32_t best_score = 0;
826 const struct ofputil_bucket *bucket;
827 const struct list *buckets;
829 group_dpif_get_buckets(group, &buckets);
830 LIST_FOR_EACH (bucket, list_node, buckets) {
831 if (bucket_is_alive(ctx, bucket, 0)) {
832 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
833 if (score >= best_score) {
834 best_bucket = bucket;
845 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
847 return (bundle->vlan_mode != PORT_VLAN_ACCESS
848 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
852 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
854 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
858 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
860 return xbundle != &ofpp_none_bundle
861 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
866 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
868 return xbundle != &ofpp_none_bundle
869 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
874 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
876 return xbundle != &ofpp_none_bundle
877 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
881 static struct xbundle *
882 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
883 bool warn, struct xport **in_xportp)
887 /* Find the port and bundle for the received packet. */
888 xport = get_ofp_port(xbridge, in_port);
892 if (xport && xport->xbundle) {
893 return xport->xbundle;
896 /* Special-case OFPP_NONE, which a controller may use as the ingress
897 * port for traffic that it is sourcing. */
898 if (in_port == OFPP_NONE) {
899 ofpp_none_bundle.name = "OFPP_NONE";
900 ofpp_none_bundle.vlan_mode = PORT_VLAN_TRUNK;
901 return &ofpp_none_bundle;
904 /* Odd. A few possible reasons here:
906 * - We deleted a port but there are still a few packets queued up
909 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
910 * we don't know about.
912 * - The ofproto client didn't configure the port as part of a bundle.
913 * This is particularly likely to happen if a packet was received on the
914 * port after it was created, but before the client had a chance to
915 * configure its bundle.
918 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
920 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
921 "port %"PRIu16, xbridge->name, in_port);
927 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
929 const struct xbridge *xbridge = ctx->xbridge;
930 mirror_mask_t mirrors;
931 struct xbundle *in_xbundle;
935 mirrors = ctx->xout->mirrors;
936 ctx->xout->mirrors = 0;
938 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
939 ctx->xin->packet != NULL, NULL);
943 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
945 /* Drop frames on bundles reserved for mirroring. */
946 if (xbundle_mirror_out(xbridge, in_xbundle)) {
947 if (ctx->xin->packet != NULL) {
948 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
949 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
950 "%s, which is reserved exclusively for mirroring",
951 ctx->xbridge->name, in_xbundle->name);
953 ofpbuf_clear(&ctx->xout->odp_actions);
958 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
959 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
962 vlan = input_vid_to_vlan(in_xbundle, vid);
968 /* Restore the original packet before adding the mirror actions. */
969 ctx->xin->flow = *orig_flow;
972 mirror_mask_t dup_mirrors;
973 struct ofbundle *out;
974 unsigned long *vlans;
979 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
980 &vlans, &dup_mirrors, &out, &out_vlan);
981 ovs_assert(has_mirror);
984 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
986 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
989 if (!vlan_mirrored) {
990 mirrors = zero_rightmost_1bit(mirrors);
994 mirrors &= ~dup_mirrors;
995 ctx->xout->mirrors |= dup_mirrors;
997 struct xbundle *out_xbundle = xbundle_lookup(out);
999 output_normal(ctx, out_xbundle, vlan);
1001 } else if (vlan != out_vlan
1002 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
1003 struct xbundle *xbundle;
1005 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1006 if (xbundle_includes_vlan(xbundle, out_vlan)
1007 && !xbundle_mirror_out(xbridge, xbundle)) {
1008 output_normal(ctx, xbundle, out_vlan);
1015 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1016 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1017 * the bundle on which the packet was received, returns the VLAN to which the
1020 * Both 'vid' and the return value are in the range 0...4095. */
1022 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1024 switch (in_xbundle->vlan_mode) {
1025 case PORT_VLAN_ACCESS:
1026 return in_xbundle->vlan;
1029 case PORT_VLAN_TRUNK:
1032 case PORT_VLAN_NATIVE_UNTAGGED:
1033 case PORT_VLAN_NATIVE_TAGGED:
1034 return vid ? vid : in_xbundle->vlan;
1041 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1042 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1045 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1046 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1049 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1051 /* Allow any VID on the OFPP_NONE port. */
1052 if (in_xbundle == &ofpp_none_bundle) {
1056 switch (in_xbundle->vlan_mode) {
1057 case PORT_VLAN_ACCESS:
1060 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1061 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1062 "packet received on port %s configured as VLAN "
1063 "%"PRIu16" access port", vid, in_xbundle->name,
1070 case PORT_VLAN_NATIVE_UNTAGGED:
1071 case PORT_VLAN_NATIVE_TAGGED:
1073 /* Port must always carry its native VLAN. */
1077 case PORT_VLAN_TRUNK:
1078 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1080 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1081 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1082 "received on port %s not configured for trunking "
1083 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1095 /* Given 'vlan', the VLAN that a packet belongs to, and
1096 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1097 * that should be included in the 802.1Q header. (If the return value is 0,
1098 * then the 802.1Q header should only be included in the packet if there is a
1101 * Both 'vlan' and the return value are in the range 0...4095. */
1103 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1105 switch (out_xbundle->vlan_mode) {
1106 case PORT_VLAN_ACCESS:
1109 case PORT_VLAN_TRUNK:
1110 case PORT_VLAN_NATIVE_TAGGED:
1113 case PORT_VLAN_NATIVE_UNTAGGED:
1114 return vlan == out_xbundle->vlan ? 0 : vlan;
1122 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1125 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1127 ovs_be16 tci, old_tci;
1128 struct xport *xport;
1130 vid = output_vlan_to_vid(out_xbundle, vlan);
1131 if (list_is_empty(&out_xbundle->xports)) {
1132 /* Partially configured bundle with no slaves. Drop the packet. */
1134 } else if (!out_xbundle->bond) {
1135 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1138 struct ofport_dpif *ofport;
1140 ofport = bond_choose_output_slave(out_xbundle->bond, &ctx->xin->flow,
1141 &ctx->xout->wc, vid);
1142 xport = xport_lookup(ofport);
1145 /* No slaves enabled, so drop packet. */
1149 if (ctx->xin->resubmit_stats) {
1150 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1151 ctx->xin->resubmit_stats->n_bytes);
1155 old_tci = *flow_tci;
1157 if (tci || out_xbundle->use_priority_tags) {
1158 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1160 tci |= htons(VLAN_CFI);
1165 compose_output_action(ctx, xport->ofp_port);
1166 *flow_tci = old_tci;
1169 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1170 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1171 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1173 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1175 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1179 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1180 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1184 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1185 if (flow->nw_proto == ARP_OP_REPLY) {
1187 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1188 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1189 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1191 return flow->nw_src == flow->nw_dst;
1197 /* Checks whether a MAC learning update is necessary for MAC learning table
1198 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1201 * Most packets processed through the MAC learning table do not actually
1202 * change it in any way. This function requires only a read lock on the MAC
1203 * learning table, so it is much cheaper in this common case.
1205 * Keep the code here synchronized with that in update_learning_table__()
1208 is_mac_learning_update_needed(const struct mac_learning *ml,
1209 const struct flow *flow,
1210 struct flow_wildcards *wc,
1211 int vlan, struct xbundle *in_xbundle)
1212 OVS_REQ_RDLOCK(ml->rwlock)
1214 struct mac_entry *mac;
1216 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1220 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1221 if (!mac || mac_entry_age(ml, mac)) {
1225 if (is_gratuitous_arp(flow, wc)) {
1226 /* We don't want to learn from gratuitous ARP packets that are
1227 * reflected back over bond slaves so we lock the learning table. */
1228 if (!in_xbundle->bond) {
1230 } else if (mac_entry_is_grat_arp_locked(mac)) {
1235 return mac->port.p != in_xbundle->ofbundle;
1239 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1240 * received on 'in_xbundle' in 'vlan'.
1242 * This code repeats all the checks in is_mac_learning_update_needed() because
1243 * the lock was released between there and here and thus the MAC learning state
1244 * could have changed.
1246 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1249 update_learning_table__(const struct xbridge *xbridge,
1250 const struct flow *flow, struct flow_wildcards *wc,
1251 int vlan, struct xbundle *in_xbundle)
1252 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1254 struct mac_entry *mac;
1256 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1260 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1261 if (is_gratuitous_arp(flow, wc)) {
1262 /* We don't want to learn from gratuitous ARP packets that are
1263 * reflected back over bond slaves so we lock the learning table. */
1264 if (!in_xbundle->bond) {
1265 mac_entry_set_grat_arp_lock(mac);
1266 } else if (mac_entry_is_grat_arp_locked(mac)) {
1271 if (mac->port.p != in_xbundle->ofbundle) {
1272 /* The log messages here could actually be useful in debugging,
1273 * so keep the rate limit relatively high. */
1274 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1276 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1277 "on port %s in VLAN %d",
1278 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1279 in_xbundle->name, vlan);
1281 mac->port.p = in_xbundle->ofbundle;
1282 mac_learning_changed(xbridge->ml);
1287 update_learning_table(const struct xbridge *xbridge,
1288 const struct flow *flow, struct flow_wildcards *wc,
1289 int vlan, struct xbundle *in_xbundle)
1293 /* Don't learn the OFPP_NONE port. */
1294 if (in_xbundle == &ofpp_none_bundle) {
1298 /* First try the common case: no change to MAC learning table. */
1299 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1300 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1302 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1305 /* Slow path: MAC learning table might need an update. */
1306 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1307 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1308 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1312 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1313 * dropped. Returns true if they may be forwarded, false if they should be
1316 * 'in_port' must be the xport that corresponds to flow->in_port.
1317 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1319 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1320 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1321 * checked by input_vid_is_valid().
1323 * May also add tags to '*tags', although the current implementation only does
1324 * so in one special case.
1327 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1330 struct xbundle *in_xbundle = in_port->xbundle;
1331 const struct xbridge *xbridge = ctx->xbridge;
1332 struct flow *flow = &ctx->xin->flow;
1334 /* Drop frames for reserved multicast addresses
1335 * only if forward_bpdu option is absent. */
1336 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1337 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1341 if (in_xbundle->bond) {
1342 struct mac_entry *mac;
1344 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1350 xlate_report(ctx, "bonding refused admissibility, dropping");
1353 case BV_DROP_IF_MOVED:
1354 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1355 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1356 if (mac && mac->port.p != in_xbundle->ofbundle &&
1357 (!is_gratuitous_arp(flow, &ctx->xout->wc)
1358 || mac_entry_is_grat_arp_locked(mac))) {
1359 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1360 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1364 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1373 xlate_normal(struct xlate_ctx *ctx)
1375 struct flow_wildcards *wc = &ctx->xout->wc;
1376 struct flow *flow = &ctx->xin->flow;
1377 struct xbundle *in_xbundle;
1378 struct xport *in_port;
1379 struct mac_entry *mac;
1384 ctx->xout->has_normal = true;
1386 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1387 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1388 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1390 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
1391 ctx->xin->packet != NULL, &in_port);
1393 xlate_report(ctx, "no input bundle, dropping");
1397 /* Drop malformed frames. */
1398 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
1399 !(flow->vlan_tci & htons(VLAN_CFI))) {
1400 if (ctx->xin->packet != NULL) {
1401 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1402 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
1403 "VLAN tag received on port %s",
1404 ctx->xbridge->name, in_xbundle->name);
1406 xlate_report(ctx, "partial VLAN tag, dropping");
1410 /* Drop frames on bundles reserved for mirroring. */
1411 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
1412 if (ctx->xin->packet != NULL) {
1413 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1414 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1415 "%s, which is reserved exclusively for mirroring",
1416 ctx->xbridge->name, in_xbundle->name);
1418 xlate_report(ctx, "input port is mirror output port, dropping");
1423 vid = vlan_tci_to_vid(flow->vlan_tci);
1424 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1425 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
1428 vlan = input_vid_to_vlan(in_xbundle, vid);
1430 /* Check other admissibility requirements. */
1431 if (in_port && !is_admissible(ctx, in_port, vlan)) {
1435 /* Learn source MAC. */
1436 if (ctx->xin->may_learn) {
1437 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
1440 /* Determine output bundle. */
1441 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
1442 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
1443 mac_port = mac ? mac->port.p : NULL;
1444 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
1447 struct xbundle *mac_xbundle = xbundle_lookup(mac_port);
1448 if (mac_xbundle && mac_xbundle != in_xbundle) {
1449 xlate_report(ctx, "forwarding to learned port");
1450 output_normal(ctx, mac_xbundle, vlan);
1451 } else if (!mac_xbundle) {
1452 xlate_report(ctx, "learned port is unknown, dropping");
1454 xlate_report(ctx, "learned port is input port, dropping");
1457 struct xbundle *xbundle;
1459 xlate_report(ctx, "no learned MAC for destination, flooding");
1460 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1461 if (xbundle != in_xbundle
1462 && xbundle_includes_vlan(xbundle, vlan)
1463 && xbundle->floodable
1464 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1465 output_normal(ctx, xbundle, vlan);
1468 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1472 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1473 * the number of packets out of UINT32_MAX to sample. The given
1474 * cookie is passed back in the callback for each sampled packet.
1477 compose_sample_action(const struct xbridge *xbridge,
1478 struct ofpbuf *odp_actions,
1479 const struct flow *flow,
1480 const uint32_t probability,
1481 const union user_action_cookie *cookie,
1482 const size_t cookie_size)
1484 size_t sample_offset, actions_offset;
1485 odp_port_t odp_port;
1489 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
1491 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
1493 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
1495 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
1496 pid = dpif_port_get_pid(xbridge->dpif, odp_port);
1497 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size, odp_actions);
1499 nl_msg_end_nested(odp_actions, actions_offset);
1500 nl_msg_end_nested(odp_actions, sample_offset);
1501 return cookie_offset;
1505 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
1506 odp_port_t odp_port, unsigned int n_outputs,
1507 union user_action_cookie *cookie)
1511 cookie->type = USER_ACTION_COOKIE_SFLOW;
1512 cookie->sflow.vlan_tci = vlan_tci;
1514 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1515 * port information") for the interpretation of cookie->output. */
1516 switch (n_outputs) {
1518 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1519 cookie->sflow.output = 0x40000000 | 256;
1523 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
1525 cookie->sflow.output = ifindex;
1530 /* 0x80000000 means "multiple output ports. */
1531 cookie->sflow.output = 0x80000000 | n_outputs;
1536 /* Compose SAMPLE action for sFlow bridge sampling. */
1538 compose_sflow_action(const struct xbridge *xbridge,
1539 struct ofpbuf *odp_actions,
1540 const struct flow *flow,
1541 odp_port_t odp_port)
1543 uint32_t probability;
1544 union user_action_cookie cookie;
1546 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
1550 probability = dpif_sflow_get_probability(xbridge->sflow);
1551 compose_sflow_cookie(xbridge, htons(0), odp_port,
1552 odp_port == ODPP_NONE ? 0 : 1, &cookie);
1554 return compose_sample_action(xbridge, odp_actions, flow, probability,
1555 &cookie, sizeof cookie.sflow);
1559 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
1560 uint32_t obs_domain_id, uint32_t obs_point_id,
1561 union user_action_cookie *cookie)
1563 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
1564 cookie->flow_sample.probability = probability;
1565 cookie->flow_sample.collector_set_id = collector_set_id;
1566 cookie->flow_sample.obs_domain_id = obs_domain_id;
1567 cookie->flow_sample.obs_point_id = obs_point_id;
1571 compose_ipfix_cookie(union user_action_cookie *cookie)
1573 cookie->type = USER_ACTION_COOKIE_IPFIX;
1576 /* Compose SAMPLE action for IPFIX bridge sampling. */
1578 compose_ipfix_action(const struct xbridge *xbridge,
1579 struct ofpbuf *odp_actions,
1580 const struct flow *flow)
1582 uint32_t probability;
1583 union user_action_cookie cookie;
1585 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
1589 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
1590 compose_ipfix_cookie(&cookie);
1592 compose_sample_action(xbridge, odp_actions, flow, probability,
1593 &cookie, sizeof cookie.ipfix);
1596 /* SAMPLE action for sFlow must be first action in any given list of
1597 * actions. At this point we do not have all information required to
1598 * build it. So try to build sample action as complete as possible. */
1600 add_sflow_action(struct xlate_ctx *ctx)
1602 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
1603 &ctx->xout->odp_actions,
1604 &ctx->xin->flow, ODPP_NONE);
1605 ctx->sflow_odp_port = 0;
1606 ctx->sflow_n_outputs = 0;
1609 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
1610 * of actions, eventually after the SAMPLE action for sFlow. */
1612 add_ipfix_action(struct xlate_ctx *ctx)
1614 compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions,
1618 /* Fix SAMPLE action according to data collected while composing ODP actions.
1619 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
1620 * USERSPACE action's user-cookie which is required for sflow. */
1622 fix_sflow_action(struct xlate_ctx *ctx)
1624 const struct flow *base = &ctx->base_flow;
1625 union user_action_cookie *cookie;
1627 if (!ctx->user_cookie_offset) {
1631 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
1632 sizeof cookie->sflow);
1633 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
1635 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
1636 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
1639 static enum slow_path_reason
1640 process_special(struct xlate_ctx *ctx, const struct flow *flow,
1641 const struct xport *xport, const struct ofpbuf *packet)
1643 struct flow_wildcards *wc = &ctx->xout->wc;
1644 const struct xbridge *xbridge = ctx->xbridge;
1648 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
1650 cfm_process_heartbeat(xport->cfm, packet);
1653 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
1655 bfd_process_packet(xport->bfd, flow, packet);
1656 /* If POLL received, immediately sends FINAL back. */
1657 if (bfd_should_send_packet(xport->bfd)) {
1659 ofproto_dpif_monitor_port_send_soon(xport->ofport);
1661 ofproto_dpif_monitor_port_send_soon_safe(xport->ofport);
1666 } else if (xport->xbundle && xport->xbundle->lacp
1667 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1669 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
1672 } else if (xbridge->stp && stp_should_process_flow(flow, wc)) {
1674 stp_process_packet(xport, packet);
1683 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
1686 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1687 struct flow_wildcards *wc = &ctx->xout->wc;
1688 struct flow *flow = &ctx->xin->flow;
1689 ovs_be16 flow_vlan_tci;
1690 uint32_t flow_pkt_mark;
1691 uint8_t flow_nw_tos;
1692 odp_port_t out_port, odp_port;
1695 /* If 'struct flow' gets additional metadata, we'll need to zero it out
1696 * before traversing a patch port. */
1697 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 23);
1700 xlate_report(ctx, "Nonexistent output port");
1702 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
1703 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
1705 } else if (check_stp) {
1706 if (eth_addr_equals(ctx->base_flow.dl_dst, eth_addr_stp)) {
1707 if (!xport_stp_listen_state(xport)) {
1708 xlate_report(ctx, "STP not in listening state, "
1709 "skipping bpdu output");
1712 } else if (!xport_stp_forward_state(xport)) {
1713 xlate_report(ctx, "STP not in forwarding state, "
1719 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
1720 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
1725 const struct xport *peer = xport->peer;
1726 struct flow old_flow = ctx->xin->flow;
1727 enum slow_path_reason special;
1729 ctx->xbridge = peer->xbridge;
1730 flow->in_port.ofp_port = peer->ofp_port;
1731 flow->metadata = htonll(0);
1732 memset(&flow->tunnel, 0, sizeof flow->tunnel);
1733 memset(flow->regs, 0, sizeof flow->regs);
1735 special = process_special(ctx, &ctx->xin->flow, peer,
1738 ctx->xout->slow |= special;
1739 } else if (may_receive(peer, ctx)) {
1740 if (xport_stp_forward_state(peer)) {
1741 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1743 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
1744 * learning action look at the packet, then drop it. */
1745 struct flow old_base_flow = ctx->base_flow;
1746 size_t old_size = ctx->xout->odp_actions.size;
1747 mirror_mask_t old_mirrors = ctx->xout->mirrors;
1748 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1749 ctx->xout->mirrors = old_mirrors;
1750 ctx->base_flow = old_base_flow;
1751 ctx->xout->odp_actions.size = old_size;
1755 ctx->xin->flow = old_flow;
1756 ctx->xbridge = xport->xbridge;
1758 if (ctx->xin->resubmit_stats) {
1759 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1760 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
1762 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
1769 flow_vlan_tci = flow->vlan_tci;
1770 flow_pkt_mark = flow->pkt_mark;
1771 flow_nw_tos = flow->nw_tos;
1773 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
1774 wc->masks.nw_tos |= IP_ECN_MASK;
1775 flow->nw_tos &= ~IP_DSCP_MASK;
1776 flow->nw_tos |= dscp;
1779 if (xport->is_tunnel) {
1780 /* Save tunnel metadata so that changes made due to
1781 * the Logical (tunnel) Port are not visible for any further
1782 * matches, while explicit set actions on tunnel metadata are.
1784 struct flow_tnl flow_tnl = flow->tunnel;
1785 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
1786 if (odp_port == ODPP_NONE) {
1787 xlate_report(ctx, "Tunneling decided against output");
1788 goto out; /* restore flow_nw_tos */
1790 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
1791 xlate_report(ctx, "Not tunneling to our own address");
1792 goto out; /* restore flow_nw_tos */
1794 if (ctx->xin->resubmit_stats) {
1795 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1797 out_port = odp_port;
1798 commit_odp_tunnel_action(flow, &ctx->base_flow,
1799 &ctx->xout->odp_actions);
1800 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
1802 ofp_port_t vlandev_port;
1804 odp_port = xport->odp_port;
1805 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
1806 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1808 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto, ofp_port,
1810 if (vlandev_port == ofp_port) {
1811 out_port = odp_port;
1813 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
1814 flow->vlan_tci = htons(0);
1818 if (out_port != ODPP_NONE) {
1819 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
1820 &ctx->xout->odp_actions,
1822 &ctx->mpls_depth_delta);
1823 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
1826 ctx->sflow_odp_port = odp_port;
1827 ctx->sflow_n_outputs++;
1828 ctx->xout->nf_output_iface = ofp_port;
1833 flow->vlan_tci = flow_vlan_tci;
1834 flow->pkt_mark = flow_pkt_mark;
1835 flow->nw_tos = flow_nw_tos;
1839 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
1841 compose_output_action__(ctx, ofp_port, true);
1845 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
1847 struct rule_dpif *old_rule = ctx->rule;
1848 struct rule_actions *actions;
1850 if (ctx->xin->resubmit_stats) {
1851 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
1857 actions = rule_dpif_get_actions(rule);
1858 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
1859 rule_actions_unref(actions);
1860 ctx->rule = old_rule;
1865 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
1867 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1869 if (ctx->recurse >= MAX_RESUBMIT_RECURSION) {
1870 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
1871 MAX_RESUBMIT_RECURSION);
1872 } else if (ctx->resubmits >= MAX_RESUBMITS) {
1873 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
1874 } else if (ctx->xout->odp_actions.size > UINT16_MAX) {
1875 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
1876 } else if (ctx->stack.size >= 65536) {
1877 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
1886 xlate_table_action(struct xlate_ctx *ctx,
1887 ofp_port_t in_port, uint8_t table_id, bool may_packet_in)
1889 if (xlate_resubmit_resource_check(ctx)) {
1890 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
1891 bool skip_wildcards = ctx->xin->skip_wildcards;
1892 uint8_t old_table_id = ctx->table_id;
1893 struct rule_dpif *rule;
1895 ctx->table_id = table_id;
1897 /* Look up a flow with 'in_port' as the input port. Then restore the
1898 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
1899 * have surprising behavior). */
1900 ctx->xin->flow.in_port.ofp_port = in_port;
1901 rule_dpif_lookup_in_table(ctx->xbridge->ofproto, &ctx->xin->flow,
1902 !skip_wildcards ? &ctx->xout->wc : NULL,
1904 ctx->xin->flow.in_port.ofp_port = old_in_port;
1906 if (ctx->xin->resubmit_hook) {
1907 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
1910 if (!rule && may_packet_in) {
1911 struct xport *xport;
1914 * check if table configuration flags
1915 * OFPTC11_TABLE_MISS_CONTROLLER, default.
1916 * OFPTC11_TABLE_MISS_CONTINUE,
1917 * OFPTC11_TABLE_MISS_DROP
1918 * When OF1.0, OFPTC11_TABLE_MISS_CONTINUE is used. What to do? */
1919 xport = get_ofp_port(ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
1920 choose_miss_rule(xport ? xport->config : 0,
1921 ctx->xbridge->miss_rule,
1922 ctx->xbridge->no_packet_in_rule, &rule);
1925 xlate_recursively(ctx, rule);
1926 rule_dpif_unref(rule);
1929 ctx->table_id = old_table_id;
1937 xlate_group_bucket(struct xlate_ctx *ctx, const struct ofputil_bucket *bucket)
1939 uint64_t action_list_stub[1024 / 8];
1940 struct ofpbuf action_list, action_set;
1942 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
1943 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
1945 ofpacts_execute_action_set(&action_list, &action_set);
1947 do_xlate_actions(action_list.data, action_list.size, ctx);
1950 ofpbuf_uninit(&action_set);
1951 ofpbuf_uninit(&action_list);
1955 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
1957 const struct ofputil_bucket *bucket;
1958 const struct list *buckets;
1959 struct flow old_flow = ctx->xin->flow;
1961 group_dpif_get_buckets(group, &buckets);
1963 LIST_FOR_EACH (bucket, list_node, buckets) {
1964 xlate_group_bucket(ctx, bucket);
1965 /* Roll back flow to previous state.
1966 * This is equivalent to cloning the packet for each bucket.
1968 * As a side effect any subsequently applied actions will
1969 * also effectively be applied to a clone of the packet taken
1970 * just before applying the all or indirect group. */
1971 ctx->xin->flow = old_flow;
1976 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
1978 const struct ofputil_bucket *bucket;
1980 bucket = group_first_live_bucket(ctx, group, 0);
1982 xlate_group_bucket(ctx, bucket);
1987 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
1989 struct flow_wildcards *wc = &ctx->xout->wc;
1990 const struct ofputil_bucket *bucket;
1993 basis = hash_bytes(ctx->xin->flow.dl_dst, sizeof ctx->xin->flow.dl_dst, 0);
1994 bucket = group_best_live_bucket(ctx, group, basis);
1996 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1997 xlate_group_bucket(ctx, bucket);
2002 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
2004 switch (group_dpif_get_type(group)) {
2006 case OFPGT11_INDIRECT:
2007 xlate_all_group(ctx, group);
2009 case OFPGT11_SELECT:
2010 xlate_select_group(ctx, group);
2013 xlate_ff_group(ctx, group);
2018 group_dpif_release(group);
2022 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
2024 if (xlate_resubmit_resource_check(ctx)) {
2025 struct group_dpif *group;
2028 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
2030 xlate_group_action__(ctx, group);
2040 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
2041 const struct ofpact_resubmit *resubmit)
2046 in_port = resubmit->in_port;
2047 if (in_port == OFPP_IN_PORT) {
2048 in_port = ctx->xin->flow.in_port.ofp_port;
2051 table_id = resubmit->table_id;
2052 if (table_id == 255) {
2053 table_id = ctx->table_id;
2056 xlate_table_action(ctx, in_port, table_id, false);
2060 flood_packets(struct xlate_ctx *ctx, bool all)
2062 const struct xport *xport;
2064 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
2065 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
2070 compose_output_action__(ctx, xport->ofp_port, false);
2071 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
2072 compose_output_action(ctx, xport->ofp_port);
2076 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2080 execute_controller_action(struct xlate_ctx *ctx, int len,
2081 enum ofp_packet_in_reason reason,
2082 uint16_t controller_id)
2084 struct ofproto_packet_in *pin;
2085 struct ofpbuf *packet;
2088 ctx->xout->slow |= SLOW_CONTROLLER;
2089 if (!ctx->xin->packet) {
2093 packet = ofpbuf_clone(ctx->xin->packet);
2095 key.skb_priority = 0;
2097 memset(&key.tunnel, 0, sizeof key.tunnel);
2099 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2100 &ctx->xout->odp_actions,
2102 &ctx->mpls_depth_delta);
2104 odp_execute_actions(NULL, packet, &key, ctx->xout->odp_actions.data,
2105 ctx->xout->odp_actions.size, NULL, NULL);
2107 pin = xmalloc(sizeof *pin);
2108 pin->up.packet_len = packet->size;
2109 pin->up.packet = ofpbuf_steal_data(packet);
2110 pin->up.reason = reason;
2111 pin->up.table_id = ctx->table_id;
2112 pin->up.cookie = (ctx->rule
2113 ? rule_dpif_get_flow_cookie(ctx->rule)
2116 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
2118 pin->controller_id = controller_id;
2119 pin->send_len = len;
2120 pin->generated_by_table_miss = (ctx->rule
2121 && rule_dpif_is_table_miss(ctx->rule));
2122 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
2123 ofpbuf_delete(packet);
2127 compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
2129 struct flow_wildcards *wc = &ctx->xout->wc;
2130 struct flow *flow = &ctx->xin->flow;
2132 ovs_assert(eth_type_mpls(eth_type));
2134 /* If mpls_depth_delta is negative then an MPLS POP action has been
2135 * composed and the resulting MPLS label stack is unknown. This means
2136 * an MPLS PUSH action can't be composed as it needs to know either the
2137 * top-most MPLS LSE to use as a template for the new MPLS LSE, or that
2138 * there is no MPLS label stack present. Thus, stop processing.
2140 * If mpls_depth_delta is positive then an MPLS PUSH action has been
2141 * composed and no further MPLS PUSH action may be performed without
2142 * losing MPLS LSE and ether type information held in xtx->xin->flow.
2143 * Thus, stop processing.
2145 * If the MPLS LSE of the flow and base_flow differ then the MPLS LSE
2146 * has been updated. Performing a MPLS PUSH action may be would result in
2147 * losing MPLS LSE and ether type information held in xtx->xin->flow.
2148 * Thus, stop processing.
2150 * It is planned that in the future this case will be handled
2151 * by recirculation */
2152 if (ctx->mpls_depth_delta ||
2153 ctx->xin->flow.mpls_lse != ctx->base_flow.mpls_lse) {
2157 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2159 ctx->pre_push_mpls_lse = ctx->xin->flow.mpls_lse;
2161 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2162 flow->mpls_lse &= ~htonl(MPLS_BOS_MASK);
2167 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2168 label = htonl(0x2); /* IPV6 Explicit Null. */
2170 label = htonl(0x0); /* IPV4 Explicit Null. */
2172 wc->masks.nw_tos |= IP_DSCP_MASK;
2173 wc->masks.nw_ttl = 0xff;
2174 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
2175 ttl = flow->nw_ttl ? flow->nw_ttl : 0x40;
2176 flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
2178 flow->dl_type = eth_type;
2179 ctx->mpls_depth_delta++;
2185 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
2187 struct flow_wildcards *wc = &ctx->xout->wc;
2189 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2193 /* If mpls_depth_delta is negative then an MPLS POP action has been
2194 * composed. Performing another MPLS POP action
2195 * would result in losing ether type that results from
2196 * the already composed MPLS POP. Thus, stop processing.
2198 * It is planned that in the future this case will be handled
2199 * by recirculation */
2200 if (ctx->mpls_depth_delta < 0) {
2204 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2206 /* If mpls_depth_delta is positive then an MPLS PUSH action has been
2207 * executed and the previous MPLS LSE saved in ctx->pre_push_mpls_lse. The
2208 * flow's MPLS LSE should be restored to that value to allow any
2209 * subsequent actions that update of the LSE to be executed correctly.
2211 if (ctx->mpls_depth_delta > 0) {
2212 ctx->xin->flow.mpls_lse = ctx->pre_push_mpls_lse;
2215 ctx->xin->flow.dl_type = eth_type;
2216 ctx->mpls_depth_delta--;
2222 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
2224 struct flow *flow = &ctx->xin->flow;
2226 if (!is_ip_any(flow)) {
2230 ctx->xout->wc.masks.nw_ttl = 0xff;
2231 if (flow->nw_ttl > 1) {
2237 for (i = 0; i < ids->n_controllers; i++) {
2238 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
2242 /* Stop processing for current table. */
2248 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
2250 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2254 /* If mpls_depth_delta is negative then an MPLS POP action has been
2255 * executed and the resulting MPLS label stack is unknown. This means
2256 * a SET MPLS LABEL action can't be executed as it needs to manipulate
2257 * the top-most MPLS LSE. Thus, stop processing.
2259 * It is planned that in the future this case will be handled
2262 if (ctx->mpls_depth_delta < 0) {
2266 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_LABEL_MASK);
2267 set_mpls_lse_label(&ctx->xin->flow.mpls_lse, label);
2272 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
2274 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2278 /* If mpls_depth_delta is negative then an MPLS POP action has been
2279 * executed and the resulting MPLS label stack is unknown. This means
2280 * a SET MPLS TC action can't be executed as it needs to manipulate
2281 * the top-most MPLS LSE. Thus, stop processing.
2283 * It is planned that in the future this case will be handled
2286 if (ctx->mpls_depth_delta < 0) {
2290 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_TC_MASK);
2291 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse, tc);
2296 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
2298 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2302 /* If mpls_depth_delta is negative then an MPLS POP action has been
2303 * executed and the resulting MPLS label stack is unknown. This means
2304 * a SET MPLS TTL push action can't be executed as it needs to manipulate
2305 * the top-most MPLS LSE. Thus, stop processing.
2307 * It is planned that in the future this case will be handled
2310 if (ctx->mpls_depth_delta < 0) {
2314 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_TTL_MASK);
2315 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl);
2320 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
2322 struct flow *flow = &ctx->xin->flow;
2323 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse);
2324 struct flow_wildcards *wc = &ctx->xout->wc;
2326 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2328 if (!eth_type_mpls(flow->dl_type)) {
2334 set_mpls_lse_ttl(&flow->mpls_lse, ttl);
2337 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
2339 /* Stop processing for current table. */
2345 xlate_output_action(struct xlate_ctx *ctx,
2346 ofp_port_t port, uint16_t max_len, bool may_packet_in)
2348 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
2350 ctx->xout->nf_output_iface = NF_OUT_DROP;
2354 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
2357 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2364 flood_packets(ctx, false);
2367 flood_packets(ctx, true);
2369 case OFPP_CONTROLLER:
2370 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
2376 if (port != ctx->xin->flow.in_port.ofp_port) {
2377 compose_output_action(ctx, port);
2379 xlate_report(ctx, "skipping output to input port");
2384 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2385 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2386 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2387 ctx->xout->nf_output_iface = prev_nf_output_iface;
2388 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2389 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2390 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2395 xlate_output_reg_action(struct xlate_ctx *ctx,
2396 const struct ofpact_output_reg *or)
2398 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
2399 if (port <= UINT16_MAX) {
2400 union mf_subvalue value;
2402 memset(&value, 0xff, sizeof value);
2403 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
2404 xlate_output_action(ctx, u16_to_ofp(port),
2405 or->max_len, false);
2410 xlate_enqueue_action(struct xlate_ctx *ctx,
2411 const struct ofpact_enqueue *enqueue)
2413 ofp_port_t ofp_port = enqueue->port;
2414 uint32_t queue_id = enqueue->queue;
2415 uint32_t flow_priority, priority;
2418 /* Translate queue to priority. */
2419 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
2421 /* Fall back to ordinary output action. */
2422 xlate_output_action(ctx, enqueue->port, 0, false);
2426 /* Check output port. */
2427 if (ofp_port == OFPP_IN_PORT) {
2428 ofp_port = ctx->xin->flow.in_port.ofp_port;
2429 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
2433 /* Add datapath actions. */
2434 flow_priority = ctx->xin->flow.skb_priority;
2435 ctx->xin->flow.skb_priority = priority;
2436 compose_output_action(ctx, ofp_port);
2437 ctx->xin->flow.skb_priority = flow_priority;
2439 /* Update NetFlow output port. */
2440 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2441 ctx->xout->nf_output_iface = ofp_port;
2442 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2443 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2448 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
2450 uint32_t skb_priority;
2452 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
2453 ctx->xin->flow.skb_priority = skb_priority;
2455 /* Couldn't translate queue to a priority. Nothing to do. A warning
2456 * has already been logged. */
2461 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
2463 const struct xbridge *xbridge = xbridge_;
2474 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
2477 port = get_ofp_port(xbridge, ofp_port);
2478 return port ? port->may_enable : false;
2483 xlate_bundle_action(struct xlate_ctx *ctx,
2484 const struct ofpact_bundle *bundle)
2488 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
2490 CONST_CAST(struct xbridge *, ctx->xbridge));
2491 if (bundle->dst.field) {
2492 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
2495 xlate_output_action(ctx, port, 0, false);
2500 xlate_learn_action(struct xlate_ctx *ctx,
2501 const struct ofpact_learn *learn)
2503 uint64_t ofpacts_stub[1024 / 8];
2504 struct ofputil_flow_mod fm;
2505 struct ofpbuf ofpacts;
2507 ctx->xout->has_learn = true;
2509 learn_mask(learn, &ctx->xout->wc);
2511 if (!ctx->xin->may_learn) {
2515 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2516 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
2517 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, &fm);
2518 ofpbuf_uninit(&ofpacts);
2522 xlate_fin_timeout(struct xlate_ctx *ctx,
2523 const struct ofpact_fin_timeout *oft)
2525 if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
2526 rule_dpif_reduce_timeouts(ctx->rule, oft->fin_idle_timeout,
2527 oft->fin_hard_timeout);
2532 xlate_sample_action(struct xlate_ctx *ctx,
2533 const struct ofpact_sample *os)
2535 union user_action_cookie cookie;
2536 /* Scale the probability from 16-bit to 32-bit while representing
2537 * the same percentage. */
2538 uint32_t probability = (os->probability << 16) | os->probability;
2540 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2541 &ctx->xout->odp_actions,
2543 &ctx->mpls_depth_delta);
2545 compose_flow_sample_cookie(os->probability, os->collector_set_id,
2546 os->obs_domain_id, os->obs_point_id, &cookie);
2547 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
2548 probability, &cookie, sizeof cookie.flow_sample);
2552 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
2554 if (xport->config & (eth_addr_equals(ctx->xin->flow.dl_dst, eth_addr_stp)
2555 ? OFPUTIL_PC_NO_RECV_STP
2556 : OFPUTIL_PC_NO_RECV)) {
2560 /* Only drop packets here if both forwarding and learning are
2561 * disabled. If just learning is enabled, we need to have
2562 * OFPP_NORMAL and the learning action have a look at the packet
2563 * before we can drop it. */
2564 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
2572 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
2574 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2575 ofpbuf_put(&ctx->action_set, on->actions, ofpact_nest_get_action_len(on));
2576 ofpact_pad(&ctx->action_set);
2580 xlate_action_set(struct xlate_ctx *ctx)
2582 uint64_t action_list_stub[1024 / 64];
2583 struct ofpbuf action_list;
2585 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2586 ofpacts_execute_action_set(&action_list, &ctx->action_set);
2587 do_xlate_actions(action_list.data, action_list.size, ctx);
2588 ofpbuf_uninit(&action_list);
2592 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
2593 struct xlate_ctx *ctx)
2595 struct flow_wildcards *wc = &ctx->xout->wc;
2596 struct flow *flow = &ctx->xin->flow;
2597 const struct ofpact *a;
2599 /* dl_type already in the mask, not set below. */
2601 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2602 struct ofpact_controller *controller;
2603 const struct ofpact_metadata *metadata;
2604 const struct ofpact_set_field *set_field;
2605 const struct mf_field *mf;
2613 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
2614 ofpact_get_OUTPUT(a)->max_len, true);
2618 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
2623 case OFPACT_CONTROLLER:
2624 controller = ofpact_get_CONTROLLER(a);
2625 execute_controller_action(ctx, controller->max_len,
2627 controller->controller_id);
2630 case OFPACT_ENQUEUE:
2631 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
2634 case OFPACT_SET_VLAN_VID:
2635 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2636 if (flow->vlan_tci & htons(VLAN_CFI) ||
2637 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2638 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
2639 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
2644 case OFPACT_SET_VLAN_PCP:
2645 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
2646 if (flow->vlan_tci & htons(VLAN_CFI) ||
2647 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2648 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
2649 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
2650 << VLAN_PCP_SHIFT) | VLAN_CFI);
2654 case OFPACT_STRIP_VLAN:
2655 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2656 flow->vlan_tci = htons(0);
2659 case OFPACT_PUSH_VLAN:
2660 /* XXX 802.1AD(QinQ) */
2661 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2662 flow->vlan_tci = htons(VLAN_CFI);
2665 case OFPACT_SET_ETH_SRC:
2666 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2667 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2670 case OFPACT_SET_ETH_DST:
2671 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2672 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2675 case OFPACT_SET_IPV4_SRC:
2676 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2677 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2678 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2682 case OFPACT_SET_IPV4_DST:
2683 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2684 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2685 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
2689 case OFPACT_SET_IP_DSCP:
2690 if (is_ip_any(flow)) {
2691 wc->masks.nw_tos |= IP_DSCP_MASK;
2692 flow->nw_tos &= ~IP_DSCP_MASK;
2693 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
2697 case OFPACT_SET_IP_ECN:
2698 if (is_ip_any(flow)) {
2699 wc->masks.nw_tos |= IP_ECN_MASK;
2700 flow->nw_tos &= ~IP_ECN_MASK;
2701 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
2705 case OFPACT_SET_IP_TTL:
2706 if (is_ip_any(flow)) {
2707 wc->masks.nw_ttl = 0xff;
2708 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
2712 case OFPACT_SET_L4_SRC_PORT:
2713 if (is_ip_any(flow)) {
2714 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2715 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2716 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2720 case OFPACT_SET_L4_DST_PORT:
2721 if (is_ip_any(flow)) {
2722 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2723 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2724 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2728 case OFPACT_RESUBMIT:
2729 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
2732 case OFPACT_SET_TUNNEL:
2733 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2736 case OFPACT_SET_QUEUE:
2737 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2740 case OFPACT_POP_QUEUE:
2741 flow->skb_priority = ctx->orig_skb_priority;
2744 case OFPACT_REG_MOVE:
2745 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
2748 case OFPACT_REG_LOAD:
2749 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow, wc);
2752 case OFPACT_SET_FIELD:
2753 set_field = ofpact_get_SET_FIELD(a);
2754 mf = set_field->field;
2756 /* Set field action only ever overwrites packet's outermost
2757 * applicable header fields. Do nothing if no header exists. */
2758 if (mf->id == MFF_VLAN_VID) {
2759 wc->masks.vlan_tci |= htons(VLAN_CFI);
2760 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
2763 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
2764 /* 'dl_type' is already unwildcarded. */
2765 && !eth_type_mpls(flow->dl_type)) {
2769 mf_mask_field_and_prereqs(mf, &wc->masks);
2770 mf_set_flow_value(mf, &set_field->value, flow);
2773 case OFPACT_STACK_PUSH:
2774 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
2778 case OFPACT_STACK_POP:
2779 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
2783 case OFPACT_PUSH_MPLS:
2784 if (compose_mpls_push_action(ctx,
2785 ofpact_get_PUSH_MPLS(a)->ethertype)) {
2790 case OFPACT_POP_MPLS:
2791 if (compose_mpls_pop_action(ctx,
2792 ofpact_get_POP_MPLS(a)->ethertype)) {
2797 case OFPACT_SET_MPLS_LABEL:
2798 if (compose_set_mpls_label_action(ctx,
2799 ofpact_get_SET_MPLS_LABEL(a)->label)) {
2804 case OFPACT_SET_MPLS_TC:
2805 if (compose_set_mpls_tc_action(ctx,
2806 ofpact_get_SET_MPLS_TC(a)->tc)) {
2811 case OFPACT_SET_MPLS_TTL:
2812 if (compose_set_mpls_ttl_action(ctx,
2813 ofpact_get_SET_MPLS_TTL(a)->ttl)) {
2818 case OFPACT_DEC_MPLS_TTL:
2819 if (compose_dec_mpls_ttl_action(ctx)) {
2824 case OFPACT_DEC_TTL:
2825 wc->masks.nw_ttl = 0xff;
2826 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
2832 /* Nothing to do. */
2835 case OFPACT_MULTIPATH:
2836 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
2840 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
2843 case OFPACT_OUTPUT_REG:
2844 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
2848 xlate_learn_action(ctx, ofpact_get_LEARN(a));
2855 case OFPACT_FIN_TIMEOUT:
2856 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2857 ctx->xout->has_fin_timeout = true;
2858 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
2861 case OFPACT_CLEAR_ACTIONS:
2862 ofpbuf_clear(&ctx->action_set);
2865 case OFPACT_WRITE_ACTIONS:
2866 xlate_write_actions(ctx, a);
2869 case OFPACT_WRITE_METADATA:
2870 metadata = ofpact_get_WRITE_METADATA(a);
2871 flow->metadata &= ~metadata->mask;
2872 flow->metadata |= metadata->metadata & metadata->mask;
2876 /* Not implemented yet. */
2879 case OFPACT_GOTO_TABLE: {
2880 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
2882 ovs_assert(ctx->table_id < ogt->table_id);
2883 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2884 ogt->table_id, true);
2889 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
2896 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
2897 const struct flow *flow, struct rule_dpif *rule,
2898 uint16_t tcp_flags, const struct ofpbuf *packet)
2900 xin->ofproto = ofproto;
2902 xin->packet = packet;
2903 xin->may_learn = packet != NULL;
2905 xin->ofpacts = NULL;
2906 xin->ofpacts_len = 0;
2907 xin->tcp_flags = tcp_flags;
2908 xin->resubmit_hook = NULL;
2909 xin->report_hook = NULL;
2910 xin->resubmit_stats = NULL;
2911 xin->skip_wildcards = false;
2915 xlate_out_uninit(struct xlate_out *xout)
2918 ofpbuf_uninit(&xout->odp_actions);
2922 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
2923 * into datapath actions, using 'ctx', and discards the datapath actions. */
2925 xlate_actions_for_side_effects(struct xlate_in *xin)
2927 struct xlate_out xout;
2929 xlate_actions(xin, &xout);
2930 xlate_out_uninit(&xout);
2934 xlate_report(struct xlate_ctx *ctx, const char *s)
2936 if (ctx->xin->report_hook) {
2937 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
2942 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
2945 dst->slow = src->slow;
2946 dst->has_learn = src->has_learn;
2947 dst->has_normal = src->has_normal;
2948 dst->has_fin_timeout = src->has_fin_timeout;
2949 dst->nf_output_iface = src->nf_output_iface;
2950 dst->mirrors = src->mirrors;
2952 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
2953 sizeof dst->odp_actions_stub);
2954 ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
2955 src->odp_actions.size);
2958 static struct skb_priority_to_dscp *
2959 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
2961 struct skb_priority_to_dscp *pdscp;
2964 hash = hash_int(skb_priority, 0);
2965 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
2966 if (pdscp->skb_priority == skb_priority) {
2974 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
2977 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
2978 *dscp = pdscp ? pdscp->dscp : 0;
2979 return pdscp != NULL;
2983 clear_skb_priorities(struct xport *xport)
2985 struct skb_priority_to_dscp *pdscp, *next;
2987 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
2988 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
2994 actions_output_to_local_port(const struct xlate_ctx *ctx)
2996 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
2997 const struct nlattr *a;
3000 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions.data,
3001 ctx->xout->odp_actions.size) {
3002 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
3003 && nl_attr_get_odp_port(a) == local_odp_port) {
3010 /* Thread safe call to xlate_actions__(). */
3012 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
3014 ovs_rwlock_rdlock(&xlate_rwlock);
3015 xlate_actions__(xin, xout);
3016 ovs_rwlock_unlock(&xlate_rwlock);
3019 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
3020 * into datapath actions in 'odp_actions', using 'ctx'.
3022 * The caller must take responsibility for eventually freeing 'xout', with
3023 * xlate_out_uninit(). */
3025 xlate_actions__(struct xlate_in *xin, struct xlate_out *xout)
3026 OVS_REQ_RDLOCK(xlate_rwlock)
3028 struct flow_wildcards *wc = &xout->wc;
3029 struct flow *flow = &xin->flow;
3030 struct rule_dpif *rule = NULL;
3032 struct rule_actions *actions = NULL;
3033 enum slow_path_reason special;
3034 const struct ofpact *ofpacts;
3035 struct xport *in_port;
3036 struct flow orig_flow;
3037 struct xlate_ctx ctx;
3042 COVERAGE_INC(xlate_actions);
3044 /* Flow initialization rules:
3045 * - 'base_flow' must match the kernel's view of the packet at the
3046 * time that action processing starts. 'flow' represents any
3047 * transformations we wish to make through actions.
3048 * - By default 'base_flow' and 'flow' are the same since the input
3049 * packet matches the output before any actions are applied.
3050 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
3051 * of the received packet as seen by the kernel. If we later output
3052 * to another device without any modifications this will cause us to
3053 * insert a new tag since the original one was stripped off by the
3055 * - Tunnel metadata as received is retained in 'flow'. This allows
3056 * tunnel metadata matching also in later tables.
3057 * Since a kernel action for setting the tunnel metadata will only be
3058 * generated with actual tunnel output, changing the tunnel metadata
3059 * values in 'flow' (such as tun_id) will only have effect with a later
3060 * tunnel output action.
3061 * - Tunnel 'base_flow' is completely cleared since that is what the
3062 * kernel does. If we wish to maintain the original values an action
3063 * needs to be generated. */
3068 ctx.xout->has_learn = false;
3069 ctx.xout->has_normal = false;
3070 ctx.xout->has_fin_timeout = false;
3071 ctx.xout->nf_output_iface = NF_OUT_DROP;
3072 ctx.xout->mirrors = 0;
3073 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
3074 sizeof ctx.xout->odp_actions_stub);
3075 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
3077 ctx.xbridge = xbridge_lookup(xin->ofproto);
3082 ctx.rule = xin->rule;
3084 ctx.base_flow = *flow;
3085 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
3086 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
3088 flow_wildcards_init_catchall(wc);
3089 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
3090 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3091 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3092 if (is_ip_any(flow)) {
3093 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3095 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
3097 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
3098 if (ctx.xbridge->netflow) {
3099 netflow_mask_wc(flow, wc);
3104 ctx.orig_skb_priority = flow->skb_priority;
3107 ctx.mpls_depth_delta = 0;
3109 if (!xin->ofpacts && !ctx.rule) {
3110 rule_dpif_lookup(ctx.xbridge->ofproto, flow,
3111 !xin->skip_wildcards ? wc : NULL, &rule);
3112 if (ctx.xin->resubmit_stats) {
3113 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
3117 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
3120 ofpacts = xin->ofpacts;
3121 ofpacts_len = xin->ofpacts_len;
3122 } else if (ctx.rule) {
3123 actions = rule_dpif_get_actions(ctx.rule);
3124 ofpacts = actions->ofpacts;
3125 ofpacts_len = actions->ofpacts_len;
3130 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
3131 ofpbuf_use_stub(&ctx.action_set,
3132 ctx.action_set_stub, sizeof ctx.action_set_stub);
3134 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3135 /* Do this conditionally because the copy is expensive enough that it
3136 * shows up in profiles. */
3140 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3141 switch (ctx.xbridge->frag) {
3142 case OFPC_FRAG_NORMAL:
3143 /* We must pretend that transport ports are unavailable. */
3144 flow->tp_src = ctx.base_flow.tp_src = htons(0);
3145 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
3148 case OFPC_FRAG_DROP:
3151 case OFPC_FRAG_REASM:
3154 case OFPC_FRAG_NX_MATCH:
3155 /* Nothing to do. */
3158 case OFPC_INVALID_TTL_TO_CONTROLLER:
3163 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
3164 if (in_port && in_port->is_tunnel && ctx.xin->resubmit_stats) {
3165 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
3167 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
3171 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
3173 ctx.xout->slow |= special;
3175 size_t sample_actions_len;
3177 if (flow->in_port.ofp_port
3178 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
3179 flow->in_port.ofp_port,
3181 ctx.base_flow.vlan_tci = 0;
3184 add_sflow_action(&ctx);
3185 add_ipfix_action(&ctx);
3186 sample_actions_len = ctx.xout->odp_actions.size;
3188 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
3189 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
3191 /* We've let OFPP_NORMAL and the learning action look at the
3192 * packet, so drop it now if forwarding is disabled. */
3193 if (in_port && !xport_stp_forward_state(in_port)) {
3194 ctx.xout->odp_actions.size = sample_actions_len;
3198 if (ctx.action_set.size) {
3199 xlate_action_set(&ctx);
3202 if (ctx.xbridge->has_in_band
3203 && in_band_must_output_to_local_port(flow)
3204 && !actions_output_to_local_port(&ctx)) {
3205 compose_output_action(&ctx, OFPP_LOCAL);
3208 fix_sflow_action(&ctx);
3210 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3211 add_mirror_actions(&ctx, &orig_flow);
3215 if (nl_attr_oversized(ctx.xout->odp_actions.size)) {
3216 /* These datapath actions are too big for a Netlink attribute, so we
3217 * can't hand them to the kernel directly. dpif_execute() can execute
3218 * them one by one with help, so just mark the result as SLOW_ACTION to
3219 * prevent the flow from being installed. */
3220 COVERAGE_INC(xlate_actions_oversize);
3221 ctx.xout->slow |= SLOW_ACTION;
3224 if (ctx.xin->resubmit_stats) {
3225 mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
3226 ctx.xin->resubmit_stats->n_packets,
3227 ctx.xin->resubmit_stats->n_bytes);
3229 if (ctx.xbridge->netflow) {
3230 const struct ofpact *ofpacts;
3233 ofpacts_len = actions->ofpacts_len;
3234 ofpacts = actions->ofpacts;
3235 if (ofpacts_len == 0
3236 || ofpacts->type != OFPACT_CONTROLLER
3237 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
3238 /* Only update netflow if we don't have controller flow. We don't
3239 * report NetFlow expiration messages for such facets because they
3240 * are just part of the control logic for the network, not real
3242 netflow_flow_update(ctx.xbridge->netflow, flow,
3243 xout->nf_output_iface,
3244 ctx.xin->resubmit_stats);
3249 ofpbuf_uninit(&ctx.stack);
3250 ofpbuf_uninit(&ctx.action_set);
3252 /* Clear the metadata and register wildcard masks, because we won't
3253 * use non-header fields as part of the cache. */
3254 flow_wildcards_clear_non_packet_fields(wc);
3256 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow uses
3257 * the low 8 bits of the 16-bit tp_src and tp_dst members to represent
3258 * these fields. The datapath interface, on the other hand, represents
3259 * them with just 8 bits each. This means that if the high 8 bits of the
3260 * masks for these fields somehow become set, then they will get chopped
3261 * off by a round trip through the datapath, and revalidation will spot
3262 * that as an inconsistency and delete the flow. Avoid the problem here by
3263 * making sure that only the low 8 bits of either field can be unwildcarded
3267 wc->masks.tp_src &= htons(UINT8_MAX);
3268 wc->masks.tp_dst &= htons(UINT8_MAX);
3272 rule_actions_unref(actions);
3273 rule_dpif_unref(rule);
3276 /* Sends 'packet' out 'ofport'.
3277 * May modify 'packet'.
3278 * Returns 0 if successful, otherwise a positive errno value. */
3280 xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3282 struct xport *xport;
3283 struct ofpact_output output;
3285 union flow_in_port in_port_;
3287 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
3288 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
3289 in_port_.ofp_port = OFPP_NONE;
3290 flow_extract(packet, 0, 0, NULL, &in_port_, &flow);
3292 ovs_rwlock_rdlock(&xlate_rwlock);
3293 xport = xport_lookup(ofport);
3295 ovs_rwlock_unlock(&xlate_rwlock);
3298 output.port = xport->ofp_port;
3300 ovs_rwlock_unlock(&xlate_rwlock);
3302 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
3303 &output.ofpact, sizeof output,