forked from rosenbaumalex/dcping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathibv_helper.h
182 lines (157 loc) · 5.13 KB
/
ibv_helper.h
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
/*
* Copyright (c) 2019 Mellanox Technologies, Inc. All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* OpenIB.org BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <dirent.h>
static int ibv_read_sysfs_file(const char *dir, const char *file,
char *buf, size_t size)
{
char *path;
int fd;
int len;
if (asprintf(&path, "%s/%s", dir, file) < 0)
return -1;
fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
free(path);
return -1;
}
len = read(fd, buf, size);
close(fd);
free(path);
if (len > 0) {
if (buf[len - 1] == '\n')
buf[--len] = '\0';
else if (len < size)
buf[len] = '\0';
else
/* We would have to truncate the contents to NULL
* terminate, so we are going to fail no matter
* what we do, either right now or later when
* we pass around an unterminated string. Fail now.
*/
return -1;
}
return len;
}
/* GID types as appear in sysfs, no change is expected as of ABI
* compatibility.
*/
#define V1_TYPE "IB/RoCE v1"
#define V2_TYPE "RoCE v2"
static int ibv_query_gid_type(struct ibv_context *context, uint8_t port_num,
unsigned int index, enum ibv_gid_type *type)
{
char name[32];
char buff[11];
snprintf(name, sizeof(name), "ports/%d/gid_attrs/types/%d", port_num,
index);
/* Reset errno so that we can rely on its value upon any error flow in
* ibv_read_sysfs_file.
*/
errno = 0;
if (ibv_read_sysfs_file(context->device->ibdev_path, name, buff,
sizeof(buff)) <= 0) {
char *dir_path;
DIR *dir;
if (errno == EINVAL) {
/* In IB, this file doesn't exist and the kernel sets
* errno to -EINVAL.
*/
*type = IBV_GID_TYPE_ROCE_V1;
return 0;
}
if (asprintf(&dir_path, "%s/%s/%d/%s/",
context->device->ibdev_path, "ports", port_num,
"gid_attrs") < 0)
return -1;
dir = opendir(dir_path);
free(dir_path);
if (!dir) {
if (errno == ENOENT)
/* Assuming that if gid_attrs doesn't exist,
* we have an old kernel and all GIDs are
* IB/RoCE v1
*/
*type = IBV_GID_TYPE_ROCE_V1;
else
return -1;
} else {
closedir(dir);
errno = EFAULT;
return -1;
}
} else {
if (!strcmp(buff, V1_TYPE)) {
*type = IBV_GID_TYPE_ROCE_V1;
} else if (!strcmp(buff, V2_TYPE)) {
*type = IBV_GID_TYPE_ROCE_V2;
} else {
errno = ENOTSUP;
return -1;
}
}
return 0;
}
void ibv_find_gid_family(union ibv_gid *gid, int *gid_family)
{
if (gid->raw[0] == 0 && gid->raw[1] == 0)
*gid_family = AF_INET;
else
*gid_family = AF_INET6;
}
int ibv_find_sgid_type(struct ibv_context *context, uint8_t port_num,
enum ibv_gid_type gid_type, int gid_family)
{
enum ibv_gid_type sgid_type = 0;
union ibv_gid sgid;
int sgid_family = -1;
int idx = 0;
do {
if (ibv_query_gid(context, port_num, idx, &sgid)) {
errno = EFAULT;
return -1;
}
if (ibv_query_gid_type(context, port_num, idx, &sgid_type)) {
errno = EFAULT;
return -1;
}
ibv_find_gid_family(&sgid, &sgid_family);
if (gid_type == sgid_type && gid_family == sgid_family) {
return idx;
}
idx++;
} while (gid_type != sgid_type || gid_family != sgid_family);
return idx;
}