summaryrefslogtreecommitdiffstats
path: root/src/messages/MOSDPing.h
blob: 8f512a7d718a0da287ceb2a1f6d9acbef5ee982c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software 
 * Foundation.  See file COPYING.
 * 
 */


/**
 * This is used to send pings between daemons (so far, the OSDs) for
 * heartbeat purposes. We include a timestamp and distinguish between
 * outgoing pings and responses to those. If you set the
 * min_message in the constructor, the message will inflate itself
 * to the specified size -- this is good for dealing with network
 * issues with jumbo frames. See http://tracker.ceph.com/issues/20087
 *
 */

#ifndef CEPH_MOSDPING_H
#define CEPH_MOSDPING_H

#include "common/Clock.h"

#include "msg/Message.h"
#include "osd/osd_types.h"


class MOSDPing final : public Message {
private:
  static constexpr int HEAD_VERSION = 5;
  static constexpr int COMPAT_VERSION = 4;

 public:
  enum {
    HEARTBEAT = 0,
    START_HEARTBEAT = 1,
    YOU_DIED = 2,
    STOP_HEARTBEAT = 3,
    PING = 4,
    PING_REPLY = 5,
  };
  const char *get_op_name(int op) const {
    switch (op) {
    case HEARTBEAT: return "heartbeat";
    case START_HEARTBEAT: return "start_heartbeat";
    case STOP_HEARTBEAT: return "stop_heartbeat";
    case YOU_DIED: return "you_died";
    case PING: return "ping";
    case PING_REPLY: return "ping_reply";
    default: return "???";
    }
  }

  uuid_d fsid;
  epoch_t map_epoch = 0;
  __u8 op = 0;
  utime_t ping_stamp;               ///< when the PING was sent
  ceph::signedspan mono_ping_stamp; ///< relative to sender's clock
  ceph::signedspan mono_send_stamp; ///< replier's send stamp
  std::optional<ceph::signedspan> delta_ub;  ///< ping sender
  epoch_t up_from = 0;

  uint32_t min_message_size = 0;

  MOSDPing(const uuid_d& f, epoch_t e, __u8 o,
	   utime_t s,
	   ceph::signedspan ms,
	   ceph::signedspan mss,
	   epoch_t upf,
	   uint32_t min_message,
	   std::optional<ceph::signedspan> delta_ub = {})
    : Message{MSG_OSD_PING, HEAD_VERSION, COMPAT_VERSION},
      fsid(f), map_epoch(e), op(o),
      ping_stamp(s),
      mono_ping_stamp(ms),
      mono_send_stamp(mss),
      delta_ub(delta_ub),
      up_from(upf),
      min_message_size(min_message)
  { }
  MOSDPing()
    : Message{MSG_OSD_PING, HEAD_VERSION, COMPAT_VERSION}
  {}
private:
  ~MOSDPing() final {}

public:
  void decode_payload() override {
    using ceph::decode;
    auto p = payload.cbegin();
    decode(fsid, p);
    decode(map_epoch, p);
    decode(op, p);
    decode(ping_stamp, p);

    int payload_mid_length = p.get_off();
    uint32_t size;
    decode(size, p);

    if (header.version >= 5) {
      decode(up_from, p);
      decode(mono_ping_stamp, p);
      decode(mono_send_stamp, p);
      decode(delta_ub, p);
    }

    p += size;
    min_message_size = size + payload_mid_length;
  }
  void encode_payload(uint64_t features) override {
    using ceph::encode;
    encode(fsid, payload);
    encode(map_epoch, payload);
    encode(op, payload);
    encode(ping_stamp, payload);

    size_t s = 0;
    if (min_message_size > payload.length()) {
      s = min_message_size - payload.length();
    }
    encode((uint32_t)s, payload);

    encode(up_from, payload);
    encode(mono_ping_stamp, payload);
    encode(mono_send_stamp, payload);
    encode(delta_ub, payload);

    if (s) {
      // this should be big enough for normal min_message padding sizes. since
      // we are targeting jumbo ethernet frames around 9000 bytes, 16k should
      // be more than sufficient!  the compiler will statically zero this so
      // that at runtime we are only adding a bufferptr reference to it.
      static char zeros[16384] = {};
      while (s > sizeof(zeros)) {
        payload.append(ceph::buffer::create_static(sizeof(zeros), zeros));
        s -= sizeof(zeros);
      }
      if (s) {
        payload.append(ceph::buffer::create_static(s, zeros));
      }
    }
  }

  std::string_view get_type_name() const override { return "osd_ping"; }
  void print(std::ostream& out) const override {
    out << "osd_ping(" << get_op_name(op)
	<< " e" << map_epoch
	<< " up_from " << up_from
	<< " ping_stamp " << ping_stamp << "/" << mono_ping_stamp
	<< " send_stamp " << mono_send_stamp;
    if (delta_ub) {
      out << " delta_ub " << *delta_ub;
    }
    out << ")";
  }
private:
  template<class T, typename... Args>
  friend boost::intrusive_ptr<T> ceph::make_message(Args&&... args);
};

#endif