forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetdev-linux.c
4591 lines (3991 loc) · 140 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 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 <errno.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <inttypes.h>
#include <linux/filter.h>
#include <linux/gen_stats.h>
#include <linux/if_ether.h>
#include <linux/if_tun.h>
#include <linux/types.h>
#include <linux/ethtool.h>
#include <linux/mii.h>
#include <linux/pkt_cls.h>
#include <linux/pkt_sched.h>
#include <linux/rtnetlink.h>
#include <linux/sockios.h>
#include <linux/version.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/if_packet.h>
#include <net/route.h>
#include <netinet/in.h>
#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "coverage.h"
#include "dpif-linux.h"
#include "dynamic-string.h"
#include "fatal-signal.h"
#include "hash.h"
#include "hmap.h"
#include "netdev-provider.h"
#include "netdev-vport.h"
#include "netlink-notifier.h"
#include "netlink-socket.h"
#include "netlink.h"
#include "ofpbuf.h"
#include "openflow/openflow.h"
#include "packets.h"
#include "poll-loop.h"
#include "rtnetlink-link.h"
#include "shash.h"
#include "socket-util.h"
#include "sset.h"
#include "timer.h"
#include "unaligned.h"
#include "vlog.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);
/* 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
static struct nln_notifier *netdev_linux_cache_notifier = NULL;
static int cache_notifier_refcount;
enum {
VALID_IFINDEX = 1 << 0,
VALID_ETHERADDR = 1 << 1,
VALID_IN4 = 1 << 2,
VALID_IN6 = 1 << 3,
VALID_MTU = 1 << 4,
VALID_POLICING = 1 << 5,
VALID_VPORT_STAT_ERROR = 1 << 6,
VALID_DRVINFO = 1 << 7,
VALID_FEATURES = 1 << 8,
};
struct tap_state {
int fd;
};
/* 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. */
};
/* 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_default;
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_default, /* Default qdisc (see tc-pfifo_fast(8)). */
&tc_ops_other, /* Some other qdisc. */
NULL
};
static unsigned int tc_make_handle(unsigned int major, unsigned int minor);
static unsigned int tc_get_major(unsigned int handle);
static unsigned int tc_get_minor(unsigned int handle);
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 struct tcmsg *tc_make_request(const struct netdev *, int type,
unsigned int flags, struct ofpbuf *);
static int tc_transact(struct ofpbuf *request, struct ofpbuf **replyp);
static int tc_add_del_ingress_qdisc(struct netdev *netdev, bool add);
static int tc_add_policer(struct netdev *netdev, int kbits_rate,
int 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);
static int tc_calc_cell_log(unsigned int mtu);
static void tc_fill_rate(struct tc_ratespec *rate, uint64_t bps, int mtu);
static void tc_put_rtab(struct ofpbuf *, uint16_t type,
const struct tc_ratespec *rate);
static int tc_calc_buffer(unsigned int Bps, int mtu, uint64_t burst_bytes);
struct netdev_linux {
struct netdev up;
struct shash_node *shash_node;
unsigned int cache_valid;
unsigned int change_seq;
bool miimon; /* Link status of last poll. */
long long int miimon_interval; /* Miimon Poll rate. Disabled if <= 0. */
struct timer miimon_timer;
/* The following are figured out "on demand" only. They are only valid
* when the corresponding VALID_* bit in 'cache_valid' is set. */
int ifindex;
uint8_t etheraddr[ETH_ADDR_LEN];
struct in_addr address, netmask;
struct in6_addr in6;
int mtu;
unsigned int ifi_flags;
long long int carrier_resets;
uint32_t kbits_rate; /* Policing data. */
uint32_t kbits_burst;
int vport_stats_error; /* Cached error code from vport_get_stats().
0 or an errno value. */
int netdev_mtu_error; /* Cached error code from SIOCGIFMTU or SIOCSIFMTU. */
int ether_addr_error; /* Cached error code from set/get etheraddr. */
int netdev_policing_error; /* Cached error code from set policing. */
int get_features_error; /* Cached error code from ETHTOOL_GSET. */
int get_ifindex_error; /* Cached error code from SIOCGIFINDEX. */
enum netdev_features current; /* Cached from ETHTOOL_GSET. */
enum netdev_features advertised; /* Cached from ETHTOOL_GSET. */
enum netdev_features supported; /* Cached from ETHTOOL_GSET. */
enum netdev_features peer; /* Cached from ETHTOOL_GSET. */
struct ethtool_drvinfo drvinfo; /* Cached from ETHTOOL_GDRVINFO. */
struct tc *tc;
union {
struct tap_state tap;
} state;
};
struct netdev_rx_linux {
struct netdev_rx up;
bool is_tap;
int fd;
};
static const struct netdev_rx_class netdev_rx_linux_class;
/* Sockets used for ioctl operations. */
static int af_inet_sock = -1; /* AF_INET, SOCK_DGRAM. */
/* A Netlink routing socket that is not subscribed to any multicast groups. */
static struct nl_sock *rtnl_sock;
/* 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);
static int netdev_linux_init(void);
static int netdev_linux_do_ethtool(const char *name, struct ethtool_cmd *,
int cmd, const char *cmd_name);
static int netdev_linux_do_ioctl(const char *name, struct ifreq *, int cmd,
const char *cmd_name);
static int netdev_linux_get_ipv4(const struct netdev *, struct in_addr *,
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 do_get_ifindex(const char *netdev_name);
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, uint8_t ea[ETH_ADDR_LEN]);
static int set_etheraddr(const char *netdev_name, const uint8_t[ETH_ADDR_LEN]);
static int get_stats_via_netlink(int ifindex, struct netdev_stats *stats);
static int get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats);
static int af_packet_sock(void);
static void netdev_linux_miimon_run(void);
static void netdev_linux_miimon_wait(void);
static bool
is_netdev_linux_class(const struct netdev_class *netdev_class)
{
return netdev_class->init == netdev_linux_init;
}
static bool
is_tap_netdev(const struct netdev *netdev)
{
return netdev_get_class(netdev) == &netdev_tap_class;
}
static struct netdev_linux *
netdev_linux_cast(const struct netdev *netdev)
{
ovs_assert(is_netdev_linux_class(netdev_get_class(netdev)));
return CONTAINER_OF(netdev, struct netdev_linux, up);
}
static struct netdev_rx_linux *
netdev_rx_linux_cast(const struct netdev_rx *rx)
{
netdev_rx_assert_class(rx, &netdev_rx_linux_class);
return CONTAINER_OF(rx, struct netdev_rx_linux, up);
}
static int
netdev_linux_init(void)
{
static int status = -1;
if (status < 0) {
/* Create AF_INET socket. */
af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
status = af_inet_sock >= 0 ? 0 : errno;
if (status) {
VLOG_ERR("failed to create inet socket: %s", ovs_strerror(status));
}
/* Create rtnetlink socket. */
if (!status) {
status = nl_sock_create(NETLINK_ROUTE, &rtnl_sock);
if (status) {
VLOG_ERR_RL(&rl, "failed to create rtnetlink socket: %s",
ovs_strerror(status));
}
}
}
return status;
}
static void
netdev_linux_run(void)
{
rtnetlink_link_run();
netdev_linux_miimon_run();
}
static void
netdev_linux_wait(void)
{
rtnetlink_link_wait();
netdev_linux_miimon_wait();
}
static void
netdev_linux_changed(struct netdev_linux *dev,
unsigned int ifi_flags, unsigned int mask)
{
dev->change_seq++;
if (!dev->change_seq) {
dev->change_seq++;
}
if ((dev->ifi_flags ^ ifi_flags) & IFF_RUNNING) {
dev->carrier_resets++;
}
dev->ifi_flags = ifi_flags;
dev->cache_valid &= mask;
}
static void
netdev_linux_update(struct netdev_linux *dev,
const struct rtnetlink_link_change *change)
{
if (change->nlmsg_type == RTM_NEWLINK) {
/* Keep drv-info */
netdev_linux_changed(dev, change->ifi_flags, VALID_DRVINFO);
/* 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->addr)) {
memcpy(dev->etheraddr, change->addr, ETH_ADDR_LEN);
dev->cache_valid |= VALID_ETHERADDR;
dev->ether_addr_error = 0;
}
dev->ifindex = change->ifi_index;
dev->cache_valid |= VALID_IFINDEX;
dev->get_ifindex_error = 0;
} else {
netdev_linux_changed(dev, change->ifi_flags, 0);
}
}
static void
netdev_linux_cache_cb(const struct rtnetlink_link_change *change,
void *aux OVS_UNUSED)
{
struct netdev_linux *dev;
if (change) {
struct netdev *base_dev = netdev_from_name(change->ifname);
if (base_dev && is_netdev_linux_class(netdev_get_class(base_dev))) {
netdev_linux_update(netdev_linux_cast(base_dev), change);
}
} else {
struct shash device_shash;
struct shash_node *node;
shash_init(&device_shash);
netdev_get_devices(&netdev_linux_class, &device_shash);
SHASH_FOR_EACH (node, &device_shash) {
unsigned int flags;
dev = node->data;
get_flags(&dev->up, &flags);
netdev_linux_changed(dev, flags, 0);
}
shash_destroy(&device_shash);
}
}
static int
cache_notifier_ref(void)
{
if (!cache_notifier_refcount) {
ovs_assert(!netdev_linux_cache_notifier);
netdev_linux_cache_notifier =
rtnetlink_link_notifier_create(netdev_linux_cache_cb, NULL);
if (!netdev_linux_cache_notifier) {
return EINVAL;
}
}
cache_notifier_refcount++;
return 0;
}
static void
cache_notifier_unref(void)
{
ovs_assert(cache_notifier_refcount > 0);
if (!--cache_notifier_refcount) {
ovs_assert(netdev_linux_cache_notifier);
rtnetlink_link_notifier_destroy(netdev_linux_cache_notifier);
netdev_linux_cache_notifier = NULL;
}
}
/* Creates system and internal devices. */
static int
netdev_linux_create(const struct netdev_class *class, const char *name,
struct netdev **netdevp)
{
struct netdev_linux *netdev;
int error;
error = cache_notifier_ref();
if (error) {
return error;
}
netdev = xzalloc(sizeof *netdev);
netdev->change_seq = 1;
netdev_init(&netdev->up, name, class);
error = get_flags(&netdev->up, &netdev->ifi_flags);
if (error == ENODEV) {
if (class != &netdev_internal_class) {
/* The device does not exist, so don't allow it to be opened. */
netdev_uninit(&netdev->up, false);
cache_notifier_unref();
free(netdev);
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. */
}
}
*netdevp = &netdev->up;
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_create_tap(const struct netdev_class *class OVS_UNUSED,
const char *name, struct netdev **netdevp)
{
struct netdev_linux *netdev;
struct tap_state *state;
static const char tap_dev[] = "/dev/net/tun";
struct ifreq ifr;
int error;
netdev = xzalloc(sizeof *netdev);
state = &netdev->state.tap;
error = cache_notifier_ref();
if (error) {
goto error;
}
/* Open tap device. */
state->fd = open(tap_dev, O_RDWR);
if (state->fd < 0) {
error = errno;
VLOG_WARN("opening \"%s\" failed: %s", tap_dev, ovs_strerror(error));
goto error_unref_notifier;
}
/* Create tap device. */
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
ovs_strzcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
if (ioctl(state->fd, TUNSETIFF, &ifr) == -1) {
VLOG_WARN("%s: creating tap device failed: %s", name,
ovs_strerror(errno));
error = errno;
goto error_unref_notifier;
}
/* Make non-blocking. */
error = set_nonblocking(state->fd);
if (error) {
goto error_unref_notifier;
}
netdev_init(&netdev->up, name, &netdev_tap_class);
*netdevp = &netdev->up;
return 0;
error_unref_notifier:
cache_notifier_unref();
error:
free(netdev);
return error;
}
static void
destroy_tap(struct netdev_linux *netdev)
{
struct tap_state *state = &netdev->state.tap;
if (state->fd >= 0) {
close(state->fd);
}
}
/* Destroys the netdev device 'netdev_'. */
static void
netdev_linux_destroy(struct netdev *netdev_)
{
struct netdev_linux *netdev = netdev_linux_cast(netdev_);
if (netdev->tc && netdev->tc->ops->tc_destroy) {
netdev->tc->ops->tc_destroy(netdev->tc);
}
if (netdev_get_class(netdev_) == &netdev_tap_class) {
destroy_tap(netdev);
}
free(netdev);
cache_notifier_unref();
}
static int
netdev_linux_rx_open(struct netdev *netdev_, struct netdev_rx **rxp)
{
struct netdev_linux *netdev = netdev_linux_cast(netdev_);
bool is_tap = is_tap_netdev(netdev_);
struct netdev_rx_linux *rx;
int error;
int fd;
if (is_tap) {
fd = netdev->state.tap.fd;
} else {
struct sockaddr_ll sll;
int ifindex;
/* Result of tcpdump -dd inbound */
static struct sock_filter filt[] = {
{ 0x28, 0, 0, 0xfffff004 }, /* ldh [0] */
{ 0x15, 0, 1, 0x00000004 }, /* jeq #4 jt 2 jf 3 */
{ 0x6, 0, 0, 0x00000000 }, /* ret #0 */
{ 0x6, 0, 0, 0x0000ffff } /* ret #65535 */
};
static struct sock_fprog fprog = { ARRAY_SIZE(filt), filt };
/* Create file descriptor. */
fd = socket(PF_PACKET, SOCK_RAW, 0);
if (fd < 0) {
error = errno;
VLOG_ERR("failed to create raw socket (%s)", ovs_strerror(error));
goto error;
}
/* Set non-blocking mode. */
error = set_nonblocking(fd);
if (error) {
goto error;
}
/* Get ethernet device index. */
error = get_ifindex(&netdev->up, &ifindex);
if (error) {
goto error;
}
/* Bind to specific ethernet device. */
memset(&sll, 0, sizeof sll);
sll.sll_family = AF_PACKET;
sll.sll_ifindex = ifindex;
sll.sll_protocol = (OVS_FORCE unsigned short int) htons(ETH_P_ALL);
if (bind(fd, (struct sockaddr *) &sll, sizeof sll) < 0) {
error = errno;
VLOG_ERR("%s: failed to bind raw socket (%s)",
netdev_get_name(netdev_), ovs_strerror(error));
goto error;
}
/* Filter for only inbound packets. */
error = setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog,
sizeof fprog);
if (error) {
error = errno;
VLOG_ERR("%s: failed attach filter (%s)",
netdev_get_name(netdev_), ovs_strerror(error));
goto error;
}
}
rx = xmalloc(sizeof *rx);
netdev_rx_init(&rx->up, netdev_, &netdev_rx_linux_class);
rx->is_tap = is_tap;
rx->fd = fd;
*rxp = &rx->up;
return 0;
error:
if (fd >= 0) {
close(fd);
}
return error;
}
static void
netdev_rx_linux_destroy(struct netdev_rx *rx_)
{
struct netdev_rx_linux *rx = netdev_rx_linux_cast(rx_);
if (!rx->is_tap) {
close(rx->fd);
}
free(rx);
}
static int
netdev_rx_linux_recv(struct netdev_rx *rx_, void *data, size_t size)
{
struct netdev_rx_linux *rx = netdev_rx_linux_cast(rx_);
ssize_t retval;
do {
retval = (rx->is_tap
? read(rx->fd, data, size)
: recv(rx->fd, data, size, MSG_TRUNC));
} while (retval < 0 && errno == EINTR);
if (retval > size) {
return -EMSGSIZE;
} else if (retval >= 0) {
return retval;
} else {
if (errno != EAGAIN) {
VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
ovs_strerror(errno), netdev_rx_get_name(rx_));
}
return -errno;
}
}
static void
netdev_rx_linux_wait(struct netdev_rx *rx_)
{
struct netdev_rx_linux *rx = netdev_rx_linux_cast(rx_);
poll_fd_wait(rx->fd, POLLIN);
}
static int
netdev_rx_linux_drain(struct netdev_rx *rx_)
{
struct netdev_rx_linux *rx = netdev_rx_linux_cast(rx_);
if (rx->is_tap) {
struct ifreq ifr;
int error = netdev_linux_do_ioctl(netdev_rx_get_name(rx_), &ifr,
SIOCGIFTXQLEN, "SIOCGIFTXQLEN");
if (error) {
return error;
}
drain_fd(rx->fd, ifr.ifr_qlen);
return 0;
} else {
return drain_rcvbuf(rx->fd);
}
}
/* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
* errno value. Returns EAGAIN without blocking if the packet cannot be queued
* immediately. Returns EMSGSIZE if a partial packet was transmitted or if
* the packet is too big or too small to transmit on the device.
*
* The caller retains ownership of 'buffer' in all cases.
*
* The kernel maintains a packet transmission queue, so the caller is not
* expected to do additional queuing of packets. */
static int
netdev_linux_send(struct netdev *netdev_, const void *data, size_t size)
{
for (;;) {
ssize_t retval;
if (!is_tap_netdev(netdev_)) {
/* Use our AF_PACKET socket to send to this device. */
struct sockaddr_ll sll;
struct msghdr msg;
struct iovec iov;
int ifindex;
int error;
int sock;
sock = af_packet_sock();
if (sock < 0) {
return -sock;
}
error = get_ifindex(netdev_, &ifindex);
if (error) {
return error;
}
/* We don't bother setting most fields in sockaddr_ll because the
* kernel ignores them for SOCK_RAW. */
memset(&sll, 0, sizeof sll);
sll.sll_family = AF_PACKET;
sll.sll_ifindex = ifindex;
iov.iov_base = CONST_CAST(void *, data);
iov.iov_len = size;
msg.msg_name = &sll;
msg.msg_namelen = sizeof sll;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = NULL;
msg.msg_controllen = 0;
msg.msg_flags = 0;
retval = sendmsg(sock, &msg, 0);
} else {
/* Use the tap fd to send to this device. This is essential for
* tap devices, because packets sent to a tap device with an
* AF_PACKET socket will loop back to be *received* again on the
* tap device. This doesn't occur on other interface types
* because we attach a socket filter to the rx socket. */
struct netdev_linux *netdev = netdev_linux_cast(netdev_);
retval = write(netdev->state.tap.fd, data, size);
}
if (retval < 0) {
/* The Linux AF_PACKET implementation never blocks waiting for room
* for packets, instead returning ENOBUFS. Translate this into
* EAGAIN for the caller. */
if (errno == ENOBUFS) {
return EAGAIN;
} else if (errno == EINTR) {
continue;
} else if (errno != EAGAIN) {
VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
netdev_get_name(netdev_), ovs_strerror(errno));
}
return errno;
} else if (retval != size) {
VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
"%zu) on %s", retval, size, netdev_get_name(netdev_));
return EMSGSIZE;
} else {
return 0;
}
}
}
/* Registers with the poll loop to wake up from the next call to poll_block()
* when the packet transmission queue has sufficient room to transmit a packet
* with netdev_send().
*
* The kernel maintains a packet transmission queue, so the client is not
* expected to do additional queuing of packets. Thus, this function is
* unlikely to ever be used. It is included for completeness. */
static void
netdev_linux_send_wait(struct netdev *netdev)
{
if (is_tap_netdev(netdev)) {
/* TAP device always accepts packets.*/
poll_immediate_wake();
}
}
/* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
* otherwise a positive errno value. */
static int
netdev_linux_set_etheraddr(struct netdev *netdev_,
const uint8_t mac[ETH_ADDR_LEN])
{
struct netdev_linux *netdev = netdev_linux_cast(netdev_);
struct netdev_saved_flags *sf = NULL;
int error;
if (netdev->cache_valid & VALID_ETHERADDR) {
if (netdev->ether_addr_error) {
return netdev->ether_addr_error;
}
if (eth_addr_equals(netdev->etheraddr, mac)) {
return 0;
}
netdev->cache_valid &= ~VALID_ETHERADDR;
}
/* Tap devices must be brought down before setting the address. */
if (is_tap_netdev(netdev_)) {
netdev_turn_flags_off(netdev_, NETDEV_UP, &sf);