1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* In-kernel rxperf server for testing purposes.
4 * Copyright (C) 2022 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) "rxperf: " fmt
9 #include <linux/module.h>
10 #include <linux/slab.h>
12 #include <net/af_rxrpc.h>
14 MODULE_DESCRIPTION("rxperf test server (afs)");
15 MODULE_AUTHOR("Red Hat, Inc.");
16 MODULE_LICENSE("GPL");
18 #define RXPERF_PORT 7009
19 #define RX_PERF_SERVICE 147
20 #define RX_PERF_VERSION 3
21 #define RX_PERF_SEND 0
22 #define RX_PERF_RECV 1
24 #define RX_PERF_FILE 4
25 #define RX_PERF_MAGIC_COOKIE 0x4711
27 struct rxperf_proto_params {
34 static const u8 rxperf_magic_cookie[] = { 0x00, 0x00, 0x47, 0x11 };
35 static const u8 secret[8] = { 0xa7, 0x83, 0x8a, 0xcb, 0xc7, 0x83, 0xec, 0x94 };
37 enum rxperf_call_state {
38 RXPERF_CALL_SV_AWAIT_PARAMS, /* Server: Awaiting parameter block */
39 RXPERF_CALL_SV_AWAIT_REQUEST, /* Server: Awaiting request data */
40 RXPERF_CALL_SV_REPLYING, /* Server: Replying */
41 RXPERF_CALL_SV_AWAIT_ACK, /* Server: Awaiting final ACK */
42 RXPERF_CALL_COMPLETE, /* Completed or failed */
46 struct rxrpc_call *rxcall;
49 struct work_struct work;
52 size_t req_len; /* Size of request blob */
53 size_t reply_len; /* Size of reply blob */
54 unsigned int debug_id;
55 unsigned int operation_id;
56 struct rxperf_proto_params params;
59 enum rxperf_call_state state;
61 unsigned short unmarshal;
63 int (*deliver)(struct rxperf_call *call);
64 void (*processor)(struct work_struct *work);
67 static struct socket *rxperf_socket;
68 static struct key *rxperf_sec_keyring; /* Ring of security/crypto keys */
69 static struct workqueue_struct *rxperf_workqueue;
71 static void rxperf_deliver_to_call(struct work_struct *work);
72 static int rxperf_deliver_param_block(struct rxperf_call *call);
73 static int rxperf_deliver_request(struct rxperf_call *call);
74 static int rxperf_process_call(struct rxperf_call *call);
75 static void rxperf_charge_preallocation(struct work_struct *work);
77 static DECLARE_WORK(rxperf_charge_preallocation_work,
78 rxperf_charge_preallocation);
80 static inline void rxperf_set_call_state(struct rxperf_call *call,
81 enum rxperf_call_state to)
86 static inline void rxperf_set_call_complete(struct rxperf_call *call,
87 int error, s32 remote_abort)
89 if (call->state != RXPERF_CALL_COMPLETE) {
90 call->abort_code = remote_abort;
92 call->state = RXPERF_CALL_COMPLETE;
96 static void rxperf_rx_discard_new_call(struct rxrpc_call *rxcall,
97 unsigned long user_call_ID)
99 kfree((struct rxperf_call *)user_call_ID);
102 static void rxperf_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
103 unsigned long user_call_ID)
105 queue_work(rxperf_workqueue, &rxperf_charge_preallocation_work);
108 static void rxperf_queue_call_work(struct rxperf_call *call)
110 queue_work(rxperf_workqueue, &call->work);
113 static void rxperf_notify_rx(struct sock *sk, struct rxrpc_call *rxcall,
114 unsigned long call_user_ID)
116 struct rxperf_call *call = (struct rxperf_call *)call_user_ID;
118 if (call->state != RXPERF_CALL_COMPLETE)
119 rxperf_queue_call_work(call);
122 static void rxperf_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
124 struct rxperf_call *call = (struct rxperf_call *)user_call_ID;
126 call->rxcall = rxcall;
129 static void rxperf_notify_end_reply_tx(struct sock *sock,
130 struct rxrpc_call *rxcall,
131 unsigned long call_user_ID)
133 rxperf_set_call_state((struct rxperf_call *)call_user_ID,
134 RXPERF_CALL_SV_AWAIT_ACK);
138 * Charge the incoming call preallocation.
140 static void rxperf_charge_preallocation(struct work_struct *work)
142 struct rxperf_call *call;
145 call = kzalloc(sizeof(*call), GFP_KERNEL);
149 call->type = "unset";
150 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
151 call->deliver = rxperf_deliver_param_block;
152 call->state = RXPERF_CALL_SV_AWAIT_PARAMS;
153 call->service_id = RX_PERF_SERVICE;
154 call->iov_len = sizeof(call->params);
155 call->kvec[0].iov_len = sizeof(call->params);
156 call->kvec[0].iov_base = &call->params;
157 iov_iter_kvec(&call->iter, READ, call->kvec, 1, call->iov_len);
158 INIT_WORK(&call->work, rxperf_deliver_to_call);
160 if (rxrpc_kernel_charge_accept(rxperf_socket,
174 * Open an rxrpc socket and bind it to be a server for callback notifications
175 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
177 static int rxperf_open_socket(void)
179 struct sockaddr_rxrpc srx;
180 struct socket *socket;
183 ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET6,
188 socket->sk->sk_allocation = GFP_NOFS;
190 /* bind the callback manager's address to make this a server socket */
191 memset(&srx, 0, sizeof(srx));
192 srx.srx_family = AF_RXRPC;
193 srx.srx_service = RX_PERF_SERVICE;
194 srx.transport_type = SOCK_DGRAM;
195 srx.transport_len = sizeof(srx.transport.sin6);
196 srx.transport.sin6.sin6_family = AF_INET6;
197 srx.transport.sin6.sin6_port = htons(RXPERF_PORT);
199 ret = rxrpc_sock_set_min_security_level(socket->sk,
200 RXRPC_SECURITY_ENCRYPT);
204 ret = rxrpc_sock_set_security_keyring(socket->sk, rxperf_sec_keyring);
206 ret = kernel_bind(socket, (struct sockaddr *)&srx, sizeof(srx));
210 rxrpc_kernel_new_call_notification(socket, rxperf_rx_new_call,
211 rxperf_rx_discard_new_call);
213 ret = kernel_listen(socket, INT_MAX);
217 rxperf_socket = socket;
218 rxperf_charge_preallocation(&rxperf_charge_preallocation_work);
222 sock_release(socket);
224 pr_err("Can't set up rxperf socket: %d\n", ret);
229 * close the rxrpc socket rxperf was using
231 static void rxperf_close_socket(void)
233 kernel_listen(rxperf_socket, 0);
234 kernel_sock_shutdown(rxperf_socket, SHUT_RDWR);
235 flush_workqueue(rxperf_workqueue);
236 sock_release(rxperf_socket);
240 * Log remote abort codes that indicate that we have a protocol disagreement
243 static void rxperf_log_error(struct rxperf_call *call, s32 remote_abort)
249 switch (remote_abort) {
250 case RX_EOF: msg = "unexpected EOF"; break;
251 case RXGEN_CC_MARSHAL: msg = "client marshalling"; break;
252 case RXGEN_CC_UNMARSHAL: msg = "client unmarshalling"; break;
253 case RXGEN_SS_MARSHAL: msg = "server marshalling"; break;
254 case RXGEN_SS_UNMARSHAL: msg = "server unmarshalling"; break;
255 case RXGEN_DECODE: msg = "opcode decode"; break;
256 case RXGEN_SS_XDRFREE: msg = "server XDR cleanup"; break;
257 case RXGEN_CC_XDRFREE: msg = "client XDR cleanup"; break;
258 case -32: msg = "insufficient data"; break;
266 pr_info("Peer reported %s failure on %s\n", msg, call->type);
271 * deliver messages to a call
273 static void rxperf_deliver_to_call(struct work_struct *work)
275 struct rxperf_call *call = container_of(work, struct rxperf_call, work);
276 enum rxperf_call_state state;
277 u32 abort_code, remote_abort = 0;
280 if (call->state == RXPERF_CALL_COMPLETE)
283 while (state = call->state,
284 state == RXPERF_CALL_SV_AWAIT_PARAMS ||
285 state == RXPERF_CALL_SV_AWAIT_REQUEST ||
286 state == RXPERF_CALL_SV_AWAIT_ACK
288 if (state == RXPERF_CALL_SV_AWAIT_ACK) {
289 if (!rxrpc_kernel_check_life(rxperf_socket, call->rxcall))
294 ret = call->deliver(call);
296 ret = rxperf_process_call(call);
305 rxperf_log_error(call, call->abort_code);
308 abort_code = RXGEN_OPCODE;
309 rxrpc_kernel_abort_call(rxperf_socket, call->rxcall,
310 abort_code, ret, "GOP");
313 abort_code = RX_USER_ABORT;
314 rxrpc_kernel_abort_call(rxperf_socket, call->rxcall,
315 abort_code, ret, "GUA");
318 pr_err("Call %u in bad state %u\n",
319 call->debug_id, call->state);
326 rxrpc_kernel_abort_call(rxperf_socket, call->rxcall,
327 RXGEN_SS_UNMARSHAL, ret, "GUM");
330 rxrpc_kernel_abort_call(rxperf_socket, call->rxcall,
331 RX_CALL_DEAD, ret, "GER");
337 rxperf_set_call_complete(call, ret, remote_abort);
338 /* The call may have been requeued */
339 rxrpc_kernel_end_call(rxperf_socket, call->rxcall);
340 cancel_work(&call->work);
345 * Extract a piece of data from the received data socket buffers.
347 static int rxperf_extract_data(struct rxperf_call *call, bool want_more)
349 u32 remote_abort = 0;
352 ret = rxrpc_kernel_recv_data(rxperf_socket, call->rxcall, &call->iter,
353 &call->iov_len, want_more, &remote_abort,
355 pr_debug("Extract i=%zu l=%zu m=%u ret=%d\n",
356 iov_iter_count(&call->iter), call->iov_len, want_more, ret);
357 if (ret == 0 || ret == -EAGAIN)
361 switch (call->state) {
362 case RXPERF_CALL_SV_AWAIT_REQUEST:
363 rxperf_set_call_state(call, RXPERF_CALL_SV_REPLYING);
365 case RXPERF_CALL_COMPLETE:
366 pr_debug("premature completion %d", call->error);
374 rxperf_set_call_complete(call, ret, remote_abort);
379 * Grab the operation ID from an incoming manager call.
381 static int rxperf_deliver_param_block(struct rxperf_call *call)
386 /* Extract the parameter block */
387 ret = rxperf_extract_data(call, true);
391 version = ntohl(call->params.version);
392 call->operation_id = ntohl(call->params.type);
393 call->deliver = rxperf_deliver_request;
395 if (version != RX_PERF_VERSION) {
396 pr_info("Version mismatch %x\n", version);
400 switch (call->operation_id) {
404 call->iov_len = 4; /* Expect req size */
409 call->iov_len = 4; /* Expect reply size */
413 call->iov_len = 8; /* Expect req size and reply size */
422 rxperf_set_call_state(call, RXPERF_CALL_SV_AWAIT_REQUEST);
423 return call->deliver(call);
427 * Deliver the request data.
429 static int rxperf_deliver_request(struct rxperf_call *call)
433 switch (call->unmarshal) {
435 call->kvec[0].iov_len = call->iov_len;
436 call->kvec[0].iov_base = call->tmp;
437 iov_iter_kvec(&call->iter, READ, call->kvec, 1, call->iov_len);
441 ret = rxperf_extract_data(call, true);
445 switch (call->operation_id) {
448 call->req_len = ntohl(call->tmp[0]);
454 call->reply_len = ntohl(call->tmp[0]);
458 call->req_len = ntohl(call->tmp[0]);
459 call->reply_len = ntohl(call->tmp[1]);
462 pr_info("Can't parse extra params\n");
466 pr_debug("CALL op=%s rq=%zx rp=%zx\n",
467 call->type, call->req_len, call->reply_len);
469 call->iov_len = call->req_len;
470 iov_iter_discard(&call->iter, READ, call->req_len);
474 ret = rxperf_extract_data(call, false);
485 * Process a call for which we've received the request.
487 static int rxperf_process_call(struct rxperf_call *call)
489 struct msghdr msg = {};
490 struct bio_vec bv[1];
493 size_t reply_len = call->reply_len, len;
495 rxrpc_kernel_set_tx_length(rxperf_socket, call->rxcall,
496 reply_len + sizeof(rxperf_magic_cookie));
498 while (reply_len > 0) {
499 len = min_t(size_t, reply_len, PAGE_SIZE);
500 bv[0].bv_page = ZERO_PAGE(0);
503 iov_iter_bvec(&msg.msg_iter, WRITE, bv, 1, len);
504 msg.msg_flags = MSG_MORE;
505 n = rxrpc_kernel_send_data(rxperf_socket, call->rxcall, &msg,
506 len, rxperf_notify_end_reply_tx);
514 len = sizeof(rxperf_magic_cookie);
515 iov[0].iov_base = (void *)rxperf_magic_cookie;
516 iov[0].iov_len = len;
517 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
519 n = rxrpc_kernel_send_data(rxperf_socket, call->rxcall, &msg, len,
520 rxperf_notify_end_reply_tx);
522 return 0; /* Success */
525 rxrpc_kernel_abort_call(rxperf_socket, call->rxcall,
526 RXGEN_SS_MARSHAL, -ENOMEM, "GOM");
531 * Add a key to the security keyring.
533 static int rxperf_add_key(struct key *keyring)
538 kref = key_create_or_update(make_key_ref(keyring, true),
540 __stringify(RX_PERF_SERVICE) ":2",
543 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH
545 KEY_ALLOC_NOT_IN_QUOTA);
548 pr_err("Can't allocate rxperf server key: %ld\n", PTR_ERR(kref));
549 return PTR_ERR(kref);
552 ret = key_link(keyring, key_ref_to_ptr(kref));
554 pr_err("Can't link rxperf server key: %d\n", ret);
560 * Initialise the rxperf server.
562 static int __init rxperf_init(void)
567 pr_info("Server registering\n");
569 rxperf_workqueue = alloc_workqueue("rxperf", 0, 0);
570 if (!rxperf_workqueue)
571 goto error_workqueue;
573 keyring = keyring_alloc("rxperf_server",
574 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
575 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
577 KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH |
579 KEY_OTH_VIEW | KEY_OTH_READ | KEY_OTH_SEARCH,
580 KEY_ALLOC_NOT_IN_QUOTA,
582 if (IS_ERR(keyring)) {
583 pr_err("Can't allocate rxperf server keyring: %ld\n",
587 rxperf_sec_keyring = keyring;
588 ret = rxperf_add_key(keyring);
592 ret = rxperf_open_socket();
599 key_put(rxperf_sec_keyring);
601 destroy_workqueue(rxperf_workqueue);
604 pr_err("Failed to register: %d\n", ret);
607 late_initcall(rxperf_init); /* Must be called after net/ to create socket */
609 static void __exit rxperf_exit(void)
611 pr_info("Server unregistering.\n");
613 rxperf_close_socket();
614 key_put(rxperf_sec_keyring);
615 destroy_workqueue(rxperf_workqueue);
618 module_exit(rxperf_exit);