3 # Copyright (c) 2014, 2019, 2020 Juniper Networks, Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 # Convert a NIC driver to use the procdural API.
32 # It doesn't take care of all the # cases yet,
33 # but still does about 95% of work.
35 # Author: Sreekanth Rupavatharam
42 echo " $0 <driver source (e.g., if_em.c)>";
46 # XXX - This needs to change if the data structure uses different name
58 toggle=$(((toggle + 1) % 4))
59 printf " %s \b\b\b" $c
62 # Handle the case where $__ifp__->if_blah = XX;
64 if echo $line | grep "$__ifp__->.* = " > /dev/null 2>&1
66 if echo $line | grep "\[$__ifp__->.* = " > /dev/null 2>&1; then
67 # Special case of array[ifp->member] = value
70 word=`echo $line | awk -F "if_" ' { print $2 }' | awk -F" =" '{ print $1 }'`
71 value=`echo $line | awk -F "=" '{ print $2 }' | sed -e 's/;//g'`
72 new=`echo if_set$word"\($__ifp__,"$value");"`
73 new=`echo $new | sed -e 's/&/\\\&/'`
74 old=`echo $line|sed -e 's/^[ ]*//'`
75 line=`echo $line| sed -e's/'$old'/'$new'/g'`
82 if echo $line | grep "$__ifp__->.*++\|++$__ifp__->.*" > /dev/null 2>&1
84 word=`echo $line | awk -F"if_" '{ print $2 }'|awk -F"\+" '{ print $1}'`
86 old=`echo $line|sed -e 's/^[ ]*//'`
87 new=`echo if_inc$word"\($__ifp__,"$value");"`
88 new=`echo $new | sed -e 's/&/\\\&/'`
89 line=`echo $line| sed -e's/'$old'/'$new'/g'`
96 if echo $line | grep "$__ifp__->.*+= " > /dev/null 2>&1
98 word=`echo $line | awk -F"if_" '{ print $2 }'|awk '{ print $1}'`
99 value=`echo $line | awk -F"=" '{ print $2}' | sed -e 's/;//g'`
100 new=`echo if_inc$word"\($__ifp__,$value);"`
101 new=`echo $new | sed -e 's/&/\\\&/'`
102 old=`echo $line|sed -e 's/^[ ]*//'`
103 line=`echo $line| sed -e's/'$old'/'$new'/g'`
111 if echo $line | grep "$__ifp__->.*|= " > /dev/null 2>&1
113 word=`echo $line | awk -F"if_" '{ print $2 }'|awk '{ print $1}'`
114 value=`echo $line | awk -F"=" '{ print $2}' | sed -e 's/;//g'`
115 new=`echo if_set${word}bit"($__ifp__,$value, 0);"`
116 new=`echo $new | sed -e 's/&/\\\&/'`
117 #line=`echo $line|sed -e 's/&/\\&/'`
118 old=`echo $line|sed -e 's/^[ ]*//'`
119 line=`echo $line| sed -e's/'$old'/'$new'/g'`
127 if echo $line |grep "$__ifp__->.*&= " > /dev/null 2>&1
129 word=`echo $line | awk -F"if_" '{ print $2 }'|awk '{ print $1}'`
130 value=`echo $line | awk -F"=" '{ print $2}' | sed -e 's/;//g'`
131 value=`echo $value | sed -e's/~//g'`
132 new=`echo if_set${word}bit"\($__ifp__, 0,$value);"`
133 new=`echo $new | sed -e 's/&/\\\&/'`
134 old=`echo $line|sed -e 's/^[ ]*//'`
135 line=`echo $line| sed -e's/'$old'/'$new'/g'`
143 if echo $line | grep "\^=" > /dev/null 2>&1
145 line=`echo $line | sed -e 's/'"$__ifp__"'->if_\(.*\) ^=\(.*\);/if_toggle\1('"$__ifp__"',\2);/g'`
152 # XXX - this needs updating
154 if echo $line | grep "$__ifp__->\(if_capabilities\|if_flags\|if_softc\|if_capenable\|if_hwassist\|if_mtu\|if_drv_flags\|if_index\|if_alloctype\|if_dname\|if_xname\|if_addr\|if_hw_tsomax\|if_hw_tsomaxsegcount\|if_hw_tsomaxsegsize\)" > /dev/null 2>&1
156 word=`echo $line |awk -F"$__ifp__->if_" '{ print $2 }' | \
157 sed -e's/[^a-zA-Z0-9_]/\@/'|awk -F"\@" '{ print $1}'`
158 old=`echo "$__ifp__->if_"${word}`
159 new=`echo "if_get"${word}"($__ifp__)"`
160 new=`echo $new | sed -e 's/&/\\\&/'`
161 line=`echo $line| sed -e's/'$old'/'$new'/g'`
173 if echo $line | grep "$orig" > /dev/null 2>&1
175 line=`echo $line | sed -e "s|$orig|$new|"`
183 replace_str "(\*$__ifp__->if_input)" "if_input" || \
184 replace_str "IFQ_DRV_IS_EMPTY(&$__ifp__->if_snd)" \
185 "if_sendq_empty($__ifp__)" || \
186 replace_str "IFQ_DRV_PREPEND(&$__ifp__->if_snd" \
187 "if_sendq_prepend($__ifp__" || \
188 replace_str "IFQ_SET_READY(&$__ifp__->if_snd)" \
189 "if_setsendqready($__ifp__)" || \
190 replace_str "VLAN_CAPABILITIES($__ifp__)" \
191 "if_vlancap($__ifp__)" || \
192 replace_str "IFQ_SET_MAXLEN(&$__ifp__->if_snd," \
193 "if_setsendqlen($__ifp__," || \
194 replace_str "IFQ_DRV_DEQUEUE(&$__ifp__->if_snd, \(.*\))" \
195 "\1 = if_dequeue($__ifp__)"
196 replace_str "$__ifp__->if_vlantrunk != NULL" \
197 "if_vlantrunkinuse($__ifp__)"
201 handle_set || handle_inc || handle_add || handle_or || handle_and || \
202 handle_toggle || handle_misc || handle_special
207 replace_str "if_setinit(" "if_setinitfn(" || \
208 replace_str "if_setioctl(" "if_setioctlfn(" || \
209 replace_str "if_setqflush(" "if_setqflushfn(" || \
210 replace_str "if_settransmit(" "if_settransmitfn(" || \
211 replace_str "if_getdrv_flags(" "if_getdrvflags(" || \
212 replace_str "if_setdrv_flagsbit(" "if_setdrvflagbits(" || \
213 replace_str "if_setstart(" "if_setstartfn(" || \
214 replace_str "if_sethwassistbit(" "if_sethwassistbits(" || \
215 replace_str "ifmedia_init(" "ifmedia_init_drv("
221 *"${__ifp__}->"*) return 0;; # Still an ifp to convert
228 line="$line /* ${FAIL_PAT} */"
237 echo -n "Conversion for $file started, please wait: "
238 FAIL_PAT="XXX - IFAPI"
244 # There is an ifp, we need to process it
255 passes=$((passes + 1))
256 if [ $passes -ge $MAX_PASSES ]; then
262 # Replace the ifnet * with if_t
265 line=`echo $line | sed -e 's/struct ifnet[ \t]*\*/if_t /g'` ;;
267 line=`echo $line | sed -e 's/IF_LLADDR(/if_getlladdr(/g'` ;;
269 printf "%s\n" "$line" >> $file.tmp
272 count=`grep $FAIL_PAT $file.tmp | wc -l`
275 echo "$count lines could not be converted to IFAPI"
276 echo "Look for /* $FAIL_PAT */ in the converted file"
278 echo "original $file has been moved to $file.orig"