Each interface has an associated cost. It is computed as follows:
reference bandwidth / configured bandwidth of interface in kbps
On Cisco routers, the reference bandwidth defaults to 100000 kbps. So, a DS-3 interface, with a configured bandwidth of 45000 kbps, has a cost of:
100,000,000 / 45,000 = 2222
The interface bandwidth must be explicitly configured. If not, the router will probably get it wrong. It is configured using the following syntax:
int h1/0 bandwidth 45000
Different manufacturers use different reference bandwidths. It is therefore common practice for every router to have it's OSPF reference bandwidth set. Many sysadmins set it to 10000(k) using the following syntax:
router ospf 100 auto-cost reference-bandwidth 10000
This reference bandwidth should be the same on all of your routers.
Once this is done, our DS-3 interface will have a cost of:
10,000,000 / 45,000 = 222
You may want to configure the costs on some of your interfaces, for instance to make the interface costs of both ends of a link match, to make path cost computation simpler for humans, or to force OSPF to prefer certain paths. To do this, use the following syntax:
int h1/0 ip ospf cost 200
Now the OSPF cost of my DS-3 is 200.
Here's the easy part. To compute the OSPF cost of a route, start at the router doing the calculation and sum the costs of the interfaces used to exit each router that is traversed.
Note that the costs of the same path in the reverse direction will be different, if the exit interfaces have different costs than the interfaces used in the forward direction. This causes asymmetrical path costs, and may result in asymmetrical routes. (There is nothing wrong with asymmetrical routes, per se.)
An ASBR is an OSPF router that redistributes routes into OSPF that were learned from another protocol. When doing this redistribution, the ASBR often sets a metric.
router OSPF 100 redistribute static subnets route-map REDIST-MAP ! route-map REDIST-MAP permit 10 match ip address REDIST-LIST set metric 180 set metric-type type-1 ! ip access-list standard REDIST-LIST permit 220.127.116.11 0.255.255.255 permit 18.104.22.168 0.255.255.255 ! ip route 22.214.171.124 255.0.0.0 172.16.25.1 ip route 126.96.36.199 255.0.0.0 172.16.25.1
This code will tell OSPF that 81.x and 82.x are reachable via 172.16.25.1, with an OSPF metric of 180 and an OSPF metric type of E1 (external type 1). Some other router will calculate the cost to these networks by summing the exit interface costs to 172.16.25.1, and then adding 180.
(Had the metric-type been E2 (external type 2), the other router would not include the interface costs, and would consider the route to have a metric of 180.)