Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 17 | #include <errno.h> |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 18 | #include <fcntl.h> |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | |
| 25 | #include "hf/dlog.h" |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 26 | #include "hf/socket.h" |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 27 | |
Andrew Walbran | 1e7c774 | 2019-12-13 17:10:02 +0000 | [diff] [blame] | 28 | #include "test/hftest.h" |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 29 | #include <sys/socket.h> |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 30 | #include <sys/syscall.h> |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | |
| 33 | #define MAX_BUF_SIZE 256 |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 34 | |
| 35 | static int finit_module(int fd, const char *param_values, int flags) |
| 36 | { |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 37 | return (int)syscall(SYS_finit_module, fd, param_values, flags); |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static int delete_module(const char *name, int flags) |
| 41 | { |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 42 | return (int)syscall(SYS_delete_module, name, flags); |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static void insmod_hafnium(void) |
| 46 | { |
| 47 | int module_file = open("/hafnium.ko", O_RDONLY); |
| 48 | if (module_file < 0) { |
| 49 | FAIL("Failed to load Hafnium kernel module from /hafnium.ko"); |
| 50 | return; |
| 51 | } |
| 52 | EXPECT_EQ(finit_module(module_file, "", 0), 0); |
| 53 | close(module_file); |
| 54 | } |
| 55 | |
| 56 | static void rmmod_hafnium(void) |
| 57 | { |
Andrew Walbran | ffd1166 | 2020-04-20 16:10:23 +0100 | [diff] [blame] | 58 | int ret = delete_module("hafnium", 0); |
| 59 | |
| 60 | EXPECT_EQ(ret, 0); |
| 61 | if (ret != 0) { |
| 62 | HFTEST_LOG("Error %d (%s) removing hafnium kernel module.", |
| 63 | errno, strerror(errno)); |
| 64 | } |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 67 | /** |
| 68 | * Loads and unloads the Hafnium kernel module. |
| 69 | */ |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 70 | TEST(linux, load_hafnium) |
| 71 | { |
| 72 | insmod_hafnium(); |
| 73 | rmmod_hafnium(); |
Andrew Walbran | ffd1166 | 2020-04-20 16:10:23 +0100 | [diff] [blame] | 74 | |
| 75 | /* Removing a second time should fail. */ |
| 76 | EXPECT_EQ(delete_module("hafnium", 0), -1); |
| 77 | EXPECT_EQ(errno, ENOENT); |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 78 | } |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * Uses the kernel module to send a socket message from the primary VM to a |
| 82 | * secondary VM and echoes it back to the primary. |
| 83 | */ |
| 84 | TEST(linux, socket_echo_hafnium) |
| 85 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 86 | ffa_vm_id_t vm_id = HF_VM_ID_OFFSET + 1; |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 87 | int port = 10; |
| 88 | int socket_id; |
| 89 | struct hf_sockaddr addr; |
| 90 | 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] | 91 | size_t send_len = sizeof(send_buf); |
Fuad Tabba | bfa42bc | 2019-08-06 13:45:50 +0100 | [diff] [blame] | 92 | char resp_buf[MAX_BUF_SIZE]; |
| 93 | ssize_t recv_len; |
| 94 | |
| 95 | ASSERT_LT(send_len, MAX_BUF_SIZE); |
| 96 | |
| 97 | insmod_hafnium(); |
| 98 | |
| 99 | /* Create Hafnium socket. */ |
| 100 | socket_id = socket(PF_HF, SOCK_DGRAM, 0); |
| 101 | if (socket_id == -1) { |
| 102 | FAIL("Socket creation failed: %s", strerror(errno)); |
| 103 | return; |
| 104 | } |
| 105 | HFTEST_LOG("Socket created successfully."); |
| 106 | |
| 107 | /* Connect to requested VM & port. */ |
| 108 | addr.family = PF_HF; |
| 109 | addr.vm_id = vm_id; |
| 110 | addr.port = port; |
| 111 | if (connect(socket_id, (struct sockaddr *)&addr, sizeof(addr)) == -1) { |
| 112 | FAIL("Socket connection failed: %s", strerror(errno)); |
| 113 | return; |
| 114 | } |
| 115 | HFTEST_LOG("Socket to secondary VM %d connected on port %d.", vm_id, |
| 116 | port); |
| 117 | |
| 118 | /* |
| 119 | * Send a message to the secondary VM. |
| 120 | * Enable the confirm flag to try again in case port is busy. |
| 121 | */ |
| 122 | if (send(socket_id, send_buf, send_len, MSG_CONFIRM) < 0) { |
| 123 | FAIL("Socket send() failed: %s", strerror(errno)); |
| 124 | return; |
| 125 | } |
| 126 | HFTEST_LOG("Packet with length %d sent.", send_len); |
| 127 | |
| 128 | /* Receive a response, which should be an echo of the sent packet. */ |
| 129 | recv_len = recv(socket_id, resp_buf, sizeof(resp_buf) - 1, 0); |
| 130 | |
| 131 | if (recv_len == -1) { |
| 132 | FAIL("Socket recv() failed: %s", strerror(errno)); |
| 133 | return; |
| 134 | } |
| 135 | HFTEST_LOG("Packet with length %d received.", recv_len); |
| 136 | |
| 137 | EXPECT_EQ(recv_len, send_len); |
| 138 | EXPECT_EQ(memcmp(send_buf, resp_buf, send_len), 0); |
| 139 | |
| 140 | EXPECT_EQ(close(socket_id), 0); |
| 141 | rmmod_hafnium(); |
| 142 | } |