File tree Expand file tree Collapse file tree 3 files changed +26
-3
lines changed Expand file tree Collapse file tree 3 files changed +26
-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
+ extern mempool_t * http_buf_pool ;
8
+
6
9
struct http_server_param {
7
10
struct socket * listen_socket ;
8
11
};
9
12
10
13
extern int http_server_daemon (void * arg );
11
14
15
+ static inline void * http_buf_alloc (gfp_t gfp_mask , void * pool_data )
16
+ {
17
+ return kzalloc (RECV_BUFFER_SIZE , gfp_mask );
18
+ }
19
+
20
+ static inline void http_buf_free (void * element , void * pool_data )
21
+ {
22
+ kfree (element );
23
+ }
12
24
#endif
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 );
@@ -154,6 +159,11 @@ static void close_listen_socket(struct socket *socket)
154
159
155
160
static int __init khttpd_init (void )
156
161
{
162
+ if (!(http_buf_pool = mempool_create (POOL_MIN_NR , http_buf_alloc ,
163
+ http_buf_free , NULL ))) {
164
+ pr_err ("failed to create mempool\n" );
165
+ return - ENOMEM ;
166
+ }
157
167
int err = open_listen_socket (port , backlog , & listen_socket );
158
168
if (err < 0 ) {
159
169
pr_err ("can't open listen socket\n" );
You can’t perform that action at this time.
0 commit comments