1*0036d835Schristos /*
2*0036d835Schristos  * Copyright (c) 2017 Pure Storage, Inc.
3*0036d835Schristos  * All rights reserved.
4*0036d835Schristos  *
5*0036d835Schristos  * Redistribution and use in source and binary forms, with or without
6*0036d835Schristos  * modification, are permitted provided that the following conditions
7*0036d835Schristos  * are met:
8*0036d835Schristos  *
9*0036d835Schristos  * 1. Redistributions of source code must retain the above copyright
10*0036d835Schristos  * notice, this list of conditions and the following disclaimer.
11*0036d835Schristos  * 2. Redistributions in binary form must reproduce the above copyright
12*0036d835Schristos  * notice, this list of conditions and the following disclaimer in the
13*0036d835Schristos  * documentation and/or other materials provided with the distribution.
14*0036d835Schristos  * 3. The name of the author may not be used to endorse or promote
15*0036d835Schristos  * products derived from this software without specific prior written
16*0036d835Schristos  * permission.
17*0036d835Schristos  *
18*0036d835Schristos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*0036d835Schristos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*0036d835Schristos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*0036d835Schristos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*0036d835Schristos  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*0036d835Schristos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*0036d835Schristos  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*0036d835Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*0036d835Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*0036d835Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*0036d835Schristos  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*0036d835Schristos  */
30*0036d835Schristos 
31*0036d835Schristos #ifdef HAVE_CONFIG_H
32*0036d835Schristos #include "config.h"
33*0036d835Schristos #endif
34*0036d835Schristos 
35*0036d835Schristos #include "pcap-int.h"
36*0036d835Schristos #include "pcap-rdmasniff.h"
37*0036d835Schristos 
38*0036d835Schristos #include <infiniband/verbs.h>
39*0036d835Schristos #include <stdlib.h>
40*0036d835Schristos #include <string.h>
41*0036d835Schristos #include <sys/time.h>
42*0036d835Schristos 
43*0036d835Schristos #if !defined(IBV_FLOW_ATTR_SNIFFER)
44*0036d835Schristos #define IBV_FLOW_ATTR_SNIFFER	3
45*0036d835Schristos #endif
46*0036d835Schristos 
47*0036d835Schristos static const int RDMASNIFF_NUM_RECEIVES = 128;
48*0036d835Schristos static const int RDMASNIFF_RECEIVE_SIZE = 10000;
49*0036d835Schristos 
50*0036d835Schristos struct pcap_rdmasniff {
51*0036d835Schristos 	struct ibv_device *		rdma_device;
52*0036d835Schristos 	struct ibv_context *		context;
53*0036d835Schristos 	struct ibv_comp_channel *	channel;
54*0036d835Schristos 	struct ibv_pd *			pd;
55*0036d835Schristos 	struct ibv_cq *			cq;
56*0036d835Schristos 	struct ibv_qp *			qp;
57*0036d835Schristos 	struct ibv_flow *               flow;
58*0036d835Schristos 	struct ibv_mr *			mr;
59*0036d835Schristos 	u_char *			oneshot_buffer;
60*0036d835Schristos 	unsigned			port_num;
61*0036d835Schristos 	int                             cq_event;
62*0036d835Schristos 	u_int                           packets_recv;
63*0036d835Schristos };
64*0036d835Schristos 
65*0036d835Schristos static int
rdmasniff_stats(pcap_t * handle,struct pcap_stat * stat)66*0036d835Schristos rdmasniff_stats(pcap_t *handle, struct pcap_stat *stat)
67*0036d835Schristos {
68*0036d835Schristos 	struct pcap_rdmasniff *priv = handle->priv;
69*0036d835Schristos 
70*0036d835Schristos 	stat->ps_recv = priv->packets_recv;
71*0036d835Schristos 	stat->ps_drop = 0;
72*0036d835Schristos 	stat->ps_ifdrop = 0;
73*0036d835Schristos 
74*0036d835Schristos 	return 0;
75*0036d835Schristos }
76*0036d835Schristos 
77*0036d835Schristos static void
rdmasniff_cleanup(pcap_t * handle)78*0036d835Schristos rdmasniff_cleanup(pcap_t *handle)
79*0036d835Schristos {
80*0036d835Schristos 	struct pcap_rdmasniff *priv = handle->priv;
81*0036d835Schristos 
82*0036d835Schristos 	ibv_dereg_mr(priv->mr);
83*0036d835Schristos 	ibv_destroy_flow(priv->flow);
84*0036d835Schristos 	ibv_destroy_qp(priv->qp);
85*0036d835Schristos 	ibv_destroy_cq(priv->cq);
86*0036d835Schristos 	ibv_dealloc_pd(priv->pd);
87*0036d835Schristos 	ibv_destroy_comp_channel(priv->channel);
88*0036d835Schristos 	ibv_close_device(priv->context);
89*0036d835Schristos 	free(priv->oneshot_buffer);
90*0036d835Schristos 
91*0036d835Schristos 	pcap_cleanup_live_common(handle);
92*0036d835Schristos }
93*0036d835Schristos 
94*0036d835Schristos static void
rdmasniff_post_recv(pcap_t * handle,uint64_t wr_id)95*0036d835Schristos rdmasniff_post_recv(pcap_t *handle, uint64_t wr_id)
96*0036d835Schristos {
97*0036d835Schristos 	struct pcap_rdmasniff *priv = handle->priv;
98*0036d835Schristos 	struct ibv_sge sg_entry;
99*0036d835Schristos 	struct ibv_recv_wr wr, *bad_wr;
100*0036d835Schristos 
101*0036d835Schristos 	sg_entry.length = RDMASNIFF_RECEIVE_SIZE;
102*0036d835Schristos 	sg_entry.addr = (uintptr_t) handle->buffer + RDMASNIFF_RECEIVE_SIZE * wr_id;
103*0036d835Schristos 	sg_entry.lkey = priv->mr->lkey;
104*0036d835Schristos 
105*0036d835Schristos 	wr.wr_id = wr_id;
106*0036d835Schristos 	wr.num_sge = 1;
107*0036d835Schristos 	wr.sg_list = &sg_entry;
108*0036d835Schristos 	wr.next = NULL;
109*0036d835Schristos 
110*0036d835Schristos 	ibv_post_recv(priv->qp, &wr, &bad_wr);
111*0036d835Schristos }
112*0036d835Schristos 
113*0036d835Schristos static int
rdmasniff_read(pcap_t * handle,int max_packets,pcap_handler callback,u_char * user)114*0036d835Schristos rdmasniff_read(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
115*0036d835Schristos {
116*0036d835Schristos 	struct pcap_rdmasniff *priv = handle->priv;
117*0036d835Schristos 	struct ibv_cq *ev_cq;
118*0036d835Schristos 	void *ev_ctx;
119*0036d835Schristos 	struct ibv_wc wc;
120*0036d835Schristos 	struct pcap_pkthdr pkth;
121*0036d835Schristos 	u_char *pktd;
122*0036d835Schristos 	int count = 0;
123*0036d835Schristos 
124*0036d835Schristos 	if (!priv->cq_event) {
125*0036d835Schristos 		while (ibv_get_cq_event(priv->channel, &ev_cq, &ev_ctx) < 0) {
126*0036d835Schristos 			if (errno != EINTR) {
127*0036d835Schristos 				return PCAP_ERROR;
128*0036d835Schristos 			}
129*0036d835Schristos 			if (handle->break_loop) {
130*0036d835Schristos 				handle->break_loop = 0;
131*0036d835Schristos 				return PCAP_ERROR_BREAK;
132*0036d835Schristos 			}
133*0036d835Schristos 		}
134*0036d835Schristos 		ibv_ack_cq_events(priv->cq, 1);
135*0036d835Schristos 		ibv_req_notify_cq(priv->cq, 0);
136*0036d835Schristos 		priv->cq_event = 1;
137*0036d835Schristos 	}
138*0036d835Schristos 
139*0036d835Schristos 	while (count < max_packets || PACKET_COUNT_IS_UNLIMITED(max_packets)) {
140*0036d835Schristos 		if (ibv_poll_cq(priv->cq, 1, &wc) != 1) {
141*0036d835Schristos 			priv->cq_event = 0;
142*0036d835Schristos 			break;
143*0036d835Schristos 		}
144*0036d835Schristos 
145*0036d835Schristos 		if (wc.status != IBV_WC_SUCCESS) {
146*0036d835Schristos 			fprintf(stderr, "failed WC wr_id %lld status %d/%s\n",
147*0036d835Schristos 				(unsigned long long) wc.wr_id,
148*0036d835Schristos 				wc.status, ibv_wc_status_str(wc.status));
149*0036d835Schristos 			continue;
150*0036d835Schristos 		}
151*0036d835Schristos 
152*0036d835Schristos 		pkth.len = wc.byte_len;
153*0036d835Schristos 		pkth.caplen = min(pkth.len, (u_int)handle->snapshot);
154*0036d835Schristos 		gettimeofday(&pkth.ts, NULL);
155*0036d835Schristos 
156*0036d835Schristos 		pktd = (u_char *) handle->buffer + wc.wr_id * RDMASNIFF_RECEIVE_SIZE;
157*0036d835Schristos 
158*0036d835Schristos 		if (handle->fcode.bf_insns == NULL ||
159*0036d835Schristos 		    bpf_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) {
160*0036d835Schristos 			callback(user, &pkth, pktd);
161*0036d835Schristos 			++priv->packets_recv;
162*0036d835Schristos 			++count;
163*0036d835Schristos 		}
164*0036d835Schristos 
165*0036d835Schristos 		rdmasniff_post_recv(handle, wc.wr_id);
166*0036d835Schristos 
167*0036d835Schristos 		if (handle->break_loop) {
168*0036d835Schristos 			handle->break_loop = 0;
169*0036d835Schristos 			return PCAP_ERROR_BREAK;
170*0036d835Schristos 		}
171*0036d835Schristos 	}
172*0036d835Schristos 
173*0036d835Schristos 	return count;
174*0036d835Schristos }
175*0036d835Schristos 
176*0036d835Schristos static void
rdmasniff_oneshot(u_char * user,const struct pcap_pkthdr * h,const u_char * bytes)177*0036d835Schristos rdmasniff_oneshot(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
178*0036d835Schristos {
179*0036d835Schristos 	struct oneshot_userdata *sp = (struct oneshot_userdata *) user;
180*0036d835Schristos 	pcap_t *handle = sp->pd;
181*0036d835Schristos 	struct pcap_rdmasniff *priv = handle->priv;
182*0036d835Schristos 
183*0036d835Schristos 	*sp->hdr = *h;
184*0036d835Schristos 	memcpy(priv->oneshot_buffer, bytes, h->caplen);
185*0036d835Schristos 	*sp->pkt = priv->oneshot_buffer;
186*0036d835Schristos }
187*0036d835Schristos 
188*0036d835Schristos static int
rdmasniff_activate(pcap_t * handle)189*0036d835Schristos rdmasniff_activate(pcap_t *handle)
190*0036d835Schristos {
191*0036d835Schristos 	struct pcap_rdmasniff *priv = handle->priv;
192*0036d835Schristos 	struct ibv_qp_init_attr qp_init_attr;
193*0036d835Schristos 	struct ibv_qp_attr qp_attr;
194*0036d835Schristos 	struct ibv_flow_attr flow_attr;
195*0036d835Schristos 	struct ibv_port_attr port_attr;
196*0036d835Schristos 	int i;
197*0036d835Schristos 
198*0036d835Schristos 	priv->context = ibv_open_device(priv->rdma_device);
199*0036d835Schristos 	if (!priv->context) {
200*0036d835Schristos 		pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
201*0036d835Schristos 			      "Failed to open device %s", handle->opt.device);
202*0036d835Schristos 		goto error;
203*0036d835Schristos 	}
204*0036d835Schristos 
205*0036d835Schristos 	priv->pd = ibv_alloc_pd(priv->context);
206*0036d835Schristos 	if (!priv->pd) {
207*0036d835Schristos 		pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
208*0036d835Schristos 			      "Failed to alloc PD for device %s", handle->opt.device);
209*0036d835Schristos 		goto error;
210*0036d835Schristos 	}
211*0036d835Schristos 
212*0036d835Schristos 	priv->channel = ibv_create_comp_channel(priv->context);
213*0036d835Schristos 	if (!priv->channel) {
214*0036d835Schristos 		pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
215*0036d835Schristos 			      "Failed to create comp channel for device %s", handle->opt.device);
216*0036d835Schristos 		goto error;
217*0036d835Schristos 	}
218*0036d835Schristos 
219*0036d835Schristos 	priv->cq = ibv_create_cq(priv->context, RDMASNIFF_NUM_RECEIVES,
220*0036d835Schristos 				 NULL, priv->channel, 0);
221*0036d835Schristos 	if (!priv->cq) {
222*0036d835Schristos 		pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
223*0036d835Schristos 			      "Failed to create CQ for device %s", handle->opt.device);
224*0036d835Schristos 		goto error;
225*0036d835Schristos 	}
226*0036d835Schristos 
227*0036d835Schristos 	ibv_req_notify_cq(priv->cq, 0);
228*0036d835Schristos 
229*0036d835Schristos 	memset(&qp_init_attr, 0, sizeof qp_init_attr);
230*0036d835Schristos 	qp_init_attr.send_cq = qp_init_attr.recv_cq = priv->cq;
231*0036d835Schristos 	qp_init_attr.cap.max_recv_wr = RDMASNIFF_NUM_RECEIVES;
232*0036d835Schristos 	qp_init_attr.cap.max_recv_sge = 1;
233*0036d835Schristos 	qp_init_attr.qp_type = IBV_QPT_RAW_PACKET;
234*0036d835Schristos 	priv->qp = ibv_create_qp(priv->pd, &qp_init_attr);
235*0036d835Schristos 	if (!priv->qp) {
236*0036d835Schristos 		pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
237*0036d835Schristos 			      "Failed to create QP for device %s", handle->opt.device);
238*0036d835Schristos 		goto error;
239*0036d835Schristos 	}
240*0036d835Schristos 
241*0036d835Schristos 	memset(&qp_attr, 0, sizeof qp_attr);
242*0036d835Schristos 	qp_attr.qp_state = IBV_QPS_INIT;
243*0036d835Schristos 	qp_attr.port_num = priv->port_num;
244*0036d835Schristos 	if (ibv_modify_qp(priv->qp, &qp_attr, IBV_QP_STATE | IBV_QP_PORT)) {
245*0036d835Schristos 		pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
246*0036d835Schristos 			      "Failed to modify QP to INIT for device %s", handle->opt.device);
247*0036d835Schristos 		goto error;
248*0036d835Schristos 	}
249*0036d835Schristos 
250*0036d835Schristos 	memset(&qp_attr, 0, sizeof qp_attr);
251*0036d835Schristos 	qp_attr.qp_state = IBV_QPS_RTR;
252*0036d835Schristos 	if (ibv_modify_qp(priv->qp, &qp_attr, IBV_QP_STATE)) {
253*0036d835Schristos 		pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
254*0036d835Schristos 			      "Failed to modify QP to RTR for device %s", handle->opt.device);
255*0036d835Schristos 		goto error;
256*0036d835Schristos 	}
257*0036d835Schristos 
258*0036d835Schristos 	memset(&flow_attr, 0, sizeof flow_attr);
259*0036d835Schristos 	flow_attr.type = IBV_FLOW_ATTR_SNIFFER;
260*0036d835Schristos 	flow_attr.size = sizeof flow_attr;
261*0036d835Schristos 	flow_attr.port = priv->port_num;
262*0036d835Schristos 	priv->flow = ibv_create_flow(priv->qp, &flow_attr);
263*0036d835Schristos 	if (!priv->flow) {
264*0036d835Schristos 		pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
265*0036d835Schristos 			      "Failed to create flow for device %s", handle->opt.device);
266*0036d835Schristos 		goto error;
267*0036d835Schristos 	}
268*0036d835Schristos 
269*0036d835Schristos 	handle->bufsize = RDMASNIFF_NUM_RECEIVES * RDMASNIFF_RECEIVE_SIZE;
270*0036d835Schristos 	handle->buffer = malloc(handle->bufsize);
271*0036d835Schristos 	if (!handle->buffer) {
272*0036d835Schristos 		pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
273*0036d835Schristos 			      "Failed to allocate receive buffer for device %s", handle->opt.device);
274*0036d835Schristos 		goto error;
275*0036d835Schristos 	}
276*0036d835Schristos 
277*0036d835Schristos 	priv->oneshot_buffer = malloc(RDMASNIFF_RECEIVE_SIZE);
278*0036d835Schristos 	if (!priv->oneshot_buffer) {
279*0036d835Schristos 		pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
280*0036d835Schristos 			      "Failed to allocate oneshot buffer for device %s", handle->opt.device);
281*0036d835Schristos 		goto error;
282*0036d835Schristos 	}
283*0036d835Schristos 
284*0036d835Schristos 	priv->mr = ibv_reg_mr(priv->pd, handle->buffer, handle->bufsize, IBV_ACCESS_LOCAL_WRITE);
285*0036d835Schristos 	if (!priv->mr) {
286*0036d835Schristos 		pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
287*0036d835Schristos 			      "Failed to register MR for device %s", handle->opt.device);
288*0036d835Schristos 		goto error;
289*0036d835Schristos 	}
290*0036d835Schristos 
291*0036d835Schristos 
292*0036d835Schristos 	for (i = 0; i < RDMASNIFF_NUM_RECEIVES; ++i) {
293*0036d835Schristos 		rdmasniff_post_recv(handle, i);
294*0036d835Schristos 	}
295*0036d835Schristos 
296*0036d835Schristos 	if (!ibv_query_port(priv->context, priv->port_num, &port_attr) &&
297*0036d835Schristos 	    port_attr.link_layer == IBV_LINK_LAYER_INFINIBAND) {
298*0036d835Schristos 		handle->linktype = DLT_INFINIBAND;
299*0036d835Schristos 	} else {
300*0036d835Schristos 		handle->linktype = DLT_EN10MB;
301*0036d835Schristos 	}
302*0036d835Schristos 
303*0036d835Schristos 	if (handle->snapshot <= 0 || handle->snapshot > RDMASNIFF_RECEIVE_SIZE)
304*0036d835Schristos 		handle->snapshot = RDMASNIFF_RECEIVE_SIZE;
305*0036d835Schristos 
306*0036d835Schristos 	handle->offset = 0;
307*0036d835Schristos 	handle->read_op = rdmasniff_read;
308*0036d835Schristos 	handle->stats_op = rdmasniff_stats;
309*0036d835Schristos 	handle->cleanup_op = rdmasniff_cleanup;
310*0036d835Schristos 	handle->setfilter_op = install_bpf_program;
311*0036d835Schristos 	handle->setdirection_op = NULL;
312*0036d835Schristos 	handle->set_datalink_op = NULL;
313*0036d835Schristos 	handle->getnonblock_op = pcap_getnonblock_fd;
314*0036d835Schristos 	handle->setnonblock_op = pcap_setnonblock_fd;
315*0036d835Schristos 	handle->oneshot_callback = rdmasniff_oneshot;
316*0036d835Schristos 	handle->selectable_fd = priv->channel->fd;
317*0036d835Schristos 
318*0036d835Schristos 	return 0;
319*0036d835Schristos 
320*0036d835Schristos error:
321*0036d835Schristos 	if (priv->mr) {
322*0036d835Schristos 		ibv_dereg_mr(priv->mr);
323*0036d835Schristos 	}
324*0036d835Schristos 
325*0036d835Schristos 	if (priv->flow) {
326*0036d835Schristos 		ibv_destroy_flow(priv->flow);
327*0036d835Schristos 	}
328*0036d835Schristos 
329*0036d835Schristos 	if (priv->qp) {
330*0036d835Schristos 		ibv_destroy_qp(priv->qp);
331*0036d835Schristos 	}
332*0036d835Schristos 
333*0036d835Schristos 	if (priv->cq) {
334*0036d835Schristos 		ibv_destroy_cq(priv->cq);
335*0036d835Schristos 	}
336*0036d835Schristos 
337*0036d835Schristos 	if (priv->channel) {
338*0036d835Schristos 		ibv_destroy_comp_channel(priv->channel);
339*0036d835Schristos 	}
340*0036d835Schristos 
341*0036d835Schristos 	if (priv->pd) {
342*0036d835Schristos 		ibv_dealloc_pd(priv->pd);
343*0036d835Schristos 	}
344*0036d835Schristos 
345*0036d835Schristos 	if (priv->context) {
346*0036d835Schristos 		ibv_close_device(priv->context);
347*0036d835Schristos 	}
348*0036d835Schristos 
349*0036d835Schristos 	if (priv->oneshot_buffer) {
350*0036d835Schristos 		free(priv->oneshot_buffer);
351*0036d835Schristos 	}
352*0036d835Schristos 
353*0036d835Schristos 	return PCAP_ERROR;
354*0036d835Schristos }
355*0036d835Schristos 
356*0036d835Schristos pcap_t *
rdmasniff_create(const char * device,char * ebuf,int * is_ours)357*0036d835Schristos rdmasniff_create(const char *device, char *ebuf, int *is_ours)
358*0036d835Schristos {
359*0036d835Schristos 	struct pcap_rdmasniff *priv;
360*0036d835Schristos 	struct ibv_device **dev_list;
361*0036d835Schristos 	int numdev;
362*0036d835Schristos 	size_t namelen;
363*0036d835Schristos 	const char *port;
364*0036d835Schristos 	unsigned port_num;
365*0036d835Schristos 	int i;
366*0036d835Schristos 	pcap_t *p = NULL;
367*0036d835Schristos 
368*0036d835Schristos 	*is_ours = 0;
369*0036d835Schristos 
370*0036d835Schristos 	dev_list = ibv_get_device_list(&numdev);
371*0036d835Schristos 	if (!dev_list || !numdev) {
372*0036d835Schristos 		return NULL;
373*0036d835Schristos 	}
374*0036d835Schristos 
375*0036d835Schristos 	namelen = strlen(device);
376*0036d835Schristos 
377*0036d835Schristos 	port = strchr(device, ':');
378*0036d835Schristos 	if (port) {
379*0036d835Schristos 		port_num = strtoul(port + 1, NULL, 10);
380*0036d835Schristos 		if (port_num > 0) {
381*0036d835Schristos 			namelen = port - device;
382*0036d835Schristos 		} else {
383*0036d835Schristos 			port_num = 1;
384*0036d835Schristos 		}
385*0036d835Schristos 	} else {
386*0036d835Schristos 		port_num = 1;
387*0036d835Schristos 	}
388*0036d835Schristos 
389*0036d835Schristos 	for (i = 0; i < numdev; ++i) {
390*0036d835Schristos 		if (strlen(dev_list[i]->name) == namelen &&
391*0036d835Schristos 		    !strncmp(device, dev_list[i]->name, namelen)) {
392*0036d835Schristos 			*is_ours = 1;
393*0036d835Schristos 
394*0036d835Schristos 			p = pcap_create_common(ebuf, sizeof (struct pcap_rdmasniff));
395*0036d835Schristos 			if (p) {
396*0036d835Schristos 				p->activate_op = rdmasniff_activate;
397*0036d835Schristos 				priv = p->priv;
398*0036d835Schristos 				priv->rdma_device = dev_list[i];
399*0036d835Schristos 				priv->port_num = port_num;
400*0036d835Schristos 			}
401*0036d835Schristos 			break;
402*0036d835Schristos 		}
403*0036d835Schristos 	}
404*0036d835Schristos 
405*0036d835Schristos 	ibv_free_device_list(dev_list);
406*0036d835Schristos 	return p;
407*0036d835Schristos }
408*0036d835Schristos 
409*0036d835Schristos int
rdmasniff_findalldevs(pcap_if_list_t * devlistp,char * err_str)410*0036d835Schristos rdmasniff_findalldevs(pcap_if_list_t *devlistp, char *err_str)
411*0036d835Schristos {
412*0036d835Schristos 	struct ibv_device **dev_list;
413*0036d835Schristos 	int numdev;
414*0036d835Schristos 	int i;
415*0036d835Schristos 	int ret = 0;
416*0036d835Schristos 
417*0036d835Schristos 	dev_list = ibv_get_device_list(&numdev);
418*0036d835Schristos 	if (!dev_list || !numdev) {
419*0036d835Schristos 		return 0;
420*0036d835Schristos 	}
421*0036d835Schristos 
422*0036d835Schristos 	for (i = 0; i < numdev; ++i) {
423*0036d835Schristos 		/*
424*0036d835Schristos 		 * XXX - do the notions of "up", "running", or
425*0036d835Schristos 		 * "connected" apply here?
426*0036d835Schristos 		 */
427*0036d835Schristos 		if (!add_dev(devlistp, dev_list[i]->name, 0, "RDMA sniffer", err_str)) {
428*0036d835Schristos 			ret = -1;
429*0036d835Schristos 			goto out;
430*0036d835Schristos 		}
431*0036d835Schristos 	}
432*0036d835Schristos 
433*0036d835Schristos out:
434*0036d835Schristos 	ibv_free_device_list(dev_list);
435*0036d835Schristos 	return ret;
436*0036d835Schristos }
437