2 * Copyright (c) 2012 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/tools/tools/netrate/pktgen/pktgen.c,v 1.4 2008/04/02 14:18:55 sephe Exp $
39 #include <sys/param.h>
41 #include <sys/device.h>
42 #include <sys/in_cksum.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
48 #include <sys/queue.h>
49 #include <sys/socket.h>
50 #include <sys/systm.h>
51 #include <sys/serialize.h>
54 #include <net/if_dl.h>
55 #include <net/if_var.h>
56 #include <net/ifq_var.h>
57 #include <net/ethernet.h>
58 #include <net/netmsg2.h>
60 #include <netinet/in.h>
61 #include <netinet/ip.h>
62 #include <netinet/udp_var.h>
66 #define CDEV_NAME "pktg"
68 #define PKTGEN_BUFSZ 2048
71 #define PKTGEN_DEVCNT 4
77 struct netmsg_base pb_nmsg; /* MUST BE THE FIRST */
82 struct ifaltq_subque *pb_ifsq;
85 struct pktgen *pb_pktg;
86 LIST_ENTRY(pktgen_buf) pb_link;
90 uint32_t pktg_flags; /* PKTG_F_ */
93 struct callout pktg_stop;
97 struct ifnet *pktg_ifp;
101 struct sockaddr_in pktg_src;
103 struct sockaddr_in *pktg_dst;
104 uint8_t pktg_dst_lladdr[ETHER_ADDR_LEN];
106 LIST_HEAD(, pktgen_buf) pktg_buflist;
109 #define PKTG_F_CONFIG 0x1
110 #define PKTG_F_RUNNING 0x4
112 static int pktgen_modevent(module_t, int, void *);
114 static void pktgen_buf_free(void *);
115 static void pktgen_buf_ref(void *);
116 static void pktgen_buf_send(netmsg_t);
118 static int pktgen_config(struct pktgen *,
119 const struct pktgen_conf *);
120 static int pktgen_start(struct pktgen *);
121 static void pktgen_free(struct pktgen *);
122 static void pktgen_stop_cb(void *);
123 static void pktgen_mbuf(struct pktgen_buf *, struct mbuf *);
125 static d_open_t pktgen_open;
126 static d_close_t pktgen_close;
127 static d_ioctl_t pktgen_ioctl;
129 static struct dev_ops pktgen_ops = {
130 { CDEV_NAME, 0, D_MPSAFE },
131 .d_open = pktgen_open,
132 .d_close = pktgen_close,
133 .d_ioctl = pktgen_ioctl,
136 static volatile int pktgen_refcnt;
137 static struct lwkt_token pktgen_tok = LWKT_TOKEN_INITIALIZER(pktgen_token);
139 MALLOC_DECLARE(M_PKTGEN);
140 MALLOC_DEFINE(M_PKTGEN, CDEV_NAME, "Packet generator");
142 DEV_MODULE(pktgen, pktgen_modevent, NULL);
145 pktgen_modevent(module_t mod, int type, void *data)
151 for (i = 0; i < PKTGEN_DEVCNT; ++i) {
152 make_dev(&pktgen_ops, 0, UID_ROOT, GID_WHEEL, 0600,
158 if (pktgen_refcnt > 0)
160 dev_ops_remove_all(&pktgen_ops);
171 pktgen_open(struct dev_open_args *ap)
173 cdev_t dev = ap->a_head.a_dev;
177 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
181 lwkt_gettoken(&pktgen_tok);
183 if (dev->si_drv1 != NULL) {
184 lwkt_reltoken(&pktgen_tok);
188 pktg = kmalloc(sizeof(*pktg), M_PKTGEN, M_ZERO | M_WAITOK);
189 callout_init_mp(&pktg->pktg_stop);
190 LIST_INIT(&pktg->pktg_buflist);
193 pktg->pktg_refcnt = 1;
195 atomic_add_int(&pktgen_refcnt, 1);
197 lwkt_reltoken(&pktgen_tok);
202 pktgen_close(struct dev_close_args *ap)
204 cdev_t dev = ap->a_head.a_dev;
205 struct pktgen *pktg = dev->si_drv1;
207 lwkt_gettoken(&pktgen_tok);
209 lwkt_reltoken(&pktgen_tok);
217 pktgen_ioctl(struct dev_ioctl_args *ap __unused)
219 cdev_t dev = ap->a_head.a_dev;
220 caddr_t data = ap->a_data;
221 struct pktgen *pktg = dev->si_drv1;
224 lwkt_gettoken(&pktgen_tok);
228 error = pktgen_start(pktg);
232 error = pktgen_config(pktg, (const struct pktgen_conf *)data);
240 lwkt_reltoken(&pktgen_tok);
245 pktgen_config(struct pktgen *pktg, const struct pktgen_conf *conf)
247 const struct sockaddr_in *sin;
248 struct sockaddr_in *dst = NULL;
249 const struct sockaddr *sa;
252 int i, error, pktenq;
254 if (pktg->pktg_flags & (PKTG_F_RUNNING | PKTG_F_CONFIG))
257 if (conf->pc_datalen <= 0 ||
258 conf->pc_datalen > ETHERMTU - sizeof(struct udpiphdr))
260 if (conf->pc_duration <= 0)
264 if (sin->sin_family != AF_INET)
265 return EPROTONOSUPPORT;
266 if (sin->sin_port == 0)
269 if (conf->pc_ndst <= 0)
271 dst_size = conf->pc_ndst * sizeof(struct sockaddr_in);
273 dst = kmalloc(dst_size, M_PKTGEN, M_WAITOK | M_NULLOK);
277 error = copyin(conf->pc_dst, dst, dst_size);
281 for (i = 0; i < conf->pc_ndst; ++i) {
283 if (sin->sin_family != AF_INET) {
284 error = EPROTONOSUPPORT;
287 if (sin->sin_port == 0) {
293 ifp = ifunit(conf->pc_ifname);
299 pktenq = conf->pc_pktenq;
300 if (pktenq < 0 || pktenq > ifp->if_snd.altq_maxlen) {
303 } else if (pktenq == 0) {
304 pktenq = (ifp->if_snd.altq_maxlen * 3) / 4;
307 sa = &conf->pc_dst_lladdr;
308 if (sa->sa_family != AF_LINK) {
309 error = EPROTONOSUPPORT;
312 if (sa->sa_len != ETHER_ADDR_LEN) {
313 error = EPROTONOSUPPORT;
316 if (ETHER_IS_MULTICAST(sa->sa_data) ||
317 bcmp(sa->sa_data, ifp->if_broadcastaddr, ifp->if_addrlen) == 0) {
318 error = EADDRNOTAVAIL;
325 pktg->pktg_flags |= PKTG_F_CONFIG;
327 pktg->pktg_duration = conf->pc_duration;
328 pktg->pktg_datalen = conf->pc_datalen;
329 pktg->pktg_pktenq = pktenq;
330 pktg->pktg_ifp = ifp;
331 pktg->pktg_src = conf->pc_src;
332 pktg->pktg_ndst = conf->pc_ndst;
333 KKASSERT(pktg->pktg_dst == NULL);
334 pktg->pktg_dst = dst;
335 bcopy(sa->sa_data, pktg->pktg_dst_lladdr, ETHER_ADDR_LEN);
341 kfree(dst, M_PKTGEN);
346 pktgen_start(struct pktgen *pktg)
348 struct mbuf *m, *head = NULL, **next;
350 struct ifaltq_subque *ifsq;
351 int cpuid, orig_cpuid, i, alloc_cnt, keep_cnt;
356 if ((pktg->pktg_flags & PKTG_F_CONFIG) == 0)
358 if (pktg->pktg_flags & PKTG_F_RUNNING)
360 pktg->pktg_flags |= PKTG_F_RUNNING;
362 ifp = pktg->pktg_ifp;
363 ifsq = ifq_get_subq_default(&ifp->if_snd);
365 orig_cpuid = mycpuid;
366 cpuid = ifsq_get_cpuid(ifsq);
367 if (cpuid != orig_cpuid)
368 lwkt_migratecpu(cpuid);
370 keep_cnt = pktg->pktg_pktenq;
371 alloc_cnt = keep_cnt * 2;
374 * Prefault enough mbuf into mbuf objcache
377 for (i = 0; i < alloc_cnt; ++i) {
378 MGETHDR(m, MB_WAIT, MT_DATA);
380 next = &m->m_nextpkt;
383 for (i = 0; i < alloc_cnt - keep_cnt; ++i) {
389 KKASSERT(head != NULL);
392 * Setup the packets' data
394 ip_len = pktg->pktg_datalen + sizeof(struct udpiphdr);
395 len = ip_len + ETHER_HDR_LEN;
397 psum = htons((u_short)pktg->pktg_datalen + sizeof(struct udphdr) +
399 ulen = htons(pktg->pktg_datalen + sizeof(struct udphdr));
405 const struct sockaddr_in *dst;
406 struct pktgen_buf *pb;
409 struct ether_header *eh;
411 atomic_add_int(&pktg->pktg_refcnt, 1);
412 atomic_add_int(&pktgen_refcnt, 1);
414 pb = kmalloc(sizeof(*pb), M_PKTGEN, M_WAITOK | M_ZERO);
418 pb->pb_buf = kmalloc(PKTGEN_BUFSZ, M_PKTGEN, M_WAITOK);
420 pb->pb_cpuid = cpuid;
422 netmsg_init(&pb->pb_nmsg, NULL, &netisr_adone_rport, 0,
424 LIST_INSERT_HEAD(&pktg->pktg_buflist, pb, pb_link);
426 dst = &pktg->pktg_dst[i % pktg->pktg_ndst];
429 m->m_ext.ext_arg = pb;
430 m->m_ext.ext_buf = pb->pb_buf;
431 m->m_ext.ext_free = pktgen_buf_free;
432 m->m_ext.ext_ref = pktgen_buf_ref;
433 m->m_ext.ext_size = PKTGEN_BUFSZ;
435 m->m_data = m->m_ext.ext_buf;
437 m->m_len = m->m_pkthdr.len = len;
439 m->m_data += ETHER_HDR_LEN;
440 m->m_len -= ETHER_HDR_LEN;
441 m->m_pkthdr.len -= ETHER_HDR_LEN;
443 ui = mtod(m, struct udpiphdr *);
444 ui->ui_pr = IPPROTO_UDP;
445 ui->ui_src.s_addr = pktg->pktg_src.sin_addr.s_addr;
446 ui->ui_dst.s_addr = dst->sin_addr.s_addr;
447 ui->ui_sport = pktg->pktg_src.sin_port;
448 ui->ui_dport = dst->sin_port;
450 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
452 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
454 ip = (struct ip *)ui;
456 ip->ip_ttl = 64; /* XXX */
457 ip->ip_tos = 0; /* XXX */
458 ip->ip_vhl = IP_VHL_BORING;
461 ip->ip_id = ip_newid();
465 ip->ip_len = htons(ip->ip_len);
466 ip->ip_sum = in_cksum_hdr(ip);
468 m->m_data -= ETHER_HDR_LEN;
469 m->m_len += ETHER_HDR_LEN;
470 m->m_pkthdr.len += ETHER_HDR_LEN;
472 eh = mtod(m, struct ether_header *);
473 bcopy(pktg->pktg_dst_lladdr, eh->ether_dhost, ETHER_ADDR_LEN);
474 bcopy(IF_LLADDR(ifp), eh->ether_shost, ETHER_ADDR_LEN);
475 eh->ether_type = htons(ETHERTYPE_IP);
477 nextm = m->m_nextpkt;
480 ifq_dispatch(ifp, m, NULL);
485 callout_reset(&pktg->pktg_stop, pktg->pktg_duration * hz,
486 pktgen_stop_cb, pktg);
488 if (cpuid != orig_cpuid)
489 lwkt_migratecpu(orig_cpuid);
495 pktgen_mbuf(struct pktgen_buf *pb, struct mbuf *m)
497 m->m_ext.ext_arg = pb;
498 m->m_ext.ext_buf = pb->pb_buf;
499 m->m_ext.ext_free = pktgen_buf_free;
500 m->m_ext.ext_ref = pktgen_buf_ref;
501 m->m_ext.ext_size = PKTGEN_BUFSZ;
503 m->m_data = m->m_ext.ext_buf;
505 m->m_len = m->m_pkthdr.len = pb->pb_len;
509 pktgen_buf_send(netmsg_t msg)
511 struct pktgen_buf *pb = (struct pktgen_buf *)msg;
514 KKASSERT(&curthread->td_msgport == netisr_portfn(pb->pb_cpuid));
517 lwkt_replymsg(&pb->pb_nmsg.lmsg, 0);
520 MGETHDR(m, MB_WAIT, MT_DATA);
522 ifq_dispatch(pb->pb_ifp, m, NULL);
526 pktgen_buf_free(void *arg)
528 struct pktgen_buf *pb = arg;
531 KKASSERT(pb->pb_inuse > 0);
533 if (atomic_fetchadd_int(&pb->pb_inuse, -1) == 1) {
538 LIST_REMOVE(pb, pb_link);
540 kfree(pb->pb_buf, M_PKTGEN);
548 if (&curthread->td_msgport != netisr_portfn(pb->pb_cpuid)) {
549 KKASSERT(pb->pb_cpuid == mycpuid);
551 KKASSERT(pb->pb_nmsg.lmsg.ms_flags & MSGF_DONE);
552 lwkt_sendmsg(netisr_portfn(pb->pb_cpuid), &pb->pb_nmsg.lmsg);
557 MGETHDR(m, MB_WAIT, MT_DATA);
559 ifsq_enqueue(pb->pb_ifsq, m, NULL);
563 pktgen_buf_ref(void *arg)
565 struct pktgen_buf *pb = arg;
567 panic("%s should never be called\n", __func__);
569 KKASSERT(pb->pb_inuse > 0);
570 atomic_add_int(&pb->pb_inuse, 1);
574 pktgen_free(struct pktgen *pktg)
576 KKASSERT(pktg->pktg_refcnt > 0);
577 if (atomic_fetchadd_int(&pktg->pktg_refcnt, -1) == 1) {
578 if (pktg->pktg_dst != NULL)
579 kfree(pktg->pktg_dst, M_PKTGEN);
580 KKASSERT(LIST_EMPTY(&pktg->pktg_buflist));
581 kfree(pktg, M_PKTGEN);
584 KKASSERT(pktgen_refcnt > 0);
585 atomic_subtract_int(&pktgen_refcnt, 1);
589 pktgen_stop_cb(void *arg)
591 struct pktgen *pktg = arg;
592 struct pktgen_buf *pb;
595 LIST_FOREACH(pb, &pktg->pktg_buflist, pb_link)