forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathofproto-dpif.c
5420 lines (4634 loc) · 166 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 Nicira Networks.
*
* 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 "autopath.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 "multipath.h"
#include "netdev.h"
#include "netlink.h"
#include "nx-match.h"
#include "odp-util.h"
#include "ofp-util.h"
#include "ofpbuf.h"
#include "ofp-print.h"
#include "ofproto-dpif-sflow.h"
#include "poll-loop.h"
#include "timer.h"
#include "unaligned.h"
#include "unixctl.h"
#include "vlan-bitmap.h"
#include "vlog.h"
VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
COVERAGE_DEFINE(ofproto_dpif_ctlr_action);
COVERAGE_DEFINE(ofproto_dpif_expired);
COVERAGE_DEFINE(ofproto_dpif_no_packet_in);
COVERAGE_DEFINE(ofproto_dpif_xlate);
COVERAGE_DEFINE(facet_changed_rule);
COVERAGE_DEFINE(facet_invalidated);
COVERAGE_DEFINE(facet_revalidate);
COVERAGE_DEFINE(facet_unexpected);
/* Maximum depth of flow table recursion (due to resubmit actions) in a
* flow translation. */
#define MAX_RESUBMIT_RECURSION 32
/* Number of implemented OpenFlow tables. */
enum { N_TABLES = 255 };
BUILD_ASSERT_DECL(N_TABLES >= 1 && N_TABLES <= 255);
struct ofport_dpif;
struct ofproto_dpif;
struct rule_dpif {
struct rule up;
long long int used; /* Time last used; time created if not used. */
/* 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 facet.
*/
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 *, uint8_t table);
#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 (mutually exclusive). */
struct ofbundle *out; /* Output port or NULL. */
int out_vlan; /* Output VLAN or -1. */
};
static void mirror_destroy(struct ofmirror *);
/* A group of one or more OpenFlow ports. */
#define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
struct ofbundle {
struct ofproto_dpif *ofproto; /* Owning ofproto. */
struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
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. */
/* Status. */
bool floodable; /* True if no port has OFPPC_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 void stp_run(struct ofproto_dpif *ofproto);
static void stp_wait(struct ofproto_dpif *ofproto);
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;
/* 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 MAC learning and NXAST_LEARN actions execute? We
* want to execute them 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;
/* If nonnull, called just before executing a resubmit action.
*
* 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 *);
/* 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. */
bool may_set_up_flow; /* True ordinarily; false if the actions must
* be reassessed for every packet. */
bool has_learn; /* Actions include NXAST_LEARN? */
bool has_normal; /* Actions output to OFPP_NORMAL? */
uint16_t nf_output_iface; /* Output interface index for NetFlow. */
/* 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. */
uint32_t priority; /* Current flow priority. 0 if none. */
struct flow base_flow; /* Flow at the last commit. */
uint32_t base_priority; /* Priority at the last commit. */
uint8_t table_id; /* OpenFlow table ID where flow was found. */
uint32_t sflow_n_outputs; /* Number of output ports. */
uint16_t sflow_odp_port; /* Output port for composing sFlow action. */
uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
};
static void action_xlate_ctx_init(struct action_xlate_ctx *,
struct ofproto_dpif *, const struct flow *,
const struct ofpbuf *);
static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
const union ofp_action *in, size_t n_in);
/* An exact-match instantiation of an OpenFlow flow. */
struct facet {
long long int used; /* Time last used; time created if not used. */
/* 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 its statistics were reset (e.g. dpif_flow_put() with
* DPIF_FP_ZERO_STATS).
*/
uint64_t packet_count; /* Number of packets received. */
uint64_t byte_count; /* Number of bytes received. */
uint64_t dp_packet_count; /* Last known packet count in the datapath. */
uint64_t dp_byte_count; /* Last known byte count in the datapath. */
uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
long long int rs_used; /* Used time pushed to resubmit children. */
uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
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. */
struct flow flow; /* Exact-match flow. */
bool installed; /* Installed in datapath? */
bool may_install; /* True ordinarily; false if actions must
* be reassessed for every packet. */
bool has_learn; /* Actions include NXAST_LEARN? */
bool has_normal; /* Actions output to OFPP_NORMAL? */
size_t actions_len; /* Number of bytes in actions[]. */
struct nlattr *actions; /* Datapath actions. */
tag_type tags; /* Tags. */
struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
};
static struct facet *facet_create(struct rule_dpif *, const struct flow *);
static void facet_remove(struct ofproto_dpif *, struct facet *);
static void facet_free(struct facet *);
static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
static struct facet *facet_lookup_valid(struct ofproto_dpif *,
const struct flow *);
static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
static bool execute_controller_action(struct ofproto_dpif *,
const struct flow *,
const struct nlattr *odp_actions,
size_t actions_len,
struct ofpbuf *packet);
static void facet_execute(struct ofproto_dpif *, struct facet *,
struct ofpbuf *packet);
static int facet_put__(struct ofproto_dpif *, struct facet *,
const struct nlattr *actions, size_t actions_len,
struct dpif_flow_stats *);
static void facet_install(struct ofproto_dpif *, struct facet *,
bool zero_stats);
static void facet_uninstall(struct ofproto_dpif *, struct facet *);
static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
static void facet_make_actions(struct ofproto_dpif *, struct facet *,
const struct ofpbuf *packet);
static void facet_update_time(struct ofproto_dpif *, struct facet *,
long long int used);
static void facet_update_stats(struct ofproto_dpif *, struct facet *,
const struct dpif_flow_stats *);
static void facet_reset_counters(struct facet *);
static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *);
static void facet_push_stats(struct facet *);
static void facet_account(struct ofproto_dpif *, struct facet *);
static bool facet_is_controller_flow(struct facet *);
static void flow_push_stats(const struct rule_dpif *,
struct flow *, uint64_t packets, uint64_t bytes,
long long int used);
static uint32_t rule_calculate_tag(const struct flow *,
const struct flow_wildcards *,
uint32_t basis);
static void rule_invalidate(const struct rule_dpif *);
struct ofport_dpif {
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. */
uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
bool may_enable; /* May be enabled in bonds. */
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;
};
static struct ofport_dpif *
ofport_dpif_cast(const struct ofport *ofport)
{
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_wait(struct ofport_dpif *);
static int set_cfm(struct ofport *, const struct cfm_settings *);
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. */
};
struct ofproto_dpif {
struct ofproto up;
struct dpif *dpif;
int max_ports;
/* Statistics. */
uint64_t n_matches;
/* Bridging. */
struct netflow *netflow;
struct dpif_sflow *sflow;
struct hmap bundles; /* Contains "struct ofbundle"s. */
struct mac_learning *ml;
struct ofmirror *mirrors[MAX_MIRRORS];
bool has_bonded_bundles;
/* Expiration. */
struct timer next_expiration;
/* Facets. */
struct hmap facets;
/* Revalidation. */
struct table_dpif tables[N_TABLES];
bool need_revalidate;
struct tag_set revalidate_set;
/* Support for debugging async flow mods. */
struct list completions;
bool has_bundle_action; /* True when the first bundle action appears. */
/* Spanning tree. */
struct stp *stp;
long long int stp_last_tick;
};
/* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
* for debugging the asynchronous flow_mod implementation.) */
static bool clogged;
static void ofproto_dpif_unixctl_init(void);
static struct ofproto_dpif *
ofproto_dpif_cast(const struct ofproto *ofproto)
{
assert(ofproto->ofproto_class == &ofproto_dpif_class);
return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
}
static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
uint16_t ofp_port);
static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
uint32_t odp_port);
/* Packet processing. */
static void update_learning_table(struct ofproto_dpif *,
const struct flow *, int vlan,
struct ofbundle *);
static bool is_admissible(struct ofproto_dpif *, const struct flow *,
bool have_packet, tag_type *, int *vlanp,
struct ofbundle **in_bundlep);
/* Upcalls. */
#define FLOW_MISS_MAX_BATCH 50
static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
static void handle_miss_upcalls(struct ofproto_dpif *,
struct dpif_upcall *, size_t n);
/* Flow expiration. */
static int expire(struct ofproto_dpif *);
/* Utilities. */
static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
const struct ofpbuf *packet);
static size_t
compose_sflow_action(const struct ofproto_dpif *, struct ofpbuf *odp_actions,
const struct flow *, uint32_t odp_port);
/* Global variables. */
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
/* Factory functions. */
static void
enumerate_types(struct sset *types)
{
dp_enumerate_types(types);
}
static int
enumerate_names(const char *type, struct sset *names)
{
return dp_enumerate_names(type, names);
}
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;
}
/* Basic life-cycle. */
static struct ofproto *
alloc(void)
{
struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
return &ofproto->up;
}
static void
dealloc(struct ofproto *ofproto_)
{
struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
free(ofproto);
}
static int
construct(struct ofproto *ofproto_, int *n_tablesp)
{
struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
const char *name = ofproto->up.name;
int error;
int i;
error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
if (error) {
VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
return error;
}
ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
ofproto->n_matches = 0;
dpif_flow_flush(ofproto->dpif);
dpif_recv_purge(ofproto->dpif);
error = dpif_recv_set_mask(ofproto->dpif,
((1u << DPIF_UC_MISS) |
(1u << DPIF_UC_ACTION)));
if (error) {
VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
dpif_close(ofproto->dpif);
return error;
}
ofproto->netflow = NULL;
ofproto->sflow = NULL;
ofproto->stp = NULL;
hmap_init(&ofproto->bundles);
ofproto->ml = mac_learning_create();
for (i = 0; i < MAX_MIRRORS; i++) {
ofproto->mirrors[i] = NULL;
}
ofproto->has_bonded_bundles = false;
timer_set_duration(&ofproto->next_expiration, 1000);
hmap_init(&ofproto->facets);
for (i = 0; i < N_TABLES; i++) {
struct table_dpif *table = &ofproto->tables[i];
table->catchall_table = NULL;
table->other_table = NULL;
table->basis = random_uint32();
}
ofproto->need_revalidate = false;
tag_set_init(&ofproto->revalidate_set);
list_init(&ofproto->completions);
ofproto_dpif_unixctl_init();
ofproto->has_bundle_action = false;
*n_tablesp = N_TABLES;
return 0;
}
static void
complete_operations(struct ofproto_dpif *ofproto)
{
struct dpif_completion *c, *next;
LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
ofoperation_complete(c->op, 0);
list_remove(&c->list_node);
free(c);
}
}
static void
destruct(struct ofproto *ofproto_)
{
struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
struct rule_dpif *rule, *next_rule;
struct classifier *table;
int i;
complete_operations(ofproto);
OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
struct cls_cursor cursor;
cls_cursor_init(&cursor, table, NULL);
CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
ofproto_rule_destroy(&rule->up);
}
}
for (i = 0; i < MAX_MIRRORS; i++) {
mirror_destroy(ofproto->mirrors[i]);
}
netflow_destroy(ofproto->netflow);
dpif_sflow_destroy(ofproto->sflow);
hmap_destroy(&ofproto->bundles);
mac_learning_destroy(ofproto->ml);
hmap_destroy(&ofproto->facets);
dpif_close(ofproto->dpif);
}
static int
run(struct ofproto *ofproto_)
{
struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
struct ofport_dpif *ofport;
struct ofbundle *bundle;
size_t n_misses;
int i;
if (!clogged) {
complete_operations(ofproto);
}
dpif_run(ofproto->dpif);
n_misses = 0;
for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
struct dpif_upcall *upcall = &misses[n_misses];
int error;
error = dpif_recv(ofproto->dpif, upcall);
if (error) {
if (error == ENODEV && n_misses == 0) {
return error;
}
break;
}
if (upcall->type == DPIF_UC_MISS) {
/* Handle it later. */
n_misses++;
} else {
handle_upcall(ofproto, upcall);
}
}
handle_miss_upcalls(ofproto, misses, n_misses);
if (timer_expired(&ofproto->next_expiration)) {
int delay = expire(ofproto);
timer_set_duration(&ofproto->next_expiration, delay);
}
if (ofproto->netflow) {
netflow_run(ofproto->netflow);
}
if (ofproto->sflow) {
dpif_sflow_run(ofproto->sflow);
}
HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
port_run(ofport);
}
HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
bundle_run(bundle);
}
stp_run(ofproto);
mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
/* Now revalidate if there's anything to do. */
if (ofproto->need_revalidate
|| !tag_set_is_empty(&ofproto->revalidate_set)) {
struct tag_set revalidate_set = ofproto->revalidate_set;
bool revalidate_all = ofproto->need_revalidate;
struct facet *facet, *next;
/* Clear the revalidation flags. */
tag_set_init(&ofproto->revalidate_set);
ofproto->need_revalidate = false;
HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
if (revalidate_all
|| tag_set_intersects(&revalidate_set, facet->tags)) {
facet_revalidate(ofproto, facet);
}
}
}
return 0;
}
static void
wait(struct ofproto *ofproto_)
{
struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
struct ofport_dpif *ofport;
struct ofbundle *bundle;
if (!clogged && !list_is_empty(&ofproto->completions)) {
poll_immediate_wake();
}
dpif_wait(ofproto->dpif);
dpif_recv_wait(ofproto->dpif);
if (ofproto->sflow) {
dpif_sflow_wait(ofproto->sflow);
}
if (!tag_set_is_empty(&ofproto->revalidate_set)) {
poll_immediate_wake();
}
HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
port_wait(ofport);
}
HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
bundle_wait(bundle);
}
mac_learning_wait(ofproto->ml);
stp_wait(ofproto);
if (ofproto->need_revalidate) {
/* Shouldn't happen, but if it does just go around again. */
VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
poll_immediate_wake();
} else {
timer_wait(&ofproto->next_expiration);
}
}
static void
flush(struct ofproto *ofproto_)
{
struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
struct facet *facet, *next_facet;
HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
/* Mark the facet as not installed so that facet_remove() doesn't
* bother trying to uninstall it. There is no point in uninstalling it
* individually since we are about to blow away all the facets with
* dpif_flow_flush(). */
facet->installed = false;
facet->dp_packet_count = 0;
facet->dp_byte_count = 0;
facet_remove(ofproto, facet);
}
dpif_flow_flush(ofproto->dpif);
}
static void
get_features(struct ofproto *ofproto_ OVS_UNUSED,
bool *arp_match_ip, uint32_t *actions)
{
*arp_match_ip = true;
*actions = ((1u << OFPAT_OUTPUT) |
(1u << OFPAT_SET_VLAN_VID) |
(1u << OFPAT_SET_VLAN_PCP) |
(1u << OFPAT_STRIP_VLAN) |
(1u << OFPAT_SET_DL_SRC) |
(1u << OFPAT_SET_DL_DST) |
(1u << OFPAT_SET_NW_SRC) |
(1u << OFPAT_SET_NW_DST) |
(1u << OFPAT_SET_NW_TOS) |
(1u << OFPAT_SET_TP_SRC) |
(1u << OFPAT_SET_TP_DST) |
(1u << OFPAT_ENQUEUE));
}
static void
get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
{
struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
struct dpif_dp_stats s;
strcpy(ots->name, "classifier");
dpif_get_dp_stats(ofproto->dpif, &s);
put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
put_32aligned_be64(&ots->matched_count,
htonll(s.n_hit + ofproto->n_matches));
}
static int
set_netflow(struct ofproto *ofproto_,
const struct netflow_options *netflow_options)
{
struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
if (netflow_options) {
if (!ofproto->netflow) {
ofproto->netflow = netflow_create();
}
return netflow_set_options(ofproto->netflow, netflow_options);
} else {
netflow_destroy(ofproto->netflow);
ofproto->netflow = NULL;
return 0;
}
}
static struct ofport *
port_alloc(void)
{
struct ofport_dpif *port = xmalloc(sizeof *port);
return &port->up;
}
static void
port_dealloc(struct ofport *port_)
{
struct ofport_dpif *port = ofport_dpif_cast(port_);
free(port);
}
static int
port_construct(struct ofport *port_)
{
struct ofport_dpif *port = ofport_dpif_cast(port_);
struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
ofproto->need_revalidate = true;
port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
port->bundle = NULL;
port->cfm = NULL;
port->tag = tag_create_random();
port->may_enable = true;
port->stp_port = NULL;
port->stp_state = STP_DISABLED;
if (ofproto->sflow) {
dpif_sflow_add_port(ofproto->sflow, port->odp_port,
netdev_get_name(port->up.netdev));
}
return 0;
}
static void
port_destruct(struct ofport *port_)
{
struct ofport_dpif *port = ofport_dpif_cast(port_);
struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
ofproto->need_revalidate = true;
bundle_remove(port_);
set_cfm(port_, NULL);
if (ofproto->sflow) {
dpif_sflow_del_port(ofproto->sflow, port->odp_port);
}
}
static void
port_modified(struct ofport *port_)
{
struct ofport_dpif *port = ofport_dpif_cast(port_);
if (port->bundle && port->bundle->bond) {
bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
}
}
static void
port_reconfigured(struct ofport *port_, ovs_be32 old_config)
{
struct ofport_dpif *port = ofport_dpif_cast(port_);
struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
ovs_be32 changed = old_config ^ port->up.opp.config;
if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
ofproto->need_revalidate = true;
if (changed & htonl(OFPPC_NO_FLOOD) && port->bundle) {
bundle_update(port->bundle);
}
}
}
static int
set_sflow(struct ofproto *ofproto_,
const struct ofproto_sflow_options *sflow_options)
{
struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
struct dpif_sflow *ds = ofproto->sflow;
if (sflow_options) {
if (!ds) {
struct ofport_dpif *ofport;
ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
dpif_sflow_add_port(ds, ofport->odp_port,
netdev_get_name(ofport->up.netdev));
}
ofproto->need_revalidate = true;
}
dpif_sflow_set_options(ds, sflow_options);
} else {
if (ds) {
dpif_sflow_destroy(ds);
ofproto->need_revalidate = true;
ofproto->sflow = NULL;
}
}
return 0;
}
static int
set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
{
struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
int error;
if (!s) {
error = 0;
} else {
if (!ofport->cfm) {
struct ofproto_dpif *ofproto;
ofproto = ofproto_dpif_cast(ofport->up.ofproto);
ofproto->need_revalidate = true;
ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
}
if (cfm_configure(ofport->cfm, s)) {
return 0;
}
error = EINVAL;
}
cfm_destroy(ofport->cfm);
ofport->cfm = NULL;
return error;
}
static int
get_cfm_fault(const struct ofport *ofport_)
{
struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
}
static int
get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
size_t *n_rmps)
{
struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
if (ofport->cfm) {
cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
return 0;
} else {
return -1;
}
}
/* Spanning Tree. */
static void
send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
{
struct ofproto_dpif *ofproto = ofproto_;
struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
struct ofport_dpif *ofport;
ofport = stp_port_get_aux(sp);
if (!ofport) {
VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
ofproto->up.name, port_num);
} else {
struct eth_header *eth = pkt->l2;
netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
if (eth_addr_is_zero(eth->eth_src)) {
VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
"with unknown MAC", ofproto->up.name, port_num);
} else {
int error = netdev_send(ofport->up.netdev, pkt);
if (error) {
VLOG_WARN_RL(&rl, "%s: sending BPDU on port %s failed (%s)",
ofproto->up.name,
netdev_get_name(ofport->up.netdev),
strerror(error));
}
}
}
ofpbuf_delete(pkt);
}
/* Configures STP on 'ofproto_' using the settings defined in 's'. */
static int
set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
{
struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
/* Only revalidate flows if the configuration changed. */
if (!s != !ofproto->stp) {
ofproto->need_revalidate = true;
}
if (s) {
if (!ofproto->stp) {
ofproto->stp = stp_create(ofproto_->name, s->system_id,
send_bpdu_cb, ofproto);
ofproto->stp_last_tick = time_msec();
}
stp_set_bridge_id(ofproto->stp, s->system_id);
stp_set_bridge_priority(ofproto->stp, s->priority);
stp_set_hello_time(ofproto->stp, s->hello_time);
stp_set_max_age(ofproto->stp, s->max_age);
stp_set_forward_delay(ofproto->stp, s->fwd_delay);
} else {
stp_destroy(ofproto->stp);
ofproto->stp = NULL;
}
return 0;
}
static int
get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
{
struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
if (ofproto->stp) {