Motr  M0
mem_xprt_ep.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 /* This file is included into mem_xprt_xo.c */
24 
25 #ifndef __KERNEL__
26 #include <stdio.h> /* sprintf */
27 #endif
28 
40 static void mem_xo_end_point_release(struct m0_ref *ref)
41 {
42  struct m0_net_end_point *ep;
43  struct m0_net_bulk_mem_domain_pvt *dp;
44 
45  ep = container_of(ref, struct m0_net_end_point, nep_ref);
46  M0_PRE(m0_mutex_is_locked(&ep->nep_tm->ntm_mutex));
48 
49  dp = mem_dom_to_pvt(ep->nep_tm->ntm_dom);
50  m0_nep_tlist_del(ep);
51  ep->nep_tm = NULL;
52  dp->xd_ops->bmo_ep_free(mem_ep_to_pvt(ep)); /* indirect free */
53 }
54 
57  const struct sockaddr_in *sa,
58  uint32_t id)
59 {
60  char dot_ip[17];
61  int i;
62  size_t len = 0;
63  in_addr_t a = ntohl(sa->sin_addr.s_addr);
64  int nib[4];
65  for (i = 3; i >= 0; i--) {
66  nib[i] = a & 0xff;
67  a >>= 8;
68  }
69  for (i = 0; i < 4; ++i) {
70  len += sprintf(&dot_ip[len], "%d.", nib[i]);
71  }
72  M0_ASSERT(len < sizeof(dot_ip));
73  dot_ip[len-1] = '\0';
74  if (id > 0)
75  sprintf(mep->xep_addr, "%s:%u:%u", dot_ip,
76  ntohs(sa->sin_port), id);
77  else
78  sprintf(mep->xep_addr, "%s:%u", dot_ip,
79  ntohs(sa->sin_port));
81 }
82 
87 {
88  struct m0_net_bulk_mem_end_point *mep;
89  M0_ALLOC_PTR(mep);
90  return mep;
91 }
92 
96 static void mem_ep_free(struct m0_net_bulk_mem_end_point *mep)
97 {
98  m0_free(mep);
99 }
100 
101 static void mem_ep_get(struct m0_net_end_point *ep)
102 {
104 }
105 
109 static int mem_ep_create(struct m0_net_end_point **epp,
110  struct m0_net_transfer_mc *tm,
111  const struct sockaddr_in *sa,
112  uint32_t id)
113 {
114  struct m0_net_bulk_mem_end_point *mep;
115  struct m0_net_bulk_mem_domain_pvt *dp;
116  struct m0_net_end_point *ep;
117 
118  M0_PRE(m0_mutex_is_locked(&tm->ntm_mutex));
120  dp = mem_dom_to_pvt(tm->ntm_dom);
121 
122  /* check if its already on the TM end point list */
123  m0_tl_for(m0_nep, &tm->ntm_end_points, ep) {
125  mep = mem_ep_to_pvt(ep);
126  if (mem_sa_eq(&mep->xep_sa, sa) && mep->xep_service_id == id) {
127  dp->xd_ops->bmo_ep_get(ep);
128  *epp = ep;
129  return 0;
130  }
131  } m0_tl_endfor;
132 
133  /* allocate a new end point of appropriate size */
134  mep = dp->xd_ops->bmo_ep_alloc(); /* indirect alloc */
135  if (mep == NULL)
136  return M0_ERR(-ENOMEM);
138  mep->xep_sa.sin_addr = sa->sin_addr;
139  mep->xep_sa.sin_port = sa->sin_port;
140  mep->xep_service_id = id;
141  mem_ep_printable(mep, sa, id);
142  ep = &mep->xep_ep;
143  m0_ref_init(&ep->nep_ref, 1, dp->xd_ops->bmo_ep_release);
144  ep->nep_tm = tm;
145  m0_nep_tlink_init_at_tail(ep, &tm->ntm_end_points);
146  ep->nep_addr = &mep->xep_addr[0];
147  M0_ASSERT(mem_ep_to_pvt(ep) == mep);
149  *epp = ep;
150  return 0;
151 }
152 
161 static bool mem_ep_equals_addr(const struct m0_net_end_point *ep,
162  const struct sockaddr_in *sa)
163 {
164  const struct m0_net_bulk_mem_end_point *mep;
165 
167  mep = mem_ep_to_pvt(ep);
168 
169  return mem_sa_eq(&mep->xep_sa, sa);
170 }
171 
179 static bool mem_eps_are_equal(const struct m0_net_end_point *ep1,
180  const struct m0_net_end_point *ep2)
181 {
182  struct m0_net_bulk_mem_end_point *mep1;
183 
184  M0_ASSERT(ep1 != NULL && ep2 != NULL);
186  if (ep1 == ep2)
187  return true;
188 
189  mep1 = mem_ep_to_pvt(ep1);
190  return mem_ep_equals_addr(ep2, &mep1->xep_sa);
191 }
192 
205 static int mem_desc_create(struct m0_net_buf_desc *desc,
206  struct m0_net_transfer_mc *tm,
207  enum m0_net_queue_type qt,
208  m0_bcount_t buflen,
209  int64_t buf_id)
210 {
211  struct mem_desc *md;
212  struct m0_net_bulk_mem_end_point *mep;
213 
214  desc->nbd_len = sizeof *md;
215  md = m0_alloc(desc->nbd_len);
216  desc->nbd_data = (typeof(desc->nbd_data)) md;
217  if (desc->nbd_data == NULL) {
218  desc->nbd_len = 0;
219  return M0_ERR(-ENOMEM);
220  }
221 
222  /* copy the passive end point address */
223  mep = mem_ep_to_pvt(tm->ntm_ep);
224  md->md_passive = mep->xep_sa;
225 
226  md->md_qt = qt;
227  md->md_len = buflen;
228  md->md_buf_id = buf_id;
229 
230  return 0;
231 }
232 
242 static int mem_desc_decode(struct m0_net_buf_desc *desc,
243  struct mem_desc **p_md)
244 {
245  if (desc->nbd_len != sizeof **p_md ||
246  desc->nbd_data == NULL)
247  return M0_ERR(-EINVAL);
248  *p_md = (struct mem_desc *) desc->nbd_data;
249  return 0;
250 }
251 
255 static bool mem_desc_equal(struct m0_net_buf_desc *d1,
256  struct m0_net_buf_desc *d2)
257 {
258  /* could do a byte comparison too */
259  struct mem_desc *md1 = NULL;
260  struct mem_desc *md2 = NULL;
261  int rc;
262  rc = mem_desc_decode(d1, &md1);
263  if (rc == 0)
264  rc = mem_desc_decode(d2, &md2);
265  if (rc != 0)
266  return false;
267  if (md1->md_buf_id == md2->md_buf_id &&
268  mem_sa_eq(&md1->md_passive, &md2->md_passive))
269  return true;
270  return false;
271 }
272 
277 /*
278  * Local variables:
279  * c-indentation-style: "K&R"
280  * c-basic-offset: 8
281  * tab-width: 8
282  * fill-column: 80
283  * scroll-step: 1
284  * End:
285  */
static bool mem_sa_eq(const struct sockaddr_in *sa1, const struct sockaddr_in *sa2)
Definition: mem_xprt.h:446
uint64_t id
Definition: cob.h:2380
static struct m0_net_bulk_mem_end_point * mem_ep_alloc(void)
Definition: mem_xprt_ep.c:86
#define M0_PRE(cond)
struct m0_net_bulk_mem_end_point *(* bmo_ep_alloc)(void)
Definition: mem_xprt.h:290
#define NULL
Definition: misc.h:38
uint32_t nbd_len
Definition: net_otw_types.h:37
static bool mem_tm_invariant(const struct m0_net_transfer_mc *tm)
static struct m0_mdstore md
Definition: sd_common.c:42
uint8_t * nbd_data
Definition: net_otw_types.h:38
struct m0_net_domain * ntm_dom
Definition: net.h:853
uint64_t m0_bcount_t
Definition: types.h:77
#define container_of(ptr, type, member)
Definition: misc.h:33
static void mem_ep_printable(struct m0_net_bulk_mem_end_point *mep, const struct sockaddr_in *sa, uint32_t id)
Definition: mem_xprt_ep.c:56
static bool mem_eps_are_equal(const struct m0_net_end_point *ep1, const struct m0_net_end_point *ep2)
Definition: mem_xprt_ep.c:179
struct m0_tl ntm_end_points
Definition: net.h:856
#define m0_tl_endfor
Definition: tlist.h:700
Definition: sock.c:754
static struct m0_net_bulk_mem_end_point * mem_ep_to_pvt(const struct m0_net_end_point *ep)
Definition: mem_xprt.h:262
int i
Definition: dir.c:1033
void m0_ref_init(struct m0_ref *ref, int init_num, void(*release)(struct m0_ref *ref))
Definition: refs.c:24
return M0_ERR(-EOPNOTSUPP)
static bool mem_ep_equals_addr(const struct m0_net_end_point *ep, const struct sockaddr_in *sa)
Definition: mem_xprt_ep.c:161
Definition: refs.h:34
struct sockaddr_in md_passive
Definition: mem_xprt_pvt.h:50
#define M0_ASSERT(cond)
M0_INTERNAL bool m0_mutex_is_locked(const struct m0_mutex *mutex)
Definition: mutex.c:95
static void mem_xo_end_point_release(struct m0_ref *ref)
Definition: mem_xprt_ep.c:40
void(* bmo_ep_release)(struct m0_ref *ref)
Definition: mem_xprt.h:298
static bool mem_desc_equal(struct m0_net_buf_desc *d1, struct m0_net_buf_desc *d2)
Definition: mem_xprt_ep.c:255
static int mem_desc_decode(struct m0_net_buf_desc *desc, struct mem_desc **p_md)
Definition: mem_xprt_ep.c:242
void(* bmo_ep_free)(struct m0_net_bulk_mem_end_point *mep)
Definition: mem_xprt.h:293
void * m0_alloc(size_t size)
Definition: memory.c:126
void(* bmo_ep_get)(struct m0_net_end_point *ep)
Definition: mem_xprt.h:304
#define M0_POST(cond)
M0_INTERNAL void m0_net_end_point_get(struct m0_net_end_point *ep)
Definition: ep.c:88
char * ep
Definition: sw.h:132
Definition: queue.c:27
struct m0_net_end_point * ntm_ep
Definition: net.h:868
m0_net_queue_type
Definition: net.h:591
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
const struct m0_net_bulk_mem_ops * xd_ops
Definition: mem_xprt.h:338
struct sockaddr_in xep_sa
Definition: mem_xprt.h:244
static bool mem_ep_invariant(const struct m0_net_end_point *ep)
struct m0_net_end_point xep_ep
Definition: mem_xprt.h:252
static int mem_ep_create(struct m0_net_end_point **epp, struct m0_net_transfer_mc *tm, const struct sockaddr_in *sa, uint32_t id)
Definition: mem_xprt_ep.c:109
static struct m0_net_bulk_mem_domain_pvt * mem_dom_to_pvt(const struct m0_net_domain *dom)
Definition: mem_xprt.h:375
static void mem_ep_get(struct m0_net_end_point *ep)
Definition: mem_xprt_ep.c:101
char xep_addr[M0_NET_BULK_MEM_XEP_ADDR_LEN]
Definition: mem_xprt.h:255
static int mem_desc_create(struct m0_net_buf_desc *desc, struct m0_net_transfer_mc *tm, enum m0_net_queue_type qt, m0_bcount_t buflen, int64_t buf_id)
Definition: mem_xprt_ep.c:205
#define m0_tl_for(name, head, obj)
Definition: tlist.h:695
void m0_free(void *data)
Definition: memory.c:146
static void mem_ep_free(struct m0_net_bulk_mem_end_point *mep)
Definition: mem_xprt_ep.c:96
int32_t rc
Definition: trigger_fop.h:47
int64_t md_buf_id
Definition: mem_xprt_pvt.h:56