1 /*
2  * Copyright (c) 2018 Intel Corporation. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <stdlib.h>
34 #include "ofi_hook.h"
35 
36 
hook_do_poll(struct fid_poll * pollset,void ** context,int count)37 static int hook_do_poll(struct fid_poll *pollset, void **context, int count)
38 {
39 	struct hook_poll *poll = container_of(pollset, struct hook_poll, poll);
40 	struct fid *fid;
41 	int i, ret;
42 
43 	ret = fi_poll(poll->hpoll, context, count);
44 	for (i = 0; i < ret; i++) {
45 		fid = context[i];
46 		context[i] = fid->context;
47 	}
48 
49 	return ret;
50 }
51 
hook_poll_add(struct fid_poll * pollset,struct fid * event_fid,uint64_t flags)52 static int hook_poll_add(struct fid_poll *pollset, struct fid *event_fid,
53 			 uint64_t flags)
54 {
55 	struct hook_poll *poll = container_of(pollset, struct hook_poll, poll);
56 
57 	return fi_poll_add(poll->hpoll, hook_to_hfid(event_fid), flags);
58 }
59 
hook_poll_del(struct fid_poll * pollset,struct fid * event_fid,uint64_t flags)60 static int hook_poll_del(struct fid_poll *pollset, struct fid *event_fid,
61 			 uint64_t flags)
62 {
63 	struct hook_poll *poll = container_of(pollset, struct hook_poll, poll);
64 
65 	return fi_poll_del(poll->hpoll, hook_to_hfid(event_fid), flags);
66 }
67 
68 static struct fi_ops_poll hook_poll_ops = {
69 	.size = sizeof(struct fi_ops_poll),
70 	.poll = hook_do_poll,
71 	.poll_add = hook_poll_add,
72 	.poll_del = hook_poll_del,
73 };
74 
hook_poll_open(struct fid_domain * domain,struct fi_poll_attr * attr,struct fid_poll ** pollset)75 int hook_poll_open(struct fid_domain *domain, struct fi_poll_attr *attr,
76 		   struct fid_poll **pollset)
77 {
78 	struct hook_domain *dom = container_of(domain, struct hook_domain, domain);
79 	struct hook_poll *poll;
80 	int ret;
81 
82 	poll = calloc(1, sizeof *poll);
83 	if (!poll)
84 		return -FI_ENOMEM;
85 
86 	poll->domain = dom;
87 	poll->poll.fid.fclass = FI_CLASS_POLL;
88 	poll->poll.fid.ops = &hook_fid_ops;
89 	poll->poll.ops = &hook_poll_ops;
90 
91 	ret = fi_poll_open(dom->hdomain, attr, &poll->hpoll);
92 	if (ret)
93 		free(poll);
94 	else
95 		*pollset = &poll->poll;
96 
97 	return ret;
98 }
99 
100 
hook_trywait(struct fid_fabric * fabric,struct fid ** fids,int count)101 int hook_trywait(struct fid_fabric *fabric, struct fid **fids, int count)
102 {
103 	struct hook_fabric *fab = container_of(fabric, struct hook_fabric, fabric);
104 	struct fid *hfid;
105 	int i, ret;
106 
107 	for (i = 0; i < count; i++) {
108 		hfid = hook_to_hfid(fids[i]);
109 		if (!hfid)
110 			return -FI_EINVAL;
111 
112 		ret = fi_trywait(fab->hfabric, &hfid, 1);
113 		if (ret)
114 			return ret;
115 	}
116 
117 	return 0;
118 }
119 
hook_do_wait(struct fid_wait * waitset,int timeout)120 static int hook_do_wait(struct fid_wait *waitset, int timeout)
121 {
122 	struct hook_wait *wait = container_of(waitset, struct hook_wait, wait);
123 
124 	return fi_wait(wait->hwait, timeout);
125 }
126 
127 static struct fi_ops_wait hook_wait_ops = {
128 	.size = sizeof(struct fi_ops_wait),
129 	.wait = hook_do_wait,
130 };
131 
hook_wait_open(struct fid_fabric * fabric,struct fi_wait_attr * attr,struct fid_wait ** waitset)132 int hook_wait_open(struct fid_fabric *fabric, struct fi_wait_attr *attr,
133 		   struct fid_wait **waitset)
134 {
135 	struct hook_fabric *fab = container_of(fabric, struct hook_fabric, fabric);
136 	struct hook_wait *wait;
137 	int ret;
138 
139 	wait = calloc(1, sizeof *wait);
140 	if (!wait)
141 		return -FI_ENOMEM;
142 
143 	wait->fabric = fab;
144 	wait->wait.fid.fclass = FI_CLASS_WAIT;
145 	wait->wait.fid.ops = &hook_fid_ops;
146 	wait->wait.ops = &hook_wait_ops;
147 
148 	ret = fi_wait_open(fab->hfabric, attr, &wait->hwait);
149 	if (ret)
150 		free(wait);
151 	else
152 		*waitset = &wait->wait;
153 
154 	return ret;
155 }
156 
157