source: rtems-libbsd/testsuite/zerocopy01/test_main.c @ 5c0aa97

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 5c0aa97 was 5c0aa97, checked in by Sebastian Huber <sebastian.huber@…>, on 02/12/15 at 12:34:09

Add zerocopy sendto

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/*
2 * Copyright (c) 2015 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.      IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/types.h>
33#include <sys/param.h>
34#include <sys/mbuf.h>
35#include <sys/malloc.h>
36#include <sys/queue.h>
37
38#include <net/if.h>
39#include <net/if_arp.h>
40#include <net/ethernet.h>
41#include <arpa/inet.h>
42#include <netinet/in.h>
43#include <netinet/ip.h>
44#include <netinet/udp.h>
45
46#include <assert.h>
47#include <errno.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <unistd.h>
52
53#include <rtems.h>
54#include <rtems/shell.h>
55#include <rtems/telnetd.h>
56#include <rtems/bsd/zerocopy.h>
57#include <rtems/bsd/test/network-config.h>
58
59#define TEST_NAME "LIBBSD ZEROCOPY 1"
60
61#define BUFFER_COUNT 128
62
63#define NOTIFY_THRESHOLD 64
64
65#define DATA_SIZE (ETHERMTU - sizeof(struct ip) - sizeof(struct udphdr))
66
67struct buffer {
68        SLIST_ENTRY(buffer) link;
69        u_int ref_cnt;
70        char *data;
71};
72
73struct buffer_control {
74        SLIST_HEAD(buffer_list, buffer) free_list;
75        rtems_interrupt_lock lock;
76        size_t free_buffers;
77        rtems_id waiting_task;
78        struct buffer buffers[BUFFER_COUNT];
79        char data[DATA_SIZE][BUFFER_COUNT];
80};
81
82static struct buffer_control buffer_control;
83
84static void
85buffer_free(void *arg1, void *arg2)
86{
87        struct buffer_control *bc = arg1;
88        struct buffer *buf = arg2;
89        rtems_status_code sc;
90        rtems_interrupt_lock_context lock_context;
91        rtems_id waiting_task;
92
93        buf->ref_cnt = 0;
94
95        rtems_interrupt_lock_acquire(&bc->lock, &lock_context);
96        SLIST_INSERT_HEAD(&bc->free_list, buf, link);
97        waiting_task = bc->waiting_task;
98        ++bc->free_buffers;
99        if (bc->free_buffers < NOTIFY_THRESHOLD) {
100                waiting_task = 0;
101        }
102        rtems_interrupt_lock_release(&bc->lock, &lock_context);
103
104        if (waiting_task != 0) {
105                sc = rtems_event_transient_send(waiting_task);
106                assert(sc == RTEMS_SUCCESSFUL);
107        }
108}
109
110static struct mbuf *
111buffer_get(struct buffer_control *bc)
112{
113        struct mbuf *m = rtems_bsd_m_gethdr(M_WAITOK, MT_DATA);
114        struct buffer *buf;
115        rtems_status_code sc;
116        rtems_interrupt_lock_context lock_context;
117
118        do {
119                rtems_interrupt_lock_acquire(&bc->lock, &lock_context);
120                if (SLIST_EMPTY(&bc->free_list)) {
121                        bc->waiting_task = rtems_task_self();
122                        rtems_interrupt_lock_release(&bc->lock, &lock_context);
123
124                        sc = rtems_event_transient_receive(RTEMS_WAIT,
125                            RTEMS_NO_TIMEOUT);
126                        assert(sc == RTEMS_SUCCESSFUL);
127
128                        buf = NULL;
129                } else {
130                        buf = SLIST_FIRST(&bc->free_list);
131                        SLIST_REMOVE_HEAD(&bc->free_list, link);
132                        --bc->free_buffers;
133                        rtems_interrupt_lock_release(&bc->lock, &lock_context);
134                }
135        } while (buf == NULL);
136
137        m->m_len = DATA_SIZE;
138        m->m_pkthdr.len = DATA_SIZE;
139        rtems_bsd_m_extaddref(m, buf, DATA_SIZE, &buf->ref_cnt, buffer_free,
140            bc, buf);
141
142        return (m);
143}
144
145static void
146network_flood_task(rtems_task_argument arg)
147{
148        int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
149        struct sockaddr_in addr = {
150                .sin_len = sizeof(addr),
151                .sin_family = AF_INET,
152                .sin_port = htons(13161),
153                .sin_addr = {
154                        .s_addr = INADDR_ANY
155                }
156        };
157        int rv = inet_aton(NET_CFG_PEER_IP, &addr.sin_addr);
158        struct buffer_control *bc = (struct buffer_control *)arg;
159
160        assert(fd >= 0);
161        assert(rv != 0);
162
163        while (true) {
164                struct mbuf *m = buffer_get(bc);
165                int error = rtems_bsd_sendto(
166                        fd,
167                        m,
168                        0,
169                        (const struct sockaddr *) &addr
170                );
171
172                if (error != 0) {
173                        printf("zerocopy sendto error: %s\n", strerror(error));
174                        sleep(1);
175                }
176        }
177}
178
179static void
180telnet_shell(char *name, void *arg)
181{
182        rtems_shell_env_t env;
183
184        memset(&env, 0, sizeof(env));
185
186        env.devname = name;
187        env.taskname = "TLNT";
188        env.login_check = NULL;
189        env.forever = false;
190
191        rtems_shell_main_loop(&env);
192}
193
194rtems_telnetd_config_table rtems_telnetd_config = {
195        .command = telnet_shell,
196        .arg = NULL,
197        .priority = 2,
198        .stack_size = 0,
199        .login_check = NULL,
200        .keep_stdio = false
201};
202
203static void
204test_main(void)
205{
206        struct buffer_control *bc = &buffer_control;
207        rtems_status_code sc;
208        rtems_id id;
209        size_t i;
210
211        sc = rtems_telnetd_initialize();
212        assert(sc == RTEMS_SUCCESSFUL);
213
214        SLIST_INIT(&bc->free_list);
215
216        for (i = 0; i < BUFFER_COUNT; ++i) {
217                SLIST_INSERT_HEAD(&bc->free_list, &bc->buffers[i], link);
218                bc->buffers[i].data = &bc->data[i][0];
219        }
220
221        sc = rtems_task_create(
222                rtems_build_name('F', 'L', 'O', 'D'),
223                3,
224                RTEMS_MINIMUM_STACK_SIZE,
225                RTEMS_DEFAULT_MODES,
226                RTEMS_DEFAULT_ATTRIBUTES,
227                &id
228        );
229        assert(sc == RTEMS_SUCCESSFUL);
230
231        sc = rtems_task_start(id, network_flood_task, (rtems_task_argument) bc);
232        assert(sc == RTEMS_SUCCESSFUL);
233
234        rtems_task_delete(RTEMS_SELF);
235        assert(0);
236}
237
238#define DEFAULT_NETWORK_DHCPCD_ENABLE
239#define DEFAULT_NETWORK_SHELL
240
241#define CONFIGURE_MAXIMUM_DRIVERS 32
242
243#include <rtems/bsd/test/default-network-init.h>
Note: See TracBrowser for help on using the repository browser.