]> git.proxmox.com Git - ceph.git/blob - ceph/src/msg/Messenger.cc
edc74a9a4904b0a50b979d810b404a256f80e51a
[ceph.git] / 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 <netdb.h>
5
6 #include "include/types.h"
7 #include "include/random.h"
8
9 #include "Messenger.h"
10
11 #include "msg/async/AsyncMessenger.h"
12
13 Messenger *Messenger::create_client_messenger(CephContext *cct, std::string lname)
14 {
15 std::string public_msgr_type = cct->_conf->ms_public_type.empty() ? cct->_conf.get_val<std::string>("ms_type") : cct->_conf->ms_public_type;
16 auto nonce = get_random_nonce();
17 return Messenger::create(cct, public_msgr_type, entity_name_t::CLIENT(),
18 std::move(lname), nonce);
19 }
20
21 uint64_t Messenger::get_pid_nonce()
22 {
23 uint64_t nonce = getpid();
24 if (nonce == 1 || getenv("CEPH_USE_RANDOM_NONCE")) {
25 // we're running in a container; use a random number instead!
26 nonce = ceph::util::generate_random_number<uint64_t>();
27 }
28 return nonce;
29 }
30
31 uint64_t Messenger::get_random_nonce()
32 {
33 return ceph::util::generate_random_number<uint64_t>();
34 }
35
36 Messenger *Messenger::create(CephContext *cct, const std::string &type,
37 entity_name_t name, std::string lname,
38 uint64_t nonce)
39 {
40 if (type == "random" || type.find("async") != std::string::npos)
41 return new AsyncMessenger(cct, name, type, std::move(lname), nonce);
42 lderr(cct) << "unrecognized ms_type '" << type << "'" << dendl;
43 return nullptr;
44 }
45
46 /**
47 * Get the default crc flags for this messenger.
48 * but not yet dispatched.
49 */
50 static int get_default_crc_flags(const ConfigProxy&);
51
52 Messenger::Messenger(CephContext *cct_, entity_name_t w)
53 : trace_endpoint("0.0.0.0", 0, "Messenger"),
54 my_name(w),
55 default_send_priority(CEPH_MSG_PRIO_DEFAULT),
56 started(false),
57 magic(0),
58 socket_priority(-1),
59 cct(cct_),
60 crcflags(get_default_crc_flags(cct->_conf)),
61 auth_registry(cct),
62 comp_registry(cct)
63 {
64 auth_registry.refresh_config();
65 comp_registry.refresh_config();
66 }
67
68 void Messenger::set_endpoint_addr(const entity_addr_t& a,
69 const entity_name_t &name)
70 {
71 size_t hostlen;
72 if (a.get_family() == AF_INET)
73 hostlen = sizeof(struct sockaddr_in);
74 else if (a.get_family() == AF_INET6)
75 hostlen = sizeof(struct sockaddr_in6);
76 else
77 hostlen = 0;
78
79 if (hostlen) {
80 char buf[NI_MAXHOST] = { 0 };
81 getnameinfo(a.get_sockaddr(), hostlen, buf, sizeof(buf),
82 NULL, 0, NI_NUMERICHOST);
83
84 trace_endpoint.copy_ip(buf);
85 }
86 trace_endpoint.set_port(a.get_port());
87 }
88
89 /**
90 * Get the default crc flags for this messenger.
91 * but not yet dispatched.
92 *
93 * Pre-calculate desired software CRC settings. CRC computation may
94 * be disabled by default for some transports (e.g., those with strong
95 * hardware checksum support).
96 */
97 int get_default_crc_flags(const ConfigProxy& conf)
98 {
99 int r = 0;
100 if (conf->ms_crc_data)
101 r |= MSG_CRC_DATA;
102 if (conf->ms_crc_header)
103 r |= MSG_CRC_HEADER;
104 return r;
105 }
106
107 int Messenger::bindv(const entity_addrvec_t& addrs)
108 {
109 return bind(addrs.legacy_addr());
110 }
111