summaryrefslogtreecommitdiff
path: root/scripts/hvsupport.py
blob: 89fd0d1d946c32d1217f4fa6c409da7f3263ef8d (plain)
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
#!/usr/bin/env python3
#
# Copyright (C) 2011-2019 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.  If not, see
# <http://www.gnu.org/licenses/>.

import sys
import os.path
import re

if len(sys.argv) != 3:
    print("syntax: %s TOP-SRCDIR TOP-BUILDDIR\n" % sys.argv[0], file=sys.stderr)

srcdir = sys.argv[1]
builddir = sys.argv[2]

symslibvirt = os.path.join(srcdir, "src", "libvirt_public.syms")
symsqemu = os.path.join(srcdir, "src", "libvirt_qemu.syms")
symslxc = os.path.join(srcdir, "src", "libvirt_lxc.syms")
drivertablefiles = [
    os.path.join(srcdir, "src", "driver-hypervisor.h"),
    os.path.join(srcdir, "src", "driver-interface.h"),
    os.path.join(srcdir, "src", "driver-network.h"),
    os.path.join(srcdir, "src", "driver-nodedev.h"),
    os.path.join(srcdir, "src", "driver-nwfilter.h"),
    os.path.join(srcdir, "src", "driver-secret.h"),
    os.path.join(srcdir, "src", "driver-state.h"),
    os.path.join(srcdir, "src", "driver-storage.h"),
    os.path.join(srcdir, "src", "driver-stream.h"),
]

groupheaders = {
    "virHypervisorDriver": "Hypervisor APIs",
    "virNetworkDriver": "Virtual Network APIs",
    "virInterfaceDriver": "Host Interface APIs",
    "virNodeDeviceDriver": "Host Device APIs",
    "virStorageDriver": "Storage Pool APIs",
    "virSecretDriver": "Secret APIs",
    "virNWFilterDriver": "Network Filter APIs",
}


srcs = []
for root, dirs, files in os.walk(os.path.join(srcdir, "src")):
    for file in files:
        if ((file.endswith("driver.c") and
             not file.endswith("vbox_driver.c")) or
                file.endswith("common.c") or
                file.endswith("tmpl.c") or
                file.endswith("monitor.c") or
                file.endswith("udev.c")):
            srcs.append(os.path.join(root, file))


# Map API functions to the header and documentation files they're in
# so that we can generate proper hyperlinks to their documentation.
#
# The function names are grep'd from the XML output of apibuild.py.
def getAPIFilenames(filename):
    files = {}

    with open(filename) as fh:
        for line in fh:
            res = re.search(r"function name='([^']+)' file='([^']+)'", line)
            if res is not None:
                files[res.group(1)] = res.group(2)

    if len(files) == 0:
        raise Exception(("No functions found in %s. " +
                         "Has the apibuild.py output changed?") %
                        filename)

    return files


def parseSymsFile(apisref, prefix, filename, xmlfilename):
    vers = None
    prevvers = None

    filenames = getAPIFilenames(xmlfilename)

    with open(filename) as fh:
        for line in fh:
            line = line.strip()

            if line == "":
                continue
            if line[0] == '#':
                continue
            if line.startswith("global:"):
                continue
            if line.startswith("local:"):
                continue

            groupstartmatch = re.search(r"^\s*%s_(\d+\.\d+\.\d+)\s*{\s*$" %
                                        prefix, line)
            groupendmatch1 = re.search(r"^\s*}\s*;\s*$", line)
            groupendmatch2 = re.search(r"^\s*}\s*%s_(\d+\.\d+\.\d+)\s*;\s*$" %
                                       prefix, line)
            symbolmatch = re.search(r"^\s*(\w+)\s*;\s*$", line)
            if groupstartmatch is not None:
                if vers is not None:
                    raise Exception("malformed syms file when starting group")

                vers = groupstartmatch.group(1)
            elif groupendmatch1 is not None:
                if prevvers is not None:
                    raise Exception("malformed syms file when ending group")

                prevvers = vers
                vers = None
            elif groupendmatch2 is not None:
                if groupendmatch2.group(1) != prevvers:
                    raise Exception(("malformed syms file %s != %s " +
                                     "when ending group") %
                                    (groupendmatch2.group(1), prevvers))

                prevvers = vers
                vers = None
            elif symbolmatch is not None:
                name = symbolmatch.group(1)
                apisref[name] = {
                    "vers": vers,
                    "file": filenames.get(name),
                }
            else:
                raise Exception("unexpected data %s" % line)


apis = {}
# Get the list of all public APIs and their corresponding version
parseSymsFile(apis, "LIBVIRT", symslibvirt,
              os.path.join(builddir, "docs", "libvirt-api.xml"))

# And the same for the QEMU specific APIs
parseSymsFile(apis, "LIBVIRT_QEMU", symsqemu,
              os.path.join(builddir, "docs", "libvirt-qemu-api.xml"))

# And the same for the LXC specific APIs
parseSymsFile(apis, "LIBVIRT_LXC", symslxc,
              os.path.join(builddir, "docs", "libvirt-lxc-api.xml"))


# Some special things which aren't public APIs,
# but we want to report
apis["virConnectSupportsFeature"] = {
    "vers": "0.3.2"
}
apis["virDomainMigratePrepare"] = {
    "vers": "0.3.2"
}
apis["virDomainMigratePerform"] = {
    "vers": "0.3.2"
}
apis["virDomainMigrateFinish"] = {
    "vers": "0.3.2"
}
apis["virDomainMigratePrepare2"] = {
    "vers": "0.5.0"
}
apis["virDomainMigrateFinish2"] = {
    "vers": "0.5.0"
}
apis["virDomainMigratePrepareTunnel"] = {
    "vers": "0.7.2"
}

apis["virDomainMigrateBegin3"] = {
    "vers": "0.9.2"
}
apis["virDomainMigratePrepare3"] = {
    "vers": "0.9.2"
}
apis["virDomainMigratePrepareTunnel3"] = {
    "vers": "0.9.2"
}
apis["virDomainMigratePerform3"] = {
    "vers": "0.9.2"
}
apis["virDomainMigrateFinish3"] = {
    "vers": "0.9.2"
}
apis["virDomainMigrateConfirm3"] = {
    "vers": "0.9.2"
}

apis["virDomainMigrateBegin3Params"] = {
    "vers": "1.1.0"
}
apis["virDomainMigratePrepare3Params"] = {
    "vers": "1.1.0"
}
apis["virDomainMigratePrepareTunnel3Params"] = {
    "vers": "1.1.0"
}
apis["virDomainMigratePerform3Params"] = {
    "vers": "1.1.0"
}
apis["virDomainMigrateFinish3Params"] = {
    "vers": "1.1.0"
}
apis["virDomainMigrateConfirm3Params"] = {
    "vers": "1.1.0"
}


# Now we want to get the mapping between public APIs
# and driver struct fields. This lets us later match
# update the driver impls with the public APis.

# Group name -> hash of APIs { fields -> api name }
groups = {}
ingrp = None
for drivertablefile in drivertablefiles:
    with open(drivertablefile) as fh:
        for line in fh:
            starttablematch = re.search(r"struct _(vir\w*Driver)", line)
            if starttablematch is not None:
                grp = starttablematch.group(1)
                if grp != "virStateDriver" and grp != "virStreamDriver":
                    ingrp = grp
                    groups[ingrp] = {
                        "apis": {},
                        "drivers": {}
                    }
            elif ingrp is not None:
                callbackmatch = re.search(r"^\s*vir(?:Drv)(\w+)\s+(\w+);\s*$",
                                          line)
                if callbackmatch is not None:
                    name = callbackmatch.group(1)
                    field = callbackmatch.group(2)

                    api = "vir" + name
                    if api in apis:
                        groups[ingrp]["apis"][field] = api
                    elif re.search(r"\w+(Open|Close|URIProbe)", api) is not None:
                        continue
                    else:
                        raise Exception(("driver %s does not have " +
                                         "a public API") % name)
                elif re.search(r"};", line):
                    ingrp = None


# Finally, we read all the primary driver files and extract
# the driver API tables from each one.

for src in srcs:
    with open(src) as fh:
        groupsre = "|".join(groups.keys())

        ingrp = None
        impl = None
        for line in fh:
            if ingrp is None:
                m = re.search(r"^\s*(static\s+)?(" +
                              groupsre +
                              r")\s+(\w+)\s*=\s*{", line)
                if m is None:
                    m = re.search(r"^\s*(static\s+)?(" +
                                  groupsre +
                                  r")\s+NAME\(\w+\)\s*=\s*{", line)
                if m is not None:
                    ingrp = m.group(2)
                    impl = src

                    implmatch = re.search(r".*/node_device_(\w+)\.c", impl)
                    if implmatch is None:
                        implmatch = re.search(r".*/(\w+?)_((\w+)_)?(\w+)\.c", impl)
                    if implmatch is None:
                        raise Exception("Unexpected impl format '%s'" % impl)
                    impl = implmatch.group(1)

                    if impl in groups[ingrp]["drivers"]:
                        raise Exception(
                            "Group %s already contains %s" % (ingrp, impl))

                    groups[ingrp]["drivers"][impl] = {}
            else:
                callbackmatch = re.search(r"\s*\.(\w+)\s*=\s*(\w+)\s*,?\s*" +
                                          r"(?:/\*\s*(\d+\.\d+\.\d+)\s*" +
                                          r"(?:-\s*(\d+\.\d+\.\d+))?\s*\*/\s*)?$",
                                          line)
                if callbackmatch is not None:
                    api = callbackmatch.group(1)
                    meth = callbackmatch.group(2)
                    vers = callbackmatch.group(3)
                    deleted = callbackmatch.group(4)

                    if api == "no" or api == "name":
                        continue

                    if meth == "NULL" and deleted is None:
                        raise Exception(
                            ("Method impl for %s is NULL, but " +
                             "no deleted version is provided") % api)

                    if meth != "NULL" and deleted is not None:
                        raise Exception(
                            ("Method impl for %s is non-NULL, but " +
                             "deleted version is provided") % api)

                    if vers is None and api != "connectURIProbe":
                        raise Exception(
                            "Method %s in %s is missing version" %
                            (meth, src))

                    if api not in groups[ingrp]["apis"]:
                        if re.search(r"\w+(Open|Close|URIProbe)", api):
                            continue

                        raise Exception("Found unexpected method " +
                                        "%s in %s" % (api, ingrp))

                    groups[ingrp]["drivers"][impl][api] = {
                        "vers": vers,
                        "deleted": deleted,
                    }

                    if (api == "domainMigratePrepare" or
                            api == "domainMigratePrepare2" or
                            api == "domainMigratePrepare3"):
                        if ("domainMigrate" not in
                                groups[ingrp]["drivers"][impl]):
                            groups[ingrp]["drivers"][impl]["domainMigrate"] = {
                                "vers": vers,
                            }
                elif line.find("}") != -1:
                    ingrp = None


# The '.open' driver method is used for 3 public APIs, so we
# have a bit of manual fixup todo with the per-driver versioning
# and support matrix

groups["virHypervisorDriver"]["apis"]["openAuth"] = \
    "virConnectOpenAuth"
groups["virHypervisorDriver"]["apis"]["openReadOnly"] = \
    "virConnectOpenReadOnly"
groups["virHypervisorDriver"]["apis"]["domainMigrate"] = \
    "virDomainMigrate"

openAuthVers = (0 * 1000 * 1000) + (4 * 1000) + 0

drivers = groups["virHypervisorDriver"]["drivers"]
for drv in drivers.keys():
    openVersStr = drivers[drv]["connectOpen"]["vers"]
    openVers = 0
    if openVersStr != "Y":
        openVersBits = openVersStr.split(".")
        if len(openVersBits) != 3:
            raise Exception("Expected 3 digit version for %s" % openVersStr)
        openVers = ((int(openVersBits[0]) * 1000 * 1000) +
                    (int(openVersBits[1]) * 1000) +
                    int(openVersBits[2]))

    # virConnectOpenReadOnly always matches virConnectOpen version
    drivers[drv]["connectOpenReadOnly"] = \
        drivers[drv]["connectOpen"]

    # virConnectOpenAuth is always 0.4.0 if the driver existed
    # before this time, otherwise it matches the version of
    # the driver's virConnectOpen entry
    if openVersStr == "Y" or openVers >= openAuthVers:
        vers = openVersStr
    else:
        vers = "0.4.0"
    drivers[drv]["connectOpenAuth"] = {
        "vers": vers,
    }


# Another special case for the virDomainCreateLinux which was replaced
# with virDomainCreateXML
groups["virHypervisorDriver"]["apis"]["domainCreateLinux"] = \
    "virDomainCreateLinux"

createAPIVers = (0 * 1000 * 1000) + (0 * 1000) + 3

for drv in drivers.keys():
    if "domainCreateXML" not in drivers[drv]:
        continue
    createVersStr = drivers[drv]["domainCreateXML"]["vers"]
    createVers = 0
    if createVersStr != "Y":
        createVersBits = createVersStr.split(".")
        if len(createVersBits) != 3:
            raise Exception("Expected 3 digit version for %s" % createVersStr)
        createVers = ((int(createVersBits[0]) * 1000 * 1000) +
                      (int(createVersBits[1]) * 1000) +
                      int(createVersBits[2]))

    # virCreateLinux is always 0.0.3 if the driver existed
    # before this time, otherwise it matches the version of
    # the driver's virCreateXML entry
    if createVersStr == "Y" or createVers >= createAPIVers:
        vers = createVersStr
    else:
        vers = "0.0.3"

    drivers[drv]["domainCreateLinux"] = {
        "vers": vers,
    }


# Finally we generate the HTML file with the tables

print('''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<body id="hvsupport">
<h1>libvirt API support matrix</h1>

<ul id="toc"></ul>

<p>
This page documents which libvirt calls work on
which libvirt drivers / hypervisors, and which version the API appeared
in. If a hypervisor driver later dropped support for the API, the version
when it was removed is also mentioned (highlighted in
<span class="removedhv">dark red</span>).
</p>
''')

for grp in sorted(groups.keys()):
    print("<h2><a id=\"%s\">%s</a></h2>" % (grp, groupheaders[grp]))
    print('''<table class="top_table">
<thead>
<tr>
<th>API</th>
<th>Version</th>''')

    for drv in sorted(groups[grp]["drivers"].keys()):
        print("  <th>%s</th>" % drv)

    print('''</tr>
</thead>
<tbody>''')

    row = 0

    def sortkey(field):
        return groups[grp]["apis"][field]

    for field in sorted(groups[grp]["apis"].keys(), key=sortkey):
        api = groups[grp]["apis"][field]
        vers = apis[api]["vers"]
        htmlgrp = apis[api].get("file")
        print("<tr>")

        if htmlgrp is not None:
            print(('''<td>\n<a href=\"html/libvirt-%s.html#%s\">''' +
                   '''%s</a>\n</td>''') %
                  (htmlgrp, api, api))
        else:
            print("<td>\n%s</td>" % api)

        print("<td>%s</td>" % vers)

        for drv in sorted(groups[grp]["drivers"].keys()):
            info = ""
            if field in groups[grp]["drivers"][drv]:
                vers = groups[grp]["drivers"][drv][field]["vers"]
                if vers is not None:
                    info = info + vers

                deleted = groups[grp]["drivers"][drv][field].get("deleted")
                if deleted is not None:
                    info = info + (''' - <span class="removedhv">''' +
                                   '''%s</span>''' % deleted)

            print("<td>%s</td>" % info)

        print("</tr>")

        row = row + 1
        if (row % 15) == 0:
            print('''<tr>
<th>API</th>
<th>Version</th>''')

            for drv in sorted(groups[grp]["drivers"].keys()):
                print("  <th>%s</th>" % drv)

            print("</tr>")

    print("</tbody>\n</table>")

print("</body>\n</html>")