Re: macvlan_config on linux 2.6.16 build problem.

Ben Greear <[email protected]>
Newsgroups gmane.linux.drivers.vlan
Organization Candela Technologies
Message-ID <[email protected]>
Dawid Ciezarkiewicz wrote:
> I'm trying to compile vconfig (vlan.1.9.tar.gz) for 2.6.16 linux kernel.

Attached is my consolidated patch which includes MAC-VLANs for 2.6.16.

I did change the ioctl API a bit to work on 64-bit systems better.

I'm also attaching my macvlan_config.c file.

Thanks,
Ben

-- 
Ben Greear <[email protected]>
Candela Technologies Inc  http://www.candelatech.com

_______________________________________________
Vlan mailing list
[email protected]
http://www.lanforge.com/mailman/listinfo/vlan
candela_2.6.16.patch (text/x-patch, 330.2 KB) - not displayed
macvlan_config.c (text/x-csrc, 12.4 KB)
/*
#######################################################################
#
# (C) Copyright 2001
# Alex Zeffertt, Cambridge Broadband Ltd, [email protected]
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#######################################################################
# Notes:
# 
# This configuration utility communicates with macvlan.o, the MAC address
# based VLAN support module.
#
# It uses an IOCTL interface which allows you to
#
# 1. enable/disable MAC address based VLANS over an ether type net_device
# 2. add/remove a MAC address based VLAN - which is an ether type net_device
#    layered over the original MACVLAN enabled ether type net_device.
# 3. bind/unbind MAC addresses to/from particular MAC address based VLANs
# 4. discover the state of MAC address based VLANs on the system.
# 5. set/get port flags, including whether to bind to destination MAC
#    or source mac.
# 6. Traffic to/from eth0 will not be affected.


#
# Example: (Assuming you are using source binding)
#
# If you enable MAC address based VLANS over eth0
#
# You may then create further VLANs, e.g. eth0#1 eth0#2 ....
# These will not receive any frames until you bind MAC addresses to them.
# If you bind 11:22:33:44:55:66 to eth0#1, then any frames received by
# eth0 with source MAC 11:22:33:44:55:66 will be routed up through eth0#1
# instead of eth0.
#
# Example: (Assuming you are using destination (local) binding)
#
# If you enable MAC address based VLANS over eth0
#
# You may then create further VLANs, e.g. eth0#1 eth0#2 ....
# These will not receive any frames until you bind MAC addresses to them.
# If you bind 11:22:33:44:55:66 to eth0#1, then any broadcast/multicast
# frames, or frames with a destination MAC 11:22:33:44:55:66
# will be routed up through eth0#1 instead of eth0
#
# For broadcasts, the packet will be duplicated for every VLAN
# with at least one MAC attached.  Attaching more than one MAC
# when destination binding makes no sense...don't do it!
#
#
# 
#######################################################################
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_macvlan.h>
#include <linux/sockios.h>
#include <string.h>
#include <errno.h>

#define MVL_VERSION "2.0"


int _do_help(int argc, char *argv[]);
int do_enable(int argc, char *argv[]);
int do_disable(int argc, char *argv[]);
int do_add(int argc, char *argv[]);
int do_del(int argc, char *argv[]);
int do_bind(int argc, char *argv[]);
int do_unbind(int argc, char *argv[]);
int do_info(int argc, char *argv[]);
int do_setflags(int argc, char *argv[]);
int do_unload(int argc, char* argv[]);

struct command {
    char *name;
    char *short_help;
    int (*fn)(int argc, char *argv[]);
    char *long_help;
} command_list[] = {
    {"help",   "help on other commands", _do_help, "help <command>"},
    {"enable", "enables mac based vlans over an ethernet device", do_enable,
         "enable <ifname>\n"
         " - enables mac based vlans over \"ifname\"\n"
         " - also creates a default vlan over \"ifname\" called \"ifname#0\""
    },
    {"disable", "disables mac based vlans over an ethernet device", do_disable, "disable <ifname>"},
    {"add", "creates new mac based vlan",  do_add,
         "add <ifname> <index>\n"
         " - creates a new mac based vlan called \"ifname#index\" layered over \"ifname\"\n"
         " - mac based vlans over \"ifname\" must first be enabled with \"enable\"\n"
         " - \"ifname#index\" will have a random MAC address by default.  Change with 'ifconfig' or 'ip'.\n"
    },
    {"del", "destroys a mac based vlan",  do_del,
         "del <ifname>\n"
         " - deletes a mac base vlan called \"ifname\""
    },
    {"unload", "Unconfigure all of the macvlan devices",
     do_unload, "Unconfigure all of the macvlan devices so module can be unloaded"},
    {"setflags", "Set port flags on a port",
     do_setflags,
         "setflags <ifname> <new_flags>\n"
         "0x01   Bind to Destination instead of source MAC"
    },
    {"info", "print state of mac based vlans",  do_info, "info"},
};
#define NCOMMANDS (sizeof(command_list)/sizeof(struct command))


int parseInt(const char* s) {
   return strtol(s, NULL, 0); //should parse HEX, Octal, and Decimal.  If not decimal, must start with 0x
}


int do_help(char *cmd) {
   int i;
   printf("version: %s\n", MVL_VERSION);
   printf("sizeof(macvlan_ioctl): %d\n", (int)(sizeof(struct macvlan_ioctl)));

   if (cmd) {
      for (i = 0; i < NCOMMANDS; i++) {
         if (!strcmp(command_list[i].name, cmd))
            break;
      }
      if (i == NCOMMANDS)
         return -1;
      puts(command_list[i].long_help);
   }
   else {
      printf("\nsubcommands:\n\n");
      for (i = 0; i < NCOMMANDS; i++) {
         printf("%s:\t%s\n", command_list[i].name,
                command_list[i].short_help);
      }
   }
   return 0;
}

int _do_help(int argc, char *argv[]) {
   if (argc < 2) {
      return do_help(NULL);
   }
   else {
      return do_help(argv[1]);
   }
}

int do_enable(int argc, char *argv[])
{
    struct macvlan_ioctl req;
    int s;

    if (argc < 2) {
        printf("usage: %s <ifname>\n", argv[0]);
        return 1;
    }

    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        return 1;
    }

    req.cmd = MACVLAN_ENABLE;
    /* 
     * name of ethernet device over which we 
     * are enabling mac based vlans
     */
    strncpy(req.ifname, argv[1], sizeof(req.ifname));

    if (ioctl(s, SIOCGIFMACVLAN, &req) < 0) {
       if (errno != EEXIST) {
          perror("ioctl (SIOCGIFMACVLAN, MACVLAN_ENABLE)");
          printf("errno: %i\n", errno);
          return 1;
       }
       else {
          return 0;
       }
    }
    return 0;
}


int do_setflags(int argc, char *argv[])
{
    struct macvlan_ioctl req;
    int s;

    if (argc < 3) {
        printf("usage: %s <ifname> <flags>\n", argv[0]);
        return 1;
    }

    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        return 1;
    }

    req.cmd = MACVLAN_SET_PORT_FLAGS;
    /* name of macvlan device. */
    strncpy(req.ifname, argv[1], sizeof(req.ifname));
    
    req.ifidx = parseInt(argv[2]);

    if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) {
        perror("ioctl (SIOCGIFMACVLAN, SET_PORT_FLAGS)");
        return 1;
    }
    return 0;
}

int _do_disable(char* port, int s) {
    struct macvlan_ioctl req;
       
    req.cmd = MACVLAN_DISABLE;

    /* name of macvlan device. */
    strncpy(req.ifname, port, sizeof(req.ifname));

    if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) {
        perror("disable-port");
        return -1;
    }
    else {
       printf("Disabled port: %s\n", port);
    }
    return 0;
}

int do_disable(int argc, char *argv[]) {
    int s;

    if (argc < 2) {
        printf("usage: %s <ifname>\n", argv[0]);
        return 1;
    }

    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        return 1;
    }

    return _do_disable(argv[1], s);
}

int do_add(int argc, char *argv[]) {
    int s;
    struct macvlan_ioctl req;

    if (argc < 3) {
        printf("usage: %s <ifname> <index>\n", argv[0]);
        return 1;
    }

    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        return 1;
    }

    req.cmd = MACVLAN_ADD;
    /* name of lower layer i/f over which we are adding an upper layer i/f */
    strncpy(req.ifname, argv[1], sizeof(req.ifname));
    req.ifidx = parseInt(argv[2]);

    if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) {
        perror("ioctl (SIOCGIFMACVLAN, MACVLAN_ADD)");
        return 1;
    }
    return 0;
}

int _do_del(char* ifname, int s) {
    struct macvlan_ioctl req;
   
    req.cmd = MACVLAN_DEL;
    /* name mac based vlan to destroy */
    strncpy(req.ifname, ifname, sizeof(req.ifname));
    
    if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) {
        printf("failed to delete interface: %s\n", ifname);
        perror("ioctl (SIOCGIFMACVLAN, MACVLAN_DEL)");
        return -1;
    }
    else {
       printf("Deleted interface: %s\n", ifname);
    }
    
    return 0;
}

int do_del(int argc, char *argv[]) {
    int s;

    if (argc < 2) {
        printf("usage: %s <ifname>\n", argv[0]);
        return 1;
    }

    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        return 1;
    }

    return _do_del(argv[1], s);
}

    

int get_num_ports(int s) {
   struct macvlan_ioctl req;
   
   req.cmd = MACVLAN_GET_NUM_PORTS;
   if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) {
      perror("ioctl (SIOCGIFMACVLAN, GET_NUM_PORTS)");
      return -1;
   }

   printf("Found: %i ports\n", req.reply.num);

   return req.reply.num;
}

int get_num_vlans(int portidx, int s) {
    struct macvlan_ioctl req;
   
    /* Get the number of mac-based-vlans layered
     * over this ethernet device 
     */
    req.cmd = MACVLAN_GET_NUM_VLANS;
    req.portidx = portidx;
    if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) {
       perror("ioctl (GET_NUM_VLANS)");
       return -1;
    }
    printf("Found: %i vlans for port: %i\n", req.reply.num, portidx);
    return req.reply.num;
}


int htoi(char *s) {
    char ch;
    int i = 0;
    while ((ch = *s++)) {
        i <<= 4;
        i += (ch>='0'&&ch<='9')?(ch-'0'):((ch>='a'&&ch<='f')?(ch-'a'+10):((ch>='A'&&ch<='F')?(ch-'A'+10):0));
    }
    return i;
}

int do_info(int argc, char *argv[]) {
    int s;
    struct macvlan_ioctl req;
    int nports;
    int portidx;
    int nifs;
    int ifidx;

    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        return 1;
    }
    /* get the number of ethernet devices which have mac based vlans
     * enabled over them
     */
    req.cmd = MACVLAN_GET_NUM_PORTS;
    if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) {
        perror("ioctl (GET_NUM_PORTS)");
        return 1;
    }
    nports = req.reply.num;
    for (portidx = 0; portidx < nports; portidx++) {
        char tmpifname[64];
        /* Get the name of this mac-based-vlan enabled 
         * ethernet device
         */
        req.cmd = MACVLAN_GET_PORT_NAME;
        req.portidx = portidx;
        if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) {
            perror("ioctl (GET_PORT_NAME)");
            return 1;
        }
        printf("-%s\n", req.reply.name);

        /* get the port flags */
        req.cmd = MACVLAN_GET_PORT_FLAGS;
        req.portidx = portidx;
        strcpy(tmpifname, req.reply.name);
        strncpy(req.ifname, tmpifname, sizeof(req.ifname));
        if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) {
            perror("ioctl (GET_PORT_FLAGS)");
            return 1;
        }
        printf("-%s flag: 0x%x\n", tmpifname, req.reply.num);

        /* Get the number of mac-based-vlans layered
         * over this ethernet device 
         */
        req.cmd = MACVLAN_GET_NUM_VLANS;
        req.portidx = portidx;
        if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) {
            perror("ioctl (GET_NUM_VLANS)");
            return 1;
        }
        nifs = req.reply.num;
        for (ifidx = 0; ifidx < nifs; ifidx++) {
            /* Get the name of this vlan */
            req.cmd = MACVLAN_GET_VLAN_NAME;
            req.portidx = portidx;
            req.ifidx = ifidx;
            if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) {
                perror("ioctl (GET_VLAN_NAME)");
                return 1;
            }
            /* print the name */
            printf(" |-%s\n", req.reply.name);
        }
    }
    return 0;
}


int do_unload(int argc, char *argv[]) {

   // Just unload the module
   return system("rmmod macvlan");
}

int main(int argc, char *argv[]) {
    unsigned int cmd;
    int err;
    
    if (argc < 2)
        goto usage;

    for (cmd = 0; cmd < NCOMMANDS; cmd++) {
        if (!strcmp(command_list[cmd].name,argv[1]))
            break;
    }
    if (cmd == NCOMMANDS)
        goto usage;

    if ((err = command_list[cmd].fn(argc-1,argv+1)))
        goto usage;
    return 0;

 usage:
    do_help(NULL);
    return err;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.