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
|
/*
* libap - A collection of tools for ap/vfio-ap management
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <ctype.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#ifdef HAVE_JSONC
#include <json-c/json.h>
#endif /* HAVE_JSONC */
#include "lib/ap.h"
#include "lib/util_file.h"
#include "lib/util_libc.h"
#include "lib/util_lockfile.h"
#include "lib/util_panic.h"
#include "lib/util_path.h"
#include "lib/util_udev.h"
static const char default_mask[AP_MASK_SIZE] =
"0x0000000000000000000000000000000000000000000000000000000000000000";
/*
* Return sysfs path to a bus attribute
* Note: caller is responsible for freeing the returned string
*/
static char *path_get_bus_attr(const char *bus, const char *attr)
{
return util_path_sysfs("bus/%s/%s", bus, attr);
}
/*
* Compare two vfio_ap nodes based upon their id value. Return:
* -1: a < b
* 1: a > b
* 0: a == b
*/
static int vfio_ap_node_cmp(void *a, void *b, void *UNUSED(data))
{
struct vfio_ap_node *a_node = a, *b_node = b;
if (a_node->id < b_node->id)
return -1;
return (a_node->id > b_node->id);
}
static void vfio_ap_node_add_tail(struct util_list *list, unsigned int val)
{
struct vfio_ap_node *node = util_zalloc(sizeof(struct vfio_ap_node));
node->id = val;
util_list_add_tail(list, node);
}
/* Remove duplicate entries from a sorted vfio_ap_node list */
static void vfio_ap_node_remove_dupes(struct util_list *list)
{
struct vfio_ap_node *node, *check, *next;
util_list_iterate(list, node) {
/* Remove any subsequent duplicates */
check = util_list_next(list, node);
while (check) {
next = util_list_next(list, check);
if (node->id == check->id) {
util_list_remove(list, check);
free(check);
}
check = next;
}
}
}
static bool starts_with(const char *str, const char *s)
{
size_t len = strlen(s);
return (strncmp(str, s, len) == 0);
}
/*
* Pass a comma-delimited string of IDs (adapter, domains or control domains)
* and add these IDs to the input list. The resulting list will be sorted
* and duplicates removed before returning.
*/
static void modify_device_attr(struct util_list *list, char *value)
{
unsigned int val;
char *curr;
curr = strtok(value, ",");
if (curr == NULL)
return;
/* Create list from input setting */
while (curr != NULL) {
val = strtol(curr, NULL, 0);
vfio_ap_node_add_tail(list, val);
curr = strtok(NULL, ",");
}
/* Cleanup the list */
util_list_sort(list, vfio_ap_node_cmp, NULL);
vfio_ap_node_remove_dupes(list);
}
/*
* Pass a comma-delimited string of masks (adapters,domains,controls) and
* for each ON bit in these masks add the associated ID to the device
* lists.
*/
static void modify_device_ap_config(struct vfio_ap_device *dev,
char *value)
{
char *mask, *adapters, *domains, *controls;
mask = util_strdup(value);
adapters = strtok(mask, ",");
domains = strtok(NULL, ",");
controls = strtok(NULL, ",");
util_assert((!strtok(NULL, ",")) && adapters && domains && controls,
"Invalid ap_config attribute encountered %s", value);
/*
* ap_config overwrites the current list of adapters, domains and
* control domains. Clear the current lists before generating new ones
* based upon the input mask values.
*/
ap_list_remove_all(dev->adapters);
ap_list_remove_all(dev->domains);
ap_list_remove_all(dev->controls);
ap_mask_to_list(adapters, dev->adapters);
ap_mask_to_list(domains, dev->domains);
ap_mask_to_list(controls, dev->controls);
}
static void load_attr_to_device(struct vfio_ap_device *dev, char *attr,
const char *value)
{
char *v = util_strdup(value);
if (strcmp(attr, "assign_adapter") == 0)
modify_device_attr(dev->adapters, v);
else if (strcmp(attr, "assign_domain") == 0)
modify_device_attr(dev->domains, v);
else if (strcmp(attr, "assign_control_domain") == 0)
modify_device_attr(dev->controls, v);
else if (strcmp(attr, "ap_config") == 0)
modify_device_ap_config(dev, v);
free(v);
}
/**
* Print the contents of the vfio-ap device struct to stderr. Used for
* debugging.
*
* @param[in] dev vfio-ap device strcucture to print
*/
void print_ap_device(struct vfio_ap_device *dev)
{
struct vfio_ap_node *node;
warnx("Device %s:", dev->uuid);
warnx("Type: %s", dev->type);
if (dev->manual)
warnx("Start: MANUAL");
else
warnx("Start: AUTO");
if (util_list_is_empty(dev->adapters)) {
warnx("Adapters: (none)");
} else {
warnx("Adapters:");
util_list_iterate(dev->adapters, node) {
warnx(" %u", node->id);
}
}
if (util_list_is_empty(dev->domains)) {
warnx("Domains: (none)");
} else {
warnx("Domains:");
util_list_iterate(dev->domains, node) {
warnx(" %u", node->id);
}
}
if (util_list_is_empty(dev->controls)) {
warnx("Controls: (none)");
} else {
warnx("Controls:");
util_list_iterate(dev->controls, node) {
warnx(" %u", node->id);
}
}
}
/**
* Determine if the input string is a valid Universally Unique Identifier
*
* @param[in] uuid Character string to inspect
*
* @retval true Specified string is a valid UUID
* @retval false Specified string is not a valid UUID
*/
bool is_valid_uuid(const char *uuid)
{
uint32_t s1, s2, s3, s4;
uint64_t s5;
char d;
return (strlen(uuid) == 36 && sscanf(uuid, "%8x-%4x-%4x-%4x-%12x %c",
&s1, &s2, &s3, &s4,
(unsigned int *) &s5, &d) == 5);
}
/*
* For an input string of hex characters, find the nth character and return its
* numeric value to the caller.
*/
static int get_hexbyte_value(int n, const char *hexbytestr)
{
int i = 0, v = 0;
char c;
if (strncmp(hexbytestr, "0x", 2) == 0)
i = 2;
/*
* The specified mask is only valid if it includes at least 64 hex
* digits. The specified mask may optionally include a leading '0x'
*/
util_assert((strlen(hexbytestr) >= (size_t)(AP_MASK_SIZE - i - 1)),
"Invalid hex string provided for mask: %s", hexbytestr);
c = hexbytestr[i + n / 4];
if (c >= '0' && c <= '9')
v = c - '0';
else if (c >= 'a' && c <= 'f')
v = 10 + c - 'a';
else if (c >= 'A' && c <= 'F')
v = 10 + c - 'A';
else
util_assert(false, "Could not parse hex digit '%c'", c);
return v;
}
/**
* For an input hex string, determine if the specified nth bit is ON or OFF.
*
* @param[in] n Bit number to test
* @param[in, out] hexbytestr Character string of hex characters
*
* @retval 0 Specified bit is OFF
* @retval != 0 Specified bit is ON
*/
int ap_test_bit(int n, const char *hexbytestr)
{
int v;
v = get_hexbyte_value(n, hexbytestr);
return v & (1 << (3 - (n % 4)));
}
/**
* For an input hex string, set the nth bit true/false
*
* @param[in] n Bit number to set
* @param[in, out] hexbytestr Character string of hex characters
* @param[in] val Bit is to be set ON (true) or OFF (false)
*/
void ap_set_bit(int n, char *hexbytestr, bool val)
{
char c = 0;
int v, m, i = 0;
v = get_hexbyte_value(n, hexbytestr);
/* Calculate the bit mask */
m = (1 << (3 - (n % 4)));
/* Return if bit already at correct value */
if (((val) && ((v & m) != 0)) || (!val && ((v & m) == 0)))
return;
if (val)
v = v + m;
else
v = v - m;
if (v < 10)
c = '0' + v;
else if (v >= 10 && v <= 15)
c = 'a' + (v - 10);
else
util_assert(false, "Could not set bit value '%d'", v);
if (strncmp(hexbytestr, "0x", 2) == 0)
i = 2;
/* Set the new value */
hexbytestr[i + n / 4] = c;
}
/**
* Return sysfs path to vfio_ap mdev
* Note: caller is responsible for freeing the returned string
*
* @param[in] uuid Character string containing an mdev UUID
*
* @retval != 0 sysfs path for a vfio-ap device with this UUID
*/
char *path_get_vfio_ap_mdev(const char *uuid)
{
return util_path_sysfs("%s/%s", VFIO_AP_PARENT_PATH, uuid);
}
/**
* Return path to mdevctl config file for specified UUID
* Note: caller is responsible for freeing the returned string
*
* @param[in] uuid Character string containing an mdev UUID
*
* @retval != 0 config file path for vfio-ap device with this UUID
*/
char *path_get_vfio_ap_mdev_config(const char *uuid)
{
char *path;
util_asprintf(&path, "%s/%s", VFIO_AP_CONFIG_PATH, uuid);
return path;
}
/**
* Return sysfs path to vfio_ap mdev attribute (matrix, remove, ...)
* Note: caller is responsible for freeing the returned string
*
* @param[in] uuid Character string containing an mdev UUID
* @param[in] attr Character string containing device attribute name
*
* @retval != 0 sysfs path to specified attribute for this device
*/
char *path_get_vfio_ap_attr(const char *uuid, const char *attr)
{
return util_path_sysfs("%s/%s/%s", VFIO_AP_PARENT_PATH, uuid, attr);
}
/**
* Return path to ap udev config file
* Note: caller is responsible for freeing the returned string
*
* @retval != 0 path to the ap udev config file
*/
char *path_get_ap_udev(void)
{
char *path;
util_asprintf(&path, "%s", AP_UDEV_FILE);
return path;
}
/**
* Take one line from the active 'matrix' attribute and parse it
* into a list of adapters and domains. Each line of the 'matrix'
* attribute is presented as a "adapter.domain" (e.g. "03.0005") where the
* numeric values are always hexadecimal.
* In a case where no adapters are assigned, a valid string might be ".0005"
* In a case where no domains are assigned, a valid string might be "03."
*
* @param[in, out] dev Vfio-ap struct that will be updated
* @param[in] matrix Character string to parse
*/
void vfio_ap_parse_matrix(struct vfio_ap_device *dev, char *matrix)
{
char *curr;
int val;
if (!matrix)
return;
if (*matrix != '.') {
/* Handle a device with adapters */
curr = strtok(matrix, ".");
val = strtol(curr, NULL, 16);
vfio_ap_node_add_tail(dev->adapters, val);
curr = strtok(NULL, "\n");
} else {
/* Handle a device with no adapters */
curr = strtok(matrix + 1, "\n");
}
/* Leave now if the device has no domains */
if (!curr)
return;
/* Get the domain */
val = strtol(curr, NULL, 16);
vfio_ap_node_add_tail(dev->domains, val);
}
/**
* Function to sort the results of repeated vfio_ap_parse_matrix calls
*
* @param[in, out] dev Vfio-ap struct whose lists will be sorted
*/
void vfio_ap_sort_matrix_results(struct vfio_ap_device *dev)
{
/* Sort the lists for later use */
util_list_sort(dev->adapters, vfio_ap_node_cmp, NULL);
util_list_sort(dev->domains, vfio_ap_node_cmp, NULL);
/* Run the lists and delete duplicates */
vfio_ap_node_remove_dupes(dev->adapters);
vfio_ap_node_remove_dupes(dev->domains);
}
/**
* Take the string provided by the active 'control_domains' attribute and
* parse it into a list of control domains
*
* @param[in, out] dev Vfio-ap struct that will be updated
* @param[in] control Character string to parse
*/
void vfio_ap_parse_control(struct vfio_ap_device *dev, char *control)
{
char *curr;
int val;
curr = strtok(control, "\n");
while (curr != NULL) {
val = strtol(curr, NULL, 16);
vfio_ap_node_add_tail(dev->controls, val);
curr = strtok(NULL, "\n");
}
}
/**
* Determine if the specified device is currently active. If so, see if
* it is enabled for dynamic configuration support.
*
* @param[in] dev Vfio-ap struct
*
* @retval True Device is active and enabled for dynamic config
* @retval False Device is not active OR no dynamic config support
*/
bool vfio_ap_need_dynamic_config(struct vfio_ap_device *dev)
{
char *attr = path_get_vfio_ap_attr(dev->uuid, "ap_config");
if (!attr)
return false;
if (!util_path_is_readable(attr))
return false;
return true;
}
#ifdef HAVE_JSONC
/**
* For a given path, read in the contents. If no path is provided, get the
* input from stdin instead.
*
* @param[in] path Path to mdevctl config file
* @param[in, out] dev Vfio-ap struct that will be updated
*
* @retval 0 Config read successfully, dev updated
* @retval -1 Failed to read config, dev may have partial info
*/
int vfio_ap_read_device_config(const char *path, struct vfio_ap_device *dev)
{
json_object *root, *type, *start, *attrs, *attr;
int i, len, rc = 0;
const char *val;
if (path == NULL)
root = json_object_from_fd(STDIN_FILENO);
else
root = json_object_from_file(path);
if (root == NULL)
return -1;
if (json_object_object_get_ex(root, "mdev_type", &type)) {
val = json_object_get_string(type);
if (!val)
goto err;
dev->type = util_strdup(val);
if (strcmp(val, "vfio_ap-passthrough") != 0)
goto err;
}
if (json_object_object_get_ex(root, "start", &start)) {
val = json_object_get_string(start);
if (!val)
goto err;
if (strcmp(val, "auto") == 0)
dev->manual = false;
else if (strcmp(val, "manual") == 0)
dev->manual = true;
else
goto err;
}
if (json_object_object_get_ex(root, "attrs", &attrs)) {
len = json_object_array_length(attrs);
for (i = 0; i < len; i++) {
attr = json_object_array_get_idx(attrs, i);
json_object_object_foreach(attr, key, setting) {
val = json_object_get_string(setting);
load_attr_to_device(dev, key, val);
}
}
}
out:
json_object_put(root);
return rc;
err:
rc = -1;
goto out;
}
#else
int vfio_ap_read_device_config(const char *path, struct vfio_ap_device *dev)
{
return -1;
}
#endif /* HAVE_JSONC */
/**
* Allocate and initialize a vfio-ap device structure.
*
* @retval !=0 Address of the new vfio-ap device structure
*/
struct vfio_ap_device *vfio_ap_device_new(void)
{
struct vfio_ap_device *dev;
dev = util_zalloc(sizeof(struct vfio_ap_device));
dev->manual = false;
dev->type = NULL;
dev->uuid = NULL;
dev->adapters = util_list_new(struct vfio_ap_node, node);
dev->domains = util_list_new(struct vfio_ap_node, node);
dev->controls = util_list_new(struct vfio_ap_node, node);
return dev;
}
/**
* Re-initialize a vfio-ap device structure, leaving the structure allocated.
*
* @param[in, out] dev Vfio-ap struct that will be re-initialized
*/
void vfio_ap_device_clear(struct vfio_ap_device *dev)
{
if (dev == NULL)
return;
dev->manual = false;
if (dev->type) {
free(dev->type);
dev->type = NULL;
}
if (dev->uuid) {
free(dev->uuid);
dev->uuid = NULL;
}
ap_list_remove_all(dev->adapters);
ap_list_remove_all(dev->domains);
ap_list_remove_all(dev->controls);
}
/**
* Clear and release a vfio-ap device structure.
*
* @param[in, out] dev Vfio-ap struct that will be freed
*/
void vfio_ap_device_free(struct vfio_ap_device *dev)
{
if (dev == NULL)
return;
vfio_ap_device_clear(dev);
util_list_free(dev->adapters);
util_list_free(dev->domains);
util_list_free(dev->controls);
free(dev);
}
static int read_sysfs_mask(const char *path, char *mask, int size)
{
return util_file_read_line(mask, size, "%s", path);
}
/**
* Get the apmask and aqmask from sysfs
*
* @param[in, out] ap Buffer to hold apmask contents
* @param[in, out] aq Buffer to hold aqmask contents
* @param[in] size Size of the mask buffers
*
* @retval 0 Both mask values read successfully
* @retval != 0 Failed to read one or both mask values
*/
int ap_read_sysfs_masks(char *ap, char *aq, int size)
{
char *path;
int rc = 0;
path = path_get_bus_attr("ap", "apmask");
rc = read_sysfs_mask(path, ap, size);
free(path);
if (rc != 0)
goto out;
path = path_get_bus_attr("ap", "aqmask");
rc = read_sysfs_mask(path, aq, size);
free(path);
out:
return rc;
}
/**
* Get the apmask and aqmask from udev, falling back to sysfs if a udev rule
* is not available or does not provide values for both masks.
* The values in read_ap and read_aq tell the caller whether each mask was
* successfully loaded from udev or if the sysfs value was substituted
* Note: both ap and aq must point to a string that is at least AP_MASK_SIZE
* in length.
*
* @param[in] path Path to the ap udev file
* @param[in, out] ap Buffer to hold apmask contents
* @param[in, out] aq Buffer to hold aqmask contents
* @param[out] read_ap Specifies if an apmask value was read from udev
* @param[out] read_aq Specifies if an aqmask value was read from udev
*
* @retval true Udev file read successfully or did not exist
* @retval false Udev file exists but error was encountered
*/
bool ap_read_udev_masks(char *path, char *ap, char *aq, bool *read_ap,
bool *read_aq)
{
struct util_udev_entry_node *entry;
struct util_udev_file *file = NULL;
struct util_udev_line_node *line;
char sysap[AP_MASK_SIZE];
char sysaq[AP_MASK_SIZE];
int rc;
/* Assume we fail to read both masks */
*read_ap = *read_aq = false;
/* If a udev file doesn't exist, quietly use the active masks */
if (!util_path_exists(path))
goto out;
rc = util_udev_read_file(path, &file);
/* If errors were encountered reading the udev file, exit now */
if (rc)
return false;
util_list_iterate(&file->lines, line) {
entry = util_list_start(&line->entries);
/* Skip comments and empty lines. */
if (!entry)
continue;
if (starts_with(entry->key, "ATTR{")) {
if (strstr(entry->key, "apmask")) {
util_strlcpy(ap, entry->value, AP_MASK_SIZE);
*read_ap = true;
} else if (strstr(entry->key, "aqmask")) {
util_strlcpy(aq, entry->value, AP_MASK_SIZE);
*read_aq = true;
}
}
}
util_udev_free_file(file);
out:
/* If we didn't read in masks, use current sysfs values */
if ((!*read_ap) || (!*read_aq)) {
rc = ap_read_sysfs_masks(sysap, sysaq, AP_MASK_SIZE);
if (rc != 0)
return false;
if (!*read_ap)
strcpy(ap, sysap);
if (!*read_aq)
strcpy(aq, sysaq);
}
return true;
}
/**
* For a given bitmask, create a list of vfio_ap_node entries corresponding
* to the ON bits in the mask.
*
* @param[in] mask Character string of hex characters
* @param[in, out] list List to be updated with entries for each ON bit
*/
void ap_mask_to_list(char *mask, struct util_list *list)
{
int i;
if (mask == NULL || list == NULL)
return;
for (i = 0; i <= AP_MAX_MASK_VALUE; i++) {
if (ap_test_bit(i, mask))
vfio_ap_node_add_tail(list, i);
}
/* Could have duplicates if the input list was not empty */
util_list_sort(list, vfio_ap_node_cmp, NULL);
vfio_ap_node_remove_dupes(list);
}
/**
* For the specified list, remove all elements and free each node
*
* @param[in, out] list List that will have all entries removed
*/
void ap_list_remove_all(struct util_list *list)
{
struct vfio_ap_node *node;
while (!util_list_is_empty(list)) {
node = util_list_start(list);
util_list_remove(list, node);
free(node);
}
}
static unsigned int random_delay(void)
{
static bool libap_seed = true;
struct timeval t;
if (libap_seed) {
gettimeofday(&t, NULL);
srand((unsigned int)((t.tv_sec + t.tv_usec) % UINT_MAX));
libap_seed = false;
}
return AP_LOCK_DELAY_US + (rand() % AP_LOCK_VARIANCE_US);
}
/**
* Return a mask of assigned adapters for the specified vfio_ap device.
* Note: caller is responsible for freeing the returned string
*
* @param[in] dev Vfio-ap struct to get adapter mask from
* @param[in, out] size Size of mask buffer returned
*
* @retval != 0 Adapter mask (hex string)
* @retval 0 Failed to generate a mask
*/
char *vfio_ap_device_get_adapter_mask(struct vfio_ap_device *dev, int *size)
{
struct vfio_ap_node *node;
char *mask;
if (!dev || !size)
return NULL;
mask = util_strdup(default_mask);
*size = AP_MASK_SIZE;
util_list_iterate(dev->adapters, node) {
ap_set_bit(node->id, mask, true);
}
return mask;
}
/**
* Return a mask of assigned domains for the specified vfio_ap device.
* Note: caller is responsible for freeing the returned string
*
* @param[in] dev Vfio-ap struct to get domain mask from
* @param[in, out] size Size of mask buffer returned
*
* @retval != 0 Domain mask (hex string)
* @retval 0 Failed to generate a mask
*/
char *vfio_ap_device_get_domain_mask(struct vfio_ap_device *dev, int *size)
{
struct vfio_ap_node *node;
char *mask;
if (!dev || !size)
return NULL;
mask = util_strdup(default_mask);
*size = AP_MASK_SIZE;
util_list_iterate(dev->domains, node) {
ap_set_bit(node->id, mask, true);
}
return mask;
}
/**
* Return a mask of assigned control domains for the specified vfio_ap device.
* Note: caller is responsible for freeing the returned string
*
* @param[in] dev Vfio-ap struct to get control domain mask from
* @param[in, out] size Size of mask buffer returned
*
* @retval != 0 Control domain mask (hex string)
* @retval 0 Failed to generate a mask
*/
char *vfio_ap_device_get_control_mask(struct vfio_ap_device *dev, int *size)
{
struct vfio_ap_node *node;
char *mask;
if (!dev || !size)
return NULL;
mask = util_strdup(default_mask);
*size = AP_MASK_SIZE;
util_list_iterate(dev->controls, node) {
ap_set_bit(node->id, mask, true);
}
return mask;
}
/**
* Acquire the ap config lock using this Process ID
*
* @retval 0 Lock acquired on behalf of this process
*
* @retval != 0 Error, lock was not obtained
*/
int ap_get_lock(void)
{
unsigned int delay = random_delay();
return util_lockfile_lock_cw(AP_LOCKFILE, AP_LOCK_RETRIES, delay, delay);
}
/**
* Acquire the ap config lock using the Parent Process ID -- intended for use
* by the mdevctl callout ap-check utility
*
* @retval 0 Lock acquired on behalf of parent process
* @retval != 0 Error, lock was not obtained
*/
int ap_get_lock_callout(void)
{
unsigned int delay = random_delay();
return util_lockfile_parent_lock_cw(AP_LOCKFILE, AP_LOCK_RETRIES, delay,
delay);
}
/**
* Attempt to acquire the ap config lock using the Parent Process ID without
* waiting/retries. Detect if the attempt was rejected because the lock is
* already held by the Parent Process ID.
*
* @retval 0 Lock acquired on behalf of parent process
* @retval 1 Lock not obtained, already held by parent
* @retval != 0 Lock was not obtained, other error
*/
int ap_try_lock_callout(void)
{
int pid, ppid, rc;
if (util_lockfile_parent_lock(AP_LOCKFILE, 0)) {
/* Lock is already held, let's peek at the owner */
ppid = getppid();
rc = util_lockfile_peek_owner(AP_LOCKFILE, &pid);
if (rc || pid != ppid) {
/* We didn't get the lock, unknown or other owner */
return 2;
}
/* Signify that the lock is already held by the caller */
return 1;
}
return 0;
}
/**
* Release the ap config lock
*
* @retval 0 Lock successfully released or file didn't exist
* @retval != 0 Error removing the lockfile
*/
int ap_release_lock(void)
{
return util_lockfile_release(AP_LOCKFILE);
}
int ap_release_lock_callout(void)
{
return util_lockfile_parent_release(AP_LOCKFILE);
}
|