-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mctp: introduce ops wrapper for socket operations
We'll want to be able to mock the kernel interactions for testing, so call all of the socket operations (on both netlink and MCTP sockets) through a central set of funtion pointers. This will allow us to override with mock implementations for testing. Signed-off-by: Jeremy Kerr <[email protected]>
- Loading branch information
Showing
6 changed files
with
155 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* SPDX-License-Identifier: GPL-2.0 */ | ||
/* | ||
* mctp-ops: Abstraction for socket operations for mctp & mctpd. | ||
* | ||
* Copyright (c) 2023 Code Construct | ||
*/ | ||
|
||
#define _GNU_SOURCE | ||
|
||
#include <unistd.h> | ||
#include <linux/netlink.h> | ||
|
||
#include "mctp-ops.h" | ||
|
||
static int mctp_op_mctp_socket(void) | ||
{ | ||
return socket(AF_MCTP, SOCK_DGRAM, 0); | ||
} | ||
|
||
static int mctp_op_netlink_socket(void) | ||
{ | ||
return socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); | ||
} | ||
|
||
static int mctp_op_bind(int sd, struct sockaddr *addr, socklen_t addrlen) | ||
{ | ||
return bind(sd, addr, addrlen); | ||
} | ||
|
||
static int mctp_op_setsockopt(int sd, int level, int optname, void *optval, | ||
socklen_t optlen) | ||
{ | ||
return setsockopt(sd, level, optname, optval, optlen); | ||
} | ||
|
||
static ssize_t mctp_op_sendto(int sd, const void *buf, size_t len, int flags, | ||
const struct sockaddr *dest, socklen_t addrlen) | ||
{ | ||
return sendto(sd, buf, len, flags, dest, addrlen); | ||
} | ||
|
||
static ssize_t mctp_op_recvfrom(int sd, void *buf, size_t len, int flags, | ||
struct sockaddr *src, socklen_t *addrlen) | ||
{ | ||
return recvfrom(sd, buf, len, flags, src, addrlen); | ||
} | ||
|
||
static int mctp_op_close(int sd) | ||
{ | ||
return close(sd); | ||
} | ||
|
||
struct mctp_ops mctp_ops = { | ||
.mctp = { | ||
.socket = mctp_op_mctp_socket, | ||
.setsockopt = mctp_op_setsockopt, | ||
.bind = mctp_op_bind, | ||
.sendto = mctp_op_sendto, | ||
.recvfrom = mctp_op_recvfrom, | ||
.close = mctp_op_close, | ||
}, | ||
.nl = { | ||
.socket = mctp_op_netlink_socket, | ||
.setsockopt = mctp_op_setsockopt, | ||
.bind = mctp_op_bind, | ||
.sendto = mctp_op_sendto, | ||
.recvfrom = mctp_op_recvfrom, | ||
.close = mctp_op_close, | ||
}, | ||
}; | ||
|
||
void mctp_ops_init(void) { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
/* SPDX-License-Identifier: GPL-2.0 */ | ||
/* | ||
* mctpd: bus owner for MCTP using Linux kernel | ||
* | ||
* Copyright (c) 2023 Code Construct | ||
*/ | ||
#pragma once | ||
|
||
#include <sys/socket.h> | ||
|
||
#define _GNU_SOURCE | ||
|
||
struct socket_ops { | ||
int (*socket)(void); | ||
int (*setsockopt)(int sd, int level, int optname, void *optval, | ||
socklen_t optlen); | ||
int (*bind)(int sd, struct sockaddr *addr, socklen_t addrlen); | ||
ssize_t (*sendto)(int sd, const void *buf, size_t len, int flags, | ||
const struct sockaddr *dest, socklen_t addrlen); | ||
ssize_t (*recvfrom)(int sd, void *buf, size_t len, int flags, | ||
struct sockaddr *src, socklen_t *addrlen); | ||
int (*close)(int sd); | ||
}; | ||
|
||
struct mctp_ops { | ||
struct socket_ops mctp; | ||
struct socket_ops nl; | ||
}; | ||
|
||
extern struct mctp_ops mctp_ops; | ||
|
||
void mctp_ops_init(void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters