uint8_t response_keep;
uint32_t first;
uint32_t last;
+ uint8_t proto;
+ unsigned short instance;
s = zclient->ibuf;
+ STREAM_GETC(s, proto);
+ STREAM_GETW(s, instance);
STREAM_GETC(s, response_keep);
STREAM_GETL(s, first);
STREAM_GETL(s, last);
+ if (zclient->redist_default != proto) {
+ zlog_err("Got LM msg with wrong proto %u", proto);
+ return;
+ }
+ if (zclient->instance != instance) {
+ zlog_err("Got LM msg with wrong instance %u", proto);
+ return;
+ }
+
if (first > last ||
first < MPLS_LABEL_UNRESERVED_MIN ||
last > MPLS_LABEL_UNRESERVED_MAX) {
return 0;
}
-static void zapi_encode_prefix(struct stream *s,
- struct prefix *p,
- uint8_t family)
+static void zapi_encode_prefix(struct stream *s, struct prefix *p,
+ uint8_t family)
{
struct prefix any;
stream_put(s, &p->u.prefix, prefix_blen(p));
}
-int zapi_pbr_rule_encode(uint8_t cmd, struct stream *s,
- struct pbr_rule *zrule)
+int zapi_pbr_rule_encode(uint8_t cmd, struct stream *s, struct pbr_rule *zrule)
{
stream_reset(s);
zclient_create_header(s, cmd, zrule->vrf_id);
zapi_encode_prefix(s, &(zrule->filter.src_ip),
zrule->filter.src_ip.family);
- stream_putw(s, zrule->filter.src_port); /* src port */
+ stream_putw(s, zrule->filter.src_port); /* src port */
zapi_encode_prefix(s, &(zrule->filter.dst_ip),
zrule->filter.src_ip.family);
- stream_putw(s, zrule->filter.dst_port); /* dst port */
- stream_putw(s, zrule->filter.fwmark); /* fwmark */
+ stream_putw(s, zrule->filter.dst_port); /* dst port */
+ stream_putw(s, zrule->filter.fwmark); /* fwmark */
stream_putl(s, zrule->action.table);
stream_putl(s, zrule->ifindex);
STREAM_GETL(s, ifi);
if (zclient_debug)
- zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__,
- seq, prio, uni, ifi);
+ zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__, seq, prio,
+ uni, ifi);
*seqno = seq;
*priority = prio;
*unique = uni;
return false;
}
-bool zapi_ipset_notify_decode(struct stream *s,
- uint32_t *unique,
- enum zapi_ipset_notify_owner *note)
+bool zapi_ipset_notify_decode(struct stream *s, uint32_t *unique,
+ enum zapi_ipset_notify_owner *note)
{
uint32_t uni;
return false;
}
-bool zapi_ipset_entry_notify_decode(struct stream *s,
- uint32_t *unique,
- char *ipset_name,
- enum zapi_ipset_entry_notify_owner *note)
+bool zapi_ipset_entry_notify_decode(struct stream *s, uint32_t *unique,
+ char *ipset_name,
+ enum zapi_ipset_entry_notify_owner *note)
{
uint32_t uni;
STREAM_GETL(s, uni);
- STREAM_GET(ipset_name, s,
- ZEBRA_IPSET_NAME_SIZE);
+ STREAM_GET(ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
if (zclient_debug)
zlog_debug("%s: %u", __PRETTY_FUNCTION__, uni);
*/
if (znh->label_num) {
nexthop_add_labels(n, ZEBRA_LSP_NONE, znh->label_num,
- znh->labels);
+ znh->labels);
}
return n;
if (nhr->nexthops[i].label_num)
STREAM_GET(&nhr->nexthops[i].labels[0], s,
nhr->nexthops[i].label_num
- * sizeof(mpls_label_t));
+ * sizeof(mpls_label_t));
}
return true;
zlog_warn(
"warning: interface %s address %s "
"with peer flag set, but no peer address!",
- ifp->name, prefix2str(ifc->address, buf,
- sizeof buf));
+ ifp->name,
+ prefix2str(ifc->address, buf,
+ sizeof buf));
UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
}
}
uint8_t result;
if (zclient_debug)
- zlog_debug("Connecting to Label Manager");
+ zlog_debug("Connecting to Label Manager (LM)");
if (zclient->sock < 0)
return -1;
ret = writen(zclient->sock, s->data, stream_get_endp(s));
if (ret < 0) {
- zlog_err("%s: can't write to zclient->sock", __func__);
+ zlog_err("Can't write to zclient sock");
close(zclient->sock);
zclient->sock = -1;
return -1;
}
if (ret == 0) {
- zlog_err("%s: zclient->sock connection closed", __func__);
+ zlog_err("Zclient sock closed");
close(zclient->sock);
zclient->sock = -1;
return -1;
}
if (zclient_debug)
- zlog_debug("%s: Label manager connect request (%d bytes) sent",
- __func__, ret);
+ zlog_debug("LM connect request sent (%d bytes)", ret);
/* read response */
if (zclient_read_sync_response(zclient, ZEBRA_LABEL_MANAGER_CONNECT)
!= 0)
return -1;
- /* result */
s = zclient->ibuf;
+
+ /* read instance and proto */
+ uint8_t proto = stream_getc(s);
+ uint16_t instance = stream_getw(s);
+
+ /* sanity */
+ if (proto != zclient->redist_default)
+ zlog_err(
+ "Wrong proto (%u) in LM connect response. Should be %u",
+ proto, zclient->redist_default);
+ if (instance != zclient->instance)
+ zlog_err(
+ "Wrong instId (%u) in LM connect response. Should be %u",
+ instance, zclient->instance);
+
+ /* result code */
result = stream_getc(s);
if (zclient_debug)
- zlog_debug(
- "%s: Label Manager connect response received, result %u",
- __func__, result);
+ zlog_debug("LM connect-response received, result %u", result);
return (int)result;
}
* @param chunk_size Amount of labels requested
* @result 0 on success, -1 otherwise
*/
-int zclient_send_get_label_chunk(
- struct zclient *zclient,
- uint8_t keep,
- uint32_t chunk_size)
+int zclient_send_get_label_chunk(struct zclient *zclient, uint8_t keep,
+ uint32_t chunk_size)
{
struct stream *s;
stream_reset(s);
zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT);
+ /* proto */
+ stream_putc(s, zclient->redist_default);
+ /* instance */
+ stream_putw(s, zclient->instance);
stream_putc(s, keep);
stream_putl(s, chunk_size);
s = zclient->obuf;
stream_reset(s);
zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT);
+ /* proto */
+ stream_putc(s, zclient->redist_default);
+ /* instance */
+ stream_putw(s, zclient->instance);
/* keep */
stream_putc(s, keep);
/* chunk size */
ret = writen(zclient->sock, s->data, stream_get_endp(s));
if (ret < 0) {
- zlog_err("%s: can't write to zclient->sock", __func__);
+ zlog_err("Can't write to zclient sock");
close(zclient->sock);
zclient->sock = -1;
return -1;
}
if (ret == 0) {
- zlog_err("%s: zclient->sock connection closed", __func__);
+ zlog_err("Zclient sock closed");
close(zclient->sock);
zclient->sock = -1;
return -1;
}
if (zclient_debug)
- zlog_debug("%s: Label chunk request (%d bytes) sent", __func__,
- ret);
+ zlog_debug("Label chunk request (%d bytes) sent", ret);
/* read response */
if (zclient_read_sync_response(zclient, ZEBRA_GET_LABEL_CHUNK) != 0)
return -1;
+ /* parse response */
s = zclient->ibuf;
+
+ /* read proto and instance */
+ uint8_t proto = stream_getc(s);
+ uint16_t instance = stream_getw(s);
+
+ /* sanities */
+ if (proto != zclient->redist_default)
+ zlog_err("Wrong proto (%u) in get chunk response. Should be %u",
+ proto, zclient->redist_default);
+ if (instance != zclient->instance)
+ zlog_err("Wrong instId (%u) in get chunk response Should be %u",
+ instance, zclient->instance);
+
/* keep */
response_keep = stream_getc(s);
/* start and end labels */
/* not owning this response */
if (keep != response_keep) {
zlog_err(
- "%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
- __func__, *start, *end, keep, response_keep);
+ "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
+ *start, *end, keep, response_keep);
}
/* sanity */
if (*start > *end || *start < MPLS_LABEL_UNRESERVED_MIN
|| *end > MPLS_LABEL_UNRESERVED_MAX) {
- zlog_err("%s: Invalid Label chunk: %u - %u", __func__, *start,
- *end);
+ zlog_err("Invalid Label chunk: %u - %u", *start, *end);
return -1;
}
if (zclient_debug)
- zlog_debug("Label Chunk assign: %u - %u (%u) ", *start, *end,
+ zlog_debug("Label Chunk assign: %u - %u (%u)", *start, *end,
response_keep);
return 0;
struct stream *s;
if (zclient_debug)
- zlog_debug("Releasing Label Chunk");
+ zlog_debug("Releasing Label Chunk %u - %u", start, end);
if (zclient->sock < 0)
return -1;
stream_reset(s);
zclient_create_header(s, ZEBRA_RELEASE_LABEL_CHUNK, VRF_DEFAULT);
+ /* proto */
+ stream_putc(s, zclient->redist_default);
+ /* instance */
+ stream_putw(s, zclient->instance);
/* start */
stream_putl(s, start);
/* end */
ret = writen(zclient->sock, s->data, stream_get_endp(s));
if (ret < 0) {
- zlog_err("%s: can't write to zclient->sock", __func__);
+ zlog_err("Can't write to zclient sock");
close(zclient->sock);
zclient->sock = -1;
return -1;
}
if (ret == 0) {
- zlog_err("%s: zclient->sock connection closed", __func__);
+ zlog_err("Zclient sock connection closed");
close(zclient->sock);
zclient->sock = -1;
return -1;
return -1;
if (zclient_debug)
- zlog_debug("%s: Table manager connect request sent",
- __func__);
+ zlog_debug("%s: Table manager connect request sent", __func__);
/* read response */
if (zclient_read_sync_response(zclient, ZEBRA_TABLE_MANAGER_CONNECT)
case ZEBRA_GET_LABEL_CHUNK:
if (zclient->label_chunk)
(*zclient->label_chunk)(command, zclient, length,
- vrf_id);
+ vrf_id);
break;
case ZEBRA_IPSET_NOTIFY_OWNER:
if (zclient->ipset_notify_owner)
* it will be a proxy to relay messages to external label manager
* This zclient thus is to connect to it
*/
-static struct stream *ibuf;
static struct stream *obuf;
static struct zclient *zclient;
bool lm_is_external;
XFREE(MTYPE_LM_CHUNK, val);
}
-static int relay_response_back(struct zserv *zserv)
+static int relay_response_back(void)
{
int ret = 0;
struct stream *src, *dst;
uint8_t version;
vrf_id_t vrf_id;
uint16_t resp_cmd;
+ uint8_t proto;
+ const char *proto_str;
+ unsigned short instance;
+ struct zserv *zserv;
+ /* input buffer with msg from label manager */
src = zclient->ibuf;
- dst = obuf;
stream_reset(src);
+ /* parse header */
ret = zclient_read_header(src, zclient->sock, &size, &marker, &version,
&vrf_id, &resp_cmd);
if (ret < 0 && errno != EAGAIN) {
- zlog_err("%s: Error reading Label Manager response: %s",
- __func__, strerror(errno));
+ zlog_err("Error reading Label Manager response: %s",
+ strerror(errno));
return -1;
}
- zlog_debug("%s: Label Manager response received, %d bytes", __func__,
- size);
+ zlog_debug("Label Manager response received, %d bytes", size);
if (size == 0)
return -1;
- /* send response back */
+ /* Get the 'proto' field of the message */
+ proto = stream_getc(src);
+
+ /* Get the 'instance' field of the message */
+ instance = stream_getw(src);
+
+ proto_str = zebra_route_string(proto);
+
+ /* lookup the client to relay the msg to */
+ zserv = zebra_find_client(proto, instance);
+ if (!zserv) {
+ zlog_err(
+ "Error relaying LM response: can't find client %s, instance %u",
+ proto_str, instance);
+ return -1;
+ }
+ zlog_debug("Found client to relay LM response to client %s instance %u",
+ proto_str, instance);
+
+ /* copy msg into output buffer */
+ dst = obuf;
stream_copy(dst, src);
- ret = writen(zserv->sock, src->data, stream_get_endp(src));
+
+ /* send response back */
+ ret = writen(zserv->sock, dst->data, stream_get_endp(dst));
if (ret <= 0) {
- zlog_err("%s: Error sending Label Manager response back: %s",
- __func__, strerror(errno));
+ zlog_err("Error relaying LM response to %s instance %u: %s",
+ proto_str, instance, strerror(errno));
return -1;
}
- zlog_debug("%s: Label Manager response (%d bytes) sent back", __func__,
- ret);
+ zlog_debug("Relayed LM response (%d bytes) to %s instance %u", ret,
+ proto_str, instance);
return 0;
}
static int lm_zclient_read(struct thread *t)
{
- struct zserv *zserv;
int ret;
- /* Get socket to zebra. */
- zserv = THREAD_ARG(t);
zclient->t_read = NULL;
/* read response and send it back */
- ret = relay_response_back(zserv);
+ ret = relay_response_back();
return ret;
}
zclient_create_header(s, cmd, vrf_id);
+ /* proto */
+ stream_putc(s, zserv->proto);
+ /* instance */
+ stream_putw(s, zserv->instance);
/* result */
stream_putc(s, 1);
* @return 0 on success, -1 otherwise
*/
int zread_relay_label_manager_request(int cmd, struct zserv *zserv,
- vrf_id_t vrf_id)
+ struct stream *msg, vrf_id_t vrf_id)
{
- struct stream *src, *dst;
+ struct stream *dst;
int ret = 0;
+ uint8_t proto;
+ const char *proto_str;
+ unsigned short instance;
if (zclient->sock < 0) {
- zlog_err(
- "%s: Error relaying label chunk request: no zclient socket",
- __func__);
+ zlog_err("Unable to relay LM request: no socket");
reply_error(cmd, zserv, vrf_id);
return -1;
}
+ /* peek msg to get proto and instance id. This zebra, which acts as
+ * a proxy needs to have such values for each client in order to
+ * relay responses back to it.
+ */
+
+ /* Get the 'proto' field of incoming msg */
+ proto = stream_getc(msg);
+
+ /* Get the 'instance' field of incoming msg */
+ instance = stream_getw(msg);
+
+ /* stringify proto */
+ proto_str = zebra_route_string(proto);
+
+ /* check & set client proto if unset */
+ if (zserv->proto && zserv->proto != proto) {
+ zlog_warn("Client proto(%u) != msg proto(%u)", zserv->proto,
+ proto);
+ return -1;
+ }
+
+ /* check & set client instance if unset */
+ if (zserv->instance && zserv->instance != instance) {
+ zlog_err("Client instance(%u) != msg instance(%u)",
+ zserv->instance, instance);
+ return -1;
+ }
+
+ /* recall proto and instance */
+ zserv->instance = instance;
+ zserv->proto = proto;
+
/* in case there's any incoming message enqueued, read and forward it */
while (ret == 0)
- ret = relay_response_back(zserv);
+ ret = relay_response_back();
- /* Send request to external label manager */
- src = ibuf;
+ /* get the msg buffer used toward the 'master' Label Manager */
dst = zclient->obuf;
- stream_copy(dst, src);
+ /* copy the message */
+ stream_copy(dst, msg);
+ /* Send request to external label manager */
ret = writen(zclient->sock, dst->data, stream_get_endp(dst));
if (ret <= 0) {
- zlog_err("%s: Error relaying label chunk request: %s", __func__,
- strerror(errno));
+ zlog_err("Error relaying LM request from %s instance %u: %s",
+ proto_str, instance, strerror(errno));
reply_error(cmd, zserv, vrf_id);
return -1;
}
- zlog_debug("%s: Label chunk request relayed. %d bytes sent", __func__,
- ret);
+ zlog_debug("Relayed LM request (%d bytes) from %s instance %u", ret,
+ proto_str, instance);
+
/* Release label chunk has no response */
if (cmd == ZEBRA_RELEASE_LABEL_CHUNK)
/* make sure we listen to the response */
if (!zclient->t_read)
- thread_add_read(zclient->master, lm_zclient_read, zserv,
+ thread_add_read(zclient->master, lm_zclient_read, NULL,
zclient->sock, &zclient->t_read);
return 0;
{
/* this is an actual label manager */
if (!lm_zserv_path) {
- zlog_debug("Initializing own label manager");
+ zlog_debug("Initializing internal label manager");
lm_is_external = false;
lbl_mgr.lc_list = list_new();
lbl_mgr.lc_list->del = delete_label_chunk;
lm_zclient_init(lm_zserv_path);
}
- ibuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
obuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
hook_register(zapi_client_close, release_daemon_label_chunks);
void label_manager_close()
{
list_delete_and_null(&lbl_mgr.lc_list);
- stream_free(ibuf);
stream_free(obuf);
}
bool lm_is_external;
int zread_relay_label_manager_request(int cmd, struct zserv *zserv,
- vrf_id_t vrf_id);
+ struct stream *msg, vrf_id_t vrf_id);
void label_manager_init(char *lm_zserv_path);
struct label_manager_chunk *assign_label_chunk(uint8_t proto,
unsigned short instance,
zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, vrf_id);
if (lmc) {
+ /* proto */
+ stream_putc(s, lmc->proto);
+ /* instance */
+ stream_putw(s, lmc->instance);
/* keep */
stream_putc(s, lmc->keep);
/* start and end labels */
zclient_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id);
+ /* proto */
+ stream_putc(s, client->proto);
+
+ /* instance */
+ stream_putw(s, client->instance);
+
/* result */
stream_putc(s, result);
stream_failure:
return;
}
+static int msg_client_id_mismatch(const char *op, struct zserv *client,
+ uint8_t proto, unsigned int instance)
+{
+ if (proto != client->proto) {
+ zlog_err("%s: msg vs client proto mismatch, client=%u msg=%u",
+ op, client->proto, proto);
+ /* TODO: fail when BGP sets proto and instance */
+ /* return 1; */
+ }
+
+ if (instance != client->instance) {
+ zlog_err(
+ "%s: msg vs client instance mismatch, client=%u msg=%u",
+ op, client->instance, instance);
+ /* TODO: fail when BGP sets proto and instance */
+ /* return 1; */
+ }
+
+ return 0;
+}
static void zread_get_label_chunk(struct zserv *client, struct stream *msg,
vrf_id_t vrf_id)
uint8_t keep;
uint32_t size;
struct label_manager_chunk *lmc;
+ uint8_t proto;
+ unsigned short instance;
/* Get input stream. */
s = msg;
/* Get data. */
+ STREAM_GETC(s, proto);
+ STREAM_GETW(s, instance);
STREAM_GETC(s, keep);
STREAM_GETL(s, size);
+ /* detect client vs message (proto,instance) mismatch */
+ if (msg_client_id_mismatch("Get-label-chunk", client, proto, instance))
+ return;
+
lmc = assign_label_chunk(client->proto, client->instance, keep, size);
if (!lmc)
- zlog_err("%s: Unable to assign Label Chunk of size %u",
- __func__, size);
+ zlog_err(
+ "Unable to assign Label Chunk of size %u to %s instance %u",
+ size, zebra_route_string(client->proto),
+ client->instance);
else
- zlog_debug("Assigned Label Chunk %u - %u to %u", lmc->start,
- lmc->end, keep);
+ zlog_debug("Assigned Label Chunk %u - %u to %s instance %u",
+ lmc->start, lmc->end,
+ zebra_route_string(client->proto), client->instance);
/* send response back */
zsend_assign_label_chunk_response(client, vrf_id, lmc);
{
struct stream *s;
uint32_t start, end;
+ uint8_t proto;
+ unsigned short instance;
/* Get input stream. */
s = msg;
/* Get data. */
+ STREAM_GETC(s, proto);
+ STREAM_GETW(s, instance);
STREAM_GETL(s, start);
STREAM_GETL(s, end);
+ /* detect client vs message (proto,instance) mismatch */
+ if (msg_client_id_mismatch("Release-label-chunk", client, proto,
+ instance))
+ return;
+
release_label_chunk(client->proto, client->instance, start, end);
stream_failure:
/* external label manager */
if (lm_is_external)
- zread_relay_label_manager_request(hdr->command, client,
+ zread_relay_label_manager_request(hdr->command, client, msg,
zvrf_id(zvrf));
/* this is a label manager */
else {