mars/kernel/mars_client.c

1065 lines
27 KiB
C
Raw Normal View History

2014-11-21 10:51:34 +00:00
/*
* MARS Long Distance Replication Software
*
* This file is part of MARS project: http://schoebel.github.io/mars/
*
* Copyright (C) 2010-2014 Thomas Schoebel-Theuer
* Copyright (C) 2011-2014 1&1 Internet AG
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
2011-02-23 20:48:06 +00:00
//#define BRICK_DEBUGGING
//#define MARS_DEBUGGING
//#define IO_DEBUGGING
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
2014-04-01 09:24:46 +00:00
#include <linux/jiffies.h>
2011-02-23 20:48:06 +00:00
#include "mars.h"
///////////////////////// own type definitions ////////////////////////
#include "mars_client.h"
#define CLIENT_HASH_MAX (PAGE_SIZE / sizeof(struct list_head))
int mars_client_abort = 10;
EXPORT_SYMBOL_GPL(mars_client_abort);
2014-04-01 09:24:46 +00:00
int max_client_channels = 1;
EXPORT_SYMBOL_GPL(max_client_channels);
int max_client_bulk = 16;
EXPORT_SYMBOL_GPL(max_client_bulk);
2011-02-23 20:48:06 +00:00
///////////////////////// own helper functions ////////////////////////
static int thread_count = 0;
2014-04-01 09:24:46 +00:00
static
void _do_resubmit(struct client_channel *ch)
2011-02-23 20:48:06 +00:00
{
2014-04-01 09:24:46 +00:00
struct client_output *output = ch->output;
spin_lock(&output->lock);
if (!list_empty(&ch->wait_list)) {
struct list_head *first = ch->wait_list.next;
struct list_head *last = ch->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(&ch->wait_list);
}
spin_unlock(&output->lock);
}
static
void _kill_thread(struct client_threadinfo *ti, const char *name)
{
struct task_struct *thread = ti->thread;
if (thread) {
MARS_DBG("stopping %s thread\n", name);
ti->thread = NULL;
2014-04-01 09:24:46 +00:00
brick_thread_stop(thread);
2011-02-23 20:48:06 +00:00
}
}
2014-04-01 09:24:46 +00:00
static
void _kill_channel(struct client_channel *ch)
2011-08-31 11:42:04 +00:00
{
2014-04-01 09:24:46 +00:00
MARS_DBG("channel = %p\n", ch);
if (mars_socket_is_alive(&ch->socket)) {
2011-08-31 11:42:04 +00:00
MARS_DBG("shutdown socket\n");
2014-04-01 09:24:46 +00:00
mars_shutdown_socket(&ch->socket);
}
_kill_thread(&ch->receiver, "receiver");
if (ch->is_open) {
MARS_DBG("close socket\n");
mars_put_socket(&ch->socket);
2011-08-31 11:42:04 +00:00
}
2014-04-01 09:24:46 +00:00
ch->recv_error = 0;
ch->is_open = false;
ch->is_connected = false;
/* Re-Submit any waiting requests
*/
_do_resubmit(ch);
2011-08-31 11:42:04 +00:00
}
2014-04-01 09:24:46 +00:00
static inline
void _kill_all_channels(struct client_bundle *bundle)
2011-03-07 05:55:10 +00:00
{
2014-04-01 09:24:46 +00:00
int i;
// first pass: shutdown in parallel without waiting
for (i = 0; i < MAX_CLIENT_CHANNELS; i++) {
struct client_channel *ch =&bundle->channel[i];
if (mars_socket_is_alive(&ch->socket)) {
MARS_DBG("shutdown socket %d\n", i);
mars_shutdown_socket(&ch->socket);
}
}
// separate pass (may wait)
for (i = 0; i < MAX_CLIENT_CHANNELS; i++) {
_kill_channel(&bundle->channel[i]);
2011-03-07 05:55:10 +00:00
}
}
2014-04-01 09:24:46 +00:00
static
int receiver_thread(void *data);
2011-08-31 11:42:04 +00:00
2014-04-01 09:24:46 +00:00
static
int _setup_channel(struct client_bundle *bundle, int ch_nr)
2011-02-23 20:48:06 +00:00
{
2014-04-01 09:24:46 +00:00
struct client_channel *ch = &bundle->channel[ch_nr];
2011-02-23 20:48:06 +00:00
struct sockaddr_storage sockaddr = {};
int status;
2014-04-01 09:24:46 +00:00
ch->ch_nr = ch_nr;
if (unlikely(ch->receiver.thread)) {
MARS_WRN("receiver thread %d unexpectedly not dead\n", ch_nr);
_kill_thread(&ch->receiver, "receiver");
}
2014-04-01 09:24:46 +00:00
status = mars_create_sockaddr(&sockaddr, bundle->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
}
2014-04-01 09:24:46 +00:00
status = mars_create_socket(&ch->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
}
2014-04-01 09:24:46 +00:00
ch->socket.s_shutdown_on_err = true;
ch->socket.s_send_abort = mars_client_abort;
ch->socket.s_recv_abort = mars_client_abort;
ch->is_open = true;
ch->receiver.thread = brick_thread_create(receiver_thread, ch, "mars_receiver%d.%d.%d", bundle->thread_count, ch_nr, ch->thread_count++);
if (unlikely(!ch->receiver.thread)) {
MARS_ERR("cannot start receiver thread %d, status = %d\n", ch_nr, status);
status = -ENOENT;
2011-08-31 11:42:04 +00:00
goto done;
}
2014-04-01 09:24:46 +00:00
ch->is_used = true;
done:
if (status < 0) {
MARS_INF("cannot connect channel %d to remote host '%s' (status = %d) -- retrying\n",
ch_nr,
bundle->host ? bundle->host : "NULL",
status);
_kill_channel(ch);
}
really_done:
return status;
}
static
void _kill_bundle(struct client_bundle *bundle)
{
MARS_DBG("\n");
_kill_thread(&bundle->sender, "sender");
_kill_all_channels(bundle);
}
static
void _maintain_bundle(struct client_bundle *bundle)
{
int i;
/* Re-open _any_ failed channel, even old ones.
* Reason: the number of channels might change during operation.
*/
for (i = 0; i < MAX_CLIENT_CHANNELS; i++) {
struct client_channel *ch = &bundle->channel[i];
int status;
if (!ch->is_used ||
(!ch->recv_error && mars_socket_is_alive(&ch->socket)))
continue;
MARS_DBG("killing channel %d\n", i);
_kill_channel(ch);
status = _setup_channel(bundle, i);
MARS_DBG("setup channel %d status=%d\n", i, status);
}
}
static
struct client_channel *_get_channel(struct client_bundle *bundle, int min_channel, int max_channel)
{
2015-06-15 05:48:54 +00:00
struct client_channel *res;
long best_space;
int best_channel;
2014-04-01 09:24:46 +00:00
int i;
if (unlikely(max_channel <= 0 || max_channel > MAX_CLIENT_CHANNELS))
max_channel = MAX_CLIENT_CHANNELS;
2015-06-15 05:48:54 +00:00
if (unlikely(min_channel < 0 || min_channel >= max_channel)) {
2014-04-01 09:24:46 +00:00
min_channel = max_channel - 1;
2015-06-15 05:48:54 +00:00
if (unlikely(min_channel < 0))
min_channel = 0;
}
/* Fast path.
* Speculate that the next channel is already usable,
* and that it has enough room.
*/
best_channel = bundle->old_channel + 1;
if (best_channel >= max_channel)
best_channel = min_channel;
res = &bundle->channel[best_channel];
if (res->is_open && res->is_connected && !res->recv_error && mars_socket_is_alive(&res->socket)) {
res->current_space = mars_socket_send_space_available(&res->socket);
if (res->current_space > (PAGE_SIZE + PAGE_SIZE / 4))
goto found;
}
2011-08-31 11:42:04 +00:00
2015-06-15 05:48:54 +00:00
/* Slow path. Do all the teady work.
*/
_maintain_bundle(bundle);
res = NULL;
best_space = -1;
best_channel = -1;
2014-04-01 09:24:46 +00:00
for (i = min_channel; i < max_channel; i++) {
struct client_channel *ch = &bundle->channel[i];
long this_space;
2011-08-31 11:42:04 +00:00
2014-04-01 09:24:46 +00:00
// create new channels when necessary
if (unlikely(!ch->is_open)) {
int status;
// only create one new channel at a time
status = _setup_channel(bundle, i);
MARS_DBG("setup channel %d status=%d\n", i, status);
if (unlikely(status < 0))
continue;
this_space = mars_socket_send_space_available(&ch->socket);
ch->current_space = this_space;
/* Always prefer the newly opened channel */
res = ch;
2015-06-15 05:48:54 +00:00
best_channel = i;
2014-04-01 09:24:46 +00:00
break;
}
// select the best usable channel
this_space = mars_socket_send_space_available(&ch->socket);
ch->current_space = this_space;
if (this_space > best_space) {
best_space = this_space;
2015-06-15 05:48:54 +00:00
best_channel = i;
2014-04-01 09:24:46 +00:00
res = ch;
}
}
2015-06-15 05:48:54 +00:00
if (unlikely(!res))
goto done;
2014-04-01 09:24:46 +00:00
// send initial connect command
2015-06-15 05:48:54 +00:00
if (unlikely(!res->is_connected)) {
2011-02-23 20:48:06 +00:00
struct mars_cmd cmd = {
.cmd_code = CMD_CONNECT,
2014-04-01 09:24:46 +00:00
.cmd_str1 = bundle->path,
2011-02-23 20:48:06 +00:00
};
2014-04-01 09:24:46 +00:00
int status = mars_send_struct(&res->socket, &cmd, mars_cmd_meta);
MARS_DBG("send CMD_CONNECT status = %d\n", status);
2011-03-04 15:14:20 +00:00
if (unlikely(status < 0)) {
2014-04-01 09:24:46 +00:00
_kill_channel(res);
res = NULL;
2011-02-23 20:48:06 +00:00
goto done;
2011-03-04 15:14:20 +00:00
}
2014-04-01 09:24:46 +00:00
res->is_connected = true;
2011-02-23 20:48:06 +00:00
}
2014-04-01 09:24:46 +00:00
2015-06-15 05:48:54 +00:00
found:
bundle->old_channel = best_channel;
2014-04-01 09:24:46 +00:00
done:
return res;
}
static
int _request_info(struct client_channel *ch)
{
struct mars_cmd cmd = {
.cmd_code = CMD_GETINFO,
};
int status;
MARS_DBG("\n");
status = mars_send_struct(&ch->socket, &cmd, mars_cmd_meta);
MARS_DBG("send CMD_GETINFO status = %d\n", status);
if (unlikely(status < 0)) {
MARS_DBG("send of getinfo failed, status = %d\n", status);
2011-02-23 20:48:06 +00:00
}
2014-04-01 09:24:46 +00:00
return status;
}
2011-02-23 20:48:06 +00:00
2014-04-01 09:24:46 +00:00
static int sender_thread(void *data);
static
int _setup_bundle(struct client_bundle *bundle, const char *str)
{
int status = -ENOMEM;
MARS_DBG("\n");
_kill_bundle(bundle);
brick_string_free(bundle->path);
bundle->path = brick_strdup(str);
status = -EINVAL;
bundle->host = strchr(bundle->path, '@');
if (unlikely(!bundle->host)) {
brick_string_free(bundle->path);
bundle->path = NULL;
MARS_ERR("parameter string '%s' contains no remote specifier with '@'-syntax\n", str);
goto done;
2011-02-23 20:48:06 +00:00
}
2014-04-01 09:24:46 +00:00
*bundle->host++ = '\0';
bundle->thread_count = thread_count++;
bundle->sender.thread = brick_thread_create(sender_thread, bundle, "mars_sender%d", bundle->thread_count);
if (unlikely(!bundle->sender.thread)) {
MARS_ERR("cannot start sender thread\n");
status = -ENOENT;
goto done;
}
status = 0;
done:
MARS_DBG("status = %d\n", status);
2011-02-23 20:48:06 +00:00
return status;
}
2017-01-10 11:48:15 +00:00
static
long _compute_timeout(struct client_brick *brick)
{
long io_timeout = brick->power.io_timeout;
if (io_timeout <= 0)
io_timeout = global_net_io_timeout;
return io_timeout;
}
2011-02-23 20:48:06 +00:00
////////////////// own brick / input / output operations //////////////////
static int client_get_info(struct client_output *output, struct mars_info *info)
{
2017-01-10 11:48:15 +00:00
struct client_brick *brick = output->brick;
long io_timeout = _compute_timeout(brick);
2011-02-23 20:48:06 +00:00
int status;
2011-03-07 05:55:10 +00:00
output->got_info = false;
if (!brick->power.led_on)
goto timeout;
2011-03-07 05:55:10 +00:00
output->get_info = true;
2014-04-01 09:24:46 +00:00
wake_up_interruptible_all(&output->bundle.sender_event);
2011-03-07 05:55:10 +00:00
2017-01-10 11:48:15 +00:00
wait_event_interruptible_timeout(output->info_event, output->got_info, io_timeout * HZ);
timeout:
status = -ETIME;
2011-02-23 20:48:06 +00:00
if (output->got_info && info) {
memcpy(info, &output->info, sizeof(*info));
status = 0;
}
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
}
2014-04-01 09:24:46 +00:00
if (!output->brick->power.led_on) {
MARS_ERR("IO submission on dead instance\n");
}
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));
2014-04-01 09:24:46 +00:00
wake_up_interruptible_all(&output->bundle.sender_event);
2011-02-23 20:48:06 +00:00
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
{
2014-04-01 09:24:46 +00:00
struct client_channel *ch = data;
struct client_output *output = ch->output;
2011-02-23 20:48:06 +00:00
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;
2014-04-01 09:24:46 +00:00
if (ch->recv_error) {
/* The protocol may be out of sync.
* Consume some data to avoid distributed deadlocks.
*/
2014-04-01 09:24:46 +00:00
(void)mars_recv_raw(&ch->socket, &cmd, 0, sizeof(cmd));
brick_msleep(100);
2014-04-01 09:24:46 +00:00
status = ch->recv_error;
continue;
}
2014-04-01 09:24:46 +00:00
status = mars_recv_struct(&ch->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);
2014-04-01 09:24:46 +00:00
if (status <= 0) {
if (!mars_socket_is_alive(&ch->socket)) {
MARS_DBG("socket is dead\n");
brick_msleep(1000);
continue;
}
2011-02-23 20:48:06 +00:00
goto done;
2014-04-01 09:24:46 +00:00
}
2011-02-23 20:48:06 +00:00
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);
2014-04-01 09:24:46 +00:00
// try to consume the corresponding payload
mref = client_alloc_mref(output->brick);
status = mars_recv_cb(&ch->socket, mref, &cmd);
client_free_mref(mref);
2011-02-23 20:48:06 +00:00
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);
2014-04-01 09:24:46 +00:00
status = mars_recv_cb(&ch->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;
}
2014-04-01 09:24:46 +00:00
if (mref->_object_cb.cb_error < 0) {
MARS_DBG("ERROR %d\n", mref->_object_cb.cb_error);
}
2013-11-30 21:34:40 +00:00
SIMPLE_CALLBACK(mref, mref->_object_cb.cb_error);
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:
2014-04-01 09:24:46 +00:00
status = mars_recv_struct(&ch->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;
2014-04-01 09:24:46 +00:00
wake_up_interruptible_all(&output->info_event);
2011-02-23 20:48:06 +00:00
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)) {
2014-04-01 09:24:46 +00:00
if (!ch->recv_error) {
MARS_DBG("signalling recv_error = %d\n", status);
ch->recv_error = status;
}
brick_msleep(100);
}
2014-04-01 09:24:46 +00:00
// wake up sender in any case
wake_up_interruptible_all(&output->bundle.sender_event);
2011-02-23 20:48:06 +00:00
}
2011-03-03 18:23:34 +00:00
if (status < 0) {
2014-04-01 09:24:46 +00:00
MARS_WRN("receiver thread terminated with status = %d\n", status);
2011-03-03 18:23:34 +00:00
}
2011-08-31 11:42:04 +00:00
2014-04-01 09:24:46 +00:00
mars_shutdown_socket(&ch->socket);
2011-02-23 20:48:06 +00:00
return status;
}
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);
2014-04-01 09:24:46 +00:00
int count = 0;
2012-08-21 11:43:56 +00:00
int rounds = 0;
2017-01-10 11:48:15 +00:00
long io_timeout = _compute_timeout(brick);
2012-12-19 11:40:52 +00:00
unsigned long flags;
2014-04-01 09:24:46 +00:00
int i;
if (list_empty(anchor))
return;
2012-12-19 11:40:52 +00:00
if (!mars_net_is_alive)
force = true;
if (!force && io_timeout <= 0) {
2014-04-01 09:24:46 +00:00
for (i = 0; i < MAX_CLIENT_CHANNELS; i++) {
struct client_channel *ch = &output->bundle.channel[i];
ch->socket.s_send_abort = mars_client_abort;
ch->socket.s_recv_abort = mars_client_abort;
}
2012-12-19 11:40:52 +00:00
return;
}
2014-04-01 09:24:46 +00:00
for (i = 0; i < MAX_CLIENT_CHANNELS; i++) {
struct client_channel *ch = &output->bundle.channel[i];
ch->socket.s_send_abort = 1;
ch->socket.s_recv_abort = 1;
}
2012-12-19 11:40:52 +00:00
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
2014-04-01 09:24:46 +00:00
count++;
2012-01-23 12:39:08 +00:00
atomic_inc(&output->timeout_count);
2017-01-18 08:18:10 +00:00
SIMPLE_CALLBACK(mref, -ETIME);
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
}
2014-04-01 09:24:46 +00:00
MARS_DBG("force = %d count = %d\n", force, count);
}
static
void _do_timeout_all(struct client_output *output, bool force)
{
int i;
for (i = 0; i < MAX_CLIENT_CHANNELS; i++) {
struct client_channel *ch = &output->bundle.channel[i];
if (!ch->is_used)
continue;
_do_timeout(output, &ch->wait_list, force);
}
_do_timeout(output, &output->mref_list, force);
2012-01-23 12:39:08 +00:00
}
2011-02-23 20:48:06 +00:00
static int sender_thread(void *data)
{
2014-04-01 09:24:46 +00:00
struct client_bundle *bundle = data;
struct client_output *output = container_of(bundle, struct client_output, bundle);
2011-02-23 20:48:06 +00:00
struct client_brick *brick = output->brick;
2014-04-01 09:24:46 +00:00
struct client_channel *ch = NULL;
bool do_timeout = false;
int ch_skip = max_client_bulk;
int status = -ENOTCONN;
unsigned long flags;
2011-05-26 14:32:32 +00:00
2017-02-24 06:10:37 +00:00
while (brick->power.button && !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;
2014-04-01 09:24:46 +00:00
int min_nr;
int max_nr;
2011-02-23 20:48:06 +00:00
2014-04-01 09:24:46 +00:00
// timeouting is a rather expensive operation, don't do it too often
if (do_timeout) {
do_timeout = false;
_do_timeout_all(output, false);
}
2014-04-01 09:24:46 +00:00
wait_event_interruptible_timeout(output->bundle.sender_event,
!list_empty(&output->mref_list) ||
2014-04-01 09:24:46 +00:00
output->get_info,
2 * HZ);
2017-01-07 23:45:44 +00:00
2011-03-07 05:55:10 +00:00
if (output->get_info) {
2014-04-01 09:24:46 +00:00
ch = _get_channel(bundle, 0, 1);
if (unlikely(!ch)) {
MARS_WRN("cannot setup info communication channel\n");
do_timeout = true;
brick_msleep(1000);
continue;
}
status = _request_info(ch);
if (likely(status >= 0)) {
2011-03-07 05:55:10 +00:00
output->get_info = false;
2012-01-23 12:39:08 +00:00
} else {
2014-04-01 09:24:46 +00:00
MARS_WRN("cannot send info request, status = %d\n", status);
do_timeout = true;
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);
2014-04-01 09:24:46 +00:00
tmp = output->mref_list.next;
if (tmp == &output->mref_list) {
2012-12-19 11:40:52 +00:00
traced_unlock(&output->lock, flags);
2014-04-01 09:24:46 +00:00
MARS_DBG("empty %d %d\n", output->get_info, brick_thread_should_stop());
do_timeout = true;
2012-12-19 11:40:52 +00:00
continue;
}
2014-04-01 09:24:46 +00:00
list_del_init(tmp);
// notice: hash_head remains in its list!
2012-12-19 11:40:52 +00:00
traced_unlock(&output->lock, flags);
2014-04-01 09:24:46 +00:00
mref_a = container_of(tmp, struct client_mref_aspect, io_head);
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);
}
2014-04-01 09:24:46 +00:00
// try to spread reads over multiple channels....
min_nr = 0;
max_nr = max_client_channels;
if (!mref->ref_rw) {
/* optionally separate reads from writes */
if (brick->separate_reads && max_nr > 1)
min_nr = 1;
} else if (!brick->allow_permuting_writes) {
max_nr = 1;
}
if (!ch || ch->recv_error ||
!mars_socket_is_alive(&ch->socket) ||
ch->ch_nr >= max_nr || --ch_skip < 0) {
ch = _get_channel(bundle, min_nr, max_nr);
if (likely(ch)) {
/* estimate: add some headroom for overhead */
ch_skip = ch->current_space / PAGE_SIZE +
ch->current_space / (PAGE_SIZE * 8);
if (ch_skip > max_client_bulk)
ch_skip = max_client_bulk;
} else {
// notice: this will re-assign hash_head without harm
_hash_insert(output, mref_a);
do_timeout = true;
MARS_WRN("cannot setup communication channel\n");
brick_msleep(1000);
continue;
}
}
spin_lock(&output->lock);
list_add(tmp, &ch->wait_list);
// notice: hash_head is already there!
spin_unlock(&output->lock);
2011-03-30 12:02:50 +00:00
2014-04-01 09:24:46 +00:00
status = mars_send_mref(&ch->socket, mref);
2011-02-23 20:48:06 +00:00
if (unlikely(status < 0)) {
2014-04-01 09:24:46 +00:00
_hash_insert(output, mref_a);
do_timeout = true;
ch = NULL;
2011-02-23 20:48:06 +00:00
// 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
2014-04-01 09:24:46 +00:00
brick_msleep(100);
2011-02-23 20:48:06 +00:00
continue;
}
}
2014-04-01 09:24:46 +00:00
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
2014-04-01 09:24:46 +00:00
_kill_all_channels(bundle);
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).
*/
2014-04-01 09:24:46 +00:00
_do_timeout_all(output, true);
wake_up_interruptible_all(&output->bundle.sender_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);
2014-04-01 09:24:46 +00:00
status = _setup_bundle(&output->bundle, brick->brick_name);
if (likely(status >= 0)) {
output->get_info = true;
brick->connection_state = 1;
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);
2014-04-01 09:24:46 +00:00
_kill_bundle(&output->bundle);
_do_timeout_all(output, true);
output->got_info = false;
brick->connection_state = 0;
2014-04-01 09:24:46 +00:00
mars_power_led_off((void*)brick, !output->bundle.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
"max_flying = %d "
"io_timeout = %d | "
"timeout_count = %d "
"fly_count = %d\n",
brick->max_flying,
brick->power.io_timeout,
2012-02-12 11:19:57 +00:00
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]);
}
2014-04-01 09:24:46 +00:00
for (i = 0; i < MAX_CLIENT_CHANNELS; i++) {
struct client_channel *ch = &output->bundle.channel[i];
ch->output = output;
INIT_LIST_HEAD(&ch->wait_list);
}
init_waitqueue_head(&output->bundle.sender_event);
2011-02-23 20:48:06 +00:00
spin_lock_init(&output->lock);
INIT_LIST_HEAD(&output->mref_list);
init_waitqueue_head(&output->info_event);
return 0;
}
static int client_output_destruct(struct client_output *output)
{
2014-04-01 09:24:46 +00:00
brick_string_free(output->bundle.path);
output->bundle.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();
}
2014-04-23 11:16:26 +00:00
void exit_mars_client(void)
2011-02-23 20:48:06 +00:00
{
MARS_INF("exit_client()\n");
client_unregister_brick_type();
}