Author: adrian.chadd
Date: Sun Feb 22 15:02:36 2009
New Revision: 13827
Modified:
playpen/LUSCA_HEAD_bgp/app/bgptest/bgptest.c
playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_core.c
playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_core.h
playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_packet.c
playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_packet.h
playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_rib.c
playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_rib.h
Log:
Tidy up the bgp_rib stuff so it ties (mostly!) into the packet processing
code just enough to add/delete some prefixes for debugging; but it doesn't
yet do things "right" enough to include AS path info.
Don't bother trying to use radix (or try to use splay!) for now for the FIB
code; debug and pick an implementation later.
Modified: playpen/LUSCA_HEAD_bgp/app/bgptest/bgptest.c
==============================================================================
--- playpen/LUSCA_HEAD_bgp/app/bgptest/bgptest.c (original)
+++ playpen/LUSCA_HEAD_bgp/app/bgptest/bgptest.c Sun Feb 22 15:02:36 2009
@@ -15,6 +15,7 @@
#include "include/Array.h"
#include "include/Stack.h"
#include "include/util.h"
+#include "include/radix.h"
#include "libcore/valgrind.h"
#include "libcore/varargs.h"
#include "libcore/debug.h"
@@ -39,9 +40,9 @@
#include "libiapp/signals.h"
#include "libiapp/mainloop.h"
-#include "libsqbgp/bgp_core.h"
#include "libsqbgp/bgp_packet.h"
#include "libsqbgp/bgp_rib.h"
+#include "libsqbgp/bgp_core.h"
sqaddr_t dest;
@@ -65,8 +66,8 @@
_db_init("ALL,1 85,99");
_db_set_stderr_debug(99);
-
+ squid_rn_init();
bzero(&sa, sizeof(sa));
inet_aton("216.12.163.51", &sa.sin_addr);
@@ -85,7 +86,7 @@
fd = socket(AF_INET, SOCK_STREAM, 0);
assert(fd != -1);
r = connect(fd, (struct sockaddr *) &sa, sizeof(sa));
- r = bgp_send_hello(fd, 65535, 120, bgp_id);
+ r = bgp_send_hello(&bi, fd, 65535, 120, bgp_id);
if (r > 0)
while (r > 0)
r = bgp_read(&bi, fd);
Modified: playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_core.c
==============================================================================
--- playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_core.c (original)
+++ playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_core.c Sun Feb 22 15:02:36 2009
@@ -16,8 +16,8 @@
#include "../libsqinet/sqinet.h"
-#include "bgp_packet.h"
#include "bgp_rib.h"
+#include "bgp_packet.h"
#include "bgp_core.h"
void
@@ -34,7 +34,7 @@
/* Free RIB entries and RIB tree */
/* Ensure AS path entries are gone, complain for leftovers! */
/* Free AS path hash */
- bgp_rib_destroy(bi->rn);
+ bgp_rib_destroy(&bi->rn);
bzero(bi, sizeof(*bi));
}
@@ -96,7 +96,7 @@
debug(85, 1) ("main: incomplete packet\n");
break;
}
- r = bgp_decode_message(fd, bi->recv.buf + i, bi->recv.bufofs -
i);
+ r = bgp_decode_message(bi, fd, bi->recv.buf + i,
bi->recv.bufofs - i);
assert(r > 0);
i += r;
debug(85, 1) ("main: pkt was %d bytes, i is now %d\n", r, i);
@@ -116,7 +116,7 @@
{
bi->state = BGP_IDLE;
/* free prefixes */
- bgp_rib_clean(bi->rn);
+ bgp_rib_clean(&bi->rn);
/* ensure no as path entries exist in the hash! */
}
Modified: playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_core.h
==============================================================================
--- playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_core.h (original)
+++ playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_core.h Sun Feb 22 15:02:36 2009
@@ -37,7 +37,7 @@
/* The AS path table */
/* The RIB */
- struct squid_radix_node_head *rn;
+ bgp_rib_head_t rn;
};
typedef struct _bgp_instance bgp_instance_t;
Modified: playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_packet.c
==============================================================================
--- playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_packet.c (original)
+++ playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_packet.c Sun Feb 22 15:02:36 2009
@@ -72,7 +72,7 @@
* XXX absolutely hacky!
*/
int
-bgp_send_hello(int fd, unsigned short asnum, short hold_time, struct
in_addr bgp_id)
+bgp_send_hello(bgp_instance_t *bi, int fd, unsigned short asnum, short
hold_time, struct in_addr bgp_id)
{
char send_buf[128];
char *p = send_buf;
@@ -127,7 +127,7 @@
}
int
-bgp_send_keepalive(int fd)
+bgp_send_keepalive(bgp_instance_t *bi, int fd)
{
char send_buf[128];
char *p = send_buf;
@@ -159,7 +159,7 @@
}
int
-bgp_handle_notification(int fd, const char *buf, int len)
+bgp_handle_notification(bgp_instance_t *bi, int fd, const char *buf, int
len)
{
u_int8_t err_code;
u_int8_t err_subcode;
@@ -173,7 +173,7 @@
}
int
-bgp_handle_open(int fd, const char *buf, int len)
+bgp_handle_open(bgp_instance_t *bi, int fd, const char *buf, int len)
{
u_int8_t version;
u_int16_t bgp_as;
@@ -194,13 +194,13 @@
debug(85, 2) ("bgp_handle_open: got version %d, AS %d, timer %d,
parm_len %d\n", version, bgp_as, hold_timer, parm_len);
/* Queue a keepalive message */
- bgp_send_keepalive(fd);
+ bgp_send_keepalive(bi, fd);
return 1;
}
int
-bgp_handle_update_withdraw(const char *buf, int len)
+bgp_handle_update_withdraw(bgp_instance_t *bi, const char *buf, int len)
{
struct in_addr pf;
u_int8_t pl, netmask;
@@ -223,6 +223,7 @@
/* XXX bounds check? */
memcpy(&pf, buf + i, pl);
debug(85, 2) (" bgp_handle_update_withdraw: prefix %s/%d\n",
inet_ntoa(pf), netmask);
+ bgp_rib_del_net(&bi->rn, pf, netmask);
i += pl;
}
return 1;
@@ -309,7 +310,7 @@
}
int
-bgp_handle_update_nlri(const char *buf, int len)
+bgp_handle_update_nlri(bgp_instance_t *bi, const char *buf, int len)
{
struct in_addr pf;
u_int8_t pl, netmask;
@@ -332,13 +333,14 @@
/* XXX bounds check? */
memcpy(&pf, buf + i, pl);
debug(85, 2) (" bgp_handle_update_nlri: prefix %s/%d\n",
inet_ntoa(pf),
netmask);
+ bgp_rib_add_net(&bi->rn, pf, netmask);
i += pl;
}
return 1;
}
int
-bgp_handle_update(int fd, const char *buf, int len)
+bgp_handle_update(bgp_instance_t *bi, int fd, const char *buf, int len)
{
u_int16_t withdraw_route_len;
u_int16_t path_attrib_len;
@@ -349,12 +351,12 @@
debug(85, 2) ("bgp_handle_update: UPDATE pktlen %d:
withdraw_route_len %d; path attrib len %d\n",
len, withdraw_route_len, path_attrib_len);
- if (! bgp_handle_update_withdraw(buf + 2, withdraw_route_len))
+ if (! bgp_handle_update_withdraw(bi, buf + 2, withdraw_route_len))
return 0;
if (! bgp_handle_update_pathattrib(buf + 2 + withdraw_route_len + 2,
path_attrib_len))
return 0;
/* XXX need to calculate the length and offset correctly! */
- if (! bgp_handle_update_nlri(buf + 2 + withdraw_route_len + 2 +
path_attrib_len, len - (2 + withdraw_route_len + 2 + path_attrib_len)))
+ if (! bgp_handle_update_nlri(bi, buf + 2 + withdraw_route_len + 2 +
path_attrib_len, len - (2 + withdraw_route_len + 2 + path_attrib_len)))
return 0;
return 1;
@@ -368,7 +370,7 @@
}
int
-bgp_decode_message(int fd, const const char *buf, int len)
+bgp_decode_message(bgp_instance_t *bi, int fd, const const char *buf, int
len)
{
int r;
@@ -382,13 +384,13 @@
debug(85, 2) ("bgp_decode_message: type %d; len %d\n", type, pkt_len);
switch (type) {
case 1: /* OPEN */
- r = bgp_handle_open(fd, buf + 19, pkt_len - 19);
+ r = bgp_handle_open(bi, fd, buf + 19, pkt_len - 19);
break;
case 2: /* UPDATE */
- r = bgp_handle_update(fd, buf + 19, pkt_len - 19);
+ r = bgp_handle_update(bi, fd, buf + 19, pkt_len - 19);
break;
case 3: /* NOTIFICATION */
- r = bgp_handle_notification(fd, buf + 19, pkt_len - 19);
+ r = bgp_handle_notification(bi, fd, buf + 19, pkt_len -
19);
break;
case 4: /* KEEPALIVE */
r = bgp_handle_keepalive(fd, buf + 19, pkt_len - 19);
Modified: playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_packet.h
==============================================================================
--- playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_packet.h (original)
+++ playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_packet.h Sun Feb 22 15:02:36 2009
@@ -6,9 +6,12 @@
extern int bgp_msg_isvalid(const char *buf, int len);
extern int bgp_msg_complete(const char *buf, int len);
-extern int bgp_send_keepalive(int fd);
-extern int bgp_send_hello(int fd, unsigned short asnum, short hold_time,
struct in_addr bgp_id);
+/* XXX eww? */
+struct _bgp_instance;
-extern int bgp_decode_message(int fd, const const char *buf, int len);
+extern int bgp_send_keepalive(struct _bgp_instance *bi, int fd);
+extern int bgp_send_hello(struct _bgp_instance *bi, int fd, unsigned short
asnum, short hold_time, struct in_addr bgp_id);
+
+extern int bgp_decode_message(struct _bgp_instance *bi, int fd, const
const char *buf, int len);
#endif
Modified: playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_rib.c
==============================================================================
--- playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_rib.c (original)
+++ playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_rib.c Sun Feb 22 15:02:36 2009
@@ -10,7 +10,6 @@
#include <arpa/inet.h>
#include "../include/util.h"
-#include "../include/radix.h"
#include "../libcore/tools.h"
#include "../libmem/intlist.h"
#include "../libsqdebug/debug.h"
@@ -22,67 +21,9 @@
#include "bgp_rib.h"
#include "bgp_core.h"
-/* BEGIN of definitions for radix tree entries */
-
-/* int in memory with length */
-typedef u_char m_int[1 + sizeof(unsigned int)];
-#define store_m_int(i, m) \
- (i = htonl(i), m[0] = sizeof(m_int), xmemcpy(m+1, &i, sizeof(unsigned
int)))
-#define get_m_int(i, m) \
- (xmemcpy(&i, m+1, sizeof(unsigned int)), ntohl(i))
-
-/* END of definitions for radix tree entries */
-
-/*
- * Structure for as number information. it could be simply
- * an intlist but it's coded as a structure for future
- * enhancements (e.g. expires)
- */
-struct _as_info {
-};
-
-typedef struct _as_info as_info;
-
-/* entry into the radix tree */
-struct _rtentry {
- struct squid_radix_node e_nodes[2];
- as_info *e_info;
- m_int e_addr;
- m_int e_mask;
-};
-
-typedef struct _rtentry rtentry;
-
-static int destroyRadixNode(struct squid_radix_node *rn, void *w);
-static void destroyRadixNodeInfo(as_info *);
-
-/* PUBLIC */
-
int
-bgp_rib_match_ip(struct squid_radix_node_head *head, void *data, struct
in_addr addr)
+bgp_rib_match_net(bgp_rib_head_t *head, struct in_addr addr, int masklen)
{
- unsigned long lh;
- struct squid_radix_node *rn;
- as_info *e;
- m_int m_addr;
- lh = ntohl(addr.s_addr);
- debug(53, 3) ("asnMatchIp: Called for %s.\n", inet_ntoa(addr));
-
- if (head == NULL)
- return 0;
- if (IsNoAddr(&addr))
- return 0;
- if (IsAnyAddr(&addr))
- return 0;
- store_m_int(lh, m_addr);
- rn = squid_rn_match(m_addr, head);
- if (rn == NULL) {
- debug(53, 3) ("asnMatchIp: Address not in as db.\n");
- return 0;
- }
- debug(53, 3) ("asnMatchIp: Found in db!\n");
- e = ((rtentry *) rn)->e_info;
- assert(e);
return 1;
}
@@ -91,142 +32,29 @@
extern int squid_max_keylen; /* yuck.. this is in lib/radix.c */
void
-bgp_rib_init(struct squid_radix_node_head **head)
+bgp_rib_init(bgp_rib_head_t *head)
{
- squid_max_keylen = 40;
- squid_rn_inithead((void **) head, 8);
}
void
-bgp_rib_destroy(struct squid_radix_node_head *head)
+bgp_rib_destroy(bgp_rib_head_t *head)
{
- squid_rn_walktree(head, destroyRadixNode, head);
- destroyRadixNode((struct squid_radix_node *) 0, (void *) head);
}
void
-bgp_rib_clean(struct squid_radix_node_head *head)
+bgp_rib_clean(bgp_rib_head_t *head)
{
- squid_rn_walktree(head, destroyRadixNode, head);
}
-/* PRIVATE */
-
-/* add a network (addr, mask) to the radix tree, with matching AS
- * number */
int
-bgp_rib_add_net(struct squid_radix_node_head *head, char *as_string, int
as_number)
+bgp_rib_add_net(bgp_rib_head_t *head, struct in_addr addr, int masklen)
{
- rtentry *e;
- struct squid_radix_node *rn;
- char dbg1[32], dbg2[32];
- as_info *asinfo = NULL;
- struct in_addr in_a, in_m;
- long mask, addr;
- char *t;
- int bitl;
-
- t = strchr(as_string, '/');
- if (t == NULL) {
- debug(53, 3) ("asnAddNet: failed, invalid response from whois
server.\n");
- return 0;
- }
- *t = '\0';
- addr = inet_addr(as_string);
- bitl = atoi(t + 1);
- if (bitl < 0)
- bitl = 0;
- if (bitl > 32)
- bitl = 32;
- mask = bitl ? 0xfffffffful << (32 - bitl) : 0;
-
- in_a.s_addr = addr;
- in_m.s_addr = mask;
- xstrncpy(dbg1, inet_ntoa(in_a), 32);
- xstrncpy(dbg2, inet_ntoa(in_m), 32);
- addr = ntohl(addr);
- /*mask = ntohl(mask); */
- debug(53, 3) ("asnAddNet: called for %s/%s\n", dbg1, dbg2);
- e = xmalloc(sizeof(rtentry));
- memset(e, '\0', sizeof(rtentry));
- store_m_int(addr, e->e_addr);
- store_m_int(mask, e->e_mask);
- rn = squid_rn_lookup(e->e_addr, e->e_mask, head);
- if (rn == NULL) {
- asinfo = xmalloc(sizeof(asinfo));
- rn = squid_rn_addroute(e->e_addr, e->e_mask, head, e->e_nodes);
- rn = squid_rn_match(e->e_addr, head);
- assert(rn != NULL);
- e->e_info = asinfo;
- if (rn == 0) { /* assert might expand to nothing */
- xfree(asinfo);
- xfree(e);
- debug(53, 3) ("asnAddNet: Could not add entry.\n");
- return 0;
- }
- } else {
- debug(85, 1) ("bgp_rib_add_net: duplicate network!?\n");
- }
- e->e_info = asinfo;
return 1;
}
-static int
-destroyRadixNode(struct squid_radix_node *rn, void *w)
+int
+bgp_rib_del_net(bgp_rib_head_t *head, struct in_addr addr, int masklen)
{
- struct squid_radix_node_head *rnh = (struct squid_radix_node_head *) w;
-
- if (rn && !(rn->rn_flags & RNF_ROOT)) {
- rtentry *e = (rtentry *) rn;
- rn = squid_rn_delete(rn->rn_key, rn->rn_mask, rnh);
- if (rn == 0)
- debug(53, 3) ("destroyRadixNode: internal screwup\n");
- destroyRadixNodeInfo(e->e_info);
- xfree(rn);
- }
return 1;
}
-
-static void
-destroyRadixNodeInfo(as_info * e_info)
-{
-}
-
-static int
-mask_len(u_long mask)
-{
- int len = 32;
- if (mask == 0)
- return 0;
- while ((mask & 1) == 0) {
- len--;
- mask >>= 1;
- }
- return len;
-}
-
-#if 0
-static int
-printRadixNode(struct squid_radix_node *rn, void *w)
-{
- StoreEntry *sentry = w;
- rtentry *e = (rtentry *) rn;
- intlist *q;
- as_info *asinfo;
- struct in_addr addr;
- struct in_addr mask;
- assert(e);
- assert(e->e_info);
- (void) get_m_int(addr.s_addr, e->e_addr);
- (void) get_m_int(mask.s_addr, e->e_mask);
- storeAppendPrintf(sentry, "%15s/%d\t",
- inet_ntoa(addr), mask_len(ntohl(mask.s_addr)));
- asinfo = e->e_info;
- assert(asinfo->as_number);
- for (q = asinfo->as_number; q; q = q->next)
- storeAppendPrintf(sentry, " %d", q->i);
- storeAppendPrintf(sentry, "\n");
- return 0;
-}
-#endif
Modified: playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_rib.h
==============================================================================
--- playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_rib.h (original)
+++ playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_rib.h Sun Feb 22 15:02:36 2009
@@ -1,10 +1,16 @@
#ifndef __LIBSQBGP_BGP_RIB_H__
#define __LIBSQBGP_BGP_RIB_H__
-extern int bgp_rib_match_ip(struct squid_radix_node_head *head, void
*data, struct in_addr addr);
-extern void bgp_rib_init(struct squid_radix_node_head **head);
-extern void bgp_rib_destroy(struct squid_radix_node_head *head);
-extern void bgp_rib_clean(struct squid_radix_node_head *head);
-extern int bgp_rib_add_net(struct squid_radix_node_head *head, char
*as_string, int as_number);
+struct _bgp_rib_head {
+
+};
+typedef struct _bgp_rib_head bgp_rib_head_t;
+
+extern int bgp_rib_match_net(bgp_rib_head_t *head, struct in_addr addr,
int masklen);
+extern int bgp_rib_add_net(bgp_rib_head_t *head, struct in_addr addr, int
masklen);
+extern int bgp_rib_del_net(bgp_rib_head_t *head, struct in_addr addr, int
masklen);
+extern void bgp_rib_init(bgp_rib_head_t *head);
+extern void bgp_rib_destroy(bgp_rib_head_t *head);
+extern void bgp_rib_clean(bgp_rib_head_t *head);
#endif
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"lusca-commit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/lusca-commit?hl=en
-~----------~----~----~----~------~----~------~--~---