mars/kernel/mars_client.c

791 lines
20 KiB
C
Raw Normal View History

2011-02-23 20:48:06 +00:00
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
// Client brick (just for demonstration)
//#define BRICK_DEBUGGING
//#define MARS_DEBUGGING
//#define IO_DEBUGGING
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#include "mars.h"
///////////////////////// own type definitions ////////////////////////
#include "mars_client.h"
#define CLIENT_HASH_MAX (PAGE_SIZE / sizeof(struct list_head))
2011-02-23 20:48:06 +00:00
///////////////////////// own helper functions ////////////////////////
static int thread_count = 0;
static void _kill_thread(struct client_threadinfo *ti, const char *name)
2011-02-23 20:48:06 +00:00
{
2011-08-31 11:42:04 +00:00
if (ti->thread) {
MARS_DBG("stopping %s thread\n", name);
brick_thread_stop(ti->thread);
ti->thread = NULL;
2011-02-23 20:48:06 +00:00
}
}
2011-08-31 11:42:04 +00:00
static void _kill_socket(struct client_output *output)
{
output->brick->connection_state = 1;
if (mars_socket_is_alive(&output->socket)) {
2011-08-31 11:42:04 +00:00
MARS_DBG("shutdown socket\n");
mars_shutdown_socket(&output->socket);
2011-08-31 11:42:04 +00:00
}
_kill_thread(&output->receiver, "receiver");
output->recv_error = 0;
MARS_DBG("close socket\n");
mars_put_socket(&output->socket);
2011-08-31 11:42:04 +00:00
}
2011-03-07 05:55:10 +00:00
static int _request_info(struct client_output *output)
{
struct mars_cmd cmd = {
.cmd_code = CMD_GETINFO,
};
int status;
MARS_DBG("\n");
status = mars_send_struct(&output->socket, &cmd, mars_cmd_meta);
2011-03-07 05:55:10 +00:00
if (unlikely(status < 0)) {
MARS_DBG("send of getinfo failed, status = %d\n", status);
}
return status;
}
2011-08-31 11:42:04 +00:00
static int receiver_thread(void *data);
2011-02-23 20:48:06 +00:00
static int _connect(struct client_output *output, const char *str)
{
struct sockaddr_storage sockaddr = {};
int status;
2011-08-31 11:42:04 +00:00
if (unlikely(!output->path)) {
2011-08-12 11:09:48 +00:00
output->path = brick_strdup(str);
2011-02-27 14:17:58 +00:00
status = -ENOMEM;
2011-03-04 15:14:20 +00:00
if (!output->path) {
MARS_DBG("no mem\n");
2011-02-23 20:48:06 +00:00
goto done;
2011-03-04 15:14:20 +00:00
}
2011-02-27 14:17:58 +00:00
status = -EINVAL;
2011-03-03 09:02:10 +00:00
output->host = strchr(output->path, '@');
if (!output->host) {
2011-08-12 11:09:48 +00:00
brick_string_free(output->path);
2011-03-03 09:02:10 +00:00
output->path = NULL;
MARS_ERR("parameter string '%s' contains no remote specifier with '@'-syntax\n", str);
2011-02-23 20:48:06 +00:00
goto done;
}
2011-03-03 09:02:10 +00:00
*output->host++ = '\0';
2011-02-23 20:48:06 +00:00
}
if (unlikely(output->receiver.thread)) {
MARS_WRN("receiver thread unexpectedly not dead\n");
_kill_thread(&output->receiver, "receiver");
}
2011-02-23 20:48:06 +00:00
status = mars_create_sockaddr(&sockaddr, output->host);
2011-03-04 15:14:20 +00:00
if (unlikely(status < 0)) {
MARS_DBG("no sockaddr, status = %d\n", status);
2011-02-23 20:48:06 +00:00
goto done;
2011-03-04 15:14:20 +00:00
}
2011-02-23 20:48:06 +00:00
status = mars_create_socket(&output->socket, &sockaddr, false);
if (unlikely(status < 0)) {
2011-03-04 15:14:20 +00:00
MARS_DBG("no socket, status = %d\n", status);
goto really_done;
2011-02-23 20:48:06 +00:00
}
output->socket.s_shutdown_on_err = true;
2011-02-23 20:48:06 +00:00
output->receiver.thread = brick_thread_create(receiver_thread, output, "mars_receiver%d", thread_count++);
if (unlikely(!output->receiver.thread)) {
2011-08-31 11:42:04 +00:00
MARS_ERR("cannot start receiver thread, status = %d\n", status);
status = -ENOENT;
2011-08-31 11:42:04 +00:00
goto done;
}
2011-02-23 20:48:06 +00:00
{
struct mars_cmd cmd = {
.cmd_code = CMD_CONNECT,
.cmd_str1 = output->path,
};
status = mars_send_struct(&output->socket, &cmd, mars_cmd_meta);
2011-03-04 15:14:20 +00:00
if (unlikely(status < 0)) {
MARS_DBG("send of connect failed, status = %d\n", status);
2011-02-23 20:48:06 +00:00
goto done;
2011-03-04 15:14:20 +00:00
}
2011-02-23 20:48:06 +00:00
}
if (status >= 0) {
2011-03-07 05:55:10 +00:00
status = _request_info(output);
2011-02-23 20:48:06 +00:00
}
done:
if (status < 0) {
MARS_INF("cannot connect to remote host '%s' (status = %d) -- retrying\n", output->host ? output->host : "NULL", status);
_kill_socket(output);
}
2011-03-04 15:14:20 +00:00
really_done:
2011-02-23 20:48:06 +00:00
return status;
}
////////////////// own brick / input / output operations //////////////////
static int client_get_info(struct client_output *output, struct mars_info *info)
{
int status;
2011-03-07 05:55:10 +00:00
output->got_info = false;
output->get_info = true;
wake_up_interruptible(&output->event);
2011-02-23 20:48:06 +00:00
wait_event_interruptible_timeout(output->info_event, output->got_info, 60 * HZ);
status = -EIO;
if (output->got_info && info) {
memcpy(info, &output->info, sizeof(*info));
status = 0;
}
//done:
return status;
}
static int client_ref_get(struct client_output *output, struct mref_object *mref)
{
int maxlen;
if (mref->ref_initialized) {
_mref_get(mref);
return mref->ref_len;
}
2011-03-22 14:36:26 +00:00
#if 1
2011-02-23 20:48:06 +00:00
/* Limit transfers to page boundaries.
* Currently, this is more restrictive than necessary.
* TODO: improve performance by doing better when possible.
* This needs help from the server in some efficient way.
*/
maxlen = PAGE_SIZE - (mref->ref_pos & (PAGE_SIZE-1));
2011-02-23 20:48:06 +00:00
if (mref->ref_len > maxlen)
mref->ref_len = maxlen;
2011-03-22 14:36:26 +00:00
#endif
2011-02-23 20:48:06 +00:00
if (!mref->ref_data) { // buffered IO
struct client_mref_aspect *mref_a = client_mref_get_aspect(output->brick, mref);
2011-02-23 20:48:06 +00:00
if (!mref_a)
return -EILSEQ;
mref->ref_data = brick_block_alloc(mref->ref_pos, (mref_a->alloc_len = mref->ref_len));
2011-02-23 20:48:06 +00:00
if (!mref->ref_data)
return -ENOMEM;
mref_a->do_dealloc = true;
mref->ref_flags = 0;
}
_mref_get_first(mref);
2011-02-23 20:48:06 +00:00
return 0;
}
static void client_ref_put(struct client_output *output, struct mref_object *mref)
{
struct client_mref_aspect *mref_a;
if (!_mref_put(mref))
2011-02-23 20:48:06 +00:00
return;
mref_a = client_mref_get_aspect(output->brick, mref);
2011-02-23 20:48:06 +00:00
if (mref_a && mref_a->do_dealloc) {
brick_block_free(mref->ref_data, mref_a->alloc_len);
2011-02-23 20:48:06 +00:00
}
client_free_mref(mref);
}
2012-12-19 11:40:52 +00:00
static
void _hash_insert(struct client_output *output, struct client_mref_aspect *mref_a)
{
struct mref_object *mref = mref_a->object;
unsigned long flags;
int hash_index;
traced_lock(&output->lock, flags);
list_del(&mref_a->io_head);
list_add_tail(&mref_a->io_head, &output->mref_list);
list_del(&mref_a->hash_head);
mref->ref_id = ++output->last_id;
hash_index = mref->ref_id % CLIENT_HASH_MAX;
list_add_tail(&mref_a->hash_head, &output->hash_table[hash_index]);
traced_unlock(&output->lock, flags);
}
2011-02-23 20:48:06 +00:00
static void client_ref_io(struct client_output *output, struct mref_object *mref)
{
struct client_mref_aspect *mref_a;
int error = -EINVAL;
mref_a = client_mref_get_aspect(output->brick, mref);
2011-02-23 20:48:06 +00:00
if (unlikely(!mref_a)) {
goto error;
}
2011-03-29 14:40:40 +00:00
while (output->brick->max_flying > 0 && atomic_read(&output->fly_count) > output->brick->max_flying) {
2011-03-30 12:02:50 +00:00
MARS_IO("sleeping request pos = %lld len = %d rw = %d (flying = %d)\n", mref->ref_pos, mref->ref_len, mref->ref_rw, atomic_read(&output->fly_count));
#ifdef IO_DEBUGGING
brick_msleep(3000);
2011-03-30 12:02:50 +00:00
#else
brick_msleep(1000 * 2 / HZ);
2011-03-30 12:02:50 +00:00
#endif
2011-03-29 14:40:40 +00:00
}
atomic_inc(&mars_global_io_flying);
2011-03-29 14:40:40 +00:00
atomic_inc(&output->fly_count);
_mref_get(mref);
2011-02-23 20:48:06 +00:00
2012-01-23 12:39:08 +00:00
mref_a->submit_jiffies = jiffies;
2012-12-19 11:40:52 +00:00
_hash_insert(output, mref_a);
2011-03-30 12:02:50 +00:00
MARS_IO("added request id = %d pos = %lld len = %d rw = %d (flying = %d)\n", mref->ref_id, mref->ref_pos, mref->ref_len, mref->ref_rw, atomic_read(&output->fly_count));
2011-02-23 20:48:06 +00:00
wake_up_interruptible(&output->event);
return;
error:
MARS_ERR("IO error = %d\n", error);
SIMPLE_CALLBACK(mref, error);
2011-02-23 20:48:06 +00:00
client_ref_put(output, mref);
}
2011-05-26 14:32:32 +00:00
static
int receiver_thread(void *data)
2011-02-23 20:48:06 +00:00
{
struct client_output *output = data;
int status = 0;
while (!brick_thread_should_stop()) {
2011-02-23 20:48:06 +00:00
struct mars_cmd cmd = {};
struct list_head *tmp;
struct client_mref_aspect *mref_a = NULL;
struct mref_object *mref = NULL;
unsigned long flags;
status = mars_recv_struct(&output->socket, &cmd, mars_cmd_meta);
2011-03-30 12:02:50 +00:00
MARS_IO("got cmd = %d status = %d\n", cmd.cmd_code, status);
2011-02-23 20:48:06 +00:00
if (status < 0)
goto done;
2012-08-01 10:09:49 +00:00
switch (cmd.cmd_code & CMD_FLAG_MASK) {
2012-01-17 14:37:14 +00:00
case CMD_NOTIFY:
mars_trigger();
break;
2011-02-23 20:48:06 +00:00
case CMD_CONNECT:
if (cmd.cmd_int1 < 0) {
status = cmd.cmd_int1;
2011-02-27 14:17:58 +00:00
MARS_ERR("at remote side: brick connect failed, remote status = %d\n", status);
2011-02-23 20:48:06 +00:00
goto done;
}
break;
case CMD_CB:
2011-03-30 12:02:50 +00:00
{
2011-05-19 11:36:00 +00:00
int hash_index = cmd.cmd_int1 % CLIENT_HASH_MAX;
2012-12-19 11:40:52 +00:00
traced_lock(&output->lock, flags);
2011-03-30 12:02:50 +00:00
for (tmp = output->hash_table[hash_index].next; tmp != &output->hash_table[hash_index]; tmp = tmp->next) {
2011-07-01 14:07:56 +00:00
struct mref_object *tmp_mref;
2011-03-30 12:02:50 +00:00
mref_a = container_of(tmp, struct client_mref_aspect, hash_head);
2011-07-01 14:07:56 +00:00
tmp_mref = mref_a->object;
if (unlikely(!tmp_mref)) {
2012-12-19 11:40:52 +00:00
traced_unlock(&output->lock, flags);
2011-07-01 14:07:56 +00:00
MARS_ERR("bad internal mref pointer\n");
status = -EBADR;
goto done;
}
if (tmp_mref->ref_id == cmd.cmd_int1) {
mref = tmp_mref;
2012-12-19 11:40:52 +00:00
list_del_init(&mref_a->hash_head);
list_del_init(&mref_a->io_head);
2011-02-23 20:48:06 +00:00
break;
}
}
2012-12-19 11:40:52 +00:00
traced_unlock(&output->lock, flags);
2011-02-23 20:48:06 +00:00
2012-12-19 11:40:52 +00:00
if (unlikely(!mref)) {
MARS_WRN("got unknown id = %d for callback\n", cmd.cmd_int1);
2011-02-23 20:48:06 +00:00
status = -EBADR;
goto done;
}
2011-03-30 12:02:50 +00:00
MARS_IO("got callback id = %d, old pos = %lld len = %d rw = %d\n", mref->ref_id, mref->ref_pos, mref->ref_len, mref->ref_rw);
2012-08-01 10:09:49 +00:00
status = mars_recv_cb(&output->socket, mref, &cmd);
2011-03-30 12:02:50 +00:00
MARS_IO("new status = %d, pos = %lld len = %d rw = %d\n", status, mref->ref_pos, mref->ref_len, mref->ref_rw);
2012-12-19 11:40:52 +00:00
if (unlikely(status < 0)) {
MARS_WRN("interrupted data transfer during callback, status = %d\n", status);
2012-12-19 11:40:52 +00:00
_hash_insert(output, mref_a);
2011-02-23 20:48:06 +00:00
goto done;
}
SIMPLE_CALLBACK(mref, 0);
2011-02-23 20:48:06 +00:00
client_ref_put(output, mref);
atomic_dec(&output->fly_count);
atomic_dec(&mars_global_io_flying);
2011-02-23 20:48:06 +00:00
break;
2011-03-30 12:02:50 +00:00
}
2011-02-23 20:48:06 +00:00
case CMD_GETINFO:
status = mars_recv_struct(&output->socket, &output->info, mars_info_meta);
2011-02-23 20:48:06 +00:00
if (status < 0) {
MARS_WRN("got bad info from remote side, status = %d\n", status);
2011-02-23 20:48:06 +00:00
goto done;
}
output->got_info = true;
wake_up_interruptible(&output->info_event);
break;
default:
2011-02-27 14:17:58 +00:00
MARS_ERR("got bad command %d from remote side, terminating.\n", cmd.cmd_code);
2011-02-23 20:48:06 +00:00
status = -EBADR;
goto done;
}
2011-08-12 12:25:10 +00:00
done:
brick_string_free(cmd.cmd_str1);
if (unlikely(status < 0)) {
if (!output->recv_error) {
MARS_DBG("signalling status = %d\n", status);
output->recv_error = status;
}
wake_up_interruptible(&output->event);
brick_msleep(100);
}
2011-02-23 20:48:06 +00:00
}
2011-03-03 18:23:34 +00:00
if (status < 0) {
MARS_WRN("receiver thread terminated with status = %d, recv_error = %d\n", status, output->recv_error);
2011-03-03 18:23:34 +00:00
}
2011-08-31 11:42:04 +00:00
mars_shutdown_socket(&output->socket);
2011-03-03 18:23:34 +00:00
wake_up_interruptible(&output->receiver.run_event);
2011-02-23 20:48:06 +00:00
return status;
}
static
void _do_resubmit(struct client_output *output)
{
2012-12-19 11:40:52 +00:00
unsigned long flags;
traced_lock(&output->lock, flags);
if (!list_empty(&output->wait_list)) {
struct list_head *first = output->wait_list.next;
struct list_head *last = output->wait_list.prev;
struct list_head *old_start = output->mref_list.next;
#define list_connect __list_del // the original routine has a misleading name: in reality it is more general
list_connect(&output->mref_list, first);
list_connect(last, old_start);
INIT_LIST_HEAD(&output->wait_list);
MARS_IO("done re-submit %p %p\n", first, last);
}
2012-12-19 11:40:52 +00:00
traced_unlock(&output->lock, flags);
}
2012-01-23 12:39:08 +00:00
static
void _do_timeout(struct client_output *output, struct list_head *anchor, bool force)
{
struct client_brick *brick = output->brick;
2012-12-19 11:40:52 +00:00
struct list_head *tmp;
struct list_head *next;
LIST_HEAD(tmp_list);
2012-08-21 11:43:56 +00:00
int rounds = 0;
2012-12-19 11:40:52 +00:00
long io_timeout = brick->io_timeout;
unsigned long flags;
2012-08-21 11:43:56 +00:00
if (io_timeout <= 0)
io_timeout = global_net_io_timeout;
2012-12-19 11:40:52 +00:00
if (!mars_net_is_alive)
force = true;
if (!force && io_timeout <= 0)
return;
io_timeout *= HZ;
traced_lock(&output->lock, flags);
for (tmp = anchor->next, next = tmp->next; tmp != anchor; tmp = next, next = tmp->next) {
2012-01-23 12:39:08 +00:00
struct client_mref_aspect *mref_a;
mref_a = container_of(tmp, struct client_mref_aspect, io_head);
2012-12-19 11:40:52 +00:00
2012-08-21 11:43:56 +00:00
if (!force &&
2012-12-19 11:40:52 +00:00
!time_is_before_jiffies(mref_a->submit_jiffies + io_timeout)) {
continue;
2012-01-23 12:39:08 +00:00
}
2012-12-19 11:40:52 +00:00
list_del_init(&mref_a->hash_head);
list_del_init(&mref_a->io_head);
list_add_tail(&mref_a->tmp_head, &tmp_list);
}
traced_unlock(&output->lock, flags);
while (!list_empty(&tmp_list)) {
struct client_mref_aspect *mref_a;
struct mref_object *mref;
tmp = tmp_list.next;
list_del_init(tmp);
mref_a = container_of(tmp, struct client_mref_aspect, tmp_head);
mref = mref_a->object;
2012-01-23 12:39:08 +00:00
2012-08-21 11:43:56 +00:00
if (!rounds++) {
2012-12-19 11:40:52 +00:00
MARS_WRN("timeout after %ld: signalling IO error at pos = %lld len = %d\n",
2012-08-21 11:43:56 +00:00
io_timeout,
mref->ref_pos,
mref->ref_len);
}
2012-12-19 11:40:52 +00:00
2012-01-23 12:39:08 +00:00
atomic_inc(&output->timeout_count);
SIMPLE_CALLBACK(mref, -ENOTCONN);
2012-01-23 12:39:08 +00:00
client_ref_put(output, mref);
2012-01-23 12:39:08 +00:00
atomic_dec(&output->fly_count);
atomic_dec(&mars_global_io_flying);
2012-01-23 12:39:08 +00:00
}
}
2011-02-23 20:48:06 +00:00
static int sender_thread(void *data)
{
struct client_output *output = data;
struct client_brick *brick = output->brick;
unsigned long flags;
bool do_kill = false;
2011-02-23 20:48:06 +00:00
int status = 0;
2011-05-26 14:32:32 +00:00
output->receiver.restart_count = 0;
while (!brick_thread_should_stop()) {
2012-01-23 12:39:08 +00:00
struct list_head *tmp = NULL;
2011-02-23 20:48:06 +00:00
struct client_mref_aspect *mref_a;
2011-03-30 12:02:50 +00:00
struct mref_object *mref;
2011-02-23 20:48:06 +00:00
if (unlikely(output->recv_error != 0 || !mars_socket_is_alive(&output->socket))) {
MARS_DBG("recv_error = %d do_kill = %d\n", output->recv_error, do_kill);
if (do_kill) {
do_kill = false;
_kill_socket(output);
brick_msleep(3000);
}
2012-01-23 12:39:08 +00:00
2011-02-23 20:48:06 +00:00
status = _connect(output, brick->brick_name);
2011-03-30 12:02:50 +00:00
MARS_IO("connect status = %d\n", status);
2011-02-23 20:48:06 +00:00
if (unlikely(status < 0)) {
brick_msleep(3000);
2012-01-23 12:39:08 +00:00
_do_timeout(output, &output->wait_list, false);
_do_timeout(output, &output->mref_list, false);
2011-02-23 20:48:06 +00:00
continue;
}
brick->connection_state = 2;
do_kill = true;
2011-02-23 20:48:06 +00:00
/* Re-Submit any waiting requests
*/
2011-03-30 12:02:50 +00:00
MARS_IO("re-submit\n");
_do_resubmit(output);
2011-02-23 20:48:06 +00:00
}
2012-12-19 11:40:52 +00:00
wait_event_interruptible_timeout(output->event,
!list_empty(&output->mref_list) ||
output->get_info ||
output->recv_error != 0 ||
brick_thread_should_stop(),
1 * HZ);
if (unlikely(output->recv_error != 0)) {
MARS_DBG("recv_error = %d\n", output->recv_error);
brick_msleep(1000);
continue;
}
2011-02-23 20:48:06 +00:00
2011-03-07 05:55:10 +00:00
if (output->get_info) {
status = _request_info(output);
if (status >= 0) {
output->get_info = false;
2012-01-23 12:39:08 +00:00
} else {
MARS_WRN("cannot get info, status = %d\n", status);
brick_msleep(1000);
2011-03-07 05:55:10 +00:00
}
}
2012-12-19 11:40:52 +00:00
/* Grab the next mref from the queue
*/
2011-02-23 20:48:06 +00:00
traced_lock(&output->lock, flags);
2012-12-19 11:40:52 +00:00
if (list_empty(&output->mref_list)) {
traced_unlock(&output->lock, flags);
continue;
}
2011-02-23 20:48:06 +00:00
tmp = output->mref_list.next;
2012-12-19 11:40:52 +00:00
list_del(tmp);
list_add(tmp, &output->wait_list);
2011-02-23 20:48:06 +00:00
mref_a = container_of(tmp, struct client_mref_aspect, io_head);
2012-12-19 11:40:52 +00:00
traced_unlock(&output->lock, flags);
2011-03-30 12:02:50 +00:00
mref = mref_a->object;
2011-02-23 20:48:06 +00:00
if (brick->limit_mode) {
int amount = 0;
if (mref->ref_cs_mode < 2)
amount = (mref->ref_len - 1) / 1024 + 1;
mars_limit_sleep(&client_limiter, amount);
}
2011-03-30 12:02:50 +00:00
MARS_IO("sending mref, id = %d pos = %lld len = %d rw = %d\n", mref->ref_id, mref->ref_pos, mref->ref_len, mref->ref_rw);
status = mars_send_mref(&output->socket, mref);
2011-03-30 12:02:50 +00:00
MARS_IO("status = %d\n", status);
2011-02-23 20:48:06 +00:00
if (unlikely(status < 0)) {
// retry submission on next occasion..
2011-09-02 12:17:40 +00:00
MARS_WRN("sending failed, status = %d\n", status);
2011-02-23 20:48:06 +00:00
if (do_kill) {
do_kill = false;
_kill_socket(output);
}
2012-12-19 11:40:52 +00:00
_hash_insert(output, mref_a);
brick_msleep(1000);
2011-02-23 20:48:06 +00:00
continue;
}
}
//done:
2011-08-31 11:42:04 +00:00
if (status < 0) {
2011-09-02 12:17:40 +00:00
MARS_WRN("sender thread terminated with status = %d\n", status);
2011-08-31 11:42:04 +00:00
}
2011-02-23 20:48:06 +00:00
if (do_kill) {
_kill_socket(output);
}
2011-02-23 20:48:06 +00:00
/* Signal error on all pending IO requests.
* We have no other chance (except probably delaying
2012-01-23 12:39:08 +00:00
* this until destruction which is probably not what
* we want).
*/
2012-01-23 12:39:08 +00:00
_do_timeout(output, &output->wait_list, true);
_do_timeout(output, &output->mref_list, true);
2011-03-03 18:23:34 +00:00
wake_up_interruptible(&output->sender.run_event);
MARS_DBG("sender terminated\n");
2011-02-23 20:48:06 +00:00
return status;
}
static int client_switch(struct client_brick *brick)
{
struct client_output *output = brick->outputs[0];
int status = 0;
if (brick->power.button) {
if (brick->power.led_on)
goto done;
2011-02-23 20:48:06 +00:00
mars_power_led_off((void*)brick, false);
if (!output->sender.thread) {
brick->connection_state = 1;
output->sender.thread = brick_thread_create(sender_thread, output, "mars_sender%d", thread_count++);
if (unlikely(!output->sender.thread)) {
MARS_ERR("cannot start sender thread\n");
status = -ENOENT;
2011-03-03 18:23:34 +00:00
goto done;
}
2011-02-23 20:48:06 +00:00
}
if (output->sender.thread) {
2011-03-03 18:23:34 +00:00
mars_power_led_on((void*)brick, true);
}
2011-02-23 20:48:06 +00:00
} else {
if (brick->power.led_off)
goto done;
2011-02-23 20:48:06 +00:00
mars_power_led_on((void*)brick, false);
_kill_thread(&output->sender, "sender");
brick->connection_state = 0;
if (!output->sender.thread) {
2011-03-03 18:23:34 +00:00
mars_power_led_off((void*)brick, !output->sender.thread);
}
2011-02-23 20:48:06 +00:00
}
done:
return status;
}
//////////////// informational / statistics ///////////////
static
char *client_statistics(struct client_brick *brick, int verbose)
{
struct client_output *output = brick->outputs[0];
2012-01-23 12:39:08 +00:00
char *res = brick_string_alloc(1024);
if (!res)
return NULL;
2012-01-23 12:39:08 +00:00
snprintf(res, 1024,
2012-02-12 11:19:57 +00:00
"#%d socket "
"max_flying = %d "
"io_timeout = %d | "
"timeout_count = %d "
"fly_count = %d\n",
output->socket.s_debug_nr,
brick->max_flying,
brick->io_timeout,
atomic_read(&output->timeout_count),
atomic_read(&output->fly_count));
2012-01-23 12:39:08 +00:00
return res;
}
static
void client_reset_statistics(struct client_brick *brick)
{
2012-01-23 12:39:08 +00:00
struct client_output *output = brick->outputs[0];
atomic_set(&output->timeout_count, 0);
}
2011-02-23 20:48:06 +00:00
//////////////// object / aspect constructors / destructors ///////////////
static int client_mref_aspect_init_fn(struct generic_aspect *_ini)
2011-02-23 20:48:06 +00:00
{
struct client_mref_aspect *ini = (void*)_ini;
INIT_LIST_HEAD(&ini->io_head);
2011-03-30 12:02:50 +00:00
INIT_LIST_HEAD(&ini->hash_head);
2012-12-19 11:40:52 +00:00
INIT_LIST_HEAD(&ini->tmp_head);
2011-02-23 20:48:06 +00:00
return 0;
}
static void client_mref_aspect_exit_fn(struct generic_aspect *_ini)
2011-02-23 20:48:06 +00:00
{
struct client_mref_aspect *ini = (void*)_ini;
2011-03-30 12:02:50 +00:00
CHECK_HEAD_EMPTY(&ini->io_head);
CHECK_HEAD_EMPTY(&ini->hash_head);
2011-02-23 20:48:06 +00:00
}
MARS_MAKE_STATICS(client);
////////////////////// brick constructors / destructors ////////////////////
static int client_brick_construct(struct client_brick *brick)
{
return 0;
}
static int client_output_construct(struct client_output *output)
{
2011-03-30 12:02:50 +00:00
int i;
output->hash_table = brick_block_alloc(0, PAGE_SIZE);
if (unlikely(!output->hash_table)) {
MARS_ERR("cannot allocate hash table\n");
return -ENOMEM;
}
2011-03-30 12:02:50 +00:00
for (i = 0; i < CLIENT_HASH_MAX; i++) {
INIT_LIST_HEAD(&output->hash_table[i]);
}
2011-02-23 20:48:06 +00:00
spin_lock_init(&output->lock);
INIT_LIST_HEAD(&output->mref_list);
INIT_LIST_HEAD(&output->wait_list);
init_waitqueue_head(&output->event);
2011-03-03 18:23:34 +00:00
init_waitqueue_head(&output->sender.run_event);
init_waitqueue_head(&output->receiver.run_event);
2011-02-23 20:48:06 +00:00
init_waitqueue_head(&output->info_event);
return 0;
}
static int client_output_destruct(struct client_output *output)
{
2011-03-03 09:02:10 +00:00
if (output->path) {
2011-08-12 11:09:48 +00:00
brick_string_free(output->path);
2011-03-03 09:02:10 +00:00
output->path = NULL;
}
brick_block_free(output->hash_table, PAGE_SIZE);
2011-02-23 20:48:06 +00:00
return 0;
}
///////////////////////// static structs ////////////////////////
static struct client_brick_ops client_brick_ops = {
.brick_switch = client_switch,
.brick_statistics = client_statistics,
.reset_statistics = client_reset_statistics,
2011-02-23 20:48:06 +00:00
};
static struct client_output_ops client_output_ops = {
.mars_get_info = client_get_info,
.mref_get = client_ref_get,
.mref_put = client_ref_put,
.mref_io = client_ref_io,
};
const struct client_input_type client_input_type = {
.type_name = "client_input",
.input_size = sizeof(struct client_input),
};
static const struct client_input_type *client_input_types[] = {
&client_input_type,
};
const struct client_output_type client_output_type = {
.type_name = "client_output",
.output_size = sizeof(struct client_output),
.master_ops = &client_output_ops,
.output_construct = &client_output_construct,
.output_destruct = &client_output_destruct,
};
static const struct client_output_type *client_output_types[] = {
&client_output_type,
};
const struct client_brick_type client_brick_type = {
.type_name = "client_brick",
.brick_size = sizeof(struct client_brick),
.max_inputs = 0,
.max_outputs = 1,
.master_ops = &client_brick_ops,
.aspect_types = client_aspect_types,
2011-02-23 20:48:06 +00:00
.default_input_types = client_input_types,
.default_output_types = client_output_types,
.brick_construct = &client_brick_construct,
};
EXPORT_SYMBOL_GPL(client_brick_type);
////////////////// module init stuff /////////////////////////
struct mars_limiter client_limiter = {
.lim_max_rate = 0,
};
EXPORT_SYMBOL_GPL(client_limiter);
2012-08-14 14:12:59 +00:00
int global_net_io_timeout = CONFIG_MARS_NETIO_TIMEOUT;
EXPORT_SYMBOL_GPL(global_net_io_timeout);
2011-08-25 10:16:32 +00:00
int __init init_mars_client(void)
2011-02-23 20:48:06 +00:00
{
MARS_INF("init_client()\n");
2011-02-28 18:00:32 +00:00
_client_brick_type = (void*)&client_brick_type;
2011-02-23 20:48:06 +00:00
return client_register_brick_type();
}
2011-08-25 10:16:32 +00:00
void __exit exit_mars_client(void)
2011-02-23 20:48:06 +00:00
{
MARS_INF("exit_client()\n");
client_unregister_brick_type();
}
2011-08-25 10:16:32 +00:00
#ifndef CONFIG_MARS_HAVE_BIGMODULE
2011-02-23 20:48:06 +00:00
MODULE_DESCRIPTION("MARS client brick");
MODULE_AUTHOR("Thomas Schoebel-Theuer <tst@1und1.de>");
MODULE_LICENSE("GPL");
2011-08-25 10:16:32 +00:00
module_init(init_mars_client);
module_exit(exit_mars_client);
#endif