| 604 | static struct nlmsg nlmsg2; |
| 605 | |
| 606 | static void initialize_devlink_ports(const char* bus_name, const char* dev_name, |
| 607 | const char* netdev_prefix) |
| 608 | { |
| 609 | struct genlmsghdr genlhdr; |
| 610 | int len, total_len, id, err, offset; |
| 611 | uint16_t netdev_index; |
| 612 | int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); |
| 613 | if (sock == -1) |
| 614 | exit(1); |
| 615 | int rtsock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); |
| 616 | if (rtsock == -1) |
| 617 | exit(1); |
| 618 | id = netlink_query_family_id(&nlmsg, sock, DEVLINK_FAMILY_NAME, true); |
| 619 | if (id == -1) |
| 620 | goto error; |
| 621 | memset(&genlhdr, 0, sizeof(genlhdr)); |
| 622 | genlhdr.cmd = DEVLINK_CMD_PORT_GET; |
| 623 | netlink_init(&nlmsg, id, NLM_F_DUMP, &genlhdr, sizeof(genlhdr)); |
| 624 | netlink_attr(&nlmsg, DEVLINK_ATTR_BUS_NAME, bus_name, strlen(bus_name) + 1); |
| 625 | netlink_attr(&nlmsg, DEVLINK_ATTR_DEV_NAME, dev_name, strlen(dev_name) + 1); |
| 626 | err = netlink_send_ext(&nlmsg, sock, id, &total_len, true); |
| 627 | if (err < 0) { |
| 628 | goto error; |
| 629 | } |
| 630 | offset = 0; |
| 631 | netdev_index = 0; |
| 632 | while ((len = netlink_next_msg(&nlmsg, offset, total_len)) != -1) { |
| 633 | struct nlattr* attr = (struct nlattr*)(nlmsg.buf + offset + NLMSG_HDRLEN + |
| 634 | NLMSG_ALIGN(sizeof(genlhdr))); |
| 635 | for (; (char*)attr < nlmsg.buf + offset + len; |
| 636 | attr = (struct nlattr*)((char*)attr + NLMSG_ALIGN(attr->nla_len))) { |
| 637 | if (attr->nla_type == DEVLINK_ATTR_NETDEV_NAME) { |
| 638 | char* port_name; |
| 639 | char netdev_name[IFNAMSIZ]; |
| 640 | port_name = (char*)(attr + 1); |
| 641 | snprintf(netdev_name, sizeof(netdev_name), "%s%d", netdev_prefix, |
| 642 | netdev_index); |
| 643 | netlink_device_change(&nlmsg2, rtsock, port_name, true, 0, 0, 0, |
| 644 | netdev_name); |
| 645 | break; |
| 646 | } |
| 647 | } |
| 648 | offset += len; |
| 649 | netdev_index++; |
| 650 | } |
| 651 | error: |
| 652 | close(rtsock); |
| 653 | close(sock); |
| 654 | } |
| 655 | |
| 656 | #define DEV_IPV4 "172.20.20.%d" |
| 657 | #define DEV_IPV6 "fe80::%02x" |
no test coverage detected