Commit ef11b762 authored by Alessandro Rubini's avatar Alessandro Rubini

overall: use network operations

Instead of having several external functions for networking, only
have one symbol: the structure of network operations. This simplifies
stuff a while, and allows more static symbols and better encapsulation.
Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent 40eb407a
......@@ -5,12 +5,6 @@
/*
* These are the functions provided by the various bare files
*/
extern int bare_open_ch(struct pp_instance *ppi, char *name);
extern int bare_recv_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t);
extern int bare_send_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t, int chtype, int use_pdelay_addr);
extern void bare_main_loop(struct pp_instance *ppi);
......
......@@ -11,8 +11,8 @@
Octet buffer_out[PP_PACKET_SIZE + 14];
/* FIXME: which socket we receive and send with? */
int bare_recv_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t)
static int bare_net_recv(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t)
{
if (t)
pp_get_tstamp(t);
......@@ -21,8 +21,8 @@ int bare_recv_packet(struct pp_instance *ppi, void *pkt, int len,
pkt - NP(ppi)->proto_ofst, len, 0);
}
int bare_send_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t, int chtype, int use_pdelay_addr)
static int bare_net_send(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t, int chtype, int use_pdelay_addr)
{
struct bare_ethhdr *hdr;
hdr = PROTO_HDR(pkt);
......@@ -43,12 +43,6 @@ int bare_send_packet(struct pp_instance *ppi, void *pkt, int len,
len + NP(ppi)->proto_ofst, 0);
}
int pp_recv_packet(struct pp_instance *ppi, void *pkt, int len, TimeInternal *t)
__attribute__((alias("bare_recv_packet")));
int pp_send_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t, int chtype, int use_pdelay_addr)
__attribute__((alias("bare_send_packet")));
#define SHUT_RD 0
#define SHUT_WR 1
#define SHUT_RDWR 2
......@@ -57,7 +51,7 @@ int pp_send_packet(struct pp_instance *ppi, void *pkt, int len,
#define SOCK_RAW 3
/* To open a channel we must bind to an interface and so on */
int bare_open_ch(struct pp_instance *ppi, char *ifname)
static int bare_open_ch(struct pp_instance *ppi, char *ifname)
{
int sock = -1;
int temp, iindex;
......@@ -137,7 +131,7 @@ int bare_open_ch(struct pp_instance *ppi, char *ifname)
return -1;
}
int bare_net_init(struct pp_instance *ppi)
static int bare_net_init(struct pp_instance *ppi)
{
ppi->buf_out = buffer_out;
ppi->buf_out = PROTO_PAYLOAD(ppi->buf_out);
......@@ -154,12 +148,16 @@ int bare_net_init(struct pp_instance *ppi)
return 0;
}
int pp_net_init(struct pp_instance *ppi)
__attribute__((alias("bare_net_init")));
int bare_net_shutdown(struct pp_instance *ppi)
static int bare_net_exit(struct pp_instance *ppi)
{
return sys_shutdown(NP(ppi)->ch[PP_NP_GEN].fd, SHUT_RDWR);
}
int pp_net_shutdown(struct pp_instance *ppi)
__attribute__((alias("bare_net_shutdown")));
struct pp_network_operations pp_net_ops = {
.init = bare_net_init,
.exit = bare_net_exit,
.recv = bare_net_recv,
.send = bare_net_send,
};
......@@ -51,15 +51,11 @@ void ppsi_main(void)
ppi->frgn_master = &frgn_master;
ppi->arch_data = NULL;
#if 0
if (bare_open_ch(ppi, "eth0")) {
pp_diag_error(ppi, bare_errno);
pp_diag_fatal(ppi, "open_ch", "");
}
#endif
/* This just llocates the stuff */
pp_open_instance(ppi, NULL);
OPTS(ppi)->iface_name = "eth0";
/* The actual sockets are opened in state-initializing */
bare_main_loop(ppi);
}
......@@ -66,8 +66,8 @@ void bare_main_loop(struct pp_instance *ppi)
*
* FIXME: we don't know which socket to receive from
*/
i = bare_recv_packet(ppi, payload, sizeof(packet),
&ppi->last_rcv_time);
i = pp_net_ops.recv(ppi, payload, sizeof(packet),
&ppi->last_rcv_time);
ppi->last_rcv_time.seconds += DSPRO(ppi)->currentUtcOffset;
/* we passed payload but it filled the ether header too */
......
......@@ -11,8 +11,8 @@
Octet buffer_out[PP_PACKET_SIZE + 14];
/* FIXME: which socket we receive and send with? */
int bare_recv_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t)
static int bare_net_recv(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t)
{
if (t)
pp_get_tstamp(t);
......@@ -21,8 +21,8 @@ int bare_recv_packet(struct pp_instance *ppi, void *pkt, int len,
pkt - NP(ppi)->proto_ofst, len, 0);
}
int bare_send_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t, int chtype, int use_pdelay_addr)
static int bare_net_send(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t, int chtype, int use_pdelay_addr)
{
struct bare_ethhdr *hdr;
hdr = PROTO_HDR(pkt);
......@@ -43,12 +43,6 @@ int bare_send_packet(struct pp_instance *ppi, void *pkt, int len,
len + NP(ppi)->proto_ofst, 0);
}
int pp_recv_packet(struct pp_instance *ppi, void *pkt, int len, TimeInternal *t)
__attribute__((alias("bare_recv_packet")));
int pp_send_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t, int chtype, int use_pdelay_addr)
__attribute__((alias("bare_send_packet")));
#define SHUT_RD 0
#define SHUT_WR 1
#define SHUT_RDWR 2
......@@ -57,7 +51,7 @@ int pp_send_packet(struct pp_instance *ppi, void *pkt, int len,
#define SOCK_RAW 3
/* To open a channel we must bind to an interface and so on */
int bare_open_ch(struct pp_instance *ppi, char *ifname)
static int bare_open_ch(struct pp_instance *ppi, char *ifname)
{
int sock = -1;
int temp, iindex;
......@@ -137,7 +131,7 @@ int bare_open_ch(struct pp_instance *ppi, char *ifname)
return -1;
}
int bare_net_init(struct pp_instance *ppi)
static int bare_net_init(struct pp_instance *ppi)
{
ppi->buf_out = buffer_out;
ppi->buf_out = PROTO_PAYLOAD(ppi->buf_out);
......@@ -154,12 +148,15 @@ int bare_net_init(struct pp_instance *ppi)
return 0;
}
int pp_net_init(struct pp_instance *ppi)
__attribute__((alias("bare_net_init")));
int bare_net_shutdown(struct pp_instance *ppi)
static int bare_net_exit(struct pp_instance *ppi)
{
return sys_shutdown(NP(ppi)->ch[PP_NP_GEN].fd, SHUT_RDWR);
}
int pp_net_shutdown(struct pp_instance *ppi)
__attribute__((alias("bare_net_shutdown")));
struct pp_network_operations pp_net_ops = {
.init = bare_net_init,
.exit = bare_net_exit,
.recv = bare_net_recv,
.send = bare_net_send,
};
......@@ -50,15 +50,12 @@ void ppsi_main(void)
ppi->servo = &servo;
ppi->frgn_master = &frgn_master;
ppi->arch_data = NULL;
#if 0
if (bare_open_ch(ppi, "eth0")) {
pp_diag_error(ppi, bare_errno);
pp_diag_fatal(ppi, "open_ch", "");
}
#endif
/* This just llocates the stuff */
pp_open_instance(ppi, NULL);
OPTS(ppi)->iface_name = "eth0";
/* The actual sockets are opened in state-initializing */
bare_main_loop(ppi);
}
......@@ -6,13 +6,6 @@
/*
* These are the functions provided by the various bare files
*/
extern int bare_open_ch(struct pp_instance *ppi, char *name);
extern int bare_recv_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t);
extern int bare_send_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t, int chtype, int use_pdelay_addr);
extern void bare_main_loop(struct pp_instance *ppi);
/* syscalls */
......
......@@ -66,8 +66,8 @@ void bare_main_loop(struct pp_instance *ppi)
*
* FIXME: we don't know which socket to receive from
*/
i = bare_recv_packet(ppi, payload, sizeof(packet) - 16,
&ppi->last_rcv_time);
i = pp_net_ops.recv(ppi, payload, sizeof(packet) - 16,
&ppi->last_rcv_time);
ppi->last_rcv_time.seconds += DSPRO(ppi)->currentUtcOffset;
/* we passed payload but it filled the ether header too */
......
......@@ -50,7 +50,7 @@ void posix_main_loop(struct pp_instance *ppi)
* We got a packet. If it's not ours, continue consuming
* the pending timeout
*/
i = posix_recv_packet(ppi, payload, sizeof(packet) - 16,
i = pp_net_ops.recv(ppi, payload, sizeof(packet) - 16,
&ppi->last_rcv_time);
ppi->last_rcv_time.seconds += DSPRO(ppi)->currentUtcOffset;
......
......@@ -21,7 +21,7 @@
#include "posix.h"
/* posix_recv_msg uses recvmsg for timestamp query */
int posix_recv_msg(int fd, void *pkt, int len, TimeInternal *t)
static int posix_recv_msg(int fd, void *pkt, int len, TimeInternal *t)
{
ssize_t ret;
struct msghdr msg;
......@@ -89,8 +89,8 @@ int posix_recv_msg(int fd, void *pkt, int len, TimeInternal *t)
}
/* Receive and send is *not* so trivial */
int posix_recv_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t)
static int posix_net_recv(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t)
{
struct pp_channel *ch1 = NULL, *ch2 = NULL;
void *hdr;
......@@ -124,8 +124,8 @@ int posix_recv_packet(struct pp_instance *ppi, void *pkt, int len,
return -1;
}
int posix_send_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t, int chtype, int use_pdelay_addr)
static int posix_net_send(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t, int chtype, int use_pdelay_addr)
{
struct sockaddr_in addr;
struct ethhdr *hdr;
......@@ -163,14 +163,8 @@ int posix_send_packet(struct pp_instance *ppi, void *pkt, int len,
return -1;
}
int pp_recv_packet(struct pp_instance *ppi, void *pkt, int len, TimeInternal *t)
__attribute__((alias("posix_recv_packet")));
int pp_send_packet(struct pp_instance *ppi, void *pkt, int len, TimeInternal *t,
int chtype, int use_pdelay_addr)
__attribute__((alias("posix_send_packet")));
/* To open a channel we must bind to an interface and so on */
int posix_open_ch(struct pp_instance *ppi, char *ifname, int chtype)
static int posix_open_ch(struct pp_instance *ppi, char *ifname, int chtype)
{
int sock = -1;
......@@ -409,14 +403,10 @@ int posix_net_init(struct pp_instance *ppi)
return 0;
}
int pp_net_init(struct pp_instance *ppi)
__attribute__((alias("posix_net_init")));
/*
* Shutdown all the network stuff
*/
int posix_net_shutdown(struct pp_instance *ppi)
static int posix_net_exit(struct pp_instance *ppi)
{
struct ip_mreq imr;
int fd;
......@@ -527,5 +517,9 @@ int posix_net_check_pkt(struct pp_instance *ppi, int delay_ms)
return ret;
}
int pp_net_shutdown(struct pp_instance *ppi)
__attribute__((alias("posix_net_shutdown")));
struct pp_network_operations pp_net_ops = {
.init = posix_net_init,
.exit = posix_net_exit,
.recv = posix_net_recv,
.send = posix_net_send,
};
......@@ -12,14 +12,6 @@ struct posix_arch_data {
int rcv_switch; /* flag for event / general receive order */
};
extern int posix_net_init(struct pp_instance *ppi);
extern int posix_net_check_pkt(struct pp_instance *ppi, int delay_ms);
extern int posix_open_ch(struct pp_instance *ppi, char *name, int chtype);
extern int posix_recv_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t);
extern int posix_send_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t, int chtype, int use_pdelay_addr);
extern void posix_main_loop(struct pp_instance *ppi);
......@@ -15,7 +15,7 @@ int wrpc_errno;
Octet buffer_out[PP_PACKET_SIZE + 14]; /* 14 == ppi->proto_ofst for eth mode */
/* This function should init the minic and get the mac address */
int wrpc_open_ch(struct pp_instance *ppi)
static int wrpc_open_ch(struct pp_instance *ppi)
{
wr_socket_t *sock;
mac_addr_t mac;
......@@ -39,8 +39,8 @@ int wrpc_open_ch(struct pp_instance *ppi)
}
/* To receive and send packets, we call the minic low-level stuff */
int wrpc_recv_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t)
static int wrpc_net_recv(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t)
{
int got;
wr_socket_t *sock;
......@@ -63,8 +63,8 @@ int wrpc_recv_packet(struct pp_instance *ppi, void *pkt, int len,
return got;
}
int wrpc_send_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t, int chtype, int use_pdelay_addr)
static int wrpc_net_send(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t, int chtype, int use_pdelay_addr)
{
int snt;
wr_socket_t *sock;
......@@ -101,7 +101,7 @@ int wrpc_send_packet(struct pp_instance *ppi, void *pkt, int len,
return snt;
}
int wrpc_net_init(struct pp_instance *ppi)
static int wrpc_net_init(struct pp_instance *ppi)
{
ppi->buf_out = buffer_out;
ppi->buf_out = PROTO_PAYLOAD(ppi->buf_out);
......@@ -113,19 +113,16 @@ int wrpc_net_init(struct pp_instance *ppi)
}
int wrpc_net_shutdown(struct pp_instance *ppi)
static int wrpc_net_exit(struct pp_instance *ppi)
{
ptpd_netif_close_socket(
(wr_socket_t *)NP(ppi)->ch[PP_NP_EVT].custom);
return 0;
}
int pp_net_init(struct pp_instance *ppi)
__attribute__((alias("wrpc_net_init")));
int pp_net_shutdown(struct pp_instance *ppi)
__attribute__((alias("wrpc_net_shutdown")));
int pp_recv_packet(struct pp_instance *ppi, void *pkt, int len, TimeInternal *t)
__attribute__((alias("wrpc_recv_packet")));
int pp_send_packet(struct pp_instance *ppi, void *pkt, int len, TimeInternal *t,
int chtype, int use_pdelay_addr)
__attribute__((alias("wrpc_send_packet")));
struct pp_network_operations pp_net_ops = {
.init = wrpc_net_init,
.exit = wrpc_net_exit,
.recv = wrpc_net_recv,
.send = wrpc_net_send,
};
......@@ -11,13 +11,6 @@
/*
* These are the functions provided by the various bare files
*/
extern int wrpc_open_ch(struct pp_instance *ppi);
extern int wrpc_recv_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t);
extern int wrpc_send_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t, int chtype, int use_pdelay_addr);
extern void wrpc_main_loop(struct pp_instance *ppi);
extern void _irq_entry(void); /* unused, to make crt0.S happy */
extern int main(void); /* alias to ppsi_main, so crt0.S is happy */
......
......@@ -327,6 +327,23 @@ struct pp_ext_hooks {
extern struct pp_ext_hooks pp_hooks; /* The one for the extension we build */
/*
* Network methods are encapsulated in a structure, so each arch only needs
* to provide that structure. This should simplify management overall.
*/
struct pp_network_operations {
int (*init)(struct pp_instance *ppi);
int (*exit)(struct pp_instance *ppi);
int (*recv)(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t);
/* chtype here is PP_NP_GEN or PP_NP_EVT */
int (*send)(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t, int chtype, int use_pdelay_addr);
};
extern struct pp_network_operations pp_net_ops;
/* The channel for an instance must be created and possibly destroyed. */
extern int pp_open_instance(struct pp_instance *ppi,
struct pp_runtime_opts *rt_opts);
......@@ -335,15 +352,6 @@ extern int pp_close_instance(struct pp_instance *ppi);
extern int pp_parse_cmdline(struct pp_instance *ppi, int argc, char **argv);
/* Network stuff */
extern int pp_net_init(struct pp_instance *ppi);
extern int pp_net_shutdown(struct pp_instance *ppi);
extern int pp_recv_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t);
extern int pp_send_packet(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t, int chtype, int use_pdelay_addr);
/* chtype: PP_NP_GEN || PP_NP_EVT */
/* Timers */
extern int pp_timer_init(struct pp_instance *ppi); /* initializes timer common
structure */
......
......@@ -41,7 +41,7 @@ int st_com_handle_pdelay_req(struct pp_instance *ppi, unsigned char *buf,
#ifdef VERB_LOG_MSGS
#define MSG_SEND_AND_RET_VARLEN(x, y, z, w) \
if (pp_send_packet(ppi, ppi->buf_out, w,\
if (pp_net_ops.send(ppi, ppi->buf_out, w,\
&ppi->last_snt_time, PP_NP_##y , z) < w) { \
PP_PRINTF("%s(%d) Message can't be sent -> FAULTY state!\n", \
pp_msg_names[PPM_##x], PPM_##x); \
......@@ -54,7 +54,7 @@ int st_com_handle_pdelay_req(struct pp_instance *ppi, unsigned char *buf,
return 0;
#else
#define MSG_SEND_AND_RET_VARLEN(x, y, z, w) \
if (pp_send_packet(ppi, ppi->buf_out, w, \
if (pp_net_ops.send(ppi, ppi->buf_out, w, \
&ppi->last_snt_time, PP_NP_##y , z) < w) { \
return -1; \
} \
......
......@@ -19,9 +19,9 @@ int pp_initializing(struct pp_instance *ppi, unsigned char *pkt, int plen)
int ret = 0;
if (NP(ppi)->inited)
pp_net_shutdown(ppi);
pp_net_ops.exit(ppi);
if (pp_net_init(ppi) < 0)
if (pp_net_ops.init(ppi) < 0)
goto failure;
NP(ppi)->inited = 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment