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? */
91 /* True if the datapath supports variable-length
92 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
93 * False if the datapath supports only 8-byte (or shorter) userdata. */
94 bool variable_length_userdata;
98 struct hmap_node hmap_node; /* In global 'xbundles' map. */
99 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
101 struct list list_node; /* In parent 'xbridges' list. */
102 struct xbridge *xbridge; /* Parent xbridge. */
104 struct list xports; /* Contains "struct xport"s. */
106 char *name; /* Name used in log messages. */
107 struct bond *bond; /* Nonnull iff more than one port. */
108 struct lacp *lacp; /* LACP handle or null. */
110 enum port_vlan_mode vlan_mode; /* VLAN mode. */
111 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
112 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
113 * NULL if all VLANs are trunked. */
114 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
115 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
119 struct hmap_node hmap_node; /* Node in global 'xports' map. */
120 struct ofport_dpif *ofport; /* Key in global 'xports map. */
122 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
123 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
125 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
127 struct list bundle_node; /* In parent xbundle (if it exists). */
128 struct xbundle *xbundle; /* Parent xbundle or null. */
130 struct netdev *netdev; /* 'ofport''s netdev. */
132 struct xbridge *xbridge; /* Parent bridge. */
133 struct xport *peer; /* Patch port peer or null. */
135 enum ofputil_port_config config; /* OpenFlow port configuration. */
136 enum ofputil_port_state state; /* OpenFlow port state. */
137 int stp_port_no; /* STP port number or -1 if not in use. */
139 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
141 bool may_enable; /* May be enabled in bonds. */
142 bool is_tunnel; /* Is a tunnel port. */
144 struct cfm *cfm; /* CFM handle or null. */
145 struct bfd *bfd; /* BFD handle or null. */
149 struct xlate_in *xin;
150 struct xlate_out *xout;
152 const struct xbridge *xbridge;
154 /* Flow at the last commit. */
155 struct flow base_flow;
157 /* Tunnel IP destination address as received. This is stored separately
158 * as the base_flow.tunnel is cleared on init to reflect the datapath
159 * behavior. Used to make sure not to send tunneled output to ourselves,
160 * which might lead to an infinite loop. This could happen easily
161 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
162 * actually set the tun_dst field. */
163 ovs_be32 orig_tunnel_ip_dst;
165 /* Stack for the push and pop actions. Each stack element is of type
166 * "union mf_subvalue". */
167 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
170 /* The rule that we are currently translating, or NULL. */
171 struct rule_dpif *rule;
173 int mpls_depth_delta; /* Delta of the mpls stack depth since
174 * actions were last committed.
175 * Must be between -1 and 1 inclusive. */
176 ovs_be32 pre_push_mpls_lse; /* Used to record the top-most MPLS LSE
177 * prior to an mpls_push so that it may be
178 * used for a subsequent mpls_pop. */
180 /* Resubmit statistics, via xlate_table_action(). */
181 int recurse; /* Current resubmit nesting depth. */
182 int resubmits; /* Total number of resubmits. */
184 uint32_t orig_skb_priority; /* Priority when packet arrived. */
185 uint8_t table_id; /* OpenFlow table ID where flow was found. */
186 uint32_t sflow_n_outputs; /* Number of output ports. */
187 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
188 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
189 bool exit; /* No further actions should be processed. */
191 /* OpenFlow 1.1+ action set.
193 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
194 * When translation is otherwise complete, ofpacts_execute_action_set()
195 * converts it to a set of "struct ofpact"s that can be translated into
196 * datapath actions. */
197 struct ofpbuf action_set; /* Action set. */
198 uint64_t action_set_stub[1024 / 8];
201 /* A controller may use OFPP_NONE as the ingress port to indicate that
202 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
203 * when an input bundle is needed for validation (e.g., mirroring or
204 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
205 * any 'port' structs, so care must be taken when dealing with it.
206 * The bundle's name and vlan mode are initialized in lookup_input_bundle() */
207 static struct xbundle ofpp_none_bundle;
209 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
210 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
211 * traffic egressing the 'ofport' with that priority should be marked with. */
212 struct skb_priority_to_dscp {
213 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
214 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
216 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
219 static struct hmap xbridges = HMAP_INITIALIZER(&xbridges);
220 static struct hmap xbundles = HMAP_INITIALIZER(&xbundles);
221 static struct hmap xports = HMAP_INITIALIZER(&xports);
223 static bool may_receive(const struct xport *, struct xlate_ctx *);
224 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
226 static void xlate_actions__(struct xlate_in *, struct xlate_out *)
227 OVS_REQ_RDLOCK(xlate_rwlock);
228 static void xlate_normal(struct xlate_ctx *);
229 static void xlate_report(struct xlate_ctx *, const char *);
230 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
231 uint8_t table_id, bool may_packet_in);
232 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
233 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
234 static void output_normal(struct xlate_ctx *, const struct xbundle *,
236 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
238 static struct xbridge *xbridge_lookup(const struct ofproto_dpif *);
239 static struct xbundle *xbundle_lookup(const struct ofbundle *);
240 static struct xport *xport_lookup(const struct ofport_dpif *);
241 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
242 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
243 uint32_t skb_priority);
244 static void clear_skb_priorities(struct xport *);
245 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
249 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
250 struct dpif *dpif, struct rule_dpif *miss_rule,
251 struct rule_dpif *no_packet_in_rule,
252 const struct mac_learning *ml, struct stp *stp,
253 const struct mbridge *mbridge,
254 const struct dpif_sflow *sflow,
255 const struct dpif_ipfix *ipfix,
256 const struct netflow *netflow, enum ofp_config_flags frag,
257 bool forward_bpdu, bool has_in_band,
258 bool variable_length_userdata)
260 struct xbridge *xbridge = xbridge_lookup(ofproto);
263 xbridge = xzalloc(sizeof *xbridge);
264 xbridge->ofproto = ofproto;
266 hmap_insert(&xbridges, &xbridge->hmap_node, hash_pointer(ofproto, 0));
267 hmap_init(&xbridge->xports);
268 list_init(&xbridge->xbundles);
271 if (xbridge->ml != ml) {
272 mac_learning_unref(xbridge->ml);
273 xbridge->ml = mac_learning_ref(ml);
276 if (xbridge->mbridge != mbridge) {
277 mbridge_unref(xbridge->mbridge);
278 xbridge->mbridge = mbridge_ref(mbridge);
281 if (xbridge->sflow != sflow) {
282 dpif_sflow_unref(xbridge->sflow);
283 xbridge->sflow = dpif_sflow_ref(sflow);
286 if (xbridge->ipfix != ipfix) {
287 dpif_ipfix_unref(xbridge->ipfix);
288 xbridge->ipfix = dpif_ipfix_ref(ipfix);
291 if (xbridge->stp != stp) {
292 stp_unref(xbridge->stp);
293 xbridge->stp = stp_ref(stp);
296 if (xbridge->netflow != netflow) {
297 netflow_unref(xbridge->netflow);
298 xbridge->netflow = netflow_ref(netflow);
302 xbridge->name = xstrdup(name);
304 xbridge->dpif = dpif;
305 xbridge->forward_bpdu = forward_bpdu;
306 xbridge->has_in_band = has_in_band;
307 xbridge->frag = frag;
308 xbridge->miss_rule = miss_rule;
309 xbridge->no_packet_in_rule = no_packet_in_rule;
310 xbridge->variable_length_userdata = variable_length_userdata;
314 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
316 struct xbridge *xbridge = xbridge_lookup(ofproto);
317 struct xbundle *xbundle, *next_xbundle;
318 struct xport *xport, *next_xport;
324 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
325 xlate_ofport_remove(xport->ofport);
328 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
329 xlate_bundle_remove(xbundle->ofbundle);
332 hmap_remove(&xbridges, &xbridge->hmap_node);
333 mac_learning_unref(xbridge->ml);
334 mbridge_unref(xbridge->mbridge);
335 dpif_sflow_unref(xbridge->sflow);
336 dpif_ipfix_unref(xbridge->ipfix);
337 stp_unref(xbridge->stp);
338 hmap_destroy(&xbridge->xports);
344 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
345 const char *name, enum port_vlan_mode vlan_mode, int vlan,
346 unsigned long *trunks, bool use_priority_tags,
347 const struct bond *bond, const struct lacp *lacp,
350 struct xbundle *xbundle = xbundle_lookup(ofbundle);
353 xbundle = xzalloc(sizeof *xbundle);
354 xbundle->ofbundle = ofbundle;
355 xbundle->xbridge = xbridge_lookup(ofproto);
357 hmap_insert(&xbundles, &xbundle->hmap_node, hash_pointer(ofbundle, 0));
358 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
359 list_init(&xbundle->xports);
362 ovs_assert(xbundle->xbridge);
365 xbundle->name = xstrdup(name);
367 xbundle->vlan_mode = vlan_mode;
368 xbundle->vlan = vlan;
369 xbundle->trunks = trunks;
370 xbundle->use_priority_tags = use_priority_tags;
371 xbundle->floodable = floodable;
373 if (xbundle->bond != bond) {
374 bond_unref(xbundle->bond);
375 xbundle->bond = bond_ref(bond);
378 if (xbundle->lacp != lacp) {
379 lacp_unref(xbundle->lacp);
380 xbundle->lacp = lacp_ref(lacp);
385 xlate_bundle_remove(struct ofbundle *ofbundle)
387 struct xbundle *xbundle = xbundle_lookup(ofbundle);
388 struct xport *xport, *next;
394 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
395 list_remove(&xport->bundle_node);
396 xport->xbundle = NULL;
399 hmap_remove(&xbundles, &xbundle->hmap_node);
400 list_remove(&xbundle->list_node);
401 bond_unref(xbundle->bond);
402 lacp_unref(xbundle->lacp);
408 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
409 struct ofport_dpif *ofport, ofp_port_t ofp_port,
410 odp_port_t odp_port, const struct netdev *netdev,
411 const struct cfm *cfm, const struct bfd *bfd,
412 struct ofport_dpif *peer, int stp_port_no,
413 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
414 enum ofputil_port_config config,
415 enum ofputil_port_state state, bool is_tunnel,
418 struct xport *xport = xport_lookup(ofport);
422 xport = xzalloc(sizeof *xport);
423 xport->ofport = ofport;
424 xport->xbridge = xbridge_lookup(ofproto);
425 xport->ofp_port = ofp_port;
427 hmap_init(&xport->skb_priorities);
428 hmap_insert(&xports, &xport->hmap_node, hash_pointer(ofport, 0));
429 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
430 hash_ofp_port(xport->ofp_port));
433 ovs_assert(xport->ofp_port == ofp_port);
435 xport->config = config;
436 xport->state = state;
437 xport->stp_port_no = stp_port_no;
438 xport->is_tunnel = is_tunnel;
439 xport->may_enable = may_enable;
440 xport->odp_port = odp_port;
442 if (xport->netdev != netdev) {
443 netdev_close(xport->netdev);
444 xport->netdev = netdev_ref(netdev);
447 if (xport->cfm != cfm) {
448 cfm_unref(xport->cfm);
449 xport->cfm = cfm_ref(cfm);
452 if (xport->bfd != bfd) {
453 bfd_unref(xport->bfd);
454 xport->bfd = bfd_ref(bfd);
458 xport->peer->peer = NULL;
460 xport->peer = xport_lookup(peer);
462 xport->peer->peer = xport;
465 if (xport->xbundle) {
466 list_remove(&xport->bundle_node);
468 xport->xbundle = xbundle_lookup(ofbundle);
469 if (xport->xbundle) {
470 list_insert(&xport->xbundle->xports, &xport->bundle_node);
473 clear_skb_priorities(xport);
474 for (i = 0; i < n_qdscp; i++) {
475 struct skb_priority_to_dscp *pdscp;
476 uint32_t skb_priority;
478 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
483 pdscp = xmalloc(sizeof *pdscp);
484 pdscp->skb_priority = skb_priority;
485 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
486 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
487 hash_int(pdscp->skb_priority, 0));
492 xlate_ofport_remove(struct ofport_dpif *ofport)
494 struct xport *xport = xport_lookup(ofport);
501 xport->peer->peer = NULL;
505 if (xport->xbundle) {
506 list_remove(&xport->bundle_node);
509 clear_skb_priorities(xport);
510 hmap_destroy(&xport->skb_priorities);
512 hmap_remove(&xports, &xport->hmap_node);
513 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
515 netdev_close(xport->netdev);
516 cfm_unref(xport->cfm);
517 bfd_unref(xport->bfd);
521 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
522 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
523 * Optionally, if nonnull, populates 'fitnessp' with the fitness of 'flow' as
524 * returned by odp_flow_key_to_flow(). Also, optionally populates 'ofproto'
525 * with the ofproto_dpif, 'odp_in_port' with the datapath in_port, that
526 * 'packet' ingressed, and 'ipfix', 'sflow', and 'netflow' with the appropriate
527 * handles for those protocols if they're enabled. Caller is responsible for
530 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
531 * 'flow''s in_port to OFPP_NONE.
533 * This function does post-processing on data returned from
534 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
535 * of the upcall processing logic. In particular, if the extracted in_port is
536 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
537 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
538 * a VLAN header onto 'packet' (if it is nonnull).
540 * Similarly, this function also includes some logic to help with tunnels. It
541 * may modify 'flow' as necessary to make the tunneling implementation
542 * transparent to the upcall processing logic.
544 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
545 * or some other positive errno if there are other problems. */
547 xlate_receive(const struct dpif_backer *backer, struct ofpbuf *packet,
548 const struct nlattr *key, size_t key_len,
549 struct flow *flow, enum odp_key_fitness *fitnessp,
550 struct ofproto_dpif **ofproto, struct dpif_ipfix **ipfix,
551 struct dpif_sflow **sflow, struct netflow **netflow,
552 odp_port_t *odp_in_port)
554 enum odp_key_fitness fitness;
555 const struct xport *xport;
558 ovs_rwlock_rdlock(&xlate_rwlock);
559 fitness = odp_flow_key_to_flow(key, key_len, flow);
560 if (fitness == ODP_FIT_ERROR) {
566 *odp_in_port = flow->in_port.odp_port;
569 xport = xport_lookup(tnl_port_should_receive(flow)
570 ? tnl_port_receive(flow)
571 : odp_port_to_ofport(backer, flow->in_port.odp_port));
573 flow->in_port.ofp_port = xport ? xport->ofp_port : OFPP_NONE;
578 if (vsp_adjust_flow(xport->xbridge->ofproto, flow)) {
580 /* Make the packet resemble the flow, so that it gets sent to
581 * an OpenFlow controller properly, so that it looks correct
582 * for sFlow, and so that flow_extract() will get the correct
583 * vlan_tci if it is called on 'packet'. */
584 eth_push_vlan(packet, flow->vlan_tci);
586 /* We can't reproduce 'key' from 'flow'. */
587 fitness = fitness == ODP_FIT_PERFECT ? ODP_FIT_TOO_MUCH : fitness;
592 *ofproto = xport->xbridge->ofproto;
596 *ipfix = dpif_ipfix_ref(xport->xbridge->ipfix);
600 *sflow = dpif_sflow_ref(xport->xbridge->sflow);
604 *netflow = netflow_ref(xport->xbridge->netflow);
611 ovs_rwlock_unlock(&xlate_rwlock);
615 static struct xbridge *
616 xbridge_lookup(const struct ofproto_dpif *ofproto)
618 struct xbridge *xbridge;
624 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
626 if (xbridge->ofproto == ofproto) {
633 static struct xbundle *
634 xbundle_lookup(const struct ofbundle *ofbundle)
636 struct xbundle *xbundle;
642 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
644 if (xbundle->ofbundle == ofbundle) {
651 static struct xport *
652 xport_lookup(const struct ofport_dpif *ofport)
660 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
662 if (xport->ofport == ofport) {
669 static struct stp_port *
670 xport_get_stp_port(const struct xport *xport)
672 return xport->xbridge->stp && xport->stp_port_no != -1
673 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
677 static enum stp_state
678 xport_stp_learn_state(const struct xport *xport)
680 struct stp_port *sp = xport_get_stp_port(xport);
681 return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
685 xport_stp_forward_state(const struct xport *xport)
687 struct stp_port *sp = xport_get_stp_port(xport);
688 return stp_forward_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 && !xport_stp_forward_state(xport)) {
1706 xlate_report(ctx, "STP not in forwarding state, skipping output");
1710 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
1711 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
1716 const struct xport *peer = xport->peer;
1717 struct flow old_flow = ctx->xin->flow;
1718 enum slow_path_reason special;
1720 ctx->xbridge = peer->xbridge;
1721 flow->in_port.ofp_port = peer->ofp_port;
1722 flow->metadata = htonll(0);
1723 memset(&flow->tunnel, 0, sizeof flow->tunnel);
1724 memset(flow->regs, 0, sizeof flow->regs);
1726 special = process_special(ctx, &ctx->xin->flow, peer,
1729 ctx->xout->slow |= special;
1730 } else if (may_receive(peer, ctx)) {
1731 if (xport_stp_forward_state(peer)) {
1732 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1734 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
1735 * learning action look at the packet, then drop it. */
1736 struct flow old_base_flow = ctx->base_flow;
1737 size_t old_size = ctx->xout->odp_actions.size;
1738 mirror_mask_t old_mirrors = ctx->xout->mirrors;
1739 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1740 ctx->xout->mirrors = old_mirrors;
1741 ctx->base_flow = old_base_flow;
1742 ctx->xout->odp_actions.size = old_size;
1746 ctx->xin->flow = old_flow;
1747 ctx->xbridge = xport->xbridge;
1749 if (ctx->xin->resubmit_stats) {
1750 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1751 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
1753 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
1760 flow_vlan_tci = flow->vlan_tci;
1761 flow_pkt_mark = flow->pkt_mark;
1762 flow_nw_tos = flow->nw_tos;
1764 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
1765 wc->masks.nw_tos |= IP_ECN_MASK;
1766 flow->nw_tos &= ~IP_DSCP_MASK;
1767 flow->nw_tos |= dscp;
1770 if (xport->is_tunnel) {
1771 /* Save tunnel metadata so that changes made due to
1772 * the Logical (tunnel) Port are not visible for any further
1773 * matches, while explicit set actions on tunnel metadata are.
1775 struct flow_tnl flow_tnl = flow->tunnel;
1776 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
1777 if (odp_port == ODPP_NONE) {
1778 xlate_report(ctx, "Tunneling decided against output");
1779 goto out; /* restore flow_nw_tos */
1781 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
1782 xlate_report(ctx, "Not tunneling to our own address");
1783 goto out; /* restore flow_nw_tos */
1785 if (ctx->xin->resubmit_stats) {
1786 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1788 out_port = odp_port;
1789 commit_odp_tunnel_action(flow, &ctx->base_flow,
1790 &ctx->xout->odp_actions);
1791 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
1793 ofp_port_t vlandev_port;
1795 odp_port = xport->odp_port;
1796 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
1797 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1799 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto, ofp_port,
1801 if (vlandev_port == ofp_port) {
1802 out_port = odp_port;
1804 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
1805 flow->vlan_tci = htons(0);
1809 if (out_port != ODPP_NONE) {
1810 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
1811 &ctx->xout->odp_actions,
1813 &ctx->mpls_depth_delta);
1814 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
1817 ctx->sflow_odp_port = odp_port;
1818 ctx->sflow_n_outputs++;
1819 ctx->xout->nf_output_iface = ofp_port;
1824 flow->vlan_tci = flow_vlan_tci;
1825 flow->pkt_mark = flow_pkt_mark;
1826 flow->nw_tos = flow_nw_tos;
1830 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
1832 compose_output_action__(ctx, ofp_port, true);
1836 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
1838 struct rule_dpif *old_rule = ctx->rule;
1839 struct rule_actions *actions;
1841 if (ctx->xin->resubmit_stats) {
1842 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
1848 actions = rule_dpif_get_actions(rule);
1849 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
1850 rule_actions_unref(actions);
1851 ctx->rule = old_rule;
1856 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
1858 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1860 if (ctx->recurse >= MAX_RESUBMIT_RECURSION) {
1861 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
1862 MAX_RESUBMIT_RECURSION);
1863 } else if (ctx->resubmits >= MAX_RESUBMITS) {
1864 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
1865 } else if (ctx->xout->odp_actions.size > UINT16_MAX) {
1866 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
1867 } else if (ctx->stack.size >= 65536) {
1868 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
1877 xlate_table_action(struct xlate_ctx *ctx,
1878 ofp_port_t in_port, uint8_t table_id, bool may_packet_in)
1880 if (xlate_resubmit_resource_check(ctx)) {
1881 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
1882 bool skip_wildcards = ctx->xin->skip_wildcards;
1883 uint8_t old_table_id = ctx->table_id;
1884 struct rule_dpif *rule;
1886 ctx->table_id = table_id;
1888 /* Look up a flow with 'in_port' as the input port. Then restore the
1889 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
1890 * have surprising behavior). */
1891 ctx->xin->flow.in_port.ofp_port = in_port;
1892 rule_dpif_lookup_in_table(ctx->xbridge->ofproto, &ctx->xin->flow,
1893 !skip_wildcards ? &ctx->xout->wc : NULL,
1895 ctx->xin->flow.in_port.ofp_port = old_in_port;
1897 if (ctx->xin->resubmit_hook) {
1898 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
1901 if (!rule && may_packet_in) {
1902 struct xport *xport;
1905 * check if table configuration flags
1906 * OFPTC11_TABLE_MISS_CONTROLLER, default.
1907 * OFPTC11_TABLE_MISS_CONTINUE,
1908 * OFPTC11_TABLE_MISS_DROP
1909 * When OF1.0, OFPTC11_TABLE_MISS_CONTINUE is used. What to do? */
1910 xport = get_ofp_port(ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
1911 choose_miss_rule(xport ? xport->config : 0,
1912 ctx->xbridge->miss_rule,
1913 ctx->xbridge->no_packet_in_rule, &rule);
1916 xlate_recursively(ctx, rule);
1917 rule_dpif_unref(rule);
1920 ctx->table_id = old_table_id;
1928 xlate_group_bucket(struct xlate_ctx *ctx, const struct ofputil_bucket *bucket)
1930 uint64_t action_list_stub[1024 / 8];
1931 struct ofpbuf action_list, action_set;
1933 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
1934 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
1936 ofpacts_execute_action_set(&action_list, &action_set);
1938 do_xlate_actions(action_list.data, action_list.size, ctx);
1941 ofpbuf_uninit(&action_set);
1942 ofpbuf_uninit(&action_list);
1946 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
1948 const struct ofputil_bucket *bucket;
1949 const struct list *buckets;
1950 struct flow old_flow = ctx->xin->flow;
1952 group_dpif_get_buckets(group, &buckets);
1954 LIST_FOR_EACH (bucket, list_node, buckets) {
1955 xlate_group_bucket(ctx, bucket);
1956 /* Roll back flow to previous state.
1957 * This is equivalent to cloning the packet for each bucket.
1959 * As a side effect any subsequently applied actions will
1960 * also effectively be applied to a clone of the packet taken
1961 * just before applying the all or indirect group. */
1962 ctx->xin->flow = old_flow;
1967 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
1969 const struct ofputil_bucket *bucket;
1971 bucket = group_first_live_bucket(ctx, group, 0);
1973 xlate_group_bucket(ctx, bucket);
1978 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
1980 struct flow_wildcards *wc = &ctx->xout->wc;
1981 const struct ofputil_bucket *bucket;
1984 basis = hash_bytes(ctx->xin->flow.dl_dst, sizeof ctx->xin->flow.dl_dst, 0);
1985 bucket = group_best_live_bucket(ctx, group, basis);
1987 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1988 xlate_group_bucket(ctx, bucket);
1993 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
1995 switch (group_dpif_get_type(group)) {
1997 case OFPGT11_INDIRECT:
1998 xlate_all_group(ctx, group);
2000 case OFPGT11_SELECT:
2001 xlate_select_group(ctx, group);
2004 xlate_ff_group(ctx, group);
2009 group_dpif_release(group);
2013 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
2015 if (xlate_resubmit_resource_check(ctx)) {
2016 struct group_dpif *group;
2019 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
2021 xlate_group_action__(ctx, group);
2031 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
2032 const struct ofpact_resubmit *resubmit)
2037 in_port = resubmit->in_port;
2038 if (in_port == OFPP_IN_PORT) {
2039 in_port = ctx->xin->flow.in_port.ofp_port;
2042 table_id = resubmit->table_id;
2043 if (table_id == 255) {
2044 table_id = ctx->table_id;
2047 xlate_table_action(ctx, in_port, table_id, false);
2051 flood_packets(struct xlate_ctx *ctx, bool all)
2053 const struct xport *xport;
2055 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
2056 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
2061 compose_output_action__(ctx, xport->ofp_port, false);
2062 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
2063 compose_output_action(ctx, xport->ofp_port);
2067 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2071 execute_controller_action(struct xlate_ctx *ctx, int len,
2072 enum ofp_packet_in_reason reason,
2073 uint16_t controller_id)
2075 struct ofproto_packet_in *pin;
2076 struct ofpbuf *packet;
2077 struct pkt_metadata md = PKT_METADATA_INITIALIZER(0);
2079 ctx->xout->slow |= SLOW_CONTROLLER;
2080 if (!ctx->xin->packet) {
2084 packet = ofpbuf_clone(ctx->xin->packet);
2086 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2087 &ctx->xout->odp_actions,
2089 &ctx->mpls_depth_delta);
2091 odp_execute_actions(NULL, packet, &md, ctx->xout->odp_actions.data,
2092 ctx->xout->odp_actions.size, NULL);
2094 pin = xmalloc(sizeof *pin);
2095 pin->up.packet_len = packet->size;
2096 pin->up.packet = ofpbuf_steal_data(packet);
2097 pin->up.reason = reason;
2098 pin->up.table_id = ctx->table_id;
2099 pin->up.cookie = (ctx->rule
2100 ? rule_dpif_get_flow_cookie(ctx->rule)
2103 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
2105 pin->controller_id = controller_id;
2106 pin->send_len = len;
2107 pin->generated_by_table_miss = (ctx->rule
2108 && rule_dpif_is_table_miss(ctx->rule));
2109 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
2110 ofpbuf_delete(packet);
2114 compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
2116 struct flow_wildcards *wc = &ctx->xout->wc;
2117 struct flow *flow = &ctx->xin->flow;
2119 ovs_assert(eth_type_mpls(eth_type));
2121 /* If mpls_depth_delta is negative then an MPLS POP action has been
2122 * composed and the resulting MPLS label stack is unknown. This means
2123 * an MPLS PUSH action can't be composed as it needs to know either the
2124 * top-most MPLS LSE to use as a template for the new MPLS LSE, or that
2125 * there is no MPLS label stack present. Thus, stop processing.
2127 * If mpls_depth_delta is positive then an MPLS PUSH action has been
2128 * composed and no further MPLS PUSH action may be performed without
2129 * losing MPLS LSE and ether type information held in xtx->xin->flow.
2130 * Thus, stop processing.
2132 * If the MPLS LSE of the flow and base_flow differ then the MPLS LSE
2133 * has been updated. Performing a MPLS PUSH action may be would result in
2134 * losing MPLS LSE and ether type information held in xtx->xin->flow.
2135 * Thus, stop processing.
2137 * It is planned that in the future this case will be handled
2138 * by recirculation */
2139 if (ctx->mpls_depth_delta ||
2140 ctx->xin->flow.mpls_lse != ctx->base_flow.mpls_lse) {
2144 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2146 ctx->pre_push_mpls_lse = ctx->xin->flow.mpls_lse;
2148 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2149 flow->mpls_lse &= ~htonl(MPLS_BOS_MASK);
2154 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2155 label = htonl(0x2); /* IPV6 Explicit Null. */
2157 label = htonl(0x0); /* IPV4 Explicit Null. */
2159 wc->masks.nw_tos |= IP_DSCP_MASK;
2160 wc->masks.nw_ttl = 0xff;
2161 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
2162 ttl = flow->nw_ttl ? flow->nw_ttl : 0x40;
2163 flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
2165 flow->dl_type = eth_type;
2166 ctx->mpls_depth_delta++;
2172 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
2174 struct flow_wildcards *wc = &ctx->xout->wc;
2176 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2180 /* If mpls_depth_delta is negative then an MPLS POP action has been
2181 * composed. Performing another MPLS POP action
2182 * would result in losing ether type that results from
2183 * the already composed MPLS POP. Thus, stop processing.
2185 * It is planned that in the future this case will be handled
2186 * by recirculation */
2187 if (ctx->mpls_depth_delta < 0) {
2191 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2193 /* If mpls_depth_delta is positive then an MPLS PUSH action has been
2194 * executed and the previous MPLS LSE saved in ctx->pre_push_mpls_lse. The
2195 * flow's MPLS LSE should be restored to that value to allow any
2196 * subsequent actions that update of the LSE to be executed correctly.
2198 if (ctx->mpls_depth_delta > 0) {
2199 ctx->xin->flow.mpls_lse = ctx->pre_push_mpls_lse;
2202 ctx->xin->flow.dl_type = eth_type;
2203 ctx->mpls_depth_delta--;
2209 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
2211 struct flow *flow = &ctx->xin->flow;
2213 if (!is_ip_any(flow)) {
2217 ctx->xout->wc.masks.nw_ttl = 0xff;
2218 if (flow->nw_ttl > 1) {
2224 for (i = 0; i < ids->n_controllers; i++) {
2225 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
2229 /* Stop processing for current table. */
2235 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
2237 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2241 /* If mpls_depth_delta is negative then an MPLS POP action has been
2242 * executed and the resulting MPLS label stack is unknown. This means
2243 * a SET MPLS LABEL action can't be executed as it needs to manipulate
2244 * the top-most MPLS LSE. Thus, stop processing.
2246 * It is planned that in the future this case will be handled
2249 if (ctx->mpls_depth_delta < 0) {
2253 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_LABEL_MASK);
2254 set_mpls_lse_label(&ctx->xin->flow.mpls_lse, label);
2259 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
2261 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2265 /* If mpls_depth_delta is negative then an MPLS POP action has been
2266 * executed and the resulting MPLS label stack is unknown. This means
2267 * a SET MPLS TC action can't be executed as it needs to manipulate
2268 * the top-most MPLS LSE. Thus, stop processing.
2270 * It is planned that in the future this case will be handled
2273 if (ctx->mpls_depth_delta < 0) {
2277 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_TC_MASK);
2278 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse, tc);
2283 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
2285 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2289 /* If mpls_depth_delta is negative then an MPLS POP action has been
2290 * executed and the resulting MPLS label stack is unknown. This means
2291 * a SET MPLS TTL push action can't be executed as it needs to manipulate
2292 * the top-most MPLS LSE. Thus, stop processing.
2294 * It is planned that in the future this case will be handled
2297 if (ctx->mpls_depth_delta < 0) {
2301 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_TTL_MASK);
2302 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl);
2307 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
2309 struct flow *flow = &ctx->xin->flow;
2310 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse);
2311 struct flow_wildcards *wc = &ctx->xout->wc;
2313 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2315 if (!eth_type_mpls(flow->dl_type)) {
2321 set_mpls_lse_ttl(&flow->mpls_lse, ttl);
2324 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
2326 /* Stop processing for current table. */
2332 xlate_output_action(struct xlate_ctx *ctx,
2333 ofp_port_t port, uint16_t max_len, bool may_packet_in)
2335 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
2337 ctx->xout->nf_output_iface = NF_OUT_DROP;
2341 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
2344 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2351 flood_packets(ctx, false);
2354 flood_packets(ctx, true);
2356 case OFPP_CONTROLLER:
2357 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
2363 if (port != ctx->xin->flow.in_port.ofp_port) {
2364 compose_output_action(ctx, port);
2366 xlate_report(ctx, "skipping output to input port");
2371 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2372 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2373 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2374 ctx->xout->nf_output_iface = prev_nf_output_iface;
2375 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2376 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2377 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2382 xlate_output_reg_action(struct xlate_ctx *ctx,
2383 const struct ofpact_output_reg *or)
2385 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
2386 if (port <= UINT16_MAX) {
2387 union mf_subvalue value;
2389 memset(&value, 0xff, sizeof value);
2390 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
2391 xlate_output_action(ctx, u16_to_ofp(port),
2392 or->max_len, false);
2397 xlate_enqueue_action(struct xlate_ctx *ctx,
2398 const struct ofpact_enqueue *enqueue)
2400 ofp_port_t ofp_port = enqueue->port;
2401 uint32_t queue_id = enqueue->queue;
2402 uint32_t flow_priority, priority;
2405 /* Translate queue to priority. */
2406 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
2408 /* Fall back to ordinary output action. */
2409 xlate_output_action(ctx, enqueue->port, 0, false);
2413 /* Check output port. */
2414 if (ofp_port == OFPP_IN_PORT) {
2415 ofp_port = ctx->xin->flow.in_port.ofp_port;
2416 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
2420 /* Add datapath actions. */
2421 flow_priority = ctx->xin->flow.skb_priority;
2422 ctx->xin->flow.skb_priority = priority;
2423 compose_output_action(ctx, ofp_port);
2424 ctx->xin->flow.skb_priority = flow_priority;
2426 /* Update NetFlow output port. */
2427 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2428 ctx->xout->nf_output_iface = ofp_port;
2429 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2430 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2435 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
2437 uint32_t skb_priority;
2439 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
2440 ctx->xin->flow.skb_priority = skb_priority;
2442 /* Couldn't translate queue to a priority. Nothing to do. A warning
2443 * has already been logged. */
2448 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
2450 const struct xbridge *xbridge = xbridge_;
2461 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
2464 port = get_ofp_port(xbridge, ofp_port);
2465 return port ? port->may_enable : false;
2470 xlate_bundle_action(struct xlate_ctx *ctx,
2471 const struct ofpact_bundle *bundle)
2475 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
2477 CONST_CAST(struct xbridge *, ctx->xbridge));
2478 if (bundle->dst.field) {
2479 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
2482 xlate_output_action(ctx, port, 0, false);
2487 xlate_learn_action(struct xlate_ctx *ctx,
2488 const struct ofpact_learn *learn)
2490 uint64_t ofpacts_stub[1024 / 8];
2491 struct ofputil_flow_mod fm;
2492 struct ofpbuf ofpacts;
2494 ctx->xout->has_learn = true;
2496 learn_mask(learn, &ctx->xout->wc);
2498 if (!ctx->xin->may_learn) {
2502 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2503 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
2504 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, &fm);
2505 ofpbuf_uninit(&ofpacts);
2509 xlate_fin_timeout(struct xlate_ctx *ctx,
2510 const struct ofpact_fin_timeout *oft)
2512 if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
2513 rule_dpif_reduce_timeouts(ctx->rule, oft->fin_idle_timeout,
2514 oft->fin_hard_timeout);
2519 xlate_sample_action(struct xlate_ctx *ctx,
2520 const struct ofpact_sample *os)
2522 union user_action_cookie cookie;
2523 /* Scale the probability from 16-bit to 32-bit while representing
2524 * the same percentage. */
2525 uint32_t probability = (os->probability << 16) | os->probability;
2527 if (!ctx->xbridge->variable_length_userdata) {
2528 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2530 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
2531 "lacks support (needs Linux 3.10+ or kernel module from "
2536 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2537 &ctx->xout->odp_actions,
2539 &ctx->mpls_depth_delta);
2541 compose_flow_sample_cookie(os->probability, os->collector_set_id,
2542 os->obs_domain_id, os->obs_point_id, &cookie);
2543 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
2544 probability, &cookie, sizeof cookie.flow_sample);
2548 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
2550 if (xport->config & (eth_addr_equals(ctx->xin->flow.dl_dst, eth_addr_stp)
2551 ? OFPUTIL_PC_NO_RECV_STP
2552 : OFPUTIL_PC_NO_RECV)) {
2556 /* Only drop packets here if both forwarding and learning are
2557 * disabled. If just learning is enabled, we need to have
2558 * OFPP_NORMAL and the learning action have a look at the packet
2559 * before we can drop it. */
2560 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
2568 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
2570 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2571 ofpbuf_put(&ctx->action_set, on->actions, ofpact_nest_get_action_len(on));
2572 ofpact_pad(&ctx->action_set);
2576 xlate_action_set(struct xlate_ctx *ctx)
2578 uint64_t action_list_stub[1024 / 64];
2579 struct ofpbuf action_list;
2581 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2582 ofpacts_execute_action_set(&action_list, &ctx->action_set);
2583 do_xlate_actions(action_list.data, action_list.size, ctx);
2584 ofpbuf_uninit(&action_list);
2588 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
2589 struct xlate_ctx *ctx)
2591 struct flow_wildcards *wc = &ctx->xout->wc;
2592 struct flow *flow = &ctx->xin->flow;
2593 const struct ofpact *a;
2595 /* dl_type already in the mask, not set below. */
2597 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2598 struct ofpact_controller *controller;
2599 const struct ofpact_metadata *metadata;
2600 const struct ofpact_set_field *set_field;
2601 const struct mf_field *mf;
2609 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
2610 ofpact_get_OUTPUT(a)->max_len, true);
2614 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
2619 case OFPACT_CONTROLLER:
2620 controller = ofpact_get_CONTROLLER(a);
2621 execute_controller_action(ctx, controller->max_len,
2623 controller->controller_id);
2626 case OFPACT_ENQUEUE:
2627 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
2630 case OFPACT_SET_VLAN_VID:
2631 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2632 if (flow->vlan_tci & htons(VLAN_CFI) ||
2633 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2634 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
2635 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
2640 case OFPACT_SET_VLAN_PCP:
2641 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
2642 if (flow->vlan_tci & htons(VLAN_CFI) ||
2643 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2644 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
2645 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
2646 << VLAN_PCP_SHIFT) | VLAN_CFI);
2650 case OFPACT_STRIP_VLAN:
2651 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2652 flow->vlan_tci = htons(0);
2655 case OFPACT_PUSH_VLAN:
2656 /* XXX 802.1AD(QinQ) */
2657 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2658 flow->vlan_tci = htons(VLAN_CFI);
2661 case OFPACT_SET_ETH_SRC:
2662 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2663 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2666 case OFPACT_SET_ETH_DST:
2667 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2668 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2671 case OFPACT_SET_IPV4_SRC:
2672 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2673 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2674 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2678 case OFPACT_SET_IPV4_DST:
2679 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2680 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2681 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
2685 case OFPACT_SET_IP_DSCP:
2686 if (is_ip_any(flow)) {
2687 wc->masks.nw_tos |= IP_DSCP_MASK;
2688 flow->nw_tos &= ~IP_DSCP_MASK;
2689 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
2693 case OFPACT_SET_IP_ECN:
2694 if (is_ip_any(flow)) {
2695 wc->masks.nw_tos |= IP_ECN_MASK;
2696 flow->nw_tos &= ~IP_ECN_MASK;
2697 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
2701 case OFPACT_SET_IP_TTL:
2702 if (is_ip_any(flow)) {
2703 wc->masks.nw_ttl = 0xff;
2704 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
2708 case OFPACT_SET_L4_SRC_PORT:
2709 if (is_ip_any(flow)) {
2710 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2711 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2712 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2716 case OFPACT_SET_L4_DST_PORT:
2717 if (is_ip_any(flow)) {
2718 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2719 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2720 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2724 case OFPACT_RESUBMIT:
2725 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
2728 case OFPACT_SET_TUNNEL:
2729 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2732 case OFPACT_SET_QUEUE:
2733 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2736 case OFPACT_POP_QUEUE:
2737 flow->skb_priority = ctx->orig_skb_priority;
2740 case OFPACT_REG_MOVE:
2741 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
2744 case OFPACT_REG_LOAD:
2745 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow, wc);
2748 case OFPACT_SET_FIELD:
2749 set_field = ofpact_get_SET_FIELD(a);
2750 mf = set_field->field;
2751 mf_mask_field_and_prereqs(mf, &wc->masks);
2753 /* Set field action only ever overwrites packet's outermost
2754 * applicable header fields. Do nothing if no header exists. */
2755 if ((mf->id != MFF_VLAN_VID || flow->vlan_tci & htons(VLAN_CFI))
2756 && ((mf->id != MFF_MPLS_LABEL && mf->id != MFF_MPLS_TC)
2757 || eth_type_mpls(flow->dl_type))) {
2758 mf_set_flow_value(mf, &set_field->value, flow);
2762 case OFPACT_STACK_PUSH:
2763 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
2767 case OFPACT_STACK_POP:
2768 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
2772 case OFPACT_PUSH_MPLS:
2773 if (compose_mpls_push_action(ctx,
2774 ofpact_get_PUSH_MPLS(a)->ethertype)) {
2779 case OFPACT_POP_MPLS:
2780 if (compose_mpls_pop_action(ctx,
2781 ofpact_get_POP_MPLS(a)->ethertype)) {
2786 case OFPACT_SET_MPLS_LABEL:
2787 if (compose_set_mpls_label_action(ctx,
2788 ofpact_get_SET_MPLS_LABEL(a)->label)) {
2793 case OFPACT_SET_MPLS_TC:
2794 if (compose_set_mpls_tc_action(ctx,
2795 ofpact_get_SET_MPLS_TC(a)->tc)) {
2800 case OFPACT_SET_MPLS_TTL:
2801 if (compose_set_mpls_ttl_action(ctx,
2802 ofpact_get_SET_MPLS_TTL(a)->ttl)) {
2807 case OFPACT_DEC_MPLS_TTL:
2808 if (compose_dec_mpls_ttl_action(ctx)) {
2813 case OFPACT_DEC_TTL:
2814 wc->masks.nw_ttl = 0xff;
2815 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
2821 /* Nothing to do. */
2824 case OFPACT_MULTIPATH:
2825 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
2829 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
2832 case OFPACT_OUTPUT_REG:
2833 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
2837 xlate_learn_action(ctx, ofpact_get_LEARN(a));
2844 case OFPACT_FIN_TIMEOUT:
2845 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2846 ctx->xout->has_fin_timeout = true;
2847 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
2850 case OFPACT_CLEAR_ACTIONS:
2851 ofpbuf_clear(&ctx->action_set);
2854 case OFPACT_WRITE_ACTIONS:
2855 xlate_write_actions(ctx, a);
2858 case OFPACT_WRITE_METADATA:
2859 metadata = ofpact_get_WRITE_METADATA(a);
2860 flow->metadata &= ~metadata->mask;
2861 flow->metadata |= metadata->metadata & metadata->mask;
2865 /* Not implemented yet. */
2868 case OFPACT_GOTO_TABLE: {
2869 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
2871 ovs_assert(ctx->table_id < ogt->table_id);
2872 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2873 ogt->table_id, true);
2878 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
2885 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
2886 const struct flow *flow, struct rule_dpif *rule,
2887 uint16_t tcp_flags, const struct ofpbuf *packet)
2889 xin->ofproto = ofproto;
2891 xin->packet = packet;
2892 xin->may_learn = packet != NULL;
2894 xin->ofpacts = NULL;
2895 xin->ofpacts_len = 0;
2896 xin->tcp_flags = tcp_flags;
2897 xin->resubmit_hook = NULL;
2898 xin->report_hook = NULL;
2899 xin->resubmit_stats = NULL;
2900 xin->skip_wildcards = false;
2904 xlate_out_uninit(struct xlate_out *xout)
2907 ofpbuf_uninit(&xout->odp_actions);
2911 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
2912 * into datapath actions, using 'ctx', and discards the datapath actions. */
2914 xlate_actions_for_side_effects(struct xlate_in *xin)
2916 struct xlate_out xout;
2918 xlate_actions(xin, &xout);
2919 xlate_out_uninit(&xout);
2923 xlate_report(struct xlate_ctx *ctx, const char *s)
2925 if (ctx->xin->report_hook) {
2926 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
2931 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
2934 dst->slow = src->slow;
2935 dst->has_learn = src->has_learn;
2936 dst->has_normal = src->has_normal;
2937 dst->has_fin_timeout = src->has_fin_timeout;
2938 dst->nf_output_iface = src->nf_output_iface;
2939 dst->mirrors = src->mirrors;
2941 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
2942 sizeof dst->odp_actions_stub);
2943 ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
2944 src->odp_actions.size);
2947 static struct skb_priority_to_dscp *
2948 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
2950 struct skb_priority_to_dscp *pdscp;
2953 hash = hash_int(skb_priority, 0);
2954 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
2955 if (pdscp->skb_priority == skb_priority) {
2963 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
2966 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
2967 *dscp = pdscp ? pdscp->dscp : 0;
2968 return pdscp != NULL;
2972 clear_skb_priorities(struct xport *xport)
2974 struct skb_priority_to_dscp *pdscp, *next;
2976 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
2977 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
2983 actions_output_to_local_port(const struct xlate_ctx *ctx)
2985 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
2986 const struct nlattr *a;
2989 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions.data,
2990 ctx->xout->odp_actions.size) {
2991 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
2992 && nl_attr_get_odp_port(a) == local_odp_port) {
2999 /* Thread safe call to xlate_actions__(). */
3001 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
3003 ovs_rwlock_rdlock(&xlate_rwlock);
3004 xlate_actions__(xin, xout);
3005 ovs_rwlock_unlock(&xlate_rwlock);
3008 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
3009 * into datapath actions in 'odp_actions', using 'ctx'.
3011 * The caller must take responsibility for eventually freeing 'xout', with
3012 * xlate_out_uninit(). */
3014 xlate_actions__(struct xlate_in *xin, struct xlate_out *xout)
3015 OVS_REQ_RDLOCK(xlate_rwlock)
3017 struct flow_wildcards *wc = &xout->wc;
3018 struct flow *flow = &xin->flow;
3019 struct rule_dpif *rule = NULL;
3021 struct rule_actions *actions = NULL;
3022 enum slow_path_reason special;
3023 const struct ofpact *ofpacts;
3024 struct xport *in_port;
3025 struct flow orig_flow;
3026 struct xlate_ctx ctx;
3030 COVERAGE_INC(xlate_actions);
3032 /* Flow initialization rules:
3033 * - 'base_flow' must match the kernel's view of the packet at the
3034 * time that action processing starts. 'flow' represents any
3035 * transformations we wish to make through actions.
3036 * - By default 'base_flow' and 'flow' are the same since the input
3037 * packet matches the output before any actions are applied.
3038 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
3039 * of the received packet as seen by the kernel. If we later output
3040 * to another device without any modifications this will cause us to
3041 * insert a new tag since the original one was stripped off by the
3043 * - Tunnel metadata as received is retained in 'flow'. This allows
3044 * tunnel metadata matching also in later tables.
3045 * Since a kernel action for setting the tunnel metadata will only be
3046 * generated with actual tunnel output, changing the tunnel metadata
3047 * values in 'flow' (such as tun_id) will only have effect with a later
3048 * tunnel output action.
3049 * - Tunnel 'base_flow' is completely cleared since that is what the
3050 * kernel does. If we wish to maintain the original values an action
3051 * needs to be generated. */
3056 ctx.xout->has_learn = false;
3057 ctx.xout->has_normal = false;
3058 ctx.xout->has_fin_timeout = false;
3059 ctx.xout->nf_output_iface = NF_OUT_DROP;
3060 ctx.xout->mirrors = 0;
3061 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
3062 sizeof ctx.xout->odp_actions_stub);
3063 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
3065 ctx.xbridge = xbridge_lookup(xin->ofproto);
3070 ctx.rule = xin->rule;
3072 ctx.base_flow = *flow;
3073 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
3074 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
3076 flow_wildcards_init_catchall(wc);
3077 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
3078 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3079 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3080 if (is_ip_any(flow)) {
3081 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3084 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
3085 if (ctx.xbridge->netflow) {
3086 netflow_mask_wc(flow, wc);
3091 ctx.orig_skb_priority = flow->skb_priority;
3094 ctx.mpls_depth_delta = 0;
3096 if (!xin->ofpacts && !ctx.rule) {
3097 rule_dpif_lookup(ctx.xbridge->ofproto, flow,
3098 !xin->skip_wildcards ? wc : NULL, &rule);
3099 if (ctx.xin->resubmit_stats) {
3100 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
3104 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
3107 ofpacts = xin->ofpacts;
3108 ofpacts_len = xin->ofpacts_len;
3109 } else if (ctx.rule) {
3110 actions = rule_dpif_get_actions(ctx.rule);
3111 ofpacts = actions->ofpacts;
3112 ofpacts_len = actions->ofpacts_len;
3117 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
3118 ofpbuf_use_stub(&ctx.action_set,
3119 ctx.action_set_stub, sizeof ctx.action_set_stub);
3121 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3122 /* Do this conditionally because the copy is expensive enough that it
3123 * shows up in profiles. */
3127 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3128 switch (ctx.xbridge->frag) {
3129 case OFPC_FRAG_NORMAL:
3130 /* We must pretend that transport ports are unavailable. */
3131 flow->tp_src = ctx.base_flow.tp_src = htons(0);
3132 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
3135 case OFPC_FRAG_DROP:
3138 case OFPC_FRAG_REASM:
3141 case OFPC_FRAG_NX_MATCH:
3142 /* Nothing to do. */
3145 case OFPC_INVALID_TTL_TO_CONTROLLER:
3150 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
3151 if (in_port && in_port->is_tunnel && ctx.xin->resubmit_stats) {
3152 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
3154 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
3158 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
3160 ctx.xout->slow |= special;
3162 size_t sample_actions_len;
3164 if (flow->in_port.ofp_port
3165 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
3166 flow->in_port.ofp_port,
3168 ctx.base_flow.vlan_tci = 0;
3171 add_sflow_action(&ctx);
3172 add_ipfix_action(&ctx);
3173 sample_actions_len = ctx.xout->odp_actions.size;
3175 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
3176 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
3178 /* We've let OFPP_NORMAL and the learning action look at the
3179 * packet, so drop it now if forwarding is disabled. */
3180 if (in_port && !xport_stp_forward_state(in_port)) {
3181 ctx.xout->odp_actions.size = sample_actions_len;
3185 if (ctx.action_set.size) {
3186 xlate_action_set(&ctx);
3189 if (ctx.xbridge->has_in_band
3190 && in_band_must_output_to_local_port(flow)
3191 && !actions_output_to_local_port(&ctx)) {
3192 compose_output_action(&ctx, OFPP_LOCAL);
3195 fix_sflow_action(&ctx);
3197 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3198 add_mirror_actions(&ctx, &orig_flow);
3202 if (nl_attr_oversized(ctx.xout->odp_actions.size)) {
3203 /* These datapath actions are too big for a Netlink attribute, so we
3204 * can't hand them to the kernel directly. dpif_execute() can execute
3205 * them one by one with help, so just mark the result as SLOW_ACTION to
3206 * prevent the flow from being installed. */
3207 COVERAGE_INC(xlate_actions_oversize);
3208 ctx.xout->slow |= SLOW_ACTION;
3211 if (ctx.xin->resubmit_stats) {
3212 mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
3213 ctx.xin->resubmit_stats->n_packets,
3214 ctx.xin->resubmit_stats->n_bytes);
3216 if (ctx.xbridge->netflow) {
3217 const struct ofpact *ofpacts;
3220 ofpacts_len = actions->ofpacts_len;
3221 ofpacts = actions->ofpacts;
3222 if (ofpacts_len == 0
3223 || ofpacts->type != OFPACT_CONTROLLER
3224 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
3225 /* Only update netflow if we don't have controller flow. We don't
3226 * report NetFlow expiration messages for such facets because they
3227 * are just part of the control logic for the network, not real
3229 netflow_flow_update(ctx.xbridge->netflow, flow,
3230 xout->nf_output_iface,
3231 ctx.xin->resubmit_stats);
3236 ofpbuf_uninit(&ctx.stack);
3237 ofpbuf_uninit(&ctx.action_set);
3239 /* Clear the metadata and register wildcard masks, because we won't
3240 * use non-header fields as part of the cache. */
3241 flow_wildcards_clear_non_packet_fields(wc);
3244 rule_actions_unref(actions);
3245 rule_dpif_unref(rule);
3248 /* Sends 'packet' out 'ofport'.
3249 * May modify 'packet'.
3250 * Returns 0 if successful, otherwise a positive errno value. */
3252 xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3254 struct xport *xport;
3255 struct ofpact_output output;
3257 union flow_in_port in_port_;
3260 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
3261 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
3262 in_port_.ofp_port = OFPP_NONE;
3263 flow_extract(packet, 0, 0, NULL, &in_port_, &flow);
3265 ovs_rwlock_rdlock(&xlate_rwlock);
3266 xport = xport_lookup(ofport);
3268 ovs_rwlock_unlock(&xlate_rwlock);
3271 output.port = xport->ofp_port;
3273 error = ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
3274 &output.ofpact, sizeof output,
3276 ovs_rwlock_unlock(&xlate_rwlock);