#ifdef ICL_KERNEL_PROXY
#include <sys/param.h>
#include <sys/capsicum.h>
#include <sys/condvar.h>
#include <sys/conf.h>
#include <sys/lock.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sx.h>
#include <sys/systm.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <dev/iscsi/icl.h>
struct icl_listen_sock {
TAILQ_ENTRY(icl_listen_sock) ils_next;
struct icl_listen *ils_listen;
struct socket *ils_socket;
bool ils_running;
int ils_id;
};
struct icl_listen {
TAILQ_HEAD(, icl_listen_sock) il_sockets;
struct sx il_lock;
void (*il_accept)(struct socket *,
struct sockaddr *, int);
};
static MALLOC_DEFINE(M_ICL_PROXY, "ICL_PROXY", "iSCSI common layer proxy");
int
icl_soft_proxy_connect(struct icl_conn *ic, int domain, int socktype,
int protocol, struct sockaddr *from_sa, struct sockaddr *to_sa)
{
struct socket *so;
int error;
error = socreate(domain, &so, socktype, protocol,
curthread->td_ucred, curthread);
if (error != 0)
return (error);
if (from_sa != NULL) {
error = sobind(so, from_sa, curthread);
if (error != 0) {
soclose(so);
return (error);
}
}
error = soconnect(so, to_sa, curthread);
if (error != 0) {
soclose(so);
return (error);
}
SOCK_LOCK(so);
while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
"icl_connect", 0);
if (error)
break;
}
if (error == 0) {
error = so->so_error;
so->so_error = 0;
}
SOCK_UNLOCK(so);
if (error != 0) {
soclose(so);
return (error);
}
error = icl_soft_handoff_sock(ic, so);
if (error != 0)
soclose(so);
return (error);
}
struct icl_listen *
icl_listen_new(void (*accept_cb)(struct socket *, struct sockaddr *, int))
{
struct icl_listen *il;
il = malloc(sizeof(*il), M_ICL_PROXY, M_ZERO | M_WAITOK);
TAILQ_INIT(&il->il_sockets);
sx_init(&il->il_lock, "icl_listen");
il->il_accept = accept_cb;
return (il);
}
void
icl_listen_free(struct icl_listen *il)
{
struct icl_listen_sock *ils;
sbintime_t sbt, pr;
sx_xlock(&il->il_lock);
while (!TAILQ_EMPTY(&il->il_sockets)) {
ils = TAILQ_FIRST(&il->il_sockets);
while (ils->ils_running) {
ICL_DEBUG("waiting for accept thread to terminate");
sx_xunlock(&il->il_lock);
SOLISTEN_LOCK(ils->ils_socket);
ils->ils_socket->so_error = ENOTCONN;
SOLISTEN_UNLOCK(ils->ils_socket);
wakeup(&ils->ils_socket->so_timeo);
sbt = mstosbt(995);
pr = mstosbt(10);
pause_sbt("icl_unlisten", sbt, pr, 0);
sx_xlock(&il->il_lock);
}
TAILQ_REMOVE(&il->il_sockets, ils, ils_next);
soclose(ils->ils_socket);
free(ils, M_ICL_PROXY);
}
sx_xunlock(&il->il_lock);
free(il, M_ICL_PROXY);
}
static void
icl_accept_thread(void *arg)
{
struct icl_listen_sock *ils;
struct socket *head, *so;
struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
int error;
ils = arg;
head = ils->ils_socket;
ils->ils_running = true;
for (;;) {
SOLISTEN_LOCK(head);
error = solisten_dequeue(head, &so, 0);
if (error == ENOTCONN) {
ICL_DEBUG("terminating");
ils->ils_running = false;
kthread_exit();
return;
}
if (error) {
ICL_WARN("solisten_dequeue error %d", error);
continue;
}
error = soaccept(so, (struct sockaddr *)&ss);
if (error != 0) {
ICL_WARN("soaccept error %d", error);
soclose(so);
continue;
}
(ils->ils_listen->il_accept)(so, (struct sockaddr *)&ss,
ils->ils_id);
}
}
static int
icl_listen_add_tcp(struct icl_listen *il, int domain, int socktype,
int protocol, struct sockaddr *sa, int portal_id)
{
struct icl_listen_sock *ils;
struct socket *so;
struct sockopt sopt;
int error, one = 1;
error = socreate(domain, &so, socktype, protocol,
curthread->td_ucred, curthread);
if (error != 0) {
ICL_WARN("socreate failed with error %d", error);
return (error);
}
sopt.sopt_dir = SOPT_SET;
sopt.sopt_level = SOL_SOCKET;
sopt.sopt_name = SO_REUSEADDR;
sopt.sopt_val = &one;
sopt.sopt_valsize = sizeof(one);
sopt.sopt_td = NULL;
error = sosetopt(so, &sopt);
if (error != 0) {
ICL_WARN("failed to set SO_REUSEADDR with error %d", error);
soclose(so);
return (error);
}
error = sobind(so, sa, curthread);
if (error != 0) {
ICL_WARN("sobind failed with error %d", error);
soclose(so);
return (error);
}
error = solisten(so, -1, curthread);
if (error != 0) {
ICL_WARN("solisten failed with error %d", error);
soclose(so);
return (error);
}
ils = malloc(sizeof(*ils), M_ICL_PROXY, M_ZERO | M_WAITOK);
ils->ils_listen = il;
ils->ils_socket = so;
ils->ils_id = portal_id;
error = kthread_add(icl_accept_thread, ils, NULL, NULL, 0, 0, "iclacc");
if (error != 0) {
ICL_WARN("kthread_add failed with error %d", error);
soclose(so);
free(ils, M_ICL_PROXY);
return (error);
}
sx_xlock(&il->il_lock);
TAILQ_INSERT_TAIL(&il->il_sockets, ils, ils_next);
sx_xunlock(&il->il_lock);
return (0);
}
int
icl_listen_add(struct icl_listen *il, bool rdma, int domain, int socktype,
int protocol, struct sockaddr *sa, int portal_id)
{
if (rdma) {
ICL_DEBUG("RDMA not supported");
return (EOPNOTSUPP);
}
return (icl_listen_add_tcp(il, domain, socktype, protocol, sa,
portal_id));
}
int
icl_listen_remove(struct icl_listen *il, struct sockaddr *sa)
{
return (EOPNOTSUPP);
}
#endif