/
devices.go
634 lines (530 loc) · 13.8 KB
/
devices.go
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
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"golang.org/x/sys/unix"
"github.com/lxc/lxd/lxd/cgroup"
"github.com/lxc/lxd/lxd/device"
"github.com/lxc/lxd/lxd/instance"
"github.com/lxc/lxd/lxd/instance/instancetype"
"github.com/lxc/lxd/lxd/state"
"github.com/lxc/lxd/shared"
log "github.com/lxc/lxd/shared/log15"
"github.com/lxc/lxd/shared/logger"
)
/*
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <stdio.h>
#include <linux/hidraw.h>
#include "include/memory_utils.h"
#ifndef HIDIOCGRAWINFO
#define HIDIOCGRAWINFO _IOR('H', 0x03, struct hidraw_devinfo)
struct hidraw_devinfo {
__u32 bustype;
__s16 vendor;
__s16 product;
};
#endif
static int get_hidraw_devinfo(int fd, struct hidraw_devinfo *info)
{
int ret;
ret = ioctl(fd, HIDIOCGRAWINFO, info);
if (ret)
return -1;
return 0;
}
*/
import "C"
type deviceTaskCPU struct {
id int
strId string
count *int
}
type deviceTaskCPUs []deviceTaskCPU
func (c deviceTaskCPUs) Len() int { return len(c) }
func (c deviceTaskCPUs) Less(i, j int) bool { return *c[i].count < *c[j].count }
func (c deviceTaskCPUs) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func deviceNetlinkListener() (chan []string, chan []string, chan device.USBEvent, chan device.UnixHotplugEvent, error) {
NETLINK_KOBJECT_UEVENT := 15
UEVENT_BUFFER_SIZE := 2048
fd, err := unix.Socket(
unix.AF_NETLINK, unix.SOCK_RAW|unix.SOCK_CLOEXEC,
NETLINK_KOBJECT_UEVENT,
)
if err != nil {
return nil, nil, nil, nil, err
}
nl := unix.SockaddrNetlink{
Family: unix.AF_NETLINK,
Pid: uint32(os.Getpid()),
Groups: 3,
}
err = unix.Bind(fd, &nl)
if err != nil {
return nil, nil, nil, nil, err
}
chCPU := make(chan []string, 1)
chNetwork := make(chan []string, 0)
chUSB := make(chan device.USBEvent)
chUnix := make(chan device.UnixHotplugEvent)
go func(chCPU chan []string, chNetwork chan []string, chUSB chan device.USBEvent, chUnix chan device.UnixHotplugEvent) {
b := make([]byte, UEVENT_BUFFER_SIZE*2)
for {
r, err := unix.Read(fd, b)
if err != nil {
continue
}
ueventBuf := make([]byte, r)
copy(ueventBuf, b)
udevEvent := false
if strings.HasPrefix(string(ueventBuf), "libudev") {
udevEvent = true
// Skip the header that libudev prepends
ueventBuf = ueventBuf[40 : len(ueventBuf)-1]
}
ueventLen := 0
ueventParts := strings.Split(string(ueventBuf), "\x00")
props := map[string]string{}
for _, part := range ueventParts {
if strings.HasPrefix(part, "SEQNUM=") {
continue
}
// libudev string prefix distinguishes udev events from kernel uevents
if strings.HasPrefix(part, "libudev") {
udevEvent = true
continue
}
ueventLen += len(part) + 1
fields := strings.SplitN(part, "=", 2)
if len(fields) != 2 {
continue
}
props[fields[0]] = fields[1]
}
ueventLen--
if props["SUBSYSTEM"] == "cpu" && !udevEvent {
if props["DRIVER"] != "processor" {
continue
}
if props["ACTION"] != "offline" && props["ACTION"] != "online" {
continue
}
// As CPU re-balancing affects all containers, no need to queue them
select {
case chCPU <- []string{path.Base(props["DEVPATH"]), props["ACTION"]}:
default:
// Channel is full, drop the event
}
}
if props["SUBSYSTEM"] == "net" && !udevEvent {
if props["ACTION"] != "add" && props["ACTION"] != "removed" {
continue
}
if !shared.PathExists(fmt.Sprintf("/sys/class/net/%s", props["INTERFACE"])) {
continue
}
// Network balancing is interface specific, so queue everything
chNetwork <- []string{props["INTERFACE"], props["ACTION"]}
}
if props["SUBSYSTEM"] == "usb" && !udevEvent {
parts := strings.Split(props["PRODUCT"], "/")
if len(parts) < 2 {
continue
}
major, ok := props["MAJOR"]
if !ok {
continue
}
minor, ok := props["MINOR"]
if !ok {
continue
}
devname, ok := props["DEVNAME"]
if !ok {
continue
}
busnum, ok := props["BUSNUM"]
if !ok {
continue
}
devnum, ok := props["DEVNUM"]
if !ok {
continue
}
zeroPad := func(s string, l int) string {
return strings.Repeat("0", l-len(s)) + s
}
usb, err := device.USBNewEvent(
props["ACTION"],
/* udev doesn't zero pad these, while
* everything else does, so let's zero pad them
* for consistency
*/
zeroPad(parts[0], 4),
zeroPad(parts[1], 4),
major,
minor,
busnum,
devnum,
devname,
ueventParts[:len(ueventParts)-1],
ueventLen,
)
if err != nil {
logger.Error("Error reading usb device", log.Ctx{"err": err, "path": props["PHYSDEVPATH"]})
continue
}
chUSB <- usb
}
// unix hotplug device events rely on information added by udev
if udevEvent {
action := props["ACTION"]
if action != "add" && action != "remove" {
continue
}
subsystem, ok := props["SUBSYSTEM"]
if !ok {
continue
}
devname, ok := props["DEVNAME"]
if !ok {
continue
}
major, ok := props["MAJOR"]
if !ok {
continue
}
minor, ok := props["MINOR"]
if !ok {
continue
}
vendor := ""
product := ""
if action == "add" {
vendor, product, ok = ueventParseVendorProduct(props, subsystem, devname)
if !ok {
continue
}
}
zeroPad := func(s string, l int) string {
return strings.Repeat("0", l-len(s)) + s
}
// zeropad
if len(vendor) < 4 {
vendor = zeroPad(vendor, 4)
}
if len(product) < 4 {
product = zeroPad(product, 4)
}
unix, err := device.UnixHotplugNewEvent(
action,
/* udev doesn't zero pad these, while
* everything else does, so let's zero pad them
* for consistency
*/
vendor,
product,
major,
minor,
subsystem,
devname,
ueventParts[:len(ueventParts)-1],
ueventLen,
)
if err != nil {
logger.Error("Error reading unix device", log.Ctx{"err": err, "path": props["PHYSDEVPATH"]})
continue
}
chUnix <- unix
}
}
}(chCPU, chNetwork, chUSB, chUnix)
return chCPU, chNetwork, chUSB, chUnix, nil
}
func deviceTaskBalance(s *state.State) {
min := func(x, y int) int {
if x < y {
return x
}
return y
}
// Don't bother running when CGroup support isn't there
if !s.OS.CGInfo.Supports(cgroup.CPUSet, nil) {
return
}
// Get effective cpus list - those are all guaranteed to be online
effectiveCpus, err := cGroupGet("cpuset", "/", "cpuset.effective_cpus")
if err != nil {
// Older kernel - use cpuset.cpus
effectiveCpus, err = cGroupGet("cpuset", "/", "cpuset.cpus")
if err != nil {
logger.Errorf("Error reading host's cpuset.cpus")
return
}
}
effectiveCpusInt, err := instance.ParseCpuset(effectiveCpus)
if err != nil {
logger.Errorf("Error parsing effective CPU set")
return
}
isolatedCpusInt := []int{}
if shared.PathExists("/sys/devices/system/cpu/isolated") {
buf, err := ioutil.ReadFile("/sys/devices/system/cpu/isolated")
if err != nil {
logger.Errorf("Error reading host's isolated cpu")
return
}
// File might exist even though there are no isolated cpus.
isolatedCpus := strings.TrimSpace(string(buf))
if isolatedCpus != "" {
isolatedCpusInt, err = instance.ParseCpuset(isolatedCpus)
if err != nil {
logger.Errorf("Error parsing isolated CPU set: %s", string(isolatedCpus))
return
}
}
}
effectiveCpusSlice := []string{}
for _, id := range effectiveCpusInt {
if shared.IntInSlice(id, isolatedCpusInt) {
continue
}
effectiveCpusSlice = append(effectiveCpusSlice, fmt.Sprintf("%d", id))
}
effectiveCpus = strings.Join(effectiveCpusSlice, ",")
err = cGroupSet("cpuset", "/lxc", "cpuset.cpus", effectiveCpus)
if err != nil && shared.PathExists("/sys/fs/cgroup/cpuset/lxc") {
logger.Warn("Error setting lxd's cpuset.cpus", log.Ctx{"err": err})
}
cpus, err := instance.ParseCpuset(effectiveCpus)
if err != nil {
logger.Error("Error parsing host's cpu set", log.Ctx{"cpuset": effectiveCpus, "err": err})
return
}
// Iterate through the instances
instances, err := instance.LoadNodeAll(s, instancetype.Container)
if err != nil {
logger.Error("Problem loading instances list", log.Ctx{"err": err})
return
}
fixedInstances := map[int][]instance.Instance{}
balancedInstances := map[instance.Instance]int{}
for _, c := range instances {
conf := c.ExpandedConfig()
cpulimit, ok := conf["limits.cpu"]
if !ok || cpulimit == "" {
cpulimit = effectiveCpus
}
if !c.IsRunning() {
continue
}
count, err := strconv.Atoi(cpulimit)
if err == nil {
// Load-balance
count = min(count, len(cpus))
balancedInstances[c] = count
} else {
// Pinned
containerCpus, err := instance.ParseCpuset(cpulimit)
if err != nil {
return
}
for _, nr := range containerCpus {
if !shared.IntInSlice(nr, cpus) {
continue
}
_, ok := fixedInstances[nr]
if ok {
fixedInstances[nr] = append(fixedInstances[nr], c)
} else {
fixedInstances[nr] = []instance.Instance{c}
}
}
}
}
// Balance things
pinning := map[instance.Instance][]string{}
usage := map[int]deviceTaskCPU{}
for _, id := range cpus {
cpu := deviceTaskCPU{}
cpu.id = id
cpu.strId = fmt.Sprintf("%d", id)
count := 0
cpu.count = &count
usage[id] = cpu
}
for cpu, ctns := range fixedInstances {
c, ok := usage[cpu]
if !ok {
logger.Errorf("Internal error: container using unavailable cpu")
continue
}
id := c.strId
for _, ctn := range ctns {
_, ok := pinning[ctn]
if ok {
pinning[ctn] = append(pinning[ctn], id)
} else {
pinning[ctn] = []string{id}
}
*c.count += 1
}
}
sortedUsage := make(deviceTaskCPUs, 0)
for _, value := range usage {
sortedUsage = append(sortedUsage, value)
}
for ctn, count := range balancedInstances {
sort.Sort(sortedUsage)
for _, cpu := range sortedUsage {
if count == 0 {
break
}
count -= 1
id := cpu.strId
_, ok := pinning[ctn]
if ok {
pinning[ctn] = append(pinning[ctn], id)
} else {
pinning[ctn] = []string{id}
}
*cpu.count += 1
}
}
// Set the new pinning
for ctn, set := range pinning {
// Confirm the container didn't just stop
if !ctn.IsRunning() {
continue
}
sort.Strings(set)
err := ctn.CGroupSet("cpuset.cpus", strings.Join(set, ","))
if err != nil {
logger.Error("balance: Unable to set cpuset", log.Ctx{"name": ctn.Name(), "err": err, "value": strings.Join(set, ",")})
}
}
}
func deviceNetworkPriority(s *state.State, netif string) {
// Don't bother running when CGroup support isn't there
if !s.OS.CGInfo.Supports(cgroup.NetPrio, nil) {
return
}
instances, err := instance.LoadNodeAll(s, instancetype.Container)
if err != nil {
return
}
for _, c := range instances {
// Extract the current priority
networkPriority := c.ExpandedConfig()["limits.network.priority"]
if networkPriority == "" {
continue
}
networkInt, err := strconv.Atoi(networkPriority)
if err != nil {
continue
}
// Set the value for the new interface
c.CGroupSet("net_prio.ifpriomap", fmt.Sprintf("%s %d", netif, networkInt))
}
return
}
func deviceEventListener(s *state.State) {
chNetlinkCPU, chNetlinkNetwork, chUSB, chUnix, err := deviceNetlinkListener()
if err != nil {
logger.Errorf("scheduler: Couldn't setup netlink listener: %v", err)
return
}
for {
select {
case e := <-chNetlinkCPU:
if len(e) != 2 {
logger.Errorf("Scheduler: received an invalid cpu hotplug event")
continue
}
if !s.OS.CGInfo.Supports(cgroup.CPUSet, nil) {
continue
}
logger.Debugf("Scheduler: cpu: %s is now %s: re-balancing", e[0], e[1])
deviceTaskBalance(s)
case e := <-chNetlinkNetwork:
if len(e) != 2 {
logger.Errorf("Scheduler: received an invalid network hotplug event")
continue
}
if !s.OS.CGInfo.Supports(cgroup.NetPrio, nil) {
continue
}
logger.Debugf("Scheduler: network: %s has been added: updating network priorities", e[0])
deviceNetworkPriority(s, e[0])
networkAutoAttach(s.Cluster, e[0])
case e := <-chUSB:
device.USBRunHandlers(s, &e)
case e := <-chUnix:
device.UnixHotplugRunHandlers(s, &e)
case e := <-cgroup.DeviceSchedRebalance:
if len(e) != 3 {
logger.Errorf("Scheduler: received an invalid rebalance event")
continue
}
if !s.OS.CGInfo.Supports(cgroup.CPUSet, nil) {
continue
}
logger.Debugf("Scheduler: %s %s %s: re-balancing", e[0], e[1], e[2])
deviceTaskBalance(s)
}
}
}
// devicesRegister calls the Register() function on all supported devices so they receive events.
func devicesRegister(s *state.State) {
instances, err := instance.LoadNodeAll(s, instancetype.Container)
if err != nil {
logger.Error("Problem loading instances list", log.Ctx{"err": err})
return
}
for _, inst := range instances {
if !inst.IsRunning() {
continue
}
inst.RegisterDevices()
}
}
func getHidrawDevInfo(fd int) (string, string, error) {
info := C.struct_hidraw_devinfo{}
ret, err := C.get_hidraw_devinfo(C.int(fd), &info)
if ret != 0 {
return "", "", err
}
return fmt.Sprintf("%04x", info.vendor), fmt.Sprintf("%04x", info.product), nil
}
func ueventParseVendorProduct(props map[string]string, subsystem string, devname string) (string, string, bool) {
vendor, vendorOk := props["ID_VENDOR_ID"]
product, productOk := props["ID_MODEL_ID"]
if vendorOk && productOk {
return vendor, product, true
}
if subsystem != "hidraw" {
return "", "", false
}
if !filepath.IsAbs(devname) {
return "", "", false
}
file, err := os.OpenFile(devname, os.O_RDWR, 0000)
if err != nil {
return "", "", false
}
defer file.Close()
vendor, product, err = getHidrawDevInfo(int(file.Fd()))
if err != nil {
logger.Debugf("Failed to retrieve device info from hidraw device \"%s\"", devname)
return "", "", false
}
return vendor, product, true
}