File tree Expand file tree Collapse file tree 3 files changed +29
-3
lines changed Expand file tree Collapse file tree 3 files changed +29
-3
lines changed Original file line number Diff line number Diff line change 7
7
#include "http_parser.h"
8
8
#include "http_server.h"
9
9
10
+ extern mempool_t * http_buf_pool ;
11
+
10
12
#define CRLF "\r\n"
11
13
12
14
#define HTTP_RESPONSE_200_DUMMY \
30
32
"Content-Type: text/plain" CRLF "Content-Length: 21" CRLF \
31
33
"Connection: KeepAlive" CRLF CRLF "501 Not Implemented" CRLF
32
34
33
- #define RECV_BUFFER_SIZE 4096
34
35
35
36
struct http_request {
36
37
struct socket * socket ;
@@ -165,7 +166,7 @@ static int http_server_worker(void *arg)
165
166
allow_signal (SIGKILL );
166
167
allow_signal (SIGTERM );
167
168
168
- buf = kzalloc ( RECV_BUFFER_SIZE , GFP_KERNEL );
169
+ buf = mempool_alloc ( http_buf_pool , GFP_KERNEL );
169
170
if (!buf ) {
170
171
pr_err ("can't allocate memory!\n" );
171
172
err = - ENOMEM ;
@@ -190,7 +191,7 @@ static int http_server_worker(void *arg)
190
191
memset (buf , 0 , RECV_BUFFER_SIZE );
191
192
}
192
193
out_free_buf :
193
- kfree (buf );
194
+ mempool_free (buf , http_buf_pool );
194
195
out :
195
196
kernel_sock_shutdown (socket , SHUT_RDWR );
196
197
sock_release (socket );
Original file line number Diff line number Diff line change 3
3
4
4
#include <net/sock.h>
5
5
6
+ #define RECV_BUFFER_SIZE 4096
7
+
6
8
struct http_server_param {
7
9
struct socket * listen_socket ;
8
10
};
Original file line number Diff line number Diff line change 1
1
#define pr_fmt (fmt ) KBUILD_MODNAME ": " fmt
2
2
3
3
#include <linux/kthread.h>
4
+ #include <linux/mempool.h>
4
5
#include <linux/sched/signal.h>
6
+ #include <linux/slab.h>
5
7
#include <linux/tcp.h>
6
8
#include <linux/version.h>
7
9
#include <net/sock.h>
10
12
11
13
#define DEFAULT_PORT 8081
12
14
#define DEFAULT_BACKLOG 100
15
+ #define POOL_MIN_NR 4
16
+
17
+ mempool_t * http_buf_pool ;
13
18
14
19
static ushort port = DEFAULT_PORT ;
15
20
module_param (port , ushort , S_IRUGO );
@@ -152,8 +157,26 @@ static void close_listen_socket(struct socket *socket)
152
157
sock_release (socket );
153
158
}
154
159
160
+
161
+ static void * http_buf_alloc (gfp_t gfp_mask , void * pool_data )
162
+ {
163
+ return kzalloc (RECV_BUFFER_SIZE , gfp_mask );
164
+ }
165
+
166
+ static void http_buf_free (void * element , void * pool_data )
167
+ {
168
+ kfree (element );
169
+ }
170
+
155
171
static int __init khttpd_init (void )
156
172
{
173
+ if (!(http_buf_pool = mempool_create (POOL_MIN_NR , http_buf_alloc ,
174
+ http_buf_free , NULL ))) {
175
+ pr_err ("failed to create mempool\n" );
176
+ return - ENOMEM ;
177
+ }
178
+
179
+
157
180
int err = open_listen_socket (port , backlog , & listen_socket );
158
181
if (err < 0 ) {
159
182
pr_err ("can't open listen socket\n" );
You can’t perform that action at this time.
0 commit comments