Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 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. |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 9 | #include <errno.h> |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 10 | #include <fcntl.h> |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 11 | #include <stdint.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
| 16 | |
| 17 | #include "hf/dlog.h" |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 18 | #include "hf/socket.h" |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 19 | |
Andrew Walbran | 1e7c774 | 2019-12-13 17:10:02 +0000 | [diff] [blame] | 20 | #include "test/hftest.h" |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 21 | #include <sys/socket.h> |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 22 | #include <sys/syscall.h> |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 23 | #include <sys/types.h> |
| 24 | |
| 25 | #define MAX_BUF_SIZE 256 |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 26 | |
| 27 | static int finit_module(int fd, const char *param_values, int flags) |
| 28 | { |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 29 | return (int)syscall(SYS_finit_module, fd, param_values, flags); |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | static int delete_module(const char *name, int flags) |
| 33 | { |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 34 | return (int)syscall(SYS_delete_module, name, flags); |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 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 | |
| 48 | static void rmmod_hafnium(void) |
| 49 | { |
Andrew Walbran | ffd1166 | 2020-04-20 16:10:23 +0100 | [diff] [blame] | 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 | } |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 59 | /** |
| 60 | * Loads and unloads the Hafnium kernel module. |
| 61 | */ |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 62 | TEST(linux, load_hafnium) |
| 63 | { |
| 64 | insmod_hafnium(); |
| 65 | rmmod_hafnium(); |
Andrew Walbran | ffd1166 | 2020-04-20 16:10:23 +0100 | [diff] [blame] | 66 | |
| 67 | /* Removing a second time should fail. */ |
| 68 | EXPECT_EQ(delete_module("hafnium", 0), -1); |
| 69 | EXPECT_EQ(errno, ENOENT); |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 70 | } |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 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 | */ |
| 76 | TEST(linux, socket_echo_hafnium) |
| 77 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 78 | ffa_vm_id_t vm_id = HF_VM_ID_OFFSET + 1; |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 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."; |
Andrew Scull | 3c351e9 | 2020-01-28 11:26:05 +0000 | [diff] [blame] | 83 | size_t send_len = sizeof(send_buf); |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 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 | } |