netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Casting (struct rtable*) to (struct dst_entry*)
@ 2003-03-20  4:55 N N Ashok
  2003-03-20  4:59 ` David S. Miller
  0 siblings, 1 reply; 5+ messages in thread
From: N N Ashok @ 2003-03-20  4:55 UTC (permalink / raw)
  To: netdev, linux-net

Hi,
   I have been looking at the networking code of Linux for my Masters thesis. 
I observed the following:
In ip_route_input(), if a route is found in the cache, the skb->dst is setup 
with the route found by casting the rtable entry to dst_entry:
	skb->dst = (struct dst_entry*)rth;

Later in ip_route_input(), skb->dst->input() is called:
	return skb->dst->input(skb);

In ip_forward(), skb->dst is again casted to rtable:
	rt = (struct rtable*)skb->dst;

I am unable to understand how a rtable structure casted to dst_entry will give 
a correct pointer to the input() function. I looked at the fields in rtable 
and dst_entry, the fields in the structures are cannot be lined up (the 
fourth field in rtable is not the same type as the fourth field in 
dst_entry). 

Can anybody help me understand this casting of rtable to dst_entry and then 
back to rtable?

Thanks,
Ashok

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Casting (struct rtable*) to (struct dst_entry*)
  2003-03-20  4:55 Casting (struct rtable*) to (struct dst_entry*) N N Ashok
@ 2003-03-20  4:59 ` David S. Miller
  2003-03-20  5:27   ` N N Ashok
  0 siblings, 1 reply; 5+ messages in thread
From: David S. Miller @ 2003-03-20  4:59 UTC (permalink / raw)
  To: nalkunda; +Cc: netdev, linux-net

   From: N N Ashok <nalkunda@cse.msu.edu>
   Date: Wed, 19 Mar 2003 23:55:02 -0500
   
   I am unable to understand how a rtable structure casted to dst_entry will give 
   a correct pointer to the input() function. I looked at the fields in rtable 
   and dst_entry, the fields in the structures are cannot be lined up (the 
   fourth field in rtable is not the same type as the fourth field in 
   dst_entry). 

"struct rtable" starts with a "struct dst_entry"

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Casting (struct rtable*) to (struct dst_entry*)
  2003-03-20  4:59 ` David S. Miller
@ 2003-03-20  5:27   ` N N Ashok
  2003-03-20  5:33     ` David S. Miller
  0 siblings, 1 reply; 5+ messages in thread
From: N N Ashok @ 2003-03-20  5:27 UTC (permalink / raw)
  To: David S. Miller; +Cc: netdev, linux-net

On Wednesday 19 March 2003 23:59, David S. Miller wrote:
>    From: N N Ashok <nalkunda@cse.msu.edu>
>    Date: Wed, 19 Mar 2003 23:55:02 -0500
>
>    I am unable to understand how a rtable structure casted to dst_entry
> will give a correct pointer to the input() function. I looked at the fields
> in rtable and dst_entry, the fields in the structures are cannot be lined
> up (the fourth field in rtable is not the same type as the fourth field in
> dst_entry).
>
> "struct rtable" starts with a "struct dst_entry"

Thanks David.

I did see that. But however, I could not understand how "struct rtable" can be 
casted to "struct dst_entry" and then back again, all the while accessing 
fields of both structures. When the (struct rtable *)rth is filled in 
ip_route_input(), the variables accessed are those of rtable. Then rth is 
cast to (struct dst_entry *) and assigned to skb->dst (which is of type 
struct dst_entry *). 
After this, in ip_rcv_finish(), the field of dst_entry is accessed as in:
skb->dst->input(). I am unable to understand how, data filled in as rtable 
fields will be valid when accessed as dst_entry fields.
Later in ip_forward() (for a packet to be forwarded), the skb->dst is cast to 
(struct rtable *) and its fields accessed.

A correction to the previous post: the skb->dst->input() is invoked in 
ip_rcv_finish() and not in ip_route_input() as mentioned in the post.

Thanks,
Ashok

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Casting (struct rtable*) to (struct dst_entry*)
  2003-03-20  5:27   ` N N Ashok
@ 2003-03-20  5:33     ` David S. Miller
  2003-03-20  6:02       ` N N Ashok
  0 siblings, 1 reply; 5+ messages in thread
From: David S. Miller @ 2003-03-20  5:33 UTC (permalink / raw)
  To: nalkunda; +Cc: netdev, linux-net

   From: N N Ashok <nalkunda@cse.msu.edu>
   Date: Thu, 20 Mar 2003 00:27:41 -0500

   I did see that. But however, I could not understand how "struct rtable" can be 
   casted to "struct dst_entry" and then back again, all the while accessing 
   fields of both structures.

You miss the point that they are the same structure.  It is allocated
the size of "struct rtable" but it may be casted back and forth
between rtable and dst_entry as desired.

void foo(void)
{
	struct rtable rt;
	struct dst_entry *dst;

	rt->u.dst.bar = 1;

	dst = (struct dst_entry *) &rt;
	ASSERT(dst->bar == 1);

	dst = &rt->u.dst;
	ASSERT(dst->bar == 1);
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Casting (struct rtable*) to (struct dst_entry*)
  2003-03-20  5:33     ` David S. Miller
@ 2003-03-20  6:02       ` N N Ashok
  0 siblings, 0 replies; 5+ messages in thread
From: N N Ashok @ 2003-03-20  6:02 UTC (permalink / raw)
  To: David S. Miller; +Cc: netdev, linux-net

On Thursday 20 March 2003 00:33, David S. Miller wrote:
>    From: N N Ashok <nalkunda@cse.msu.edu>
>    Date: Thu, 20 Mar 2003 00:27:41 -0500
>
>    I did see that. But however, I could not understand how "struct rtable"
> can be casted to "struct dst_entry" and then back again, all the while
> accessing fields of both structures.
>
> You miss the point that they are the same structure.  It is allocated
> the size of "struct rtable" but it may be casted back and forth
> between rtable and dst_entry as desired.
>
> void foo(void)
> {
> 	struct rtable rt;
> 	struct dst_entry *dst;
>
> 	rt->u.dst.bar = 1;
>
> 	dst = (struct dst_entry *) &rt;
> 	ASSERT(dst->bar == 1);
>
> 	dst = &rt->u.dst;
> 	ASSERT(dst->bar == 1);
> }

I think I finally understand the whole setup. Please correct if I'm wrong.
"struct rtable" has its first field "u" which is a union of "dst_entry" and 
"struct rtable *". Thus when we cast rtable to dst_entry, we are accessing 
the rtable.u.dst_entry itself and not any other part of rtable.
Since originally the data was allocated the size of "rtable", when we cast 
"dst_entry" to "struct rtable" we can access all the fields of "struct 
table".

Thanks a lot for the clarification.

Ashok

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2003-03-20  6:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-03-20  4:55 Casting (struct rtable*) to (struct dst_entry*) N N Ashok
2003-03-20  4:59 ` David S. Miller
2003-03-20  5:27   ` N N Ashok
2003-03-20  5:33     ` David S. Miller
2003-03-20  6:02       ` N N Ashok

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).