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