2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "ofproto/ofproto-provider.h"
26 #include "byte-order.h"
31 #include "dynamic-string.h"
32 #include "fail-open.h"
36 #include "mac-learning.h"
37 #include "multipath.h"
44 #include "ofp-print.h"
45 #include "ofproto-dpif-sflow.h"
46 #include "poll-loop.h"
48 #include "unaligned.h"
50 #include "vlan-bitmap.h"
53 VLOG_DEFINE_THIS_MODULE(ofproto_dpif
);
55 COVERAGE_DEFINE(ofproto_dpif_ctlr_action
);
56 COVERAGE_DEFINE(ofproto_dpif_expired
);
57 COVERAGE_DEFINE(ofproto_dpif_no_packet_in
);
58 COVERAGE_DEFINE(ofproto_dpif_xlate
);
59 COVERAGE_DEFINE(facet_changed_rule
);
60 COVERAGE_DEFINE(facet_invalidated
);
61 COVERAGE_DEFINE(facet_revalidate
);
62 COVERAGE_DEFINE(facet_unexpected
);
64 /* Maximum depth of flow table recursion (due to resubmit actions) in a
65 * flow translation. */
66 #define MAX_RESUBMIT_RECURSION 16
68 /* Number of implemented OpenFlow tables. */
69 enum { N_TABLES
= 255 };
70 BUILD_ASSERT_DECL(N_TABLES
>= 1 && N_TABLES
<= 255);
78 long long int used
; /* Time last used; time created if not used. */
82 * - Do include packets and bytes from facets that have been deleted or
83 * whose own statistics have been folded into the rule.
85 * - Do include packets and bytes sent "by hand" that were accounted to
86 * the rule without any facet being involved (this is a rare corner
87 * case in rule_execute()).
89 * - Do not include packet or bytes that can be obtained from any facet's
90 * packet_count or byte_count member or that can be obtained from the
91 * datapath by, e.g., dpif_flow_get() for any facet.
93 uint64_t packet_count
; /* Number of packets received. */
94 uint64_t byte_count
; /* Number of bytes received. */
96 tag_type tag
; /* Caches rule_calculate_tag() result. */
98 struct list facets
; /* List of "struct facet"s. */
101 static struct rule_dpif
*rule_dpif_cast(const struct rule
*rule
)
103 return rule
? CONTAINER_OF(rule
, struct rule_dpif
, up
) : NULL
;
106 static struct rule_dpif
*rule_dpif_lookup(struct ofproto_dpif
*,
107 const struct flow
*, uint8_t table
);
109 #define MAX_MIRRORS 32
110 typedef uint32_t mirror_mask_t
;
111 #define MIRROR_MASK_C(X) UINT32_C(X)
112 BUILD_ASSERT_DECL(sizeof(mirror_mask_t
) * CHAR_BIT
>= MAX_MIRRORS
);
114 struct ofproto_dpif
*ofproto
; /* Owning ofproto. */
115 size_t idx
; /* In ofproto's "mirrors" array. */
116 void *aux
; /* Key supplied by ofproto's client. */
117 char *name
; /* Identifier for log messages. */
119 /* Selection criteria. */
120 struct hmapx srcs
; /* Contains "struct ofbundle *"s. */
121 struct hmapx dsts
; /* Contains "struct ofbundle *"s. */
122 unsigned long *vlans
; /* Bitmap of chosen VLANs, NULL selects all. */
124 /* Output (mutually exclusive). */
125 struct ofbundle
*out
; /* Output port or NULL. */
126 int out_vlan
; /* Output VLAN or -1. */
129 static void mirror_destroy(struct ofmirror
*);
131 /* A group of one or more OpenFlow ports. */
132 #define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
134 struct ofproto_dpif
*ofproto
; /* Owning ofproto. */
135 struct hmap_node hmap_node
; /* In struct ofproto's "bundles" hmap. */
136 void *aux
; /* Key supplied by ofproto's client. */
137 char *name
; /* Identifier for log messages. */
140 struct list ports
; /* Contains "struct ofport"s. */
141 int vlan
; /* -1=trunk port, else a 12-bit VLAN ID. */
142 unsigned long *trunks
; /* Bitmap of trunked VLANs, if 'vlan' == -1.
143 * NULL if all VLANs are trunked. */
144 struct lacp
*lacp
; /* LACP if LACP is enabled, otherwise NULL. */
145 struct bond
*bond
; /* Nonnull iff more than one port. */
148 bool floodable
; /* True if no port has OFPPC_NO_FLOOD set. */
150 /* Port mirroring info. */
151 mirror_mask_t src_mirrors
; /* Mirrors triggered when packet received. */
152 mirror_mask_t dst_mirrors
; /* Mirrors triggered when packet sent. */
153 mirror_mask_t mirror_out
; /* Mirrors that output to this bundle. */
156 static void bundle_remove(struct ofport
*);
157 static void bundle_destroy(struct ofbundle
*);
158 static void bundle_del_port(struct ofport_dpif
*);
159 static void bundle_run(struct ofbundle
*);
160 static void bundle_wait(struct ofbundle
*);
162 struct action_xlate_ctx
{
163 /* action_xlate_ctx_init() initializes these members. */
166 struct ofproto_dpif
*ofproto
;
168 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
169 * this flow when actions change header fields. */
172 /* The packet corresponding to 'flow', or a null pointer if we are
173 * revalidating without a packet to refer to. */
174 const struct ofpbuf
*packet
;
176 /* Should OFPP_NORMAL MAC learning and NXAST_LEARN actions execute? We
177 * want to execute them if we are actually processing a packet, or if we
178 * are accounting for packets that the datapath has processed, but not if
179 * we are just revalidating. */
182 /* If nonnull, called just before executing a resubmit action.
184 * This is normally null so the client has to set it manually after
185 * calling action_xlate_ctx_init(). */
186 void (*resubmit_hook
)(struct action_xlate_ctx
*, struct rule_dpif
*);
188 /* xlate_actions() initializes and uses these members. The client might want
189 * to look at them after it returns. */
191 struct ofpbuf
*odp_actions
; /* Datapath actions. */
192 tag_type tags
; /* Tags associated with actions. */
193 bool may_set_up_flow
; /* True ordinarily; false if the actions must
194 * be reassessed for every packet. */
195 bool has_learn
; /* Actions include NXAST_LEARN? */
196 bool has_normal
; /* Actions output to OFPP_NORMAL? */
197 uint16_t nf_output_iface
; /* Output interface index for NetFlow. */
199 /* xlate_actions() initializes and uses these members, but the client has no
200 * reason to look at them. */
202 int recurse
; /* Recursion level, via xlate_table_action. */
203 uint32_t priority
; /* Current flow priority. 0 if none. */
204 struct flow base_flow
; /* Flow at the last commit. */
205 uint32_t base_priority
; /* Priority at the last commit. */
206 uint8_t table_id
; /* OpenFlow table ID where flow was found. */
209 static void action_xlate_ctx_init(struct action_xlate_ctx
*,
210 struct ofproto_dpif
*, const struct flow
*,
211 const struct ofpbuf
*);
212 static struct ofpbuf
*xlate_actions(struct action_xlate_ctx
*,
213 const union ofp_action
*in
, size_t n_in
);
215 /* An exact-match instantiation of an OpenFlow flow. */
217 long long int used
; /* Time last used; time created if not used. */
221 * - Do include packets and bytes sent "by hand", e.g. with
224 * - Do include packets and bytes that were obtained from the datapath
225 * when its statistics were reset (e.g. dpif_flow_put() with
226 * DPIF_FP_ZERO_STATS).
228 uint64_t packet_count
; /* Number of packets received. */
229 uint64_t byte_count
; /* Number of bytes received. */
231 uint64_t dp_packet_count
; /* Last known packet count in the datapath. */
232 uint64_t dp_byte_count
; /* Last known byte count in the datapath. */
234 uint64_t rs_packet_count
; /* Packets pushed to resubmit children. */
235 uint64_t rs_byte_count
; /* Bytes pushed to resubmit children. */
236 long long int rs_used
; /* Used time pushed to resubmit children. */
238 uint64_t accounted_bytes
; /* Bytes processed by facet_account(). */
240 struct hmap_node hmap_node
; /* In owning ofproto's 'facets' hmap. */
241 struct list list_node
; /* In owning rule's 'facets' list. */
242 struct rule_dpif
*rule
; /* Owning rule. */
243 struct flow flow
; /* Exact-match flow. */
244 bool installed
; /* Installed in datapath? */
245 bool may_install
; /* True ordinarily; false if actions must
246 * be reassessed for every packet. */
247 bool has_learn
; /* Actions include NXAST_LEARN? */
248 bool has_normal
; /* Actions output to OFPP_NORMAL? */
249 size_t actions_len
; /* Number of bytes in actions[]. */
250 struct nlattr
*actions
; /* Datapath actions. */
251 tag_type tags
; /* Tags. */
252 struct netflow_flow nf_flow
; /* Per-flow NetFlow tracking data. */
255 static struct facet
*facet_create(struct rule_dpif
*, const struct flow
*,
256 const struct ofpbuf
*packet
);
257 static void facet_remove(struct ofproto_dpif
*, struct facet
*);
258 static void facet_free(struct facet
*);
260 static struct facet
*facet_find(struct ofproto_dpif
*, const struct flow
*);
261 static struct facet
*facet_lookup_valid(struct ofproto_dpif
*,
262 const struct flow
*);
263 static bool facet_revalidate(struct ofproto_dpif
*, struct facet
*);
265 static void facet_execute(struct ofproto_dpif
*, struct facet
*,
266 struct ofpbuf
*packet
);
268 static int facet_put__(struct ofproto_dpif
*, struct facet
*,
269 const struct nlattr
*actions
, size_t actions_len
,
270 struct dpif_flow_stats
*);
271 static void facet_install(struct ofproto_dpif
*, struct facet
*,
273 static void facet_uninstall(struct ofproto_dpif
*, struct facet
*);
274 static void facet_flush_stats(struct ofproto_dpif
*, struct facet
*);
276 static void facet_make_actions(struct ofproto_dpif
*, struct facet
*,
277 const struct ofpbuf
*packet
);
278 static void facet_update_time(struct ofproto_dpif
*, struct facet
*,
280 static void facet_update_stats(struct ofproto_dpif
*, struct facet
*,
281 const struct dpif_flow_stats
*);
282 static void facet_reset_counters(struct facet
*);
283 static void facet_reset_dp_stats(struct facet
*, struct dpif_flow_stats
*);
284 static void facet_push_stats(struct facet
*);
285 static void facet_account(struct ofproto_dpif
*, struct facet
*);
287 static bool facet_is_controller_flow(struct facet
*);
289 static void flow_push_stats(const struct rule_dpif
*,
290 struct flow
*, uint64_t packets
, uint64_t bytes
,
293 static uint32_t rule_calculate_tag(const struct flow
*,
294 const struct flow_wildcards
*,
296 static void rule_invalidate(const struct rule_dpif
*);
302 struct ofbundle
*bundle
; /* Bundle that contains this port, if any. */
303 struct list bundle_node
; /* In struct ofbundle's "ports" list. */
304 struct cfm
*cfm
; /* Connectivity Fault Management, if any. */
305 tag_type tag
; /* Tag associated with this port. */
306 uint32_t bond_stable_id
; /* stable_id to use as bond slave, or 0. */
307 bool may_enable
; /* May be enabled in bonds. */
310 static struct ofport_dpif
*
311 ofport_dpif_cast(const struct ofport
*ofport
)
313 assert(ofport
->ofproto
->ofproto_class
== &ofproto_dpif_class
);
314 return ofport
? CONTAINER_OF(ofport
, struct ofport_dpif
, up
) : NULL
;
317 static void port_run(struct ofport_dpif
*);
318 static void port_wait(struct ofport_dpif
*);
319 static int set_cfm(struct ofport
*, const struct cfm_settings
*);
321 struct dpif_completion
{
322 struct list list_node
;
323 struct ofoperation
*op
;
326 /* Extra information about a classifier table.
327 * Currently used just for optimized flow revalidation. */
329 /* If either of these is nonnull, then this table has a form that allows
330 * flows to be tagged to avoid revalidating most flows for the most common
331 * kinds of flow table changes. */
332 struct cls_table
*catchall_table
; /* Table that wildcards all fields. */
333 struct cls_table
*other_table
; /* Table with any other wildcard set. */
334 uint32_t basis
; /* Keeps each table's tags separate. */
337 struct ofproto_dpif
{
346 struct netflow
*netflow
;
347 struct dpif_sflow
*sflow
;
348 struct hmap bundles
; /* Contains "struct ofbundle"s. */
349 struct mac_learning
*ml
;
350 struct ofmirror
*mirrors
[MAX_MIRRORS
];
351 bool has_bonded_bundles
;
354 struct timer next_expiration
;
360 struct table_dpif tables
[N_TABLES
];
361 bool need_revalidate
;
362 struct tag_set revalidate_set
;
364 /* Support for debugging async flow mods. */
365 struct list completions
;
367 bool has_bundle_action
; /* True when the first bundle action appears. */
370 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
371 * for debugging the asynchronous flow_mod implementation.) */
374 static void ofproto_dpif_unixctl_init(void);
376 static struct ofproto_dpif
*
377 ofproto_dpif_cast(const struct ofproto
*ofproto
)
379 assert(ofproto
->ofproto_class
== &ofproto_dpif_class
);
380 return CONTAINER_OF(ofproto
, struct ofproto_dpif
, up
);
383 static struct ofport_dpif
*get_ofp_port(struct ofproto_dpif
*,
385 static struct ofport_dpif
*get_odp_port(struct ofproto_dpif
*,
388 /* Packet processing. */
389 static void update_learning_table(struct ofproto_dpif
*,
390 const struct flow
*, int vlan
,
392 static bool is_admissible(struct ofproto_dpif
*, const struct flow
*,
393 bool have_packet
, tag_type
*, int *vlanp
,
394 struct ofbundle
**in_bundlep
);
395 static void handle_upcall(struct ofproto_dpif
*, struct dpif_upcall
*);
397 /* Flow expiration. */
398 static int expire(struct ofproto_dpif
*);
401 static int send_packet(struct ofproto_dpif
*, uint32_t odp_port
,
402 const struct ofpbuf
*packet
);
404 /* Global variables. */
405 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
407 /* Factory functions. */
410 enumerate_types(struct sset
*types
)
412 dp_enumerate_types(types
);
416 enumerate_names(const char *type
, struct sset
*names
)
418 return dp_enumerate_names(type
, names
);
422 del(const char *type
, const char *name
)
427 error
= dpif_open(name
, type
, &dpif
);
429 error
= dpif_delete(dpif
);
435 /* Basic life-cycle. */
437 static struct ofproto
*
440 struct ofproto_dpif
*ofproto
= xmalloc(sizeof *ofproto
);
445 dealloc(struct ofproto
*ofproto_
)
447 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
452 construct(struct ofproto
*ofproto_
, int *n_tablesp
)
454 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
455 const char *name
= ofproto
->up
.name
;
459 error
= dpif_create_and_open(name
, ofproto
->up
.type
, &ofproto
->dpif
);
461 VLOG_ERR("failed to open datapath %s: %s", name
, strerror(error
));
465 ofproto
->max_ports
= dpif_get_max_ports(ofproto
->dpif
);
466 ofproto
->n_matches
= 0;
468 dpif_flow_flush(ofproto
->dpif
);
469 dpif_recv_purge(ofproto
->dpif
);
471 error
= dpif_recv_set_mask(ofproto
->dpif
,
472 ((1u << DPIF_UC_MISS
) |
473 (1u << DPIF_UC_ACTION
) |
474 (1u << DPIF_UC_SAMPLE
)));
476 VLOG_ERR("failed to listen on datapath %s: %s", name
, strerror(error
));
477 dpif_close(ofproto
->dpif
);
481 ofproto
->netflow
= NULL
;
482 ofproto
->sflow
= NULL
;
483 hmap_init(&ofproto
->bundles
);
484 ofproto
->ml
= mac_learning_create();
485 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
486 ofproto
->mirrors
[i
] = NULL
;
488 ofproto
->has_bonded_bundles
= false;
490 timer_set_duration(&ofproto
->next_expiration
, 1000);
492 hmap_init(&ofproto
->facets
);
494 for (i
= 0; i
< N_TABLES
; i
++) {
495 struct table_dpif
*table
= &ofproto
->tables
[i
];
497 table
->catchall_table
= NULL
;
498 table
->other_table
= NULL
;
499 table
->basis
= random_uint32();
501 ofproto
->need_revalidate
= false;
502 tag_set_init(&ofproto
->revalidate_set
);
504 list_init(&ofproto
->completions
);
506 ofproto_dpif_unixctl_init();
508 ofproto
->has_bundle_action
= false;
510 *n_tablesp
= N_TABLES
;
515 complete_operations(struct ofproto_dpif
*ofproto
)
517 struct dpif_completion
*c
, *next
;
519 LIST_FOR_EACH_SAFE (c
, next
, list_node
, &ofproto
->completions
) {
520 ofoperation_complete(c
->op
, 0);
521 list_remove(&c
->list_node
);
527 destruct(struct ofproto
*ofproto_
)
529 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
530 struct rule_dpif
*rule
, *next_rule
;
531 struct classifier
*table
;
534 complete_operations(ofproto
);
536 OFPROTO_FOR_EACH_TABLE (table
, &ofproto
->up
) {
537 struct cls_cursor cursor
;
539 cls_cursor_init(&cursor
, table
, NULL
);
540 CLS_CURSOR_FOR_EACH_SAFE (rule
, next_rule
, up
.cr
, &cursor
) {
541 ofproto_rule_destroy(&rule
->up
);
545 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
546 mirror_destroy(ofproto
->mirrors
[i
]);
549 netflow_destroy(ofproto
->netflow
);
550 dpif_sflow_destroy(ofproto
->sflow
);
551 hmap_destroy(&ofproto
->bundles
);
552 mac_learning_destroy(ofproto
->ml
);
554 hmap_destroy(&ofproto
->facets
);
556 dpif_close(ofproto
->dpif
);
560 run(struct ofproto
*ofproto_
)
562 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
563 struct ofport_dpif
*ofport
;
564 struct ofbundle
*bundle
;
568 complete_operations(ofproto
);
570 dpif_run(ofproto
->dpif
);
572 for (i
= 0; i
< 50; i
++) {
573 struct dpif_upcall packet
;
576 error
= dpif_recv(ofproto
->dpif
, &packet
);
578 if (error
== ENODEV
) {
579 /* Datapath destroyed. */
585 handle_upcall(ofproto
, &packet
);
588 if (timer_expired(&ofproto
->next_expiration
)) {
589 int delay
= expire(ofproto
);
590 timer_set_duration(&ofproto
->next_expiration
, delay
);
593 if (ofproto
->netflow
) {
594 netflow_run(ofproto
->netflow
);
596 if (ofproto
->sflow
) {
597 dpif_sflow_run(ofproto
->sflow
);
600 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
603 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
607 mac_learning_run(ofproto
->ml
, &ofproto
->revalidate_set
);
609 /* Now revalidate if there's anything to do. */
610 if (ofproto
->need_revalidate
611 || !tag_set_is_empty(&ofproto
->revalidate_set
)) {
612 struct tag_set revalidate_set
= ofproto
->revalidate_set
;
613 bool revalidate_all
= ofproto
->need_revalidate
;
614 struct facet
*facet
, *next
;
616 /* Clear the revalidation flags. */
617 tag_set_init(&ofproto
->revalidate_set
);
618 ofproto
->need_revalidate
= false;
620 HMAP_FOR_EACH_SAFE (facet
, next
, hmap_node
, &ofproto
->facets
) {
622 || tag_set_intersects(&revalidate_set
, facet
->tags
)) {
623 facet_revalidate(ofproto
, facet
);
632 wait(struct ofproto
*ofproto_
)
634 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
635 struct ofport_dpif
*ofport
;
636 struct ofbundle
*bundle
;
638 if (!clogged
&& !list_is_empty(&ofproto
->completions
)) {
639 poll_immediate_wake();
642 dpif_wait(ofproto
->dpif
);
643 dpif_recv_wait(ofproto
->dpif
);
644 if (ofproto
->sflow
) {
645 dpif_sflow_wait(ofproto
->sflow
);
647 if (!tag_set_is_empty(&ofproto
->revalidate_set
)) {
648 poll_immediate_wake();
650 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
653 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
656 mac_learning_wait(ofproto
->ml
);
657 if (ofproto
->need_revalidate
) {
658 /* Shouldn't happen, but if it does just go around again. */
659 VLOG_DBG_RL(&rl
, "need revalidate in ofproto_wait_cb()");
660 poll_immediate_wake();
662 timer_wait(&ofproto
->next_expiration
);
667 flush(struct ofproto
*ofproto_
)
669 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
670 struct facet
*facet
, *next_facet
;
672 HMAP_FOR_EACH_SAFE (facet
, next_facet
, hmap_node
, &ofproto
->facets
) {
673 /* Mark the facet as not installed so that facet_remove() doesn't
674 * bother trying to uninstall it. There is no point in uninstalling it
675 * individually since we are about to blow away all the facets with
676 * dpif_flow_flush(). */
677 facet
->installed
= false;
678 facet
->dp_packet_count
= 0;
679 facet
->dp_byte_count
= 0;
680 facet_remove(ofproto
, facet
);
682 dpif_flow_flush(ofproto
->dpif
);
686 get_features(struct ofproto
*ofproto_ OVS_UNUSED
,
687 bool *arp_match_ip
, uint32_t *actions
)
689 *arp_match_ip
= true;
690 *actions
= ((1u << OFPAT_OUTPUT
) |
691 (1u << OFPAT_SET_VLAN_VID
) |
692 (1u << OFPAT_SET_VLAN_PCP
) |
693 (1u << OFPAT_STRIP_VLAN
) |
694 (1u << OFPAT_SET_DL_SRC
) |
695 (1u << OFPAT_SET_DL_DST
) |
696 (1u << OFPAT_SET_NW_SRC
) |
697 (1u << OFPAT_SET_NW_DST
) |
698 (1u << OFPAT_SET_NW_TOS
) |
699 (1u << OFPAT_SET_TP_SRC
) |
700 (1u << OFPAT_SET_TP_DST
) |
701 (1u << OFPAT_ENQUEUE
));
705 get_tables(struct ofproto
*ofproto_
, struct ofp_table_stats
*ots
)
707 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
708 struct ovs_dp_stats s
;
710 strcpy(ots
->name
, "classifier");
712 dpif_get_dp_stats(ofproto
->dpif
, &s
);
713 put_32aligned_be64(&ots
->lookup_count
, htonll(s
.n_hit
+ s
.n_missed
));
714 put_32aligned_be64(&ots
->matched_count
,
715 htonll(s
.n_hit
+ ofproto
->n_matches
));
719 set_netflow(struct ofproto
*ofproto_
,
720 const struct netflow_options
*netflow_options
)
722 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
724 if (netflow_options
) {
725 if (!ofproto
->netflow
) {
726 ofproto
->netflow
= netflow_create();
728 return netflow_set_options(ofproto
->netflow
, netflow_options
);
730 netflow_destroy(ofproto
->netflow
);
731 ofproto
->netflow
= NULL
;
736 static struct ofport
*
739 struct ofport_dpif
*port
= xmalloc(sizeof *port
);
744 port_dealloc(struct ofport
*port_
)
746 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
751 port_construct(struct ofport
*port_
)
753 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
754 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
756 port
->odp_port
= ofp_port_to_odp_port(port
->up
.ofp_port
);
759 port
->tag
= tag_create_random();
760 port
->may_enable
= true;
762 if (ofproto
->sflow
) {
763 dpif_sflow_add_port(ofproto
->sflow
, port
->odp_port
,
764 netdev_get_name(port
->up
.netdev
));
771 port_destruct(struct ofport
*port_
)
773 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
774 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
776 bundle_remove(port_
);
777 set_cfm(port_
, NULL
);
778 if (ofproto
->sflow
) {
779 dpif_sflow_del_port(ofproto
->sflow
, port
->odp_port
);
784 port_modified(struct ofport
*port_
)
786 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
788 if (port
->bundle
&& port
->bundle
->bond
) {
789 bond_slave_set_netdev(port
->bundle
->bond
, port
, port
->up
.netdev
);
794 port_reconfigured(struct ofport
*port_
, ovs_be32 old_config
)
796 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
797 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
798 ovs_be32 changed
= old_config
^ port
->up
.opp
.config
;
800 if (changed
& htonl(OFPPC_NO_RECV
| OFPPC_NO_RECV_STP
|
801 OFPPC_NO_FWD
| OFPPC_NO_FLOOD
)) {
802 ofproto
->need_revalidate
= true;
807 set_sflow(struct ofproto
*ofproto_
,
808 const struct ofproto_sflow_options
*sflow_options
)
810 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
811 struct dpif_sflow
*ds
= ofproto
->sflow
;
814 struct ofport_dpif
*ofport
;
816 ds
= ofproto
->sflow
= dpif_sflow_create(ofproto
->dpif
);
817 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
818 dpif_sflow_add_port(ds
, ofport
->odp_port
,
819 netdev_get_name(ofport
->up
.netdev
));
822 dpif_sflow_set_options(ds
, sflow_options
);
824 dpif_sflow_destroy(ds
);
825 ofproto
->sflow
= NULL
;
831 set_cfm(struct ofport
*ofport_
, const struct cfm_settings
*s
)
833 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
840 struct ofproto_dpif
*ofproto
;
842 ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
843 ofproto
->need_revalidate
= true;
844 ofport
->cfm
= cfm_create(netdev_get_name(ofport
->up
.netdev
));
847 if (cfm_configure(ofport
->cfm
, s
)) {
853 cfm_destroy(ofport
->cfm
);
859 get_cfm_fault(const struct ofport
*ofport_
)
861 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
863 return ofport
->cfm
? cfm_get_fault(ofport
->cfm
) : -1;
867 get_cfm_remote_mpids(const struct ofport
*ofport_
, const uint64_t **rmps
,
870 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
873 cfm_get_remote_mpids(ofport
->cfm
, rmps
, n_rmps
);
882 /* Expires all MAC learning entries associated with 'port' and forces ofproto
883 * to revalidate every flow. */
885 bundle_flush_macs(struct ofbundle
*bundle
)
887 struct ofproto_dpif
*ofproto
= bundle
->ofproto
;
888 struct mac_learning
*ml
= ofproto
->ml
;
889 struct mac_entry
*mac
, *next_mac
;
891 ofproto
->need_revalidate
= true;
892 LIST_FOR_EACH_SAFE (mac
, next_mac
, lru_node
, &ml
->lrus
) {
893 if (mac
->port
.p
== bundle
) {
894 mac_learning_expire(ml
, mac
);
899 static struct ofbundle
*
900 bundle_lookup(const struct ofproto_dpif
*ofproto
, void *aux
)
902 struct ofbundle
*bundle
;
904 HMAP_FOR_EACH_IN_BUCKET (bundle
, hmap_node
, hash_pointer(aux
, 0),
906 if (bundle
->aux
== aux
) {
913 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
914 * ones that are found to 'bundles'. */
916 bundle_lookup_multiple(struct ofproto_dpif
*ofproto
,
917 void **auxes
, size_t n_auxes
,
918 struct hmapx
*bundles
)
923 for (i
= 0; i
< n_auxes
; i
++) {
924 struct ofbundle
*bundle
= bundle_lookup(ofproto
, auxes
[i
]);
926 hmapx_add(bundles
, bundle
);
932 bundle_del_port(struct ofport_dpif
*port
)
934 struct ofbundle
*bundle
= port
->bundle
;
936 bundle
->ofproto
->need_revalidate
= true;
938 list_remove(&port
->bundle_node
);
942 lacp_slave_unregister(bundle
->lacp
, port
);
945 bond_slave_unregister(bundle
->bond
, port
);
948 bundle
->floodable
= true;
949 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
950 if (port
->up
.opp
.config
& htonl(OFPPC_NO_FLOOD
)) {
951 bundle
->floodable
= false;
957 bundle_add_port(struct ofbundle
*bundle
, uint32_t ofp_port
,
958 struct lacp_slave_settings
*lacp
,
959 uint32_t bond_stable_id
)
961 struct ofport_dpif
*port
;
963 port
= get_ofp_port(bundle
->ofproto
, ofp_port
);
968 if (port
->bundle
!= bundle
) {
969 bundle
->ofproto
->need_revalidate
= true;
971 bundle_del_port(port
);
974 port
->bundle
= bundle
;
975 list_push_back(&bundle
->ports
, &port
->bundle_node
);
976 if (port
->up
.opp
.config
& htonl(OFPPC_NO_FLOOD
)) {
977 bundle
->floodable
= false;
981 lacp_slave_register(bundle
->lacp
, port
, lacp
);
984 port
->bond_stable_id
= bond_stable_id
;
990 bundle_destroy(struct ofbundle
*bundle
)
992 struct ofproto_dpif
*ofproto
;
993 struct ofport_dpif
*port
, *next_port
;
1000 ofproto
= bundle
->ofproto
;
1001 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
1002 struct ofmirror
*m
= ofproto
->mirrors
[i
];
1004 if (m
->out
== bundle
) {
1006 } else if (hmapx_find_and_delete(&m
->srcs
, bundle
)
1007 || hmapx_find_and_delete(&m
->dsts
, bundle
)) {
1008 ofproto
->need_revalidate
= true;
1013 LIST_FOR_EACH_SAFE (port
, next_port
, bundle_node
, &bundle
->ports
) {
1014 bundle_del_port(port
);
1017 bundle_flush_macs(bundle
);
1018 hmap_remove(&ofproto
->bundles
, &bundle
->hmap_node
);
1020 free(bundle
->trunks
);
1021 lacp_destroy(bundle
->lacp
);
1022 bond_destroy(bundle
->bond
);
1027 bundle_set(struct ofproto
*ofproto_
, void *aux
,
1028 const struct ofproto_bundle_settings
*s
)
1030 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1031 bool need_flush
= false;
1032 const unsigned long *trunks
;
1033 struct ofport_dpif
*port
;
1034 struct ofbundle
*bundle
;
1039 bundle_destroy(bundle_lookup(ofproto
, aux
));
1043 assert(s
->n_slaves
== 1 || s
->bond
!= NULL
);
1044 assert((s
->lacp
!= NULL
) == (s
->lacp_slaves
!= NULL
));
1046 bundle
= bundle_lookup(ofproto
, aux
);
1048 bundle
= xmalloc(sizeof *bundle
);
1050 bundle
->ofproto
= ofproto
;
1051 hmap_insert(&ofproto
->bundles
, &bundle
->hmap_node
,
1052 hash_pointer(aux
, 0));
1054 bundle
->name
= NULL
;
1056 list_init(&bundle
->ports
);
1058 bundle
->trunks
= NULL
;
1059 bundle
->lacp
= NULL
;
1060 bundle
->bond
= NULL
;
1062 bundle
->floodable
= true;
1064 bundle
->src_mirrors
= 0;
1065 bundle
->dst_mirrors
= 0;
1066 bundle
->mirror_out
= 0;
1069 if (!bundle
->name
|| strcmp(s
->name
, bundle
->name
)) {
1071 bundle
->name
= xstrdup(s
->name
);
1076 if (!bundle
->lacp
) {
1077 ofproto
->need_revalidate
= true;
1078 bundle
->lacp
= lacp_create();
1080 lacp_configure(bundle
->lacp
, s
->lacp
);
1082 lacp_destroy(bundle
->lacp
);
1083 bundle
->lacp
= NULL
;
1086 /* Update set of ports. */
1088 for (i
= 0; i
< s
->n_slaves
; i
++) {
1089 if (!bundle_add_port(bundle
, s
->slaves
[i
],
1090 s
->lacp
? &s
->lacp_slaves
[i
] : NULL
,
1091 s
->bond_stable_ids
? s
->bond_stable_ids
[i
] : 0)) {
1095 if (!ok
|| list_size(&bundle
->ports
) != s
->n_slaves
) {
1096 struct ofport_dpif
*next_port
;
1098 LIST_FOR_EACH_SAFE (port
, next_port
, bundle_node
, &bundle
->ports
) {
1099 for (i
= 0; i
< s
->n_slaves
; i
++) {
1100 if (s
->slaves
[i
] == port
->up
.ofp_port
) {
1105 bundle_del_port(port
);
1109 assert(list_size(&bundle
->ports
) <= s
->n_slaves
);
1111 if (list_is_empty(&bundle
->ports
)) {
1112 bundle_destroy(bundle
);
1117 if (s
->vlan
!= bundle
->vlan
) {
1118 bundle
->vlan
= s
->vlan
;
1122 /* Get trunked VLANs. */
1123 trunks
= s
->vlan
== -1 ? s
->trunks
: NULL
;
1124 if (!vlan_bitmap_equal(trunks
, bundle
->trunks
)) {
1125 free(bundle
->trunks
);
1126 bundle
->trunks
= vlan_bitmap_clone(trunks
);
1131 if (!list_is_short(&bundle
->ports
)) {
1132 bundle
->ofproto
->has_bonded_bundles
= true;
1134 if (bond_reconfigure(bundle
->bond
, s
->bond
)) {
1135 ofproto
->need_revalidate
= true;
1138 bundle
->bond
= bond_create(s
->bond
);
1139 ofproto
->need_revalidate
= true;
1142 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
1143 bond_slave_register(bundle
->bond
, port
, port
->bond_stable_id
,
1147 bond_destroy(bundle
->bond
);
1148 bundle
->bond
= NULL
;
1151 /* If we changed something that would affect MAC learning, un-learn
1152 * everything on this port and force flow revalidation. */
1154 bundle_flush_macs(bundle
);
1161 bundle_remove(struct ofport
*port_
)
1163 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1164 struct ofbundle
*bundle
= port
->bundle
;
1167 bundle_del_port(port
);
1168 if (list_is_empty(&bundle
->ports
)) {
1169 bundle_destroy(bundle
);
1170 } else if (list_is_short(&bundle
->ports
)) {
1171 bond_destroy(bundle
->bond
);
1172 bundle
->bond
= NULL
;
1178 send_pdu_cb(void *port_
, const void *pdu
, size_t pdu_size
)
1180 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 10);
1181 struct ofport_dpif
*port
= port_
;
1182 uint8_t ea
[ETH_ADDR_LEN
];
1185 error
= netdev_get_etheraddr(port
->up
.netdev
, ea
);
1187 struct ofpbuf packet
;
1190 ofpbuf_init(&packet
, 0);
1191 packet_pdu
= eth_compose(&packet
, eth_addr_lacp
, ea
, ETH_TYPE_LACP
,
1193 memcpy(packet_pdu
, pdu
, pdu_size
);
1195 error
= netdev_send(port
->up
.netdev
, &packet
);
1197 VLOG_WARN_RL(&rl
, "port %s: sending LACP PDU on iface %s failed "
1198 "(%s)", port
->bundle
->name
,
1199 netdev_get_name(port
->up
.netdev
), strerror(error
));
1201 ofpbuf_uninit(&packet
);
1203 VLOG_ERR_RL(&rl
, "port %s: cannot obtain Ethernet address of iface "
1204 "%s (%s)", port
->bundle
->name
,
1205 netdev_get_name(port
->up
.netdev
), strerror(error
));
1210 bundle_send_learning_packets(struct ofbundle
*bundle
)
1212 struct ofproto_dpif
*ofproto
= bundle
->ofproto
;
1213 int error
, n_packets
, n_errors
;
1214 struct mac_entry
*e
;
1216 error
= n_packets
= n_errors
= 0;
1217 LIST_FOR_EACH (e
, lru_node
, &ofproto
->ml
->lrus
) {
1218 if (e
->port
.p
!= bundle
) {
1219 int ret
= bond_send_learning_packet(bundle
->bond
, e
->mac
, e
->vlan
);
1229 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1230 VLOG_WARN_RL(&rl
, "bond %s: %d errors sending %d gratuitous learning "
1231 "packets, last error was: %s",
1232 bundle
->name
, n_errors
, n_packets
, strerror(error
));
1234 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1235 bundle
->name
, n_packets
);
1240 bundle_run(struct ofbundle
*bundle
)
1243 lacp_run(bundle
->lacp
, send_pdu_cb
);
1246 struct ofport_dpif
*port
;
1248 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
1249 bond_slave_set_may_enable(bundle
->bond
, port
, port
->may_enable
);
1252 bond_run(bundle
->bond
, &bundle
->ofproto
->revalidate_set
,
1253 lacp_negotiated(bundle
->lacp
));
1254 if (bond_should_send_learning_packets(bundle
->bond
)) {
1255 bundle_send_learning_packets(bundle
);
1261 bundle_wait(struct ofbundle
*bundle
)
1264 lacp_wait(bundle
->lacp
);
1267 bond_wait(bundle
->bond
);
1274 mirror_scan(struct ofproto_dpif
*ofproto
)
1278 for (idx
= 0; idx
< MAX_MIRRORS
; idx
++) {
1279 if (!ofproto
->mirrors
[idx
]) {
1286 static struct ofmirror
*
1287 mirror_lookup(struct ofproto_dpif
*ofproto
, void *aux
)
1291 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
1292 struct ofmirror
*mirror
= ofproto
->mirrors
[i
];
1293 if (mirror
&& mirror
->aux
== aux
) {
1302 mirror_set(struct ofproto
*ofproto_
, void *aux
,
1303 const struct ofproto_mirror_settings
*s
)
1305 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1306 mirror_mask_t mirror_bit
;
1307 struct ofbundle
*bundle
;
1308 struct ofmirror
*mirror
;
1309 struct ofbundle
*out
;
1310 struct hmapx srcs
; /* Contains "struct ofbundle *"s. */
1311 struct hmapx dsts
; /* Contains "struct ofbundle *"s. */
1314 mirror
= mirror_lookup(ofproto
, aux
);
1316 mirror_destroy(mirror
);
1322 idx
= mirror_scan(ofproto
);
1324 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1326 ofproto
->up
.name
, MAX_MIRRORS
, s
->name
);
1330 mirror
= ofproto
->mirrors
[idx
] = xzalloc(sizeof *mirror
);
1331 mirror
->ofproto
= ofproto
;
1334 mirror
->out_vlan
= -1;
1335 mirror
->name
= NULL
;
1338 if (!mirror
->name
|| strcmp(s
->name
, mirror
->name
)) {
1340 mirror
->name
= xstrdup(s
->name
);
1343 /* Get the new configuration. */
1344 if (s
->out_bundle
) {
1345 out
= bundle_lookup(ofproto
, s
->out_bundle
);
1347 mirror_destroy(mirror
);
1353 out_vlan
= s
->out_vlan
;
1355 bundle_lookup_multiple(ofproto
, s
->srcs
, s
->n_srcs
, &srcs
);
1356 bundle_lookup_multiple(ofproto
, s
->dsts
, s
->n_dsts
, &dsts
);
1358 /* If the configuration has not changed, do nothing. */
1359 if (hmapx_equals(&srcs
, &mirror
->srcs
)
1360 && hmapx_equals(&dsts
, &mirror
->dsts
)
1361 && vlan_bitmap_equal(mirror
->vlans
, s
->src_vlans
)
1362 && mirror
->out
== out
1363 && mirror
->out_vlan
== out_vlan
)
1365 hmapx_destroy(&srcs
);
1366 hmapx_destroy(&dsts
);
1370 hmapx_swap(&srcs
, &mirror
->srcs
);
1371 hmapx_destroy(&srcs
);
1373 hmapx_swap(&dsts
, &mirror
->dsts
);
1374 hmapx_destroy(&dsts
);
1376 free(mirror
->vlans
);
1377 mirror
->vlans
= vlan_bitmap_clone(s
->src_vlans
);
1380 mirror
->out_vlan
= out_vlan
;
1382 /* Update bundles. */
1383 mirror_bit
= MIRROR_MASK_C(1) << mirror
->idx
;
1384 HMAP_FOR_EACH (bundle
, hmap_node
, &mirror
->ofproto
->bundles
) {
1385 if (hmapx_contains(&mirror
->srcs
, bundle
)) {
1386 bundle
->src_mirrors
|= mirror_bit
;
1388 bundle
->src_mirrors
&= ~mirror_bit
;
1391 if (hmapx_contains(&mirror
->dsts
, bundle
)) {
1392 bundle
->dst_mirrors
|= mirror_bit
;
1394 bundle
->dst_mirrors
&= ~mirror_bit
;
1397 if (mirror
->out
== bundle
) {
1398 bundle
->mirror_out
|= mirror_bit
;
1400 bundle
->mirror_out
&= ~mirror_bit
;
1404 ofproto
->need_revalidate
= true;
1405 mac_learning_flush(ofproto
->ml
);
1411 mirror_destroy(struct ofmirror
*mirror
)
1413 struct ofproto_dpif
*ofproto
;
1414 mirror_mask_t mirror_bit
;
1415 struct ofbundle
*bundle
;
1421 ofproto
= mirror
->ofproto
;
1422 ofproto
->need_revalidate
= true;
1423 mac_learning_flush(ofproto
->ml
);
1425 mirror_bit
= MIRROR_MASK_C(1) << mirror
->idx
;
1426 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1427 bundle
->src_mirrors
&= ~mirror_bit
;
1428 bundle
->dst_mirrors
&= ~mirror_bit
;
1429 bundle
->mirror_out
&= ~mirror_bit
;
1432 hmapx_destroy(&mirror
->srcs
);
1433 hmapx_destroy(&mirror
->dsts
);
1434 free(mirror
->vlans
);
1436 ofproto
->mirrors
[mirror
->idx
] = NULL
;
1442 set_flood_vlans(struct ofproto
*ofproto_
, unsigned long *flood_vlans
)
1444 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1445 if (mac_learning_set_flood_vlans(ofproto
->ml
, flood_vlans
)) {
1446 ofproto
->need_revalidate
= true;
1447 mac_learning_flush(ofproto
->ml
);
1453 is_mirror_output_bundle(struct ofproto
*ofproto_
, void *aux
)
1455 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1456 struct ofbundle
*bundle
= bundle_lookup(ofproto
, aux
);
1457 return bundle
&& bundle
->mirror_out
!= 0;
1461 forward_bpdu_changed(struct ofproto
*ofproto_
)
1463 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1464 /* Revalidate cached flows whenever forward_bpdu option changes. */
1465 ofproto
->need_revalidate
= true;
1470 static struct ofport_dpif
*
1471 get_ofp_port(struct ofproto_dpif
*ofproto
, uint16_t ofp_port
)
1473 struct ofport
*ofport
= ofproto_get_port(&ofproto
->up
, ofp_port
);
1474 return ofport
? ofport_dpif_cast(ofport
) : NULL
;
1477 static struct ofport_dpif
*
1478 get_odp_port(struct ofproto_dpif
*ofproto
, uint32_t odp_port
)
1480 return get_ofp_port(ofproto
, odp_port_to_ofp_port(odp_port
));
1484 ofproto_port_from_dpif_port(struct ofproto_port
*ofproto_port
,
1485 struct dpif_port
*dpif_port
)
1487 ofproto_port
->name
= dpif_port
->name
;
1488 ofproto_port
->type
= dpif_port
->type
;
1489 ofproto_port
->ofp_port
= odp_port_to_ofp_port(dpif_port
->port_no
);
1493 port_run(struct ofport_dpif
*ofport
)
1495 bool enable
= netdev_get_carrier(ofport
->up
.netdev
);
1498 cfm_run(ofport
->cfm
);
1500 if (cfm_should_send_ccm(ofport
->cfm
)) {
1501 struct ofpbuf packet
;
1503 ofpbuf_init(&packet
, 0);
1504 cfm_compose_ccm(ofport
->cfm
, &packet
, ofport
->up
.opp
.hw_addr
);
1505 send_packet(ofproto_dpif_cast(ofport
->up
.ofproto
),
1506 ofport
->odp_port
, &packet
);
1507 ofpbuf_uninit(&packet
);
1510 enable
= enable
&& !cfm_get_fault(ofport
->cfm
);
1513 if (ofport
->bundle
) {
1514 enable
= enable
&& lacp_slave_may_enable(ofport
->bundle
->lacp
, ofport
);
1517 if (ofport
->may_enable
!= enable
) {
1518 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
1520 if (ofproto
->has_bundle_action
) {
1521 ofproto
->need_revalidate
= true;
1525 ofport
->may_enable
= enable
;
1529 port_wait(struct ofport_dpif
*ofport
)
1532 cfm_wait(ofport
->cfm
);
1537 port_query_by_name(const struct ofproto
*ofproto_
, const char *devname
,
1538 struct ofproto_port
*ofproto_port
)
1540 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1541 struct dpif_port dpif_port
;
1544 error
= dpif_port_query_by_name(ofproto
->dpif
, devname
, &dpif_port
);
1546 ofproto_port_from_dpif_port(ofproto_port
, &dpif_port
);
1552 port_add(struct ofproto
*ofproto_
, struct netdev
*netdev
, uint16_t *ofp_portp
)
1554 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1558 error
= dpif_port_add(ofproto
->dpif
, netdev
, &odp_port
);
1560 *ofp_portp
= odp_port_to_ofp_port(odp_port
);
1566 port_del(struct ofproto
*ofproto_
, uint16_t ofp_port
)
1568 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1571 error
= dpif_port_del(ofproto
->dpif
, ofp_port_to_odp_port(ofp_port
));
1573 struct ofport_dpif
*ofport
= get_ofp_port(ofproto
, ofp_port
);
1575 /* The caller is going to close ofport->up.netdev. If this is a
1576 * bonded port, then the bond is using that netdev, so remove it
1577 * from the bond. The client will need to reconfigure everything
1578 * after deleting ports, so then the slave will get re-added. */
1579 bundle_remove(&ofport
->up
);
1585 struct port_dump_state
{
1586 struct dpif_port_dump dump
;
1591 port_dump_start(const struct ofproto
*ofproto_
, void **statep
)
1593 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1594 struct port_dump_state
*state
;
1596 *statep
= state
= xmalloc(sizeof *state
);
1597 dpif_port_dump_start(&state
->dump
, ofproto
->dpif
);
1598 state
->done
= false;
1603 port_dump_next(const struct ofproto
*ofproto_ OVS_UNUSED
, void *state_
,
1604 struct ofproto_port
*port
)
1606 struct port_dump_state
*state
= state_
;
1607 struct dpif_port dpif_port
;
1609 if (dpif_port_dump_next(&state
->dump
, &dpif_port
)) {
1610 ofproto_port_from_dpif_port(port
, &dpif_port
);
1613 int error
= dpif_port_dump_done(&state
->dump
);
1615 return error
? error
: EOF
;
1620 port_dump_done(const struct ofproto
*ofproto_ OVS_UNUSED
, void *state_
)
1622 struct port_dump_state
*state
= state_
;
1625 dpif_port_dump_done(&state
->dump
);
1632 port_poll(const struct ofproto
*ofproto_
, char **devnamep
)
1634 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1635 return dpif_port_poll(ofproto
->dpif
, devnamep
);
1639 port_poll_wait(const struct ofproto
*ofproto_
)
1641 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1642 dpif_port_poll_wait(ofproto
->dpif
);
1646 port_is_lacp_current(const struct ofport
*ofport_
)
1648 const struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
1649 return (ofport
->bundle
&& ofport
->bundle
->lacp
1650 ? lacp_slave_is_current(ofport
->bundle
->lacp
, ofport
)
1654 /* Upcall handling. */
1656 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
1657 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
1658 * their individual configurations.
1660 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
1661 * Otherwise, ownership is transferred to this function. */
1663 send_packet_in(struct ofproto_dpif
*ofproto
, struct dpif_upcall
*upcall
,
1664 const struct flow
*flow
, bool clone
)
1666 struct ofputil_packet_in pin
;
1668 pin
.packet
= upcall
->packet
;
1669 pin
.in_port
= flow
->in_port
;
1670 pin
.reason
= upcall
->type
== DPIF_UC_MISS
? OFPR_NO_MATCH
: OFPR_ACTION
;
1671 pin
.buffer_id
= 0; /* not yet known */
1672 pin
.send_len
= upcall
->userdata
;
1673 connmgr_send_packet_in(ofproto
->up
.connmgr
, &pin
, flow
,
1674 clone
? NULL
: upcall
->packet
);
1678 process_special(struct ofproto_dpif
*ofproto
, const struct flow
*flow
,
1679 const struct ofpbuf
*packet
)
1681 struct ofport_dpif
*ofport
= get_ofp_port(ofproto
, flow
->in_port
);
1687 if (ofport
->cfm
&& cfm_should_process_flow(ofport
->cfm
, flow
)) {
1689 cfm_process_heartbeat(ofport
->cfm
, packet
);
1692 } else if (ofport
->bundle
&& ofport
->bundle
->lacp
1693 && flow
->dl_type
== htons(ETH_TYPE_LACP
)) {
1695 lacp_process_packet(ofport
->bundle
->lacp
, ofport
, packet
);
1703 handle_miss_upcall(struct ofproto_dpif
*ofproto
, struct dpif_upcall
*upcall
)
1705 struct facet
*facet
;
1708 /* Obtain in_port and tun_id, at least. */
1709 odp_flow_key_to_flow(upcall
->key
, upcall
->key_len
, &flow
);
1711 /* Set header pointers in 'flow'. */
1712 flow_extract(upcall
->packet
, flow
.tun_id
, flow
.in_port
, &flow
);
1714 /* Handle 802.1ag and LACP. */
1715 if (process_special(ofproto
, &flow
, upcall
->packet
)) {
1716 ofpbuf_delete(upcall
->packet
);
1717 ofproto
->n_matches
++;
1721 /* Check with in-band control to see if this packet should be sent
1722 * to the local port regardless of the flow table. */
1723 if (connmgr_msg_in_hook(ofproto
->up
.connmgr
, &flow
, upcall
->packet
)) {
1724 send_packet(ofproto
, OVSP_LOCAL
, upcall
->packet
);
1727 facet
= facet_lookup_valid(ofproto
, &flow
);
1729 struct rule_dpif
*rule
= rule_dpif_lookup(ofproto
, &flow
, 0);
1731 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
1732 struct ofport_dpif
*port
= get_ofp_port(ofproto
, flow
.in_port
);
1734 if (port
->up
.opp
.config
& htonl(OFPPC_NO_PACKET_IN
)) {
1735 COVERAGE_INC(ofproto_dpif_no_packet_in
);
1736 /* XXX install 'drop' flow entry */
1737 ofpbuf_delete(upcall
->packet
);
1741 VLOG_WARN_RL(&rl
, "packet-in on unknown port %"PRIu16
,
1745 send_packet_in(ofproto
, upcall
, &flow
, false);
1749 facet
= facet_create(rule
, &flow
, upcall
->packet
);
1750 } else if (!facet
->may_install
) {
1751 /* The facet is not installable, that is, we need to process every
1752 * packet, so process the current packet's actions into 'facet'. */
1753 facet_make_actions(ofproto
, facet
, upcall
->packet
);
1756 if (facet
->rule
->up
.cr
.priority
== FAIL_OPEN_PRIORITY
) {
1758 * Extra-special case for fail-open mode.
1760 * We are in fail-open mode and the packet matched the fail-open rule,
1761 * but we are connected to a controller too. We should send the packet
1762 * up to the controller in the hope that it will try to set up a flow
1763 * and thereby allow us to exit fail-open.
1765 * See the top-level comment in fail-open.c for more information.
1767 send_packet_in(ofproto
, upcall
, &flow
, true);
1770 facet_execute(ofproto
, facet
, upcall
->packet
);
1771 facet_install(ofproto
, facet
, false);
1772 ofproto
->n_matches
++;
1776 handle_upcall(struct ofproto_dpif
*ofproto
, struct dpif_upcall
*upcall
)
1780 switch (upcall
->type
) {
1781 case DPIF_UC_ACTION
:
1782 COVERAGE_INC(ofproto_dpif_ctlr_action
);
1783 odp_flow_key_to_flow(upcall
->key
, upcall
->key_len
, &flow
);
1784 send_packet_in(ofproto
, upcall
, &flow
, false);
1787 case DPIF_UC_SAMPLE
:
1788 if (ofproto
->sflow
) {
1789 odp_flow_key_to_flow(upcall
->key
, upcall
->key_len
, &flow
);
1790 dpif_sflow_received(ofproto
->sflow
, upcall
, &flow
);
1792 ofpbuf_delete(upcall
->packet
);
1796 handle_miss_upcall(ofproto
, upcall
);
1799 case DPIF_N_UC_TYPES
:
1801 VLOG_WARN_RL(&rl
, "upcall has unexpected type %"PRIu32
, upcall
->type
);
1806 /* Flow expiration. */
1808 static int facet_max_idle(const struct ofproto_dpif
*);
1809 static void update_stats(struct ofproto_dpif
*);
1810 static void rule_expire(struct rule_dpif
*);
1811 static void expire_facets(struct ofproto_dpif
*, int dp_max_idle
);
1813 /* This function is called periodically by run(). Its job is to collect
1814 * updates for the flows that have been installed into the datapath, most
1815 * importantly when they last were used, and then use that information to
1816 * expire flows that have not been used recently.
1818 * Returns the number of milliseconds after which it should be called again. */
1820 expire(struct ofproto_dpif
*ofproto
)
1822 struct rule_dpif
*rule
, *next_rule
;
1823 struct classifier
*table
;
1826 /* Update stats for each flow in the datapath. */
1827 update_stats(ofproto
);
1829 /* Expire facets that have been idle too long. */
1830 dp_max_idle
= facet_max_idle(ofproto
);
1831 expire_facets(ofproto
, dp_max_idle
);
1833 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
1834 OFPROTO_FOR_EACH_TABLE (table
, &ofproto
->up
) {
1835 struct cls_cursor cursor
;
1837 cls_cursor_init(&cursor
, table
, NULL
);
1838 CLS_CURSOR_FOR_EACH_SAFE (rule
, next_rule
, up
.cr
, &cursor
) {
1843 /* All outstanding data in existing flows has been accounted, so it's a
1844 * good time to do bond rebalancing. */
1845 if (ofproto
->has_bonded_bundles
) {
1846 struct ofbundle
*bundle
;
1848 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1850 bond_rebalance(bundle
->bond
, &ofproto
->revalidate_set
);
1855 return MIN(dp_max_idle
, 1000);
1858 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
1860 * This function also pushes statistics updates to rules which each facet
1861 * resubmits into. Generally these statistics will be accurate. However, if a
1862 * facet changes the rule it resubmits into at some time in between
1863 * update_stats() runs, it is possible that statistics accrued to the
1864 * old rule will be incorrectly attributed to the new rule. This could be
1865 * avoided by calling update_stats() whenever rules are created or
1866 * deleted. However, the performance impact of making so many calls to the
1867 * datapath do not justify the benefit of having perfectly accurate statistics.
1870 update_stats(struct ofproto_dpif
*p
)
1872 const struct dpif_flow_stats
*stats
;
1873 struct dpif_flow_dump dump
;
1874 const struct nlattr
*key
;
1877 dpif_flow_dump_start(&dump
, p
->dpif
);
1878 while (dpif_flow_dump_next(&dump
, &key
, &key_len
, NULL
, NULL
, &stats
)) {
1879 struct facet
*facet
;
1882 if (odp_flow_key_to_flow(key
, key_len
, &flow
)) {
1886 odp_flow_key_format(key
, key_len
, &s
);
1887 VLOG_WARN_RL(&rl
, "failed to convert datapath flow key to flow: %s",
1893 facet
= facet_find(p
, &flow
);
1895 if (facet
&& facet
->installed
) {
1897 if (stats
->n_packets
>= facet
->dp_packet_count
) {
1898 uint64_t extra
= stats
->n_packets
- facet
->dp_packet_count
;
1899 facet
->packet_count
+= extra
;
1901 VLOG_WARN_RL(&rl
, "unexpected packet count from the datapath");
1904 if (stats
->n_bytes
>= facet
->dp_byte_count
) {
1905 facet
->byte_count
+= stats
->n_bytes
- facet
->dp_byte_count
;
1907 VLOG_WARN_RL(&rl
, "unexpected byte count from datapath");
1910 facet
->dp_packet_count
= stats
->n_packets
;
1911 facet
->dp_byte_count
= stats
->n_bytes
;
1913 facet_update_time(p
, facet
, stats
->used
);
1914 facet_account(p
, facet
);
1915 facet_push_stats(facet
);
1917 /* There's a flow in the datapath that we know nothing about.
1919 COVERAGE_INC(facet_unexpected
);
1920 dpif_flow_del(p
->dpif
, key
, key_len
, NULL
);
1923 dpif_flow_dump_done(&dump
);
1926 /* Calculates and returns the number of milliseconds of idle time after which
1927 * facets should expire from the datapath and we should fold their statistics
1928 * into their parent rules in userspace. */
1930 facet_max_idle(const struct ofproto_dpif
*ofproto
)
1933 * Idle time histogram.
1935 * Most of the time a switch has a relatively small number of facets. When
1936 * this is the case we might as well keep statistics for all of them in
1937 * userspace and to cache them in the kernel datapath for performance as
1940 * As the number of facets increases, the memory required to maintain
1941 * statistics about them in userspace and in the kernel becomes
1942 * significant. However, with a large number of facets it is likely that
1943 * only a few of them are "heavy hitters" that consume a large amount of
1944 * bandwidth. At this point, only heavy hitters are worth caching in the
1945 * kernel and maintaining in userspaces; other facets we can discard.
1947 * The technique used to compute the idle time is to build a histogram with
1948 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
1949 * that is installed in the kernel gets dropped in the appropriate bucket.
1950 * After the histogram has been built, we compute the cutoff so that only
1951 * the most-recently-used 1% of facets (but at least
1952 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
1953 * the most-recently-used bucket of facets is kept, so actually an
1954 * arbitrary number of facets can be kept in any given expiration run
1955 * (though the next run will delete most of those unless they receive
1958 * This requires a second pass through the facets, in addition to the pass
1959 * made by update_stats(), because the former function never looks
1960 * at uninstallable facets.
1962 enum { BUCKET_WIDTH
= ROUND_UP(100, TIME_UPDATE_INTERVAL
) };
1963 enum { N_BUCKETS
= 5000 / BUCKET_WIDTH
};
1964 int buckets
[N_BUCKETS
] = { 0 };
1965 int total
, subtotal
, bucket
;
1966 struct facet
*facet
;
1970 total
= hmap_count(&ofproto
->facets
);
1971 if (total
<= ofproto
->up
.flow_eviction_threshold
) {
1972 return N_BUCKETS
* BUCKET_WIDTH
;
1975 /* Build histogram. */
1977 HMAP_FOR_EACH (facet
, hmap_node
, &ofproto
->facets
) {
1978 long long int idle
= now
- facet
->used
;
1979 int bucket
= (idle
<= 0 ? 0
1980 : idle
>= BUCKET_WIDTH
* N_BUCKETS
? N_BUCKETS
- 1
1981 : (unsigned int) idle
/ BUCKET_WIDTH
);
1985 /* Find the first bucket whose flows should be expired. */
1986 subtotal
= bucket
= 0;
1988 subtotal
+= buckets
[bucket
++];
1989 } while (bucket
< N_BUCKETS
&&
1990 subtotal
< MAX(ofproto
->up
.flow_eviction_threshold
, total
/ 100));
1992 if (VLOG_IS_DBG_ENABLED()) {
1996 ds_put_cstr(&s
, "keep");
1997 for (i
= 0; i
< N_BUCKETS
; i
++) {
1999 ds_put_cstr(&s
, ", drop");
2002 ds_put_format(&s
, " %d:%d", i
* BUCKET_WIDTH
, buckets
[i
]);
2005 VLOG_INFO("%s: %s (msec:count)", ofproto
->up
.name
, ds_cstr(&s
));
2009 return bucket
* BUCKET_WIDTH
;
2013 facet_active_timeout(struct ofproto_dpif
*ofproto
, struct facet
*facet
)
2015 if (ofproto
->netflow
&& !facet_is_controller_flow(facet
) &&
2016 netflow_active_timeout_expired(ofproto
->netflow
, &facet
->nf_flow
)) {
2017 struct ofexpired expired
;
2019 if (facet
->installed
) {
2020 struct dpif_flow_stats stats
;
2022 facet_put__(ofproto
, facet
, facet
->actions
, facet
->actions_len
,
2024 facet_update_stats(ofproto
, facet
, &stats
);
2027 expired
.flow
= facet
->flow
;
2028 expired
.packet_count
= facet
->packet_count
;
2029 expired
.byte_count
= facet
->byte_count
;
2030 expired
.used
= facet
->used
;
2031 netflow_expire(ofproto
->netflow
, &facet
->nf_flow
, &expired
);
2036 expire_facets(struct ofproto_dpif
*ofproto
, int dp_max_idle
)
2038 long long int cutoff
= time_msec() - dp_max_idle
;
2039 struct facet
*facet
, *next_facet
;
2041 HMAP_FOR_EACH_SAFE (facet
, next_facet
, hmap_node
, &ofproto
->facets
) {
2042 facet_active_timeout(ofproto
, facet
);
2043 if (facet
->used
< cutoff
) {
2044 facet_remove(ofproto
, facet
);
2049 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2050 * then delete it entirely. */
2052 rule_expire(struct rule_dpif
*rule
)
2054 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
2055 struct facet
*facet
, *next_facet
;
2059 /* Has 'rule' expired? */
2061 if (rule
->up
.hard_timeout
2062 && now
> rule
->up
.modified
+ rule
->up
.hard_timeout
* 1000) {
2063 reason
= OFPRR_HARD_TIMEOUT
;
2064 } else if (rule
->up
.idle_timeout
&& list_is_empty(&rule
->facets
)
2065 && now
> rule
->used
+ rule
->up
.idle_timeout
* 1000) {
2066 reason
= OFPRR_IDLE_TIMEOUT
;
2071 COVERAGE_INC(ofproto_dpif_expired
);
2073 /* Update stats. (This is a no-op if the rule expired due to an idle
2074 * timeout, because that only happens when the rule has no facets left.) */
2075 LIST_FOR_EACH_SAFE (facet
, next_facet
, list_node
, &rule
->facets
) {
2076 facet_remove(ofproto
, facet
);
2079 /* Get rid of the rule. */
2080 ofproto_rule_expire(&rule
->up
, reason
);
2085 /* Creates and returns a new facet owned by 'rule', given a 'flow' and an
2086 * example 'packet' within that flow.
2088 * The caller must already have determined that no facet with an identical
2089 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2090 * the ofproto's classifier table. */
2091 static struct facet
*
2092 facet_create(struct rule_dpif
*rule
, const struct flow
*flow
,
2093 const struct ofpbuf
*packet
)
2095 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
2096 struct facet
*facet
;
2098 facet
= xzalloc(sizeof *facet
);
2099 facet
->used
= time_msec();
2100 hmap_insert(&ofproto
->facets
, &facet
->hmap_node
, flow_hash(flow
, 0));
2101 list_push_back(&rule
->facets
, &facet
->list_node
);
2103 facet
->flow
= *flow
;
2104 netflow_flow_init(&facet
->nf_flow
);
2105 netflow_flow_update_time(ofproto
->netflow
, &facet
->nf_flow
, facet
->used
);
2107 facet_make_actions(ofproto
, facet
, packet
);
2113 facet_free(struct facet
*facet
)
2115 free(facet
->actions
);
2119 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2120 * 'packet', which arrived on 'in_port'.
2122 * Takes ownership of 'packet'. */
2124 execute_odp_actions(struct ofproto_dpif
*ofproto
, const struct flow
*flow
,
2125 const struct nlattr
*odp_actions
, size_t actions_len
,
2126 struct ofpbuf
*packet
)
2128 if (actions_len
== NLA_ALIGN(NLA_HDRLEN
+ sizeof(uint64_t))
2129 && odp_actions
->nla_type
== OVS_ACTION_ATTR_USERSPACE
) {
2130 /* As an optimization, avoid a round-trip from userspace to kernel to
2131 * userspace. This also avoids possibly filling up kernel packet
2132 * buffers along the way. */
2133 struct dpif_upcall upcall
;
2135 upcall
.type
= DPIF_UC_ACTION
;
2136 upcall
.packet
= packet
;
2139 upcall
.userdata
= nl_attr_get_u64(odp_actions
);
2140 upcall
.sample_pool
= 0;
2141 upcall
.actions
= NULL
;
2142 upcall
.actions_len
= 0;
2144 send_packet_in(ofproto
, &upcall
, flow
, false);
2148 struct odputil_keybuf keybuf
;
2152 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
2153 odp_flow_key_from_flow(&key
, flow
);
2155 error
= dpif_execute(ofproto
->dpif
, key
.data
, key
.size
,
2156 odp_actions
, actions_len
, packet
);
2158 ofpbuf_delete(packet
);
2163 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2164 * statistics appropriately. 'packet' must have at least sizeof(struct
2165 * ofp_packet_in) bytes of headroom.
2167 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2168 * applying flow_extract() to 'packet' would yield the same flow as
2171 * 'facet' must have accurately composed datapath actions; that is, it must
2172 * not be in need of revalidation.
2174 * Takes ownership of 'packet'. */
2176 facet_execute(struct ofproto_dpif
*ofproto
, struct facet
*facet
,
2177 struct ofpbuf
*packet
)
2179 struct dpif_flow_stats stats
;
2181 assert(ofpbuf_headroom(packet
) >= sizeof(struct ofp_packet_in
));
2183 flow_extract_stats(&facet
->flow
, packet
, &stats
);
2184 stats
.used
= time_msec();
2185 if (execute_odp_actions(ofproto
, &facet
->flow
,
2186 facet
->actions
, facet
->actions_len
, packet
)) {
2187 facet_update_stats(ofproto
, facet
, &stats
);
2191 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2193 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2194 * rule's statistics, via facet_uninstall().
2196 * - Removes 'facet' from its rule and from ofproto->facets.
2199 facet_remove(struct ofproto_dpif
*ofproto
, struct facet
*facet
)
2201 facet_uninstall(ofproto
, facet
);
2202 facet_flush_stats(ofproto
, facet
);
2203 hmap_remove(&ofproto
->facets
, &facet
->hmap_node
);
2204 list_remove(&facet
->list_node
);
2208 /* Composes the datapath actions for 'facet' based on its rule's actions. */
2210 facet_make_actions(struct ofproto_dpif
*p
, struct facet
*facet
,
2211 const struct ofpbuf
*packet
)
2213 const struct rule_dpif
*rule
= facet
->rule
;
2214 struct ofpbuf
*odp_actions
;
2215 struct action_xlate_ctx ctx
;
2217 action_xlate_ctx_init(&ctx
, p
, &facet
->flow
, packet
);
2218 odp_actions
= xlate_actions(&ctx
, rule
->up
.actions
, rule
->up
.n_actions
);
2219 facet
->tags
= ctx
.tags
;
2220 facet
->may_install
= ctx
.may_set_up_flow
;
2221 facet
->has_learn
= ctx
.has_learn
;
2222 facet
->has_normal
= ctx
.has_normal
;
2223 facet
->nf_flow
.output_iface
= ctx
.nf_output_iface
;
2225 if (facet
->actions_len
!= odp_actions
->size
2226 || memcmp(facet
->actions
, odp_actions
->data
, odp_actions
->size
)) {
2227 free(facet
->actions
);
2228 facet
->actions_len
= odp_actions
->size
;
2229 facet
->actions
= xmemdup(odp_actions
->data
, odp_actions
->size
);
2232 ofpbuf_delete(odp_actions
);
2235 /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
2236 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
2237 * in the datapath will be zeroed and 'stats' will be updated with traffic new
2238 * since 'facet' was last updated.
2240 * Returns 0 if successful, otherwise a positive errno value.*/
2242 facet_put__(struct ofproto_dpif
*ofproto
, struct facet
*facet
,
2243 const struct nlattr
*actions
, size_t actions_len
,
2244 struct dpif_flow_stats
*stats
)
2246 struct odputil_keybuf keybuf
;
2247 enum dpif_flow_put_flags flags
;
2251 flags
= DPIF_FP_CREATE
| DPIF_FP_MODIFY
;
2253 flags
|= DPIF_FP_ZERO_STATS
;
2256 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
2257 odp_flow_key_from_flow(&key
, &facet
->flow
);
2259 ret
= dpif_flow_put(ofproto
->dpif
, flags
, key
.data
, key
.size
,
2260 actions
, actions_len
, stats
);
2263 facet_reset_dp_stats(facet
, stats
);
2269 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2270 * 'zero_stats' is true, clears any existing statistics from the datapath for
2273 facet_install(struct ofproto_dpif
*p
, struct facet
*facet
, bool zero_stats
)
2275 struct dpif_flow_stats stats
;
2277 if (facet
->may_install
2278 && !facet_put__(p
, facet
, facet
->actions
, facet
->actions_len
,
2279 zero_stats
? &stats
: NULL
)) {
2280 facet
->installed
= true;
2285 vlan_tci_to_openflow_vlan(ovs_be16 vlan_tci
)
2287 return vlan_tci
!= htons(0) ? vlan_tci_to_vid(vlan_tci
) : OFP_VLAN_NONE
;
2291 facet_account(struct ofproto_dpif
*ofproto
, struct facet
*facet
)
2294 const struct nlattr
*a
;
2298 if (facet
->byte_count
<= facet
->accounted_bytes
) {
2301 n_bytes
= facet
->byte_count
- facet
->accounted_bytes
;
2302 facet
->accounted_bytes
= facet
->byte_count
;
2304 /* Feed information from the active flows back into the learning table to
2305 * ensure that table is always in sync with what is actually flowing
2306 * through the datapath. */
2307 if (facet
->has_learn
|| facet
->has_normal
) {
2308 struct action_xlate_ctx ctx
;
2310 action_xlate_ctx_init(&ctx
, ofproto
, &facet
->flow
, NULL
);
2311 ctx
.may_learn
= true;
2312 ofpbuf_delete(xlate_actions(&ctx
, facet
->rule
->up
.actions
,
2313 facet
->rule
->up
.n_actions
));
2316 if (!facet
->has_normal
|| !ofproto
->has_bonded_bundles
) {
2320 /* This loop feeds byte counters to bond_account() for rebalancing to use
2321 * as a basis. We also need to track the actual VLAN on which the packet
2322 * is going to be sent to ensure that it matches the one passed to
2323 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2325 vlan_tci
= facet
->flow
.vlan_tci
;
2326 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, facet
->actions
, facet
->actions_len
) {
2327 struct ofport_dpif
*port
;
2329 switch (nl_attr_type(a
)) {
2330 case OVS_ACTION_ATTR_OUTPUT
:
2331 port
= get_odp_port(ofproto
, nl_attr_get_u32(a
));
2332 if (port
&& port
->bundle
&& port
->bundle
->bond
) {
2333 bond_account(port
->bundle
->bond
, &facet
->flow
,
2334 vlan_tci_to_openflow_vlan(vlan_tci
), n_bytes
);
2338 case OVS_ACTION_ATTR_POP_VLAN
:
2339 vlan_tci
= htons(0);
2342 case OVS_ACTION_ATTR_PUSH_VLAN
:
2343 vlan_tci
= nl_attr_get_be16(a
);
2349 /* If 'rule' is installed in the datapath, uninstalls it. */
2351 facet_uninstall(struct ofproto_dpif
*p
, struct facet
*facet
)
2353 if (facet
->installed
) {
2354 struct odputil_keybuf keybuf
;
2355 struct dpif_flow_stats stats
;
2359 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
2360 odp_flow_key_from_flow(&key
, &facet
->flow
);
2362 error
= dpif_flow_del(p
->dpif
, key
.data
, key
.size
, &stats
);
2363 facet_reset_dp_stats(facet
, &stats
);
2365 facet_update_stats(p
, facet
, &stats
);
2367 facet
->installed
= false;
2369 assert(facet
->dp_packet_count
== 0);
2370 assert(facet
->dp_byte_count
== 0);
2374 /* Returns true if the only action for 'facet' is to send to the controller.
2375 * (We don't report NetFlow expiration messages for such facets because they
2376 * are just part of the control logic for the network, not real traffic). */
2378 facet_is_controller_flow(struct facet
*facet
)
2381 && facet
->rule
->up
.n_actions
== 1
2382 && action_outputs_to_port(&facet
->rule
->up
.actions
[0],
2383 htons(OFPP_CONTROLLER
)));
2386 /* Resets 'facet''s datapath statistics counters. This should be called when
2387 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
2388 * it should contain the statistics returned by dpif when 'facet' was reset in
2389 * the datapath. 'stats' will be modified to only included statistics new
2390 * since 'facet' was last updated. */
2392 facet_reset_dp_stats(struct facet
*facet
, struct dpif_flow_stats
*stats
)
2394 if (stats
&& facet
->dp_packet_count
<= stats
->n_packets
2395 && facet
->dp_byte_count
<= stats
->n_bytes
) {
2396 stats
->n_packets
-= facet
->dp_packet_count
;
2397 stats
->n_bytes
-= facet
->dp_byte_count
;
2400 facet
->dp_packet_count
= 0;
2401 facet
->dp_byte_count
= 0;
2404 /* Folds all of 'facet''s statistics into its rule. Also updates the
2405 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
2406 * 'facet''s statistics in the datapath should have been zeroed and folded into
2407 * its packet and byte counts before this function is called. */
2409 facet_flush_stats(struct ofproto_dpif
*ofproto
, struct facet
*facet
)
2411 assert(!facet
->dp_byte_count
);
2412 assert(!facet
->dp_packet_count
);
2414 facet_push_stats(facet
);
2415 facet_account(ofproto
, facet
);
2417 if (ofproto
->netflow
&& !facet_is_controller_flow(facet
)) {
2418 struct ofexpired expired
;
2419 expired
.flow
= facet
->flow
;
2420 expired
.packet_count
= facet
->packet_count
;
2421 expired
.byte_count
= facet
->byte_count
;
2422 expired
.used
= facet
->used
;
2423 netflow_expire(ofproto
->netflow
, &facet
->nf_flow
, &expired
);
2426 facet
->rule
->packet_count
+= facet
->packet_count
;
2427 facet
->rule
->byte_count
+= facet
->byte_count
;
2429 /* Reset counters to prevent double counting if 'facet' ever gets
2431 facet_reset_counters(facet
);
2433 netflow_flow_clear(&facet
->nf_flow
);
2436 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2437 * Returns it if found, otherwise a null pointer.
2439 * The returned facet might need revalidation; use facet_lookup_valid()
2440 * instead if that is important. */
2441 static struct facet
*
2442 facet_find(struct ofproto_dpif
*ofproto
, const struct flow
*flow
)
2444 struct facet
*facet
;
2446 HMAP_FOR_EACH_WITH_HASH (facet
, hmap_node
, flow_hash(flow
, 0),
2448 if (flow_equal(flow
, &facet
->flow
)) {
2456 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2457 * Returns it if found, otherwise a null pointer.
2459 * The returned facet is guaranteed to be valid. */
2460 static struct facet
*
2461 facet_lookup_valid(struct ofproto_dpif
*ofproto
, const struct flow
*flow
)
2463 struct facet
*facet
= facet_find(ofproto
, flow
);
2465 /* The facet we found might not be valid, since we could be in need of
2466 * revalidation. If it is not valid, don't return it. */
2468 && ofproto
->need_revalidate
2469 && !facet_revalidate(ofproto
, facet
)) {
2470 COVERAGE_INC(facet_invalidated
);
2477 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
2479 * - If the rule found is different from 'facet''s current rule, moves
2480 * 'facet' to the new rule and recompiles its actions.
2482 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
2483 * where it is and recompiles its actions anyway.
2485 * - If there is none, destroys 'facet'.
2487 * Returns true if 'facet' still exists, false if it has been destroyed. */
2489 facet_revalidate(struct ofproto_dpif
*ofproto
, struct facet
*facet
)
2491 struct action_xlate_ctx ctx
;
2492 struct ofpbuf
*odp_actions
;
2493 struct rule_dpif
*new_rule
;
2494 bool actions_changed
;
2496 COVERAGE_INC(facet_revalidate
);
2498 /* Determine the new rule. */
2499 new_rule
= rule_dpif_lookup(ofproto
, &facet
->flow
, 0);
2501 /* No new rule, so delete the facet. */
2502 facet_remove(ofproto
, facet
);
2506 /* Calculate new datapath actions.
2508 * We do not modify any 'facet' state yet, because we might need to, e.g.,
2509 * emit a NetFlow expiration and, if so, we need to have the old state
2510 * around to properly compose it. */
2511 action_xlate_ctx_init(&ctx
, ofproto
, &facet
->flow
, NULL
);
2512 odp_actions
= xlate_actions(&ctx
,
2513 new_rule
->up
.actions
, new_rule
->up
.n_actions
);
2514 actions_changed
= (facet
->actions_len
!= odp_actions
->size
2515 || memcmp(facet
->actions
, odp_actions
->data
,
2516 facet
->actions_len
));
2518 /* If the datapath actions changed or the installability changed,
2519 * then we need to talk to the datapath. */
2520 if (actions_changed
|| ctx
.may_set_up_flow
!= facet
->installed
) {
2521 if (ctx
.may_set_up_flow
) {
2522 struct dpif_flow_stats stats
;
2524 facet_put__(ofproto
, facet
,
2525 odp_actions
->data
, odp_actions
->size
, &stats
);
2526 facet_update_stats(ofproto
, facet
, &stats
);
2528 facet_uninstall(ofproto
, facet
);
2531 /* The datapath flow is gone or has zeroed stats, so push stats out of
2532 * 'facet' into 'rule'. */
2533 facet_flush_stats(ofproto
, facet
);
2536 /* Update 'facet' now that we've taken care of all the old state. */
2537 facet
->tags
= ctx
.tags
;
2538 facet
->nf_flow
.output_iface
= ctx
.nf_output_iface
;
2539 facet
->may_install
= ctx
.may_set_up_flow
;
2540 facet
->has_learn
= ctx
.has_learn
;
2541 facet
->has_normal
= ctx
.has_normal
;
2542 if (actions_changed
) {
2543 free(facet
->actions
);
2544 facet
->actions_len
= odp_actions
->size
;
2545 facet
->actions
= xmemdup(odp_actions
->data
, odp_actions
->size
);
2547 if (facet
->rule
!= new_rule
) {
2548 COVERAGE_INC(facet_changed_rule
);
2549 list_remove(&facet
->list_node
);
2550 list_push_back(&new_rule
->facets
, &facet
->list_node
);
2551 facet
->rule
= new_rule
;
2552 facet
->used
= new_rule
->up
.created
;
2553 facet
->rs_used
= facet
->used
;
2556 ofpbuf_delete(odp_actions
);
2561 /* Updates 'facet''s used time. Caller is responsible for calling
2562 * facet_push_stats() to update the flows which 'facet' resubmits into. */
2564 facet_update_time(struct ofproto_dpif
*ofproto
, struct facet
*facet
,
2567 if (used
> facet
->used
) {
2569 if (used
> facet
->rule
->used
) {
2570 facet
->rule
->used
= used
;
2572 netflow_flow_update_time(ofproto
->netflow
, &facet
->nf_flow
, used
);
2576 /* Folds the statistics from 'stats' into the counters in 'facet'.
2578 * Because of the meaning of a facet's counters, it only makes sense to do this
2579 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
2580 * packet that was sent by hand or if it represents statistics that have been
2581 * cleared out of the datapath. */
2583 facet_update_stats(struct ofproto_dpif
*ofproto
, struct facet
*facet
,
2584 const struct dpif_flow_stats
*stats
)
2586 if (stats
->n_packets
|| stats
->used
> facet
->used
) {
2587 facet_update_time(ofproto
, facet
, stats
->used
);
2588 facet
->packet_count
+= stats
->n_packets
;
2589 facet
->byte_count
+= stats
->n_bytes
;
2590 facet_push_stats(facet
);
2591 netflow_flow_update_flags(&facet
->nf_flow
, stats
->tcp_flags
);
2596 facet_reset_counters(struct facet
*facet
)
2598 facet
->packet_count
= 0;
2599 facet
->byte_count
= 0;
2600 facet
->rs_packet_count
= 0;
2601 facet
->rs_byte_count
= 0;
2602 facet
->accounted_bytes
= 0;
2606 facet_push_stats(struct facet
*facet
)
2608 uint64_t rs_packets
, rs_bytes
;
2610 assert(facet
->packet_count
>= facet
->rs_packet_count
);
2611 assert(facet
->byte_count
>= facet
->rs_byte_count
);
2612 assert(facet
->used
>= facet
->rs_used
);
2614 rs_packets
= facet
->packet_count
- facet
->rs_packet_count
;
2615 rs_bytes
= facet
->byte_count
- facet
->rs_byte_count
;
2617 if (rs_packets
|| rs_bytes
|| facet
->used
> facet
->rs_used
) {
2618 facet
->rs_packet_count
= facet
->packet_count
;
2619 facet
->rs_byte_count
= facet
->byte_count
;
2620 facet
->rs_used
= facet
->used
;
2622 flow_push_stats(facet
->rule
, &facet
->flow
,
2623 rs_packets
, rs_bytes
, facet
->used
);
2627 struct ofproto_push
{
2628 struct action_xlate_ctx ctx
;
2635 push_resubmit(struct action_xlate_ctx
*ctx
, struct rule_dpif
*rule
)
2637 struct ofproto_push
*push
= CONTAINER_OF(ctx
, struct ofproto_push
, ctx
);
2640 rule
->packet_count
+= push
->packets
;
2641 rule
->byte_count
+= push
->bytes
;
2642 rule
->used
= MAX(push
->used
, rule
->used
);
2646 /* Pushes flow statistics to the rules which 'flow' resubmits into given
2647 * 'rule''s actions. */
2649 flow_push_stats(const struct rule_dpif
*rule
,
2650 struct flow
*flow
, uint64_t packets
, uint64_t bytes
,
2653 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
2654 struct ofproto_push push
;
2656 push
.packets
= packets
;
2660 action_xlate_ctx_init(&push
.ctx
, ofproto
, flow
, NULL
);
2661 push
.ctx
.resubmit_hook
= push_resubmit
;
2662 ofpbuf_delete(xlate_actions(&push
.ctx
,
2663 rule
->up
.actions
, rule
->up
.n_actions
));
2668 static struct rule_dpif
*
2669 rule_dpif_lookup(struct ofproto_dpif
*ofproto
, const struct flow
*flow
,
2672 if (table_id
>= N_TABLES
) {
2676 return rule_dpif_cast(rule_from_cls_rule(
2677 classifier_lookup(&ofproto
->up
.tables
[table_id
],
2682 complete_operation(struct rule_dpif
*rule
)
2684 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
2686 rule_invalidate(rule
);
2688 struct dpif_completion
*c
= xmalloc(sizeof *c
);
2689 c
->op
= rule
->up
.pending
;
2690 list_push_back(&ofproto
->completions
, &c
->list_node
);
2692 ofoperation_complete(rule
->up
.pending
, 0);
2696 static struct rule
*
2699 struct rule_dpif
*rule
= xmalloc(sizeof *rule
);
2704 rule_dealloc(struct rule
*rule_
)
2706 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
2711 rule_construct(struct rule
*rule_
)
2713 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
2714 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
2715 struct rule_dpif
*victim
;
2719 error
= validate_actions(rule
->up
.actions
, rule
->up
.n_actions
,
2720 &rule
->up
.cr
.flow
, ofproto
->max_ports
);
2725 rule
->used
= rule
->up
.created
;
2726 rule
->packet_count
= 0;
2727 rule
->byte_count
= 0;
2729 victim
= rule_dpif_cast(ofoperation_get_victim(rule
->up
.pending
));
2730 if (victim
&& !list_is_empty(&victim
->facets
)) {
2731 struct facet
*facet
;
2733 rule
->facets
= victim
->facets
;
2734 list_moved(&rule
->facets
);
2735 LIST_FOR_EACH (facet
, list_node
, &rule
->facets
) {
2736 /* XXX: We're only clearing our local counters here. It's possible
2737 * that quite a few packets are unaccounted for in the datapath
2738 * statistics. These will be accounted to the new rule instead of
2739 * cleared as required. This could be fixed by clearing out the
2740 * datapath statistics for this facet, but currently it doesn't
2742 facet_reset_counters(facet
);
2746 /* Must avoid list_moved() in this case. */
2747 list_init(&rule
->facets
);
2750 table_id
= rule
->up
.table_id
;
2751 rule
->tag
= (victim
? victim
->tag
2753 : rule_calculate_tag(&rule
->up
.cr
.flow
, &rule
->up
.cr
.wc
,
2754 ofproto
->tables
[table_id
].basis
));
2756 complete_operation(rule
);
2761 rule_destruct(struct rule
*rule_
)
2763 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
2764 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
2765 struct facet
*facet
, *next_facet
;
2767 LIST_FOR_EACH_SAFE (facet
, next_facet
, list_node
, &rule
->facets
) {
2768 facet_revalidate(ofproto
, facet
);
2771 complete_operation(rule
);
2775 rule_get_stats(struct rule
*rule_
, uint64_t *packets
, uint64_t *bytes
)
2777 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
2778 struct facet
*facet
;
2780 /* Start from historical data for 'rule' itself that are no longer tracked
2781 * in facets. This counts, for example, facets that have expired. */
2782 *packets
= rule
->packet_count
;
2783 *bytes
= rule
->byte_count
;
2785 /* Add any statistics that are tracked by facets. This includes
2786 * statistical data recently updated by ofproto_update_stats() as well as
2787 * stats for packets that were executed "by hand" via dpif_execute(). */
2788 LIST_FOR_EACH (facet
, list_node
, &rule
->facets
) {
2789 *packets
+= facet
->packet_count
;
2790 *bytes
+= facet
->byte_count
;
2795 rule_execute(struct rule
*rule_
, struct flow
*flow
, struct ofpbuf
*packet
)
2797 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
2798 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
2799 struct action_xlate_ctx ctx
;
2800 struct ofpbuf
*odp_actions
;
2801 struct facet
*facet
;
2804 /* First look for a related facet. If we find one, account it to that. */
2805 facet
= facet_lookup_valid(ofproto
, flow
);
2806 if (facet
&& facet
->rule
== rule
) {
2807 facet_execute(ofproto
, facet
, packet
);
2811 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
2812 * create a new facet for it and use that. */
2813 if (rule_dpif_lookup(ofproto
, flow
, 0) == rule
) {
2814 facet
= facet_create(rule
, flow
, packet
);
2815 facet_execute(ofproto
, facet
, packet
);
2816 facet_install(ofproto
, facet
, true);
2820 /* We can't account anything to a facet. If we were to try, then that
2821 * facet would have a non-matching rule, busting our invariants. */
2822 action_xlate_ctx_init(&ctx
, ofproto
, flow
, packet
);
2823 odp_actions
= xlate_actions(&ctx
, rule
->up
.actions
, rule
->up
.n_actions
);
2824 size
= packet
->size
;
2825 if (execute_odp_actions(ofproto
, flow
, odp_actions
->data
,
2826 odp_actions
->size
, packet
)) {
2827 rule
->used
= time_msec();
2828 rule
->packet_count
++;
2829 rule
->byte_count
+= size
;
2830 flow_push_stats(rule
, flow
, 1, size
, rule
->used
);
2832 ofpbuf_delete(odp_actions
);
2838 rule_modify_actions(struct rule
*rule_
)
2840 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
2841 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
2844 error
= validate_actions(rule
->up
.actions
, rule
->up
.n_actions
,
2845 &rule
->up
.cr
.flow
, ofproto
->max_ports
);
2847 ofoperation_complete(rule
->up
.pending
, error
);
2851 complete_operation(rule
);
2854 /* Sends 'packet' out of port 'odp_port' within 'p'.
2855 * Returns 0 if successful, otherwise a positive errno value. */
2857 send_packet(struct ofproto_dpif
*ofproto
, uint32_t odp_port
,
2858 const struct ofpbuf
*packet
)
2860 struct ofpbuf key
, odp_actions
;
2861 struct odputil_keybuf keybuf
;
2865 flow_extract((struct ofpbuf
*) packet
, 0, 0, &flow
);
2866 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
2867 odp_flow_key_from_flow(&key
, &flow
);
2869 ofpbuf_init(&odp_actions
, 32);
2870 nl_msg_put_u32(&odp_actions
, OVS_ACTION_ATTR_OUTPUT
, odp_port
);
2871 error
= dpif_execute(ofproto
->dpif
,
2873 odp_actions
.data
, odp_actions
.size
,
2875 ofpbuf_uninit(&odp_actions
);
2878 VLOG_WARN_RL(&rl
, "%s: failed to send packet on port %"PRIu32
" (%s)",
2879 ofproto
->up
.name
, odp_port
, strerror(error
));
2884 /* OpenFlow to datapath action translation. */
2886 static void do_xlate_actions(const union ofp_action
*in
, size_t n_in
,
2887 struct action_xlate_ctx
*ctx
);
2888 static void xlate_normal(struct action_xlate_ctx
*);
2891 commit_odp_actions(struct action_xlate_ctx
*ctx
)
2893 const struct flow
*flow
= &ctx
->flow
;
2894 struct flow
*base
= &ctx
->base_flow
;
2895 struct ofpbuf
*odp_actions
= ctx
->odp_actions
;
2897 if (base
->tun_id
!= flow
->tun_id
) {
2898 nl_msg_put_be64(odp_actions
, OVS_ACTION_ATTR_SET_TUNNEL
, flow
->tun_id
);
2899 base
->tun_id
= flow
->tun_id
;
2902 if (base
->nw_src
!= flow
->nw_src
) {
2903 nl_msg_put_be32(odp_actions
, OVS_ACTION_ATTR_SET_NW_SRC
, flow
->nw_src
);
2904 base
->nw_src
= flow
->nw_src
;
2907 if (base
->nw_dst
!= flow
->nw_dst
) {
2908 nl_msg_put_be32(odp_actions
, OVS_ACTION_ATTR_SET_NW_DST
, flow
->nw_dst
);
2909 base
->nw_dst
= flow
->nw_dst
;
2912 if (base
->nw_tos
!= flow
->nw_tos
) {
2913 nl_msg_put_u8(odp_actions
, OVS_ACTION_ATTR_SET_NW_TOS
, flow
->nw_tos
);
2914 base
->nw_tos
= flow
->nw_tos
;
2917 if (base
->vlan_tci
!= flow
->vlan_tci
) {
2918 if (!(flow
->vlan_tci
& htons(VLAN_CFI
))) {
2919 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
2921 if (base
->vlan_tci
!= htons(0)) {
2922 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
2924 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
2925 flow
->vlan_tci
& ~htons(VLAN_CFI
));
2927 base
->vlan_tci
= flow
->vlan_tci
;
2930 if (base
->tp_src
!= flow
->tp_src
) {
2931 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_SET_TP_SRC
, flow
->tp_src
);
2932 base
->tp_src
= flow
->tp_src
;
2935 if (base
->tp_dst
!= flow
->tp_dst
) {
2936 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_SET_TP_DST
, flow
->tp_dst
);
2937 base
->tp_dst
= flow
->tp_dst
;
2940 if (!eth_addr_equals(base
->dl_src
, flow
->dl_src
)) {
2941 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_SET_DL_SRC
,
2942 flow
->dl_src
, ETH_ADDR_LEN
);
2943 memcpy(base
->dl_src
, flow
->dl_src
, ETH_ADDR_LEN
);
2946 if (!eth_addr_equals(base
->dl_dst
, flow
->dl_dst
)) {
2947 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_SET_DL_DST
,
2948 flow
->dl_dst
, ETH_ADDR_LEN
);
2949 memcpy(base
->dl_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
2952 if (ctx
->base_priority
!= ctx
->priority
) {
2953 if (ctx
->priority
) {
2954 nl_msg_put_u32(odp_actions
, OVS_ACTION_ATTR_SET_PRIORITY
,
2957 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_PRIORITY
);
2959 ctx
->base_priority
= ctx
->priority
;
2964 add_output_action(struct action_xlate_ctx
*ctx
, uint16_t ofp_port
)
2966 const struct ofport_dpif
*ofport
= get_ofp_port(ctx
->ofproto
, ofp_port
);
2967 uint16_t odp_port
= ofp_port_to_odp_port(ofp_port
);
2970 if (ofport
->up
.opp
.config
& htonl(OFPPC_NO_FWD
)) {
2971 /* Forwarding disabled on port. */
2976 * We don't have an ofport record for this port, but it doesn't hurt to
2977 * allow forwarding to it anyhow. Maybe such a port will appear later
2978 * and we're pre-populating the flow table.
2982 commit_odp_actions(ctx
);
2983 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_OUTPUT
, odp_port
);
2984 ctx
->nf_output_iface
= ofp_port
;
2988 xlate_table_action(struct action_xlate_ctx
*ctx
,
2989 uint16_t in_port
, uint8_t table_id
)
2991 if (ctx
->recurse
< MAX_RESUBMIT_RECURSION
) {
2992 struct ofproto_dpif
*ofproto
= ctx
->ofproto
;
2993 struct rule_dpif
*rule
;
2994 uint16_t old_in_port
;
2995 uint8_t old_table_id
;
2997 old_table_id
= ctx
->table_id
;
2998 ctx
->table_id
= table_id
;
3000 /* Look up a flow with 'in_port' as the input port. */
3001 old_in_port
= ctx
->flow
.in_port
;
3002 ctx
->flow
.in_port
= in_port
;
3003 rule
= rule_dpif_lookup(ofproto
, &ctx
->flow
, table_id
);
3006 if (table_id
> 0 && table_id
< N_TABLES
) {
3007 struct table_dpif
*table
= &ofproto
->tables
[table_id
];
3008 if (table
->other_table
) {
3011 : rule_calculate_tag(&ctx
->flow
,
3012 &table
->other_table
->wc
,
3017 /* Restore the original input port. Otherwise OFPP_NORMAL and
3018 * OFPP_IN_PORT will have surprising behavior. */
3019 ctx
->flow
.in_port
= old_in_port
;
3021 if (ctx
->resubmit_hook
) {
3022 ctx
->resubmit_hook(ctx
, rule
);
3027 do_xlate_actions(rule
->up
.actions
, rule
->up
.n_actions
, ctx
);
3031 ctx
->table_id
= old_table_id
;
3033 static struct vlog_rate_limit recurse_rl
= VLOG_RATE_LIMIT_INIT(1, 1);
3035 VLOG_ERR_RL(&recurse_rl
, "resubmit actions recursed over %d times",
3036 MAX_RESUBMIT_RECURSION
);
3041 xlate_resubmit_table(struct action_xlate_ctx
*ctx
,
3042 const struct nx_action_resubmit
*nar
)
3047 in_port
= (nar
->in_port
== htons(OFPP_IN_PORT
)
3049 : ntohs(nar
->in_port
));
3050 table_id
= nar
->table
== 255 ? ctx
->table_id
: nar
->table
;
3052 xlate_table_action(ctx
, in_port
, table_id
);
3056 flood_packets(struct action_xlate_ctx
*ctx
, ovs_be32 mask
)
3058 struct ofport_dpif
*ofport
;
3060 commit_odp_actions(ctx
);
3061 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ctx
->ofproto
->up
.ports
) {
3062 uint16_t ofp_port
= ofport
->up
.ofp_port
;
3063 if (ofp_port
!= ctx
->flow
.in_port
&& !(ofport
->up
.opp
.config
& mask
)) {
3064 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_OUTPUT
,
3069 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
3073 xlate_output_action__(struct action_xlate_ctx
*ctx
,
3074 uint16_t port
, uint16_t max_len
)
3076 uint16_t prev_nf_output_iface
= ctx
->nf_output_iface
;
3078 ctx
->nf_output_iface
= NF_OUT_DROP
;
3082 add_output_action(ctx
, ctx
->flow
.in_port
);
3085 xlate_table_action(ctx
, ctx
->flow
.in_port
, ctx
->table_id
);
3091 flood_packets(ctx
, htonl(OFPPC_NO_FLOOD
));
3094 flood_packets(ctx
, htonl(0));
3096 case OFPP_CONTROLLER
:
3097 commit_odp_actions(ctx
);
3098 nl_msg_put_u64(ctx
->odp_actions
, OVS_ACTION_ATTR_USERSPACE
, max_len
);
3101 add_output_action(ctx
, OFPP_LOCAL
);
3106 if (port
!= ctx
->flow
.in_port
) {
3107 add_output_action(ctx
, port
);
3112 if (prev_nf_output_iface
== NF_OUT_FLOOD
) {
3113 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
3114 } else if (ctx
->nf_output_iface
== NF_OUT_DROP
) {
3115 ctx
->nf_output_iface
= prev_nf_output_iface
;
3116 } else if (prev_nf_output_iface
!= NF_OUT_DROP
&&
3117 ctx
->nf_output_iface
!= NF_OUT_FLOOD
) {
3118 ctx
->nf_output_iface
= NF_OUT_MULTI
;
3123 xlate_output_reg_action(struct action_xlate_ctx
*ctx
,
3124 const struct nx_action_output_reg
*naor
)
3128 ofp_port
= nxm_read_field_bits(naor
->src
, naor
->ofs_nbits
, &ctx
->flow
);
3130 if (ofp_port
<= UINT16_MAX
) {
3131 xlate_output_action__(ctx
, ofp_port
, ntohs(naor
->max_len
));
3136 xlate_output_action(struct action_xlate_ctx
*ctx
,
3137 const struct ofp_action_output
*oao
)
3139 xlate_output_action__(ctx
, ntohs(oao
->port
), ntohs(oao
->max_len
));
3143 xlate_enqueue_action(struct action_xlate_ctx
*ctx
,
3144 const struct ofp_action_enqueue
*oae
)
3146 uint16_t ofp_port
, odp_port
;
3147 uint32_t ctx_priority
, priority
;
3150 error
= dpif_queue_to_priority(ctx
->ofproto
->dpif
, ntohl(oae
->queue_id
),
3153 /* Fall back to ordinary output action. */
3154 xlate_output_action__(ctx
, ntohs(oae
->port
), 0);
3158 /* Figure out datapath output port. */
3159 ofp_port
= ntohs(oae
->port
);
3160 if (ofp_port
== OFPP_IN_PORT
) {
3161 ofp_port
= ctx
->flow
.in_port
;
3163 odp_port
= ofp_port_to_odp_port(ofp_port
);
3165 /* Add datapath actions. */
3166 ctx_priority
= ctx
->priority
;
3167 ctx
->priority
= priority
;
3168 add_output_action(ctx
, odp_port
);
3169 ctx
->priority
= ctx_priority
;
3171 /* Update NetFlow output port. */
3172 if (ctx
->nf_output_iface
== NF_OUT_DROP
) {
3173 ctx
->nf_output_iface
= odp_port
;
3174 } else if (ctx
->nf_output_iface
!= NF_OUT_FLOOD
) {
3175 ctx
->nf_output_iface
= NF_OUT_MULTI
;
3180 xlate_set_queue_action(struct action_xlate_ctx
*ctx
,
3181 const struct nx_action_set_queue
*nasq
)
3186 error
= dpif_queue_to_priority(ctx
->ofproto
->dpif
, ntohl(nasq
->queue_id
),
3189 /* Couldn't translate queue to a priority, so ignore. A warning
3190 * has already been logged. */
3194 ctx
->priority
= priority
;
3197 struct xlate_reg_state
{
3203 xlate_autopath(struct action_xlate_ctx
*ctx
,
3204 const struct nx_action_autopath
*naa
)
3206 uint16_t ofp_port
= ntohl(naa
->id
);
3207 struct ofport_dpif
*port
= get_ofp_port(ctx
->ofproto
, ofp_port
);
3209 if (!port
|| !port
->bundle
) {
3210 ofp_port
= OFPP_NONE
;
3211 } else if (port
->bundle
->bond
) {
3212 /* Autopath does not support VLAN hashing. */
3213 struct ofport_dpif
*slave
= bond_choose_output_slave(
3214 port
->bundle
->bond
, &ctx
->flow
, OFP_VLAN_NONE
, &ctx
->tags
);
3216 ofp_port
= slave
->up
.ofp_port
;
3219 autopath_execute(naa
, &ctx
->flow
, ofp_port
);
3223 slave_enabled_cb(uint16_t ofp_port
, void *ofproto_
)
3225 struct ofproto_dpif
*ofproto
= ofproto_
;
3226 struct ofport_dpif
*port
;
3236 case OFPP_CONTROLLER
: /* Not supported by the bundle action. */
3239 port
= get_ofp_port(ofproto
, ofp_port
);
3240 return port
? port
->may_enable
: false;
3245 xlate_learn_action(struct action_xlate_ctx
*ctx
,
3246 const struct nx_action_learn
*learn
)
3248 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
3249 struct ofputil_flow_mod fm
;
3252 learn_execute(learn
, &ctx
->flow
, &fm
);
3254 error
= ofproto_flow_mod(&ctx
->ofproto
->up
, &fm
);
3255 if (error
&& !VLOG_DROP_WARN(&rl
)) {
3256 char *msg
= ofputil_error_to_string(error
);
3257 VLOG_WARN("learning action failed to modify flow table (%s)", msg
);
3265 do_xlate_actions(const union ofp_action
*in
, size_t n_in
,
3266 struct action_xlate_ctx
*ctx
)
3268 const struct ofport_dpif
*port
;
3269 const union ofp_action
*ia
;
3272 port
= get_ofp_port(ctx
->ofproto
, ctx
->flow
.in_port
);
3274 && port
->up
.opp
.config
& htonl(OFPPC_NO_RECV
| OFPPC_NO_RECV_STP
) &&
3275 port
->up
.opp
.config
& (eth_addr_equals(ctx
->flow
.dl_dst
, eth_addr_stp
)
3276 ? htonl(OFPPC_NO_RECV_STP
)
3277 : htonl(OFPPC_NO_RECV
))) {
3278 /* Drop this flow. */
3282 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia
, left
, in
, n_in
) {
3283 const struct ofp_action_dl_addr
*oada
;
3284 const struct nx_action_resubmit
*nar
;
3285 const struct nx_action_set_tunnel
*nast
;
3286 const struct nx_action_set_queue
*nasq
;
3287 const struct nx_action_multipath
*nam
;
3288 const struct nx_action_autopath
*naa
;
3289 const struct nx_action_bundle
*nab
;
3290 const struct nx_action_output_reg
*naor
;
3291 enum ofputil_action_code code
;
3294 code
= ofputil_decode_action_unsafe(ia
);
3296 case OFPUTIL_OFPAT_OUTPUT
:
3297 xlate_output_action(ctx
, &ia
->output
);
3300 case OFPUTIL_OFPAT_SET_VLAN_VID
:
3301 ctx
->flow
.vlan_tci
&= ~htons(VLAN_VID_MASK
);
3302 ctx
->flow
.vlan_tci
|= ia
->vlan_vid
.vlan_vid
| htons(VLAN_CFI
);
3305 case OFPUTIL_OFPAT_SET_VLAN_PCP
:
3306 ctx
->flow
.vlan_tci
&= ~htons(VLAN_PCP_MASK
);
3307 ctx
->flow
.vlan_tci
|= htons(
3308 (ia
->vlan_pcp
.vlan_pcp
<< VLAN_PCP_SHIFT
) | VLAN_CFI
);
3311 case OFPUTIL_OFPAT_STRIP_VLAN
:
3312 ctx
->flow
.vlan_tci
= htons(0);
3315 case OFPUTIL_OFPAT_SET_DL_SRC
:
3316 oada
= ((struct ofp_action_dl_addr
*) ia
);
3317 memcpy(ctx
->flow
.dl_src
, oada
->dl_addr
, ETH_ADDR_LEN
);
3320 case OFPUTIL_OFPAT_SET_DL_DST
:
3321 oada
= ((struct ofp_action_dl_addr
*) ia
);
3322 memcpy(ctx
->flow
.dl_dst
, oada
->dl_addr
, ETH_ADDR_LEN
);
3325 case OFPUTIL_OFPAT_SET_NW_SRC
:
3326 ctx
->flow
.nw_src
= ia
->nw_addr
.nw_addr
;
3329 case OFPUTIL_OFPAT_SET_NW_DST
:
3330 ctx
->flow
.nw_dst
= ia
->nw_addr
.nw_addr
;
3333 case OFPUTIL_OFPAT_SET_NW_TOS
:
3334 ctx
->flow
.nw_tos
= ia
->nw_tos
.nw_tos
& IP_DSCP_MASK
;
3337 case OFPUTIL_OFPAT_SET_TP_SRC
:
3338 ctx
->flow
.tp_src
= ia
->tp_port
.tp_port
;
3341 case OFPUTIL_OFPAT_SET_TP_DST
:
3342 ctx
->flow
.tp_dst
= ia
->tp_port
.tp_port
;
3345 case OFPUTIL_OFPAT_ENQUEUE
:
3346 xlate_enqueue_action(ctx
, (const struct ofp_action_enqueue
*) ia
);
3349 case OFPUTIL_NXAST_RESUBMIT
:
3350 nar
= (const struct nx_action_resubmit
*) ia
;
3351 xlate_table_action(ctx
, ntohs(nar
->in_port
), ctx
->table_id
);
3354 case OFPUTIL_NXAST_RESUBMIT_TABLE
:
3355 xlate_resubmit_table(ctx
, (const struct nx_action_resubmit
*) ia
);
3358 case OFPUTIL_NXAST_SET_TUNNEL
:
3359 nast
= (const struct nx_action_set_tunnel
*) ia
;
3360 tun_id
= htonll(ntohl(nast
->tun_id
));
3361 ctx
->flow
.tun_id
= tun_id
;
3364 case OFPUTIL_NXAST_SET_QUEUE
:
3365 nasq
= (const struct nx_action_set_queue
*) ia
;
3366 xlate_set_queue_action(ctx
, nasq
);
3369 case OFPUTIL_NXAST_POP_QUEUE
:
3373 case OFPUTIL_NXAST_REG_MOVE
:
3374 nxm_execute_reg_move((const struct nx_action_reg_move
*) ia
,
3378 case OFPUTIL_NXAST_REG_LOAD
:
3379 nxm_execute_reg_load((const struct nx_action_reg_load
*) ia
,
3383 case OFPUTIL_NXAST_NOTE
:
3384 /* Nothing to do. */
3387 case OFPUTIL_NXAST_SET_TUNNEL64
:
3388 tun_id
= ((const struct nx_action_set_tunnel64
*) ia
)->tun_id
;
3389 ctx
->flow
.tun_id
= tun_id
;
3392 case OFPUTIL_NXAST_MULTIPATH
:
3393 nam
= (const struct nx_action_multipath
*) ia
;
3394 multipath_execute(nam
, &ctx
->flow
);
3397 case OFPUTIL_NXAST_AUTOPATH
:
3398 naa
= (const struct nx_action_autopath
*) ia
;
3399 xlate_autopath(ctx
, naa
);
3402 case OFPUTIL_NXAST_BUNDLE
:
3403 ctx
->ofproto
->has_bundle_action
= true;
3404 nab
= (const struct nx_action_bundle
*) ia
;
3405 xlate_output_action__(ctx
, bundle_execute(nab
, &ctx
->flow
,
3410 case OFPUTIL_NXAST_BUNDLE_LOAD
:
3411 ctx
->ofproto
->has_bundle_action
= true;
3412 nab
= (const struct nx_action_bundle
*) ia
;
3413 bundle_execute_load(nab
, &ctx
->flow
, slave_enabled_cb
,
3417 case OFPUTIL_NXAST_OUTPUT_REG
:
3418 naor
= (const struct nx_action_output_reg
*) ia
;
3419 xlate_output_reg_action(ctx
, naor
);
3422 case OFPUTIL_NXAST_LEARN
:
3423 ctx
->has_learn
= true;
3424 if (ctx
->may_learn
) {
3425 xlate_learn_action(ctx
, (const struct nx_action_learn
*) ia
);
3433 action_xlate_ctx_init(struct action_xlate_ctx
*ctx
,
3434 struct ofproto_dpif
*ofproto
, const struct flow
*flow
,
3435 const struct ofpbuf
*packet
)
3437 ctx
->ofproto
= ofproto
;
3439 ctx
->packet
= packet
;
3440 ctx
->may_learn
= packet
!= NULL
;
3441 ctx
->resubmit_hook
= NULL
;
3444 static struct ofpbuf
*
3445 xlate_actions(struct action_xlate_ctx
*ctx
,
3446 const union ofp_action
*in
, size_t n_in
)
3448 COVERAGE_INC(ofproto_dpif_xlate
);
3450 ctx
->odp_actions
= ofpbuf_new(512);
3452 ctx
->may_set_up_flow
= true;
3453 ctx
->has_learn
= false;
3454 ctx
->has_normal
= false;
3455 ctx
->nf_output_iface
= NF_OUT_DROP
;
3458 ctx
->base_priority
= 0;
3459 ctx
->base_flow
= ctx
->flow
;
3460 ctx
->base_flow
.tun_id
= 0;
3463 if (process_special(ctx
->ofproto
, &ctx
->flow
, ctx
->packet
)) {
3464 ctx
->may_set_up_flow
= false;
3466 do_xlate_actions(in
, n_in
, ctx
);
3469 /* Check with in-band control to see if we're allowed to set up this
3471 if (!connmgr_may_set_up_flow(ctx
->ofproto
->up
.connmgr
, &ctx
->flow
,
3472 ctx
->odp_actions
->data
,
3473 ctx
->odp_actions
->size
)) {
3474 ctx
->may_set_up_flow
= false;
3477 return ctx
->odp_actions
;
3480 /* OFPP_NORMAL implementation. */
3483 struct ofport_dpif
*port
;
3488 struct dst builtin
[32];
3490 size_t n
, allocated
;
3493 static void dst_set_init(struct dst_set
*);
3494 static void dst_set_add(struct dst_set
*, const struct dst
*);
3495 static void dst_set_free(struct dst_set
*);
3497 static struct ofport_dpif
*ofbundle_get_a_port(const struct ofbundle
*);
3500 set_dst(struct action_xlate_ctx
*ctx
, struct dst
*dst
,
3501 const struct ofbundle
*in_bundle
, const struct ofbundle
*out_bundle
)
3503 dst
->vlan
= (out_bundle
->vlan
>= 0 ? OFP_VLAN_NONE
3504 : in_bundle
->vlan
>= 0 ? in_bundle
->vlan
3505 : ctx
->flow
.vlan_tci
== 0 ? OFP_VLAN_NONE
3506 : vlan_tci_to_vid(ctx
->flow
.vlan_tci
));
3508 dst
->port
= (!out_bundle
->bond
3509 ? ofbundle_get_a_port(out_bundle
)
3510 : bond_choose_output_slave(out_bundle
->bond
, &ctx
->flow
,
3511 dst
->vlan
, &ctx
->tags
));
3513 return dst
->port
!= NULL
;
3517 mirror_mask_ffs(mirror_mask_t mask
)
3519 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask
));
3524 dst_set_init(struct dst_set
*set
)
3526 set
->dsts
= set
->builtin
;
3528 set
->allocated
= ARRAY_SIZE(set
->builtin
);
3532 dst_set_add(struct dst_set
*set
, const struct dst
*dst
)
3534 if (set
->n
>= set
->allocated
) {
3535 size_t new_allocated
;
3536 struct dst
*new_dsts
;
3538 new_allocated
= set
->allocated
* 2;
3539 new_dsts
= xmalloc(new_allocated
* sizeof *new_dsts
);
3540 memcpy(new_dsts
, set
->dsts
, set
->n
* sizeof *new_dsts
);
3544 set
->dsts
= new_dsts
;
3545 set
->allocated
= new_allocated
;
3547 set
->dsts
[set
->n
++] = *dst
;
3551 dst_set_free(struct dst_set
*set
)
3553 if (set
->dsts
!= set
->builtin
) {
3559 dst_is_duplicate(const struct dst_set
*set
, const struct dst
*test
)
3562 for (i
= 0; i
< set
->n
; i
++) {
3563 if (set
->dsts
[i
].vlan
== test
->vlan
3564 && set
->dsts
[i
].port
== test
->port
) {
3572 ofbundle_trunks_vlan(const struct ofbundle
*bundle
, uint16_t vlan
)
3574 return (bundle
->vlan
< 0
3575 && (!bundle
->trunks
|| bitmap_is_set(bundle
->trunks
, vlan
)));
3579 ofbundle_includes_vlan(const struct ofbundle
*bundle
, uint16_t vlan
)
3581 return vlan
== bundle
->vlan
|| ofbundle_trunks_vlan(bundle
, vlan
);
3584 /* Returns an arbitrary interface within 'bundle'. */
3585 static struct ofport_dpif
*
3586 ofbundle_get_a_port(const struct ofbundle
*bundle
)
3588 return CONTAINER_OF(list_front(&bundle
->ports
),
3589 struct ofport_dpif
, bundle_node
);
3593 compose_dsts(struct action_xlate_ctx
*ctx
, uint16_t vlan
,
3594 const struct ofbundle
*in_bundle
,
3595 const struct ofbundle
*out_bundle
, struct dst_set
*set
)
3599 if (out_bundle
== OFBUNDLE_FLOOD
) {
3600 struct ofbundle
*bundle
;
3602 HMAP_FOR_EACH (bundle
, hmap_node
, &ctx
->ofproto
->bundles
) {
3603 if (bundle
!= in_bundle
3604 && ofbundle_includes_vlan(bundle
, vlan
)
3605 && bundle
->floodable
3606 && !bundle
->mirror_out
3607 && set_dst(ctx
, &dst
, in_bundle
, bundle
)) {
3608 dst_set_add(set
, &dst
);
3611 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
3612 } else if (out_bundle
&& set_dst(ctx
, &dst
, in_bundle
, out_bundle
)) {
3613 dst_set_add(set
, &dst
);
3614 ctx
->nf_output_iface
= dst
.port
->odp_port
;
3619 vlan_is_mirrored(const struct ofmirror
*m
, int vlan
)
3621 return !m
->vlans
|| bitmap_is_set(m
->vlans
, vlan
);
3624 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
3625 * to a VLAN. In general most packets may be mirrored but we want to drop
3626 * protocols that may confuse switches. */
3628 eth_dst_may_rspan(const uint8_t dst
[ETH_ADDR_LEN
])
3630 /* If you change this function's behavior, please update corresponding
3631 * documentation in vswitch.xml at the same time. */
3632 if (dst
[0] != 0x01) {
3633 /* All the currently banned MACs happen to start with 01 currently, so
3634 * this is a quick way to eliminate most of the good ones. */
3636 if (eth_addr_is_reserved(dst
)) {
3637 /* Drop STP, IEEE pause frames, and other reserved protocols
3638 * (01-80-c2-00-00-0x). */
3642 if (dst
[0] == 0x01 && dst
[1] == 0x00 && dst
[2] == 0x0c) {
3644 if ((dst
[3] & 0xfe) == 0xcc &&
3645 (dst
[4] & 0xfe) == 0xcc &&
3646 (dst
[5] & 0xfe) == 0xcc) {
3647 /* Drop the following protocols plus others following the same
3650 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
3651 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
3652 STP Uplink Fast (01-00-0c-cd-cd-cd) */
3656 if (!(dst
[3] | dst
[4] | dst
[5])) {
3657 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
3666 compose_mirror_dsts(struct action_xlate_ctx
*ctx
,
3667 uint16_t vlan
, const struct ofbundle
*in_bundle
,
3668 struct dst_set
*set
)
3670 struct ofproto_dpif
*ofproto
= ctx
->ofproto
;
3671 mirror_mask_t mirrors
;
3675 mirrors
= in_bundle
->src_mirrors
;
3676 for (i
= 0; i
< set
->n
; i
++) {
3677 mirrors
|= set
->dsts
[i
].port
->bundle
->dst_mirrors
;
3684 flow_vlan
= vlan_tci_to_vid(ctx
->flow
.vlan_tci
);
3685 if (flow_vlan
== 0) {
3686 flow_vlan
= OFP_VLAN_NONE
;
3690 struct ofmirror
*m
= ofproto
->mirrors
[mirror_mask_ffs(mirrors
) - 1];
3691 if (vlan_is_mirrored(m
, vlan
)) {
3695 if (set_dst(ctx
, &dst
, in_bundle
, m
->out
)
3696 && !dst_is_duplicate(set
, &dst
)) {
3697 dst_set_add(set
, &dst
);
3699 } else if (eth_dst_may_rspan(ctx
->flow
.dl_dst
)) {
3700 struct ofbundle
*bundle
;
3702 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
3703 if (ofbundle_includes_vlan(bundle
, m
->out_vlan
)
3704 && set_dst(ctx
, &dst
, in_bundle
, bundle
))
3706 if (bundle
->vlan
< 0) {
3707 dst
.vlan
= m
->out_vlan
;
3709 if (dst_is_duplicate(set
, &dst
)) {
3713 /* Use the vlan tag on the original flow instead of
3714 * the one passed in the vlan parameter. This ensures
3715 * that we compare the vlan from before any implicit
3716 * tagging tags place. This is necessary because
3717 * dst->vlan is the final vlan, after removing implicit
3719 if (bundle
== in_bundle
&& dst
.vlan
== flow_vlan
) {
3720 /* Don't send out input port on same VLAN. */
3723 dst_set_add(set
, &dst
);
3728 mirrors
&= mirrors
- 1;
3733 compose_actions(struct action_xlate_ctx
*ctx
, uint16_t vlan
,
3734 const struct ofbundle
*in_bundle
,
3735 const struct ofbundle
*out_bundle
)
3737 uint16_t initial_vlan
, cur_vlan
;
3738 const struct dst
*dst
;
3742 compose_dsts(ctx
, vlan
, in_bundle
, out_bundle
, &set
);
3743 compose_mirror_dsts(ctx
, vlan
, in_bundle
, &set
);
3745 /* Output all the packets we can without having to change the VLAN. */
3746 initial_vlan
= vlan_tci_to_vid(ctx
->flow
.vlan_tci
);
3747 if (initial_vlan
== 0) {
3748 initial_vlan
= OFP_VLAN_NONE
;
3750 for (dst
= set
.dsts
; dst
< &set
.dsts
[set
.n
]; dst
++) {
3751 if (dst
->vlan
!= initial_vlan
) {
3754 nl_msg_put_u32(ctx
->odp_actions
,
3755 OVS_ACTION_ATTR_OUTPUT
, dst
->port
->odp_port
);
3758 /* Then output the rest. */
3759 cur_vlan
= initial_vlan
;
3760 for (dst
= set
.dsts
; dst
< &set
.dsts
[set
.n
]; dst
++) {
3761 if (dst
->vlan
== initial_vlan
) {
3764 if (dst
->vlan
!= cur_vlan
) {
3765 if (dst
->vlan
== OFP_VLAN_NONE
) {
3766 nl_msg_put_flag(ctx
->odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
3770 if (cur_vlan
!= OFP_VLAN_NONE
) {
3771 nl_msg_put_flag(ctx
->odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
3773 tci
= htons(dst
->vlan
& VLAN_VID_MASK
);
3774 tci
|= ctx
->flow
.vlan_tci
& htons(VLAN_PCP_MASK
);
3775 nl_msg_put_be16(ctx
->odp_actions
,
3776 OVS_ACTION_ATTR_PUSH_VLAN
, tci
);
3778 cur_vlan
= dst
->vlan
;
3780 nl_msg_put_u32(ctx
->odp_actions
,
3781 OVS_ACTION_ATTR_OUTPUT
, dst
->port
->odp_port
);
3787 /* Returns the effective vlan of a packet, taking into account both the
3788 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
3789 * the packet is untagged and -1 indicates it has an invalid header and
3790 * should be dropped. */
3792 flow_get_vlan(struct ofproto_dpif
*ofproto
, const struct flow
*flow
,
3793 struct ofbundle
*in_bundle
, bool have_packet
)
3795 int vlan
= vlan_tci_to_vid(flow
->vlan_tci
);
3796 if (in_bundle
->vlan
>= 0) {
3799 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3800 VLOG_WARN_RL(&rl
, "bridge %s: dropping VLAN %d tagged "
3801 "packet received on port %s configured with "
3802 "implicit VLAN %"PRIu16
,
3803 ofproto
->up
.name
, vlan
,
3804 in_bundle
->name
, in_bundle
->vlan
);
3808 vlan
= in_bundle
->vlan
;
3810 if (!ofbundle_includes_vlan(in_bundle
, vlan
)) {
3812 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3813 VLOG_WARN_RL(&rl
, "bridge %s: dropping VLAN %d tagged "
3814 "packet received on port %s not configured for "
3816 ofproto
->up
.name
, vlan
, in_bundle
->name
, vlan
);
3825 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
3826 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
3827 * indicate this; newer upstream kernels use gratuitous ARP requests. */
3829 is_gratuitous_arp(const struct flow
*flow
)
3831 return (flow
->dl_type
== htons(ETH_TYPE_ARP
)
3832 && eth_addr_is_broadcast(flow
->dl_dst
)
3833 && (flow
->nw_proto
== ARP_OP_REPLY
3834 || (flow
->nw_proto
== ARP_OP_REQUEST
3835 && flow
->nw_src
== flow
->nw_dst
)));
3839 update_learning_table(struct ofproto_dpif
*ofproto
,
3840 const struct flow
*flow
, int vlan
,
3841 struct ofbundle
*in_bundle
)
3843 struct mac_entry
*mac
;
3845 if (!mac_learning_may_learn(ofproto
->ml
, flow
->dl_src
, vlan
)) {
3849 mac
= mac_learning_insert(ofproto
->ml
, flow
->dl_src
, vlan
);
3850 if (is_gratuitous_arp(flow
)) {
3851 /* We don't want to learn from gratuitous ARP packets that are
3852 * reflected back over bond slaves so we lock the learning table. */
3853 if (!in_bundle
->bond
) {
3854 mac_entry_set_grat_arp_lock(mac
);
3855 } else if (mac_entry_is_grat_arp_locked(mac
)) {
3860 if (mac_entry_is_new(mac
) || mac
->port
.p
!= in_bundle
) {
3861 /* The log messages here could actually be useful in debugging,
3862 * so keep the rate limit relatively high. */
3863 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(30, 300);
3864 VLOG_DBG_RL(&rl
, "bridge %s: learned that "ETH_ADDR_FMT
" is "
3865 "on port %s in VLAN %d",
3866 ofproto
->up
.name
, ETH_ADDR_ARGS(flow
->dl_src
),
3867 in_bundle
->name
, vlan
);
3869 mac
->port
.p
= in_bundle
;
3870 tag_set_add(&ofproto
->revalidate_set
,
3871 mac_learning_changed(ofproto
->ml
, mac
));
3875 /* Determines whether packets in 'flow' within 'br' should be forwarded or
3876 * dropped. Returns true if they may be forwarded, false if they should be
3879 * If 'have_packet' is true, it indicates that the caller is processing a
3880 * received packet. If 'have_packet' is false, then the caller is just
3881 * revalidating an existing flow because configuration has changed. Either
3882 * way, 'have_packet' only affects logging (there is no point in logging errors
3883 * during revalidation).
3885 * Sets '*in_portp' to the input port. This will be a null pointer if
3886 * flow->in_port does not designate a known input port (in which case
3887 * is_admissible() returns false).
3889 * When returning true, sets '*vlanp' to the effective VLAN of the input
3890 * packet, as returned by flow_get_vlan().
3892 * May also add tags to '*tags', although the current implementation only does
3893 * so in one special case.
3896 is_admissible(struct ofproto_dpif
*ofproto
, const struct flow
*flow
,
3898 tag_type
*tags
, int *vlanp
, struct ofbundle
**in_bundlep
)
3900 struct ofport_dpif
*in_port
;
3901 struct ofbundle
*in_bundle
;
3904 /* Find the port and bundle for the received packet. */
3905 in_port
= get_ofp_port(ofproto
, flow
->in_port
);
3906 *in_bundlep
= in_bundle
= in_port
? in_port
->bundle
: NULL
;
3907 if (!in_port
|| !in_bundle
) {
3908 /* No interface? Something fishy... */
3910 /* Odd. A few possible reasons here:
3912 * - We deleted a port but there are still a few packets queued up
3915 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
3916 * we don't know about.
3918 * - Packet arrived on the local port but the local port is not
3921 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3923 VLOG_WARN_RL(&rl
, "bridge %s: received packet on unknown "
3925 ofproto
->up
.name
, flow
->in_port
);
3930 *vlanp
= vlan
= flow_get_vlan(ofproto
, flow
, in_bundle
, have_packet
);
3935 /* Drop frames for reserved multicast addresses
3936 * only if forward_bpdu option is absent. */
3937 if (eth_addr_is_reserved(flow
->dl_dst
) &&
3938 !ofproto
->up
.forward_bpdu
) {
3942 /* Drop frames on bundles reserved for mirroring. */
3943 if (in_bundle
->mirror_out
) {
3945 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3946 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet received on port "
3947 "%s, which is reserved exclusively for mirroring",
3948 ofproto
->up
.name
, in_bundle
->name
);
3953 if (in_bundle
->bond
) {
3954 struct mac_entry
*mac
;
3956 switch (bond_check_admissibility(in_bundle
->bond
, in_port
,
3957 flow
->dl_dst
, tags
)) {
3964 case BV_DROP_IF_MOVED
:
3965 mac
= mac_learning_lookup(ofproto
->ml
, flow
->dl_src
, vlan
, NULL
);
3966 if (mac
&& mac
->port
.p
!= in_bundle
&&
3967 (!is_gratuitous_arp(flow
)
3968 || mac_entry_is_grat_arp_locked(mac
))) {
3979 xlate_normal(struct action_xlate_ctx
*ctx
)
3981 struct ofbundle
*in_bundle
;
3982 struct ofbundle
*out_bundle
;
3983 struct mac_entry
*mac
;
3986 ctx
->has_normal
= true;
3988 /* Check whether we should drop packets in this flow. */
3989 if (!is_admissible(ctx
->ofproto
, &ctx
->flow
, ctx
->packet
!= NULL
,
3990 &ctx
->tags
, &vlan
, &in_bundle
)) {
3995 /* Learn source MAC. */
3996 if (ctx
->may_learn
) {
3997 update_learning_table(ctx
->ofproto
, &ctx
->flow
, vlan
, in_bundle
);
4000 /* Determine output bundle. */
4001 mac
= mac_learning_lookup(ctx
->ofproto
->ml
, ctx
->flow
.dl_dst
, vlan
,
4004 out_bundle
= mac
->port
.p
;
4005 } else if (!ctx
->packet
&& !eth_addr_is_multicast(ctx
->flow
.dl_dst
)) {
4006 /* If we are revalidating but don't have a learning entry then eject
4007 * the flow. Installing a flow that floods packets opens up a window
4008 * of time where we could learn from a packet reflected on a bond and
4009 * blackhole packets before the learning table is updated to reflect
4010 * the correct port. */
4011 ctx
->may_set_up_flow
= false;
4014 out_bundle
= OFBUNDLE_FLOOD
;
4017 /* Don't send packets out their input bundles. */
4018 if (in_bundle
== out_bundle
) {
4024 compose_actions(ctx
, vlan
, in_bundle
, out_bundle
);
4028 /* Optimized flow revalidation.
4030 * It's a difficult problem, in general, to tell which facets need to have
4031 * their actions recalculated whenever the OpenFlow flow table changes. We
4032 * don't try to solve that general problem: for most kinds of OpenFlow flow
4033 * table changes, we recalculate the actions for every facet. This is
4034 * relatively expensive, but it's good enough if the OpenFlow flow table
4035 * doesn't change very often.
4037 * However, we can expect one particular kind of OpenFlow flow table change to
4038 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
4039 * of CPU on revalidating every facet whenever MAC learning modifies the flow
4040 * table, we add a special case that applies to flow tables in which every rule
4041 * has the same form (that is, the same wildcards), except that the table is
4042 * also allowed to have a single "catch-all" flow that matches all packets. We
4043 * optimize this case by tagging all of the facets that resubmit into the table
4044 * and invalidating the same tag whenever a flow changes in that table. The
4045 * end result is that we revalidate just the facets that need it (and sometimes
4046 * a few more, but not all of the facets or even all of the facets that
4047 * resubmit to the table modified by MAC learning). */
4049 /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
4050 * into an OpenFlow table with the given 'basis'. */
4052 rule_calculate_tag(const struct flow
*flow
, const struct flow_wildcards
*wc
,
4055 if (flow_wildcards_is_catchall(wc
)) {
4058 struct flow tag_flow
= *flow
;
4059 flow_zero_wildcards(&tag_flow
, wc
);
4060 return tag_create_deterministic(flow_hash(&tag_flow
, secret
));
4064 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
4065 * taggability of that table.
4067 * This function must be called after *each* change to a flow table. If you
4068 * skip calling it on some changes then the pointer comparisons at the end can
4069 * be invalid if you get unlucky. For example, if a flow removal causes a
4070 * cls_table to be destroyed and then a flow insertion causes a cls_table with
4071 * different wildcards to be created with the same address, then this function
4072 * will incorrectly skip revalidation. */
4074 table_update_taggable(struct ofproto_dpif
*ofproto
, uint8_t table_id
)
4076 struct table_dpif
*table
= &ofproto
->tables
[table_id
];
4077 const struct classifier
*cls
= &ofproto
->up
.tables
[table_id
];
4078 struct cls_table
*catchall
, *other
;
4079 struct cls_table
*t
;
4081 catchall
= other
= NULL
;
4083 switch (hmap_count(&cls
->tables
)) {
4085 /* We could tag this OpenFlow table but it would make the logic a
4086 * little harder and it's a corner case that doesn't seem worth it
4092 HMAP_FOR_EACH (t
, hmap_node
, &cls
->tables
) {
4093 if (cls_table_is_catchall(t
)) {
4095 } else if (!other
) {
4098 /* Indicate that we can't tag this by setting both tables to
4099 * NULL. (We know that 'catchall' is already NULL.) */
4106 /* Can't tag this table. */
4110 if (table
->catchall_table
!= catchall
|| table
->other_table
!= other
) {
4111 table
->catchall_table
= catchall
;
4112 table
->other_table
= other
;
4113 ofproto
->need_revalidate
= true;
4117 /* Given 'rule' that has changed in some way (either it is a rule being
4118 * inserted, a rule being deleted, or a rule whose actions are being
4119 * modified), marks facets for revalidation to ensure that packets will be
4120 * forwarded correctly according to the new state of the flow table.
4122 * This function must be called after *each* change to a flow table. See
4123 * the comment on table_update_taggable() for more information. */
4125 rule_invalidate(const struct rule_dpif
*rule
)
4127 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
4129 table_update_taggable(ofproto
, rule
->up
.table_id
);
4131 if (!ofproto
->need_revalidate
) {
4132 struct table_dpif
*table
= &ofproto
->tables
[rule
->up
.table_id
];
4134 if (table
->other_table
&& rule
->tag
) {
4135 tag_set_add(&ofproto
->revalidate_set
, rule
->tag
);
4137 ofproto
->need_revalidate
= true;
4143 get_drop_frags(struct ofproto
*ofproto_
)
4145 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4148 dpif_get_drop_frags(ofproto
->dpif
, &drop_frags
);
4153 set_drop_frags(struct ofproto
*ofproto_
, bool drop_frags
)
4155 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4157 dpif_set_drop_frags(ofproto
->dpif
, drop_frags
);
4161 packet_out(struct ofproto
*ofproto_
, struct ofpbuf
*packet
,
4162 const struct flow
*flow
,
4163 const union ofp_action
*ofp_actions
, size_t n_ofp_actions
)
4165 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4168 error
= validate_actions(ofp_actions
, n_ofp_actions
, flow
,
4169 ofproto
->max_ports
);
4171 struct odputil_keybuf keybuf
;
4172 struct action_xlate_ctx ctx
;
4173 struct ofpbuf
*odp_actions
;
4176 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
4177 odp_flow_key_from_flow(&key
, flow
);
4179 action_xlate_ctx_init(&ctx
, ofproto
, flow
, packet
);
4180 odp_actions
= xlate_actions(&ctx
, ofp_actions
, n_ofp_actions
);
4181 dpif_execute(ofproto
->dpif
, key
.data
, key
.size
,
4182 odp_actions
->data
, odp_actions
->size
, packet
);
4183 ofpbuf_delete(odp_actions
);
4189 get_netflow_ids(const struct ofproto
*ofproto_
,
4190 uint8_t *engine_type
, uint8_t *engine_id
)
4192 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4194 dpif_get_netflow_ids(ofproto
->dpif
, engine_type
, engine_id
);
4197 static struct ofproto_dpif
*
4198 ofproto_dpif_lookup(const char *name
)
4200 struct ofproto
*ofproto
= ofproto_lookup(name
);
4201 return (ofproto
&& ofproto
->ofproto_class
== &ofproto_dpif_class
4202 ? ofproto_dpif_cast(ofproto
)
4207 ofproto_unixctl_fdb_show(struct unixctl_conn
*conn
,
4208 const char *args
, void *aux OVS_UNUSED
)
4210 struct ds ds
= DS_EMPTY_INITIALIZER
;
4211 const struct ofproto_dpif
*ofproto
;
4212 const struct mac_entry
*e
;
4214 ofproto
= ofproto_dpif_lookup(args
);
4216 unixctl_command_reply(conn
, 501, "no such bridge");
4220 ds_put_cstr(&ds
, " port VLAN MAC Age\n");
4221 LIST_FOR_EACH (e
, lru_node
, &ofproto
->ml
->lrus
) {
4222 struct ofbundle
*bundle
= e
->port
.p
;
4223 ds_put_format(&ds
, "%5d %4d "ETH_ADDR_FMT
" %3d\n",
4224 ofbundle_get_a_port(bundle
)->odp_port
,
4225 e
->vlan
, ETH_ADDR_ARGS(e
->mac
), mac_entry_age(e
));
4227 unixctl_command_reply(conn
, 200, ds_cstr(&ds
));
4231 struct ofproto_trace
{
4232 struct action_xlate_ctx ctx
;
4238 trace_format_rule(struct ds
*result
, uint8_t table_id
, int level
,
4239 const struct rule_dpif
*rule
)
4241 ds_put_char_multiple(result
, '\t', level
);
4243 ds_put_cstr(result
, "No match\n");
4247 ds_put_format(result
, "Rule: table=%"PRIu8
" cookie=%#"PRIx64
" ",
4248 table_id
, ntohll(rule
->up
.flow_cookie
));
4249 cls_rule_format(&rule
->up
.cr
, result
);
4250 ds_put_char(result
, '\n');
4252 ds_put_char_multiple(result
, '\t', level
);
4253 ds_put_cstr(result
, "OpenFlow ");
4254 ofp_print_actions(result
, rule
->up
.actions
, rule
->up
.n_actions
);
4255 ds_put_char(result
, '\n');
4259 trace_format_flow(struct ds
*result
, int level
, const char *title
,
4260 struct ofproto_trace
*trace
)
4262 ds_put_char_multiple(result
, '\t', level
);
4263 ds_put_format(result
, "%s: ", title
);
4264 if (flow_equal(&trace
->ctx
.flow
, &trace
->flow
)) {
4265 ds_put_cstr(result
, "unchanged");
4267 flow_format(result
, &trace
->ctx
.flow
);
4268 trace
->flow
= trace
->ctx
.flow
;
4270 ds_put_char(result
, '\n');
4274 trace_format_regs(struct ds
*result
, int level
, const char *title
,
4275 struct ofproto_trace
*trace
)
4279 ds_put_char_multiple(result
, '\t', level
);
4280 ds_put_format(result
, "%s:", title
);
4281 for (i
= 0; i
< FLOW_N_REGS
; i
++) {
4282 ds_put_format(result
, " reg%zu=0x%"PRIx32
, i
, trace
->flow
.regs
[i
]);
4284 ds_put_char(result
, '\n');
4288 trace_resubmit(struct action_xlate_ctx
*ctx
, struct rule_dpif
*rule
)
4290 struct ofproto_trace
*trace
= CONTAINER_OF(ctx
, struct ofproto_trace
, ctx
);
4291 struct ds
*result
= trace
->result
;
4293 ds_put_char(result
, '\n');
4294 trace_format_flow(result
, ctx
->recurse
+ 1, "Resubmitted flow", trace
);
4295 trace_format_regs(result
, ctx
->recurse
+ 1, "Resubmitted regs", trace
);
4296 trace_format_rule(result
, ctx
->table_id
, ctx
->recurse
+ 1, rule
);
4300 ofproto_unixctl_trace(struct unixctl_conn
*conn
, const char *args_
,
4301 void *aux OVS_UNUSED
)
4303 char *dpname
, *arg1
, *arg2
, *arg3
;
4304 char *args
= xstrdup(args_
);
4305 char *save_ptr
= NULL
;
4306 struct ofproto_dpif
*ofproto
;
4307 struct ofpbuf odp_key
;
4308 struct ofpbuf
*packet
;
4309 struct rule_dpif
*rule
;
4315 ofpbuf_init(&odp_key
, 0);
4318 dpname
= strtok_r(args
, " ", &save_ptr
);
4319 arg1
= strtok_r(NULL
, " ", &save_ptr
);
4320 arg2
= strtok_r(NULL
, " ", &save_ptr
);
4321 arg3
= strtok_r(NULL
, "", &save_ptr
); /* Get entire rest of line. */
4322 if (dpname
&& arg1
&& (!arg2
|| !strcmp(arg2
, "-generate")) && !arg3
) {
4323 /* ofproto/trace dpname flow [-generate] */
4326 /* Convert string to datapath key. */
4327 ofpbuf_init(&odp_key
, 0);
4328 error
= odp_flow_key_from_string(arg1
, &odp_key
);
4330 unixctl_command_reply(conn
, 501, "Bad flow syntax");
4334 /* Convert odp_key to flow. */
4335 error
= odp_flow_key_to_flow(odp_key
.data
, odp_key
.size
, &flow
);
4337 unixctl_command_reply(conn
, 501, "Invalid flow");
4341 /* Generate a packet, if requested. */
4343 packet
= ofpbuf_new(0);
4344 flow_compose(packet
, &flow
);
4346 } else if (dpname
&& arg1
&& arg2
&& arg3
) {
4347 /* ofproto/trace dpname tun_id in_port packet */
4351 tun_id
= htonll(strtoull(arg1
, NULL
, 0));
4352 in_port
= ofp_port_to_odp_port(atoi(arg2
));
4354 packet
= ofpbuf_new(strlen(args
) / 2);
4355 arg3
= ofpbuf_put_hex(packet
, arg3
, NULL
);
4356 arg3
+= strspn(arg3
, " ");
4357 if (*arg3
!= '\0') {
4358 unixctl_command_reply(conn
, 501, "Trailing garbage in command");
4361 if (packet
->size
< ETH_HEADER_LEN
) {
4362 unixctl_command_reply(conn
, 501,
4363 "Packet data too short for Ethernet");
4367 ds_put_cstr(&result
, "Packet: ");
4368 s
= ofp_packet_to_string(packet
->data
, packet
->size
, packet
->size
);
4369 ds_put_cstr(&result
, s
);
4372 flow_extract(packet
, tun_id
, in_port
, &flow
);
4374 unixctl_command_reply(conn
, 501, "Bad command syntax");
4378 ofproto
= ofproto_dpif_lookup(dpname
);
4380 unixctl_command_reply(conn
, 501, "Unknown ofproto (use ofproto/list "
4385 ds_put_cstr(&result
, "Flow: ");
4386 flow_format(&result
, &flow
);
4387 ds_put_char(&result
, '\n');
4389 rule
= rule_dpif_lookup(ofproto
, &flow
, 0);
4390 trace_format_rule(&result
, 0, 0, rule
);
4392 struct ofproto_trace trace
;
4393 struct ofpbuf
*odp_actions
;
4395 trace
.result
= &result
;
4397 action_xlate_ctx_init(&trace
.ctx
, ofproto
, &flow
, packet
);
4398 trace
.ctx
.resubmit_hook
= trace_resubmit
;
4399 odp_actions
= xlate_actions(&trace
.ctx
,
4400 rule
->up
.actions
, rule
->up
.n_actions
);
4402 ds_put_char(&result
, '\n');
4403 trace_format_flow(&result
, 0, "Final flow", &trace
);
4404 ds_put_cstr(&result
, "Datapath actions: ");
4405 format_odp_actions(&result
, odp_actions
->data
, odp_actions
->size
);
4406 ofpbuf_delete(odp_actions
);
4408 if (!trace
.ctx
.may_set_up_flow
) {
4410 ds_put_cstr(&result
, "\nThis flow is not cachable.");
4412 ds_put_cstr(&result
, "\nThe datapath actions are incomplete--"
4413 "for complete actions, please supply a packet.");
4418 unixctl_command_reply(conn
, 200, ds_cstr(&result
));
4421 ds_destroy(&result
);
4422 ofpbuf_delete(packet
);
4423 ofpbuf_uninit(&odp_key
);
4428 ofproto_dpif_clog(struct unixctl_conn
*conn OVS_UNUSED
,
4429 const char *args_ OVS_UNUSED
, void *aux OVS_UNUSED
)
4432 unixctl_command_reply(conn
, 200, NULL
);
4436 ofproto_dpif_unclog(struct unixctl_conn
*conn OVS_UNUSED
,
4437 const char *args_ OVS_UNUSED
, void *aux OVS_UNUSED
)
4440 unixctl_command_reply(conn
, 200, NULL
);
4444 ofproto_dpif_unixctl_init(void)
4446 static bool registered
;
4452 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace
, NULL
);
4453 unixctl_command_register("fdb/show", ofproto_unixctl_fdb_show
, NULL
);
4455 unixctl_command_register("ofproto/clog", ofproto_dpif_clog
, NULL
);
4456 unixctl_command_register("ofproto/unclog", ofproto_dpif_unclog
, NULL
);
4459 const struct ofproto_class ofproto_dpif_class
= {
4486 port_is_lacp_current
,
4487 NULL
, /* rule_choose_table */
4494 rule_modify_actions
,
4503 get_cfm_remote_mpids
,
4508 is_mirror_output_bundle
,
4509 forward_bpdu_changed
,