|
| 1 | +/* |
| 2 | + * Copyright (C) 2014 Michio Honda. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * 1. Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * |
| 13 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 14 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 17 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 18 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 19 | + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 20 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 21 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 22 | + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 23 | + * SUCH DAMAGE. |
| 24 | + */ |
| 25 | + |
| 26 | +#if defined (__FreeBSD__) |
| 27 | +#include <sys/cdefs.h> /* prerequisite */ |
| 28 | +#include <sys/types.h> |
| 29 | +#include <sys/param.h> |
| 30 | +#include <sys/systm.h> |
| 31 | +#include <sys/kernel.h> |
| 32 | +#include <sys/conf.h> /* cdevsw struct */ |
| 33 | +#include <sys/module.h> |
| 34 | +#include <sys/conf.h> |
| 35 | + |
| 36 | +/* to compile netmap_kern.h */ |
| 37 | +#include <sys/malloc.h> |
| 38 | +#include <machine/bus.h> |
| 39 | +#include <sys/socket.h> |
| 40 | +#include <sys/selinfo.h> |
| 41 | +#include <sys/sysctl.h> |
| 42 | +#include <sys/sockio.h> /* XXX _IOWR. Should we use ioccom.h ? */ |
| 43 | +#include <sys/proc.h> |
| 44 | +#include <net/if.h> |
| 45 | +#include <net/if_var.h> /* struct ifnet */ |
| 46 | + |
| 47 | +#define MODULE_GLOBAL(__SYMBOL) V_##__SYMBOL |
| 48 | + |
| 49 | +#elif defined (linux) |
| 50 | +#include <bsd_glue.h> /* from netmap-release */ |
| 51 | + |
| 52 | +#define ETHER_HDR_LEN ETH_HLEN |
| 53 | +#define ETHER_ADDR_LEN 6 |
| 54 | +struct ip { |
| 55 | +#if defined(__LITTLE_ENDIAN_BITFIELD) |
| 56 | + u_char ip_hl:4, /* header length */ |
| 57 | + ip_v:4; /* version */ |
| 58 | +#elif defined (__BIG_ENDIAN_BITFIELD) |
| 59 | + u_char ip_v:4, /* version */ |
| 60 | + ip_hl:4; /* header length */ |
| 61 | +#endif |
| 62 | + u_char ip_tos; /* type of service */ |
| 63 | + u_short ip_len; /* total length */ |
| 64 | + u_short ip_id; /* identification */ |
| 65 | + u_short ip_off; /* fragment offset field */ |
| 66 | +#define IP_RF 0x8000 /* reserved fragment flag */ |
| 67 | +#define IP_DF 0x4000 /* dont fragment flag */ |
| 68 | +#define IP_MF 0x2000 /* more fragments flag */ |
| 69 | +#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */ |
| 70 | + u_char ip_ttl; /* time to live */ |
| 71 | + u_char ip_p; /* protocol */ |
| 72 | + u_short ip_sum; /* checksum */ |
| 73 | + struct in_addr ip_src,ip_dst; /* source and dest address */ |
| 74 | +} __packed __aligned(4); |
| 75 | + |
| 76 | +#define ETHERTYPE_IP 0x0800 /* IP protocol */ |
| 77 | +#endif /* linux */ |
| 78 | + |
| 79 | +/* Common headers */ |
| 80 | +#define WITH_VALE |
| 81 | +#include <net/netmap.h> |
| 82 | +#include <dev/netmap/netmap_kern.h> /* XXX Provide path in Makefile */ |
| 83 | +#include <net/mymodule.h> |
| 84 | + |
| 85 | +#define MY_NAME "vale0:" |
| 86 | + |
| 87 | +u_int my_lookup(struct nm_bdg_fwd *, uint8_t *, struct netmap_vp_adapter *); |
| 88 | + |
| 89 | +uint16_t my_routes[NM_BDG_MAXPORTS]; |
| 90 | + |
| 91 | +u_int |
| 92 | +my_lookup(struct nm_bdg_fwd *ft, uint8_t *hint, |
| 93 | + struct netmap_vp_adapter *vpna) |
| 94 | +{ |
| 95 | + char *buf = ft->ft_buf; |
| 96 | + u_int my_port = vpna->bdg_port; |
| 97 | +#if 0 |
| 98 | + |
| 99 | + /* You can do whatever youw ant on buf */ |
| 100 | + /* You can also specify dst ring index on hint */ |
| 101 | + |
| 102 | + return my_routes[my_port]; |
| 103 | +#endif |
| 104 | + int eth_type = ntohs(*(uint16_t *)(buf + 12)); |
| 105 | + |
| 106 | + if (eth_type == 0x0800) { |
| 107 | + return my_port + 1; |
| 108 | + } |
| 109 | + return NM_BDG_BROADCAST; |
| 110 | +} |
| 111 | + |
| 112 | +static void |
| 113 | +my_dtor(const struct netmap_vp_adapter *vpna) |
| 114 | +{ |
| 115 | + return; |
| 116 | +} |
| 117 | + |
| 118 | +/* |
| 119 | + * CLI backend |
| 120 | + */ |
| 121 | +static int |
| 122 | +my_config(struct nm_ifreq *data) |
| 123 | +{ |
| 124 | + struct mmreq *mreq = (struct mmreq *)data; |
| 125 | + |
| 126 | + if (mreq->mr_sport >= NM_BDG_MAXPORTS) { |
| 127 | + D("invalid sport index %d", mreq->mr_sport); |
| 128 | + return EINVAL; |
| 129 | + } |
| 130 | + else if (mreq->mr_dport > NM_BDG_MAXPORTS) { |
| 131 | + D("invalid dport index %d", mreq->mr_dport); |
| 132 | + return EINVAL; |
| 133 | + } |
| 134 | + my_routes[mreq->mr_sport] = mreq->mr_dport; |
| 135 | + return 0; |
| 136 | +} |
| 137 | + |
| 138 | +static struct netmap_bdg_ops my_ops = {my_lookup, my_config, my_dtor}; |
| 139 | + |
| 140 | +#ifdef linux |
| 141 | +static int mymodule_init(void); |
| 142 | +static void mymodule_fini(void); |
| 143 | + |
| 144 | +static int linux_mymodule_init(void) |
| 145 | +{ |
| 146 | + return -mymodule_init(); |
| 147 | +} |
| 148 | + |
| 149 | +module_init(linux_mymodule_init); |
| 150 | +module_exit(mymodule_fini); |
| 151 | +MODULE_AUTHOR("Michio Honda"); |
| 152 | +MODULE_DESCRIPTION("A simple switching module"); |
| 153 | +MODULE_LICENSE("Dual BSD/GPL"); |
| 154 | +#endif /* Linux */ |
| 155 | + |
| 156 | +static int |
| 157 | +mymodule_init(void) |
| 158 | +{ |
| 159 | + struct nmreq nmr; |
| 160 | + |
| 161 | + bzero(&nmr, sizeof(nmr)); |
| 162 | + nmr.nr_version = NETMAP_API; |
| 163 | + strncpy(nmr.nr_name, MY_NAME, strlen(MY_NAME)); |
| 164 | + nmr.nr_cmd = NETMAP_BDG_REGOPS; |
| 165 | + if (netmap_bdg_ctl(&nmr, &my_ops)) { |
| 166 | + D("create a bridge named %s beforehand using vale-ctl", |
| 167 | + nmr.nr_name); |
| 168 | + return ENOENT; |
| 169 | + } |
| 170 | + bzero(my_routes, sizeof(my_routes)); |
| 171 | + |
| 172 | + //printf("Mymodule: loaded module\n"); |
| 173 | + return 0; |
| 174 | +} |
| 175 | + |
| 176 | +static void |
| 177 | +mymodule_fini(void) |
| 178 | +{ |
| 179 | + struct nmreq nmr; |
| 180 | + int error; |
| 181 | + struct netmap_bdg_ops tmp = {netmap_bdg_learning, NULL, NULL}; |
| 182 | + |
| 183 | + bzero(&nmr, sizeof(nmr)); |
| 184 | + nmr.nr_version = NETMAP_API; |
| 185 | + strncpy(nmr.nr_name, MY_NAME, sizeof(nmr.nr_name)); |
| 186 | + nmr.nr_cmd = NETMAP_BDG_REGOPS; |
| 187 | + error = netmap_bdg_ctl(&nmr, &tmp); |
| 188 | + if (error) |
| 189 | + D("failed to release VALE bridge %d", error); |
| 190 | + //printf("Mymodule: Unloaded module\n"); |
| 191 | +} |
| 192 | + |
| 193 | +#ifdef __FreeBSD__ |
| 194 | +static int |
| 195 | +mymodule_loader(module_t mod, int type, void *data) |
| 196 | +{ |
| 197 | + int error = 0; |
| 198 | + |
| 199 | + switch (type) { |
| 200 | + case MOD_LOAD: |
| 201 | + error = mymodule_init(); |
| 202 | + break; |
| 203 | + case MOD_UNLOAD: |
| 204 | + mymodule_fini(); |
| 205 | + break; |
| 206 | + default: |
| 207 | + error = EINVAL; |
| 208 | + } |
| 209 | + return error; |
| 210 | +} |
| 211 | + |
| 212 | +DEV_MODULE(mymodule, mymodule_loader, NULL); |
| 213 | +#endif /* __FreeBSD__ */ |
0 commit comments