1 /*
2  * Copyright 2019 The Hafnium Authors.
3  *
4  * Use of this source code is governed by a BSD-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/BSD-3-Clause.
7  */
8 
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 
17 #include "hf/dlog.h"
18 #include "hf/socket.h"
19 
20 #include "test/hftest.h"
21 #include <sys/socket.h>
22 #include <sys/syscall.h>
23 #include <sys/types.h>
24 
25 #define MAX_BUF_SIZE 256
26 
finit_module(int fd,const char * param_values,int flags)27 static int finit_module(int fd, const char *param_values, int flags)
28 {
29 	return (int)syscall(SYS_finit_module, fd, param_values, flags);
30 }
31 
delete_module(const char * name,int flags)32 static int delete_module(const char *name, int flags)
33 {
34 	return (int)syscall(SYS_delete_module, name, flags);
35 }
36 
insmod_hafnium(void)37 static void insmod_hafnium(void)
38 {
39 	int module_file = open("/hafnium.ko", O_RDONLY);
40 	if (module_file < 0) {
41 		FAIL("Failed to load Hafnium kernel module from /hafnium.ko");
42 		return;
43 	}
44 	EXPECT_EQ(finit_module(module_file, "", 0), 0);
45 	close(module_file);
46 }
47 
rmmod_hafnium(void)48 static void rmmod_hafnium(void)
49 {
50 	int ret = delete_module("hafnium", 0);
51 
52 	EXPECT_EQ(ret, 0);
53 	if (ret != 0) {
54 		HFTEST_LOG("Error %d (%s) removing hafnium kernel module.",
55 			   errno, strerror(errno));
56 	}
57 }
58 
59 /**
60  * Loads and unloads the Hafnium kernel module.
61  */
TEST(linux,load_hafnium)62 TEST(linux, load_hafnium)
63 {
64 	insmod_hafnium();
65 	rmmod_hafnium();
66 
67 	/* Removing a second time should fail. */
68 	EXPECT_EQ(delete_module("hafnium", 0), -1);
69 	EXPECT_EQ(errno, ENOENT);
70 }
71 
72 /**
73  * Uses the kernel module to send a socket message from the primary VM to a
74  * secondary VM and echoes it back to the primary.
75  */
TEST(linux,socket_echo_hafnium)76 TEST(linux, socket_echo_hafnium)
77 {
78 	ffa_vm_id_t vm_id = HF_VM_ID_OFFSET + 1;
79 	int port = 10;
80 	int socket_id;
81 	struct hf_sockaddr addr;
82 	const char send_buf[] = "The quick brown fox jumps over the lazy dogs.";
83 	size_t send_len = sizeof(send_buf);
84 	char resp_buf[MAX_BUF_SIZE];
85 	ssize_t recv_len;
86 
87 	ASSERT_LT(send_len, MAX_BUF_SIZE);
88 
89 	insmod_hafnium();
90 
91 	/* Create Hafnium socket. */
92 	socket_id = socket(PF_HF, SOCK_DGRAM, 0);
93 	if (socket_id == -1) {
94 		FAIL("Socket creation failed: %s", strerror(errno));
95 		return;
96 	}
97 	HFTEST_LOG("Socket created successfully.");
98 
99 	/* Connect to requested VM & port. */
100 	addr.family = PF_HF;
101 	addr.vm_id = vm_id;
102 	addr.port = port;
103 	if (connect(socket_id, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
104 		FAIL("Socket connection failed: %s", strerror(errno));
105 		return;
106 	}
107 	HFTEST_LOG("Socket to secondary VM %d connected on port %d.", vm_id,
108 		   port);
109 
110 	/*
111 	 * Send a message to the secondary VM.
112 	 * Enable the confirm flag to try again in case port is busy.
113 	 */
114 	if (send(socket_id, send_buf, send_len, MSG_CONFIRM) < 0) {
115 		FAIL("Socket send() failed: %s", strerror(errno));
116 		return;
117 	}
118 	HFTEST_LOG("Packet with length %d sent.", send_len);
119 
120 	/* Receive a response, which should be an echo of the sent packet. */
121 	recv_len = recv(socket_id, resp_buf, sizeof(resp_buf) - 1, 0);
122 
123 	if (recv_len == -1) {
124 		FAIL("Socket recv() failed: %s", strerror(errno));
125 		return;
126 	}
127 	HFTEST_LOG("Packet with length %d received.", recv_len);
128 
129 	EXPECT_EQ(recv_len, send_len);
130 	EXPECT_EQ(memcmp(send_buf, resp_buf, send_len), 0);
131 
132 	EXPECT_EQ(close(socket_id), 0);
133 	rmmod_hafnium();
134 }
135