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"
33 #include "mac-learning.h"
34 #include "meta-flow.h"
35 #include "multipath.h"
36 #include "netdev-vport.h"
39 #include "odp-execute.h"
40 #include "ofp-actions.h"
41 #include "ofproto/ofproto-dpif-ipfix.h"
42 #include "ofproto/ofproto-dpif-mirror.h"
43 #include "ofproto/ofproto-dpif-sflow.h"
44 #include "ofproto/ofproto-dpif.h"
48 COVERAGE_DEFINE(xlate_actions);
50 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
52 /* Maximum depth of flow table recursion (due to resubmit actions) in a
53 * flow translation. */
54 #define MAX_RESUBMIT_RECURSION 64
57 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
58 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
60 struct list xbundles; /* Owned xbundles. */
61 struct hmap xports; /* Indexed by ofp_port. */
63 char *name; /* Name used in log messages. */
64 struct mac_learning *ml; /* Mac learning handle. */
65 struct mbridge *mbridge; /* Mirroring. */
66 struct dpif_sflow *sflow; /* SFlow handle, or null. */
67 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
69 enum ofp_config_flags frag; /* Fragmentation handling. */
70 bool has_stp; /* Bridge runs stp? */
71 bool has_netflow; /* Bridge runs netflow? */
72 bool has_in_band; /* Bridge has in band control? */
73 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
77 struct hmap_node hmap_node; /* In global 'xbundles' map. */
78 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
80 struct list list_node; /* In parent 'xbridges' list. */
81 struct xbridge *xbridge; /* Parent xbridge. */
83 struct list xports; /* Contains "struct xport"s. */
85 char *name; /* Name used in log messages. */
86 struct bond *bond; /* Nonnull iff more than one port. */
87 struct lacp *lacp; /* LACP handle or null. */
89 enum port_vlan_mode vlan_mode; /* VLAN mode. */
90 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
91 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
92 * NULL if all VLANs are trunked. */
93 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
94 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
98 struct hmap_node hmap_node; /* Node in global 'xports' map. */
99 struct ofport_dpif *ofport; /* Key in global 'xports map. */
101 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
102 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
104 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
106 struct list bundle_node; /* In parent xbundle (if it exists). */
107 struct xbundle *xbundle; /* Parent xbundle or null. */
109 struct netdev *netdev; /* 'ofport''s netdev. */
111 struct xbridge *xbridge; /* Parent bridge. */
112 struct xport *peer; /* Patch port peer or null. */
114 enum ofputil_port_config config; /* OpenFlow port configuration. */
115 enum stp_state stp_state; /* STP_DISABLED if STP not in use. */
117 bool may_enable; /* May be enabled in bonds. */
118 bool is_tunnel; /* Is a tunnel port. */
120 struct cfm *cfm; /* CFM handle or null. */
121 struct bfd *bfd; /* BFD handle or null. */
125 struct xlate_in *xin;
126 struct xlate_out *xout;
128 const struct xbridge *xbridge;
130 /* Flow at the last commit. */
131 struct flow base_flow;
133 /* Tunnel IP destination address as received. This is stored separately
134 * as the base_flow.tunnel is cleared on init to reflect the datapath
135 * behavior. Used to make sure not to send tunneled output to ourselves,
136 * which might lead to an infinite loop. This could happen easily
137 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
138 * actually set the tun_dst field. */
139 ovs_be32 orig_tunnel_ip_dst;
141 /* Stack for the push and pop actions. Each stack element is of type
142 * "union mf_subvalue". */
143 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
146 /* The rule that we are currently translating, or NULL. */
147 struct rule_dpif *rule;
149 int recurse; /* Recursion level, via xlate_table_action. */
150 bool max_resubmit_trigger; /* Recursed too deeply during translation. */
151 uint32_t orig_skb_priority; /* Priority when packet arrived. */
152 uint8_t table_id; /* OpenFlow table ID where flow was found. */
153 uint32_t sflow_n_outputs; /* Number of output ports. */
154 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
155 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
156 bool exit; /* No further actions should be processed. */
159 /* A controller may use OFPP_NONE as the ingress port to indicate that
160 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
161 * when an input bundle is needed for validation (e.g., mirroring or
162 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
163 * any 'port' structs, so care must be taken when dealing with it.
164 * The bundle's name and vlan mode are initialized in lookup_input_bundle() */
165 static struct xbundle ofpp_none_bundle;
167 static struct hmap xbridges = HMAP_INITIALIZER(&xbridges);
168 static struct hmap xbundles = HMAP_INITIALIZER(&xbundles);
169 static struct hmap xports = HMAP_INITIALIZER(&xports);
171 static bool may_receive(const struct xport *, struct xlate_ctx *);
172 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
174 static void xlate_normal(struct xlate_ctx *);
175 static void xlate_report(struct xlate_ctx *, const char *);
176 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
177 uint8_t table_id, bool may_packet_in);
178 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
179 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
180 static void output_normal(struct xlate_ctx *, const struct xbundle *,
182 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
184 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
186 static struct xbridge *xbridge_lookup(const struct ofproto_dpif *);
187 static struct xbundle *xbundle_lookup(const struct ofbundle *);
188 static struct xport *xport_lookup(struct ofport_dpif *);
189 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
192 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
193 const struct mac_learning *ml, const struct mbridge *mbridge,
194 const struct dpif_sflow *sflow,
195 const struct dpif_ipfix *ipfix, enum ofp_config_flags frag,
196 bool forward_bpdu, bool has_in_band, bool has_netflow,
199 struct xbridge *xbridge = xbridge_lookup(ofproto);
202 xbridge = xzalloc(sizeof *xbridge);
203 xbridge->ofproto = ofproto;
205 hmap_insert(&xbridges, &xbridge->hmap_node, hash_pointer(ofproto, 0));
206 hmap_init(&xbridge->xports);
207 list_init(&xbridge->xbundles);
210 if (xbridge->ml != ml) {
211 mac_learning_unref(xbridge->ml);
212 xbridge->ml = mac_learning_ref(ml);
215 if (xbridge->mbridge != mbridge) {
216 mbridge_unref(xbridge->mbridge);
217 xbridge->mbridge = mbridge_ref(mbridge);
220 if (xbridge->sflow != sflow) {
221 dpif_sflow_unref(xbridge->sflow);
222 xbridge->sflow = dpif_sflow_ref(sflow);
225 if (xbridge->ipfix != ipfix) {
226 dpif_ipfix_unref(xbridge->ipfix);
227 xbridge->ipfix = dpif_ipfix_ref(ipfix);
231 xbridge->name = xstrdup(name);
233 xbridge->forward_bpdu = forward_bpdu;
234 xbridge->has_in_band = has_in_band;
235 xbridge->has_netflow = has_netflow;
236 xbridge->has_stp = has_stp;
237 xbridge->frag = frag;
241 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
243 struct xbridge *xbridge = xbridge_lookup(ofproto);
244 struct xbundle *xbundle, *next_xbundle;
245 struct xport *xport, *next_xport;
251 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
252 xlate_ofport_remove(xport->ofport);
255 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
256 xlate_bundle_remove(xbundle->ofbundle);
259 hmap_remove(&xbridges, &xbridge->hmap_node);
265 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
266 const char *name, enum port_vlan_mode vlan_mode, int vlan,
267 unsigned long *trunks, bool use_priority_tags,
268 const struct bond *bond, const struct lacp *lacp,
271 struct xbundle *xbundle = xbundle_lookup(ofbundle);
274 xbundle = xzalloc(sizeof *xbundle);
275 xbundle->ofbundle = ofbundle;
276 xbundle->xbridge = xbridge_lookup(ofproto);
278 hmap_insert(&xbundles, &xbundle->hmap_node, hash_pointer(ofbundle, 0));
279 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
280 list_init(&xbundle->xports);
283 ovs_assert(xbundle->xbridge);
286 xbundle->name = xstrdup(name);
288 xbundle->vlan_mode = vlan_mode;
289 xbundle->vlan = vlan;
290 xbundle->trunks = trunks;
291 xbundle->use_priority_tags = use_priority_tags;
292 xbundle->floodable = floodable;
294 if (xbundle->bond != bond) {
295 bond_unref(xbundle->bond);
296 xbundle->bond = bond_ref(bond);
299 if (xbundle->lacp != lacp) {
300 lacp_unref(xbundle->lacp);
301 xbundle->lacp = lacp_ref(lacp);
306 xlate_bundle_remove(struct ofbundle *ofbundle)
308 struct xbundle *xbundle = xbundle_lookup(ofbundle);
309 struct xport *xport, *next;
315 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
316 list_remove(&xport->bundle_node);
317 xport->xbundle = NULL;
320 hmap_remove(&xbundles, &xbundle->hmap_node);
321 list_remove(&xbundle->list_node);
322 bond_unref(xbundle->bond);
323 lacp_unref(xbundle->lacp);
329 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
330 struct ofport_dpif *ofport, ofp_port_t ofp_port,
331 odp_port_t odp_port, const struct netdev *netdev,
332 const struct cfm *cfm, const struct bfd *bfd,
333 struct ofport_dpif *peer, enum ofputil_port_config config,
334 enum stp_state stp_state, bool is_tunnel, bool may_enable)
336 struct xport *xport = xport_lookup(ofport);
339 xport = xzalloc(sizeof *xport);
340 xport->ofport = ofport;
341 xport->xbridge = xbridge_lookup(ofproto);
342 xport->ofp_port = ofp_port;
344 hmap_insert(&xports, &xport->hmap_node, hash_pointer(ofport, 0));
345 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
346 hash_ofp_port(xport->ofp_port));
349 ovs_assert(xport->ofp_port == ofp_port);
351 xport->config = config;
352 xport->stp_state = stp_state;
353 xport->is_tunnel = is_tunnel;
354 xport->may_enable = may_enable;
355 xport->odp_port = odp_port;
357 if (xport->netdev != netdev) {
358 netdev_close(xport->netdev);
359 xport->netdev = netdev_ref(netdev);
362 if (xport->cfm != cfm) {
363 cfm_unref(xport->cfm);
364 xport->cfm = cfm_ref(cfm);
367 if (xport->bfd != bfd) {
368 bfd_unref(xport->bfd);
369 xport->bfd = bfd_ref(bfd);
373 xport->peer->peer = NULL;
375 xport->peer = peer ? xport_lookup(peer) : NULL;
377 xport->peer->peer = xport;
380 if (xport->xbundle) {
381 list_remove(&xport->bundle_node);
383 xport->xbundle = ofbundle ? xbundle_lookup(ofbundle) : NULL;
384 if (xport->xbundle) {
385 list_insert(&xport->xbundle->xports, &xport->bundle_node);
390 xlate_ofport_remove(struct ofport_dpif *ofport)
392 struct xport *xport = xport_lookup(ofport);
399 xport->peer->peer = NULL;
403 if (xport->xbundle) {
404 list_remove(&xport->bundle_node);
407 hmap_remove(&xports, &xport->hmap_node);
408 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
410 netdev_close(xport->netdev);
411 cfm_unref(xport->cfm);
412 bfd_unref(xport->bfd);
416 static struct xbridge *
417 xbridge_lookup(const struct ofproto_dpif *ofproto)
419 struct xbridge *xbridge;
421 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
423 if (xbridge->ofproto == ofproto) {
430 static struct xbundle *
431 xbundle_lookup(const struct ofbundle *ofbundle)
433 struct xbundle *xbundle;
435 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
437 if (xbundle->ofbundle == ofbundle) {
444 static struct xport *
445 xport_lookup(struct ofport_dpif *ofport)
449 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
451 if (xport->ofport == ofport) {
458 static struct xport *
459 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
463 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
465 if (xport->ofp_port == ofp_port) {
473 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
475 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
476 return xport ? xport->odp_port : ODPP_NONE;
480 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
482 return (bundle->vlan_mode != PORT_VLAN_ACCESS
483 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
487 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
489 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
493 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
495 return xbundle != &ofpp_none_bundle
496 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
501 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
503 return xbundle != &ofpp_none_bundle
504 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
509 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
511 return xbundle != &ofpp_none_bundle
512 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
516 static struct xbundle *
517 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
518 bool warn, struct xport **in_xportp)
522 /* Find the port and bundle for the received packet. */
523 xport = get_ofp_port(xbridge, in_port);
527 if (xport && xport->xbundle) {
528 return xport->xbundle;
531 /* Special-case OFPP_NONE, which a controller may use as the ingress
532 * port for traffic that it is sourcing. */
533 if (in_port == OFPP_NONE) {
534 ofpp_none_bundle.name = "OFPP_NONE";
535 ofpp_none_bundle.vlan_mode = PORT_VLAN_TRUNK;
536 return &ofpp_none_bundle;
539 /* Odd. A few possible reasons here:
541 * - We deleted a port but there are still a few packets queued up
544 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
545 * we don't know about.
547 * - The ofproto client didn't configure the port as part of a bundle.
548 * This is particularly likely to happen if a packet was received on the
549 * port after it was created, but before the client had a chance to
550 * configure its bundle.
553 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
555 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
556 "port %"PRIu16, xbridge->name, in_port);
562 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
564 const struct xbridge *xbridge = ctx->xbridge;
565 mirror_mask_t mirrors;
566 struct xbundle *in_xbundle;
570 mirrors = ctx->xout->mirrors;
571 ctx->xout->mirrors = 0;
573 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
574 ctx->xin->packet != NULL, NULL);
578 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
580 /* Drop frames on bundles reserved for mirroring. */
581 if (xbundle_mirror_out(xbridge, in_xbundle)) {
582 if (ctx->xin->packet != NULL) {
583 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
584 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
585 "%s, which is reserved exclusively for mirroring",
586 ctx->xbridge->name, in_xbundle->name);
588 ofpbuf_clear(&ctx->xout->odp_actions);
593 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
594 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
597 vlan = input_vid_to_vlan(in_xbundle, vid);
603 /* Restore the original packet before adding the mirror actions. */
604 ctx->xin->flow = *orig_flow;
607 mirror_mask_t dup_mirrors;
608 struct ofbundle *out;
609 unsigned long *vlans;
614 has_mirror = mirror_get(xbridge->mbridge, mirror_mask_ffs(mirrors) - 1,
615 &vlans, &dup_mirrors, &out, &out_vlan);
616 ovs_assert(has_mirror);
619 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
621 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
624 if (!vlan_mirrored) {
625 mirrors = zero_rightmost_1bit(mirrors);
629 mirrors &= ~dup_mirrors;
630 ctx->xout->mirrors |= dup_mirrors;
632 struct xbundle *out_xbundle = xbundle_lookup(out);
634 output_normal(ctx, out_xbundle, vlan);
636 } else if (vlan != out_vlan
637 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
638 struct xbundle *xbundle;
640 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
641 if (xbundle_includes_vlan(xbundle, out_vlan)
642 && !xbundle_mirror_out(xbridge, xbundle)) {
643 output_normal(ctx, xbundle, out_vlan);
650 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
651 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
652 * the bundle on which the packet was received, returns the VLAN to which the
655 * Both 'vid' and the return value are in the range 0...4095. */
657 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
659 switch (in_xbundle->vlan_mode) {
660 case PORT_VLAN_ACCESS:
661 return in_xbundle->vlan;
664 case PORT_VLAN_TRUNK:
667 case PORT_VLAN_NATIVE_UNTAGGED:
668 case PORT_VLAN_NATIVE_TAGGED:
669 return vid ? vid : in_xbundle->vlan;
676 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
677 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
680 * 'vid' should be the VID obtained from the 802.1Q header that was received as
681 * part of a packet (specify 0 if there was no 802.1Q header), in the range
684 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
686 /* Allow any VID on the OFPP_NONE port. */
687 if (in_xbundle == &ofpp_none_bundle) {
691 switch (in_xbundle->vlan_mode) {
692 case PORT_VLAN_ACCESS:
695 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
696 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
697 "packet received on port %s configured as VLAN "
698 "%"PRIu16" access port", vid, in_xbundle->name,
705 case PORT_VLAN_NATIVE_UNTAGGED:
706 case PORT_VLAN_NATIVE_TAGGED:
708 /* Port must always carry its native VLAN. */
712 case PORT_VLAN_TRUNK:
713 if (!xbundle_includes_vlan(in_xbundle, vid)) {
715 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
716 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
717 "received on port %s not configured for trunking "
718 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
730 /* Given 'vlan', the VLAN that a packet belongs to, and
731 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
732 * that should be included in the 802.1Q header. (If the return value is 0,
733 * then the 802.1Q header should only be included in the packet if there is a
736 * Both 'vlan' and the return value are in the range 0...4095. */
738 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
740 switch (out_xbundle->vlan_mode) {
741 case PORT_VLAN_ACCESS:
744 case PORT_VLAN_TRUNK:
745 case PORT_VLAN_NATIVE_TAGGED:
748 case PORT_VLAN_NATIVE_UNTAGGED:
749 return vlan == out_xbundle->vlan ? 0 : vlan;
757 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
760 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
762 ovs_be16 tci, old_tci;
765 vid = output_vlan_to_vid(out_xbundle, vlan);
766 if (list_is_empty(&out_xbundle->xports)) {
767 /* Partially configured bundle with no slaves. Drop the packet. */
769 } else if (!out_xbundle->bond) {
770 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
773 struct ofport_dpif *ofport;
775 ofport = bond_choose_output_slave(out_xbundle->bond, &ctx->xin->flow,
778 xport = ofport ? xport_lookup(ofport) : NULL;
781 /* No slaves enabled, so drop packet. */
788 if (tci || out_xbundle->use_priority_tags) {
789 tci |= *flow_tci & htons(VLAN_PCP_MASK);
791 tci |= htons(VLAN_CFI);
796 compose_output_action(ctx, xport->ofp_port);
800 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
801 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
802 * indicate this; newer upstream kernels use gratuitous ARP requests. */
804 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
806 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
810 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
811 if (!eth_addr_is_broadcast(flow->dl_dst)) {
815 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
816 if (flow->nw_proto == ARP_OP_REPLY) {
818 } else if (flow->nw_proto == ARP_OP_REQUEST) {
819 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
820 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
822 return flow->nw_src == flow->nw_dst;
829 update_learning_table(const struct xbridge *xbridge,
830 const struct flow *flow, struct flow_wildcards *wc,
831 int vlan, struct xbundle *in_xbundle)
833 struct mac_entry *mac;
835 /* Don't learn the OFPP_NONE port. */
836 if (in_xbundle == &ofpp_none_bundle) {
840 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
844 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
845 if (is_gratuitous_arp(flow, wc)) {
846 /* We don't want to learn from gratuitous ARP packets that are
847 * reflected back over bond slaves so we lock the learning table. */
848 if (!in_xbundle->bond) {
849 mac_entry_set_grat_arp_lock(mac);
850 } else if (mac_entry_is_grat_arp_locked(mac)) {
855 if (mac_entry_is_new(mac) || mac->port.p != in_xbundle->ofbundle) {
856 /* The log messages here could actually be useful in debugging,
857 * so keep the rate limit relatively high. */
858 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
859 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
860 "on port %s in VLAN %d",
861 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
862 in_xbundle->name, vlan);
864 mac->port.p = in_xbundle->ofbundle;
865 mac_learning_changed(xbridge->ml, mac);
869 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
870 * dropped. Returns true if they may be forwarded, false if they should be
873 * 'in_port' must be the xport that corresponds to flow->in_port.
874 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
876 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
877 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
878 * checked by input_vid_is_valid().
880 * May also add tags to '*tags', although the current implementation only does
881 * so in one special case.
884 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
887 struct xbundle *in_xbundle = in_port->xbundle;
888 const struct xbridge *xbridge = ctx->xbridge;
889 struct flow *flow = &ctx->xin->flow;
891 /* Drop frames for reserved multicast addresses
892 * only if forward_bpdu option is absent. */
893 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
894 xlate_report(ctx, "packet has reserved destination MAC, dropping");
898 if (in_xbundle->bond) {
899 struct mac_entry *mac;
901 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
902 flow->dl_dst, &ctx->xout->tags)) {
907 xlate_report(ctx, "bonding refused admissibility, dropping");
910 case BV_DROP_IF_MOVED:
911 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan, NULL);
912 if (mac && mac->port.p != in_xbundle->ofbundle &&
913 (!is_gratuitous_arp(flow, &ctx->xout->wc)
914 || mac_entry_is_grat_arp_locked(mac))) {
915 xlate_report(ctx, "SLB bond thinks this packet looped back, "
927 xlate_normal(struct xlate_ctx *ctx)
929 struct flow_wildcards *wc = &ctx->xout->wc;
930 struct flow *flow = &ctx->xin->flow;
931 struct xbundle *in_xbundle;
932 struct xport *in_port;
933 struct mac_entry *mac;
937 ctx->xout->has_normal = true;
939 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
940 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
941 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
943 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
944 ctx->xin->packet != NULL, &in_port);
946 xlate_report(ctx, "no input bundle, dropping");
950 /* Drop malformed frames. */
951 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
952 !(flow->vlan_tci & htons(VLAN_CFI))) {
953 if (ctx->xin->packet != NULL) {
954 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
955 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
956 "VLAN tag received on port %s",
957 ctx->xbridge->name, in_xbundle->name);
959 xlate_report(ctx, "partial VLAN tag, dropping");
963 /* Drop frames on bundles reserved for mirroring. */
964 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
965 if (ctx->xin->packet != NULL) {
966 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
967 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
968 "%s, which is reserved exclusively for mirroring",
969 ctx->xbridge->name, in_xbundle->name);
971 xlate_report(ctx, "input port is mirror output port, dropping");
976 vid = vlan_tci_to_vid(flow->vlan_tci);
977 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
978 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
981 vlan = input_vid_to_vlan(in_xbundle, vid);
983 /* Check other admissibility requirements. */
984 if (in_port && !is_admissible(ctx, in_port, vlan)) {
988 /* Learn source MAC. */
989 if (ctx->xin->may_learn) {
990 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
993 /* Determine output bundle. */
994 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan,
997 struct xbundle *mac_xbundle = xbundle_lookup(mac->port.p);
998 if (mac_xbundle && mac_xbundle != in_xbundle) {
999 xlate_report(ctx, "forwarding to learned port");
1000 output_normal(ctx, mac_xbundle, vlan);
1001 } else if (!mac_xbundle) {
1002 xlate_report(ctx, "learned port is unknown, dropping");
1004 xlate_report(ctx, "learned port is input port, dropping");
1007 struct xbundle *xbundle;
1009 xlate_report(ctx, "no learned MAC for destination, flooding");
1010 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1011 if (xbundle != in_xbundle
1012 && xbundle_includes_vlan(xbundle, vlan)
1013 && xbundle->floodable
1014 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1015 output_normal(ctx, xbundle, vlan);
1018 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1022 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1023 * the number of packets out of UINT32_MAX to sample. The given
1024 * cookie is passed back in the callback for each sampled packet.
1027 compose_sample_action(const struct xbridge *xbridge,
1028 struct ofpbuf *odp_actions,
1029 const struct flow *flow,
1030 const uint32_t probability,
1031 const union user_action_cookie *cookie,
1032 const size_t cookie_size)
1034 size_t sample_offset, actions_offset;
1037 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
1039 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
1041 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
1042 cookie_offset = put_userspace_action(xbridge->ofproto, odp_actions, flow,
1043 cookie, cookie_size);
1045 nl_msg_end_nested(odp_actions, actions_offset);
1046 nl_msg_end_nested(odp_actions, sample_offset);
1047 return cookie_offset;
1051 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
1052 odp_port_t odp_port, unsigned int n_outputs,
1053 union user_action_cookie *cookie)
1057 cookie->type = USER_ACTION_COOKIE_SFLOW;
1058 cookie->sflow.vlan_tci = vlan_tci;
1060 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1061 * port information") for the interpretation of cookie->output. */
1062 switch (n_outputs) {
1064 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1065 cookie->sflow.output = 0x40000000 | 256;
1069 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
1071 cookie->sflow.output = ifindex;
1076 /* 0x80000000 means "multiple output ports. */
1077 cookie->sflow.output = 0x80000000 | n_outputs;
1082 /* Compose SAMPLE action for sFlow bridge sampling. */
1084 compose_sflow_action(const struct xbridge *xbridge,
1085 struct ofpbuf *odp_actions,
1086 const struct flow *flow,
1087 odp_port_t odp_port)
1089 uint32_t probability;
1090 union user_action_cookie cookie;
1092 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
1096 probability = dpif_sflow_get_probability(xbridge->sflow);
1097 compose_sflow_cookie(xbridge, htons(0), odp_port,
1098 odp_port == ODPP_NONE ? 0 : 1, &cookie);
1100 return compose_sample_action(xbridge, odp_actions, flow, probability,
1101 &cookie, sizeof cookie.sflow);
1105 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
1106 uint32_t obs_domain_id, uint32_t obs_point_id,
1107 union user_action_cookie *cookie)
1109 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
1110 cookie->flow_sample.probability = probability;
1111 cookie->flow_sample.collector_set_id = collector_set_id;
1112 cookie->flow_sample.obs_domain_id = obs_domain_id;
1113 cookie->flow_sample.obs_point_id = obs_point_id;
1117 compose_ipfix_cookie(union user_action_cookie *cookie)
1119 cookie->type = USER_ACTION_COOKIE_IPFIX;
1122 /* Compose SAMPLE action for IPFIX bridge sampling. */
1124 compose_ipfix_action(const struct xbridge *xbridge,
1125 struct ofpbuf *odp_actions,
1126 const struct flow *flow)
1128 uint32_t probability;
1129 union user_action_cookie cookie;
1131 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
1135 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
1136 compose_ipfix_cookie(&cookie);
1138 compose_sample_action(xbridge, odp_actions, flow, probability,
1139 &cookie, sizeof cookie.ipfix);
1142 /* SAMPLE action for sFlow must be first action in any given list of
1143 * actions. At this point we do not have all information required to
1144 * build it. So try to build sample action as complete as possible. */
1146 add_sflow_action(struct xlate_ctx *ctx)
1148 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
1149 &ctx->xout->odp_actions,
1150 &ctx->xin->flow, ODPP_NONE);
1151 ctx->sflow_odp_port = 0;
1152 ctx->sflow_n_outputs = 0;
1155 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
1156 * of actions, eventually after the SAMPLE action for sFlow. */
1158 add_ipfix_action(struct xlate_ctx *ctx)
1160 compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions,
1164 /* Fix SAMPLE action according to data collected while composing ODP actions.
1165 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
1166 * USERSPACE action's user-cookie which is required for sflow. */
1168 fix_sflow_action(struct xlate_ctx *ctx)
1170 const struct flow *base = &ctx->base_flow;
1171 union user_action_cookie *cookie;
1173 if (!ctx->user_cookie_offset) {
1177 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
1178 sizeof cookie->sflow);
1179 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
1181 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
1182 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
1185 static enum slow_path_reason
1186 process_special(struct xlate_ctx *ctx, const struct flow *flow,
1187 const struct xport *xport, const struct ofpbuf *packet)
1189 struct flow_wildcards *wc = &ctx->xout->wc;
1190 const struct xbridge *xbridge = ctx->xbridge;
1194 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
1196 cfm_process_heartbeat(xport->cfm, packet);
1199 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
1201 bfd_process_packet(xport->bfd, flow, packet);
1204 } else if (xport->xbundle && xport->xbundle->lacp
1205 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1207 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
1210 } else if (xbridge->has_stp && stp_should_process_flow(flow, wc)) {
1212 stp_process_packet(xport->ofport, packet);
1221 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
1224 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1225 struct flow_wildcards *wc = &ctx->xout->wc;
1226 struct flow *flow = &ctx->xin->flow;
1227 ovs_be16 flow_vlan_tci;
1228 uint32_t flow_skb_mark;
1229 uint8_t flow_nw_tos;
1230 odp_port_t out_port, odp_port;
1233 /* If 'struct flow' gets additional metadata, we'll need to zero it out
1234 * before traversing a patch port. */
1235 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 20);
1238 xlate_report(ctx, "Nonexistent output port");
1240 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
1241 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
1243 } else if (check_stp && !stp_forward_in_state(xport->stp_state)) {
1244 xlate_report(ctx, "STP not in forwarding state, skipping output");
1248 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
1249 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
1254 const struct xport *peer = xport->peer;
1255 struct flow old_flow = ctx->xin->flow;
1256 enum slow_path_reason special;
1258 ctx->xbridge = peer->xbridge;
1259 flow->in_port.ofp_port = peer->ofp_port;
1260 flow->metadata = htonll(0);
1261 memset(&flow->tunnel, 0, sizeof flow->tunnel);
1262 memset(flow->regs, 0, sizeof flow->regs);
1264 special = process_special(ctx, &ctx->xin->flow, peer,
1267 ctx->xout->slow = special;
1268 } else if (may_receive(peer, ctx)) {
1269 if (stp_forward_in_state(peer->stp_state)) {
1270 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1272 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
1273 * learning action look at the packet, then drop it. */
1274 struct flow old_base_flow = ctx->base_flow;
1275 size_t old_size = ctx->xout->odp_actions.size;
1276 mirror_mask_t old_mirrors = ctx->xout->mirrors;
1277 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1278 ctx->xout->mirrors = old_mirrors;
1279 ctx->base_flow = old_base_flow;
1280 ctx->xout->odp_actions.size = old_size;
1284 ctx->xin->flow = old_flow;
1285 ctx->xbridge = xport->xbundle->xbridge;
1287 if (ctx->xin->resubmit_stats) {
1288 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1289 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
1295 flow_vlan_tci = flow->vlan_tci;
1296 flow_skb_mark = flow->skb_mark;
1297 flow_nw_tos = flow->nw_tos;
1299 if (ofproto_dpif_dscp_from_priority(xport->ofport, flow->skb_priority,
1301 wc->masks.nw_tos |= IP_ECN_MASK;
1302 flow->nw_tos &= ~IP_DSCP_MASK;
1303 flow->nw_tos |= dscp;
1306 if (xport->is_tunnel) {
1307 /* Save tunnel metadata so that changes made due to
1308 * the Logical (tunnel) Port are not visible for any further
1309 * matches, while explicit set actions on tunnel metadata are.
1311 struct flow_tnl flow_tnl = flow->tunnel;
1312 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
1313 if (odp_port == ODPP_NONE) {
1314 xlate_report(ctx, "Tunneling decided against output");
1315 goto out; /* restore flow_nw_tos */
1317 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
1318 xlate_report(ctx, "Not tunneling to our own address");
1319 goto out; /* restore flow_nw_tos */
1321 if (ctx->xin->resubmit_stats) {
1322 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1324 out_port = odp_port;
1325 commit_odp_tunnel_action(flow, &ctx->base_flow,
1326 &ctx->xout->odp_actions);
1327 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
1329 ofp_port_t vlandev_port;
1331 odp_port = xport->odp_port;
1332 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
1333 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1335 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto, ofp_port,
1337 if (vlandev_port == ofp_port) {
1338 out_port = odp_port;
1340 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
1341 flow->vlan_tci = htons(0);
1343 flow->skb_mark &= ~IPSEC_MARK;
1346 if (out_port != ODPP_NONE) {
1347 commit_odp_actions(flow, &ctx->base_flow,
1348 &ctx->xout->odp_actions, &ctx->xout->wc);
1349 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
1352 ctx->sflow_odp_port = odp_port;
1353 ctx->sflow_n_outputs++;
1354 ctx->xout->nf_output_iface = ofp_port;
1359 flow->vlan_tci = flow_vlan_tci;
1360 flow->skb_mark = flow_skb_mark;
1361 flow->nw_tos = flow_nw_tos;
1365 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
1367 compose_output_action__(ctx, ofp_port, true);
1370 /* Common rule processing in one place to avoid duplicating code. */
1371 static struct rule_dpif *
1372 ctx_rule_hooks(struct xlate_ctx *ctx, struct rule_dpif *rule,
1375 if (ctx->xin->resubmit_hook) {
1376 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
1378 if (rule == NULL && may_packet_in) {
1380 * check if table configuration flags
1381 * OFPTC_TABLE_MISS_CONTROLLER, default.
1382 * OFPTC_TABLE_MISS_CONTINUE,
1383 * OFPTC_TABLE_MISS_DROP
1384 * When OF1.0, OFPTC_TABLE_MISS_CONTINUE is used. What to do?
1386 rule = rule_dpif_miss_rule(ctx->xbridge->ofproto, &ctx->xin->flow);
1388 if (rule && ctx->xin->resubmit_stats) {
1389 rule_credit_stats(rule, ctx->xin->resubmit_stats);
1395 xlate_table_action(struct xlate_ctx *ctx,
1396 ofp_port_t in_port, uint8_t table_id, bool may_packet_in)
1398 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
1399 struct rule_dpif *rule;
1400 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
1401 uint8_t old_table_id = ctx->table_id;
1403 ctx->table_id = table_id;
1405 /* Look up a flow with 'in_port' as the input port. */
1406 ctx->xin->flow.in_port.ofp_port = in_port;
1407 rule = rule_dpif_lookup_in_table(ctx->xbridge->ofproto,
1408 &ctx->xin->flow, &ctx->xout->wc,
1411 ctx->xout->tags |= calculate_flow_tag(ctx->xbridge->ofproto,
1412 &ctx->xin->flow, ctx->table_id,
1415 /* Restore the original input port. Otherwise OFPP_NORMAL and
1416 * OFPP_IN_PORT will have surprising behavior. */
1417 ctx->xin->flow.in_port.ofp_port = old_in_port;
1419 rule = ctx_rule_hooks(ctx, rule, may_packet_in);
1422 struct rule_dpif *old_rule = ctx->rule;
1426 do_xlate_actions(rule->up.ofpacts, rule->up.ofpacts_len, ctx);
1427 ctx->rule = old_rule;
1431 ctx->table_id = old_table_id;
1433 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
1435 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
1436 MAX_RESUBMIT_RECURSION);
1437 ctx->max_resubmit_trigger = true;
1442 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
1443 const struct ofpact_resubmit *resubmit)
1448 in_port = resubmit->in_port;
1449 if (in_port == OFPP_IN_PORT) {
1450 in_port = ctx->xin->flow.in_port.ofp_port;
1453 table_id = resubmit->table_id;
1454 if (table_id == 255) {
1455 table_id = ctx->table_id;
1458 xlate_table_action(ctx, in_port, table_id, false);
1462 flood_packets(struct xlate_ctx *ctx, bool all)
1464 const struct xport *xport;
1466 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
1467 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
1472 compose_output_action__(ctx, xport->ofp_port, false);
1473 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
1474 compose_output_action(ctx, xport->ofp_port);
1478 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1482 execute_controller_action(struct xlate_ctx *ctx, int len,
1483 enum ofp_packet_in_reason reason,
1484 uint16_t controller_id)
1486 struct ofputil_packet_in pin;
1487 struct ofpbuf *packet;
1490 ovs_assert(!ctx->xout->slow || ctx->xout->slow == SLOW_CONTROLLER);
1491 ctx->xout->slow = SLOW_CONTROLLER;
1492 if (!ctx->xin->packet) {
1496 packet = ofpbuf_clone(ctx->xin->packet);
1498 key.skb_priority = 0;
1500 memset(&key.tunnel, 0, sizeof key.tunnel);
1502 commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1503 &ctx->xout->odp_actions, &ctx->xout->wc);
1505 odp_execute_actions(NULL, packet, &key, ctx->xout->odp_actions.data,
1506 ctx->xout->odp_actions.size, NULL, NULL);
1508 pin.packet = packet->data;
1509 pin.packet_len = packet->size;
1510 pin.reason = reason;
1511 pin.controller_id = controller_id;
1512 pin.table_id = ctx->table_id;
1513 pin.cookie = ctx->rule ? ctx->rule->up.flow_cookie : 0;
1516 flow_get_metadata(&ctx->xin->flow, &pin.fmd);
1518 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, &pin);
1519 ofpbuf_delete(packet);
1523 compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1525 struct flow_wildcards *wc = &ctx->xout->wc;
1526 struct flow *flow = &ctx->xin->flow;
1528 ovs_assert(eth_type_mpls(eth_type));
1530 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1531 memset(&wc->masks.mpls_depth, 0xff, sizeof wc->masks.mpls_depth);
1533 if (flow->mpls_depth) {
1534 flow->mpls_lse &= ~htonl(MPLS_BOS_MASK);
1540 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1541 label = htonl(0x2); /* IPV6 Explicit Null. */
1543 label = htonl(0x0); /* IPV4 Explicit Null. */
1545 wc->masks.nw_tos |= IP_DSCP_MASK;
1546 wc->masks.nw_ttl = 0xff;
1547 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
1548 ttl = flow->nw_ttl ? flow->nw_ttl : 0x40;
1549 flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
1550 flow->mpls_depth = 1;
1552 flow->dl_type = eth_type;
1556 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1558 struct flow_wildcards *wc = &ctx->xout->wc;
1559 struct flow *flow = &ctx->xin->flow;
1561 ovs_assert(eth_type_mpls(ctx->xin->flow.dl_type));
1562 ovs_assert(!eth_type_mpls(eth_type));
1564 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1565 memset(&wc->masks.mpls_depth, 0xff, sizeof wc->masks.mpls_depth);
1567 if (flow->mpls_depth) {
1569 flow->mpls_lse = htonl(0);
1570 if (!flow->mpls_depth) {
1571 flow->dl_type = eth_type;
1577 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
1579 struct flow *flow = &ctx->xin->flow;
1581 if (!is_ip_any(flow)) {
1585 ctx->xout->wc.masks.nw_ttl = 0xff;
1586 if (flow->nw_ttl > 1) {
1592 for (i = 0; i < ids->n_controllers; i++) {
1593 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
1597 /* Stop processing for current table. */
1603 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
1605 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
1609 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl);
1614 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
1616 struct flow *flow = &ctx->xin->flow;
1617 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse);
1618 struct flow_wildcards *wc = &ctx->xout->wc;
1620 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1622 if (!eth_type_mpls(flow->dl_type)) {
1628 set_mpls_lse_ttl(&flow->mpls_lse, ttl);
1631 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
1633 /* Stop processing for current table. */
1639 xlate_output_action(struct xlate_ctx *ctx,
1640 ofp_port_t port, uint16_t max_len, bool may_packet_in)
1642 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
1644 ctx->xout->nf_output_iface = NF_OUT_DROP;
1648 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
1651 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
1658 flood_packets(ctx, false);
1661 flood_packets(ctx, true);
1663 case OFPP_CONTROLLER:
1664 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
1670 if (port != ctx->xin->flow.in_port.ofp_port) {
1671 compose_output_action(ctx, port);
1673 xlate_report(ctx, "skipping output to input port");
1678 if (prev_nf_output_iface == NF_OUT_FLOOD) {
1679 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1680 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
1681 ctx->xout->nf_output_iface = prev_nf_output_iface;
1682 } else if (prev_nf_output_iface != NF_OUT_DROP &&
1683 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
1684 ctx->xout->nf_output_iface = NF_OUT_MULTI;
1689 xlate_output_reg_action(struct xlate_ctx *ctx,
1690 const struct ofpact_output_reg *or)
1692 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
1693 if (port <= UINT16_MAX) {
1694 union mf_subvalue value;
1696 memset(&value, 0xff, sizeof value);
1697 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
1698 xlate_output_action(ctx, u16_to_ofp(port),
1699 or->max_len, false);
1704 xlate_enqueue_action(struct xlate_ctx *ctx,
1705 const struct ofpact_enqueue *enqueue)
1707 ofp_port_t ofp_port = enqueue->port;
1708 uint32_t queue_id = enqueue->queue;
1709 uint32_t flow_priority, priority;
1712 /* Translate queue to priority. */
1713 error = ofproto_dpif_queue_to_priority(ctx->xbridge->ofproto, queue_id,
1716 /* Fall back to ordinary output action. */
1717 xlate_output_action(ctx, enqueue->port, 0, false);
1721 /* Check output port. */
1722 if (ofp_port == OFPP_IN_PORT) {
1723 ofp_port = ctx->xin->flow.in_port.ofp_port;
1724 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
1728 /* Add datapath actions. */
1729 flow_priority = ctx->xin->flow.skb_priority;
1730 ctx->xin->flow.skb_priority = priority;
1731 compose_output_action(ctx, ofp_port);
1732 ctx->xin->flow.skb_priority = flow_priority;
1734 /* Update NetFlow output port. */
1735 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
1736 ctx->xout->nf_output_iface = ofp_port;
1737 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
1738 ctx->xout->nf_output_iface = NF_OUT_MULTI;
1743 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
1745 uint32_t skb_priority;
1747 if (!ofproto_dpif_queue_to_priority(ctx->xbridge->ofproto, queue_id,
1749 ctx->xin->flow.skb_priority = skb_priority;
1751 /* Couldn't translate queue to a priority. Nothing to do. A warning
1752 * has already been logged. */
1757 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
1759 const struct xbridge *xbridge = xbridge_;
1770 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
1773 port = get_ofp_port(xbridge, ofp_port);
1774 return port ? port->may_enable : false;
1779 xlate_bundle_action(struct xlate_ctx *ctx,
1780 const struct ofpact_bundle *bundle)
1784 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
1786 CONST_CAST(struct xbridge *, ctx->xbridge));
1787 if (bundle->dst.field) {
1788 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow);
1790 xlate_output_action(ctx, port, 0, false);
1795 xlate_learn_action(struct xlate_ctx *ctx,
1796 const struct ofpact_learn *learn)
1798 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
1799 struct ofputil_flow_mod fm;
1800 uint64_t ofpacts_stub[1024 / 8];
1801 struct ofpbuf ofpacts;
1804 ctx->xout->has_learn = true;
1806 learn_mask(learn, &ctx->xout->wc);
1808 if (!ctx->xin->may_learn) {
1812 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1813 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
1815 error = ofproto_dpif_flow_mod(ctx->xbridge->ofproto, &fm);
1816 if (error && !VLOG_DROP_WARN(&rl)) {
1817 VLOG_WARN("learning action failed to modify flow table (%s)",
1818 ofperr_get_name(error));
1821 ofpbuf_uninit(&ofpacts);
1824 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
1825 * means "infinite". */
1827 reduce_timeout(uint16_t max, uint16_t *timeout)
1829 if (max && (!*timeout || *timeout > max)) {
1835 xlate_fin_timeout(struct xlate_ctx *ctx,
1836 const struct ofpact_fin_timeout *oft)
1838 if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
1839 struct rule_dpif *rule = ctx->rule;
1841 if (list_is_empty(&rule->up.expirable)) {
1842 list_insert(&rule->up.ofproto->expirable, &rule->up.expirable);
1845 reduce_timeout(oft->fin_idle_timeout, &rule->up.idle_timeout);
1846 reduce_timeout(oft->fin_hard_timeout, &rule->up.hard_timeout);
1851 xlate_sample_action(struct xlate_ctx *ctx,
1852 const struct ofpact_sample *os)
1854 union user_action_cookie cookie;
1855 /* Scale the probability from 16-bit to 32-bit while representing
1856 * the same percentage. */
1857 uint32_t probability = (os->probability << 16) | os->probability;
1859 commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1860 &ctx->xout->odp_actions, &ctx->xout->wc);
1862 compose_flow_sample_cookie(os->probability, os->collector_set_id,
1863 os->obs_domain_id, os->obs_point_id, &cookie);
1864 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
1865 probability, &cookie, sizeof cookie.flow_sample);
1869 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
1871 if (xport->config & (eth_addr_equals(ctx->xin->flow.dl_dst, eth_addr_stp)
1872 ? OFPUTIL_PC_NO_RECV_STP
1873 : OFPUTIL_PC_NO_RECV)) {
1877 /* Only drop packets here if both forwarding and learning are
1878 * disabled. If just learning is enabled, we need to have
1879 * OFPP_NORMAL and the learning action have a look at the packet
1880 * before we can drop it. */
1881 if (!stp_forward_in_state(xport->stp_state)
1882 && !stp_learn_in_state(xport->stp_state)) {
1890 tunnel_ecn_ok(struct xlate_ctx *ctx)
1892 if (is_ip_any(&ctx->base_flow)
1893 && (ctx->xin->flow.tunnel.ip_tos & IP_ECN_MASK) == IP_ECN_CE) {
1894 if ((ctx->base_flow.nw_tos & IP_ECN_MASK) == IP_ECN_NOT_ECT) {
1895 VLOG_WARN_RL(&rl, "dropping tunnel packet marked ECN CE"
1896 " but is not ECN capable");
1899 /* Set the ECN CE value in the tunneled packet. */
1900 ctx->xin->flow.nw_tos |= IP_ECN_CE;
1908 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
1909 struct xlate_ctx *ctx)
1911 struct flow_wildcards *wc = &ctx->xout->wc;
1912 struct flow *flow = &ctx->xin->flow;
1913 bool was_evictable = true;
1914 const struct ofpact *a;
1917 /* Don't let the rule we're working on get evicted underneath us. */
1918 was_evictable = ctx->rule->up.evictable;
1919 ctx->rule->up.evictable = false;
1922 do_xlate_actions_again:
1923 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1924 struct ofpact_controller *controller;
1925 const struct ofpact_metadata *metadata;
1933 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
1934 ofpact_get_OUTPUT(a)->max_len, true);
1937 case OFPACT_CONTROLLER:
1938 controller = ofpact_get_CONTROLLER(a);
1939 execute_controller_action(ctx, controller->max_len,
1941 controller->controller_id);
1944 case OFPACT_ENQUEUE:
1945 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
1948 case OFPACT_SET_VLAN_VID:
1949 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
1950 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
1954 case OFPACT_SET_VLAN_PCP:
1955 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
1957 htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp << VLAN_PCP_SHIFT)
1961 case OFPACT_STRIP_VLAN:
1962 flow->vlan_tci = htons(0);
1965 case OFPACT_PUSH_VLAN:
1966 /* XXX 802.1AD(QinQ) */
1967 flow->vlan_tci = htons(VLAN_CFI);
1970 case OFPACT_SET_ETH_SRC:
1971 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1974 case OFPACT_SET_ETH_DST:
1975 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1978 case OFPACT_SET_IPV4_SRC:
1979 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1980 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1984 case OFPACT_SET_IPV4_DST:
1985 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1986 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
1990 case OFPACT_SET_IPV4_DSCP:
1991 /* OpenFlow 1.0 only supports IPv4. */
1992 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1993 flow->nw_tos &= ~IP_DSCP_MASK;
1994 flow->nw_tos |= ofpact_get_SET_IPV4_DSCP(a)->dscp;
1998 case OFPACT_SET_L4_SRC_PORT:
1999 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2000 if (is_ip_any(flow)) {
2001 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2005 case OFPACT_SET_L4_DST_PORT:
2006 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2007 if (is_ip_any(flow)) {
2008 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2012 case OFPACT_RESUBMIT:
2013 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
2016 case OFPACT_SET_TUNNEL:
2017 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2020 case OFPACT_SET_QUEUE:
2021 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2024 case OFPACT_POP_QUEUE:
2025 flow->skb_priority = ctx->orig_skb_priority;
2028 case OFPACT_REG_MOVE:
2029 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
2032 case OFPACT_REG_LOAD:
2033 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow);
2036 case OFPACT_STACK_PUSH:
2037 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
2041 case OFPACT_STACK_POP:
2042 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, &ctx->stack);
2045 case OFPACT_PUSH_MPLS:
2046 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a)->ethertype);
2049 case OFPACT_POP_MPLS:
2050 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
2053 case OFPACT_SET_MPLS_TTL:
2054 if (compose_set_mpls_ttl_action(ctx,
2055 ofpact_get_SET_MPLS_TTL(a)->ttl)) {
2060 case OFPACT_DEC_MPLS_TTL:
2061 if (compose_dec_mpls_ttl_action(ctx)) {
2066 case OFPACT_DEC_TTL:
2067 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
2073 /* Nothing to do. */
2076 case OFPACT_MULTIPATH:
2077 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
2081 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
2084 case OFPACT_OUTPUT_REG:
2085 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
2089 xlate_learn_action(ctx, ofpact_get_LEARN(a));
2096 case OFPACT_FIN_TIMEOUT:
2097 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2098 ctx->xout->has_fin_timeout = true;
2099 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
2102 case OFPACT_CLEAR_ACTIONS:
2104 * Nothing to do because writa-actions is not supported for now.
2105 * When writa-actions is supported, clear-actions also must
2106 * be supported at the same time.
2110 case OFPACT_WRITE_METADATA:
2111 metadata = ofpact_get_WRITE_METADATA(a);
2112 flow->metadata &= ~metadata->mask;
2113 flow->metadata |= metadata->metadata & metadata->mask;
2117 /* Not implemented yet. */
2120 case OFPACT_GOTO_TABLE: {
2121 /* It is assumed that goto-table is the last action. */
2122 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
2123 struct rule_dpif *rule;
2125 ovs_assert(ctx->table_id < ogt->table_id);
2127 ctx->table_id = ogt->table_id;
2129 /* Look up a flow from the new table. */
2130 rule = rule_dpif_lookup_in_table(ctx->xbridge->ofproto, flow, wc,
2133 ctx->xout->tags |= calculate_flow_tag(ctx->xbridge->ofproto,
2135 ctx->table_id, rule);
2137 rule = ctx_rule_hooks(ctx, rule, true);
2141 ctx->rule->up.evictable = was_evictable;
2144 was_evictable = rule->up.evictable;
2145 rule->up.evictable = false;
2147 /* Tail recursion removal. */
2148 ofpacts = rule->up.ofpacts;
2149 ofpacts_len = rule->up.ofpacts_len;
2150 goto do_xlate_actions_again;
2156 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
2163 ctx->rule->up.evictable = was_evictable;
2168 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
2169 const struct flow *flow, struct rule_dpif *rule,
2170 uint8_t tcp_flags, const struct ofpbuf *packet)
2172 xin->ofproto = ofproto;
2174 xin->packet = packet;
2175 xin->may_learn = packet != NULL;
2177 xin->ofpacts = NULL;
2178 xin->ofpacts_len = 0;
2179 xin->tcp_flags = tcp_flags;
2180 xin->resubmit_hook = NULL;
2181 xin->report_hook = NULL;
2182 xin->resubmit_stats = NULL;
2186 xlate_out_uninit(struct xlate_out *xout)
2189 ofpbuf_uninit(&xout->odp_actions);
2193 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
2194 * into datapath actions, using 'ctx', and discards the datapath actions. */
2196 xlate_actions_for_side_effects(struct xlate_in *xin)
2198 struct xlate_out xout;
2200 xlate_actions(xin, &xout);
2201 xlate_out_uninit(&xout);
2205 xlate_report(struct xlate_ctx *ctx, const char *s)
2207 if (ctx->xin->report_hook) {
2208 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
2213 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
2216 dst->tags = src->tags;
2217 dst->slow = src->slow;
2218 dst->has_learn = src->has_learn;
2219 dst->has_normal = src->has_normal;
2220 dst->has_fin_timeout = src->has_fin_timeout;
2221 dst->nf_output_iface = src->nf_output_iface;
2222 dst->mirrors = src->mirrors;
2224 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
2225 sizeof dst->odp_actions_stub);
2226 ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
2227 src->odp_actions.size);
2231 actions_output_to_local_port(const struct xlate_ctx *ctx)
2233 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
2234 const struct nlattr *a;
2237 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions.data,
2238 ctx->xout->odp_actions.size) {
2239 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
2240 && nl_attr_get_odp_port(a) == local_odp_port) {
2247 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
2248 * into datapath actions in 'odp_actions', using 'ctx'. */
2250 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
2252 /* Normally false. Set to true if we ever hit MAX_RESUBMIT_RECURSION, so
2253 * that in the future we always keep a copy of the original flow for
2254 * tracing purposes. */
2255 static bool hit_resubmit_limit;
2257 struct flow_wildcards *wc = &xout->wc;
2258 struct flow *flow = &xin->flow;
2260 enum slow_path_reason special;
2261 const struct ofpact *ofpacts;
2262 struct xport *in_port;
2263 struct flow orig_flow;
2264 struct xlate_ctx ctx;
2267 COVERAGE_INC(xlate_actions);
2269 /* Flow initialization rules:
2270 * - 'base_flow' must match the kernel's view of the packet at the
2271 * time that action processing starts. 'flow' represents any
2272 * transformations we wish to make through actions.
2273 * - By default 'base_flow' and 'flow' are the same since the input
2274 * packet matches the output before any actions are applied.
2275 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
2276 * of the received packet as seen by the kernel. If we later output
2277 * to another device without any modifications this will cause us to
2278 * insert a new tag since the original one was stripped off by the
2280 * - Tunnel metadata as received is retained in 'flow'. This allows
2281 * tunnel metadata matching also in later tables.
2282 * Since a kernel action for setting the tunnel metadata will only be
2283 * generated with actual tunnel output, changing the tunnel metadata
2284 * values in 'flow' (such as tun_id) will only have effect with a later
2285 * tunnel output action.
2286 * - Tunnel 'base_flow' is completely cleared since that is what the
2287 * kernel does. If we wish to maintain the original values an action
2288 * needs to be generated. */
2294 ctx.xout->has_learn = false;
2295 ctx.xout->has_normal = false;
2296 ctx.xout->has_fin_timeout = false;
2297 ctx.xout->nf_output_iface = NF_OUT_DROP;
2298 ctx.xout->mirrors = 0;
2299 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
2300 sizeof ctx.xout->odp_actions_stub);
2301 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
2303 ctx.xbridge = xbridge_lookup(xin->ofproto);
2308 ctx.rule = xin->rule;
2310 ctx.base_flow = *flow;
2311 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
2312 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
2314 flow_wildcards_init_catchall(wc);
2315 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
2316 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
2317 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
2318 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
2320 if (tnl_port_should_receive(&ctx.xin->flow)) {
2321 memset(&wc->masks.tunnel, 0xff, sizeof wc->masks.tunnel);
2323 if (ctx.xbridge->has_netflow) {
2324 netflow_mask_wc(flow, wc);
2328 ctx.max_resubmit_trigger = false;
2329 ctx.orig_skb_priority = flow->skb_priority;
2334 ofpacts = xin->ofpacts;
2335 ofpacts_len = xin->ofpacts_len;
2336 } else if (xin->rule) {
2337 ofpacts = xin->rule->up.ofpacts;
2338 ofpacts_len = xin->rule->up.ofpacts_len;
2343 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
2345 if (mbridge_has_mirrors(ctx.xbridge->mbridge) || hit_resubmit_limit) {
2346 /* Do this conditionally because the copy is expensive enough that it
2347 * shows up in profiles. */
2351 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
2352 switch (ctx.xbridge->frag) {
2353 case OFPC_FRAG_NORMAL:
2354 /* We must pretend that transport ports are unavailable. */
2355 flow->tp_src = ctx.base_flow.tp_src = htons(0);
2356 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
2359 case OFPC_FRAG_DROP:
2362 case OFPC_FRAG_REASM:
2365 case OFPC_FRAG_NX_MATCH:
2366 /* Nothing to do. */
2369 case OFPC_INVALID_TTL_TO_CONTROLLER:
2374 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
2375 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
2377 ctx.xout->slow = special;
2379 static struct vlog_rate_limit trace_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2380 size_t sample_actions_len;
2382 if (flow->in_port.ofp_port
2383 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
2384 flow->in_port.ofp_port,
2386 ctx.base_flow.vlan_tci = 0;
2389 add_sflow_action(&ctx);
2390 add_ipfix_action(&ctx);
2391 sample_actions_len = ctx.xout->odp_actions.size;
2393 if (tunnel_ecn_ok(&ctx) && (!in_port || may_receive(in_port, &ctx))) {
2394 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
2396 /* We've let OFPP_NORMAL and the learning action look at the
2397 * packet, so drop it now if forwarding is disabled. */
2398 if (in_port && !stp_forward_in_state(in_port->stp_state)) {
2399 ctx.xout->odp_actions.size = sample_actions_len;
2403 if (ctx.max_resubmit_trigger && !ctx.xin->resubmit_hook) {
2404 if (!hit_resubmit_limit) {
2405 /* We didn't record the original flow. Make sure we do from
2407 hit_resubmit_limit = true;
2408 } else if (!VLOG_DROP_ERR(&trace_rl)) {
2409 struct ds ds = DS_EMPTY_INITIALIZER;
2411 ofproto_trace(ctx.xbridge->ofproto, &orig_flow,
2412 ctx.xin->packet, &ds);
2413 VLOG_ERR("Trace triggered by excessive resubmit "
2414 "recursion:\n%s", ds_cstr(&ds));
2419 if (ctx.xbridge->has_in_band
2420 && in_band_must_output_to_local_port(flow)
2421 && !actions_output_to_local_port(&ctx)) {
2422 compose_output_action(&ctx, OFPP_LOCAL);
2425 fix_sflow_action(&ctx);
2427 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
2428 add_mirror_actions(&ctx, &orig_flow);
2432 ofpbuf_uninit(&ctx.stack);
2434 /* Clear the metadata and register wildcard masks, because we won't
2435 * use non-header fields as part of the cache. */
2436 memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
2437 memset(&wc->masks.regs, 0, sizeof wc->masks.regs);