Network Encoding

This describes the encoding used to serialize data. It doesn’t cover specificobjects/messages but focuses on the base types.

The types are not self documenting in any way. They can not be decoded unlessyou know what they are.

Conventions

Integers

The integer types used will be named {signed}{size}{endian}. For exampleu16le is an unsigned 16 bit integer encoded in little endian byte orderwhile s64be is a signed 64 bit integer in big endian. Additionally u8and s8 will represent signed and unsigned bytes respectively. Signedintegers use two’s complement encoding.

Complex Types

This document will use a c-like syntax for describing structures. Thestructure represents the data that will go over the wire. There will be nopadding between the elements and the elements will be sent in the order theyappear. For example:

  1. struct foo {
  2. u8 tag;
  3. u32le data;
  4. }

When encoding the values 0x05 and 0x12345678 respectively will appear onthe wire as 05 78 56 34 12.

Variable Arrays

Unlike c, length arrays can be used anywhere in structures and will be inline inthe protocol. Furthermore the length may be described using an earlier item inthe structure.

  1. struct blob {
  2. u32le size;
  3. u8 data[size];
  4. u32le checksum;
  5. }

This structure is encoded as a 32 bit size, followed by size data bytes,then a 32 bit checksum.

Primitive Aliases

These types are just aliases for primitive types.

  1. // From /src/include/types.h
  2.  
  3. typedef u32le epoch_t;
  4. typedef u32le ceph_seq_t;
  5. typedef u64le ceph_tid_t;
  6. typedef u64le version_t;

Structures

These are the way structures are encoded. Note that these structures don’tactually exist in the source but are the way that different types are encoded.

Optional

Optionals are represented as a presence byte, followed by the item if it exists.

  1. struct ceph_optional<T> {
  2. u8 present;
  3. T element[present? 1 : 0]; // Only if present is non-zero.
  4. }

Optionals are used to encode boost::optional.

Pair

Pairs are simply the first item followed by the second.

  1. struct ceph_pair<A,B> {
  2. A a;
  3. B b;
  4. }

Pairs are used to encode std::pair.

Triple

Triples are simply the tree elements one after another.

  1. struct ceph_triple<A,B,C> {
  2. A a;
  3. B b;
  4. C c;
  5. }

Triples are used to encode ceph::triple.

List

Lists are represented as an element count followed by that many elements.

  1. struct ceph_list<T> {
  2. u32le length;
  3. T elements[length];
  4. }

Note

The size of the elements in the list are not necessarily uniform.

Lists are used to encode std::list, std::vector, std::deque,std::set and ceph::unordered_set.

Blob

A Blob is simply a list of bytes.

  1. struct ceph_string {
  2. ceph_list<u8>;
  3. }
  4.  
  5. // AKA
  6.  
  7. struct ceph_string {
  8. u32le size;
  9. u8 data[size];
  10. }

Blobs are used to encode std::string, const char * and bufferlist.

Note

The content of a Blob is arbitrary binary data.

Map

Maps are a list of pairs.

  1. struct ceph_map<K,V> {
  2. ceph_list<ceph_pair<K,V>>;
  3. }
  4.  
  5. // AKA
  6.  
  7. struct ceph_map<K,V> {
  8. u32le length;
  9. ceph_pair<K,V> entries[length];
  10. }

Maps are used to encode std::map, std::multimap andceph::unordered_map.

Complex Types

These aren’t hard to find in the source but the common ones are listed here forconvenience.

utime_t

  1. // From /src/include/utime.h
  2. struct utime_t {
  3. u32le tv_sec; // Seconds since epoch.
  4. u32le tv_nsec; // Nanoseconds since the last second.
  5. }

ceph_entity_name

  1. // From /src/include/msgr.h
  2. struct ceph_entity_name {
  3. u8 type; // CEPH_ENTITY_TYPE_*
  4. u64le num;
  5. }
  6.  
  7. // CEPH_ENTITY_TYPE_* defined in /src/include/msgr.h