Motr  M0
serialize.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-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/misc.h" /* M0_SET0 */
24 #include "lib/memory.h" /* m0_alloc */
25 #include "lib/errno.h" /* ENOMEM */
26 #include "lib/byteorder.h" /* m0_byteorder_cpu_to_le16 */
27 
28 #include "net/test/serialize.h"
29 
41 M0_BASSERT(sizeof(long) == 8);
42 M0_BASSERT(sizeof(void *) == 8);
43 M0_BASSERT(sizeof(int) == 4);
44 
45 static void net_test_serialize_cpu_to_le(char *d, char *s, m0_bcount_t len)
46 {
47  if (len == 1) {
48  *d = *s;
49  } else if (len == 2) {
50  * (uint16_t *) d = m0_byteorder_cpu_to_le16(* (uint16_t *) s);
51  } else if (len == 4) {
52  * (uint32_t *) d = m0_byteorder_cpu_to_le32(* (uint32_t *) s);
53  } else if (len == 8) {
54  * (uint64_t *) d = m0_byteorder_cpu_to_le64(* (uint64_t *) s);
55  } else {
56  M0_IMPOSSIBLE("len isn't a power of 2");
57  }
58 
59 }
60 
61 static void net_test_serialize_le_to_cpu(char *d, char *s, m0_bcount_t len)
62 {
63  if (len == 1) {
64  *d = *s;
65  } else if (len == 2) {
66  * (uint16_t *) d = m0_byteorder_le16_to_cpu(* (uint16_t *) s);
67  } else if (len == 4) {
68  * (uint32_t *) d = m0_byteorder_le32_to_cpu(* (uint32_t *) s);
69  } else if (len == 8) {
70  * (uint64_t *) d = m0_byteorder_le64_to_cpu(* (uint64_t *) s);
71  } else {
72  M0_IMPOSSIBLE("len isn't a power of 2");
73  }
74 
75 }
76 
82  char *buf,
83  char *data,
84  m0_bcount_t len)
85 {
86  M0_PRE(len == 1 || len == 2 || len == 4 || len == 8);
87 
90  else
92 }
93 
106  void *data,
107  m0_bcount_t data_len,
108  bool plain_data,
109  struct m0_bufvec *bv,
110  m0_bcount_t bv_offset,
111  m0_bcount_t bv_length)
112 {
113  struct m0_bufvec_cursor bv_cur;
114  struct m0_bufvec_cursor data_cur;
115  char buf[8];
116  void *data_addr = plain_data ? data : buf;
117  struct m0_bufvec data_bv = M0_BUFVEC_INIT_BUF(&data_addr,
118  &data_len);
119  m0_bcount_t copied;
120  bool end_reached;
121 
122  M0_PRE(data_len > 0);
123  M0_PRE(plain_data || data_len == 1 || data_len == 2 ||
124  data_len == 4 || data_len == 8);
125 
126  /* if buffer is NULL and operation is 'serialize' then return size */
127  if (bv == NULL)
128  return op == M0_NET_TEST_SERIALIZE ? data_len : 0;
129  /* if buffer is not large enough then return 0 */
130  if (bv_offset + data_len > bv_length)
131  return 0;
132 
133  /*
134  Take care about endianness.
135  Store all endian-dependent data in little-endian format.
136  */
137  if (!plain_data && op == M0_NET_TEST_SERIALIZE)
138  net_test_serialize_reorder(op, buf, data, data_len);
139 
140  /* initialize cursors and copy data */
141  m0_bufvec_cursor_init(&bv_cur, bv);
142  end_reached = m0_bufvec_cursor_move(&bv_cur, bv_offset);
143  M0_ASSERT(!end_reached);
144 
145  m0_bufvec_cursor_init(&data_cur, &data_bv);
146 
147  if (op == M0_NET_TEST_SERIALIZE)
148  copied = m0_bufvec_cursor_copy(&bv_cur, &data_cur, data_len);
149  else
150  copied = m0_bufvec_cursor_copy(&data_cur, &bv_cur, data_len);
151  M0_ASSERT(copied == data_len);
152 
153  /*
154  Take care about endianness.
155  Read all endian-dependent data from little-endian buffer.
156  */
157  if (!plain_data && op == M0_NET_TEST_DESERIALIZE)
158  net_test_serialize_reorder(op, buf, data, data_len);
159 
160  return data_len;
161 }
162 
164  void *data,
165  m0_bcount_t data_len,
166  bool plain_data,
167  struct m0_bufvec *bv,
168  m0_bcount_t bv_offset)
169 {
170  m0_bcount_t bv_length = bv == NULL ? 0 : m0_vec_count(&bv->ov_vec);
171 
172  return net_test_serialize_data(op, data, data_len, plain_data,
173  bv, bv_offset, bv_length);
174 }
175 
177  void *obj,
178  const struct m0_net_test_descr descr[],
179  size_t descr_nr,
180  struct m0_bufvec *bv,
181  m0_bcount_t bv_offset)
182 {
183  size_t i;
184  const struct m0_net_test_descr *d_i;
185  void *addr;
186  m0_bcount_t len_total = 0;
187  m0_bcount_t len;
188  m0_bcount_t bv_length;
189 
191  M0_PRE(obj != NULL);
192  M0_PRE(descr != NULL);
193 
194  bv_length = bv == NULL ? 0 : m0_vec_count(&bv->ov_vec);
195 
196  for (i = 0; i < descr_nr; ++i) {
197  d_i = &descr[i];
198  addr = &((char *) obj)[d_i->ntd_offset];
200  d_i->ntd_plain_data,
201  bv, bv_offset + len_total,
202  bv_length);
203  len_total = net_test_len_accumulate(len_total, len);
204  if (len_total == 0)
205  break;
206  }
207  return len_total;
208 }
209 
214 /*
215  * Local variables:
216  * c-indentation-style: "K&R"
217  * c-basic-offset: 8
218  * tab-width: 8
219  * fill-column: 79
220  * scroll-step: 1
221  * End:
222  */
#define M0_BUFVEC_INIT_BUF(addr_ptr, count_ptr)
Definition: vec.h:165
static void net_test_serialize_reorder(enum m0_net_test_serialize_op op, char *buf, char *data, m0_bcount_t len)
Definition: serialize.c:81
static uint16_t m0_byteorder_le16_to_cpu(uint16_t little_endian_16bits)
Definition: byteorder.h:64
#define M0_PRE(cond)
#define NULL
Definition: misc.h:38
m0_bcount_t m0_net_test_serialize(enum m0_net_test_serialize_op op, void *obj, const struct m0_net_test_descr descr[], size_t descr_nr, struct m0_bufvec *bv, m0_bcount_t bv_offset)
Definition: serialize.c:176
static void net_test_serialize_le_to_cpu(char *d, char *s, m0_bcount_t len)
Definition: serialize.c:61
M0_BASSERT(sizeof(long)==8)
struct m0_vec ov_vec
Definition: vec.h:147
static m0_bcount_t net_test_serialize_data(enum m0_net_test_serialize_op op, void *data, m0_bcount_t data_len, bool plain_data, struct m0_bufvec *bv, m0_bcount_t bv_offset, m0_bcount_t bv_length)
Definition: serialize.c:105
struct m0_bufvec data
Definition: di.c:40
static void net_test_serialize_cpu_to_le(char *d, char *s, m0_bcount_t len)
Definition: serialize.c:45
uint64_t m0_bcount_t
Definition: types.h:77
static int void * buf
Definition: dir.c:1019
static m0_bcount_t net_test_len_accumulate(m0_bcount_t accumulator, m0_bcount_t addend)
Definition: serialize.h:122
static struct foo * obj
Definition: tlist.c:302
Definition: sock.c:887
op
Definition: libdemo.c:64
static uint32_t m0_byteorder_le32_to_cpu(uint32_t little_endian_32bits)
Definition: byteorder.h:84
M0_INTERNAL bool m0_bufvec_cursor_move(struct m0_bufvec_cursor *cur, m0_bcount_t count)
Definition: vec.c:574
static char * addr
Definition: node_k.c:37
int i
Definition: dir.c:1033
size_t ntd_offset
Definition: serialize.h:49
static uint16_t m0_byteorder_cpu_to_le16(uint16_t cpu_16bits)
Definition: byteorder.h:54
static uint64_t m0_byteorder_cpu_to_le64(uint64_t cpu_64bits)
Definition: byteorder.h:94
M0_INTERNAL m0_bcount_t m0_bufvec_cursor_copy(struct m0_bufvec_cursor *dcur, struct m0_bufvec_cursor *scur, m0_bcount_t num_bytes)
Definition: vec.c:620
#define M0_ASSERT(cond)
M0_INTERNAL void m0_bufvec_cursor_init(struct m0_bufvec_cursor *cur, const struct m0_bufvec *bvec)
Definition: vec.c:563
Definition: xcode.h:73
M0_INTERNAL m0_bcount_t m0_vec_count(const struct m0_vec *vec)
Definition: vec.c:53
static uint32_t m0_byteorder_cpu_to_le32(uint32_t cpu_32bits)
Definition: byteorder.h:74
size_t ntd_length
Definition: serialize.h:50
m0_net_test_serialize_op
Definition: serialize.h:42
static struct m0_addb2_source * s
Definition: consumer.c:39
m0_bcount_t m0_net_test_serialize_data(enum m0_net_test_serialize_op op, void *data, m0_bcount_t data_len, bool plain_data, struct m0_bufvec *bv, m0_bcount_t bv_offset)
Definition: serialize.c:163
static uint64_t m0_byteorder_le64_to_cpu(uint64_t little_endian_64bits)
Definition: byteorder.h:104
Definition: vec.h:145
#define M0_IMPOSSIBLE(fmt,...)