iproute2/testsuite/tools/generate_nlmsg.c
<<
>>
Prefs
   1/*
   2 * generate_nlmsg.c     Testsuite helper generating nlmsg blob
   3 *
   4 *              This program is free software; you can redistribute it and/or
   5 *              modify it under the terms of the GNU General Public License
   6 *              as published by the Free Software Foundation; either version
   7 *              2 of the License, or (at your option) any later version.
   8 *
   9 * Authors:     Phil Sutter <phil@nwl.cc>
  10 */
  11
  12#include <netinet/ether.h>
  13#include <libnetlink.h>
  14#include <sys/socket.h>
  15#include <linux/if.h>
  16#include <errno.h>
  17#include <stdio.h>
  18
  19int fill_vf_rate_test(void *buf, size_t buflen)
  20{
  21        char bcmac[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  22        struct ifla_vf_mac vf_mac = {
  23                .mac = { 0x0, 0x26, 0x6c, 0xff, 0xb5, 0xc0 },
  24        };
  25        struct ifla_vf_link_state vf_link_state = { 0 };
  26        struct ifla_vf_tx_rate vf_tx_rate = { 0 };
  27        struct ifla_vf_spoofchk vf_spoofchk = {
  28                .setting = 1,
  29        };
  30        struct ifla_vf_vlan vf_vlan = { 0 };
  31        struct rtattr *vfinfo_list, *vfinfo;
  32        struct nlmsghdr *h = buf;
  33        struct ifinfomsg *ifi;
  34
  35        h->nlmsg_type = RTM_NEWLINK;
  36        h->nlmsg_len = NLMSG_LENGTH(sizeof(*ifi));
  37
  38        ifi = NLMSG_DATA(h);
  39        ifi->ifi_type = ARPHRD_ETHER;
  40        ifi->ifi_index = 1;
  41        ifi->ifi_flags = IFF_RUNNING | IFF_BROADCAST |
  42                         IFF_MULTICAST | IFF_UP | IFF_LOWER_UP;
  43
  44#define ASSERT(x) if (x < 0) return -1
  45#define ATTR_L(t, v, l) ASSERT(addattr_l(h, buflen, t, v, l))
  46#define ATTR_8(t, v)    ASSERT(addattr8(h, buflen, t, v))
  47#define ATTR_32(t, v)   ASSERT(addattr32(h, buflen, t, v))
  48#define ATTR_STRZ(t, v) ASSERT(addattrstrz(h, buflen, t, v))
  49
  50#define NEST(t) addattr_nest(h, buflen, t)
  51#define NEST_END(t) addattr_nest_end(h, t)
  52
  53        ATTR_STRZ(IFLA_IFNAME, "eth0");
  54        ATTR_32(IFLA_TXQLEN, 10000);
  55        ATTR_8(IFLA_OPERSTATE, 6);
  56        ATTR_8(IFLA_LINKMODE, 0);
  57        ATTR_32(IFLA_MTU, 9000);
  58        ATTR_32(IFLA_GROUP, 0);
  59        ATTR_32(IFLA_PROMISCUITY, 0);
  60        ATTR_32(IFLA_NUM_TX_QUEUES, 8);
  61        ATTR_32(IFLA_NUM_RX_QUEUES, 8);
  62        ATTR_8(IFLA_CARRIER, 1);
  63        ATTR_STRZ(IFLA_QDISC, "mq");
  64        ATTR_L(IFLA_ADDRESS, vf_mac.mac, ETH_ALEN);
  65        ATTR_L(IFLA_BROADCAST, bcmac, sizeof(bcmac));
  66        ATTR_32(IFLA_NUM_VF, 2);
  67
  68        vfinfo_list = NEST(IFLA_VFINFO_LIST);
  69
  70        vfinfo = NEST(IFLA_VF_INFO);
  71        ATTR_L(IFLA_VF_MAC, &vf_mac, sizeof(vf_mac));
  72        ATTR_L(IFLA_VF_VLAN, &vf_vlan, sizeof(vf_vlan));
  73        ATTR_L(IFLA_VF_TX_RATE, &vf_tx_rate, sizeof(vf_tx_rate));
  74        ATTR_L(IFLA_VF_SPOOFCHK, &vf_spoofchk, sizeof(vf_spoofchk));
  75        ATTR_L(IFLA_VF_LINK_STATE, &vf_link_state, sizeof(vf_link_state));
  76        NEST_END(vfinfo);
  77
  78        vf_mac.vf = vf_vlan.vf = vf_tx_rate.vf = 1;
  79        vf_spoofchk.vf = vf_link_state.vf = 1;
  80
  81        vfinfo = NEST(IFLA_VF_INFO);
  82        ATTR_L(IFLA_VF_MAC, &vf_mac, sizeof(vf_mac));
  83        ATTR_L(IFLA_VF_VLAN, &vf_vlan, sizeof(vf_vlan));
  84        ATTR_L(IFLA_VF_TX_RATE, &vf_tx_rate, sizeof(vf_tx_rate));
  85        ATTR_L(IFLA_VF_SPOOFCHK, &vf_spoofchk, sizeof(vf_spoofchk));
  86        ATTR_L(IFLA_VF_LINK_STATE, &vf_link_state, sizeof(vf_link_state));
  87        NEST_END(vfinfo);
  88
  89        NEST_END(vfinfo_list);
  90
  91        return h->nlmsg_len;
  92}
  93
  94int main(void)
  95{
  96        char buf[16384] = { 0 };
  97        int msglen;
  98        FILE *fp;
  99
 100        msglen = fill_vf_rate_test(buf, sizeof(buf));
 101        if (msglen < 0) {
 102                fprintf(stderr, "fill_vf_rate_test() failed!\n");
 103                return 1;
 104        }
 105        fp = fopen("tests/ip/link/dev_wo_vf_rate.nl", "w");
 106        if (!fp) {
 107                perror("fopen()");
 108                return 1;
 109        }
 110        if (fwrite(buf, msglen, 1, fp) != 1) {
 111                perror("fwrite()");
 112                return 1;
 113        }
 114        fclose(fp);
 115        return 0;
 116}
 117