~emersion/drmdb

ref: 925209c63a2c93f532a72ea08a0327ea5af5e944 drmdb/server.go -rw-r--r-- 7.0 KiB
925209c6Simon Ser go fmt 2 years ago
                                                                                
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
package drmdb

import (
	"encoding/json"
	"fmt"
	"html/template"
	"io"
	"net/http"
	"os"
	"strings"

	"git.sr.ht/~emersion/drmdb/drmtree"
	"git.sr.ht/~emersion/go-drm"
	"git.sr.ht/~emersion/go-hwids"
	"github.com/labstack/echo/v4"
	"github.com/mcuadros/go-version"
)

type Template struct {
	t *template.Template
}

func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
	return t.t.ExecuteTemplate(w, name, data)
}

func badRequest(c echo.Context, msg string, err error) error {
	if err != nil {
		c.Logger().Printf("%v: %v", msg, err)
	}
	return c.String(http.StatusBadRequest, "Error: "+msg+"\n")
}

type indexData struct {
	Host string
}

type propertyData struct {
	Type       drm.PropertyType
	ObjectType drm.ObjectType
	Drivers    map[string]bool
}

type deviceData struct {
	ID      string
	BusType drm.BusType
	Vendor  string
	Name    string
	Driver  string
}

func walkProps(props drmtree.PropertyMap, obj drm.AnyID, f func(drm.AnyID, string, *drmtree.Property) error) error {
	for name, prop := range props {
		if err := f(obj, name, &prop); err != nil {
			return err
		}
	}
	return nil
}

func walkNodeProps(n *drmtree.Node, f func(drm.AnyID, string, *drmtree.Property) error) error {
	for _, conn := range n.Connectors {
		if err := walkProps(conn.Properties, conn.ID, f); err != nil {
			return err
		}
	}
	for _, crtc := range n.CRTCs {
		if err := walkProps(crtc.Properties, crtc.ID, f); err != nil {
			return err
		}
	}
	for _, plane := range n.Planes {
		if err := walkProps(plane.Properties, plane.ID, f); err != nil {
			return err
		}
	}
	return nil
}

func walkLatestDriver(fn func(n *drmtree.Node) error) error {
	latest := make(map[string]*drmtree.Node)
	err := walk(func(n *drmtree.Node) error {
		other, ok := latest[n.Driver.Name]
		if ok {
			if n.Driver.Version.Less(&other.Driver.Version) {
				return nil
			}
			// Linux is the upstream
			if other.Driver.Kernel.SysName == "Linux" && n.Driver.Kernel.SysName != "Linux" {
				return nil
			}
			if version.Compare(n.Driver.Kernel.Release, other.Driver.Kernel.Release, "<=") {
				return nil
			}
		}
		latest[n.Driver.Name] = n
		return nil
	})
	if err != nil {
		return err
	}

	for _, n := range latest {
		if err := fn(n); err != nil {
			return err
		}
	}
	return nil
}

func loadPCIIDs() (vendors map[uint16]string, devices map[uint32]string, err error) {
	vendors = make(map[uint16]string)
	devices = make(map[uint32]string)

	db, err := hwids.OpenPCI()
	if err != nil {
		return nil, nil, err
	}
	defer db.Close()

	for {
		id, err := db.NextID()
		if err == io.EOF {
			break
		} else if err != nil {
			return nil, nil, err
		}

		switch id.Type {
		case hwids.PCIVendor:
			vendors[id.Vendor] = id.VendorName
		case hwids.PCIDevice:
			k := uint32(id.Vendor)<<16 | uint32(id.Device)
			devices[k] = id.DeviceName
		}
	}

	return vendors, devices, db.Close()
}

func New() *echo.Echo {
	e := echo.New()

	if err := os.MkdirAll(dbDir, 0755); err != nil {
		e.Logger.Fatal("Failed to create DB dir:", err)
	}

	e.Renderer = &Template{
		t: template.Must(template.ParseGlob("public/*.html")),
	}

	pciVendors, pciDevices, err := loadPCIIDs()
	if err != nil {
		e.Logger.Error("Failed to load PCI IDs:", err)
	}

	e.GET("/", func(c echo.Context) error {
		return c.Render(http.StatusOK, "index.html", &indexData{
			Host: c.Request().Host,
		})
	})

	e.POST("/submit", func(c echo.Context) error {
		lr := io.LimitedReader{R: c.Request().Body, N: 512 * 1024}
		var nodes drmtree.NodeMap
		if err := json.NewDecoder(&lr).Decode(&nodes); err != nil {
			return badRequest(c, "invalid JSON", err)
		} else if len(nodes) == 0 {
			return badRequest(c, "data is empty", nil)
		}

		success := false
		for name, n := range nodes {
			if err := store(n); err != nil {
				fmt.Fprintf(c.Response(), "%s: error: %v\n", name, err)
			} else {
				fmt.Fprintf(c.Response(), "%s: data uploaded\n", name)
				success = true
			}
		}

		if success {
			fmt.Fprintf(c.Response(), "Thanks!\n")
		}
		return nil
	})

	e.GET("/snapshot.tar.gz", func(c echo.Context) error {
		h := c.Response().Header()
		h.Set("Content-Type", "application/gzip")
		h.Set("Content-Disposition", "attachment; filename=\"drmdb-snapshot.tar.gz\"")
		return writeSnapshot(c.Response())
	})

	e.GET("/drivers", func(c echo.Context) error {
		var drivers []*drmtree.Driver
		err := walkLatestDriver(func(n *drmtree.Node) error {
			drivers = append(drivers, n.Driver)
			return nil
		})
		if err != nil {
			return err
		}

		return c.Render(http.StatusOK, "drivers.html", struct {
			Drivers []*drmtree.Driver
		}{drivers})
	})

	e.GET("/devices", func(c echo.Context) error {
		devices := make(map[string]deviceData)
		err := walk(func(n *drmtree.Node) error {
			switch dev := n.Device.DeviceData.(type) {
			case *drmtree.DevicePCI:
				id := fmt.Sprintf("%04X:%04X", dev.Vendor, dev.Device)
				devices[id] = deviceData{
					ID:      id,
					BusType: n.Device.BusType,
					Vendor:  pciVendors[uint16(dev.Vendor)],
					Name:    pciDevices[dev.Vendor<<16|dev.Device],
					Driver:  n.Driver.Name,
				}
			case *drmtree.DevicePlatform:
				id := strings.Join(dev.Compatible, " ")
				devices[id] = deviceData{
					ID:      id,
					BusType: n.Device.BusType,
					Driver:  n.Driver.Name,
				}
			}

			return nil
		})
		if err != nil {
			return err
		}

		return c.Render(http.StatusOK, "devices.html", struct {
			Devices map[string]deviceData
		}{devices})
	})

	e.GET("/capabilities", func(c echo.Context) error {
		var drivers []string
		caps := make(map[string]map[string]*uint64)
		clientCaps := make(map[string]map[string]bool)
		err := walkLatestDriver(func(n *drmtree.Node) error {
			drv := n.Driver.Name
			drivers = append(drivers, drv)

			for name, val := range n.Driver.Caps {
				if _, ok := caps[name]; !ok {
					caps[name] = make(map[string]*uint64)
				}
				caps[name][drv] = val
			}

			for name, supported := range n.Driver.ClientCaps {
				if _, ok := clientCaps[name]; !ok {
					clientCaps[name] = make(map[string]bool)
				}
				clientCaps[name][drv] = supported
			}

			return nil
		})
		if err != nil {
			return err
		}

		return c.Render(http.StatusOK, "capabilities.html", struct {
			Drivers    []string
			Caps       map[string]map[string]*uint64
			ClientCaps map[string]map[string]bool
		}{drivers, caps, clientCaps})
	})

	e.GET("/properties", func(c echo.Context) error {
		drivers := make(map[string]struct{})
		props := make(map[string]propertyData)
		err := walk(func(n *drmtree.Node) error {
			drv := n.Driver.Name
			drivers[drv] = struct{}{}

			return walkNodeProps(n, func(obj drm.AnyID, name string, prop *drmtree.Property) error {
				if _, ok := props[name]; !ok {
					props[name] = propertyData{
						Type:       prop.Type,
						ObjectType: obj.Type(),
						Drivers:    make(map[string]bool),
					}
				}
				props[name].Drivers[drv] = true
				return nil
			})
		})
		if err != nil {
			return err
		}

		return c.Render(http.StatusOK, "properties.html", struct {
			Drivers    map[string]struct{}
			Properties map[string]propertyData
		}{drivers, props})
	})

	e.Static("/assets", "public/assets")

	return e
}