Add RDMA ibverb driver plugin
[vpp.git] / src / plugins / rdma / input.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <vlib/vlib.h>
19 #include <vlib/unix/unix.h>
20 #include <vlib/pci/pci.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/devices/devices.h>
23
24 #include <rdma/rdma.h>
25
26 #define foreach_rdma_input_error \
27   _(BUFFER_ALLOC, "buffer alloc error")
28
29 typedef enum
30 {
31 #define _(f,s) RDMA_INPUT_ERROR_##f,
32   foreach_rdma_input_error
33 #undef _
34     RDMA_INPUT_N_ERROR,
35 } rdma_input_error_t;
36
37 static __clib_unused char *rdma_input_error_strings[] = {
38 #define _(n,s) s,
39   foreach_rdma_input_error
40 #undef _
41 };
42
43 static_always_inline void
44 rdma_device_input_refill (vlib_main_t * vm, rdma_device_t * rd,
45                           rdma_rxq_t * rxq)
46 {
47   u32 n_alloc, n;
48   struct ibv_sge sg_entry;
49   struct ibv_recv_wr wr, *bad_wr;
50   u32 buffers[VLIB_FRAME_SIZE];
51
52   if (rxq->n_enq >= rxq->size)
53     return;
54
55   n_alloc = clib_min (VLIB_FRAME_SIZE, rxq->size - rxq->n_enq);
56   n_alloc = vlib_buffer_alloc (vm, buffers, n_alloc);
57
58   sg_entry.length = vlib_buffer_get_default_data_size (vm);
59   sg_entry.lkey = rd->mr->lkey;
60   wr.num_sge = 1;
61   wr.sg_list = &sg_entry;
62   wr.next = NULL;
63   for (n = 0; n < n_alloc; n++)
64     {
65       vlib_buffer_t *b = vlib_get_buffer (vm, buffers[n]);
66       sg_entry.addr = vlib_buffer_get_va (b);
67       wr.wr_id = buffers[n];
68       if (ibv_post_recv (rxq->qp, &wr, &bad_wr) != 0)
69         vlib_buffer_free (vm, buffers + n, 1);
70       else
71         rxq->n_enq++;
72     }
73 }
74
75 static_always_inline uword
76 rdma_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
77                           vlib_frame_t * frame, rdma_device_t * rd, u16 qid)
78 {
79   vnet_main_t *vnm = vnet_get_main ();
80   rdma_rxq_t *rxq = vec_elt_at_index (rd->rxqs, qid);
81   u32 n_trace;
82   struct ibv_wc wc[VLIB_FRAME_SIZE];
83   u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
84   u32 *bi, *to_next, n_left_to_next;
85   int i;
86   u32 n_rx_packets = 0, n_rx_bytes = 0;
87
88   n_rx_packets = ibv_poll_cq (rxq->cq, VLIB_FRAME_SIZE, wc);
89
90   if (n_rx_packets <= 0)
91     rdma_device_input_refill (vm, rd, rxq);
92
93   if (PREDICT_FALSE (rd->per_interface_next_index != ~0))
94     next_index = rd->per_interface_next_index;
95
96   vlib_get_new_next_frame (vm, node, next_index, to_next, n_left_to_next);
97
98   for (i = 0; i < n_rx_packets; i++)
99     {
100       u32 bi = wc[i].wr_id;
101       vlib_buffer_t *b = vlib_get_buffer (vm, bi);
102       b->current_length = wc[i].byte_len;
103       vnet_buffer (b)->sw_if_index[VLIB_RX] = rd->sw_if_index;
104       vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
105       to_next[i] = bi;
106       n_rx_bytes += wc[i].byte_len;
107     }
108
109   if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
110     {
111       u32 n_left = n_rx_packets, i = 0;
112       bi = to_next;
113
114       while (n_trace && n_left)
115         {
116           vlib_buffer_t *b;
117           rdma_input_trace_t *tr;
118           b = vlib_get_buffer (vm, bi[0]);
119           vlib_trace_buffer (vm, node, next_index, b, /* follow_chain */ 0);
120           tr = vlib_add_trace (vm, node, b, sizeof (*tr));
121           tr->next_index = next_index;
122           tr->hw_if_index = rd->hw_if_index;
123
124           /* next */
125           n_trace--;
126           n_left--;
127           bi++;
128           i++;
129         }
130       vlib_set_trace_count (vm, node, n_trace);
131     }
132
133   if (PREDICT_TRUE (next_index == VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT))
134     {
135       vlib_next_frame_t *nf;
136       vlib_frame_t *f;
137       ethernet_input_frame_t *ef;
138       nf = vlib_node_runtime_get_next_frame (vm, node, next_index);
139       f = vlib_get_frame (vm, nf->frame_index);
140       f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX;
141
142       ef = vlib_frame_scalar_args (f);
143       ef->sw_if_index = rd->sw_if_index;
144       ef->hw_if_index = rd->hw_if_index;
145       //f->flags |= ETH_INPUT_FRAME_F_IP4_CKSUM_OK;
146     }
147
148   n_left_to_next -= n_rx_packets;
149   vlib_put_next_frame (vm, node, next_index, n_left_to_next);
150
151   vlib_increment_combined_counter
152     (vnm->interface_main.combined_sw_if_counters +
153      VNET_INTERFACE_COUNTER_RX, vm->thread_index,
154      rd->hw_if_index, n_rx_packets, n_rx_bytes);
155
156   rxq->n_enq -= n_rx_packets;
157   rdma_device_input_refill (vm, rd, rxq);
158
159   return n_rx_packets;
160 }
161
162 VLIB_NODE_FN (rdma_input_node) (vlib_main_t * vm,
163                                 vlib_node_runtime_t * node,
164                                 vlib_frame_t * frame)
165 {
166   u32 n_rx = 0;
167   rdma_main_t *rm = &rdma_main;
168   vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
169   vnet_device_and_queue_t *dq;
170
171   foreach_device_and_queue (dq, rt->devices_and_queues)
172   {
173     rdma_device_t *rd;
174     rd = vec_elt_at_index (rm->devices, dq->dev_instance);
175     if ((rd->flags & RDMA_DEVICE_F_ADMIN_UP) == 0)
176       continue;
177     n_rx += rdma_device_input_inline (vm, node, frame, rd, dq->queue_id);
178   }
179   return n_rx;
180 }
181
182 /* *INDENT-OFF* */
183 VLIB_REGISTER_NODE (rdma_input_node) = {
184   .name = "rdma-input",
185   .sibling_of = "device-input",
186   .format_trace = format_rdma_input_trace,
187   .type = VLIB_NODE_TYPE_INPUT,
188   .state = VLIB_NODE_STATE_DISABLED,
189   .n_errors = RDMA_INPUT_N_ERROR,
190   .error_strings = rdma_input_error_strings,
191 };
192
193 /* *INDENT-ON* */
194
195
196 /*
197  * fd.io coding-style-patch-verification: ON
198  *
199  * Local Variables:
200  * eval: (c-set-style "gnu")
201  * End:
202  */