Basic Verbs Programming: Show Devices - Code Example

Version 5

    This post shows a simple example that extracts the device names on the server and prints them to the terminal.

    This post is basic and meant for developers.

     

    References

     

    Setup

    1. Make sure that you have access to a Linux server equipped with Mellanox Adapters.

    In this example we will have a server equipped with ConnectX-4 and ConnectX-3 Pro.

     

    2. Make sure to have the latest release of MLNX_OFED driver installed.

     

    Code

    1. Create a directory and add the following files to it device_list.c and makefile.

     

    device_list.c

    #include <stdio.h>

    #include <infiniband/verbs.h>

    #include <infiniband/arch.h>

     

    int main(int argc, char *argv[])

    {

            struct ibv_device **dev_list;

            int num_devices, i;

     

            dev_list = ibv_get_device_list(&num_devices);

            if (!dev_list) {

                    perror("Failed to get IB devices list");

                    return 1;

            }

     

            printf("    %-16s\t\n", "device");

            printf("    %-16s\t\n", "------");

     

            for (i = 0; i < num_devices; ++i)

                    printf("    %-16s\t\n",ibv_get_device_name(dev_list[i]));

     

            ibv_free_device_list(dev_list);

            return 0;

    }

     

     

    makefile

    .PHONY: clean

     

    CFLAGS  := -Wall -Werror -g -O0

    LD      := gcc

    LDFLAGS := ${LDFLAGS} -libverbs

     

    APPS    := ibv_devices

     

    all: ${APPS}

     

    ibv_devices: device_list.o

            ${LD} -o $@ $^ ${LDFLAGS}

     

    clean:

            rm -f *.o ${APPS}

     

    Compilation

    Compile the file:

    # make

    cc -Wall -Werror -g -O0   -c -o device_list.o device_list.c

    gcc -o ibv_devices device_list.o  -libverbs

     

    Verification

    Run the program to get the list of devices on this server.

    # ./ibv_devices

        device

        ------

        mlx5_1

        mlx5_0

        mlx4_0