forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathofproto-dpif.c
9059 lines (7711 loc) · 290 KB
/
ofproto-dpif.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include "ofproto/ofproto-provider.h"
#include <errno.h>
#include "bond.h"
#include "bundle.h"
#include "byte-order.h"
#include "connmgr.h"
#include "coverage.h"
#include "cfm.h"
#include "dpif.h"
#include "dynamic-string.h"
#include "fail-open.h"
#include "hmapx.h"
#include "lacp.h"
#include "learn.h"
#include "mac-learning.h"
#include "meta-flow.h"
#include "multipath.h"
#include "netdev-vport.h"
#include "netdev.h"
#include "netlink.h"
#include "nx-match.h"
#include "odp-util.h"
#include "ofp-util.h"
#include "ofpbuf.h"
#include "ofp-actions.h"
#include "ofp-parse.h"
#include "ofp-print.h"
#include "ofproto-dpif-governor.h"
#include "ofproto-dpif-ipfix.h"
#include "ofproto-dpif-sflow.h"
#include "poll-loop.h"
#include "simap.h"
#include "smap.h"
#include "timer.h"
#include "tunnel.h"
#include "unaligned.h"
#include "unixctl.h"
#include "vlan-bitmap.h"
#include "vlog.h"
VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
COVERAGE_DEFINE(ofproto_dpif_expired);
COVERAGE_DEFINE(ofproto_dpif_xlate);
COVERAGE_DEFINE(facet_changed_rule);
COVERAGE_DEFINE(facet_revalidate);
COVERAGE_DEFINE(facet_unexpected);
COVERAGE_DEFINE(facet_suppress);
/* Maximum depth of flow table recursion (due to resubmit actions) in a
* flow translation. */
#define MAX_RESUBMIT_RECURSION 64
/* Number of implemented OpenFlow tables. */
enum { N_TABLES = 255 };
enum { TBL_INTERNAL = N_TABLES - 1 }; /* Used for internal hidden rules. */
BUILD_ASSERT_DECL(N_TABLES >= 2 && N_TABLES <= 255);
struct ofport_dpif;
struct ofproto_dpif;
struct flow_miss;
struct facet;
struct rule_dpif {
struct rule up;
/* These statistics:
*
* - Do include packets and bytes from facets that have been deleted or
* whose own statistics have been folded into the rule.
*
* - Do include packets and bytes sent "by hand" that were accounted to
* the rule without any facet being involved (this is a rare corner
* case in rule_execute()).
*
* - Do not include packet or bytes that can be obtained from any facet's
* packet_count or byte_count member or that can be obtained from the
* datapath by, e.g., dpif_flow_get() for any subfacet.
*/
uint64_t packet_count; /* Number of packets received. */
uint64_t byte_count; /* Number of bytes received. */
tag_type tag; /* Caches rule_calculate_tag() result. */
struct list facets; /* List of "struct facet"s. */
};
static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
{
return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
}
static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *,
const struct flow *);
static struct rule_dpif *rule_dpif_lookup__(struct ofproto_dpif *,
const struct flow *,
uint8_t table);
static struct rule_dpif *rule_dpif_miss_rule(struct ofproto_dpif *ofproto,
const struct flow *flow);
static void rule_credit_stats(struct rule_dpif *,
const struct dpif_flow_stats *);
static void flow_push_stats(struct facet *, const struct dpif_flow_stats *);
static tag_type rule_calculate_tag(const struct flow *,
const struct minimask *, uint32_t basis);
static void rule_invalidate(const struct rule_dpif *);
#define MAX_MIRRORS 32
typedef uint32_t mirror_mask_t;
#define MIRROR_MASK_C(X) UINT32_C(X)
BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
struct ofmirror {
struct ofproto_dpif *ofproto; /* Owning ofproto. */
size_t idx; /* In ofproto's "mirrors" array. */
void *aux; /* Key supplied by ofproto's client. */
char *name; /* Identifier for log messages. */
/* Selection criteria. */
struct hmapx srcs; /* Contains "struct ofbundle *"s. */
struct hmapx dsts; /* Contains "struct ofbundle *"s. */
unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
/* Output (exactly one of out == NULL and out_vlan == -1 is true). */
struct ofbundle *out; /* Output port or NULL. */
int out_vlan; /* Output VLAN or -1. */
mirror_mask_t dup_mirrors; /* Bitmap of mirrors with the same output. */
/* Counters. */
int64_t packet_count; /* Number of packets sent. */
int64_t byte_count; /* Number of bytes sent. */
};
static void mirror_destroy(struct ofmirror *);
static void update_mirror_stats(struct ofproto_dpif *ofproto,
mirror_mask_t mirrors,
uint64_t packets, uint64_t bytes);
struct ofbundle {
struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
struct ofproto_dpif *ofproto; /* Owning ofproto. */
void *aux; /* Key supplied by ofproto's client. */
char *name; /* Identifier for log messages. */
/* Configuration. */
struct list ports; /* Contains "struct ofport"s. */
enum port_vlan_mode vlan_mode; /* VLAN mode */
int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
* NULL if all VLANs are trunked. */
struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
struct bond *bond; /* Nonnull iff more than one port. */
bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
/* Status. */
bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
/* Port mirroring info. */
mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
};
static void bundle_remove(struct ofport *);
static void bundle_update(struct ofbundle *);
static void bundle_destroy(struct ofbundle *);
static void bundle_del_port(struct ofport_dpif *);
static void bundle_run(struct ofbundle *);
static void bundle_wait(struct ofbundle *);
static struct ofbundle *lookup_input_bundle(const struct ofproto_dpif *,
uint16_t in_port, bool warn,
struct ofport_dpif **in_ofportp);
/* A controller may use OFPP_NONE as the ingress port to indicate that
* it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
* when an input bundle is needed for validation (e.g., mirroring or
* OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
* any 'port' structs, so care must be taken when dealing with it. */
static struct ofbundle ofpp_none_bundle = {
.name = "OFPP_NONE",
.vlan_mode = PORT_VLAN_TRUNK
};
static void stp_run(struct ofproto_dpif *ofproto);
static void stp_wait(struct ofproto_dpif *ofproto);
static int set_stp_port(struct ofport *,
const struct ofproto_port_stp_settings *);
static bool ofbundle_includes_vlan(const struct ofbundle *, uint16_t vlan);
struct action_xlate_ctx {
/* action_xlate_ctx_init() initializes these members. */
/* The ofproto. */
struct ofproto_dpif *ofproto;
/* Flow to which the OpenFlow actions apply. xlate_actions() will modify
* this flow when actions change header fields. */
struct flow flow;
/* stack for the push and pop actions.
* Each stack element is of the type "union mf_subvalue". */
struct ofpbuf stack;
union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
/* The packet corresponding to 'flow', or a null pointer if we are
* revalidating without a packet to refer to. */
const struct ofpbuf *packet;
/* Should OFPP_NORMAL update the MAC learning table? Should "learn"
* actions update the flow table?
*
* We want to update these tables if we are actually processing a packet,
* or if we are accounting for packets that the datapath has processed, but
* not if we are just revalidating. */
bool may_learn;
/* The rule that we are currently translating, or NULL. */
struct rule_dpif *rule;
/* Union of the set of TCP flags seen so far in this flow. (Used only by
* NXAST_FIN_TIMEOUT. Set to zero to avoid updating updating rules'
* timeouts.) */
uint8_t tcp_flags;
/* If nonnull, flow translation calls this function just before executing a
* resubmit or OFPP_TABLE action. In addition, disables logging of traces
* when the recursion depth is exceeded.
*
* 'rule' is the rule being submitted into. It will be null if the
* resubmit or OFPP_TABLE action didn't find a matching rule.
*
* This is normally null so the client has to set it manually after
* calling action_xlate_ctx_init(). */
void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *rule);
/* If nonnull, flow translation calls this function to report some
* significant decision, e.g. to explain why OFPP_NORMAL translation
* dropped a packet. */
void (*report_hook)(struct action_xlate_ctx *, const char *s);
/* If nonnull, flow translation credits the specified statistics to each
* rule reached through a resubmit or OFPP_TABLE action.
*
* This is normally null so the client has to set it manually after
* calling action_xlate_ctx_init(). */
const struct dpif_flow_stats *resubmit_stats;
/* xlate_actions() initializes and uses these members. The client might want
* to look at them after it returns. */
struct ofpbuf *odp_actions; /* Datapath actions. */
tag_type tags; /* Tags associated with actions. */
enum slow_path_reason slow; /* 0 if fast path may be used. */
bool has_learn; /* Actions include NXAST_LEARN? */
bool has_normal; /* Actions output to OFPP_NORMAL? */
bool has_fin_timeout; /* Actions include NXAST_FIN_TIMEOUT? */
uint16_t nf_output_iface; /* Output interface index for NetFlow. */
mirror_mask_t mirrors; /* Bitmap of associated mirrors. */
/* xlate_actions() initializes and uses these members, but the client has no
* reason to look at them. */
int recurse; /* Recursion level, via xlate_table_action. */
bool max_resubmit_trigger; /* Recursed too deeply during translation. */
struct flow base_flow; /* Flow at the last commit. */
uint32_t orig_skb_priority; /* Priority when packet arrived. */
uint8_t table_id; /* OpenFlow table ID where flow was found. */
uint32_t sflow_n_outputs; /* Number of output ports. */
uint32_t sflow_odp_port; /* Output port for composing sFlow action. */
uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
bool exit; /* No further actions should be processed. */
};
/* Initial values of fields of the packet that may be changed during
* flow processing and needed later. */
struct initial_vals {
/* This is the value of vlan_tci in the packet as actually received from
* dpif. This is the same as the facet's flow.vlan_tci unless the packet
* was received via a VLAN splinter. In that case, this value is 0
* (because the packet as actually received from the dpif had no 802.1Q
* tag) but the facet's flow.vlan_tci is set to the VLAN that the splinter
* represents.
*
* This member should be removed when the VLAN splinters feature is no
* longer needed. */
ovs_be16 vlan_tci;
/* If received on a tunnel, the IP TOS value of the tunnel. */
uint8_t tunnel_ip_tos;
};
static void action_xlate_ctx_init(struct action_xlate_ctx *,
struct ofproto_dpif *, const struct flow *,
const struct initial_vals *initial_vals,
struct rule_dpif *,
uint8_t tcp_flags, const struct ofpbuf *);
static void xlate_actions(struct action_xlate_ctx *,
const struct ofpact *ofpacts, size_t ofpacts_len,
struct ofpbuf *odp_actions);
static void xlate_actions_for_side_effects(struct action_xlate_ctx *,
const struct ofpact *ofpacts,
size_t ofpacts_len);
static void xlate_table_action(struct action_xlate_ctx *, uint16_t in_port,
uint8_t table_id, bool may_packet_in);
static size_t put_userspace_action(const struct ofproto_dpif *,
struct ofpbuf *odp_actions,
const struct flow *,
const union user_action_cookie *,
const size_t);
static void compose_slow_path(const struct ofproto_dpif *, const struct flow *,
enum slow_path_reason,
uint64_t *stub, size_t stub_size,
const struct nlattr **actionsp,
size_t *actions_lenp);
static void xlate_report(struct action_xlate_ctx *ctx, const char *s);
/* A subfacet (see "struct subfacet" below) has three possible installation
* states:
*
* - SF_NOT_INSTALLED: Not installed in the datapath. This will only be the
* case just after the subfacet is created, just before the subfacet is
* destroyed, or if the datapath returns an error when we try to install a
* subfacet.
*
* - SF_FAST_PATH: The subfacet's actions are installed in the datapath.
*
* - SF_SLOW_PATH: An action that sends every packet for the subfacet through
* ofproto_dpif is installed in the datapath.
*/
enum subfacet_path {
SF_NOT_INSTALLED, /* No datapath flow for this subfacet. */
SF_FAST_PATH, /* Full actions are installed. */
SF_SLOW_PATH, /* Send-to-userspace action is installed. */
};
static const char *subfacet_path_to_string(enum subfacet_path);
/* A dpif flow and actions associated with a facet.
*
* See also the large comment on struct facet. */
struct subfacet {
/* Owners. */
struct hmap_node hmap_node; /* In struct ofproto_dpif 'subfacets' list. */
struct list list_node; /* In struct facet's 'facets' list. */
struct facet *facet; /* Owning facet. */
enum odp_key_fitness key_fitness;
struct nlattr *key;
int key_len;
long long int used; /* Time last used; time created if not used. */
long long int created; /* Time created. */
uint64_t dp_packet_count; /* Last known packet count in the datapath. */
uint64_t dp_byte_count; /* Last known byte count in the datapath. */
/* Datapath actions.
*
* These should be essentially identical for every subfacet in a facet, but
* may differ in trivial ways due to VLAN splinters. */
size_t actions_len; /* Number of bytes in actions[]. */
struct nlattr *actions; /* Datapath actions. */
enum slow_path_reason slow; /* 0 if fast path may be used. */
enum subfacet_path path; /* Installed in datapath? */
/* Initial values of the packet that may be needed later. */
struct initial_vals initial_vals;
/* Datapath port the packet arrived on. This is needed to remove
* flows for ports that are no longer part of the bridge. Since the
* flow definition only has the OpenFlow port number and the port is
* no longer part of the bridge, we can't determine the datapath port
* number needed to delete the flow from the datapath. */
uint32_t odp_in_port;
};
#define SUBFACET_DESTROY_MAX_BATCH 50
static struct subfacet *subfacet_create(struct facet *, struct flow_miss *miss,
long long int now);
static struct subfacet *subfacet_find(struct ofproto_dpif *,
const struct nlattr *key, size_t key_len,
uint32_t key_hash);
static void subfacet_destroy(struct subfacet *);
static void subfacet_destroy__(struct subfacet *);
static void subfacet_destroy_batch(struct ofproto_dpif *,
struct subfacet **, int n);
static void subfacet_reset_dp_stats(struct subfacet *,
struct dpif_flow_stats *);
static void subfacet_update_time(struct subfacet *, long long int used);
static void subfacet_update_stats(struct subfacet *,
const struct dpif_flow_stats *);
static void subfacet_make_actions(struct subfacet *,
const struct ofpbuf *packet,
struct ofpbuf *odp_actions);
static int subfacet_install(struct subfacet *,
const struct nlattr *actions, size_t actions_len,
struct dpif_flow_stats *, enum slow_path_reason);
static void subfacet_uninstall(struct subfacet *);
static enum subfacet_path subfacet_want_path(enum slow_path_reason);
/* An exact-match instantiation of an OpenFlow flow.
*
* A facet associates a "struct flow", which represents the Open vSwitch
* userspace idea of an exact-match flow, with one or more subfacets. Each
* subfacet tracks the datapath's idea of the exact-match flow equivalent to
* the facet. When the kernel module (or other dpif implementation) and Open
* vSwitch userspace agree on the definition of a flow key, there is exactly
* one subfacet per facet. If the dpif implementation supports more-specific
* flow matching than userspace, however, a facet can have more than one
* subfacet, each of which corresponds to some distinction in flow that
* userspace simply doesn't understand.
*
* Flow expiration works in terms of subfacets, so a facet must have at least
* one subfacet or it will never expire, leaking memory. */
struct facet {
/* Owners. */
struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
struct list list_node; /* In owning rule's 'facets' list. */
struct rule_dpif *rule; /* Owning rule. */
/* Owned data. */
struct list subfacets;
long long int used; /* Time last used; time created if not used. */
/* Key. */
struct flow flow;
/* These statistics:
*
* - Do include packets and bytes sent "by hand", e.g. with
* dpif_execute().
*
* - Do include packets and bytes that were obtained from the datapath
* when a subfacet's statistics were reset (e.g. dpif_flow_put() with
* DPIF_FP_ZERO_STATS).
*
* - Do not include packets or bytes that can be obtained from the
* datapath for any existing subfacet.
*/
uint64_t packet_count; /* Number of packets received. */
uint64_t byte_count; /* Number of bytes received. */
/* Resubmit statistics. */
uint64_t prev_packet_count; /* Number of packets from last stats push. */
uint64_t prev_byte_count; /* Number of bytes from last stats push. */
long long int prev_used; /* Used time from last stats push. */
/* Accounting. */
uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
uint8_t tcp_flags; /* TCP flags seen for this 'rule'. */
/* Properties of datapath actions.
*
* Every subfacet has its own actions because actions can differ slightly
* between splintered and non-splintered subfacets due to the VLAN tag
* being initially different (present vs. absent). All of them have these
* properties in common so we just store one copy of them here. */
bool has_learn; /* Actions include NXAST_LEARN? */
bool has_normal; /* Actions output to OFPP_NORMAL? */
bool has_fin_timeout; /* Actions include NXAST_FIN_TIMEOUT? */
tag_type tags; /* Tags that would require revalidation. */
mirror_mask_t mirrors; /* Bitmap of dependent mirrors. */
/* Storage for a single subfacet, to reduce malloc() time and space
* overhead. (A facet always has at least one subfacet and in the common
* case has exactly one subfacet. However, 'one_subfacet' may not
* always be valid, since it could have been removed after newer
* subfacets were pushed onto the 'subfacets' list.) */
struct subfacet one_subfacet;
long long int learn_rl; /* Rate limiter for facet_learn(). */
};
static struct facet *facet_create(struct rule_dpif *,
const struct flow *, uint32_t hash);
static void facet_remove(struct facet *);
static void facet_free(struct facet *);
static struct facet *facet_find(struct ofproto_dpif *,
const struct flow *, uint32_t hash);
static struct facet *facet_lookup_valid(struct ofproto_dpif *,
const struct flow *, uint32_t hash);
static void facet_revalidate(struct facet *);
static bool facet_check_consistency(struct facet *);
static void facet_flush_stats(struct facet *);
static void facet_update_time(struct facet *, long long int used);
static void facet_reset_counters(struct facet *);
static void facet_push_stats(struct facet *);
static void facet_learn(struct facet *);
static void facet_account(struct facet *);
static void push_all_stats(void);
static struct subfacet *facet_get_subfacet(struct facet *);
static bool facet_is_controller_flow(struct facet *);
struct ofport_dpif {
struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
struct ofport up;
uint32_t odp_port;
struct ofbundle *bundle; /* Bundle that contains this port, if any. */
struct list bundle_node; /* In struct ofbundle's "ports" list. */
struct cfm *cfm; /* Connectivity Fault Management, if any. */
tag_type tag; /* Tag associated with this port. */
bool may_enable; /* May be enabled in bonds. */
long long int carrier_seq; /* Carrier status changes. */
struct tnl_port *tnl_port; /* Tunnel handle, or null. */
/* Spanning tree. */
struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
long long int stp_state_entered;
struct hmap priorities; /* Map of attached 'priority_to_dscp's. */
/* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
*
* This is deprecated. It is only for compatibility with broken device
* drivers in old versions of Linux that do not properly support VLANs when
* VLAN devices are not used. When broken device drivers are no longer in
* widespread use, we will delete these interfaces. */
uint16_t realdev_ofp_port;
int vlandev_vid;
};
/* Node in 'ofport_dpif''s 'priorities' map. Used to maintain a map from
* 'priority' (the datapath's term for QoS queue) to the dscp bits which all
* traffic egressing the 'ofport' with that priority should be marked with. */
struct priority_to_dscp {
struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'priorities' map. */
uint32_t priority; /* Priority of this queue (see struct flow). */
uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
};
/* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
*
* This is deprecated. It is only for compatibility with broken device drivers
* in old versions of Linux that do not properly support VLANs when VLAN
* devices are not used. When broken device drivers are no longer in
* widespread use, we will delete these interfaces. */
struct vlan_splinter {
struct hmap_node realdev_vid_node;
struct hmap_node vlandev_node;
uint16_t realdev_ofp_port;
uint16_t vlandev_ofp_port;
int vid;
};
static uint32_t vsp_realdev_to_vlandev(const struct ofproto_dpif *,
uint32_t realdev, ovs_be16 vlan_tci);
static bool vsp_adjust_flow(const struct ofproto_dpif *, struct flow *);
static void vsp_remove(struct ofport_dpif *);
static void vsp_add(struct ofport_dpif *, uint16_t realdev_ofp_port, int vid);
static uint32_t ofp_port_to_odp_port(const struct ofproto_dpif *,
uint16_t ofp_port);
static uint16_t odp_port_to_ofp_port(const struct ofproto_dpif *,
uint32_t odp_port);
static struct ofport_dpif *
ofport_dpif_cast(const struct ofport *ofport)
{
ovs_assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
}
static void port_run(struct ofport_dpif *);
static void port_run_fast(struct ofport_dpif *);
static void port_wait(struct ofport_dpif *);
static int set_cfm(struct ofport *, const struct cfm_settings *);
static void ofport_clear_priorities(struct ofport_dpif *);
static void run_fast_rl(void);
struct dpif_completion {
struct list list_node;
struct ofoperation *op;
};
/* Extra information about a classifier table.
* Currently used just for optimized flow revalidation. */
struct table_dpif {
/* If either of these is nonnull, then this table has a form that allows
* flows to be tagged to avoid revalidating most flows for the most common
* kinds of flow table changes. */
struct cls_table *catchall_table; /* Table that wildcards all fields. */
struct cls_table *other_table; /* Table with any other wildcard set. */
uint32_t basis; /* Keeps each table's tags separate. */
};
/* Reasons that we might need to revalidate every facet, and corresponding
* coverage counters.
*
* A value of 0 means that there is no need to revalidate.
*
* It would be nice to have some cleaner way to integrate with coverage
* counters, but with only a few reasons I guess this is good enough for
* now. */
enum revalidate_reason {
REV_RECONFIGURE = 1, /* Switch configuration changed. */
REV_STP, /* Spanning tree protocol port status change. */
REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
REV_FLOW_TABLE, /* Flow table changed. */
REV_INCONSISTENCY /* Facet self-check failed. */
};
COVERAGE_DEFINE(rev_reconfigure);
COVERAGE_DEFINE(rev_stp);
COVERAGE_DEFINE(rev_port_toggled);
COVERAGE_DEFINE(rev_flow_table);
COVERAGE_DEFINE(rev_inconsistency);
/* Drop keys are odp flow keys which have drop flows installed in the kernel.
* These are datapath flows which have no associated ofproto, if they did we
* would use facets. */
struct drop_key {
struct hmap_node hmap_node;
struct nlattr *key;
size_t key_len;
};
/* All datapaths of a given type share a single dpif backer instance. */
struct dpif_backer {
char *type;
int refcount;
struct dpif *dpif;
struct timer next_expiration;
struct hmap odp_to_ofport_map; /* ODP port to ofport mapping. */
struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
/* Facet revalidation flags applying to facets which use this backer. */
enum revalidate_reason need_revalidate; /* Revalidate every facet. */
struct tag_set revalidate_set; /* Revalidate only matching facets. */
struct hmap drop_keys; /* Set of dropped odp keys. */
};
/* All existing ofproto_backer instances, indexed by ofproto->up.type. */
static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
static void drop_key_clear(struct dpif_backer *);
static struct ofport_dpif *
odp_port_to_ofport(const struct dpif_backer *, uint32_t odp_port);
static void dpif_stats_update_hit_count(struct ofproto_dpif *ofproto,
uint64_t delta);
struct avg_subfacet_rates {
double add_rate; /* Moving average of new flows created per minute. */
double del_rate; /* Moving average of flows deleted per minute. */
};
static void show_dp_rates(struct ds *ds, const char *heading,
const struct avg_subfacet_rates *rates);
static void exp_mavg(double *avg, int base, double new);
struct ofproto_dpif {
struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
struct ofproto up;
struct dpif_backer *backer;
/* Special OpenFlow rules. */
struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
/* Statistics. */
uint64_t n_matches;
/* Bridging. */
struct netflow *netflow;
struct dpif_sflow *sflow;
struct dpif_ipfix *ipfix;
struct hmap bundles; /* Contains "struct ofbundle"s. */
struct mac_learning *ml;
struct ofmirror *mirrors[MAX_MIRRORS];
bool has_mirrors;
bool has_bonded_bundles;
/* Facets. */
struct hmap facets;
struct hmap subfacets;
struct governor *governor;
long long int consistency_rl;
/* Revalidation. */
struct table_dpif tables[N_TABLES];
/* Support for debugging async flow mods. */
struct list completions;
bool has_bundle_action; /* True when the first bundle action appears. */
struct netdev_stats stats; /* To account packets generated and consumed in
* userspace. */
/* Spanning tree. */
struct stp *stp;
long long int stp_last_tick;
/* VLAN splinters. */
struct hmap realdev_vid_map; /* (realdev,vid) -> vlandev. */
struct hmap vlandev_map; /* vlandev -> (realdev,vid). */
/* Ports. */
struct sset ports; /* Set of standard port names. */
struct sset ghost_ports; /* Ports with no datapath port. */
struct sset port_poll_set; /* Queued names for port_poll() reply. */
int port_poll_errno; /* Last errno for port_poll() reply. */
/* Per ofproto's dpif stats. */
uint64_t n_hit;
uint64_t n_missed;
/* Subfacet statistics.
*
* These keep track of the total number of subfacets added and deleted and
* flow life span. They are useful for computing the flow rates stats
* exposed via "ovs-appctl dpif/show". The goal is to learn about
* traffic patterns in ways that we can use later to improve Open vSwitch
* performance in new situations. */
long long int created; /* Time when it is created. */
unsigned int max_n_subfacet; /* Maximum number of flows */
/* The average number of subfacets... */
struct avg_subfacet_rates hourly; /* ...over the last hour. */
struct avg_subfacet_rates daily; /* ...over the last day. */
long long int last_minute; /* Last time 'hourly' was updated. */
/* Number of subfacets added or deleted since 'last_minute'. */
unsigned int subfacet_add_count;
unsigned int subfacet_del_count;
/* Number of subfacets added or deleted from 'created' to 'last_minute.' */
unsigned long long int total_subfacet_add_count;
unsigned long long int total_subfacet_del_count;
/* Sum of the number of milliseconds that each subfacet existed,
* over the subfacets that have been added and then later deleted. */
unsigned long long int total_subfacet_life_span;
/* Incremented by the number of currently existing subfacets, each
* time we pull statistics from the kernel. */
unsigned long long int total_subfacet_count;
/* Number of times we pull statistics from the kernel. */
unsigned long long int n_update_stats;
};
static unsigned long long int avg_subfacet_life_span(
const struct ofproto_dpif *);
static double avg_subfacet_count(const struct ofproto_dpif *ofproto);
static void update_moving_averages(struct ofproto_dpif *ofproto);
static void dpif_stats_update_hit_count(struct ofproto_dpif *ofproto,
uint64_t delta);
static void update_max_subfacet_count(struct ofproto_dpif *ofproto);
/* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
* for debugging the asynchronous flow_mod implementation.) */
static bool clogged;
/* All existing ofproto_dpif instances, indexed by ->up.name. */
static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
static void ofproto_dpif_unixctl_init(void);
static struct ofproto_dpif *
ofproto_dpif_cast(const struct ofproto *ofproto)
{
ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
}
static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *,
uint16_t ofp_port);
static struct ofport_dpif *get_odp_port(const struct ofproto_dpif *,
uint32_t odp_port);
static void ofproto_trace(struct ofproto_dpif *, const struct flow *,
const struct ofpbuf *,
const struct initial_vals *, struct ds *);
/* Packet processing. */
static void update_learning_table(struct ofproto_dpif *,
const struct flow *, int vlan,
struct ofbundle *);
/* Upcalls. */
#define FLOW_MISS_MAX_BATCH 50
static int handle_upcalls(struct dpif_backer *, unsigned int max_batch);
/* Flow expiration. */
static int expire(struct dpif_backer *);
/* NetFlow. */
static void send_netflow_active_timeouts(struct ofproto_dpif *);
/* Utilities. */
static int send_packet(const struct ofport_dpif *, struct ofpbuf *packet);
static size_t compose_sflow_action(const struct ofproto_dpif *,
struct ofpbuf *odp_actions,
const struct flow *, uint32_t odp_port);
static void compose_ipfix_action(const struct ofproto_dpif *,
struct ofpbuf *odp_actions,
const struct flow *);
static void add_mirror_actions(struct action_xlate_ctx *ctx,
const struct flow *flow);
/* Global variables. */
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
/* Initial mappings of port to bridge mappings. */
static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
/* Factory functions. */
static void
init(const struct shash *iface_hints)
{
struct shash_node *node;
/* Make a local copy, since we don't own 'iface_hints' elements. */
SHASH_FOR_EACH(node, iface_hints) {
const struct iface_hint *orig_hint = node->data;
struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
new_hint->br_name = xstrdup(orig_hint->br_name);
new_hint->br_type = xstrdup(orig_hint->br_type);
new_hint->ofp_port = orig_hint->ofp_port;
shash_add(&init_ofp_ports, node->name, new_hint);
}
}
static void
enumerate_types(struct sset *types)
{
dp_enumerate_types(types);
}
static int
enumerate_names(const char *type, struct sset *names)
{
struct ofproto_dpif *ofproto;
sset_clear(names);
HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
if (strcmp(type, ofproto->up.type)) {
continue;
}
sset_add(names, ofproto->up.name);
}
return 0;
}
static int
del(const char *type, const char *name)
{
struct dpif *dpif;
int error;
error = dpif_open(name, type, &dpif);
if (!error) {
error = dpif_delete(dpif);
dpif_close(dpif);
}
return error;
}
static const char *
port_open_type(const char *datapath_type, const char *port_type)
{
return dpif_port_open_type(datapath_type, port_type);
}
/* Type functions. */
static struct ofproto_dpif *
lookup_ofproto_dpif_by_port_name(const char *name)
{
struct ofproto_dpif *ofproto;
HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
if (sset_contains(&ofproto->ports, name)) {
return ofproto;
}
}
return NULL;
}
static int
type_run(const char *type)
{
static long long int push_timer = LLONG_MIN;
struct dpif_backer *backer;
char *devname;
int error;
backer = shash_find_data(&all_dpif_backers, type);
if (!backer) {
/* This is not necessarily a problem, since backers are only
* created on demand. */
return 0;
}
dpif_run(backer->dpif);
/* The most natural place to push facet statistics is when they're pulled
* from the datapath. However, when there are many flows in the datapath,
* this expensive operation can occur so frequently, that it reduces our
* ability to quickly set up flows. To reduce the cost, we push statistics
* here instead. */
if (time_msec() > push_timer) {
push_timer = time_msec() + 2000;
push_all_stats();
}
if (backer->need_revalidate
|| !tag_set_is_empty(&backer->revalidate_set)) {
struct tag_set revalidate_set = backer->revalidate_set;
bool need_revalidate = backer->need_revalidate;
struct ofproto_dpif *ofproto;
struct simap_node *node;
struct simap tmp_backers;
/* Handle tunnel garbage collection. */
simap_init(&tmp_backers);
simap_swap(&backer->tnl_backers, &tmp_backers);
HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
struct ofport_dpif *iter;
if (backer != ofproto->backer) {
continue;
}
HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
const char *dp_port;
if (!iter->tnl_port) {
continue;
}
dp_port = netdev_vport_get_dpif_port(iter->up.netdev);
node = simap_find(&tmp_backers, dp_port);
if (node) {
simap_put(&backer->tnl_backers, dp_port, node->data);
simap_delete(&tmp_backers, node);
node = simap_find(&backer->tnl_backers, dp_port);
} else {
node = simap_find(&backer->tnl_backers, dp_port);
if (!node) {
uint32_t odp_port = UINT32_MAX;
if (!dpif_port_add(backer->dpif, iter->up.netdev,
&odp_port)) {
simap_put(&backer->tnl_backers, dp_port, odp_port);
node = simap_find(&backer->tnl_backers, dp_port);
}
}
}
iter->odp_port = node ? node->data : OVSP_NONE;
if (tnl_port_reconfigure(&iter->up, iter->odp_port,
&iter->tnl_port)) {
backer->need_revalidate = REV_RECONFIGURE;
}
}
}
SIMAP_FOR_EACH (node, &tmp_backers) {
dpif_port_del(backer->dpif, node->data);
}
simap_destroy(&tmp_backers);
switch (backer->need_revalidate) {