Motr  M0
session_utils.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 #ifndef __KERNEL__
24 #include <sys/stat.h> /* S_ISDIR */
25 #endif
26 
27 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_RPC
28 #include "lib/trace.h"
29 #include "lib/errno.h"
30 #include "lib/memory.h"
31 #include "lib/misc.h"
32 #include "lib/arith.h"
33 #include "lib/bitstring.h"
34 #include "lib/finject.h" /* M0_FI_ENABLED */
35 #include "lib/uuid.h"
36 #include "lib/hash.h" /* m0_hash() */
37 #include "fop/fop.h"
38 #include "reqh/reqh.h"
39 #include "rpc/rpc_internal.h"
40 
47 M0_INTERNAL int m0_rpc_session_module_init(void)
48 {
49  return m0_rpc_session_fop_init();
50 }
51 
52 M0_INTERNAL void m0_rpc_session_module_fini(void)
53 {
55 }
56 
57 M0_INTERNAL int m0_rpc__post_locked(struct m0_rpc_item *item);
58 
62 M0_INTERNAL int m0_rpc__fop_post(struct m0_fop *fop,
63  struct m0_rpc_session *session,
64  const struct m0_rpc_item_ops *ops,
65  m0_time_t abs_timeout)
66 {
67  struct m0_rpc_item *item;
68  m0_time_t now = m0_time_now();
69 
70  M0_ENTRY("fop: %p, session: %p, item %p", fop, session, &fop->f_item);
71 
72  item = &fop->f_item;
75  item->ri_deadline = 0;
76  item->ri_ops = ops;
78 
79  if (M0_FI_ENABLED("fake_error"))
80  return M0_ERR(-EINVAL);
81 
82  if (M0_FI_ENABLED("do_nothing"))
83  return 0;
84 
85  if (abs_timeout != M0_TIME_NEVER) {
86  item->ri_nr_sent_max = 0;
87  if (abs_timeout > now)
88  item->ri_nr_sent_max = m0_time_sub(abs_timeout, now) /
90  if (item->ri_nr_sent_max == 0)
91  item->ri_nr_sent_max = 1;
92  }
94 }
95 
96 M0_INTERNAL uint64_t m0_rpc_id_generate(const struct m0_fid *uniq_fid)
97 {
98  static struct m0_atomic64 cnt;
99  uint64_t id;
100  uint64_t millisec;
101 
102  do {
104  millisec = m0_time_nanoseconds(m0_time_now()) * 1000000;
105  id = (millisec << 10) | (m0_atomic64_get(&cnt) & 0x3FF);
106  id = m0_hash(m0_fid_hash(uniq_fid) + id);
107  } while (id == 0 || id == UINT64_MAX);
108 
109  return id;
110 }
111 
112 M0_INTERNAL int m0_rpc_item_dispatch(struct m0_rpc_item *item)
113 {
114  int rc;
115  const struct m0_rpc_item_type_ops *itops = item->ri_type->rit_ops;
116 
117  M0_ENTRY("item %p[%u], xid=%"PRIu64,
119 
121  if (rc != 0)
122  return M0_RC(0);
123 
124  if (itops != NULL && itops->rito_deliver != NULL)
125  rc = itops->rito_deliver(item->ri_rmachine, item);
126  else
132  return M0_RC(rc);
133 }
134 
135 #undef M0_TRACE_SUBSYSTEM
136 
static void m0_atomic64_inc(struct m0_atomic64 *a)
uint64_t id
Definition: cob.h:2380
uint32_t rit_opcode
Definition: item.h:474
m0_time_t ri_resend_interval
Definition: item.h:144
enum m0_rpc_item_priority ri_prio
Definition: item.h:133
M0_INTERNAL uint64_t m0_fid_hash(const struct m0_fid *fid)
Definition: fid.c:295
#define NULL
Definition: misc.h:38
const m0_time_t M0_TIME_NEVER
Definition: time.c:108
const struct m0_rpc_item_type_ops * rit_ops
Definition: item.h:476
uint64_t m0_time_t
Definition: time.h:37
M0_INTERNAL int m0_reqh_fop_handle(struct m0_reqh *reqh, struct m0_fop *fop)
Definition: reqh.c:546
uint64_t m0_time_nanoseconds(const m0_time_t time)
Definition: time.c:89
M0_INTERNAL int m0_rpc_session_fop_init(void)
Definition: session_fops.c:147
M0_INTERNAL void m0_rpc_session_fop_fini(void)
Definition: session_fops.c:129
static struct m0_rpc_session session
Definition: formation2.c:38
static struct m0_rpc_item * item
Definition: item.c:56
return M0_RC(rc)
#define M0_ENTRY(...)
Definition: trace.h:170
uint64_t osr_xid
Definition: onwire.h:105
#define PRIu64
Definition: types.h:58
return M0_ERR(-EOPNOTSUPP)
Definition: cnt.h:36
M0_INTERNAL uint64_t m0_rpc_id_generate(const struct m0_fid *uniq_fid)
Definition: session_utils.c:96
M0_INTERNAL int m0_rpc__fop_post(struct m0_fop *fop, struct m0_rpc_session *session, const struct m0_rpc_item_ops *ops, m0_time_t abs_timeout)
Definition: session_utils.c:62
m0_time_t m0_time_now(void)
Definition: time.c:134
struct m0_rpc_item_header2 ri_header
Definition: item.h:193
M0_INTERNAL int m0_rpc_session_module_init(void)
Definition: session_utils.c:47
const struct m0_rpc_item_type * ri_type
Definition: item.h:200
M0_INTERNAL int m0_ha_epoch_check(const struct m0_rpc_item *item)
Definition: epoch.c:266
uint64_t ri_nr_sent_max
Definition: item.h:146
M0_INTERNAL struct m0_rpc_machine * session_machine(const struct m0_rpc_session *s)
Definition: session.c:147
#define UINT64_MAX
Definition: types.h:44
static int64_t m0_atomic64_get(const struct m0_atomic64 *a)
#define M0_FI_ENABLED(tag)
Definition: finject.h:231
Definition: fid.h:38
M0_INTERNAL int m0_rpc__post_locked(struct m0_rpc_item *item)
Definition: rpc.c:83
const struct m0_rpc_item_ops * ri_ops
Definition: item.h:149
m0_time_t m0_time_sub(const m0_time_t t1, const m0_time_t t2)
Definition: time.c:65
struct m0_rpc_session * ri_session
Definition: item.h:147
M0_INTERNAL void m0_rpc_session_module_fini(void)
Definition: session_utils.c:52
static struct m0_fop * fop
Definition: item.c:57
struct m0_fop * m0_rpc_item_to_fop(const struct m0_rpc_item *item)
Definition: fop.c:346
int(* rito_deliver)(struct m0_rpc_machine *rpcmach, struct m0_rpc_item *item)
Definition: item.h:438
struct m0_fom_ops ops
Definition: io_foms.c:623
struct m0_rpc_machine * ri_rmachine
Definition: item.h:160
struct m0_rpc_item f_item
Definition: fop.h:83
M0_INTERNAL int m0_rpc_item_dispatch(struct m0_rpc_item *item)
int32_t rc
Definition: trigger_fop.h:47
struct m0_reqh * rm_reqh
Definition: rpc_machine.h:105
Definition: fop.h:79
m0_time_t ri_deadline
Definition: item.h:141
M0_INTERNAL uint64_t m0_hash(uint64_t x)
Definition: hash.c:279