1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. */
17 #include "ofproto/ofproto-dpif-xlate.h"
25 #include "byte-order.h"
30 #include "dynamic-string.h"
35 #include "mac-learning.h"
36 #include "meta-flow.h"
37 #include "multipath.h"
38 #include "netdev-vport.h"
41 #include "odp-execute.h"
42 #include "ofp-actions.h"
43 #include "ofproto/ofproto-dpif-ipfix.h"
44 #include "ofproto/ofproto-dpif-mirror.h"
45 #include "ofproto/ofproto-dpif-monitor.h"
46 #include "ofproto/ofproto-dpif-sflow.h"
47 #include "ofproto/ofproto-dpif.h"
48 #include "ofproto/ofproto-provider.h"
52 COVERAGE_DEFINE(xlate_actions);
53 COVERAGE_DEFINE(xlate_actions_oversize);
54 COVERAGE_DEFINE(xlate_actions_mpls_overflow);
56 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
58 /* Maximum depth of flow table recursion (due to resubmit actions) in a
59 * flow translation. */
60 #define MAX_RESUBMIT_RECURSION 64
62 /* Maximum number of resubmit actions in a flow translation, whether they are
63 * recursive or not. */
64 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
66 struct ovs_rwlock xlate_rwlock = OVS_RWLOCK_INITIALIZER;
69 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
70 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
72 struct list xbundles; /* Owned xbundles. */
73 struct hmap xports; /* Indexed by ofp_port. */
75 char *name; /* Name used in log messages. */
76 struct dpif *dpif; /* Datapath interface. */
77 struct mac_learning *ml; /* Mac learning handle. */
78 struct mbridge *mbridge; /* Mirroring. */
79 struct dpif_sflow *sflow; /* SFlow handle, or null. */
80 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
81 struct netflow *netflow; /* Netflow handle, or null. */
82 struct stp *stp; /* STP or null if disabled. */
84 /* Special rules installed by ofproto-dpif. */
85 struct rule_dpif *miss_rule;
86 struct rule_dpif *no_packet_in_rule;
88 enum ofp_config_flags frag; /* Fragmentation handling. */
89 bool has_in_band; /* Bridge has in band control? */
90 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
92 /* True if the datapath supports variable-length
93 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
94 * False if the datapath supports only 8-byte (or shorter) userdata. */
95 bool variable_length_userdata;
97 /* Number of MPLS label stack entries that the datapath supports
99 size_t max_mpls_depth;
103 struct hmap_node hmap_node; /* In global 'xbundles' map. */
104 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
106 struct list list_node; /* In parent 'xbridges' list. */
107 struct xbridge *xbridge; /* Parent xbridge. */
109 struct list xports; /* Contains "struct xport"s. */
111 char *name; /* Name used in log messages. */
112 struct bond *bond; /* Nonnull iff more than one port. */
113 struct lacp *lacp; /* LACP handle or null. */
115 enum port_vlan_mode vlan_mode; /* VLAN mode. */
116 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
117 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
118 * NULL if all VLANs are trunked. */
119 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
120 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
124 struct hmap_node hmap_node; /* Node in global 'xports' map. */
125 struct ofport_dpif *ofport; /* Key in global 'xports map. */
127 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
128 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
130 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
132 struct list bundle_node; /* In parent xbundle (if it exists). */
133 struct xbundle *xbundle; /* Parent xbundle or null. */
135 struct netdev *netdev; /* 'ofport''s netdev. */
137 struct xbridge *xbridge; /* Parent bridge. */
138 struct xport *peer; /* Patch port peer or null. */
140 enum ofputil_port_config config; /* OpenFlow port configuration. */
141 enum ofputil_port_state state; /* OpenFlow port state. */
142 int stp_port_no; /* STP port number or -1 if not in use. */
144 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
146 bool may_enable; /* May be enabled in bonds. */
147 bool is_tunnel; /* Is a tunnel port. */
149 struct cfm *cfm; /* CFM handle or null. */
150 struct bfd *bfd; /* BFD handle or null. */
154 struct xlate_in *xin;
155 struct xlate_out *xout;
157 const struct xbridge *xbridge;
159 /* Flow at the last commit. */
160 struct flow base_flow;
162 /* Tunnel IP destination address as received. This is stored separately
163 * as the base_flow.tunnel is cleared on init to reflect the datapath
164 * behavior. Used to make sure not to send tunneled output to ourselves,
165 * which might lead to an infinite loop. This could happen easily
166 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
167 * actually set the tun_dst field. */
168 ovs_be32 orig_tunnel_ip_dst;
170 /* Stack for the push and pop actions. Each stack element is of type
171 * "union mf_subvalue". */
172 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
175 /* The rule that we are currently translating, or NULL. */
176 struct rule_dpif *rule;
178 /* Resubmit statistics, via xlate_table_action(). */
179 int recurse; /* Current resubmit nesting depth. */
180 int resubmits; /* Total number of resubmits. */
181 bool in_group; /* Currently translating ofgroup, if true. */
183 uint32_t orig_skb_priority; /* Priority when packet arrived. */
184 uint8_t table_id; /* OpenFlow table ID where flow was found. */
185 uint32_t sflow_n_outputs; /* Number of output ports. */
186 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
187 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
188 bool exit; /* No further actions should be processed. */
190 /* OpenFlow 1.1+ action set.
192 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
193 * When translation is otherwise complete, ofpacts_execute_action_set()
194 * converts it to a set of "struct ofpact"s that can be translated into
195 * datapath actions. */
196 struct ofpbuf action_set; /* Action set. */
197 uint64_t action_set_stub[1024 / 8];
200 /* A controller may use OFPP_NONE as the ingress port to indicate that
201 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
202 * when an input bundle is needed for validation (e.g., mirroring or
203 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
204 * any 'port' structs, so care must be taken when dealing with it. */
205 static struct xbundle ofpp_none_bundle = {
207 .vlan_mode = PORT_VLAN_TRUNK
210 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
211 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
212 * traffic egressing the 'ofport' with that priority should be marked with. */
213 struct skb_priority_to_dscp {
214 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
215 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
217 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
220 static struct hmap xbridges = HMAP_INITIALIZER(&xbridges);
221 static struct hmap xbundles = HMAP_INITIALIZER(&xbundles);
222 static struct hmap xports = HMAP_INITIALIZER(&xports);
224 static bool may_receive(const struct xport *, struct xlate_ctx *);
225 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
227 static void xlate_actions__(struct xlate_in *, struct xlate_out *)
228 OVS_REQ_RDLOCK(xlate_rwlock);
229 static void xlate_normal(struct xlate_ctx *);
230 static void xlate_report(struct xlate_ctx *, const char *);
231 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
232 uint8_t table_id, bool may_packet_in);
233 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
234 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
235 static void output_normal(struct xlate_ctx *, const struct xbundle *,
237 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
239 static struct xbridge *xbridge_lookup(const struct ofproto_dpif *);
240 static struct xbundle *xbundle_lookup(const struct ofbundle *);
241 static struct xport *xport_lookup(const struct ofport_dpif *);
242 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
243 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
244 uint32_t skb_priority);
245 static void clear_skb_priorities(struct xport *);
246 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
250 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
251 struct dpif *dpif, struct rule_dpif *miss_rule,
252 struct rule_dpif *no_packet_in_rule,
253 const struct mac_learning *ml, struct stp *stp,
254 const struct mbridge *mbridge,
255 const struct dpif_sflow *sflow,
256 const struct dpif_ipfix *ipfix,
257 const struct netflow *netflow, enum ofp_config_flags frag,
258 bool forward_bpdu, bool has_in_band,
259 bool variable_length_userdata,
260 size_t max_mpls_depth)
262 struct xbridge *xbridge = xbridge_lookup(ofproto);
265 xbridge = xzalloc(sizeof *xbridge);
266 xbridge->ofproto = ofproto;
268 hmap_insert(&xbridges, &xbridge->hmap_node, hash_pointer(ofproto, 0));
269 hmap_init(&xbridge->xports);
270 list_init(&xbridge->xbundles);
273 if (xbridge->ml != ml) {
274 mac_learning_unref(xbridge->ml);
275 xbridge->ml = mac_learning_ref(ml);
278 if (xbridge->mbridge != mbridge) {
279 mbridge_unref(xbridge->mbridge);
280 xbridge->mbridge = mbridge_ref(mbridge);
283 if (xbridge->sflow != sflow) {
284 dpif_sflow_unref(xbridge->sflow);
285 xbridge->sflow = dpif_sflow_ref(sflow);
288 if (xbridge->ipfix != ipfix) {
289 dpif_ipfix_unref(xbridge->ipfix);
290 xbridge->ipfix = dpif_ipfix_ref(ipfix);
293 if (xbridge->stp != stp) {
294 stp_unref(xbridge->stp);
295 xbridge->stp = stp_ref(stp);
298 if (xbridge->netflow != netflow) {
299 netflow_unref(xbridge->netflow);
300 xbridge->netflow = netflow_ref(netflow);
304 xbridge->name = xstrdup(name);
306 xbridge->dpif = dpif;
307 xbridge->forward_bpdu = forward_bpdu;
308 xbridge->has_in_band = has_in_band;
309 xbridge->frag = frag;
310 xbridge->miss_rule = miss_rule;
311 xbridge->no_packet_in_rule = no_packet_in_rule;
312 xbridge->variable_length_userdata = variable_length_userdata;
313 xbridge->max_mpls_depth = max_mpls_depth;
317 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
319 struct xbridge *xbridge = xbridge_lookup(ofproto);
320 struct xbundle *xbundle, *next_xbundle;
321 struct xport *xport, *next_xport;
327 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
328 xlate_ofport_remove(xport->ofport);
331 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
332 xlate_bundle_remove(xbundle->ofbundle);
335 hmap_remove(&xbridges, &xbridge->hmap_node);
336 mac_learning_unref(xbridge->ml);
337 mbridge_unref(xbridge->mbridge);
338 dpif_sflow_unref(xbridge->sflow);
339 dpif_ipfix_unref(xbridge->ipfix);
340 stp_unref(xbridge->stp);
341 hmap_destroy(&xbridge->xports);
347 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
348 const char *name, enum port_vlan_mode vlan_mode, int vlan,
349 unsigned long *trunks, bool use_priority_tags,
350 const struct bond *bond, const struct lacp *lacp,
353 struct xbundle *xbundle = xbundle_lookup(ofbundle);
356 xbundle = xzalloc(sizeof *xbundle);
357 xbundle->ofbundle = ofbundle;
358 xbundle->xbridge = xbridge_lookup(ofproto);
360 hmap_insert(&xbundles, &xbundle->hmap_node, hash_pointer(ofbundle, 0));
361 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
362 list_init(&xbundle->xports);
365 ovs_assert(xbundle->xbridge);
368 xbundle->name = xstrdup(name);
370 xbundle->vlan_mode = vlan_mode;
371 xbundle->vlan = vlan;
372 xbundle->trunks = trunks;
373 xbundle->use_priority_tags = use_priority_tags;
374 xbundle->floodable = floodable;
376 if (xbundle->bond != bond) {
377 bond_unref(xbundle->bond);
378 xbundle->bond = bond_ref(bond);
381 if (xbundle->lacp != lacp) {
382 lacp_unref(xbundle->lacp);
383 xbundle->lacp = lacp_ref(lacp);
388 xlate_bundle_remove(struct ofbundle *ofbundle)
390 struct xbundle *xbundle = xbundle_lookup(ofbundle);
391 struct xport *xport, *next;
397 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
398 list_remove(&xport->bundle_node);
399 xport->xbundle = NULL;
402 hmap_remove(&xbundles, &xbundle->hmap_node);
403 list_remove(&xbundle->list_node);
404 bond_unref(xbundle->bond);
405 lacp_unref(xbundle->lacp);
411 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
412 struct ofport_dpif *ofport, ofp_port_t ofp_port,
413 odp_port_t odp_port, const struct netdev *netdev,
414 const struct cfm *cfm, const struct bfd *bfd,
415 struct ofport_dpif *peer, int stp_port_no,
416 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
417 enum ofputil_port_config config,
418 enum ofputil_port_state state, bool is_tunnel,
421 struct xport *xport = xport_lookup(ofport);
425 xport = xzalloc(sizeof *xport);
426 xport->ofport = ofport;
427 xport->xbridge = xbridge_lookup(ofproto);
428 xport->ofp_port = ofp_port;
430 hmap_init(&xport->skb_priorities);
431 hmap_insert(&xports, &xport->hmap_node, hash_pointer(ofport, 0));
432 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
433 hash_ofp_port(xport->ofp_port));
436 ovs_assert(xport->ofp_port == ofp_port);
438 xport->config = config;
439 xport->state = state;
440 xport->stp_port_no = stp_port_no;
441 xport->is_tunnel = is_tunnel;
442 xport->may_enable = may_enable;
443 xport->odp_port = odp_port;
445 if (xport->netdev != netdev) {
446 netdev_close(xport->netdev);
447 xport->netdev = netdev_ref(netdev);
450 if (xport->cfm != cfm) {
451 cfm_unref(xport->cfm);
452 xport->cfm = cfm_ref(cfm);
455 if (xport->bfd != bfd) {
456 bfd_unref(xport->bfd);
457 xport->bfd = bfd_ref(bfd);
461 xport->peer->peer = NULL;
463 xport->peer = xport_lookup(peer);
465 xport->peer->peer = xport;
468 if (xport->xbundle) {
469 list_remove(&xport->bundle_node);
471 xport->xbundle = xbundle_lookup(ofbundle);
472 if (xport->xbundle) {
473 list_insert(&xport->xbundle->xports, &xport->bundle_node);
476 clear_skb_priorities(xport);
477 for (i = 0; i < n_qdscp; i++) {
478 struct skb_priority_to_dscp *pdscp;
479 uint32_t skb_priority;
481 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
486 pdscp = xmalloc(sizeof *pdscp);
487 pdscp->skb_priority = skb_priority;
488 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
489 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
490 hash_int(pdscp->skb_priority, 0));
495 xlate_ofport_remove(struct ofport_dpif *ofport)
497 struct xport *xport = xport_lookup(ofport);
504 xport->peer->peer = NULL;
508 if (xport->xbundle) {
509 list_remove(&xport->bundle_node);
512 clear_skb_priorities(xport);
513 hmap_destroy(&xport->skb_priorities);
515 hmap_remove(&xports, &xport->hmap_node);
516 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
518 netdev_close(xport->netdev);
519 cfm_unref(xport->cfm);
520 bfd_unref(xport->bfd);
524 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
525 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
526 * Optionally populates 'ofproto' with the ofproto_dpif, 'odp_in_port' with
527 * the datapath in_port, that 'packet' ingressed, and 'ipfix', 'sflow', and
528 * 'netflow' with the appropriate handles for those protocols if they're
529 * enabled. Caller is responsible for unrefing them.
531 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
532 * 'flow''s in_port to OFPP_NONE.
534 * This function does post-processing on data returned from
535 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
536 * of the upcall processing logic. In particular, if the extracted in_port is
537 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
538 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
539 * a VLAN header onto 'packet' (if it is nonnull).
541 * Similarly, this function also includes some logic to help with tunnels. It
542 * may modify 'flow' as necessary to make the tunneling implementation
543 * transparent to the upcall processing logic.
545 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
546 * or some other positive errno if there are other problems. */
548 xlate_receive(const struct dpif_backer *backer, struct ofpbuf *packet,
549 const struct nlattr *key, size_t key_len, struct flow *flow,
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 const struct xport *xport;
557 ovs_rwlock_rdlock(&xlate_rwlock);
558 if (odp_flow_key_to_flow(key, key_len, flow) == ODP_FIT_ERROR) {
564 *odp_in_port = flow->in_port.odp_port;
567 xport = xport_lookup(tnl_port_should_receive(flow)
568 ? tnl_port_receive(flow)
569 : odp_port_to_ofport(backer, flow->in_port.odp_port));
571 flow->in_port.ofp_port = xport ? xport->ofp_port : OFPP_NONE;
576 if (vsp_adjust_flow(xport->xbridge->ofproto, flow)) {
578 /* Make the packet resemble the flow, so that it gets sent to
579 * an OpenFlow controller properly, so that it looks correct
580 * for sFlow, and so that flow_extract() will get the correct
581 * vlan_tci if it is called on 'packet'. */
582 eth_push_vlan(packet, htons(ETH_TYPE_VLAN), flow->vlan_tci);
588 *ofproto = xport->xbridge->ofproto;
592 *ipfix = dpif_ipfix_ref(xport->xbridge->ipfix);
596 *sflow = dpif_sflow_ref(xport->xbridge->sflow);
600 *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 return &ofpp_none_bundle;
902 /* Odd. A few possible reasons here:
904 * - We deleted a port but there are still a few packets queued up
907 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
908 * we don't know about.
910 * - The ofproto client didn't configure the port as part of a bundle.
911 * This is particularly likely to happen if a packet was received on the
912 * port after it was created, but before the client had a chance to
913 * configure its bundle.
916 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
918 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
919 "port %"PRIu16, xbridge->name, in_port);
925 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
927 const struct xbridge *xbridge = ctx->xbridge;
928 mirror_mask_t mirrors;
929 struct xbundle *in_xbundle;
933 mirrors = ctx->xout->mirrors;
934 ctx->xout->mirrors = 0;
936 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
937 ctx->xin->packet != NULL, NULL);
941 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
943 /* Drop frames on bundles reserved for mirroring. */
944 if (xbundle_mirror_out(xbridge, in_xbundle)) {
945 if (ctx->xin->packet != NULL) {
946 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
947 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
948 "%s, which is reserved exclusively for mirroring",
949 ctx->xbridge->name, in_xbundle->name);
951 ofpbuf_clear(&ctx->xout->odp_actions);
956 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
957 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
960 vlan = input_vid_to_vlan(in_xbundle, vid);
966 /* Restore the original packet before adding the mirror actions. */
967 ctx->xin->flow = *orig_flow;
970 mirror_mask_t dup_mirrors;
971 struct ofbundle *out;
972 unsigned long *vlans;
977 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
978 &vlans, &dup_mirrors, &out, &out_vlan);
979 ovs_assert(has_mirror);
982 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
984 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
987 if (!vlan_mirrored) {
988 mirrors = zero_rightmost_1bit(mirrors);
992 mirrors &= ~dup_mirrors;
993 ctx->xout->mirrors |= dup_mirrors;
995 struct xbundle *out_xbundle = xbundle_lookup(out);
997 output_normal(ctx, out_xbundle, vlan);
999 } else if (vlan != out_vlan
1000 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
1001 struct xbundle *xbundle;
1003 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1004 if (xbundle_includes_vlan(xbundle, out_vlan)
1005 && !xbundle_mirror_out(xbridge, xbundle)) {
1006 output_normal(ctx, xbundle, out_vlan);
1013 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1014 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1015 * the bundle on which the packet was received, returns the VLAN to which the
1018 * Both 'vid' and the return value are in the range 0...4095. */
1020 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1022 switch (in_xbundle->vlan_mode) {
1023 case PORT_VLAN_ACCESS:
1024 return in_xbundle->vlan;
1027 case PORT_VLAN_TRUNK:
1030 case PORT_VLAN_NATIVE_UNTAGGED:
1031 case PORT_VLAN_NATIVE_TAGGED:
1032 return vid ? vid : in_xbundle->vlan;
1039 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1040 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1043 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1044 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1047 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1049 /* Allow any VID on the OFPP_NONE port. */
1050 if (in_xbundle == &ofpp_none_bundle) {
1054 switch (in_xbundle->vlan_mode) {
1055 case PORT_VLAN_ACCESS:
1058 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1059 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1060 "packet received on port %s configured as VLAN "
1061 "%"PRIu16" access port", vid, in_xbundle->name,
1068 case PORT_VLAN_NATIVE_UNTAGGED:
1069 case PORT_VLAN_NATIVE_TAGGED:
1071 /* Port must always carry its native VLAN. */
1075 case PORT_VLAN_TRUNK:
1076 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1078 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1079 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1080 "received on port %s not configured for trunking "
1081 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1093 /* Given 'vlan', the VLAN that a packet belongs to, and
1094 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1095 * that should be included in the 802.1Q header. (If the return value is 0,
1096 * then the 802.1Q header should only be included in the packet if there is a
1099 * Both 'vlan' and the return value are in the range 0...4095. */
1101 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1103 switch (out_xbundle->vlan_mode) {
1104 case PORT_VLAN_ACCESS:
1107 case PORT_VLAN_TRUNK:
1108 case PORT_VLAN_NATIVE_TAGGED:
1111 case PORT_VLAN_NATIVE_UNTAGGED:
1112 return vlan == out_xbundle->vlan ? 0 : vlan;
1120 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1123 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1125 ovs_be16 tci, old_tci;
1126 struct xport *xport;
1128 vid = output_vlan_to_vid(out_xbundle, vlan);
1129 if (list_is_empty(&out_xbundle->xports)) {
1130 /* Partially configured bundle with no slaves. Drop the packet. */
1132 } else if (!out_xbundle->bond) {
1133 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1136 struct ofport_dpif *ofport;
1138 ofport = bond_choose_output_slave(out_xbundle->bond, &ctx->xin->flow,
1139 &ctx->xout->wc, vid);
1140 xport = xport_lookup(ofport);
1143 /* No slaves enabled, so drop packet. */
1147 if (ctx->xin->resubmit_stats) {
1148 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1149 ctx->xin->resubmit_stats->n_bytes);
1153 old_tci = *flow_tci;
1155 if (tci || out_xbundle->use_priority_tags) {
1156 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1158 tci |= htons(VLAN_CFI);
1163 compose_output_action(ctx, xport->ofp_port);
1164 *flow_tci = old_tci;
1167 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1168 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1169 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1171 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1173 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1177 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1178 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1182 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1183 if (flow->nw_proto == ARP_OP_REPLY) {
1185 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1186 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1187 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1189 return flow->nw_src == flow->nw_dst;
1195 /* Checks whether a MAC learning update is necessary for MAC learning table
1196 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1199 * Most packets processed through the MAC learning table do not actually
1200 * change it in any way. This function requires only a read lock on the MAC
1201 * learning table, so it is much cheaper in this common case.
1203 * Keep the code here synchronized with that in update_learning_table__()
1206 is_mac_learning_update_needed(const struct mac_learning *ml,
1207 const struct flow *flow,
1208 struct flow_wildcards *wc,
1209 int vlan, struct xbundle *in_xbundle)
1210 OVS_REQ_RDLOCK(ml->rwlock)
1212 struct mac_entry *mac;
1214 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1218 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1219 if (!mac || mac_entry_age(ml, mac)) {
1223 if (is_gratuitous_arp(flow, wc)) {
1224 /* We don't want to learn from gratuitous ARP packets that are
1225 * reflected back over bond slaves so we lock the learning table. */
1226 if (!in_xbundle->bond) {
1228 } else if (mac_entry_is_grat_arp_locked(mac)) {
1233 return mac->port.p != in_xbundle->ofbundle;
1237 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1238 * received on 'in_xbundle' in 'vlan'.
1240 * This code repeats all the checks in is_mac_learning_update_needed() because
1241 * the lock was released between there and here and thus the MAC learning state
1242 * could have changed.
1244 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1247 update_learning_table__(const struct xbridge *xbridge,
1248 const struct flow *flow, struct flow_wildcards *wc,
1249 int vlan, struct xbundle *in_xbundle)
1250 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1252 struct mac_entry *mac;
1254 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1258 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1259 if (is_gratuitous_arp(flow, wc)) {
1260 /* We don't want to learn from gratuitous ARP packets that are
1261 * reflected back over bond slaves so we lock the learning table. */
1262 if (!in_xbundle->bond) {
1263 mac_entry_set_grat_arp_lock(mac);
1264 } else if (mac_entry_is_grat_arp_locked(mac)) {
1269 if (mac->port.p != in_xbundle->ofbundle) {
1270 /* The log messages here could actually be useful in debugging,
1271 * so keep the rate limit relatively high. */
1272 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1274 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1275 "on port %s in VLAN %d",
1276 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1277 in_xbundle->name, vlan);
1279 mac->port.p = in_xbundle->ofbundle;
1280 mac_learning_changed(xbridge->ml);
1285 update_learning_table(const struct xbridge *xbridge,
1286 const struct flow *flow, struct flow_wildcards *wc,
1287 int vlan, struct xbundle *in_xbundle)
1291 /* Don't learn the OFPP_NONE port. */
1292 if (in_xbundle == &ofpp_none_bundle) {
1296 /* First try the common case: no change to MAC learning table. */
1297 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1298 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1300 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1303 /* Slow path: MAC learning table might need an update. */
1304 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1305 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1306 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1310 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1311 * dropped. Returns true if they may be forwarded, false if they should be
1314 * 'in_port' must be the xport that corresponds to flow->in_port.
1315 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1317 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1318 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1319 * checked by input_vid_is_valid().
1321 * May also add tags to '*tags', although the current implementation only does
1322 * so in one special case.
1325 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1328 struct xbundle *in_xbundle = in_port->xbundle;
1329 const struct xbridge *xbridge = ctx->xbridge;
1330 struct flow *flow = &ctx->xin->flow;
1332 /* Drop frames for reserved multicast addresses
1333 * only if forward_bpdu option is absent. */
1334 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1335 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1339 if (in_xbundle->bond) {
1340 struct mac_entry *mac;
1342 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1348 xlate_report(ctx, "bonding refused admissibility, dropping");
1351 case BV_DROP_IF_MOVED:
1352 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1353 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1354 if (mac && mac->port.p != in_xbundle->ofbundle &&
1355 (!is_gratuitous_arp(flow, &ctx->xout->wc)
1356 || mac_entry_is_grat_arp_locked(mac))) {
1357 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1358 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1362 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1371 xlate_normal(struct xlate_ctx *ctx)
1373 struct flow_wildcards *wc = &ctx->xout->wc;
1374 struct flow *flow = &ctx->xin->flow;
1375 struct xbundle *in_xbundle;
1376 struct xport *in_port;
1377 struct mac_entry *mac;
1382 ctx->xout->has_normal = true;
1384 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1385 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1386 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1388 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
1389 ctx->xin->packet != NULL, &in_port);
1391 xlate_report(ctx, "no input bundle, dropping");
1395 /* Drop malformed frames. */
1396 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
1397 !(flow->vlan_tci & htons(VLAN_CFI))) {
1398 if (ctx->xin->packet != NULL) {
1399 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1400 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
1401 "VLAN tag received on port %s",
1402 ctx->xbridge->name, in_xbundle->name);
1404 xlate_report(ctx, "partial VLAN tag, dropping");
1408 /* Drop frames on bundles reserved for mirroring. */
1409 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
1410 if (ctx->xin->packet != NULL) {
1411 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1412 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1413 "%s, which is reserved exclusively for mirroring",
1414 ctx->xbridge->name, in_xbundle->name);
1416 xlate_report(ctx, "input port is mirror output port, dropping");
1421 vid = vlan_tci_to_vid(flow->vlan_tci);
1422 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1423 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
1426 vlan = input_vid_to_vlan(in_xbundle, vid);
1428 /* Check other admissibility requirements. */
1429 if (in_port && !is_admissible(ctx, in_port, vlan)) {
1433 /* Learn source MAC. */
1434 if (ctx->xin->may_learn) {
1435 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
1438 /* Determine output bundle. */
1439 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
1440 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
1441 mac_port = mac ? mac->port.p : NULL;
1442 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
1445 struct xbundle *mac_xbundle = xbundle_lookup(mac_port);
1446 if (mac_xbundle && mac_xbundle != in_xbundle) {
1447 xlate_report(ctx, "forwarding to learned port");
1448 output_normal(ctx, mac_xbundle, vlan);
1449 } else if (!mac_xbundle) {
1450 xlate_report(ctx, "learned port is unknown, dropping");
1452 xlate_report(ctx, "learned port is input port, dropping");
1455 struct xbundle *xbundle;
1457 xlate_report(ctx, "no learned MAC for destination, flooding");
1458 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1459 if (xbundle != in_xbundle
1460 && xbundle_includes_vlan(xbundle, vlan)
1461 && xbundle->floodable
1462 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1463 output_normal(ctx, xbundle, vlan);
1466 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1470 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1471 * the number of packets out of UINT32_MAX to sample. The given
1472 * cookie is passed back in the callback for each sampled packet.
1475 compose_sample_action(const struct xbridge *xbridge,
1476 struct ofpbuf *odp_actions,
1477 const struct flow *flow,
1478 const uint32_t probability,
1479 const union user_action_cookie *cookie,
1480 const size_t cookie_size)
1482 size_t sample_offset, actions_offset;
1483 odp_port_t odp_port;
1487 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
1489 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
1491 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
1493 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
1494 pid = dpif_port_get_pid(xbridge->dpif, odp_port, 0);
1495 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size, odp_actions);
1497 nl_msg_end_nested(odp_actions, actions_offset);
1498 nl_msg_end_nested(odp_actions, sample_offset);
1499 return cookie_offset;
1503 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
1504 odp_port_t odp_port, unsigned int n_outputs,
1505 union user_action_cookie *cookie)
1509 cookie->type = USER_ACTION_COOKIE_SFLOW;
1510 cookie->sflow.vlan_tci = vlan_tci;
1512 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1513 * port information") for the interpretation of cookie->output. */
1514 switch (n_outputs) {
1516 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1517 cookie->sflow.output = 0x40000000 | 256;
1521 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
1523 cookie->sflow.output = ifindex;
1528 /* 0x80000000 means "multiple output ports. */
1529 cookie->sflow.output = 0x80000000 | n_outputs;
1534 /* Compose SAMPLE action for sFlow bridge sampling. */
1536 compose_sflow_action(const struct xbridge *xbridge,
1537 struct ofpbuf *odp_actions,
1538 const struct flow *flow,
1539 odp_port_t odp_port)
1541 uint32_t probability;
1542 union user_action_cookie cookie;
1544 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
1548 probability = dpif_sflow_get_probability(xbridge->sflow);
1549 compose_sflow_cookie(xbridge, htons(0), odp_port,
1550 odp_port == ODPP_NONE ? 0 : 1, &cookie);
1552 return compose_sample_action(xbridge, odp_actions, flow, probability,
1553 &cookie, sizeof cookie.sflow);
1557 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
1558 uint32_t obs_domain_id, uint32_t obs_point_id,
1559 union user_action_cookie *cookie)
1561 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
1562 cookie->flow_sample.probability = probability;
1563 cookie->flow_sample.collector_set_id = collector_set_id;
1564 cookie->flow_sample.obs_domain_id = obs_domain_id;
1565 cookie->flow_sample.obs_point_id = obs_point_id;
1569 compose_ipfix_cookie(union user_action_cookie *cookie)
1571 cookie->type = USER_ACTION_COOKIE_IPFIX;
1574 /* Compose SAMPLE action for IPFIX bridge sampling. */
1576 compose_ipfix_action(const struct xbridge *xbridge,
1577 struct ofpbuf *odp_actions,
1578 const struct flow *flow)
1580 uint32_t probability;
1581 union user_action_cookie cookie;
1583 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
1587 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
1588 compose_ipfix_cookie(&cookie);
1590 compose_sample_action(xbridge, odp_actions, flow, probability,
1591 &cookie, sizeof cookie.ipfix);
1594 /* SAMPLE action for sFlow must be first action in any given list of
1595 * actions. At this point we do not have all information required to
1596 * build it. So try to build sample action as complete as possible. */
1598 add_sflow_action(struct xlate_ctx *ctx)
1600 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
1601 &ctx->xout->odp_actions,
1602 &ctx->xin->flow, ODPP_NONE);
1603 ctx->sflow_odp_port = 0;
1604 ctx->sflow_n_outputs = 0;
1607 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
1608 * of actions, eventually after the SAMPLE action for sFlow. */
1610 add_ipfix_action(struct xlate_ctx *ctx)
1612 compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions,
1616 /* Fix SAMPLE action according to data collected while composing ODP actions.
1617 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
1618 * USERSPACE action's user-cookie which is required for sflow. */
1620 fix_sflow_action(struct xlate_ctx *ctx)
1622 const struct flow *base = &ctx->base_flow;
1623 union user_action_cookie *cookie;
1625 if (!ctx->user_cookie_offset) {
1629 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
1630 sizeof cookie->sflow);
1631 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
1633 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
1634 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
1637 static enum slow_path_reason
1638 process_special(struct xlate_ctx *ctx, const struct flow *flow,
1639 const struct xport *xport, const struct ofpbuf *packet)
1641 struct flow_wildcards *wc = &ctx->xout->wc;
1642 const struct xbridge *xbridge = ctx->xbridge;
1646 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
1648 cfm_process_heartbeat(xport->cfm, packet);
1651 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
1653 bfd_process_packet(xport->bfd, flow, packet);
1654 /* If POLL received, immediately sends FINAL back. */
1655 if (bfd_should_send_packet(xport->bfd)) {
1657 ofproto_dpif_monitor_port_send_soon(xport->ofport);
1659 ofproto_dpif_monitor_port_send_soon_safe(xport->ofport);
1664 } else if (xport->xbundle && xport->xbundle->lacp
1665 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1667 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
1670 } else if (xbridge->stp && stp_should_process_flow(flow, wc)) {
1672 stp_process_packet(xport, packet);
1681 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
1684 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1685 struct flow_wildcards *wc = &ctx->xout->wc;
1686 struct flow *flow = &ctx->xin->flow;
1687 ovs_be16 flow_vlan_tci;
1688 uint32_t flow_pkt_mark;
1689 uint8_t flow_nw_tos;
1690 odp_port_t out_port, odp_port;
1693 /* If 'struct flow' gets additional metadata, we'll need to zero it out
1694 * before traversing a patch port. */
1695 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 24);
1698 xlate_report(ctx, "Nonexistent output port");
1700 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
1701 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
1703 } else if (check_stp) {
1704 if (eth_addr_equals(ctx->base_flow.dl_dst, eth_addr_stp)) {
1705 if (!xport_stp_listen_state(xport)) {
1706 xlate_report(ctx, "STP not in listening state, "
1707 "skipping bpdu output");
1710 } else if (!xport_stp_forward_state(xport)) {
1711 xlate_report(ctx, "STP not in forwarding state, "
1717 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
1718 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
1723 const struct xport *peer = xport->peer;
1724 struct flow old_flow = ctx->xin->flow;
1725 enum slow_path_reason special;
1727 ctx->xbridge = peer->xbridge;
1728 flow->in_port.ofp_port = peer->ofp_port;
1729 flow->metadata = htonll(0);
1730 memset(&flow->tunnel, 0, sizeof flow->tunnel);
1731 memset(flow->regs, 0, sizeof flow->regs);
1733 special = process_special(ctx, &ctx->xin->flow, peer,
1736 ctx->xout->slow |= special;
1737 } else if (may_receive(peer, ctx)) {
1738 if (xport_stp_forward_state(peer)) {
1739 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1741 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
1742 * learning action look at the packet, then drop it. */
1743 struct flow old_base_flow = ctx->base_flow;
1744 size_t old_size = ctx->xout->odp_actions.size;
1745 mirror_mask_t old_mirrors = ctx->xout->mirrors;
1746 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1747 ctx->xout->mirrors = old_mirrors;
1748 ctx->base_flow = old_base_flow;
1749 ctx->xout->odp_actions.size = old_size;
1753 ctx->xin->flow = old_flow;
1754 ctx->xbridge = xport->xbridge;
1756 if (ctx->xin->resubmit_stats) {
1757 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1758 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
1760 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
1767 flow_vlan_tci = flow->vlan_tci;
1768 flow_pkt_mark = flow->pkt_mark;
1769 flow_nw_tos = flow->nw_tos;
1771 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
1772 wc->masks.nw_tos |= IP_ECN_MASK;
1773 flow->nw_tos &= ~IP_DSCP_MASK;
1774 flow->nw_tos |= dscp;
1777 if (xport->is_tunnel) {
1778 /* Save tunnel metadata so that changes made due to
1779 * the Logical (tunnel) Port are not visible for any further
1780 * matches, while explicit set actions on tunnel metadata are.
1782 struct flow_tnl flow_tnl = flow->tunnel;
1783 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
1784 if (odp_port == ODPP_NONE) {
1785 xlate_report(ctx, "Tunneling decided against output");
1786 goto out; /* restore flow_nw_tos */
1788 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
1789 xlate_report(ctx, "Not tunneling to our own address");
1790 goto out; /* restore flow_nw_tos */
1792 if (ctx->xin->resubmit_stats) {
1793 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1795 out_port = odp_port;
1796 commit_odp_tunnel_action(flow, &ctx->base_flow,
1797 &ctx->xout->odp_actions);
1798 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
1800 odp_port = xport->odp_port;
1801 out_port = odp_port;
1802 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
1803 ofp_port_t vlandev_port;
1805 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1806 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
1807 ofp_port, flow->vlan_tci);
1808 if (vlandev_port != ofp_port) {
1809 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
1810 flow->vlan_tci = htons(0);
1815 if (out_port != ODPP_NONE) {
1816 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
1817 &ctx->xout->odp_actions,
1819 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
1822 ctx->sflow_odp_port = odp_port;
1823 ctx->sflow_n_outputs++;
1824 ctx->xout->nf_output_iface = ofp_port;
1829 flow->vlan_tci = flow_vlan_tci;
1830 flow->pkt_mark = flow_pkt_mark;
1831 flow->nw_tos = flow_nw_tos;
1835 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
1837 compose_output_action__(ctx, ofp_port, true);
1841 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
1843 struct rule_dpif *old_rule = ctx->rule;
1844 struct rule_actions *actions;
1846 if (ctx->xin->resubmit_stats) {
1847 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
1853 actions = rule_dpif_get_actions(rule);
1854 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
1855 ctx->rule = old_rule;
1860 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
1862 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1864 if (ctx->recurse >= MAX_RESUBMIT_RECURSION) {
1865 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
1866 MAX_RESUBMIT_RECURSION);
1867 } else if (ctx->resubmits >= MAX_RESUBMITS) {
1868 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
1869 } else if (ctx->xout->odp_actions.size > UINT16_MAX) {
1870 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
1871 } else if (ctx->stack.size >= 65536) {
1872 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
1881 xlate_table_action(struct xlate_ctx *ctx,
1882 ofp_port_t in_port, uint8_t table_id, bool may_packet_in)
1884 if (xlate_resubmit_resource_check(ctx)) {
1885 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
1886 bool skip_wildcards = ctx->xin->skip_wildcards;
1887 uint8_t old_table_id = ctx->table_id;
1888 struct rule_dpif *rule;
1890 ctx->table_id = table_id;
1892 /* Look up a flow with 'in_port' as the input port. Then restore the
1893 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
1894 * have surprising behavior). */
1895 ctx->xin->flow.in_port.ofp_port = in_port;
1896 rule_dpif_lookup_in_table(ctx->xbridge->ofproto, &ctx->xin->flow,
1897 !skip_wildcards ? &ctx->xout->wc : NULL,
1899 ctx->xin->flow.in_port.ofp_port = old_in_port;
1901 if (ctx->xin->resubmit_hook) {
1902 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
1905 if (!rule && may_packet_in) {
1906 struct xport *xport;
1909 * check if table configuration flags
1910 * OFPTC11_TABLE_MISS_CONTROLLER, default.
1911 * OFPTC11_TABLE_MISS_CONTINUE,
1912 * OFPTC11_TABLE_MISS_DROP
1913 * When OF1.0, OFPTC11_TABLE_MISS_CONTINUE is used. What to do? */
1914 xport = get_ofp_port(ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
1915 choose_miss_rule(xport ? xport->config : 0,
1916 ctx->xbridge->miss_rule,
1917 ctx->xbridge->no_packet_in_rule, &rule);
1920 xlate_recursively(ctx, rule);
1921 rule_dpif_unref(rule);
1924 ctx->table_id = old_table_id;
1932 xlate_group_bucket(struct xlate_ctx *ctx, const struct ofputil_bucket *bucket)
1934 uint64_t action_list_stub[1024 / 8];
1935 struct ofpbuf action_list, action_set;
1937 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
1938 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
1940 ofpacts_execute_action_set(&action_list, &action_set);
1942 do_xlate_actions(action_list.data, action_list.size, ctx);
1945 ofpbuf_uninit(&action_set);
1946 ofpbuf_uninit(&action_list);
1950 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
1952 const struct ofputil_bucket *bucket;
1953 const struct list *buckets;
1954 struct flow old_flow = ctx->xin->flow;
1956 group_dpif_get_buckets(group, &buckets);
1958 LIST_FOR_EACH (bucket, list_node, buckets) {
1959 xlate_group_bucket(ctx, bucket);
1960 /* Roll back flow to previous state.
1961 * This is equivalent to cloning the packet for each bucket.
1963 * As a side effect any subsequently applied actions will
1964 * also effectively be applied to a clone of the packet taken
1965 * just before applying the all or indirect group. */
1966 ctx->xin->flow = old_flow;
1971 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
1973 const struct ofputil_bucket *bucket;
1975 bucket = group_first_live_bucket(ctx, group, 0);
1977 xlate_group_bucket(ctx, bucket);
1982 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
1984 struct flow_wildcards *wc = &ctx->xout->wc;
1985 const struct ofputil_bucket *bucket;
1988 basis = hash_bytes(ctx->xin->flow.dl_dst, sizeof ctx->xin->flow.dl_dst, 0);
1989 bucket = group_best_live_bucket(ctx, group, basis);
1991 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1992 xlate_group_bucket(ctx, bucket);
1997 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
1999 ctx->in_group = true;
2001 switch (group_dpif_get_type(group)) {
2003 case OFPGT11_INDIRECT:
2004 xlate_all_group(ctx, group);
2006 case OFPGT11_SELECT:
2007 xlate_select_group(ctx, group);
2010 xlate_ff_group(ctx, group);
2015 group_dpif_release(group);
2017 ctx->in_group = false;
2021 xlate_group_resource_check(struct xlate_ctx *ctx)
2023 if (!xlate_resubmit_resource_check(ctx)) {
2025 } else if (ctx->in_group) {
2026 /* Prevent nested translation of OpenFlow groups.
2028 * OpenFlow allows this restriction. We enforce this restriction only
2029 * because, with the current architecture, we would otherwise have to
2030 * take a possibly recursive read lock on the ofgroup rwlock, which is
2031 * unsafe given that POSIX allows taking a read lock to block if there
2032 * is a thread blocked on taking the write lock. Other solutions
2033 * without this restriction are also possible, but seem unwarranted
2034 * given the current limited use of groups. */
2035 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2037 VLOG_ERR_RL(&rl, "cannot recursively translate OpenFlow group");
2045 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
2047 if (xlate_group_resource_check(ctx)) {
2048 struct group_dpif *group;
2051 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
2053 xlate_group_action__(ctx, group);
2063 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
2064 const struct ofpact_resubmit *resubmit)
2069 in_port = resubmit->in_port;
2070 if (in_port == OFPP_IN_PORT) {
2071 in_port = ctx->xin->flow.in_port.ofp_port;
2074 table_id = resubmit->table_id;
2075 if (table_id == 255) {
2076 table_id = ctx->table_id;
2079 xlate_table_action(ctx, in_port, table_id, false);
2083 flood_packets(struct xlate_ctx *ctx, bool all)
2085 const struct xport *xport;
2087 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
2088 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
2093 compose_output_action__(ctx, xport->ofp_port, false);
2094 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
2095 compose_output_action(ctx, xport->ofp_port);
2099 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2103 execute_controller_action(struct xlate_ctx *ctx, int len,
2104 enum ofp_packet_in_reason reason,
2105 uint16_t controller_id)
2107 struct ofproto_packet_in *pin;
2108 struct ofpbuf *packet;
2109 struct pkt_metadata md = PKT_METADATA_INITIALIZER(0);
2111 ctx->xout->slow |= SLOW_CONTROLLER;
2112 if (!ctx->xin->packet) {
2116 packet = ofpbuf_clone(ctx->xin->packet);
2118 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2119 &ctx->xout->odp_actions,
2122 odp_execute_actions(NULL, packet, &md, ctx->xout->odp_actions.data,
2123 ctx->xout->odp_actions.size, NULL);
2125 pin = xmalloc(sizeof *pin);
2126 pin->up.packet_len = packet->size;
2127 pin->up.packet = ofpbuf_steal_data(packet);
2128 pin->up.reason = reason;
2129 pin->up.table_id = ctx->table_id;
2130 pin->up.cookie = (ctx->rule
2131 ? rule_dpif_get_flow_cookie(ctx->rule)
2134 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
2136 pin->controller_id = controller_id;
2137 pin->send_len = len;
2138 pin->generated_by_table_miss = (ctx->rule
2139 && rule_dpif_is_table_miss(ctx->rule));
2140 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
2141 ofpbuf_delete(packet);
2145 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
2147 struct flow_wildcards *wc = &ctx->xout->wc;
2148 struct flow *flow = &ctx->xin->flow;
2151 ovs_assert(eth_type_mpls(mpls->ethertype));
2153 n = flow_count_mpls_labels(flow, wc);
2155 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
2156 &ctx->xout->odp_actions,
2158 } else if (n >= FLOW_MAX_MPLS_LABELS) {
2159 if (ctx->xin->packet != NULL) {
2160 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2161 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
2162 "MPLS push action can't be performed as it would "
2163 "have more MPLS LSEs than the %d supported.",
2164 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
2168 } else if (n >= ctx->xbridge->max_mpls_depth) {
2169 COVERAGE_INC(xlate_actions_mpls_overflow);
2170 ctx->xout->slow |= SLOW_ACTION;
2173 flow_push_mpls(flow, n, mpls->ethertype, wc);
2177 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
2179 struct flow_wildcards *wc = &ctx->xout->wc;
2180 struct flow *flow = &ctx->xin->flow;
2181 int n = flow_count_mpls_labels(flow, wc);
2183 if (!flow_pop_mpls(flow, n, eth_type, wc) && n >= FLOW_MAX_MPLS_LABELS) {
2184 if (ctx->xin->packet != NULL) {
2185 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2186 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
2187 "MPLS pop action can't be performed as it has "
2188 "more MPLS LSEs than the %d supported.",
2189 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
2192 ofpbuf_clear(&ctx->xout->odp_actions);
2197 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
2199 struct flow *flow = &ctx->xin->flow;
2201 if (!is_ip_any(flow)) {
2205 ctx->xout->wc.masks.nw_ttl = 0xff;
2206 if (flow->nw_ttl > 1) {
2212 for (i = 0; i < ids->n_controllers; i++) {
2213 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
2217 /* Stop processing for current table. */
2223 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
2225 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2226 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
2227 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
2232 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
2234 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2235 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
2236 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
2241 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
2243 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2244 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
2245 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
2250 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
2252 struct flow *flow = &ctx->xin->flow;
2253 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
2254 struct flow_wildcards *wc = &ctx->xout->wc;
2256 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2257 if (eth_type_mpls(flow->dl_type)) {
2260 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
2263 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
2265 /* Stop processing for current table. */
2274 xlate_output_action(struct xlate_ctx *ctx,
2275 ofp_port_t port, uint16_t max_len, bool may_packet_in)
2277 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
2279 ctx->xout->nf_output_iface = NF_OUT_DROP;
2283 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
2286 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2293 flood_packets(ctx, false);
2296 flood_packets(ctx, true);
2298 case OFPP_CONTROLLER:
2299 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
2305 if (port != ctx->xin->flow.in_port.ofp_port) {
2306 compose_output_action(ctx, port);
2308 xlate_report(ctx, "skipping output to input port");
2313 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2314 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2315 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2316 ctx->xout->nf_output_iface = prev_nf_output_iface;
2317 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2318 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2319 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2324 xlate_output_reg_action(struct xlate_ctx *ctx,
2325 const struct ofpact_output_reg *or)
2327 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
2328 if (port <= UINT16_MAX) {
2329 union mf_subvalue value;
2331 memset(&value, 0xff, sizeof value);
2332 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
2333 xlate_output_action(ctx, u16_to_ofp(port),
2334 or->max_len, false);
2339 xlate_enqueue_action(struct xlate_ctx *ctx,
2340 const struct ofpact_enqueue *enqueue)
2342 ofp_port_t ofp_port = enqueue->port;
2343 uint32_t queue_id = enqueue->queue;
2344 uint32_t flow_priority, priority;
2347 /* Translate queue to priority. */
2348 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
2350 /* Fall back to ordinary output action. */
2351 xlate_output_action(ctx, enqueue->port, 0, false);
2355 /* Check output port. */
2356 if (ofp_port == OFPP_IN_PORT) {
2357 ofp_port = ctx->xin->flow.in_port.ofp_port;
2358 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
2362 /* Add datapath actions. */
2363 flow_priority = ctx->xin->flow.skb_priority;
2364 ctx->xin->flow.skb_priority = priority;
2365 compose_output_action(ctx, ofp_port);
2366 ctx->xin->flow.skb_priority = flow_priority;
2368 /* Update NetFlow output port. */
2369 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2370 ctx->xout->nf_output_iface = ofp_port;
2371 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2372 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2377 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
2379 uint32_t skb_priority;
2381 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
2382 ctx->xin->flow.skb_priority = skb_priority;
2384 /* Couldn't translate queue to a priority. Nothing to do. A warning
2385 * has already been logged. */
2390 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
2392 const struct xbridge *xbridge = xbridge_;
2403 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
2406 port = get_ofp_port(xbridge, ofp_port);
2407 return port ? port->may_enable : false;
2412 xlate_bundle_action(struct xlate_ctx *ctx,
2413 const struct ofpact_bundle *bundle)
2417 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
2419 CONST_CAST(struct xbridge *, ctx->xbridge));
2420 if (bundle->dst.field) {
2421 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
2424 xlate_output_action(ctx, port, 0, false);
2429 xlate_learn_action(struct xlate_ctx *ctx,
2430 const struct ofpact_learn *learn)
2432 uint64_t ofpacts_stub[1024 / 8];
2433 struct ofputil_flow_mod fm;
2434 struct ofpbuf ofpacts;
2436 ctx->xout->has_learn = true;
2438 learn_mask(learn, &ctx->xout->wc);
2440 if (!ctx->xin->may_learn) {
2444 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2445 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
2446 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, &fm);
2447 ofpbuf_uninit(&ofpacts);
2451 xlate_fin_timeout(struct xlate_ctx *ctx,
2452 const struct ofpact_fin_timeout *oft)
2454 if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
2455 rule_dpif_reduce_timeouts(ctx->rule, oft->fin_idle_timeout,
2456 oft->fin_hard_timeout);
2461 xlate_sample_action(struct xlate_ctx *ctx,
2462 const struct ofpact_sample *os)
2464 union user_action_cookie cookie;
2465 /* Scale the probability from 16-bit to 32-bit while representing
2466 * the same percentage. */
2467 uint32_t probability = (os->probability << 16) | os->probability;
2469 if (!ctx->xbridge->variable_length_userdata) {
2470 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2472 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
2473 "lacks support (needs Linux 3.10+ or kernel module from "
2478 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2479 &ctx->xout->odp_actions,
2482 compose_flow_sample_cookie(os->probability, os->collector_set_id,
2483 os->obs_domain_id, os->obs_point_id, &cookie);
2484 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
2485 probability, &cookie, sizeof cookie.flow_sample);
2489 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
2491 if (xport->config & (eth_addr_equals(ctx->xin->flow.dl_dst, eth_addr_stp)
2492 ? OFPUTIL_PC_NO_RECV_STP
2493 : OFPUTIL_PC_NO_RECV)) {
2497 /* Only drop packets here if both forwarding and learning are
2498 * disabled. If just learning is enabled, we need to have
2499 * OFPP_NORMAL and the learning action have a look at the packet
2500 * before we can drop it. */
2501 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
2509 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
2511 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2512 ofpbuf_put(&ctx->action_set, on->actions, ofpact_nest_get_action_len(on));
2513 ofpact_pad(&ctx->action_set);
2517 xlate_action_set(struct xlate_ctx *ctx)
2519 uint64_t action_list_stub[1024 / 64];
2520 struct ofpbuf action_list;
2522 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2523 ofpacts_execute_action_set(&action_list, &ctx->action_set);
2524 do_xlate_actions(action_list.data, action_list.size, ctx);
2525 ofpbuf_uninit(&action_list);
2529 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
2530 struct xlate_ctx *ctx)
2532 struct flow_wildcards *wc = &ctx->xout->wc;
2533 struct flow *flow = &ctx->xin->flow;
2534 const struct ofpact *a;
2536 /* dl_type already in the mask, not set below. */
2538 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2539 struct ofpact_controller *controller;
2540 const struct ofpact_metadata *metadata;
2541 const struct ofpact_set_field *set_field;
2542 const struct mf_field *mf;
2550 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
2551 ofpact_get_OUTPUT(a)->max_len, true);
2555 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
2560 case OFPACT_CONTROLLER:
2561 controller = ofpact_get_CONTROLLER(a);
2562 execute_controller_action(ctx, controller->max_len,
2564 controller->controller_id);
2567 case OFPACT_ENQUEUE:
2568 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
2571 case OFPACT_SET_VLAN_VID:
2572 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2573 if (flow->vlan_tci & htons(VLAN_CFI) ||
2574 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2575 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
2576 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
2581 case OFPACT_SET_VLAN_PCP:
2582 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
2583 if (flow->vlan_tci & htons(VLAN_CFI) ||
2584 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2585 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
2586 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
2587 << VLAN_PCP_SHIFT) | VLAN_CFI);
2591 case OFPACT_STRIP_VLAN:
2592 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2593 flow->vlan_tci = htons(0);
2596 case OFPACT_PUSH_VLAN:
2597 /* XXX 802.1AD(QinQ) */
2598 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2599 flow->vlan_tci = htons(VLAN_CFI);
2602 case OFPACT_SET_ETH_SRC:
2603 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2604 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2607 case OFPACT_SET_ETH_DST:
2608 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2609 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2612 case OFPACT_SET_IPV4_SRC:
2613 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2614 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2615 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2619 case OFPACT_SET_IPV4_DST:
2620 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2621 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2622 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
2626 case OFPACT_SET_IP_DSCP:
2627 if (is_ip_any(flow)) {
2628 wc->masks.nw_tos |= IP_DSCP_MASK;
2629 flow->nw_tos &= ~IP_DSCP_MASK;
2630 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
2634 case OFPACT_SET_IP_ECN:
2635 if (is_ip_any(flow)) {
2636 wc->masks.nw_tos |= IP_ECN_MASK;
2637 flow->nw_tos &= ~IP_ECN_MASK;
2638 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
2642 case OFPACT_SET_IP_TTL:
2643 if (is_ip_any(flow)) {
2644 wc->masks.nw_ttl = 0xff;
2645 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
2649 case OFPACT_SET_L4_SRC_PORT:
2650 if (is_ip_any(flow)) {
2651 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2652 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2653 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2657 case OFPACT_SET_L4_DST_PORT:
2658 if (is_ip_any(flow)) {
2659 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2660 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2661 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2665 case OFPACT_RESUBMIT:
2666 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
2669 case OFPACT_SET_TUNNEL:
2670 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2673 case OFPACT_SET_QUEUE:
2674 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2677 case OFPACT_POP_QUEUE:
2678 flow->skb_priority = ctx->orig_skb_priority;
2681 case OFPACT_REG_MOVE:
2682 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
2685 case OFPACT_REG_LOAD:
2686 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow, wc);
2689 case OFPACT_SET_FIELD:
2690 set_field = ofpact_get_SET_FIELD(a);
2691 mf = set_field->field;
2692 mf_mask_field_and_prereqs(mf, &wc->masks);
2694 /* Set field action only ever overwrites packet's outermost
2695 * applicable header fields. Do nothing if no header exists. */
2696 if ((mf->id != MFF_VLAN_VID || flow->vlan_tci & htons(VLAN_CFI))
2697 && ((mf->id != MFF_MPLS_LABEL && mf->id != MFF_MPLS_TC)
2698 || eth_type_mpls(flow->dl_type))) {
2699 mf_set_flow_value(mf, &set_field->value, flow);
2703 case OFPACT_STACK_PUSH:
2704 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
2708 case OFPACT_STACK_POP:
2709 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
2713 case OFPACT_PUSH_MPLS:
2714 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
2717 case OFPACT_POP_MPLS:
2718 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
2721 case OFPACT_SET_MPLS_LABEL:
2722 compose_set_mpls_label_action(
2723 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
2726 case OFPACT_SET_MPLS_TC:
2727 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
2730 case OFPACT_SET_MPLS_TTL:
2731 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
2734 case OFPACT_DEC_MPLS_TTL:
2735 if (compose_dec_mpls_ttl_action(ctx)) {
2740 case OFPACT_DEC_TTL:
2741 wc->masks.nw_ttl = 0xff;
2742 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
2748 /* Nothing to do. */
2751 case OFPACT_MULTIPATH:
2752 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
2756 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
2759 case OFPACT_OUTPUT_REG:
2760 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
2764 xlate_learn_action(ctx, ofpact_get_LEARN(a));
2771 case OFPACT_FIN_TIMEOUT:
2772 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2773 ctx->xout->has_fin_timeout = true;
2774 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
2777 case OFPACT_CLEAR_ACTIONS:
2778 ofpbuf_clear(&ctx->action_set);
2781 case OFPACT_WRITE_ACTIONS:
2782 xlate_write_actions(ctx, a);
2785 case OFPACT_WRITE_METADATA:
2786 metadata = ofpact_get_WRITE_METADATA(a);
2787 flow->metadata &= ~metadata->mask;
2788 flow->metadata |= metadata->metadata & metadata->mask;
2792 /* Not implemented yet. */
2795 case OFPACT_GOTO_TABLE: {
2796 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
2798 ovs_assert(ctx->table_id < ogt->table_id);
2799 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2800 ogt->table_id, true);
2805 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
2812 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
2813 const struct flow *flow, struct rule_dpif *rule,
2814 uint16_t tcp_flags, const struct ofpbuf *packet)
2816 xin->ofproto = ofproto;
2818 xin->packet = packet;
2819 xin->may_learn = packet != NULL;
2821 xin->ofpacts = NULL;
2822 xin->ofpacts_len = 0;
2823 xin->tcp_flags = tcp_flags;
2824 xin->resubmit_hook = NULL;
2825 xin->report_hook = NULL;
2826 xin->resubmit_stats = NULL;
2827 xin->skip_wildcards = false;
2831 xlate_out_uninit(struct xlate_out *xout)
2834 ofpbuf_uninit(&xout->odp_actions);
2838 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
2839 * into datapath actions, using 'ctx', and discards the datapath actions. */
2841 xlate_actions_for_side_effects(struct xlate_in *xin)
2843 struct xlate_out xout;
2845 xlate_actions(xin, &xout);
2846 xlate_out_uninit(&xout);
2850 xlate_report(struct xlate_ctx *ctx, const char *s)
2852 if (ctx->xin->report_hook) {
2853 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
2858 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
2861 dst->slow = src->slow;
2862 dst->has_learn = src->has_learn;
2863 dst->has_normal = src->has_normal;
2864 dst->has_fin_timeout = src->has_fin_timeout;
2865 dst->nf_output_iface = src->nf_output_iface;
2866 dst->mirrors = src->mirrors;
2868 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
2869 sizeof dst->odp_actions_stub);
2870 ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
2871 src->odp_actions.size);
2874 static struct skb_priority_to_dscp *
2875 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
2877 struct skb_priority_to_dscp *pdscp;
2880 hash = hash_int(skb_priority, 0);
2881 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
2882 if (pdscp->skb_priority == skb_priority) {
2890 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
2893 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
2894 *dscp = pdscp ? pdscp->dscp : 0;
2895 return pdscp != NULL;
2899 clear_skb_priorities(struct xport *xport)
2901 struct skb_priority_to_dscp *pdscp, *next;
2903 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
2904 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
2910 actions_output_to_local_port(const struct xlate_ctx *ctx)
2912 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
2913 const struct nlattr *a;
2916 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions.data,
2917 ctx->xout->odp_actions.size) {
2918 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
2919 && nl_attr_get_odp_port(a) == local_odp_port) {
2926 /* Thread safe call to xlate_actions__(). */
2928 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
2929 OVS_EXCLUDED(xlate_rwlock)
2931 ovs_rwlock_rdlock(&xlate_rwlock);
2932 xlate_actions__(xin, xout);
2933 ovs_rwlock_unlock(&xlate_rwlock);
2936 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
2937 * into datapath actions in 'odp_actions', using 'ctx'.
2939 * The caller must take responsibility for eventually freeing 'xout', with
2940 * xlate_out_uninit(). */
2942 xlate_actions__(struct xlate_in *xin, struct xlate_out *xout)
2943 OVS_REQ_RDLOCK(xlate_rwlock)
2945 struct flow_wildcards *wc = &xout->wc;
2946 struct flow *flow = &xin->flow;
2947 struct rule_dpif *rule = NULL;
2949 struct rule_actions *actions = NULL;
2950 enum slow_path_reason special;
2951 const struct ofpact *ofpacts;
2952 struct xport *in_port;
2953 struct flow orig_flow;
2954 struct xlate_ctx ctx;
2959 COVERAGE_INC(xlate_actions);
2961 /* Flow initialization rules:
2962 * - 'base_flow' must match the kernel's view of the packet at the
2963 * time that action processing starts. 'flow' represents any
2964 * transformations we wish to make through actions.
2965 * - By default 'base_flow' and 'flow' are the same since the input
2966 * packet matches the output before any actions are applied.
2967 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
2968 * of the received packet as seen by the kernel. If we later output
2969 * to another device without any modifications this will cause us to
2970 * insert a new tag since the original one was stripped off by the
2972 * - Tunnel metadata as received is retained in 'flow'. This allows
2973 * tunnel metadata matching also in later tables.
2974 * Since a kernel action for setting the tunnel metadata will only be
2975 * generated with actual tunnel output, changing the tunnel metadata
2976 * values in 'flow' (such as tun_id) will only have effect with a later
2977 * tunnel output action.
2978 * - Tunnel 'base_flow' is completely cleared since that is what the
2979 * kernel does. If we wish to maintain the original values an action
2980 * needs to be generated. */
2985 ctx.xout->has_learn = false;
2986 ctx.xout->has_normal = false;
2987 ctx.xout->has_fin_timeout = false;
2988 ctx.xout->nf_output_iface = NF_OUT_DROP;
2989 ctx.xout->mirrors = 0;
2990 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
2991 sizeof ctx.xout->odp_actions_stub);
2992 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
2994 ctx.xbridge = xbridge_lookup(xin->ofproto);
2999 ctx.rule = xin->rule;
3001 ctx.base_flow = *flow;
3002 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
3003 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
3005 flow_wildcards_init_catchall(wc);
3006 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
3007 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3008 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3009 if (is_ip_any(flow)) {
3010 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3012 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
3014 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
3015 if (ctx.xbridge->netflow) {
3016 netflow_mask_wc(flow, wc);
3021 ctx.in_group = false;
3022 ctx.orig_skb_priority = flow->skb_priority;
3026 if (!xin->ofpacts && !ctx.rule) {
3027 rule_dpif_lookup(ctx.xbridge->ofproto, flow,
3028 !xin->skip_wildcards ? wc : NULL, &rule);
3029 if (ctx.xin->resubmit_stats) {
3030 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
3034 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
3037 ofpacts = xin->ofpacts;
3038 ofpacts_len = xin->ofpacts_len;
3039 } else if (ctx.rule) {
3040 actions = rule_dpif_get_actions(ctx.rule);
3041 ofpacts = actions->ofpacts;
3042 ofpacts_len = actions->ofpacts_len;
3047 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
3048 ofpbuf_use_stub(&ctx.action_set,
3049 ctx.action_set_stub, sizeof ctx.action_set_stub);
3051 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3052 /* Do this conditionally because the copy is expensive enough that it
3053 * shows up in profiles. */
3057 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3058 switch (ctx.xbridge->frag) {
3059 case OFPC_FRAG_NORMAL:
3060 /* We must pretend that transport ports are unavailable. */
3061 flow->tp_src = ctx.base_flow.tp_src = htons(0);
3062 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
3065 case OFPC_FRAG_DROP:
3068 case OFPC_FRAG_REASM:
3071 case OFPC_FRAG_NX_MATCH:
3072 /* Nothing to do. */
3075 case OFPC_INVALID_TTL_TO_CONTROLLER:
3080 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
3081 if (in_port && in_port->is_tunnel && ctx.xin->resubmit_stats) {
3082 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
3084 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
3088 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
3090 ctx.xout->slow |= special;
3092 size_t sample_actions_len;
3094 if (flow->in_port.ofp_port
3095 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
3096 flow->in_port.ofp_port,
3098 ctx.base_flow.vlan_tci = 0;
3101 add_sflow_action(&ctx);
3102 add_ipfix_action(&ctx);
3103 sample_actions_len = ctx.xout->odp_actions.size;
3105 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
3106 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
3108 /* We've let OFPP_NORMAL and the learning action look at the
3109 * packet, so drop it now if forwarding is disabled. */
3110 if (in_port && !xport_stp_forward_state(in_port)) {
3111 ctx.xout->odp_actions.size = sample_actions_len;
3115 if (ctx.action_set.size) {
3116 xlate_action_set(&ctx);
3119 if (ctx.xbridge->has_in_band
3120 && in_band_must_output_to_local_port(flow)
3121 && !actions_output_to_local_port(&ctx)) {
3122 compose_output_action(&ctx, OFPP_LOCAL);
3125 fix_sflow_action(&ctx);
3127 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3128 add_mirror_actions(&ctx, &orig_flow);
3132 if (nl_attr_oversized(ctx.xout->odp_actions.size)) {
3133 /* These datapath actions are too big for a Netlink attribute, so we
3134 * can't hand them to the kernel directly. dpif_execute() can execute
3135 * them one by one with help, so just mark the result as SLOW_ACTION to
3136 * prevent the flow from being installed. */
3137 COVERAGE_INC(xlate_actions_oversize);
3138 ctx.xout->slow |= SLOW_ACTION;
3141 if (ctx.xin->resubmit_stats) {
3142 mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
3143 ctx.xin->resubmit_stats->n_packets,
3144 ctx.xin->resubmit_stats->n_bytes);
3146 if (ctx.xbridge->netflow) {
3147 const struct ofpact *ofpacts;
3150 ofpacts_len = actions->ofpacts_len;
3151 ofpacts = actions->ofpacts;
3152 if (ofpacts_len == 0
3153 || ofpacts->type != OFPACT_CONTROLLER
3154 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
3155 /* Only update netflow if we don't have controller flow. We don't
3156 * report NetFlow expiration messages for such facets because they
3157 * are just part of the control logic for the network, not real
3159 netflow_flow_update(ctx.xbridge->netflow, flow,
3160 xout->nf_output_iface,
3161 ctx.xin->resubmit_stats);
3166 ofpbuf_uninit(&ctx.stack);
3167 ofpbuf_uninit(&ctx.action_set);
3169 /* Clear the metadata and register wildcard masks, because we won't
3170 * use non-header fields as part of the cache. */
3171 flow_wildcards_clear_non_packet_fields(wc);
3173 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow uses
3174 * the low 8 bits of the 16-bit tp_src and tp_dst members to represent
3175 * these fields. The datapath interface, on the other hand, represents
3176 * them with just 8 bits each. This means that if the high 8 bits of the
3177 * masks for these fields somehow become set, then they will get chopped
3178 * off by a round trip through the datapath, and revalidation will spot
3179 * that as an inconsistency and delete the flow. Avoid the problem here by
3180 * making sure that only the low 8 bits of either field can be unwildcarded
3184 wc->masks.tp_src &= htons(UINT8_MAX);
3185 wc->masks.tp_dst &= htons(UINT8_MAX);
3189 rule_dpif_unref(rule);
3192 /* Sends 'packet' out 'ofport'.
3193 * May modify 'packet'.
3194 * Returns 0 if successful, otherwise a positive errno value. */
3196 xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3198 struct xport *xport;
3199 struct ofpact_output output;
3202 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
3203 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
3204 flow_extract(packet, NULL, &flow);
3205 flow.in_port.ofp_port = OFPP_NONE;
3207 ovs_rwlock_rdlock(&xlate_rwlock);
3208 xport = xport_lookup(ofport);
3210 ovs_rwlock_unlock(&xlate_rwlock);
3213 output.port = xport->ofp_port;
3215 ovs_rwlock_unlock(&xlate_rwlock);
3217 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
3218 &output.ofpact, sizeof output,