summaryrefslogtreecommitdiffstats
path: root/src/plugins/rdma/input.c
blob: 001d1c5d493d1b072b80c736083479e24d03ef3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 *------------------------------------------------------------------
 * Copyright (c) 2018 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vlib/pci/pci.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/devices/devices.h>

#include <rdma/rdma.h>

#define foreach_rdma_input_error \
  _(BUFFER_ALLOC, "buffer alloc error")

typedef enum
{
#define _(f,s) RDMA_INPUT_ERROR_##f,
  foreach_rdma_input_error
#undef _
    RDMA_INPUT_N_ERROR,
} rdma_input_error_t;

static __clib_unused char *rdma_input_error_strings[] = {
#define _(n,s) s,
  foreach_rdma_input_error
#undef _
};

static_always_inline void
rdma_device_input_refill (vlib_main_t * vm, rdma_device_t * rd,
			  rdma_rxq_t * rxq)
{
  u32 n_alloc, n;
  struct ibv_sge sg_entry;
  struct ibv_recv_wr wr, *bad_wr;
  u32 buffers[VLIB_FRAME_SIZE];

  if (rxq->n_enq >= rxq->size)
    return;

  n_alloc = clib_min (VLIB_FRAME_SIZE, rxq->size - rxq->n_enq);
  n_alloc = vlib_buffer_alloc (vm, buffers, n_alloc);

  sg_entry.length = vlib_buffer_get_default_data_size (vm);
  sg_entry.lkey = rd->mr->lkey;
  wr.num_sge = 1;
  wr.sg_list = &sg_entry;
  wr.next = NULL;
  for (n = 0; n < n_alloc; n++)
    {
      vlib_buffer_t *b = vlib_get_buffer (vm, buffers[n]);
      sg_entry.addr = vlib_buffer_get_va (b);
      wr.wr_id = buffers[n];
      if (ibv_post_recv (rxq->qp, &wr, &bad_wr) != 0)
	vlib_buffer_free (vm, buffers + n, 1);
      else
	rxq->n_enq++;
    }
}

static_always_inline uword
rdma_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
			  vlib_frame_t * frame, rdma_device_t * rd, u16 qid)
{
  vnet_main_t *vnm = vnet_get_main ();
  rdma_rxq_t *rxq = vec_elt_at_index (rd->rxqs, qid);
  u32 n_trace;
  struct ibv_wc wc[VLIB_FRAME_SIZE];
  u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
  u32 *bi, *to_next, n_left_to_next;
  int i;
  u32 n_rx_packets = 0, n_rx_bytes = 0;

  n_rx_packets = ibv_poll_cq (rxq->cq, VLIB_FRAME_SIZE, wc);

  if (n_rx_packets <= 0)
    rdma_device_input_refill (vm, rd, rxq);

  if (PREDICT_FALSE (rd->per_interface_next_index != ~0))
    next_index = rd->per_interface_next_index;

  vlib_get_new_next_frame (vm, node, next_index, to_next, n_left_to_next);

  for (i = 0; i < n_rx_packets; i++)
    {
      u32 bi = wc[i].wr_id;
      vlib_buffer_t *b = vlib_get_buffer (vm, bi);
      b->current_length = wc[i].byte_len;
      vnet_buffer (b)->sw_if_index[VLIB_RX] = rd->sw_if_index;
      vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
      to_next[i] = bi;
      n_rx_bytes += wc[i].byte_len;
    }

  if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
    {
      u32 n_left = n_rx_packets, i = 0;
      bi = to_next;

      while (n_trace && n_left)
	{
	  vlib_buffer_t *b;
	  rdma_input_trace_t *tr;
	  b = vlib_get_buffer (vm, bi[0]);
	  vlib_trace_buffer (vm, node, next_index, b, /* follow_chain */ 0);
	  tr = vlib_add_trace (vm, node, b, sizeof (*tr));
	  tr->next_index = next_index;
	  tr->hw_if_index = rd->hw_if_index;

	  /* next */
	  n_trace--;
	  n_left--;
	  bi++;
	  i++;
	}
      vlib_set_trace_count (vm, node, n_trace);
    }

  if (PREDICT_TRUE (next_index == VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT))
    {
      vlib_next_frame_t *nf;
      vlib_frame_t *f;
      ethernet_input_frame_t *ef;
      nf = vlib_node_runtime_get_next_frame (vm, node, next_index);
      f = vlib_get_frame (vm, nf->frame_index);
      f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX;

      ef = vlib_frame_scalar_args (f);
      ef->sw_if_index = rd->sw_if_index;
      ef->hw_if_index = rd->hw_if_index;
      //f->flags |= ETH_INPUT_FRAME_F_IP4_CKSUM_OK;
    }

  n_left_to_next -= n_rx_packets;
  vlib_put_next_frame (vm, node, next_index, n_left_to_next);

  vlib_increment_combined_counter
    (vnm->interface_main.combined_sw_if_counters +
     VNET_INTERFACE_COUNTER_RX, vm->thread_index,
     rd->hw_if_index, n_rx_packets, n_rx_bytes);

  rxq->n_enq -= n_rx_packets;
  rdma_device_input_refill (vm, rd, rxq);

  return n_rx_packets;
}

VLIB_NODE_FN (rdma_input_node) (vlib_main_t * vm,
				vlib_node_runtime_t * node,
				vlib_frame_t * frame)
{
  u32 n_rx = 0;
  rdma_main_t *rm = &rdma_main;
  vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
  vnet_device_and_queue_t *dq;

  foreach_device_and_queue (dq, rt->devices_and_queues)
  {
    rdma_device_t *rd;
    rd = vec_elt_at_index (rm->devices, dq->dev_instance);
    if ((rd->flags & RDMA_DEVICE_F_ADMIN_UP) == 0)
      continue;
    n_rx += rdma_device_input_inline (vm, node, frame, rd, dq->queue_id);
  }
  return n_rx;
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (rdma_input_node) = {
  .name = "rdma-input",
  .sibling_of = "device-input",
  .format_trace = format_rdma_input_trace,
  .type = VLIB_NODE_TYPE_INPUT,
  .state = VLIB_NODE_STATE_DISABLED,
  .n_errors = RDMA_INPUT_N_ERROR,
  .error_strings = rdma_input_error_strings,
};

/* *INDENT-ON* */


/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
s="n">vlib_get_thread_index () == 0); if (level > sc->level) { use_formatted_log_entry = false; goto syslog; } if ((delta > lm->unthrottle_time) || (sc->is_throttling == 0 && (delta > 1))) { sc->last_event_timestamp = t; sc->last_sec_count = 0; sc->is_throttling = 0; } else { sc->last_sec_count++; if (sc->last_sec_count > sc->rate_limit) return; else if (sc->last_sec_count == sc->rate_limit) { vec_reset_length (s); s = format (0, "--- message(s) throttled ---"); sc->is_throttling = 1; } } if (s == 0) { va_start (va, fmt); s = va_format (s, fmt, &va); va_end (va); } e = vec_elt_at_index (lm->entries, lm->next); vec_free (e->string); e->level = level; e->class = class; e->string = s; e->timestamp = t; lm->next = (lm->next + 1) % lm->size; if (lm->size > lm->count) lm->count++; syslog: if (sc->syslog_level != VLIB_LOG_LEVEL_DISABLED && level <= sc->syslog_level) { u8 *tmp = format (NULL, "%U", format_vlib_log_class, class); if (use_formatted_log_entry) { syslog (vlib_log_level_to_syslog_priority (level), "%.*s: %.*s", (int) vec_len (tmp), tmp, (int) (vec_len (s) - (vec_c_string_is_terminated (s) ? 1 : 0)), s); } else { tmp = format (tmp, ": "); va_start (va, fmt); tmp = va_format (tmp, fmt, &va); va_end (va); syslog (vlib_log_level_to_syslog_priority (level), "%.*s", (int) (vec_len (tmp) - (vec_c_string_is_terminated (tmp) ? 1 : 0)), tmp); } vec_free (tmp); } } vlib_log_class_t vlib_log_register_class (char *class, char *subclass) { vlib_log_main_t *lm = &log_main; vlib_log_class_data_t *c = NULL; vlib_log_subclass_data_t *s; vlib_log_class_data_t *tmp; vec_foreach (tmp, lm->classes) { if (vec_len (tmp->name) != strlen (class)) continue; if (!memcmp (class, tmp->name, vec_len (tmp->name))) { c = tmp; break; } } if (!c) { vec_add2 (lm->classes, c, 1); c->index = c - lm->classes; c->name = format (0, "%s", class); } vec_add2 (c->subclasses, s, 1); s->index = s - c->subclasses; s->name = subclass ? format (0, "%s", subclass) : 0; s->rate_limit = lm->default_rate_limit; s->level = lm->default_log_level; s->syslog_level = lm->default_syslog_log_level; return (c->index << 16) | (s->index); } u8 * format_vlib_log_level (u8 * s, va_list * args) { vlib_log_level_t i = va_arg (*args, vlib_log_level_t); char *t = 0; switch (i) { #define _(v,uc,lc) case VLIB_LOG_LEVEL_##uc: t = #lc; break; foreach_vlib_log_level #undef _ default: return format (s, "unknown"); } return format (s, "%s", t); } u32 vlib_log_get_indent () { return log_main.indent; } static clib_error_t * vlib_log_init (vlib_main_t * vm) { vlib_log_main_t *lm = &log_main; gettimeofday (&lm->time_zero_timeval, 0); lm->time_zero = vlib_time_now (vm); vec_validate (lm->entries, lm->size); lm->log_class = vlib_log_register_class ("log", 0); u8 *tmp = format (NULL, "%U %-10U %-10U ", format_time_float, 0, (f64) 0, format_white_space, 255, format_white_space, 255); log_main.indent = vec_len (tmp); vec_free (tmp); return 0; } VLIB_INIT_FUNCTION (vlib_log_init); static clib_error_t * show_log (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { clib_error_t *error = 0; vlib_log_main_t *lm = &log_main; vlib_log_entry_t *e; int i = last_log_entry (); int count = lm->count; f64 time_offset; time_offset = (f64) lm->time_zero_timeval.tv_sec + (((f64) lm->time_zero_timeval.tv_usec) * 1e-6) - lm->time_zero; while (count--) { e = vec_elt_at_index (lm->entries, i); vlib_cli_output (vm, "%U %-10U %-14U %v", format_time_float, 0, e->timestamp + time_offset, format_vlib_log_level, e->level, format_vlib_log_class, e->class, e->string); i = (i + 1) % lm->size; } return error; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_show_log, static) = { .path = "show logging", .short_help = "show logging", .function = show_log, }; /* *INDENT-ON* */ static clib_error_t * show_log_config (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { clib_error_t *error = 0; vlib_log_main_t *lm = &log_main; vlib_log_class_data_t *c; vlib_log_subclass_data_t *sc; vlib_cli_output (vm, "%-20s %u entries", "Buffer Size:", lm->size); vlib_cli_output (vm, "Defaults:\n"); vlib_cli_output (vm, "%-20s %U", " Log Level:", format_vlib_log_level, lm->default_log_level); vlib_cli_output (vm, "%-20s %U", " Syslog Log Level:", format_vlib_log_level, lm->default_syslog_log_level); vlib_cli_output (vm, "%-20s %u msgs/sec", " Rate Limit:", lm->default_rate_limit); vlib_cli_output (vm, "\n"); vlib_cli_output (vm, "%-22s %-14s %-14s %s", "Class/Subclass", "Level", "Syslog Level", "Rate Limit"); u8 *defstr = format (0, "default"); vec_foreach (c, lm->classes) { vlib_cli_output (vm, "%v", c->name); vec_foreach (sc, c->subclasses) { vlib_cli_output (vm, " %-20v %-14U %-14U %d", sc->name ? sc->name : defstr, format_vlib_log_level, sc->level, format_vlib_log_level, sc->syslog_level, sc->rate_limit); } } vec_free (defstr); return error; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_show_log_config, static) = { .path = "show logging configuration", .short_help = "show logging configuration", .function = show_log_config, }; /* *INDENT-ON* */ static clib_error_t * clear_log (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { clib_error_t *error = 0; vlib_log_main_t *lm = &log_main; vlib_log_entry_t *e; int i = last_log_entry (); int count = lm->count; while (count--) { e = vec_elt_at_index (lm->entries, i); vec_free (e->string); i = (i + 1) % lm->size; } lm->count = 0; lm->next = 0; vlib_log_info (lm->log_class, "log cleared"); return error; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_clear_log, static) = { .path = "clear logging", .short_help = "clear logging", .function = clear_log, }; /* *INDENT-ON* */ static uword unformat_vlib_log_level (unformat_input_t * input, va_list * args) { vlib_log_level_t *level = va_arg (*args, vlib_log_level_t *); u8 *level_str = NULL; uword rv = 1; if (unformat (input, "%s", &level_str)) { #define _(v, uc, lc) \ const char __##uc[] = #lc; \ if (!strcmp ((const char *) level_str, __##uc)) \ { \ *level = VLIB_LOG_LEVEL_##uc; \ rv = 1; \ goto done; \ } foreach_vlib_log_level; rv = 0; #undef _ } done: vec_free (level_str); return rv; } static uword unformat_vlib_log_class (unformat_input_t * input, va_list * args) { vlib_log_class_data_t **class = va_arg (*args, vlib_log_class_data_t **); uword rv = 0; u8 *class_str = NULL; vlib_log_main_t *lm = &log_main; if (unformat (input, "%v", &class_str)) { vlib_log_class_data_t *cdata; vec_foreach (cdata, lm->classes) { if (vec_is_equal (cdata->name, class_str)) { *class = cdata; rv = 1; break; } } } vec_free (class_str); return rv; } static clib_error_t * set_log_class (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; clib_error_t *rv = NULL; int rate_limit; bool set_rate_limit = false; bool set_level = false; bool set_syslog_level = false; vlib_log_level_t level; vlib_log_level_t syslog_level; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; vlib_log_class_data_t *class = NULL; if (!unformat (line_input, "%U", unformat_vlib_log_class, &class)) { return clib_error_return (0, "unknown log class `%U'", format_unformat_error, line_input); } while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "rate-limit %d", &rate_limit)) { set_rate_limit = true; } else if (unformat (line_input, "level %U", unformat_vlib_log_level, &level)) { set_level = true; } else if (unformat (line_input, "syslog-level %U", unformat_vlib_log_level, &syslog_level)) { set_syslog_level = true; } else { return clib_error_return (0, "unknown input `%U'", format_unformat_error, line_input); } } if (set_level) { vlib_log_subclass_data_t *subclass; vec_foreach (subclass, class->subclasses) { subclass->level = level; } } if (set_syslog_level) { vlib_log_subclass_data_t *subclass; vec_foreach (subclass, class->subclasses) { subclass->syslog_level = syslog_level; } } if (set_rate_limit) { vlib_log_subclass_data_t *subclass; vec_foreach (subclass, class->subclasses) { subclass->rate_limit = rate_limit; } } return rv; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_set_log, static) = { .path = "set logging class", .short_help = "set logging class <class> [rate-limit <int>] " "[level <level>] [syslog-level <level>]", .function = set_log_class, }; /* *INDENT-ON* */ static clib_error_t * set_log_unth_time (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; clib_error_t *rv = NULL; int unthrottle_time; vlib_log_main_t *lm = &log_main; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "%d", &unthrottle_time)) lm->unthrottle_time = unthrottle_time; else return clib_error_return (0, "unknown input `%U'", format_unformat_error, line_input); } return rv; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_set_log_params, static) = { .path = "set logging unthrottle-time", .short_help = "set logging unthrottle-time <int>", .function = set_log_unth_time, }; /* *INDENT-ON* */ static clib_error_t * set_log_size (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; clib_error_t *rv = NULL; int size; vlib_log_main_t *lm = &log_main; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "%d", &size)) { lm->size = size; vec_validate (lm->entries, lm->size); } else return clib_error_return (0, "unknown input `%U'", format_unformat_error, line_input); } return rv; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_set_log_size, static) = { .path = "set logging size", .short_help = "set logging size <int>", .function = set_log_size, }; /* *INDENT-ON* */ static uword unformat_vlib_log_subclass (unformat_input_t * input, va_list * args) { vlib_log_class_data_t *class = va_arg (*args, vlib_log_class_data_t *); vlib_log_subclass_data_t **subclass = va_arg (*args, vlib_log_subclass_data_t **); uword rv = 0; u8 *subclass_str = NULL; if (unformat (input, "%v", &subclass_str)) { vlib_log_subclass_data_t *scdata; vec_foreach (scdata, class->subclasses) { if (vec_is_equal (scdata->name, subclass_str)) { rv = 1; *subclass = scdata; break; } } } vec_free (subclass_str); return rv; } static clib_error_t * test_log_class_subclass (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; vlib_log_class_data_t *class = NULL; vlib_log_subclass_data_t *subclass = NULL; vlib_log_level_t level; if (unformat (line_input, "%U", unformat_vlib_log_level, &level)) { if (unformat (line_input, "%U", unformat_vlib_log_class, &class)) { if (unformat (line_input, "%U", unformat_vlib_log_subclass, class, &subclass)) { vlib_log (level, (class->index << 16) | (subclass->index), "%U", format_unformat_input, line_input); } else { return clib_error_return (0, "unknown log subclass near beginning of `%U'", format_unformat_error, line_input); } } else { return clib_error_return (0, "unknown log class near beginning of `%U'", format_unformat_error, line_input); } } else { return clib_error_return (0, "unknown log level near beginning of `%U'", format_unformat_error, line_input); } return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_test_log, static) = { .path = "test log", .short_help = "test log <level> <class> <subclass> <message>", .function = test_log_class_subclass, }; /* *INDENT-ON* */ static clib_error_t * log_config (vlib_main_t * vm, unformat_input_t * input) { vlib_log_main_t *lm = &log_main; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "size %d", &lm->size)) vec_validate (lm->entries, lm->size); else if (unformat (input, "unthrottle-time %d", &lm->unthrottle_time)) ; else if (unformat (input, "default-log-level %U", unformat_vlib_log_level, &lm->default_log_level)) ; else if (unformat (input, "default-syslog-log-level %U", unformat_vlib_log_level, &lm->default_syslog_log_level)) ; else { return unformat_parse_error (input); } } return 0; } VLIB_EARLY_CONFIG_FUNCTION (log_config, "logging"); /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */