/
vr_sandesh.c
143 lines (126 loc) · 4.22 KB
/
vr_sandesh.c
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
/*
* vr_sandesh.c -- sandesh messaging protocol for datapath
*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "vr_os.h"
#include "vr_types.h"
#include "vr_message.h"
#include "vr_sandesh.h"
struct sandesh_object_md sandesh_md[] = {
[VR_NULL_OBJECT_ID] = {
.obj_len = sizeof(vr_response),
.obj_type_string = "vr_null_object",
},
[VR_INTERFACE_OBJECT_ID] = {
.obj_len = 4 * sizeof(vr_interface_req),
.obj_type_string = "vr_interface_req",
},
[VR_NEXTHOP_OBJECT_ID] = {
.obj_len = 4 * sizeof(vr_nexthop_req),
.obj_type_string = "vr_nexthop_req",
},
[VR_ROUTE_OBJECT_ID] = {
.obj_len = 4 * sizeof(vr_route_req),
.obj_type_string = "vr_route_req",
},
[VR_MPLS_OBJECT_ID] = {
.obj_len = 4 * sizeof(vr_mpls_req),
.obj_type_string = "vr_mpls_req"
},
[VR_MIRROR_OBJECT_ID] = {
.obj_len = 4 * sizeof(vr_mirror_req),
.obj_type_string = "vr_mirror_req",
},
[VR_FLOW_OBJECT_ID] = {
.obj_len = ((4 * sizeof(vr_flow_req)) +
(64 * sizeof(unsigned int))),
.obj_type_string = "vr_flow_req",
},
[VR_VRF_ASSIGN_OBJECT_ID] = {
.obj_len = 4 * sizeof(vr_vrf_assign_req),
.obj_type_string = "vr_vrf_assign_req",
},
[VR_VRF_STATS_OBJECT_ID] = {
.obj_len = 4 * sizeof(vr_vrf_stats_req),
.obj_type_string = "vr_vrf_stats_req",
},
[VR_DROP_STATS_OBJECT_ID] = {
.obj_len = 4 * sizeof(vr_vrf_stats_req),
.obj_type_string = "vr_drop_stats_req",
},
[VR_RESPONSE_OBJECT_ID] = {
.obj_len = 4 * sizeof(vr_response),
.obj_type_string = "vr_response",
},
[VR_VXLAN_OBJECT_ID] = {
.obj_len = 4 * sizeof(vr_vxlan_req),
.obj_type_string = "vr_vxlan_req",
},
};
static unsigned int
sandesh_proto_buf_len(unsigned int object_type, void *object)
{
if (!object && object_type != VR_RESPONSE_OBJECT_ID)
return 0;
return sandesh_md[object_type].obj_len;
}
static int
sandesh_proto_encode(char *buf, unsigned int len,
unsigned int object_type, void *object,
unsigned int message_type)
{
int off = 0, error = 0;
if (object)
off = sandesh_encode(object,
sandesh_md[object_type].obj_type_string,
vr_find_sandesh_info, (unsigned char *)buf, len, &error);
if (error)
return -1;
return off;
}
static int
sandesh_proto_encode_response(char *buf, unsigned int len,
unsigned int object_type, void *object, int ret)
{
int off, error;
vr_response resp;
resp.h_op = SANDESH_OP_RESPONSE;
resp.resp_code = ret;
off = sandesh_encode(&resp, "vr_response", vr_find_sandesh_info,
(unsigned char *)buf, len, &error);
if (off < 0)
return off;
error = sandesh_proto_encode(buf + off, len - off,
object_type, object, VR_MESSAGE_TYPE_RESPONSE);
if (error < 0)
return error;
return off + error;
}
static int
sandesh_proto_decode(char *buf, unsigned int len,
int (*cb)(void *, unsigned int, void *), void *cb_arg)
{
int ret = 0;
sandesh_decode((unsigned char *)buf, len, vr_find_sandesh_info, &ret);
return ret;
}
static struct vr_mproto sandesh_mproto = {
.mproto_type = VR_MPROTO_SANDESH,
.mproto_buf_len = sandesh_proto_buf_len,
.mproto_encode = sandesh_proto_encode,
.mproto_encode_response = sandesh_proto_encode_response,
.mproto_decode = sandesh_proto_decode,
};
void
vr_sandesh_exit(void)
{
vr_message_proto_unregister(&sandesh_mproto);
return;
}
int
vr_sandesh_init(void)
{
vr_message_proto_register(&sandesh_mproto);
return 0;
}