Skip to content

Commit 54acd25

Browse files
VALE example
1 parent 83bff2c commit 54acd25

File tree

6 files changed

+346
-0
lines changed

6 files changed

+346
-0
lines changed

mymodule/LINUX/Makefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CONFIG_MYMODULE:=m
2+
mymodule_lin-objs := mymodule.o
3+
obj-$(CONFIG_MYMODULE) = mymodule_lin.o
4+
5+
M:=$(CURDIR)
6+
SRC ?= $(KSRC)
7+
EXTRA_CFLAGS := -I$(NSRC)/ -I$(NSRC)/LINUX -I$(NSRC)/sys -I$(M)/../sys -DCONFIG_NETMAP
8+
9+
all: build
10+
build:
11+
make -C $(SRC) M=$(CURDIR) \
12+
CONFIG_NETMAP=m CONFIG_NETMAP_VALE=y CONFIG_MYMODULE=m \
13+
EXTRA_CFLAGS='$(EXTRA_CFLAGS)' KBUILD_EXTRA_SYMBOLS=$(NSRC)/Module.symvers
14+
ls -l `find . -name \*.ko`
15+
clean:
16+
(rm -rf *.o *.ko modules.order mymodule_lin.mod.c Module.symvers)
17+
18+
$(obj)/mymodule.o: $(M)/../sys/contrib/mymodule/mymodule.c
19+
$(call cmd,cc_o_c)
20+
$(call cmd,modversions)

mymodule/Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
PROGS = mmctl
2+
CLEANFILES = $(PROGS)*.o
3+
CFLAGS = -O2 -pipe
4+
CFLAGS += -Werror -Wall -Wextra
5+
NMSRC := ../netmap
6+
CFLAGS += -I sys -I$(NMSRC)/sys
7+
8+
all: $(PROGS)
9+
mmctl: mmctl.c
10+
$(CC) $(CFLAGS) -o mmctl mmctl.c
11+
clean:
12+
-@rm -rf $(CLEANFILES)

mymodule/mmctl.c

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* BSD LICENSE
3+
*
4+
* Copyright(c) 2015 NEC Europe Ltd. All rights reserved.
5+
* All rights reserved.
6+
* Author: Michio Honda
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
*
12+
* * Redistributions of source code must retain the above copyright
13+
* notice, this list of conditions and the following disclaimer.
14+
* * Redistributions in binary form must reproduce the above copyright
15+
* notice, this list of conditions and the following disclaimer in
16+
* the documentation and/or other materials provided with the
17+
* distribution.
18+
* * Neither the name of NEC Europe Ltd. nor the names of
19+
* its contributors may be used to endorse or promote products derived
20+
* from this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33+
*/
34+
35+
#include <sys/types.h>
36+
#include <sys/mman.h>
37+
#include <sys/ioctl.h>
38+
#include <sys/socket.h>
39+
#include <net/ethernet.h>
40+
#include <netinet/in.h>
41+
#include <netinet/ip.h>
42+
#include <netinet/tcp.h>
43+
#include <arpa/inet.h>
44+
#include <net/if.h>
45+
#include <net/netmap.h>
46+
#define NETMAP_WITH_LIBS
47+
#include <net/netmap_user.h>
48+
#include <net/mymodule.h>
49+
#include <unistd.h>
50+
#include <fcntl.h>
51+
#include <stdlib.h>
52+
#include <stdio.h>
53+
#include <string.h>
54+
#include <time.h>
55+
56+
int
57+
main(int argc, char **argv)
58+
{
59+
struct nm_desc *nmd;
60+
struct mmreq mreq;
61+
char name[16];
62+
63+
if (argc != 3) {
64+
fprintf(stdout,
65+
"usage: mmctl srcport dstport\n");
66+
return 0;
67+
}
68+
69+
snprintf(name, sizeof(name), "%svi0", MYMODULE_BDG_NAME);
70+
bzero(&mreq, sizeof(mreq));
71+
strncpy(mreq.mr_name, name, strlen(name));
72+
mreq.mr_sport = atoi(argv[1]);
73+
mreq.mr_dport = atoi(argv[2]);
74+
nmd = nm_open(name, NULL, 0, NULL);
75+
if (nmd == NULL) {
76+
D("Unable to open %s", name);
77+
return -1;
78+
}
79+
80+
if (ioctl(nmd->fd, NIOCCONFIG, &mreq)) {
81+
perror("ioctl");
82+
}
83+
return 0;
84+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.PATH: .
2+
KMOD= mymodule
3+
SRCS= mymodule.c
4+
TARGET!= uname -m
5+
IDENT!= uname -i
6+
CFLAGS+= -I$(KSRC)
7+
CFLAGS+= -I../../../sys/
8+
CFLAGS+= -I$(KSRC)/sys/$(TARGET)/compile/$(IDENT)
9+
10+
.include <bsd.kmod.mk>
+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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__ */

mymodule/sys/net/mymodule.h

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#define MYMODULE_BDG_NAME "vale0:"
2+
struct mmreq {
3+
char mr_name[IFNAMSIZ];
4+
uint16_t mr_sport;
5+
uint16_t mr_dport;
6+
int mr_cmd;
7+
};

0 commit comments

Comments
 (0)