forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpif-netlink.c
3616 lines (3078 loc) · 111 KB
/
dpif-netlink.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) 2008-2017 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 "dpif-netlink.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <net/if.h>
#include <linux/types.h>
#include <linux/pkt_sched.h>
#include <poll.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/epoll.h>
#include <sys/stat.h>
#include <unistd.h>
#include "bitmap.h"
#include "dpif-provider.h"
#include "dpif-netlink-rtnl.h"
#include "openvswitch/dynamic-string.h"
#include "flow.h"
#include "fat-rwlock.h"
#include "netdev.h"
#include "netdev-provider.h"
#include "netdev-linux.h"
#include "netdev-vport.h"
#include "netlink-conntrack.h"
#include "netlink-notifier.h"
#include "netlink-socket.h"
#include "netlink.h"
#include "odp-util.h"
#include "openvswitch/ofpbuf.h"
#include "packets.h"
#include "openvswitch/poll-loop.h"
#include "random.h"
#include "openvswitch/shash.h"
#include "sset.h"
#include "timeval.h"
#include "unaligned.h"
#include "util.h"
#include "openvswitch/vlog.h"
#include "openvswitch/flow.h"
VLOG_DEFINE_THIS_MODULE(dpif_netlink);
#ifdef _WIN32
#include "wmi.h"
enum { WINDOWS = 1 };
#else
enum { WINDOWS = 0 };
#endif
enum { MAX_PORTS = USHRT_MAX };
/* This ethtool flag was introduced in Linux 2.6.24, so it might be
* missing if we have old headers. */
#define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
#define FLOW_DUMP_MAX_BATCH 50
#define OPERATE_MAX_OPS 50
struct dpif_netlink_dp {
/* Generic Netlink header. */
uint8_t cmd;
/* struct ovs_header. */
int dp_ifindex;
/* Attributes. */
const char *name; /* OVS_DP_ATTR_NAME. */
const uint32_t *upcall_pid; /* OVS_DP_ATTR_UPCALL_PID. */
uint32_t user_features; /* OVS_DP_ATTR_USER_FEATURES */
const struct ovs_dp_stats *stats; /* OVS_DP_ATTR_STATS. */
const struct ovs_dp_megaflow_stats *megaflow_stats;
/* OVS_DP_ATTR_MEGAFLOW_STATS.*/
};
static void dpif_netlink_dp_init(struct dpif_netlink_dp *);
static int dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *,
const struct ofpbuf *);
static void dpif_netlink_dp_dump_start(struct nl_dump *);
static int dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
struct dpif_netlink_dp *reply,
struct ofpbuf **bufp);
static int dpif_netlink_dp_get(const struct dpif *,
struct dpif_netlink_dp *reply,
struct ofpbuf **bufp);
struct dpif_netlink_flow {
/* Generic Netlink header. */
uint8_t cmd;
/* struct ovs_header. */
unsigned int nlmsg_flags;
int dp_ifindex;
/* Attributes.
*
* The 'stats' member points to 64-bit data that might only be aligned on
* 32-bit boundaries, so get_unaligned_u64() should be used to access its
* values.
*
* If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
* the Netlink version of the command, even if actions_len is zero. */
const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
size_t key_len;
const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
size_t mask_len;
const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
size_t actions_len;
ovs_u128 ufid; /* OVS_FLOW_ATTR_FLOW_ID. */
bool ufid_present; /* Is there a UFID? */
bool ufid_terse; /* Skip serializing key/mask/acts? */
const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
bool clear; /* OVS_FLOW_ATTR_CLEAR. */
bool probe; /* OVS_FLOW_ATTR_PROBE. */
};
static void dpif_netlink_flow_init(struct dpif_netlink_flow *);
static int dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *,
const struct ofpbuf *);
static void dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *,
struct ofpbuf *);
static int dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
struct dpif_netlink_flow *reply,
struct ofpbuf **bufp);
static void dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *,
struct dpif_flow_stats *);
static void dpif_netlink_flow_to_dpif_flow(struct dpif *, struct dpif_flow *,
const struct dpif_netlink_flow *);
/* One of the dpif channels between the kernel and userspace. */
struct dpif_channel {
struct nl_sock *sock; /* Netlink socket. */
long long int last_poll; /* Last time this channel was polled. */
};
#ifdef _WIN32
#define VPORT_SOCK_POOL_SIZE 1
/* On Windows, there is no native support for epoll. There are equivalent
* interfaces though, that are not used currently. For simpicity, a pool of
* netlink sockets is used. Each socket is represented by 'struct
* dpif_windows_vport_sock'. Since it is a pool, multiple OVS ports may be
* sharing the same socket. In the future, we can add a reference count and
* such fields. */
struct dpif_windows_vport_sock {
struct nl_sock *nl_sock; /* netlink socket. */
};
#endif
struct dpif_handler {
struct dpif_channel *channels;/* Array of channels for each handler. */
struct epoll_event *epoll_events;
int epoll_fd; /* epoll fd that includes channel socks. */
int n_events; /* Num events returned by epoll_wait(). */
int event_offset; /* Offset into 'epoll_events'. */
#ifdef _WIN32
/* Pool of sockets. */
struct dpif_windows_vport_sock *vport_sock_pool;
size_t last_used_pool_idx; /* Index to aid in allocating a
socket in the pool to a port. */
#endif
};
/* Datapath interface for the openvswitch Linux kernel module. */
struct dpif_netlink {
struct dpif dpif;
int dp_ifindex;
/* Upcall messages. */
struct fat_rwlock upcall_lock;
struct dpif_handler *handlers;
uint32_t n_handlers; /* Num of upcall handlers. */
int uc_array_size; /* Size of 'handler->channels' and */
/* 'handler->epoll_events'. */
/* Change notification. */
struct nl_sock *port_notifier; /* vport multicast group subscriber. */
bool refresh_channels;
};
static void report_loss(struct dpif_netlink *, struct dpif_channel *,
uint32_t ch_idx, uint32_t handler_id);
static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
/* Generic Netlink family numbers for OVS.
*
* Initialized by dpif_netlink_init(). */
static int ovs_datapath_family;
static int ovs_vport_family;
static int ovs_flow_family;
static int ovs_packet_family;
/* Generic Netlink multicast groups for OVS.
*
* Initialized by dpif_netlink_init(). */
static unsigned int ovs_vport_mcgroup;
/* If true, tunnel devices are created using OVS compat/genetlink.
* If false, tunnel devices are created with rtnetlink and using light weight
* tunnels. If we fail to create the tunnel the rtnetlink+LWT, then we fallback
* to using the compat interface. */
static bool ovs_tunnels_out_of_tree = true;
static int dpif_netlink_init(void);
static int open_dpif(const struct dpif_netlink_dp *, struct dpif **);
static uint32_t dpif_netlink_port_get_pid(const struct dpif *,
odp_port_t port_no, uint32_t hash);
static void dpif_netlink_handler_uninit(struct dpif_handler *handler);
static int dpif_netlink_refresh_channels(struct dpif_netlink *,
uint32_t n_handlers);
static void dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *,
struct ofpbuf *);
static int dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *,
const struct ofpbuf *);
static int dpif_netlink_port_query__(const struct dpif_netlink *dpif,
odp_port_t port_no, const char *port_name,
struct dpif_port *dpif_port);
static struct dpif_netlink *
dpif_netlink_cast(const struct dpif *dpif)
{
dpif_assert_class(dpif, &dpif_netlink_class);
return CONTAINER_OF(dpif, struct dpif_netlink, dpif);
}
static int
dpif_netlink_enumerate(struct sset *all_dps,
const struct dpif_class *dpif_class OVS_UNUSED)
{
struct nl_dump dump;
uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
struct ofpbuf msg, buf;
int error;
error = dpif_netlink_init();
if (error) {
return error;
}
ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
dpif_netlink_dp_dump_start(&dump);
while (nl_dump_next(&dump, &msg, &buf)) {
struct dpif_netlink_dp dp;
if (!dpif_netlink_dp_from_ofpbuf(&dp, &msg)) {
sset_add(all_dps, dp.name);
}
}
ofpbuf_uninit(&buf);
return nl_dump_done(&dump);
}
static int
dpif_netlink_open(const struct dpif_class *class OVS_UNUSED, const char *name,
bool create, struct dpif **dpifp)
{
struct dpif_netlink_dp dp_request, dp;
struct ofpbuf *buf;
uint32_t upcall_pid;
int error;
error = dpif_netlink_init();
if (error) {
return error;
}
/* Create or look up datapath. */
dpif_netlink_dp_init(&dp_request);
if (create) {
dp_request.cmd = OVS_DP_CMD_NEW;
upcall_pid = 0;
dp_request.upcall_pid = &upcall_pid;
} else {
/* Use OVS_DP_CMD_SET to report user features */
dp_request.cmd = OVS_DP_CMD_SET;
}
dp_request.name = name;
dp_request.user_features |= OVS_DP_F_UNALIGNED;
dp_request.user_features |= OVS_DP_F_VPORT_PIDS;
error = dpif_netlink_dp_transact(&dp_request, &dp, &buf);
if (error) {
return error;
}
error = open_dpif(&dp, dpifp);
ofpbuf_delete(buf);
return error;
}
static int
open_dpif(const struct dpif_netlink_dp *dp, struct dpif **dpifp)
{
struct dpif_netlink *dpif;
dpif = xzalloc(sizeof *dpif);
dpif->port_notifier = NULL;
fat_rwlock_init(&dpif->upcall_lock);
dpif_init(&dpif->dpif, &dpif_netlink_class, dp->name,
dp->dp_ifindex, dp->dp_ifindex);
dpif->dp_ifindex = dp->dp_ifindex;
*dpifp = &dpif->dpif;
return 0;
}
/* Destroys the netlink sockets pointed by the elements in 'socksp'
* and frees the 'socksp'. */
static void
vport_del_socksp__(struct nl_sock **socksp, uint32_t n_socks)
{
size_t i;
for (i = 0; i < n_socks; i++) {
nl_sock_destroy(socksp[i]);
}
free(socksp);
}
/* Creates an array of netlink sockets. Returns an array of the
* corresponding pointers. Records the error in 'error'. */
static struct nl_sock **
vport_create_socksp__(uint32_t n_socks, int *error)
{
struct nl_sock **socksp = xzalloc(n_socks * sizeof *socksp);
size_t i;
for (i = 0; i < n_socks; i++) {
*error = nl_sock_create(NETLINK_GENERIC, &socksp[i]);
if (*error) {
goto error;
}
}
return socksp;
error:
vport_del_socksp__(socksp, n_socks);
return NULL;
}
#ifdef _WIN32
static void
vport_delete_sock_pool(struct dpif_handler *handler)
OVS_REQ_WRLOCK(dpif->upcall_lock)
{
if (handler->vport_sock_pool) {
uint32_t i;
struct dpif_windows_vport_sock *sock_pool =
handler->vport_sock_pool;
for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
if (sock_pool[i].nl_sock) {
nl_sock_unsubscribe_packets(sock_pool[i].nl_sock);
nl_sock_destroy(sock_pool[i].nl_sock);
sock_pool[i].nl_sock = NULL;
}
}
free(handler->vport_sock_pool);
handler->vport_sock_pool = NULL;
}
}
static int
vport_create_sock_pool(struct dpif_handler *handler)
OVS_REQ_WRLOCK(dpif->upcall_lock)
{
struct dpif_windows_vport_sock *sock_pool;
size_t i;
int error = 0;
sock_pool = xzalloc(VPORT_SOCK_POOL_SIZE * sizeof *sock_pool);
for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
error = nl_sock_create(NETLINK_GENERIC, &sock_pool[i].nl_sock);
if (error) {
goto error;
}
/* Enable the netlink socket to receive packets. This is equivalent to
* calling nl_sock_join_mcgroup() to receive events. */
error = nl_sock_subscribe_packets(sock_pool[i].nl_sock);
if (error) {
goto error;
}
}
handler->vport_sock_pool = sock_pool;
handler->last_used_pool_idx = 0;
return 0;
error:
vport_delete_sock_pool(handler);
return error;
}
/* Returns an array pointers to netlink sockets. The sockets are picked from a
* pool. Records the error in 'error'. */
static struct nl_sock **
vport_create_socksp_windows(struct dpif_netlink *dpif, int *error)
OVS_REQ_WRLOCK(dpif->upcall_lock)
{
uint32_t n_socks = dpif->n_handlers;
struct nl_sock **socksp;
size_t i;
ovs_assert(n_socks <= 1);
socksp = xzalloc(n_socks * sizeof *socksp);
/* Pick netlink sockets to use in a round-robin fashion from each
* handler's pool of sockets. */
for (i = 0; i < n_socks; i++) {
struct dpif_handler *handler = &dpif->handlers[i];
struct dpif_windows_vport_sock *sock_pool = handler->vport_sock_pool;
size_t index = handler->last_used_pool_idx;
/* A pool of sockets is allocated when the handler is initialized. */
if (sock_pool == NULL) {
free(socksp);
*error = EINVAL;
return NULL;
}
ovs_assert(index < VPORT_SOCK_POOL_SIZE);
socksp[i] = sock_pool[index].nl_sock;
socksp[i] = sock_pool[index].nl_sock;
ovs_assert(socksp[i]);
index = (index == VPORT_SOCK_POOL_SIZE - 1) ? 0 : index + 1;
handler->last_used_pool_idx = index;
}
return socksp;
}
static void
vport_del_socksp_windows(struct dpif_netlink *dpif, struct nl_sock **socksp)
{
free(socksp);
}
#endif /* _WIN32 */
static struct nl_sock **
vport_create_socksp(struct dpif_netlink *dpif, int *error)
{
#ifdef _WIN32
return vport_create_socksp_windows(dpif, error);
#else
return vport_create_socksp__(dpif->n_handlers, error);
#endif
}
static void
vport_del_socksp(struct dpif_netlink *dpif, struct nl_sock **socksp)
{
#ifdef _WIN32
vport_del_socksp_windows(dpif, socksp);
#else
vport_del_socksp__(socksp, dpif->n_handlers);
#endif
}
/* Given the array of pointers to netlink sockets 'socksp', returns
* the array of corresponding pids. If the 'socksp' is NULL, returns
* a single-element array of value 0. */
static uint32_t *
vport_socksp_to_pids(struct nl_sock **socksp, uint32_t n_socks)
{
uint32_t *pids;
if (!socksp) {
pids = xzalloc(sizeof *pids);
} else {
size_t i;
pids = xzalloc(n_socks * sizeof *pids);
for (i = 0; i < n_socks; i++) {
pids[i] = nl_sock_pid(socksp[i]);
}
}
return pids;
}
/* Given the port number 'port_idx', extracts the pids of netlink sockets
* associated to the port and assigns it to 'upcall_pids'. */
static bool
vport_get_pids(struct dpif_netlink *dpif, uint32_t port_idx,
uint32_t **upcall_pids)
{
uint32_t *pids;
size_t i;
/* Since the nl_sock can only be assigned in either all
* or none "dpif->handlers" channels, the following check
* would suffice. */
if (!dpif->handlers[0].channels[port_idx].sock) {
return false;
}
ovs_assert(!WINDOWS || dpif->n_handlers <= 1);
pids = xzalloc(dpif->n_handlers * sizeof *pids);
for (i = 0; i < dpif->n_handlers; i++) {
pids[i] = nl_sock_pid(dpif->handlers[i].channels[port_idx].sock);
}
*upcall_pids = pids;
return true;
}
static int
vport_add_channels(struct dpif_netlink *dpif, odp_port_t port_no,
struct nl_sock **socksp)
{
struct epoll_event event;
uint32_t port_idx = odp_to_u32(port_no);
size_t i, j;
int error;
if (dpif->handlers == NULL) {
return 0;
}
/* We assume that the datapath densely chooses port numbers, which can
* therefore be used as an index into 'channels' and 'epoll_events' of
* 'dpif->handler'. */
if (port_idx >= dpif->uc_array_size) {
uint32_t new_size = port_idx + 1;
if (new_size > MAX_PORTS) {
VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
dpif_name(&dpif->dpif), port_no);
return EFBIG;
}
for (i = 0; i < dpif->n_handlers; i++) {
struct dpif_handler *handler = &dpif->handlers[i];
handler->channels = xrealloc(handler->channels,
new_size * sizeof *handler->channels);
for (j = dpif->uc_array_size; j < new_size; j++) {
handler->channels[j].sock = NULL;
}
handler->epoll_events = xrealloc(handler->epoll_events,
new_size * sizeof *handler->epoll_events);
}
dpif->uc_array_size = new_size;
}
memset(&event, 0, sizeof event);
event.events = EPOLLIN;
event.data.u32 = port_idx;
for (i = 0; i < dpif->n_handlers; i++) {
struct dpif_handler *handler = &dpif->handlers[i];
#ifndef _WIN32
if (epoll_ctl(handler->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(socksp[i]),
&event) < 0) {
error = errno;
goto error;
}
#endif
dpif->handlers[i].channels[port_idx].sock = socksp[i];
dpif->handlers[i].channels[port_idx].last_poll = LLONG_MIN;
}
return 0;
error:
for (j = 0; j < i; j++) {
#ifndef _WIN32
epoll_ctl(dpif->handlers[j].epoll_fd, EPOLL_CTL_DEL,
nl_sock_fd(socksp[j]), NULL);
#endif
dpif->handlers[j].channels[port_idx].sock = NULL;
}
return error;
}
static void
vport_del_channels(struct dpif_netlink *dpif, odp_port_t port_no)
{
uint32_t port_idx = odp_to_u32(port_no);
size_t i;
if (!dpif->handlers || port_idx >= dpif->uc_array_size) {
return;
}
/* Since the sock can only be assigned in either all or none
* of "dpif->handlers" channels, the following check would
* suffice. */
if (!dpif->handlers[0].channels[port_idx].sock) {
return;
}
for (i = 0; i < dpif->n_handlers; i++) {
struct dpif_handler *handler = &dpif->handlers[i];
#ifndef _WIN32
epoll_ctl(handler->epoll_fd, EPOLL_CTL_DEL,
nl_sock_fd(handler->channels[port_idx].sock), NULL);
nl_sock_destroy(handler->channels[port_idx].sock);
#endif
handler->channels[port_idx].sock = NULL;
handler->event_offset = handler->n_events = 0;
}
}
static void
destroy_all_channels(struct dpif_netlink *dpif)
OVS_REQ_WRLOCK(dpif->upcall_lock)
{
unsigned int i;
if (!dpif->handlers) {
return;
}
for (i = 0; i < dpif->uc_array_size; i++ ) {
struct dpif_netlink_vport vport_request;
uint32_t upcall_pids = 0;
/* Since the sock can only be assigned in either all or none
* of "dpif->handlers" channels, the following check would
* suffice. */
if (!dpif->handlers[0].channels[i].sock) {
continue;
}
/* Turn off upcalls. */
dpif_netlink_vport_init(&vport_request);
vport_request.cmd = OVS_VPORT_CMD_SET;
vport_request.dp_ifindex = dpif->dp_ifindex;
vport_request.port_no = u32_to_odp(i);
vport_request.n_upcall_pids = 1;
vport_request.upcall_pids = &upcall_pids;
dpif_netlink_vport_transact(&vport_request, NULL, NULL);
vport_del_channels(dpif, u32_to_odp(i));
}
for (i = 0; i < dpif->n_handlers; i++) {
struct dpif_handler *handler = &dpif->handlers[i];
dpif_netlink_handler_uninit(handler);
free(handler->epoll_events);
free(handler->channels);
}
free(dpif->handlers);
dpif->handlers = NULL;
dpif->n_handlers = 0;
dpif->uc_array_size = 0;
}
static void
dpif_netlink_close(struct dpif *dpif_)
{
struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
nl_sock_destroy(dpif->port_notifier);
fat_rwlock_wrlock(&dpif->upcall_lock);
destroy_all_channels(dpif);
fat_rwlock_unlock(&dpif->upcall_lock);
fat_rwlock_destroy(&dpif->upcall_lock);
free(dpif);
}
static int
dpif_netlink_destroy(struct dpif *dpif_)
{
struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
struct dpif_netlink_dp dp;
dpif_netlink_dp_init(&dp);
dp.cmd = OVS_DP_CMD_DEL;
dp.dp_ifindex = dpif->dp_ifindex;
return dpif_netlink_dp_transact(&dp, NULL, NULL);
}
static bool
dpif_netlink_run(struct dpif *dpif_)
{
struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
if (dpif->refresh_channels) {
dpif->refresh_channels = false;
fat_rwlock_wrlock(&dpif->upcall_lock);
dpif_netlink_refresh_channels(dpif, dpif->n_handlers);
fat_rwlock_unlock(&dpif->upcall_lock);
}
return false;
}
static int
dpif_netlink_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
{
struct dpif_netlink_dp dp;
struct ofpbuf *buf;
int error;
error = dpif_netlink_dp_get(dpif_, &dp, &buf);
if (!error) {
memset(stats, 0, sizeof *stats);
if (dp.stats) {
stats->n_hit = get_32aligned_u64(&dp.stats->n_hit);
stats->n_missed = get_32aligned_u64(&dp.stats->n_missed);
stats->n_lost = get_32aligned_u64(&dp.stats->n_lost);
stats->n_flows = get_32aligned_u64(&dp.stats->n_flows);
}
if (dp.megaflow_stats) {
stats->n_masks = dp.megaflow_stats->n_masks;
stats->n_mask_hit = get_32aligned_u64(
&dp.megaflow_stats->n_mask_hit);
} else {
stats->n_masks = UINT32_MAX;
stats->n_mask_hit = UINT64_MAX;
}
ofpbuf_delete(buf);
}
return error;
}
static const char *
get_vport_type(const struct dpif_netlink_vport *vport)
{
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
switch (vport->type) {
case OVS_VPORT_TYPE_NETDEV: {
const char *type = netdev_get_type_from_name(vport->name);
return type ? type : "system";
}
case OVS_VPORT_TYPE_INTERNAL:
return "internal";
case OVS_VPORT_TYPE_GENEVE:
return "geneve";
case OVS_VPORT_TYPE_GRE:
return "gre";
case OVS_VPORT_TYPE_VXLAN:
return "vxlan";
case OVS_VPORT_TYPE_LISP:
return "lisp";
case OVS_VPORT_TYPE_STT:
return "stt";
case OVS_VPORT_TYPE_UNSPEC:
case __OVS_VPORT_TYPE_MAX:
break;
}
VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
vport->dp_ifindex, vport->name, (unsigned int) vport->type);
return "unknown";
}
enum ovs_vport_type
netdev_to_ovs_vport_type(const char *type)
{
if (!strcmp(type, "tap") || !strcmp(type, "system")) {
return OVS_VPORT_TYPE_NETDEV;
} else if (!strcmp(type, "internal")) {
return OVS_VPORT_TYPE_INTERNAL;
} else if (strstr(type, "stt")) {
return OVS_VPORT_TYPE_STT;
} else if (!strcmp(type, "geneve")) {
return OVS_VPORT_TYPE_GENEVE;
} else if (strstr(type, "gre")) {
return OVS_VPORT_TYPE_GRE;
} else if (!strcmp(type, "vxlan")) {
return OVS_VPORT_TYPE_VXLAN;
} else if (!strcmp(type, "lisp")) {
return OVS_VPORT_TYPE_LISP;
} else {
return OVS_VPORT_TYPE_UNSPEC;
}
}
static int
dpif_netlink_port_add__(struct dpif_netlink *dpif, const char *name,
enum ovs_vport_type type,
struct ofpbuf *options,
odp_port_t *port_nop)
OVS_REQ_WRLOCK(dpif->upcall_lock)
{
struct dpif_netlink_vport request, reply;
struct ofpbuf *buf;
struct nl_sock **socksp = NULL;
uint32_t *upcall_pids;
int error = 0;
if (dpif->handlers) {
socksp = vport_create_socksp(dpif, &error);
if (!socksp) {
return error;
}
}
dpif_netlink_vport_init(&request);
request.cmd = OVS_VPORT_CMD_NEW;
request.dp_ifindex = dpif->dp_ifindex;
request.type = type;
request.name = name;
request.port_no = *port_nop;
upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
request.n_upcall_pids = socksp ? dpif->n_handlers : 1;
request.upcall_pids = upcall_pids;
if (options) {
request.options = options->data;
request.options_len = options->size;
}
error = dpif_netlink_vport_transact(&request, &reply, &buf);
if (!error) {
*port_nop = reply.port_no;
} else {
if (error == EBUSY && *port_nop != ODPP_NONE) {
VLOG_INFO("%s: requested port %"PRIu32" is in use",
dpif_name(&dpif->dpif), *port_nop);
}
vport_del_socksp(dpif, socksp);
goto exit;
}
if (socksp) {
error = vport_add_channels(dpif, *port_nop, socksp);
if (error) {
VLOG_INFO("%s: could not add channel for port %s",
dpif_name(&dpif->dpif), name);
/* Delete the port. */
dpif_netlink_vport_init(&request);
request.cmd = OVS_VPORT_CMD_DEL;
request.dp_ifindex = dpif->dp_ifindex;
request.port_no = *port_nop;
dpif_netlink_vport_transact(&request, NULL, NULL);
vport_del_socksp(dpif, socksp);
goto exit;
}
}
free(socksp);
exit:
ofpbuf_delete(buf);
free(upcall_pids);
return error;
}
static int
dpif_netlink_port_add_compat(struct dpif_netlink *dpif, struct netdev *netdev,
odp_port_t *port_nop)
OVS_REQ_WRLOCK(dpif->upcall_lock)
{
const struct netdev_tunnel_config *tnl_cfg;
char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
const char *type = netdev_get_type(netdev);
uint64_t options_stub[64 / 8];
enum ovs_vport_type ovs_type;
struct ofpbuf options;
const char *name;
name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
ovs_type = netdev_to_ovs_vport_type(netdev_get_type(netdev));
if (ovs_type == OVS_VPORT_TYPE_UNSPEC) {
VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
"unsupported type `%s'",
dpif_name(&dpif->dpif), name, type);
return EINVAL;
}
if (ovs_type == OVS_VPORT_TYPE_NETDEV) {
#ifdef _WIN32
/* XXX : Map appropiate Windows handle */
#else
netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
#endif
}
#ifdef _WIN32
if (ovs_type == OVS_VPORT_TYPE_INTERNAL) {
if (!create_wmi_port(name)){
VLOG_ERR("Could not create wmi internal port with name:%s", name);
return EINVAL;
};
}
#endif
tnl_cfg = netdev_get_tunnel_config(netdev);
if (tnl_cfg && (tnl_cfg->dst_port != 0 || tnl_cfg->exts)) {
ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
if (tnl_cfg->dst_port) {
nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
ntohs(tnl_cfg->dst_port));
}
if (tnl_cfg->exts) {
size_t ext_ofs;
int i;
ext_ofs = nl_msg_start_nested(&options, OVS_TUNNEL_ATTR_EXTENSION);
for (i = 0; i < 32; i++) {
if (tnl_cfg->exts & (1 << i)) {
nl_msg_put_flag(&options, i);
}
}
nl_msg_end_nested(&options, ext_ofs);
}
return dpif_netlink_port_add__(dpif, name, ovs_type, &options,
port_nop);
} else {
return dpif_netlink_port_add__(dpif, name, ovs_type, NULL, port_nop);
}
}
static int
dpif_netlink_rtnl_port_create_and_add(struct dpif_netlink *dpif,
struct netdev *netdev,
odp_port_t *port_nop)
OVS_REQ_WRLOCK(dpif->upcall_lock)
{
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
const char *name;
int error;
error = dpif_netlink_rtnl_port_create(netdev);
if (error) {
if (error != EOPNOTSUPP) {
VLOG_WARN_RL(&rl, "Failed to create %s with rtnetlink: %s",
netdev_get_name(netdev), ovs_strerror(error));
}
return error;
}
name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
error = dpif_netlink_port_add__(dpif, name, OVS_VPORT_TYPE_NETDEV, NULL,
port_nop);
if (error) {
dpif_netlink_rtnl_port_destroy(name, netdev_get_type(netdev));
}
return error;
}
static int
dpif_netlink_port_add(struct dpif *dpif_, struct netdev *netdev,
odp_port_t *port_nop)
{
struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
int error = EOPNOTSUPP;
fat_rwlock_wrlock(&dpif->upcall_lock);
if (!ovs_tunnels_out_of_tree) {
error = dpif_netlink_rtnl_port_create_and_add(dpif, netdev, port_nop);
}