1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
use core::{
any::{self, TypeId},
fmt,
marker::PhantomData,
mem, ptr,
};
use crate::comms::{kchannel, oneshot::Reusable};
use maitake::sync::{RwLock, WaitQueue};
use mnemos_alloc::containers::FixedVec;
use portable_atomic::{AtomicU32, Ordering};
use postcard::experimental::max_size::MaxSize;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use spitebuf::EnqueueError;
use tracing::{self, debug, info, trace, warn, Level};
pub use uuid::{uuid, Uuid};
use crate::comms::{
bbq,
kchannel::{ErasedKProducer, KProducer},
oneshot::{ReusableError, Sender},
};
pub mod listener;
pub use self::listener::{Listener, Registration};
#[cfg(test)]
mod tests;
/// A partial list of known UUIDs of driver services
pub mod known_uuids {
use super::*;
/// Kernel UUIDs
pub mod kernel {
use super::*;
pub const SERIAL_MUX: Uuid = uuid!("54c983fa-736f-4223-b90d-c4360a308647");
pub const SIMPLE_SERIAL_PORT: Uuid = uuid!("f06aac01-2773-4266-8681-583ffe756554");
#[deprecated(note = "Use EMB_DISPLAY_V2 instead")]
pub const EMB_DISPLAY: Uuid = uuid!("b54db574-3eb7-4c89-8bfb-1a20890be68e");
pub const FORTH_SPAWNULATOR: Uuid = uuid!("4ae4a406-005a-4bde-be91-afc1900f76fa");
pub const I2C: Uuid = uuid!("011ebd3e-1b14-4bfd-b581-6138239b82f3");
pub const KEYBOARD: Uuid = uuid!("524d77b1-499c-440b-bd62-e63c0918efb5");
pub const KEYBOARD_MUX: Uuid = uuid!("70861d1c-9f01-4e9b-89e6-ede77d8f26d8");
pub const EMB_DISPLAY_V2: Uuid = uuid!("aa6a2af8-afd8-40e3-83c2-2c501c698aa8");
pub const SDMMC: Uuid = uuid!("9f4f8244-c986-4212-982e-d35890260de4");
}
// In case you need to iterate over every UUID
#[allow(deprecated)]
pub static ALL: &[Uuid] = &[
kernel::SERIAL_MUX,
kernel::SIMPLE_SERIAL_PORT,
kernel::EMB_DISPLAY,
kernel::FORTH_SPAWNULATOR,
kernel::I2C,
kernel::KEYBOARD,
kernel::KEYBOARD_MUX,
kernel::EMB_DISPLAY_V2,
];
}
/// A marker trait designating a registerable driver service.
///
/// Typically used with [`Registry::register`] or [`Registry::register_konly`].
/// A connection to the service can be established using [`Registry::connect`],
/// [`Registry::try_connect`], [`Registry::connect_userspace`], or
/// [`Registry::try_connect_userspace] (depending on the service), after
/// the service has been registered.
pub trait RegisteredDriver {
/// This is the type of the request sent TO the driver service
type Request: 'static;
/// This is the type of a SUCCESSFUL response sent FROM the driver service
type Response: 'static;
/// This is the type of an UNSUCCESSFUL response sent FROM the driver service
type Error: 'static;
/// An initial message sent to the service by a client when establishing a
/// connection.
///
/// This may be used by the service to route connections to specific
/// resources owned by that service, or to determine whether or not the
/// connection can be established. If the service does not require initial
/// data from the client, this type can be set to [`()`].
// XXX(eliza): ideally, we could default `Hello` to () and `ConnectError` to
// `Infallible`...do we want to do that? it requires a nightly feature.
type Hello: 'static;
/// Errors returned by the service if an incoming connection handshake is
/// rejected.
///
/// If the service does not reject connections, this should be set to
/// [`core::convert::Infallible`].
type ConnectError: 'static;
/// This is the UUID of the driver service
const UUID: Uuid;
/// Get the [`TypeId`] used to make sure that driver instances are correctly typed.
/// Corresponds to the same type ID as `(`[`Self::Request`]`, `[`Self::Response`]`,
/// `[`Self::Error`]`, `[`Self::Hello`]`, `[`Self::ConnectError`]`)`.
fn type_id() -> RegistryType {
RegistryType {
tuple_type_id: TypeId::of::<(
Self::Request,
Self::Response,
Self::Error,
Self::Hello,
Self::ConnectError,
)>(),
}
}
}
pub struct RegistryType {
tuple_type_id: TypeId,
}
/// The driver registry used by the kernel.
pub struct Registry {
items: RwLock<FixedVec<RegistryItem>>,
counter: AtomicU32,
service_added: WaitQueue,
}
// TODO: This probably goes into the ABI crate, here is fine for now
#[derive(Serialize, Deserialize)]
pub struct UserRequest<'a> {
// TODO: Maybe not the UUID, maybe pre-discover a shorter UID?
uid: Uuid,
nonce: u32,
#[serde(borrow)]
req_bytes: &'a [u8],
}
// TODO: This probably goes into the ABI crate, here is fine for now
#[derive(Serialize, Deserialize)]
pub struct UserResponse<U, E> {
// TODO: Maybe not the UUID, maybe pre-discover a shorter UID?
uuid: Uuid,
nonce: u32,
reply: Result<U, E>,
//
// KEEP IN SYNC WITH POSTCARD_MAX_SIZE BELOW!
//
}
// UserResponse
impl<U: MaxSize, E: MaxSize> MaxSize for UserResponse<U, E> {
//
// KEEP IN SYNC WITH STRUCT DEFINITION ABOVE!
//
const POSTCARD_MAX_SIZE: usize = {
<[u8; 16] as MaxSize>::POSTCARD_MAX_SIZE
+ <u32 as MaxSize>::POSTCARD_MAX_SIZE
+ <Result<U, E> as MaxSize>::POSTCARD_MAX_SIZE
};
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct ServiceId(pub(crate) u32);
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct ClientId(pub(crate) u32);
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct RequestResponseId(u32);
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum MessageKind {
Request,
Response,
}
impl RequestResponseId {
pub fn new(id: u32, kind: MessageKind) -> Self {
let bit = match kind {
MessageKind::Request => 0b1,
MessageKind::Response => 0b0,
};
Self((id << 1) | bit)
}
pub fn id(&self) -> u32 {
self.0 >> 1
}
pub fn kind(&self) -> MessageKind {
let bit = self.0 & 0b1;
if bit == 1 {
MessageKind::Request
} else {
MessageKind::Response
}
}
}
/// A wrapper for a message TO and FROM a driver service.
/// Used to be able to add additional message metadata without
/// changing the fundamental message type.
#[non_exhaustive]
pub struct Envelope<P> {
pub body: P,
service_id: ServiceId,
client_id: ClientId,
request_id: RequestResponseId,
}
pub struct OpenEnvelope<P> {
body: PhantomData<fn() -> P>,
service_id: ServiceId,
client_id: ClientId,
request_id: RequestResponseId,
}
/// The [Message] kind represents a full reply/response sequence to
/// a driver service. This is the concrete type received by the driver
/// service.
///
/// It contains the Request, e.g. [RegisteredDriver::Request], as well
/// as a [ReplyTo] that allows the driver service to respond to a given
/// request
pub struct Message<RD: RegisteredDriver> {
pub msg: Envelope<RD::Request>,
pub reply: ReplyTo<RD>,
}
/// A `ReplyTo` is used to allow the CLIENT of a service to choose the
/// way that the driver SERVICE replies to us. Essentially, this acts
/// as a "self addressed stamped envelope" for the SERVICE to use to
/// reply to the CLIENT.
pub enum ReplyTo<RD: RegisteredDriver> {
// This can be used to reply directly to another kernel entity,
// without a serialization step
KChannel(KProducer<Envelope<Result<RD::Response, RD::Error>>>),
// This can be used to reply directly ONCE to another kernel entity,
// without a serialization step
OneShot(Sender<Envelope<Result<RD::Response, RD::Error>>>),
// This can be used to reply to userspace. Responses are serialized
// and sent over the bbq::MpscProducer
Userspace {
nonce: u32,
outgoing: bbq::MpscProducer,
},
}
#[derive(Debug, Eq, PartialEq)]
pub enum ReplyError {
KOnlyUserspaceResponse,
ReplyChannelClosed,
UserspaceSerializationError,
InternalError,
}
#[derive(Debug, Eq, PartialEq)]
pub enum UserHandlerError {
DeserializationFailed,
QueueFull,
}
#[derive(Debug, Eq, PartialEq)]
pub enum RegistrationError {
UuidAlreadyRegistered(Uuid),
RegistryFull,
}
/// Errors returned by [`Registry::connect`] and [`Registry::try_connect`].
pub enum ConnectError<D: RegisteredDriver> {
/// No [`RegisteredDriver`] of this type was found!
///
/// The [`RegisteredDriver::Hello`] message is returned, so that it can be
/// used again.
NotFound(D::Hello),
/// The remote [`RegisteredDriver`] rejected the connection.
Rejected(D::ConnectError),
/// The remote [`RegisteredDriver`] has been registered, but the service
/// task has terminated.
DriverDead,
}
/// Errors returned by [`Registry::connect_userspace`] and
/// [`Registry::try_connect_userspace`].
pub enum UserConnectError<D: RegisteredDriver> {
/// No [`RegisteredDriver`] of this type was found!
NotFound,
/// The remote [`RegisteredDriver`] rejected the connection.
Rejected(D::ConnectError),
/// The remote [`RegisteredDriver`] has been registered, but the service
/// task has terminated.
DriverDead,
/// Deserializing the userspace `Hello` message failed.
DeserializationFailed(postcard::Error),
/// The requested driver is not exposed.
NotUserspace,
}
#[derive(Debug, Eq, PartialEq)]
pub enum OneshotRequestError {
/// An error occurred while acquiring a sender.
Sender(ReusableError),
/// Sending the request failed.
Send,
/// An error occurred while receiving the response.
Receive(ReusableError),
}
#[derive(Debug, Eq, PartialEq)]
pub enum SendError {
/// The service on the other end of the [`KernelHandle`] has terminated!
Closed,
}
impl From<ReusableError> for ReplyError {
fn from(err: ReusableError) -> Self {
match err {
ReusableError::ChannelClosed => ReplyError::ReplyChannelClosed,
_ => ReplyError::InternalError,
}
}
}
impl<T> From<EnqueueError<T>> for ReplyError {
fn from(enq: EnqueueError<T>) -> Self {
match enq {
// Should not be possible with async calls
EnqueueError::Full(_) => ReplyError::InternalError,
EnqueueError::Closed(_) => ReplyError::ReplyChannelClosed,
}
}
}
/// A UserspaceHandle is used to process incoming serialized messages from
/// userspace. It contains a method that can be used to deserialize messages
/// from a given UUID, and send that request (if the deserialization is
/// successful) to a given driver service.
pub struct UserspaceHandle {
req_producer_leaked: ErasedKProducer,
req_deser: ErasedReqDeser,
service_id: ServiceId,
client_id: ClientId,
}
/// A KernelHandle is used to send typed messages to a kernelspace Driver
/// service.
pub struct KernelHandle<RD: RegisteredDriver> {
prod: KProducer<Message<RD>>,
service_id: ServiceId,
client_id: ClientId,
request_ctr: u32,
}
type ErasedReqDeser = unsafe fn(
UserRequest<'_>,
&ErasedKProducer,
&bbq::MpscProducer,
ServiceId,
ClientId,
) -> Result<(), UserHandlerError>;
type ErasedHandshake = unsafe fn(
&maitake::scheduler::LocalScheduler,
&[u8],
&ErasedKProducer,
ptr::NonNull<()>,
) -> maitake::task::JoinHandle<Result<(), postcard::Error>>;
/// The payload of a registry item.
///
/// The typeid is stored here to allow the userspace handle to look up the UUID key
/// without knowing the proper typeid. Kernel space drivers should always check that the
/// tuple type id is correct.
struct RegistryValue {
req_resp_tuple_id: TypeId,
conn_prod: ErasedKProducer,
user_vtable: Option<UserVtable>,
service_id: ServiceId,
}
/// A [virtual function pointer table][vtable] (vtable) that specifies how
/// userspace requests are serialized and deserialized.
///
/// [vtable]: https://en.wikipedia.org/wiki/Virtual_method_table
#[derive(Copy, Clone)]
struct UserVtable {
/// Deserializes userspace requests.
req_deser: ErasedReqDeser,
/// Deserializes handshakes from userspace.
handshake: ErasedHandshake,
}
/// Right now we don't use a real HashMap, but rather a hand-rolled index map.
/// Therefore our registry is basically a `Vec<RegistryItem>`.
struct RegistryItem {
key: Uuid,
value: RegistryValue,
}
// RegistryType
impl RegistryType {
pub fn type_of(&self) -> TypeId {
self.tuple_type_id
}
}
// Registry
impl Registry {
/// Create a new registry with room for up to `max_items` registered drivers.
pub fn new(max_items: usize) -> Self {
let items = FixedVec::try_new(max_items).unwrap();
Self {
items: RwLock::new(items),
counter: AtomicU32::new(0),
service_added: WaitQueue::new(),
}
}
/// Bind a kernel-only [`Listener`] for a driver service of type `RD`.
///
/// This is a helper method which creates a [`Listener`] using
/// [`Listener::new`] and then registers that [`Listener`]'s
/// [`listener::Registration`] with the registry using
/// [`Registry::register_konly`].
///
/// Driver services registered with [`Registry::bind_konly`] can NOT be queried
/// or interfaced with from Userspace. If a registered service has request
/// and response types that are serializable, it can instead be registered
/// with [`Registry::bind`] which allows for userspace access.
pub async fn bind_konly<RD>(&self, capacity: usize) -> Result<Listener<RD>, RegistrationError>
where
RD: RegisteredDriver,
{
let (listener, registration) = Listener::new(capacity).await;
self.register_konly(registration).await?;
Ok(listener)
}
/// Bind a [`Listener`] for a driver service of type `RD`.
///
/// This is a helper method which creates a [`Listener`] using
/// [`Listener::new`] and then registers that [`Listener`]'s
/// [`listener::Registration`] with the registry using
/// [`Registry::register`].
///
/// Driver services registered with [`Registry::bind`] can be accessed both
/// by the kernel and by userspace. This requires that the
/// [`RegisteredDriver`]'s message types implement [`Serialize`] and
/// [`DeserializeOwned`]. Driver services whose message types are *not*
/// serializable may still bind listeners using [`Registry::bind_konly`],
/// but these listeners will not be accessible from userspace.
pub async fn bind<RD>(&self, capacity: usize) -> Result<Listener<RD>, RegistrationError>
where
RD: RegisteredDriver + 'static,
RD::Hello: Serialize + DeserializeOwned,
RD::ConnectError: Serialize + DeserializeOwned,
RD::Request: Serialize + DeserializeOwned,
RD::Response: Serialize + DeserializeOwned,
{
let (listener, registration) = Listener::new(capacity).await;
self.register(registration).await?;
Ok(listener)
}
/// Register a driver service ONLY for use in the kernel, including drivers.
///
/// Driver services registered with [Registry::register_konly] can NOT be queried
/// or interfaced with from Userspace. If a registered service has request
/// and response types that are serializable, it can instead be registered
/// with [Registry::register] which allows for userspace access.
#[tracing::instrument(
name = "Registry::register_konly",
level = Level::INFO,
skip(self, registration),
fields(svc = %any::type_name::<RD>()),
err(Display),
)]
pub async fn register_konly<RD: RegisteredDriver>(
&self,
registration: listener::Registration<RD>,
) -> Result<(), RegistrationError> {
let conn_prod = registration.tx.type_erase();
let service_id = self.counter.fetch_add(1, Ordering::Relaxed);
self.insert_item(RegistryItem {
key: RD::UUID,
value: RegistryValue {
req_resp_tuple_id: RD::type_id().type_of(),
conn_prod,
user_vtable: None,
service_id: ServiceId(service_id),
},
})
.await?;
info!(uuid = ?RD::UUID, service_id, "Registered KOnly");
Ok(())
}
/// Register a driver service for use in the kernel (including drivers) as
/// well as in userspace.
///
/// See [Registry::register_konly] if the request and response types are not
/// serializable.
#[tracing::instrument(
name = "Registry::register",
level = Level::INFO,
skip(self, registration),
fields(svc = %any::type_name::<RD>()),
err(Display),
)]
pub async fn register<RD>(
&self,
registration: listener::Registration<RD>,
) -> Result<(), RegistrationError>
where
RD: RegisteredDriver + 'static,
RD::Hello: Serialize + DeserializeOwned,
RD::ConnectError: Serialize + DeserializeOwned,
RD::Request: Serialize + DeserializeOwned,
RD::Response: Serialize + DeserializeOwned,
{
let service_id = self.counter.fetch_add(1, Ordering::Relaxed);
let conn_prod = registration.tx.type_erase();
self.insert_item(RegistryItem {
key: RD::UUID,
value: RegistryValue {
req_resp_tuple_id: RD::type_id().type_of(),
conn_prod,
user_vtable: Some(UserVtable::new::<RD>()),
service_id: ServiceId(service_id),
},
})
.await?;
info!(uuid = ?RD::UUID, service_id, "Registered");
Ok(())
}
/// Attempt to get a kernelspace (including drivers) handle of a given driver service,
/// which does not require sending a [`RegisteredDriver::Hello`] message.
///
/// This can be used by drivers and tasks to interface with a registered driver
/// service.
///
/// The driver service MUST have already been registered using [Registry::register] or
/// [Registry::register_konly] prior to making this call, otherwise no handle will
/// be returned. To wait until a driver is registered, use
/// [`Registry::connect`] instead.
///
/// # Returns
///
/// - [`Ok`]`(`[KernelHandle`]`)` if the requested service was found and
/// a connection was successfully established.
///
/// - [`Ok`]`(`[KernelHandle`]`)` if the requested service was found and
/// a connection was successfully established.
///
/// - [`Err`]`(`[`ConnectError::Rejected`]`)` if the service [rejected] the
/// incoming connection.
///
/// - [`Err`]`(`[`ConnectError::DriverDead`]`)` if the service has been
/// registered but is no longer running.
///
/// - [`Err`]`(`[`ConnectError::NotFound`]`)` if no service matching the
/// requested [`RegisteredDriver`] type exists in the registry.
///
/// [rejected]: listener::Handshake::reject
#[tracing::instrument(
name = "Registry::try_connect",
level = Level::DEBUG,
skip(self, hello),
fields(svc = %any::type_name::<RD>()),
)]
pub async fn try_connect<RD: RegisteredDriver>(
&self,
hello: RD::Hello,
) -> Result<KernelHandle<RD>, ConnectError<RD>> {
let (tx, service_id) = {
// /!\ WARNING: Load-bearing scope /!\
//
// We need to ensure that we only hold the lock on `self.items`
// while we're accessing the item; *not* while we're `await`ing a
// bunch of other stuff to connect to the service. This is
// important, because if we held the lock, no other task would be
// able to connect while we're waiting for the handshake,
// potentially causing a deadlock...
let items = self.items.read().await;
let item = match Self::get::<RD>(&items) {
Some(item) => item,
None => return Err(ConnectError::NotFound(hello)),
};
// cast the erased connection sender back to a typed sender.
let tx = unsafe {
// Safety: we just checked that the type IDs match above.
item.value
.conn_prod
.clone_typed::<listener::Handshake<RD>>()
};
(tx, item.value.service_id)
};
// TODO(eliza): it would be nice if we could reuse the oneshot receiver
// every time this driver is connected to? This would require type
// erasing it...
let rx = Reusable::new_async().await;
let reply = rx
.sender()
.await
.expect("we just created the oneshot, so this should never fail");
// send the connection request...
tx.enqueue_async(listener::Handshake {
hello,
accept: listener::Accept { reply }
}).await.map_err(|err| match err {
kchannel::EnqueueError::Closed(_) => ConnectError::DriverDead,
kchannel::EnqueueError::Full(_) => unreachable!("the channel should not be full, as we are using `enqueue_async`, which waits for capacity")
})?;
// ...and wait for a response with an established connection.
let prod = rx
.receive()
.await
// this is a `Reusable<Result<KProducer, RD::ConnectError>>>`, so
// the outer `Result` is the error returned by `receive()`...
.map_err(|_| ConnectError::DriverDead)?
// ...and the inner `Result` is the error returned by the driver.
.map_err(ConnectError::Rejected)?;
let client_id = self.counter.fetch_add(1, Ordering::Relaxed);
let res = Ok(KernelHandle {
prod,
service_id,
client_id: ClientId(client_id),
request_ctr: 0,
});
info!(
svc = %any::type_name::<RD>(),
uuid = ?RD::UUID,
service_id = service_id.0,
client_id,
"Got KernelHandle from Registry",
);
res
}
/// Get a kernelspace (including drivers) handle of a given driver service,
/// waiting until the service is registered if it does not already exist.
///
/// This can be used by drivers and tasks to interface with a registered
/// driver service.
///
/// If no service matching the requested [`RegisteredDriver`] type has been
/// registered, this method will wait until that service is added to the
/// registry, unless the registry becomes full.
///
/// # Returns
///
/// - [`Ok`]`(`[KernelHandle`]`)` if the requested service was found and
/// a connection was successfully established.
///
/// - [`Err`]`(`[`ConnectError::Rejected`]`)` if the service [rejected] the
/// incoming connection.
///
/// - [`Err`]`(`[`ConnectError::DriverDead`]`)` if the service has been
/// registered but is no longer running.
///
/// - [`Err`]`(`[`ConnectError::NotFound`]`)` if no service matching the
/// requested [`RegisteredDriver`] type exists *and* the registry was
/// full.
///
/// [rejected]: listener::Handshake::reject
#[tracing::instrument(
name = "Registry::connect",
level = Level::DEBUG,
skip(self, hello),
fields(svc = %any::type_name::<RD>()),
)]
pub async fn connect<RD>(&self, hello: RD::Hello) -> Result<KernelHandle<RD>, ConnectError<RD>>
where
RD: RegisteredDriver,
{
let mut hello = Some(hello);
let mut is_full = false;
loop {
match self.try_connect(hello.take().unwrap()).await {
Ok(handle) => return Ok(handle),
Err(ConnectError::NotFound(h)) if !is_full => {
hello = Some(h);
debug!("no service found; waiting for one to be added...");
// wait for a service to be added to the registry
is_full = self.service_added.wait().await.is_err();
}
Err(err) => return Err(err),
}
}
}
/// Get a kernelspace (including drivers) handle of a given driver service,
/// waiting until the service is registered if it does not already exist.
///
/// This can be used by drivers and tasks to interface with a registered driver
/// service.
///
/// # Returns
///
/// - [`Ok`]`(`[KernelHandle`]`)` if the requested service was found and
/// a connection was successfully established.
///
/// - [`Err`]`(`[`ConnectError`]`)` if the requested service was not
/// found in the registry, or if the service [rejected] the incoming
/// connection. Note that [`ConnectError::NotFound`] is not returned
/// _unless_ the registry is full and no more services will be added.
///
/// [rejected]: listener::Handshake::reject
#[tracing::instrument(
name = "Registry::connect_userspace",
level = Level::DEBUG,
skip(self),
fields(svc = %any::type_name::<RD>()),
)]
pub async fn connect_userspace<RD>(
&self,
scheduler: &maitake::scheduler::LocalScheduler,
user_hello: &[u8],
) -> Result<UserspaceHandle, UserConnectError<RD>>
where
RD: RegisteredDriver,
RD::Hello: Serialize + DeserializeOwned,
RD::ConnectError: Serialize + DeserializeOwned,
RD::Request: Serialize + DeserializeOwned,
RD::Response: Serialize + DeserializeOwned,
{
let mut is_full = false;
loop {
match self.try_connect_userspace(scheduler, user_hello).await {
Ok(handle) => return Ok(handle),
Err(UserConnectError::NotFound) if !is_full => {
debug!("no service found; waiting for one to be added...");
// wait for a service to be added to the registry
is_full = self.service_added.wait().await.is_err();
}
Err(err) => return Err(err),
}
}
}
/// Try to get a handle capable of processing serialized userspace messages to a
/// registered driver service, given a byte buffer for the userspace
/// [`RegisteredDriver::Hello`] message.
///
/// The driver service MUST have already been registered using [Registry::register] or
/// prior to making this call, otherwise no handle will be returned.
///
/// Driver services registered with [`Registry::register_konly`] cannot be
/// retrieved via a call to [`Registry::try_connect_userspace`].
#[tracing::instrument(
name = "Registry::try_connect_userspace",
level = Level::DEBUG,
skip(self, scheduler),
fields(svc = %any::type_name::<RD>()),
)]
pub async fn try_connect_userspace<RD>(
&self,
scheduler: &maitake::scheduler::LocalScheduler,
user_hello: &[u8],
) -> Result<UserspaceHandle, UserConnectError<RD>>
where
RD: RegisteredDriver,
RD::Hello: Serialize + DeserializeOwned,
RD::ConnectError: Serialize + DeserializeOwned,
RD::Request: Serialize + DeserializeOwned,
RD::Response: Serialize + DeserializeOwned,
{
let (vtable, conn_prod, service_id) = {
// /!\ WARNING: Load-bearing scope /!\
//
// We need to ensure that we only hold the lock on `self.items`
// while we're accessing the item; *not* while we're `await`ing a
// bunch of other stuff to connect to the service. This is
// important, because if we held the lock, no other task would be
// able to connect while we're waiting for the handshake,
// potentially causing a deadlock...
let items = self.items.read().await;
let item = Self::get::<RD>(&items).ok_or_else(|| UserConnectError::NotFound)?;
let vtable = item
.value
.user_vtable
// if the registry item has no userspace vtable, it's not exposed to
// userspace.
// this is *weird*, since this method requires that `RD`'s message
// types be serializable/deserializable, but it's possible that the
// driver was (accidentally?) registered with `register_konly` even
// though it didn't *need* to be due to serializability...
.ok_or(UserConnectError::NotUserspace)?;
let conn_prod = item.value.conn_prod.clone();
let service_id = item.value.service_id;
(vtable, conn_prod, service_id)
};
let mut handshake_result = mem::MaybeUninit::<UserHandshakeResult<RD>>::uninit();
let outptr = ptr::NonNull::from(&mut handshake_result).cast::<()>();
let handshake = unsafe { (vtable.handshake)(scheduler, user_hello, &conn_prod, outptr) };
let req_producer_leaked = match handshake.await {
// Outer `Result` is the `JoinError` from `maitake` --- it should
// always succeed, because we own the task's joinhandle, and we
// never cancel it.
Err(_) => unreachable!("handshake task should not be canceled"),
// Couldn't deserialize the userspace handshake bytes!
Ok(Err(error)) => {
return Err(UserConnectError::DeserializationFailed(error));
}
// Safe to touch the out pointer!
Ok(Ok(())) => unsafe {
// Safety: `handshake_result` is guaranteed to be initialized by
// `erased_handshake` if and only if its future completes with
// an `Ok(())`. and it did!
handshake_result.assume_init()?.type_erase()
},
};
let client_id = self.counter.fetch_add(1, Ordering::Relaxed);
info!(
svc = %any::type_name::<RD>(),
uuid = ?RD::UUID,
service_id = service_id.0,
client_id,
"Got KernelHandle from Registry",
);
Ok(UserspaceHandle {
req_producer_leaked,
req_deser: vtable.req_deser,
service_id,
client_id: ClientId(client_id),
})
}
async fn insert_item(&self, item: RegistryItem) -> Result<(), RegistrationError> {
{
let mut items = self.items.write().await;
if items.as_slice().iter().any(|i| i.key == item.key) {
return Err(RegistrationError::UuidAlreadyRegistered(item.key));
}
items.try_push(item).map_err(|_| {
warn!("failed to insert new registry item; the registry is full!");
// close the "service added" waitcell, because no new services will
// ever be added.
self.service_added.close();
RegistrationError::RegistryFull
})?;
}
self.service_added.wake_all();
Ok(())
}
fn get<RD: RegisteredDriver>(items: &FixedVec<RegistryItem>) -> Option<&RegistryItem> {
let Some(item) = items.as_slice().iter().find(|i| i.key == RD::UUID) else {
debug!(
svc = %any::type_name::<RD>(),
uuid = ?RD::UUID,
"No service for this UUID exists in the registry!"
);
return None;
};
let expected_type_id = RD::type_id().type_of();
let actual_type_id = item.value.req_resp_tuple_id;
if expected_type_id != actual_type_id {
warn!(
svc = %any::type_name::<RD>(),
uuid = ?RD::UUID,
type_id.expected = ?expected_type_id,
type_id.actual = ?actual_type_id,
"Registry entry's type ID did not match driver's type ID. This is (probably?) a bug!"
);
return None;
}
Some(item)
}
}
// UserRequest
// Envelope
impl<P> OpenEnvelope<P> {
pub fn fill(self, contents: P) -> Envelope<P> {
Envelope {
body: contents,
service_id: self.service_id,
client_id: self.client_id,
request_id: self.request_id,
}
}
}
impl<P> Envelope<P> {
// NOTE: proper types are constrained by [Message::split]
fn split_reply<R>(self) -> (P, OpenEnvelope<R>) {
let env = OpenEnvelope {
body: PhantomData,
service_id: self.service_id,
client_id: self.client_id,
request_id: RequestResponseId::new(self.request_id.id(), MessageKind::Response),
};
(self.body, env)
}
/// Create a response Envelope from a given request Envelope.
///
/// Maintains the same Service ID and Client ID, and increments the
/// request ID by one.
pub fn reply_with<U>(&self, body: U) -> Envelope<U> {
Envelope {
body,
service_id: self.service_id,
client_id: self.client_id,
request_id: RequestResponseId::new(self.request_id.id(), MessageKind::Response),
}
}
/// Create a response Envelope from a given request Envelope.
///
/// Maintains the same Service ID and Client ID, and increments the
/// request ID by one.
///
/// This variant also gives you the request body in case you need it for
/// the response.
pub fn reply_with_body<F, U>(self, f: F) -> Envelope<U>
where
F: FnOnce(P) -> U,
{
Envelope {
service_id: self.service_id,
client_id: self.client_id,
request_id: RequestResponseId::new(self.request_id.id(), MessageKind::Response),
body: f(self.body),
}
}
}
// Message
impl<RD: RegisteredDriver> Message<RD> {
// Would adding type aliases really make this any better? Who cares.
#[allow(clippy::type_complexity)]
pub fn split(
self,
) -> (
RD::Request,
OpenEnvelope<Result<RD::Response, RD::Error>>,
ReplyTo<RD>,
) {
let Self { msg, reply } = self;
let (req, env) = msg.split_reply();
(req, env, reply)
}
}
// ReplyTo
impl<RD: RegisteredDriver> ReplyTo<RD> {
pub async fn reply_konly(
self,
envelope: Envelope<Result<RD::Response, RD::Error>>,
) -> Result<(), ReplyError> {
trace!(
service_id = envelope.service_id.0,
client_id = envelope.client_id.0,
response_id = envelope.request_id.id(),
svc = %any::type_name::<RD>(),
"Replying KOnly",
);
match self {
ReplyTo::KChannel(kprod) => {
kprod.enqueue_async(envelope).await?;
}
ReplyTo::OneShot(sender) => {
sender.send(envelope)?;
}
ReplyTo::Userspace { .. } => return Err(ReplyError::KOnlyUserspaceResponse),
}
Ok(())
}
}
impl<RD: RegisteredDriver> ReplyTo<RD>
where
RD::Response: Serialize + MaxSize,
RD::Error: Serialize + MaxSize,
{
pub async fn reply(
self,
uuid_source: Uuid,
envelope: Envelope<Result<RD::Response, RD::Error>>,
) -> Result<(), ReplyError> {
trace!(
service_id = envelope.service_id.0,
client_id = envelope.client_id.0,
response_id = envelope.request_id.id(),
svc = %any::type_name::<RD>(),
"Replying",
);
match self {
ReplyTo::KChannel(kprod) => {
kprod.enqueue_async(envelope).await?;
Ok(())
}
ReplyTo::OneShot(sender) => {
sender.send(envelope)?;
Ok(())
}
ReplyTo::Userspace { nonce, outgoing } => {
let mut wgr = outgoing
.send_grant_exact(
<UserResponse<RD::Response, RD::Error> as MaxSize>::POSTCARD_MAX_SIZE,
)
.await;
let used = postcard::to_slice(
&UserResponse {
uuid: uuid_source,
nonce,
reply: envelope.body,
},
&mut wgr,
)
.map_err(|_| ReplyError::UserspaceSerializationError)?;
let len = used.len();
wgr.commit(len);
Ok(())
}
}
}
}
// UserspaceHandle
impl UserspaceHandle {
pub fn process_msg(
&self,
user_msg: UserRequest<'_>,
user_ring: &bbq::MpscProducer,
) -> Result<(), UserHandlerError> {
unsafe {
(self.req_deser)(
user_msg,
&self.req_producer_leaked,
user_ring,
self.service_id,
self.client_id,
)
}
}
}
// KernelHandle
impl<RD: RegisteredDriver> KernelHandle<RD> {
pub async fn send(&mut self, msg: RD::Request, reply: ReplyTo<RD>) -> Result<(), SendError> {
let request_id = RequestResponseId::new(self.request_ctr, MessageKind::Request);
self.request_ctr = self.request_ctr.wrapping_add(1);
self.prod
.enqueue_async(Message {
msg: Envelope {
body: msg,
service_id: self.service_id,
client_id: self.client_id,
request_id,
},
reply,
})
.await
.map_err(|_| SendError::Closed)?;
trace!(
service_id = self.service_id.0,
client_id = self.client_id.0,
request_id = request_id.id(),
svc = %any::type_name::<RD>(),
"Sent Request"
);
Ok(())
}
/// Send a [`ReplyTo::OneShot`] request using the provided [`Reusable`]
/// oneshot channel, and await the response from that channel.
pub async fn request_oneshot(
&mut self,
msg: RD::Request,
reply: &Reusable<Envelope<Result<RD::Response, RD::Error>>>,
) -> Result<Envelope<Result<RD::Response, RD::Error>>, OneshotRequestError> {
let tx = reply.sender().await.map_err(OneshotRequestError::Sender)?;
self.send(msg, ReplyTo::OneShot(tx))
.await
.map_err(|_| OneshotRequestError::Send)?;
reply.receive().await.map_err(OneshotRequestError::Receive)
}
}
// UserVtable
impl UserVtable {
const fn new<RD>() -> Self
where
RD: RegisteredDriver + 'static,
RD::Hello: Serialize + DeserializeOwned,
RD::ConnectError: Serialize + DeserializeOwned,
RD::Request: Serialize + DeserializeOwned,
RD::Response: Serialize + DeserializeOwned,
{
Self {
req_deser: map_deser::<RD>,
handshake: erased_user_handshake::<RD>,
}
}
}
/// A monomorphizable function that allows us to store the serialization type within
/// the function itself, allowing for a type-erased function pointer to be stored
/// inside of the registry.
///
/// SAFETY:
///
/// This function MUST be called with a `RegisteredDriver` type matching the type
/// used to create the `ErasedKProducer`.
unsafe fn map_deser<RD>(
umsg: UserRequest<'_>,
req_tx: &ErasedKProducer,
user_resp: &bbq::MpscProducer,
service_id: ServiceId,
client_id: ClientId,
) -> Result<(), UserHandlerError>
where
RD: RegisteredDriver,
RD::Request: Serialize + DeserializeOwned,
RD::Response: Serialize + DeserializeOwned,
{
// Un-type-erase the producer channel
//
// TODO: We don't really need to clone the producer, we just need a reference valid
// for the lifetime of `req_tx`. Consider adding a method for this before merging
// https://github.com/tosc-rs/mnemos/pull/25.
//
// This PROBABLY would require a "with"/closure method to make sure the producer ref
// doesn't outlive the LeakedKProducer reference.
let req_prod = req_tx.clone_typed::<Message<RD>>();
// Deserialize the request, if it doesn't have the right contents, deserialization will fail.
let u_payload: RD::Request = postcard::from_bytes(umsg.req_bytes)
.map_err(|_| UserHandlerError::DeserializationFailed)?;
// Create the message type to be sent on the channel
let msg: Message<RD> = Message {
msg: Envelope {
body: u_payload,
service_id,
client_id,
request_id: RequestResponseId::new(umsg.nonce, MessageKind::Request),
},
reply: ReplyTo::Userspace {
nonce: umsg.nonce,
outgoing: user_resp.clone(),
},
};
// Send the message, and report any failures
req_prod
.enqueue_sync(msg)
.map_err(|_| UserHandlerError::QueueFull)
}
type UserHandshakeResult<RD> = Result<KProducer<Message<RD>>, UserConnectError<RD>>;
/// Perform a type-erased userspace handshake, deserializing the
/// [`RegisteredDriver::Hello`] message from `hello_bytes` and returning a
/// future that writes the handshake result to the provided `outptr`, if the
/// future completes successfully.
///
/// # Safety
///
/// - This function MUST be called with a [`RegisteredDriver`] type matching the
/// type used to create the [`ErasedKProducer`].
/// - `outptr` MUST be a valid pointer to a
/// [`mem::MaybeUninit`]`<`[`UserHandshakeResult`]`<RD>>`, and MUST live as
/// long as the future returned from this function.
/// - `outptr` is guaranteed to be initialized IF AND ONLY IF the future
/// returned by this method returns [`Ok`]`(())`. If this method returns an
/// [`Err`], `outptr` will NOT be initialized.
unsafe fn erased_user_handshake<RD>(
scheduler: &maitake::scheduler::LocalScheduler,
hello_bytes: &[u8],
conn_tx: &ErasedKProducer,
outptr: core::ptr::NonNull<()>,
) -> maitake::task::JoinHandle<Result<(), postcard::Error>>
where
RD: RegisteredDriver + 'static,
RD::Hello: Serialize + DeserializeOwned,
RD::ConnectError: Serialize + DeserializeOwned,
RD::Request: Serialize + DeserializeOwned,
RD::Response: Serialize + DeserializeOwned,
{
let conn_tx = conn_tx.clone_typed::<listener::Handshake<RD>>();
// Deserialize the request, if it doesn't have the right contents, deserialization will fail.
let hello: Result<RD::Hello, _> = postcard::from_bytes(hello_bytes);
// spawn a task to allow us to perform async work from a type-erased context
scheduler.spawn(async move {
let hello = hello?;
// TODO(eliza): it would be nice if we could reuse the oneshot receiver
// every time this driver is connected to? This would require type
// erasing it...
let rx = Reusable::new_async().await;
let reply = rx
.sender()
.await
.expect("we just created the oneshot, so this should never fail");
// send the connection request...
conn_tx.enqueue_async(listener::Handshake {
hello,
accept: listener::Accept { reply }
}).await.map_err(|err| match err {
kchannel::EnqueueError::Closed(_) => todo!(),
kchannel::EnqueueError::Full(_) => unreachable!("the channel should not be full, as we are using `enqueue_async`, which waits for capacity")
})?;
// ...and wait for a response with an established connection.
let result = rx
.receive()
.await
// this is a `Reusable<Result<KProducer, RD::ConnectError>>>`, so
// the outer `Result` is the error returned by `receive()`...
.map_err(|_| UserConnectError::DriverDead)
// ...and the inner result is the connect error returned by the service.
.and_then(|res| res.map_err(UserConnectError::Rejected));
outptr
// Safety: the caller is responsible for ensuring the out pointer is
// correctly typed.
.cast::<mem::MaybeUninit<UserHandshakeResult<RD>>>()
.as_mut()
.write(result);
Ok(())
})
}
// UserHandlerError
impl fmt::Display for UserHandlerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::QueueFull => f.pad("service queue full"),
Self::DeserializationFailed => f.pad("failed to deserialize user request"),
}
}
}
// ConnectError
impl<D> PartialEq for ConnectError<D>
where
D: RegisteredDriver,
D::ConnectError: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::DriverDead, Self::DriverDead) => true,
(Self::NotFound(_), Self::NotFound(_)) => true,
(Self::Rejected(this), Self::Rejected(that)) => this == that,
_ => false,
}
}
}
impl<D> Eq for ConnectError<D>
where
D: RegisteredDriver,
D::ConnectError: Eq,
{
}
impl<D> fmt::Debug for ConnectError<D>
where
D: RegisteredDriver,
D::ConnectError: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut dbs = match self {
Self::DriverDead => f.debug_struct("DriverDead"),
Self::NotFound(_) => f.debug_struct("NotFound"),
Self::Rejected(error) => {
let mut d = f.debug_struct("Rejected");
d.field("error", error);
d
}
};
dbs.field("svc", &mycelium_util::fmt::display(any::type_name::<D>()))
.finish()
}
}
impl<D> fmt::Display for ConnectError<D>
where
D: RegisteredDriver,
D::ConnectError: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = any::type_name::<D>();
match self {
Self::DriverDead => write!(f, "the {name} service has terminated"),
Self::NotFound(_) => write!(f, "no {name} service found in the registry",),
Self::Rejected(err) => write!(f, "the {name} service rejected the connection: {err}",),
}
}
}
// UserConnectError
impl<D> PartialEq for UserConnectError<D>
where
D: RegisteredDriver,
D::ConnectError: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::DeserializationFailed(this), Self::DeserializationFailed(that)) => this == that,
(Self::Rejected(this), Self::Rejected(that)) => this == that,
(Self::NotFound, Self::NotFound) => true,
(Self::DriverDead, Self::DriverDead) => true,
(Self::NotUserspace, Self::NotUserspace) => true,
_ => false,
}
}
}
impl<D> Eq for UserConnectError<D>
where
D: RegisteredDriver,
D::ConnectError: Eq,
{
}
impl<D> fmt::Debug for UserConnectError<D>
where
D: RegisteredDriver,
D::ConnectError: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::DeserializationFailed(error) => {
let mut d = f.debug_struct("DeserializationFailed");
d.field("error", error);
d
}
Self::NotFound => f.debug_struct("NotFound"),
Self::DriverDead => f.debug_struct("NotFound"),
Self::Rejected(error) => {
let mut d = f.debug_struct("Rejected");
d.field("error", &error);
d
}
Self::NotUserspace => f.debug_struct("NotUserspace"),
}
.field("svc", &mycelium_util::fmt::display(any::type_name::<D>()))
.finish()
}
}
impl<D> fmt::Display for UserConnectError<D>
where
D: RegisteredDriver,
D::ConnectError: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = any::type_name::<D>();
match self {
Self::DriverDead => write!(f, "the {name} service has terminated"),
Self::NotFound => write!(f, "no {name} service found in the registry"),
Self::Rejected(err) => write!(f, "the {name} service rejected the connection: {err}",),
Self::DeserializationFailed(err) => write!(
f,
"failed to deserialize userspace Hello for the {} service: {err}",
any::type_name::<D>()
),
Self::NotUserspace => write!(
f,
"the {} service is not exposed to userspace",
any::type_name::<D>()
),
}
}
}
// RegistrationError
impl fmt::Display for RegistrationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::RegistryFull => "the registry is full".fmt(f),
Self::UuidAlreadyRegistered(uuid) => {
write!(f, "a service with UUID {uuid} has already been registered")
}
}
}
}