1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. */
17 #include "ofproto/ofproto-dpif-xlate.h"
23 #include "byte-order.h"
28 #include "dynamic-string.h"
31 #include "mac-learning.h"
32 #include "meta-flow.h"
33 #include "multipath.h"
34 #include "netdev-vport.h"
37 #include "odp-execute.h"
38 #include "ofp-actions.h"
39 #include "ofproto/ofproto-dpif-ipfix.h"
40 #include "ofproto/ofproto-dpif-sflow.h"
41 #include "ofproto/ofproto-dpif.h"
45 COVERAGE_DEFINE(ofproto_dpif_xlate);
47 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
49 /* Maximum depth of flow table recursion (due to resubmit actions) in a
50 * flow translation. */
51 #define MAX_RESUBMIT_RECURSION 64
55 struct xlate_out *xout;
57 struct ofproto_dpif *ofproto;
59 /* Flow at the last commit. */
60 struct flow base_flow;
62 /* Tunnel IP destination address as received. This is stored separately
63 * as the base_flow.tunnel is cleared on init to reflect the datapath
64 * behavior. Used to make sure not to send tunneled output to ourselves,
65 * which might lead to an infinite loop. This could happen easily
66 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
67 * actually set the tun_dst field. */
68 ovs_be32 orig_tunnel_ip_dst;
70 /* Stack for the push and pop actions. Each stack element is of type
71 * "union mf_subvalue". */
72 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
75 /* The rule that we are currently translating, or NULL. */
76 struct rule_dpif *rule;
78 int recurse; /* Recursion level, via xlate_table_action. */
79 bool max_resubmit_trigger; /* Recursed too deeply during translation. */
80 uint32_t orig_skb_priority; /* Priority when packet arrived. */
81 uint8_t table_id; /* OpenFlow table ID where flow was found. */
82 uint32_t sflow_n_outputs; /* Number of output ports. */
83 uint32_t sflow_odp_port; /* Output port for composing sFlow action. */
84 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
85 bool exit; /* No further actions should be processed. */
88 /* A controller may use OFPP_NONE as the ingress port to indicate that
89 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
90 * when an input bundle is needed for validation (e.g., mirroring or
91 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
92 * any 'port' structs, so care must be taken when dealing with it. */
93 static struct ofbundle ofpp_none_bundle = {
95 .vlan_mode = PORT_VLAN_TRUNK
98 static bool may_receive(const struct ofport_dpif *, struct xlate_ctx *);
99 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
101 static void xlate_normal(struct xlate_ctx *);
102 static void xlate_report(struct xlate_ctx *, const char *);
103 static void xlate_table_action(struct xlate_ctx *, uint16_t in_port,
104 uint8_t table_id, bool may_packet_in);
105 static bool input_vid_is_valid(uint16_t vid, struct ofbundle *, bool warn);
106 static uint16_t input_vid_to_vlan(const struct ofbundle *, uint16_t vid);
107 static void output_normal(struct xlate_ctx *, const struct ofbundle *,
109 static void compose_output_action(struct xlate_ctx *, uint16_t ofp_port);
111 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
114 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
116 return (bundle->vlan_mode != PORT_VLAN_ACCESS
117 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
121 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
123 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
127 vlan_is_mirrored(const struct ofmirror *m, int vlan)
129 return !m->vlans || bitmap_is_set(m->vlans, vlan);
132 static struct ofbundle *
133 lookup_input_bundle(const struct ofproto_dpif *ofproto, uint16_t in_port,
134 bool warn, struct ofport_dpif **in_ofportp)
136 struct ofport_dpif *ofport;
138 /* Find the port and bundle for the received packet. */
139 ofport = get_ofp_port(ofproto, in_port);
141 *in_ofportp = ofport;
143 if (ofport && ofport->bundle) {
144 return ofport->bundle;
147 /* Special-case OFPP_NONE, which a controller may use as the ingress
148 * port for traffic that it is sourcing. */
149 if (in_port == OFPP_NONE) {
150 return &ofpp_none_bundle;
153 /* Odd. A few possible reasons here:
155 * - We deleted a port but there are still a few packets queued up
158 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
159 * we don't know about.
161 * - The ofproto client didn't configure the port as part of a bundle.
162 * This is particularly likely to happen if a packet was received on the
163 * port after it was created, but before the client had a chance to
164 * configure its bundle.
167 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
169 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
170 "port %"PRIu16, ofproto->up.name, in_port);
176 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
178 struct ofproto_dpif *ofproto = ctx->ofproto;
179 mirror_mask_t mirrors;
180 struct ofbundle *in_bundle;
183 const struct nlattr *a;
186 in_bundle = lookup_input_bundle(ctx->ofproto, orig_flow->in_port,
187 ctx->xin->packet != NULL, NULL);
191 mirrors = in_bundle->src_mirrors;
193 /* Drop frames on bundles reserved for mirroring. */
194 if (in_bundle->mirror_out) {
195 if (ctx->xin->packet != NULL) {
196 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
197 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
198 "%s, which is reserved exclusively for mirroring",
199 ctx->ofproto->up.name, in_bundle->name);
205 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
206 if (!input_vid_is_valid(vid, in_bundle, ctx->xin->packet != NULL)) {
209 vlan = input_vid_to_vlan(in_bundle, vid);
211 /* Look at the output ports to check for destination selections. */
213 NL_ATTR_FOR_EACH (a, left, ctx->xout->odp_actions.data,
214 ctx->xout->odp_actions.size) {
215 enum ovs_action_attr type = nl_attr_type(a);
216 struct ofport_dpif *ofport;
218 if (type != OVS_ACTION_ATTR_OUTPUT) {
222 ofport = get_odp_port(ofproto, nl_attr_get_u32(a));
223 if (ofport && ofport->bundle) {
224 mirrors |= ofport->bundle->dst_mirrors;
232 /* Restore the original packet before adding the mirror actions. */
233 ctx->xin->flow = *orig_flow;
238 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
241 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
244 if (!vlan_is_mirrored(m, vlan)) {
245 mirrors = zero_rightmost_1bit(mirrors);
249 mirrors &= ~m->dup_mirrors;
250 ctx->xout->mirrors |= m->dup_mirrors;
252 output_normal(ctx, m->out, vlan);
253 } else if (vlan != m->out_vlan
254 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
255 struct ofbundle *bundle;
257 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
258 if (ofbundle_includes_vlan(bundle, m->out_vlan)
259 && !bundle->mirror_out) {
260 output_normal(ctx, bundle, m->out_vlan);
267 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
268 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
269 * the bundle on which the packet was received, returns the VLAN to which the
272 * Both 'vid' and the return value are in the range 0...4095. */
274 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
276 switch (in_bundle->vlan_mode) {
277 case PORT_VLAN_ACCESS:
278 return in_bundle->vlan;
281 case PORT_VLAN_TRUNK:
284 case PORT_VLAN_NATIVE_UNTAGGED:
285 case PORT_VLAN_NATIVE_TAGGED:
286 return vid ? vid : in_bundle->vlan;
293 /* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'.
294 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
297 * 'vid' should be the VID obtained from the 802.1Q header that was received as
298 * part of a packet (specify 0 if there was no 802.1Q header), in the range
301 input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn)
303 /* Allow any VID on the OFPP_NONE port. */
304 if (in_bundle == &ofpp_none_bundle) {
308 switch (in_bundle->vlan_mode) {
309 case PORT_VLAN_ACCESS:
312 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
313 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
314 "packet received on port %s configured as VLAN "
315 "%"PRIu16" access port",
316 in_bundle->ofproto->up.name, vid,
317 in_bundle->name, in_bundle->vlan);
323 case PORT_VLAN_NATIVE_UNTAGGED:
324 case PORT_VLAN_NATIVE_TAGGED:
326 /* Port must always carry its native VLAN. */
330 case PORT_VLAN_TRUNK:
331 if (!ofbundle_includes_vlan(in_bundle, vid)) {
333 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
334 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet "
335 "received on port %s not configured for trunking "
337 in_bundle->ofproto->up.name, vid,
338 in_bundle->name, vid);
350 /* Given 'vlan', the VLAN that a packet belongs to, and
351 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
352 * that should be included in the 802.1Q header. (If the return value is 0,
353 * then the 802.1Q header should only be included in the packet if there is a
356 * Both 'vlan' and the return value are in the range 0...4095. */
358 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
360 switch (out_bundle->vlan_mode) {
361 case PORT_VLAN_ACCESS:
364 case PORT_VLAN_TRUNK:
365 case PORT_VLAN_NATIVE_TAGGED:
368 case PORT_VLAN_NATIVE_UNTAGGED:
369 return vlan == out_bundle->vlan ? 0 : vlan;
377 output_normal(struct xlate_ctx *ctx, const struct ofbundle *out_bundle,
380 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
381 struct ofport_dpif *port;
383 ovs_be16 tci, old_tci;
385 vid = output_vlan_to_vid(out_bundle, vlan);
386 if (!out_bundle->bond) {
387 port = ofbundle_get_a_port(out_bundle);
389 port = bond_choose_output_slave(out_bundle->bond, &ctx->xin->flow,
390 &ctx->xout->wc, vid, &ctx->xout->tags);
392 /* No slaves enabled, so drop packet. */
399 if (tci || out_bundle->use_priority_tags) {
400 tci |= *flow_tci & htons(VLAN_PCP_MASK);
402 tci |= htons(VLAN_CFI);
407 compose_output_action(ctx, port->up.ofp_port);
411 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
412 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
413 * indicate this; newer upstream kernels use gratuitous ARP requests. */
415 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
417 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
421 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
422 if (!eth_addr_is_broadcast(flow->dl_dst)) {
426 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
427 if (flow->nw_proto == ARP_OP_REPLY) {
429 } else if (flow->nw_proto == ARP_OP_REQUEST) {
430 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
431 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
433 return flow->nw_src == flow->nw_dst;
440 update_learning_table(struct ofproto_dpif *ofproto,
441 const struct flow *flow, struct flow_wildcards *wc,
442 int vlan, struct ofbundle *in_bundle)
444 struct mac_entry *mac;
446 /* Don't learn the OFPP_NONE port. */
447 if (in_bundle == &ofpp_none_bundle) {
451 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
455 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
456 if (is_gratuitous_arp(flow, wc)) {
457 /* We don't want to learn from gratuitous ARP packets that are
458 * reflected back over bond slaves so we lock the learning table. */
459 if (!in_bundle->bond) {
460 mac_entry_set_grat_arp_lock(mac);
461 } else if (mac_entry_is_grat_arp_locked(mac)) {
466 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
467 /* The log messages here could actually be useful in debugging,
468 * so keep the rate limit relatively high. */
469 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
470 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
471 "on port %s in VLAN %d",
472 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
473 in_bundle->name, vlan);
475 mac->port.p = in_bundle;
476 mac_learning_changed(ofproto->ml, mac);
480 /* Determines whether packets in 'flow' within 'ofproto' should be forwarded or
481 * dropped. Returns true if they may be forwarded, false if they should be
484 * 'in_port' must be the ofport_dpif that corresponds to flow->in_port.
485 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
487 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
488 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
489 * checked by input_vid_is_valid().
491 * May also add tags to '*tags', although the current implementation only does
492 * so in one special case.
495 is_admissible(struct xlate_ctx *ctx, struct ofport_dpif *in_port,
498 struct ofproto_dpif *ofproto = ctx->ofproto;
499 struct flow *flow = &ctx->xin->flow;
500 struct ofbundle *in_bundle = in_port->bundle;
502 /* Drop frames for reserved multicast addresses
503 * only if forward_bpdu option is absent. */
504 if (!ofproto->up.forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
505 xlate_report(ctx, "packet has reserved destination MAC, dropping");
509 if (in_bundle->bond) {
510 struct mac_entry *mac;
512 switch (bond_check_admissibility(in_bundle->bond, in_port,
513 flow->dl_dst, &ctx->xout->tags)) {
518 xlate_report(ctx, "bonding refused admissibility, dropping");
521 case BV_DROP_IF_MOVED:
522 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
523 if (mac && mac->port.p != in_bundle &&
524 (!is_gratuitous_arp(flow, &ctx->xout->wc)
525 || mac_entry_is_grat_arp_locked(mac))) {
526 xlate_report(ctx, "SLB bond thinks this packet looped back, "
538 xlate_normal(struct xlate_ctx *ctx)
540 struct flow_wildcards *wc = &ctx->xout->wc;
541 struct flow *flow = &ctx->xin->flow;
542 struct ofport_dpif *in_port;
543 struct ofbundle *in_bundle;
544 struct mac_entry *mac;
548 ctx->xout->has_normal = true;
550 /* Check the dl_type, since we may check for gratuituous ARP. */
551 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
552 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
553 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
554 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
556 in_bundle = lookup_input_bundle(ctx->ofproto, flow->in_port,
557 ctx->xin->packet != NULL, &in_port);
559 xlate_report(ctx, "no input bundle, dropping");
563 /* Drop malformed frames. */
564 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
565 !(flow->vlan_tci & htons(VLAN_CFI))) {
566 if (ctx->xin->packet != NULL) {
567 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
568 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
569 "VLAN tag received on port %s",
570 ctx->ofproto->up.name, in_bundle->name);
572 xlate_report(ctx, "partial VLAN tag, dropping");
576 /* Drop frames on bundles reserved for mirroring. */
577 if (in_bundle->mirror_out) {
578 if (ctx->xin->packet != NULL) {
579 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
580 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
581 "%s, which is reserved exclusively for mirroring",
582 ctx->ofproto->up.name, in_bundle->name);
584 xlate_report(ctx, "input port is mirror output port, dropping");
589 vid = vlan_tci_to_vid(flow->vlan_tci);
590 if (!input_vid_is_valid(vid, in_bundle, ctx->xin->packet != NULL)) {
591 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
594 vlan = input_vid_to_vlan(in_bundle, vid);
596 /* Check other admissibility requirements. */
597 if (in_port && !is_admissible(ctx, in_port, vlan)) {
601 /* Learn source MAC. */
602 if (ctx->xin->may_learn) {
603 update_learning_table(ctx->ofproto, flow, wc, vlan, in_bundle);
606 /* Determine output bundle. */
607 mac = mac_learning_lookup(ctx->ofproto->ml, flow->dl_dst, vlan,
610 if (mac->port.p != in_bundle) {
611 xlate_report(ctx, "forwarding to learned port");
612 output_normal(ctx, mac->port.p, vlan);
614 xlate_report(ctx, "learned port is input port, dropping");
617 struct ofbundle *bundle;
619 xlate_report(ctx, "no learned MAC for destination, flooding");
620 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
621 if (bundle != in_bundle
622 && ofbundle_includes_vlan(bundle, vlan)
624 && !bundle->mirror_out) {
625 output_normal(ctx, bundle, vlan);
628 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
632 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
633 * the number of packets out of UINT32_MAX to sample. The given
634 * cookie is passed back in the callback for each sampled packet.
637 compose_sample_action(const struct ofproto_dpif *ofproto,
638 struct ofpbuf *odp_actions,
639 const struct flow *flow,
640 const uint32_t probability,
641 const union user_action_cookie *cookie,
642 const size_t cookie_size)
644 size_t sample_offset, actions_offset;
647 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
649 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
651 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
652 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, cookie,
655 nl_msg_end_nested(odp_actions, actions_offset);
656 nl_msg_end_nested(odp_actions, sample_offset);
657 return cookie_offset;
661 compose_sflow_cookie(const struct ofproto_dpif *ofproto,
662 ovs_be16 vlan_tci, uint32_t odp_port,
663 unsigned int n_outputs, union user_action_cookie *cookie)
667 cookie->type = USER_ACTION_COOKIE_SFLOW;
668 cookie->sflow.vlan_tci = vlan_tci;
670 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
671 * port information") for the interpretation of cookie->output. */
674 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
675 cookie->sflow.output = 0x40000000 | 256;
679 ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
681 cookie->sflow.output = ifindex;
686 /* 0x80000000 means "multiple output ports. */
687 cookie->sflow.output = 0x80000000 | n_outputs;
692 /* Compose SAMPLE action for sFlow bridge sampling. */
694 compose_sflow_action(const struct ofproto_dpif *ofproto,
695 struct ofpbuf *odp_actions,
696 const struct flow *flow,
699 uint32_t probability;
700 union user_action_cookie cookie;
702 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
706 probability = dpif_sflow_get_probability(ofproto->sflow);
707 compose_sflow_cookie(ofproto, htons(0), odp_port,
708 odp_port == OVSP_NONE ? 0 : 1, &cookie);
710 return compose_sample_action(ofproto, odp_actions, flow, probability,
711 &cookie, sizeof cookie.sflow);
715 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
716 uint32_t obs_domain_id, uint32_t obs_point_id,
717 union user_action_cookie *cookie)
719 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
720 cookie->flow_sample.probability = probability;
721 cookie->flow_sample.collector_set_id = collector_set_id;
722 cookie->flow_sample.obs_domain_id = obs_domain_id;
723 cookie->flow_sample.obs_point_id = obs_point_id;
727 compose_ipfix_cookie(union user_action_cookie *cookie)
729 cookie->type = USER_ACTION_COOKIE_IPFIX;
732 /* Compose SAMPLE action for IPFIX bridge sampling. */
734 compose_ipfix_action(const struct ofproto_dpif *ofproto,
735 struct ofpbuf *odp_actions,
736 const struct flow *flow)
738 uint32_t probability;
739 union user_action_cookie cookie;
741 if (!ofproto->ipfix || flow->in_port == OFPP_NONE) {
745 probability = dpif_ipfix_get_bridge_exporter_probability(ofproto->ipfix);
746 compose_ipfix_cookie(&cookie);
748 compose_sample_action(ofproto, odp_actions, flow, probability,
749 &cookie, sizeof cookie.ipfix);
752 /* SAMPLE action for sFlow must be first action in any given list of
753 * actions. At this point we do not have all information required to
754 * build it. So try to build sample action as complete as possible. */
756 add_sflow_action(struct xlate_ctx *ctx)
758 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
759 &ctx->xout->odp_actions,
760 &ctx->xin->flow, OVSP_NONE);
761 ctx->sflow_odp_port = 0;
762 ctx->sflow_n_outputs = 0;
765 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
766 * of actions, eventually after the SAMPLE action for sFlow. */
768 add_ipfix_action(struct xlate_ctx *ctx)
770 compose_ipfix_action(ctx->ofproto, &ctx->xout->odp_actions,
774 /* Fix SAMPLE action according to data collected while composing ODP actions.
775 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
776 * USERSPACE action's user-cookie which is required for sflow. */
778 fix_sflow_action(struct xlate_ctx *ctx)
780 const struct flow *base = &ctx->base_flow;
781 union user_action_cookie *cookie;
783 if (!ctx->user_cookie_offset) {
787 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
788 sizeof cookie->sflow);
789 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
791 compose_sflow_cookie(ctx->ofproto, base->vlan_tci,
792 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
795 static enum slow_path_reason
796 process_special(struct xlate_ctx *ctx, const struct flow *flow,
797 const struct ofport_dpif *ofport, const struct ofpbuf *packet)
799 struct ofproto_dpif *ofproto = ctx->ofproto;
800 struct flow_wildcards *wc = &ctx->xout->wc;
804 } else if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow, wc)) {
806 cfm_process_heartbeat(ofport->cfm, packet);
809 } else if (ofport->bfd && bfd_should_process_flow(flow, wc)) {
811 bfd_process_packet(ofport->bfd, flow, packet);
814 } else if (ofport->bundle && ofport->bundle->lacp
815 && flow->dl_type == htons(ETH_TYPE_LACP)) {
816 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
818 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
821 } else if (ofproto->stp && stp_should_process_flow(flow, wc)) {
823 stp_process_packet(ofport, packet);
832 compose_output_action__(struct xlate_ctx *ctx, uint16_t ofp_port,
835 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
836 struct flow_wildcards *wc = &ctx->xout->wc;
837 struct flow *flow = &ctx->xin->flow;
838 ovs_be16 flow_vlan_tci;
839 uint32_t flow_skb_mark;
841 uint32_t out_port, odp_port;
844 /* If 'struct flow' gets additional metadata, we'll need to zero it out
845 * before traversing a patch port. */
846 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 20);
849 xlate_report(ctx, "Nonexistent output port");
851 } else if (ofport->up.pp.config & OFPUTIL_PC_NO_FWD) {
852 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
854 } else if (check_stp && !stp_forward_in_state(ofport->stp_state)) {
855 xlate_report(ctx, "STP not in forwarding state, skipping output");
860 struct ofport_dpif *peer = ofport->peer;
861 struct flow old_flow = ctx->xin->flow;
862 enum slow_path_reason special;
864 ctx->ofproto = ofproto_dpif_cast(peer->up.ofproto);
865 flow->in_port = peer->up.ofp_port;
866 flow->metadata = htonll(0);
867 memset(&flow->tunnel, 0, sizeof flow->tunnel);
868 memset(flow->regs, 0, sizeof flow->regs);
870 special = process_special(ctx, &ctx->xin->flow, peer,
873 ctx->xout->slow = special;
874 } else if (may_receive(peer, ctx)) {
875 if (stp_forward_in_state(peer->stp_state)) {
876 xlate_table_action(ctx, flow->in_port, 0, true);
878 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
879 * learning action look at the packet, then drop it. */
880 struct flow old_base_flow = ctx->base_flow;
881 size_t old_size = ctx->xout->odp_actions.size;
882 xlate_table_action(ctx, flow->in_port, 0, true);
883 ctx->base_flow = old_base_flow;
884 ctx->xout->odp_actions.size = old_size;
888 ctx->xin->flow = old_flow;
889 ctx->ofproto = ofproto_dpif_cast(ofport->up.ofproto);
891 if (ctx->xin->resubmit_stats) {
892 netdev_vport_inc_tx(ofport->up.netdev, ctx->xin->resubmit_stats);
893 netdev_vport_inc_rx(peer->up.netdev, ctx->xin->resubmit_stats);
899 flow_vlan_tci = flow->vlan_tci;
900 flow_skb_mark = flow->skb_mark;
901 flow_nw_tos = flow->nw_tos;
903 if (ofproto_dpif_dscp_from_priority(ofport, flow->skb_priority, &dscp)) {
904 wc->masks.nw_tos |= IP_ECN_MASK;
905 flow->nw_tos &= ~IP_DSCP_MASK;
906 flow->nw_tos |= dscp;
909 if (ofport->tnl_port) {
910 /* Save tunnel metadata so that changes made due to
911 * the Logical (tunnel) Port are not visible for any further
912 * matches, while explicit set actions on tunnel metadata are.
914 struct flow_tnl flow_tnl = flow->tunnel;
915 odp_port = tnl_port_send(ofport->tnl_port, flow, &ctx->xout->wc);
916 if (odp_port == OVSP_NONE) {
917 xlate_report(ctx, "Tunneling decided against output");
918 goto out; /* restore flow_nw_tos */
920 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
921 xlate_report(ctx, "Not tunneling to our own address");
922 goto out; /* restore flow_nw_tos */
924 if (ctx->xin->resubmit_stats) {
925 netdev_vport_inc_tx(ofport->up.netdev, ctx->xin->resubmit_stats);
928 commit_odp_tunnel_action(flow, &ctx->base_flow,
929 &ctx->xout->odp_actions);
930 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
932 uint16_t vlandev_port;
934 odp_port = ofport->odp_port;
935 if (!hmap_is_empty(&ctx->ofproto->realdev_vid_map)) {
936 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
938 vlandev_port = vsp_realdev_to_vlandev(ctx->ofproto, ofp_port,
940 if (vlandev_port == ofp_port) {
943 out_port = ofp_port_to_odp_port(ctx->ofproto, vlandev_port);
944 flow->vlan_tci = htons(0);
946 flow->skb_mark &= ~IPSEC_MARK;
949 if (out_port != OVSP_NONE) {
950 commit_odp_actions(flow, &ctx->base_flow,
951 &ctx->xout->odp_actions, &ctx->xout->wc);
952 nl_msg_put_u32(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
955 ctx->sflow_odp_port = odp_port;
956 ctx->sflow_n_outputs++;
957 ctx->xout->nf_output_iface = ofp_port;
962 flow->vlan_tci = flow_vlan_tci;
963 flow->skb_mark = flow_skb_mark;
964 flow->nw_tos = flow_nw_tos;
968 compose_output_action(struct xlate_ctx *ctx, uint16_t ofp_port)
970 compose_output_action__(ctx, ofp_port, true);
974 tag_the_flow(struct xlate_ctx *ctx, struct rule_dpif *rule)
976 struct ofproto_dpif *ofproto = ctx->ofproto;
977 uint8_t table_id = ctx->table_id;
979 if (table_id > 0 && table_id < N_TABLES) {
980 struct table_dpif *table = &ofproto->tables[table_id];
981 if (table->other_table) {
982 ctx->xout->tags |= (rule && rule->tag
984 : rule_calculate_tag(&ctx->xin->flow,
985 &table->other_table->mask,
991 /* Common rule processing in one place to avoid duplicating code. */
992 static struct rule_dpif *
993 ctx_rule_hooks(struct xlate_ctx *ctx, struct rule_dpif *rule,
996 if (ctx->xin->resubmit_hook) {
997 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
999 if (rule == NULL && may_packet_in) {
1001 * check if table configuration flags
1002 * OFPTC_TABLE_MISS_CONTROLLER, default.
1003 * OFPTC_TABLE_MISS_CONTINUE,
1004 * OFPTC_TABLE_MISS_DROP
1005 * When OF1.0, OFPTC_TABLE_MISS_CONTINUE is used. What to do?
1007 rule = rule_dpif_miss_rule(ctx->ofproto, &ctx->xin->flow);
1009 if (rule && ctx->xin->resubmit_stats) {
1010 rule_credit_stats(rule, ctx->xin->resubmit_stats);
1016 xlate_table_action(struct xlate_ctx *ctx,
1017 uint16_t in_port, uint8_t table_id, bool may_packet_in)
1019 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
1020 struct rule_dpif *rule;
1021 uint16_t old_in_port = ctx->xin->flow.in_port;
1022 uint8_t old_table_id = ctx->table_id;
1024 ctx->table_id = table_id;
1026 /* Look up a flow with 'in_port' as the input port. */
1027 ctx->xin->flow.in_port = in_port;
1028 rule = rule_dpif_lookup_in_table(ctx->ofproto, &ctx->xin->flow,
1029 &ctx->xout->wc, table_id);
1031 tag_the_flow(ctx, rule);
1033 /* Restore the original input port. Otherwise OFPP_NORMAL and
1034 * OFPP_IN_PORT will have surprising behavior. */
1035 ctx->xin->flow.in_port = old_in_port;
1037 rule = ctx_rule_hooks(ctx, rule, may_packet_in);
1040 struct rule_dpif *old_rule = ctx->rule;
1044 do_xlate_actions(rule->up.ofpacts, rule->up.ofpacts_len, ctx);
1045 ctx->rule = old_rule;
1049 ctx->table_id = old_table_id;
1051 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
1053 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
1054 MAX_RESUBMIT_RECURSION);
1055 ctx->max_resubmit_trigger = true;
1060 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
1061 const struct ofpact_resubmit *resubmit)
1066 in_port = resubmit->in_port;
1067 if (in_port == OFPP_IN_PORT) {
1068 in_port = ctx->xin->flow.in_port;
1071 table_id = resubmit->table_id;
1072 if (table_id == 255) {
1073 table_id = ctx->table_id;
1076 xlate_table_action(ctx, in_port, table_id, false);
1080 flood_packets(struct xlate_ctx *ctx, bool all)
1082 struct ofport_dpif *ofport;
1084 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
1085 uint16_t ofp_port = ofport->up.ofp_port;
1087 if (ofp_port == ctx->xin->flow.in_port) {
1092 compose_output_action__(ctx, ofp_port, false);
1093 } else if (!(ofport->up.pp.config & OFPUTIL_PC_NO_FLOOD)) {
1094 compose_output_action(ctx, ofp_port);
1098 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1102 execute_controller_action(struct xlate_ctx *ctx, int len,
1103 enum ofp_packet_in_reason reason,
1104 uint16_t controller_id)
1106 struct ofputil_packet_in pin;
1107 struct ofpbuf *packet;
1110 ovs_assert(!ctx->xout->slow || ctx->xout->slow == SLOW_CONTROLLER);
1111 ctx->xout->slow = SLOW_CONTROLLER;
1112 if (!ctx->xin->packet) {
1116 packet = ofpbuf_clone(ctx->xin->packet);
1118 key.skb_priority = 0;
1120 memset(&key.tunnel, 0, sizeof key.tunnel);
1122 commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1123 &ctx->xout->odp_actions, &ctx->xout->wc);
1125 odp_execute_actions(NULL, packet, &key, ctx->xout->odp_actions.data,
1126 ctx->xout->odp_actions.size, NULL, NULL);
1128 pin.packet = packet->data;
1129 pin.packet_len = packet->size;
1130 pin.reason = reason;
1131 pin.controller_id = controller_id;
1132 pin.table_id = ctx->table_id;
1133 pin.cookie = ctx->rule ? ctx->rule->up.flow_cookie : 0;
1136 flow_get_metadata(&ctx->xin->flow, &pin.fmd);
1138 connmgr_send_packet_in(ctx->ofproto->up.connmgr, &pin);
1139 ofpbuf_delete(packet);
1143 compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1145 struct flow_wildcards *wc = &ctx->xout->wc;
1146 struct flow *flow = &ctx->xin->flow;
1148 ovs_assert(eth_type_mpls(eth_type));
1150 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1151 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1152 memset(&wc->masks.mpls_depth, 0xff, sizeof wc->masks.mpls_depth);
1154 if (flow->mpls_depth) {
1155 flow->mpls_lse &= ~htonl(MPLS_BOS_MASK);
1161 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1162 label = htonl(0x2); /* IPV6 Explicit Null. */
1164 label = htonl(0x0); /* IPV4 Explicit Null. */
1166 wc->masks.nw_tos |= IP_DSCP_MASK;
1167 wc->masks.nw_ttl = 0xff;
1168 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
1169 ttl = flow->nw_ttl ? flow->nw_ttl : 0x40;
1170 flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
1171 flow->mpls_depth = 1;
1173 flow->dl_type = eth_type;
1177 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1179 struct flow_wildcards *wc = &ctx->xout->wc;
1180 struct flow *flow = &ctx->xin->flow;
1182 ovs_assert(eth_type_mpls(ctx->xin->flow.dl_type));
1183 ovs_assert(!eth_type_mpls(eth_type));
1185 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1186 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1187 memset(&wc->masks.mpls_depth, 0xff, sizeof wc->masks.mpls_depth);
1189 if (flow->mpls_depth) {
1191 flow->mpls_lse = htonl(0);
1192 if (!flow->mpls_depth) {
1193 flow->dl_type = eth_type;
1199 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
1201 struct flow *flow = &ctx->xin->flow;
1203 if (!is_ip_any(flow)) {
1207 ctx->xout->wc.masks.nw_ttl = 0xff;
1208 if (flow->nw_ttl > 1) {
1214 for (i = 0; i < ids->n_controllers; i++) {
1215 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
1219 /* Stop processing for current table. */
1225 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
1227 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
1231 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl);
1236 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
1238 struct flow *flow = &ctx->xin->flow;
1239 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse);
1240 struct flow_wildcards *wc = &ctx->xout->wc;
1242 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1243 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1245 if (!eth_type_mpls(flow->dl_type)) {
1251 set_mpls_lse_ttl(&flow->mpls_lse, ttl);
1254 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
1256 /* Stop processing for current table. */
1262 xlate_output_action(struct xlate_ctx *ctx,
1263 uint16_t port, uint16_t max_len, bool may_packet_in)
1265 uint16_t prev_nf_output_iface = ctx->xout->nf_output_iface;
1267 ctx->xout->nf_output_iface = NF_OUT_DROP;
1271 compose_output_action(ctx, ctx->xin->flow.in_port);
1274 xlate_table_action(ctx, ctx->xin->flow.in_port, 0, may_packet_in);
1280 flood_packets(ctx, false);
1283 flood_packets(ctx, true);
1285 case OFPP_CONTROLLER:
1286 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
1292 if (port != ctx->xin->flow.in_port) {
1293 compose_output_action(ctx, port);
1295 xlate_report(ctx, "skipping output to input port");
1300 if (prev_nf_output_iface == NF_OUT_FLOOD) {
1301 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1302 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
1303 ctx->xout->nf_output_iface = prev_nf_output_iface;
1304 } else if (prev_nf_output_iface != NF_OUT_DROP &&
1305 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
1306 ctx->xout->nf_output_iface = NF_OUT_MULTI;
1311 xlate_output_reg_action(struct xlate_ctx *ctx,
1312 const struct ofpact_output_reg *or)
1314 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
1315 if (port <= UINT16_MAX) {
1316 union mf_subvalue value;
1318 memset(&value, 0xff, sizeof value);
1319 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
1320 xlate_output_action(ctx, port, or->max_len, false);
1325 xlate_enqueue_action(struct xlate_ctx *ctx,
1326 const struct ofpact_enqueue *enqueue)
1328 uint16_t ofp_port = enqueue->port;
1329 uint32_t queue_id = enqueue->queue;
1330 uint32_t flow_priority, priority;
1333 /* Translate queue to priority. */
1334 error = ofproto_dpif_queue_to_priority(ctx->ofproto, queue_id, &priority);
1336 /* Fall back to ordinary output action. */
1337 xlate_output_action(ctx, enqueue->port, 0, false);
1341 /* Check output port. */
1342 if (ofp_port == OFPP_IN_PORT) {
1343 ofp_port = ctx->xin->flow.in_port;
1344 } else if (ofp_port == ctx->xin->flow.in_port) {
1348 /* Add datapath actions. */
1349 flow_priority = ctx->xin->flow.skb_priority;
1350 ctx->xin->flow.skb_priority = priority;
1351 compose_output_action(ctx, ofp_port);
1352 ctx->xin->flow.skb_priority = flow_priority;
1354 /* Update NetFlow output port. */
1355 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
1356 ctx->xout->nf_output_iface = ofp_port;
1357 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
1358 ctx->xout->nf_output_iface = NF_OUT_MULTI;
1363 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
1365 uint32_t skb_priority;
1367 if (!ofproto_dpif_queue_to_priority(ctx->ofproto, queue_id,
1369 ctx->xin->flow.skb_priority = skb_priority;
1371 /* Couldn't translate queue to a priority. Nothing to do. A warning
1372 * has already been logged. */
1377 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
1379 struct ofproto_dpif *ofproto = ofproto_;
1380 struct ofport_dpif *port;
1390 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
1393 port = get_ofp_port(ofproto, ofp_port);
1394 return port ? port->may_enable : false;
1399 xlate_bundle_action(struct xlate_ctx *ctx,
1400 const struct ofpact_bundle *bundle)
1404 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
1405 slave_enabled_cb, ctx->ofproto);
1406 if (bundle->dst.field) {
1407 nxm_reg_load(&bundle->dst, port, &ctx->xin->flow);
1409 xlate_output_action(ctx, port, 0, false);
1414 xlate_learn_action(struct xlate_ctx *ctx,
1415 const struct ofpact_learn *learn)
1417 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
1418 struct ofputil_flow_mod fm;
1419 uint64_t ofpacts_stub[1024 / 8];
1420 struct ofpbuf ofpacts;
1423 ctx->xout->has_learn = true;
1425 learn_mask(learn, &ctx->xout->wc);
1427 if (!ctx->xin->may_learn) {
1431 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1432 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
1434 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
1435 if (error && !VLOG_DROP_WARN(&rl)) {
1436 VLOG_WARN("learning action failed to modify flow table (%s)",
1437 ofperr_get_name(error));
1440 ofpbuf_uninit(&ofpacts);
1443 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
1444 * means "infinite". */
1446 reduce_timeout(uint16_t max, uint16_t *timeout)
1448 if (max && (!*timeout || *timeout > max)) {
1454 xlate_fin_timeout(struct xlate_ctx *ctx,
1455 const struct ofpact_fin_timeout *oft)
1457 if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
1458 struct rule_dpif *rule = ctx->rule;
1460 reduce_timeout(oft->fin_idle_timeout, &rule->up.idle_timeout);
1461 reduce_timeout(oft->fin_hard_timeout, &rule->up.hard_timeout);
1466 xlate_sample_action(struct xlate_ctx *ctx,
1467 const struct ofpact_sample *os)
1469 union user_action_cookie cookie;
1470 /* Scale the probability from 16-bit to 32-bit while representing
1471 * the same percentage. */
1472 uint32_t probability = (os->probability << 16) | os->probability;
1474 commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1475 &ctx->xout->odp_actions, &ctx->xout->wc);
1477 compose_flow_sample_cookie(os->probability, os->collector_set_id,
1478 os->obs_domain_id, os->obs_point_id, &cookie);
1479 compose_sample_action(ctx->ofproto, &ctx->xout->odp_actions, &ctx->xin->flow,
1480 probability, &cookie, sizeof cookie.flow_sample);
1484 may_receive(const struct ofport_dpif *port, struct xlate_ctx *ctx)
1486 if (port->up.pp.config & (eth_addr_equals(ctx->xin->flow.dl_dst,
1488 ? OFPUTIL_PC_NO_RECV_STP
1489 : OFPUTIL_PC_NO_RECV)) {
1493 /* Only drop packets here if both forwarding and learning are
1494 * disabled. If just learning is enabled, we need to have
1495 * OFPP_NORMAL and the learning action have a look at the packet
1496 * before we can drop it. */
1497 if (!stp_forward_in_state(port->stp_state)
1498 && !stp_learn_in_state(port->stp_state)) {
1506 tunnel_ecn_ok(struct xlate_ctx *ctx)
1508 if (is_ip_any(&ctx->base_flow)
1509 && (ctx->xin->flow.tunnel.ip_tos & IP_ECN_MASK) == IP_ECN_CE) {
1510 if ((ctx->base_flow.nw_tos & IP_ECN_MASK) == IP_ECN_NOT_ECT) {
1511 VLOG_WARN_RL(&rl, "dropping tunnel packet marked ECN CE"
1512 " but is not ECN capable");
1515 /* Set the ECN CE value in the tunneled packet. */
1516 ctx->xin->flow.nw_tos |= IP_ECN_CE;
1524 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
1525 struct xlate_ctx *ctx)
1527 struct flow_wildcards *wc = &ctx->xout->wc;
1528 struct flow *flow = &ctx->xin->flow;
1529 bool was_evictable = true;
1530 const struct ofpact *a;
1533 /* Don't let the rule we're working on get evicted underneath us. */
1534 was_evictable = ctx->rule->up.evictable;
1535 ctx->rule->up.evictable = false;
1538 do_xlate_actions_again:
1539 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1540 struct ofpact_controller *controller;
1541 const struct ofpact_metadata *metadata;
1549 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
1550 ofpact_get_OUTPUT(a)->max_len, true);
1553 case OFPACT_CONTROLLER:
1554 controller = ofpact_get_CONTROLLER(a);
1555 execute_controller_action(ctx, controller->max_len,
1557 controller->controller_id);
1560 case OFPACT_ENQUEUE:
1561 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
1564 case OFPACT_SET_VLAN_VID:
1565 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
1566 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
1570 case OFPACT_SET_VLAN_PCP:
1571 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
1573 htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp << VLAN_PCP_SHIFT)
1577 case OFPACT_STRIP_VLAN:
1578 flow->vlan_tci = htons(0);
1581 case OFPACT_PUSH_VLAN:
1582 /* XXX 802.1AD(QinQ) */
1583 flow->vlan_tci = htons(VLAN_CFI);
1586 case OFPACT_SET_ETH_SRC:
1587 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1590 case OFPACT_SET_ETH_DST:
1591 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1594 case OFPACT_SET_IPV4_SRC:
1595 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1596 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1597 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1601 case OFPACT_SET_IPV4_DST:
1602 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1603 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1604 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
1608 case OFPACT_SET_IPV4_DSCP:
1609 /* OpenFlow 1.0 only supports IPv4. */
1610 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1611 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1612 flow->nw_tos &= ~IP_DSCP_MASK;
1613 flow->nw_tos |= ofpact_get_SET_IPV4_DSCP(a)->dscp;
1617 case OFPACT_SET_L4_SRC_PORT:
1618 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1619 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1620 if (is_ip_any(flow)) {
1621 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1625 case OFPACT_SET_L4_DST_PORT:
1626 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1627 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1628 if (is_ip_any(flow)) {
1629 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1633 case OFPACT_RESUBMIT:
1634 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
1637 case OFPACT_SET_TUNNEL:
1638 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
1641 case OFPACT_SET_QUEUE:
1642 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
1645 case OFPACT_POP_QUEUE:
1646 flow->skb_priority = ctx->orig_skb_priority;
1649 case OFPACT_REG_MOVE:
1650 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
1653 case OFPACT_REG_LOAD:
1654 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow);
1657 case OFPACT_STACK_PUSH:
1658 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
1662 case OFPACT_STACK_POP:
1663 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, &ctx->stack);
1666 case OFPACT_PUSH_MPLS:
1667 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a)->ethertype);
1670 case OFPACT_POP_MPLS:
1671 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
1674 case OFPACT_SET_MPLS_TTL:
1675 if (compose_set_mpls_ttl_action(ctx,
1676 ofpact_get_SET_MPLS_TTL(a)->ttl)) {
1681 case OFPACT_DEC_MPLS_TTL:
1682 if (compose_dec_mpls_ttl_action(ctx)) {
1687 case OFPACT_DEC_TTL:
1688 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1689 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
1695 /* Nothing to do. */
1698 case OFPACT_MULTIPATH:
1699 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
1703 ctx->ofproto->has_bundle_action = true;
1704 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
1707 case OFPACT_OUTPUT_REG:
1708 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
1712 xlate_learn_action(ctx, ofpact_get_LEARN(a));
1719 case OFPACT_FIN_TIMEOUT:
1720 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1721 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1722 ctx->xout->has_fin_timeout = true;
1723 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
1726 case OFPACT_CLEAR_ACTIONS:
1728 * Nothing to do because writa-actions is not supported for now.
1729 * When writa-actions is supported, clear-actions also must
1730 * be supported at the same time.
1734 case OFPACT_WRITE_METADATA:
1735 metadata = ofpact_get_WRITE_METADATA(a);
1736 flow->metadata &= ~metadata->mask;
1737 flow->metadata |= metadata->metadata & metadata->mask;
1740 case OFPACT_GOTO_TABLE: {
1741 /* It is assumed that goto-table is the last action. */
1742 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
1743 struct rule_dpif *rule;
1745 ovs_assert(ctx->table_id < ogt->table_id);
1747 ctx->table_id = ogt->table_id;
1749 /* Look up a flow from the new table. */
1750 rule = rule_dpif_lookup_in_table(ctx->ofproto, flow, wc,
1753 tag_the_flow(ctx, rule);
1755 rule = ctx_rule_hooks(ctx, rule, true);
1759 ctx->rule->up.evictable = was_evictable;
1762 was_evictable = rule->up.evictable;
1763 rule->up.evictable = false;
1765 /* Tail recursion removal. */
1766 ofpacts = rule->up.ofpacts;
1767 ofpacts_len = rule->up.ofpacts_len;
1768 goto do_xlate_actions_again;
1774 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
1781 ctx->rule->up.evictable = was_evictable;
1786 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
1787 const struct flow *flow, struct rule_dpif *rule,
1788 uint8_t tcp_flags, const struct ofpbuf *packet)
1790 xin->ofproto = ofproto;
1792 xin->packet = packet;
1793 xin->may_learn = packet != NULL;
1795 xin->ofpacts = NULL;
1796 xin->ofpacts_len = 0;
1797 xin->tcp_flags = tcp_flags;
1798 xin->resubmit_hook = NULL;
1799 xin->report_hook = NULL;
1800 xin->resubmit_stats = NULL;
1804 xlate_out_uninit(struct xlate_out *xout)
1807 ofpbuf_uninit(&xout->odp_actions);
1811 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
1812 * into datapath actions, using 'ctx', and discards the datapath actions. */
1814 xlate_actions_for_side_effects(struct xlate_in *xin)
1816 struct xlate_out xout;
1818 xlate_actions(xin, &xout);
1819 xlate_out_uninit(&xout);
1823 xlate_report(struct xlate_ctx *ctx, const char *s)
1825 if (ctx->xin->report_hook) {
1826 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
1831 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
1834 dst->tags = src->tags;
1835 dst->slow = src->slow;
1836 dst->has_learn = src->has_learn;
1837 dst->has_normal = src->has_normal;
1838 dst->has_fin_timeout = src->has_fin_timeout;
1839 dst->nf_output_iface = src->nf_output_iface;
1840 dst->mirrors = src->mirrors;
1842 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
1843 sizeof dst->odp_actions_stub);
1844 ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
1845 src->odp_actions.size);
1849 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
1850 * into datapath actions in 'odp_actions', using 'ctx'. */
1852 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
1854 /* Normally false. Set to true if we ever hit MAX_RESUBMIT_RECURSION, so
1855 * that in the future we always keep a copy of the original flow for
1856 * tracing purposes. */
1857 static bool hit_resubmit_limit;
1859 struct flow_wildcards *wc = &xout->wc;
1860 struct flow *flow = &xin->flow;
1862 enum slow_path_reason special;
1863 const struct ofpact *ofpacts;
1864 struct ofport_dpif *in_port;
1865 struct flow orig_flow;
1866 struct xlate_ctx ctx;
1869 COVERAGE_INC(ofproto_dpif_xlate);
1871 /* Flow initialization rules:
1872 * - 'base_flow' must match the kernel's view of the packet at the
1873 * time that action processing starts. 'flow' represents any
1874 * transformations we wish to make through actions.
1875 * - By default 'base_flow' and 'flow' are the same since the input
1876 * packet matches the output before any actions are applied.
1877 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
1878 * of the received packet as seen by the kernel. If we later output
1879 * to another device without any modifications this will cause us to
1880 * insert a new tag since the original one was stripped off by the
1882 * - Tunnel metadata as received is retained in 'flow'. This allows
1883 * tunnel metadata matching also in later tables.
1884 * Since a kernel action for setting the tunnel metadata will only be
1885 * generated with actual tunnel output, changing the tunnel metadata
1886 * values in 'flow' (such as tun_id) will only have effect with a later
1887 * tunnel output action.
1888 * - Tunnel 'base_flow' is completely cleared since that is what the
1889 * kernel does. If we wish to maintain the original values an action
1890 * needs to be generated. */
1895 ctx.ofproto = xin->ofproto;
1896 ctx.rule = xin->rule;
1898 ctx.base_flow = *flow;
1899 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
1900 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
1902 flow_wildcards_init_catchall(wc);
1903 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
1904 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
1905 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
1907 if (tnl_port_should_receive(&ctx.xin->flow)) {
1908 memset(&wc->masks.tunnel, 0xff, sizeof wc->masks.tunnel);
1910 if (xin->ofproto->netflow) {
1911 netflow_mask_wc(wc);
1916 ctx.xout->has_learn = false;
1917 ctx.xout->has_normal = false;
1918 ctx.xout->has_fin_timeout = false;
1919 ctx.xout->nf_output_iface = NF_OUT_DROP;
1920 ctx.xout->mirrors = 0;
1922 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
1923 sizeof ctx.xout->odp_actions_stub);
1924 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
1927 ctx.max_resubmit_trigger = false;
1928 ctx.orig_skb_priority = flow->skb_priority;
1933 ofpacts = xin->ofpacts;
1934 ofpacts_len = xin->ofpacts_len;
1935 } else if (xin->rule) {
1936 ofpacts = xin->rule->up.ofpacts;
1937 ofpacts_len = xin->rule->up.ofpacts_len;
1942 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
1944 if (ctx.ofproto->has_mirrors || hit_resubmit_limit) {
1945 /* Do this conditionally because the copy is expensive enough that it
1946 * shows up in profiles. */
1950 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
1951 switch (ctx.ofproto->up.frag_handling) {
1952 case OFPC_FRAG_NORMAL:
1953 /* We must pretend that transport ports are unavailable. */
1954 flow->tp_src = ctx.base_flow.tp_src = htons(0);
1955 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
1958 case OFPC_FRAG_DROP:
1961 case OFPC_FRAG_REASM:
1964 case OFPC_FRAG_NX_MATCH:
1965 /* Nothing to do. */
1968 case OFPC_INVALID_TTL_TO_CONTROLLER:
1973 in_port = get_ofp_port(ctx.ofproto, flow->in_port);
1974 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
1976 ctx.xout->slow = special;
1978 static struct vlog_rate_limit trace_rl = VLOG_RATE_LIMIT_INIT(1, 1);
1979 size_t sample_actions_len;
1980 uint32_t local_odp_port;
1983 != vsp_realdev_to_vlandev(ctx.ofproto, flow->in_port,
1985 ctx.base_flow.vlan_tci = 0;
1988 add_sflow_action(&ctx);
1989 add_ipfix_action(&ctx);
1990 sample_actions_len = ctx.xout->odp_actions.size;
1992 if (tunnel_ecn_ok(&ctx) && (!in_port || may_receive(in_port, &ctx))) {
1993 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
1995 /* We've let OFPP_NORMAL and the learning action look at the
1996 * packet, so drop it now if forwarding is disabled. */
1997 if (in_port && !stp_forward_in_state(in_port->stp_state)) {
1998 ctx.xout->odp_actions.size = sample_actions_len;
2002 if (ctx.max_resubmit_trigger && !ctx.xin->resubmit_hook) {
2003 if (!hit_resubmit_limit) {
2004 /* We didn't record the original flow. Make sure we do from
2006 hit_resubmit_limit = true;
2007 } else if (!VLOG_DROP_ERR(&trace_rl)) {
2008 struct ds ds = DS_EMPTY_INITIALIZER;
2010 ofproto_trace(ctx.ofproto, &orig_flow, ctx.xin->packet, &ds);
2011 VLOG_ERR("Trace triggered by excessive resubmit "
2012 "recursion:\n%s", ds_cstr(&ds));
2017 local_odp_port = ofp_port_to_odp_port(ctx.ofproto, OFPP_LOCAL);
2018 if (!connmgr_must_output_local(ctx.ofproto->up.connmgr, flow,
2020 ctx.xout->odp_actions.data,
2021 ctx.xout->odp_actions.size)) {
2022 compose_output_action(&ctx, OFPP_LOCAL);
2024 if (ctx.ofproto->has_mirrors) {
2025 add_mirror_actions(&ctx, &orig_flow);
2027 fix_sflow_action(&ctx);
2030 ofpbuf_uninit(&ctx.stack);
2032 /* Clear the metadata and register wildcard masks, because we won't
2033 * use non-header fields as part of the cache. */
2034 memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
2035 memset(&wc->masks.regs, 0, sizeof wc->masks.regs);