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"
22 #include "byte-order.h"
26 #include "dynamic-string.h"
28 #include "mac-learning.h"
29 #include "meta-flow.h"
30 #include "multipath.h"
31 #include "netdev-vport.h"
34 #include "odp-execute.h"
35 #include "ofp-actions.h"
36 #include "ofproto/ofproto-dpif-ipfix.h"
37 #include "ofproto/ofproto-dpif-sflow.h"
38 #include "ofproto/ofproto-dpif.h"
42 COVERAGE_DEFINE(ofproto_dpif_xlate);
44 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
48 struct xlate_out *xout;
50 struct ofproto_dpif *ofproto;
52 /* Flow at the last commit. */
53 struct flow base_flow;
55 /* Tunnel IP destination address as received. This is stored separately
56 * as the base_flow.tunnel is cleared on init to reflect the datapath
57 * behavior. Used to make sure not to send tunneled output to ourselves,
58 * which might lead to an infinite loop. This could happen easily
59 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
60 * actually set the tun_dst field. */
61 ovs_be32 orig_tunnel_ip_dst;
63 /* Stack for the push and pop actions. Each stack element is of type
64 * "union mf_subvalue". */
65 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
68 /* The rule that we are currently translating, or NULL. */
69 struct rule_dpif *rule;
71 int recurse; /* Recursion level, via xlate_table_action. */
72 bool max_resubmit_trigger; /* Recursed too deeply during translation. */
73 uint32_t orig_skb_priority; /* Priority when packet arrived. */
74 uint8_t table_id; /* OpenFlow table ID where flow was found. */
75 uint32_t sflow_n_outputs; /* Number of output ports. */
76 uint32_t sflow_odp_port; /* Output port for composing sFlow action. */
77 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
78 bool exit; /* No further actions should be processed. */
81 /* A controller may use OFPP_NONE as the ingress port to indicate that
82 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
83 * when an input bundle is needed for validation (e.g., mirroring or
84 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
85 * any 'port' structs, so care must be taken when dealing with it. */
86 static struct ofbundle ofpp_none_bundle = {
88 .vlan_mode = PORT_VLAN_TRUNK
91 static bool may_receive(const struct ofport_dpif *, struct xlate_ctx *);
92 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
94 static void xlate_normal(struct xlate_ctx *);
95 static void xlate_report(struct xlate_ctx *, const char *);
96 static void xlate_table_action(struct xlate_ctx *, uint16_t in_port,
97 uint8_t table_id, bool may_packet_in);
98 static bool input_vid_is_valid(uint16_t vid, struct ofbundle *, bool warn);
99 static uint16_t input_vid_to_vlan(const struct ofbundle *, uint16_t vid);
100 static void output_normal(struct xlate_ctx *, const struct ofbundle *,
102 static void compose_output_action(struct xlate_ctx *, uint16_t ofp_port);
104 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
107 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
109 return (bundle->vlan_mode != PORT_VLAN_ACCESS
110 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
114 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
116 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
120 vlan_is_mirrored(const struct ofmirror *m, int vlan)
122 return !m->vlans || bitmap_is_set(m->vlans, vlan);
125 static struct ofbundle *
126 lookup_input_bundle(const struct ofproto_dpif *ofproto, uint16_t in_port,
127 bool warn, struct ofport_dpif **in_ofportp)
129 struct ofport_dpif *ofport;
131 /* Find the port and bundle for the received packet. */
132 ofport = get_ofp_port(ofproto, in_port);
134 *in_ofportp = ofport;
136 if (ofport && ofport->bundle) {
137 return ofport->bundle;
140 /* Special-case OFPP_NONE, which a controller may use as the ingress
141 * port for traffic that it is sourcing. */
142 if (in_port == OFPP_NONE) {
143 return &ofpp_none_bundle;
146 /* Odd. A few possible reasons here:
148 * - We deleted a port but there are still a few packets queued up
151 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
152 * we don't know about.
154 * - The ofproto client didn't configure the port as part of a bundle.
155 * This is particularly likely to happen if a packet was received on the
156 * port after it was created, but before the client had a chance to
157 * configure its bundle.
160 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
162 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
163 "port %"PRIu16, ofproto->up.name, in_port);
169 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
171 struct ofproto_dpif *ofproto = ctx->ofproto;
172 mirror_mask_t mirrors;
173 struct ofbundle *in_bundle;
176 const struct nlattr *a;
179 in_bundle = lookup_input_bundle(ctx->ofproto, orig_flow->in_port,
180 ctx->xin->packet != NULL, NULL);
184 mirrors = in_bundle->src_mirrors;
186 /* Drop frames on bundles reserved for mirroring. */
187 if (in_bundle->mirror_out) {
188 if (ctx->xin->packet != NULL) {
189 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
190 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
191 "%s, which is reserved exclusively for mirroring",
192 ctx->ofproto->up.name, in_bundle->name);
198 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
199 if (!input_vid_is_valid(vid, in_bundle, ctx->xin->packet != NULL)) {
202 vlan = input_vid_to_vlan(in_bundle, vid);
204 /* Look at the output ports to check for destination selections. */
206 NL_ATTR_FOR_EACH (a, left, ctx->xout->odp_actions.data,
207 ctx->xout->odp_actions.size) {
208 enum ovs_action_attr type = nl_attr_type(a);
209 struct ofport_dpif *ofport;
211 if (type != OVS_ACTION_ATTR_OUTPUT) {
215 ofport = get_odp_port(ofproto, nl_attr_get_u32(a));
216 if (ofport && ofport->bundle) {
217 mirrors |= ofport->bundle->dst_mirrors;
225 /* Restore the original packet before adding the mirror actions. */
226 ctx->xin->flow = *orig_flow;
231 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
234 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
237 if (!vlan_is_mirrored(m, vlan)) {
238 mirrors = zero_rightmost_1bit(mirrors);
242 mirrors &= ~m->dup_mirrors;
243 ctx->xout->mirrors |= m->dup_mirrors;
245 output_normal(ctx, m->out, vlan);
246 } else if (vlan != m->out_vlan
247 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
248 struct ofbundle *bundle;
250 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
251 if (ofbundle_includes_vlan(bundle, m->out_vlan)
252 && !bundle->mirror_out) {
253 output_normal(ctx, bundle, m->out_vlan);
260 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
261 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
262 * the bundle on which the packet was received, returns the VLAN to which the
265 * Both 'vid' and the return value are in the range 0...4095. */
267 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
269 switch (in_bundle->vlan_mode) {
270 case PORT_VLAN_ACCESS:
271 return in_bundle->vlan;
274 case PORT_VLAN_TRUNK:
277 case PORT_VLAN_NATIVE_UNTAGGED:
278 case PORT_VLAN_NATIVE_TAGGED:
279 return vid ? vid : in_bundle->vlan;
286 /* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'.
287 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
290 * 'vid' should be the VID obtained from the 802.1Q header that was received as
291 * part of a packet (specify 0 if there was no 802.1Q header), in the range
294 input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn)
296 /* Allow any VID on the OFPP_NONE port. */
297 if (in_bundle == &ofpp_none_bundle) {
301 switch (in_bundle->vlan_mode) {
302 case PORT_VLAN_ACCESS:
305 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
306 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
307 "packet received on port %s configured as VLAN "
308 "%"PRIu16" access port",
309 in_bundle->ofproto->up.name, vid,
310 in_bundle->name, in_bundle->vlan);
316 case PORT_VLAN_NATIVE_UNTAGGED:
317 case PORT_VLAN_NATIVE_TAGGED:
319 /* Port must always carry its native VLAN. */
323 case PORT_VLAN_TRUNK:
324 if (!ofbundle_includes_vlan(in_bundle, vid)) {
326 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
327 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet "
328 "received on port %s not configured for trunking "
330 in_bundle->ofproto->up.name, vid,
331 in_bundle->name, vid);
343 /* Given 'vlan', the VLAN that a packet belongs to, and
344 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
345 * that should be included in the 802.1Q header. (If the return value is 0,
346 * then the 802.1Q header should only be included in the packet if there is a
349 * Both 'vlan' and the return value are in the range 0...4095. */
351 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
353 switch (out_bundle->vlan_mode) {
354 case PORT_VLAN_ACCESS:
357 case PORT_VLAN_TRUNK:
358 case PORT_VLAN_NATIVE_TAGGED:
361 case PORT_VLAN_NATIVE_UNTAGGED:
362 return vlan == out_bundle->vlan ? 0 : vlan;
370 output_normal(struct xlate_ctx *ctx, const struct ofbundle *out_bundle,
373 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
374 struct ofport_dpif *port;
376 ovs_be16 tci, old_tci;
378 vid = output_vlan_to_vid(out_bundle, vlan);
379 if (!out_bundle->bond) {
380 port = ofbundle_get_a_port(out_bundle);
382 port = bond_choose_output_slave(out_bundle->bond, &ctx->xin->flow,
383 &ctx->xout->wc, vid, &ctx->xout->tags);
385 /* No slaves enabled, so drop packet. */
392 if (tci || out_bundle->use_priority_tags) {
393 tci |= *flow_tci & htons(VLAN_PCP_MASK);
395 tci |= htons(VLAN_CFI);
400 compose_output_action(ctx, port->up.ofp_port);
404 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
405 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
406 * indicate this; newer upstream kernels use gratuitous ARP requests. */
408 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
410 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
414 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
415 if (!eth_addr_is_broadcast(flow->dl_dst)) {
419 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
420 if (flow->nw_proto == ARP_OP_REPLY) {
422 } else if (flow->nw_proto == ARP_OP_REQUEST) {
423 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
424 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
426 return flow->nw_src == flow->nw_dst;
433 update_learning_table(struct ofproto_dpif *ofproto,
434 const struct flow *flow, struct flow_wildcards *wc,
435 int vlan, struct ofbundle *in_bundle)
437 struct mac_entry *mac;
439 /* Don't learn the OFPP_NONE port. */
440 if (in_bundle == &ofpp_none_bundle) {
444 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
448 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
449 if (is_gratuitous_arp(flow, wc)) {
450 /* We don't want to learn from gratuitous ARP packets that are
451 * reflected back over bond slaves so we lock the learning table. */
452 if (!in_bundle->bond) {
453 mac_entry_set_grat_arp_lock(mac);
454 } else if (mac_entry_is_grat_arp_locked(mac)) {
459 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
460 /* The log messages here could actually be useful in debugging,
461 * so keep the rate limit relatively high. */
462 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
463 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
464 "on port %s in VLAN %d",
465 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
466 in_bundle->name, vlan);
468 mac->port.p = in_bundle;
469 tag_set_add(&ofproto->backer->revalidate_set,
470 mac_learning_changed(ofproto->ml, mac));
474 /* Determines whether packets in 'flow' within 'ofproto' should be forwarded or
475 * dropped. Returns true if they may be forwarded, false if they should be
478 * 'in_port' must be the ofport_dpif that corresponds to flow->in_port.
479 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
481 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
482 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
483 * checked by input_vid_is_valid().
485 * May also add tags to '*tags', although the current implementation only does
486 * so in one special case.
489 is_admissible(struct xlate_ctx *ctx, struct ofport_dpif *in_port,
492 struct ofproto_dpif *ofproto = ctx->ofproto;
493 struct flow *flow = &ctx->xin->flow;
494 struct ofbundle *in_bundle = in_port->bundle;
496 /* Drop frames for reserved multicast addresses
497 * only if forward_bpdu option is absent. */
498 if (!ofproto->up.forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
499 xlate_report(ctx, "packet has reserved destination MAC, dropping");
503 if (in_bundle->bond) {
504 struct mac_entry *mac;
506 switch (bond_check_admissibility(in_bundle->bond, in_port,
507 flow->dl_dst, &ctx->xout->tags)) {
512 xlate_report(ctx, "bonding refused admissibility, dropping");
515 case BV_DROP_IF_MOVED:
516 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
517 if (mac && mac->port.p != in_bundle &&
518 (!is_gratuitous_arp(flow, &ctx->xout->wc)
519 || mac_entry_is_grat_arp_locked(mac))) {
520 xlate_report(ctx, "SLB bond thinks this packet looped back, "
532 xlate_normal(struct xlate_ctx *ctx)
534 struct flow_wildcards *wc = &ctx->xout->wc;
535 struct flow *flow = &ctx->xin->flow;
536 struct ofport_dpif *in_port;
537 struct ofbundle *in_bundle;
538 struct mac_entry *mac;
542 ctx->xout->has_normal = true;
544 /* Check the dl_type, since we may check for gratuituous ARP. */
545 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
547 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
548 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
549 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
551 in_bundle = lookup_input_bundle(ctx->ofproto, flow->in_port,
552 ctx->xin->packet != NULL, &in_port);
554 xlate_report(ctx, "no input bundle, dropping");
558 /* Drop malformed frames. */
559 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
560 !(flow->vlan_tci & htons(VLAN_CFI))) {
561 if (ctx->xin->packet != NULL) {
562 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
563 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
564 "VLAN tag received on port %s",
565 ctx->ofproto->up.name, in_bundle->name);
567 xlate_report(ctx, "partial VLAN tag, dropping");
571 /* Drop frames on bundles reserved for mirroring. */
572 if (in_bundle->mirror_out) {
573 if (ctx->xin->packet != NULL) {
574 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
575 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
576 "%s, which is reserved exclusively for mirroring",
577 ctx->ofproto->up.name, in_bundle->name);
579 xlate_report(ctx, "input port is mirror output port, dropping");
584 vid = vlan_tci_to_vid(flow->vlan_tci);
585 if (!input_vid_is_valid(vid, in_bundle, ctx->xin->packet != NULL)) {
586 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
589 vlan = input_vid_to_vlan(in_bundle, vid);
591 /* Check other admissibility requirements. */
592 if (in_port && !is_admissible(ctx, in_port, vlan)) {
596 /* Learn source MAC. */
597 if (ctx->xin->may_learn) {
598 update_learning_table(ctx->ofproto, flow, wc, vlan, in_bundle);
601 /* Determine output bundle. */
602 mac = mac_learning_lookup(ctx->ofproto->ml, flow->dl_dst, vlan,
605 if (mac->port.p != in_bundle) {
606 xlate_report(ctx, "forwarding to learned port");
607 output_normal(ctx, mac->port.p, vlan);
609 xlate_report(ctx, "learned port is input port, dropping");
612 struct ofbundle *bundle;
614 xlate_report(ctx, "no learned MAC for destination, flooding");
615 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
616 if (bundle != in_bundle
617 && ofbundle_includes_vlan(bundle, vlan)
619 && !bundle->mirror_out) {
620 output_normal(ctx, bundle, vlan);
623 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
627 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
628 * the number of packets out of UINT32_MAX to sample. The given
629 * cookie is passed back in the callback for each sampled packet.
632 compose_sample_action(const struct ofproto_dpif *ofproto,
633 struct ofpbuf *odp_actions,
634 const struct flow *flow,
635 const uint32_t probability,
636 const union user_action_cookie *cookie,
637 const size_t cookie_size)
639 size_t sample_offset, actions_offset;
642 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
644 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
646 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
647 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, cookie,
650 nl_msg_end_nested(odp_actions, actions_offset);
651 nl_msg_end_nested(odp_actions, sample_offset);
652 return cookie_offset;
656 compose_sflow_cookie(const struct ofproto_dpif *ofproto,
657 ovs_be16 vlan_tci, uint32_t odp_port,
658 unsigned int n_outputs, union user_action_cookie *cookie)
662 cookie->type = USER_ACTION_COOKIE_SFLOW;
663 cookie->sflow.vlan_tci = vlan_tci;
665 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
666 * port information") for the interpretation of cookie->output. */
669 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
670 cookie->sflow.output = 0x40000000 | 256;
674 ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
676 cookie->sflow.output = ifindex;
681 /* 0x80000000 means "multiple output ports. */
682 cookie->sflow.output = 0x80000000 | n_outputs;
687 /* Compose SAMPLE action for sFlow bridge sampling. */
689 compose_sflow_action(const struct ofproto_dpif *ofproto,
690 struct ofpbuf *odp_actions,
691 const struct flow *flow,
694 uint32_t probability;
695 union user_action_cookie cookie;
697 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
701 probability = dpif_sflow_get_probability(ofproto->sflow);
702 compose_sflow_cookie(ofproto, htons(0), odp_port,
703 odp_port == OVSP_NONE ? 0 : 1, &cookie);
705 return compose_sample_action(ofproto, odp_actions, flow, probability,
706 &cookie, sizeof cookie.sflow);
710 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
711 uint32_t obs_domain_id, uint32_t obs_point_id,
712 union user_action_cookie *cookie)
714 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
715 cookie->flow_sample.probability = probability;
716 cookie->flow_sample.collector_set_id = collector_set_id;
717 cookie->flow_sample.obs_domain_id = obs_domain_id;
718 cookie->flow_sample.obs_point_id = obs_point_id;
722 compose_ipfix_cookie(union user_action_cookie *cookie)
724 cookie->type = USER_ACTION_COOKIE_IPFIX;
727 /* Compose SAMPLE action for IPFIX bridge sampling. */
729 compose_ipfix_action(const struct ofproto_dpif *ofproto,
730 struct ofpbuf *odp_actions,
731 const struct flow *flow)
733 uint32_t probability;
734 union user_action_cookie cookie;
736 if (!ofproto->ipfix || flow->in_port == OFPP_NONE) {
740 probability = dpif_ipfix_get_bridge_exporter_probability(ofproto->ipfix);
741 compose_ipfix_cookie(&cookie);
743 compose_sample_action(ofproto, odp_actions, flow, probability,
744 &cookie, sizeof cookie.ipfix);
747 /* SAMPLE action for sFlow must be first action in any given list of
748 * actions. At this point we do not have all information required to
749 * build it. So try to build sample action as complete as possible. */
751 add_sflow_action(struct xlate_ctx *ctx)
753 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
754 &ctx->xout->odp_actions,
755 &ctx->xin->flow, OVSP_NONE);
756 ctx->sflow_odp_port = 0;
757 ctx->sflow_n_outputs = 0;
760 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
761 * of actions, eventually after the SAMPLE action for sFlow. */
763 add_ipfix_action(struct xlate_ctx *ctx)
765 compose_ipfix_action(ctx->ofproto, &ctx->xout->odp_actions,
769 /* Fix SAMPLE action according to data collected while composing ODP actions.
770 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
771 * USERSPACE action's user-cookie which is required for sflow. */
773 fix_sflow_action(struct xlate_ctx *ctx)
775 const struct flow *base = &ctx->base_flow;
776 union user_action_cookie *cookie;
778 if (!ctx->user_cookie_offset) {
782 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
783 sizeof cookie->sflow);
784 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
786 compose_sflow_cookie(ctx->ofproto, base->vlan_tci,
787 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
791 compose_output_action__(struct xlate_ctx *ctx, uint16_t ofp_port,
794 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
795 struct flow *flow = &ctx->xin->flow;
796 ovs_be16 flow_vlan_tci;
797 uint32_t flow_skb_mark;
799 struct priority_to_dscp *pdscp;
800 uint32_t out_port, odp_port;
802 /* If 'struct flow' gets additional metadata, we'll need to zero it out
803 * before traversing a patch port. */
804 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 20);
807 xlate_report(ctx, "Nonexistent output port");
809 } else if (ofport->up.pp.config & OFPUTIL_PC_NO_FWD) {
810 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
812 } else if (check_stp && !stp_forward_in_state(ofport->stp_state)) {
813 xlate_report(ctx, "STP not in forwarding state, skipping output");
817 if (netdev_vport_is_patch(ofport->up.netdev)) {
818 struct ofport_dpif *peer = ofport_get_peer(ofport);
819 struct flow old_flow = ctx->xin->flow;
820 const struct ofproto_dpif *peer_ofproto;
821 enum slow_path_reason special;
822 struct ofport_dpif *in_port;
825 xlate_report(ctx, "Nonexistent patch port peer");
829 peer_ofproto = ofproto_dpif_cast(peer->up.ofproto);
830 if (peer_ofproto->backer != ctx->ofproto->backer) {
831 xlate_report(ctx, "Patch port peer on a different datapath");
835 ctx->ofproto = ofproto_dpif_cast(peer->up.ofproto);
836 flow->in_port = peer->up.ofp_port;
837 flow->metadata = htonll(0);
838 memset(&flow->tunnel, 0, sizeof flow->tunnel);
839 memset(flow->regs, 0, sizeof flow->regs);
841 in_port = get_ofp_port(ctx->ofproto, flow->in_port);
842 special = process_special(ctx->ofproto, &ctx->xin->flow, in_port,
845 ctx->xout->slow = special;
846 } else if (!in_port || may_receive(in_port, ctx)) {
847 if (!in_port || stp_forward_in_state(in_port->stp_state)) {
848 xlate_table_action(ctx, flow->in_port, 0, true);
850 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
851 * learning action look at the packet, then drop it. */
852 struct flow old_base_flow = ctx->base_flow;
853 size_t old_size = ctx->xout->odp_actions.size;
854 xlate_table_action(ctx, flow->in_port, 0, true);
855 ctx->base_flow = old_base_flow;
856 ctx->xout->odp_actions.size = old_size;
860 ctx->xin->flow = old_flow;
861 ctx->ofproto = ofproto_dpif_cast(ofport->up.ofproto);
863 if (ctx->xin->resubmit_stats) {
864 netdev_vport_inc_tx(ofport->up.netdev, ctx->xin->resubmit_stats);
865 netdev_vport_inc_rx(peer->up.netdev, ctx->xin->resubmit_stats);
871 flow_vlan_tci = flow->vlan_tci;
872 flow_skb_mark = flow->skb_mark;
873 flow_nw_tos = flow->nw_tos;
875 pdscp = get_priority(ofport, flow->skb_priority);
877 flow->nw_tos &= ~IP_DSCP_MASK;
878 flow->nw_tos |= pdscp->dscp;
881 if (ofport->tnl_port) {
882 /* Save tunnel metadata so that changes made due to
883 * the Logical (tunnel) Port are not visible for any further
884 * matches, while explicit set actions on tunnel metadata are.
886 struct flow_tnl flow_tnl = flow->tunnel;
887 odp_port = tnl_port_send(ofport->tnl_port, flow);
888 if (odp_port == OVSP_NONE) {
889 xlate_report(ctx, "Tunneling decided against output");
890 goto out; /* restore flow_nw_tos */
892 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
893 xlate_report(ctx, "Not tunneling to our own address");
894 goto out; /* restore flow_nw_tos */
896 if (ctx->xin->resubmit_stats) {
897 netdev_vport_inc_tx(ofport->up.netdev, ctx->xin->resubmit_stats);
900 commit_odp_tunnel_action(flow, &ctx->base_flow,
901 &ctx->xout->odp_actions);
902 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
904 uint16_t vlandev_port;
905 odp_port = ofport->odp_port;
906 vlandev_port = vsp_realdev_to_vlandev(ctx->ofproto, ofp_port,
908 if (vlandev_port == ofp_port) {
911 out_port = ofp_port_to_odp_port(ctx->ofproto, vlandev_port);
912 flow->vlan_tci = htons(0);
914 flow->skb_mark &= ~IPSEC_MARK;
916 commit_odp_actions(flow, &ctx->base_flow, &ctx->xout->odp_actions);
917 nl_msg_put_u32(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT, out_port);
919 ctx->sflow_odp_port = odp_port;
920 ctx->sflow_n_outputs++;
921 ctx->xout->nf_output_iface = ofp_port;
924 flow->vlan_tci = flow_vlan_tci;
925 flow->skb_mark = flow_skb_mark;
927 flow->nw_tos = flow_nw_tos;
931 compose_output_action(struct xlate_ctx *ctx, uint16_t ofp_port)
933 compose_output_action__(ctx, ofp_port, true);
937 tag_the_flow(struct xlate_ctx *ctx, struct rule_dpif *rule)
939 struct ofproto_dpif *ofproto = ctx->ofproto;
940 uint8_t table_id = ctx->table_id;
942 if (table_id > 0 && table_id < N_TABLES) {
943 struct table_dpif *table = &ofproto->tables[table_id];
944 if (table->other_table) {
945 ctx->xout->tags |= (rule && rule->tag
947 : rule_calculate_tag(&ctx->xin->flow,
948 &table->other_table->mask,
954 /* Common rule processing in one place to avoid duplicating code. */
955 static struct rule_dpif *
956 ctx_rule_hooks(struct xlate_ctx *ctx, struct rule_dpif *rule,
959 if (ctx->xin->resubmit_hook) {
960 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
962 if (rule == NULL && may_packet_in) {
964 * check if table configuration flags
965 * OFPTC_TABLE_MISS_CONTROLLER, default.
966 * OFPTC_TABLE_MISS_CONTINUE,
967 * OFPTC_TABLE_MISS_DROP
968 * When OF1.0, OFPTC_TABLE_MISS_CONTINUE is used. What to do?
970 rule = rule_dpif_miss_rule(ctx->ofproto, &ctx->xin->flow);
972 if (rule && ctx->xin->resubmit_stats) {
973 rule_credit_stats(rule, ctx->xin->resubmit_stats);
979 xlate_table_action(struct xlate_ctx *ctx,
980 uint16_t in_port, uint8_t table_id, bool may_packet_in)
982 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
983 struct rule_dpif *rule;
984 uint16_t old_in_port = ctx->xin->flow.in_port;
985 uint8_t old_table_id = ctx->table_id;
987 ctx->table_id = table_id;
989 /* Look up a flow with 'in_port' as the input port. */
990 ctx->xin->flow.in_port = in_port;
991 rule = rule_dpif_lookup_in_table(ctx->ofproto, &ctx->xin->flow,
992 &ctx->xout->wc, table_id);
994 tag_the_flow(ctx, rule);
996 /* Restore the original input port. Otherwise OFPP_NORMAL and
997 * OFPP_IN_PORT will have surprising behavior. */
998 ctx->xin->flow.in_port = old_in_port;
1000 rule = ctx_rule_hooks(ctx, rule, may_packet_in);
1003 struct rule_dpif *old_rule = ctx->rule;
1007 do_xlate_actions(rule->up.ofpacts, rule->up.ofpacts_len, ctx);
1008 ctx->rule = old_rule;
1012 ctx->table_id = old_table_id;
1014 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
1016 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
1017 MAX_RESUBMIT_RECURSION);
1018 ctx->max_resubmit_trigger = true;
1023 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
1024 const struct ofpact_resubmit *resubmit)
1029 in_port = resubmit->in_port;
1030 if (in_port == OFPP_IN_PORT) {
1031 in_port = ctx->xin->flow.in_port;
1034 table_id = resubmit->table_id;
1035 if (table_id == 255) {
1036 table_id = ctx->table_id;
1039 xlate_table_action(ctx, in_port, table_id, false);
1043 flood_packets(struct xlate_ctx *ctx, bool all)
1045 struct ofport_dpif *ofport;
1047 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
1048 uint16_t ofp_port = ofport->up.ofp_port;
1050 if (ofp_port == ctx->xin->flow.in_port) {
1055 compose_output_action__(ctx, ofp_port, false);
1056 } else if (!(ofport->up.pp.config & OFPUTIL_PC_NO_FLOOD)) {
1057 compose_output_action(ctx, ofp_port);
1061 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1065 execute_controller_action(struct xlate_ctx *ctx, int len,
1066 enum ofp_packet_in_reason reason,
1067 uint16_t controller_id)
1069 struct ofputil_packet_in pin;
1070 struct ofpbuf *packet;
1073 ovs_assert(!ctx->xout->slow || ctx->xout->slow == SLOW_CONTROLLER);
1074 ctx->xout->slow = SLOW_CONTROLLER;
1075 if (!ctx->xin->packet) {
1079 packet = ofpbuf_clone(ctx->xin->packet);
1081 key.skb_priority = 0;
1083 memset(&key.tunnel, 0, sizeof key.tunnel);
1085 commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1086 &ctx->xout->odp_actions);
1088 odp_execute_actions(NULL, packet, &key, ctx->xout->odp_actions.data,
1089 ctx->xout->odp_actions.size, NULL, NULL);
1091 pin.packet = packet->data;
1092 pin.packet_len = packet->size;
1093 pin.reason = reason;
1094 pin.controller_id = controller_id;
1095 pin.table_id = ctx->table_id;
1096 pin.cookie = ctx->rule ? ctx->rule->up.flow_cookie : 0;
1099 flow_get_metadata(&ctx->xin->flow, &pin.fmd);
1101 connmgr_send_packet_in(ctx->ofproto->up.connmgr, &pin);
1102 ofpbuf_delete(packet);
1106 compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1108 struct flow_wildcards *wc = &ctx->xout->wc;
1109 struct flow *flow = &ctx->xin->flow;
1111 ovs_assert(eth_type_mpls(eth_type));
1113 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1114 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1115 memset(&wc->masks.mpls_depth, 0xff, sizeof wc->masks.mpls_depth);
1117 if (flow->mpls_depth) {
1118 flow->mpls_lse &= ~htonl(MPLS_BOS_MASK);
1124 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1125 label = htonl(0x2); /* IPV6 Explicit Null. */
1127 label = htonl(0x0); /* IPV4 Explicit Null. */
1129 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
1130 ttl = flow->nw_ttl ? flow->nw_ttl : 0x40;
1131 flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
1132 flow->mpls_depth = 1;
1134 flow->dl_type = eth_type;
1138 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1140 struct flow_wildcards *wc = &ctx->xout->wc;
1141 struct flow *flow = &ctx->xin->flow;
1143 ovs_assert(eth_type_mpls(ctx->xin->flow.dl_type));
1144 ovs_assert(!eth_type_mpls(eth_type));
1146 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1147 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1148 memset(&wc->masks.mpls_depth, 0xff, sizeof wc->masks.mpls_depth);
1150 if (flow->mpls_depth) {
1152 flow->mpls_lse = htonl(0);
1153 if (!flow->mpls_depth) {
1154 flow->dl_type = eth_type;
1160 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
1162 struct flow *flow = &ctx->xin->flow;
1164 if (!is_ip_any(flow)) {
1168 if (flow->nw_ttl > 1) {
1174 for (i = 0; i < ids->n_controllers; i++) {
1175 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
1179 /* Stop processing for current table. */
1185 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
1187 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
1191 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl);
1196 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
1198 struct flow *flow = &ctx->xin->flow;
1199 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse);
1201 if (!eth_type_mpls(flow->dl_type)) {
1207 set_mpls_lse_ttl(&flow->mpls_lse, ttl);
1210 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
1212 /* Stop processing for current table. */
1218 xlate_output_action(struct xlate_ctx *ctx,
1219 uint16_t port, uint16_t max_len, bool may_packet_in)
1221 uint16_t prev_nf_output_iface = ctx->xout->nf_output_iface;
1223 ctx->xout->nf_output_iface = NF_OUT_DROP;
1227 compose_output_action(ctx, ctx->xin->flow.in_port);
1230 xlate_table_action(ctx, ctx->xin->flow.in_port, 0, may_packet_in);
1236 flood_packets(ctx, false);
1239 flood_packets(ctx, true);
1241 case OFPP_CONTROLLER:
1242 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
1248 if (port != ctx->xin->flow.in_port) {
1249 compose_output_action(ctx, port);
1251 xlate_report(ctx, "skipping output to input port");
1256 if (prev_nf_output_iface == NF_OUT_FLOOD) {
1257 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1258 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
1259 ctx->xout->nf_output_iface = prev_nf_output_iface;
1260 } else if (prev_nf_output_iface != NF_OUT_DROP &&
1261 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
1262 ctx->xout->nf_output_iface = NF_OUT_MULTI;
1267 xlate_output_reg_action(struct xlate_ctx *ctx,
1268 const struct ofpact_output_reg *or)
1270 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
1271 if (port <= UINT16_MAX) {
1272 union mf_subvalue value;
1274 memset(&value, 0xff, sizeof value);
1275 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
1276 xlate_output_action(ctx, port, or->max_len, false);
1281 xlate_enqueue_action(struct xlate_ctx *ctx,
1282 const struct ofpact_enqueue *enqueue)
1284 uint16_t ofp_port = enqueue->port;
1285 uint32_t queue_id = enqueue->queue;
1286 uint32_t flow_priority, priority;
1289 /* Translate queue to priority. */
1290 error = dpif_queue_to_priority(ctx->ofproto->backer->dpif,
1291 queue_id, &priority);
1293 /* Fall back to ordinary output action. */
1294 xlate_output_action(ctx, enqueue->port, 0, false);
1298 /* Check output port. */
1299 if (ofp_port == OFPP_IN_PORT) {
1300 ofp_port = ctx->xin->flow.in_port;
1301 } else if (ofp_port == ctx->xin->flow.in_port) {
1305 /* Add datapath actions. */
1306 flow_priority = ctx->xin->flow.skb_priority;
1307 ctx->xin->flow.skb_priority = priority;
1308 compose_output_action(ctx, ofp_port);
1309 ctx->xin->flow.skb_priority = flow_priority;
1311 /* Update NetFlow output port. */
1312 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
1313 ctx->xout->nf_output_iface = ofp_port;
1314 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
1315 ctx->xout->nf_output_iface = NF_OUT_MULTI;
1320 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
1322 uint32_t skb_priority;
1324 if (!dpif_queue_to_priority(ctx->ofproto->backer->dpif,
1325 queue_id, &skb_priority)) {
1326 ctx->xin->flow.skb_priority = skb_priority;
1328 /* Couldn't translate queue to a priority. Nothing to do. A warning
1329 * has already been logged. */
1334 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
1336 struct ofproto_dpif *ofproto = ofproto_;
1337 struct ofport_dpif *port;
1347 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
1350 port = get_ofp_port(ofproto, ofp_port);
1351 return port ? port->may_enable : false;
1356 xlate_bundle_action(struct xlate_ctx *ctx,
1357 const struct ofpact_bundle *bundle)
1361 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
1362 slave_enabled_cb, ctx->ofproto);
1363 if (bundle->dst.field) {
1364 nxm_reg_load(&bundle->dst, port, &ctx->xin->flow);
1366 xlate_output_action(ctx, port, 0, false);
1371 xlate_learn_action(struct xlate_ctx *ctx,
1372 const struct ofpact_learn *learn)
1374 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
1375 struct ofputil_flow_mod fm;
1376 uint64_t ofpacts_stub[1024 / 8];
1377 struct ofpbuf ofpacts;
1380 ctx->xout->has_learn = true;
1382 learn_mask(learn, &ctx->xout->wc);
1384 if (!ctx->xin->may_learn) {
1388 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1389 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
1391 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
1392 if (error && !VLOG_DROP_WARN(&rl)) {
1393 VLOG_WARN("learning action failed to modify flow table (%s)",
1394 ofperr_get_name(error));
1397 ofpbuf_uninit(&ofpacts);
1400 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
1401 * means "infinite". */
1403 reduce_timeout(uint16_t max, uint16_t *timeout)
1405 if (max && (!*timeout || *timeout > max)) {
1411 xlate_fin_timeout(struct xlate_ctx *ctx,
1412 const struct ofpact_fin_timeout *oft)
1414 if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
1415 struct rule_dpif *rule = ctx->rule;
1417 reduce_timeout(oft->fin_idle_timeout, &rule->up.idle_timeout);
1418 reduce_timeout(oft->fin_hard_timeout, &rule->up.hard_timeout);
1423 xlate_sample_action(struct xlate_ctx *ctx,
1424 const struct ofpact_sample *os)
1426 union user_action_cookie cookie;
1427 /* Scale the probability from 16-bit to 32-bit while representing
1428 * the same percentage. */
1429 uint32_t probability = (os->probability << 16) | os->probability;
1431 commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1432 &ctx->xout->odp_actions);
1434 compose_flow_sample_cookie(os->probability, os->collector_set_id,
1435 os->obs_domain_id, os->obs_point_id, &cookie);
1436 compose_sample_action(ctx->ofproto, &ctx->xout->odp_actions, &ctx->xin->flow,
1437 probability, &cookie, sizeof cookie.flow_sample);
1441 may_receive(const struct ofport_dpif *port, struct xlate_ctx *ctx)
1443 if (port->up.pp.config & (eth_addr_equals(ctx->xin->flow.dl_dst,
1445 ? OFPUTIL_PC_NO_RECV_STP
1446 : OFPUTIL_PC_NO_RECV)) {
1450 /* Only drop packets here if both forwarding and learning are
1451 * disabled. If just learning is enabled, we need to have
1452 * OFPP_NORMAL and the learning action have a look at the packet
1453 * before we can drop it. */
1454 if (!stp_forward_in_state(port->stp_state)
1455 && !stp_learn_in_state(port->stp_state)) {
1463 tunnel_ecn_ok(struct xlate_ctx *ctx)
1465 if (is_ip_any(&ctx->base_flow)
1466 && (ctx->xin->flow.tunnel.ip_tos & IP_ECN_MASK) == IP_ECN_CE) {
1467 if ((ctx->base_flow.nw_tos & IP_ECN_MASK) == IP_ECN_NOT_ECT) {
1468 VLOG_WARN_RL(&rl, "dropping tunnel packet marked ECN CE"
1469 " but is not ECN capable");
1472 /* Set the ECN CE value in the tunneled packet. */
1473 ctx->xin->flow.nw_tos |= IP_ECN_CE;
1481 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
1482 struct xlate_ctx *ctx)
1484 struct flow_wildcards *wc = &ctx->xout->wc;
1485 struct flow *flow = &ctx->xin->flow;
1486 bool was_evictable = true;
1487 const struct ofpact *a;
1490 /* Don't let the rule we're working on get evicted underneath us. */
1491 was_evictable = ctx->rule->up.evictable;
1492 ctx->rule->up.evictable = false;
1495 do_xlate_actions_again:
1496 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1497 struct ofpact_controller *controller;
1498 const struct ofpact_metadata *metadata;
1506 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
1507 ofpact_get_OUTPUT(a)->max_len, true);
1510 case OFPACT_CONTROLLER:
1511 controller = ofpact_get_CONTROLLER(a);
1512 execute_controller_action(ctx, controller->max_len,
1514 controller->controller_id);
1517 case OFPACT_ENQUEUE:
1518 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
1521 case OFPACT_SET_VLAN_VID:
1522 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
1523 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
1527 case OFPACT_SET_VLAN_PCP:
1528 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
1530 htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp << VLAN_PCP_SHIFT)
1534 case OFPACT_STRIP_VLAN:
1535 flow->vlan_tci = htons(0);
1538 case OFPACT_PUSH_VLAN:
1539 /* XXX 802.1AD(QinQ) */
1540 flow->vlan_tci = htons(VLAN_CFI);
1543 case OFPACT_SET_ETH_SRC:
1544 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1547 case OFPACT_SET_ETH_DST:
1548 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1551 case OFPACT_SET_IPV4_SRC:
1552 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1553 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1554 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1558 case OFPACT_SET_IPV4_DST:
1559 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1560 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1561 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
1565 case OFPACT_SET_IPV4_DSCP:
1566 /* OpenFlow 1.0 only supports IPv4. */
1567 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1568 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1569 flow->nw_tos &= ~IP_DSCP_MASK;
1570 flow->nw_tos |= ofpact_get_SET_IPV4_DSCP(a)->dscp;
1574 case OFPACT_SET_L4_SRC_PORT:
1575 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1576 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1577 if (is_ip_any(flow)) {
1578 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1582 case OFPACT_SET_L4_DST_PORT:
1583 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1584 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1585 if (is_ip_any(flow)) {
1586 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1590 case OFPACT_RESUBMIT:
1591 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
1594 case OFPACT_SET_TUNNEL:
1595 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
1598 case OFPACT_SET_QUEUE:
1599 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
1602 case OFPACT_POP_QUEUE:
1603 memset(&wc->masks.skb_priority, 0xff,
1604 sizeof wc->masks.skb_priority);
1606 flow->skb_priority = ctx->orig_skb_priority;
1609 case OFPACT_REG_MOVE:
1610 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
1613 case OFPACT_REG_LOAD:
1614 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow);
1617 case OFPACT_STACK_PUSH:
1618 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
1622 case OFPACT_STACK_POP:
1623 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, &ctx->stack);
1626 case OFPACT_PUSH_MPLS:
1627 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a)->ethertype);
1630 case OFPACT_POP_MPLS:
1631 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
1634 case OFPACT_SET_MPLS_TTL:
1635 if (compose_set_mpls_ttl_action(ctx,
1636 ofpact_get_SET_MPLS_TTL(a)->ttl)) {
1641 case OFPACT_DEC_MPLS_TTL:
1642 if (compose_dec_mpls_ttl_action(ctx)) {
1647 case OFPACT_DEC_TTL:
1648 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1649 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
1655 /* Nothing to do. */
1658 case OFPACT_MULTIPATH:
1659 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
1663 ctx->ofproto->has_bundle_action = true;
1664 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
1667 case OFPACT_OUTPUT_REG:
1668 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
1672 xlate_learn_action(ctx, ofpact_get_LEARN(a));
1679 case OFPACT_FIN_TIMEOUT:
1680 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1681 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1682 ctx->xout->has_fin_timeout = true;
1683 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
1686 case OFPACT_CLEAR_ACTIONS:
1688 * Nothing to do because writa-actions is not supported for now.
1689 * When writa-actions is supported, clear-actions also must
1690 * be supported at the same time.
1694 case OFPACT_WRITE_METADATA:
1695 metadata = ofpact_get_WRITE_METADATA(a);
1696 flow->metadata &= ~metadata->mask;
1697 flow->metadata |= metadata->metadata & metadata->mask;
1700 case OFPACT_GOTO_TABLE: {
1701 /* It is assumed that goto-table is the last action. */
1702 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
1703 struct rule_dpif *rule;
1705 ovs_assert(ctx->table_id < ogt->table_id);
1707 ctx->table_id = ogt->table_id;
1709 /* Look up a flow from the new table. */
1710 rule = rule_dpif_lookup_in_table(ctx->ofproto, flow, wc,
1713 tag_the_flow(ctx, rule);
1715 rule = ctx_rule_hooks(ctx, rule, true);
1719 ctx->rule->up.evictable = was_evictable;
1722 was_evictable = rule->up.evictable;
1723 rule->up.evictable = false;
1725 /* Tail recursion removal. */
1726 ofpacts = rule->up.ofpacts;
1727 ofpacts_len = rule->up.ofpacts_len;
1728 goto do_xlate_actions_again;
1734 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
1741 ctx->rule->up.evictable = was_evictable;
1746 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
1747 const struct flow *flow, struct rule_dpif *rule,
1748 uint8_t tcp_flags, const struct ofpbuf *packet)
1750 xin->ofproto = ofproto;
1752 xin->packet = packet;
1753 xin->may_learn = packet != NULL;
1755 xin->ofpacts = NULL;
1756 xin->ofpacts_len = 0;
1757 xin->tcp_flags = tcp_flags;
1758 xin->resubmit_hook = NULL;
1759 xin->report_hook = NULL;
1760 xin->resubmit_stats = NULL;
1764 xlate_out_uninit(struct xlate_out *xout)
1767 ofpbuf_uninit(&xout->odp_actions);
1771 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
1772 * into datapath actions, using 'ctx', and discards the datapath actions. */
1774 xlate_actions_for_side_effects(struct xlate_in *xin)
1776 struct xlate_out xout;
1778 xlate_actions(xin, &xout);
1779 xlate_out_uninit(&xout);
1783 xlate_report(struct xlate_ctx *ctx, const char *s)
1785 if (ctx->xin->report_hook) {
1786 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
1791 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
1794 dst->tags = src->tags;
1795 dst->slow = src->slow;
1796 dst->has_learn = src->has_learn;
1797 dst->has_normal = src->has_normal;
1798 dst->has_fin_timeout = src->has_fin_timeout;
1799 dst->nf_output_iface = src->nf_output_iface;
1800 dst->mirrors = src->mirrors;
1802 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
1803 sizeof dst->odp_actions_stub);
1804 ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
1805 src->odp_actions.size);
1809 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
1810 * into datapath actions in 'odp_actions', using 'ctx'. */
1812 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
1814 /* Normally false. Set to true if we ever hit MAX_RESUBMIT_RECURSION, so
1815 * that in the future we always keep a copy of the original flow for
1816 * tracing purposes. */
1817 static bool hit_resubmit_limit;
1819 struct flow_wildcards *wc = &xout->wc;
1820 struct flow *flow = &xin->flow;
1822 enum slow_path_reason special;
1823 const struct ofpact *ofpacts;
1824 struct ofport_dpif *in_port;
1825 struct flow orig_flow;
1826 struct xlate_ctx ctx;
1829 COVERAGE_INC(ofproto_dpif_xlate);
1831 /* Flow initialization rules:
1832 * - 'base_flow' must match the kernel's view of the packet at the
1833 * time that action processing starts. 'flow' represents any
1834 * transformations we wish to make through actions.
1835 * - By default 'base_flow' and 'flow' are the same since the input
1836 * packet matches the output before any actions are applied.
1837 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
1838 * of the received packet as seen by the kernel. If we later output
1839 * to another device without any modifications this will cause us to
1840 * insert a new tag since the original one was stripped off by the
1842 * - Tunnel metadata as received is retained in 'flow'. This allows
1843 * tunnel metadata matching also in later tables.
1844 * Since a kernel action for setting the tunnel metadata will only be
1845 * generated with actual tunnel output, changing the tunnel metadata
1846 * values in 'flow' (such as tun_id) will only have effect with a later
1847 * tunnel output action.
1848 * - Tunnel 'base_flow' is completely cleared since that is what the
1849 * kernel does. If we wish to maintain the original values an action
1850 * needs to be generated. */
1855 ctx.ofproto = xin->ofproto;
1856 ctx.rule = xin->rule;
1858 ctx.base_flow = *flow;
1859 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
1860 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
1862 flow_wildcards_init_catchall(wc);
1863 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
1865 if (tnl_port_should_receive(&ctx.xin->flow)) {
1866 memset(&wc->masks.tunnel, 0xff, sizeof wc->masks.tunnel);
1869 /* Disable most wildcarding for NetFlow. */
1870 if (xin->ofproto->netflow) {
1871 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1872 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1873 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1874 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
1875 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1876 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1877 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1878 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
1879 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
1884 ctx.xout->has_learn = false;
1885 ctx.xout->has_normal = false;
1886 ctx.xout->has_fin_timeout = false;
1887 ctx.xout->nf_output_iface = NF_OUT_DROP;
1888 ctx.xout->mirrors = 0;
1890 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
1891 sizeof ctx.xout->odp_actions_stub);
1892 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
1895 ctx.max_resubmit_trigger = false;
1896 ctx.orig_skb_priority = flow->skb_priority;
1901 ofpacts = xin->ofpacts;
1902 ofpacts_len = xin->ofpacts_len;
1903 } else if (xin->rule) {
1904 ofpacts = xin->rule->up.ofpacts;
1905 ofpacts_len = xin->rule->up.ofpacts_len;
1910 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
1912 if (ctx.ofproto->has_mirrors || hit_resubmit_limit) {
1913 /* Do this conditionally because the copy is expensive enough that it
1914 * shows up in profiles. */
1918 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
1919 switch (ctx.ofproto->up.frag_handling) {
1920 case OFPC_FRAG_NORMAL:
1921 /* We must pretend that transport ports are unavailable. */
1922 flow->tp_src = ctx.base_flow.tp_src = htons(0);
1923 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
1926 case OFPC_FRAG_DROP:
1929 case OFPC_FRAG_REASM:
1932 case OFPC_FRAG_NX_MATCH:
1933 /* Nothing to do. */
1936 case OFPC_INVALID_TTL_TO_CONTROLLER:
1941 in_port = get_ofp_port(ctx.ofproto, flow->in_port);
1942 special = process_special(ctx.ofproto, flow, in_port, ctx.xin->packet);
1944 ctx.xout->slow = special;
1946 static struct vlog_rate_limit trace_rl = VLOG_RATE_LIMIT_INIT(1, 1);
1947 size_t sample_actions_len;
1948 uint32_t local_odp_port;
1951 != vsp_realdev_to_vlandev(ctx.ofproto, flow->in_port,
1953 ctx.base_flow.vlan_tci = 0;
1956 add_sflow_action(&ctx);
1957 add_ipfix_action(&ctx);
1958 sample_actions_len = ctx.xout->odp_actions.size;
1960 if (tunnel_ecn_ok(&ctx) && (!in_port || may_receive(in_port, &ctx))) {
1961 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
1963 /* We've let OFPP_NORMAL and the learning action look at the
1964 * packet, so drop it now if forwarding is disabled. */
1965 if (in_port && !stp_forward_in_state(in_port->stp_state)) {
1966 ctx.xout->odp_actions.size = sample_actions_len;
1970 if (ctx.max_resubmit_trigger && !ctx.xin->resubmit_hook) {
1971 if (!hit_resubmit_limit) {
1972 /* We didn't record the original flow. Make sure we do from
1974 hit_resubmit_limit = true;
1975 } else if (!VLOG_DROP_ERR(&trace_rl)) {
1976 struct ds ds = DS_EMPTY_INITIALIZER;
1978 ofproto_trace(ctx.ofproto, &orig_flow, ctx.xin->packet, &ds);
1979 VLOG_ERR("Trace triggered by excessive resubmit "
1980 "recursion:\n%s", ds_cstr(&ds));
1985 local_odp_port = ofp_port_to_odp_port(ctx.ofproto, OFPP_LOCAL);
1986 if (!connmgr_must_output_local(ctx.ofproto->up.connmgr, flow,
1988 ctx.xout->odp_actions.data,
1989 ctx.xout->odp_actions.size)) {
1990 compose_output_action(&ctx, OFPP_LOCAL);
1992 if (ctx.ofproto->has_mirrors) {
1993 add_mirror_actions(&ctx, &orig_flow);
1995 fix_sflow_action(&ctx);
1998 ofpbuf_uninit(&ctx.stack);
2000 /* Clear the metadata and register wildcard masks, because we won't
2001 * use non-header fields as part of the cache. */
2002 memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
2003 memset(&wc->masks.regs, 0, sizeof wc->masks.regs);