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"
25 #include "byte-order.h"
30 #include "dynamic-string.h"
35 #include "mac-learning.h"
36 #include "meta-flow.h"
37 #include "multipath.h"
38 #include "netdev-vport.h"
41 #include "odp-execute.h"
42 #include "ofp-actions.h"
43 #include "ofproto/ofproto-dpif-ipfix.h"
44 #include "ofproto/ofproto-dpif-mirror.h"
45 #include "ofproto/ofproto-dpif-sflow.h"
46 #include "ofproto/ofproto-dpif.h"
50 COVERAGE_DEFINE(xlate_actions);
52 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
54 /* Maximum depth of flow table recursion (due to resubmit actions) in a
55 * flow translation. */
56 #define MAX_RESUBMIT_RECURSION 64
59 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
60 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
62 struct list xbundles; /* Owned xbundles. */
63 struct hmap xports; /* Indexed by ofp_port. */
65 char *name; /* Name used in log messages. */
66 struct dpif *dpif; /* Datapath interface. */
67 struct mac_learning *ml; /* Mac learning handle. */
68 struct mbridge *mbridge; /* Mirroring. */
69 struct dpif_sflow *sflow; /* SFlow handle, or null. */
70 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
71 struct stp *stp; /* STP or null if disabled. */
73 enum ofp_config_flags frag; /* Fragmentation handling. */
74 bool has_netflow; /* Bridge runs netflow? */
75 bool has_in_band; /* Bridge has in band control? */
76 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
80 struct hmap_node hmap_node; /* In global 'xbundles' map. */
81 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
83 struct list list_node; /* In parent 'xbridges' list. */
84 struct xbridge *xbridge; /* Parent xbridge. */
86 struct list xports; /* Contains "struct xport"s. */
88 char *name; /* Name used in log messages. */
89 struct bond *bond; /* Nonnull iff more than one port. */
90 struct lacp *lacp; /* LACP handle or null. */
92 enum port_vlan_mode vlan_mode; /* VLAN mode. */
93 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
94 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
95 * NULL if all VLANs are trunked. */
96 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
97 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
101 struct hmap_node hmap_node; /* Node in global 'xports' map. */
102 struct ofport_dpif *ofport; /* Key in global 'xports map. */
104 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
105 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
107 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
109 struct list bundle_node; /* In parent xbundle (if it exists). */
110 struct xbundle *xbundle; /* Parent xbundle or null. */
112 struct netdev *netdev; /* 'ofport''s netdev. */
114 struct xbridge *xbridge; /* Parent bridge. */
115 struct xport *peer; /* Patch port peer or null. */
117 enum ofputil_port_config config; /* OpenFlow port configuration. */
118 int stp_port_no; /* STP port number or 0 if not in use. */
120 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
122 bool may_enable; /* May be enabled in bonds. */
123 bool is_tunnel; /* Is a tunnel port. */
125 struct cfm *cfm; /* CFM handle or null. */
126 struct bfd *bfd; /* BFD handle or null. */
130 struct xlate_in *xin;
131 struct xlate_out *xout;
133 const struct xbridge *xbridge;
135 /* Flow at the last commit. */
136 struct flow base_flow;
138 /* Tunnel IP destination address as received. This is stored separately
139 * as the base_flow.tunnel is cleared on init to reflect the datapath
140 * behavior. Used to make sure not to send tunneled output to ourselves,
141 * which might lead to an infinite loop. This could happen easily
142 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
143 * actually set the tun_dst field. */
144 ovs_be32 orig_tunnel_ip_dst;
146 /* Stack for the push and pop actions. Each stack element is of type
147 * "union mf_subvalue". */
148 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
151 /* The rule that we are currently translating, or NULL. */
152 struct rule_dpif *rule;
154 int recurse; /* Recursion level, via xlate_table_action. */
155 uint32_t orig_skb_priority; /* Priority when packet arrived. */
156 uint8_t table_id; /* OpenFlow table ID where flow was found. */
157 uint32_t sflow_n_outputs; /* Number of output ports. */
158 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
159 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
160 bool exit; /* No further actions should be processed. */
163 /* A controller may use OFPP_NONE as the ingress port to indicate that
164 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
165 * when an input bundle is needed for validation (e.g., mirroring or
166 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
167 * any 'port' structs, so care must be taken when dealing with it.
168 * The bundle's name and vlan mode are initialized in lookup_input_bundle() */
169 static struct xbundle ofpp_none_bundle;
171 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
172 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
173 * traffic egressing the 'ofport' with that priority should be marked with. */
174 struct skb_priority_to_dscp {
175 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
176 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
178 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
181 static struct hmap xbridges = HMAP_INITIALIZER(&xbridges);
182 static struct hmap xbundles = HMAP_INITIALIZER(&xbundles);
183 static struct hmap xports = HMAP_INITIALIZER(&xports);
185 static bool may_receive(const struct xport *, struct xlate_ctx *);
186 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
188 static void xlate_normal(struct xlate_ctx *);
189 static void xlate_report(struct xlate_ctx *, const char *);
190 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
191 uint8_t table_id, bool may_packet_in);
192 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
193 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
194 static void output_normal(struct xlate_ctx *, const struct xbundle *,
196 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
198 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
200 static struct xbridge *xbridge_lookup(const struct ofproto_dpif *);
201 static struct xbundle *xbundle_lookup(const struct ofbundle *);
202 static struct xport *xport_lookup(const struct ofport_dpif *);
203 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
204 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
205 uint32_t skb_priority);
206 static void clear_skb_priorities(struct xport *);
207 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
211 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
212 struct dpif *dpif, const struct mac_learning *ml,
213 struct stp *stp, const struct mbridge *mbridge,
214 const struct dpif_sflow *sflow,
215 const struct dpif_ipfix *ipfix, enum ofp_config_flags frag,
216 bool forward_bpdu, bool has_in_band, bool has_netflow)
218 struct xbridge *xbridge = xbridge_lookup(ofproto);
221 xbridge = xzalloc(sizeof *xbridge);
222 xbridge->ofproto = ofproto;
224 hmap_insert(&xbridges, &xbridge->hmap_node, hash_pointer(ofproto, 0));
225 hmap_init(&xbridge->xports);
226 list_init(&xbridge->xbundles);
229 if (xbridge->ml != ml) {
230 mac_learning_unref(xbridge->ml);
231 xbridge->ml = mac_learning_ref(ml);
234 if (xbridge->mbridge != mbridge) {
235 mbridge_unref(xbridge->mbridge);
236 xbridge->mbridge = mbridge_ref(mbridge);
239 if (xbridge->sflow != sflow) {
240 dpif_sflow_unref(xbridge->sflow);
241 xbridge->sflow = dpif_sflow_ref(sflow);
244 if (xbridge->ipfix != ipfix) {
245 dpif_ipfix_unref(xbridge->ipfix);
246 xbridge->ipfix = dpif_ipfix_ref(ipfix);
249 if (xbridge->stp != stp) {
250 stp_unref(xbridge->stp);
251 xbridge->stp = stp_ref(stp);
255 xbridge->name = xstrdup(name);
257 xbridge->dpif = dpif;
258 xbridge->forward_bpdu = forward_bpdu;
259 xbridge->has_in_band = has_in_band;
260 xbridge->has_netflow = has_netflow;
261 xbridge->frag = frag;
265 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
267 struct xbridge *xbridge = xbridge_lookup(ofproto);
268 struct xbundle *xbundle, *next_xbundle;
269 struct xport *xport, *next_xport;
275 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
276 xlate_ofport_remove(xport->ofport);
279 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
280 xlate_bundle_remove(xbundle->ofbundle);
283 hmap_remove(&xbridges, &xbridge->hmap_node);
289 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
290 const char *name, enum port_vlan_mode vlan_mode, int vlan,
291 unsigned long *trunks, bool use_priority_tags,
292 const struct bond *bond, const struct lacp *lacp,
295 struct xbundle *xbundle = xbundle_lookup(ofbundle);
298 xbundle = xzalloc(sizeof *xbundle);
299 xbundle->ofbundle = ofbundle;
300 xbundle->xbridge = xbridge_lookup(ofproto);
302 hmap_insert(&xbundles, &xbundle->hmap_node, hash_pointer(ofbundle, 0));
303 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
304 list_init(&xbundle->xports);
307 ovs_assert(xbundle->xbridge);
310 xbundle->name = xstrdup(name);
312 xbundle->vlan_mode = vlan_mode;
313 xbundle->vlan = vlan;
314 xbundle->trunks = trunks;
315 xbundle->use_priority_tags = use_priority_tags;
316 xbundle->floodable = floodable;
318 if (xbundle->bond != bond) {
319 bond_unref(xbundle->bond);
320 xbundle->bond = bond_ref(bond);
323 if (xbundle->lacp != lacp) {
324 lacp_unref(xbundle->lacp);
325 xbundle->lacp = lacp_ref(lacp);
330 xlate_bundle_remove(struct ofbundle *ofbundle)
332 struct xbundle *xbundle = xbundle_lookup(ofbundle);
333 struct xport *xport, *next;
339 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
340 list_remove(&xport->bundle_node);
341 xport->xbundle = NULL;
344 hmap_remove(&xbundles, &xbundle->hmap_node);
345 list_remove(&xbundle->list_node);
346 bond_unref(xbundle->bond);
347 lacp_unref(xbundle->lacp);
353 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
354 struct ofport_dpif *ofport, ofp_port_t ofp_port,
355 odp_port_t odp_port, const struct netdev *netdev,
356 const struct cfm *cfm, const struct bfd *bfd,
357 struct ofport_dpif *peer, int stp_port_no,
358 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
359 enum ofputil_port_config config, bool is_tunnel,
362 struct xport *xport = xport_lookup(ofport);
366 xport = xzalloc(sizeof *xport);
367 xport->ofport = ofport;
368 xport->xbridge = xbridge_lookup(ofproto);
369 xport->ofp_port = ofp_port;
371 hmap_init(&xport->skb_priorities);
372 hmap_insert(&xports, &xport->hmap_node, hash_pointer(ofport, 0));
373 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
374 hash_ofp_port(xport->ofp_port));
377 ovs_assert(xport->ofp_port == ofp_port);
379 xport->config = config;
380 xport->stp_port_no = stp_port_no;
381 xport->is_tunnel = is_tunnel;
382 xport->may_enable = may_enable;
383 xport->odp_port = odp_port;
385 if (xport->netdev != netdev) {
386 netdev_close(xport->netdev);
387 xport->netdev = netdev_ref(netdev);
390 if (xport->cfm != cfm) {
391 cfm_unref(xport->cfm);
392 xport->cfm = cfm_ref(cfm);
395 if (xport->bfd != bfd) {
396 bfd_unref(xport->bfd);
397 xport->bfd = bfd_ref(bfd);
401 xport->peer->peer = NULL;
403 xport->peer = xport_lookup(peer);
405 xport->peer->peer = xport;
408 if (xport->xbundle) {
409 list_remove(&xport->bundle_node);
411 xport->xbundle = xbundle_lookup(ofbundle);
412 if (xport->xbundle) {
413 list_insert(&xport->xbundle->xports, &xport->bundle_node);
416 clear_skb_priorities(xport);
417 for (i = 0; i < n_qdscp; i++) {
418 struct skb_priority_to_dscp *pdscp;
419 uint32_t skb_priority;
421 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
426 pdscp = xmalloc(sizeof *pdscp);
427 pdscp->skb_priority = skb_priority;
428 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
429 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
430 hash_int(pdscp->skb_priority, 0));
435 xlate_ofport_remove(struct ofport_dpif *ofport)
437 struct xport *xport = xport_lookup(ofport);
444 xport->peer->peer = NULL;
448 if (xport->xbundle) {
449 list_remove(&xport->bundle_node);
452 clear_skb_priorities(xport);
453 hmap_destroy(&xport->skb_priorities);
455 hmap_remove(&xports, &xport->hmap_node);
456 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
458 netdev_close(xport->netdev);
459 cfm_unref(xport->cfm);
460 bfd_unref(xport->bfd);
464 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
465 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
466 * Optionally, if nonnull, populates 'fitnessp' with the fitness of 'flow' as
467 * returned by odp_flow_key_to_flow(). Also, optionally populates 'ofproto'
468 * with the ofproto_dpif, and 'odp_in_port' with the datapath in_port, that
469 * 'packet' ingressed.
471 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
472 * 'flow''s in_port to OFPP_NONE.
474 * This function does post-processing on data returned from
475 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
476 * of the upcall processing logic. In particular, if the extracted in_port is
477 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
478 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
479 * a VLAN header onto 'packet' (if it is nonnull).
481 * Similarly, this function also includes some logic to help with tunnels. It
482 * may modify 'flow' as necessary to make the tunneling implementation
483 * transparent to the upcall processing logic.
485 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
486 * or some other positive errno if there are other problems. */
488 xlate_receive(const struct dpif_backer *backer, struct ofpbuf *packet,
489 const struct nlattr *key, size_t key_len,
490 struct flow *flow, enum odp_key_fitness *fitnessp,
491 struct ofproto_dpif **ofproto, odp_port_t *odp_in_port)
493 enum odp_key_fitness fitness;
494 const struct xport *xport;
497 fitness = odp_flow_key_to_flow(key, key_len, flow);
498 if (fitness == ODP_FIT_ERROR) {
504 *odp_in_port = flow->in_port.odp_port;
507 xport = xport_lookup(tnl_port_should_receive(flow)
508 ? tnl_port_receive(flow)
509 : odp_port_to_ofport(backer, flow->in_port.odp_port));
511 flow->in_port.ofp_port = xport ? xport->ofp_port : OFPP_NONE;
516 if (vsp_adjust_flow(xport->xbridge->ofproto, flow)) {
518 /* Make the packet resemble the flow, so that it gets sent to
519 * an OpenFlow controller properly, so that it looks correct
520 * for sFlow, and so that flow_extract() will get the correct
521 * vlan_tci if it is called on 'packet'.
523 * The allocated space inside 'packet' probably also contains
524 * 'key', that is, both 'packet' and 'key' are probably part of
525 * a struct dpif_upcall (see the large comment on that
526 * structure definition), so pushing data on 'packet' is in
527 * general not a good idea since it could overwrite 'key' or
528 * free it as a side effect. However, it's OK in this special
529 * case because we know that 'packet' is inside a Netlink
530 * attribute: pushing 4 bytes will just overwrite the 4-byte
531 * "struct nlattr", which is fine since we don't need that
533 eth_push_vlan(packet, flow->vlan_tci);
535 /* We can't reproduce 'key' from 'flow'. */
536 fitness = fitness == ODP_FIT_PERFECT ? ODP_FIT_TOO_MUCH : fitness;
541 *ofproto = xport->xbridge->ofproto;
551 static struct xbridge *
552 xbridge_lookup(const struct ofproto_dpif *ofproto)
554 struct xbridge *xbridge;
560 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
562 if (xbridge->ofproto == ofproto) {
569 static struct xbundle *
570 xbundle_lookup(const struct ofbundle *ofbundle)
572 struct xbundle *xbundle;
578 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
580 if (xbundle->ofbundle == ofbundle) {
587 static struct xport *
588 xport_lookup(const struct ofport_dpif *ofport)
596 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
598 if (xport->ofport == ofport) {
605 static struct stp_port *
606 xport_get_stp_port(const struct xport *xport)
608 return xport->xbridge->stp && xport->stp_port_no
609 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
613 static enum stp_state
614 xport_stp_learn_state(const struct xport *xport)
616 struct stp_port *sp = xport_get_stp_port(xport);
617 return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
621 xport_stp_forward_state(const struct xport *xport)
623 struct stp_port *sp = xport_get_stp_port(xport);
624 return stp_forward_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
627 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
628 * were used to make the determination.*/
630 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
632 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
633 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
637 stp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
639 struct stp_port *sp = xport_get_stp_port(xport);
640 struct ofpbuf payload = *packet;
641 struct eth_header *eth = payload.data;
643 /* Sink packets on ports that have STP disabled when the bridge has
645 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
649 /* Trim off padding on payload. */
650 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
651 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
654 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
655 stp_received_bpdu(sp, payload.data, payload.size);
659 static struct xport *
660 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
664 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
666 if (xport->ofp_port == ofp_port) {
674 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
676 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
677 return xport ? xport->odp_port : ODPP_NONE;
681 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
683 return (bundle->vlan_mode != PORT_VLAN_ACCESS
684 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
688 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
690 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
694 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
696 return xbundle != &ofpp_none_bundle
697 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
702 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
704 return xbundle != &ofpp_none_bundle
705 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
710 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
712 return xbundle != &ofpp_none_bundle
713 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
717 static struct xbundle *
718 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
719 bool warn, struct xport **in_xportp)
723 /* Find the port and bundle for the received packet. */
724 xport = get_ofp_port(xbridge, in_port);
728 if (xport && xport->xbundle) {
729 return xport->xbundle;
732 /* Special-case OFPP_NONE, which a controller may use as the ingress
733 * port for traffic that it is sourcing. */
734 if (in_port == OFPP_NONE) {
735 ofpp_none_bundle.name = "OFPP_NONE";
736 ofpp_none_bundle.vlan_mode = PORT_VLAN_TRUNK;
737 return &ofpp_none_bundle;
740 /* Odd. A few possible reasons here:
742 * - We deleted a port but there are still a few packets queued up
745 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
746 * we don't know about.
748 * - The ofproto client didn't configure the port as part of a bundle.
749 * This is particularly likely to happen if a packet was received on the
750 * port after it was created, but before the client had a chance to
751 * configure its bundle.
754 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
756 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
757 "port %"PRIu16, xbridge->name, in_port);
763 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
765 const struct xbridge *xbridge = ctx->xbridge;
766 mirror_mask_t mirrors;
767 struct xbundle *in_xbundle;
771 mirrors = ctx->xout->mirrors;
772 ctx->xout->mirrors = 0;
774 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
775 ctx->xin->packet != NULL, NULL);
779 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
781 /* Drop frames on bundles reserved for mirroring. */
782 if (xbundle_mirror_out(xbridge, in_xbundle)) {
783 if (ctx->xin->packet != NULL) {
784 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
785 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
786 "%s, which is reserved exclusively for mirroring",
787 ctx->xbridge->name, in_xbundle->name);
789 ofpbuf_clear(&ctx->xout->odp_actions);
794 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
795 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
798 vlan = input_vid_to_vlan(in_xbundle, vid);
804 /* Restore the original packet before adding the mirror actions. */
805 ctx->xin->flow = *orig_flow;
808 mirror_mask_t dup_mirrors;
809 struct ofbundle *out;
810 unsigned long *vlans;
815 has_mirror = mirror_get(xbridge->mbridge, mirror_mask_ffs(mirrors) - 1,
816 &vlans, &dup_mirrors, &out, &out_vlan);
817 ovs_assert(has_mirror);
820 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
822 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
825 if (!vlan_mirrored) {
826 mirrors = zero_rightmost_1bit(mirrors);
830 mirrors &= ~dup_mirrors;
831 ctx->xout->mirrors |= dup_mirrors;
833 struct xbundle *out_xbundle = xbundle_lookup(out);
835 output_normal(ctx, out_xbundle, vlan);
837 } else if (vlan != out_vlan
838 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
839 struct xbundle *xbundle;
841 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
842 if (xbundle_includes_vlan(xbundle, out_vlan)
843 && !xbundle_mirror_out(xbridge, xbundle)) {
844 output_normal(ctx, xbundle, out_vlan);
851 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
852 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
853 * the bundle on which the packet was received, returns the VLAN to which the
856 * Both 'vid' and the return value are in the range 0...4095. */
858 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
860 switch (in_xbundle->vlan_mode) {
861 case PORT_VLAN_ACCESS:
862 return in_xbundle->vlan;
865 case PORT_VLAN_TRUNK:
868 case PORT_VLAN_NATIVE_UNTAGGED:
869 case PORT_VLAN_NATIVE_TAGGED:
870 return vid ? vid : in_xbundle->vlan;
877 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
878 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
881 * 'vid' should be the VID obtained from the 802.1Q header that was received as
882 * part of a packet (specify 0 if there was no 802.1Q header), in the range
885 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
887 /* Allow any VID on the OFPP_NONE port. */
888 if (in_xbundle == &ofpp_none_bundle) {
892 switch (in_xbundle->vlan_mode) {
893 case PORT_VLAN_ACCESS:
896 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
897 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
898 "packet received on port %s configured as VLAN "
899 "%"PRIu16" access port", vid, in_xbundle->name,
906 case PORT_VLAN_NATIVE_UNTAGGED:
907 case PORT_VLAN_NATIVE_TAGGED:
909 /* Port must always carry its native VLAN. */
913 case PORT_VLAN_TRUNK:
914 if (!xbundle_includes_vlan(in_xbundle, vid)) {
916 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
917 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
918 "received on port %s not configured for trunking "
919 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
931 /* Given 'vlan', the VLAN that a packet belongs to, and
932 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
933 * that should be included in the 802.1Q header. (If the return value is 0,
934 * then the 802.1Q header should only be included in the packet if there is a
937 * Both 'vlan' and the return value are in the range 0...4095. */
939 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
941 switch (out_xbundle->vlan_mode) {
942 case PORT_VLAN_ACCESS:
945 case PORT_VLAN_TRUNK:
946 case PORT_VLAN_NATIVE_TAGGED:
949 case PORT_VLAN_NATIVE_UNTAGGED:
950 return vlan == out_xbundle->vlan ? 0 : vlan;
958 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
961 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
963 ovs_be16 tci, old_tci;
966 vid = output_vlan_to_vid(out_xbundle, vlan);
967 if (list_is_empty(&out_xbundle->xports)) {
968 /* Partially configured bundle with no slaves. Drop the packet. */
970 } else if (!out_xbundle->bond) {
971 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
974 struct ofport_dpif *ofport;
976 ofport = bond_choose_output_slave(out_xbundle->bond, &ctx->xin->flow,
977 &ctx->xout->wc, vid);
978 xport = xport_lookup(ofport);
981 /* No slaves enabled, so drop packet. */
988 if (tci || out_xbundle->use_priority_tags) {
989 tci |= *flow_tci & htons(VLAN_PCP_MASK);
991 tci |= htons(VLAN_CFI);
996 compose_output_action(ctx, xport->ofp_port);
1000 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1001 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1002 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1004 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1006 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1010 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1011 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1015 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1016 if (flow->nw_proto == ARP_OP_REPLY) {
1018 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1019 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1020 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1022 return flow->nw_src == flow->nw_dst;
1029 update_learning_table(const struct xbridge *xbridge,
1030 const struct flow *flow, struct flow_wildcards *wc,
1031 int vlan, struct xbundle *in_xbundle)
1033 struct mac_entry *mac;
1035 /* Don't learn the OFPP_NONE port. */
1036 if (in_xbundle == &ofpp_none_bundle) {
1040 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1041 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1045 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1046 if (is_gratuitous_arp(flow, wc)) {
1047 /* We don't want to learn from gratuitous ARP packets that are
1048 * reflected back over bond slaves so we lock the learning table. */
1049 if (!in_xbundle->bond) {
1050 mac_entry_set_grat_arp_lock(mac);
1051 } else if (mac_entry_is_grat_arp_locked(mac)) {
1056 if (mac->port.p != in_xbundle->ofbundle) {
1057 /* The log messages here could actually be useful in debugging,
1058 * so keep the rate limit relatively high. */
1059 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1060 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1061 "on port %s in VLAN %d",
1062 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1063 in_xbundle->name, vlan);
1065 mac->port.p = in_xbundle->ofbundle;
1066 mac_learning_changed(xbridge->ml);
1069 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1072 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1073 * dropped. Returns true if they may be forwarded, false if they should be
1076 * 'in_port' must be the xport that corresponds to flow->in_port.
1077 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1079 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1080 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1081 * checked by input_vid_is_valid().
1083 * May also add tags to '*tags', although the current implementation only does
1084 * so in one special case.
1087 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1090 struct xbundle *in_xbundle = in_port->xbundle;
1091 const struct xbridge *xbridge = ctx->xbridge;
1092 struct flow *flow = &ctx->xin->flow;
1094 /* Drop frames for reserved multicast addresses
1095 * only if forward_bpdu option is absent. */
1096 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1097 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1101 if (in_xbundle->bond) {
1102 struct mac_entry *mac;
1104 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1110 xlate_report(ctx, "bonding refused admissibility, dropping");
1113 case BV_DROP_IF_MOVED:
1114 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1115 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1116 if (mac && mac->port.p != in_xbundle->ofbundle &&
1117 (!is_gratuitous_arp(flow, &ctx->xout->wc)
1118 || mac_entry_is_grat_arp_locked(mac))) {
1119 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1120 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1124 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1133 xlate_normal(struct xlate_ctx *ctx)
1135 struct flow_wildcards *wc = &ctx->xout->wc;
1136 struct flow *flow = &ctx->xin->flow;
1137 struct xbundle *in_xbundle;
1138 struct xport *in_port;
1139 struct mac_entry *mac;
1143 ctx->xout->has_normal = true;
1145 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1146 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1147 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1149 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
1150 ctx->xin->packet != NULL, &in_port);
1152 xlate_report(ctx, "no input bundle, dropping");
1156 /* Drop malformed frames. */
1157 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
1158 !(flow->vlan_tci & htons(VLAN_CFI))) {
1159 if (ctx->xin->packet != NULL) {
1160 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1161 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
1162 "VLAN tag received on port %s",
1163 ctx->xbridge->name, in_xbundle->name);
1165 xlate_report(ctx, "partial VLAN tag, dropping");
1169 /* Drop frames on bundles reserved for mirroring. */
1170 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
1171 if (ctx->xin->packet != NULL) {
1172 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1173 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1174 "%s, which is reserved exclusively for mirroring",
1175 ctx->xbridge->name, in_xbundle->name);
1177 xlate_report(ctx, "input port is mirror output port, dropping");
1182 vid = vlan_tci_to_vid(flow->vlan_tci);
1183 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1184 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
1187 vlan = input_vid_to_vlan(in_xbundle, vid);
1189 /* Check other admissibility requirements. */
1190 if (in_port && !is_admissible(ctx, in_port, vlan)) {
1194 /* Learn source MAC. */
1195 if (ctx->xin->may_learn) {
1196 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
1199 /* Determine output bundle. */
1200 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
1201 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
1203 struct xbundle *mac_xbundle = xbundle_lookup(mac->port.p);
1204 if (mac_xbundle && mac_xbundle != in_xbundle) {
1205 xlate_report(ctx, "forwarding to learned port");
1206 output_normal(ctx, mac_xbundle, vlan);
1207 } else if (!mac_xbundle) {
1208 xlate_report(ctx, "learned port is unknown, dropping");
1210 xlate_report(ctx, "learned port is input port, dropping");
1213 struct xbundle *xbundle;
1215 xlate_report(ctx, "no learned MAC for destination, flooding");
1216 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1217 if (xbundle != in_xbundle
1218 && xbundle_includes_vlan(xbundle, vlan)
1219 && xbundle->floodable
1220 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1221 output_normal(ctx, xbundle, vlan);
1224 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1226 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
1229 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1230 * the number of packets out of UINT32_MAX to sample. The given
1231 * cookie is passed back in the callback for each sampled packet.
1234 compose_sample_action(const struct xbridge *xbridge,
1235 struct ofpbuf *odp_actions,
1236 const struct flow *flow,
1237 const uint32_t probability,
1238 const union user_action_cookie *cookie,
1239 const size_t cookie_size)
1241 size_t sample_offset, actions_offset;
1242 odp_port_t odp_port;
1246 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
1248 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
1250 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
1252 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
1253 pid = dpif_port_get_pid(xbridge->dpif, odp_port);
1254 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size, odp_actions);
1256 nl_msg_end_nested(odp_actions, actions_offset);
1257 nl_msg_end_nested(odp_actions, sample_offset);
1258 return cookie_offset;
1262 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
1263 odp_port_t odp_port, unsigned int n_outputs,
1264 union user_action_cookie *cookie)
1268 cookie->type = USER_ACTION_COOKIE_SFLOW;
1269 cookie->sflow.vlan_tci = vlan_tci;
1271 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1272 * port information") for the interpretation of cookie->output. */
1273 switch (n_outputs) {
1275 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1276 cookie->sflow.output = 0x40000000 | 256;
1280 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
1282 cookie->sflow.output = ifindex;
1287 /* 0x80000000 means "multiple output ports. */
1288 cookie->sflow.output = 0x80000000 | n_outputs;
1293 /* Compose SAMPLE action for sFlow bridge sampling. */
1295 compose_sflow_action(const struct xbridge *xbridge,
1296 struct ofpbuf *odp_actions,
1297 const struct flow *flow,
1298 odp_port_t odp_port)
1300 uint32_t probability;
1301 union user_action_cookie cookie;
1303 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
1307 probability = dpif_sflow_get_probability(xbridge->sflow);
1308 compose_sflow_cookie(xbridge, htons(0), odp_port,
1309 odp_port == ODPP_NONE ? 0 : 1, &cookie);
1311 return compose_sample_action(xbridge, odp_actions, flow, probability,
1312 &cookie, sizeof cookie.sflow);
1316 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
1317 uint32_t obs_domain_id, uint32_t obs_point_id,
1318 union user_action_cookie *cookie)
1320 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
1321 cookie->flow_sample.probability = probability;
1322 cookie->flow_sample.collector_set_id = collector_set_id;
1323 cookie->flow_sample.obs_domain_id = obs_domain_id;
1324 cookie->flow_sample.obs_point_id = obs_point_id;
1328 compose_ipfix_cookie(union user_action_cookie *cookie)
1330 cookie->type = USER_ACTION_COOKIE_IPFIX;
1333 /* Compose SAMPLE action for IPFIX bridge sampling. */
1335 compose_ipfix_action(const struct xbridge *xbridge,
1336 struct ofpbuf *odp_actions,
1337 const struct flow *flow)
1339 uint32_t probability;
1340 union user_action_cookie cookie;
1342 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
1346 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
1347 compose_ipfix_cookie(&cookie);
1349 compose_sample_action(xbridge, odp_actions, flow, probability,
1350 &cookie, sizeof cookie.ipfix);
1353 /* SAMPLE action for sFlow must be first action in any given list of
1354 * actions. At this point we do not have all information required to
1355 * build it. So try to build sample action as complete as possible. */
1357 add_sflow_action(struct xlate_ctx *ctx)
1359 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
1360 &ctx->xout->odp_actions,
1361 &ctx->xin->flow, ODPP_NONE);
1362 ctx->sflow_odp_port = 0;
1363 ctx->sflow_n_outputs = 0;
1366 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
1367 * of actions, eventually after the SAMPLE action for sFlow. */
1369 add_ipfix_action(struct xlate_ctx *ctx)
1371 compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions,
1375 /* Fix SAMPLE action according to data collected while composing ODP actions.
1376 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
1377 * USERSPACE action's user-cookie which is required for sflow. */
1379 fix_sflow_action(struct xlate_ctx *ctx)
1381 const struct flow *base = &ctx->base_flow;
1382 union user_action_cookie *cookie;
1384 if (!ctx->user_cookie_offset) {
1388 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
1389 sizeof cookie->sflow);
1390 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
1392 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
1393 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
1396 static enum slow_path_reason
1397 process_special(struct xlate_ctx *ctx, const struct flow *flow,
1398 const struct xport *xport, const struct ofpbuf *packet)
1400 struct flow_wildcards *wc = &ctx->xout->wc;
1401 const struct xbridge *xbridge = ctx->xbridge;
1405 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
1407 cfm_process_heartbeat(xport->cfm, packet);
1410 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
1412 bfd_process_packet(xport->bfd, flow, packet);
1415 } else if (xport->xbundle && xport->xbundle->lacp
1416 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1418 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
1421 } else if (xbridge->stp && stp_should_process_flow(flow, wc)) {
1423 stp_process_packet(xport, packet);
1432 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
1435 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1436 struct flow_wildcards *wc = &ctx->xout->wc;
1437 struct flow *flow = &ctx->xin->flow;
1438 ovs_be16 flow_vlan_tci;
1439 uint32_t flow_skb_mark;
1440 uint8_t flow_nw_tos;
1441 odp_port_t out_port, odp_port;
1444 /* If 'struct flow' gets additional metadata, we'll need to zero it out
1445 * before traversing a patch port. */
1446 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 20);
1449 xlate_report(ctx, "Nonexistent output port");
1451 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
1452 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
1454 } else if (check_stp && !xport_stp_forward_state(xport)) {
1455 xlate_report(ctx, "STP not in forwarding state, skipping output");
1459 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
1460 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
1465 const struct xport *peer = xport->peer;
1466 struct flow old_flow = ctx->xin->flow;
1467 enum slow_path_reason special;
1469 ctx->xbridge = peer->xbridge;
1470 flow->in_port.ofp_port = peer->ofp_port;
1471 flow->metadata = htonll(0);
1472 memset(&flow->tunnel, 0, sizeof flow->tunnel);
1473 memset(flow->regs, 0, sizeof flow->regs);
1475 special = process_special(ctx, &ctx->xin->flow, peer,
1478 ctx->xout->slow = special;
1479 } else if (may_receive(peer, ctx)) {
1480 if (xport_stp_forward_state(peer)) {
1481 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1483 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
1484 * learning action look at the packet, then drop it. */
1485 struct flow old_base_flow = ctx->base_flow;
1486 size_t old_size = ctx->xout->odp_actions.size;
1487 mirror_mask_t old_mirrors = ctx->xout->mirrors;
1488 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1489 ctx->xout->mirrors = old_mirrors;
1490 ctx->base_flow = old_base_flow;
1491 ctx->xout->odp_actions.size = old_size;
1495 ctx->xin->flow = old_flow;
1496 ctx->xbridge = xport->xbundle->xbridge;
1498 if (ctx->xin->resubmit_stats) {
1499 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1500 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
1506 flow_vlan_tci = flow->vlan_tci;
1507 flow_skb_mark = flow->skb_mark;
1508 flow_nw_tos = flow->nw_tos;
1510 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
1511 wc->masks.nw_tos |= IP_ECN_MASK;
1512 flow->nw_tos &= ~IP_DSCP_MASK;
1513 flow->nw_tos |= dscp;
1516 if (xport->is_tunnel) {
1517 /* Save tunnel metadata so that changes made due to
1518 * the Logical (tunnel) Port are not visible for any further
1519 * matches, while explicit set actions on tunnel metadata are.
1521 struct flow_tnl flow_tnl = flow->tunnel;
1522 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
1523 if (odp_port == ODPP_NONE) {
1524 xlate_report(ctx, "Tunneling decided against output");
1525 goto out; /* restore flow_nw_tos */
1527 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
1528 xlate_report(ctx, "Not tunneling to our own address");
1529 goto out; /* restore flow_nw_tos */
1531 if (ctx->xin->resubmit_stats) {
1532 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1534 out_port = odp_port;
1535 commit_odp_tunnel_action(flow, &ctx->base_flow,
1536 &ctx->xout->odp_actions);
1537 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
1539 ofp_port_t vlandev_port;
1541 odp_port = xport->odp_port;
1542 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
1543 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1545 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto, ofp_port,
1547 if (vlandev_port == ofp_port) {
1548 out_port = odp_port;
1550 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
1551 flow->vlan_tci = htons(0);
1553 flow->skb_mark &= ~IPSEC_MARK;
1556 if (out_port != ODPP_NONE) {
1557 commit_odp_actions(flow, &ctx->base_flow,
1558 &ctx->xout->odp_actions, &ctx->xout->wc);
1559 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
1562 ctx->sflow_odp_port = odp_port;
1563 ctx->sflow_n_outputs++;
1564 ctx->xout->nf_output_iface = ofp_port;
1569 flow->vlan_tci = flow_vlan_tci;
1570 flow->skb_mark = flow_skb_mark;
1571 flow->nw_tos = flow_nw_tos;
1575 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
1577 compose_output_action__(ctx, ofp_port, true);
1580 /* Common rule processing in one place to avoid duplicating code. */
1581 static struct rule_dpif *
1582 ctx_rule_hooks(struct xlate_ctx *ctx, struct rule_dpif *rule,
1585 if (ctx->xin->resubmit_hook) {
1586 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
1588 if (rule == NULL && may_packet_in) {
1590 * check if table configuration flags
1591 * OFPTC_TABLE_MISS_CONTROLLER, default.
1592 * OFPTC_TABLE_MISS_CONTINUE,
1593 * OFPTC_TABLE_MISS_DROP
1594 * When OF1.0, OFPTC_TABLE_MISS_CONTINUE is used. What to do?
1596 rule = rule_dpif_miss_rule(ctx->xbridge->ofproto, &ctx->xin->flow);
1598 if (rule && ctx->xin->resubmit_stats) {
1599 rule_credit_stats(rule, ctx->xin->resubmit_stats);
1605 xlate_table_action(struct xlate_ctx *ctx,
1606 ofp_port_t in_port, uint8_t table_id, bool may_packet_in)
1608 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
1609 struct rule_dpif *rule;
1610 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
1611 uint8_t old_table_id = ctx->table_id;
1613 ctx->table_id = table_id;
1615 /* Look up a flow with 'in_port' as the input port. */
1616 ctx->xin->flow.in_port.ofp_port = in_port;
1617 rule = rule_dpif_lookup_in_table(ctx->xbridge->ofproto,
1618 &ctx->xin->flow, &ctx->xout->wc,
1621 /* Restore the original input port. Otherwise OFPP_NORMAL and
1622 * OFPP_IN_PORT will have surprising behavior. */
1623 ctx->xin->flow.in_port.ofp_port = old_in_port;
1625 rule = ctx_rule_hooks(ctx, rule, may_packet_in);
1628 struct rule_dpif *old_rule = ctx->rule;
1632 do_xlate_actions(rule->up.ofpacts, rule->up.ofpacts_len, ctx);
1633 ctx->rule = old_rule;
1637 ctx->table_id = old_table_id;
1639 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
1641 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
1642 MAX_RESUBMIT_RECURSION);
1647 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
1648 const struct ofpact_resubmit *resubmit)
1653 in_port = resubmit->in_port;
1654 if (in_port == OFPP_IN_PORT) {
1655 in_port = ctx->xin->flow.in_port.ofp_port;
1658 table_id = resubmit->table_id;
1659 if (table_id == 255) {
1660 table_id = ctx->table_id;
1663 xlate_table_action(ctx, in_port, table_id, false);
1667 flood_packets(struct xlate_ctx *ctx, bool all)
1669 const struct xport *xport;
1671 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
1672 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
1677 compose_output_action__(ctx, xport->ofp_port, false);
1678 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
1679 compose_output_action(ctx, xport->ofp_port);
1683 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1687 execute_controller_action(struct xlate_ctx *ctx, int len,
1688 enum ofp_packet_in_reason reason,
1689 uint16_t controller_id)
1691 struct ofputil_packet_in pin;
1692 struct ofpbuf *packet;
1695 ovs_assert(!ctx->xout->slow || ctx->xout->slow == SLOW_CONTROLLER);
1696 ctx->xout->slow = SLOW_CONTROLLER;
1697 if (!ctx->xin->packet) {
1701 packet = ofpbuf_clone(ctx->xin->packet);
1703 key.skb_priority = 0;
1705 memset(&key.tunnel, 0, sizeof key.tunnel);
1707 commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1708 &ctx->xout->odp_actions, &ctx->xout->wc);
1710 odp_execute_actions(NULL, packet, &key, ctx->xout->odp_actions.data,
1711 ctx->xout->odp_actions.size, NULL, NULL);
1713 pin.packet = packet->data;
1714 pin.packet_len = packet->size;
1715 pin.reason = reason;
1716 pin.controller_id = controller_id;
1717 pin.table_id = ctx->table_id;
1718 pin.cookie = ctx->rule ? ctx->rule->up.flow_cookie : 0;
1721 flow_get_metadata(&ctx->xin->flow, &pin.fmd);
1723 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, &pin);
1724 ofpbuf_delete(packet);
1728 compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1730 struct flow_wildcards *wc = &ctx->xout->wc;
1731 struct flow *flow = &ctx->xin->flow;
1733 ovs_assert(eth_type_mpls(eth_type));
1735 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1736 memset(&wc->masks.mpls_depth, 0xff, sizeof wc->masks.mpls_depth);
1738 if (flow->mpls_depth) {
1739 flow->mpls_lse &= ~htonl(MPLS_BOS_MASK);
1745 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1746 label = htonl(0x2); /* IPV6 Explicit Null. */
1748 label = htonl(0x0); /* IPV4 Explicit Null. */
1750 wc->masks.nw_tos |= IP_DSCP_MASK;
1751 wc->masks.nw_ttl = 0xff;
1752 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
1753 ttl = flow->nw_ttl ? flow->nw_ttl : 0x40;
1754 flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
1755 flow->mpls_depth = 1;
1757 flow->dl_type = eth_type;
1761 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1763 struct flow_wildcards *wc = &ctx->xout->wc;
1764 struct flow *flow = &ctx->xin->flow;
1766 ovs_assert(eth_type_mpls(ctx->xin->flow.dl_type));
1767 ovs_assert(!eth_type_mpls(eth_type));
1769 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1770 memset(&wc->masks.mpls_depth, 0xff, sizeof wc->masks.mpls_depth);
1772 if (flow->mpls_depth) {
1774 flow->mpls_lse = htonl(0);
1775 if (!flow->mpls_depth) {
1776 flow->dl_type = eth_type;
1782 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
1784 struct flow *flow = &ctx->xin->flow;
1786 if (!is_ip_any(flow)) {
1790 ctx->xout->wc.masks.nw_ttl = 0xff;
1791 if (flow->nw_ttl > 1) {
1797 for (i = 0; i < ids->n_controllers; i++) {
1798 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
1802 /* Stop processing for current table. */
1808 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
1810 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
1814 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_TTL_MASK);
1815 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl);
1820 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
1822 struct flow *flow = &ctx->xin->flow;
1823 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse);
1824 struct flow_wildcards *wc = &ctx->xout->wc;
1826 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1828 if (!eth_type_mpls(flow->dl_type)) {
1834 set_mpls_lse_ttl(&flow->mpls_lse, ttl);
1837 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
1839 /* Stop processing for current table. */
1845 xlate_output_action(struct xlate_ctx *ctx,
1846 ofp_port_t port, uint16_t max_len, bool may_packet_in)
1848 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
1850 ctx->xout->nf_output_iface = NF_OUT_DROP;
1854 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
1857 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
1864 flood_packets(ctx, false);
1867 flood_packets(ctx, true);
1869 case OFPP_CONTROLLER:
1870 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
1876 if (port != ctx->xin->flow.in_port.ofp_port) {
1877 compose_output_action(ctx, port);
1879 xlate_report(ctx, "skipping output to input port");
1884 if (prev_nf_output_iface == NF_OUT_FLOOD) {
1885 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1886 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
1887 ctx->xout->nf_output_iface = prev_nf_output_iface;
1888 } else if (prev_nf_output_iface != NF_OUT_DROP &&
1889 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
1890 ctx->xout->nf_output_iface = NF_OUT_MULTI;
1895 xlate_output_reg_action(struct xlate_ctx *ctx,
1896 const struct ofpact_output_reg *or)
1898 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
1899 if (port <= UINT16_MAX) {
1900 union mf_subvalue value;
1902 memset(&value, 0xff, sizeof value);
1903 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
1904 xlate_output_action(ctx, u16_to_ofp(port),
1905 or->max_len, false);
1910 xlate_enqueue_action(struct xlate_ctx *ctx,
1911 const struct ofpact_enqueue *enqueue)
1913 ofp_port_t ofp_port = enqueue->port;
1914 uint32_t queue_id = enqueue->queue;
1915 uint32_t flow_priority, priority;
1918 /* Translate queue to priority. */
1919 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
1921 /* Fall back to ordinary output action. */
1922 xlate_output_action(ctx, enqueue->port, 0, false);
1926 /* Check output port. */
1927 if (ofp_port == OFPP_IN_PORT) {
1928 ofp_port = ctx->xin->flow.in_port.ofp_port;
1929 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
1933 /* Add datapath actions. */
1934 flow_priority = ctx->xin->flow.skb_priority;
1935 ctx->xin->flow.skb_priority = priority;
1936 compose_output_action(ctx, ofp_port);
1937 ctx->xin->flow.skb_priority = flow_priority;
1939 /* Update NetFlow output port. */
1940 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
1941 ctx->xout->nf_output_iface = ofp_port;
1942 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
1943 ctx->xout->nf_output_iface = NF_OUT_MULTI;
1948 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
1950 uint32_t skb_priority;
1952 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
1953 ctx->xin->flow.skb_priority = skb_priority;
1955 /* Couldn't translate queue to a priority. Nothing to do. A warning
1956 * has already been logged. */
1961 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
1963 const struct xbridge *xbridge = xbridge_;
1974 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
1977 port = get_ofp_port(xbridge, ofp_port);
1978 return port ? port->may_enable : false;
1983 xlate_bundle_action(struct xlate_ctx *ctx,
1984 const struct ofpact_bundle *bundle)
1988 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
1990 CONST_CAST(struct xbridge *, ctx->xbridge));
1991 if (bundle->dst.field) {
1992 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
1995 xlate_output_action(ctx, port, 0, false);
2000 xlate_learn_action(struct xlate_ctx *ctx,
2001 const struct ofpact_learn *learn)
2003 struct ofputil_flow_mod *fm;
2004 struct ofpbuf ofpacts;
2006 ctx->xout->has_learn = true;
2008 learn_mask(learn, &ctx->xout->wc);
2010 if (!ctx->xin->may_learn) {
2014 fm = xmalloc(sizeof *fm);
2015 ofpbuf_init(&ofpacts, 0);
2016 learn_execute(learn, &ctx->xin->flow, fm, &ofpacts);
2018 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
2021 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
2022 * means "infinite". */
2024 reduce_timeout(uint16_t max, uint16_t *timeout)
2026 if (max && (!*timeout || *timeout > max)) {
2032 xlate_fin_timeout(struct xlate_ctx *ctx,
2033 const struct ofpact_fin_timeout *oft)
2035 if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
2036 struct rule_dpif *rule = ctx->rule;
2038 if (list_is_empty(&rule->up.expirable)) {
2039 list_insert(&rule->up.ofproto->expirable, &rule->up.expirable);
2042 reduce_timeout(oft->fin_idle_timeout, &rule->up.idle_timeout);
2043 reduce_timeout(oft->fin_hard_timeout, &rule->up.hard_timeout);
2048 xlate_sample_action(struct xlate_ctx *ctx,
2049 const struct ofpact_sample *os)
2051 union user_action_cookie cookie;
2052 /* Scale the probability from 16-bit to 32-bit while representing
2053 * the same percentage. */
2054 uint32_t probability = (os->probability << 16) | os->probability;
2056 commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2057 &ctx->xout->odp_actions, &ctx->xout->wc);
2059 compose_flow_sample_cookie(os->probability, os->collector_set_id,
2060 os->obs_domain_id, os->obs_point_id, &cookie);
2061 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
2062 probability, &cookie, sizeof cookie.flow_sample);
2066 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
2068 if (xport->config & (eth_addr_equals(ctx->xin->flow.dl_dst, eth_addr_stp)
2069 ? OFPUTIL_PC_NO_RECV_STP
2070 : OFPUTIL_PC_NO_RECV)) {
2074 /* Only drop packets here if both forwarding and learning are
2075 * disabled. If just learning is enabled, we need to have
2076 * OFPP_NORMAL and the learning action have a look at the packet
2077 * before we can drop it. */
2078 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
2086 tunnel_ecn_ok(struct xlate_ctx *ctx)
2088 if (is_ip_any(&ctx->base_flow)
2089 && (ctx->xin->flow.tunnel.ip_tos & IP_ECN_MASK) == IP_ECN_CE) {
2090 if ((ctx->base_flow.nw_tos & IP_ECN_MASK) == IP_ECN_NOT_ECT) {
2091 VLOG_WARN_RL(&rl, "dropping tunnel packet marked ECN CE"
2092 " but is not ECN capable");
2095 /* Set the ECN CE value in the tunneled packet. */
2096 ctx->xin->flow.nw_tos |= IP_ECN_CE;
2104 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
2105 struct xlate_ctx *ctx)
2107 struct flow_wildcards *wc = &ctx->xout->wc;
2108 struct flow *flow = &ctx->xin->flow;
2109 bool was_evictable = true;
2110 const struct ofpact *a;
2113 /* Don't let the rule we're working on get evicted underneath us. */
2114 was_evictable = ctx->rule->up.evictable;
2115 ctx->rule->up.evictable = false;
2118 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2119 struct ofpact_controller *controller;
2120 const struct ofpact_metadata *metadata;
2128 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
2129 ofpact_get_OUTPUT(a)->max_len, true);
2132 case OFPACT_CONTROLLER:
2133 controller = ofpact_get_CONTROLLER(a);
2134 execute_controller_action(ctx, controller->max_len,
2136 controller->controller_id);
2139 case OFPACT_ENQUEUE:
2140 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
2143 case OFPACT_SET_VLAN_VID:
2144 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2145 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
2146 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
2150 case OFPACT_SET_VLAN_PCP:
2151 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
2152 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
2154 htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp << VLAN_PCP_SHIFT)
2158 case OFPACT_STRIP_VLAN:
2159 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2160 flow->vlan_tci = htons(0);
2163 case OFPACT_PUSH_VLAN:
2164 /* XXX 802.1AD(QinQ) */
2165 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2166 flow->vlan_tci = htons(VLAN_CFI);
2169 case OFPACT_SET_ETH_SRC:
2170 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2171 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2174 case OFPACT_SET_ETH_DST:
2175 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2176 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2179 case OFPACT_SET_IPV4_SRC:
2180 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2181 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2182 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2186 case OFPACT_SET_IPV4_DST:
2187 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2188 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2189 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
2193 case OFPACT_SET_IPV4_DSCP:
2194 wc->masks.nw_tos |= IP_DSCP_MASK;
2195 /* OpenFlow 1.0 only supports IPv4. */
2196 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2197 flow->nw_tos &= ~IP_DSCP_MASK;
2198 flow->nw_tos |= ofpact_get_SET_IPV4_DSCP(a)->dscp;
2202 case OFPACT_SET_L4_SRC_PORT:
2203 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2204 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2205 if (is_ip_any(flow)) {
2206 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2210 case OFPACT_SET_L4_DST_PORT:
2211 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2212 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2213 if (is_ip_any(flow)) {
2214 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2218 case OFPACT_RESUBMIT:
2219 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
2222 case OFPACT_SET_TUNNEL:
2223 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2226 case OFPACT_SET_QUEUE:
2227 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2230 case OFPACT_POP_QUEUE:
2231 flow->skb_priority = ctx->orig_skb_priority;
2234 case OFPACT_REG_MOVE:
2235 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
2238 case OFPACT_REG_LOAD:
2239 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow);
2242 case OFPACT_STACK_PUSH:
2243 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
2247 case OFPACT_STACK_POP:
2248 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
2252 case OFPACT_PUSH_MPLS:
2253 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a)->ethertype);
2256 case OFPACT_POP_MPLS:
2257 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
2260 case OFPACT_SET_MPLS_TTL:
2261 if (compose_set_mpls_ttl_action(ctx,
2262 ofpact_get_SET_MPLS_TTL(a)->ttl)) {
2267 case OFPACT_DEC_MPLS_TTL:
2268 if (compose_dec_mpls_ttl_action(ctx)) {
2273 case OFPACT_DEC_TTL:
2274 wc->masks.nw_ttl = 0xff;
2275 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
2281 /* Nothing to do. */
2284 case OFPACT_MULTIPATH:
2285 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
2289 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
2292 case OFPACT_OUTPUT_REG:
2293 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
2297 xlate_learn_action(ctx, ofpact_get_LEARN(a));
2304 case OFPACT_FIN_TIMEOUT:
2305 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2306 ctx->xout->has_fin_timeout = true;
2307 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
2310 case OFPACT_CLEAR_ACTIONS:
2312 * Nothing to do because writa-actions is not supported for now.
2313 * When writa-actions is supported, clear-actions also must
2314 * be supported at the same time.
2318 case OFPACT_WRITE_METADATA:
2319 metadata = ofpact_get_WRITE_METADATA(a);
2320 flow->metadata &= ~metadata->mask;
2321 flow->metadata |= metadata->metadata & metadata->mask;
2325 /* Not implemented yet. */
2328 case OFPACT_GOTO_TABLE: {
2329 /* It is assumed that goto-table is the last action. */
2330 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
2332 ovs_assert(ctx->table_id < ogt->table_id);
2333 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2334 ogt->table_id, true);
2339 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
2346 ctx->rule->up.evictable = was_evictable;
2351 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
2352 const struct flow *flow, struct rule_dpif *rule,
2353 uint8_t tcp_flags, const struct ofpbuf *packet)
2355 xin->ofproto = ofproto;
2357 xin->packet = packet;
2358 xin->may_learn = packet != NULL;
2360 xin->ofpacts = NULL;
2361 xin->ofpacts_len = 0;
2362 xin->tcp_flags = tcp_flags;
2363 xin->resubmit_hook = NULL;
2364 xin->report_hook = NULL;
2365 xin->resubmit_stats = NULL;
2369 xlate_out_uninit(struct xlate_out *xout)
2372 ofpbuf_uninit(&xout->odp_actions);
2376 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
2377 * into datapath actions, using 'ctx', and discards the datapath actions. */
2379 xlate_actions_for_side_effects(struct xlate_in *xin)
2381 struct xlate_out xout;
2383 xlate_actions(xin, &xout);
2384 xlate_out_uninit(&xout);
2388 xlate_report(struct xlate_ctx *ctx, const char *s)
2390 if (ctx->xin->report_hook) {
2391 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
2396 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
2399 dst->slow = src->slow;
2400 dst->has_learn = src->has_learn;
2401 dst->has_normal = src->has_normal;
2402 dst->has_fin_timeout = src->has_fin_timeout;
2403 dst->nf_output_iface = src->nf_output_iface;
2404 dst->mirrors = src->mirrors;
2406 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
2407 sizeof dst->odp_actions_stub);
2408 ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
2409 src->odp_actions.size);
2412 static struct skb_priority_to_dscp *
2413 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
2415 struct skb_priority_to_dscp *pdscp;
2418 hash = hash_int(skb_priority, 0);
2419 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
2420 if (pdscp->skb_priority == skb_priority) {
2428 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
2431 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
2432 *dscp = pdscp ? pdscp->dscp : 0;
2433 return pdscp != NULL;
2437 clear_skb_priorities(struct xport *xport)
2439 struct skb_priority_to_dscp *pdscp, *next;
2441 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
2442 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
2448 actions_output_to_local_port(const struct xlate_ctx *ctx)
2450 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
2451 const struct nlattr *a;
2454 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions.data,
2455 ctx->xout->odp_actions.size) {
2456 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
2457 && nl_attr_get_odp_port(a) == local_odp_port) {
2464 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
2465 * into datapath actions in 'odp_actions', using 'ctx'. */
2467 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
2469 struct flow_wildcards *wc = &xout->wc;
2470 struct flow *flow = &xin->flow;
2472 enum slow_path_reason special;
2473 const struct ofpact *ofpacts;
2474 struct xport *in_port;
2475 struct flow orig_flow;
2476 struct xlate_ctx ctx;
2479 COVERAGE_INC(xlate_actions);
2481 /* Flow initialization rules:
2482 * - 'base_flow' must match the kernel's view of the packet at the
2483 * time that action processing starts. 'flow' represents any
2484 * transformations we wish to make through actions.
2485 * - By default 'base_flow' and 'flow' are the same since the input
2486 * packet matches the output before any actions are applied.
2487 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
2488 * of the received packet as seen by the kernel. If we later output
2489 * to another device without any modifications this will cause us to
2490 * insert a new tag since the original one was stripped off by the
2492 * - Tunnel metadata as received is retained in 'flow'. This allows
2493 * tunnel metadata matching also in later tables.
2494 * Since a kernel action for setting the tunnel metadata will only be
2495 * generated with actual tunnel output, changing the tunnel metadata
2496 * values in 'flow' (such as tun_id) will only have effect with a later
2497 * tunnel output action.
2498 * - Tunnel 'base_flow' is completely cleared since that is what the
2499 * kernel does. If we wish to maintain the original values an action
2500 * needs to be generated. */
2505 ctx.xout->has_learn = false;
2506 ctx.xout->has_normal = false;
2507 ctx.xout->has_fin_timeout = false;
2508 ctx.xout->nf_output_iface = NF_OUT_DROP;
2509 ctx.xout->mirrors = 0;
2510 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
2511 sizeof ctx.xout->odp_actions_stub);
2512 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
2514 ctx.xbridge = xbridge_lookup(xin->ofproto);
2519 ctx.rule = xin->rule;
2521 ctx.base_flow = *flow;
2522 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
2523 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
2525 flow_wildcards_init_catchall(wc);
2526 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
2527 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
2528 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
2529 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
2531 if (tnl_port_should_receive(&ctx.xin->flow)) {
2532 memset(&wc->masks.tunnel, 0xff, sizeof wc->masks.tunnel);
2533 /* skb_mark is currently used only by tunnels but that will likely
2534 * change in the future. */
2535 memset(&wc->masks.skb_mark, 0xff, sizeof wc->masks.skb_mark);
2537 if (ctx.xbridge->has_netflow) {
2538 netflow_mask_wc(flow, wc);
2542 ctx.orig_skb_priority = flow->skb_priority;
2547 ofpacts = xin->ofpacts;
2548 ofpacts_len = xin->ofpacts_len;
2549 } else if (xin->rule) {
2550 ofpacts = xin->rule->up.ofpacts;
2551 ofpacts_len = xin->rule->up.ofpacts_len;
2556 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
2558 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
2559 /* Do this conditionally because the copy is expensive enough that it
2560 * shows up in profiles. */
2564 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
2565 switch (ctx.xbridge->frag) {
2566 case OFPC_FRAG_NORMAL:
2567 /* We must pretend that transport ports are unavailable. */
2568 flow->tp_src = ctx.base_flow.tp_src = htons(0);
2569 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
2572 case OFPC_FRAG_DROP:
2575 case OFPC_FRAG_REASM:
2578 case OFPC_FRAG_NX_MATCH:
2579 /* Nothing to do. */
2582 case OFPC_INVALID_TTL_TO_CONTROLLER:
2587 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
2588 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
2590 ctx.xout->slow = special;
2592 size_t sample_actions_len;
2594 if (flow->in_port.ofp_port
2595 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
2596 flow->in_port.ofp_port,
2598 ctx.base_flow.vlan_tci = 0;
2601 add_sflow_action(&ctx);
2602 add_ipfix_action(&ctx);
2603 sample_actions_len = ctx.xout->odp_actions.size;
2605 if (tunnel_ecn_ok(&ctx) && (!in_port || may_receive(in_port, &ctx))) {
2606 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
2608 /* We've let OFPP_NORMAL and the learning action look at the
2609 * packet, so drop it now if forwarding is disabled. */
2610 if (in_port && !xport_stp_forward_state(in_port)) {
2611 ctx.xout->odp_actions.size = sample_actions_len;
2615 if (ctx.xbridge->has_in_band
2616 && in_band_must_output_to_local_port(flow)
2617 && !actions_output_to_local_port(&ctx)) {
2618 compose_output_action(&ctx, OFPP_LOCAL);
2621 fix_sflow_action(&ctx);
2623 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
2624 add_mirror_actions(&ctx, &orig_flow);
2628 ofpbuf_uninit(&ctx.stack);
2630 /* Clear the metadata and register wildcard masks, because we won't
2631 * use non-header fields as part of the cache. */
2632 memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
2633 memset(&wc->masks.regs, 0, sizeof wc->masks.regs);