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"
47 #include "ofproto/ofproto-provider.h"
51 COVERAGE_DEFINE(xlate_actions);
53 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
55 /* Maximum depth of flow table recursion (due to resubmit actions) in a
56 * flow translation. */
57 #define MAX_RESUBMIT_RECURSION 64
59 /* Maximum number of resubmit actions in a flow translation, whether they are
60 * recursive or not. */
61 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
63 struct ovs_rwlock xlate_rwlock = OVS_RWLOCK_INITIALIZER;
66 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
67 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
69 struct list xbundles; /* Owned xbundles. */
70 struct hmap xports; /* Indexed by ofp_port. */
72 char *name; /* Name used in log messages. */
73 struct dpif *dpif; /* Datapath interface. */
74 struct mac_learning *ml; /* Mac learning handle. */
75 struct mbridge *mbridge; /* Mirroring. */
76 struct dpif_sflow *sflow; /* SFlow handle, or null. */
77 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
78 struct stp *stp; /* STP or null if disabled. */
80 /* Special rules installed by ofproto-dpif. */
81 struct rule_dpif *miss_rule;
82 struct rule_dpif *no_packet_in_rule;
84 enum ofp_config_flags frag; /* Fragmentation handling. */
85 bool has_netflow; /* Bridge runs netflow? */
86 bool has_in_band; /* Bridge has in band control? */
87 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
91 struct hmap_node hmap_node; /* In global 'xbundles' map. */
92 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
94 struct list list_node; /* In parent 'xbridges' list. */
95 struct xbridge *xbridge; /* Parent xbridge. */
97 struct list xports; /* Contains "struct xport"s. */
99 char *name; /* Name used in log messages. */
100 struct bond *bond; /* Nonnull iff more than one port. */
101 struct lacp *lacp; /* LACP handle or null. */
103 enum port_vlan_mode vlan_mode; /* VLAN mode. */
104 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
105 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
106 * NULL if all VLANs are trunked. */
107 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
108 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
112 struct hmap_node hmap_node; /* Node in global 'xports' map. */
113 struct ofport_dpif *ofport; /* Key in global 'xports map. */
115 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
116 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
118 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
120 struct list bundle_node; /* In parent xbundle (if it exists). */
121 struct xbundle *xbundle; /* Parent xbundle or null. */
123 struct netdev *netdev; /* 'ofport''s netdev. */
125 struct xbridge *xbridge; /* Parent bridge. */
126 struct xport *peer; /* Patch port peer or null. */
128 enum ofputil_port_config config; /* OpenFlow port configuration. */
129 int stp_port_no; /* STP port number or -1 if not in use. */
131 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
133 bool may_enable; /* May be enabled in bonds. */
134 bool is_tunnel; /* Is a tunnel port. */
136 struct cfm *cfm; /* CFM handle or null. */
137 struct bfd *bfd; /* BFD handle or null. */
141 struct xlate_in *xin;
142 struct xlate_out *xout;
144 const struct xbridge *xbridge;
146 /* Flow at the last commit. */
147 struct flow base_flow;
149 /* Tunnel IP destination address as received. This is stored separately
150 * as the base_flow.tunnel is cleared on init to reflect the datapath
151 * behavior. Used to make sure not to send tunneled output to ourselves,
152 * which might lead to an infinite loop. This could happen easily
153 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
154 * actually set the tun_dst field. */
155 ovs_be32 orig_tunnel_ip_dst;
157 /* Stack for the push and pop actions. Each stack element is of type
158 * "union mf_subvalue". */
159 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
162 /* The rule that we are currently translating, or NULL. */
163 struct rule_dpif *rule;
165 int mpls_depth_delta; /* Delta of the mpls stack depth since
166 * actions were last committed.
167 * Must be between -1 and 1 inclusive. */
168 ovs_be32 pre_push_mpls_lse; /* Used to record the top-most MPLS LSE
169 * prior to an mpls_push so that it may be
170 * used for a subsequent mpls_pop. */
172 /* Resubmit statistics, via xlate_table_action(). */
173 int recurse; /* Current resubmit nesting depth. */
174 int resubmits; /* Total number of resubmits. */
176 uint32_t orig_skb_priority; /* Priority when packet arrived. */
177 uint8_t table_id; /* OpenFlow table ID where flow was found. */
178 uint32_t sflow_n_outputs; /* Number of output ports. */
179 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
180 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
181 bool exit; /* No further actions should be processed. */
184 /* A controller may use OFPP_NONE as the ingress port to indicate that
185 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
186 * when an input bundle is needed for validation (e.g., mirroring or
187 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
188 * any 'port' structs, so care must be taken when dealing with it.
189 * The bundle's name and vlan mode are initialized in lookup_input_bundle() */
190 static struct xbundle ofpp_none_bundle;
192 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
193 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
194 * traffic egressing the 'ofport' with that priority should be marked with. */
195 struct skb_priority_to_dscp {
196 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
197 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
199 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
202 static struct hmap xbridges = HMAP_INITIALIZER(&xbridges);
203 static struct hmap xbundles = HMAP_INITIALIZER(&xbundles);
204 static struct hmap xports = HMAP_INITIALIZER(&xports);
206 static bool may_receive(const struct xport *, struct xlate_ctx *);
207 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
209 static void xlate_normal(struct xlate_ctx *);
210 static void xlate_report(struct xlate_ctx *, const char *);
211 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
212 uint8_t table_id, bool may_packet_in);
213 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
214 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
215 static void output_normal(struct xlate_ctx *, const struct xbundle *,
217 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
219 static struct xbridge *xbridge_lookup(const struct ofproto_dpif *);
220 static struct xbundle *xbundle_lookup(const struct ofbundle *);
221 static struct xport *xport_lookup(const struct ofport_dpif *);
222 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
223 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
224 uint32_t skb_priority);
225 static void clear_skb_priorities(struct xport *);
226 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
230 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
231 struct dpif *dpif, struct rule_dpif *miss_rule,
232 struct rule_dpif *no_packet_in_rule,
233 const struct mac_learning *ml, struct stp *stp,
234 const struct mbridge *mbridge,
235 const struct dpif_sflow *sflow,
236 const struct dpif_ipfix *ipfix, enum ofp_config_flags frag,
237 bool forward_bpdu, bool has_in_band, bool has_netflow)
239 struct xbridge *xbridge = xbridge_lookup(ofproto);
242 xbridge = xzalloc(sizeof *xbridge);
243 xbridge->ofproto = ofproto;
245 hmap_insert(&xbridges, &xbridge->hmap_node, hash_pointer(ofproto, 0));
246 hmap_init(&xbridge->xports);
247 list_init(&xbridge->xbundles);
250 if (xbridge->ml != ml) {
251 mac_learning_unref(xbridge->ml);
252 xbridge->ml = mac_learning_ref(ml);
255 if (xbridge->mbridge != mbridge) {
256 mbridge_unref(xbridge->mbridge);
257 xbridge->mbridge = mbridge_ref(mbridge);
260 if (xbridge->sflow != sflow) {
261 dpif_sflow_unref(xbridge->sflow);
262 xbridge->sflow = dpif_sflow_ref(sflow);
265 if (xbridge->ipfix != ipfix) {
266 dpif_ipfix_unref(xbridge->ipfix);
267 xbridge->ipfix = dpif_ipfix_ref(ipfix);
270 if (xbridge->stp != stp) {
271 stp_unref(xbridge->stp);
272 xbridge->stp = stp_ref(stp);
276 xbridge->name = xstrdup(name);
278 xbridge->dpif = dpif;
279 xbridge->forward_bpdu = forward_bpdu;
280 xbridge->has_in_band = has_in_band;
281 xbridge->has_netflow = has_netflow;
282 xbridge->frag = frag;
283 xbridge->miss_rule = miss_rule;
284 xbridge->no_packet_in_rule = no_packet_in_rule;
288 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
290 struct xbridge *xbridge = xbridge_lookup(ofproto);
291 struct xbundle *xbundle, *next_xbundle;
292 struct xport *xport, *next_xport;
298 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
299 xlate_ofport_remove(xport->ofport);
302 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
303 xlate_bundle_remove(xbundle->ofbundle);
306 hmap_remove(&xbridges, &xbridge->hmap_node);
307 mac_learning_unref(xbridge->ml);
308 mbridge_unref(xbridge->mbridge);
309 dpif_sflow_unref(xbridge->sflow);
310 dpif_ipfix_unref(xbridge->ipfix);
311 stp_unref(xbridge->stp);
312 hmap_destroy(&xbridge->xports);
318 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
319 const char *name, enum port_vlan_mode vlan_mode, int vlan,
320 unsigned long *trunks, bool use_priority_tags,
321 const struct bond *bond, const struct lacp *lacp,
324 struct xbundle *xbundle = xbundle_lookup(ofbundle);
327 xbundle = xzalloc(sizeof *xbundle);
328 xbundle->ofbundle = ofbundle;
329 xbundle->xbridge = xbridge_lookup(ofproto);
331 hmap_insert(&xbundles, &xbundle->hmap_node, hash_pointer(ofbundle, 0));
332 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
333 list_init(&xbundle->xports);
336 ovs_assert(xbundle->xbridge);
339 xbundle->name = xstrdup(name);
341 xbundle->vlan_mode = vlan_mode;
342 xbundle->vlan = vlan;
343 xbundle->trunks = trunks;
344 xbundle->use_priority_tags = use_priority_tags;
345 xbundle->floodable = floodable;
347 if (xbundle->bond != bond) {
348 bond_unref(xbundle->bond);
349 xbundle->bond = bond_ref(bond);
352 if (xbundle->lacp != lacp) {
353 lacp_unref(xbundle->lacp);
354 xbundle->lacp = lacp_ref(lacp);
359 xlate_bundle_remove(struct ofbundle *ofbundle)
361 struct xbundle *xbundle = xbundle_lookup(ofbundle);
362 struct xport *xport, *next;
368 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
369 list_remove(&xport->bundle_node);
370 xport->xbundle = NULL;
373 hmap_remove(&xbundles, &xbundle->hmap_node);
374 list_remove(&xbundle->list_node);
375 bond_unref(xbundle->bond);
376 lacp_unref(xbundle->lacp);
382 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
383 struct ofport_dpif *ofport, ofp_port_t ofp_port,
384 odp_port_t odp_port, const struct netdev *netdev,
385 const struct cfm *cfm, const struct bfd *bfd,
386 struct ofport_dpif *peer, int stp_port_no,
387 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
388 enum ofputil_port_config config, bool is_tunnel,
391 struct xport *xport = xport_lookup(ofport);
395 xport = xzalloc(sizeof *xport);
396 xport->ofport = ofport;
397 xport->xbridge = xbridge_lookup(ofproto);
398 xport->ofp_port = ofp_port;
400 hmap_init(&xport->skb_priorities);
401 hmap_insert(&xports, &xport->hmap_node, hash_pointer(ofport, 0));
402 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
403 hash_ofp_port(xport->ofp_port));
406 ovs_assert(xport->ofp_port == ofp_port);
408 xport->config = config;
409 xport->stp_port_no = stp_port_no;
410 xport->is_tunnel = is_tunnel;
411 xport->may_enable = may_enable;
412 xport->odp_port = odp_port;
414 if (xport->netdev != netdev) {
415 netdev_close(xport->netdev);
416 xport->netdev = netdev_ref(netdev);
419 if (xport->cfm != cfm) {
420 cfm_unref(xport->cfm);
421 xport->cfm = cfm_ref(cfm);
424 if (xport->bfd != bfd) {
425 bfd_unref(xport->bfd);
426 xport->bfd = bfd_ref(bfd);
430 xport->peer->peer = NULL;
432 xport->peer = xport_lookup(peer);
434 xport->peer->peer = xport;
437 if (xport->xbundle) {
438 list_remove(&xport->bundle_node);
440 xport->xbundle = xbundle_lookup(ofbundle);
441 if (xport->xbundle) {
442 list_insert(&xport->xbundle->xports, &xport->bundle_node);
445 clear_skb_priorities(xport);
446 for (i = 0; i < n_qdscp; i++) {
447 struct skb_priority_to_dscp *pdscp;
448 uint32_t skb_priority;
450 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
455 pdscp = xmalloc(sizeof *pdscp);
456 pdscp->skb_priority = skb_priority;
457 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
458 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
459 hash_int(pdscp->skb_priority, 0));
464 xlate_ofport_remove(struct ofport_dpif *ofport)
466 struct xport *xport = xport_lookup(ofport);
473 xport->peer->peer = NULL;
477 if (xport->xbundle) {
478 list_remove(&xport->bundle_node);
481 clear_skb_priorities(xport);
482 hmap_destroy(&xport->skb_priorities);
484 hmap_remove(&xports, &xport->hmap_node);
485 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
487 netdev_close(xport->netdev);
488 cfm_unref(xport->cfm);
489 bfd_unref(xport->bfd);
493 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
494 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
495 * Optionally, if nonnull, populates 'fitnessp' with the fitness of 'flow' as
496 * returned by odp_flow_key_to_flow(). Also, optionally populates 'ofproto'
497 * with the ofproto_dpif, and 'odp_in_port' with the datapath in_port, that
498 * 'packet' ingressed.
500 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
501 * 'flow''s in_port to OFPP_NONE.
503 * This function does post-processing on data returned from
504 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
505 * of the upcall processing logic. In particular, if the extracted in_port is
506 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
507 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
508 * a VLAN header onto 'packet' (if it is nonnull).
510 * Similarly, this function also includes some logic to help with tunnels. It
511 * may modify 'flow' as necessary to make the tunneling implementation
512 * transparent to the upcall processing logic.
514 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
515 * or some other positive errno if there are other problems. */
517 xlate_receive(const struct dpif_backer *backer, struct ofpbuf *packet,
518 const struct nlattr *key, size_t key_len,
519 struct flow *flow, enum odp_key_fitness *fitnessp,
520 struct ofproto_dpif **ofproto, odp_port_t *odp_in_port)
522 enum odp_key_fitness fitness;
523 const struct xport *xport;
526 ovs_rwlock_rdlock(&xlate_rwlock);
527 fitness = odp_flow_key_to_flow(key, key_len, flow);
528 if (fitness == ODP_FIT_ERROR) {
534 *odp_in_port = flow->in_port.odp_port;
537 xport = xport_lookup(tnl_port_should_receive(flow)
538 ? tnl_port_receive(flow)
539 : odp_port_to_ofport(backer, flow->in_port.odp_port));
541 flow->in_port.ofp_port = xport ? xport->ofp_port : OFPP_NONE;
546 if (vsp_adjust_flow(xport->xbridge->ofproto, flow)) {
548 /* Make the packet resemble the flow, so that it gets sent to
549 * an OpenFlow controller properly, so that it looks correct
550 * for sFlow, and so that flow_extract() will get the correct
551 * vlan_tci if it is called on 'packet'.
553 * The allocated space inside 'packet' probably also contains
554 * 'key', that is, both 'packet' and 'key' are probably part of
555 * a struct dpif_upcall (see the large comment on that
556 * structure definition), so pushing data on 'packet' is in
557 * general not a good idea since it could overwrite 'key' or
558 * free it as a side effect. However, it's OK in this special
559 * case because we know that 'packet' is inside a Netlink
560 * attribute: pushing 4 bytes will just overwrite the 4-byte
561 * "struct nlattr", which is fine since we don't need that
563 eth_push_vlan(packet, flow->vlan_tci);
565 /* We can't reproduce 'key' from 'flow'. */
566 fitness = fitness == ODP_FIT_PERFECT ? ODP_FIT_TOO_MUCH : fitness;
571 *ofproto = xport->xbridge->ofproto;
578 ovs_rwlock_unlock(&xlate_rwlock);
582 static struct xbridge *
583 xbridge_lookup(const struct ofproto_dpif *ofproto)
585 struct xbridge *xbridge;
591 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
593 if (xbridge->ofproto == ofproto) {
600 static struct xbundle *
601 xbundle_lookup(const struct ofbundle *ofbundle)
603 struct xbundle *xbundle;
609 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
611 if (xbundle->ofbundle == ofbundle) {
618 static struct xport *
619 xport_lookup(const struct ofport_dpif *ofport)
627 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
629 if (xport->ofport == ofport) {
636 static struct stp_port *
637 xport_get_stp_port(const struct xport *xport)
639 return xport->xbridge->stp && xport->stp_port_no != -1
640 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
644 static enum stp_state
645 xport_stp_learn_state(const struct xport *xport)
647 struct stp_port *sp = xport_get_stp_port(xport);
648 return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
652 xport_stp_forward_state(const struct xport *xport)
654 struct stp_port *sp = xport_get_stp_port(xport);
655 return stp_forward_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
658 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
659 * were used to make the determination.*/
661 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
663 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
664 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
668 stp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
670 struct stp_port *sp = xport_get_stp_port(xport);
671 struct ofpbuf payload = *packet;
672 struct eth_header *eth = payload.data;
674 /* Sink packets on ports that have STP disabled when the bridge has
676 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
680 /* Trim off padding on payload. */
681 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
682 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
685 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
686 stp_received_bpdu(sp, payload.data, payload.size);
690 static struct xport *
691 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
695 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
697 if (xport->ofp_port == ofp_port) {
705 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
707 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
708 return xport ? xport->odp_port : ODPP_NONE;
712 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
714 return (bundle->vlan_mode != PORT_VLAN_ACCESS
715 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
719 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
721 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
725 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
727 return xbundle != &ofpp_none_bundle
728 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
733 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
735 return xbundle != &ofpp_none_bundle
736 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
741 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
743 return xbundle != &ofpp_none_bundle
744 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
748 static struct xbundle *
749 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
750 bool warn, struct xport **in_xportp)
754 /* Find the port and bundle for the received packet. */
755 xport = get_ofp_port(xbridge, in_port);
759 if (xport && xport->xbundle) {
760 return xport->xbundle;
763 /* Special-case OFPP_NONE, which a controller may use as the ingress
764 * port for traffic that it is sourcing. */
765 if (in_port == OFPP_NONE) {
766 ofpp_none_bundle.name = "OFPP_NONE";
767 ofpp_none_bundle.vlan_mode = PORT_VLAN_TRUNK;
768 return &ofpp_none_bundle;
771 /* Odd. A few possible reasons here:
773 * - We deleted a port but there are still a few packets queued up
776 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
777 * we don't know about.
779 * - The ofproto client didn't configure the port as part of a bundle.
780 * This is particularly likely to happen if a packet was received on the
781 * port after it was created, but before the client had a chance to
782 * configure its bundle.
785 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
787 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
788 "port %"PRIu16, xbridge->name, in_port);
794 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
796 const struct xbridge *xbridge = ctx->xbridge;
797 mirror_mask_t mirrors;
798 struct xbundle *in_xbundle;
802 mirrors = ctx->xout->mirrors;
803 ctx->xout->mirrors = 0;
805 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
806 ctx->xin->packet != NULL, NULL);
810 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
812 /* Drop frames on bundles reserved for mirroring. */
813 if (xbundle_mirror_out(xbridge, in_xbundle)) {
814 if (ctx->xin->packet != NULL) {
815 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
816 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
817 "%s, which is reserved exclusively for mirroring",
818 ctx->xbridge->name, in_xbundle->name);
820 ofpbuf_clear(&ctx->xout->odp_actions);
825 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
826 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
829 vlan = input_vid_to_vlan(in_xbundle, vid);
835 /* Restore the original packet before adding the mirror actions. */
836 ctx->xin->flow = *orig_flow;
839 mirror_mask_t dup_mirrors;
840 struct ofbundle *out;
841 unsigned long *vlans;
846 has_mirror = mirror_get(xbridge->mbridge, mirror_mask_ffs(mirrors) - 1,
847 &vlans, &dup_mirrors, &out, &out_vlan);
848 ovs_assert(has_mirror);
851 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
853 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
856 if (!vlan_mirrored) {
857 mirrors = zero_rightmost_1bit(mirrors);
861 mirrors &= ~dup_mirrors;
862 ctx->xout->mirrors |= dup_mirrors;
864 struct xbundle *out_xbundle = xbundle_lookup(out);
866 output_normal(ctx, out_xbundle, vlan);
868 } else if (vlan != out_vlan
869 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
870 struct xbundle *xbundle;
872 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
873 if (xbundle_includes_vlan(xbundle, out_vlan)
874 && !xbundle_mirror_out(xbridge, xbundle)) {
875 output_normal(ctx, xbundle, out_vlan);
882 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
883 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
884 * the bundle on which the packet was received, returns the VLAN to which the
887 * Both 'vid' and the return value are in the range 0...4095. */
889 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
891 switch (in_xbundle->vlan_mode) {
892 case PORT_VLAN_ACCESS:
893 return in_xbundle->vlan;
896 case PORT_VLAN_TRUNK:
899 case PORT_VLAN_NATIVE_UNTAGGED:
900 case PORT_VLAN_NATIVE_TAGGED:
901 return vid ? vid : in_xbundle->vlan;
908 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
909 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
912 * 'vid' should be the VID obtained from the 802.1Q header that was received as
913 * part of a packet (specify 0 if there was no 802.1Q header), in the range
916 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
918 /* Allow any VID on the OFPP_NONE port. */
919 if (in_xbundle == &ofpp_none_bundle) {
923 switch (in_xbundle->vlan_mode) {
924 case PORT_VLAN_ACCESS:
927 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
928 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
929 "packet received on port %s configured as VLAN "
930 "%"PRIu16" access port", vid, in_xbundle->name,
937 case PORT_VLAN_NATIVE_UNTAGGED:
938 case PORT_VLAN_NATIVE_TAGGED:
940 /* Port must always carry its native VLAN. */
944 case PORT_VLAN_TRUNK:
945 if (!xbundle_includes_vlan(in_xbundle, vid)) {
947 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
948 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
949 "received on port %s not configured for trunking "
950 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
962 /* Given 'vlan', the VLAN that a packet belongs to, and
963 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
964 * that should be included in the 802.1Q header. (If the return value is 0,
965 * then the 802.1Q header should only be included in the packet if there is a
968 * Both 'vlan' and the return value are in the range 0...4095. */
970 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
972 switch (out_xbundle->vlan_mode) {
973 case PORT_VLAN_ACCESS:
976 case PORT_VLAN_TRUNK:
977 case PORT_VLAN_NATIVE_TAGGED:
980 case PORT_VLAN_NATIVE_UNTAGGED:
981 return vlan == out_xbundle->vlan ? 0 : vlan;
989 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
992 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
994 ovs_be16 tci, old_tci;
997 vid = output_vlan_to_vid(out_xbundle, vlan);
998 if (list_is_empty(&out_xbundle->xports)) {
999 /* Partially configured bundle with no slaves. Drop the packet. */
1001 } else if (!out_xbundle->bond) {
1002 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1005 struct ofport_dpif *ofport;
1007 ofport = bond_choose_output_slave(out_xbundle->bond, &ctx->xin->flow,
1008 &ctx->xout->wc, vid);
1009 xport = xport_lookup(ofport);
1012 /* No slaves enabled, so drop packet. */
1017 old_tci = *flow_tci;
1019 if (tci || out_xbundle->use_priority_tags) {
1020 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1022 tci |= htons(VLAN_CFI);
1027 compose_output_action(ctx, xport->ofp_port);
1028 *flow_tci = old_tci;
1031 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1032 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1033 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1035 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1037 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1041 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1042 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1046 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1047 if (flow->nw_proto == ARP_OP_REPLY) {
1049 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1050 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1051 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1053 return flow->nw_src == flow->nw_dst;
1059 /* Checks whether a MAC learning update is necessary for MAC learning table
1060 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1063 * Most packets processed through the MAC learning table do not actually
1064 * change it in any way. This function requires only a read lock on the MAC
1065 * learning table, so it is much cheaper in this common case.
1067 * Keep the code here synchronized with that in update_learning_table__()
1070 is_mac_learning_update_needed(const struct mac_learning *ml,
1071 const struct flow *flow,
1072 struct flow_wildcards *wc,
1073 int vlan, struct xbundle *in_xbundle)
1074 OVS_REQ_RDLOCK(ml->rwlock)
1076 struct mac_entry *mac;
1078 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1082 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1083 if (!mac || mac_entry_age(ml, mac)) {
1087 if (is_gratuitous_arp(flow, wc)) {
1088 /* We don't want to learn from gratuitous ARP packets that are
1089 * reflected back over bond slaves so we lock the learning table. */
1090 if (!in_xbundle->bond) {
1092 } else if (mac_entry_is_grat_arp_locked(mac)) {
1097 return mac->port.p != in_xbundle->ofbundle;
1101 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1102 * received on 'in_xbundle' in 'vlan'.
1104 * This code repeats all the checks in is_mac_learning_update_needed() because
1105 * the lock was released between there and here and thus the MAC learning state
1106 * could have changed.
1108 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1111 update_learning_table__(const struct xbridge *xbridge,
1112 const struct flow *flow, struct flow_wildcards *wc,
1113 int vlan, struct xbundle *in_xbundle)
1114 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1116 struct mac_entry *mac;
1118 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1122 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1123 if (is_gratuitous_arp(flow, wc)) {
1124 /* We don't want to learn from gratuitous ARP packets that are
1125 * reflected back over bond slaves so we lock the learning table. */
1126 if (!in_xbundle->bond) {
1127 mac_entry_set_grat_arp_lock(mac);
1128 } else if (mac_entry_is_grat_arp_locked(mac)) {
1133 if (mac->port.p != in_xbundle->ofbundle) {
1134 /* The log messages here could actually be useful in debugging,
1135 * so keep the rate limit relatively high. */
1136 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1138 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1139 "on port %s in VLAN %d",
1140 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1141 in_xbundle->name, vlan);
1143 mac->port.p = in_xbundle->ofbundle;
1144 mac_learning_changed(xbridge->ml);
1149 update_learning_table(const struct xbridge *xbridge,
1150 const struct flow *flow, struct flow_wildcards *wc,
1151 int vlan, struct xbundle *in_xbundle)
1155 /* Don't learn the OFPP_NONE port. */
1156 if (in_xbundle == &ofpp_none_bundle) {
1160 /* First try the common case: no change to MAC learning table. */
1161 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1162 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1164 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1167 /* Slow path: MAC learning table might need an update. */
1168 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1169 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1170 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1174 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1175 * dropped. Returns true if they may be forwarded, false if they should be
1178 * 'in_port' must be the xport that corresponds to flow->in_port.
1179 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1181 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1182 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1183 * checked by input_vid_is_valid().
1185 * May also add tags to '*tags', although the current implementation only does
1186 * so in one special case.
1189 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1192 struct xbundle *in_xbundle = in_port->xbundle;
1193 const struct xbridge *xbridge = ctx->xbridge;
1194 struct flow *flow = &ctx->xin->flow;
1196 /* Drop frames for reserved multicast addresses
1197 * only if forward_bpdu option is absent. */
1198 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1199 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1203 if (in_xbundle->bond) {
1204 struct mac_entry *mac;
1206 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1212 xlate_report(ctx, "bonding refused admissibility, dropping");
1215 case BV_DROP_IF_MOVED:
1216 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1217 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1218 if (mac && mac->port.p != in_xbundle->ofbundle &&
1219 (!is_gratuitous_arp(flow, &ctx->xout->wc)
1220 || mac_entry_is_grat_arp_locked(mac))) {
1221 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1222 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1226 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1235 xlate_normal(struct xlate_ctx *ctx)
1237 struct flow_wildcards *wc = &ctx->xout->wc;
1238 struct flow *flow = &ctx->xin->flow;
1239 struct xbundle *in_xbundle;
1240 struct xport *in_port;
1241 struct mac_entry *mac;
1246 ctx->xout->has_normal = true;
1248 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1249 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1250 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1252 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
1253 ctx->xin->packet != NULL, &in_port);
1255 xlate_report(ctx, "no input bundle, dropping");
1259 /* Drop malformed frames. */
1260 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
1261 !(flow->vlan_tci & htons(VLAN_CFI))) {
1262 if (ctx->xin->packet != NULL) {
1263 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1264 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
1265 "VLAN tag received on port %s",
1266 ctx->xbridge->name, in_xbundle->name);
1268 xlate_report(ctx, "partial VLAN tag, dropping");
1272 /* Drop frames on bundles reserved for mirroring. */
1273 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
1274 if (ctx->xin->packet != NULL) {
1275 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1276 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1277 "%s, which is reserved exclusively for mirroring",
1278 ctx->xbridge->name, in_xbundle->name);
1280 xlate_report(ctx, "input port is mirror output port, dropping");
1285 vid = vlan_tci_to_vid(flow->vlan_tci);
1286 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1287 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
1290 vlan = input_vid_to_vlan(in_xbundle, vid);
1292 /* Check other admissibility requirements. */
1293 if (in_port && !is_admissible(ctx, in_port, vlan)) {
1297 /* Learn source MAC. */
1298 if (ctx->xin->may_learn) {
1299 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
1302 /* Determine output bundle. */
1303 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
1304 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
1305 mac_port = mac ? mac->port.p : NULL;
1306 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
1309 struct xbundle *mac_xbundle = xbundle_lookup(mac_port);
1310 if (mac_xbundle && mac_xbundle != in_xbundle) {
1311 xlate_report(ctx, "forwarding to learned port");
1312 output_normal(ctx, mac_xbundle, vlan);
1313 } else if (!mac_xbundle) {
1314 xlate_report(ctx, "learned port is unknown, dropping");
1316 xlate_report(ctx, "learned port is input port, dropping");
1319 struct xbundle *xbundle;
1321 xlate_report(ctx, "no learned MAC for destination, flooding");
1322 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1323 if (xbundle != in_xbundle
1324 && xbundle_includes_vlan(xbundle, vlan)
1325 && xbundle->floodable
1326 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1327 output_normal(ctx, xbundle, vlan);
1330 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1334 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1335 * the number of packets out of UINT32_MAX to sample. The given
1336 * cookie is passed back in the callback for each sampled packet.
1339 compose_sample_action(const struct xbridge *xbridge,
1340 struct ofpbuf *odp_actions,
1341 const struct flow *flow,
1342 const uint32_t probability,
1343 const union user_action_cookie *cookie,
1344 const size_t cookie_size)
1346 size_t sample_offset, actions_offset;
1347 odp_port_t odp_port;
1351 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
1353 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
1355 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
1357 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
1358 pid = dpif_port_get_pid(xbridge->dpif, odp_port);
1359 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size, odp_actions);
1361 nl_msg_end_nested(odp_actions, actions_offset);
1362 nl_msg_end_nested(odp_actions, sample_offset);
1363 return cookie_offset;
1367 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
1368 odp_port_t odp_port, unsigned int n_outputs,
1369 union user_action_cookie *cookie)
1373 cookie->type = USER_ACTION_COOKIE_SFLOW;
1374 cookie->sflow.vlan_tci = vlan_tci;
1376 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1377 * port information") for the interpretation of cookie->output. */
1378 switch (n_outputs) {
1380 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1381 cookie->sflow.output = 0x40000000 | 256;
1385 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
1387 cookie->sflow.output = ifindex;
1392 /* 0x80000000 means "multiple output ports. */
1393 cookie->sflow.output = 0x80000000 | n_outputs;
1398 /* Compose SAMPLE action for sFlow bridge sampling. */
1400 compose_sflow_action(const struct xbridge *xbridge,
1401 struct ofpbuf *odp_actions,
1402 const struct flow *flow,
1403 odp_port_t odp_port)
1405 uint32_t probability;
1406 union user_action_cookie cookie;
1408 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
1412 probability = dpif_sflow_get_probability(xbridge->sflow);
1413 compose_sflow_cookie(xbridge, htons(0), odp_port,
1414 odp_port == ODPP_NONE ? 0 : 1, &cookie);
1416 return compose_sample_action(xbridge, odp_actions, flow, probability,
1417 &cookie, sizeof cookie.sflow);
1421 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
1422 uint32_t obs_domain_id, uint32_t obs_point_id,
1423 union user_action_cookie *cookie)
1425 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
1426 cookie->flow_sample.probability = probability;
1427 cookie->flow_sample.collector_set_id = collector_set_id;
1428 cookie->flow_sample.obs_domain_id = obs_domain_id;
1429 cookie->flow_sample.obs_point_id = obs_point_id;
1433 compose_ipfix_cookie(union user_action_cookie *cookie)
1435 cookie->type = USER_ACTION_COOKIE_IPFIX;
1438 /* Compose SAMPLE action for IPFIX bridge sampling. */
1440 compose_ipfix_action(const struct xbridge *xbridge,
1441 struct ofpbuf *odp_actions,
1442 const struct flow *flow)
1444 uint32_t probability;
1445 union user_action_cookie cookie;
1447 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
1451 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
1452 compose_ipfix_cookie(&cookie);
1454 compose_sample_action(xbridge, odp_actions, flow, probability,
1455 &cookie, sizeof cookie.ipfix);
1458 /* SAMPLE action for sFlow must be first action in any given list of
1459 * actions. At this point we do not have all information required to
1460 * build it. So try to build sample action as complete as possible. */
1462 add_sflow_action(struct xlate_ctx *ctx)
1464 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
1465 &ctx->xout->odp_actions,
1466 &ctx->xin->flow, ODPP_NONE);
1467 ctx->sflow_odp_port = 0;
1468 ctx->sflow_n_outputs = 0;
1471 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
1472 * of actions, eventually after the SAMPLE action for sFlow. */
1474 add_ipfix_action(struct xlate_ctx *ctx)
1476 compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions,
1480 /* Fix SAMPLE action according to data collected while composing ODP actions.
1481 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
1482 * USERSPACE action's user-cookie which is required for sflow. */
1484 fix_sflow_action(struct xlate_ctx *ctx)
1486 const struct flow *base = &ctx->base_flow;
1487 union user_action_cookie *cookie;
1489 if (!ctx->user_cookie_offset) {
1493 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
1494 sizeof cookie->sflow);
1495 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
1497 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
1498 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
1501 static enum slow_path_reason
1502 process_special(struct xlate_ctx *ctx, const struct flow *flow,
1503 const struct xport *xport, const struct ofpbuf *packet)
1505 struct flow_wildcards *wc = &ctx->xout->wc;
1506 const struct xbridge *xbridge = ctx->xbridge;
1510 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
1512 cfm_process_heartbeat(xport->cfm, packet);
1515 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
1517 bfd_process_packet(xport->bfd, flow, packet);
1520 } else if (xport->xbundle && xport->xbundle->lacp
1521 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1523 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
1526 } else if (xbridge->stp && stp_should_process_flow(flow, wc)) {
1528 stp_process_packet(xport, packet);
1537 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
1540 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1541 struct flow_wildcards *wc = &ctx->xout->wc;
1542 struct flow *flow = &ctx->xin->flow;
1543 ovs_be16 flow_vlan_tci;
1544 uint32_t flow_pkt_mark;
1545 uint8_t flow_nw_tos;
1546 odp_port_t out_port, odp_port;
1549 /* If 'struct flow' gets additional metadata, we'll need to zero it out
1550 * before traversing a patch port. */
1551 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 21);
1554 xlate_report(ctx, "Nonexistent output port");
1556 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
1557 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
1559 } else if (check_stp && !xport_stp_forward_state(xport)) {
1560 xlate_report(ctx, "STP not in forwarding state, skipping output");
1564 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
1565 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
1570 const struct xport *peer = xport->peer;
1571 struct flow old_flow = ctx->xin->flow;
1572 enum slow_path_reason special;
1574 ctx->xbridge = peer->xbridge;
1575 flow->in_port.ofp_port = peer->ofp_port;
1576 flow->metadata = htonll(0);
1577 memset(&flow->tunnel, 0, sizeof flow->tunnel);
1578 memset(flow->regs, 0, sizeof flow->regs);
1580 special = process_special(ctx, &ctx->xin->flow, peer,
1583 ctx->xout->slow = special;
1584 } else if (may_receive(peer, ctx)) {
1585 if (xport_stp_forward_state(peer)) {
1586 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1588 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
1589 * learning action look at the packet, then drop it. */
1590 struct flow old_base_flow = ctx->base_flow;
1591 size_t old_size = ctx->xout->odp_actions.size;
1592 mirror_mask_t old_mirrors = ctx->xout->mirrors;
1593 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1594 ctx->xout->mirrors = old_mirrors;
1595 ctx->base_flow = old_base_flow;
1596 ctx->xout->odp_actions.size = old_size;
1600 ctx->xin->flow = old_flow;
1601 ctx->xbridge = xport->xbridge;
1603 if (ctx->xin->resubmit_stats) {
1604 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1605 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
1611 flow_vlan_tci = flow->vlan_tci;
1612 flow_pkt_mark = flow->pkt_mark;
1613 flow_nw_tos = flow->nw_tos;
1615 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
1616 wc->masks.nw_tos |= IP_ECN_MASK;
1617 flow->nw_tos &= ~IP_DSCP_MASK;
1618 flow->nw_tos |= dscp;
1621 if (xport->is_tunnel) {
1622 /* Save tunnel metadata so that changes made due to
1623 * the Logical (tunnel) Port are not visible for any further
1624 * matches, while explicit set actions on tunnel metadata are.
1626 struct flow_tnl flow_tnl = flow->tunnel;
1627 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
1628 if (odp_port == ODPP_NONE) {
1629 xlate_report(ctx, "Tunneling decided against output");
1630 goto out; /* restore flow_nw_tos */
1632 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
1633 xlate_report(ctx, "Not tunneling to our own address");
1634 goto out; /* restore flow_nw_tos */
1636 if (ctx->xin->resubmit_stats) {
1637 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1639 out_port = odp_port;
1640 commit_odp_tunnel_action(flow, &ctx->base_flow,
1641 &ctx->xout->odp_actions);
1642 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
1644 ofp_port_t vlandev_port;
1646 odp_port = xport->odp_port;
1647 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
1648 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1650 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto, ofp_port,
1652 if (vlandev_port == ofp_port) {
1653 out_port = odp_port;
1655 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
1656 flow->vlan_tci = htons(0);
1660 if (out_port != ODPP_NONE) {
1661 commit_odp_actions(flow, &ctx->base_flow,
1662 &ctx->xout->odp_actions, &ctx->xout->wc,
1663 &ctx->mpls_depth_delta);
1664 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
1667 ctx->sflow_odp_port = odp_port;
1668 ctx->sflow_n_outputs++;
1669 ctx->xout->nf_output_iface = ofp_port;
1674 flow->vlan_tci = flow_vlan_tci;
1675 flow->pkt_mark = flow_pkt_mark;
1676 flow->nw_tos = flow_nw_tos;
1680 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
1682 compose_output_action__(ctx, ofp_port, true);
1686 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
1688 struct rule_dpif *old_rule = ctx->rule;
1689 struct rule_actions *actions;
1691 if (ctx->xin->resubmit_stats) {
1692 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
1698 actions = rule_dpif_get_actions(rule);
1699 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
1700 rule_actions_unref(actions);
1701 ctx->rule = old_rule;
1706 xlate_table_action(struct xlate_ctx *ctx,
1707 ofp_port_t in_port, uint8_t table_id, bool may_packet_in)
1709 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1711 if (ctx->recurse >= MAX_RESUBMIT_RECURSION) {
1712 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
1713 MAX_RESUBMIT_RECURSION);
1714 } else if (ctx->resubmits >= MAX_RESUBMITS) {
1715 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
1716 } else if (ctx->xout->odp_actions.size > UINT16_MAX) {
1717 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
1718 } else if (ctx->stack.size >= 65536) {
1719 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
1721 struct rule_dpif *rule;
1722 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
1723 uint8_t old_table_id = ctx->table_id;
1725 ctx->table_id = table_id;
1727 /* Look up a flow with 'in_port' as the input port. Then restore the
1728 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
1729 * have surprising behavior). */
1730 ctx->xin->flow.in_port.ofp_port = in_port;
1731 rule_dpif_lookup_in_table(ctx->xbridge->ofproto,
1732 &ctx->xin->flow, &ctx->xout->wc,
1734 ctx->xin->flow.in_port.ofp_port = old_in_port;
1736 if (ctx->xin->resubmit_hook) {
1737 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
1740 if (!rule && may_packet_in) {
1741 struct xport *xport;
1744 * check if table configuration flags
1745 * OFPTC_TABLE_MISS_CONTROLLER, default.
1746 * OFPTC_TABLE_MISS_CONTINUE,
1747 * OFPTC_TABLE_MISS_DROP
1748 * When OF1.0, OFPTC_TABLE_MISS_CONTINUE is used. What to do? */
1749 xport = get_ofp_port(ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
1750 choose_miss_rule(xport ? xport->config : 0,
1751 ctx->xbridge->miss_rule,
1752 ctx->xbridge->no_packet_in_rule, &rule);
1755 xlate_recursively(ctx, rule);
1756 rule_dpif_unref(rule);
1759 ctx->table_id = old_table_id;
1767 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
1768 const struct ofpact_resubmit *resubmit)
1773 in_port = resubmit->in_port;
1774 if (in_port == OFPP_IN_PORT) {
1775 in_port = ctx->xin->flow.in_port.ofp_port;
1778 table_id = resubmit->table_id;
1779 if (table_id == 255) {
1780 table_id = ctx->table_id;
1783 xlate_table_action(ctx, in_port, table_id, false);
1787 flood_packets(struct xlate_ctx *ctx, bool all)
1789 const struct xport *xport;
1791 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
1792 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
1797 compose_output_action__(ctx, xport->ofp_port, false);
1798 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
1799 compose_output_action(ctx, xport->ofp_port);
1803 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1807 execute_controller_action(struct xlate_ctx *ctx, int len,
1808 enum ofp_packet_in_reason reason,
1809 uint16_t controller_id)
1811 struct ofputil_packet_in *pin;
1812 struct ofpbuf *packet;
1815 ovs_assert(!ctx->xout->slow || ctx->xout->slow == SLOW_CONTROLLER);
1816 ctx->xout->slow = SLOW_CONTROLLER;
1817 if (!ctx->xin->packet) {
1821 packet = ofpbuf_clone(ctx->xin->packet);
1823 key.skb_priority = 0;
1825 memset(&key.tunnel, 0, sizeof key.tunnel);
1827 commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1828 &ctx->xout->odp_actions, &ctx->xout->wc,
1829 &ctx->mpls_depth_delta);
1831 odp_execute_actions(NULL, packet, &key, ctx->xout->odp_actions.data,
1832 ctx->xout->odp_actions.size, NULL, NULL);
1834 pin = xmalloc(sizeof *pin);
1835 pin->packet_len = packet->size;
1836 pin->packet = ofpbuf_steal_data(packet);
1837 pin->reason = reason;
1838 pin->controller_id = controller_id;
1839 pin->table_id = ctx->table_id;
1840 pin->cookie = ctx->rule ? rule_dpif_get_flow_cookie(ctx->rule) : 0;
1842 pin->send_len = len;
1843 flow_get_metadata(&ctx->xin->flow, &pin->fmd);
1845 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
1846 ofpbuf_delete(packet);
1850 compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1852 struct flow_wildcards *wc = &ctx->xout->wc;
1853 struct flow *flow = &ctx->xin->flow;
1855 ovs_assert(eth_type_mpls(eth_type));
1857 /* If mpls_depth_delta is negative then an MPLS POP action has been
1858 * composed and the resulting MPLS label stack is unknown. This means
1859 * an MPLS PUSH action can't be composed as it needs to know either the
1860 * top-most MPLS LSE to use as a template for the new MPLS LSE, or that
1861 * there is no MPLS label stack present. Thus, stop processing.
1863 * If mpls_depth_delta is positive then an MPLS PUSH action has been
1864 * composed and no further MPLS PUSH action may be performed without
1865 * losing MPLS LSE and ether type information held in xtx->xin->flow.
1866 * Thus, stop processing.
1868 * If the MPLS LSE of the flow and base_flow differ then the MPLS LSE
1869 * has been updated. Performing a MPLS PUSH action may be would result in
1870 * losing MPLS LSE and ether type information held in xtx->xin->flow.
1871 * Thus, stop processing.
1873 * It is planned that in the future this case will be handled
1874 * by recirculation */
1875 if (ctx->mpls_depth_delta ||
1876 ctx->xin->flow.mpls_lse != ctx->base_flow.mpls_lse) {
1880 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1882 ctx->pre_push_mpls_lse = ctx->xin->flow.mpls_lse;
1884 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
1885 flow->mpls_lse &= ~htonl(MPLS_BOS_MASK);
1890 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1891 label = htonl(0x2); /* IPV6 Explicit Null. */
1893 label = htonl(0x0); /* IPV4 Explicit Null. */
1895 wc->masks.nw_tos |= IP_DSCP_MASK;
1896 wc->masks.nw_ttl = 0xff;
1897 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
1898 ttl = flow->nw_ttl ? flow->nw_ttl : 0x40;
1899 flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
1901 flow->dl_type = eth_type;
1902 ctx->mpls_depth_delta++;
1908 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1910 struct flow_wildcards *wc = &ctx->xout->wc;
1912 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
1916 /* If mpls_depth_delta is negative then an MPLS POP action has been
1917 * composed. Performing another MPLS POP action
1918 * would result in losing ether type that results from
1919 * the already composed MPLS POP. Thus, stop processing.
1921 * It is planned that in the future this case will be handled
1922 * by recirculation */
1923 if (ctx->mpls_depth_delta < 0) {
1927 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1929 /* If mpls_depth_delta is positive then an MPLS PUSH action has been
1930 * executed and the previous MPLS LSE saved in ctx->pre_push_mpls_lse. The
1931 * flow's MPLS LSE should be restored to that value to allow any
1932 * subsequent actions that update of the LSE to be executed correctly.
1934 if (ctx->mpls_depth_delta > 0) {
1935 ctx->xin->flow.mpls_lse = ctx->pre_push_mpls_lse;
1938 ctx->xin->flow.dl_type = eth_type;
1939 ctx->mpls_depth_delta--;
1945 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
1947 struct flow *flow = &ctx->xin->flow;
1949 if (!is_ip_any(flow)) {
1953 ctx->xout->wc.masks.nw_ttl = 0xff;
1954 if (flow->nw_ttl > 1) {
1960 for (i = 0; i < ids->n_controllers; i++) {
1961 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
1965 /* Stop processing for current table. */
1971 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
1973 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
1977 /* If mpls_depth_delta is negative then an MPLS POP action has been
1978 * executed and the resulting MPLS label stack is unknown. This means
1979 * a SET MPLS TTL push action can't be executed as it needs to manipulate
1980 * the top-most MPLS LSE. Thus, stop processing.
1982 * It is planned that in the future this case will be handled
1985 if (ctx->mpls_depth_delta < 0) {
1989 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_TTL_MASK);
1990 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl);
1995 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
1997 struct flow *flow = &ctx->xin->flow;
1998 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse);
1999 struct flow_wildcards *wc = &ctx->xout->wc;
2001 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2003 if (!eth_type_mpls(flow->dl_type)) {
2009 set_mpls_lse_ttl(&flow->mpls_lse, ttl);
2012 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
2014 /* Stop processing for current table. */
2020 xlate_output_action(struct xlate_ctx *ctx,
2021 ofp_port_t port, uint16_t max_len, bool may_packet_in)
2023 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
2025 ctx->xout->nf_output_iface = NF_OUT_DROP;
2029 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
2032 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2039 flood_packets(ctx, false);
2042 flood_packets(ctx, true);
2044 case OFPP_CONTROLLER:
2045 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
2051 if (port != ctx->xin->flow.in_port.ofp_port) {
2052 compose_output_action(ctx, port);
2054 xlate_report(ctx, "skipping output to input port");
2059 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2060 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2061 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2062 ctx->xout->nf_output_iface = prev_nf_output_iface;
2063 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2064 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2065 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2070 xlate_output_reg_action(struct xlate_ctx *ctx,
2071 const struct ofpact_output_reg *or)
2073 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
2074 if (port <= UINT16_MAX) {
2075 union mf_subvalue value;
2077 memset(&value, 0xff, sizeof value);
2078 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
2079 xlate_output_action(ctx, u16_to_ofp(port),
2080 or->max_len, false);
2085 xlate_enqueue_action(struct xlate_ctx *ctx,
2086 const struct ofpact_enqueue *enqueue)
2088 ofp_port_t ofp_port = enqueue->port;
2089 uint32_t queue_id = enqueue->queue;
2090 uint32_t flow_priority, priority;
2093 /* Translate queue to priority. */
2094 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
2096 /* Fall back to ordinary output action. */
2097 xlate_output_action(ctx, enqueue->port, 0, false);
2101 /* Check output port. */
2102 if (ofp_port == OFPP_IN_PORT) {
2103 ofp_port = ctx->xin->flow.in_port.ofp_port;
2104 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
2108 /* Add datapath actions. */
2109 flow_priority = ctx->xin->flow.skb_priority;
2110 ctx->xin->flow.skb_priority = priority;
2111 compose_output_action(ctx, ofp_port);
2112 ctx->xin->flow.skb_priority = flow_priority;
2114 /* Update NetFlow output port. */
2115 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2116 ctx->xout->nf_output_iface = ofp_port;
2117 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2118 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2123 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
2125 uint32_t skb_priority;
2127 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
2128 ctx->xin->flow.skb_priority = skb_priority;
2130 /* Couldn't translate queue to a priority. Nothing to do. A warning
2131 * has already been logged. */
2136 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
2138 const struct xbridge *xbridge = xbridge_;
2149 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
2152 port = get_ofp_port(xbridge, ofp_port);
2153 return port ? port->may_enable : false;
2158 xlate_bundle_action(struct xlate_ctx *ctx,
2159 const struct ofpact_bundle *bundle)
2163 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
2165 CONST_CAST(struct xbridge *, ctx->xbridge));
2166 if (bundle->dst.field) {
2167 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
2170 xlate_output_action(ctx, port, 0, false);
2175 xlate_learn_action(struct xlate_ctx *ctx,
2176 const struct ofpact_learn *learn)
2178 uint64_t ofpacts_stub[1024 / 8];
2179 struct ofputil_flow_mod fm;
2180 struct ofpbuf ofpacts;
2182 ctx->xout->has_learn = true;
2184 learn_mask(learn, &ctx->xout->wc);
2186 if (!ctx->xin->may_learn) {
2190 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2191 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
2192 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, &fm);
2193 ofpbuf_uninit(&ofpacts);
2197 xlate_fin_timeout(struct xlate_ctx *ctx,
2198 const struct ofpact_fin_timeout *oft)
2200 if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
2201 rule_dpif_reduce_timeouts(ctx->rule, oft->fin_idle_timeout,
2202 oft->fin_hard_timeout);
2207 xlate_sample_action(struct xlate_ctx *ctx,
2208 const struct ofpact_sample *os)
2210 union user_action_cookie cookie;
2211 /* Scale the probability from 16-bit to 32-bit while representing
2212 * the same percentage. */
2213 uint32_t probability = (os->probability << 16) | os->probability;
2215 commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2216 &ctx->xout->odp_actions, &ctx->xout->wc,
2217 &ctx->mpls_depth_delta);
2219 compose_flow_sample_cookie(os->probability, os->collector_set_id,
2220 os->obs_domain_id, os->obs_point_id, &cookie);
2221 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
2222 probability, &cookie, sizeof cookie.flow_sample);
2226 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
2228 if (xport->config & (eth_addr_equals(ctx->xin->flow.dl_dst, eth_addr_stp)
2229 ? OFPUTIL_PC_NO_RECV_STP
2230 : OFPUTIL_PC_NO_RECV)) {
2234 /* Only drop packets here if both forwarding and learning are
2235 * disabled. If just learning is enabled, we need to have
2236 * OFPP_NORMAL and the learning action have a look at the packet
2237 * before we can drop it. */
2238 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
2246 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
2247 struct xlate_ctx *ctx)
2249 struct flow_wildcards *wc = &ctx->xout->wc;
2250 struct flow *flow = &ctx->xin->flow;
2251 const struct ofpact *a;
2253 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2254 struct ofpact_controller *controller;
2255 const struct ofpact_metadata *metadata;
2263 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
2264 ofpact_get_OUTPUT(a)->max_len, true);
2268 /* XXX not yet implemented */
2271 case OFPACT_CONTROLLER:
2272 controller = ofpact_get_CONTROLLER(a);
2273 execute_controller_action(ctx, controller->max_len,
2275 controller->controller_id);
2278 case OFPACT_ENQUEUE:
2279 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
2282 case OFPACT_SET_VLAN_VID:
2283 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2284 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
2285 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
2289 case OFPACT_SET_VLAN_PCP:
2290 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
2291 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
2293 htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp << VLAN_PCP_SHIFT)
2297 case OFPACT_STRIP_VLAN:
2298 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2299 flow->vlan_tci = htons(0);
2302 case OFPACT_PUSH_VLAN:
2303 /* XXX 802.1AD(QinQ) */
2304 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2305 flow->vlan_tci = htons(VLAN_CFI);
2308 case OFPACT_SET_ETH_SRC:
2309 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2310 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2313 case OFPACT_SET_ETH_DST:
2314 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2315 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2318 case OFPACT_SET_IPV4_SRC:
2319 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2320 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2321 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2325 case OFPACT_SET_IPV4_DST:
2326 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2327 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2328 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
2332 case OFPACT_SET_IPV4_DSCP:
2333 wc->masks.nw_tos |= IP_DSCP_MASK;
2334 /* OpenFlow 1.0 only supports IPv4. */
2335 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2336 flow->nw_tos &= ~IP_DSCP_MASK;
2337 flow->nw_tos |= ofpact_get_SET_IPV4_DSCP(a)->dscp;
2341 case OFPACT_SET_L4_SRC_PORT:
2342 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2343 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2344 if (is_ip_any(flow)) {
2345 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2349 case OFPACT_SET_L4_DST_PORT:
2350 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2351 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2352 if (is_ip_any(flow)) {
2353 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2357 case OFPACT_RESUBMIT:
2358 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
2361 case OFPACT_SET_TUNNEL:
2362 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2365 case OFPACT_SET_QUEUE:
2366 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2369 case OFPACT_POP_QUEUE:
2370 flow->skb_priority = ctx->orig_skb_priority;
2373 case OFPACT_REG_MOVE:
2374 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
2377 case OFPACT_REG_LOAD:
2378 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow);
2381 case OFPACT_STACK_PUSH:
2382 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
2386 case OFPACT_STACK_POP:
2387 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
2391 case OFPACT_PUSH_MPLS:
2392 if (compose_mpls_push_action(ctx,
2393 ofpact_get_PUSH_MPLS(a)->ethertype)) {
2398 case OFPACT_POP_MPLS:
2399 if (compose_mpls_pop_action(ctx,
2400 ofpact_get_POP_MPLS(a)->ethertype)) {
2405 case OFPACT_SET_MPLS_TTL:
2406 if (compose_set_mpls_ttl_action(ctx,
2407 ofpact_get_SET_MPLS_TTL(a)->ttl)) {
2412 case OFPACT_DEC_MPLS_TTL:
2413 if (compose_dec_mpls_ttl_action(ctx)) {
2418 case OFPACT_DEC_TTL:
2419 wc->masks.nw_ttl = 0xff;
2420 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
2426 /* Nothing to do. */
2429 case OFPACT_MULTIPATH:
2430 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
2434 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
2437 case OFPACT_OUTPUT_REG:
2438 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
2442 xlate_learn_action(ctx, ofpact_get_LEARN(a));
2449 case OFPACT_FIN_TIMEOUT:
2450 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2451 ctx->xout->has_fin_timeout = true;
2452 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
2455 case OFPACT_CLEAR_ACTIONS:
2457 * Nothing to do because writa-actions is not supported for now.
2458 * When writa-actions is supported, clear-actions also must
2459 * be supported at the same time.
2463 case OFPACT_WRITE_METADATA:
2464 metadata = ofpact_get_WRITE_METADATA(a);
2465 flow->metadata &= ~metadata->mask;
2466 flow->metadata |= metadata->metadata & metadata->mask;
2470 /* Not implemented yet. */
2473 case OFPACT_GOTO_TABLE: {
2474 /* It is assumed that goto-table is the last action. */
2475 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
2477 ovs_assert(ctx->table_id < ogt->table_id);
2478 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2479 ogt->table_id, true);
2484 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
2491 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
2492 const struct flow *flow, struct rule_dpif *rule,
2493 uint8_t tcp_flags, const struct ofpbuf *packet)
2495 xin->ofproto = ofproto;
2497 xin->packet = packet;
2498 xin->may_learn = packet != NULL;
2500 xin->ofpacts = NULL;
2501 xin->ofpacts_len = 0;
2502 xin->tcp_flags = tcp_flags;
2503 xin->resubmit_hook = NULL;
2504 xin->report_hook = NULL;
2505 xin->resubmit_stats = NULL;
2509 xlate_out_uninit(struct xlate_out *xout)
2512 ofpbuf_uninit(&xout->odp_actions);
2516 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
2517 * into datapath actions, using 'ctx', and discards the datapath actions. */
2519 xlate_actions_for_side_effects(struct xlate_in *xin)
2521 struct xlate_out xout;
2523 xlate_actions(xin, &xout);
2524 xlate_out_uninit(&xout);
2528 xlate_report(struct xlate_ctx *ctx, const char *s)
2530 if (ctx->xin->report_hook) {
2531 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
2536 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
2539 dst->slow = src->slow;
2540 dst->has_learn = src->has_learn;
2541 dst->has_normal = src->has_normal;
2542 dst->has_fin_timeout = src->has_fin_timeout;
2543 dst->nf_output_iface = src->nf_output_iface;
2544 dst->mirrors = src->mirrors;
2546 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
2547 sizeof dst->odp_actions_stub);
2548 ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
2549 src->odp_actions.size);
2552 /* Returns a reference to the sflow handled associated with ofproto, or NULL if
2553 * there is none. The caller is responsible for decrementing the results ref
2554 * count with dpif_sflow_unref(). */
2556 xlate_get_sflow(const struct ofproto_dpif *ofproto)
2558 struct dpif_sflow *sflow = NULL;
2559 struct xbridge *xbridge;
2561 ovs_rwlock_rdlock(&xlate_rwlock);
2562 xbridge = xbridge_lookup(ofproto);
2564 sflow = dpif_sflow_ref(xbridge->sflow);
2566 ovs_rwlock_unlock(&xlate_rwlock);
2571 /* Returns a reference to the ipfix handled associated with ofproto, or NULL if
2572 * there is none. The caller is responsible for decrementing the results ref
2573 * count with dpif_ipfix_unref(). */
2575 xlate_get_ipfix(const struct ofproto_dpif *ofproto)
2577 struct dpif_ipfix *ipfix = NULL;
2578 struct xbridge *xbridge;
2580 ovs_rwlock_rdlock(&xlate_rwlock);
2581 xbridge = xbridge_lookup(ofproto);
2583 ipfix = dpif_ipfix_ref(xbridge->ipfix);
2585 ovs_rwlock_unlock(&xlate_rwlock);
2590 static struct skb_priority_to_dscp *
2591 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
2593 struct skb_priority_to_dscp *pdscp;
2596 hash = hash_int(skb_priority, 0);
2597 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
2598 if (pdscp->skb_priority == skb_priority) {
2606 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
2609 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
2610 *dscp = pdscp ? pdscp->dscp : 0;
2611 return pdscp != NULL;
2615 clear_skb_priorities(struct xport *xport)
2617 struct skb_priority_to_dscp *pdscp, *next;
2619 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
2620 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
2626 actions_output_to_local_port(const struct xlate_ctx *ctx)
2628 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
2629 const struct nlattr *a;
2632 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions.data,
2633 ctx->xout->odp_actions.size) {
2634 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
2635 && nl_attr_get_odp_port(a) == local_odp_port) {
2642 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
2643 * into datapath actions in 'odp_actions', using 'ctx'.
2645 * The caller must take responsibility for eventually freeing 'xout', with
2646 * xlate_out_uninit(). */
2648 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
2650 struct flow_wildcards *wc = &xout->wc;
2651 struct flow *flow = &xin->flow;
2653 struct rule_actions *actions = NULL;
2654 enum slow_path_reason special;
2655 const struct ofpact *ofpacts;
2656 struct xport *in_port;
2657 struct flow orig_flow;
2658 struct xlate_ctx ctx;
2662 COVERAGE_INC(xlate_actions);
2664 ovs_rwlock_rdlock(&xlate_rwlock);
2666 /* Flow initialization rules:
2667 * - 'base_flow' must match the kernel's view of the packet at the
2668 * time that action processing starts. 'flow' represents any
2669 * transformations we wish to make through actions.
2670 * - By default 'base_flow' and 'flow' are the same since the input
2671 * packet matches the output before any actions are applied.
2672 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
2673 * of the received packet as seen by the kernel. If we later output
2674 * to another device without any modifications this will cause us to
2675 * insert a new tag since the original one was stripped off by the
2677 * - Tunnel metadata as received is retained in 'flow'. This allows
2678 * tunnel metadata matching also in later tables.
2679 * Since a kernel action for setting the tunnel metadata will only be
2680 * generated with actual tunnel output, changing the tunnel metadata
2681 * values in 'flow' (such as tun_id) will only have effect with a later
2682 * tunnel output action.
2683 * - Tunnel 'base_flow' is completely cleared since that is what the
2684 * kernel does. If we wish to maintain the original values an action
2685 * needs to be generated. */
2690 ctx.xout->has_learn = false;
2691 ctx.xout->has_normal = false;
2692 ctx.xout->has_fin_timeout = false;
2693 ctx.xout->nf_output_iface = NF_OUT_DROP;
2694 ctx.xout->mirrors = 0;
2695 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
2696 sizeof ctx.xout->odp_actions_stub);
2697 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
2699 ctx.xbridge = xbridge_lookup(xin->ofproto);
2704 ctx.rule = xin->rule;
2706 ctx.base_flow = *flow;
2707 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
2708 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
2710 flow_wildcards_init_catchall(wc);
2711 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
2712 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
2713 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
2714 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
2716 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
2717 if (ctx.xbridge->has_netflow) {
2718 netflow_mask_wc(flow, wc);
2723 ctx.orig_skb_priority = flow->skb_priority;
2726 ctx.mpls_depth_delta = 0;
2729 ofpacts = xin->ofpacts;
2730 ofpacts_len = xin->ofpacts_len;
2731 } else if (xin->rule) {
2732 actions = rule_dpif_get_actions(xin->rule);
2733 ofpacts = actions->ofpacts;
2734 ofpacts_len = actions->ofpacts_len;
2739 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
2741 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
2742 /* Do this conditionally because the copy is expensive enough that it
2743 * shows up in profiles. */
2747 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
2748 switch (ctx.xbridge->frag) {
2749 case OFPC_FRAG_NORMAL:
2750 /* We must pretend that transport ports are unavailable. */
2751 flow->tp_src = ctx.base_flow.tp_src = htons(0);
2752 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
2755 case OFPC_FRAG_DROP:
2758 case OFPC_FRAG_REASM:
2761 case OFPC_FRAG_NX_MATCH:
2762 /* Nothing to do. */
2765 case OFPC_INVALID_TTL_TO_CONTROLLER:
2770 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
2771 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
2773 ctx.xout->slow = special;
2775 size_t sample_actions_len;
2777 if (flow->in_port.ofp_port
2778 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
2779 flow->in_port.ofp_port,
2781 ctx.base_flow.vlan_tci = 0;
2784 add_sflow_action(&ctx);
2785 add_ipfix_action(&ctx);
2786 sample_actions_len = ctx.xout->odp_actions.size;
2788 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
2789 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
2791 /* We've let OFPP_NORMAL and the learning action look at the
2792 * packet, so drop it now if forwarding is disabled. */
2793 if (in_port && !xport_stp_forward_state(in_port)) {
2794 ctx.xout->odp_actions.size = sample_actions_len;
2798 if (ctx.xbridge->has_in_band
2799 && in_band_must_output_to_local_port(flow)
2800 && !actions_output_to_local_port(&ctx)) {
2801 compose_output_action(&ctx, OFPP_LOCAL);
2804 fix_sflow_action(&ctx);
2806 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
2807 add_mirror_actions(&ctx, &orig_flow);
2811 ofpbuf_uninit(&ctx.stack);
2813 /* Clear the metadata and register wildcard masks, because we won't
2814 * use non-header fields as part of the cache. */
2815 memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
2816 memset(&wc->masks.regs, 0, sizeof wc->masks.regs);
2819 ovs_rwlock_unlock(&xlate_rwlock);
2821 rule_actions_unref(actions);