forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetdev-linux.c
6636 lines (5741 loc) · 201 KB
/
netdev-linux.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, 2014, 2015, 2016, 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 "netdev-linux.h"
#include "netdev-linux-private.h"
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <inttypes.h>
#include <math.h>
#include <linux/filter.h>
#include <linux/gen_stats.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/if_tun.h>
#include <linux/types.h>
#include <linux/ethtool.h>
#include <linux/mii.h>
#include <linux/rtnetlink.h>
#include <linux/sockios.h>
#include <linux/virtio_net.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/utsname.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/route.h>
#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "coverage.h"
#include "dp-packet.h"
#include "dpif-netlink.h"
#include "dpif-netdev.h"
#include "openvswitch/dynamic-string.h"
#include "fatal-signal.h"
#include "hash.h"
#include "openvswitch/hmap.h"
#include "netdev-afxdp.h"
#include "netdev-provider.h"
#include "netdev-vport.h"
#include "netlink-notifier.h"
#include "netlink-socket.h"
#include "netlink.h"
#include "netnsid.h"
#include "openvswitch/ofpbuf.h"
#include "openflow/openflow.h"
#include "ovs-atomic.h"
#include "ovs-numa.h"
#include "packets.h"
#include "openvswitch/poll-loop.h"
#include "rtnetlink.h"
#include "openvswitch/shash.h"
#include "socket-util.h"
#include "sset.h"
#include "tc.h"
#include "timer.h"
#include "unaligned.h"
#include "openvswitch/vlog.h"
#include "userspace-tso.h"
#include "util.h"
VLOG_DEFINE_THIS_MODULE(netdev_linux);
COVERAGE_DEFINE(netdev_set_policing);
COVERAGE_DEFINE(netdev_arp_lookup);
COVERAGE_DEFINE(netdev_get_ifindex);
COVERAGE_DEFINE(netdev_get_hwaddr);
COVERAGE_DEFINE(netdev_set_hwaddr);
COVERAGE_DEFINE(netdev_get_ethtool);
COVERAGE_DEFINE(netdev_set_ethtool);
#ifndef IFLA_IF_NETNSID
#define IFLA_IF_NETNSID 0x45
#endif
/* These were introduced in Linux 2.6.14, so they might be missing if we have
* old headers. */
#ifndef ADVERTISED_Pause
#define ADVERTISED_Pause (1 << 13)
#endif
#ifndef ADVERTISED_Asym_Pause
#define ADVERTISED_Asym_Pause (1 << 14)
#endif
/* These were introduced in Linux 2.6.24, so they might be missing if we
* have old headers. */
#ifndef ETHTOOL_GFLAGS
#define ETHTOOL_GFLAGS 0x00000025 /* Get flags bitmap(ethtool_value) */
#endif
#ifndef ETHTOOL_SFLAGS
#define ETHTOOL_SFLAGS 0x00000026 /* Set flags bitmap(ethtool_value) */
#endif
/* This was introduced in Linux 2.6.25, so it might be missing if we have old
* headers. */
#ifndef TC_RTAB_SIZE
#define TC_RTAB_SIZE 1024
#endif
/* Linux 2.6.21 introduced struct tpacket_auxdata.
* Linux 2.6.27 added the tp_vlan_tci member.
* Linux 3.0 defined TP_STATUS_VLAN_VALID.
* Linux 3.13 repurposed a padding member for tp_vlan_tpid and defined
* TP_STATUS_VLAN_TPID_VALID.
*
* With all this churn it's easiest to unconditionally define a replacement
* structure that has everything we want.
*/
#ifndef PACKET_AUXDATA
#define PACKET_AUXDATA 8
#endif
#ifndef TP_STATUS_VLAN_VALID
#define TP_STATUS_VLAN_VALID (1 << 4)
#endif
#ifndef TP_STATUS_VLAN_TPID_VALID
#define TP_STATUS_VLAN_TPID_VALID (1 << 6)
#endif
#undef tpacket_auxdata
#define tpacket_auxdata rpl_tpacket_auxdata
struct tpacket_auxdata {
uint32_t tp_status;
uint32_t tp_len;
uint32_t tp_snaplen;
uint16_t tp_mac;
uint16_t tp_net;
uint16_t tp_vlan_tci;
uint16_t tp_vlan_tpid;
};
/* Linux 2.6.27 introduced ethtool_cmd_speed
*
* To avoid revisiting problems reported with using configure to detect
* compatibility (see report at
* https://mail.openvswitch.org/pipermail/ovs-dev/2014-October/291521.html)
* unconditionally replace ethtool_cmd_speed. */
#define ethtool_cmd_speed rpl_ethtool_cmd_speed
static inline uint32_t rpl_ethtool_cmd_speed(const struct ethtool_cmd *ep)
{
return ep->speed | (ep->speed_hi << 16);
}
/* Linux 2.6.30 introduced supported and advertised flags for
* 1G base KX, and 10G base KX4, KR and R. */
#ifndef SUPPORTED_1000baseKX_Full
#define SUPPORTED_1000baseKX_Full (1 << 17)
#define SUPPORTED_10000baseKX4_Full (1 << 18)
#define SUPPORTED_10000baseKR_Full (1 << 19)
#define SUPPORTED_10000baseR_FEC (1 << 20)
#define ADVERTISED_1000baseKX_Full (1 << 17)
#define ADVERTISED_10000baseKX4_Full (1 << 18)
#define ADVERTISED_10000baseKR_Full (1 << 19)
#define ADVERTISED_10000baseR_FEC (1 << 20)
#endif
/* Linux 3.5 introduced supported and advertised flags for
* 40G base KR4, CR4, SR4 and LR4. */
#ifndef SUPPORTED_40000baseKR4_Full
#define SUPPORTED_40000baseKR4_Full (1 << 23)
#define SUPPORTED_40000baseCR4_Full (1 << 24)
#define SUPPORTED_40000baseSR4_Full (1 << 25)
#define SUPPORTED_40000baseLR4_Full (1 << 26)
#define ADVERTISED_40000baseKR4_Full (1 << 23)
#define ADVERTISED_40000baseCR4_Full (1 << 24)
#define ADVERTISED_40000baseSR4_Full (1 << 25)
#define ADVERTISED_40000baseLR4_Full (1 << 26)
#endif
/* Linux 2.6.35 introduced IFLA_STATS64 and rtnl_link_stats64.
*
* Tests for rtnl_link_stats64 don't seem to consistently work, e.g. on
* 2.6.32-431.29.2.el6.x86_64 (see report at
* https://mail.openvswitch.org/pipermail/ovs-dev/2014-October/291521.html).
* Maybe if_link.h is not self-contained on those kernels. It is easiest to
* unconditionally define a replacement. */
#ifndef IFLA_STATS64
#define IFLA_STATS64 23
#endif
#define rtnl_link_stats64 rpl_rtnl_link_stats64
struct rtnl_link_stats64 {
uint64_t rx_packets;
uint64_t tx_packets;
uint64_t rx_bytes;
uint64_t tx_bytes;
uint64_t rx_errors;
uint64_t tx_errors;
uint64_t rx_dropped;
uint64_t tx_dropped;
uint64_t multicast;
uint64_t collisions;
uint64_t rx_length_errors;
uint64_t rx_over_errors;
uint64_t rx_crc_errors;
uint64_t rx_frame_errors;
uint64_t rx_fifo_errors;
uint64_t rx_missed_errors;
uint64_t tx_aborted_errors;
uint64_t tx_carrier_errors;
uint64_t tx_fifo_errors;
uint64_t tx_heartbeat_errors;
uint64_t tx_window_errors;
uint64_t rx_compressed;
uint64_t tx_compressed;
};
/* Linux 3.19 introduced virtio_types.h. It might be missing
* if we are using old kernel. */
#ifndef HAVE_VIRTIO_TYPES
typedef __u16 __bitwise__ __virtio16;
typedef __u32 __bitwise__ __virtio32;
typedef __u64 __bitwise__ __virtio64;
#endif
enum {
VALID_IFINDEX = 1 << 0,
VALID_ETHERADDR = 1 << 1,
VALID_IN = 1 << 2,
VALID_MTU = 1 << 3,
VALID_POLICING = 1 << 4,
VALID_VPORT_STAT_ERROR = 1 << 5,
VALID_DRVINFO = 1 << 6,
VALID_FEATURES = 1 << 7,
VALID_NUMA_ID = 1 << 8,
};
/* Use one for the packet buffer and another for the aux buffer to receive
* TSO packets. */
#define IOV_STD_SIZE 1
#define IOV_TSO_SIZE 2
enum {
IOV_PACKET = 0,
IOV_AUXBUF = 1,
};
struct linux_lag_slave {
uint32_t block_id;
struct shash_node *node;
};
/* Protects 'lag_shash' and the mutable members of struct linux_lag_slave. */
static struct ovs_mutex lag_mutex = OVS_MUTEX_INITIALIZER;
/* All slaves whose LAG masters are network devices in OvS. */
static struct shash lag_shash OVS_GUARDED_BY(lag_mutex)
= SHASH_INITIALIZER(&lag_shash);
/* Traffic control. */
/* An instance of a traffic control class. Always associated with a particular
* network device.
*
* Each TC implementation subclasses this with whatever additional data it
* needs. */
struct tc {
const struct tc_ops *ops;
struct hmap queues; /* Contains "struct tc_queue"s.
* Read by generic TC layer.
* Written only by TC implementation. */
};
#define TC_INITIALIZER(TC, OPS) { OPS, HMAP_INITIALIZER(&(TC)->queues) }
/* One traffic control queue.
*
* Each TC implementation subclasses this with whatever additional data it
* needs. */
struct tc_queue {
struct hmap_node hmap_node; /* In struct tc's "queues" hmap. */
unsigned int queue_id; /* OpenFlow queue ID. */
long long int created; /* Time queue was created, in msecs. */
};
/* A particular kind of traffic control. Each implementation generally maps to
* one particular Linux qdisc class.
*
* The functions below return 0 if successful or a positive errno value on
* failure, except where otherwise noted. All of them must be provided, except
* where otherwise noted. */
struct tc_ops {
/* Name used by kernel in the TCA_KIND attribute of tcmsg, e.g. "htb".
* This is null for tc_ops_default and tc_ops_other, for which there are no
* appropriate values. */
const char *linux_name;
/* Name used in OVS database, e.g. "linux-htb". Must be nonnull. */
const char *ovs_name;
/* Number of supported OpenFlow queues, 0 for qdiscs that have no
* queues. The queues are numbered 0 through n_queues - 1. */
unsigned int n_queues;
/* Called to install this TC class on 'netdev'. The implementation should
* make the Netlink calls required to set up 'netdev' with the right qdisc
* and configure it according to 'details'. The implementation may assume
* that the current qdisc is the default; that is, there is no need for it
* to delete the current qdisc before installing itself.
*
* The contents of 'details' should be documented as valid for 'ovs_name'
* in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
* (which is built as ovs-vswitchd.conf.db(8)).
*
* This function must return 0 if and only if it sets 'netdev->tc' to an
* initialized 'struct tc'.
*
* (This function is null for tc_ops_other, which cannot be installed. For
* other TC classes it should always be nonnull.) */
int (*tc_install)(struct netdev *netdev, const struct smap *details);
/* Called when the netdev code determines (through a Netlink query) that
* this TC class's qdisc is installed on 'netdev', but we didn't install
* it ourselves and so don't know any of the details.
*
* 'nlmsg' is the kernel reply to a RTM_GETQDISC Netlink message for
* 'netdev'. The TCA_KIND attribute of 'nlmsg' is 'linux_name'. The
* implementation should parse the other attributes of 'nlmsg' as
* necessary to determine its configuration. If necessary it should also
* use Netlink queries to determine the configuration of queues on
* 'netdev'.
*
* This function must return 0 if and only if it sets 'netdev->tc' to an
* initialized 'struct tc'. */
int (*tc_load)(struct netdev *netdev, struct ofpbuf *nlmsg);
/* Destroys the data structures allocated by the implementation as part of
* 'tc'. (This includes destroying 'tc->queues' by calling
* tc_destroy(tc).
*
* The implementation should not need to perform any Netlink calls. If
* desirable, the caller is responsible for deconfiguring the kernel qdisc.
* (But it may not be desirable.)
*
* This function may be null if 'tc' is trivial. */
void (*tc_destroy)(struct tc *tc);
/* Retrieves details of 'netdev->tc' configuration into 'details'.
*
* The implementation should not need to perform any Netlink calls, because
* the 'tc_install' or 'tc_load' that instantiated 'netdev->tc' should have
* cached the configuration.
*
* The contents of 'details' should be documented as valid for 'ovs_name'
* in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
* (which is built as ovs-vswitchd.conf.db(8)).
*
* This function may be null if 'tc' is not configurable.
*/
int (*qdisc_get)(const struct netdev *netdev, struct smap *details);
/* Reconfigures 'netdev->tc' according to 'details', performing any
* required Netlink calls to complete the reconfiguration.
*
* The contents of 'details' should be documented as valid for 'ovs_name'
* in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
* (which is built as ovs-vswitchd.conf.db(8)).
*
* This function may be null if 'tc' is not configurable.
*/
int (*qdisc_set)(struct netdev *, const struct smap *details);
/* Retrieves details of 'queue' on 'netdev->tc' into 'details'. 'queue' is
* one of the 'struct tc_queue's within 'netdev->tc->queues'.
*
* The contents of 'details' should be documented as valid for 'ovs_name'
* in the "other_config" column in the "Queue" table in
* vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
*
* The implementation should not need to perform any Netlink calls, because
* the 'tc_install' or 'tc_load' that instantiated 'netdev->tc' should have
* cached the queue configuration.
*
* This function may be null if 'tc' does not have queues ('n_queues' is
* 0). */
int (*class_get)(const struct netdev *netdev, const struct tc_queue *queue,
struct smap *details);
/* Configures or reconfigures 'queue_id' on 'netdev->tc' according to
* 'details', perfoming any required Netlink calls to complete the
* reconfiguration. The caller ensures that 'queue_id' is less than
* 'n_queues'.
*
* The contents of 'details' should be documented as valid for 'ovs_name'
* in the "other_config" column in the "Queue" table in
* vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
*
* This function may be null if 'tc' does not have queues or its queues are
* not configurable. */
int (*class_set)(struct netdev *, unsigned int queue_id,
const struct smap *details);
/* Deletes 'queue' from 'netdev->tc'. 'queue' is one of the 'struct
* tc_queue's within 'netdev->tc->queues'.
*
* This function may be null if 'tc' does not have queues or its queues
* cannot be deleted. */
int (*class_delete)(struct netdev *, struct tc_queue *queue);
/* Obtains stats for 'queue' from 'netdev->tc'. 'queue' is one of the
* 'struct tc_queue's within 'netdev->tc->queues'.
*
* On success, initializes '*stats'.
*
* This function may be null if 'tc' does not have queues or if it cannot
* report queue statistics. */
int (*class_get_stats)(const struct netdev *netdev,
const struct tc_queue *queue,
struct netdev_queue_stats *stats);
/* Extracts queue stats from 'nlmsg', which is a response to a
* RTM_GETTCLASS message, and passes them to 'cb' along with 'aux'.
*
* This function may be null if 'tc' does not have queues or if it cannot
* report queue statistics. */
int (*class_dump_stats)(const struct netdev *netdev,
const struct ofpbuf *nlmsg,
netdev_dump_queue_stats_cb *cb, void *aux);
};
static void
tc_init(struct tc *tc, const struct tc_ops *ops)
{
tc->ops = ops;
hmap_init(&tc->queues);
}
static void
tc_destroy(struct tc *tc)
{
hmap_destroy(&tc->queues);
}
static const struct tc_ops tc_ops_htb;
static const struct tc_ops tc_ops_hfsc;
static const struct tc_ops tc_ops_codel;
static const struct tc_ops tc_ops_fqcodel;
static const struct tc_ops tc_ops_sfq;
static const struct tc_ops tc_ops_netem;
static const struct tc_ops tc_ops_default;
static const struct tc_ops tc_ops_noop;
static const struct tc_ops tc_ops_other;
static const struct tc_ops *const tcs[] = {
&tc_ops_htb, /* Hierarchy token bucket (see tc-htb(8)). */
&tc_ops_hfsc, /* Hierarchical fair service curve. */
&tc_ops_codel, /* Controlled delay */
&tc_ops_fqcodel, /* Fair queue controlled delay */
&tc_ops_sfq, /* Stochastic fair queueing */
&tc_ops_netem, /* Network Emulator */
&tc_ops_noop, /* Non operating qos type. */
&tc_ops_default, /* Default qdisc (see tc-pfifo_fast(8)). */
&tc_ops_other, /* Some other qdisc. */
NULL
};
static unsigned int tc_ticks_to_bytes(unsigned int rate, unsigned int ticks);
static unsigned int tc_bytes_to_ticks(unsigned int rate, unsigned int size);
static unsigned int tc_buffer_per_jiffy(unsigned int rate);
static uint32_t tc_time_to_ticks(uint32_t time);
static struct tcmsg *netdev_linux_tc_make_request(const struct netdev *,
int type,
unsigned int flags,
struct ofpbuf *);
static int tc_add_policer(struct netdev *,
uint32_t kbits_rate, uint32_t kbits_burst);
static int tc_parse_qdisc(const struct ofpbuf *, const char **kind,
struct nlattr **options);
static int tc_parse_class(const struct ofpbuf *, unsigned int *queue_id,
struct nlattr **options,
struct netdev_queue_stats *);
static int tc_query_class(const struct netdev *,
unsigned int handle, unsigned int parent,
struct ofpbuf **replyp);
static int tc_delete_class(const struct netdev *, unsigned int handle);
static int tc_del_qdisc(struct netdev *netdev);
static int tc_query_qdisc(const struct netdev *netdev);
void
tc_put_rtab(struct ofpbuf *msg, uint16_t type, const struct tc_ratespec *rate);
static int tc_calc_cell_log(unsigned int mtu);
static void tc_fill_rate(struct tc_ratespec *rate, uint64_t bps, int mtu);
static int tc_calc_buffer(unsigned int Bps, int mtu, uint64_t burst_bytes);
/* This is set pretty low because we probably won't learn anything from the
* additional log messages. */
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
/* Polling miimon status for all ports causes performance degradation when
* handling a large number of ports. If there are no devices using miimon, then
* we skip netdev_linux_miimon_run() and netdev_linux_miimon_wait().
*
* Readers do not depend on this variable synchronizing with the related
* changes in the device miimon status, so we can use atomic_count. */
static atomic_count miimon_cnt = ATOMIC_COUNT_INIT(0);
static int netdev_linux_parse_vnet_hdr(struct dp_packet *b);
static void netdev_linux_prepend_vnet_hdr(struct dp_packet *b, int mtu);
static int netdev_linux_do_ethtool(const char *name, struct ethtool_cmd *,
int cmd, const char *cmd_name);
static int get_flags(const struct netdev *, unsigned int *flags);
static int set_flags(const char *, unsigned int flags);
static int update_flags(struct netdev_linux *netdev, enum netdev_flags off,
enum netdev_flags on, enum netdev_flags *old_flagsp)
OVS_REQUIRES(netdev->mutex);
static int get_ifindex(const struct netdev *, int *ifindexp);
static int do_set_addr(struct netdev *netdev,
int ioctl_nr, const char *ioctl_name,
struct in_addr addr);
static int get_etheraddr(const char *netdev_name, struct eth_addr *ea);
static int set_etheraddr(const char *netdev_name, const struct eth_addr);
static int af_packet_sock(void);
static bool netdev_linux_miimon_enabled(void);
static void netdev_linux_miimon_run(void);
static void netdev_linux_miimon_wait(void);
static int netdev_linux_get_mtu__(struct netdev_linux *netdev, int *mtup);
static bool
is_tap_netdev(const struct netdev *netdev)
{
return netdev_get_class(netdev) == &netdev_tap_class;
}
static int
netdev_linux_netnsid_update__(struct netdev_linux *netdev)
{
struct dpif_netlink_vport reply;
struct ofpbuf *buf;
int error;
error = dpif_netlink_vport_get(netdev_get_name(&netdev->up), &reply, &buf);
if (error) {
if (error == ENOENT) {
/* Assume it is local if there is no API (e.g. if the openvswitch
* kernel module is not loaded). */
netnsid_set_local(&netdev->netnsid);
} else {
netnsid_unset(&netdev->netnsid);
}
return error;
}
netnsid_set(&netdev->netnsid, reply.netnsid);
ofpbuf_delete(buf);
return 0;
}
static int
netdev_linux_netnsid_update(struct netdev_linux *netdev)
{
if (netnsid_is_unset(netdev->netnsid)) {
if (netdev_get_class(&netdev->up) == &netdev_tap_class) {
netnsid_set_local(&netdev->netnsid);
} else {
return netdev_linux_netnsid_update__(netdev);
}
}
return 0;
}
static bool
netdev_linux_netnsid_is_eq(struct netdev_linux *netdev, int nsid)
{
netdev_linux_netnsid_update(netdev);
return netnsid_eq(netdev->netnsid, nsid);
}
static bool
netdev_linux_netnsid_is_remote(struct netdev_linux *netdev)
{
netdev_linux_netnsid_update(netdev);
return netnsid_is_remote(netdev->netnsid);
}
static int netdev_linux_update_via_netlink(struct netdev_linux *);
static void netdev_linux_update(struct netdev_linux *netdev, int,
const struct rtnetlink_change *)
OVS_REQUIRES(netdev->mutex);
static void netdev_linux_changed(struct netdev_linux *netdev,
unsigned int ifi_flags, unsigned int mask)
OVS_REQUIRES(netdev->mutex);
/* Returns a NETLINK_ROUTE socket listening for RTNLGRP_LINK,
* RTNLGRP_IPV4_IFADDR and RTNLGRP_IPV6_IFADDR changes, or NULL
* if no such socket could be created. */
static struct nl_sock *
netdev_linux_notify_sock(void)
{
static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
static struct nl_sock *sock;
unsigned int mcgroups[] = {RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR,
RTNLGRP_IPV6_IFADDR, RTNLGRP_IPV6_IFINFO};
if (ovsthread_once_start(&once)) {
int error;
error = nl_sock_create(NETLINK_ROUTE, &sock);
if (!error) {
size_t i;
for (i = 0; i < ARRAY_SIZE(mcgroups); i++) {
error = nl_sock_join_mcgroup(sock, mcgroups[i]);
if (error) {
nl_sock_destroy(sock);
sock = NULL;
break;
}
}
}
nl_sock_listen_all_nsid(sock, true);
ovsthread_once_done(&once);
}
return sock;
}
static bool
netdev_linux_miimon_enabled(void)
{
return atomic_count_get(&miimon_cnt) > 0;
}
static bool
netdev_linux_kind_is_lag(const char *kind)
{
if (!strcmp(kind, "bond") || !strcmp(kind, "team")) {
return true;
}
return false;
}
static void
netdev_linux_update_lag(struct rtnetlink_change *change)
OVS_REQUIRES(lag_mutex)
{
struct linux_lag_slave *lag;
if (change->slave && netdev_linux_kind_is_lag(change->slave)) {
lag = shash_find_data(&lag_shash, change->ifname);
if (!lag) {
struct netdev *master_netdev;
char master_name[IFNAMSIZ];
uint32_t block_id;
int error = 0;
if_indextoname(change->master_ifindex, master_name);
master_netdev = netdev_from_name(master_name);
if (!master_netdev) {
return;
}
if (is_netdev_linux_class(master_netdev->netdev_class)) {
block_id = netdev_get_block_id(master_netdev);
if (!block_id) {
netdev_close(master_netdev);
return;
}
lag = xmalloc(sizeof *lag);
lag->block_id = block_id;
lag->node = shash_add(&lag_shash, change->ifname, lag);
/* delete ingress block in case it exists */
tc_add_del_qdisc(change->if_index, false, 0, TC_INGRESS);
/* LAG master is linux netdev so add slave to same block. */
error = tc_add_del_qdisc(change->if_index, true, block_id,
TC_INGRESS);
if (error) {
VLOG_WARN("failed to bind LAG slave %s to master's block",
change->ifname);
shash_delete(&lag_shash, lag->node);
free(lag);
}
}
netdev_close(master_netdev);
}
} else if (change->master_ifindex == 0) {
/* Check if this was a lag slave that has been freed. */
lag = shash_find_data(&lag_shash, change->ifname);
if (lag) {
tc_add_del_qdisc(change->if_index, false, lag->block_id,
TC_INGRESS);
shash_delete(&lag_shash, lag->node);
free(lag);
}
}
}
void
netdev_linux_run(const struct netdev_class *netdev_class OVS_UNUSED)
{
struct nl_sock *sock;
int error;
if (netdev_linux_miimon_enabled()) {
netdev_linux_miimon_run();
}
sock = netdev_linux_notify_sock();
if (!sock) {
return;
}
do {
uint64_t buf_stub[4096 / 8];
int nsid;
struct ofpbuf buf;
ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
error = nl_sock_recv(sock, &buf, &nsid, false);
if (!error) {
struct rtnetlink_change change;
if (rtnetlink_parse(&buf, &change)) {
struct netdev *netdev_ = NULL;
char dev_name[IFNAMSIZ];
if (!change.ifname) {
change.ifname = if_indextoname(change.if_index, dev_name);
}
if (change.ifname) {
netdev_ = netdev_from_name(change.ifname);
}
if (netdev_ && is_netdev_linux_class(netdev_->netdev_class)) {
struct netdev_linux *netdev = netdev_linux_cast(netdev_);
ovs_mutex_lock(&netdev->mutex);
netdev_linux_update(netdev, nsid, &change);
ovs_mutex_unlock(&netdev->mutex);
}
if (change.ifname &&
rtnetlink_type_is_rtnlgrp_link(change.nlmsg_type)) {
/* Need to try updating the LAG information. */
ovs_mutex_lock(&lag_mutex);
netdev_linux_update_lag(&change);
ovs_mutex_unlock(&lag_mutex);
}
netdev_close(netdev_);
}
} else if (error == ENOBUFS) {
struct shash device_shash;
struct shash_node *node;
nl_sock_drain(sock);
shash_init(&device_shash);
netdev_get_devices(&netdev_linux_class, &device_shash);
SHASH_FOR_EACH (node, &device_shash) {
struct netdev *netdev_ = node->data;
struct netdev_linux *netdev = netdev_linux_cast(netdev_);
unsigned int flags;
ovs_mutex_lock(&netdev->mutex);
get_flags(netdev_, &flags);
netdev_linux_changed(netdev, flags, 0);
ovs_mutex_unlock(&netdev->mutex);
netdev_close(netdev_);
}
shash_destroy(&device_shash);
} else if (error != EAGAIN) {
static struct vlog_rate_limit rll = VLOG_RATE_LIMIT_INIT(1, 5);
VLOG_WARN_RL(&rll, "error reading or parsing netlink (%s)",
ovs_strerror(error));
}
ofpbuf_uninit(&buf);
} while (!error);
}
static void
netdev_linux_wait(const struct netdev_class *netdev_class OVS_UNUSED)
{
struct nl_sock *sock;
if (netdev_linux_miimon_enabled()) {
netdev_linux_miimon_wait();
}
sock = netdev_linux_notify_sock();
if (sock) {
nl_sock_wait(sock, POLLIN);
}
}
static void
netdev_linux_changed(struct netdev_linux *dev,
unsigned int ifi_flags, unsigned int mask)
OVS_REQUIRES(dev->mutex)
{
netdev_change_seq_changed(&dev->up);
if ((dev->ifi_flags ^ ifi_flags) & IFF_RUNNING) {
dev->carrier_resets++;
}
dev->ifi_flags = ifi_flags;
dev->cache_valid &= mask;
if (!(mask & VALID_IN)) {
netdev_get_addrs_list_flush();
}
}
static void
netdev_linux_update__(struct netdev_linux *dev,
const struct rtnetlink_change *change)
OVS_REQUIRES(dev->mutex)
{
if (rtnetlink_type_is_rtnlgrp_link(change->nlmsg_type)) {
if (change->nlmsg_type == RTM_NEWLINK) {
/* Keep drv-info, ip addresses, and NUMA id. */
netdev_linux_changed(dev, change->ifi_flags,
VALID_DRVINFO | VALID_IN | VALID_NUMA_ID);
/* Update netdev from rtnl-change msg. */
if (change->mtu) {
dev->mtu = change->mtu;
dev->cache_valid |= VALID_MTU;
dev->netdev_mtu_error = 0;
}
if (!eth_addr_is_zero(change->mac)) {
dev->etheraddr = change->mac;
dev->cache_valid |= VALID_ETHERADDR;
dev->ether_addr_error = 0;
/* The mac addr has been changed, report it now. */
rtnetlink_report_link();
}
if (change->master && netdev_linux_kind_is_lag(change->master)) {
dev->is_lag_master = true;
}
dev->ifindex = change->if_index;
dev->cache_valid |= VALID_IFINDEX;
dev->get_ifindex_error = 0;
dev->present = true;
} else {
/* FIXME */
netdev_linux_changed(dev, change->ifi_flags, 0);
dev->present = false;
netnsid_unset(&dev->netnsid);
}
} else if (rtnetlink_type_is_rtnlgrp_addr(change->nlmsg_type)) {
/* Invalidates in4, in6. */
netdev_linux_changed(dev, dev->ifi_flags, ~VALID_IN);
} else {
OVS_NOT_REACHED();
}
}
static void
netdev_linux_update(struct netdev_linux *dev, int nsid,
const struct rtnetlink_change *change)
OVS_REQUIRES(dev->mutex)
{
if (netdev_linux_netnsid_is_eq(dev, nsid)) {
netdev_linux_update__(dev, change);
}
}
static struct netdev *
netdev_linux_alloc(void)
{
struct netdev_linux *netdev = xzalloc(sizeof *netdev);
return &netdev->up;
}
static int
netdev_linux_common_construct(struct netdev *netdev_)
{
/* Prevent any attempt to create (or open) a network device named "default"
* or "all". These device names are effectively reserved on Linux because
* /proc/sys/net/ipv4/conf/ always contains directories by these names. By
* itself this wouldn't call for any special treatment, but in practice if
* a program tries to create devices with these names, it causes the kernel
* to fire a "new device" notification event even though creation failed,
* and in turn that causes OVS to wake up and try to create them again,
* which ends up as a 100% CPU loop. */
struct netdev_linux *netdev = netdev_linux_cast(netdev_);
const char *name = netdev_->name;
if (!strcmp(name, "default") || !strcmp(name, "all")) {
static struct vlog_rate_limit rll = VLOG_RATE_LIMIT_INIT(1, 1);
VLOG_WARN_RL(&rll, "%s: Linux forbids network device with this name",
name);
return EINVAL;
}
/* The device could be in the same network namespace or in another one. */
netnsid_unset(&netdev->netnsid);
ovs_mutex_init(&netdev->mutex);
if (userspace_tso_enabled()) {
netdev_->ol_flags |= NETDEV_TX_OFFLOAD_TCP_TSO;
netdev_->ol_flags |= NETDEV_TX_OFFLOAD_TCP_CKSUM;
netdev_->ol_flags |= NETDEV_TX_OFFLOAD_UDP_CKSUM;
netdev_->ol_flags |= NETDEV_TX_OFFLOAD_SCTP_CKSUM;
netdev_->ol_flags |= NETDEV_TX_OFFLOAD_IPV4_CKSUM;
}
return 0;
}
/* Creates system and internal devices. */
int
netdev_linux_construct(struct netdev *netdev_)
{
struct netdev_linux *netdev = netdev_linux_cast(netdev_);
int error = netdev_linux_common_construct(netdev_);
if (error) {
return error;
}
error = get_flags(&netdev->up, &netdev->ifi_flags);
if (error == ENODEV) {
if (netdev->up.netdev_class != &netdev_internal_class) {
/* The device does not exist, so don't allow it to be opened. */
return ENODEV;
} else {
/* "Internal" netdevs have to be created as netdev objects before
* they exist in the kernel, because creating them in the kernel
* happens by passing a netdev object to dpif_port_add().
* Therefore, ignore the error. */
}
}
return 0;
}
/* For most types of netdevs we open the device for each call of
* netdev_open(). However, this is not the case with tap devices,
* since it is only possible to open the device once. In this
* situation we share a single file descriptor, and consequently
* buffers, across all readers. Therefore once data is read it will
* be unavailable to other reads for tap devices. */
static int
netdev_linux_construct_tap(struct netdev *netdev_)
{
struct netdev_linux *netdev = netdev_linux_cast(netdev_);
static const char tap_dev[] = "/dev/net/tun";
const char *name = netdev_->name;
struct ifreq ifr;
int error = netdev_linux_common_construct(netdev_);
if (error) {
return error;
}
/* Open tap device. */
netdev->tap_fd = open(tap_dev, O_RDWR);
if (netdev->tap_fd < 0) {
error = errno;
VLOG_WARN("opening \"%s\" failed: %s", tap_dev, ovs_strerror(error));
return error;
}
/* Create tap device. */
get_flags(&netdev->up, &netdev->ifi_flags);
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
if (userspace_tso_enabled()) {
ifr.ifr_flags |= IFF_VNET_HDR;
}
ovs_strzcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
if (ioctl(netdev->tap_fd, TUNSETIFF, &ifr) == -1) {
VLOG_WARN("%s: creating tap device failed: %s", name,
ovs_strerror(errno));