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. */
68 struct stp *stp; /* STP or null if disabled. */
70 enum ofp_config_flags frag; /* Fragmentation handling. */
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 int stp_port_no; /* STP port number or 0 if not in use. */
117 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
119 bool may_enable; /* May be enabled in bonds. */
120 bool is_tunnel; /* Is a tunnel port. */
122 struct cfm *cfm; /* CFM handle or null. */
123 struct bfd *bfd; /* BFD handle or null. */
127 struct xlate_in *xin;
128 struct xlate_out *xout;
130 const struct xbridge *xbridge;
132 /* Flow at the last commit. */
133 struct flow base_flow;
135 /* Tunnel IP destination address as received. This is stored separately
136 * as the base_flow.tunnel is cleared on init to reflect the datapath
137 * behavior. Used to make sure not to send tunneled output to ourselves,
138 * which might lead to an infinite loop. This could happen easily
139 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
140 * actually set the tun_dst field. */
141 ovs_be32 orig_tunnel_ip_dst;
143 /* Stack for the push and pop actions. Each stack element is of type
144 * "union mf_subvalue". */
145 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
148 /* The rule that we are currently translating, or NULL. */
149 struct rule_dpif *rule;
151 int recurse; /* Recursion level, via xlate_table_action. */
152 uint32_t orig_skb_priority; /* Priority when packet arrived. */
153 uint8_t table_id; /* OpenFlow table ID where flow was found. */
154 uint32_t sflow_n_outputs; /* Number of output ports. */
155 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
156 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
157 bool exit; /* No further actions should be processed. */
160 /* A controller may use OFPP_NONE as the ingress port to indicate that
161 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
162 * when an input bundle is needed for validation (e.g., mirroring or
163 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
164 * any 'port' structs, so care must be taken when dealing with it.
165 * The bundle's name and vlan mode are initialized in lookup_input_bundle() */
166 static struct xbundle ofpp_none_bundle;
168 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
169 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
170 * traffic egressing the 'ofport' with that priority should be marked with. */
171 struct skb_priority_to_dscp {
172 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
173 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
175 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
178 static struct hmap xbridges = HMAP_INITIALIZER(&xbridges);
179 static struct hmap xbundles = HMAP_INITIALIZER(&xbundles);
180 static struct hmap xports = HMAP_INITIALIZER(&xports);
182 static bool may_receive(const struct xport *, struct xlate_ctx *);
183 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
185 static void xlate_normal(struct xlate_ctx *);
186 static void xlate_report(struct xlate_ctx *, const char *);
187 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
188 uint8_t table_id, bool may_packet_in);
189 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
190 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
191 static void output_normal(struct xlate_ctx *, const struct xbundle *,
193 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
195 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
197 static struct xbridge *xbridge_lookup(const struct ofproto_dpif *);
198 static struct xbundle *xbundle_lookup(const struct ofbundle *);
199 static struct xport *xport_lookup(struct ofport_dpif *);
200 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
201 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
202 uint32_t skb_priority);
203 static void clear_skb_priorities(struct xport *);
204 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
208 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
209 const struct mac_learning *ml, struct stp *stp,
210 const struct mbridge *mbridge,
211 const struct dpif_sflow *sflow,
212 const struct dpif_ipfix *ipfix, enum ofp_config_flags frag,
213 bool forward_bpdu, bool has_in_band, bool has_netflow)
215 struct xbridge *xbridge = xbridge_lookup(ofproto);
218 xbridge = xzalloc(sizeof *xbridge);
219 xbridge->ofproto = ofproto;
221 hmap_insert(&xbridges, &xbridge->hmap_node, hash_pointer(ofproto, 0));
222 hmap_init(&xbridge->xports);
223 list_init(&xbridge->xbundles);
226 if (xbridge->ml != ml) {
227 mac_learning_unref(xbridge->ml);
228 xbridge->ml = mac_learning_ref(ml);
231 if (xbridge->mbridge != mbridge) {
232 mbridge_unref(xbridge->mbridge);
233 xbridge->mbridge = mbridge_ref(mbridge);
236 if (xbridge->sflow != sflow) {
237 dpif_sflow_unref(xbridge->sflow);
238 xbridge->sflow = dpif_sflow_ref(sflow);
241 if (xbridge->ipfix != ipfix) {
242 dpif_ipfix_unref(xbridge->ipfix);
243 xbridge->ipfix = dpif_ipfix_ref(ipfix);
246 if (xbridge->stp != stp) {
247 stp_unref(xbridge->stp);
248 xbridge->stp = stp_ref(stp);
252 xbridge->name = xstrdup(name);
254 xbridge->forward_bpdu = forward_bpdu;
255 xbridge->has_in_band = has_in_band;
256 xbridge->has_netflow = has_netflow;
257 xbridge->frag = frag;
261 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
263 struct xbridge *xbridge = xbridge_lookup(ofproto);
264 struct xbundle *xbundle, *next_xbundle;
265 struct xport *xport, *next_xport;
271 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
272 xlate_ofport_remove(xport->ofport);
275 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
276 xlate_bundle_remove(xbundle->ofbundle);
279 hmap_remove(&xbridges, &xbridge->hmap_node);
285 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
286 const char *name, enum port_vlan_mode vlan_mode, int vlan,
287 unsigned long *trunks, bool use_priority_tags,
288 const struct bond *bond, const struct lacp *lacp,
291 struct xbundle *xbundle = xbundle_lookup(ofbundle);
294 xbundle = xzalloc(sizeof *xbundle);
295 xbundle->ofbundle = ofbundle;
296 xbundle->xbridge = xbridge_lookup(ofproto);
298 hmap_insert(&xbundles, &xbundle->hmap_node, hash_pointer(ofbundle, 0));
299 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
300 list_init(&xbundle->xports);
303 ovs_assert(xbundle->xbridge);
306 xbundle->name = xstrdup(name);
308 xbundle->vlan_mode = vlan_mode;
309 xbundle->vlan = vlan;
310 xbundle->trunks = trunks;
311 xbundle->use_priority_tags = use_priority_tags;
312 xbundle->floodable = floodable;
314 if (xbundle->bond != bond) {
315 bond_unref(xbundle->bond);
316 xbundle->bond = bond_ref(bond);
319 if (xbundle->lacp != lacp) {
320 lacp_unref(xbundle->lacp);
321 xbundle->lacp = lacp_ref(lacp);
326 xlate_bundle_remove(struct ofbundle *ofbundle)
328 struct xbundle *xbundle = xbundle_lookup(ofbundle);
329 struct xport *xport, *next;
335 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
336 list_remove(&xport->bundle_node);
337 xport->xbundle = NULL;
340 hmap_remove(&xbundles, &xbundle->hmap_node);
341 list_remove(&xbundle->list_node);
342 bond_unref(xbundle->bond);
343 lacp_unref(xbundle->lacp);
349 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
350 struct ofport_dpif *ofport, ofp_port_t ofp_port,
351 odp_port_t odp_port, const struct netdev *netdev,
352 const struct cfm *cfm, const struct bfd *bfd,
353 struct ofport_dpif *peer, int stp_port_no,
354 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
355 enum ofputil_port_config config, bool is_tunnel,
358 struct xport *xport = xport_lookup(ofport);
362 xport = xzalloc(sizeof *xport);
363 xport->ofport = ofport;
364 xport->xbridge = xbridge_lookup(ofproto);
365 xport->ofp_port = ofp_port;
367 hmap_init(&xport->skb_priorities);
368 hmap_insert(&xports, &xport->hmap_node, hash_pointer(ofport, 0));
369 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
370 hash_ofp_port(xport->ofp_port));
373 ovs_assert(xport->ofp_port == ofp_port);
375 xport->config = config;
376 xport->stp_port_no = stp_port_no;
377 xport->is_tunnel = is_tunnel;
378 xport->may_enable = may_enable;
379 xport->odp_port = odp_port;
381 if (xport->netdev != netdev) {
382 netdev_close(xport->netdev);
383 xport->netdev = netdev_ref(netdev);
386 if (xport->cfm != cfm) {
387 cfm_unref(xport->cfm);
388 xport->cfm = cfm_ref(cfm);
391 if (xport->bfd != bfd) {
392 bfd_unref(xport->bfd);
393 xport->bfd = bfd_ref(bfd);
397 xport->peer->peer = NULL;
399 xport->peer = peer ? xport_lookup(peer) : NULL;
401 xport->peer->peer = xport;
404 if (xport->xbundle) {
405 list_remove(&xport->bundle_node);
407 xport->xbundle = ofbundle ? xbundle_lookup(ofbundle) : NULL;
408 if (xport->xbundle) {
409 list_insert(&xport->xbundle->xports, &xport->bundle_node);
412 clear_skb_priorities(xport);
413 for (i = 0; i < n_qdscp; i++) {
414 struct skb_priority_to_dscp *pdscp;
415 uint32_t skb_priority;
417 if (ofproto_dpif_queue_to_priority(xport->xbridge->ofproto,
423 pdscp = xmalloc(sizeof *pdscp);
424 pdscp->skb_priority = skb_priority;
425 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
426 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
427 hash_int(pdscp->skb_priority, 0));
432 xlate_ofport_remove(struct ofport_dpif *ofport)
434 struct xport *xport = xport_lookup(ofport);
441 xport->peer->peer = NULL;
445 if (xport->xbundle) {
446 list_remove(&xport->bundle_node);
449 clear_skb_priorities(xport);
450 hmap_destroy(&xport->skb_priorities);
452 hmap_remove(&xports, &xport->hmap_node);
453 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
455 netdev_close(xport->netdev);
456 cfm_unref(xport->cfm);
457 bfd_unref(xport->bfd);
461 static struct xbridge *
462 xbridge_lookup(const struct ofproto_dpif *ofproto)
464 struct xbridge *xbridge;
466 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
468 if (xbridge->ofproto == ofproto) {
475 static struct xbundle *
476 xbundle_lookup(const struct ofbundle *ofbundle)
478 struct xbundle *xbundle;
480 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
482 if (xbundle->ofbundle == ofbundle) {
489 static struct xport *
490 xport_lookup(struct ofport_dpif *ofport)
494 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
496 if (xport->ofport == ofport) {
503 static struct stp_port *
504 xport_get_stp_port(const struct xport *xport)
506 return xport->xbridge->stp && xport->stp_port_no
507 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
511 static enum stp_state
512 xport_stp_learn_state(const struct xport *xport)
514 struct stp_port *sp = xport_get_stp_port(xport);
515 return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
519 xport_stp_forward_state(const struct xport *xport)
521 struct stp_port *sp = xport_get_stp_port(xport);
522 return stp_forward_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
525 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
526 * were used to make the determination.*/
528 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
530 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
531 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
535 stp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
537 struct stp_port *sp = xport_get_stp_port(xport);
538 struct ofpbuf payload = *packet;
539 struct eth_header *eth = payload.data;
541 /* Sink packets on ports that have STP disabled when the bridge has
543 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
547 /* Trim off padding on payload. */
548 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
549 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
552 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
553 stp_received_bpdu(sp, payload.data, payload.size);
557 static struct xport *
558 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
562 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
564 if (xport->ofp_port == ofp_port) {
572 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
574 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
575 return xport ? xport->odp_port : ODPP_NONE;
579 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
581 return (bundle->vlan_mode != PORT_VLAN_ACCESS
582 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
586 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
588 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
592 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
594 return xbundle != &ofpp_none_bundle
595 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
600 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
602 return xbundle != &ofpp_none_bundle
603 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
608 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
610 return xbundle != &ofpp_none_bundle
611 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
615 static struct xbundle *
616 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
617 bool warn, struct xport **in_xportp)
621 /* Find the port and bundle for the received packet. */
622 xport = get_ofp_port(xbridge, in_port);
626 if (xport && xport->xbundle) {
627 return xport->xbundle;
630 /* Special-case OFPP_NONE, which a controller may use as the ingress
631 * port for traffic that it is sourcing. */
632 if (in_port == OFPP_NONE) {
633 ofpp_none_bundle.name = "OFPP_NONE";
634 ofpp_none_bundle.vlan_mode = PORT_VLAN_TRUNK;
635 return &ofpp_none_bundle;
638 /* Odd. A few possible reasons here:
640 * - We deleted a port but there are still a few packets queued up
643 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
644 * we don't know about.
646 * - The ofproto client didn't configure the port as part of a bundle.
647 * This is particularly likely to happen if a packet was received on the
648 * port after it was created, but before the client had a chance to
649 * configure its bundle.
652 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
654 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
655 "port %"PRIu16, xbridge->name, in_port);
661 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
663 const struct xbridge *xbridge = ctx->xbridge;
664 mirror_mask_t mirrors;
665 struct xbundle *in_xbundle;
669 mirrors = ctx->xout->mirrors;
670 ctx->xout->mirrors = 0;
672 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
673 ctx->xin->packet != NULL, NULL);
677 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
679 /* Drop frames on bundles reserved for mirroring. */
680 if (xbundle_mirror_out(xbridge, in_xbundle)) {
681 if (ctx->xin->packet != NULL) {
682 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
683 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
684 "%s, which is reserved exclusively for mirroring",
685 ctx->xbridge->name, in_xbundle->name);
687 ofpbuf_clear(&ctx->xout->odp_actions);
692 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
693 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
696 vlan = input_vid_to_vlan(in_xbundle, vid);
702 /* Restore the original packet before adding the mirror actions. */
703 ctx->xin->flow = *orig_flow;
706 mirror_mask_t dup_mirrors;
707 struct ofbundle *out;
708 unsigned long *vlans;
713 has_mirror = mirror_get(xbridge->mbridge, mirror_mask_ffs(mirrors) - 1,
714 &vlans, &dup_mirrors, &out, &out_vlan);
715 ovs_assert(has_mirror);
718 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
720 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
723 if (!vlan_mirrored) {
724 mirrors = zero_rightmost_1bit(mirrors);
728 mirrors &= ~dup_mirrors;
729 ctx->xout->mirrors |= dup_mirrors;
731 struct xbundle *out_xbundle = xbundle_lookup(out);
733 output_normal(ctx, out_xbundle, vlan);
735 } else if (vlan != out_vlan
736 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
737 struct xbundle *xbundle;
739 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
740 if (xbundle_includes_vlan(xbundle, out_vlan)
741 && !xbundle_mirror_out(xbridge, xbundle)) {
742 output_normal(ctx, xbundle, out_vlan);
749 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
750 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
751 * the bundle on which the packet was received, returns the VLAN to which the
754 * Both 'vid' and the return value are in the range 0...4095. */
756 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
758 switch (in_xbundle->vlan_mode) {
759 case PORT_VLAN_ACCESS:
760 return in_xbundle->vlan;
763 case PORT_VLAN_TRUNK:
766 case PORT_VLAN_NATIVE_UNTAGGED:
767 case PORT_VLAN_NATIVE_TAGGED:
768 return vid ? vid : in_xbundle->vlan;
775 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
776 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
779 * 'vid' should be the VID obtained from the 802.1Q header that was received as
780 * part of a packet (specify 0 if there was no 802.1Q header), in the range
783 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
785 /* Allow any VID on the OFPP_NONE port. */
786 if (in_xbundle == &ofpp_none_bundle) {
790 switch (in_xbundle->vlan_mode) {
791 case PORT_VLAN_ACCESS:
794 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
795 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
796 "packet received on port %s configured as VLAN "
797 "%"PRIu16" access port", vid, in_xbundle->name,
804 case PORT_VLAN_NATIVE_UNTAGGED:
805 case PORT_VLAN_NATIVE_TAGGED:
807 /* Port must always carry its native VLAN. */
811 case PORT_VLAN_TRUNK:
812 if (!xbundle_includes_vlan(in_xbundle, vid)) {
814 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
815 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
816 "received on port %s not configured for trunking "
817 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
829 /* Given 'vlan', the VLAN that a packet belongs to, and
830 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
831 * that should be included in the 802.1Q header. (If the return value is 0,
832 * then the 802.1Q header should only be included in the packet if there is a
835 * Both 'vlan' and the return value are in the range 0...4095. */
837 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
839 switch (out_xbundle->vlan_mode) {
840 case PORT_VLAN_ACCESS:
843 case PORT_VLAN_TRUNK:
844 case PORT_VLAN_NATIVE_TAGGED:
847 case PORT_VLAN_NATIVE_UNTAGGED:
848 return vlan == out_xbundle->vlan ? 0 : vlan;
856 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
859 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
861 ovs_be16 tci, old_tci;
864 vid = output_vlan_to_vid(out_xbundle, vlan);
865 if (list_is_empty(&out_xbundle->xports)) {
866 /* Partially configured bundle with no slaves. Drop the packet. */
868 } else if (!out_xbundle->bond) {
869 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
872 struct ofport_dpif *ofport;
874 ofport = bond_choose_output_slave(out_xbundle->bond, &ctx->xin->flow,
875 &ctx->xout->wc, vid);
876 xport = ofport ? xport_lookup(ofport) : NULL;
879 /* No slaves enabled, so drop packet. */
886 if (tci || out_xbundle->use_priority_tags) {
887 tci |= *flow_tci & htons(VLAN_PCP_MASK);
889 tci |= htons(VLAN_CFI);
894 compose_output_action(ctx, xport->ofp_port);
898 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
899 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
900 * indicate this; newer upstream kernels use gratuitous ARP requests. */
902 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
904 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
908 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
909 if (!eth_addr_is_broadcast(flow->dl_dst)) {
913 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
914 if (flow->nw_proto == ARP_OP_REPLY) {
916 } else if (flow->nw_proto == ARP_OP_REQUEST) {
917 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
918 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
920 return flow->nw_src == flow->nw_dst;
927 update_learning_table(const struct xbridge *xbridge,
928 const struct flow *flow, struct flow_wildcards *wc,
929 int vlan, struct xbundle *in_xbundle)
931 struct mac_entry *mac;
933 /* Don't learn the OFPP_NONE port. */
934 if (in_xbundle == &ofpp_none_bundle) {
938 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
939 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
943 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
944 if (is_gratuitous_arp(flow, wc)) {
945 /* We don't want to learn from gratuitous ARP packets that are
946 * reflected back over bond slaves so we lock the learning table. */
947 if (!in_xbundle->bond) {
948 mac_entry_set_grat_arp_lock(mac);
949 } else if (mac_entry_is_grat_arp_locked(mac)) {
954 if (mac->port.p != in_xbundle->ofbundle) {
955 /* The log messages here could actually be useful in debugging,
956 * so keep the rate limit relatively high. */
957 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
958 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
959 "on port %s in VLAN %d",
960 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
961 in_xbundle->name, vlan);
963 mac->port.p = in_xbundle->ofbundle;
964 mac_learning_changed(xbridge->ml);
967 ovs_rwlock_unlock(&xbridge->ml->rwlock);
970 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
971 * dropped. Returns true if they may be forwarded, false if they should be
974 * 'in_port' must be the xport that corresponds to flow->in_port.
975 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
977 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
978 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
979 * checked by input_vid_is_valid().
981 * May also add tags to '*tags', although the current implementation only does
982 * so in one special case.
985 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
988 struct xbundle *in_xbundle = in_port->xbundle;
989 const struct xbridge *xbridge = ctx->xbridge;
990 struct flow *flow = &ctx->xin->flow;
992 /* Drop frames for reserved multicast addresses
993 * only if forward_bpdu option is absent. */
994 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
995 xlate_report(ctx, "packet has reserved destination MAC, dropping");
999 if (in_xbundle->bond) {
1000 struct mac_entry *mac;
1002 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1008 xlate_report(ctx, "bonding refused admissibility, dropping");
1011 case BV_DROP_IF_MOVED:
1012 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1013 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1014 if (mac && mac->port.p != in_xbundle->ofbundle &&
1015 (!is_gratuitous_arp(flow, &ctx->xout->wc)
1016 || mac_entry_is_grat_arp_locked(mac))) {
1017 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1018 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1022 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1031 xlate_normal(struct xlate_ctx *ctx)
1033 struct flow_wildcards *wc = &ctx->xout->wc;
1034 struct flow *flow = &ctx->xin->flow;
1035 struct xbundle *in_xbundle;
1036 struct xport *in_port;
1037 struct mac_entry *mac;
1041 ctx->xout->has_normal = true;
1043 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1044 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1045 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1047 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
1048 ctx->xin->packet != NULL, &in_port);
1050 xlate_report(ctx, "no input bundle, dropping");
1054 /* Drop malformed frames. */
1055 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
1056 !(flow->vlan_tci & htons(VLAN_CFI))) {
1057 if (ctx->xin->packet != NULL) {
1058 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1059 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
1060 "VLAN tag received on port %s",
1061 ctx->xbridge->name, in_xbundle->name);
1063 xlate_report(ctx, "partial VLAN tag, dropping");
1067 /* Drop frames on bundles reserved for mirroring. */
1068 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
1069 if (ctx->xin->packet != NULL) {
1070 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1071 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1072 "%s, which is reserved exclusively for mirroring",
1073 ctx->xbridge->name, in_xbundle->name);
1075 xlate_report(ctx, "input port is mirror output port, dropping");
1080 vid = vlan_tci_to_vid(flow->vlan_tci);
1081 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1082 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
1085 vlan = input_vid_to_vlan(in_xbundle, vid);
1087 /* Check other admissibility requirements. */
1088 if (in_port && !is_admissible(ctx, in_port, vlan)) {
1092 /* Learn source MAC. */
1093 if (ctx->xin->may_learn) {
1094 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
1097 /* Determine output bundle. */
1098 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
1099 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
1101 struct xbundle *mac_xbundle = xbundle_lookup(mac->port.p);
1102 if (mac_xbundle && mac_xbundle != in_xbundle) {
1103 xlate_report(ctx, "forwarding to learned port");
1104 output_normal(ctx, mac_xbundle, vlan);
1105 } else if (!mac_xbundle) {
1106 xlate_report(ctx, "learned port is unknown, dropping");
1108 xlate_report(ctx, "learned port is input port, dropping");
1111 struct xbundle *xbundle;
1113 xlate_report(ctx, "no learned MAC for destination, flooding");
1114 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1115 if (xbundle != in_xbundle
1116 && xbundle_includes_vlan(xbundle, vlan)
1117 && xbundle->floodable
1118 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1119 output_normal(ctx, xbundle, vlan);
1122 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1124 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
1127 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1128 * the number of packets out of UINT32_MAX to sample. The given
1129 * cookie is passed back in the callback for each sampled packet.
1132 compose_sample_action(const struct xbridge *xbridge,
1133 struct ofpbuf *odp_actions,
1134 const struct flow *flow,
1135 const uint32_t probability,
1136 const union user_action_cookie *cookie,
1137 const size_t cookie_size)
1139 size_t sample_offset, actions_offset;
1142 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
1144 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
1146 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
1147 cookie_offset = put_userspace_action(xbridge->ofproto, odp_actions, flow,
1148 cookie, cookie_size);
1150 nl_msg_end_nested(odp_actions, actions_offset);
1151 nl_msg_end_nested(odp_actions, sample_offset);
1152 return cookie_offset;
1156 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
1157 odp_port_t odp_port, unsigned int n_outputs,
1158 union user_action_cookie *cookie)
1162 cookie->type = USER_ACTION_COOKIE_SFLOW;
1163 cookie->sflow.vlan_tci = vlan_tci;
1165 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1166 * port information") for the interpretation of cookie->output. */
1167 switch (n_outputs) {
1169 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1170 cookie->sflow.output = 0x40000000 | 256;
1174 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
1176 cookie->sflow.output = ifindex;
1181 /* 0x80000000 means "multiple output ports. */
1182 cookie->sflow.output = 0x80000000 | n_outputs;
1187 /* Compose SAMPLE action for sFlow bridge sampling. */
1189 compose_sflow_action(const struct xbridge *xbridge,
1190 struct ofpbuf *odp_actions,
1191 const struct flow *flow,
1192 odp_port_t odp_port)
1194 uint32_t probability;
1195 union user_action_cookie cookie;
1197 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
1201 probability = dpif_sflow_get_probability(xbridge->sflow);
1202 compose_sflow_cookie(xbridge, htons(0), odp_port,
1203 odp_port == ODPP_NONE ? 0 : 1, &cookie);
1205 return compose_sample_action(xbridge, odp_actions, flow, probability,
1206 &cookie, sizeof cookie.sflow);
1210 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
1211 uint32_t obs_domain_id, uint32_t obs_point_id,
1212 union user_action_cookie *cookie)
1214 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
1215 cookie->flow_sample.probability = probability;
1216 cookie->flow_sample.collector_set_id = collector_set_id;
1217 cookie->flow_sample.obs_domain_id = obs_domain_id;
1218 cookie->flow_sample.obs_point_id = obs_point_id;
1222 compose_ipfix_cookie(union user_action_cookie *cookie)
1224 cookie->type = USER_ACTION_COOKIE_IPFIX;
1227 /* Compose SAMPLE action for IPFIX bridge sampling. */
1229 compose_ipfix_action(const struct xbridge *xbridge,
1230 struct ofpbuf *odp_actions,
1231 const struct flow *flow)
1233 uint32_t probability;
1234 union user_action_cookie cookie;
1236 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
1240 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
1241 compose_ipfix_cookie(&cookie);
1243 compose_sample_action(xbridge, odp_actions, flow, probability,
1244 &cookie, sizeof cookie.ipfix);
1247 /* SAMPLE action for sFlow must be first action in any given list of
1248 * actions. At this point we do not have all information required to
1249 * build it. So try to build sample action as complete as possible. */
1251 add_sflow_action(struct xlate_ctx *ctx)
1253 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
1254 &ctx->xout->odp_actions,
1255 &ctx->xin->flow, ODPP_NONE);
1256 ctx->sflow_odp_port = 0;
1257 ctx->sflow_n_outputs = 0;
1260 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
1261 * of actions, eventually after the SAMPLE action for sFlow. */
1263 add_ipfix_action(struct xlate_ctx *ctx)
1265 compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions,
1269 /* Fix SAMPLE action according to data collected while composing ODP actions.
1270 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
1271 * USERSPACE action's user-cookie which is required for sflow. */
1273 fix_sflow_action(struct xlate_ctx *ctx)
1275 const struct flow *base = &ctx->base_flow;
1276 union user_action_cookie *cookie;
1278 if (!ctx->user_cookie_offset) {
1282 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
1283 sizeof cookie->sflow);
1284 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
1286 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
1287 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
1290 static enum slow_path_reason
1291 process_special(struct xlate_ctx *ctx, const struct flow *flow,
1292 const struct xport *xport, const struct ofpbuf *packet)
1294 struct flow_wildcards *wc = &ctx->xout->wc;
1295 const struct xbridge *xbridge = ctx->xbridge;
1299 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
1301 cfm_process_heartbeat(xport->cfm, packet);
1304 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
1306 bfd_process_packet(xport->bfd, flow, packet);
1309 } else if (xport->xbundle && xport->xbundle->lacp
1310 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1312 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
1315 } else if (xbridge->stp && stp_should_process_flow(flow, wc)) {
1317 stp_process_packet(xport, packet);
1326 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
1329 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1330 struct flow_wildcards *wc = &ctx->xout->wc;
1331 struct flow *flow = &ctx->xin->flow;
1332 ovs_be16 flow_vlan_tci;
1333 uint32_t flow_skb_mark;
1334 uint8_t flow_nw_tos;
1335 odp_port_t out_port, odp_port;
1338 /* If 'struct flow' gets additional metadata, we'll need to zero it out
1339 * before traversing a patch port. */
1340 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 20);
1343 xlate_report(ctx, "Nonexistent output port");
1345 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
1346 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
1348 } else if (check_stp && !xport_stp_forward_state(xport)) {
1349 xlate_report(ctx, "STP not in forwarding state, skipping output");
1353 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
1354 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
1359 const struct xport *peer = xport->peer;
1360 struct flow old_flow = ctx->xin->flow;
1361 enum slow_path_reason special;
1363 ctx->xbridge = peer->xbridge;
1364 flow->in_port.ofp_port = peer->ofp_port;
1365 flow->metadata = htonll(0);
1366 memset(&flow->tunnel, 0, sizeof flow->tunnel);
1367 memset(flow->regs, 0, sizeof flow->regs);
1369 special = process_special(ctx, &ctx->xin->flow, peer,
1372 ctx->xout->slow = special;
1373 } else if (may_receive(peer, ctx)) {
1374 if (xport_stp_forward_state(peer)) {
1375 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1377 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
1378 * learning action look at the packet, then drop it. */
1379 struct flow old_base_flow = ctx->base_flow;
1380 size_t old_size = ctx->xout->odp_actions.size;
1381 mirror_mask_t old_mirrors = ctx->xout->mirrors;
1382 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1383 ctx->xout->mirrors = old_mirrors;
1384 ctx->base_flow = old_base_flow;
1385 ctx->xout->odp_actions.size = old_size;
1389 ctx->xin->flow = old_flow;
1390 ctx->xbridge = xport->xbundle->xbridge;
1392 if (ctx->xin->resubmit_stats) {
1393 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1394 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
1400 flow_vlan_tci = flow->vlan_tci;
1401 flow_skb_mark = flow->skb_mark;
1402 flow_nw_tos = flow->nw_tos;
1404 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
1405 wc->masks.nw_tos |= IP_ECN_MASK;
1406 flow->nw_tos &= ~IP_DSCP_MASK;
1407 flow->nw_tos |= dscp;
1410 if (xport->is_tunnel) {
1411 /* Save tunnel metadata so that changes made due to
1412 * the Logical (tunnel) Port are not visible for any further
1413 * matches, while explicit set actions on tunnel metadata are.
1415 struct flow_tnl flow_tnl = flow->tunnel;
1416 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
1417 if (odp_port == ODPP_NONE) {
1418 xlate_report(ctx, "Tunneling decided against output");
1419 goto out; /* restore flow_nw_tos */
1421 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
1422 xlate_report(ctx, "Not tunneling to our own address");
1423 goto out; /* restore flow_nw_tos */
1425 if (ctx->xin->resubmit_stats) {
1426 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1428 out_port = odp_port;
1429 commit_odp_tunnel_action(flow, &ctx->base_flow,
1430 &ctx->xout->odp_actions);
1431 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
1433 ofp_port_t vlandev_port;
1435 odp_port = xport->odp_port;
1436 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
1437 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1439 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto, ofp_port,
1441 if (vlandev_port == ofp_port) {
1442 out_port = odp_port;
1444 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
1445 flow->vlan_tci = htons(0);
1447 flow->skb_mark &= ~IPSEC_MARK;
1450 if (out_port != ODPP_NONE) {
1451 commit_odp_actions(flow, &ctx->base_flow,
1452 &ctx->xout->odp_actions, &ctx->xout->wc);
1453 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
1456 ctx->sflow_odp_port = odp_port;
1457 ctx->sflow_n_outputs++;
1458 ctx->xout->nf_output_iface = ofp_port;
1463 flow->vlan_tci = flow_vlan_tci;
1464 flow->skb_mark = flow_skb_mark;
1465 flow->nw_tos = flow_nw_tos;
1469 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
1471 compose_output_action__(ctx, ofp_port, true);
1474 /* Common rule processing in one place to avoid duplicating code. */
1475 static struct rule_dpif *
1476 ctx_rule_hooks(struct xlate_ctx *ctx, struct rule_dpif *rule,
1479 if (ctx->xin->resubmit_hook) {
1480 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
1482 if (rule == NULL && may_packet_in) {
1484 * check if table configuration flags
1485 * OFPTC_TABLE_MISS_CONTROLLER, default.
1486 * OFPTC_TABLE_MISS_CONTINUE,
1487 * OFPTC_TABLE_MISS_DROP
1488 * When OF1.0, OFPTC_TABLE_MISS_CONTINUE is used. What to do?
1490 rule = rule_dpif_miss_rule(ctx->xbridge->ofproto, &ctx->xin->flow);
1492 if (rule && ctx->xin->resubmit_stats) {
1493 rule_credit_stats(rule, ctx->xin->resubmit_stats);
1499 xlate_table_action(struct xlate_ctx *ctx,
1500 ofp_port_t in_port, uint8_t table_id, bool may_packet_in)
1502 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
1503 struct rule_dpif *rule;
1504 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
1505 uint8_t old_table_id = ctx->table_id;
1507 ctx->table_id = table_id;
1509 /* Look up a flow with 'in_port' as the input port. */
1510 ctx->xin->flow.in_port.ofp_port = in_port;
1511 rule = rule_dpif_lookup_in_table(ctx->xbridge->ofproto,
1512 &ctx->xin->flow, &ctx->xout->wc,
1515 /* Restore the original input port. Otherwise OFPP_NORMAL and
1516 * OFPP_IN_PORT will have surprising behavior. */
1517 ctx->xin->flow.in_port.ofp_port = old_in_port;
1519 rule = ctx_rule_hooks(ctx, rule, may_packet_in);
1522 struct rule_dpif *old_rule = ctx->rule;
1526 do_xlate_actions(rule->up.ofpacts, rule->up.ofpacts_len, ctx);
1527 ctx->rule = old_rule;
1531 ctx->table_id = old_table_id;
1533 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
1535 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
1536 MAX_RESUBMIT_RECURSION);
1541 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
1542 const struct ofpact_resubmit *resubmit)
1547 in_port = resubmit->in_port;
1548 if (in_port == OFPP_IN_PORT) {
1549 in_port = ctx->xin->flow.in_port.ofp_port;
1552 table_id = resubmit->table_id;
1553 if (table_id == 255) {
1554 table_id = ctx->table_id;
1557 xlate_table_action(ctx, in_port, table_id, false);
1561 flood_packets(struct xlate_ctx *ctx, bool all)
1563 const struct xport *xport;
1565 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
1566 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
1571 compose_output_action__(ctx, xport->ofp_port, false);
1572 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
1573 compose_output_action(ctx, xport->ofp_port);
1577 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1581 execute_controller_action(struct xlate_ctx *ctx, int len,
1582 enum ofp_packet_in_reason reason,
1583 uint16_t controller_id)
1585 struct ofputil_packet_in pin;
1586 struct ofpbuf *packet;
1589 ovs_assert(!ctx->xout->slow || ctx->xout->slow == SLOW_CONTROLLER);
1590 ctx->xout->slow = SLOW_CONTROLLER;
1591 if (!ctx->xin->packet) {
1595 packet = ofpbuf_clone(ctx->xin->packet);
1597 key.skb_priority = 0;
1599 memset(&key.tunnel, 0, sizeof key.tunnel);
1601 commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1602 &ctx->xout->odp_actions, &ctx->xout->wc);
1604 odp_execute_actions(NULL, packet, &key, ctx->xout->odp_actions.data,
1605 ctx->xout->odp_actions.size, NULL, NULL);
1607 pin.packet = packet->data;
1608 pin.packet_len = packet->size;
1609 pin.reason = reason;
1610 pin.controller_id = controller_id;
1611 pin.table_id = ctx->table_id;
1612 pin.cookie = ctx->rule ? ctx->rule->up.flow_cookie : 0;
1615 flow_get_metadata(&ctx->xin->flow, &pin.fmd);
1617 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, &pin);
1618 ofpbuf_delete(packet);
1622 compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1624 struct flow_wildcards *wc = &ctx->xout->wc;
1625 struct flow *flow = &ctx->xin->flow;
1627 ovs_assert(eth_type_mpls(eth_type));
1629 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1630 memset(&wc->masks.mpls_depth, 0xff, sizeof wc->masks.mpls_depth);
1632 if (flow->mpls_depth) {
1633 flow->mpls_lse &= ~htonl(MPLS_BOS_MASK);
1639 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1640 label = htonl(0x2); /* IPV6 Explicit Null. */
1642 label = htonl(0x0); /* IPV4 Explicit Null. */
1644 wc->masks.nw_tos |= IP_DSCP_MASK;
1645 wc->masks.nw_ttl = 0xff;
1646 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
1647 ttl = flow->nw_ttl ? flow->nw_ttl : 0x40;
1648 flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
1649 flow->mpls_depth = 1;
1651 flow->dl_type = eth_type;
1655 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1657 struct flow_wildcards *wc = &ctx->xout->wc;
1658 struct flow *flow = &ctx->xin->flow;
1660 ovs_assert(eth_type_mpls(ctx->xin->flow.dl_type));
1661 ovs_assert(!eth_type_mpls(eth_type));
1663 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1664 memset(&wc->masks.mpls_depth, 0xff, sizeof wc->masks.mpls_depth);
1666 if (flow->mpls_depth) {
1668 flow->mpls_lse = htonl(0);
1669 if (!flow->mpls_depth) {
1670 flow->dl_type = eth_type;
1676 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
1678 struct flow *flow = &ctx->xin->flow;
1680 if (!is_ip_any(flow)) {
1684 ctx->xout->wc.masks.nw_ttl = 0xff;
1685 if (flow->nw_ttl > 1) {
1691 for (i = 0; i < ids->n_controllers; i++) {
1692 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
1696 /* Stop processing for current table. */
1702 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
1704 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
1708 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_TTL_MASK);
1709 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl);
1714 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
1716 struct flow *flow = &ctx->xin->flow;
1717 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse);
1718 struct flow_wildcards *wc = &ctx->xout->wc;
1720 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1722 if (!eth_type_mpls(flow->dl_type)) {
1728 set_mpls_lse_ttl(&flow->mpls_lse, ttl);
1731 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
1733 /* Stop processing for current table. */
1739 xlate_output_action(struct xlate_ctx *ctx,
1740 ofp_port_t port, uint16_t max_len, bool may_packet_in)
1742 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
1744 ctx->xout->nf_output_iface = NF_OUT_DROP;
1748 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
1751 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
1758 flood_packets(ctx, false);
1761 flood_packets(ctx, true);
1763 case OFPP_CONTROLLER:
1764 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
1770 if (port != ctx->xin->flow.in_port.ofp_port) {
1771 compose_output_action(ctx, port);
1773 xlate_report(ctx, "skipping output to input port");
1778 if (prev_nf_output_iface == NF_OUT_FLOOD) {
1779 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1780 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
1781 ctx->xout->nf_output_iface = prev_nf_output_iface;
1782 } else if (prev_nf_output_iface != NF_OUT_DROP &&
1783 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
1784 ctx->xout->nf_output_iface = NF_OUT_MULTI;
1789 xlate_output_reg_action(struct xlate_ctx *ctx,
1790 const struct ofpact_output_reg *or)
1792 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
1793 if (port <= UINT16_MAX) {
1794 union mf_subvalue value;
1796 memset(&value, 0xff, sizeof value);
1797 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
1798 xlate_output_action(ctx, u16_to_ofp(port),
1799 or->max_len, false);
1804 xlate_enqueue_action(struct xlate_ctx *ctx,
1805 const struct ofpact_enqueue *enqueue)
1807 ofp_port_t ofp_port = enqueue->port;
1808 uint32_t queue_id = enqueue->queue;
1809 uint32_t flow_priority, priority;
1812 /* Translate queue to priority. */
1813 error = ofproto_dpif_queue_to_priority(ctx->xbridge->ofproto, queue_id,
1816 /* Fall back to ordinary output action. */
1817 xlate_output_action(ctx, enqueue->port, 0, false);
1821 /* Check output port. */
1822 if (ofp_port == OFPP_IN_PORT) {
1823 ofp_port = ctx->xin->flow.in_port.ofp_port;
1824 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
1828 /* Add datapath actions. */
1829 flow_priority = ctx->xin->flow.skb_priority;
1830 ctx->xin->flow.skb_priority = priority;
1831 compose_output_action(ctx, ofp_port);
1832 ctx->xin->flow.skb_priority = flow_priority;
1834 /* Update NetFlow output port. */
1835 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
1836 ctx->xout->nf_output_iface = ofp_port;
1837 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
1838 ctx->xout->nf_output_iface = NF_OUT_MULTI;
1843 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
1845 uint32_t skb_priority;
1847 if (!ofproto_dpif_queue_to_priority(ctx->xbridge->ofproto, queue_id,
1849 ctx->xin->flow.skb_priority = skb_priority;
1851 /* Couldn't translate queue to a priority. Nothing to do. A warning
1852 * has already been logged. */
1857 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
1859 const struct xbridge *xbridge = xbridge_;
1870 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
1873 port = get_ofp_port(xbridge, ofp_port);
1874 return port ? port->may_enable : false;
1879 xlate_bundle_action(struct xlate_ctx *ctx,
1880 const struct ofpact_bundle *bundle)
1884 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
1886 CONST_CAST(struct xbridge *, ctx->xbridge));
1887 if (bundle->dst.field) {
1888 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
1891 xlate_output_action(ctx, port, 0, false);
1896 xlate_learn_action(struct xlate_ctx *ctx,
1897 const struct ofpact_learn *learn)
1899 struct ofputil_flow_mod *fm;
1900 struct ofpbuf ofpacts;
1902 ctx->xout->has_learn = true;
1904 learn_mask(learn, &ctx->xout->wc);
1906 if (!ctx->xin->may_learn) {
1910 fm = xmalloc(sizeof *fm);
1911 ofpbuf_init(&ofpacts, 0);
1912 learn_execute(learn, &ctx->xin->flow, fm, &ofpacts);
1914 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
1917 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
1918 * means "infinite". */
1920 reduce_timeout(uint16_t max, uint16_t *timeout)
1922 if (max && (!*timeout || *timeout > max)) {
1928 xlate_fin_timeout(struct xlate_ctx *ctx,
1929 const struct ofpact_fin_timeout *oft)
1931 if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
1932 struct rule_dpif *rule = ctx->rule;
1934 if (list_is_empty(&rule->up.expirable)) {
1935 list_insert(&rule->up.ofproto->expirable, &rule->up.expirable);
1938 reduce_timeout(oft->fin_idle_timeout, &rule->up.idle_timeout);
1939 reduce_timeout(oft->fin_hard_timeout, &rule->up.hard_timeout);
1944 xlate_sample_action(struct xlate_ctx *ctx,
1945 const struct ofpact_sample *os)
1947 union user_action_cookie cookie;
1948 /* Scale the probability from 16-bit to 32-bit while representing
1949 * the same percentage. */
1950 uint32_t probability = (os->probability << 16) | os->probability;
1952 commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1953 &ctx->xout->odp_actions, &ctx->xout->wc);
1955 compose_flow_sample_cookie(os->probability, os->collector_set_id,
1956 os->obs_domain_id, os->obs_point_id, &cookie);
1957 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
1958 probability, &cookie, sizeof cookie.flow_sample);
1962 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
1964 if (xport->config & (eth_addr_equals(ctx->xin->flow.dl_dst, eth_addr_stp)
1965 ? OFPUTIL_PC_NO_RECV_STP
1966 : OFPUTIL_PC_NO_RECV)) {
1970 /* Only drop packets here if both forwarding and learning are
1971 * disabled. If just learning is enabled, we need to have
1972 * OFPP_NORMAL and the learning action have a look at the packet
1973 * before we can drop it. */
1974 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
1982 tunnel_ecn_ok(struct xlate_ctx *ctx)
1984 if (is_ip_any(&ctx->base_flow)
1985 && (ctx->xin->flow.tunnel.ip_tos & IP_ECN_MASK) == IP_ECN_CE) {
1986 if ((ctx->base_flow.nw_tos & IP_ECN_MASK) == IP_ECN_NOT_ECT) {
1987 VLOG_WARN_RL(&rl, "dropping tunnel packet marked ECN CE"
1988 " but is not ECN capable");
1991 /* Set the ECN CE value in the tunneled packet. */
1992 ctx->xin->flow.nw_tos |= IP_ECN_CE;
2000 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
2001 struct xlate_ctx *ctx)
2003 struct flow_wildcards *wc = &ctx->xout->wc;
2004 struct flow *flow = &ctx->xin->flow;
2005 bool was_evictable = true;
2006 const struct ofpact *a;
2009 /* Don't let the rule we're working on get evicted underneath us. */
2010 was_evictable = ctx->rule->up.evictable;
2011 ctx->rule->up.evictable = false;
2014 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2015 struct ofpact_controller *controller;
2016 const struct ofpact_metadata *metadata;
2024 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
2025 ofpact_get_OUTPUT(a)->max_len, true);
2028 case OFPACT_CONTROLLER:
2029 controller = ofpact_get_CONTROLLER(a);
2030 execute_controller_action(ctx, controller->max_len,
2032 controller->controller_id);
2035 case OFPACT_ENQUEUE:
2036 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
2039 case OFPACT_SET_VLAN_VID:
2040 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2041 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
2042 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
2046 case OFPACT_SET_VLAN_PCP:
2047 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
2048 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
2050 htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp << VLAN_PCP_SHIFT)
2054 case OFPACT_STRIP_VLAN:
2055 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2056 flow->vlan_tci = htons(0);
2059 case OFPACT_PUSH_VLAN:
2060 /* XXX 802.1AD(QinQ) */
2061 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2062 flow->vlan_tci = htons(VLAN_CFI);
2065 case OFPACT_SET_ETH_SRC:
2066 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2067 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2070 case OFPACT_SET_ETH_DST:
2071 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2072 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2075 case OFPACT_SET_IPV4_SRC:
2076 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2077 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2078 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2082 case OFPACT_SET_IPV4_DST:
2083 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2084 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2085 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
2089 case OFPACT_SET_IPV4_DSCP:
2090 wc->masks.nw_tos |= IP_DSCP_MASK;
2091 /* OpenFlow 1.0 only supports IPv4. */
2092 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2093 flow->nw_tos &= ~IP_DSCP_MASK;
2094 flow->nw_tos |= ofpact_get_SET_IPV4_DSCP(a)->dscp;
2098 case OFPACT_SET_L4_SRC_PORT:
2099 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2100 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2101 if (is_ip_any(flow)) {
2102 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2106 case OFPACT_SET_L4_DST_PORT:
2107 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2108 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2109 if (is_ip_any(flow)) {
2110 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2114 case OFPACT_RESUBMIT:
2115 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
2118 case OFPACT_SET_TUNNEL:
2119 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2122 case OFPACT_SET_QUEUE:
2123 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2126 case OFPACT_POP_QUEUE:
2127 flow->skb_priority = ctx->orig_skb_priority;
2130 case OFPACT_REG_MOVE:
2131 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
2134 case OFPACT_REG_LOAD:
2135 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow);
2138 case OFPACT_STACK_PUSH:
2139 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
2143 case OFPACT_STACK_POP:
2144 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
2148 case OFPACT_PUSH_MPLS:
2149 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a)->ethertype);
2152 case OFPACT_POP_MPLS:
2153 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
2156 case OFPACT_SET_MPLS_TTL:
2157 if (compose_set_mpls_ttl_action(ctx,
2158 ofpact_get_SET_MPLS_TTL(a)->ttl)) {
2163 case OFPACT_DEC_MPLS_TTL:
2164 if (compose_dec_mpls_ttl_action(ctx)) {
2169 case OFPACT_DEC_TTL:
2170 wc->masks.nw_ttl = 0xff;
2171 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
2177 /* Nothing to do. */
2180 case OFPACT_MULTIPATH:
2181 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
2185 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
2188 case OFPACT_OUTPUT_REG:
2189 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
2193 xlate_learn_action(ctx, ofpact_get_LEARN(a));
2200 case OFPACT_FIN_TIMEOUT:
2201 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2202 ctx->xout->has_fin_timeout = true;
2203 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
2206 case OFPACT_CLEAR_ACTIONS:
2208 * Nothing to do because writa-actions is not supported for now.
2209 * When writa-actions is supported, clear-actions also must
2210 * be supported at the same time.
2214 case OFPACT_WRITE_METADATA:
2215 metadata = ofpact_get_WRITE_METADATA(a);
2216 flow->metadata &= ~metadata->mask;
2217 flow->metadata |= metadata->metadata & metadata->mask;
2221 /* Not implemented yet. */
2224 case OFPACT_GOTO_TABLE: {
2225 /* It is assumed that goto-table is the last action. */
2226 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
2228 ovs_assert(ctx->table_id < ogt->table_id);
2229 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2230 ogt->table_id, true);
2235 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
2242 ctx->rule->up.evictable = was_evictable;
2247 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
2248 const struct flow *flow, struct rule_dpif *rule,
2249 uint8_t tcp_flags, const struct ofpbuf *packet)
2251 xin->ofproto = ofproto;
2253 xin->packet = packet;
2254 xin->may_learn = packet != NULL;
2256 xin->ofpacts = NULL;
2257 xin->ofpacts_len = 0;
2258 xin->tcp_flags = tcp_flags;
2259 xin->resubmit_hook = NULL;
2260 xin->report_hook = NULL;
2261 xin->resubmit_stats = NULL;
2265 xlate_out_uninit(struct xlate_out *xout)
2268 ofpbuf_uninit(&xout->odp_actions);
2272 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
2273 * into datapath actions, using 'ctx', and discards the datapath actions. */
2275 xlate_actions_for_side_effects(struct xlate_in *xin)
2277 struct xlate_out xout;
2279 xlate_actions(xin, &xout);
2280 xlate_out_uninit(&xout);
2284 xlate_report(struct xlate_ctx *ctx, const char *s)
2286 if (ctx->xin->report_hook) {
2287 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
2292 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
2295 dst->slow = src->slow;
2296 dst->has_learn = src->has_learn;
2297 dst->has_normal = src->has_normal;
2298 dst->has_fin_timeout = src->has_fin_timeout;
2299 dst->nf_output_iface = src->nf_output_iface;
2300 dst->mirrors = src->mirrors;
2302 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
2303 sizeof dst->odp_actions_stub);
2304 ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
2305 src->odp_actions.size);
2308 static struct skb_priority_to_dscp *
2309 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
2311 struct skb_priority_to_dscp *pdscp;
2314 hash = hash_int(skb_priority, 0);
2315 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
2316 if (pdscp->skb_priority == skb_priority) {
2324 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
2327 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
2328 *dscp = pdscp ? pdscp->dscp : 0;
2329 return pdscp != NULL;
2333 clear_skb_priorities(struct xport *xport)
2335 struct skb_priority_to_dscp *pdscp, *next;
2337 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
2338 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
2344 actions_output_to_local_port(const struct xlate_ctx *ctx)
2346 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
2347 const struct nlattr *a;
2350 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions.data,
2351 ctx->xout->odp_actions.size) {
2352 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
2353 && nl_attr_get_odp_port(a) == local_odp_port) {
2360 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
2361 * into datapath actions in 'odp_actions', using 'ctx'. */
2363 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
2365 struct flow_wildcards *wc = &xout->wc;
2366 struct flow *flow = &xin->flow;
2368 enum slow_path_reason special;
2369 const struct ofpact *ofpacts;
2370 struct xport *in_port;
2371 struct flow orig_flow;
2372 struct xlate_ctx ctx;
2375 COVERAGE_INC(xlate_actions);
2377 /* Flow initialization rules:
2378 * - 'base_flow' must match the kernel's view of the packet at the
2379 * time that action processing starts. 'flow' represents any
2380 * transformations we wish to make through actions.
2381 * - By default 'base_flow' and 'flow' are the same since the input
2382 * packet matches the output before any actions are applied.
2383 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
2384 * of the received packet as seen by the kernel. If we later output
2385 * to another device without any modifications this will cause us to
2386 * insert a new tag since the original one was stripped off by the
2388 * - Tunnel metadata as received is retained in 'flow'. This allows
2389 * tunnel metadata matching also in later tables.
2390 * Since a kernel action for setting the tunnel metadata will only be
2391 * generated with actual tunnel output, changing the tunnel metadata
2392 * values in 'flow' (such as tun_id) will only have effect with a later
2393 * tunnel output action.
2394 * - Tunnel 'base_flow' is completely cleared since that is what the
2395 * kernel does. If we wish to maintain the original values an action
2396 * needs to be generated. */
2401 ctx.xout->has_learn = false;
2402 ctx.xout->has_normal = false;
2403 ctx.xout->has_fin_timeout = false;
2404 ctx.xout->nf_output_iface = NF_OUT_DROP;
2405 ctx.xout->mirrors = 0;
2406 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
2407 sizeof ctx.xout->odp_actions_stub);
2408 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
2410 ctx.xbridge = xbridge_lookup(xin->ofproto);
2415 ctx.rule = xin->rule;
2417 ctx.base_flow = *flow;
2418 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
2419 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
2421 flow_wildcards_init_catchall(wc);
2422 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
2423 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
2424 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
2425 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
2427 if (tnl_port_should_receive(&ctx.xin->flow)) {
2428 memset(&wc->masks.tunnel, 0xff, sizeof wc->masks.tunnel);
2429 /* skb_mark is currently used only by tunnels but that will likely
2430 * change in the future. */
2431 memset(&wc->masks.skb_mark, 0xff, sizeof wc->masks.skb_mark);
2433 if (ctx.xbridge->has_netflow) {
2434 netflow_mask_wc(flow, wc);
2438 ctx.orig_skb_priority = flow->skb_priority;
2443 ofpacts = xin->ofpacts;
2444 ofpacts_len = xin->ofpacts_len;
2445 } else if (xin->rule) {
2446 ofpacts = xin->rule->up.ofpacts;
2447 ofpacts_len = xin->rule->up.ofpacts_len;
2452 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
2454 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
2455 /* Do this conditionally because the copy is expensive enough that it
2456 * shows up in profiles. */
2460 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
2461 switch (ctx.xbridge->frag) {
2462 case OFPC_FRAG_NORMAL:
2463 /* We must pretend that transport ports are unavailable. */
2464 flow->tp_src = ctx.base_flow.tp_src = htons(0);
2465 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
2468 case OFPC_FRAG_DROP:
2471 case OFPC_FRAG_REASM:
2474 case OFPC_FRAG_NX_MATCH:
2475 /* Nothing to do. */
2478 case OFPC_INVALID_TTL_TO_CONTROLLER:
2483 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
2484 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
2486 ctx.xout->slow = special;
2488 size_t sample_actions_len;
2490 if (flow->in_port.ofp_port
2491 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
2492 flow->in_port.ofp_port,
2494 ctx.base_flow.vlan_tci = 0;
2497 add_sflow_action(&ctx);
2498 add_ipfix_action(&ctx);
2499 sample_actions_len = ctx.xout->odp_actions.size;
2501 if (tunnel_ecn_ok(&ctx) && (!in_port || may_receive(in_port, &ctx))) {
2502 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
2504 /* We've let OFPP_NORMAL and the learning action look at the
2505 * packet, so drop it now if forwarding is disabled. */
2506 if (in_port && !xport_stp_forward_state(in_port)) {
2507 ctx.xout->odp_actions.size = sample_actions_len;
2511 if (ctx.xbridge->has_in_band
2512 && in_band_must_output_to_local_port(flow)
2513 && !actions_output_to_local_port(&ctx)) {
2514 compose_output_action(&ctx, OFPP_LOCAL);
2517 fix_sflow_action(&ctx);
2519 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
2520 add_mirror_actions(&ctx, &orig_flow);
2524 ofpbuf_uninit(&ctx.stack);
2526 /* Clear the metadata and register wildcard masks, because we won't
2527 * use non-header fields as part of the cache. */
2528 memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
2529 memset(&wc->masks.regs, 0, sizeof wc->masks.regs);