Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / messenger / message_helper.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software 
11  * Foundation.  See file COPYING.
12  * 
13  */
14
15 #ifndef MESSAGE_HELPER_H
16 #define MESSAGE_HELPER_H
17
18 #include "msg/msg_types.h"
19 #include "messages/MDataPing.h"
20 #if defined(HAVE_XIO)
21 #include "msg/xio/XioMessenger.h"
22 #endif
23
24 static inline Message* new_ping_monstyle(const char *tag, int mult)
25 {
26   Message *m = new MPing();
27   Formatter *f = new JSONFormatter(true);
28
29   string str = "one giant step for ";
30
31   f->open_object_section(tag);
32   for (int ix = 0; ix < mult; ++ix) {
33     f->dump_string(tag, str);
34   }
35   f->close_section();
36
37   bufferlist bl;
38   stringstream ss;
39
40   f->flush(ss);
41   ::encode(ss.str(), bl);
42   m->set_payload(bl);
43
44   return m;
45 }
46
47 #if defined(HAVE_XIO)
48 extern struct xio_mempool *xio_msgr_mpool;
49
50 void xio_hook_func(struct xio_reg_mem *mp)
51 {
52   xio_mempool_free(mp);
53 }
54
55 static inline Message* new_ping_with_data(const char *tag, uint32_t size)
56 {
57   static uint32_t counter;
58
59   MDataPing *m = new MDataPing();
60   m->counter = counter++;
61   m->tag = tag;
62
63   bufferlist bl;
64   void *p;
65
66   struct xio_reg_mem *mp = m->get_mp();
67   int e = xio_mempool_alloc(xio_msgr_mpool, size, mp);
68   assert(e == 0);
69   p = mp->addr;
70   m->set_rdma_hook(xio_hook_func);
71
72   strcpy((char*) p, tag);
73   uint32_t* t = (uint32_t* ) (((char*) p) + size - 32);
74   *t = counter;
75
76   bl.append(buffer::create_static(size, (char*) p));
77   m->set_data(bl);
78
79   return static_cast<Message*>(m);
80 }
81 #endif
82
83 static inline Message* new_simple_ping_with_data(const char *tag,
84                                                  uint32_t size,
85                                                  uint32_t nfrags)
86 {
87   static size_t pagesize = sysconf(_SC_PAGESIZE);
88   static uint32_t counter;
89   uint32_t segsize;
90   int do_page_alignment;
91
92   MDataPing *m = new MDataPing();
93   m->counter = counter++;
94   m->tag = tag;
95
96   bufferlist bl;
97   void *p;
98
99   segsize = (size+nfrags-1)/nfrags;
100   segsize = (segsize + 7) & ~7;
101   if (segsize < 32) segsize = 32;
102
103   do_page_alignment = segsize >= 1024;
104   if (do_page_alignment)
105     segsize = (segsize + pagesize - 1) & ~(pagesize - 1);
106   m->free_data = true;
107   for (uint32_t i = 0; i < nfrags; ++i) {
108     if (do_page_alignment) {
109       if (posix_memalign(&p, pagesize, segsize))
110         p = nullptr;
111     } else {
112         p = malloc(segsize);
113     }
114     if (!p)
115       throw std::bad_alloc();
116     strcpy((char*) p, tag);
117     uint32_t* t = (uint32_t* ) (((char*) p) + segsize - 32);
118     *t = counter;
119     t[1] = i;
120
121     bl.append(buffer::create_static(segsize, (char*) p));
122   }
123   m->set_data(bl);
124
125   return static_cast<Message*>(m);
126 }
127
128 static inline Message* new_simple_ping_with_data(const char *tag,
129                                                  uint32_t size)
130 {
131   return new_simple_ping_with_data(tag, size, 1);
132 }
133
134
135 #endif /* MESSAGE_HELPER_H */