Motr  M0
net.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 /*
24  * Compile separately if not building "altogether".
25  */
26 
27 #include "lib/errno.h"
28 #include "lib/memory.h"
29 #include "lib/misc.h"
30 #include "lib/mutex.h"
31 #ifndef __KERNEL__
32 # include "lib/string.h" /* m0_streq */
33 #endif
34 
35 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_NET
36 #include "lib/trace.h"
37 
38 #include "net/net_otw_types.h"
39 #include "net/net.h"
40 #include "rpc/rpc_machine.h" /* M0_RPC_DEF_MAX_RPC_MSG_SIZE */
41 
42 #define XPRT_MAX 4
43 
44 static struct m0_net_xprt *xprts[XPRT_MAX] = { NULL };
45 static struct m0_net_xprt *xprt_default = NULL;
60 
63 M0_INTERNAL int m0_net_init(void)
64 {
66  return 0;
67 }
68 
69 M0_INTERNAL void m0_net_fini(void)
70 {
72 }
73 
74 M0_INTERNAL int m0_net_desc_copy(const struct m0_net_buf_desc *from_desc,
75  struct m0_net_buf_desc *to_desc)
76 {
77  M0_PRE(from_desc->nbd_len > 0);
78  M0_ALLOC_ARR(to_desc->nbd_data, from_desc->nbd_len);
79  if (to_desc->nbd_data == NULL)
80  return M0_ERR(-ENOMEM);
81  memcpy(to_desc->nbd_data, from_desc->nbd_data, from_desc->nbd_len);
82  to_desc->nbd_len = from_desc->nbd_len;
83  return 0;
84 }
85 M0_EXPORTED(m0_net_desc_copy);
86 
87 M0_INTERNAL void m0_net_desc_free(struct m0_net_buf_desc *desc)
88 {
89  if (desc->nbd_len > 0) {
90  M0_PRE(desc->nbd_data != NULL);
91  m0_free(desc->nbd_data);
92  desc->nbd_len = 0;
93  }
94  desc->nbd_data = NULL;
95 }
96 M0_EXPORTED(m0_net_desc_free);
97 
98 #ifndef __KERNEL__
99 M0_INTERNAL bool m0_net_endpoint_is_valid(const char *endpoint)
100 {
101  char addr[16]; /* strlen("255.255.255.255") + 1 */
102  const char *networks[] = { "@lo", "@tcp", "@o2ib" };
103  int32_t n[4];
104  size_t i;
105  int rc;
106 
107  rc = sscanf(endpoint, "%15[0-9.]", addr);
108  if (rc != 1)
109  return M0_RC(false);
110  endpoint += strlen(addr); /* skip address part */
111 
112  if (!m0_exists(j, ARRAY_SIZE(networks),
113  m0_startswith(networks[i = j], endpoint)))
114  return M0_RC(false);
115  endpoint += strlen(networks[i]);
116 
117  if (m0_streq(networks[i], "@lo")) {
118  if (!m0_streq(addr, "0"))
119  return M0_RC(false);
120  } else {
121  rc = sscanf(addr, "%d.%d.%d.%d", &n[0], &n[1], &n[2], &n[3]);
122  if (rc != 4 ||
123  m0_exists(i, ARRAY_SIZE(n), n[i] < 0 || n[i] > 255))
124  return M0_RC(false); /* invalid IPv4 address */
125  if (isdigit(*endpoint))
126  ++endpoint; /* skip optional digit */
127  }
128 
129  if (!m0_startswith(":12345", endpoint))
130  return M0_RC(false);
131  endpoint += 6; /* strlen(":12345") */
132 
133  for (i = 0; i < 2; ++i) {
134  rc = sscanf(endpoint, ":%15[0-9]", addr);
135  if (rc != 1)
136  return M0_RC(false);
137  endpoint += 1 + strlen(addr); /* 1 is for ':' */
138  }
139  return M0_RC(*endpoint == '\0');
140 }
141 #endif /* !__KERNEL__ */
142 
143 M0_INTERNAL void m0_net_xprt_default_set(const struct m0_net_xprt *xprt)
144 {
145  M0_ENTRY();
146  M0_LOG(M0_DEBUG, "setting default xprt to %p:%s", xprt, xprt->nx_name);
147  xprt_default = (struct m0_net_xprt *) xprt;
148 }
149 M0_EXPORTED(m0_net_xprt_default_set);
150 
152 {
153  M0_ENTRY();
154  M0_LOG(M0_DEBUG, "getting default xprt to %p:%s",
155  xprt_default,
157  return xprt_default;
158 }
159 M0_EXPORTED(m0_net_xprt_default_get);
160 
162 {
163  M0_ENTRY();
164  return xprts;
165 }
166 M0_EXPORTED(m0_net_all_xprt_get);
167 
168 int m0_net_xprt_nr(void)
169 {
170  int i;
171  int count = 0;
172 
173  M0_ENTRY();
174  for (i = 0; i < ARRAY_SIZE(xprts); i++) {
175  if (xprts[i] != NULL)
176  count++;
177  }
178  return count;
179 }
180 M0_EXPORTED(m0_net_xprt_nr);
181 
182 M0_INTERNAL void m0_net_xprt_register(const struct m0_net_xprt *xprt)
183 {
184  int i;
185 
186  for (i = 0; i < ARRAY_SIZE(xprts); ++i) {
187  M0_ASSERT(xprts[i] != xprt);
188  if (xprts[i] == NULL) {
189  xprts[i] = (struct m0_net_xprt *) xprt;
190  return;
191  }
192  }
193  M0_IMPOSSIBLE("Too many xprts.");
194 }
195 M0_EXPORTED(m0_net_xprt_register);
196 
197 M0_INTERNAL void m0_net_xprt_deregister(const struct m0_net_xprt *xprt)
198 {
199  int i;
200  int j;
201  for (i = 0; i < ARRAY_SIZE(xprts); ++i) {
202  if (xprts[i] == xprt) {
203  if (xprt == xprt_default)
204  xprt_default = NULL;
205  for (j = i; j < ARRAY_SIZE(xprts) - 1; ++j)
206  xprts[j] = xprts[j + 1];
207  xprts[j] = NULL;
208  return;
209  }
210  }
211  M0_IMPOSSIBLE("Wrong xprt.");
212 }
213 M0_EXPORTED(m0_net_xprt_deregister);
214 
215 M0_INTERNAL void m0_net_print_xprt(void)
216 {
217  int i;
218  for (i = 0; i < ARRAY_SIZE(xprts); ++i) {
219  if (xprts[i] != NULL)
220  M0_LOG(M0_DEBUG, "xprt name:%s", xprts[i]->nx_name);
221  }
222 }
223 M0_EXPORTED(m0_net_print_xprt);
224 
225 M0_INTERNAL bool m0_net_check_xprt(const struct m0_net_xprt *xprt)
226 {
227  bool found = false;
228  int i;
229 
230  for (i = 0; i < ARRAY_SIZE(xprts); ++i)
231  {
232  if (xprts[i] == xprt)
233  found = true;
234  }
235  return found;
236 }
237 M0_EXPORTED(m0_net_check_xprt);
238 
240 {
241  M0_PRE(ndom != NULL);
242 
244 }
245 M0_EXPORTED(default_xo_rpc_max_seg_size);
246 
247 M0_INTERNAL uint32_t default_xo_rpc_max_segs_nr(struct m0_net_domain *ndom)
248 {
249  M0_PRE(ndom != NULL);
250 
251  return 1;
252 }
253 M0_EXPORTED(default_xo_rpc_max_segs_nr);
254 
256  m0_bcount_t rpc_size)
257 {
258  m0_bcount_t mbs;
259 
260  M0_PRE(ndom != NULL);
261 
263  return rpc_size != 0 ? m0_clip64u(M0_SEG_SIZE, mbs, rpc_size) : mbs;
264 }
265 M0_EXPORTED(default_xo_rpc_max_msg_size);
266 
267 M0_INTERNAL uint32_t default_xo_rpc_max_recv_msgs(struct m0_net_domain *ndom,
268  m0_bcount_t rpc_size)
269 {
270  M0_PRE(ndom != NULL);
271 
272  return 1;
273 }
274 M0_EXPORTED(default_xo_rpc_max_recv_msgs);
275 
276 
277 #undef M0_TRACE_SUBSYSTEM
278 
279 /*
280  * Local variables:
281  * c-indentation-style: "K&R"
282  * c-basic-offset: 8
283  * tab-width: 8
284  * fill-column: 80
285  * scroll-step: 1
286  * End:
287  */
static struct m0_net_xprt * xprts[XPRT_MAX]
Definition: net.c:44
M0_INTERNAL bool m0_net_endpoint_is_valid(const char *endpoint)
Definition: net.c:99
#define M0_PRE(cond)
#define M0_ALLOC_ARR(arr, nr)
Definition: memory.h:84
#define XPRT_MAX
Definition: net.c:42
#define NULL
Definition: misc.h:38
uint32_t nbd_len
Definition: net_otw_types.h:37
#define M0_LOG(level,...)
Definition: trace.h:167
uint8_t * nbd_data
Definition: net_otw_types.h:38
M0_INTERNAL void m0_net_xprt_default_set(const struct m0_net_xprt *xprt)
Definition: net.c:143
#define m0_exists(var, nr,...)
Definition: misc.h:134
uint64_t m0_bcount_t
Definition: types.h:77
struct m0_mutex m0_net_mutex
Definition: net.c:59
M0_INTERNAL bool m0_startswith(const char *prefix, const char *str)
Definition: string.c:98
static m0_bcount_t count
Definition: xcode.c:167
M0_INTERNAL uint32_t default_xo_rpc_max_segs_nr(struct m0_net_domain *ndom)
Definition: net.c:247
return M0_RC(rc)
static uint64_t m0_clip64u(uint64_t lo, uint64_t hi, uint64_t x)
Definition: arith.h:131
#define M0_ENTRY(...)
Definition: trace.h:170
int i
Definition: dir.c:1033
M0_INTERNAL void m0_net_xprt_register(const struct m0_net_xprt *xprt)
Definition: net.c:182
return M0_ERR(-EOPNOTSUPP)
#define M0_ASSERT(cond)
static struct m0_net_xprt * xprt_default
Definition: net.c:45
M0_INTERNAL void m0_net_fini(void)
Definition: net.c:69
#define m0_streq(a, b)
Definition: string.h:34
M0_INTERNAL int m0_net_init(void)
Definition: net.c:63
M0_INTERNAL void m0_net_print_xprt(void)
Definition: net.c:215
int m0_net_xprt_nr(void)
Definition: net.c:168
M0_INTERNAL m0_bcount_t default_xo_rpc_max_seg_size(struct m0_net_domain *ndom)
Definition: net.c:239
struct m0_net_xprt * m0_net_xprt_default_get(void)
Definition: net.c:151
M0_INTERNAL void m0_mutex_init(struct m0_mutex *mutex)
Definition: mutex.c:35
Definition: xcode.h:73
M0_INTERNAL void m0_net_desc_free(struct m0_net_buf_desc *desc)
Definition: net.c:87
M0_INTERNAL m0_bcount_t m0_net_domain_get_max_buffer_size(struct m0_net_domain *dom)
M0_INTERNAL int m0_net_desc_copy(const struct m0_net_buf_desc *from_desc, struct m0_net_buf_desc *to_desc)
Definition: net.c:74
uint64_t n
Definition: fops.h:107
M0_INTERNAL bool m0_net_check_xprt(const struct m0_net_xprt *xprt)
Definition: net.c:225
struct m0_net_xprt ** m0_net_all_xprt_get(void)
Definition: net.c:161
M0_INTERNAL void m0_mutex_fini(struct m0_mutex *mutex)
Definition: mutex.c:42
const char * nx_name
Definition: net.h:125
M0_INTERNAL void m0_net_xprt_deregister(const struct m0_net_xprt *xprt)
Definition: net.c:197
static uint64_t found
Definition: base.c:376
struct m0_net_xprt * xprt
Definition: module.c:61
M0_INTERNAL uint32_t default_xo_rpc_max_recv_msgs(struct m0_net_domain *ndom, m0_bcount_t rpc_size)
Definition: net.c:267
void m0_free(void *data)
Definition: memory.c:146
Definition: mutex.h:47
M0_INTERNAL m0_bcount_t default_xo_rpc_max_msg_size(struct m0_net_domain *ndom, m0_bcount_t rpc_size)
Definition: net.c:255
int32_t rc
Definition: trigger_fop.h:47
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_IMPOSSIBLE(fmt,...)