Repository navigation
Expand file tree
/
Copy pathudev-builtin-net_id.c
More file actions
1477 lines (1170 loc) · 55.7 KB
/
Copy pathudev-builtin-net_id.c
File metadata and controls
1477 lines (1170 loc) · 55.7 KB
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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Predictable network interface device names based on:
* - firmware/bios-provided index numbers for on-board devices
* - firmware-provided pci-express hotplug slot index number
* - physical/geographical location of the hardware
* - the interface's MAC address
*
* https://systemd.io/PREDICTABLE_INTERFACE_NAMES
*
* When the code here is changed, man/systemd.net-naming-scheme.xml must be updated too.
*/
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/netdevice.h>
#include <linux/pci_regs.h>
#include <unistd.h>
#include "alloc-util.h"
#include "device-private.h"
#include "device-util.h"
#include "dirent-util.h"
#include "ether-addr-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "glyph-util.h"
#include "netif-naming-scheme.h"
#include "parse-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "udev-builtin.h"
#define ONBOARD_14BIT_INDEX_MAX ((1U << 14) - 1)
#define ONBOARD_16BIT_INDEX_MAX ((1U << 16) - 1)
static int device_get_parent_skip_virtio(sd_device *dev, sd_device **ret) {
int r;
assert(dev);
assert(ret);
/* This provides the parent device, but skips intermediate virtio devices. There can only ever be one
* virtio bus per parent device, so we can safely ignore any virtio buses. See
* https://lore.kernel.org/virtualization/CAPXgP137A=CdmggtVPUZXbnpTbU9Tewq-sOjg9T8ohYktct1kQ@mail.gmail.com/ */
for (;;) {
r = sd_device_get_parent(dev, &dev);
if (r < 0)
return r;
r = device_in_subsystem(dev, "virtio");
if (r < 0)
return r;
if (r == 0) {
*ret = dev;
return 0;
}
}
}
static int get_matching_parent(
sd_device *dev,
char * const *parent_subsystems,
bool skip_virtio,
sd_device **ret) {
sd_device *parent;
int r;
assert(dev);
if (skip_virtio)
r = device_get_parent_skip_virtio(dev, &parent);
else
r = sd_device_get_parent(dev, &parent);
if (r < 0)
return r;
/* check if our direct parent is in an expected subsystem. */
r = device_in_subsystem_strv(parent, parent_subsystems);
if (r < 0)
return r;
if (r == 0)
return -ENODEV;
if (ret)
*ret = parent;
return 0;
}
static int get_first_syspath_component(sd_device *dev, const char *prefix, char **ret) {
_cleanup_free_ char *buf = NULL;
const char *syspath, *p, *q;
int r;
assert(dev);
assert(prefix);
assert(ret);
r = sd_device_get_syspath(dev, &syspath);
if (r < 0)
return r;
p = path_startswith(syspath, prefix);
if (!p)
return -EINVAL;
r = path_find_first_component(&p, /* accept_dot_dot= */ false, &q);
if (r < 0)
return r;
buf = strndup(q, r);
if (!buf)
return -ENOMEM;
*ret = TAKE_PTR(buf);
return r; /* return the length of the string */
}
static int get_virtfn_info(sd_device *pcidev, sd_device **ret_physfn_pcidev, char **ret_suffix) {
_cleanup_(sd_device_unrefp) sd_device *physfn_pcidev = NULL;
const char *syspath, *name;
int r;
assert(pcidev);
assert(ret_physfn_pcidev);
assert(ret_suffix);
r = sd_device_get_syspath(pcidev, &syspath);
if (r < 0)
return r;
/* Get physical function's pci device. */
r = sd_device_new_child(&physfn_pcidev, pcidev, "physfn");
if (r < 0)
return r;
/* Find the virtual function number by finding the right virtfn link. */
FOREACH_DEVICE_CHILD_WITH_SUFFIX(physfn_pcidev, child, name) {
const char *n, *s;
/* Only accepts e.g. virtfn0, virtfn1, and so on. */
n = startswith(name, "virtfn");
if (isempty(n) || !in_charset(n, DIGITS))
continue;
if (sd_device_get_syspath(child, &s) < 0)
continue;
if (streq(s, syspath)) {
char *suffix;
suffix = strjoin("v", n);
if (!suffix)
return -ENOMEM;
*ret_physfn_pcidev = sd_device_ref(physfn_pcidev);
*ret_suffix = suffix;
return 0;
}
}
return -ENOENT;
}
static int get_subfunc_info(sd_device *aux_dev, sd_device **ret_parent_pcidev, char **ret_suffix) {
sd_device *parent;
unsigned sfnum;
char *suffix;
int r;
assert(aux_dev);
assert(ret_parent_pcidev);
assert(ret_suffix);
/* The auxiliary device must expose an 'sfnum' attribute. This is currently used by the
* mlx5_core driver for sub-function (SF) auxiliary devices. The sfnum is the user-defined
* stable identifier passed to "devlink port add ... sfnum N". */
r = device_get_sysattr_unsigned_filtered(aux_dev, "sfnum", &sfnum);
if (r < 0)
return r;
/* Walk one hop up: the auxiliary device's parent must be a PCI function. It can be either
* the PF directly, or an SR-IOV VF — mlx5 also supports SFs hosted on VFs (VF-SF), see
* Documentation/networking/representors.rst in the kernel tree. The VF case is handled by
* the existing virtfn logic in names_pci(), so here we just return the immediate PCI
* parent and a single "S<sfnum>" suffix piece. */
r = sd_device_get_parent(aux_dev, &parent);
if (r < 0)
return r;
r = device_in_subsystem(parent, "pci");
if (r < 0)
return r;
if (r == 0)
return -ENODEV;
if (asprintf(&suffix, "S%u", sfnum) < 0)
return log_oom_debug();
*ret_parent_pcidev = sd_device_ref(parent);
*ret_suffix = suffix;
return 0;
}
static int get_port_specifier(sd_device *dev, char **ret) {
const char *phys_port_name;
unsigned dev_port;
char *buf;
int r;
assert(dev);
assert(ret);
/* First, try to use the kernel provided front panel port name for multiple port PCI device. */
r = device_get_sysattr_safe_string_filtered(dev, "phys_port_name", &phys_port_name);
if (r >= 0 && !isempty(phys_port_name)) {
if (naming_scheme_has(NAMING_SR_IOV_R)) {
int vf_id = -1;
/* Check if phys_port_name indicates virtual device representor. */
(void) sscanf(phys_port_name, "pf%*uvf%d", &vf_id);
if (vf_id >= 0) {
/* For VF representor append 'r<VF_NUM>'. */
if (asprintf(&buf, "r%d", vf_id) < 0)
return log_oom_debug();
*ret = buf;
return 1;
}
}
/* Otherwise, use phys_port_name as is. */
buf = strjoin("n", phys_port_name);
if (!buf)
return log_oom_debug();
*ret = buf;
return 1;
}
/* Then, try to use the kernel provided port index for the case when multiple ports on a single PCI
* function. */
r = device_get_sysattr_unsigned_filtered(dev, "dev_port", &dev_port);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to get device port index: %m");
if (r > 0) {
assert(dev_port > 0);
if (asprintf(&buf, "d%u", dev_port) < 0)
return log_oom_debug();
*ret = buf;
return 1;
}
*ret = NULL;
return 0;
}
static bool is_valid_onboard_index(unsigned idx) {
/* Some BIOSes report rubbish indexes that are excessively high (2^24-1 is an index VMware likes to
* report for example). Let's define a cut-off where we don't consider the index reliable anymore. We
* pick some arbitrary cut-off, which is somewhere beyond the realistic number of physical network
* interface a system might have. Ideally the kernel would already filter this crap for us, but it
* doesn't currently. The initial cut-off value (2^14-1) was too conservative for s390 PCI which
* allows for index values up 2^16-1 which is now enabled with the NAMING_16BIT_INDEX naming flag. */
return idx <= (naming_scheme_has(NAMING_16BIT_INDEX) ? ONBOARD_16BIT_INDEX_MAX : ONBOARD_14BIT_INDEX_MAX);
}
static int pci_get_onboard_index(sd_device *dev, unsigned *ret) {
unsigned idx;
int r;
assert(dev);
assert(ret);
/* ACPI _DSM — device specific method for naming a PCI or PCI Express device */
r = device_get_sysattr_unsigned_filtered(dev, "acpi_index", &idx);
if (r < 0)
/* SMBIOS type 41 — Onboard Devices Extended Information */
r = device_get_sysattr_unsigned_filtered(dev, "index", &idx);
if (r < 0)
return log_device_debug_errno(dev, r, "Could not obtain onboard index: %m");
if (idx == 0 && !naming_scheme_has(NAMING_ZERO_ACPI_INDEX))
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL),
"Naming scheme does not allow onboard index==0.");
if (!is_valid_onboard_index(idx))
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL),
"Not a valid onboard index: %u", idx);
*ret = idx;
return 0;
}
static int names_pci_onboard(UdevEvent *event, sd_device *pci_dev, const char *prefix, const char *suffix) {
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
_cleanup_free_ char *port = NULL;
unsigned idx = 0; /* avoid false maybe-uninitialized warning */
int r;
assert(pci_dev);
assert(prefix);
/* retrieve on-board index number from firmware */
r = pci_get_onboard_index(pci_dev, &idx);
if (r < 0)
return r;
r = get_port_specifier(dev, &port);
if (r < 0)
return r;
char str[ALTIFNAMSIZ];
if (snprintf_ok(str, sizeof str, "%so%u%s%s", prefix, idx, strempty(port), strempty(suffix)))
udev_builtin_add_property(event, "ID_NET_NAME_ONBOARD", str);
log_device_debug(dev, "PCI onboard index identifier: index=%u port=%s %s %s",
idx, strna(port),
glyph(GLYPH_ARROW_RIGHT), empty_to_na(str));
return 0;
}
static int names_pci_onboard_label(UdevEvent *event, sd_device *pci_dev, const char *prefix) {
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
const char *label;
int r;
assert(prefix);
/* retrieve on-board label from firmware */
r = device_get_sysattr_safe_string_filtered(pci_dev, "label", &label);
if (r < 0)
return log_device_debug_errno(pci_dev, r, "Failed to get PCI onboard label: %m");
char str[ALTIFNAMSIZ];
if (snprintf_ok(str, sizeof str, "%s%s",
naming_scheme_has(NAMING_LABEL_NOPREFIX) ? "" : prefix,
label))
udev_builtin_add_property(event, "ID_NET_LABEL_ONBOARD", str);
log_device_debug(dev, "Onboard label from PCI device: %s", label);
return 0;
}
/* read the 256 bytes PCI configuration space to check the multi-function bit */
static int is_pci_multifunction(sd_device *dev) {
_cleanup_free_ uint8_t *config = NULL;
const char *filename, *syspath;
size_t len;
int r;
assert(dev);
r = sd_device_get_syspath(dev, &syspath);
if (r < 0)
return r;
filename = strjoina(syspath, "/config");
r = read_virtual_file(filename, PCI_HEADER_TYPE + 1, (char **) &config, &len);
if (r < 0)
return r;
if (len < PCI_HEADER_TYPE + 1)
return -EINVAL;
#ifndef PCI_HEADER_TYPE_MULTIFUNC
#define PCI_HEADER_TYPE_MULTIFUNC 0x80
#endif
/* bit 0-6 header type, bit 7 multi/single function device */
return config[PCI_HEADER_TYPE] & PCI_HEADER_TYPE_MULTIFUNC;
}
static bool is_pci_ari_enabled(sd_device *dev) {
assert(dev);
return device_get_sysattr_bool_filtered(dev, "ari_enabled") > 0;
}
static bool is_pci_bridge(sd_device *dev) {
const char *v, *p;
assert(dev);
if (device_get_sysattr_safe_string_filtered(dev, "modalias", &v) < 0)
return false;
if (!startswith(v, "pci:"))
return false;
p = strrchr(v, 's');
if (!p)
return false;
if (p[1] != 'c')
return false;
/* PCI device subclass 04 corresponds to PCI bridge */
bool b = strneq(p + 2, "04", 2);
if (b)
log_device_debug(dev, "Device is a PCI bridge.");
return b;
}
static int parse_hotplug_slot_from_function_id(sd_device *dev, int slots_dirfd, uint32_t *ret) {
uint64_t function_id;
char filename[NAME_MAX+1];
const char *attr;
int r;
/* The <sysname>/function_id attribute is unique to the s390 PCI driver. If present, we know that the
* slot's directory name for this device is /sys/bus/pci/slots/XXXXXXXX/ where XXXXXXXX is the fixed
* length 8 hexadecimal character string representation of function_id. Therefore we can short cut
* here and just check for the existence of the slot directory. As this directory has to exist, we're
* emitting a debug message for the unlikely case it's not found. Note that the domain part doesn't
* belong to the slot name here because there's a 1-to-1 relationship between PCI function and its
* hotplug slot. See https://docs.kernel.org/arch/s390/pci.html for more details. */
assert(dev);
assert(slots_dirfd >= 0);
assert(ret);
if (!naming_scheme_has(NAMING_SLOT_FUNCTION_ID)) {
*ret = 0;
return 0;
}
if (device_get_sysattr_safe_string_filtered(dev, "function_id", &attr) < 0) {
*ret = 0;
return 0;
}
r = safe_atou64(attr, &function_id);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to parse function_id, ignoring: '%s'", attr);
if (function_id <= 0 || function_id > UINT32_MAX)
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL),
"Invalid function id (0x%"PRIx64"), ignoring.",
function_id);
if (!snprintf_ok(filename, sizeof(filename), "%08"PRIx64, function_id))
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(ENAMETOOLONG),
"PCI slot path is too long, ignoring.");
if (faccessat(slots_dirfd, filename, F_OK, 0) < 0)
return log_device_debug_errno(dev, errno, "Cannot access '%s' under pci slots, ignoring: %m", filename);
*ret = (uint32_t) function_id;
return 1; /* Found. We should ignore domain part. */
}
static int pci_get_hotplug_slot_from_address(
sd_device *dev,
sd_device *pci,
DIR *dir,
uint32_t *ret) {
const char *sysname;
int r;
assert(dev);
assert(pci);
assert(dir);
assert(ret);
r = sd_device_get_sysname(dev, &sysname);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to get sysname: %m");
rewinddir(dir);
FOREACH_DIRENT_ALL(de, dir, break) {
_cleanup_free_ char *path = NULL;
const char *address;
uint32_t slot;
if (dot_or_dot_dot(de->d_name))
continue;
if (de->d_type != DT_DIR)
continue;
r = safe_atou32(de->d_name, &slot);
if (r < 0 || slot <= 0)
continue;
path = path_join("slots", de->d_name, "address");
if (!path)
return log_oom_debug();
if (device_get_sysattr_safe_string_filtered(pci, path, &address) < 0)
continue;
/* match slot address with device by stripping the function */
if (!startswith(sysname, address))
continue;
*ret = slot;
return 1; /* found */
}
*ret = 0;
return 0; /* not found */
}
static int pci_get_hotplug_slot(sd_device *dev, uint32_t *ret) {
_cleanup_(sd_device_unrefp) sd_device *pci = NULL;
_cleanup_closedir_ DIR *dir = NULL;
int r;
assert(dev);
assert(ret);
/* ACPI _SUN — slot user number */
r = sd_device_new_from_subsystem_sysname(&pci, "subsystem", "pci");
if (r < 0)
return log_debug_errno(r, "Failed to create sd_device object for pci subsystem: %m");
r = device_opendir(pci, "slots", &dir);
if (r < 0)
return log_device_debug_errno(dev, r, "Cannot open 'slots' subdirectory: %m");
for (sd_device *slot_dev = dev; slot_dev; ) {
uint32_t slot = 0; /* avoid false maybe-uninitialized warning */
r = parse_hotplug_slot_from_function_id(slot_dev, dirfd(dir), &slot);
if (r < 0)
return r;
if (r > 0) {
*ret = slot;
return 1; /* domain should be ignored. */
}
r = pci_get_hotplug_slot_from_address(slot_dev, pci, dir, &slot);
if (r < 0)
return r;
if (r > 0) {
/* We found the match between PCI device and slot. However, we won't use the slot
* index if the device is a PCI bridge, because it can have other child devices that
* will try to claim the same index and that would create name collision. */
if (naming_scheme_has(NAMING_BRIDGE_NO_SLOT) && is_pci_bridge(slot_dev)) {
if (naming_scheme_has(NAMING_BRIDGE_MULTIFUNCTION_SLOT) && is_pci_multifunction(dev) <= 0)
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(ESTALE),
"Not using slot information because the PCI device associated with "
"the hotplug slot is a bridge and the PCI device has a single function.");
if (!naming_scheme_has(NAMING_BRIDGE_MULTIFUNCTION_SLOT))
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(ESTALE),
"Not using slot information because the PCI device is a bridge.");
}
*ret = slot;
return 0; /* domain can be still used. */
}
if (sd_device_get_parent_with_subsystem_devtype(slot_dev, "pci", NULL, &slot_dev) < 0)
break;
}
return -ENOENT;
}
static int get_device_firmware_node_sun(sd_device *dev, uint32_t *ret) {
const char *attr;
int r;
assert(dev);
assert(ret);
r = device_get_sysattr_safe_string_filtered(dev, "firmware_node/sun", &attr);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to read firmware_node/sun, ignoring: %m");
uint32_t sun;
r = safe_atou32(attr, &sun);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to parse firmware_node/sun '%s', ignoring: %m", attr);
if (sun == 0)
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL), "firmware_node/sun == 0, ignoring.");
*ret = sun;
return 0;
}
static int pci_get_slot_from_firmware_node_sun(sd_device *dev, uint32_t *ret) {
sd_device *slot_dev;
int r;
assert(dev);
assert(ret);
if (!naming_scheme_has(NAMING_FIRMWARE_NODE_SUN))
return -EOPNOTSUPP;
/* Try getting the ACPI _SUN for the device */
if (get_device_firmware_node_sun(dev, ret) >= 0)
return 0;
r = sd_device_get_parent_with_subsystem_devtype(dev, "pci", NULL, &slot_dev);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to find parent PCI device, ignoring: %m");
if (is_pci_bridge(slot_dev) && is_pci_multifunction(dev) <= 0)
return log_device_debug_errno(
dev, SYNTHETIC_ERRNO(ESTALE),
"Not using slot information because the parent PCI slot is a bridge and the PCI device is not multifunction.");
/* Try getting the ACPI _SUN from the parent pcieport */
if (get_device_firmware_node_sun(slot_dev, ret) >= 0)
return 0;
return -ENOENT;
}
static int get_pci_slot_specifiers(
sd_device *dev,
char **ret_domain,
char **ret_bus_and_slot,
char **ret_func) {
_cleanup_free_ char *domain_spec = NULL, *bus_and_slot_spec = NULL, *func_spec = NULL;
unsigned domain, bus, slot, func;
const char *sysname;
int r;
assert(dev);
assert(ret_domain);
assert(ret_bus_and_slot);
assert(ret_func);
r = sd_device_get_sysname(dev, &sysname);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to get sysname: %m");
r = sscanf(sysname, "%x:%x:%x.%u", &domain, &bus, &slot, &func);
if (r != 4)
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL),
"Failed to parse slot information from PCI device sysname.");
if (naming_scheme_has(NAMING_NPAR_ARI) &&
is_pci_ari_enabled(dev))
/* ARI devices support up to 256 functions on a single device ("slot"), and interpret the
* traditional 5-bit slot and 3-bit function number as a single 8-bit function number,
* where the slot makes up the upper 5 bits. */
func += slot * 8;
/* Include the PCI domain in the name if the ID_NET_NAME_INCLUDE_DOMAIN property says so, if it is
* set. If it is not set, include it if the domain is non-zero. */
r = device_get_property_bool(dev, "ID_NET_NAME_INCLUDE_DOMAIN");
if (r < 0) {
if (r != -ENOENT)
log_device_warning_errno(dev, r, "Failed to read property \"ID_NET_NAME_INCLUDE_DOMAIN\", ignoring: %m");
r = domain > 0;
}
if (r > 0 && asprintf(&domain_spec, "P%u", domain) < 0)
return log_oom_debug();
if (asprintf(&bus_and_slot_spec, "p%us%u", bus, slot) < 0)
return log_oom_debug();
if ((func > 0 || is_pci_multifunction(dev) > 0) &&
asprintf(&func_spec, "f%u", func) < 0)
return log_oom_debug();
*ret_domain = TAKE_PTR(domain_spec);
*ret_bus_and_slot = TAKE_PTR(bus_and_slot_spec);
*ret_func = TAKE_PTR(func_spec);
return 0;
}
static int names_pci_slot(UdevEvent *event, sd_device *pci_dev, const char *prefix, const char *suffix) {
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
_cleanup_free_ char *domain = NULL, *bus_and_slot = NULL, *func = NULL, *port = NULL;
uint32_t slot = 0; /* avoid false maybe-uninitialized warning */
char str[ALTIFNAMSIZ];
int r;
assert(pci_dev);
assert(prefix);
r = get_pci_slot_specifiers(pci_dev, &domain, &bus_and_slot, &func);
if (r < 0)
return r;
r = get_port_specifier(dev, &port);
if (r < 0)
return r;
/* compose a name based on the raw kernel's PCI bus, slot numbers */
if (snprintf_ok(str, sizeof str, "%s%s%s%s%s%s",
prefix, strempty(domain), bus_and_slot, strempty(func), strempty(port), strempty(suffix)))
udev_builtin_add_property(event, "ID_NET_NAME_PATH", str);
log_device_debug(dev, "PCI path identifier: domain=%s bus_and_slot=%s func=%s port=%s %s %s",
strna(domain), bus_and_slot, strna(func), strna(port),
glyph(GLYPH_ARROW_RIGHT), empty_to_na(str));
if (pci_get_slot_from_firmware_node_sun(pci_dev, &slot) < 0) {
/* If we don't find a slot using firmware_node/sun, fallback to hotplug_slot */
r = pci_get_hotplug_slot(pci_dev, &slot);
if (r < 0)
return r;
if (r > 0)
/* If the hotplug slot is found through the function ID, then drop the domain from the name.
* See comments in parse_hotplug_slot_from_function_id(). */
domain = mfree(domain);
}
if (snprintf_ok(str, sizeof str, "%s%ss%"PRIu32"%s%s%s",
prefix, strempty(domain), slot, strempty(func), strempty(port), strempty(suffix)))
udev_builtin_add_property(event, "ID_NET_NAME_SLOT", str);
log_device_debug(dev, "PCI slot identifier: domain=%s slot=%"PRIu32" func=%s port=%s %s %s",
strna(domain), slot, strna(func), strna(port),
glyph(GLYPH_ARROW_RIGHT), empty_to_na(str));
return 0;
}
static int names_vio(UdevEvent *event, const char *prefix) {
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
_cleanup_free_ char *s = NULL;
unsigned slotid;
int r;
assert(prefix);
/* get ibmveth/ibmvnic slot-based names. */
/* check if our direct parent is a VIO device with no other bus in-between */
if (get_matching_parent(dev, STRV_MAKE("vio"), /* skip_virtio= */ false, NULL) < 0)
return 0;
log_device_debug(dev, "Parent device is in the vio subsystem.");
/* The devices' $DEVPATH number is tied to (virtual) hardware (slot id
* selected in the HMC), thus this provides a reliable naming (e.g.
* "/devices/vio/30000002/net/eth1"); we ignore the bus number, as
* there should only ever be one bus, and then remove leading zeros. */
r = get_first_syspath_component(dev, "/sys/devices/vio/", &s);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to get VIO bus ID and slot ID: %m");
if (r != 8)
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL),
"VIO bus ID and slot ID have invalid length: '%s'", s);
if (!in_charset(s, HEXDIGITS))
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL),
"VIO bus ID and slot ID contain invalid characters: '%s'", s);
/* Parse only slot ID (the last 4 hexdigits). */
r = safe_atou_full(s + 4, 16, &slotid);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to parse VIO slot from '%s': %m", s);
char str[ALTIFNAMSIZ];
if (snprintf_ok(str, sizeof str, "%sv%u", prefix, slotid))
udev_builtin_add_property(event, "ID_NET_NAME_SLOT", str);
log_device_debug(dev, "Vio slot identifier: slotid=%u %s %s",
slotid, glyph(GLYPH_ARROW_RIGHT), str + strlen(prefix));
return 0;
}
static int names_platform(UdevEvent *event, const char *prefix) {
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
_cleanup_free_ char *p = NULL;
const char *validchars;
char *vendor, *model_str, *instance_str;
unsigned model, instance;
int r;
assert(prefix);
/* get ACPI path names for ARM64 platform devices */
/* check if our direct parent is a platform device with no other bus in-between */
if (get_matching_parent(dev, STRV_MAKE("platform"), /* skip_virtio= */ false, NULL) < 0)
return 0;
log_device_debug(dev, "Parent device is in the platform subsystem.");
r = get_first_syspath_component(dev, "/sys/devices/platform/", &p);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to get platform ID: %m");
/* Platform devices are named after ACPI table match, and instance id
* eg. "/sys/devices/platform/HISI00C2:00"
* The Vendor (3 or 4 char), followed by hexadecimal model number : instance id. */
if (r == 10 && p[7] == ':') {
/* 3 char vendor string */
vendor = strndupa_safe(p, 3);
model_str = strndupa_safe(p + 3, 4);
instance_str = strndupa_safe(p + 8, 2);
validchars = UPPERCASE_LETTERS;
} else if (r == 11 && p[8] == ':') {
/* 4 char vendor string */
vendor = strndupa_safe(p, 4);
model_str = strndupa_safe(p + 4, 4);
instance_str = strndupa_safe(p + 9, 2);
validchars = UPPERCASE_LETTERS DIGITS;
} else
return -EOPNOTSUPP;
if (!in_charset(vendor, validchars))
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL),
"Platform vendor contains invalid characters: '%s'", vendor);
ascii_strlower(vendor);
r = safe_atou_full(model_str, 16, &model);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to parse model number \"%s\": %m", model_str);
r = safe_atou_full(instance_str, 16, &instance);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to parse instance id \"%s\": %m", instance_str);
char str[ALTIFNAMSIZ];
if (snprintf_ok(str, sizeof str, "%sa%s%xi%u", prefix, vendor, model, instance))
udev_builtin_add_property(event, "ID_NET_NAME_PATH", str);
log_device_debug(dev, "Platform identifier: vendor=%s model=%x instance=%u %s %s",
vendor, model, instance, glyph(GLYPH_ARROW_RIGHT), str + strlen(prefix));
return 0;
}
static int names_devicetree_alias_prefix(UdevEvent *event, const char *prefix, const char *alias_prefix) {
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
_cleanup_(sd_device_unrefp) sd_device *aliases_dev = NULL, *ofnode_dev = NULL, *devicetree_dev = NULL;
const char *ofnode_path, *ofnode_syspath, *devicetree_syspath;
int r;
assert(prefix);
assert(alias_prefix);
/* Returns 1 if found, 0 if not found, negative if error */
_cleanup_free_ char *alias_prefix_0 = strjoin(alias_prefix, "0");
if (!alias_prefix_0)
return log_oom_debug();
/* check if the device itself has an of_node */
if (naming_scheme_has(NAMING_DEVICETREE_PORT_ALIASES)) {
r = sd_device_new_child(&ofnode_dev, dev, "of_node");
if (r < 0)
log_device_debug_errno(dev, r, "Failed to get device of_node, ignoring: %m");
}
if (!ofnode_dev) {
sd_device *parent;
/* check if our direct parent has an of_node as a fallback */
r = sd_device_get_parent(dev, &parent);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to get parent device: %m");
r = sd_device_new_child(&ofnode_dev, parent, "of_node");
if (r < 0)
return log_device_debug_errno(parent, r, "Failed to get device of_node: %m");
}
r = sd_device_get_syspath(ofnode_dev, &ofnode_syspath);
if (r < 0)
return log_device_debug_errno(ofnode_dev, r, "Failed to get syspath: %m");
/* /proc/device-tree should be a symlink to /sys/firmware/devicetree/base. */
r = sd_device_new_from_path(&devicetree_dev, "/proc/device-tree");
if (r < 0)
return log_debug_errno(r, "Failed to create sd-device object from '/proc/device-tree': %m");
r = sd_device_get_syspath(devicetree_dev, &devicetree_syspath);
if (r < 0)
return log_device_debug_errno(devicetree_dev, r, "Failed to get syspath: %m");
/*
* Example paths:
* devicetree_syspath = /sys/firmware/devicetree/base
* ofnode_syspath = /sys/firmware/devicetree/base/soc/ethernet@deadbeef
* ofnode_path = soc/ethernet@deadbeef
*/
ofnode_path = path_startswith(ofnode_syspath, devicetree_syspath);
if (!ofnode_path)
return log_device_debug_errno(ofnode_dev, SYNTHETIC_ERRNO(EINVAL),
"The device '%s' is not a child device of '%s'.",
ofnode_syspath, devicetree_syspath);
/* Get back our leading / to match the contents of the aliases */
ofnode_path--;
assert(path_is_absolute(ofnode_path));
r = sd_device_new_child(&aliases_dev, devicetree_dev, "aliases");
if (r < 0)
return log_device_debug_errno(devicetree_dev, r,
"Failed to get 'aliases' child device: %m");
FOREACH_DEVICE_SYSATTR(aliases_dev, alias) {
const char *alias_path, *alias_index, *conflict;
unsigned i;
alias_index = startswith(alias, alias_prefix);
if (!alias_index)
continue;
if (device_get_sysattr_safe_string_filtered(aliases_dev, alias, &alias_path) < 0)
continue;
if (!path_equal(ofnode_path, alias_path))
continue;
/* If there's no index, we default to 0... */
if (isempty(alias_index)) {
i = 0;
conflict = alias_prefix_0;
} else {
r = safe_atou(alias_index, &i);
if (r < 0)
return log_device_debug_errno(dev, r,
"Could not get index of alias '%s': %m", alias);
conflict = alias_prefix;
}
/* ...but make sure we don't have an alias conflict */
if (i == 0 && device_get_sysattr_safe_string_filtered(aliases_dev, conflict, NULL) >= 0)
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EEXIST),
"DeviceTree alias conflict: '%s' and '%s' both exist.",
alias_prefix, alias_prefix_0);
char str[ALTIFNAMSIZ];
if (snprintf_ok(str, sizeof str, "%sd%u", prefix, i))
udev_builtin_add_property(event, "ID_NET_NAME_ONBOARD", str);
log_device_debug(dev, "DeviceTree identifier: alias_index=%u %s \"%s\"",
i, glyph(GLYPH_ARROW_RIGHT), str + strlen(prefix));
return 1;
}
return 0;
}
static int names_devicetree(UdevEvent *event, const char *prefix) {
int r;
assert(event);
assert(prefix);
if (!naming_scheme_has(NAMING_DEVICETREE_ALIASES))
return 0;
if (streq(prefix, "en"))
r = names_devicetree_alias_prefix(event, prefix, "ethernet");
else if (naming_scheme_has(NAMING_DEVICETREE_ALIASES_WLAN) &&
streq(prefix, "wl")) {
r = names_devicetree_alias_prefix(event, prefix, "wifi");
/* Sometimes DeviceTrees have WLAN devices with alias ethernetN, fall back to those */
if (r == 0)
r = names_devicetree_alias_prefix(event, prefix, "ethernet");
} else
return -EOPNOTSUPP; /* Unsupported interface type */
if (r < 0)
return r;
if (r == 0) /* Not found */
return -ENOENT;
return 0; /* Found */
};
static int names_pci(UdevEvent *event, const char *prefix) {
sd_device *parent, *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
_cleanup_(sd_device_unrefp) sd_device *physfn_pcidev = NULL, *parent_pcidev = NULL;
_cleanup_free_ char *virtfn_suffix = NULL, *subfunc_suffix = NULL, *combined_suffix = NULL;
const char *suffix = NULL;
assert(prefix);
/* If the network device's direct parent is an auxiliary device that exposes a stable
* 'sfnum' attribute (currently mlx5_core sub-functions), peel off the SF identity into
* a 'S<sfnum>' suffix piece and pick up the aux device's underlying PCI function as
* 'parent'. The aux device is just a bump on the path; everything below — PF/VF
* resolution, slot/onboard lookup — proceeds the same way as for any PCI-rooted
* network device. */
if (naming_scheme_has(NAMING_SUBFUNC)) {
sd_device *aux;
if (get_matching_parent(dev, STRV_MAKE("auxiliary"),
/* skip_virtio= */ false, &aux) >= 0)
(void) get_subfunc_info(aux, &parent_pcidev, &subfunc_suffix);
}
/* SF: parent is the aux device's PCI function. Otherwise the network device's direct
* parent must itself be a PCI device. */
if (subfunc_suffix)
parent = parent_pcidev;
else if (get_matching_parent(dev, STRV_MAKE("pci"), /* skip_virtio= */ true, &parent) < 0)
return 0;
/* If parent is an SR-IOV VF, walk to the parent PF and add a 'v<N>' suffix piece. The
* onboard BIOS label is intentionally not exposed for any "child function" (VF, SF, or
* VF-SF), since the label refers to the parent's physical port, not to a logical child. */