]> git.proxmox.com Git - ceph.git/blame - ceph/src/mon/MonCommand.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / mon / MonCommand.h
CommitLineData
c07f9fc5
FG
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) 2017 John Spray <john.spray@redhat.com>
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#pragma once
15
16#include <string>
17#include "include/encoding.h"
18
19struct MonCommand {
20 std::string cmdstring;
21 std::string helpstring;
22 std::string module;
23 std::string req_perms;
c07f9fc5
FG
24 uint64_t flags;
25
26 // MonCommand flags
27 static const uint64_t FLAG_NONE = 0;
28 static const uint64_t FLAG_NOFORWARD = 1 << 0;
29 static const uint64_t FLAG_OBSOLETE = 1 << 1;
30 static const uint64_t FLAG_DEPRECATED = 1 << 2;
31 static const uint64_t FLAG_MGR = 1 << 3;
11fdf7f2
TL
32 static const uint64_t FLAG_POLL = 1 << 4;
33 static const uint64_t FLAG_HIDDEN = 1 << 5;
9f95a23c
TL
34 // asok and tell commands are not forwarded, and they should not be listed
35 // in --help output.
36 static const uint64_t FLAG_TELL = (FLAG_NOFORWARD | FLAG_HIDDEN);
c07f9fc5 37
9f95a23c 38 bool has_flag(uint64_t flag) const { return (flags & flag) == flag; }
c07f9fc5
FG
39 void set_flag(uint64_t flag) { flags |= flag; }
40 void unset_flag(uint64_t flag) { flags &= ~flag; }
41
42 void encode(bufferlist &bl) const {
43 ENCODE_START(1, 1, bl);
44 encode_bare(bl);
11fdf7f2 45 encode(flags, bl);
c07f9fc5
FG
46 ENCODE_FINISH(bl);
47 }
48
11fdf7f2 49 void decode(bufferlist::const_iterator &bl) {
c07f9fc5
FG
50 DECODE_START(1, bl);
51 decode_bare(bl);
11fdf7f2 52 decode(flags, bl);
c07f9fc5
FG
53 DECODE_FINISH(bl);
54 }
55
56 /**
57 * Unversioned encoding for use within encode_array.
58 */
59 void encode_bare(bufferlist &bl) const {
11fdf7f2
TL
60 using ceph::encode;
61 encode(cmdstring, bl);
62 encode(helpstring, bl);
63 encode(module, bl);
64 encode(req_perms, bl);
65 std::string availability = "cli,rest"; // Removed field, for backward compat
66 encode(availability, bl);
c07f9fc5 67 }
11fdf7f2
TL
68 void decode_bare(bufferlist::const_iterator &bl) {
69 using ceph::decode;
70 decode(cmdstring, bl);
71 decode(helpstring, bl);
72 decode(module, bl);
73 decode(req_perms, bl);
74 std::string availability; // Removed field, for backward compat
75 decode(availability, bl);
c07f9fc5
FG
76 }
77 bool is_compat(const MonCommand* o) const {
78 return cmdstring == o->cmdstring &&
11fdf7f2 79 module == o->module && req_perms == o->req_perms;
c07f9fc5
FG
80 }
81
9f95a23c
TL
82 bool is_tell() const {
83 return has_flag(MonCommand::FLAG_TELL);
84 }
85
c07f9fc5
FG
86 bool is_noforward() const {
87 return has_flag(MonCommand::FLAG_NOFORWARD);
88 }
89
90 bool is_obsolete() const {
91 return has_flag(MonCommand::FLAG_OBSOLETE);
92 }
93
94 bool is_deprecated() const {
95 return has_flag(MonCommand::FLAG_DEPRECATED);
96 }
97
98 bool is_mgr() const {
99 return has_flag(MonCommand::FLAG_MGR);
100 }
101
11fdf7f2
TL
102 bool is_hidden() const {
103 return has_flag(MonCommand::FLAG_HIDDEN);
104 }
105
c07f9fc5
FG
106 static void encode_array(const MonCommand *cmds, int size, bufferlist &bl) {
107 ENCODE_START(2, 1, bl);
108 uint16_t s = size;
11fdf7f2 109 encode(s, bl);
c07f9fc5
FG
110 for (int i = 0; i < size; ++i) {
111 cmds[i].encode_bare(bl);
112 }
113 for (int i = 0; i < size; i++) {
11fdf7f2 114 encode(cmds[i].flags, bl);
c07f9fc5
FG
115 }
116 ENCODE_FINISH(bl);
117 }
118 static void decode_array(MonCommand **cmds, int *size,
11fdf7f2 119 bufferlist::const_iterator &bl) {
c07f9fc5
FG
120 DECODE_START(2, bl);
121 uint16_t s = 0;
11fdf7f2 122 decode(s, bl);
c07f9fc5
FG
123 *size = s;
124 *cmds = new MonCommand[*size];
125 for (int i = 0; i < *size; ++i) {
126 (*cmds)[i].decode_bare(bl);
127 }
128 if (struct_v >= 2) {
129 for (int i = 0; i < *size; i++)
11fdf7f2 130 decode((*cmds)[i].flags, bl);
c07f9fc5
FG
131 } else {
132 for (int i = 0; i < *size; i++)
133 (*cmds)[i].flags = 0;
134 }
135 DECODE_FINISH(bl);
136 }
137
d2e6a577
FG
138 // this uses a u16 for the count, so we need a special encoder/decoder.
139 static void encode_vector(const std::vector<MonCommand>& cmds,
140 bufferlist &bl) {
141 ENCODE_START(2, 1, bl);
142 uint16_t s = cmds.size();
11fdf7f2 143 encode(s, bl);
d2e6a577
FG
144 for (unsigned i = 0; i < s; ++i) {
145 cmds[i].encode_bare(bl);
146 }
147 for (unsigned i = 0; i < s; i++) {
11fdf7f2 148 encode(cmds[i].flags, bl);
d2e6a577
FG
149 }
150 ENCODE_FINISH(bl);
151 }
152 static void decode_vector(std::vector<MonCommand> &cmds,
11fdf7f2 153 bufferlist::const_iterator &bl) {
d2e6a577
FG
154 DECODE_START(2, bl);
155 uint16_t s = 0;
11fdf7f2 156 decode(s, bl);
d2e6a577
FG
157 cmds.resize(s);
158 for (unsigned i = 0; i < s; ++i) {
159 cmds[i].decode_bare(bl);
160 }
161 if (struct_v >= 2) {
162 for (unsigned i = 0; i < s; i++)
11fdf7f2 163 decode(cmds[i].flags, bl);
d2e6a577
FG
164 } else {
165 for (unsigned i = 0; i < s; i++)
166 cmds[i].flags = 0;
167 }
168 DECODE_FINISH(bl);
169 }
170
c07f9fc5
FG
171 bool requires_perm(char p) const {
172 return (req_perms.find(p) != std::string::npos);
173 }
174};
175WRITE_CLASS_ENCODER(MonCommand)