Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / msg / Messenger.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include <random>
5 #include <netdb.h>
6 #include "include/Spinlock.h"
7
8 #include "include/types.h"
9 #include "Messenger.h"
10
11 #include "msg/simple/SimpleMessenger.h"
12 #include "msg/async/AsyncMessenger.h"
13 #ifdef HAVE_XIO
14 #include "msg/xio/XioMessenger.h"
15 #endif
16
17 Messenger *Messenger::create_client_messenger(CephContext *cct, string lname)
18 {
19   std::string public_msgr_type = cct->_conf->ms_public_type.empty() ? cct->_conf->get_val<std::string>("ms_type") : cct->_conf->ms_public_type;
20   uint64_t nonce = 0;
21   get_random_bytes((char*)&nonce, sizeof(nonce));
22   return Messenger::create(cct, public_msgr_type, entity_name_t::CLIENT(),
23                            std::move(lname), nonce, 0);
24 }
25
26 Messenger *Messenger::create(CephContext *cct, const string &type,
27                              entity_name_t name, string lname,
28                              uint64_t nonce, uint64_t cflags)
29 {
30   int r = -1;
31   if (type == "random") {
32     static std::random_device seed;
33     static std::default_random_engine random_engine(seed());
34     static Spinlock random_lock;
35
36     std::lock_guard<Spinlock> lock(random_lock);
37     std::uniform_int_distribution<> dis(0, 1);
38     r = dis(random_engine);
39   }
40   if (r == 0 || type == "simple")
41     return new SimpleMessenger(cct, name, std::move(lname), nonce);
42   else if (r == 1 || type.find("async") != std::string::npos)
43     return new AsyncMessenger(cct, name, type, std::move(lname), nonce);
44 #ifdef HAVE_XIO
45   else if ((type == "xio") &&
46            cct->check_experimental_feature_enabled("ms-type-xio"))
47     return new XioMessenger(cct, name, std::move(lname), nonce, cflags);
48 #endif
49   lderr(cct) << "unrecognized ms_type '" << type << "'" << dendl;
50   return nullptr;
51 }
52
53 void Messenger::set_endpoint_addr(const entity_addr_t& a,
54                                   const entity_name_t &name)
55 {
56   size_t hostlen;
57   if (a.get_family() == AF_INET)
58     hostlen = sizeof(struct sockaddr_in);
59   else if (a.get_family() == AF_INET6)
60     hostlen = sizeof(struct sockaddr_in6);
61   else
62     hostlen = 0;
63
64   if (hostlen) {
65     char buf[NI_MAXHOST] = { 0 };
66     getnameinfo(a.get_sockaddr(), hostlen, buf, sizeof(buf),
67                 NULL, 0, NI_NUMERICHOST);
68
69     trace_endpoint.copy_ip(buf);
70   }
71   trace_endpoint.set_port(a.get_port());
72 }
73
74 /*
75  * Pre-calculate desired software CRC settings.  CRC computation may
76  * be disabled by default for some transports (e.g., those with strong
77  * hardware checksum support).
78  */
79 int Messenger::get_default_crc_flags(md_config_t * conf)
80 {
81   int r = 0;
82   if (conf->ms_crc_data)
83     r |= MSG_CRC_DATA;
84   if (conf->ms_crc_header)
85     r |= MSG_CRC_HEADER;
86   return r;
87 }