From f9d08002d1c5969d7f48be92185660fd17511a05 Mon Sep 17 00:00:00 2001
From: "Alexander V. Chernikov" <melifaro@edge.ipfw.ru>
Date: Mon, 9 Feb 2015 00:17:32 +0300
Subject: [PATCH 4/8] Store protocol instance size inside protocol structure.
 Make proto_config_new() use this info instead of supplied size.

---
 nest/config.Y         | 2 +-
 nest/proto.c          | 5 ++---
 nest/protocol.h       | 3 ++-
 nest/rt-dev.c         | 1 +
 proto/bfd/bfd.c       | 1 +
 proto/bfd/config.Y    | 2 +-
 proto/bgp/bgp.c       | 1 +
 proto/bgp/config.Y    | 2 +-
 proto/ospf/config.Y   | 2 +-
 proto/ospf/ospf.c     | 1 +
 proto/pipe/config.Y   | 2 +-
 proto/pipe/pipe.c     | 1 +
 proto/radv/config.Y   | 2 +-
 proto/radv/radv.c     | 1 +
 proto/rip/config.Y    | 2 +-
 proto/rip/rip.c       | 1 +
 proto/static/config.Y | 2 +-
 proto/static/static.c | 1 +
 sysdep/unix/krt.c     | 6 ++++--
 19 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/nest/config.Y b/nest/config.Y
index 59a776b..8b69729 100644
--- a/nest/config.Y
+++ b/nest/config.Y
@@ -307,7 +307,7 @@ tos:
 CF_ADDTO(proto, dev_proto '}')
 
 dev_proto_start: proto_start DIRECT {
-     this_proto = proto_config_new(&proto_device, sizeof(struct rt_dev_config), $1);
+     this_proto = proto_config_new(&proto_device, $1);
      init_list(&DIRECT_CFG->iface_list);
    }
  ;
diff --git a/nest/proto.c b/nest/proto.c
index 7a1e9bf..1166573 100644
--- a/nest/proto.c
+++ b/nest/proto.c
@@ -245,7 +245,6 @@ proto_free_ahooks(struct proto *p)
 /**
  * proto_config_new - create a new protocol configuration
  * @pr: protocol the configuration will belong to
- * @size: size of the structure including generic data
  * @class: SYM_PROTO or SYM_TEMPLATE
  *
  * Whenever the configuration file says that a new instance
@@ -262,9 +261,9 @@ proto_free_ahooks(struct proto *p)
  * initialized during protos_commit()).
  */
 void *
-proto_config_new(struct protocol *pr, unsigned size, int class)
+proto_config_new(struct protocol *pr, int class)
 {
-  struct proto_config *c = cfg_allocz(size);
+  struct proto_config *c = cfg_allocz(pr->proto_size);
 
   if (class == SYM_PROTO)
     add_tail(&new_config->protos, &c->n);
diff --git a/nest/protocol.h b/nest/protocol.h
index eb43418..233ffa6 100644
--- a/nest/protocol.h
+++ b/nest/protocol.h
@@ -41,6 +41,7 @@ struct protocol {
   int attr_class;			/* Attribute class known to this protocol */
   int multitable;			/* Protocol handles all announce hooks itself */
   unsigned preference;			/* Default protocol preference */
+  size_t proto_size;			/* size of protocol instance */
 
   void (*preconfig)(struct protocol *, struct config *);	/* Just before configuring */
   void (*postconfig)(struct proto_config *);			/* After configuring each instance */
@@ -239,7 +240,7 @@ struct proto_spec {
 
 
 void *proto_new(struct proto_config *, unsigned size);
-void *proto_config_new(struct protocol *, unsigned size, int class);
+void *proto_config_new(struct protocol *, int class);
 void proto_copy_config(struct proto_config *dest, struct proto_config *src);
 void proto_request_feeding(struct proto *p);
 
diff --git a/nest/rt-dev.c b/nest/rt-dev.c
index 1a859da..5d22396 100644
--- a/nest/rt-dev.c
+++ b/nest/rt-dev.c
@@ -115,6 +115,7 @@ struct protocol proto_device = {
   name:		"Direct",
   template:	"direct%d",
   preference:	DEF_PREF_DIRECT,
+  proto_size:	sizeof(struct rt_dev_config),
   init:		dev_init,
   reconfigure:	dev_reconfigure,
   copy_config:	dev_copy_config
diff --git a/proto/bfd/bfd.c b/proto/bfd/bfd.c
index 23e04e4..bdd27b4 100644
--- a/proto/bfd/bfd.c
+++ b/proto/bfd/bfd.c
@@ -1112,6 +1112,7 @@ bfd_show_sessions(struct proto *P)
 struct protocol proto_bfd = {
   .name =		"BFD",
   .template =		"bfd%d",
+  .proto_size=		sizeof(struct bfd_config),
   .init =		bfd_init,
   .start =		bfd_start,
   .shutdown =		bfd_shutdown,
diff --git a/proto/bfd/config.Y b/proto/bfd/config.Y
index 1b07495..4affb92 100644
--- a/proto/bfd/config.Y
+++ b/proto/bfd/config.Y
@@ -34,7 +34,7 @@ CF_ADDTO(proto, bfd_proto)
 
 bfd_proto_start: proto_start BFD
 {
-  this_proto = proto_config_new(&proto_bfd, sizeof(struct bfd_config), $1);
+  this_proto = proto_config_new(&proto_bfd, $1);
   init_list(&BFD_CFG->patt_list);
   init_list(&BFD_CFG->neigh_list);
 
diff --git a/proto/bgp/bgp.c b/proto/bgp/bgp.c
index 99ec1ce..0fd2b68 100644
--- a/proto/bgp/bgp.c
+++ b/proto/bgp/bgp.c
@@ -1430,6 +1430,7 @@ struct protocol proto_bgp = {
   template:		"bgp%d",
   attr_class:		EAP_BGP,
   preference:		DEF_PREF_BGP,
+  proto_size:		sizeof(struct bgp_config),
   init:			bgp_init,
   start:		bgp_start,
   shutdown:		bgp_shutdown,
diff --git a/proto/bgp/config.Y b/proto/bgp/config.Y
index 8e0b241..d0ae064 100644
--- a/proto/bgp/config.Y
+++ b/proto/bgp/config.Y
@@ -33,7 +33,7 @@ CF_GRAMMAR
 CF_ADDTO(proto, bgp_proto '}' { bgp_check_config(BGP_CFG); } )
 
 bgp_proto_start: proto_start BGP {
-     this_proto = proto_config_new(&proto_bgp, sizeof(struct bgp_config), $1);
+     this_proto = proto_config_new(&proto_bgp, $1);
      BGP_CFG->multihop = -1;	/* undefined */
      BGP_CFG->hold_time = 240;
      BGP_CFG->connect_retry_time = 120;
diff --git a/proto/ospf/config.Y b/proto/ospf/config.Y
index 478529b..58cc52d 100644
--- a/proto/ospf/config.Y
+++ b/proto/ospf/config.Y
@@ -143,7 +143,7 @@ CF_GRAMMAR
 CF_ADDTO(proto, ospf_proto '}' { ospf_proto_finish(); } )
 
 ospf_proto_start: proto_start OSPF {
-     this_proto = proto_config_new(&proto_ospf, sizeof(struct ospf_config), $1);
+     this_proto = proto_config_new(&proto_ospf, $1);
      init_list(&OSPF_CFG->area_list);
      init_list(&OSPF_CFG->vlink_list);
      OSPF_CFG->rfc1583 = DEFAULT_RFC1583;
diff --git a/proto/ospf/ospf.c b/proto/ospf/ospf.c
index 6756ff4..3f5a6a4 100644
--- a/proto/ospf/ospf.c
+++ b/proto/ospf/ospf.c
@@ -1590,6 +1590,7 @@ struct protocol proto_ospf = {
   template:		"ospf%d",
   attr_class:		EAP_OSPF,
   preference:		DEF_PREF_OSPF,
+  proto_size:		sizeof(struct ospf_config),
   init:			ospf_init,
   dump:			ospf_dump,
   start:		ospf_start,
diff --git a/proto/pipe/config.Y b/proto/pipe/config.Y
index 4063755..8daf2e7 100644
--- a/proto/pipe/config.Y
+++ b/proto/pipe/config.Y
@@ -23,7 +23,7 @@ CF_GRAMMAR
 CF_ADDTO(proto, pipe_proto '}')
 
 pipe_proto_start: proto_start PIPE {
-     this_proto = proto_config_new(&proto_pipe, sizeof(struct pipe_config), $1);
+     this_proto = proto_config_new(&proto_pipe, $1);
      PIPE_CFG->mode = PIPE_TRANSPARENT;
   }
  ;
diff --git a/proto/pipe/pipe.c b/proto/pipe/pipe.c
index 22381c3..fc9f948 100644
--- a/proto/pipe/pipe.c
+++ b/proto/pipe/pipe.c
@@ -340,6 +340,7 @@ struct protocol proto_pipe = {
   template:		"pipe%d",
   multitable:		1,
   preference:		DEF_PREF_PIPE,
+  proto_size:		sizeof(struct pipe_config),
   postconfig:		pipe_postconfig,
   init:			pipe_init,
   start:		pipe_start,
diff --git a/proto/radv/config.Y b/proto/radv/config.Y
index a26ea88..da30066 100644
--- a/proto/radv/config.Y
+++ b/proto/radv/config.Y
@@ -40,7 +40,7 @@ CF_ADDTO(proto, radv_proto)
 
 radv_proto_start: proto_start RADV
 {
-  this_proto = proto_config_new(&proto_radv, sizeof(struct radv_config), $1);
+  this_proto = proto_config_new(&proto_radv, $1);
   init_list(&RADV_CFG->patt_list);
   init_list(&RADV_CFG->pref_list);
   init_list(&RADV_CFG->rdnss_list);
diff --git a/proto/radv/radv.c b/proto/radv/radv.c
index 6be7cd8..eb2a693 100644
--- a/proto/radv/radv.c
+++ b/proto/radv/radv.c
@@ -426,6 +426,7 @@ radv_get_status(struct proto *p, byte *buf)
 struct protocol proto_radv = {
   .name =		"RAdv",
   .template =		"radv%d",
+  .proto_size=		sizeof(struct radv_config),
   .init =		radv_init,
   .start =		radv_start,
   .shutdown =		radv_shutdown,
diff --git a/proto/rip/config.Y b/proto/rip/config.Y
index 7639752..b2b9909 100644
--- a/proto/rip/config.Y
+++ b/proto/rip/config.Y
@@ -43,7 +43,7 @@ CF_GRAMMAR
 CF_ADDTO(proto, rip_cfg '}' { RIP_CFG->passwords = get_passwords(); } )
 
 rip_cfg_start: proto_start RIP {
-     this_proto = proto_config_new(&proto_rip, sizeof(struct rip_proto_config), $1);
+     this_proto = proto_config_new(&proto_rip, $1);
      rip_init_config(RIP_CFG);
    }
  ;
diff --git a/proto/rip/rip.c b/proto/rip/rip.c
index bc9ffc5..85f1d93 100644
--- a/proto/rip/rip.c
+++ b/proto/rip/rip.c
@@ -1050,6 +1050,7 @@ struct protocol proto_rip = {
   template: "rip%d",
   attr_class: EAP_RIP,
   preference: DEF_PREF_RIP,
+  proto_size: sizeof(struct rip_proto_config),
   get_route_info: rip_get_route_info,
   get_attr: rip_get_attr,
 
diff --git a/proto/static/config.Y b/proto/static/config.Y
index 2d9d4b4..a8bfa36 100644
--- a/proto/static/config.Y
+++ b/proto/static/config.Y
@@ -26,7 +26,7 @@ CF_GRAMMAR
 CF_ADDTO(proto, static_proto '}')
 
 static_proto_start: proto_start STATIC {
-     this_proto = proto_config_new(&proto_static, sizeof(struct static_config), $1);
+     this_proto = proto_config_new(&proto_static, $1);
      static_init_config((struct static_config *) this_proto);
   }
  ;
diff --git a/proto/static/static.c b/proto/static/static.c
index d3a595d..7d571b3 100644
--- a/proto/static/static.c
+++ b/proto/static/static.c
@@ -531,6 +531,7 @@ struct protocol proto_static = {
   name:		"Static",
   template:	"static%d",
   preference:	DEF_PREF_STATIC,
+  proto_size:	sizeof(struct static_config),
   init:		static_init,
   dump:		static_dump,
   start:	static_start,
diff --git a/sysdep/unix/krt.c b/sysdep/unix/krt.c
index a2fb83d..6aa264f 100644
--- a/sysdep/unix/krt.c
+++ b/sysdep/unix/krt.c
@@ -251,7 +251,7 @@ kif_init_config(int class)
   if (kif_cf)
     cf_error("Kernel device protocol already defined");
 
-  kif_cf = (struct kif_config *) proto_config_new(&proto_unix_iface, sizeof(struct kif_config), class);
+  kif_cf = (struct kif_config *) proto_config_new(&proto_unix_iface, class);
   kif_cf->scan_time = 60;
   init_list(&kif_cf->primary);
 
@@ -280,6 +280,7 @@ struct protocol proto_unix_iface = {
   name:		"Device",
   template:	"device%d",
   preference:	DEF_PREF_DIRECT,
+  proto_size:	sizeof(struct kif_config),
   preconfig:	kif_preconfig,
   init:		kif_init,
   start:	kif_start,
@@ -1150,7 +1151,7 @@ krt_init_config(int class)
     cf_error("Kernel protocol already defined");
 #endif
 
-  krt_cf = (struct krt_config *) proto_config_new(&proto_unix_kernel, sizeof(struct krt_config), class);
+  krt_cf = (struct krt_config *) proto_config_new(&proto_unix_kernel, class);
   krt_cf->scan_time = 60;
 
   krt_sys_init_config(krt_cf);
@@ -1202,6 +1203,7 @@ struct protocol proto_unix_kernel = {
   template:	"kernel%d",
   attr_class:	EAP_KRT,
   preference:	DEF_PREF_INHERITED,
+  proto_size:	sizeof(struct krt_config),
   preconfig:	krt_preconfig,
   postconfig:	krt_postconfig,
   init:		krt_init,
-- 
2.1.2

