Motr  M0
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 #include "lib/assert.h"
24 #include "lib/errno.h"
25 #include "lib/finject.h"
26 #include "net/net_internal.h"
27 #include "motr/magic.h"
28 
29 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_NET
30 #include "lib/trace.h"
31 
37 M0_TL_DESCR_DEFINE(m0_nep, "net end points", M0_INTERNAL,
38  struct m0_net_end_point, nep_tm_linkage, nep_magix,
40 M0_TL_DEFINE(m0_nep, M0_INTERNAL, struct m0_net_end_point);
41 
42 M0_INTERNAL bool m0_net__ep_invariant(struct m0_net_end_point *ep,
43  struct m0_net_transfer_mc *tm,
44  bool under_tm_mutex)
45 {
46  return
47  _0C(ep != NULL) &&
48  _0C(m0_atomic64_get(&ep->nep_ref.ref_cnt) > 0) &&
49  _0C(ep->nep_ref.release != NULL) &&
50  _0C(ep->nep_tm == tm) &&
51  _0C(ep->nep_addr != NULL) &&
52  _0C(ergo(under_tm_mutex,
53  m0_nep_tlist_contains(&tm->ntm_end_points, ep)));
54 }
55 
56 M0_INTERNAL int m0_net_end_point_create(struct m0_net_end_point **epp,
57  struct m0_net_transfer_mc *tm,
58  const char *addr)
59 {
60  int rc;
61  struct m0_net_domain *dom;
62 
63  M0_PRE(tm != NULL && tm->ntm_state == M0_NET_TM_STARTED);
64  M0_PRE(epp != NULL);
65 
66  if (M0_FI_ENABLED("fake_error"))
67  return M0_ERR(-EINVAL);
68 
69  dom = tm->ntm_dom;
70  M0_PRE(dom->nd_xprt != NULL);
71 
72  m0_mutex_lock(&tm->ntm_mutex);
73 
74  *epp = NULL;
75 
76  rc = dom->nd_xprt->nx_ops->xo_end_point_create(epp, tm, addr);
77 
78  /*
79  * Either we failed or we got back a properly initialized end point
80  * with reference count of at least 1.
81  */
82  M0_POST(ergo(rc == 0, m0_net__ep_invariant(*epp, tm, true)));
83  m0_mutex_unlock(&tm->ntm_mutex);
84  return M0_RC(rc);
85 }
86 M0_EXPORTED(m0_net_end_point_create);
87 
88 M0_INTERNAL void m0_net_end_point_get(struct m0_net_end_point *ep)
89 {
90  struct m0_ref *ref = &ep->nep_ref;
91  M0_PRE(ep != NULL);
92  M0_PRE(m0_atomic64_get(&ref->ref_cnt) >= 1);
93  m0_ref_get(ref);
94  return;
95 }
96 M0_EXPORTED(m0_net_end_point_get);
97 
99 {
100  struct m0_ref *ref = &ep->nep_ref;
101  struct m0_net_transfer_mc *tm;
102  M0_PRE(ep != NULL);
103  M0_PRE(m0_atomic64_get(&ref->ref_cnt) >= 1);
104  tm = ep->nep_tm;
105  M0_PRE(tm != NULL);
106  /* hold the transfer machine lock to synchronize release(), if called */
107  m0_mutex_lock(&tm->ntm_mutex);
108  m0_ref_put(ref);
109  m0_mutex_unlock(&tm->ntm_mutex);
110  return;
111 }
112 M0_EXPORTED(m0_net_end_point_put);
113 
114 #undef M0_TRACE_SUBSYSTEM
115 
118 /*
119  * Local variables:
120  * c-indentation-style: "K&R"
121  * c-basic-offset: 8
122  * tab-width: 8
123  * fill-column: 79
124  * scroll-step: 1
125  * End:
126  */
#define M0_PRE(cond)
M0_INTERNAL void m0_mutex_unlock(struct m0_mutex *mutex)
Definition: mutex.c:66
#define NULL
Definition: misc.h:38
#define ergo(a, b)
Definition: misc.h:293
enum m0_net_tm_state ntm_state
Definition: net.h:819
struct m0_net_domain * ntm_dom
Definition: net.h:853
M0_INTERNAL bool m0_net__ep_invariant(struct m0_net_end_point *ep, struct m0_net_transfer_mc *tm, bool under_tm_mutex)
Definition: ep.c:42
M0_TL_DEFINE(m0_nep, M0_INTERNAL, struct m0_net_end_point)
M0_INTERNAL void m0_mutex_lock(struct m0_mutex *mutex)
Definition: mutex.c:49
struct m0_tl ntm_end_points
Definition: net.h:856
return M0_RC(rc)
Definition: sock.c:754
M0_INTERNAL void m0_ref_put(struct m0_ref *ref)
Definition: refs.c:38
return M0_ERR(-EOPNOTSUPP)
M0_INTERNAL void m0_ref_get(struct m0_ref *ref)
Definition: refs.c:32
Definition: refs.h:34
struct m0_atomic64 ref_cnt
Definition: refs.h:38
static struct m0_stob_domain * dom
Definition: storage.c:38
#define M0_POST(cond)
Definition: xcode.h:73
M0_INTERNAL void m0_net_end_point_get(struct m0_net_end_point *ep)
Definition: ep.c:88
static int64_t m0_atomic64_get(const struct m0_atomic64 *a)
void m0_net_end_point_put(struct m0_net_end_point *ep)
Definition: ep.c:98
#define M0_FI_ENABLED(tag)
Definition: finject.h:231
#define _0C(exp)
Definition: assert.h:311
int32_t rc
Definition: trigger_fop.h:47
M0_INTERNAL int m0_net_end_point_create(struct m0_net_end_point **epp, struct m0_net_transfer_mc *tm, const char *addr)
Definition: ep.c:56
M0_TL_DESCR_DEFINE(m0_nep, "net end points", M0_INTERNAL, struct m0_net_end_point, nep_tm_linkage, nep_magix, M0_NET_NEP_MAGIC, M0_NET_NEP_HEAD_MAGIC)