summaryrefslogtreecommitdiffstats
path: root/src/vnet/dhcp/dhcp6_pd_client_dp.h
blob: 88c731c2e67dd7e667cdf180fa3d7fadf02acd36 (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
169
170
171
172
173
174
175
176
177
178
179
/*
 * Copyright (c) 2018 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef included_vnet_dhcp6_pd_client_dp_h
#define included_vnet_dhcp6_pd_client_dp_h

#include <vlib/vlib.h>
#include <vnet/dhcp/dhcp6_client_common_dp.h>

typedef struct
{
  u32 preferred_lt;
  u32 valid_lt;
  ip6_address_t prefix;
  u8 prefix_length;
} dhcp6_pd_send_client_message_params_prefix_t;

typedef struct
{
  u32 sw_if_index;
  u32 server_index;
  u32 irt;
  u32 mrt;
  u32 mrc;
  u32 mrd;
  u8 msg_type;
  u32 T1;
  u32 T2;
  dhcp6_pd_send_client_message_params_prefix_t *prefixes;
} dhcp6_pd_send_client_message_params_t;

typedef struct
{
  u8 entry_valid;
  u8 keep_sending_client_message;	/* when true then next fields are valid */
  dhcp6_pd_send_client_message_params_t params;
  f64 transaction_start;
  f64 sleep_interval;
  f64 due_time;
  u32 n_left;
  f64 start_time;
  u32 transaction_id;
  vlib_buffer_t *buffer;
  u32 elapsed_pos;
  u32 adj_index;
} dhcp6_pd_client_state_t;

typedef struct
{
  dhcp6_pd_client_state_t *client_state_by_sw_if_index;

  uword publisher_node;
  uword publisher_et;

  u32 seed;

  /* convenience */
  vlib_main_t *vlib_main;
  vnet_main_t *vnet_main;
} dhcp6_pd_client_main_t;

extern dhcp6_pd_client_main_t dhcp6_pd_client_main;

typedef struct
{
  ip6_address_t prefix;
  u8 prefix_length;
  u32 valid_time;
  u32 preferred_time;
  u16 status_code;
} dhcp6_prefix_info_t;

typedef struct
{
  dhcp6_report_common_t body;
  u32 n_prefixes;
  dhcp6_prefix_info_t *prefixes;
} prefix_report_t;

#define vl_typedefs		/* define message structures */
#include <vnet/vnet_all_api_h.h>
#undef vl_typedefs

void dhcp6_pd_send_client_message (vlib_main_t * vm, u32 sw_if_index, u8 stop,
				   dhcp6_pd_send_client_message_params_t *
				   params);
void dhcp6_pd_set_publisher_node (uword node_index, uword event_type);
int dhcp6_pd_publish_report (prefix_report_t * r);

void
  vl_api_want_dhcp6_pd_reply_events_t_handler
  (vl_api_want_dhcp6_pd_reply_events_t * mp);
void
  vl_api_dhcp6_pd_send_client_message_t_handler
  (vl_api_dhcp6_pd_send_client_message_t * mp);
void
  vl_api_dhcp6_clients_enable_disable_t_handler
  (vl_api_dhcp6_clients_enable_disable_t * mp);

extern vlib_node_registration_t dhcp6_pd_reply_process_node;

enum
{ DHCP6_PD_DP_REPLY_REPORT, DHCP6_PD_DP_REPORT_MAX };

typedef struct _vnet_dhcp6_pd_reply_function_list_elt
{
  struct _vnet_dhcp6_pd_reply_function_list_elt
    *next_dhcp6_pd_reply_event_function;
  clib_error_t *(*fp) (vl_api_dhcp6_pd_reply_event_t * mp);
} _vnet_dhcp6_pd_reply_event_function_list_elt_t;

typedef struct
{
  _vnet_dhcp6_pd_reply_event_function_list_elt_t *functions;
} dhcp6_pd_client_public_main_t;

extern dhcp6_pd_client_public_main_t dhcp6_pd_client_public_main;

#define VNET_DHCP6_PD_REPLY_EVENT_FUNCTION(f)                             \
                                                                          \
static void __vnet_dhcp6_pd_reply_event_function_init_##f (void)          \
    __attribute__((__constructor__)) ;                                    \
                                                                          \
static void __vnet_dhcp6_pd_reply_event_function_init_##f (void)          \
{                                                                         \
 dhcp6_pd_client_public_main_t * nm = &dhcp6_pd_client_public_main;       \
 static _vnet_dhcp6_pd_reply_event_function_list_elt_t init_function;     \
 init_function.next_dhcp6_pd_reply_event_function = nm->functions;        \
 nm->functions = &init_function;                                          \
 init_function.fp = (void *) &f;                                          \
}                                                                         \
                                                                          \
static void __vnet_dhcp6_pd_reply_event_function_deinit_##f (void)        \
    __attribute__((__destructor__)) ;                                     \
                                                                          \
static void __vnet_dhcp6_pd_reply_event_function_deinit_##f (void)        \
{                                                                         \
 dhcp6_pd_client_public_main_t * nm = &dhcp6_pd_client_public_main;       \
 _vnet_dhcp6_pd_reply_event_function_list_elt_t *next;                    \
 if (nm->functions->fp == (void *) &f)                                    \
    {                                                                     \
      nm->functions =                                                     \
        nm->functions->next_dhcp6_pd_reply_event_function;                \
      return;                                                             \
    }                                                                     \
  next = nm->functions;                                                   \
  while (next->next_dhcp6_pd_reply_event_function)                        \
    {                                                                     \
      if (next->next_dhcp6_pd_reply_event_function->fp == (void *) &f)    \
        {                                                                 \
          next->next_dhcp6_pd_reply_event_function =                      \
            next->next_dhcp6_pd_reply_event_function->next_dhcp6_pd_reply_event_function; \
          return;                                                         \
        }                                                                 \
      next = next->next_dhcp6_pd_reply_event_function;                    \
    }                                                                     \
}

#endif /* included_vnet_dhcp6_pd_client_dp_h */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
variable length stored in: %s)" % (self.name, self.type, self.nelem_field)) def is_vla(self): return self.nelem_field is not None def has_vla(self): return self.is_vla() or self.type.has_vla() class Alias(Field): pass class Type(object): def __init__(self, name): self.name = name def __str__(self): return self.name class SimpleType (Type): def has_vla(self): return False def get_msg_header_defs(struct_type_class, field_class, json_parser, logger): return [ struct_type_class(['msg_header1_t', ['u16', '_vl_msg_id'], ['u32', 'context'], ], json_parser, field_class, logger ), struct_type_class(['msg_header2_t', ['u16', '_vl_msg_id'], ['u32', 'client_index'], ['u32', 'context'], ], json_parser, field_class, logger ), ] class Struct(object): def __init__(self, name, fields): self.name = name self.fields = fields self.field_names = [n.name for n in self.fields] self.depends = [f.type for f in self.fields] def __str__(self): return "[%s]" % "], [".join([str(f) for f in self.fields]) def has_vla(self): for f in self.fields: if f.has_vla(): return True return False class Enum(SimpleType): def __init__(self, name, value_pairs, enumtype): super(Enum, self).__init__(name) self.type = enumtype self.value_pairs = value_pairs def __str__(self): return "Enum(%s, [%s])" % ( self.name, "], [" .join(["%s => %s" % (i, j) for i, j in self.value_pairs]) ) class Union(Type): def __init__(self, name, type_pairs, crc): Type.__init__(self, name) self.crc = crc self.type_pairs = type_pairs self.depends = [t for t, _ in self.type_pairs] def __str__(self): return "Union(%s, [%s])" % ( self.name, "], [" .join(["%s %s" % (i, j) for i, j in self.type_pairs]) ) def has_vla(self): return False class Message(object): def __init__(self, logger, definition, json_parser): struct_type_class = json_parser.struct_type_class field_class = json_parser.field_class self.request = None self.logger = logger m = definition logger.debug("Parsing message definition `%s'" % m) name = m[0] self.name = name logger.debug("Message name is `%s'" % name) ignore = True self.header = None self.is_reply = json_parser.is_reply(self.name) self.is_event = json_parser.is_event(self.name) fields = [] for header in get_msg_header_defs(struct_type_class, field_class, json_parser, logger): logger.debug("Probing header `%s'" % header.name) if header.is_part_of_def(m[1:]): self.header = header logger.debug("Found header `%s'" % header.name) fields.append(field_class(field_name='header', field_type=self.header)) ignore = False break if ignore and not self.is_event and not self.is_reply: raise ParseError("While parsing message `%s': could not find all " "common header fields" % name) for field in m[1:]: if len(field) == 1 and 'crc' in field: self.crc = field['crc'] logger.debug("Found CRC `%s'" % self.crc) continue else: field_type = json_parser.lookup_type_like_id(field[0]) logger.debug("Parsing message field `%s'" % field) if len(field) == 2: if self.header is not None and\ self.header.has_field(field[1]): continue p = field_class(field_name=field[1], field_type=field_type) elif len(field) == 3: if field[2] == 0: raise ParseError( "While parsing message `%s': variable length " "array `%s' doesn't have reference to member " "containing the actual length" % ( name, field[1])) p = field_class( field_name=field[1], field_type=field_type, array_len=field[2]) elif len(field) == 4: nelem_field = None for f in fields: if f.name == field[3]: nelem_field = f if nelem_field is None: raise ParseError( "While parsing message `%s': couldn't find " "variable length array `%s' member containing " "the actual length `%s'" % ( name, field[1], field[3])) p = field_class( field_name=field[1], field_type=field_type, array_len=field[2], nelem_field=nelem_field) else: raise Exception("Don't know how to parse message " "definition for message `%s': `%s'" % (m, m[1:])) logger.debug("Parsed field `%s'" % p) fields.append(p) self.fields = fields self.depends = [f.type for f in self.fields] logger.debug("Parsed message: %s" % self) def __str__(self): return "Message(%s, [%s], {crc: %s}" % \ (self.name, "], [".join([str(f) for f in self.fields]), self.crc) class StructType (Type, Struct): def __init__(self, definition, json_parser, field_class, logger): t = definition logger.debug("Parsing struct definition `%s'" % t) name = t[0] fields = [] for field in t[1:]: if len(field) == 1 and 'crc' in field: self.crc = field['crc'] continue field_type = json_parser.lookup_type_like_id(field[0]) logger.debug("Parsing type field `%s'" % field) if len(field) == 2: p = field_class(field_name=field[1], field_type=field_type) elif len(field) == 3: if field[2] == 0: raise ParseError("While parsing type `%s': array `%s' has " "variable length" % (name, field[1])) p = field_class(field_name=field[1], field_type=field_type, array_len=field[2]) elif len(field) == 4: nelem_field = None for f in fields: if f.name == field[3]: nelem_field = f if nelem_field is None: raise ParseError( "While parsing message `%s': couldn't find " "variable length array `%s' member containing " "the actual length `%s'" % ( name, field[1], field[3])) p = field_class(field_name=field[1], field_type=field_type, array_len=field[2], nelem_field=nelem_field) else: raise ParseError( "Don't know how to parse field `%s' of type definition " "for type `%s'" % (field, t)) fields.append(p) Type.__init__(self, name) Struct.__init__(self, name, fields) def __str__(self): return "StructType(%s, %s)" % (Type.__str__(self), Struct.__str__(self)) def has_field(self, name): return name in self.field_names def is_part_of_def(self, definition): for idx in range(len(self.fields)): field = definition[idx] p = self.fields[idx] if field[1] != p.name: return False if field[0] != p.type.name: raise ParseError( "Unexpected field type `%s' (should be `%s'), " "while parsing msg/def/field `%s/%s/%s'" % (field[0], p.type, p.name, definition, field)) return True class JsonParser(object): def __init__(self, logger, files, simple_type_class=SimpleType, enum_class=Enum, union_class=Union, struct_type_class=StructType, field_class=Field, message_class=Message, alias_class=Alias): self.services = {} self.messages = {} self.enums = {} self.unions = {} self.aliases = {} self.types = { x: simple_type_class(x) for x in [ 'i8', 'i16', 'i32', 'i64', 'u8', 'u16', 'u32', 'u64', 'f64' ] } self.types['string'] = simple_type_class('vl_api_string_t') self.replies = set() self.events = set() self.simple_type_class = simple_type_class self.enum_class = enum_class self.union_class = union_class self.struct_type_class = struct_type_class self.field_class = field_class self.alias_class = alias_class self.message_class = message_class self.exceptions = [] self.json_files = [] self.types_by_json = {} self.enums_by_json = {} self.unions_by_json = {} self.aliases_by_json = {} self.messages_by_json = {} self.logger = logger for f in files: self.parse_json_file(f) self.finalize_parsing() def parse_json_file(self, path): self.logger.info("Parsing json api file: `%s'" % path) self.json_files.append(path) self.types_by_json[path] = [] self.enums_by_json[path] = [] self.unions_by_json[path] = [] self.aliases_by_json[path] = [] self.messages_by_json[path] = {} with open(path) as f: j = json.load(f) for k in j['services']: if k in self.services: raise ParseError("Duplicate service `%s'" % k) self.services[k] = j['services'][k] self.replies.add(self.services[k]["reply"]) if "events" in self.services[k]: for x in self.services[k]["events"]: self.events.add(x) for e in j['enums']: name = e[0] value_pairs = e[1:-1] enumtype = self.types[e[-1]["enumtype"]] enum = self.enum_class(name, value_pairs, enumtype) self.enums[enum.name] = enum self.logger.debug("Parsed enum: %s" % enum) self.enums_by_json[path].append(enum) exceptions = [] progress = 0 last_progress = 0 while True: for u in j['unions']: name = u[0] if name in self.unions: progress = progress + 1 continue try: type_pairs = [[self.lookup_type_like_id(t), n] for t, n in u[1:-1]] crc = u[-1]["crc"] union = self.union_class(name, type_pairs, crc) progress = progress + 1 except ParseError as e: exceptions.append(e) continue self.unions[union.name] = union self.logger.debug("Parsed union: %s" % union) self.unions_by_json[path].append(union) for name, body in j['aliases'].iteritems(): if name in self.aliases: progress = progress + 1 continue if 'length' in body: array_len = body['length'] else: array_len = None t = self.types[body['type']] alias = self.alias_class(name, t, array_len) self.aliases[name] = alias self.logger.debug("Parsed alias: %s" % alias) self.aliases_by_json[path].append(alias) for t in j['types']: if t[0] in self.types: progress = progress + 1 continue try: type_ = self.struct_type_class(t, self, self.field_class, self.logger) if type_.name in self.types: raise ParseError( "Duplicate type `%s'" % type_.name) progress = progress + 1 except ParseError as e: exceptions.append(e) continue self.types[type_.name] = type_ self.types_by_json[path].append(type_) self.logger.debug("Parsed type: %s" % type_) if not exceptions: # finished parsing break if progress <= last_progress: # cannot make forward progress self.exceptions.extend(exceptions) break exceptions = [] last_progress = progress progress = 0 prev_length = len(self.messages) processed = [] while True: exceptions = [] for m in j['messages']: if m in processed: continue try: msg = self.message_class(self.logger, m, self) if msg.name in self.messages: raise ParseError( "Duplicate message `%s'" % msg.name) except ParseError as e: exceptions.append(e) continue self.messages[msg.name] = msg self.messages_by_json[path][msg.name] = msg processed.append(m) if prev_length == len(self.messages): # cannot make forward progress ... self.exceptions.extend(exceptions) break prev_length = len(self.messages) def lookup_type_like_id(self, name): mundane_name = remove_magic(name) if name in self.types: return self.types[name] elif name in self.enums: return self.enums[name] elif name in self.unions: return self.unions[name] elif name in self.aliases: return self.aliases[name] elif mundane_name in self.types: return self.types[mundane_name] elif mundane_name in self.enums: return self.enums[mundane_name] elif mundane_name in self.unions: return self.unions[mundane_name] elif mundane_name in self.aliases: return self.aliases[mundane_name] raise ParseError( "Could not find type, enum or union by magic name `%s' nor by " "mundane name `%s'" % (name, mundane_name)) def is_reply(self, message): return message in self.replies def is_event(self, message): return message in self.events def get_reply(self, message): return self.messages[self.services[message]['reply']] def finalize_parsing(self): if len(self.messages) == 0: for e in self.exceptions: self.logger.warning(e) for jn, j in self.messages_by_json.items(): remove = [] for n, m in j.items(): try: if not m.is_reply and not m.is_event: try: m.reply = self.get_reply(n) if "stream" in self.services[m.name]: m.reply_is_stream = \ self.services[m.name]["stream"] else: m.reply_is_stream = False m.reply.request = m except: raise ParseError( "Cannot find reply to message `%s'" % n) except ParseError as e: self.exceptions.append(e) remove.append(n) self.messages_by_json[jn] = { k: v for k, v in j.items() if k not in remove}