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"
32 #include "mac-learning.h"
33 #include "meta-flow.h"
34 #include "multipath.h"
35 #include "netdev-vport.h"
38 #include "odp-execute.h"
39 #include "ofp-actions.h"
40 #include "ofproto/ofproto-dpif-ipfix.h"
41 #include "ofproto/ofproto-dpif-sflow.h"
42 #include "ofproto/ofproto-dpif.h"
46 COVERAGE_DEFINE(ofproto_dpif_xlate);
48 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
50 /* Maximum depth of flow table recursion (due to resubmit actions) in a
51 * flow translation. */
52 #define MAX_RESUBMIT_RECURSION 64
56 struct xlate_out *xout;
58 struct ofproto_dpif *ofproto;
60 /* Flow at the last commit. */
61 struct flow base_flow;
63 /* Tunnel IP destination address as received. This is stored separately
64 * as the base_flow.tunnel is cleared on init to reflect the datapath
65 * behavior. Used to make sure not to send tunneled output to ourselves,
66 * which might lead to an infinite loop. This could happen easily
67 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
68 * actually set the tun_dst field. */
69 ovs_be32 orig_tunnel_ip_dst;
71 /* Stack for the push and pop actions. Each stack element is of type
72 * "union mf_subvalue". */
73 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
76 /* The rule that we are currently translating, or NULL. */
77 struct rule_dpif *rule;
79 int recurse; /* Recursion level, via xlate_table_action. */
80 bool max_resubmit_trigger; /* Recursed too deeply during translation. */
81 uint32_t orig_skb_priority; /* Priority when packet arrived. */
82 uint8_t table_id; /* OpenFlow table ID where flow was found. */
83 uint32_t sflow_n_outputs; /* Number of output ports. */
84 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
85 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
86 bool exit; /* No further actions should be processed. */
89 /* A controller may use OFPP_NONE as the ingress port to indicate that
90 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
91 * when an input bundle is needed for validation (e.g., mirroring or
92 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
93 * any 'port' structs, so care must be taken when dealing with it. */
94 static struct ofbundle ofpp_none_bundle = {
96 .vlan_mode = PORT_VLAN_TRUNK
99 static bool may_receive(const struct ofport_dpif *, struct xlate_ctx *);
100 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
102 static void xlate_normal(struct xlate_ctx *);
103 static void xlate_report(struct xlate_ctx *, const char *);
104 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
105 uint8_t table_id, bool may_packet_in);
106 static bool input_vid_is_valid(uint16_t vid, struct ofbundle *, bool warn);
107 static uint16_t input_vid_to_vlan(const struct ofbundle *, uint16_t vid);
108 static void output_normal(struct xlate_ctx *, const struct ofbundle *,
110 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
112 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
115 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
117 return (bundle->vlan_mode != PORT_VLAN_ACCESS
118 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
122 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
124 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
128 vlan_is_mirrored(const struct ofmirror *m, int vlan)
130 return !m->vlans || bitmap_is_set(m->vlans, vlan);
133 static struct ofbundle *
134 lookup_input_bundle(const struct ofproto_dpif *ofproto, ofp_port_t in_port,
135 bool warn, struct ofport_dpif **in_ofportp)
137 struct ofport_dpif *ofport;
139 /* Find the port and bundle for the received packet. */
140 ofport = get_ofp_port(ofproto, in_port);
142 *in_ofportp = ofport;
144 if (ofport && ofport->bundle) {
145 return ofport->bundle;
148 /* Special-case OFPP_NONE, which a controller may use as the ingress
149 * port for traffic that it is sourcing. */
150 if (in_port == OFPP_NONE) {
151 return &ofpp_none_bundle;
154 /* Odd. A few possible reasons here:
156 * - We deleted a port but there are still a few packets queued up
159 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
160 * we don't know about.
162 * - The ofproto client didn't configure the port as part of a bundle.
163 * This is particularly likely to happen if a packet was received on the
164 * port after it was created, but before the client had a chance to
165 * configure its bundle.
168 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
170 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
171 "port %"PRIu16, ofproto->up.name, in_port);
177 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
179 struct ofproto_dpif *ofproto = ctx->ofproto;
180 mirror_mask_t mirrors;
181 struct ofbundle *in_bundle;
184 const struct nlattr *a;
187 in_bundle = lookup_input_bundle(ctx->ofproto, orig_flow->in_port.ofp_port,
188 ctx->xin->packet != NULL, NULL);
192 mirrors = in_bundle->src_mirrors;
194 /* Drop frames on bundles reserved for mirroring. */
195 if (in_bundle->mirror_out) {
196 if (ctx->xin->packet != NULL) {
197 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
198 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
199 "%s, which is reserved exclusively for mirroring",
200 ctx->ofproto->up.name, in_bundle->name);
206 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
207 if (!input_vid_is_valid(vid, in_bundle, ctx->xin->packet != NULL)) {
210 vlan = input_vid_to_vlan(in_bundle, vid);
212 /* Look at the output ports to check for destination selections. */
214 NL_ATTR_FOR_EACH (a, left, ctx->xout->odp_actions.data,
215 ctx->xout->odp_actions.size) {
216 enum ovs_action_attr type = nl_attr_type(a);
217 struct ofport_dpif *ofport;
219 if (type != OVS_ACTION_ATTR_OUTPUT) {
223 ofport = get_odp_port(ofproto, nl_attr_get_odp_port(a));
224 if (ofport && ofport->bundle) {
225 mirrors |= ofport->bundle->dst_mirrors;
233 /* Restore the original packet before adding the mirror actions. */
234 ctx->xin->flow = *orig_flow;
239 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
242 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
245 if (!vlan_is_mirrored(m, vlan)) {
246 mirrors = zero_rightmost_1bit(mirrors);
250 mirrors &= ~m->dup_mirrors;
251 ctx->xout->mirrors |= m->dup_mirrors;
253 output_normal(ctx, m->out, vlan);
254 } else if (vlan != m->out_vlan
255 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
256 struct ofbundle *bundle;
258 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
259 if (ofbundle_includes_vlan(bundle, m->out_vlan)
260 && !bundle->mirror_out) {
261 output_normal(ctx, bundle, m->out_vlan);
268 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
269 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
270 * the bundle on which the packet was received, returns the VLAN to which the
273 * Both 'vid' and the return value are in the range 0...4095. */
275 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
277 switch (in_bundle->vlan_mode) {
278 case PORT_VLAN_ACCESS:
279 return in_bundle->vlan;
282 case PORT_VLAN_TRUNK:
285 case PORT_VLAN_NATIVE_UNTAGGED:
286 case PORT_VLAN_NATIVE_TAGGED:
287 return vid ? vid : in_bundle->vlan;
294 /* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'.
295 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
298 * 'vid' should be the VID obtained from the 802.1Q header that was received as
299 * part of a packet (specify 0 if there was no 802.1Q header), in the range
302 input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn)
304 /* Allow any VID on the OFPP_NONE port. */
305 if (in_bundle == &ofpp_none_bundle) {
309 switch (in_bundle->vlan_mode) {
310 case PORT_VLAN_ACCESS:
313 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
314 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
315 "packet received on port %s configured as VLAN "
316 "%"PRIu16" access port",
317 in_bundle->ofproto->up.name, vid,
318 in_bundle->name, in_bundle->vlan);
324 case PORT_VLAN_NATIVE_UNTAGGED:
325 case PORT_VLAN_NATIVE_TAGGED:
327 /* Port must always carry its native VLAN. */
331 case PORT_VLAN_TRUNK:
332 if (!ofbundle_includes_vlan(in_bundle, vid)) {
334 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
335 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet "
336 "received on port %s not configured for trunking "
338 in_bundle->ofproto->up.name, vid,
339 in_bundle->name, vid);
351 /* Given 'vlan', the VLAN that a packet belongs to, and
352 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
353 * that should be included in the 802.1Q header. (If the return value is 0,
354 * then the 802.1Q header should only be included in the packet if there is a
357 * Both 'vlan' and the return value are in the range 0...4095. */
359 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
361 switch (out_bundle->vlan_mode) {
362 case PORT_VLAN_ACCESS:
365 case PORT_VLAN_TRUNK:
366 case PORT_VLAN_NATIVE_TAGGED:
369 case PORT_VLAN_NATIVE_UNTAGGED:
370 return vlan == out_bundle->vlan ? 0 : vlan;
378 output_normal(struct xlate_ctx *ctx, const struct ofbundle *out_bundle,
381 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
382 struct ofport_dpif *port;
384 ovs_be16 tci, old_tci;
386 vid = output_vlan_to_vid(out_bundle, vlan);
387 if (!out_bundle->bond) {
388 port = ofbundle_get_a_port(out_bundle);
390 port = bond_choose_output_slave(out_bundle->bond, &ctx->xin->flow,
391 &ctx->xout->wc, vid, &ctx->xout->tags);
393 /* No slaves enabled, so drop packet. */
400 if (tci || out_bundle->use_priority_tags) {
401 tci |= *flow_tci & htons(VLAN_PCP_MASK);
403 tci |= htons(VLAN_CFI);
408 compose_output_action(ctx, port->up.ofp_port);
412 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
413 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
414 * indicate this; newer upstream kernels use gratuitous ARP requests. */
416 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
418 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
422 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
423 if (!eth_addr_is_broadcast(flow->dl_dst)) {
427 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
428 if (flow->nw_proto == ARP_OP_REPLY) {
430 } else if (flow->nw_proto == ARP_OP_REQUEST) {
431 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
432 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
434 return flow->nw_src == flow->nw_dst;
441 update_learning_table(struct ofproto_dpif *ofproto,
442 const struct flow *flow, struct flow_wildcards *wc,
443 int vlan, struct ofbundle *in_bundle)
445 struct mac_entry *mac;
447 /* Don't learn the OFPP_NONE port. */
448 if (in_bundle == &ofpp_none_bundle) {
452 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
456 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
457 if (is_gratuitous_arp(flow, wc)) {
458 /* We don't want to learn from gratuitous ARP packets that are
459 * reflected back over bond slaves so we lock the learning table. */
460 if (!in_bundle->bond) {
461 mac_entry_set_grat_arp_lock(mac);
462 } else if (mac_entry_is_grat_arp_locked(mac)) {
467 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
468 /* The log messages here could actually be useful in debugging,
469 * so keep the rate limit relatively high. */
470 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
471 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
472 "on port %s in VLAN %d",
473 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
474 in_bundle->name, vlan);
476 mac->port.p = in_bundle;
477 mac_learning_changed(ofproto->ml, mac);
481 /* Determines whether packets in 'flow' within 'ofproto' should be forwarded or
482 * dropped. Returns true if they may be forwarded, false if they should be
485 * 'in_port' must be the ofport_dpif that corresponds to flow->in_port.
486 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
488 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
489 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
490 * checked by input_vid_is_valid().
492 * May also add tags to '*tags', although the current implementation only does
493 * so in one special case.
496 is_admissible(struct xlate_ctx *ctx, struct ofport_dpif *in_port,
499 struct ofproto_dpif *ofproto = ctx->ofproto;
500 struct flow *flow = &ctx->xin->flow;
501 struct ofbundle *in_bundle = in_port->bundle;
503 /* Drop frames for reserved multicast addresses
504 * only if forward_bpdu option is absent. */
505 if (!ofproto->up.forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
506 xlate_report(ctx, "packet has reserved destination MAC, dropping");
510 if (in_bundle->bond) {
511 struct mac_entry *mac;
513 switch (bond_check_admissibility(in_bundle->bond, in_port,
514 flow->dl_dst, &ctx->xout->tags)) {
519 xlate_report(ctx, "bonding refused admissibility, dropping");
522 case BV_DROP_IF_MOVED:
523 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
524 if (mac && mac->port.p != in_bundle &&
525 (!is_gratuitous_arp(flow, &ctx->xout->wc)
526 || mac_entry_is_grat_arp_locked(mac))) {
527 xlate_report(ctx, "SLB bond thinks this packet looped back, "
539 xlate_normal(struct xlate_ctx *ctx)
541 struct flow_wildcards *wc = &ctx->xout->wc;
542 struct flow *flow = &ctx->xin->flow;
543 struct ofport_dpif *in_port;
544 struct ofbundle *in_bundle;
545 struct mac_entry *mac;
549 ctx->xout->has_normal = true;
551 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
552 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
553 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
555 in_bundle = lookup_input_bundle(ctx->ofproto, flow->in_port.ofp_port,
556 ctx->xin->packet != NULL, &in_port);
558 xlate_report(ctx, "no input bundle, dropping");
562 /* Drop malformed frames. */
563 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
564 !(flow->vlan_tci & htons(VLAN_CFI))) {
565 if (ctx->xin->packet != NULL) {
566 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
567 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
568 "VLAN tag received on port %s",
569 ctx->ofproto->up.name, in_bundle->name);
571 xlate_report(ctx, "partial VLAN tag, dropping");
575 /* Drop frames on bundles reserved for mirroring. */
576 if (in_bundle->mirror_out) {
577 if (ctx->xin->packet != NULL) {
578 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
579 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
580 "%s, which is reserved exclusively for mirroring",
581 ctx->ofproto->up.name, in_bundle->name);
583 xlate_report(ctx, "input port is mirror output port, dropping");
588 vid = vlan_tci_to_vid(flow->vlan_tci);
589 if (!input_vid_is_valid(vid, in_bundle, ctx->xin->packet != NULL)) {
590 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
593 vlan = input_vid_to_vlan(in_bundle, vid);
595 /* Check other admissibility requirements. */
596 if (in_port && !is_admissible(ctx, in_port, vlan)) {
600 /* Learn source MAC. */
601 if (ctx->xin->may_learn) {
602 update_learning_table(ctx->ofproto, flow, wc, vlan, in_bundle);
605 /* Determine output bundle. */
606 mac = mac_learning_lookup(ctx->ofproto->ml, flow->dl_dst, vlan,
609 if (mac->port.p != in_bundle) {
610 xlate_report(ctx, "forwarding to learned port");
611 output_normal(ctx, mac->port.p, vlan);
613 xlate_report(ctx, "learned port is input port, dropping");
616 struct ofbundle *bundle;
618 xlate_report(ctx, "no learned MAC for destination, flooding");
619 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
620 if (bundle != in_bundle
621 && ofbundle_includes_vlan(bundle, vlan)
623 && !bundle->mirror_out) {
624 output_normal(ctx, bundle, vlan);
627 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
631 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
632 * the number of packets out of UINT32_MAX to sample. The given
633 * cookie is passed back in the callback for each sampled packet.
636 compose_sample_action(const struct ofproto_dpif *ofproto,
637 struct ofpbuf *odp_actions,
638 const struct flow *flow,
639 const uint32_t probability,
640 const union user_action_cookie *cookie,
641 const size_t cookie_size)
643 size_t sample_offset, actions_offset;
646 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
648 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
650 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
651 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, cookie,
654 nl_msg_end_nested(odp_actions, actions_offset);
655 nl_msg_end_nested(odp_actions, sample_offset);
656 return cookie_offset;
660 compose_sflow_cookie(const struct ofproto_dpif *ofproto,
661 ovs_be16 vlan_tci, odp_port_t odp_port,
662 unsigned int n_outputs, union user_action_cookie *cookie)
666 cookie->type = USER_ACTION_COOKIE_SFLOW;
667 cookie->sflow.vlan_tci = vlan_tci;
669 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
670 * port information") for the interpretation of cookie->output. */
673 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
674 cookie->sflow.output = 0x40000000 | 256;
678 ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
680 cookie->sflow.output = ifindex;
685 /* 0x80000000 means "multiple output ports. */
686 cookie->sflow.output = 0x80000000 | n_outputs;
691 /* Compose SAMPLE action for sFlow bridge sampling. */
693 compose_sflow_action(const struct ofproto_dpif *ofproto,
694 struct ofpbuf *odp_actions,
695 const struct flow *flow,
698 uint32_t probability;
699 union user_action_cookie cookie;
701 if (!ofproto->sflow || flow->in_port.ofp_port == OFPP_NONE) {
705 probability = dpif_sflow_get_probability(ofproto->sflow);
706 compose_sflow_cookie(ofproto, htons(0), odp_port,
707 odp_port == ODPP_NONE ? 0 : 1, &cookie);
709 return compose_sample_action(ofproto, odp_actions, flow, probability,
710 &cookie, sizeof cookie.sflow);
714 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
715 uint32_t obs_domain_id, uint32_t obs_point_id,
716 union user_action_cookie *cookie)
718 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
719 cookie->flow_sample.probability = probability;
720 cookie->flow_sample.collector_set_id = collector_set_id;
721 cookie->flow_sample.obs_domain_id = obs_domain_id;
722 cookie->flow_sample.obs_point_id = obs_point_id;
726 compose_ipfix_cookie(union user_action_cookie *cookie)
728 cookie->type = USER_ACTION_COOKIE_IPFIX;
731 /* Compose SAMPLE action for IPFIX bridge sampling. */
733 compose_ipfix_action(const struct ofproto_dpif *ofproto,
734 struct ofpbuf *odp_actions,
735 const struct flow *flow)
737 uint32_t probability;
738 union user_action_cookie cookie;
740 if (!ofproto->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
744 probability = dpif_ipfix_get_bridge_exporter_probability(ofproto->ipfix);
745 compose_ipfix_cookie(&cookie);
747 compose_sample_action(ofproto, odp_actions, flow, probability,
748 &cookie, sizeof cookie.ipfix);
751 /* SAMPLE action for sFlow must be first action in any given list of
752 * actions. At this point we do not have all information required to
753 * build it. So try to build sample action as complete as possible. */
755 add_sflow_action(struct xlate_ctx *ctx)
757 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
758 &ctx->xout->odp_actions,
759 &ctx->xin->flow, ODPP_NONE);
760 ctx->sflow_odp_port = 0;
761 ctx->sflow_n_outputs = 0;
764 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
765 * of actions, eventually after the SAMPLE action for sFlow. */
767 add_ipfix_action(struct xlate_ctx *ctx)
769 compose_ipfix_action(ctx->ofproto, &ctx->xout->odp_actions,
773 /* Fix SAMPLE action according to data collected while composing ODP actions.
774 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
775 * USERSPACE action's user-cookie which is required for sflow. */
777 fix_sflow_action(struct xlate_ctx *ctx)
779 const struct flow *base = &ctx->base_flow;
780 union user_action_cookie *cookie;
782 if (!ctx->user_cookie_offset) {
786 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
787 sizeof cookie->sflow);
788 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
790 compose_sflow_cookie(ctx->ofproto, base->vlan_tci,
791 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
794 static enum slow_path_reason
795 process_special(struct xlate_ctx *ctx, const struct flow *flow,
796 const struct ofport_dpif *ofport, const struct ofpbuf *packet)
798 struct ofproto_dpif *ofproto = ctx->ofproto;
799 struct flow_wildcards *wc = &ctx->xout->wc;
803 } else if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow, wc)) {
805 cfm_process_heartbeat(ofport->cfm, packet);
808 } else if (ofport->bfd && bfd_should_process_flow(flow, wc)) {
810 bfd_process_packet(ofport->bfd, flow, packet);
813 } else if (ofport->bundle && ofport->bundle->lacp
814 && flow->dl_type == htons(ETH_TYPE_LACP)) {
816 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
819 } else if (ofproto->stp && stp_should_process_flow(flow, wc)) {
821 stp_process_packet(ofport, packet);
830 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
833 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
834 struct flow_wildcards *wc = &ctx->xout->wc;
835 struct flow *flow = &ctx->xin->flow;
836 ovs_be16 flow_vlan_tci;
837 uint32_t flow_skb_mark;
839 odp_port_t out_port, odp_port;
842 /* If 'struct flow' gets additional metadata, we'll need to zero it out
843 * before traversing a patch port. */
844 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 20);
847 xlate_report(ctx, "Nonexistent output port");
849 } else if (ofport->up.pp.config & OFPUTIL_PC_NO_FWD) {
850 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
852 } else if (check_stp && !stp_forward_in_state(ofport->stp_state)) {
853 xlate_report(ctx, "STP not in forwarding state, skipping output");
858 struct ofport_dpif *peer = ofport->peer;
859 struct flow old_flow = ctx->xin->flow;
860 enum slow_path_reason special;
862 ctx->ofproto = ofproto_dpif_cast(peer->up.ofproto);
863 flow->in_port.ofp_port = peer->up.ofp_port;
864 flow->metadata = htonll(0);
865 memset(&flow->tunnel, 0, sizeof flow->tunnel);
866 memset(flow->regs, 0, sizeof flow->regs);
868 special = process_special(ctx, &ctx->xin->flow, peer,
871 ctx->xout->slow = special;
872 } else if (may_receive(peer, ctx)) {
873 if (stp_forward_in_state(peer->stp_state)) {
874 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
876 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
877 * learning action look at the packet, then drop it. */
878 struct flow old_base_flow = ctx->base_flow;
879 size_t old_size = ctx->xout->odp_actions.size;
880 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
881 ctx->base_flow = old_base_flow;
882 ctx->xout->odp_actions.size = old_size;
886 ctx->xin->flow = old_flow;
887 ctx->ofproto = ofproto_dpif_cast(ofport->up.ofproto);
889 if (ctx->xin->resubmit_stats) {
890 netdev_vport_inc_tx(ofport->up.netdev, ctx->xin->resubmit_stats);
891 netdev_vport_inc_rx(peer->up.netdev, ctx->xin->resubmit_stats);
897 flow_vlan_tci = flow->vlan_tci;
898 flow_skb_mark = flow->skb_mark;
899 flow_nw_tos = flow->nw_tos;
901 if (ofproto_dpif_dscp_from_priority(ofport, flow->skb_priority, &dscp)) {
902 wc->masks.nw_tos |= IP_ECN_MASK;
903 flow->nw_tos &= ~IP_DSCP_MASK;
904 flow->nw_tos |= dscp;
907 if (ofport->is_tunnel) {
908 /* Save tunnel metadata so that changes made due to
909 * the Logical (tunnel) Port are not visible for any further
910 * matches, while explicit set actions on tunnel metadata are.
912 struct flow_tnl flow_tnl = flow->tunnel;
913 odp_port = tnl_port_send(ofport, flow, &ctx->xout->wc);
914 if (odp_port == ODPP_NONE) {
915 xlate_report(ctx, "Tunneling decided against output");
916 goto out; /* restore flow_nw_tos */
918 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
919 xlate_report(ctx, "Not tunneling to our own address");
920 goto out; /* restore flow_nw_tos */
922 if (ctx->xin->resubmit_stats) {
923 netdev_vport_inc_tx(ofport->up.netdev, ctx->xin->resubmit_stats);
926 commit_odp_tunnel_action(flow, &ctx->base_flow,
927 &ctx->xout->odp_actions);
928 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
930 ofp_port_t vlandev_port;
932 odp_port = ofport->odp_port;
933 if (!hmap_is_empty(&ctx->ofproto->realdev_vid_map)) {
934 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
936 vlandev_port = vsp_realdev_to_vlandev(ctx->ofproto, ofp_port,
938 if (vlandev_port == ofp_port) {
941 out_port = ofp_port_to_odp_port(ctx->ofproto, vlandev_port);
942 flow->vlan_tci = htons(0);
944 flow->skb_mark &= ~IPSEC_MARK;
947 if (out_port != ODPP_NONE) {
948 commit_odp_actions(flow, &ctx->base_flow,
949 &ctx->xout->odp_actions, &ctx->xout->wc);
950 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
953 ctx->sflow_odp_port = odp_port;
954 ctx->sflow_n_outputs++;
955 ctx->xout->nf_output_iface = ofp_port;
960 flow->vlan_tci = flow_vlan_tci;
961 flow->skb_mark = flow_skb_mark;
962 flow->nw_tos = flow_nw_tos;
966 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
968 compose_output_action__(ctx, ofp_port, true);
971 /* Common rule processing in one place to avoid duplicating code. */
972 static struct rule_dpif *
973 ctx_rule_hooks(struct xlate_ctx *ctx, struct rule_dpif *rule,
976 if (ctx->xin->resubmit_hook) {
977 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
979 if (rule == NULL && may_packet_in) {
981 * check if table configuration flags
982 * OFPTC_TABLE_MISS_CONTROLLER, default.
983 * OFPTC_TABLE_MISS_CONTINUE,
984 * OFPTC_TABLE_MISS_DROP
985 * When OF1.0, OFPTC_TABLE_MISS_CONTINUE is used. What to do?
987 rule = rule_dpif_miss_rule(ctx->ofproto, &ctx->xin->flow);
989 if (rule && ctx->xin->resubmit_stats) {
990 rule_credit_stats(rule, ctx->xin->resubmit_stats);
996 xlate_table_action(struct xlate_ctx *ctx,
997 ofp_port_t in_port, uint8_t table_id, bool may_packet_in)
999 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
1000 struct rule_dpif *rule;
1001 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
1002 uint8_t old_table_id = ctx->table_id;
1004 ctx->table_id = table_id;
1006 /* Look up a flow with 'in_port' as the input port. */
1007 ctx->xin->flow.in_port.ofp_port = in_port;
1008 rule = rule_dpif_lookup_in_table(ctx->ofproto, &ctx->xin->flow,
1009 &ctx->xout->wc, table_id);
1011 ctx->xout->tags |= calculate_flow_tag(ctx->ofproto, &ctx->xin->flow,
1012 ctx->table_id, rule);
1014 /* Restore the original input port. Otherwise OFPP_NORMAL and
1015 * OFPP_IN_PORT will have surprising behavior. */
1016 ctx->xin->flow.in_port.ofp_port = old_in_port;
1018 rule = ctx_rule_hooks(ctx, rule, may_packet_in);
1021 struct rule_dpif *old_rule = ctx->rule;
1025 do_xlate_actions(rule->up.ofpacts, rule->up.ofpacts_len, ctx);
1026 ctx->rule = old_rule;
1030 ctx->table_id = old_table_id;
1032 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
1034 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
1035 MAX_RESUBMIT_RECURSION);
1036 ctx->max_resubmit_trigger = true;
1041 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
1042 const struct ofpact_resubmit *resubmit)
1047 in_port = resubmit->in_port;
1048 if (in_port == OFPP_IN_PORT) {
1049 in_port = ctx->xin->flow.in_port.ofp_port;
1052 table_id = resubmit->table_id;
1053 if (table_id == 255) {
1054 table_id = ctx->table_id;
1057 xlate_table_action(ctx, in_port, table_id, false);
1061 flood_packets(struct xlate_ctx *ctx, bool all)
1063 struct ofport_dpif *ofport;
1065 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
1066 ofp_port_t ofp_port = ofport->up.ofp_port;
1068 if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
1073 compose_output_action__(ctx, ofp_port, false);
1074 } else if (!(ofport->up.pp.config & OFPUTIL_PC_NO_FLOOD)) {
1075 compose_output_action(ctx, ofp_port);
1079 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1083 execute_controller_action(struct xlate_ctx *ctx, int len,
1084 enum ofp_packet_in_reason reason,
1085 uint16_t controller_id)
1087 struct ofputil_packet_in pin;
1088 struct ofpbuf *packet;
1091 ovs_assert(!ctx->xout->slow || ctx->xout->slow == SLOW_CONTROLLER);
1092 ctx->xout->slow = SLOW_CONTROLLER;
1093 if (!ctx->xin->packet) {
1097 packet = ofpbuf_clone(ctx->xin->packet);
1099 key.skb_priority = 0;
1101 memset(&key.tunnel, 0, sizeof key.tunnel);
1103 commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1104 &ctx->xout->odp_actions, &ctx->xout->wc);
1106 odp_execute_actions(NULL, packet, &key, ctx->xout->odp_actions.data,
1107 ctx->xout->odp_actions.size, NULL, NULL);
1109 pin.packet = packet->data;
1110 pin.packet_len = packet->size;
1111 pin.reason = reason;
1112 pin.controller_id = controller_id;
1113 pin.table_id = ctx->table_id;
1114 pin.cookie = ctx->rule ? ctx->rule->up.flow_cookie : 0;
1117 flow_get_metadata(&ctx->xin->flow, &pin.fmd);
1119 connmgr_send_packet_in(ctx->ofproto->up.connmgr, &pin);
1120 ofpbuf_delete(packet);
1124 compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1126 struct flow_wildcards *wc = &ctx->xout->wc;
1127 struct flow *flow = &ctx->xin->flow;
1129 ovs_assert(eth_type_mpls(eth_type));
1131 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1132 memset(&wc->masks.mpls_depth, 0xff, sizeof wc->masks.mpls_depth);
1134 if (flow->mpls_depth) {
1135 flow->mpls_lse &= ~htonl(MPLS_BOS_MASK);
1141 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1142 label = htonl(0x2); /* IPV6 Explicit Null. */
1144 label = htonl(0x0); /* IPV4 Explicit Null. */
1146 wc->masks.nw_tos |= IP_DSCP_MASK;
1147 wc->masks.nw_ttl = 0xff;
1148 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
1149 ttl = flow->nw_ttl ? flow->nw_ttl : 0x40;
1150 flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
1151 flow->mpls_depth = 1;
1153 flow->dl_type = eth_type;
1157 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1159 struct flow_wildcards *wc = &ctx->xout->wc;
1160 struct flow *flow = &ctx->xin->flow;
1162 ovs_assert(eth_type_mpls(ctx->xin->flow.dl_type));
1163 ovs_assert(!eth_type_mpls(eth_type));
1165 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1166 memset(&wc->masks.mpls_depth, 0xff, sizeof wc->masks.mpls_depth);
1168 if (flow->mpls_depth) {
1170 flow->mpls_lse = htonl(0);
1171 if (!flow->mpls_depth) {
1172 flow->dl_type = eth_type;
1178 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
1180 struct flow *flow = &ctx->xin->flow;
1182 if (!is_ip_any(flow)) {
1186 ctx->xout->wc.masks.nw_ttl = 0xff;
1187 if (flow->nw_ttl > 1) {
1193 for (i = 0; i < ids->n_controllers; i++) {
1194 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
1198 /* Stop processing for current table. */
1204 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
1206 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
1210 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl);
1215 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
1217 struct flow *flow = &ctx->xin->flow;
1218 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse);
1219 struct flow_wildcards *wc = &ctx->xout->wc;
1221 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1223 if (!eth_type_mpls(flow->dl_type)) {
1229 set_mpls_lse_ttl(&flow->mpls_lse, ttl);
1232 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
1234 /* Stop processing for current table. */
1240 xlate_output_action(struct xlate_ctx *ctx,
1241 ofp_port_t port, uint16_t max_len, bool may_packet_in)
1243 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
1245 ctx->xout->nf_output_iface = NF_OUT_DROP;
1249 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
1252 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
1259 flood_packets(ctx, false);
1262 flood_packets(ctx, true);
1264 case OFPP_CONTROLLER:
1265 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
1271 if (port != ctx->xin->flow.in_port.ofp_port) {
1272 compose_output_action(ctx, port);
1274 xlate_report(ctx, "skipping output to input port");
1279 if (prev_nf_output_iface == NF_OUT_FLOOD) {
1280 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1281 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
1282 ctx->xout->nf_output_iface = prev_nf_output_iface;
1283 } else if (prev_nf_output_iface != NF_OUT_DROP &&
1284 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
1285 ctx->xout->nf_output_iface = NF_OUT_MULTI;
1290 xlate_output_reg_action(struct xlate_ctx *ctx,
1291 const struct ofpact_output_reg *or)
1293 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
1294 if (port <= UINT16_MAX) {
1295 union mf_subvalue value;
1297 memset(&value, 0xff, sizeof value);
1298 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
1299 xlate_output_action(ctx, u16_to_ofp(port),
1300 or->max_len, false);
1305 xlate_enqueue_action(struct xlate_ctx *ctx,
1306 const struct ofpact_enqueue *enqueue)
1308 ofp_port_t ofp_port = enqueue->port;
1309 uint32_t queue_id = enqueue->queue;
1310 uint32_t flow_priority, priority;
1313 /* Translate queue to priority. */
1314 error = ofproto_dpif_queue_to_priority(ctx->ofproto, queue_id, &priority);
1316 /* Fall back to ordinary output action. */
1317 xlate_output_action(ctx, enqueue->port, 0, false);
1321 /* Check output port. */
1322 if (ofp_port == OFPP_IN_PORT) {
1323 ofp_port = ctx->xin->flow.in_port.ofp_port;
1324 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
1328 /* Add datapath actions. */
1329 flow_priority = ctx->xin->flow.skb_priority;
1330 ctx->xin->flow.skb_priority = priority;
1331 compose_output_action(ctx, ofp_port);
1332 ctx->xin->flow.skb_priority = flow_priority;
1334 /* Update NetFlow output port. */
1335 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
1336 ctx->xout->nf_output_iface = ofp_port;
1337 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
1338 ctx->xout->nf_output_iface = NF_OUT_MULTI;
1343 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
1345 uint32_t skb_priority;
1347 if (!ofproto_dpif_queue_to_priority(ctx->ofproto, queue_id,
1349 ctx->xin->flow.skb_priority = skb_priority;
1351 /* Couldn't translate queue to a priority. Nothing to do. A warning
1352 * has already been logged. */
1357 slave_enabled_cb(ofp_port_t ofp_port, void *ofproto_)
1359 struct ofproto_dpif *ofproto = ofproto_;
1360 struct ofport_dpif *port;
1370 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
1373 port = get_ofp_port(ofproto, ofp_port);
1374 return port ? port->may_enable : false;
1379 xlate_bundle_action(struct xlate_ctx *ctx,
1380 const struct ofpact_bundle *bundle)
1384 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
1385 slave_enabled_cb, ctx->ofproto);
1386 if (bundle->dst.field) {
1387 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow);
1389 xlate_output_action(ctx, port, 0, false);
1394 xlate_learn_action(struct xlate_ctx *ctx,
1395 const struct ofpact_learn *learn)
1397 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
1398 struct ofputil_flow_mod fm;
1399 uint64_t ofpacts_stub[1024 / 8];
1400 struct ofpbuf ofpacts;
1403 ctx->xout->has_learn = true;
1405 learn_mask(learn, &ctx->xout->wc);
1407 if (!ctx->xin->may_learn) {
1411 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1412 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
1414 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
1415 if (error && !VLOG_DROP_WARN(&rl)) {
1416 VLOG_WARN("learning action failed to modify flow table (%s)",
1417 ofperr_get_name(error));
1420 ofpbuf_uninit(&ofpacts);
1423 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
1424 * means "infinite". */
1426 reduce_timeout(uint16_t max, uint16_t *timeout)
1428 if (max && (!*timeout || *timeout > max)) {
1434 xlate_fin_timeout(struct xlate_ctx *ctx,
1435 const struct ofpact_fin_timeout *oft)
1437 if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
1438 struct rule_dpif *rule = ctx->rule;
1440 reduce_timeout(oft->fin_idle_timeout, &rule->up.idle_timeout);
1441 reduce_timeout(oft->fin_hard_timeout, &rule->up.hard_timeout);
1446 xlate_sample_action(struct xlate_ctx *ctx,
1447 const struct ofpact_sample *os)
1449 union user_action_cookie cookie;
1450 /* Scale the probability from 16-bit to 32-bit while representing
1451 * the same percentage. */
1452 uint32_t probability = (os->probability << 16) | os->probability;
1454 commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1455 &ctx->xout->odp_actions, &ctx->xout->wc);
1457 compose_flow_sample_cookie(os->probability, os->collector_set_id,
1458 os->obs_domain_id, os->obs_point_id, &cookie);
1459 compose_sample_action(ctx->ofproto, &ctx->xout->odp_actions, &ctx->xin->flow,
1460 probability, &cookie, sizeof cookie.flow_sample);
1464 may_receive(const struct ofport_dpif *port, struct xlate_ctx *ctx)
1466 if (port->up.pp.config & (eth_addr_equals(ctx->xin->flow.dl_dst,
1468 ? OFPUTIL_PC_NO_RECV_STP
1469 : OFPUTIL_PC_NO_RECV)) {
1473 /* Only drop packets here if both forwarding and learning are
1474 * disabled. If just learning is enabled, we need to have
1475 * OFPP_NORMAL and the learning action have a look at the packet
1476 * before we can drop it. */
1477 if (!stp_forward_in_state(port->stp_state)
1478 && !stp_learn_in_state(port->stp_state)) {
1486 tunnel_ecn_ok(struct xlate_ctx *ctx)
1488 if (is_ip_any(&ctx->base_flow)
1489 && (ctx->xin->flow.tunnel.ip_tos & IP_ECN_MASK) == IP_ECN_CE) {
1490 if ((ctx->base_flow.nw_tos & IP_ECN_MASK) == IP_ECN_NOT_ECT) {
1491 VLOG_WARN_RL(&rl, "dropping tunnel packet marked ECN CE"
1492 " but is not ECN capable");
1495 /* Set the ECN CE value in the tunneled packet. */
1496 ctx->xin->flow.nw_tos |= IP_ECN_CE;
1504 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
1505 struct xlate_ctx *ctx)
1507 struct flow_wildcards *wc = &ctx->xout->wc;
1508 struct flow *flow = &ctx->xin->flow;
1509 bool was_evictable = true;
1510 const struct ofpact *a;
1513 /* Don't let the rule we're working on get evicted underneath us. */
1514 was_evictable = ctx->rule->up.evictable;
1515 ctx->rule->up.evictable = false;
1518 do_xlate_actions_again:
1519 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1520 struct ofpact_controller *controller;
1521 const struct ofpact_metadata *metadata;
1529 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
1530 ofpact_get_OUTPUT(a)->max_len, true);
1533 case OFPACT_CONTROLLER:
1534 controller = ofpact_get_CONTROLLER(a);
1535 execute_controller_action(ctx, controller->max_len,
1537 controller->controller_id);
1540 case OFPACT_ENQUEUE:
1541 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
1544 case OFPACT_SET_VLAN_VID:
1545 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
1546 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
1550 case OFPACT_SET_VLAN_PCP:
1551 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
1553 htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp << VLAN_PCP_SHIFT)
1557 case OFPACT_STRIP_VLAN:
1558 flow->vlan_tci = htons(0);
1561 case OFPACT_PUSH_VLAN:
1562 /* XXX 802.1AD(QinQ) */
1563 flow->vlan_tci = htons(VLAN_CFI);
1566 case OFPACT_SET_ETH_SRC:
1567 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1570 case OFPACT_SET_ETH_DST:
1571 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1574 case OFPACT_SET_IPV4_SRC:
1575 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1576 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1580 case OFPACT_SET_IPV4_DST:
1581 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1582 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
1586 case OFPACT_SET_IPV4_DSCP:
1587 /* OpenFlow 1.0 only supports IPv4. */
1588 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1589 flow->nw_tos &= ~IP_DSCP_MASK;
1590 flow->nw_tos |= ofpact_get_SET_IPV4_DSCP(a)->dscp;
1594 case OFPACT_SET_L4_SRC_PORT:
1595 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1596 if (is_ip_any(flow)) {
1597 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1601 case OFPACT_SET_L4_DST_PORT:
1602 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1603 if (is_ip_any(flow)) {
1604 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1608 case OFPACT_RESUBMIT:
1609 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
1612 case OFPACT_SET_TUNNEL:
1613 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
1616 case OFPACT_SET_QUEUE:
1617 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
1620 case OFPACT_POP_QUEUE:
1621 flow->skb_priority = ctx->orig_skb_priority;
1624 case OFPACT_REG_MOVE:
1625 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
1628 case OFPACT_REG_LOAD:
1629 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow);
1632 case OFPACT_STACK_PUSH:
1633 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
1637 case OFPACT_STACK_POP:
1638 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, &ctx->stack);
1641 case OFPACT_PUSH_MPLS:
1642 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a)->ethertype);
1645 case OFPACT_POP_MPLS:
1646 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
1649 case OFPACT_SET_MPLS_TTL:
1650 if (compose_set_mpls_ttl_action(ctx,
1651 ofpact_get_SET_MPLS_TTL(a)->ttl)) {
1656 case OFPACT_DEC_MPLS_TTL:
1657 if (compose_dec_mpls_ttl_action(ctx)) {
1662 case OFPACT_DEC_TTL:
1663 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
1669 /* Nothing to do. */
1672 case OFPACT_MULTIPATH:
1673 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
1677 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
1680 case OFPACT_OUTPUT_REG:
1681 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
1685 xlate_learn_action(ctx, ofpact_get_LEARN(a));
1692 case OFPACT_FIN_TIMEOUT:
1693 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1694 ctx->xout->has_fin_timeout = true;
1695 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
1698 case OFPACT_CLEAR_ACTIONS:
1700 * Nothing to do because writa-actions is not supported for now.
1701 * When writa-actions is supported, clear-actions also must
1702 * be supported at the same time.
1706 case OFPACT_WRITE_METADATA:
1707 metadata = ofpact_get_WRITE_METADATA(a);
1708 flow->metadata &= ~metadata->mask;
1709 flow->metadata |= metadata->metadata & metadata->mask;
1713 /* Not implemented yet. */
1716 case OFPACT_GOTO_TABLE: {
1717 /* It is assumed that goto-table is the last action. */
1718 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
1719 struct rule_dpif *rule;
1721 ovs_assert(ctx->table_id < ogt->table_id);
1723 ctx->table_id = ogt->table_id;
1725 /* Look up a flow from the new table. */
1726 rule = rule_dpif_lookup_in_table(ctx->ofproto, flow, wc,
1729 ctx->xout->tags = calculate_flow_tag(ctx->ofproto, &ctx->xin->flow,
1730 ctx->table_id, rule);
1732 rule = ctx_rule_hooks(ctx, rule, true);
1736 ctx->rule->up.evictable = was_evictable;
1739 was_evictable = rule->up.evictable;
1740 rule->up.evictable = false;
1742 /* Tail recursion removal. */
1743 ofpacts = rule->up.ofpacts;
1744 ofpacts_len = rule->up.ofpacts_len;
1745 goto do_xlate_actions_again;
1751 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
1758 ctx->rule->up.evictable = was_evictable;
1763 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
1764 const struct flow *flow, struct rule_dpif *rule,
1765 uint8_t tcp_flags, const struct ofpbuf *packet)
1767 xin->ofproto = ofproto;
1769 xin->packet = packet;
1770 xin->may_learn = packet != NULL;
1772 xin->ofpacts = NULL;
1773 xin->ofpacts_len = 0;
1774 xin->tcp_flags = tcp_flags;
1775 xin->resubmit_hook = NULL;
1776 xin->report_hook = NULL;
1777 xin->resubmit_stats = NULL;
1781 xlate_out_uninit(struct xlate_out *xout)
1784 ofpbuf_uninit(&xout->odp_actions);
1788 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
1789 * into datapath actions, using 'ctx', and discards the datapath actions. */
1791 xlate_actions_for_side_effects(struct xlate_in *xin)
1793 struct xlate_out xout;
1795 xlate_actions(xin, &xout);
1796 xlate_out_uninit(&xout);
1800 xlate_report(struct xlate_ctx *ctx, const char *s)
1802 if (ctx->xin->report_hook) {
1803 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
1808 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
1811 dst->tags = src->tags;
1812 dst->slow = src->slow;
1813 dst->has_learn = src->has_learn;
1814 dst->has_normal = src->has_normal;
1815 dst->has_fin_timeout = src->has_fin_timeout;
1816 dst->nf_output_iface = src->nf_output_iface;
1817 dst->mirrors = src->mirrors;
1819 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
1820 sizeof dst->odp_actions_stub);
1821 ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
1822 src->odp_actions.size);
1826 actions_output_to_local_port(const struct xlate_ctx *ctx)
1828 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->ofproto, OFPP_LOCAL);
1829 const struct nlattr *a;
1832 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions.data,
1833 ctx->xout->odp_actions.size) {
1834 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
1835 && nl_attr_get_odp_port(a) == local_odp_port) {
1842 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
1843 * into datapath actions in 'odp_actions', using 'ctx'. */
1845 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
1847 /* Normally false. Set to true if we ever hit MAX_RESUBMIT_RECURSION, so
1848 * that in the future we always keep a copy of the original flow for
1849 * tracing purposes. */
1850 static bool hit_resubmit_limit;
1852 struct flow_wildcards *wc = &xout->wc;
1853 struct flow *flow = &xin->flow;
1855 enum slow_path_reason special;
1856 const struct ofpact *ofpacts;
1857 struct ofport_dpif *in_port;
1858 struct flow orig_flow;
1859 struct xlate_ctx ctx;
1862 COVERAGE_INC(ofproto_dpif_xlate);
1864 /* Flow initialization rules:
1865 * - 'base_flow' must match the kernel's view of the packet at the
1866 * time that action processing starts. 'flow' represents any
1867 * transformations we wish to make through actions.
1868 * - By default 'base_flow' and 'flow' are the same since the input
1869 * packet matches the output before any actions are applied.
1870 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
1871 * of the received packet as seen by the kernel. If we later output
1872 * to another device without any modifications this will cause us to
1873 * insert a new tag since the original one was stripped off by the
1875 * - Tunnel metadata as received is retained in 'flow'. This allows
1876 * tunnel metadata matching also in later tables.
1877 * Since a kernel action for setting the tunnel metadata will only be
1878 * generated with actual tunnel output, changing the tunnel metadata
1879 * values in 'flow' (such as tun_id) will only have effect with a later
1880 * tunnel output action.
1881 * - Tunnel 'base_flow' is completely cleared since that is what the
1882 * kernel does. If we wish to maintain the original values an action
1883 * needs to be generated. */
1888 ctx.ofproto = xin->ofproto;
1889 ctx.rule = xin->rule;
1891 ctx.base_flow = *flow;
1892 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
1893 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
1895 flow_wildcards_init_catchall(wc);
1896 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
1897 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
1898 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1899 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
1901 if (tnl_port_should_receive(&ctx.xin->flow)) {
1902 memset(&wc->masks.tunnel, 0xff, sizeof wc->masks.tunnel);
1904 if (xin->ofproto->netflow) {
1905 netflow_mask_wc(flow, wc);
1910 ctx.xout->has_learn = false;
1911 ctx.xout->has_normal = false;
1912 ctx.xout->has_fin_timeout = false;
1913 ctx.xout->nf_output_iface = NF_OUT_DROP;
1914 ctx.xout->mirrors = 0;
1916 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
1917 sizeof ctx.xout->odp_actions_stub);
1918 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
1921 ctx.max_resubmit_trigger = false;
1922 ctx.orig_skb_priority = flow->skb_priority;
1927 ofpacts = xin->ofpacts;
1928 ofpacts_len = xin->ofpacts_len;
1929 } else if (xin->rule) {
1930 ofpacts = xin->rule->up.ofpacts;
1931 ofpacts_len = xin->rule->up.ofpacts_len;
1936 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
1938 if (ctx.ofproto->has_mirrors || hit_resubmit_limit) {
1939 /* Do this conditionally because the copy is expensive enough that it
1940 * shows up in profiles. */
1944 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
1945 switch (ctx.ofproto->up.frag_handling) {
1946 case OFPC_FRAG_NORMAL:
1947 /* We must pretend that transport ports are unavailable. */
1948 flow->tp_src = ctx.base_flow.tp_src = htons(0);
1949 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
1952 case OFPC_FRAG_DROP:
1955 case OFPC_FRAG_REASM:
1958 case OFPC_FRAG_NX_MATCH:
1959 /* Nothing to do. */
1962 case OFPC_INVALID_TTL_TO_CONTROLLER:
1967 in_port = get_ofp_port(ctx.ofproto, flow->in_port.ofp_port);
1968 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
1970 ctx.xout->slow = special;
1972 static struct vlog_rate_limit trace_rl = VLOG_RATE_LIMIT_INIT(1, 1);
1973 size_t sample_actions_len;
1975 if (flow->in_port.ofp_port
1976 != vsp_realdev_to_vlandev(ctx.ofproto, flow->in_port.ofp_port,
1978 ctx.base_flow.vlan_tci = 0;
1981 add_sflow_action(&ctx);
1982 add_ipfix_action(&ctx);
1983 sample_actions_len = ctx.xout->odp_actions.size;
1985 if (tunnel_ecn_ok(&ctx) && (!in_port || may_receive(in_port, &ctx))) {
1986 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
1988 /* We've let OFPP_NORMAL and the learning action look at the
1989 * packet, so drop it now if forwarding is disabled. */
1990 if (in_port && !stp_forward_in_state(in_port->stp_state)) {
1991 ctx.xout->odp_actions.size = sample_actions_len;
1995 if (ctx.max_resubmit_trigger && !ctx.xin->resubmit_hook) {
1996 if (!hit_resubmit_limit) {
1997 /* We didn't record the original flow. Make sure we do from
1999 hit_resubmit_limit = true;
2000 } else if (!VLOG_DROP_ERR(&trace_rl)) {
2001 struct ds ds = DS_EMPTY_INITIALIZER;
2003 ofproto_trace(ctx.ofproto, &orig_flow, ctx.xin->packet, &ds);
2004 VLOG_ERR("Trace triggered by excessive resubmit "
2005 "recursion:\n%s", ds_cstr(&ds));
2010 if (connmgr_has_in_band(ctx.ofproto->up.connmgr)
2011 && in_band_must_output_to_local_port(flow)
2012 && !actions_output_to_local_port(&ctx)) {
2013 compose_output_action(&ctx, OFPP_LOCAL);
2015 if (ctx.ofproto->has_mirrors) {
2016 add_mirror_actions(&ctx, &orig_flow);
2018 fix_sflow_action(&ctx);
2021 ofpbuf_uninit(&ctx.stack);
2023 /* Clear the metadata and register wildcard masks, because we won't
2024 * use non-header fields as part of the cache. */
2025 memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
2026 memset(&wc->masks.regs, 0, sizeof wc->masks.regs);