1 /* mlag generic code.
2  * Copyright (C) 2018 Cumulus Networks, Inc.
3  *                    Donald Sharp
4  *
5  * This file is part of FRR.
6  *
7  * FRR is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2, or (at your option) any
10  * later version.
11  *
12  * FRR is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with FRR; see the file COPYING.  If not, write to the Free
19  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20  * 02111-1307, USA.
21  */
22 #include <zebra.h>
23 
24 #include <mlag.h>
25 
mlag_role2str(enum mlag_role role,char * buf,size_t size)26 char *mlag_role2str(enum mlag_role role, char *buf, size_t size)
27 {
28 	switch (role) {
29 	case MLAG_ROLE_NONE:
30 		snprintf(buf, size, "NONE");
31 		break;
32 	case MLAG_ROLE_PRIMARY:
33 		snprintf(buf, size, "PRIMARY");
34 		break;
35 	case MLAG_ROLE_SECONDARY:
36 		snprintf(buf, size, "SECONDARY");
37 		break;
38 	}
39 
40 	return buf;
41 }
42 
mlag_lib_msgid_to_str(enum mlag_msg_type msg_type,char * buf,size_t size)43 char *mlag_lib_msgid_to_str(enum mlag_msg_type msg_type, char *buf, size_t size)
44 {
45 	switch (msg_type) {
46 	case MLAG_REGISTER:
47 		snprintf(buf, size, "Register");
48 		break;
49 	case MLAG_DEREGISTER:
50 		snprintf(buf, size, "De-Register");
51 		break;
52 	case MLAG_MROUTE_ADD:
53 		snprintf(buf, size, "Mroute add");
54 		break;
55 	case MLAG_MROUTE_DEL:
56 		snprintf(buf, size, "Mroute del");
57 		break;
58 	case MLAG_DUMP:
59 		snprintf(buf, size, "Mlag Replay");
60 		break;
61 	case MLAG_MROUTE_ADD_BULK:
62 		snprintf(buf, size, "Mroute Add Batch");
63 		break;
64 	case MLAG_MROUTE_DEL_BULK:
65 		snprintf(buf, size, "Mroute Del Batch");
66 		break;
67 	case MLAG_STATUS_UPDATE:
68 		snprintf(buf, size, "Mlag Status");
69 		break;
70 	case MLAG_VXLAN_UPDATE:
71 		snprintf(buf, size, "Mlag vxlan update");
72 		break;
73 	case MLAG_PEER_FRR_STATUS:
74 		snprintf(buf, size, "Mlag Peer FRR Status");
75 		break;
76 	default:
77 		snprintf(buf, size, "Unknown %d", msg_type);
78 		break;
79 	}
80 	return buf;
81 }
82 
83 
mlag_lib_decode_mlag_hdr(struct stream * s,struct mlag_msg * msg,size_t * length)84 int mlag_lib_decode_mlag_hdr(struct stream *s, struct mlag_msg *msg,
85 			     size_t *length)
86 {
87 #define LIB_MLAG_HDR_LENGTH 8
88 	if (s == NULL || msg == NULL)
89 		return -1;
90 
91 	*length = stream_get_endp(s);
92 
93 	if (*length < LIB_MLAG_HDR_LENGTH)
94 		return -1;
95 
96 	*length -= LIB_MLAG_HDR_LENGTH;
97 
98 	STREAM_GETL(s, msg->msg_type);
99 	STREAM_GETW(s, msg->data_len);
100 	STREAM_GETW(s, msg->msg_cnt);
101 
102 	return 0;
103 stream_failure:
104 	return -1;
105 }
106 
107 #define MLAG_MROUTE_ADD_LENGTH                                                 \
108 	(VRF_NAMSIZ + INTERFACE_NAMSIZ + 4 + 4 + 4 + 4 + 1 + 1 + 4)
109 
mlag_lib_decode_mroute_add(struct stream * s,struct mlag_mroute_add * msg,size_t * length)110 int mlag_lib_decode_mroute_add(struct stream *s, struct mlag_mroute_add *msg,
111 			       size_t *length)
112 {
113 	if (s == NULL || msg == NULL || *length < MLAG_MROUTE_ADD_LENGTH)
114 		return -1;
115 
116 	STREAM_GET(msg->vrf_name, s, VRF_NAMSIZ);
117 	STREAM_GETL(s, msg->source_ip);
118 	STREAM_GETL(s, msg->group_ip);
119 	STREAM_GETL(s, msg->cost_to_rp);
120 	STREAM_GETL(s, msg->owner_id);
121 	STREAM_GETC(s, msg->am_i_dr);
122 	STREAM_GETC(s, msg->am_i_dual_active);
123 	STREAM_GETL(s, msg->vrf_id);
124 	STREAM_GET(msg->intf_name, s, INTERFACE_NAMSIZ);
125 
126 	return 0;
127 stream_failure:
128 	return -1;
129 }
130 
131 #define MLAG_MROUTE_DEL_LENGTH (VRF_NAMSIZ + INTERFACE_NAMSIZ + 4 + 4 + 4 + 4)
132 
mlag_lib_decode_mroute_del(struct stream * s,struct mlag_mroute_del * msg,size_t * length)133 int mlag_lib_decode_mroute_del(struct stream *s, struct mlag_mroute_del *msg,
134 			       size_t *length)
135 {
136 	if (s == NULL || msg == NULL || *length < MLAG_MROUTE_DEL_LENGTH)
137 		return -1;
138 
139 	STREAM_GET(msg->vrf_name, s, VRF_NAMSIZ);
140 	STREAM_GETL(s, msg->source_ip);
141 	STREAM_GETL(s, msg->group_ip);
142 	STREAM_GETL(s, msg->owner_id);
143 	STREAM_GETL(s, msg->vrf_id);
144 	STREAM_GET(msg->intf_name, s, INTERFACE_NAMSIZ);
145 
146 	return 0;
147 stream_failure:
148 	return -1;
149 }
150 
mlag_lib_decode_mlag_status(struct stream * s,struct mlag_status * msg)151 int mlag_lib_decode_mlag_status(struct stream *s, struct mlag_status *msg)
152 {
153 	if (s == NULL || msg == NULL)
154 		return -1;
155 
156 	STREAM_GET(msg->peerlink_rif, s, INTERFACE_NAMSIZ);
157 	STREAM_GETL(s, msg->my_role);
158 	STREAM_GETL(s, msg->peer_state);
159 	return 0;
160 stream_failure:
161 	return -1;
162 }
163 
mlag_lib_decode_vxlan_update(struct stream * s,struct mlag_vxlan * msg)164 int mlag_lib_decode_vxlan_update(struct stream *s, struct mlag_vxlan *msg)
165 {
166 	if (s == NULL || msg == NULL)
167 		return -1;
168 
169 	STREAM_GETL(s, msg->anycast_ip);
170 	STREAM_GETL(s, msg->local_ip);
171 	return 0;
172 
173 stream_failure:
174 	return -1;
175 }
176 
mlag_lib_decode_frr_status(struct stream * s,struct mlag_frr_status * msg)177 int mlag_lib_decode_frr_status(struct stream *s, struct mlag_frr_status *msg)
178 {
179 	if (s == NULL || msg == NULL)
180 		return -1;
181 
182 	STREAM_GETL(s, msg->frr_state);
183 	return 0;
184 stream_failure:
185 	return -1;
186 }
187