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_actions__(struct xlate_in *, struct xlate_out *)
210 OVS_REQ_RDLOCK(xlate_rwlock);
211 static void xlate_normal(struct xlate_ctx *);
212 static void xlate_report(struct xlate_ctx *, const char *);
213 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
214 uint8_t table_id, bool may_packet_in);
215 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
216 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
217 static void output_normal(struct xlate_ctx *, const struct xbundle *,
219 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
221 static struct xbridge *xbridge_lookup(const struct ofproto_dpif *);
222 static struct xbundle *xbundle_lookup(const struct ofbundle *);
223 static struct xport *xport_lookup(const struct ofport_dpif *);
224 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
225 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
226 uint32_t skb_priority);
227 static void clear_skb_priorities(struct xport *);
228 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
232 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
233 struct dpif *dpif, struct rule_dpif *miss_rule,
234 struct rule_dpif *no_packet_in_rule,
235 const struct mac_learning *ml, struct stp *stp,
236 const struct mbridge *mbridge,
237 const struct dpif_sflow *sflow,
238 const struct dpif_ipfix *ipfix, enum ofp_config_flags frag,
239 bool forward_bpdu, bool has_in_band, bool has_netflow)
241 struct xbridge *xbridge = xbridge_lookup(ofproto);
244 xbridge = xzalloc(sizeof *xbridge);
245 xbridge->ofproto = ofproto;
247 hmap_insert(&xbridges, &xbridge->hmap_node, hash_pointer(ofproto, 0));
248 hmap_init(&xbridge->xports);
249 list_init(&xbridge->xbundles);
252 if (xbridge->ml != ml) {
253 mac_learning_unref(xbridge->ml);
254 xbridge->ml = mac_learning_ref(ml);
257 if (xbridge->mbridge != mbridge) {
258 mbridge_unref(xbridge->mbridge);
259 xbridge->mbridge = mbridge_ref(mbridge);
262 if (xbridge->sflow != sflow) {
263 dpif_sflow_unref(xbridge->sflow);
264 xbridge->sflow = dpif_sflow_ref(sflow);
267 if (xbridge->ipfix != ipfix) {
268 dpif_ipfix_unref(xbridge->ipfix);
269 xbridge->ipfix = dpif_ipfix_ref(ipfix);
272 if (xbridge->stp != stp) {
273 stp_unref(xbridge->stp);
274 xbridge->stp = stp_ref(stp);
278 xbridge->name = xstrdup(name);
280 xbridge->dpif = dpif;
281 xbridge->forward_bpdu = forward_bpdu;
282 xbridge->has_in_band = has_in_band;
283 xbridge->has_netflow = has_netflow;
284 xbridge->frag = frag;
285 xbridge->miss_rule = miss_rule;
286 xbridge->no_packet_in_rule = no_packet_in_rule;
290 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
292 struct xbridge *xbridge = xbridge_lookup(ofproto);
293 struct xbundle *xbundle, *next_xbundle;
294 struct xport *xport, *next_xport;
300 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
301 xlate_ofport_remove(xport->ofport);
304 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
305 xlate_bundle_remove(xbundle->ofbundle);
308 hmap_remove(&xbridges, &xbridge->hmap_node);
309 mac_learning_unref(xbridge->ml);
310 mbridge_unref(xbridge->mbridge);
311 dpif_sflow_unref(xbridge->sflow);
312 dpif_ipfix_unref(xbridge->ipfix);
313 stp_unref(xbridge->stp);
314 hmap_destroy(&xbridge->xports);
320 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
321 const char *name, enum port_vlan_mode vlan_mode, int vlan,
322 unsigned long *trunks, bool use_priority_tags,
323 const struct bond *bond, const struct lacp *lacp,
326 struct xbundle *xbundle = xbundle_lookup(ofbundle);
329 xbundle = xzalloc(sizeof *xbundle);
330 xbundle->ofbundle = ofbundle;
331 xbundle->xbridge = xbridge_lookup(ofproto);
333 hmap_insert(&xbundles, &xbundle->hmap_node, hash_pointer(ofbundle, 0));
334 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
335 list_init(&xbundle->xports);
338 ovs_assert(xbundle->xbridge);
341 xbundle->name = xstrdup(name);
343 xbundle->vlan_mode = vlan_mode;
344 xbundle->vlan = vlan;
345 xbundle->trunks = trunks;
346 xbundle->use_priority_tags = use_priority_tags;
347 xbundle->floodable = floodable;
349 if (xbundle->bond != bond) {
350 bond_unref(xbundle->bond);
351 xbundle->bond = bond_ref(bond);
354 if (xbundle->lacp != lacp) {
355 lacp_unref(xbundle->lacp);
356 xbundle->lacp = lacp_ref(lacp);
361 xlate_bundle_remove(struct ofbundle *ofbundle)
363 struct xbundle *xbundle = xbundle_lookup(ofbundle);
364 struct xport *xport, *next;
370 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
371 list_remove(&xport->bundle_node);
372 xport->xbundle = NULL;
375 hmap_remove(&xbundles, &xbundle->hmap_node);
376 list_remove(&xbundle->list_node);
377 bond_unref(xbundle->bond);
378 lacp_unref(xbundle->lacp);
384 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
385 struct ofport_dpif *ofport, ofp_port_t ofp_port,
386 odp_port_t odp_port, const struct netdev *netdev,
387 const struct cfm *cfm, const struct bfd *bfd,
388 struct ofport_dpif *peer, int stp_port_no,
389 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
390 enum ofputil_port_config config, bool is_tunnel,
393 struct xport *xport = xport_lookup(ofport);
397 xport = xzalloc(sizeof *xport);
398 xport->ofport = ofport;
399 xport->xbridge = xbridge_lookup(ofproto);
400 xport->ofp_port = ofp_port;
402 hmap_init(&xport->skb_priorities);
403 hmap_insert(&xports, &xport->hmap_node, hash_pointer(ofport, 0));
404 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
405 hash_ofp_port(xport->ofp_port));
408 ovs_assert(xport->ofp_port == ofp_port);
410 xport->config = config;
411 xport->stp_port_no = stp_port_no;
412 xport->is_tunnel = is_tunnel;
413 xport->may_enable = may_enable;
414 xport->odp_port = odp_port;
416 if (xport->netdev != netdev) {
417 netdev_close(xport->netdev);
418 xport->netdev = netdev_ref(netdev);
421 if (xport->cfm != cfm) {
422 cfm_unref(xport->cfm);
423 xport->cfm = cfm_ref(cfm);
426 if (xport->bfd != bfd) {
427 bfd_unref(xport->bfd);
428 xport->bfd = bfd_ref(bfd);
432 xport->peer->peer = NULL;
434 xport->peer = xport_lookup(peer);
436 xport->peer->peer = xport;
439 if (xport->xbundle) {
440 list_remove(&xport->bundle_node);
442 xport->xbundle = xbundle_lookup(ofbundle);
443 if (xport->xbundle) {
444 list_insert(&xport->xbundle->xports, &xport->bundle_node);
447 clear_skb_priorities(xport);
448 for (i = 0; i < n_qdscp; i++) {
449 struct skb_priority_to_dscp *pdscp;
450 uint32_t skb_priority;
452 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
457 pdscp = xmalloc(sizeof *pdscp);
458 pdscp->skb_priority = skb_priority;
459 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
460 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
461 hash_int(pdscp->skb_priority, 0));
466 xlate_ofport_remove(struct ofport_dpif *ofport)
468 struct xport *xport = xport_lookup(ofport);
475 xport->peer->peer = NULL;
479 if (xport->xbundle) {
480 list_remove(&xport->bundle_node);
483 clear_skb_priorities(xport);
484 hmap_destroy(&xport->skb_priorities);
486 hmap_remove(&xports, &xport->hmap_node);
487 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
489 netdev_close(xport->netdev);
490 cfm_unref(xport->cfm);
491 bfd_unref(xport->bfd);
495 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
496 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
497 * Optionally, if nonnull, populates 'fitnessp' with the fitness of 'flow' as
498 * returned by odp_flow_key_to_flow(). Also, optionally populates 'ofproto'
499 * with the ofproto_dpif, and 'odp_in_port' with the datapath in_port, that
500 * 'packet' ingressed.
502 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
503 * 'flow''s in_port to OFPP_NONE.
505 * This function does post-processing on data returned from
506 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
507 * of the upcall processing logic. In particular, if the extracted in_port is
508 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
509 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
510 * a VLAN header onto 'packet' (if it is nonnull).
512 * Similarly, this function also includes some logic to help with tunnels. It
513 * may modify 'flow' as necessary to make the tunneling implementation
514 * transparent to the upcall processing logic.
516 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
517 * or some other positive errno if there are other problems. */
519 xlate_receive(const struct dpif_backer *backer, struct ofpbuf *packet,
520 const struct nlattr *key, size_t key_len,
521 struct flow *flow, enum odp_key_fitness *fitnessp,
522 struct ofproto_dpif **ofproto, odp_port_t *odp_in_port)
524 enum odp_key_fitness fitness;
525 const struct xport *xport;
528 ovs_rwlock_rdlock(&xlate_rwlock);
529 fitness = odp_flow_key_to_flow(key, key_len, flow);
530 if (fitness == ODP_FIT_ERROR) {
536 *odp_in_port = flow->in_port.odp_port;
539 xport = xport_lookup(tnl_port_should_receive(flow)
540 ? tnl_port_receive(flow)
541 : odp_port_to_ofport(backer, flow->in_port.odp_port));
543 flow->in_port.ofp_port = xport ? xport->ofp_port : OFPP_NONE;
548 if (vsp_adjust_flow(xport->xbridge->ofproto, flow)) {
550 /* Make the packet resemble the flow, so that it gets sent to
551 * an OpenFlow controller properly, so that it looks correct
552 * for sFlow, and so that flow_extract() will get the correct
553 * vlan_tci if it is called on 'packet'.
555 * The allocated space inside 'packet' probably also contains
556 * 'key', that is, both 'packet' and 'key' are probably part of
557 * a struct dpif_upcall (see the large comment on that
558 * structure definition), so pushing data on 'packet' is in
559 * general not a good idea since it could overwrite 'key' or
560 * free it as a side effect. However, it's OK in this special
561 * case because we know that 'packet' is inside a Netlink
562 * attribute: pushing 4 bytes will just overwrite the 4-byte
563 * "struct nlattr", which is fine since we don't need that
565 eth_push_vlan(packet, flow->vlan_tci);
567 /* We can't reproduce 'key' from 'flow'. */
568 fitness = fitness == ODP_FIT_PERFECT ? ODP_FIT_TOO_MUCH : fitness;
573 *ofproto = xport->xbridge->ofproto;
580 ovs_rwlock_unlock(&xlate_rwlock);
584 static struct xbridge *
585 xbridge_lookup(const struct ofproto_dpif *ofproto)
587 struct xbridge *xbridge;
593 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
595 if (xbridge->ofproto == ofproto) {
602 static struct xbundle *
603 xbundle_lookup(const struct ofbundle *ofbundle)
605 struct xbundle *xbundle;
611 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
613 if (xbundle->ofbundle == ofbundle) {
620 static struct xport *
621 xport_lookup(const struct ofport_dpif *ofport)
629 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
631 if (xport->ofport == ofport) {
638 static struct stp_port *
639 xport_get_stp_port(const struct xport *xport)
641 return xport->xbridge->stp && xport->stp_port_no != -1
642 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
646 static enum stp_state
647 xport_stp_learn_state(const struct xport *xport)
649 struct stp_port *sp = xport_get_stp_port(xport);
650 return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
654 xport_stp_forward_state(const struct xport *xport)
656 struct stp_port *sp = xport_get_stp_port(xport);
657 return stp_forward_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
660 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
661 * were used to make the determination.*/
663 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
665 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
666 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
670 stp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
672 struct stp_port *sp = xport_get_stp_port(xport);
673 struct ofpbuf payload = *packet;
674 struct eth_header *eth = payload.data;
676 /* Sink packets on ports that have STP disabled when the bridge has
678 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
682 /* Trim off padding on payload. */
683 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
684 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
687 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
688 stp_received_bpdu(sp, payload.data, payload.size);
692 static struct xport *
693 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
697 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
699 if (xport->ofp_port == ofp_port) {
707 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
709 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
710 return xport ? xport->odp_port : ODPP_NONE;
714 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
716 return (bundle->vlan_mode != PORT_VLAN_ACCESS
717 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
721 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
723 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
727 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
729 return xbundle != &ofpp_none_bundle
730 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
735 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
737 return xbundle != &ofpp_none_bundle
738 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
743 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
745 return xbundle != &ofpp_none_bundle
746 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
750 static struct xbundle *
751 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
752 bool warn, struct xport **in_xportp)
756 /* Find the port and bundle for the received packet. */
757 xport = get_ofp_port(xbridge, in_port);
761 if (xport && xport->xbundle) {
762 return xport->xbundle;
765 /* Special-case OFPP_NONE, which a controller may use as the ingress
766 * port for traffic that it is sourcing. */
767 if (in_port == OFPP_NONE) {
768 ofpp_none_bundle.name = "OFPP_NONE";
769 ofpp_none_bundle.vlan_mode = PORT_VLAN_TRUNK;
770 return &ofpp_none_bundle;
773 /* Odd. A few possible reasons here:
775 * - We deleted a port but there are still a few packets queued up
778 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
779 * we don't know about.
781 * - The ofproto client didn't configure the port as part of a bundle.
782 * This is particularly likely to happen if a packet was received on the
783 * port after it was created, but before the client had a chance to
784 * configure its bundle.
787 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
789 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
790 "port %"PRIu16, xbridge->name, in_port);
796 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
798 const struct xbridge *xbridge = ctx->xbridge;
799 mirror_mask_t mirrors;
800 struct xbundle *in_xbundle;
804 mirrors = ctx->xout->mirrors;
805 ctx->xout->mirrors = 0;
807 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
808 ctx->xin->packet != NULL, NULL);
812 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
814 /* Drop frames on bundles reserved for mirroring. */
815 if (xbundle_mirror_out(xbridge, in_xbundle)) {
816 if (ctx->xin->packet != NULL) {
817 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
818 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
819 "%s, which is reserved exclusively for mirroring",
820 ctx->xbridge->name, in_xbundle->name);
822 ofpbuf_clear(&ctx->xout->odp_actions);
827 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
828 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
831 vlan = input_vid_to_vlan(in_xbundle, vid);
837 /* Restore the original packet before adding the mirror actions. */
838 ctx->xin->flow = *orig_flow;
841 mirror_mask_t dup_mirrors;
842 struct ofbundle *out;
843 unsigned long *vlans;
848 has_mirror = mirror_get(xbridge->mbridge, mirror_mask_ffs(mirrors) - 1,
849 &vlans, &dup_mirrors, &out, &out_vlan);
850 ovs_assert(has_mirror);
853 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
855 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
858 if (!vlan_mirrored) {
859 mirrors = zero_rightmost_1bit(mirrors);
863 mirrors &= ~dup_mirrors;
864 ctx->xout->mirrors |= dup_mirrors;
866 struct xbundle *out_xbundle = xbundle_lookup(out);
868 output_normal(ctx, out_xbundle, vlan);
870 } else if (vlan != out_vlan
871 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
872 struct xbundle *xbundle;
874 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
875 if (xbundle_includes_vlan(xbundle, out_vlan)
876 && !xbundle_mirror_out(xbridge, xbundle)) {
877 output_normal(ctx, xbundle, out_vlan);
884 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
885 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
886 * the bundle on which the packet was received, returns the VLAN to which the
889 * Both 'vid' and the return value are in the range 0...4095. */
891 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
893 switch (in_xbundle->vlan_mode) {
894 case PORT_VLAN_ACCESS:
895 return in_xbundle->vlan;
898 case PORT_VLAN_TRUNK:
901 case PORT_VLAN_NATIVE_UNTAGGED:
902 case PORT_VLAN_NATIVE_TAGGED:
903 return vid ? vid : in_xbundle->vlan;
910 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
911 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
914 * 'vid' should be the VID obtained from the 802.1Q header that was received as
915 * part of a packet (specify 0 if there was no 802.1Q header), in the range
918 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
920 /* Allow any VID on the OFPP_NONE port. */
921 if (in_xbundle == &ofpp_none_bundle) {
925 switch (in_xbundle->vlan_mode) {
926 case PORT_VLAN_ACCESS:
929 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
930 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
931 "packet received on port %s configured as VLAN "
932 "%"PRIu16" access port", vid, in_xbundle->name,
939 case PORT_VLAN_NATIVE_UNTAGGED:
940 case PORT_VLAN_NATIVE_TAGGED:
942 /* Port must always carry its native VLAN. */
946 case PORT_VLAN_TRUNK:
947 if (!xbundle_includes_vlan(in_xbundle, vid)) {
949 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
950 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
951 "received on port %s not configured for trunking "
952 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
964 /* Given 'vlan', the VLAN that a packet belongs to, and
965 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
966 * that should be included in the 802.1Q header. (If the return value is 0,
967 * then the 802.1Q header should only be included in the packet if there is a
970 * Both 'vlan' and the return value are in the range 0...4095. */
972 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
974 switch (out_xbundle->vlan_mode) {
975 case PORT_VLAN_ACCESS:
978 case PORT_VLAN_TRUNK:
979 case PORT_VLAN_NATIVE_TAGGED:
982 case PORT_VLAN_NATIVE_UNTAGGED:
983 return vlan == out_xbundle->vlan ? 0 : vlan;
991 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
994 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
996 ovs_be16 tci, old_tci;
999 vid = output_vlan_to_vid(out_xbundle, vlan);
1000 if (list_is_empty(&out_xbundle->xports)) {
1001 /* Partially configured bundle with no slaves. Drop the packet. */
1003 } else if (!out_xbundle->bond) {
1004 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1007 struct ofport_dpif *ofport;
1009 ofport = bond_choose_output_slave(out_xbundle->bond, &ctx->xin->flow,
1010 &ctx->xout->wc, vid);
1011 xport = xport_lookup(ofport);
1014 /* No slaves enabled, so drop packet. */
1019 old_tci = *flow_tci;
1021 if (tci || out_xbundle->use_priority_tags) {
1022 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1024 tci |= htons(VLAN_CFI);
1029 compose_output_action(ctx, xport->ofp_port);
1030 *flow_tci = old_tci;
1033 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1034 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1035 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1037 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1039 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1043 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1044 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1048 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1049 if (flow->nw_proto == ARP_OP_REPLY) {
1051 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1052 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1053 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1055 return flow->nw_src == flow->nw_dst;
1061 /* Checks whether a MAC learning update is necessary for MAC learning table
1062 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1065 * Most packets processed through the MAC learning table do not actually
1066 * change it in any way. This function requires only a read lock on the MAC
1067 * learning table, so it is much cheaper in this common case.
1069 * Keep the code here synchronized with that in update_learning_table__()
1072 is_mac_learning_update_needed(const struct mac_learning *ml,
1073 const struct flow *flow,
1074 struct flow_wildcards *wc,
1075 int vlan, struct xbundle *in_xbundle)
1076 OVS_REQ_RDLOCK(ml->rwlock)
1078 struct mac_entry *mac;
1080 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1084 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1085 if (!mac || mac_entry_age(ml, mac)) {
1089 if (is_gratuitous_arp(flow, wc)) {
1090 /* We don't want to learn from gratuitous ARP packets that are
1091 * reflected back over bond slaves so we lock the learning table. */
1092 if (!in_xbundle->bond) {
1094 } else if (mac_entry_is_grat_arp_locked(mac)) {
1099 return mac->port.p != in_xbundle->ofbundle;
1103 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1104 * received on 'in_xbundle' in 'vlan'.
1106 * This code repeats all the checks in is_mac_learning_update_needed() because
1107 * the lock was released between there and here and thus the MAC learning state
1108 * could have changed.
1110 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1113 update_learning_table__(const struct xbridge *xbridge,
1114 const struct flow *flow, struct flow_wildcards *wc,
1115 int vlan, struct xbundle *in_xbundle)
1116 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1118 struct mac_entry *mac;
1120 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1124 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1125 if (is_gratuitous_arp(flow, wc)) {
1126 /* We don't want to learn from gratuitous ARP packets that are
1127 * reflected back over bond slaves so we lock the learning table. */
1128 if (!in_xbundle->bond) {
1129 mac_entry_set_grat_arp_lock(mac);
1130 } else if (mac_entry_is_grat_arp_locked(mac)) {
1135 if (mac->port.p != in_xbundle->ofbundle) {
1136 /* The log messages here could actually be useful in debugging,
1137 * so keep the rate limit relatively high. */
1138 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1140 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1141 "on port %s in VLAN %d",
1142 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1143 in_xbundle->name, vlan);
1145 mac->port.p = in_xbundle->ofbundle;
1146 mac_learning_changed(xbridge->ml);
1151 update_learning_table(const struct xbridge *xbridge,
1152 const struct flow *flow, struct flow_wildcards *wc,
1153 int vlan, struct xbundle *in_xbundle)
1157 /* Don't learn the OFPP_NONE port. */
1158 if (in_xbundle == &ofpp_none_bundle) {
1162 /* First try the common case: no change to MAC learning table. */
1163 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1164 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1166 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1169 /* Slow path: MAC learning table might need an update. */
1170 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1171 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1172 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1176 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1177 * dropped. Returns true if they may be forwarded, false if they should be
1180 * 'in_port' must be the xport that corresponds to flow->in_port.
1181 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1183 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1184 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1185 * checked by input_vid_is_valid().
1187 * May also add tags to '*tags', although the current implementation only does
1188 * so in one special case.
1191 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1194 struct xbundle *in_xbundle = in_port->xbundle;
1195 const struct xbridge *xbridge = ctx->xbridge;
1196 struct flow *flow = &ctx->xin->flow;
1198 /* Drop frames for reserved multicast addresses
1199 * only if forward_bpdu option is absent. */
1200 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1201 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1205 if (in_xbundle->bond) {
1206 struct mac_entry *mac;
1208 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1214 xlate_report(ctx, "bonding refused admissibility, dropping");
1217 case BV_DROP_IF_MOVED:
1218 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1219 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1220 if (mac && mac->port.p != in_xbundle->ofbundle &&
1221 (!is_gratuitous_arp(flow, &ctx->xout->wc)
1222 || mac_entry_is_grat_arp_locked(mac))) {
1223 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1224 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1228 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1237 xlate_normal(struct xlate_ctx *ctx)
1239 struct flow_wildcards *wc = &ctx->xout->wc;
1240 struct flow *flow = &ctx->xin->flow;
1241 struct xbundle *in_xbundle;
1242 struct xport *in_port;
1243 struct mac_entry *mac;
1248 ctx->xout->has_normal = true;
1250 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1251 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1252 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1254 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
1255 ctx->xin->packet != NULL, &in_port);
1257 xlate_report(ctx, "no input bundle, dropping");
1261 /* Drop malformed frames. */
1262 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
1263 !(flow->vlan_tci & htons(VLAN_CFI))) {
1264 if (ctx->xin->packet != NULL) {
1265 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1266 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
1267 "VLAN tag received on port %s",
1268 ctx->xbridge->name, in_xbundle->name);
1270 xlate_report(ctx, "partial VLAN tag, dropping");
1274 /* Drop frames on bundles reserved for mirroring. */
1275 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
1276 if (ctx->xin->packet != NULL) {
1277 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1278 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1279 "%s, which is reserved exclusively for mirroring",
1280 ctx->xbridge->name, in_xbundle->name);
1282 xlate_report(ctx, "input port is mirror output port, dropping");
1287 vid = vlan_tci_to_vid(flow->vlan_tci);
1288 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1289 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
1292 vlan = input_vid_to_vlan(in_xbundle, vid);
1294 /* Check other admissibility requirements. */
1295 if (in_port && !is_admissible(ctx, in_port, vlan)) {
1299 /* Learn source MAC. */
1300 if (ctx->xin->may_learn) {
1301 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
1304 /* Determine output bundle. */
1305 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
1306 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
1307 mac_port = mac ? mac->port.p : NULL;
1308 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
1311 struct xbundle *mac_xbundle = xbundle_lookup(mac_port);
1312 if (mac_xbundle && mac_xbundle != in_xbundle) {
1313 xlate_report(ctx, "forwarding to learned port");
1314 output_normal(ctx, mac_xbundle, vlan);
1315 } else if (!mac_xbundle) {
1316 xlate_report(ctx, "learned port is unknown, dropping");
1318 xlate_report(ctx, "learned port is input port, dropping");
1321 struct xbundle *xbundle;
1323 xlate_report(ctx, "no learned MAC for destination, flooding");
1324 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1325 if (xbundle != in_xbundle
1326 && xbundle_includes_vlan(xbundle, vlan)
1327 && xbundle->floodable
1328 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1329 output_normal(ctx, xbundle, vlan);
1332 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1336 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1337 * the number of packets out of UINT32_MAX to sample. The given
1338 * cookie is passed back in the callback for each sampled packet.
1341 compose_sample_action(const struct xbridge *xbridge,
1342 struct ofpbuf *odp_actions,
1343 const struct flow *flow,
1344 const uint32_t probability,
1345 const union user_action_cookie *cookie,
1346 const size_t cookie_size)
1348 size_t sample_offset, actions_offset;
1349 odp_port_t odp_port;
1353 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
1355 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
1357 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
1359 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
1360 pid = dpif_port_get_pid(xbridge->dpif, odp_port);
1361 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size, odp_actions);
1363 nl_msg_end_nested(odp_actions, actions_offset);
1364 nl_msg_end_nested(odp_actions, sample_offset);
1365 return cookie_offset;
1369 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
1370 odp_port_t odp_port, unsigned int n_outputs,
1371 union user_action_cookie *cookie)
1375 cookie->type = USER_ACTION_COOKIE_SFLOW;
1376 cookie->sflow.vlan_tci = vlan_tci;
1378 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1379 * port information") for the interpretation of cookie->output. */
1380 switch (n_outputs) {
1382 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1383 cookie->sflow.output = 0x40000000 | 256;
1387 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
1389 cookie->sflow.output = ifindex;
1394 /* 0x80000000 means "multiple output ports. */
1395 cookie->sflow.output = 0x80000000 | n_outputs;
1400 /* Compose SAMPLE action for sFlow bridge sampling. */
1402 compose_sflow_action(const struct xbridge *xbridge,
1403 struct ofpbuf *odp_actions,
1404 const struct flow *flow,
1405 odp_port_t odp_port)
1407 uint32_t probability;
1408 union user_action_cookie cookie;
1410 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
1414 probability = dpif_sflow_get_probability(xbridge->sflow);
1415 compose_sflow_cookie(xbridge, htons(0), odp_port,
1416 odp_port == ODPP_NONE ? 0 : 1, &cookie);
1418 return compose_sample_action(xbridge, odp_actions, flow, probability,
1419 &cookie, sizeof cookie.sflow);
1423 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
1424 uint32_t obs_domain_id, uint32_t obs_point_id,
1425 union user_action_cookie *cookie)
1427 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
1428 cookie->flow_sample.probability = probability;
1429 cookie->flow_sample.collector_set_id = collector_set_id;
1430 cookie->flow_sample.obs_domain_id = obs_domain_id;
1431 cookie->flow_sample.obs_point_id = obs_point_id;
1435 compose_ipfix_cookie(union user_action_cookie *cookie)
1437 cookie->type = USER_ACTION_COOKIE_IPFIX;
1440 /* Compose SAMPLE action for IPFIX bridge sampling. */
1442 compose_ipfix_action(const struct xbridge *xbridge,
1443 struct ofpbuf *odp_actions,
1444 const struct flow *flow)
1446 uint32_t probability;
1447 union user_action_cookie cookie;
1449 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
1453 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
1454 compose_ipfix_cookie(&cookie);
1456 compose_sample_action(xbridge, odp_actions, flow, probability,
1457 &cookie, sizeof cookie.ipfix);
1460 /* SAMPLE action for sFlow must be first action in any given list of
1461 * actions. At this point we do not have all information required to
1462 * build it. So try to build sample action as complete as possible. */
1464 add_sflow_action(struct xlate_ctx *ctx)
1466 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
1467 &ctx->xout->odp_actions,
1468 &ctx->xin->flow, ODPP_NONE);
1469 ctx->sflow_odp_port = 0;
1470 ctx->sflow_n_outputs = 0;
1473 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
1474 * of actions, eventually after the SAMPLE action for sFlow. */
1476 add_ipfix_action(struct xlate_ctx *ctx)
1478 compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions,
1482 /* Fix SAMPLE action according to data collected while composing ODP actions.
1483 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
1484 * USERSPACE action's user-cookie which is required for sflow. */
1486 fix_sflow_action(struct xlate_ctx *ctx)
1488 const struct flow *base = &ctx->base_flow;
1489 union user_action_cookie *cookie;
1491 if (!ctx->user_cookie_offset) {
1495 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
1496 sizeof cookie->sflow);
1497 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
1499 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
1500 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
1503 static enum slow_path_reason
1504 process_special(struct xlate_ctx *ctx, const struct flow *flow,
1505 const struct xport *xport, const struct ofpbuf *packet)
1507 struct flow_wildcards *wc = &ctx->xout->wc;
1508 const struct xbridge *xbridge = ctx->xbridge;
1512 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
1514 cfm_process_heartbeat(xport->cfm, packet);
1517 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
1519 bfd_process_packet(xport->bfd, flow, packet);
1522 } else if (xport->xbundle && xport->xbundle->lacp
1523 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1525 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
1528 } else if (xbridge->stp && stp_should_process_flow(flow, wc)) {
1530 stp_process_packet(xport, packet);
1539 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
1542 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1543 struct flow_wildcards *wc = &ctx->xout->wc;
1544 struct flow *flow = &ctx->xin->flow;
1545 ovs_be16 flow_vlan_tci;
1546 uint32_t flow_pkt_mark;
1547 uint8_t flow_nw_tos;
1548 odp_port_t out_port, odp_port;
1551 /* If 'struct flow' gets additional metadata, we'll need to zero it out
1552 * before traversing a patch port. */
1553 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 21);
1556 xlate_report(ctx, "Nonexistent output port");
1558 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
1559 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
1561 } else if (check_stp && !xport_stp_forward_state(xport)) {
1562 xlate_report(ctx, "STP not in forwarding state, skipping output");
1566 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
1567 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
1572 const struct xport *peer = xport->peer;
1573 struct flow old_flow = ctx->xin->flow;
1574 enum slow_path_reason special;
1576 ctx->xbridge = peer->xbridge;
1577 flow->in_port.ofp_port = peer->ofp_port;
1578 flow->metadata = htonll(0);
1579 memset(&flow->tunnel, 0, sizeof flow->tunnel);
1580 memset(flow->regs, 0, sizeof flow->regs);
1582 special = process_special(ctx, &ctx->xin->flow, peer,
1585 ctx->xout->slow |= special;
1586 } else if (may_receive(peer, ctx)) {
1587 if (xport_stp_forward_state(peer)) {
1588 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1590 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
1591 * learning action look at the packet, then drop it. */
1592 struct flow old_base_flow = ctx->base_flow;
1593 size_t old_size = ctx->xout->odp_actions.size;
1594 mirror_mask_t old_mirrors = ctx->xout->mirrors;
1595 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1596 ctx->xout->mirrors = old_mirrors;
1597 ctx->base_flow = old_base_flow;
1598 ctx->xout->odp_actions.size = old_size;
1602 ctx->xin->flow = old_flow;
1603 ctx->xbridge = xport->xbridge;
1605 if (ctx->xin->resubmit_stats) {
1606 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1607 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
1613 flow_vlan_tci = flow->vlan_tci;
1614 flow_pkt_mark = flow->pkt_mark;
1615 flow_nw_tos = flow->nw_tos;
1617 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
1618 wc->masks.nw_tos |= IP_ECN_MASK;
1619 flow->nw_tos &= ~IP_DSCP_MASK;
1620 flow->nw_tos |= dscp;
1623 if (xport->is_tunnel) {
1624 /* Save tunnel metadata so that changes made due to
1625 * the Logical (tunnel) Port are not visible for any further
1626 * matches, while explicit set actions on tunnel metadata are.
1628 struct flow_tnl flow_tnl = flow->tunnel;
1629 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
1630 if (odp_port == ODPP_NONE) {
1631 xlate_report(ctx, "Tunneling decided against output");
1632 goto out; /* restore flow_nw_tos */
1634 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
1635 xlate_report(ctx, "Not tunneling to our own address");
1636 goto out; /* restore flow_nw_tos */
1638 if (ctx->xin->resubmit_stats) {
1639 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1641 out_port = odp_port;
1642 commit_odp_tunnel_action(flow, &ctx->base_flow,
1643 &ctx->xout->odp_actions);
1644 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
1646 ofp_port_t vlandev_port;
1648 odp_port = xport->odp_port;
1649 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
1650 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1652 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto, ofp_port,
1654 if (vlandev_port == ofp_port) {
1655 out_port = odp_port;
1657 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
1658 flow->vlan_tci = htons(0);
1662 if (out_port != ODPP_NONE) {
1663 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
1664 &ctx->xout->odp_actions,
1666 &ctx->mpls_depth_delta);
1667 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
1670 ctx->sflow_odp_port = odp_port;
1671 ctx->sflow_n_outputs++;
1672 ctx->xout->nf_output_iface = ofp_port;
1677 flow->vlan_tci = flow_vlan_tci;
1678 flow->pkt_mark = flow_pkt_mark;
1679 flow->nw_tos = flow_nw_tos;
1683 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
1685 compose_output_action__(ctx, ofp_port, true);
1689 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
1691 struct rule_dpif *old_rule = ctx->rule;
1692 struct rule_actions *actions;
1694 if (ctx->xin->resubmit_stats) {
1695 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
1701 actions = rule_dpif_get_actions(rule);
1702 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
1703 rule_actions_unref(actions);
1704 ctx->rule = old_rule;
1709 xlate_table_action(struct xlate_ctx *ctx,
1710 ofp_port_t in_port, uint8_t table_id, bool may_packet_in)
1712 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1714 if (ctx->recurse >= MAX_RESUBMIT_RECURSION) {
1715 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
1716 MAX_RESUBMIT_RECURSION);
1717 } else if (ctx->resubmits >= MAX_RESUBMITS) {
1718 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
1719 } else if (ctx->xout->odp_actions.size > UINT16_MAX) {
1720 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
1721 } else if (ctx->stack.size >= 65536) {
1722 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
1724 struct rule_dpif *rule;
1725 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
1726 uint8_t old_table_id = ctx->table_id;
1728 ctx->table_id = table_id;
1730 /* Look up a flow with 'in_port' as the input port. Then restore the
1731 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
1732 * have surprising behavior). */
1733 ctx->xin->flow.in_port.ofp_port = in_port;
1734 rule_dpif_lookup_in_table(ctx->xbridge->ofproto,
1735 &ctx->xin->flow, &ctx->xout->wc,
1737 ctx->xin->flow.in_port.ofp_port = old_in_port;
1739 if (ctx->xin->resubmit_hook) {
1740 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
1743 if (!rule && may_packet_in) {
1744 struct xport *xport;
1747 * check if table configuration flags
1748 * OFPTC_TABLE_MISS_CONTROLLER, default.
1749 * OFPTC_TABLE_MISS_CONTINUE,
1750 * OFPTC_TABLE_MISS_DROP
1751 * When OF1.0, OFPTC_TABLE_MISS_CONTINUE is used. What to do? */
1752 xport = get_ofp_port(ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
1753 choose_miss_rule(xport ? xport->config : 0,
1754 ctx->xbridge->miss_rule,
1755 ctx->xbridge->no_packet_in_rule, &rule);
1758 xlate_recursively(ctx, rule);
1759 rule_dpif_unref(rule);
1762 ctx->table_id = old_table_id;
1770 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
1771 const struct ofpact_resubmit *resubmit)
1776 in_port = resubmit->in_port;
1777 if (in_port == OFPP_IN_PORT) {
1778 in_port = ctx->xin->flow.in_port.ofp_port;
1781 table_id = resubmit->table_id;
1782 if (table_id == 255) {
1783 table_id = ctx->table_id;
1786 xlate_table_action(ctx, in_port, table_id, false);
1790 flood_packets(struct xlate_ctx *ctx, bool all)
1792 const struct xport *xport;
1794 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
1795 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
1800 compose_output_action__(ctx, xport->ofp_port, false);
1801 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
1802 compose_output_action(ctx, xport->ofp_port);
1806 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1810 execute_controller_action(struct xlate_ctx *ctx, int len,
1811 enum ofp_packet_in_reason reason,
1812 uint16_t controller_id)
1814 struct ofputil_packet_in *pin;
1815 struct ofpbuf *packet;
1818 ctx->xout->slow |= SLOW_CONTROLLER;
1819 if (!ctx->xin->packet) {
1823 packet = ofpbuf_clone(ctx->xin->packet);
1825 key.skb_priority = 0;
1827 memset(&key.tunnel, 0, sizeof key.tunnel);
1829 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
1830 &ctx->xout->odp_actions,
1832 &ctx->mpls_depth_delta);
1834 odp_execute_actions(NULL, packet, &key, ctx->xout->odp_actions.data,
1835 ctx->xout->odp_actions.size, NULL, NULL);
1837 pin = xmalloc(sizeof *pin);
1838 pin->packet_len = packet->size;
1839 pin->packet = ofpbuf_steal_data(packet);
1840 pin->reason = reason;
1841 pin->controller_id = controller_id;
1842 pin->table_id = ctx->table_id;
1843 pin->cookie = ctx->rule ? rule_dpif_get_flow_cookie(ctx->rule) : 0;
1845 pin->send_len = len;
1846 flow_get_metadata(&ctx->xin->flow, &pin->fmd);
1848 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
1849 ofpbuf_delete(packet);
1853 compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1855 struct flow_wildcards *wc = &ctx->xout->wc;
1856 struct flow *flow = &ctx->xin->flow;
1858 ovs_assert(eth_type_mpls(eth_type));
1860 /* If mpls_depth_delta is negative then an MPLS POP action has been
1861 * composed and the resulting MPLS label stack is unknown. This means
1862 * an MPLS PUSH action can't be composed as it needs to know either the
1863 * top-most MPLS LSE to use as a template for the new MPLS LSE, or that
1864 * there is no MPLS label stack present. Thus, stop processing.
1866 * If mpls_depth_delta is positive then an MPLS PUSH action has been
1867 * composed and no further MPLS PUSH action may be performed without
1868 * losing MPLS LSE and ether type information held in xtx->xin->flow.
1869 * Thus, stop processing.
1871 * If the MPLS LSE of the flow and base_flow differ then the MPLS LSE
1872 * has been updated. Performing a MPLS PUSH action may be would result in
1873 * losing MPLS LSE and ether type information held in xtx->xin->flow.
1874 * Thus, stop processing.
1876 * It is planned that in the future this case will be handled
1877 * by recirculation */
1878 if (ctx->mpls_depth_delta ||
1879 ctx->xin->flow.mpls_lse != ctx->base_flow.mpls_lse) {
1883 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1885 ctx->pre_push_mpls_lse = ctx->xin->flow.mpls_lse;
1887 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
1888 flow->mpls_lse &= ~htonl(MPLS_BOS_MASK);
1893 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1894 label = htonl(0x2); /* IPV6 Explicit Null. */
1896 label = htonl(0x0); /* IPV4 Explicit Null. */
1898 wc->masks.nw_tos |= IP_DSCP_MASK;
1899 wc->masks.nw_ttl = 0xff;
1900 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
1901 ttl = flow->nw_ttl ? flow->nw_ttl : 0x40;
1902 flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
1904 flow->dl_type = eth_type;
1905 ctx->mpls_depth_delta++;
1911 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
1913 struct flow_wildcards *wc = &ctx->xout->wc;
1915 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
1919 /* If mpls_depth_delta is negative then an MPLS POP action has been
1920 * composed. Performing another MPLS POP action
1921 * would result in losing ether type that results from
1922 * the already composed MPLS POP. Thus, stop processing.
1924 * It is planned that in the future this case will be handled
1925 * by recirculation */
1926 if (ctx->mpls_depth_delta < 0) {
1930 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1932 /* If mpls_depth_delta is positive then an MPLS PUSH action has been
1933 * executed and the previous MPLS LSE saved in ctx->pre_push_mpls_lse. The
1934 * flow's MPLS LSE should be restored to that value to allow any
1935 * subsequent actions that update of the LSE to be executed correctly.
1937 if (ctx->mpls_depth_delta > 0) {
1938 ctx->xin->flow.mpls_lse = ctx->pre_push_mpls_lse;
1941 ctx->xin->flow.dl_type = eth_type;
1942 ctx->mpls_depth_delta--;
1948 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
1950 struct flow *flow = &ctx->xin->flow;
1952 if (!is_ip_any(flow)) {
1956 ctx->xout->wc.masks.nw_ttl = 0xff;
1957 if (flow->nw_ttl > 1) {
1963 for (i = 0; i < ids->n_controllers; i++) {
1964 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
1968 /* Stop processing for current table. */
1974 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
1976 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
1980 /* If mpls_depth_delta is negative then an MPLS POP action has been
1981 * executed and the resulting MPLS label stack is unknown. This means
1982 * a SET MPLS TTL push action can't be executed as it needs to manipulate
1983 * the top-most MPLS LSE. Thus, stop processing.
1985 * It is planned that in the future this case will be handled
1988 if (ctx->mpls_depth_delta < 0) {
1992 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_TTL_MASK);
1993 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl);
1998 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
2000 struct flow *flow = &ctx->xin->flow;
2001 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse);
2002 struct flow_wildcards *wc = &ctx->xout->wc;
2004 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2006 if (!eth_type_mpls(flow->dl_type)) {
2012 set_mpls_lse_ttl(&flow->mpls_lse, ttl);
2015 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
2017 /* Stop processing for current table. */
2023 xlate_output_action(struct xlate_ctx *ctx,
2024 ofp_port_t port, uint16_t max_len, bool may_packet_in)
2026 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
2028 ctx->xout->nf_output_iface = NF_OUT_DROP;
2032 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
2035 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2042 flood_packets(ctx, false);
2045 flood_packets(ctx, true);
2047 case OFPP_CONTROLLER:
2048 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
2054 if (port != ctx->xin->flow.in_port.ofp_port) {
2055 compose_output_action(ctx, port);
2057 xlate_report(ctx, "skipping output to input port");
2062 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2063 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2064 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2065 ctx->xout->nf_output_iface = prev_nf_output_iface;
2066 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2067 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2068 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2073 xlate_output_reg_action(struct xlate_ctx *ctx,
2074 const struct ofpact_output_reg *or)
2076 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
2077 if (port <= UINT16_MAX) {
2078 union mf_subvalue value;
2080 memset(&value, 0xff, sizeof value);
2081 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
2082 xlate_output_action(ctx, u16_to_ofp(port),
2083 or->max_len, false);
2088 xlate_enqueue_action(struct xlate_ctx *ctx,
2089 const struct ofpact_enqueue *enqueue)
2091 ofp_port_t ofp_port = enqueue->port;
2092 uint32_t queue_id = enqueue->queue;
2093 uint32_t flow_priority, priority;
2096 /* Translate queue to priority. */
2097 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
2099 /* Fall back to ordinary output action. */
2100 xlate_output_action(ctx, enqueue->port, 0, false);
2104 /* Check output port. */
2105 if (ofp_port == OFPP_IN_PORT) {
2106 ofp_port = ctx->xin->flow.in_port.ofp_port;
2107 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
2111 /* Add datapath actions. */
2112 flow_priority = ctx->xin->flow.skb_priority;
2113 ctx->xin->flow.skb_priority = priority;
2114 compose_output_action(ctx, ofp_port);
2115 ctx->xin->flow.skb_priority = flow_priority;
2117 /* Update NetFlow output port. */
2118 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2119 ctx->xout->nf_output_iface = ofp_port;
2120 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2121 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2126 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
2128 uint32_t skb_priority;
2130 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
2131 ctx->xin->flow.skb_priority = skb_priority;
2133 /* Couldn't translate queue to a priority. Nothing to do. A warning
2134 * has already been logged. */
2139 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
2141 const struct xbridge *xbridge = xbridge_;
2152 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
2155 port = get_ofp_port(xbridge, ofp_port);
2156 return port ? port->may_enable : false;
2161 xlate_bundle_action(struct xlate_ctx *ctx,
2162 const struct ofpact_bundle *bundle)
2166 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
2168 CONST_CAST(struct xbridge *, ctx->xbridge));
2169 if (bundle->dst.field) {
2170 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
2173 xlate_output_action(ctx, port, 0, false);
2178 xlate_learn_action(struct xlate_ctx *ctx,
2179 const struct ofpact_learn *learn)
2181 uint64_t ofpacts_stub[1024 / 8];
2182 struct ofputil_flow_mod fm;
2183 struct ofpbuf ofpacts;
2185 ctx->xout->has_learn = true;
2187 learn_mask(learn, &ctx->xout->wc);
2189 if (!ctx->xin->may_learn) {
2193 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2194 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
2195 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, &fm);
2196 ofpbuf_uninit(&ofpacts);
2200 xlate_fin_timeout(struct xlate_ctx *ctx,
2201 const struct ofpact_fin_timeout *oft)
2203 if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
2204 rule_dpif_reduce_timeouts(ctx->rule, oft->fin_idle_timeout,
2205 oft->fin_hard_timeout);
2210 xlate_sample_action(struct xlate_ctx *ctx,
2211 const struct ofpact_sample *os)
2213 union user_action_cookie cookie;
2214 /* Scale the probability from 16-bit to 32-bit while representing
2215 * the same percentage. */
2216 uint32_t probability = (os->probability << 16) | os->probability;
2218 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2219 &ctx->xout->odp_actions,
2221 &ctx->mpls_depth_delta);
2223 compose_flow_sample_cookie(os->probability, os->collector_set_id,
2224 os->obs_domain_id, os->obs_point_id, &cookie);
2225 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
2226 probability, &cookie, sizeof cookie.flow_sample);
2230 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
2232 if (xport->config & (eth_addr_equals(ctx->xin->flow.dl_dst, eth_addr_stp)
2233 ? OFPUTIL_PC_NO_RECV_STP
2234 : OFPUTIL_PC_NO_RECV)) {
2238 /* Only drop packets here if both forwarding and learning are
2239 * disabled. If just learning is enabled, we need to have
2240 * OFPP_NORMAL and the learning action have a look at the packet
2241 * before we can drop it. */
2242 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
2250 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
2251 struct xlate_ctx *ctx)
2253 struct flow_wildcards *wc = &ctx->xout->wc;
2254 struct flow *flow = &ctx->xin->flow;
2255 const struct ofpact *a;
2257 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2258 struct ofpact_controller *controller;
2259 const struct ofpact_metadata *metadata;
2267 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
2268 ofpact_get_OUTPUT(a)->max_len, true);
2272 /* XXX not yet implemented */
2275 case OFPACT_CONTROLLER:
2276 controller = ofpact_get_CONTROLLER(a);
2277 execute_controller_action(ctx, controller->max_len,
2279 controller->controller_id);
2282 case OFPACT_ENQUEUE:
2283 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
2286 case OFPACT_SET_VLAN_VID:
2287 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2288 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
2289 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
2293 case OFPACT_SET_VLAN_PCP:
2294 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
2295 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
2297 htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp << VLAN_PCP_SHIFT)
2301 case OFPACT_STRIP_VLAN:
2302 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2303 flow->vlan_tci = htons(0);
2306 case OFPACT_PUSH_VLAN:
2307 /* XXX 802.1AD(QinQ) */
2308 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2309 flow->vlan_tci = htons(VLAN_CFI);
2312 case OFPACT_SET_ETH_SRC:
2313 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2314 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2317 case OFPACT_SET_ETH_DST:
2318 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2319 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2322 case OFPACT_SET_IPV4_SRC:
2323 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2324 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2325 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2329 case OFPACT_SET_IPV4_DST:
2330 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2331 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2332 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
2336 case OFPACT_SET_IPV4_DSCP:
2337 wc->masks.nw_tos |= IP_DSCP_MASK;
2338 /* OpenFlow 1.0 only supports IPv4. */
2339 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2340 flow->nw_tos &= ~IP_DSCP_MASK;
2341 flow->nw_tos |= ofpact_get_SET_IPV4_DSCP(a)->dscp;
2345 case OFPACT_SET_L4_SRC_PORT:
2346 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2347 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2348 if (is_ip_any(flow)) {
2349 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2353 case OFPACT_SET_L4_DST_PORT:
2354 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2355 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2356 if (is_ip_any(flow)) {
2357 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2361 case OFPACT_RESUBMIT:
2362 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
2365 case OFPACT_SET_TUNNEL:
2366 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2369 case OFPACT_SET_QUEUE:
2370 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2373 case OFPACT_POP_QUEUE:
2374 flow->skb_priority = ctx->orig_skb_priority;
2377 case OFPACT_REG_MOVE:
2378 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
2381 case OFPACT_REG_LOAD:
2382 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow);
2385 case OFPACT_STACK_PUSH:
2386 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
2390 case OFPACT_STACK_POP:
2391 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
2395 case OFPACT_PUSH_MPLS:
2396 if (compose_mpls_push_action(ctx,
2397 ofpact_get_PUSH_MPLS(a)->ethertype)) {
2402 case OFPACT_POP_MPLS:
2403 if (compose_mpls_pop_action(ctx,
2404 ofpact_get_POP_MPLS(a)->ethertype)) {
2409 case OFPACT_SET_MPLS_TTL:
2410 if (compose_set_mpls_ttl_action(ctx,
2411 ofpact_get_SET_MPLS_TTL(a)->ttl)) {
2416 case OFPACT_DEC_MPLS_TTL:
2417 if (compose_dec_mpls_ttl_action(ctx)) {
2422 case OFPACT_DEC_TTL:
2423 wc->masks.nw_ttl = 0xff;
2424 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
2430 /* Nothing to do. */
2433 case OFPACT_MULTIPATH:
2434 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
2438 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
2441 case OFPACT_OUTPUT_REG:
2442 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
2446 xlate_learn_action(ctx, ofpact_get_LEARN(a));
2453 case OFPACT_FIN_TIMEOUT:
2454 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2455 ctx->xout->has_fin_timeout = true;
2456 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
2459 case OFPACT_CLEAR_ACTIONS:
2461 * Nothing to do because writa-actions is not supported for now.
2462 * When writa-actions is supported, clear-actions also must
2463 * be supported at the same time.
2467 case OFPACT_WRITE_METADATA:
2468 metadata = ofpact_get_WRITE_METADATA(a);
2469 flow->metadata &= ~metadata->mask;
2470 flow->metadata |= metadata->metadata & metadata->mask;
2474 /* Not implemented yet. */
2477 case OFPACT_GOTO_TABLE: {
2478 /* It is assumed that goto-table is the last action. */
2479 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
2481 ovs_assert(ctx->table_id < ogt->table_id);
2482 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2483 ogt->table_id, true);
2488 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
2495 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
2496 const struct flow *flow, struct rule_dpif *rule,
2497 uint8_t tcp_flags, const struct ofpbuf *packet)
2499 xin->ofproto = ofproto;
2501 xin->packet = packet;
2502 xin->may_learn = packet != NULL;
2504 xin->ofpacts = NULL;
2505 xin->ofpacts_len = 0;
2506 xin->tcp_flags = tcp_flags;
2507 xin->resubmit_hook = NULL;
2508 xin->report_hook = NULL;
2509 xin->resubmit_stats = NULL;
2513 xlate_out_uninit(struct xlate_out *xout)
2516 ofpbuf_uninit(&xout->odp_actions);
2520 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
2521 * into datapath actions, using 'ctx', and discards the datapath actions. */
2523 xlate_actions_for_side_effects(struct xlate_in *xin)
2525 struct xlate_out xout;
2527 xlate_actions(xin, &xout);
2528 xlate_out_uninit(&xout);
2532 xlate_report(struct xlate_ctx *ctx, const char *s)
2534 if (ctx->xin->report_hook) {
2535 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
2540 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
2543 dst->slow = src->slow;
2544 dst->has_learn = src->has_learn;
2545 dst->has_normal = src->has_normal;
2546 dst->has_fin_timeout = src->has_fin_timeout;
2547 dst->nf_output_iface = src->nf_output_iface;
2548 dst->mirrors = src->mirrors;
2550 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
2551 sizeof dst->odp_actions_stub);
2552 ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
2553 src->odp_actions.size);
2556 /* Returns a reference to the sflow handled associated with ofproto, or NULL if
2557 * there is none. The caller is responsible for decrementing the results ref
2558 * count with dpif_sflow_unref(). */
2560 xlate_get_sflow(const struct ofproto_dpif *ofproto)
2562 struct dpif_sflow *sflow = NULL;
2563 struct xbridge *xbridge;
2565 ovs_rwlock_rdlock(&xlate_rwlock);
2566 xbridge = xbridge_lookup(ofproto);
2568 sflow = dpif_sflow_ref(xbridge->sflow);
2570 ovs_rwlock_unlock(&xlate_rwlock);
2575 /* Returns a reference to the ipfix handled associated with ofproto, or NULL if
2576 * there is none. The caller is responsible for decrementing the results ref
2577 * count with dpif_ipfix_unref(). */
2579 xlate_get_ipfix(const struct ofproto_dpif *ofproto)
2581 struct dpif_ipfix *ipfix = NULL;
2582 struct xbridge *xbridge;
2584 ovs_rwlock_rdlock(&xlate_rwlock);
2585 xbridge = xbridge_lookup(ofproto);
2587 ipfix = dpif_ipfix_ref(xbridge->ipfix);
2589 ovs_rwlock_unlock(&xlate_rwlock);
2594 static struct skb_priority_to_dscp *
2595 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
2597 struct skb_priority_to_dscp *pdscp;
2600 hash = hash_int(skb_priority, 0);
2601 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
2602 if (pdscp->skb_priority == skb_priority) {
2610 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
2613 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
2614 *dscp = pdscp ? pdscp->dscp : 0;
2615 return pdscp != NULL;
2619 clear_skb_priorities(struct xport *xport)
2621 struct skb_priority_to_dscp *pdscp, *next;
2623 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
2624 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
2630 actions_output_to_local_port(const struct xlate_ctx *ctx)
2632 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
2633 const struct nlattr *a;
2636 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions.data,
2637 ctx->xout->odp_actions.size) {
2638 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
2639 && nl_attr_get_odp_port(a) == local_odp_port) {
2646 /* Thread safe call to xlate_actions__(). */
2648 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
2650 ovs_rwlock_rdlock(&xlate_rwlock);
2651 xlate_actions__(xin, xout);
2652 ovs_rwlock_unlock(&xlate_rwlock);
2655 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
2656 * into datapath actions in 'odp_actions', using 'ctx'.
2658 * The caller must take responsibility for eventually freeing 'xout', with
2659 * xlate_out_uninit(). */
2661 xlate_actions__(struct xlate_in *xin, struct xlate_out *xout)
2662 OVS_REQ_RDLOCK(xlate_rwlock)
2664 struct flow_wildcards *wc = &xout->wc;
2665 struct flow *flow = &xin->flow;
2666 struct rule_dpif *rule = NULL;
2668 struct rule_actions *actions = NULL;
2669 enum slow_path_reason special;
2670 const struct ofpact *ofpacts;
2671 struct xport *in_port;
2672 struct flow orig_flow;
2673 struct xlate_ctx ctx;
2677 COVERAGE_INC(xlate_actions);
2679 /* Flow initialization rules:
2680 * - 'base_flow' must match the kernel's view of the packet at the
2681 * time that action processing starts. 'flow' represents any
2682 * transformations we wish to make through actions.
2683 * - By default 'base_flow' and 'flow' are the same since the input
2684 * packet matches the output before any actions are applied.
2685 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
2686 * of the received packet as seen by the kernel. If we later output
2687 * to another device without any modifications this will cause us to
2688 * insert a new tag since the original one was stripped off by the
2690 * - Tunnel metadata as received is retained in 'flow'. This allows
2691 * tunnel metadata matching also in later tables.
2692 * Since a kernel action for setting the tunnel metadata will only be
2693 * generated with actual tunnel output, changing the tunnel metadata
2694 * values in 'flow' (such as tun_id) will only have effect with a later
2695 * tunnel output action.
2696 * - Tunnel 'base_flow' is completely cleared since that is what the
2697 * kernel does. If we wish to maintain the original values an action
2698 * needs to be generated. */
2703 ctx.xout->has_learn = false;
2704 ctx.xout->has_normal = false;
2705 ctx.xout->has_fin_timeout = false;
2706 ctx.xout->nf_output_iface = NF_OUT_DROP;
2707 ctx.xout->mirrors = 0;
2708 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
2709 sizeof ctx.xout->odp_actions_stub);
2710 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
2712 ctx.xbridge = xbridge_lookup(xin->ofproto);
2717 ctx.rule = xin->rule;
2719 ctx.base_flow = *flow;
2720 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
2721 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
2723 flow_wildcards_init_catchall(wc);
2724 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
2725 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
2726 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
2727 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
2729 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
2730 if (ctx.xbridge->has_netflow) {
2731 netflow_mask_wc(flow, wc);
2736 ctx.orig_skb_priority = flow->skb_priority;
2739 ctx.mpls_depth_delta = 0;
2741 if (!xin->ofpacts && !ctx.rule) {
2742 rule_dpif_lookup(ctx.xbridge->ofproto, flow, wc, &rule);
2743 if (ctx.xin->resubmit_stats) {
2744 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
2748 xout->fail_open = ctx.rule && rule_dpif_fail_open(ctx.rule);
2751 ofpacts = xin->ofpacts;
2752 ofpacts_len = xin->ofpacts_len;
2753 } else if (ctx.rule) {
2754 actions = rule_dpif_get_actions(ctx.rule);
2755 ofpacts = actions->ofpacts;
2756 ofpacts_len = actions->ofpacts_len;
2761 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
2763 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
2764 /* Do this conditionally because the copy is expensive enough that it
2765 * shows up in profiles. */
2769 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
2770 switch (ctx.xbridge->frag) {
2771 case OFPC_FRAG_NORMAL:
2772 /* We must pretend that transport ports are unavailable. */
2773 flow->tp_src = ctx.base_flow.tp_src = htons(0);
2774 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
2777 case OFPC_FRAG_DROP:
2780 case OFPC_FRAG_REASM:
2783 case OFPC_FRAG_NX_MATCH:
2784 /* Nothing to do. */
2787 case OFPC_INVALID_TTL_TO_CONTROLLER:
2792 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
2793 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
2795 ctx.xout->slow |= special;
2797 size_t sample_actions_len;
2799 if (flow->in_port.ofp_port
2800 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
2801 flow->in_port.ofp_port,
2803 ctx.base_flow.vlan_tci = 0;
2806 add_sflow_action(&ctx);
2807 add_ipfix_action(&ctx);
2808 sample_actions_len = ctx.xout->odp_actions.size;
2810 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
2811 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
2813 /* We've let OFPP_NORMAL and the learning action look at the
2814 * packet, so drop it now if forwarding is disabled. */
2815 if (in_port && !xport_stp_forward_state(in_port)) {
2816 ctx.xout->odp_actions.size = sample_actions_len;
2820 if (ctx.xbridge->has_in_band
2821 && in_band_must_output_to_local_port(flow)
2822 && !actions_output_to_local_port(&ctx)) {
2823 compose_output_action(&ctx, OFPP_LOCAL);
2826 fix_sflow_action(&ctx);
2828 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
2829 add_mirror_actions(&ctx, &orig_flow);
2833 if (nl_attr_oversized(ctx.xout->odp_actions.size)) {
2834 /* These datapath actions are too big for a Netlink attribute, so we
2835 * can't execute them. */
2836 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2838 VLOG_ERR_RL(&rl, "discarding oversize datapath actions");
2839 ofpbuf_clear(&ctx.xout->odp_actions);
2842 ofpbuf_uninit(&ctx.stack);
2844 /* Clear the metadata and register wildcard masks, because we won't
2845 * use non-header fields as part of the cache. */
2846 memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
2847 memset(&wc->masks.regs, 0, sizeof wc->masks.regs);
2850 rule_actions_unref(actions);
2851 rule_dpif_unref(rule);
2854 /* Sends 'packet' out 'ofport'.
2855 * May modify 'packet'.
2856 * Returns 0 if successful, otherwise a positive errno value. */
2858 xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
2860 uint64_t odp_actions_stub[1024 / 8];
2861 struct xport *xport;
2862 struct ofpbuf key, odp_actions;
2863 struct dpif_flow_stats stats;
2864 struct odputil_keybuf keybuf;
2865 struct ofpact_output output;
2866 struct xlate_out xout;
2867 struct xlate_in xin;
2869 union flow_in_port in_port_;
2872 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
2873 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2874 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
2875 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
2876 in_port_.ofp_port = OFPP_NONE;
2877 flow_extract(packet, 0, 0, NULL, &in_port_, &flow);
2879 ovs_rwlock_rdlock(&xlate_rwlock);
2880 xport = xport_lookup(ofport);
2882 ovs_rwlock_unlock(&xlate_rwlock);
2886 odp_flow_key_from_flow(&key, &flow, ofp_port_to_odp_port(xport->xbridge, OFPP_LOCAL));
2887 dpif_flow_stats_extract(&flow, packet, time_msec(), &stats);
2888 output.port = xport->ofp_port;
2890 xlate_in_init(&xin, xport->xbridge->ofproto, &flow, NULL, 0, packet);
2891 xin.ofpacts_len = sizeof output;
2892 xin.ofpacts = &output.ofpact;
2893 xin.resubmit_stats = &stats;
2894 /* Calls xlate_actions__ directly, since the rdlock is acquired. */
2895 xlate_actions__(&xin, &xout);
2896 error = dpif_execute(xport->xbridge->dpif,
2898 xout.odp_actions.data, xout.odp_actions.size,
2899 packet, (xout.slow & SLOW_ACTION) != 0);
2900 ovs_rwlock_unlock(&xlate_rwlock);
2901 xlate_out_uninit(&xout);