forked from snapcore/snapd
/
hotplug.go
455 lines (400 loc) · 16.4 KB
/
hotplug.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2018 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package ifacestate
import (
"crypto/sha256"
"fmt"
"strings"
"unicode"
"github.com/snapcore/snapd/features"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/hotplug"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/overlord/configstate/config"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/snap"
)
// deviceKey determines a key for given device and hotplug interface. Every interface may provide a custom HotplugDeviceKey method
// to compute device key - if it doesn't, we fall back to defaultDeviceKey.
func deviceKey(device *hotplug.HotplugDeviceInfo, iface interfaces.Interface, defaultDeviceKey snap.HotplugKey) (deviceKey snap.HotplugKey, err error) {
if keyhandler, ok := iface.(hotplug.HotplugKeyHandler); ok {
deviceKey, err = keyhandler.HotplugKey(device)
if err != nil {
return "", fmt.Errorf("cannot create hotplug key for interface %q: %s", iface.Name(), err)
}
if deviceKey != "" {
return deviceKey, nil
}
}
return defaultDeviceKey, nil
}
// List of attributes that determine the computation of default device key.
// Attributes are grouped by similarity, the first non-empty attribute within the group goes into the key.
// The final key is composed of 4 attributes (some of which may be empty), separated by "/".
// Warning, any future changes to these definitions require a new key version.
var attrGroups = [][][]string{
// key version 0
{
// Name
{"ID_V4L_PRODUCT", "NAME", "ID_NET_NAME", "PCI_SLOT_NAME"},
// Vendor
{"ID_VENDOR_ID", "ID_VENDOR", "ID_WWN", "ID_WWN_WITH_EXTENSION", "ID_VENDOR_FROM_DATABASE", "ID_VENDOR_ENC", "ID_OUI_FROM_DATABASE"},
// Model
{"ID_MODEL_ID", "ID_MODEL_ENC"},
// Identifier
{"ID_SERIAL", "ID_SERIAL_SHORT", "ID_NET_NAME_MAC", "ID_REVISION"},
},
}
// deviceKeyVersion is the current version number for the default keys computed by hotplug subsystem.
// Fresh device keys always use current version format
var deviceKeyVersion = len(attrGroups) - 1
// defaultDeviceKey computes device key from the attributes of
// HotplugDeviceInfo. Empty string is returned if too few attributes are present
// to compute a good key. Attributes used to compute device key are defined in
// attrGroups list above and they depend on the keyVersion passed to the
// function.
// The resulting key returned by the function has the following format:
// <version><checksum> where checksum is the sha256 checksum computed over
// select attributes of the device.
func defaultDeviceKey(devinfo *hotplug.HotplugDeviceInfo, keyVersion int) (snap.HotplugKey, error) {
found := 0
key := sha256.New()
if keyVersion >= 16 || keyVersion >= len(attrGroups) {
return "", fmt.Errorf("internal error: invalid key version %d", keyVersion)
}
for _, group := range attrGroups[keyVersion] {
for _, attr := range group {
if val, ok := devinfo.Attribute(attr); ok && val != "" {
key.Write([]byte(attr))
key.Write([]byte{0})
key.Write([]byte(val))
key.Write([]byte{0})
found++
break
}
}
}
if found < 2 {
return "", nil
}
return snap.HotplugKey(fmt.Sprintf("%x%x", keyVersion, key.Sum(nil))), nil
}
// hotplugDeviceAdded gets called when a device is added to the system.
func (m *InterfaceManager) hotplugDeviceAdded(devinfo *hotplug.HotplugDeviceInfo) {
st := m.state
st.Lock()
defer st.Unlock()
if _, err := systemSnapInfo(st); err != nil {
logger.Noticef("system snap not available, hotplug events ignored")
return
}
defaultKey, err := defaultDeviceKey(devinfo, deviceKeyVersion)
if err != nil {
logger.Noticef("cannot compute default hotplug key for device %s: %v", devinfo, err.Error())
}
hotplugFeature, err := m.hotplugEnabled()
if err != nil {
logger.Noticef("internal error: cannot get hotplug feature flag: %v", err.Error())
return
}
deviceCtx, err := snapstate.DeviceCtxFromState(st, nil)
if err != nil {
logger.Noticef("internal error: cannot get global device context: %v", err)
return
}
gadget, err := snapstate.GadgetInfo(st, deviceCtx)
if err != nil && err != state.ErrNoState {
logger.Noticef("internal error: cannot get gadget information: %v", err)
}
hotplugIfaces := m.repo.AllHotplugInterfaces()
gadgetSlotsByInterface := make(map[string][]*snap.SlotInfo)
if gadget != nil {
for _, gadgetSlot := range gadget.Slots {
if _, ok := hotplugIfaces[gadgetSlot.Interface]; ok {
gadgetSlotsByInterface[gadgetSlot.Interface] = append(gadgetSlotsByInterface[gadgetSlot.Interface], gadgetSlot)
}
}
}
InterfacesLoop:
// iterate over all hotplug interfaces
for _, iface := range hotplugIfaces {
hotplugHandler := iface.(hotplug.Definer)
// ignore device that is already handled by a gadget slot
if gadgetSlots, ok := gadgetSlotsByInterface[iface.Name()]; ok {
for _, gslot := range gadgetSlots {
if pred, ok := iface.(hotplug.HandledByGadgetPredicate); ok {
if pred.HandledByGadget(devinfo, gslot) {
logger.Debugf("ignoring device %s, interface %q (handled by gadget slot %s)", devinfo, iface.Name(), gslot.Name)
continue InterfacesLoop
}
}
}
}
proposedSlot, err := hotplugHandler.HotplugDeviceDetected(devinfo)
if err != nil {
logger.Noticef("cannot process hotplug event by the rule of interface %q: %s", iface.Name(), err)
continue
}
// if the interface doesn't propose a slot, carry on and go to the next interface
if proposedSlot == nil {
continue
}
// Check the key when we know the interface wants to create a hotplug slot, doing this earlier would generate too much log noise about irrelevant devices
key, err := deviceKey(devinfo, iface, defaultKey)
if err != nil {
logger.Noticef("internal error: cannot compute hotplug key for device %s: %v", devinfo, err.Error())
continue
}
if key == "" {
logger.Noticef("no valid hotplug key provided by interface %q, device %s ignored", iface.Name(), devinfo)
continue
}
proposedSlot, err = proposedSlot.Clean()
if err != nil {
logger.Noticef("cannot validate hotplug slot proposed by interface %q for device %s: %v", iface.Name(), devinfo, err.Error())
continue
}
if proposedSlot.Label == "" {
si := interfaces.StaticInfoOf(iface)
proposedSlot.Label = si.Summary
}
if !hotplugFeature {
logger.Noticef("hotplug device add event ignored, enable experimental.hotplug")
return
}
logger.Debugf("adding hotplug device %s for interface %q, hotplug key %q", devinfo, iface.Name(), key)
seq, err := allocHotplugSeq(st)
if err != nil {
logger.Noticef("internal error: cannot handle hotplug device %s: %v", devinfo, err)
continue
}
if !m.enumerationDone {
if m.enumeratedDeviceKeys[iface.Name()] == nil {
m.enumeratedDeviceKeys[iface.Name()] = make(map[snap.HotplugKey]bool)
}
m.enumeratedDeviceKeys[iface.Name()][key] = true
}
devPath := devinfo.DevicePath()
// We may have different interfaces at same paths (e.g. a "foo-observe" and "foo-control" interfaces), therefore use lists.
// Duplicates are not expected here because if a device is plugged twice, there will be an udev "remove" event between the adds
// and hotplugDeviceRemoved() will remove affected path from hotplugDevicePaths.
m.hotplugDevicePaths[devPath] = append(m.hotplugDevicePaths[devPath], deviceData{hotplugKey: key, ifaceName: iface.Name()})
hotplugAdd := st.NewTask("hotplug-add-slot", fmt.Sprintf("Create slot for device %s with hotplug key %q", devinfo.ShortString(), key.ShortString()))
setHotplugAttrs(hotplugAdd, iface.Name(), key)
hotplugAdd.Set("device-info", devinfo)
hotplugAdd.Set("proposed-slot", proposedSlot)
hotplugConnect := st.NewTask("hotplug-connect", fmt.Sprintf("Recreate connections of interface %q for device %s with hotplug key %q", iface.Name(), devinfo.ShortString(), key.ShortString()))
setHotplugAttrs(hotplugConnect, iface.Name(), key)
hotplugConnect.WaitFor(hotplugAdd)
chg := st.NewChange(fmt.Sprintf("hotplug-add-slot-%s", iface), fmt.Sprintf("Add hotplug slot of interface %q for device %s with hotplug key %q", devinfo.ShortString(), iface.Name(), key.ShortString()))
chg.AddTask(hotplugAdd)
chg.AddTask(hotplugConnect)
addHotplugSeqWaitTask(chg, key, seq)
st.EnsureBefore(0)
}
}
// hotplugDeviceRemoved gets called when a device is removed from the system.
func (m *InterfaceManager) hotplugDeviceRemoved(devinfo *hotplug.HotplugDeviceInfo) {
st := m.state
st.Lock()
defer st.Unlock()
hotplugFeature, err := m.hotplugEnabled()
if err != nil {
logger.Noticef("internal error: cannot get hotplug feature flag: %s", err.Error())
return
}
devPath := devinfo.DevicePath()
devs := m.hotplugDevicePaths[devPath]
delete(m.hotplugDevicePaths, devPath)
var changed bool
for _, dev := range devs {
hotplugKey := dev.hotplugKey
ifaceName := dev.ifaceName
slot, err := m.repo.SlotForHotplugKey(ifaceName, hotplugKey)
if err != nil {
logger.Noticef("internal error: cannot obtain slot for hotplug interface %q, hotplug key %q: %v", ifaceName, hotplugKey, err)
continue
}
if slot == nil {
continue
}
if !hotplugFeature {
logger.Noticef("hotplug device remove event ignored, enable experimental.hotplug")
return
}
logger.Debugf("removing hotplug device %s for interface %q, hotplug key %q", devinfo, ifaceName, hotplugKey)
seq, err := allocHotplugSeq(st)
if err != nil {
logger.Noticef("internal error: cannot handle removal of hotplug device %s, hotplug key %q: %v", devinfo, hotplugKey, err)
continue
}
ts := removeDevice(st, ifaceName, hotplugKey)
chg := st.NewChange(fmt.Sprintf("hotplug-remove-%s", ifaceName), fmt.Sprintf("Remove hotplug connections and slots of device %s with interface %q", devinfo.ShortString(), ifaceName))
chg.AddAll(ts)
addHotplugSeqWaitTask(chg, hotplugKey, seq)
changed = true
}
if changed {
st.EnsureBefore(0)
}
}
// hotplugEnumerationDone gets called when initial enumeration on startup is finished.
func (m *InterfaceManager) hotplugEnumerationDone() {
st := m.state
st.Lock()
defer st.Unlock()
hotplugSlots, err := getHotplugSlots(st)
if err != nil {
logger.Noticef("internal error obtaining hotplug slots: %v", err.Error())
return
}
for _, slot := range hotplugSlots {
if byIface, ok := m.enumeratedDeviceKeys[slot.Interface]; ok {
if byIface[slot.HotplugKey] {
continue
}
}
// device not present, disconnect its slots and remove them (as if it was unplugged)
seq, err := allocHotplugSeq(st)
if err != nil {
logger.Noticef("internal error: cannot handle removal of hotplug slot %q: %v", slot.Name, err)
continue
}
ts := removeDevice(st, slot.Interface, slot.HotplugKey)
chg := st.NewChange(fmt.Sprintf("hotplug-remove-%s", slot.Interface), fmt.Sprintf("Remove hotplug connections and slots of interface %q", slot.Interface))
chg.AddAll(ts)
addHotplugSeqWaitTask(chg, slot.HotplugKey, seq)
}
st.EnsureBefore(0)
// the map of enumeratedDeviceKeys is not needed anymore
m.enumeratedDeviceKeys = nil
m.enumerationDone = true
}
func (m *InterfaceManager) hotplugEnabled() (bool, error) {
tr := config.NewTransaction(m.state)
return features.Flag(tr, features.Hotplug)
}
// ensureUniqueName modifies proposedName so that it's unique according to isUnique predicate.
// Uniqueness is achieved by appending a numeric suffix.
func ensureUniqueName(proposedName string, isUnique func(string) bool) string {
// if the name is unique right away, do nothing
if isUnique(proposedName) {
return proposedName
}
baseName := proposedName
suffixNumValue := 1
// increase suffix value until we have a unique name
for {
proposedName = fmt.Sprintf("%s-%d", baseName, suffixNumValue)
if isUnique(proposedName) {
return proposedName
}
suffixNumValue++
}
}
const maxGenerateSlotNameLen = 20
// makeSlotName sanitizes a string to make it a valid slot name that
// passes validation rules implemented by ValidateSlotName (see snap/validate.go):
// - only lowercase letter, digits and dashes are allowed
// - must start with a letter
// - no double dashes, cannot end with a dash.
// In addition names are truncated not to exceed maxGenerateSlotNameLen characters.
func makeSlotName(s string) string {
var out []rune
// the dash flag is used to prevent consecutive dashes, and the dash in the front
dash := true
for _, c := range s {
switch {
case c == '-' && !dash:
dash = true
out = append(out, '-')
case unicode.IsLetter(c):
out = append(out, unicode.ToLower(c))
dash = false
case unicode.IsDigit(c) && len(out) > 0:
out = append(out, c)
dash = false
default:
// any other character is ignored
}
if len(out) >= maxGenerateSlotNameLen {
break
}
}
// make sure the name doesn't end with a dash
return strings.TrimRight(string(out), "-")
}
var nameAttrs = []string{"NAME", "ID_MODEL_FROM_DATABASE", "ID_MODEL"}
// suggestedSlotName returns the shortest name derived from attributes defined
// by nameAttrs, or the fallbackName if there is no known attribute to derive
// name from. The name created from attributes is sanitized to ensure it's a
// valid slot name. The fallbackName is typically the name of the interface.
func suggestedSlotName(devinfo *hotplug.HotplugDeviceInfo, fallbackName string) string {
var shortestName string
for _, attr := range nameAttrs {
name, ok := devinfo.Attribute(attr)
if ok {
if name := makeSlotName(name); name != "" {
if shortestName == "" || len(name) < len(shortestName) {
shortestName = name
}
}
}
}
if len(shortestName) == 0 {
return fallbackName
}
return shortestName
}
// hotplugSlotName returns a slot name derived from slotSpecName or device attributes, or interface name, in that priority order, depending
// on which information is available. The chosen name is guaranteed to be unique
func hotplugSlotName(hotplugKey snap.HotplugKey, systemSnapInstanceName, slotSpecName, ifaceName string, devinfo *hotplug.HotplugDeviceInfo, repo *interfaces.Repository, stateSlots map[string]*HotplugSlotInfo) string {
proposedName := slotSpecName
if proposedName == "" {
proposedName = suggestedSlotName(devinfo, ifaceName)
}
proposedName = ensureUniqueName(proposedName, func(slotName string) bool {
if slot, ok := stateSlots[slotName]; ok {
return slot.HotplugKey == hotplugKey
}
return repo.Slot(systemSnapInstanceName, slotName) == nil
})
return proposedName
}
// updateDevice creates tasks to disconnect slots of given device and update the slot in the repository.
func updateDevice(st *state.State, ifaceName string, hotplugKey snap.HotplugKey, newAttrs map[string]interface{}) *state.TaskSet {
hotplugDisconnect := st.NewTask("hotplug-disconnect", fmt.Sprintf("Disable connections of interface %q, hotplug key %q", ifaceName, hotplugKey.ShortString()))
setHotplugAttrs(hotplugDisconnect, ifaceName, hotplugKey)
updateSlot := st.NewTask("hotplug-update-slot", fmt.Sprintf("Update slot of interface %q, hotplug key %q", ifaceName, hotplugKey.ShortString()))
setHotplugAttrs(updateSlot, ifaceName, hotplugKey)
updateSlot.Set("slot-attrs", newAttrs)
updateSlot.WaitFor(hotplugDisconnect)
return state.NewTaskSet(hotplugDisconnect, updateSlot)
}
// removeDevice creates tasks to disconnect slots of given device and remove affected slots.
func removeDevice(st *state.State, ifaceName string, hotplugKey snap.HotplugKey) *state.TaskSet {
// hotplug-disconnect task will create hooks and disconnect the slot
hotplugDisconnect := st.NewTask("hotplug-disconnect", fmt.Sprintf("Disable connections of interface %q, hotplug key %q", ifaceName, hotplugKey.ShortString()))
setHotplugAttrs(hotplugDisconnect, ifaceName, hotplugKey)
// hotplug-remove-slot will remove this device's slot from the repository.
removeSlot := st.NewTask("hotplug-remove-slot", fmt.Sprintf("Remove slot for interface %q, hotplug key %q", ifaceName, hotplugKey.ShortString()))
setHotplugAttrs(removeSlot, ifaceName, hotplugKey)
removeSlot.WaitFor(hotplugDisconnect)
return state.NewTaskSet(hotplugDisconnect, removeSlot)
}